1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
29 #include "TypeConversions.hxx"
30 #include "ConversionException.hxx"
31 #include "RuntimeSALOME.hxx"
32 #include "Salome_file_i.hxx"
33 #include "TypeCode.hxx"
35 #include "SALOME_GenericObj.hh"
44 int mkstemp(char *tmpl)
47 mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
53 #include "YacsTrace.hxx"
61 void printbin(const std::string& bin)
64 for(int i=0;i<bin.length();i++)
67 if (c < ' ' || c >= 0x7f)
69 fprintf(stderr,"\\x%02x",c & 0xff);
72 fprintf(stderr,"%c",c);
77 std::string getImplName(ImplType impl)
96 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
99 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
101 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
104 msg << "Conversion not implemented: kind= " << t->kind();
105 msg << " : " << __FILE__ << ":" << __LINE__;
106 throw YACS::ENGINE::ConversionException(msg.str());
109 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
111 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
114 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
116 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
119 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
121 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
124 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
126 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
129 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
131 DEBTRACE( t->name() << " " << t->shortName() << " " << t->id());
132 CORBA::TypeCode_ptr tc;
133 if(strncmp(t->id(),"python",6)==0 )
134 tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
135 else if(strncmp(t->id(),"json",4)==0)
136 tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
138 tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
140 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
145 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
147 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
148 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
150 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
151 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
156 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
158 CORBA::StructMemberSeq mseq;
159 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
160 int nMember=tst->memberCount();
161 mseq.length(nMember);
162 for(int i=0;i<nMember;i++)
164 const char * name=tst->memberName(i);
165 TypeCode* tm=tst->memberType(i);
166 mseq[i].name=CORBA::string_dup(name);
167 mseq[i].type=getCorbaTC(tm);
169 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
171 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
176 getCorbaTCFn getCorbaTCFns[]=
189 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
192 return getCorbaTCFns[tk](t);
196 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
200 * Section that defines functions to check adaptation from one implementation to another
201 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
202 * can be converted to TypeCode t2 from implementation IMPLOUT
203 * IMPLIN is the implementation of an output port
204 * IMPLOUT is the implementation of an input port
205 * If the check is True, the input port can be adapted to the output port
208 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
210 template <ImplType IMPLIN,ImplType IMPLOUT>
211 struct isAdaptableDouble
213 static inline int apply(const TypeCode *t1,const TypeCode* t2)
215 if(t1->kind() == Double)return 1;
216 if(t1->kind() == Int)return 1;
220 template <ImplType IMPLIN,ImplType IMPLOUT>
221 struct isAdaptableInt
223 static inline int apply(const TypeCode *t1,const TypeCode* t2)
225 if(t1->kind() == Int)return 1;
229 template <ImplType IMPLIN,ImplType IMPLOUT>
230 struct isAdaptableString
232 static inline int apply(const TypeCode *t1,const TypeCode* t2)
234 if(t1->kind() == String)return 1;
238 template <ImplType IMPLIN,ImplType IMPLOUT>
239 struct isAdaptableBool
241 static inline int apply(const TypeCode *t1,const TypeCode* t2)
243 if(t1->kind() == Bool)return 1;
244 if(t1->kind() == Int)return 1;
248 template <ImplType IMPLIN,ImplType IMPLOUT>
249 struct isAdaptableObjref
251 static inline int apply(const TypeCode *t1,const TypeCode* t2)
253 if(t1->kind() == Objref)
255 //The inport type must be more general than outport type
256 if( t1->isA(t2->id()) )
262 template <ImplType IMPLIN,ImplType IMPLOUT>
263 struct isAdaptableSequence
265 static inline int apply(const TypeCode *t1,const TypeCode* t2)
267 if(t1->kind() == Sequence)
269 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
277 template <ImplType IMPLIN,ImplType IMPLOUT>
278 struct isAdaptableArray
280 static inline int apply(const TypeCode *t1,const TypeCode* t2)
285 template <ImplType IMPLIN,ImplType IMPLOUT>
286 struct isAdaptableStruct
288 static inline int apply(const TypeCode *t1,const TypeCode* t2)
290 if(t1->kind() == Struct)
300 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
301 * t1 is the IMPLIN output port type
302 * t2 is the IMPLOUT input port type
304 template <ImplType IMPLIN,ImplType IMPLOUT>
305 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
310 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
312 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
314 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
316 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
318 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
320 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
322 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
324 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
331 //xxx to Python adaptations
332 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
334 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
336 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
338 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
340 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
342 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
345 //xxx to Neutral adaptations
346 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
348 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
350 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
352 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
354 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
356 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
358 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
360 return isAdaptableNeutralCorba(t1, t2);
363 //xxx to XML adaptations
364 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
366 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
369 //xxx to Corba adaptations
370 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
372 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
374 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
376 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
378 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
380 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
382 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
384 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
387 //! Basic template convertor from type TIN to Yacs<TOUT> type
389 * This convertor does nothing : throws exception
390 * It must be partially specialize for a specific type (TIN)
392 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
393 struct convertToYacsDouble
395 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
398 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
399 msg << " : " << __FILE__ << ":" << __LINE__;
400 throw YACS::ENGINE::ConversionException(msg.str());
403 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
404 struct convertToYacsInt
406 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
409 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
410 msg << " : " << __FILE__ << ":" << __LINE__;
411 throw YACS::ENGINE::ConversionException(msg.str());
414 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
415 struct convertToYacsString
417 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
420 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
421 msg << " : " << __FILE__ << ":" << __LINE__;
422 throw YACS::ENGINE::ConversionException(msg.str());
425 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
426 struct convertToYacsBool
428 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
431 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
432 msg << " : " << __FILE__ << ":" << __LINE__;
433 throw YACS::ENGINE::ConversionException(msg.str());
436 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
437 struct convertToYacsObjref
439 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
442 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
443 msg << " : " << __FILE__ << ":" << __LINE__;
444 throw YACS::ENGINE::ConversionException(msg.str());
447 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
448 struct convertToYacsSequence
450 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
453 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
454 msg << " : " << __FILE__ << ":" << __LINE__;
455 throw YACS::ENGINE::ConversionException(msg.str());
458 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
459 struct convertToYacsArray
461 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
464 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
465 msg << " : " << __FILE__ << ":" << __LINE__;
466 throw YACS::ENGINE::ConversionException(msg.str());
469 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
470 struct convertToYacsStruct
472 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
475 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
476 msg << " : " << __FILE__ << ":" << __LINE__;
477 throw YACS::ENGINE::ConversionException(msg.str());
481 //! Basic convertor from Yacs<TOUT> type to full TOUT type
485 template <ImplType IMPLOUT, class TOUT>
486 struct convertFromYacsDouble
488 static inline TOUT convert(const TypeCode *t,double o)
491 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
492 msg << " : " << __FILE__ << ":" << __LINE__;
493 throw YACS::ENGINE::ConversionException(msg.str());
496 template <ImplType IMPLOUT, class TOUT>
497 struct convertFromYacsInt
499 static inline TOUT convert(const TypeCode *t,long o)
502 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
503 msg << " : " << __FILE__ << ":" << __LINE__;
504 throw YACS::ENGINE::ConversionException(msg.str());
507 template <ImplType IMPLOUT, class TOUT>
508 struct convertFromYacsString
510 static inline TOUT convert(const TypeCode *t,std::string o)
513 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
514 msg << " : " << __FILE__ << ":" << __LINE__;
515 throw YACS::ENGINE::ConversionException(msg.str());
518 template <ImplType IMPLOUT, class TOUT>
519 struct convertFromYacsBool
521 static inline TOUT convert(const TypeCode *t,bool o)
524 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
525 msg << " : " << __FILE__ << ":" << __LINE__;
526 throw YACS::ENGINE::ConversionException(msg.str());
529 template <ImplType IMPLOUT, class TOUT>
530 struct convertFromYacsObjref
532 static inline TOUT convert(const TypeCode *t,std::string o)
535 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
536 msg << " : " << __FILE__ << ":" << __LINE__;
537 throw YACS::ENGINE::ConversionException(msg.str());
540 template <ImplType IMPLOUT, class TOUT>
541 struct convertFromYacsSequence
543 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
546 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
547 msg << " : " << __FILE__ << ":" << __LINE__;
548 throw YACS::ENGINE::ConversionException(msg.str());
551 template <ImplType IMPLOUT, class TOUT>
552 struct convertFromYacsArray
554 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
557 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
558 msg << " : " << __FILE__ << ":" << __LINE__;
559 throw YACS::ENGINE::ConversionException(msg.str());
562 template <ImplType IMPLOUT, class TOUT>
563 struct convertFromYacsStruct
565 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
568 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
569 msg << " : " << __FILE__ << ":" << __LINE__;
570 throw YACS::ENGINE::ConversionException(msg.str());
573 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
574 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
576 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
578 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
581 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
582 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
584 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
586 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
589 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
590 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
592 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
594 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
597 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
598 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
600 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
602 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
605 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
606 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
609 if(IMPLOUT==XMLImpl || IMPLOUT==NEUTRALImpl)
611 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
613 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
617 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
618 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
621 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
622 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
625 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
626 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
629 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
630 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
633 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
634 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
636 std::map<std::string,TOUT> v;
637 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
638 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
642 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
643 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
649 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
651 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
653 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
655 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
657 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
659 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
661 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
663 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
668 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
669 msg << " : " << __FILE__ << ":" << __LINE__;
670 throw YACS::ENGINE::ConversionException(msg.str());
673 //! ToYacs Convertor for PYTHONImpl
675 * This convertor converts Python object to YACS<TOUT> types
676 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
678 template <ImplType IMPLOUT, class TOUT>
679 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
681 static inline double convert(const TypeCode *t,PyObject* o,void*)
684 if (PyFloat_Check(o))
685 x=PyFloat_AS_DOUBLE(o);
686 else if (PyInt_Check(o))
688 else if(PyLong_Check(o))
693 msg << "Not a python double. ";
695 msg << "kind=" << t->kind() ;
696 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
698 throw YACS::ENGINE::ConversionException(msg.str());
703 template <ImplType IMPLOUT, class TOUT>
704 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
706 static inline long convert(const TypeCode *t,PyObject* o,void*)
711 else if(PyLong_Check(o))
716 msg << "Not a python integer. ";
718 msg << "kind=" << t->kind() ;
719 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
721 throw YACS::ENGINE::ConversionException(msg.str());
726 template <ImplType IMPLOUT, class TOUT>
727 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
729 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
732 if (PyString_Check(o))
733 s= PyString_AS_STRING(o);
737 msg << "Not a python string. ";
739 msg << "kind=" << t->kind() ;
740 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
742 throw YACS::ENGINE::ConversionException(msg.str());
747 template <ImplType IMPLOUT, class TOUT>
748 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
750 static inline bool convert(const TypeCode *t,PyObject* o,void*)
755 else if (PyInt_Check(o))
756 l=(PyInt_AS_LONG(o)!=0);
757 else if(PyLong_Check(o))
758 l=(PyLong_AsLong(o)!=0);
762 msg << "Not a python boolean. ";
764 msg << "kind=" << t->kind() ;
765 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
767 throw YACS::ENGINE::ConversionException(msg.str());
772 template <ImplType IMPLOUT, class TOUT>
773 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
775 static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
777 if (PyString_Check(o))
779 // the objref is used by Python as a string (prefix:value) keep it as a string
780 return PyString_AS_STRING(o);
782 if(strncmp(t->id(),"python",6)==0)
784 // It's a native Python object pickle it
785 PyObject* mod=PyImport_ImportModule("cPickle");
786 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
787 DEBTRACE(PyObject_REPR(pickled) );
792 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
794 std::string mystr(PyString_AsString(pickled),PyString_Size(pickled));
798 else if(strncmp(t->id(),"json",4)==0)
800 // It's a Python object convert it to json
801 PyObject* mod=PyImport_ImportModule("simplejson");
805 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
807 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
812 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
814 std::string mystr=PyString_AsString(pickled);
820 // It should be a CORBA Object convert it to an IOR string
821 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
825 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
827 std::string mystr=PyString_AsString(pystring);
833 template <ImplType IMPLOUT, class TOUT>
834 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
836 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
838 if(!PySequence_Check(o))
841 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
843 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
845 throw YACS::ENGINE::ConversionException(msg.str());
847 int length=PySequence_Size(o);
848 DEBTRACE("length: " << length );
850 for(int i=0;i<length;i++)
852 PyObject *item=PySequence_ITEM(o,i);
854 std::cerr <<"item[" << i << "]=";
855 PyObject_Print(item,stderr,Py_PRINT_RAW);
856 std::cerr << std::endl;
858 DEBTRACE( "item refcnt: " << item->ob_refcnt );
861 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
865 catch(ConversionException& ex)
868 msg << ex.what() << " for sequence element " << i;
869 throw YACS::ENGINE::ConversionException(msg.str(),false);
874 template <ImplType IMPLOUT, class TOUT>
875 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
877 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
879 DEBTRACE( "o refcnt: " << o->ob_refcnt );
880 PyObject *key, *value;
881 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
882 int nMember=tst->memberCount();
883 DEBTRACE("nMember="<<nMember);
884 for(int i=0;i<nMember;i++)
886 std::string name=tst->memberName(i);
887 DEBTRACE("Member name="<<name);
888 TypeCode* tm=tst->memberType(i);
889 value=PyDict_GetItemString(o, name.c_str());
892 //member name not present
893 //TODO delete all allocated objects in m
895 PyObject_Print(o,stderr,Py_PRINT_RAW);
896 std::cerr << std::endl;
899 msg << "member " << name << " not present " ;
900 throw YACS::ENGINE::ConversionException(msg.str());
902 DEBTRACE( "value refcnt: " << value->ob_refcnt );
905 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
908 catch(ConversionException& ex)
910 std::string s=" for struct member "+name;
911 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
916 /* End of ToYacs Convertor for PYTHONImpl */
918 //! FromYacs Convertor for PYTHONImpl
920 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
923 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
925 static inline PyObject* convert(const TypeCode *t,double o)
927 PyObject *pyob=PyFloat_FromDouble(o);
932 struct convertFromYacsInt<PYTHONImpl,PyObject*>
934 static inline PyObject* convert(const TypeCode *t,long o)
936 PyObject *pyob=PyLong_FromLong(o);
941 struct convertFromYacsString<PYTHONImpl,PyObject*>
943 static inline PyObject* convert(const TypeCode *t,std::string& o)
945 return PyString_FromString(o.c_str());
949 struct convertFromYacsBool<PYTHONImpl,PyObject*>
951 static inline PyObject* convert(const TypeCode *t,bool o)
953 return PyBool_FromLong ((long)o);
957 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
959 static inline PyObject* convert(const TypeCode *t,std::string& o)
966 if(t->isA(Runtime::_tc_file))
968 //It's an objref file. Convert it specially
969 return PyString_FromString(o.c_str());
971 if(strncmp(t->id(),"python",6)==0)
973 //It's a python pickled object, unpickled it
974 PyObject* mod=PyImport_ImportModule("cPickle");
975 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",o.c_str(),o.length());
976 DEBTRACE(PyObject_REPR(ob));
981 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
985 if(strncmp(t->id(),"json",4)==0)
987 // It's a json object unpack it
988 PyObject* mod=PyImport_ImportModule("simplejson");
992 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
994 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s",o.c_str());
999 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1004 /* another way to convert IOR string to CORBA PyObject
1005 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
1006 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
1010 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1011 CORBA::Object_var obref;
1014 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1016 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1019 catch(CORBA::Exception& ex)
1021 DEBTRACE( "Can't get reference to object." );
1022 throw ConversionException("Can't get reference to object");
1025 if(obref->_non_existent())
1027 throw ConversionException("non_existent object");
1030 if( CORBA::is_nil(obref) )
1032 DEBTRACE( "Can't get reference to object (or it was nil)." );
1033 throw ConversionException("Can't get reference to object");
1036 if(!obref->_is_a(t->id()))
1039 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1040 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1041 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1042 throw YACS::ENGINE::ConversionException(msg.str());
1045 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1048 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1049 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
1052 //hold_lock is true: caller is supposed to hold the GIL.
1053 //omniorb will not take the GIL
1054 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1057 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1058 std::cerr << std::endl;
1059 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1060 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1061 std::cerr << std::endl;
1064 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1065 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1067 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1069 PyErr_Clear();//Exception during narrow. Keep ob
1070 else if(result==Py_None)
1071 Py_DECREF(result); //Can't narrow. Keep ob
1074 //Can narrow. Keep result
1076 PyObject_Print(result,stderr,Py_PRINT_RAW);
1077 std::cerr << std::endl;
1085 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1092 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1094 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1096 std::vector<PyObject*>::const_iterator iter;
1097 PyObject *pyob = PyList_New(v.size());
1099 for(iter=v.begin();iter!=v.end();iter++)
1101 PyObject* item=*iter;
1102 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1103 PyList_SetItem(pyob,i,item);
1104 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1111 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1113 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1115 PyObject *pyob = PyDict_New();
1116 std::map<std::string, PyObject*>::const_iterator pt;
1117 for(pt=m.begin();pt!=m.end();pt++)
1119 std::string name=(*pt).first;
1120 PyObject* item=(*pt).second;
1121 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1122 PyDict_SetItemString(pyob,name.c_str(),item);
1124 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1126 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1130 /* End of FromYacs Convertor for PYTHONImpl */
1132 //! ToYacs Convertor for XMLImpl
1134 * Partial specialization for XML implementation (XMLImpl)
1135 * This convertor converts xml object to YACS<TOUT> types
1137 template <ImplType IMPLOUT, class TOUT>
1138 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1140 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1143 cur = cur->xmlChildrenNode;
1146 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1148 //wait a double, got a double
1150 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1153 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1154 d=Cstr2d((const char *)s);
1159 DEBTRACE("############### workaround to improve...");
1163 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1165 //wait a double, got an int
1167 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1170 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1171 d=Cstr2d((const char *)s);
1176 DEBTRACE("############### workaround to improve...");
1183 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1184 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1185 throw YACS::ENGINE::ConversionException(msg.str());
1188 template <ImplType IMPLOUT, class TOUT>
1189 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1191 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1194 cur = cur->xmlChildrenNode;
1197 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1200 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1203 DEBTRACE( "convertToYacsInt " << (const char *)s );
1204 d=atol((const char *)s);
1209 DEBTRACE("############### workaround to improve...");
1216 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1217 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1218 throw YACS::ENGINE::ConversionException(msg.str());
1221 template <ImplType IMPLOUT, class TOUT>
1222 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1224 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1226 cur = cur->xmlChildrenNode;
1229 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1231 //wait a string, got a string
1233 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1235 DEBTRACE( "convertToYacsString " << (const char *)s );
1236 std::string mystr=std::string((const char *)s);
1243 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1244 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1245 throw YACS::ENGINE::ConversionException(msg.str());
1248 template <ImplType IMPLOUT, class TOUT>
1249 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1251 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1253 cur = cur->xmlChildrenNode;
1256 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1258 //wait a boolean, got a boolean
1260 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1264 DEBTRACE( "convertToYacsBool " << (const char *)s );
1265 ob=atoi((const char*)s)!=0;
1270 DEBTRACE("############### workaround to improve...");
1277 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1278 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1279 throw YACS::ENGINE::ConversionException(msg.str());
1282 template <ImplType IMPLOUT, class TOUT>
1283 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1285 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1287 cur = cur->xmlChildrenNode;
1290 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1292 //we wait a objref, we have got a objref
1294 std::string mystr = "";
1295 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1298 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1299 mystr = (const char *)s;
1304 DEBTRACE("############### workaround to improve...");
1311 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1312 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1313 throw YACS::ENGINE::ConversionException(msg.str());
1316 template <ImplType IMPLOUT, class TOUT>
1317 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1319 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1321 cur = cur->xmlChildrenNode;
1324 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1326 DEBTRACE( "parse sequence " );
1327 xmlNodePtr cur1=cur->xmlChildrenNode;
1328 while (cur1 != NULL)
1330 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1332 DEBTRACE( "parse data " );
1333 xmlNodePtr cur2=cur1->xmlChildrenNode;
1334 while (cur2 != NULL)
1336 //collect all values
1337 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1339 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1343 } // end while value
1351 } // end while array
1354 template <ImplType IMPLOUT, class TOUT>
1355 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1357 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1359 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1360 int nMember=tst->memberCount();
1361 DEBTRACE("nMember="<<nMember);
1362 std::map<std::string,TypeCode*> mtc;
1363 for(int i=0;i<nMember;i++)
1365 mtc[tst->memberName(i)]=tst->memberType(i);
1368 cur = cur->xmlChildrenNode;
1371 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1373 DEBTRACE( "parse struct " );
1374 xmlNodePtr cur1=cur->xmlChildrenNode;
1375 while (cur1 != NULL)
1377 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1379 DEBTRACE( "parse member " );
1380 xmlNodePtr cur2=cur1->xmlChildrenNode;
1381 while (cur2 != NULL)
1384 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1387 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1388 std::string name= (char *)s;
1390 while (cur2 != NULL)
1392 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1394 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1404 } // end while member/value
1407 } // end while member
1411 } // end while struct
1414 /* End of ToYacs Convertor for XMLImpl */
1416 //! FromYacs Convertor for XMLImpl
1418 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1421 struct convertFromYacsDouble<XMLImpl,std::string>
1423 static inline std::string convert(const TypeCode *t,double o)
1426 msg << "<value><double>" << o << "</double></value>\n";
1431 struct convertFromYacsInt<XMLImpl,std::string>
1433 static inline std::string convert(const TypeCode *t,long o)
1436 msg << "<value><int>" << o << "</int></value>\n";
1441 struct convertFromYacsString<XMLImpl,std::string>
1443 static inline std::string convert(const TypeCode *t,std::string& o)
1445 std::string msg="<value><string>";
1446 return msg+o+"</string></value>\n";
1450 struct convertFromYacsBool<XMLImpl,std::string>
1452 static inline std::string convert(const TypeCode *t,bool o)
1455 msg << "<value><boolean>" << o << "</boolean></value>\n";
1460 struct convertFromYacsObjref<XMLImpl,std::string>
1462 static inline std::string convert(const TypeCode *t,std::string& o)
1464 if(strncmp(t->id(),"python",6)==0 )
1465 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1466 else if(strncmp(t->id(),"json",4)==0)
1467 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1469 return "<value><objref>" + o + "</objref></value>\n";
1474 struct convertFromYacsSequence<XMLImpl,std::string>
1476 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1478 std::vector<std::string>::const_iterator iter;
1480 xmlob << "<value><array><data>\n";
1481 for(iter=v.begin();iter!=v.end();iter++)
1485 xmlob << "</data></array></value>\n";
1486 DEBTRACE("Sequence= " << xmlob);
1491 struct convertFromYacsStruct<XMLImpl,std::string>
1493 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1495 std::string result="<value><struct>\n";
1496 std::map<std::string, std::string>::const_iterator pt;
1497 for(pt=m.begin();pt!=m.end();pt++)
1499 std::string name=(*pt).first;
1500 std::string item=(*pt).second;
1501 result=result+"<member>\n";
1502 result=result+"<name>"+name+"</name>\n";
1504 result=result+"</member>\n";
1506 result=result+"</struct></value>\n";
1511 /* End of FromYacs Convertor for XMLImpl */
1513 //! ToYacs Convertor for NEUTRALImpl
1515 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1516 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1518 template <ImplType IMPLOUT, class TOUT>
1519 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1521 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1523 if(o->getType()->kind()==Double)
1524 return o->getDoubleValue();
1525 else if(o->getType()->kind()==Int)
1526 return o->getIntValue();
1529 msg << "Problem in conversion: a double or int is expected " ;
1530 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1531 throw YACS::ENGINE::ConversionException(msg.str());
1534 template <ImplType IMPLOUT, class TOUT>
1535 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1537 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1539 if(o->getType()->kind()==Int)
1540 return o->getIntValue();
1542 msg << "Problem in conversion: a int is expected " ;
1543 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1544 throw YACS::ENGINE::ConversionException(msg.str());
1547 template <ImplType IMPLOUT, class TOUT>
1548 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1550 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1552 if(o->getType()->kind()==String)
1553 return o->getStringValue();
1555 msg << "Problem in conversion: a string is expected " ;
1556 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1557 throw YACS::ENGINE::ConversionException(msg.str());
1560 template <ImplType IMPLOUT, class TOUT>
1561 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1563 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1565 if(o->getType()->kind()==Bool)
1566 return o->getBoolValue();
1567 else if(o->getType()->kind()==Int)
1568 return o->getIntValue() != 0;
1570 msg << "Problem in conversion: a bool or int is expected " ;
1571 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1572 throw YACS::ENGINE::ConversionException(msg.str());
1575 template <ImplType IMPLOUT, class TOUT>
1576 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1578 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1580 if(o->getType()->kind()==String)
1581 return o->getStringValue();
1583 msg << "Problem in conversion: a objref(string) is expected " ;
1584 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1585 throw YACS::ENGINE::ConversionException(msg.str());
1588 template <ImplType IMPLOUT, class TOUT>
1589 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1591 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1593 SequenceAny* sdata= (SequenceAny*)o;
1594 int length=sdata->size();
1596 for(int i=0;i<length;i++)
1598 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1603 template <ImplType IMPLOUT, class TOUT>
1604 struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1606 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
1608 StructAny * sdata = dynamic_cast<StructAny *>(o);
1609 YASSERT(sdata != NULL);
1610 const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
1611 YASSERT(tst != NULL);
1612 for (int i=0 ; i<tst->memberCount() ; i++)
1614 string name = tst->memberName(i);
1615 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
1620 /* End of ToYacs Convertor for NEUTRALImpl */
1622 //! FromYacs Convertor for NEUTRALImpl
1624 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1627 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1629 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1631 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1636 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1638 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1640 return YACS::ENGINE::AtomAny::New((int)o);
1644 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1646 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1648 return YACS::ENGINE::AtomAny::New(o);
1652 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1654 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1656 return YACS::ENGINE::AtomAny::New(o);
1660 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1662 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1664 //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
1665 if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
1666 return YACS::ENGINE::AtomAny::New(o);
1668 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1669 CORBA::Object_var obref;
1672 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1674 catch(CORBA::Exception& ex)
1676 throw ConversionException("Can't get reference to object");
1678 if(obref->_non_existent())
1679 throw ConversionException("non_existent object");
1680 if( CORBA::is_nil(obref) )
1681 throw ConversionException("Can't get reference to object");
1682 if(!obref->_is_a(t->id()))
1685 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1686 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1687 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1688 throw YACS::ENGINE::ConversionException(msg.str());
1691 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
1692 if(!CORBA::is_nil(gobj))
1694 DEBTRACE("It's a SALOME::GenericObj register it");
1698 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
1700 return YACS::ENGINE::AtomAny::New(o);
1705 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1707 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1709 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1710 //Objref are managed as string within YACS::ENGINE::Any objs
1712 any=SequenceAny::New(t->contentType());
1713 for(iter=v.begin();iter!=v.end();iter++)
1715 any->pushBack(*iter);
1718 DEBTRACE( "refcnt: " << any->getRefCnt() );
1724 struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
1726 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
1728 StructAny * any = StructAny::New((TypeCodeStruct *)t);
1729 std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
1730 for (it=m.begin() ; it!=m.end() ; it++)
1732 any->setEltAtRank(it->first.c_str(), it->second);
1733 it->second->decrRef();
1738 /* End of FromYacs Convertor for NEUTRALImpl */
1740 //! ToYacs Convertor for CORBAImpl
1742 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1743 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1745 template <ImplType IMPLOUT, class TOUT>
1746 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1748 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1750 CORBA::TypeCode_var tc = o->type();
1751 if (tc->equivalent(CORBA::_tc_double))
1757 if (tc->equivalent(CORBA::_tc_long))
1764 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1765 msg << " : " << __FILE__ << ":" << __LINE__;
1766 throw YACS::ENGINE::ConversionException(msg.str());
1769 template <ImplType IMPLOUT, class TOUT>
1770 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1772 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1778 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1779 msg << " : " << __FILE__ << ":" << __LINE__;
1780 throw YACS::ENGINE::ConversionException(msg.str());
1783 template <ImplType IMPLOUT, class TOUT>
1784 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1786 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1792 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1793 msg << " : " << __FILE__ << ":" << __LINE__;
1794 throw YACS::ENGINE::ConversionException(msg.str());
1797 template <ImplType IMPLOUT, class TOUT>
1798 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1800 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1803 if(*o >>= CORBA::Any::to_boolean(b))
1806 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1807 msg << " : " << __FILE__ << ":" << __LINE__;
1808 throw YACS::ENGINE::ConversionException(msg.str());
1811 template <ImplType IMPLOUT, class TOUT>
1812 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1814 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1816 char file[]="/tmp/XXXXXX";
1817 if(t->isA(Runtime::_tc_file))
1819 Engines::Salome_file_ptr sf;
1821 Salome_file_i* f=new Salome_file_i();
1823 f->setDistributedFile(file);
1829 else if(strncmp(t->id(),"python",6)==0)
1832 Engines::fileBlock * buffer;
1835 s=(const char*)buffer->get_buffer();
1839 std::string mystr(s,buffer->length());
1843 PyGILState_STATE gstate = PyGILState_Ensure();
1844 PyObject* mod=PyImport_ImportModule("cPickle");
1845 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",s,buffer->length());
1846 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
1847 DEBTRACE(PyObject_REPR(pickled));
1848 std::string mystr=PyString_AsString(pickled);
1852 PyGILState_Release(gstate);
1857 msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1858 msg << " : " << __FILE__ << ":" << __LINE__;
1859 throw YACS::ENGINE::ConversionException(msg.str());
1861 else if(strncmp(t->id(),"json",4)==0)
1869 msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1870 msg << " : " << __FILE__ << ":" << __LINE__;
1871 throw YACS::ENGINE::ConversionException(msg.str());
1875 CORBA::Object_var ObjRef ;
1876 *o >>= CORBA::Any::to_object(ObjRef) ;
1877 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1878 return (char *)objref;
1882 template <ImplType IMPLOUT, class TOUT>
1883 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1885 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1887 CORBA::TypeCode_var tc=o->type();
1888 if (tc->kind() != CORBA::tk_sequence)
1891 msg << "Not a sequence corba type " << tc->kind();
1892 msg << " : " << __FILE__ << ":" << __LINE__;
1893 throw YACS::ENGINE::ConversionException(msg.str());
1895 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1896 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1897 CORBA::release(dynany);
1898 DynamicAny::AnySeq_var as=ds->get_elements();
1899 int len=as->length();
1901 for(int i=0;i<len;i++)
1904 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1906 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1911 for(int i=0;i<len;i++)
1914 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1919 template <ImplType IMPLOUT, class TOUT>
1920 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1922 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1924 CORBA::TypeCode_var tc=o->type();
1925 DEBTRACE(tc->kind());
1926 if (tc->kind() != CORBA::tk_struct)
1929 msg << "Not a struct corba type " << tc->kind();
1930 msg << " : " << __FILE__ << ":" << __LINE__;
1931 throw YACS::ENGINE::ConversionException(msg.str());
1933 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1934 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1935 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1936 CORBA::release(dynany);
1937 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1938 int len=as->length();
1939 for(int i=0;i<len;i++)
1941 std::string name=as[i].id.in();
1943 CORBA::Any value=as[i].value;
1945 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1947 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1954 /* End of ToYacs Convertor for CORBAImpl */
1956 //! FromYacs Convertor for CORBAImpl
1958 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1961 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1963 static inline CORBA::Any* convert(const TypeCode *t,double o)
1965 CORBA::Any *any = new CORBA::Any();
1966 *any <<= (CORBA::Double)o;
1971 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
1973 static inline CORBA::Any* convert(const TypeCode *t,long o)
1975 CORBA::Any *any = new CORBA::Any();
1976 *any <<= (CORBA::Long)o;
1981 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
1983 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1985 CORBA::Any *any = new CORBA::Any();
1991 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
1993 static inline CORBA::Any* convert(const TypeCode *t,bool o)
1995 CORBA::Any *any = new CORBA::Any();
1996 *any <<= CORBA::Any::from_boolean(o);
2001 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
2003 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2005 CORBA::Object_var obref;
2007 if(t->isA(Runtime::_tc_file))
2009 //It's an objref file. Convert it specially
2010 Salome_file_i* aSalome_file = new Salome_file_i();
2013 aSalome_file->setLocalFile(o.c_str());
2014 obref = aSalome_file->_this();
2015 aSalome_file->_remove_ref();
2017 catch (const SALOME::SALOME_Exception& e)
2020 msg << e.details.text;
2021 msg << " : " << __FILE__ << ":" << __LINE__;
2022 throw YACS::ENGINE::ConversionException(msg.str());
2025 else if(strncmp(t->id(),"python",6)==0 )
2027 CORBA::Any *any = new CORBA::Any();
2028 Engines::fileBlock * buffer=new Engines::fileBlock();
2029 buffer->length(o.length());
2030 CORBA::Octet *buf=buffer->get_buffer();
2031 memcpy(buf,o.c_str(),o.length());
2035 else if(strncmp(t->id(),"json",4)==0)
2037 CORBA::Any *any = new CORBA::Any();
2045 obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
2047 catch(CORBA::Exception& ex)
2049 throw ConversionException("Can't get reference to object");
2051 if( CORBA::is_nil(obref) )
2053 throw ConversionException("Can't get reference to object");
2057 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2059 CORBA::Any *any = new CORBA::Any();
2062 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2069 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
2071 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
2073 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2074 std::vector<CORBA::Any*>::const_iterator iter;
2076 // Build an Any from vector v
2078 if(t->contentType()->kind() == Objref)
2081 CORBA::TypeCode_var tc=getCorbaTC(t);
2083 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2084 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
2085 ds->set_length(v.size());
2087 for(iter=v.begin();iter!=v.end();iter++)
2089 DynamicAny::DynAny_var temp=ds->current_component();
2090 CORBA::Any* a=*iter;
2091 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2094 CORBA::Object_var zzobj ;
2095 *a >>= CORBA::Any::to_object(zzobj) ;
2096 temp->insert_reference(zzobj);
2101 //delete intermediate any
2106 CORBA::Any *any=ds->to_any();
2112 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2114 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2116 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2118 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2119 int nMember=tst->memberCount();
2120 DEBTRACE("nMember="<<nMember);
2122 CORBA::TypeCode_var tc=getCorbaTC(t);
2123 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2124 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2126 for(int i=0;i<nMember;i++)
2128 DynamicAny::DynAny_var temp=ds->current_component();
2129 const char * name=tst->memberName(i);
2130 DEBTRACE("Member name="<<name);
2131 //do not test member presence : test has been done in ToYacs convertor
2132 CORBA::Any* a=m[name];
2133 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2134 CORBA::TypeCode_var atc = tc->member_type(i);
2135 if(atc->kind()==CORBA::tk_objref)
2137 //special treatment for objref
2138 CORBA::Object_var zzobj ;
2139 *a >>= CORBA::Any::to_object(zzobj) ;
2140 temp->insert_reference(zzobj);
2146 //delete intermediate any
2150 CORBA::Any *any=ds->to_any();
2156 /* End of FromYacs Convertor for CORBAImpl */
2158 /* Some shortcuts for CORBA to CORBA conversion */
2160 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2162 CORBA::TypeCode_var tc = o->type();
2163 if (tc->equivalent(CORBA::_tc_double))
2167 if (tc->equivalent(CORBA::_tc_long))
2171 CORBA::Any *any = new CORBA::Any();
2172 *any <<= (CORBA::Double)d;
2176 msg << "Not a double or long corba type " << tc->kind();
2177 msg << " : " << __FILE__ << ":" << __LINE__;
2178 throw YACS::ENGINE::ConversionException(msg.str());
2181 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2186 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2191 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2196 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2201 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2205 /* End of shortcuts for CORBA to CORBA conversion */
2207 //! ToYacs Convertor for CPPImpl
2209 * This convertor converts Python object to YACS<TOUT> types
2210 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2212 template <ImplType IMPLOUT, class TOUT>
2213 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2215 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2217 if(intype->kind()==YACS::ENGINE::Double)
2221 else if(intype->kind()==YACS::ENGINE::Int)
2226 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2227 msg << " : " << __FILE__ << ":" << __LINE__;
2228 throw YACS::ENGINE::ConversionException(msg.str());
2231 template <ImplType IMPLOUT, class TOUT>
2232 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2234 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2236 if(intype->kind()==YACS::ENGINE::Int)
2241 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2242 msg << " : " << __FILE__ << ":" << __LINE__;
2243 throw YACS::ENGINE::ConversionException(msg.str());
2246 /* End of ToYacs Convertor for CPPImpl */
2248 //Python conversions
2249 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2251 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2253 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2255 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2257 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2259 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2261 PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2263 return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2266 std::string convertPyObjectToString(PyObject* ob)
2269 PyGILState_STATE gstate = PyGILState_Ensure();
2271 std::string ss(PyString_AsString(s),PyString_Size(s));
2273 PyGILState_Release(gstate);
2278 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2280 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2282 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2284 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2286 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2288 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2290 PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2295 doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2298 std::stringstream msg;
2299 msg << "Problem in conversion: XML Document not parsed successfully ";
2300 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2301 throw YACS::ENGINE::ConversionException(msg.str());
2303 cur = xmlDocGetRootElement(doc);
2307 std::stringstream msg;
2308 msg << "Problem in conversion: empty XML Document";
2309 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2310 throw YACS::ENGINE::ConversionException(msg.str());
2314 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2316 ob=convertXmlPyObject(t,doc,cur);
2324 std::stringstream msg;
2325 msg << "Problem in conversion: incorrect XML value";
2326 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2327 throw YACS::ENGINE::ConversionException(msg.str());
2331 //NEUTRAL conversions
2332 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2334 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2336 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2338 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2340 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2342 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2344 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2351 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2353 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2355 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2357 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2359 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2361 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2363 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2365 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2368 //! Basic template checker from type TIN
2370 * This checker does nothing : throws exception
2371 * It must be partially specialize for a specific type (TIN)
2373 template <ImplType IMPLIN,class TIN,class TIN2>
2374 static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2377 msg << "Check not implemented for Implementation: " << IMPLIN ;
2378 msg << " : " << __FILE__ << ":" << __LINE__;
2379 throw YACS::ENGINE::ConversionException(msg.str());
2381 template <ImplType IMPLIN,class TIN,class TIN2>
2382 static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2385 msg << "Check not implemented for Implementation: " << IMPLIN ;
2386 msg << " : " << __FILE__ << ":" << __LINE__;
2387 throw YACS::ENGINE::ConversionException(msg.str());
2389 template <ImplType IMPLIN,class TIN,class TIN2>
2390 static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2393 msg << "Check not implemented for Implementation: " << IMPLIN ;
2394 msg << " : " << __FILE__ << ":" << __LINE__;
2395 throw YACS::ENGINE::ConversionException(msg.str());
2397 template <ImplType IMPLIN,class TIN,class TIN2>
2398 static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2401 msg << "Check not implemented for Implementation: " << IMPLIN ;
2402 msg << " : " << __FILE__ << ":" << __LINE__;
2403 throw YACS::ENGINE::ConversionException(msg.str());
2405 template <ImplType IMPLIN,class TIN,class TIN2>
2406 static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2409 msg << "Check not implemented for Implementation: " << IMPLIN ;
2410 msg << " : " << __FILE__ << ":" << __LINE__;
2411 throw YACS::ENGINE::ConversionException(msg.str());
2413 template <ImplType IMPLIN,class TIN,class TIN2>
2414 static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2417 msg << "Check not implemented for Implementation: " << IMPLIN ;
2418 msg << " : " << __FILE__ << ":" << __LINE__;
2419 throw YACS::ENGINE::ConversionException(msg.str());
2421 template <ImplType IMPLIN,class TIN,class TIN2>
2422 static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2425 msg << "Check not implemented for Implementation: " << IMPLIN ;
2426 msg << " : " << __FILE__ << ":" << __LINE__;
2427 throw YACS::ENGINE::ConversionException(msg.str());
2429 template <ImplType IMPLIN,class TIN,class TIN2>
2430 static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2433 msg << "Check not implemented for Implementation: " << IMPLIN ;
2434 msg << " : " << __FILE__ << ":" << __LINE__;
2435 throw YACS::ENGINE::ConversionException(msg.str());
2438 template <ImplType IMPLIN,class TIN,class TIN2>
2439 inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2445 return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2447 return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2449 return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2451 return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2453 return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2455 return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2457 return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2459 return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2464 msg << "Check not implemented for kind= " << tk ;
2465 msg << " : " << __FILE__ << ":" << __LINE__;
2466 throw YACS::ENGINE::ConversionException(msg.str());
2469 inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2471 if (PyFloat_Check(o))
2473 else if (PyInt_Check(o))
2475 else if(PyLong_Check(o))
2480 msg << "Not a python double ";
2481 throw YACS::ENGINE::ConversionException(msg.str());
2485 inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2487 if (PyInt_Check(o) || PyLong_Check(o))
2492 msg << "Not a python integer ";
2493 throw YACS::ENGINE::ConversionException(msg.str());
2497 inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2499 if (PyBool_Check(o))
2501 else if (PyInt_Check(o))
2503 else if(PyLong_Check(o))
2508 msg << "Not a python boolean " ;
2509 throw YACS::ENGINE::ConversionException(msg.str());
2514 inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2516 if (PyString_Check(o))
2521 msg << "Not a python string " ;
2522 throw YACS::ENGINE::ConversionException(msg.str());
2526 inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2528 if (PyString_Check(o))
2530 if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2532 else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2534 // The python object should be json compliant (to improve)
2539 // The python object should be a CORBA obj (to improve)
2544 inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2546 if(!PySequence_Check(o))
2549 msg << "python object is not a sequence " ;
2550 throw YACS::ENGINE::ConversionException(msg.str());
2552 int length=PySequence_Size(o);
2553 for(int i=0;i<length;i++)
2555 PyObject *item=PySequence_ITEM(o,i);
2558 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2560 catch(ConversionException& ex)
2563 msg << ex.what() << " for sequence element " << i;
2564 throw YACS::ENGINE::ConversionException(msg.str(),false);
2571 inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2574 if(!PyDict_Check(o))
2577 msg << "python object is not a dict " ;
2578 throw YACS::ENGINE::ConversionException(msg.str());
2580 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2581 int nMember=tst->memberCount();
2582 for(int i=0;i<nMember;i++)
2584 std::string name=tst->memberName(i);
2585 TypeCode* tm=tst->memberType(i);
2586 value=PyDict_GetItemString(o, name.c_str());
2590 msg << "member " << name << " not present " ;
2591 throw YACS::ENGINE::ConversionException(msg.str());
2595 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2597 catch(ConversionException& ex)
2599 std::string s=" for struct member "+name;
2600 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2606 bool checkPyObject(const TypeCode *t,PyObject* ob)
2608 return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);