1 // Copyright (C) 2006-2008 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.
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
22 #define private public
23 #define protected public
24 #include <omniORB4/CORBA.h>
25 #include <omniORB4/internal/typecode.h>
28 #include "TypeConversions.hxx"
29 #include "ConversionException.hxx"
30 #include "RuntimeSALOME.hxx"
31 #include "Salome_file_i.hxx"
32 #include "TypeCode.hxx"
34 #include "SALOME_GenericObj.hh"
40 #include "YacsTrace.hxx"
48 void printbin(const std::string& bin)
51 for(int i=0;i<bin.length();i++)
54 if (c < ' ' || c >= 0x7f)
56 fprintf(stderr,"\\x%02x",c & 0xff);
59 fprintf(stderr,"%c",c);
64 std::string getImplName(ImplType impl)
83 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
86 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
88 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
91 msg << "Conversion not implemented: kind= " << t->kind();
92 msg << " : " << __FILE__ << ":" << __LINE__;
93 throw YACS::ENGINE::ConversionException(msg.str());
96 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
98 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
101 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
103 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
106 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
108 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
111 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
113 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
116 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
118 DEBTRACE( t->name() << " " << t->shortName());
119 CORBA::TypeCode_ptr tc;
120 if(strncmp(t->id(),"python",6)==0 )
121 tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
122 else if(strncmp(t->id(),"json",4)==0)
123 tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
125 tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
127 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
132 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
134 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
135 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
137 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
138 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
143 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
145 CORBA::StructMemberSeq mseq;
146 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
147 int nMember=tst->memberCount();
148 mseq.length(nMember);
149 for(int i=0;i<nMember;i++)
151 const char * name=tst->memberName(i);
152 TypeCode* tm=tst->memberType(i);
153 mseq[i].name=CORBA::string_dup(name);
154 mseq[i].type=getCorbaTC(tm);
156 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
158 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
163 getCorbaTCFn getCorbaTCFns[]=
176 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
179 return getCorbaTCFns[tk](t);
183 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
187 * Section that defines functions to check adaptation from one implementation to another
188 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
189 * can be converted to TypeCode t2 from implementation IMPLOUT
190 * IMPLIN is the implementation of an output port
191 * IMPLOUT is the implementation of an input port
192 * If the check is True, the input port can be adapted to the output port
195 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
197 template <ImplType IMPLIN,ImplType IMPLOUT>
198 struct isAdaptableDouble
200 static inline int apply(const TypeCode *t1,const TypeCode* t2)
202 if(t1->kind() == Double)return 1;
203 if(t1->kind() == Int)return 1;
207 template <ImplType IMPLIN,ImplType IMPLOUT>
208 struct isAdaptableInt
210 static inline int apply(const TypeCode *t1,const TypeCode* t2)
212 if(t1->kind() == Int)return 1;
216 template <ImplType IMPLIN,ImplType IMPLOUT>
217 struct isAdaptableString
219 static inline int apply(const TypeCode *t1,const TypeCode* t2)
221 if(t1->kind() == String)return 1;
225 template <ImplType IMPLIN,ImplType IMPLOUT>
226 struct isAdaptableBool
228 static inline int apply(const TypeCode *t1,const TypeCode* t2)
230 if(t1->kind() == Bool)return 1;
231 if(t1->kind() == Int)return 1;
235 template <ImplType IMPLIN,ImplType IMPLOUT>
236 struct isAdaptableObjref
238 static inline int apply(const TypeCode *t1,const TypeCode* t2)
240 if(t1->kind() == Objref)
242 //The inport type must be more general than outport type
243 if( t1->isA(t2->id()) )
249 template <ImplType IMPLIN,ImplType IMPLOUT>
250 struct isAdaptableSequence
252 static inline int apply(const TypeCode *t1,const TypeCode* t2)
254 if(t1->kind() == Sequence)
256 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
264 template <ImplType IMPLIN,ImplType IMPLOUT>
265 struct isAdaptableArray
267 static inline int apply(const TypeCode *t1,const TypeCode* t2)
272 template <ImplType IMPLIN,ImplType IMPLOUT>
273 struct isAdaptableStruct
275 static inline int apply(const TypeCode *t1,const TypeCode* t2)
277 if(t1->kind() == Struct)
287 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
288 * t1 is the IMPLIN output port type
289 * t2 is the IMPLOUT input port type
291 template <ImplType IMPLIN,ImplType IMPLOUT>
292 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
297 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
299 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
301 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
303 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
305 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
307 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
309 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
311 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
318 //xxx to Python adaptations
319 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
321 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
323 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
325 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
327 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
329 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
332 //xxx to Neutral adaptations
333 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
335 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
337 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
339 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
341 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
343 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
345 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
347 return isAdaptableNeutralCorba(t1, t2);
350 //xxx to XML adaptations
351 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
353 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
356 //xxx to Corba adaptations
357 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
359 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
361 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
363 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
365 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
367 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
369 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
371 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
374 //! Basic template convertor from type TIN to Yacs<TOUT> type
376 * This convertor does nothing : throws exception
377 * It must be partially specialize for a specific type (TIN)
379 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
380 struct convertToYacsDouble
382 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
385 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
386 msg << " : " << __FILE__ << ":" << __LINE__;
387 throw YACS::ENGINE::ConversionException(msg.str());
390 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
391 struct convertToYacsInt
393 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
396 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
397 msg << " : " << __FILE__ << ":" << __LINE__;
398 throw YACS::ENGINE::ConversionException(msg.str());
401 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
402 struct convertToYacsString
404 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
407 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
408 msg << " : " << __FILE__ << ":" << __LINE__;
409 throw YACS::ENGINE::ConversionException(msg.str());
412 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
413 struct convertToYacsBool
415 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
418 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
419 msg << " : " << __FILE__ << ":" << __LINE__;
420 throw YACS::ENGINE::ConversionException(msg.str());
423 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
424 struct convertToYacsObjref
426 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
429 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
430 msg << " : " << __FILE__ << ":" << __LINE__;
431 throw YACS::ENGINE::ConversionException(msg.str());
434 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
435 struct convertToYacsSequence
437 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
440 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
441 msg << " : " << __FILE__ << ":" << __LINE__;
442 throw YACS::ENGINE::ConversionException(msg.str());
445 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
446 struct convertToYacsArray
448 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
451 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
452 msg << " : " << __FILE__ << ":" << __LINE__;
453 throw YACS::ENGINE::ConversionException(msg.str());
456 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
457 struct convertToYacsStruct
459 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
462 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
463 msg << " : " << __FILE__ << ":" << __LINE__;
464 throw YACS::ENGINE::ConversionException(msg.str());
468 //! Basic convertor from Yacs<TOUT> type to full TOUT type
472 template <ImplType IMPLOUT, class TOUT>
473 struct convertFromYacsDouble
475 static inline TOUT convert(const TypeCode *t,double o)
478 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
479 msg << " : " << __FILE__ << ":" << __LINE__;
480 throw YACS::ENGINE::ConversionException(msg.str());
483 template <ImplType IMPLOUT, class TOUT>
484 struct convertFromYacsInt
486 static inline TOUT convert(const TypeCode *t,long o)
489 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
490 msg << " : " << __FILE__ << ":" << __LINE__;
491 throw YACS::ENGINE::ConversionException(msg.str());
494 template <ImplType IMPLOUT, class TOUT>
495 struct convertFromYacsString
497 static inline TOUT convert(const TypeCode *t,std::string o)
500 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
501 msg << " : " << __FILE__ << ":" << __LINE__;
502 throw YACS::ENGINE::ConversionException(msg.str());
505 template <ImplType IMPLOUT, class TOUT>
506 struct convertFromYacsBool
508 static inline TOUT convert(const TypeCode *t,bool o)
511 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
512 msg << " : " << __FILE__ << ":" << __LINE__;
513 throw YACS::ENGINE::ConversionException(msg.str());
516 template <ImplType IMPLOUT, class TOUT>
517 struct convertFromYacsObjref
519 static inline TOUT convert(const TypeCode *t,std::string o)
522 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
523 msg << " : " << __FILE__ << ":" << __LINE__;
524 throw YACS::ENGINE::ConversionException(msg.str());
527 template <ImplType IMPLOUT, class TOUT>
528 struct convertFromYacsSequence
530 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
533 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
534 msg << " : " << __FILE__ << ":" << __LINE__;
535 throw YACS::ENGINE::ConversionException(msg.str());
538 template <ImplType IMPLOUT, class TOUT>
539 struct convertFromYacsArray
541 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
544 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
545 msg << " : " << __FILE__ << ":" << __LINE__;
546 throw YACS::ENGINE::ConversionException(msg.str());
549 template <ImplType IMPLOUT, class TOUT>
550 struct convertFromYacsStruct
552 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
555 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
556 msg << " : " << __FILE__ << ":" << __LINE__;
557 throw YACS::ENGINE::ConversionException(msg.str());
560 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
561 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
563 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
565 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
568 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
569 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
571 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
573 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
576 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
577 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
579 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
581 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
584 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
585 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
587 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
589 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
592 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
593 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
596 if(IMPLOUT==XMLImpl || IMPLOUT==NEUTRALImpl)
598 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
600 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
604 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
605 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
608 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
609 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
612 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
613 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
616 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
617 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
620 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
621 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
623 std::map<std::string,TOUT> v;
624 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
625 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
629 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
630 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
636 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
638 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
640 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
642 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
644 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
646 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
648 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
650 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
655 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
656 msg << " : " << __FILE__ << ":" << __LINE__;
657 throw YACS::ENGINE::ConversionException(msg.str());
660 //! ToYacs Convertor for PYTHONImpl
662 * This convertor converts Python object to YACS<TOUT> types
663 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
665 template <ImplType IMPLOUT, class TOUT>
666 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
668 static inline double convert(const TypeCode *t,PyObject* o,void*)
671 if (PyFloat_Check(o))
672 x=PyFloat_AS_DOUBLE(o);
673 else if (PyInt_Check(o))
675 else if(PyLong_Check(o))
680 msg << "Not a python double. ";
682 msg << "kind=" << t->kind() ;
683 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
685 throw YACS::ENGINE::ConversionException(msg.str());
690 template <ImplType IMPLOUT, class TOUT>
691 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
693 static inline long convert(const TypeCode *t,PyObject* o,void*)
698 else if(PyLong_Check(o))
703 msg << "Not a python integer. ";
705 msg << "kind=" << t->kind() ;
706 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
708 throw YACS::ENGINE::ConversionException(msg.str());
713 template <ImplType IMPLOUT, class TOUT>
714 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
716 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
719 if (PyString_Check(o))
720 s= PyString_AS_STRING(o);
724 msg << "Not a python string. ";
726 msg << "kind=" << t->kind() ;
727 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
729 throw YACS::ENGINE::ConversionException(msg.str());
734 template <ImplType IMPLOUT, class TOUT>
735 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
737 static inline bool convert(const TypeCode *t,PyObject* o,void*)
742 else if (PyInt_Check(o))
743 l=(PyInt_AS_LONG(o)!=0);
744 else if(PyLong_Check(o))
745 l=(PyLong_AsLong(o)!=0);
749 msg << "Not a python boolean. ";
751 msg << "kind=" << t->kind() ;
752 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
754 throw YACS::ENGINE::ConversionException(msg.str());
759 template <ImplType IMPLOUT, class TOUT>
760 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
762 static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
764 if (PyString_Check(o))
766 // the objref is used by Python as a string (prefix:value) keep it as a string
767 return PyString_AS_STRING(o);
769 if(strncmp(t->id(),"python",6)==0)
771 // It's a native Python object pickle it
772 PyObject* mod=PyImport_ImportModule("cPickle");
773 PyObject *pickled=PyObject_CallMethod(mod,"dumps","Oi",o,protocol);
774 DEBTRACE(PyObject_REPR(pickled) );
779 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
781 std::string mystr(PyString_AsString(pickled),PyString_Size(pickled));
785 else if(strncmp(t->id(),"json",4)==0)
787 // It's a Python object convert it to json
788 PyObject* mod=PyImport_ImportModule("simplejson");
792 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
794 PyObject *pickled=PyObject_CallMethod(mod,"dumps","O",o);
799 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
801 std::string mystr=PyString_AsString(pickled);
807 // It's a CORBA Object convert it to an IOR string
808 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"object_to_string","O",o);
812 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
814 std::string mystr=PyString_AsString(pystring);
820 template <ImplType IMPLOUT, class TOUT>
821 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
823 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
825 if(!PySequence_Check(o))
828 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
830 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
832 throw YACS::ENGINE::ConversionException(msg.str());
834 int length=PySequence_Size(o);
835 DEBTRACE("length: " << length );
837 for(int i=0;i<length;i++)
839 PyObject *item=PySequence_ITEM(o,i);
841 std::cerr <<"item[" << i << "]=";
842 PyObject_Print(item,stderr,Py_PRINT_RAW);
843 std::cerr << std::endl;
845 DEBTRACE( "item refcnt: " << item->ob_refcnt );
848 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
852 catch(ConversionException& ex)
855 msg << ex.what() << " for sequence element " << i;
856 throw YACS::ENGINE::ConversionException(msg.str(),false);
861 template <ImplType IMPLOUT, class TOUT>
862 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
864 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
866 DEBTRACE( "o refcnt: " << o->ob_refcnt );
867 PyObject *key, *value;
868 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
869 int nMember=tst->memberCount();
870 DEBTRACE("nMember="<<nMember);
871 for(int i=0;i<nMember;i++)
873 std::string name=tst->memberName(i);
874 DEBTRACE("Member name="<<name);
875 TypeCode* tm=tst->memberType(i);
876 value=PyDict_GetItemString(o, name.c_str());
879 //member name not present
880 //TODO delete all allocated objects in m
882 PyObject_Print(o,stderr,Py_PRINT_RAW);
883 std::cerr << std::endl;
886 msg << "member " << name << " not present " ;
887 throw YACS::ENGINE::ConversionException(msg.str());
889 DEBTRACE( "value refcnt: " << value->ob_refcnt );
892 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
895 catch(ConversionException& ex)
897 std::string s=" for struct member "+name;
898 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
903 /* End of ToYacs Convertor for PYTHONImpl */
905 //! FromYacs Convertor for PYTHONImpl
907 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
910 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
912 static inline PyObject* convert(const TypeCode *t,double o)
914 PyObject *pyob=PyFloat_FromDouble(o);
919 struct convertFromYacsInt<PYTHONImpl,PyObject*>
921 static inline PyObject* convert(const TypeCode *t,long o)
923 PyObject *pyob=PyLong_FromLong(o);
928 struct convertFromYacsString<PYTHONImpl,PyObject*>
930 static inline PyObject* convert(const TypeCode *t,std::string& o)
932 return PyString_FromString(o.c_str());
936 struct convertFromYacsBool<PYTHONImpl,PyObject*>
938 static inline PyObject* convert(const TypeCode *t,bool o)
940 return PyBool_FromLong ((long)o);
944 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
946 static inline PyObject* convert(const TypeCode *t,std::string& o)
948 if(t->isA(Runtime::_tc_file))
950 //It's an objref file. Convert it specially
951 return PyString_FromString(o.c_str());
953 if(strncmp(t->id(),"python",6)==0)
955 //It's a python pickled object, unpickled it
956 PyObject* mod=PyImport_ImportModule("cPickle");
957 PyObject *ob=PyObject_CallMethod(mod,"loads","s#",o.c_str(),o.length());
958 DEBTRACE(PyObject_REPR(ob));
963 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
967 if(strncmp(t->id(),"json",4)==0)
969 // It's a json object unpack it
970 PyObject* mod=PyImport_ImportModule("simplejson");
974 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
976 PyObject *ob=PyObject_CallMethod(mod,"loads","s",o.c_str());
981 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
986 /* another way to convert IOR string to CORBA PyObject
987 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
988 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
992 //Objref CORBA. prefix=IOR,corbaname,corbaloc
993 CORBA::Object_var obref;
996 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
998 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1001 catch(CORBA::Exception& ex)
1003 DEBTRACE( "Can't get reference to object." );
1004 throw ConversionException("Can't get reference to object");
1007 if( CORBA::is_nil(obref) )
1009 DEBTRACE( "Can't get reference to object (or it was nil)." );
1010 throw ConversionException("Can't get reference to object");
1013 if(!obref->_is_a(t->id()))
1016 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1017 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1018 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1019 throw YACS::ENGINE::ConversionException(msg.str());
1022 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1025 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1026 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
1029 //hold_lock is true: caller is supposed to hold the GIL.
1030 //omniorb will not take the GIL
1031 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1034 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1035 std::cerr << std::endl;
1036 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1037 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1038 std::cerr << std::endl;
1041 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1042 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1044 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), "narrow", "Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1046 PyErr_Clear();//Exception during narrow. Keep ob
1047 else if(result==Py_None)
1048 Py_DECREF(result); //Can't narrow. Keep ob
1051 //Can narrow. Keep result
1053 PyObject_Print(result,stderr,Py_PRINT_RAW);
1054 std::cerr << std::endl;
1062 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1069 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1071 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1073 std::vector<PyObject*>::const_iterator iter;
1074 PyObject *pyob = PyList_New(v.size());
1076 for(iter=v.begin();iter!=v.end();iter++)
1078 PyObject* item=*iter;
1079 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1080 PyList_SetItem(pyob,i,item);
1081 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1088 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1090 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1092 PyObject *pyob = PyDict_New();
1093 std::map<std::string, PyObject*>::const_iterator pt;
1094 for(pt=m.begin();pt!=m.end();pt++)
1096 std::string name=(*pt).first;
1097 PyObject* item=(*pt).second;
1098 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1099 PyDict_SetItemString(pyob,name.c_str(),item);
1101 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1103 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1107 /* End of FromYacs Convertor for PYTHONImpl */
1109 //! ToYacs Convertor for XMLImpl
1111 * Partial specialization for XML implementation (XMLImpl)
1112 * This convertor converts xml object to YACS<TOUT> types
1114 template <ImplType IMPLOUT, class TOUT>
1115 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1117 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1120 cur = cur->xmlChildrenNode;
1123 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1125 //wait a double, got a double
1127 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1130 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1131 d=Cstr2d((const char *)s);
1136 DEBTRACE("############### workaround to improve...");
1140 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1142 //wait a double, got an int
1144 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1147 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1148 d=Cstr2d((const char *)s);
1153 DEBTRACE("############### workaround to improve...");
1160 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1161 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1162 throw YACS::ENGINE::ConversionException(msg.str());
1165 template <ImplType IMPLOUT, class TOUT>
1166 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1168 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1171 cur = cur->xmlChildrenNode;
1174 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1177 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1180 DEBTRACE( "convertToYacsInt " << (const char *)s );
1181 d=atol((const char *)s);
1186 DEBTRACE("############### workaround to improve...");
1193 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1194 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1195 throw YACS::ENGINE::ConversionException(msg.str());
1198 template <ImplType IMPLOUT, class TOUT>
1199 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1201 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1203 cur = cur->xmlChildrenNode;
1206 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1208 //wait a string, got a string
1210 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1212 DEBTRACE( "convertToYacsString " << (const char *)s );
1213 std::string mystr=std::string((const char *)s);
1220 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1221 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1222 throw YACS::ENGINE::ConversionException(msg.str());
1225 template <ImplType IMPLOUT, class TOUT>
1226 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1228 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1230 cur = cur->xmlChildrenNode;
1233 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1235 //wait a boolean, got a boolean
1237 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1241 DEBTRACE( "convertToYacsBool " << (const char *)s );
1242 ob=atoi((const char*)s)!=0;
1247 DEBTRACE("############### workaround to improve...");
1254 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1255 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1256 throw YACS::ENGINE::ConversionException(msg.str());
1259 template <ImplType IMPLOUT, class TOUT>
1260 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1262 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1264 cur = cur->xmlChildrenNode;
1267 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1269 //we wait a objref, we have got a objref
1271 std::string mystr = "";
1272 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1275 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1276 mystr = (const char *)s;
1281 DEBTRACE("############### workaround to improve...");
1288 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1289 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1290 throw YACS::ENGINE::ConversionException(msg.str());
1293 template <ImplType IMPLOUT, class TOUT>
1294 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1296 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1298 cur = cur->xmlChildrenNode;
1301 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1303 DEBTRACE( "parse sequence " );
1304 xmlNodePtr cur1=cur->xmlChildrenNode;
1305 while (cur1 != NULL)
1307 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1309 DEBTRACE( "parse data " );
1310 xmlNodePtr cur2=cur1->xmlChildrenNode;
1311 while (cur2 != NULL)
1313 //collect all values
1314 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1316 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1320 } // end while value
1328 } // end while array
1331 template <ImplType IMPLOUT, class TOUT>
1332 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1334 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1336 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1337 int nMember=tst->memberCount();
1338 DEBTRACE("nMember="<<nMember);
1339 std::map<std::string,TypeCode*> mtc;
1340 for(int i=0;i<nMember;i++)
1342 mtc[tst->memberName(i)]=tst->memberType(i);
1345 cur = cur->xmlChildrenNode;
1348 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1350 DEBTRACE( "parse struct " );
1351 xmlNodePtr cur1=cur->xmlChildrenNode;
1352 while (cur1 != NULL)
1354 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1356 DEBTRACE( "parse member " );
1357 xmlNodePtr cur2=cur1->xmlChildrenNode;
1358 while (cur2 != NULL)
1361 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1364 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1365 std::string name= (char *)s;
1367 while (cur2 != NULL)
1369 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1371 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1381 } // end while member/value
1384 } // end while member
1388 } // end while struct
1391 /* End of ToYacs Convertor for XMLImpl */
1393 //! FromYacs Convertor for XMLImpl
1395 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1398 struct convertFromYacsDouble<XMLImpl,std::string>
1400 static inline std::string convert(const TypeCode *t,double o)
1403 msg << "<value><double>" << o << "</double></value>\n";
1408 struct convertFromYacsInt<XMLImpl,std::string>
1410 static inline std::string convert(const TypeCode *t,long o)
1413 msg << "<value><int>" << o << "</int></value>\n";
1418 struct convertFromYacsString<XMLImpl,std::string>
1420 static inline std::string convert(const TypeCode *t,std::string& o)
1422 std::string msg="<value><string>";
1423 return msg+o+"</string></value>\n";
1427 struct convertFromYacsBool<XMLImpl,std::string>
1429 static inline std::string convert(const TypeCode *t,bool o)
1432 msg << "<value><boolean>" << o << "</boolean></value>\n";
1437 struct convertFromYacsObjref<XMLImpl,std::string>
1439 static inline std::string convert(const TypeCode *t,std::string& o)
1441 if(strncmp(t->id(),"python",6)==0 )
1442 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1443 else if(strncmp(t->id(),"json",4)==0)
1444 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1446 return "<value><objref>" + o + "</objref></value>\n";
1451 struct convertFromYacsSequence<XMLImpl,std::string>
1453 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1455 std::vector<std::string>::const_iterator iter;
1457 xmlob << "<value><array><data>\n";
1458 for(iter=v.begin();iter!=v.end();iter++)
1462 xmlob << "</data></array></value>\n";
1463 DEBTRACE("Sequence= " << xmlob);
1468 struct convertFromYacsStruct<XMLImpl,std::string>
1470 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1472 std::string result="<value><struct>\n";
1473 std::map<std::string, std::string>::const_iterator pt;
1474 for(pt=m.begin();pt!=m.end();pt++)
1476 std::string name=(*pt).first;
1477 std::string item=(*pt).second;
1478 result=result+"<member>\n";
1479 result=result+"<name>"+name+"</name>\n";
1481 result=result+"</member>\n";
1483 result=result+"</struct></value>\n";
1488 /* End of FromYacs Convertor for XMLImpl */
1490 //! ToYacs Convertor for NEUTRALImpl
1492 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1493 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1495 template <ImplType IMPLOUT, class TOUT>
1496 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1498 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1500 if(o->getType()->kind()==Double)
1501 return o->getDoubleValue();
1502 else if(o->getType()->kind()==Int)
1503 return o->getIntValue();
1506 msg << "Problem in conversion: a double or int is expected " ;
1507 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1508 throw YACS::ENGINE::ConversionException(msg.str());
1511 template <ImplType IMPLOUT, class TOUT>
1512 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1514 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1516 if(o->getType()->kind()==Int)
1517 return o->getIntValue();
1519 msg << "Problem in conversion: a int is expected " ;
1520 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1521 throw YACS::ENGINE::ConversionException(msg.str());
1524 template <ImplType IMPLOUT, class TOUT>
1525 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1527 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1529 if(o->getType()->kind()==String)
1530 return o->getStringValue();
1532 msg << "Problem in conversion: a string is expected " ;
1533 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1534 throw YACS::ENGINE::ConversionException(msg.str());
1537 template <ImplType IMPLOUT, class TOUT>
1538 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1540 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1542 if(o->getType()->kind()==Bool)
1543 return o->getBoolValue();
1544 else if(o->getType()->kind()==Int)
1545 return o->getIntValue() != 0;
1547 msg << "Problem in conversion: a bool or int is expected " ;
1548 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1549 throw YACS::ENGINE::ConversionException(msg.str());
1552 template <ImplType IMPLOUT, class TOUT>
1553 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1555 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1557 if(o->getType()->kind()==String)
1558 return o->getStringValue();
1560 msg << "Problem in conversion: a objref(string) is expected " ;
1561 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1562 throw YACS::ENGINE::ConversionException(msg.str());
1565 template <ImplType IMPLOUT, class TOUT>
1566 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1568 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1570 SequenceAny* sdata= (SequenceAny*)o;
1571 int length=sdata->size();
1573 for(int i=0;i<length;i++)
1575 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1580 /* End of ToYacs Convertor for NEUTRALImpl */
1582 //! FromYacs Convertor for NEUTRALImpl
1584 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1587 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1589 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1591 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1596 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1598 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1600 return YACS::ENGINE::AtomAny::New((int)o);
1604 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1606 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1608 return YACS::ENGINE::AtomAny::New(o);
1612 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1614 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1616 return YACS::ENGINE::AtomAny::New(o);
1620 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1622 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1624 return YACS::ENGINE::AtomAny::New(o);
1629 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1631 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1633 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1634 //Objref are managed as string within YACS::ENGINE::Any objs
1636 any=SequenceAny::New(t->contentType());
1637 for(iter=v.begin();iter!=v.end();iter++)
1639 any->pushBack(*iter);
1642 DEBTRACE( "refcnt: " << any->getRefCnt() );
1646 /* End of FromYacs Convertor for NEUTRALImpl */
1648 //! ToYacs Convertor for CORBAImpl
1650 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1651 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1653 template <ImplType IMPLOUT, class TOUT>
1654 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1656 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1658 CORBA::TypeCode_var tc = o->type();
1659 if (tc->equivalent(CORBA::_tc_double))
1665 if (tc->equivalent(CORBA::_tc_long))
1672 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1673 msg << " : " << __FILE__ << ":" << __LINE__;
1674 throw YACS::ENGINE::ConversionException(msg.str());
1677 template <ImplType IMPLOUT, class TOUT>
1678 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1680 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1686 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1687 msg << " : " << __FILE__ << ":" << __LINE__;
1688 throw YACS::ENGINE::ConversionException(msg.str());
1691 template <ImplType IMPLOUT, class TOUT>
1692 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1694 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1700 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1701 msg << " : " << __FILE__ << ":" << __LINE__;
1702 throw YACS::ENGINE::ConversionException(msg.str());
1705 template <ImplType IMPLOUT, class TOUT>
1706 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1708 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1711 if(*o >>= CORBA::Any::to_boolean(b))
1714 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1715 msg << " : " << __FILE__ << ":" << __LINE__;
1716 throw YACS::ENGINE::ConversionException(msg.str());
1719 template <ImplType IMPLOUT, class TOUT>
1720 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1722 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1724 char file[]="/tmp/XXXXXX";
1725 if(t->isA(Runtime::_tc_file))
1727 Engines::Salome_file_ptr sf;
1729 Salome_file_i* f=new Salome_file_i();
1731 f->setDistributedFile(file);
1737 else if(strncmp(t->id(),"python",6)==0)
1740 Engines::fileBlock * buffer;
1743 s=(const char*)buffer->get_buffer();
1747 std::string mystr(s,buffer->length());
1751 PyGILState_STATE gstate = PyGILState_Ensure();
1752 PyObject* mod=PyImport_ImportModule("cPickle");
1753 PyObject *ob=PyObject_CallMethod(mod,"loads","s#",s,buffer->length());
1754 PyObject *pickled=PyObject_CallMethod(mod,"dumps","Oi",ob,protocol);
1755 DEBTRACE(PyObject_REPR(pickled));
1756 std::string mystr=PyString_AsString(pickled);
1760 PyGILState_Release(gstate);
1765 msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1766 msg << " : " << __FILE__ << ":" << __LINE__;
1767 throw YACS::ENGINE::ConversionException(msg.str());
1769 else if(strncmp(t->id(),"json",4)==0)
1777 msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1778 msg << " : " << __FILE__ << ":" << __LINE__;
1779 throw YACS::ENGINE::ConversionException(msg.str());
1783 CORBA::Object_var ObjRef ;
1784 *o >>= CORBA::Any::to_object(ObjRef) ;
1785 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1786 return (char *)objref;
1790 template <ImplType IMPLOUT, class TOUT>
1791 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1793 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1795 CORBA::TypeCode_var tc=o->type();
1796 if (tc->kind() != CORBA::tk_sequence)
1799 msg << "Not a sequence corba type " << tc->kind();
1800 msg << " : " << __FILE__ << ":" << __LINE__;
1801 throw YACS::ENGINE::ConversionException(msg.str());
1803 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1804 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1805 CORBA::release(dynany);
1806 DynamicAny::AnySeq_var as=ds->get_elements();
1807 int len=as->length();
1809 for(int i=0;i<len;i++)
1812 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1814 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1819 for(int i=0;i<len;i++)
1822 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1827 template <ImplType IMPLOUT, class TOUT>
1828 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1830 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1832 CORBA::TypeCode_var tc=o->type();
1833 DEBTRACE(tc->kind());
1834 if (tc->kind() != CORBA::tk_struct)
1837 msg << "Not a struct corba type " << tc->kind();
1838 msg << " : " << __FILE__ << ":" << __LINE__;
1839 throw YACS::ENGINE::ConversionException(msg.str());
1841 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1842 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1843 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1844 CORBA::release(dynany);
1845 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1846 int len=as->length();
1847 for(int i=0;i<len;i++)
1849 std::string name=as[i].id.in();
1851 CORBA::Any value=as[i].value;
1853 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1855 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1862 /* End of ToYacs Convertor for CORBAImpl */
1864 //! FromYacs Convertor for CORBAImpl
1866 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1869 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1871 static inline CORBA::Any* convert(const TypeCode *t,double o)
1873 CORBA::Any *any = new CORBA::Any();
1874 *any <<= (CORBA::Double)o;
1879 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
1881 static inline CORBA::Any* convert(const TypeCode *t,long o)
1883 CORBA::Any *any = new CORBA::Any();
1884 *any <<= (CORBA::Long)o;
1889 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
1891 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1893 CORBA::Any *any = new CORBA::Any();
1899 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
1901 static inline CORBA::Any* convert(const TypeCode *t,bool o)
1903 CORBA::Any *any = new CORBA::Any();
1904 *any <<= CORBA::Any::from_boolean(o);
1909 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
1911 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1913 CORBA::Object_var obref;
1915 if(t->isA(Runtime::_tc_file))
1917 //It's an objref file. Convert it specially
1918 Salome_file_i* aSalome_file = new Salome_file_i();
1921 aSalome_file->setLocalFile(o.c_str());
1922 obref = aSalome_file->_this();
1923 aSalome_file->_remove_ref();
1925 catch (const SALOME::SALOME_Exception& e)
1928 msg << e.details.text;
1929 msg << " : " << __FILE__ << ":" << __LINE__;
1930 throw YACS::ENGINE::ConversionException(msg.str());
1933 else if(strncmp(t->id(),"python",6)==0 )
1935 CORBA::Any *any = new CORBA::Any();
1936 Engines::fileBlock * buffer=new Engines::fileBlock();
1937 buffer->length(o.length());
1938 CORBA::Octet *buf=buffer->get_buffer();
1939 memcpy(buf,o.c_str(),o.length());
1943 else if(strncmp(t->id(),"json",4)==0)
1945 CORBA::Any *any = new CORBA::Any();
1953 obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1955 catch(CORBA::Exception& ex)
1957 throw ConversionException("Can't get reference to object");
1959 if( CORBA::is_nil(obref) )
1961 throw ConversionException("Can't get reference to object");
1965 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1967 CORBA::Any *any = new CORBA::Any();
1970 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1977 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
1979 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
1981 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
1982 std::vector<CORBA::Any*>::const_iterator iter;
1984 // Build an Any from vector v
1986 if(t->contentType()->kind() == Objref)
1989 CORBA::TypeCode_var tc=getCorbaTC(t);
1991 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
1992 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
1993 ds->set_length(v.size());
1995 for(iter=v.begin();iter!=v.end();iter++)
1997 DynamicAny::DynAny_var temp=ds->current_component();
1998 CORBA::Any* a=*iter;
2001 CORBA::Object_var zzobj ;
2002 *a >>= CORBA::Any::to_object(zzobj) ;
2003 temp->insert_reference(zzobj);
2008 //delete intermediate any
2013 CORBA::Any *any=ds->to_any();
2019 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2021 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2023 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2025 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2026 int nMember=tst->memberCount();
2027 DEBTRACE("nMember="<<nMember);
2029 CORBA::StructMemberSeq mseq;
2030 mseq.length(nMember);
2031 for(int i=0;i<nMember;i++)
2033 const char * name=tst->memberName(i);
2034 if(m.count(name) !=0)
2036 mseq[i].type=m[name]->type();
2039 CORBA::TypeCode_var tc= orb->create_struct_tc("","",mseq);
2040 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2041 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2043 for(int i=0;i<nMember;i++)
2045 DynamicAny::DynAny_var temp=ds->current_component();
2046 const char * name=tst->memberName(i);
2047 DEBTRACE("Member name="<<name);
2048 //do not test member presence : test has been done in ToYacs convertor
2049 CORBA::Any* a=m[name];
2051 //delete intermediate any
2055 CORBA::Any *any=ds->to_any();
2061 /* End of FromYacs Convertor for CORBAImpl */
2063 /* Some shortcuts for CORBA to CORBA conversion */
2065 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2067 CORBA::TypeCode_var tc = o->type();
2068 if (tc->equivalent(CORBA::_tc_double))
2072 if (tc->equivalent(CORBA::_tc_long))
2076 CORBA::Any *any = new CORBA::Any();
2077 *any <<= (CORBA::Double)d;
2081 msg << "Not a double or long corba type " << tc->kind();
2082 msg << " : " << __FILE__ << ":" << __LINE__;
2083 throw YACS::ENGINE::ConversionException(msg.str());
2086 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2091 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2096 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2101 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2106 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2110 /* End of shortcuts for CORBA to CORBA conversion */
2112 //! ToYacs Convertor for CPPImpl
2114 * This convertor converts Python object to YACS<TOUT> types
2115 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2117 template <ImplType IMPLOUT, class TOUT>
2118 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2120 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2122 if(intype->kind()==YACS::ENGINE::Double)
2126 else if(intype->kind()==YACS::ENGINE::Int)
2131 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2132 msg << " : " << __FILE__ << ":" << __LINE__;
2133 throw YACS::ENGINE::ConversionException(msg.str());
2136 template <ImplType IMPLOUT, class TOUT>
2137 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2139 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2141 if(intype->kind()==YACS::ENGINE::Int)
2146 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2147 msg << " : " << __FILE__ << ":" << __LINE__;
2148 throw YACS::ENGINE::ConversionException(msg.str());
2151 /* End of ToYacs Convertor for CPPImpl */
2153 //Python conversions
2154 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2156 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2158 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2160 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2162 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2164 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2166 PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2168 return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2171 std::string convertPyObjectToString(PyObject* ob)
2174 PyGILState_STATE gstate = PyGILState_Ensure();
2176 std::string ss(PyString_AsString(s),PyString_Size(s));
2178 PyGILState_Release(gstate);
2183 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2185 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2187 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2189 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2191 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2193 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2195 PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2200 doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2203 std::stringstream msg;
2204 msg << "Problem in conversion: XML Document not parsed successfully ";
2205 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2206 throw YACS::ENGINE::ConversionException(msg.str());
2208 cur = xmlDocGetRootElement(doc);
2212 std::stringstream msg;
2213 msg << "Problem in conversion: empty XML Document";
2214 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2215 throw YACS::ENGINE::ConversionException(msg.str());
2219 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2221 ob=convertXmlPyObject(t,doc,cur);
2229 std::stringstream msg;
2230 msg << "Problem in conversion: incorrect XML value";
2231 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2232 throw YACS::ENGINE::ConversionException(msg.str());
2236 //NEUTRAL conversions
2237 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2239 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2241 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2243 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2245 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2247 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2249 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2256 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2258 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2260 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2262 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2264 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2266 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2268 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2270 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2273 //! Basic template checker from type TIN
2275 * This checker does nothing : throws exception
2276 * It must be partially specialize for a specific type (TIN)
2278 template <ImplType IMPLIN,class TIN,class TIN2>
2279 static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2282 msg << "Check not implemented for Implementation: " << IMPLIN ;
2283 msg << " : " << __FILE__ << ":" << __LINE__;
2284 throw YACS::ENGINE::ConversionException(msg.str());
2286 template <ImplType IMPLIN,class TIN,class TIN2>
2287 static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2290 msg << "Check not implemented for Implementation: " << IMPLIN ;
2291 msg << " : " << __FILE__ << ":" << __LINE__;
2292 throw YACS::ENGINE::ConversionException(msg.str());
2294 template <ImplType IMPLIN,class TIN,class TIN2>
2295 static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2298 msg << "Check not implemented for Implementation: " << IMPLIN ;
2299 msg << " : " << __FILE__ << ":" << __LINE__;
2300 throw YACS::ENGINE::ConversionException(msg.str());
2302 template <ImplType IMPLIN,class TIN,class TIN2>
2303 static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2306 msg << "Check not implemented for Implementation: " << IMPLIN ;
2307 msg << " : " << __FILE__ << ":" << __LINE__;
2308 throw YACS::ENGINE::ConversionException(msg.str());
2310 template <ImplType IMPLIN,class TIN,class TIN2>
2311 static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2314 msg << "Check not implemented for Implementation: " << IMPLIN ;
2315 msg << " : " << __FILE__ << ":" << __LINE__;
2316 throw YACS::ENGINE::ConversionException(msg.str());
2318 template <ImplType IMPLIN,class TIN,class TIN2>
2319 static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2322 msg << "Check not implemented for Implementation: " << IMPLIN ;
2323 msg << " : " << __FILE__ << ":" << __LINE__;
2324 throw YACS::ENGINE::ConversionException(msg.str());
2326 template <ImplType IMPLIN,class TIN,class TIN2>
2327 static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2330 msg << "Check not implemented for Implementation: " << IMPLIN ;
2331 msg << " : " << __FILE__ << ":" << __LINE__;
2332 throw YACS::ENGINE::ConversionException(msg.str());
2334 template <ImplType IMPLIN,class TIN,class TIN2>
2335 static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2338 msg << "Check not implemented for Implementation: " << IMPLIN ;
2339 msg << " : " << __FILE__ << ":" << __LINE__;
2340 throw YACS::ENGINE::ConversionException(msg.str());
2343 template <ImplType IMPLIN,class TIN,class TIN2>
2344 inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2350 return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2352 return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2354 return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2356 return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2358 return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2360 return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2362 return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2364 return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2369 msg << "Check not implemented for kind= " << tk ;
2370 msg << " : " << __FILE__ << ":" << __LINE__;
2371 throw YACS::ENGINE::ConversionException(msg.str());
2374 inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2376 if (PyFloat_Check(o))
2378 else if (PyInt_Check(o))
2380 else if(PyLong_Check(o))
2385 msg << "Not a python double ";
2386 throw YACS::ENGINE::ConversionException(msg.str());
2390 inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2392 if (PyInt_Check(o) || PyLong_Check(o))
2397 msg << "Not a python integer ";
2398 throw YACS::ENGINE::ConversionException(msg.str());
2402 inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2404 if (PyBool_Check(o))
2406 else if (PyInt_Check(o))
2408 else if(PyLong_Check(o))
2413 msg << "Not a python boolean " ;
2414 throw YACS::ENGINE::ConversionException(msg.str());
2419 inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2421 if (PyString_Check(o))
2426 msg << "Not a python string " ;
2427 throw YACS::ENGINE::ConversionException(msg.str());
2431 inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2433 if (PyString_Check(o))
2435 if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2437 else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2439 // The python object should be json compliant (to improve)
2444 // The python object should be a CORBA obj (to improve)
2449 inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2451 if(!PySequence_Check(o))
2454 msg << "python object is not a sequence " ;
2455 throw YACS::ENGINE::ConversionException(msg.str());
2457 int length=PySequence_Size(o);
2458 for(int i=0;i<length;i++)
2460 PyObject *item=PySequence_ITEM(o,i);
2463 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2465 catch(ConversionException& ex)
2468 msg << ex.what() << " for sequence element " << i;
2469 throw YACS::ENGINE::ConversionException(msg.str(),false);
2476 inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2479 if(!PyDict_Check(o))
2482 msg << "python object is not a dict " ;
2483 throw YACS::ENGINE::ConversionException(msg.str());
2485 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2486 int nMember=tst->memberCount();
2487 for(int i=0;i<nMember;i++)
2489 std::string name=tst->memberName(i);
2490 TypeCode* tm=tst->memberType(i);
2491 value=PyDict_GetItemString(o, name.c_str());
2495 msg << "member " << name << " not present " ;
2496 throw YACS::ENGINE::ConversionException(msg.str());
2500 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2502 catch(ConversionException& ex)
2504 std::string s=" for struct member "+name;
2505 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2511 bool checkPyObject(const TypeCode *t,PyObject* ob)
2513 return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);