1 // Copyright (C) 2006-2022 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"
36 #include "PythonNode.hxx"
46 int mkstemp(char *tmpl)
49 mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
55 #include "YacsTrace.hxx"
63 std::string getImplName(ImplType impl)
82 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
85 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
87 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
90 msg << "Conversion not implemented: kind= " << t->kind();
91 msg << " : " << __FILE__ << ":" << __LINE__;
92 throw YACS::ENGINE::ConversionException(msg.str());
95 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
97 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
100 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
102 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
105 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
107 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
110 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
112 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
115 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
117 DEBTRACE( t->name() << " " << t->shortName() << " " << t->id());
118 CORBA::TypeCode_ptr tc;
119 if(strncmp(t->id(),"python",6)==0 )
120 tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
121 else if(strncmp(t->id(),"json",4)==0)
122 tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
124 tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
126 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
131 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
133 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
134 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
136 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
137 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
142 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
144 CORBA::StructMemberSeq mseq;
145 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
146 int nMember=tst->memberCount();
147 mseq.length(nMember);
148 for(int i=0;i<nMember;i++)
150 const char * name=tst->memberName(i);
151 TypeCode* tm=tst->memberType(i);
152 mseq[i].name=CORBA::string_dup(name);
153 mseq[i].type=getCorbaTC(tm);
155 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
157 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
162 getCorbaTCFn getCorbaTCFns[]=
175 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
178 return getCorbaTCFns[tk](t);
182 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
186 * Section that defines functions to check adaptation from one implementation to another
187 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
188 * can be converted to TypeCode t2 from implementation IMPLOUT
189 * IMPLIN is the implementation of an output port
190 * IMPLOUT is the implementation of an input port
191 * If the check is True, the input port can be adapted to the output port
194 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
196 template <ImplType IMPLIN,ImplType IMPLOUT>
197 struct isAdaptableDouble
199 static inline int apply(const TypeCode *t1,const TypeCode* t2)
201 if(t1->kind() == Double)return 1;
202 if(t1->kind() == Int)return 1;
206 template <ImplType IMPLIN,ImplType IMPLOUT>
207 struct isAdaptableInt
209 static inline int apply(const TypeCode *t1,const TypeCode* t2)
211 if(t1->kind() == Int)return 1;
215 template <ImplType IMPLIN,ImplType IMPLOUT>
216 struct isAdaptableString
218 static inline int apply(const TypeCode *t1,const TypeCode* t2)
220 if(t1->kind() == String)return 1;
224 template <ImplType IMPLIN,ImplType IMPLOUT>
225 struct isAdaptableBool
227 static inline int apply(const TypeCode *t1,const TypeCode* t2)
229 if(t1->kind() == Bool)return 1;
230 if(t1->kind() == Int)return 1;
234 template <ImplType IMPLIN,ImplType IMPLOUT>
235 struct isAdaptableObjref
237 static inline int apply(const TypeCode *t1,const TypeCode* t2)
239 if(t1->kind() == Objref)
241 //The inport type must be more general than outport type
242 if( t1->isA(t2->id()) )
245 else if(t1->kind() == Sequence)
247 const TypeCodeSeq *t1c(dynamic_cast<const TypeCodeSeq *>(t1));
250 const TypeCode *t1cc(t1c->contentType());
253 if(t1cc->kind() == Objref && std::string(t1cc->id())==std::string(t2->id()))
259 template <ImplType IMPLIN,ImplType IMPLOUT>
260 struct isAdaptableSequence
262 static inline int apply(const TypeCode *t1,const TypeCode* t2)
264 if(t1->kind() == Sequence)
266 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
274 template <ImplType IMPLIN,ImplType IMPLOUT>
275 struct isAdaptableArray
277 static inline int apply(const TypeCode *t1,const TypeCode* t2)
282 template <ImplType IMPLIN,ImplType IMPLOUT>
283 struct isAdaptableStruct
285 static inline int apply(const TypeCode *t1,const TypeCode* t2)
287 if(t1->kind() == Struct)
297 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
298 * t1 is the IMPLIN output port type
299 * t2 is the IMPLOUT input port type
301 template <ImplType IMPLIN,ImplType IMPLOUT>
302 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
307 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
309 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
311 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
313 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
315 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
317 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
319 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
321 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
328 //xxx to Python adaptations
329 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
331 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
333 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
335 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
337 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
339 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
342 //xxx to Neutral adaptations
343 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
345 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
347 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
349 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
351 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
353 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
355 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
357 return isAdaptableNeutralCorba(t1, t2);
360 //xxx to XML adaptations
361 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
363 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
366 //xxx to Corba adaptations
367 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
369 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
371 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
373 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
375 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
377 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
379 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
381 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
384 //! Basic template convertor from type TIN to Yacs<TOUT> type
386 * This convertor does nothing : throws exception
387 * It must be partially specialize for a specific type (TIN)
389 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
390 struct convertToYacsDouble
392 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
395 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
396 msg << " : " << __FILE__ << ":" << __LINE__;
397 throw YACS::ENGINE::ConversionException(msg.str());
400 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
401 struct convertToYacsInt
403 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
406 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
407 msg << " : " << __FILE__ << ":" << __LINE__;
408 throw YACS::ENGINE::ConversionException(msg.str());
411 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
412 struct convertToYacsString
414 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
417 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
418 msg << " : " << __FILE__ << ":" << __LINE__;
419 throw YACS::ENGINE::ConversionException(msg.str());
422 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
423 struct convertToYacsBool
425 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
428 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
429 msg << " : " << __FILE__ << ":" << __LINE__;
430 throw YACS::ENGINE::ConversionException(msg.str());
433 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
434 struct convertToYacsObjref
436 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
439 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
440 msg << " : " << __FILE__ << ":" << __LINE__;
441 throw YACS::ENGINE::ConversionException(msg.str());
444 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
445 struct convertToYacsSequence
447 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
450 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
451 msg << " : " << __FILE__ << ":" << __LINE__;
452 throw YACS::ENGINE::ConversionException(msg.str());
455 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
456 struct convertToYacsArray
458 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
461 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
462 msg << " : " << __FILE__ << ":" << __LINE__;
463 throw YACS::ENGINE::ConversionException(msg.str());
466 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
467 struct convertToYacsStruct
469 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
472 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
473 msg << " : " << __FILE__ << ":" << __LINE__;
474 throw YACS::ENGINE::ConversionException(msg.str());
478 //! Basic convertor from Yacs<TOUT> type to full TOUT type
482 template <ImplType IMPLOUT, class TOUT>
483 struct convertFromYacsDouble
485 static inline TOUT convert(const TypeCode *t,double o)
488 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
489 msg << " : " << __FILE__ << ":" << __LINE__;
490 throw YACS::ENGINE::ConversionException(msg.str());
493 template <ImplType IMPLOUT, class TOUT>
494 struct convertFromYacsInt
496 static inline TOUT convert(const TypeCode *t,long o)
499 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
500 msg << " : " << __FILE__ << ":" << __LINE__;
501 throw YACS::ENGINE::ConversionException(msg.str());
504 template <ImplType IMPLOUT, class TOUT>
505 struct convertFromYacsString
507 static inline TOUT convert(const TypeCode *t,std::string o)
510 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
511 msg << " : " << __FILE__ << ":" << __LINE__;
512 throw YACS::ENGINE::ConversionException(msg.str());
515 template <ImplType IMPLOUT, class TOUT>
516 struct convertFromYacsBool
518 static inline TOUT convert(const TypeCode *t,bool o)
521 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
522 msg << " : " << __FILE__ << ":" << __LINE__;
523 throw YACS::ENGINE::ConversionException(msg.str());
526 template <ImplType IMPLOUT, class TOUT>
527 struct convertFromYacsObjref
529 static inline TOUT convert(const TypeCode *t,std::string o)
532 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
533 msg << " : " << __FILE__ << ":" << __LINE__;
534 throw YACS::ENGINE::ConversionException(msg.str());
537 template <ImplType IMPLOUT, class TOUT>
538 struct convertFromYacsSequence
540 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
543 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
544 msg << " : " << __FILE__ << ":" << __LINE__;
545 throw YACS::ENGINE::ConversionException(msg.str());
548 template <ImplType IMPLOUT, class TOUT>
549 struct convertFromYacsArray
551 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
554 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
555 msg << " : " << __FILE__ << ":" << __LINE__;
556 throw YACS::ENGINE::ConversionException(msg.str());
559 template <ImplType IMPLOUT, class TOUT>
560 struct convertFromYacsStruct
562 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
565 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
566 msg << " : " << __FILE__ << ":" << __LINE__;
567 throw YACS::ENGINE::ConversionException(msg.str());
570 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
571 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
573 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
575 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
578 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
579 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
581 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
583 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
586 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
587 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
589 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
591 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
594 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
595 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
597 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
599 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
602 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
603 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
607 protocol=0;//to avoid presence of \0 into XML generated file
608 if(IMPLOUT==NEUTRALImpl)
610 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
612 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
616 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
617 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
620 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
621 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
624 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
625 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
628 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
629 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
632 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
633 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
635 std::map<std::string,TOUT> v;
636 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
637 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
641 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
642 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
648 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
650 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
652 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
654 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
656 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
658 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
660 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
662 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
667 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
668 msg << " : " << __FILE__ << ":" << __LINE__;
669 throw YACS::ENGINE::ConversionException(msg.str());
672 //! ToYacs Convertor for PYTHONImpl
674 * This convertor converts Python object to YACS<TOUT> types
675 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
677 template <ImplType IMPLOUT, class TOUT>
678 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
680 static inline double convert(const TypeCode *t,PyObject* o,void*)
683 x=PyFloat_AsDouble(o);
684 if( PyErr_Occurred() )
686 PyErr_Restore(nullptr,nullptr,nullptr);
688 msg << "Not a python double. ";
690 msg << "kind=" << t->kind() ;
691 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
693 throw YACS::ENGINE::ConversionException(msg.str());
698 template <ImplType IMPLOUT, class TOUT>
699 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
701 static inline long convert(const TypeCode *t,PyObject* o,void*)
705 if( PyErr_Occurred() )
707 PyErr_Restore(nullptr,nullptr,nullptr);
709 msg << "Not a python integer. ";
711 msg << "kind=" << t->kind() ;
712 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
714 throw YACS::ENGINE::ConversionException(msg.str());
719 template <ImplType IMPLOUT, class TOUT>
720 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
722 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
725 if (PyUnicode_Check(o))
728 const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
730 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
736 msg << "Not a python string. ";
738 msg << "kind=" << t->kind() ;
739 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
741 throw YACS::ENGINE::ConversionException(msg.str());
746 template <ImplType IMPLOUT, class TOUT>
747 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
749 static inline bool convert(const TypeCode *t,PyObject* o,void*)
754 else if(PyLong_Check(o))
755 l=(PyLong_AsLong(o)!=0);
759 msg << "Not a python boolean. ";
761 msg << "kind=" << t->kind() ;
762 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
764 throw YACS::ENGINE::ConversionException(msg.str());
769 template <ImplType IMPLOUT, class TOUT>
770 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
772 static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
774 if (PyUnicode_Check(o) && strncmp(t->id(),"python",6)!=0)
776 // the objref is used by Python as a string (prefix:value) keep it as a string
779 const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
781 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
785 if(strncmp(t->id(),"python",6)==0)
787 bool somthingToDo = YACS::ENGINE::PythonEntry::GetDestroyStatus(o);
789 YACS::ENGINE::PythonEntry::DoNotTouchFileIfProxy(o);
790 // It's a native Python object pickle it
791 PyObject* mod=PyImport_ImportModule("pickle");
792 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
794 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(o);
795 DEBTRACE(PyObject_Repr(pickled) );
800 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
802 std::string mystr(PyBytes_AsString(pickled),PyBytes_Size(pickled));
806 else if(strncmp(t->id(),"json",4)==0)
808 // It's a Python object convert it to json
809 PyObject* mod=PyImport_ImportModule("simplejson");
813 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
815 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
820 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
822 std::string mystr=PyBytes_AsString(pickled);
828 // It should be a CORBA Object convert it to an IOR string
829 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
833 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
837 const char *ptr = PyUnicode_AsUTF8AndSize(pystring, &size);
839 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
840 mystr.assign(ptr, size);
846 template <ImplType IMPLOUT, class TOUT>
847 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
849 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
851 if(!PySequence_Check(o))
854 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
856 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
858 throw YACS::ENGINE::ConversionException(msg.str());
860 int length=PySequence_Size(o);
861 DEBTRACE("length: " << length );
863 for(int i=0;i<length;i++)
865 PyObject *item=PySequence_ITEM(o,i);
867 std::cerr <<"item[" << i << "]=";
868 PyObject_Print(item,stderr,Py_PRINT_RAW);
869 std::cerr << std::endl;
871 DEBTRACE( "item refcnt: " << item->ob_refcnt );
874 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
878 catch(ConversionException& ex)
881 msg << ex.what() << " for sequence element " << i;
882 throw YACS::ENGINE::ConversionException(msg.str(),false);
887 template <ImplType IMPLOUT, class TOUT>
888 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
890 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
892 DEBTRACE( "o refcnt: " << o->ob_refcnt );
893 PyObject *key, *value;
894 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
895 int nMember=tst->memberCount();
896 DEBTRACE("nMember="<<nMember);
897 for(int i=0;i<nMember;i++)
899 std::string name=tst->memberName(i);
900 DEBTRACE("Member name="<<name);
901 TypeCode* tm=tst->memberType(i);
902 value=PyDict_GetItemString(o, name.c_str());
905 //member name not present
906 //TODO delete all allocated objects in m
908 PyObject_Print(o,stderr,Py_PRINT_RAW);
909 std::cerr << std::endl;
912 msg << "member " << name << " not present " ;
913 throw YACS::ENGINE::ConversionException(msg.str());
915 DEBTRACE( "value refcnt: " << value->ob_refcnt );
918 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
921 catch(ConversionException& ex)
923 std::string s=" for struct member "+name;
924 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
929 /* End of ToYacs Convertor for PYTHONImpl */
931 //! FromYacs Convertor for PYTHONImpl
933 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
936 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
938 static inline PyObject* convert(const TypeCode *t,double o)
940 PyObject *pyob=PyFloat_FromDouble(o);
945 struct convertFromYacsInt<PYTHONImpl,PyObject*>
947 static inline PyObject* convert(const TypeCode *t,long o)
949 PyObject *pyob=PyLong_FromLong(o);
954 struct convertFromYacsString<PYTHONImpl,PyObject*>
956 static inline PyObject* convert(const TypeCode *t,std::string& o)
958 return PyUnicode_FromString(o.c_str());
962 struct convertFromYacsBool<PYTHONImpl,PyObject*>
964 static inline PyObject* convert(const TypeCode *t,bool o)
966 return PyBool_FromLong ((long)o);
970 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
972 static inline PyObject* convert(const TypeCode *t,std::string& o)
979 if(t->isA(Runtime::_tc_file))
981 //It's an objref file. Convert it specially
982 return PyUnicode_FromString(o.c_str());
984 if(strncmp(t->id(),"python",6)==0) //ex: "python:obj:1.0"
986 //It's a python pickled object, unpickled it
987 PyObject* mod=PyImport_ImportModule("pickle");
988 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",o.c_str(),o.length());
989 DEBTRACE(PyObject_Repr(ob));
994 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
998 if(strncmp(t->id(),"json",4)==0)
1000 // It's a json object unpack it
1001 PyObject* mod=PyImport_ImportModule("simplejson");
1005 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
1007 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y",o.c_str());
1012 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1017 /* another way to convert IOR string to CORBA PyObject
1018 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
1019 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
1023 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1024 CORBA::Object_var obref;
1027 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1029 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1032 catch(CORBA::Exception& ex)
1034 DEBTRACE( "Can't get reference to object." );
1035 throw ConversionException("Can't get reference to object");
1038 if(obref->_non_existent())
1040 throw ConversionException("non_existent object");
1043 if( CORBA::is_nil(obref) )
1045 DEBTRACE( "Can't get reference to object (or it was nil)." );
1046 throw ConversionException("Can't get reference to object");
1049 if(!obref->_is_a(t->id()))
1052 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1053 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1054 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1055 throw YACS::ENGINE::ConversionException(msg.str());
1058 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1061 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1062 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
1065 //hold_lock is true: caller is supposed to hold the GIL.
1066 //omniorb will not take the GIL
1067 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1070 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1071 std::cerr << std::endl;
1072 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1073 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1074 std::cerr << std::endl;
1077 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1078 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1080 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1082 PyErr_Clear();//Exception during narrow. Keep ob
1083 else if(result==Py_None)
1084 Py_DECREF(result); //Can't narrow. Keep ob
1087 //Can narrow. Keep result
1089 PyObject_Print(result,stderr,Py_PRINT_RAW);
1090 std::cerr << std::endl;
1098 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1105 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1107 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1109 std::vector<PyObject*>::const_iterator iter;
1110 PyObject *pyob = PyList_New(v.size());
1112 for(iter=v.begin();iter!=v.end();iter++)
1114 PyObject* item=*iter;
1115 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1116 PyList_SetItem(pyob,i,item);
1117 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1124 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1126 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1128 PyObject *pyob = PyDict_New();
1129 std::map<std::string, PyObject*>::const_iterator pt;
1130 for(pt=m.begin();pt!=m.end();pt++)
1132 std::string name=(*pt).first;
1133 PyObject* item=(*pt).second;
1134 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1135 PyDict_SetItemString(pyob,name.c_str(),item);
1137 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1139 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1143 /* End of FromYacs Convertor for PYTHONImpl */
1145 //! ToYacs Convertor for XMLImpl
1147 * Partial specialization for XML implementation (XMLImpl)
1148 * This convertor converts xml object to YACS<TOUT> types
1150 template <ImplType IMPLOUT, class TOUT>
1151 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1153 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1156 cur = cur->xmlChildrenNode;
1159 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1161 //wait a double, got a double
1163 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1166 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1167 d=Cstr2d((const char *)s);
1172 DEBTRACE("############### workaround to improve...");
1176 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1178 //wait a double, got an int
1180 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1183 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1184 d=Cstr2d((const char *)s);
1189 DEBTRACE("############### workaround to improve...");
1196 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1197 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1198 throw YACS::ENGINE::ConversionException(msg.str());
1201 template <ImplType IMPLOUT, class TOUT>
1202 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1204 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1207 cur = cur->xmlChildrenNode;
1210 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1213 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1216 DEBTRACE( "convertToYacsInt " << (const char *)s );
1217 d=atol((const char *)s);
1222 DEBTRACE("############### workaround to improve...");
1229 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1230 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1231 throw YACS::ENGINE::ConversionException(msg.str());
1234 template <ImplType IMPLOUT, class TOUT>
1235 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1237 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1239 cur = cur->xmlChildrenNode;
1242 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1244 //wait a string, got a string
1246 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1248 DEBTRACE( "convertToYacsString " << (const char *)s );
1249 std::string mystr=std::string((const char *)s);
1256 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1257 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1258 throw YACS::ENGINE::ConversionException(msg.str());
1261 template <ImplType IMPLOUT, class TOUT>
1262 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1264 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1266 cur = cur->xmlChildrenNode;
1269 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1271 //wait a boolean, got a boolean
1273 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1277 DEBTRACE( "convertToYacsBool " << (const char *)s );
1278 ob=atoi((const char*)s)!=0;
1283 DEBTRACE("############### workaround to improve...");
1290 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1291 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1292 throw YACS::ENGINE::ConversionException(msg.str());
1295 template <ImplType IMPLOUT, class TOUT>
1296 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1298 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1300 cur = cur->xmlChildrenNode;
1303 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1305 //we wait a objref, we have got a objref
1307 std::string mystr = "";
1308 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1311 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1312 mystr = (const char *)s;
1317 DEBTRACE("############### workaround to improve...");
1319 if(strncmp(t->id(),"python",6)==0 )
1320 return FromBase64Safe(mystr);
1324 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))// <- here case where pyobj value has been stored in XML. pyobj has kind==ObjRef. And the stored format is String ! EDF11027
1326 //wait a string, got a string
1328 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1330 DEBTRACE( "convertToYacsString " << (const char *)s );
1331 std::string mystr=std::string((const char *)s);
1338 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1339 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1340 throw YACS::ENGINE::ConversionException(msg.str());
1343 template <ImplType IMPLOUT, class TOUT>
1344 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1346 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1348 cur = cur->xmlChildrenNode;
1351 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1353 DEBTRACE( "parse sequence " );
1354 xmlNodePtr cur1=cur->xmlChildrenNode;
1355 while (cur1 != NULL)
1357 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1359 DEBTRACE( "parse data " );
1360 xmlNodePtr cur2=cur1->xmlChildrenNode;
1361 while (cur2 != NULL)
1363 //collect all values
1364 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1366 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1370 } // end while value
1378 } // end while array
1381 template <ImplType IMPLOUT, class TOUT>
1382 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1384 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1386 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1387 int nMember=tst->memberCount();
1388 DEBTRACE("nMember="<<nMember);
1389 std::map<std::string,TypeCode*> mtc;
1390 for(int i=0;i<nMember;i++)
1392 mtc[tst->memberName(i)]=tst->memberType(i);
1395 cur = cur->xmlChildrenNode;
1398 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1400 DEBTRACE( "parse struct " );
1401 xmlNodePtr cur1=cur->xmlChildrenNode;
1402 while (cur1 != NULL)
1404 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1406 DEBTRACE( "parse member " );
1407 xmlNodePtr cur2=cur1->xmlChildrenNode;
1408 while (cur2 != NULL)
1411 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1414 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1415 std::string name= (char *)s;
1417 while (cur2 != NULL)
1419 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1421 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1431 } // end while member/value
1434 } // end while member
1438 } // end while struct
1441 /* End of ToYacs Convertor for XMLImpl */
1443 //! FromYacs Convertor for XMLImpl
1445 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1448 struct convertFromYacsDouble<XMLImpl,std::string>
1450 static inline std::string convert(const TypeCode *t,double o)
1453 msg << "<value><double>" << setprecision(16) << o << "</double></value>\n";
1458 struct convertFromYacsInt<XMLImpl,std::string>
1460 static inline std::string convert(const TypeCode *t,long o)
1463 msg << "<value><int>" << o << "</int></value>\n";
1468 struct convertFromYacsString<XMLImpl,std::string>
1470 static inline std::string convert(const TypeCode *t,std::string& o)
1472 std::string msg="<value><string>";
1473 return msg+o+"</string></value>\n";
1477 struct convertFromYacsBool<XMLImpl,std::string>
1479 static inline std::string convert(const TypeCode *t,bool o)
1482 msg << "<value><boolean>" << o << "</boolean></value>\n";
1487 struct convertFromYacsObjref<XMLImpl,std::string>
1489 static inline std::string convert(const TypeCode *t,std::string& o)
1491 if(strncmp(t->id(),"python",6)==0 )
1492 return "<value><objref><![CDATA[" + ToBase64(o) + "]]></objref></value>\n";
1493 else if(strncmp(t->id(),"json",4)==0)
1494 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1496 return "<value><objref>" + o + "</objref></value>\n";
1501 struct convertFromYacsSequence<XMLImpl,std::string>
1503 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1505 std::vector<std::string>::const_iterator iter;
1507 xmlob << "<value><array><data>\n";
1508 for(iter=v.begin();iter!=v.end();iter++)
1512 xmlob << "</data></array></value>\n";
1513 DEBTRACE("Sequence= " << xmlob);
1518 struct convertFromYacsStruct<XMLImpl,std::string>
1520 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1522 std::string result="<value><struct>\n";
1523 std::map<std::string, std::string>::const_iterator pt;
1524 for(pt=m.begin();pt!=m.end();pt++)
1526 std::string name=(*pt).first;
1527 std::string item=(*pt).second;
1528 result=result+"<member>\n";
1529 result=result+"<name>"+name+"</name>\n";
1531 result=result+"</member>\n";
1533 result=result+"</struct></value>\n";
1538 /* End of FromYacs Convertor for XMLImpl */
1540 //! ToYacs Convertor for NEUTRALImpl
1542 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1543 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1545 template <ImplType IMPLOUT, class TOUT>
1546 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1548 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1550 if(o->getType()->kind()==Double)
1551 return o->getDoubleValue();
1552 else if(o->getType()->kind()==Int)
1553 return o->getIntValue();
1556 msg << "Problem in conversion: a double or int is expected " ;
1557 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1558 throw YACS::ENGINE::ConversionException(msg.str());
1561 template <ImplType IMPLOUT, class TOUT>
1562 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1564 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1566 if(o->getType()->kind()==Int)
1567 return o->getIntValue();
1569 msg << "Problem in conversion: a int is expected " ;
1570 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1571 throw YACS::ENGINE::ConversionException(msg.str());
1574 template <ImplType IMPLOUT, class TOUT>
1575 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1577 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1579 if(o->getType()->kind()==String)
1580 return o->getStringValue();
1582 msg << "Problem in conversion: a string is expected " ;
1583 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1584 throw YACS::ENGINE::ConversionException(msg.str());
1587 template <ImplType IMPLOUT, class TOUT>
1588 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1590 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1592 if(o->getType()->kind()==Bool)
1593 return o->getBoolValue();
1594 else if(o->getType()->kind()==Int)
1595 return o->getIntValue() != 0;
1597 msg << "Problem in conversion: a bool or int is expected " ;
1598 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1599 throw YACS::ENGINE::ConversionException(msg.str());
1602 template <ImplType IMPLOUT, class TOUT>
1603 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1605 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1607 if(o->getType()->kind()==String || o->getType()->kind()==Objref)
1608 return o->getStringValue();
1610 msg << "Problem in conversion: a objref(string) is expected " ;
1611 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1612 throw YACS::ENGINE::ConversionException(msg.str());
1615 template <ImplType IMPLOUT, class TOUT>
1616 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1618 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1620 SequenceAny* sdata= (SequenceAny*)o;
1621 int length=sdata->size();
1623 for(int i=0;i<length;i++)
1625 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1630 template <ImplType IMPLOUT, class TOUT>
1631 struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1633 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
1635 StructAny * sdata = dynamic_cast<StructAny *>(o);
1636 YASSERT(sdata != NULL);
1637 const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
1638 YASSERT(tst != NULL);
1639 for (int i=0 ; i<tst->memberCount() ; i++)
1641 string name = tst->memberName(i);
1642 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
1647 /* End of ToYacs Convertor for NEUTRALImpl */
1649 //! FromYacs Convertor for NEUTRALImpl
1651 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1654 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1656 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1658 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1663 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1665 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1667 return YACS::ENGINE::AtomAny::New((int)o);
1671 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1673 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1675 return YACS::ENGINE::AtomAny::New(o);
1679 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1681 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1683 return YACS::ENGINE::AtomAny::New(o);
1687 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1689 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1691 //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
1692 if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
1693 return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1695 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1696 CORBA::Object_var obref;
1699 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1701 catch(CORBA::Exception& ex)
1703 throw ConversionException("Can't get reference to object");
1705 if(obref->_non_existent())
1706 throw ConversionException("non_existent object");
1707 if( CORBA::is_nil(obref) )
1708 throw ConversionException("Can't get reference to object");
1709 if(!obref->_is_a(t->id()))
1712 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1713 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1714 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1715 throw YACS::ENGINE::ConversionException(msg.str());
1718 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
1719 if(!CORBA::is_nil(gobj))
1721 DEBTRACE("It's a SALOME::GenericObj register it");
1725 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
1727 return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1732 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1734 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1736 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1737 //Objref are managed as string within YACS::ENGINE::Any objs
1739 any=SequenceAny::New(t->contentType());
1740 for(iter=v.begin();iter!=v.end();iter++)
1742 any->pushBack(*iter);
1745 DEBTRACE( "refcnt: " << any->getRefCnt() );
1751 struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
1753 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
1755 StructAny * any = StructAny::New((TypeCodeStruct *)t);
1756 std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
1757 for (it=m.begin() ; it!=m.end() ; it++)
1759 any->setEltAtRank(it->first.c_str(), it->second);
1760 it->second->decrRef();
1765 /* End of FromYacs Convertor for NEUTRALImpl */
1767 //! ToYacs Convertor for CORBAImpl
1769 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1770 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1772 template <ImplType IMPLOUT, class TOUT>
1773 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1775 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1777 CORBA::TypeCode_var tc = o->type();
1778 if (tc->equivalent(CORBA::_tc_double))
1784 if (tc->equivalent(CORBA::_tc_long))
1791 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1792 msg << " : " << __FILE__ << ":" << __LINE__;
1793 throw YACS::ENGINE::ConversionException(msg.str());
1796 template <ImplType IMPLOUT, class TOUT>
1797 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1799 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1805 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1806 msg << " : " << __FILE__ << ":" << __LINE__;
1807 throw YACS::ENGINE::ConversionException(msg.str());
1810 template <ImplType IMPLOUT, class TOUT>
1811 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1813 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1819 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1820 msg << " : " << __FILE__ << ":" << __LINE__;
1821 throw YACS::ENGINE::ConversionException(msg.str());
1824 template <ImplType IMPLOUT, class TOUT>
1825 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1827 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1830 if(*o >>= CORBA::Any::to_boolean(b))
1833 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1834 msg << " : " << __FILE__ << ":" << __LINE__;
1835 throw YACS::ENGINE::ConversionException(msg.str());
1838 template <ImplType IMPLOUT, class TOUT>
1839 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1841 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1843 char file[]="/tmp/XXXXXX";
1844 if(t->isA(Runtime::_tc_file))
1846 Engines::Salome_file_ptr sf;
1848 Salome_file_i* f=new Salome_file_i();
1850 f->setDistributedFile(file);
1856 else if(strncmp(t->id(),"python",6)==0)
1859 Engines::fileBlock * buffer;
1862 s=(const char*)buffer->get_buffer();
1866 std::string mystr(s,buffer->length());
1870 PyGILState_STATE gstate = PyGILState_Ensure();
1871 PyObject* mod=PyImport_ImportModule("pickle");
1872 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",s,buffer->length());
1873 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
1874 DEBTRACE(PyObject_Repr(pickled));
1875 std::string mystr=PyBytes_AsString(pickled);
1879 PyGILState_Release(gstate);
1884 msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1885 msg << " : " << __FILE__ << ":" << __LINE__;
1886 throw YACS::ENGINE::ConversionException(msg.str());
1888 else if(strncmp(t->id(),"json",4)==0)
1896 msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1897 msg << " : " << __FILE__ << ":" << __LINE__;
1898 throw YACS::ENGINE::ConversionException(msg.str());
1902 CORBA::Object_var ObjRef ;
1903 *o >>= CORBA::Any::to_object(ObjRef) ;
1904 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1905 return (char *)objref;
1909 template <ImplType IMPLOUT, class TOUT>
1910 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1912 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1914 CORBA::TypeCode_var tc=o->type();
1915 if (tc->kind() != CORBA::tk_sequence)
1918 msg << "Not a sequence corba type " << tc->kind();
1919 msg << " : " << __FILE__ << ":" << __LINE__;
1920 throw YACS::ENGINE::ConversionException(msg.str());
1922 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1923 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1924 CORBA::release(dynany);
1925 DynamicAny::AnySeq_var as=ds->get_elements();
1926 int len=as->length();
1928 for(int i=0;i<len;i++)
1931 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1933 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1938 for(int i=0;i<len;i++)
1941 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1946 template <ImplType IMPLOUT, class TOUT>
1947 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1949 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1951 CORBA::TypeCode_var tc=o->type();
1952 DEBTRACE(tc->kind());
1953 if (tc->kind() != CORBA::tk_struct)
1956 msg << "Not a struct corba type " << tc->kind();
1957 msg << " : " << __FILE__ << ":" << __LINE__;
1958 throw YACS::ENGINE::ConversionException(msg.str());
1960 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1961 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1962 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1963 CORBA::release(dynany);
1964 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1965 int len=as->length();
1966 for(int i=0;i<len;i++)
1968 std::string name=as[i].id.in();
1970 CORBA::Any value=as[i].value;
1972 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1974 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1981 /* End of ToYacs Convertor for CORBAImpl */
1983 //! FromYacs Convertor for CORBAImpl
1985 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1988 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1990 static inline CORBA::Any* convert(const TypeCode *t,double o)
1992 CORBA::Any *any = new CORBA::Any();
1993 *any <<= (CORBA::Double)o;
1998 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
2000 static inline CORBA::Any* convert(const TypeCode *t,long o)
2002 CORBA::Any *any = new CORBA::Any();
2003 *any <<= (CORBA::Long)o;
2008 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
2010 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2012 CORBA::Any *any = new CORBA::Any();
2018 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
2020 static inline CORBA::Any* convert(const TypeCode *t,bool o)
2022 CORBA::Any *any = new CORBA::Any();
2023 *any <<= CORBA::Any::from_boolean(o);
2028 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
2030 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2032 CORBA::Object_var obref;
2034 if(t->isA(Runtime::_tc_file))
2036 //It's an objref file. Convert it specially
2037 Salome_file_i* aSalome_file = new Salome_file_i();
2040 aSalome_file->setLocalFile(o.c_str());
2041 obref = aSalome_file->_this();
2042 aSalome_file->_remove_ref();
2044 catch (const SALOME::SALOME_Exception& e)
2047 msg << e.details.text;
2048 msg << " : " << __FILE__ << ":" << __LINE__;
2049 throw YACS::ENGINE::ConversionException(msg.str());
2052 else if(strncmp(t->id(),"python",6)==0 )
2054 CORBA::Any *any = new CORBA::Any();
2055 Engines::fileBlock * buffer=new Engines::fileBlock();
2056 buffer->length(o.length());
2057 CORBA::Octet *buf=buffer->get_buffer();
2058 memcpy(buf,o.c_str(),o.length());
2062 else if(strncmp(t->id(),"json",4)==0)
2064 CORBA::Any *any = new CORBA::Any();
2072 obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
2074 catch(CORBA::Exception& ex)
2076 throw ConversionException("Can't get reference to object");
2078 if( CORBA::is_nil(obref) )
2080 throw ConversionException("Can't get reference to object");
2084 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2086 CORBA::Any *any = new CORBA::Any();
2089 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2096 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
2098 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
2100 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2101 std::vector<CORBA::Any*>::const_iterator iter;
2103 // Build an Any from vector v
2105 if(t->contentType()->kind() == Objref)
2108 CORBA::TypeCode_var tc=getCorbaTC(t);
2110 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2111 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
2112 ds->set_length(v.size());
2114 for(iter=v.begin();iter!=v.end();iter++)
2116 DynamicAny::DynAny_var temp=ds->current_component();
2117 CORBA::Any* a=*iter;
2118 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2121 CORBA::Object_var zzobj ;
2122 *a >>= CORBA::Any::to_object(zzobj) ;
2123 temp->insert_reference(zzobj);
2128 //delete intermediate any
2133 CORBA::Any *any=ds->to_any();
2139 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2141 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2143 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2145 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2146 int nMember=tst->memberCount();
2147 DEBTRACE("nMember="<<nMember);
2149 CORBA::TypeCode_var tc=getCorbaTC(t);
2150 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2151 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2153 for(int i=0;i<nMember;i++)
2155 DynamicAny::DynAny_var temp=ds->current_component();
2156 const char * name=tst->memberName(i);
2157 DEBTRACE("Member name="<<name);
2158 //do not test member presence : test has been done in ToYacs convertor
2159 CORBA::Any* a=m[name];
2160 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2161 CORBA::TypeCode_var atc = tc->member_type(i);
2162 if(atc->kind()==CORBA::tk_objref)
2164 //special treatment for objref
2165 CORBA::Object_var zzobj ;
2166 *a >>= CORBA::Any::to_object(zzobj) ;
2167 temp->insert_reference(zzobj);
2173 //delete intermediate any
2177 CORBA::Any *any=ds->to_any();
2183 /* End of FromYacs Convertor for CORBAImpl */
2185 /* Some shortcuts for CORBA to CORBA conversion */
2187 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2189 CORBA::TypeCode_var tc = o->type();
2190 if (tc->equivalent(CORBA::_tc_double))
2194 if (tc->equivalent(CORBA::_tc_long))
2198 CORBA::Any *any = new CORBA::Any();
2199 *any <<= (CORBA::Double)d;
2203 msg << "Not a double or long corba type " << tc->kind();
2204 msg << " : " << __FILE__ << ":" << __LINE__;
2205 throw YACS::ENGINE::ConversionException(msg.str());
2208 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2213 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2218 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2223 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2228 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2232 /* End of shortcuts for CORBA to CORBA conversion */
2234 //! ToYacs Convertor for CPPImpl
2236 * This convertor converts Python object to YACS<TOUT> types
2237 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2239 template <ImplType IMPLOUT, class TOUT>
2240 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2242 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2244 if(intype->kind()==YACS::ENGINE::Double)
2248 else if(intype->kind()==YACS::ENGINE::Int)
2253 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2254 msg << " : " << __FILE__ << ":" << __LINE__;
2255 throw YACS::ENGINE::ConversionException(msg.str());
2258 template <ImplType IMPLOUT, class TOUT>
2259 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2261 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2263 if(intype->kind()==YACS::ENGINE::Int)
2268 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2269 msg << " : " << __FILE__ << ":" << __LINE__;
2270 throw YACS::ENGINE::ConversionException(msg.str());
2273 /* End of ToYacs Convertor for CPPImpl */
2275 //Python conversions
2276 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2278 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2280 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2282 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2284 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2286 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2288 PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2290 return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2293 std::string convertPyObjectToString(PyObject* ob)
2296 PyGILState_STATE gstate = PyGILState_Ensure();
2297 // TODO: separate treatment for string (maybe with bad encoding?) and other types of PyObject ?
2298 // Do we need str() or repr() and what are the possible causes of failure of str() ?
2299 // not clear, needs more analysis.
2301 if (s == NULL) // for instance string with bad encoding, non utf-8
2303 s=PyObject_ASCII(ob); // escape non ASCII characters and like repr(), which is not the same as str()...
2306 const char* characters = PyUnicode_AsUTF8AndSize(s, &size);
2307 std::string ss( characters, size);
2309 PyGILState_Release(gstate);
2314 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2316 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2318 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2320 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2322 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2324 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2326 PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2331 doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2334 std::stringstream msg;
2335 msg << "Problem in conversion: XML Document not parsed successfully ";
2336 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2337 throw YACS::ENGINE::ConversionException(msg.str());
2339 cur = xmlDocGetRootElement(doc);
2343 std::stringstream msg;
2344 msg << "Problem in conversion: empty XML Document";
2345 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2346 throw YACS::ENGINE::ConversionException(msg.str());
2350 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2352 ob=convertXmlPyObject(t,doc,cur);
2360 std::stringstream msg;
2361 msg << "Problem in conversion: incorrect XML value";
2362 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2363 throw YACS::ENGINE::ConversionException(msg.str());
2367 //NEUTRAL conversions
2368 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2370 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2372 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2374 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2376 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2378 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2380 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2387 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2389 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2391 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2393 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2395 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2397 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2399 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2401 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2404 //! Basic template checker from type TIN
2406 * This checker does nothing : throws exception
2407 * It must be partially specialize for a specific type (TIN)
2409 template <ImplType IMPLIN,class TIN,class TIN2>
2410 static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2413 msg << "Check not implemented for Implementation: " << IMPLIN ;
2414 msg << " : " << __FILE__ << ":" << __LINE__;
2415 throw YACS::ENGINE::ConversionException(msg.str());
2417 template <ImplType IMPLIN,class TIN,class TIN2>
2418 static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2421 msg << "Check not implemented for Implementation: " << IMPLIN ;
2422 msg << " : " << __FILE__ << ":" << __LINE__;
2423 throw YACS::ENGINE::ConversionException(msg.str());
2425 template <ImplType IMPLIN,class TIN,class TIN2>
2426 static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2429 msg << "Check not implemented for Implementation: " << IMPLIN ;
2430 msg << " : " << __FILE__ << ":" << __LINE__;
2431 throw YACS::ENGINE::ConversionException(msg.str());
2433 template <ImplType IMPLIN,class TIN,class TIN2>
2434 static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2437 msg << "Check not implemented for Implementation: " << IMPLIN ;
2438 msg << " : " << __FILE__ << ":" << __LINE__;
2439 throw YACS::ENGINE::ConversionException(msg.str());
2441 template <ImplType IMPLIN,class TIN,class TIN2>
2442 static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2445 msg << "Check not implemented for Implementation: " << IMPLIN ;
2446 msg << " : " << __FILE__ << ":" << __LINE__;
2447 throw YACS::ENGINE::ConversionException(msg.str());
2449 template <ImplType IMPLIN,class TIN,class TIN2>
2450 static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2453 msg << "Check not implemented for Implementation: " << IMPLIN ;
2454 msg << " : " << __FILE__ << ":" << __LINE__;
2455 throw YACS::ENGINE::ConversionException(msg.str());
2457 template <ImplType IMPLIN,class TIN,class TIN2>
2458 static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2461 msg << "Check not implemented for Implementation: " << IMPLIN ;
2462 msg << " : " << __FILE__ << ":" << __LINE__;
2463 throw YACS::ENGINE::ConversionException(msg.str());
2465 template <ImplType IMPLIN,class TIN,class TIN2>
2466 static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2469 msg << "Check not implemented for Implementation: " << IMPLIN ;
2470 msg << " : " << __FILE__ << ":" << __LINE__;
2471 throw YACS::ENGINE::ConversionException(msg.str());
2474 template <ImplType IMPLIN,class TIN,class TIN2>
2475 inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2481 return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2483 return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2485 return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2487 return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2489 return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2491 return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2493 return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2495 return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2500 msg << "Check not implemented for kind= " << tk ;
2501 msg << " : " << __FILE__ << ":" << __LINE__;
2502 throw YACS::ENGINE::ConversionException(msg.str());
2505 inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2507 if (PyFloat_Check(o))
2509 else if(PyLong_Check(o))
2514 msg << "Not a python double ";
2515 throw YACS::ENGINE::ConversionException(msg.str());
2519 inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2521 if (PyLong_Check(o))
2526 msg << "Not a python integer ";
2527 throw YACS::ENGINE::ConversionException(msg.str());
2531 inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2533 if (PyBool_Check(o))
2535 else if(PyLong_Check(o))
2540 msg << "Not a python boolean " ;
2541 throw YACS::ENGINE::ConversionException(msg.str());
2546 inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2548 if (PyUnicode_Check(o))
2553 msg << "Not a python string " ;
2554 throw YACS::ENGINE::ConversionException(msg.str());
2558 inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2560 if (PyUnicode_Check(o))
2562 if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2564 else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2566 // The python object should be json compliant (to improve)
2571 // The python object should be a CORBA obj (to improve)
2576 inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2578 if(!PySequence_Check(o))
2581 msg << "python object is not a sequence " ;
2582 throw YACS::ENGINE::ConversionException(msg.str());
2584 int length=PySequence_Size(o);
2585 for(int i=0;i<length;i++)
2587 PyObject *item=PySequence_ITEM(o,i);
2590 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2592 catch(ConversionException& ex)
2595 msg << ex.what() << " for sequence element " << i;
2596 throw YACS::ENGINE::ConversionException(msg.str(),false);
2603 inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2606 if(!PyDict_Check(o))
2609 msg << "python object is not a dict " ;
2610 throw YACS::ENGINE::ConversionException(msg.str());
2612 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2613 int nMember=tst->memberCount();
2614 for(int i=0;i<nMember;i++)
2616 std::string name=tst->memberName(i);
2617 TypeCode* tm=tst->memberType(i);
2618 value=PyDict_GetItemString(o, name.c_str());
2622 msg << "member " << name << " not present " ;
2623 throw YACS::ENGINE::ConversionException(msg.str());
2627 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2629 catch(ConversionException& ex)
2631 std::string s=" for struct member "+name;
2632 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2638 bool checkPyObject(const TypeCode *t,PyObject* ob)
2640 return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);