+// Copyright (C) 2006-2015 CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
//#define REFCNT
+//
#ifdef REFCNT
#define private public
#define protected public
#include <iostream>
#include <sstream>
+#ifdef WIN32
+#include <fcntl.h>
+#define _S_IREAD 256
+#define _S_IWRITE 128
+int mkstemp(char *tmpl)
+{
+ int ret=-1;
+ mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
+ return ret;
+}
+#endif
+
//#define _DEVDEBUG_
#include "YacsTrace.hxx"
{
namespace ENGINE
{
+ void printbin(const std::string& bin)
+ {
+ register char c;
+ for(int i=0;i<bin.length();i++)
+ {
+ c=bin[i];
+ if (c < ' ' || c >= 0x7f)
+ {
+ fprintf(stderr,"\\x%02x",c & 0xff);
+ }
+ else
+ fprintf(stderr,"%c",c);
+ }
+ fprintf(stderr,"\n");
+ }
+
std::string getImplName(ImplType impl)
{
switch(impl)
CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
{
- DEBTRACE( t->name() << " " << t->shortName());
- CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
+ DEBTRACE( t->name() << " " << t->shortName() << " " << t->id());
+ CORBA::TypeCode_ptr tc;
+ if(strncmp(t->id(),"python",6)==0 )
+ tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
+ else if(strncmp(t->id(),"json",4)==0)
+ tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
+ else
+ tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
#ifdef REFCNT
DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
#endif
template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
struct convertToYacsObjref
{
- static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
+ static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
{
stringstream msg;
msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
{
- std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
+ int protocol=-1;
+ if(IMPLOUT==XMLImpl || IMPLOUT==NEUTRALImpl)
+ protocol=0;
+ std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
DEBTRACE( d );
TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
return r;
else
{
stringstream msg;
- msg << "Not a python double. kind=" << t->kind() ;
+ msg << "Not a python double. ";
+#ifdef _DEVDEBUG_
+ msg << "kind=" << t->kind() ;
msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
+#endif
throw YACS::ENGINE::ConversionException(msg.str());
}
return x;
else
{
stringstream msg;
- msg << "Not a python integer. kind=" << t->kind() ;
+ msg << "Not a python integer. ";
+#ifdef _DEVDEBUG_
+ msg << "kind=" << t->kind() ;
msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
+#endif
throw YACS::ENGINE::ConversionException(msg.str());
}
return l;
{
static inline std::string convert(const TypeCode *t,PyObject* o,void*)
{
+ std::string s;
if (PyString_Check(o))
- return PyString_AS_STRING(o);
+ s= PyString_AS_STRING(o);
else
{
stringstream msg;
- msg << "Not a python string. kind=" << t->kind() ;
+ msg << "Not a python string. ";
+#ifdef _DEVDEBUG_
+ msg << "kind=" << t->kind() ;
msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
+#endif
throw YACS::ENGINE::ConversionException(msg.str());
}
+ return s;
}
};
template <ImplType IMPLOUT, class TOUT>
else
{
stringstream msg;
- msg << "Not a python boolean. kind=" << t->kind() ;
+ msg << "Not a python boolean. ";
+#ifdef _DEVDEBUG_
+ msg << "kind=" << t->kind() ;
msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
+#endif
throw YACS::ENGINE::ConversionException(msg.str());
}
return l;
template <ImplType IMPLOUT, class TOUT>
struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
{
- static inline std::string convert(const TypeCode *t,PyObject* o,void*)
+ static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
{
- if (PyString_Check(o))
+ if (PyString_Check(o) && strncmp(t->id(),"python",6)!=0)
{
// the objref is used by Python as a string (prefix:value) keep it as a string
return PyString_AS_STRING(o);
}
- PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"object_to_string","O",o);
- if(pystring==NULL)
+ if(strncmp(t->id(),"python",6)==0)
{
- PyErr_Print();
- throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
+ // It's a native Python object pickle it
+ PyObject* mod=PyImport_ImportModule("cPickle");
+ PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
+ DEBTRACE(PyObject_REPR(pickled) );
+ Py_DECREF(mod);
+ if(pickled==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
+ }
+ std::string mystr(PyString_AsString(pickled),PyString_Size(pickled));
+ Py_DECREF(pickled);
+ return mystr;
+ }
+ else if(strncmp(t->id(),"json",4)==0)
+ {
+ // It's a Python object convert it to json
+ PyObject* mod=PyImport_ImportModule("simplejson");
+ if(mod==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
+ }
+ PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
+ Py_DECREF(mod);
+ if(pickled==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
+ }
+ std::string mystr=PyString_AsString(pickled);
+ Py_DECREF(pickled);
+ return mystr;
+ }
+ else
+ {
+ // It should be a CORBA Object convert it to an IOR string
+ PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
+ if(pystring==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
+ }
+ std::string mystr=PyString_AsString(pystring);
+ Py_DECREF(pystring);
+ return mystr;
}
- std::string mystr=PyString_AsString(pystring);
- Py_DECREF(pystring);
- return mystr;
}
};
template <ImplType IMPLOUT, class TOUT>
{
stringstream msg;
msg << "Problem in conversion: the python object is not a sequence " << std::endl;
- msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+#ifdef _DEVDEBUG_
+ msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
+#endif
throw YACS::ENGINE::ConversionException(msg.str());
}
int length=PySequence_Size(o);
std::cerr << std::endl;
#endif
DEBTRACE( "item refcnt: " << item->ob_refcnt );
- TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
- v[i]=ro;
- Py_DECREF(item);
+ try
+ {
+ TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
+ v[i]=ro;
+ Py_DECREF(item);
+ }
+ catch(ConversionException& ex)
+ {
+ stringstream msg;
+ msg << ex.what() << " for sequence element " << i;
+ throw YACS::ENGINE::ConversionException(msg.str(),false);
+ }
}
}
};
std::cerr << std::endl;
#endif
stringstream msg;
- msg << "Problem in conversion: member " << name << " not present " << endl;
- msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+ msg << "member " << name << " not present " ;
throw YACS::ENGINE::ConversionException(msg.str());
}
DEBTRACE( "value refcnt: " << value->ob_refcnt );
- TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
- m[name]=ro;
+ try
+ {
+ TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
+ m[name]=ro;
+ }
+ catch(ConversionException& ex)
+ {
+ std::string s=" for struct member "+name;
+ throw YACS::ENGINE::ConversionException(ex.what()+s,false);
+ }
}
}
};
{
static inline PyObject* convert(const TypeCode *t,std::string& o)
{
+ if(o=="")
+ {
+ Py_INCREF(Py_None);
+ return Py_None;
+ }
if(t->isA(Runtime::_tc_file))
{
//It's an objref file. Convert it specially
return PyString_FromString(o.c_str());
}
- /* another way
+ if(strncmp(t->id(),"python",6)==0)
+ {
+ //It's a python pickled object, unpickled it
+ PyObject* mod=PyImport_ImportModule("cPickle");
+ PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",o.c_str(),o.length());
+ DEBTRACE(PyObject_REPR(ob));
+ Py_DECREF(mod);
+ if(ob==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
+ }
+ return ob;
+ }
+ if(strncmp(t->id(),"json",4)==0)
+ {
+ // It's a json object unpack it
+ PyObject* mod=PyImport_ImportModule("simplejson");
+ if(mod==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
+ }
+ PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s",o.c_str());
+ Py_DECREF(mod);
+ if(ob==NULL)
+ {
+ PyErr_Print();
+ throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
+ }
+ return ob;
+ }
+
+ /* another way to convert IOR string to CORBA PyObject
PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
return ob;
throw ConversionException("Can't get reference to object");
}
+ if(obref->_non_existent())
+ {
+ throw ConversionException("non_existent object");
+ }
+
if( CORBA::is_nil(obref) )
{
DEBTRACE( "Can't get reference to object (or it was nil)." );
//ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
{
- PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), "narrow", "Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
+ PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
if(result==NULL)
PyErr_Clear();//Exception during narrow. Keep ob
else if(result==Py_None)
template <ImplType IMPLOUT, class TOUT>
struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
{
- static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
+ static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
{
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
static inline std::string convert(const TypeCode *t,std::string& o)
{
- return "<value><objref>" + o + "</objref></value>\n";
+ if(strncmp(t->id(),"python",6)==0 )
+ return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
+ else if(strncmp(t->id(),"json",4)==0)
+ return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
+ else
+ return "<value><objref>" + o + "</objref></value>\n";
}
};
template <ImplType IMPLOUT, class TOUT>
struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
{
- static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
+ static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
{
if(o->getType()->kind()==String)
return o->getStringValue();
}
}
};
+ template <ImplType IMPLOUT, class TOUT>
+ struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
+ {
+ static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
+ {
+ StructAny * sdata = dynamic_cast<StructAny *>(o);
+ YASSERT(sdata != NULL);
+ const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
+ YASSERT(tst != NULL);
+ for (int i=0 ; i<tst->memberCount() ; i++)
+ {
+ string name = tst->memberName(i);
+ TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
+ m[name]=ro;
+ }
+ }
+ };
/* End of ToYacs Convertor for NEUTRALImpl */
//! FromYacs Convertor for NEUTRALImpl
{
static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
{
+ //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
+ if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
+ return YACS::ENGINE::AtomAny::New(o);
+
+ //Objref CORBA. prefix=IOR,corbaname,corbaloc
+ CORBA::Object_var obref;
+ try
+ {
+ obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
+ }
+ catch(CORBA::Exception& ex)
+ {
+ throw ConversionException("Can't get reference to object");
+ }
+ if(obref->_non_existent())
+ throw ConversionException("non_existent object");
+ if( CORBA::is_nil(obref) )
+ throw ConversionException("Can't get reference to object");
+ if(!obref->_is_a(t->id()))
+ {
+ stringstream msg;
+ msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
+ msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
+ msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+
+ SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
+ if(!CORBA::is_nil(gobj))
+ {
+ DEBTRACE("It's a SALOME::GenericObj register it");
+ gobj->Register();
+ }
+ else
+ DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
+
return YACS::ENGINE::AtomAny::New(o);
}
};
return any;
}
};
+
+ template <>
+ struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
+ {
+ static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
+ {
+ StructAny * any = StructAny::New((TypeCodeStruct *)t);
+ std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
+ for (it=m.begin() ; it!=m.end() ; it++)
+ {
+ any->setEltAtRank(it->first.c_str(), it->second);
+ it->second->decrRef();
+ }
+ return any;
+ }
+ };
/* End of FromYacs Convertor for NEUTRALImpl */
//! ToYacs Convertor for CORBAImpl
template <ImplType IMPLOUT, class TOUT>
struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
{
- static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
+ static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
{
char file[]="/tmp/XXXXXX";
if(t->isA(Runtime::_tc_file))
delete f;
return file;
}
+ else if(strncmp(t->id(),"python",6)==0)
+ {
+ const char *s;
+ Engines::fileBlock * buffer;
+ if(*o >>=buffer)
+ {
+ s=(const char*)buffer->get_buffer();
+
+ if(protocol !=0)
+ {
+ std::string mystr(s,buffer->length());
+ return mystr;
+ }
+
+ PyGILState_STATE gstate = PyGILState_Ensure();
+ PyObject* mod=PyImport_ImportModule("cPickle");
+ PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",s,buffer->length());
+ PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
+ DEBTRACE(PyObject_REPR(pickled));
+ std::string mystr=PyString_AsString(pickled);
+ Py_DECREF(mod);
+ Py_DECREF(ob);
+ Py_DECREF(pickled);
+ PyGILState_Release(gstate);
+
+ return mystr;
+ }
+ stringstream msg;
+ msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ else if(strncmp(t->id(),"json",4)==0)
+ {
+ const char *s;
+ if(*o >>=s)
+ {
+ return s;
+ }
+ stringstream msg;
+ msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
else
{
CORBA::Object_var ObjRef ;
static inline CORBA::Any* convert(const TypeCode *t,double o)
{
CORBA::Any *any = new CORBA::Any();
- *any <<= o;
+ *any <<= (CORBA::Double)o;
return any;
}
};
static inline CORBA::Any* convert(const TypeCode *t,long o)
{
CORBA::Any *any = new CORBA::Any();
- *any <<= o;
+ *any <<= (CORBA::Long)o;
return any;
}
};
static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
{
CORBA::Object_var obref;
+
if(t->isA(Runtime::_tc_file))
{
//It's an objref file. Convert it specially
throw YACS::ENGINE::ConversionException(msg.str());
}
}
+ else if(strncmp(t->id(),"python",6)==0 )
+ {
+ CORBA::Any *any = new CORBA::Any();
+ Engines::fileBlock * buffer=new Engines::fileBlock();
+ buffer->length(o.length());
+ CORBA::Octet *buf=buffer->get_buffer();
+ memcpy(buf,o.c_str(),o.length());
+ *any <<= buffer;
+ return any;
+ }
+ else if(strncmp(t->id(),"json",4)==0)
+ {
+ CORBA::Any *any = new CORBA::Any();
+ *any <<= o.c_str();
+ return any;
+ }
else
{
try
{
DynamicAny::DynAny_var temp=ds->current_component();
CORBA::Any* a=*iter;
+ //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
if(isObjref)
{
CORBA::Object_var zzobj ;
int nMember=tst->memberCount();
DEBTRACE("nMember="<<nMember);
- CORBA::StructMemberSeq mseq;
- mseq.length(nMember);
- for(int i=0;i<nMember;i++)
- {
- const char * name=tst->memberName(i);
- if(m.count(name) !=0)
- {
- mseq[i].type=m[name]->type();
- }
- }
- CORBA::TypeCode_var tc= orb->create_struct_tc("","",mseq);
+ CORBA::TypeCode_var tc=getCorbaTC(t);
DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
DEBTRACE("Member name="<<name);
//do not test member presence : test has been done in ToYacs convertor
CORBA::Any* a=m[name];
- temp->from_any(*a);
+ //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
+ CORBA::TypeCode_var atc = tc->member_type(i);
+ if(atc->kind()==CORBA::tk_objref)
+ {
+ //special treatment for objref
+ CORBA::Object_var zzobj ;
+ *a >>= CORBA::Any::to_object(zzobj) ;
+ temp->insert_reference(zzobj);
+ }
+ else
+ {
+ temp->from_any(*a);
+ }
//delete intermediate any
delete a;
ds->next();
{
return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
}
+ PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
+ {
+ return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
+ }
+
+ std::string convertPyObjectToString(PyObject* ob)
+ {
+ PyObject *s;
+ PyGILState_STATE gstate = PyGILState_Ensure();
+ s=PyObject_Str(ob);
+ std::string ss(PyString_AsString(s),PyString_Size(s));
+ Py_DECREF(s);
+ PyGILState_Release(gstate);
+ return ss;
+ }
//XML conversions
PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
{
return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
}
+ PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
+ {
+ xmlDocPtr doc;
+ xmlNodePtr cur;
+ PyObject *ob=NULL;
+ doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
+ if (doc == NULL )
+ {
+ std::stringstream msg;
+ msg << "Problem in conversion: XML Document not parsed successfully ";
+ msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ cur = xmlDocGetRootElement(doc);
+ if (cur == NULL)
+ {
+ xmlFreeDoc(doc);
+ std::stringstream msg;
+ msg << "Problem in conversion: empty XML Document";
+ msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ while (cur != NULL)
+ {
+ if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
+ {
+ ob=convertXmlPyObject(t,doc,cur);
+ break;
+ }
+ cur = cur->next;
+ }
+ xmlFreeDoc(doc);
+ if(ob==NULL)
+ {
+ std::stringstream msg;
+ msg << "Problem in conversion: incorrect XML value";
+ msg << " (" << __FILE__ << ":" << __LINE__ << ")";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ return ob;
+ }
//NEUTRAL conversions
PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
{
{
return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
}
+
+ //! Basic template checker from type TIN
+ /*!
+ * This checker does nothing : throws exception
+ * It must be partially specialize for a specific type (TIN)
+ */
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ stringstream msg;
+ msg << "Check not implemented for Implementation: " << IMPLIN ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+
+ template <ImplType IMPLIN,class TIN,class TIN2>
+ inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
+ {
+ int tk=t->kind();
+ switch(t->kind())
+ {
+ case Double:
+ return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Int:
+ return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
+ case String:
+ return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Bool:
+ return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Objref:
+ return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Sequence:
+ return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Array:
+ return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
+ case Struct:
+ return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
+ default:
+ break;
+ }
+ stringstream msg;
+ msg << "Check not implemented for kind= " << tk ;
+ msg << " : " << __FILE__ << ":" << __LINE__;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ template<>
+ inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if (PyFloat_Check(o))
+ return true;
+ else if (PyInt_Check(o))
+ return true;
+ else if(PyLong_Check(o))
+ return true;
+ else
+ {
+ stringstream msg;
+ msg << "Not a python double ";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ }
+ template<>
+ inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if (PyInt_Check(o) || PyLong_Check(o))
+ return true;
+ else
+ {
+ stringstream msg;
+ msg << "Not a python integer ";
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ }
+ template<>
+ inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if (PyBool_Check(o))
+ return true;
+ else if (PyInt_Check(o))
+ return true;
+ else if(PyLong_Check(o))
+ return true;
+ else
+ {
+ stringstream msg;
+ msg << "Not a python boolean " ;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+
+ }
+ template<>
+ inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if (PyString_Check(o))
+ return true;
+ else
+ {
+ stringstream msg;
+ msg << "Not a python string " ;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ }
+ template<>
+ inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if (PyString_Check(o))
+ return true;
+ if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
+ return true;
+ else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
+ {
+ // The python object should be json compliant (to improve)
+ return true;
+ }
+ else
+ {
+ // The python object should be a CORBA obj (to improve)
+ return true;
+ }
+ }
+ template<>
+ inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ if(!PySequence_Check(o))
+ {
+ stringstream msg;
+ msg << "python object is not a sequence " ;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ int length=PySequence_Size(o);
+ for(int i=0;i<length;i++)
+ {
+ PyObject *item=PySequence_ITEM(o,i);
+ try
+ {
+ YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
+ }
+ catch(ConversionException& ex)
+ {
+ stringstream msg;
+ msg << ex.what() << " for sequence element " << i;
+ throw YACS::ENGINE::ConversionException(msg.str(),false);
+ }
+ Py_DECREF(item);
+ }
+ return true;
+ }
+ template<>
+ inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
+ {
+ PyObject *value;
+ if(!PyDict_Check(o))
+ {
+ stringstream msg;
+ msg << "python object is not a dict " ;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
+ int nMember=tst->memberCount();
+ for(int i=0;i<nMember;i++)
+ {
+ std::string name=tst->memberName(i);
+ TypeCode* tm=tst->memberType(i);
+ value=PyDict_GetItemString(o, name.c_str());
+ if(value==NULL)
+ {
+ stringstream msg;
+ msg << "member " << name << " not present " ;
+ throw YACS::ENGINE::ConversionException(msg.str());
+ }
+ try
+ {
+ YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
+ }
+ catch(ConversionException& ex)
+ {
+ std::string s=" for struct member "+name;
+ throw YACS::ENGINE::ConversionException(ex.what()+s,false);
+ }
+ }
+ return true;
+ }
+
+ bool checkPyObject(const TypeCode *t,PyObject* ob)
+ {
+ return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);
+ }
}
}