1 // Copyright (C) 2006-2024 CEA, EDF
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);
795 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(o);
796 YACS::ENGINE::PythonEntry::IfProxyDoSomething(o,"incrRef");
798 DEBTRACE(PyObject_Repr(pickled) );
803 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
805 std::string mystr(PyBytes_AsString(pickled),PyBytes_Size(pickled));
809 else if(strncmp(t->id(),"json",4)==0)
811 // It's a Python object convert it to json
812 PyObject* mod=PyImport_ImportModule("simplejson");
816 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
818 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
823 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
825 std::string mystr=PyBytes_AsString(pickled);
831 // It should be a CORBA Object convert it to an IOR string
832 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
836 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
840 const char *ptr = PyUnicode_AsUTF8AndSize(pystring, &size);
842 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
843 mystr.assign(ptr, size);
849 template <ImplType IMPLOUT, class TOUT>
850 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
852 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
854 if(!PySequence_Check(o))
857 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
859 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
861 throw YACS::ENGINE::ConversionException(msg.str());
863 int length=PySequence_Size(o);
864 GURU_YACSTRACE("convertToYacsSequence : length = " << length);
866 for(int i=0;i<length;i++)
868 PyObject *item=PySequence_ITEM(o,i);
869 GURU_YACSTRACE("convertToYacsSequence : analyze if proxy");
870 bool somthingToDo = YACS::ENGINE::PythonEntry::IsProxy(item);
873 GURU_YACSTRACE("convertToYacsSequence : it s proxy -> unlink");
874 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(item);
875 GURU_YACSTRACE("convertToYacsSequence : it s proxy -> incrRef");
876 YACS::ENGINE::PythonEntry::IfProxyDoSomething(item,"incrRef");
879 std::cerr <<"item[" << i << "]=";
880 PyObject_Print(item,stderr,Py_PRINT_RAW);
881 std::cerr << std::endl;
883 DEBTRACE( "item refcnt: " << item->ob_refcnt );
886 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
890 catch(ConversionException& ex)
893 msg << ex.what() << " for sequence element " << i;
894 throw YACS::ENGINE::ConversionException(msg.str(),false);
899 template <ImplType IMPLOUT, class TOUT>
900 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
902 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
904 DEBTRACE( "o refcnt: " << o->ob_refcnt );
905 PyObject *key, *value;
906 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
907 int nMember=tst->memberCount();
908 DEBTRACE("nMember="<<nMember);
909 for(int i=0;i<nMember;i++)
911 std::string name=tst->memberName(i);
912 DEBTRACE("Member name="<<name);
913 TypeCode* tm=tst->memberType(i);
914 value=PyDict_GetItemString(o, name.c_str());
917 //member name not present
918 //TODO delete all allocated objects in m
920 PyObject_Print(o,stderr,Py_PRINT_RAW);
921 std::cerr << std::endl;
924 msg << "member " << name << " not present " ;
925 throw YACS::ENGINE::ConversionException(msg.str());
927 DEBTRACE( "value refcnt: " << value->ob_refcnt );
930 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
933 catch(ConversionException& ex)
935 std::string s=" for struct member "+name;
936 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
941 /* End of ToYacs Convertor for PYTHONImpl */
943 //! FromYacs Convertor for PYTHONImpl
945 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
948 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
950 static inline PyObject* convert(const TypeCode *t,double o)
952 PyObject *pyob=PyFloat_FromDouble(o);
957 struct convertFromYacsInt<PYTHONImpl,PyObject*>
959 static inline PyObject* convert(const TypeCode *t,long o)
961 PyObject *pyob=PyLong_FromLong(o);
966 struct convertFromYacsString<PYTHONImpl,PyObject*>
968 static inline PyObject* convert(const TypeCode *t,std::string& o)
970 return PyUnicode_FromString(o.c_str());
974 struct convertFromYacsBool<PYTHONImpl,PyObject*>
976 static inline PyObject* convert(const TypeCode *t,bool o)
978 return PyBool_FromLong ((long)o);
982 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
984 static inline PyObject* convert(const TypeCode *t,std::string& o)
991 if(t->isA(Runtime::_tc_file))
993 //It's an objref file. Convert it specially
994 return PyUnicode_FromString(o.c_str());
996 if(strncmp(t->id(),"python",6)==0) //ex: "python:obj:1.0"
998 //It's a python pickled object, unpickled it
999 PyObject* mod=PyImport_ImportModule("pickle");
1000 GURU_YACSTRACE("convertFromYacsObjref : unpickling...");
1001 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",o.c_str(),o.length());
1002 GURU_YACSTRACE("convertFromYacsObjref : unpickling done...");
1003 DEBTRACE(PyObject_Repr(ob));
1005 GURU_YACSTRACE("convertFromYacsObjref : Analyze if it's a proxy");
1006 bool somthingToDo = YACS::ENGINE::PythonEntry::IsProxy(ob);
1009 // no incrRef here because the incrRef has been done before dumps that construct o.
1010 GURU_YACSTRACE("convertFromYacsObjref : It's a proxy -> activate on deletion");
1011 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(ob);
1016 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1020 if(strncmp(t->id(),"json",4)==0)
1022 // It's a json object unpack it
1023 PyObject* mod=PyImport_ImportModule("simplejson");
1027 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
1029 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y",o.c_str());
1034 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1039 /* another way to convert IOR string to CORBA PyObject
1040 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
1041 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
1045 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1046 CORBA::Object_var obref;
1049 obref = getSALOMERuntime()->getFromNS(o.c_str());
1051 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1054 catch(CORBA::Exception& ex)
1056 DEBTRACE( "Can't get reference to object." );
1057 throw ConversionException("Can't get reference to object");
1060 if(obref->_non_existent())
1062 throw ConversionException("non_existent object");
1065 if( CORBA::is_nil(obref) )
1067 DEBTRACE( "Can't get reference to object (or it was nil)." );
1068 throw ConversionException("Can't get reference to object");
1071 if(!obref->_is_a(t->id()))
1074 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1075 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1076 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1077 throw YACS::ENGINE::ConversionException(msg.str());
1080 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1083 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1084 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
1087 //hold_lock is true: caller is supposed to hold the GIL.
1088 //omniorb will not take the GIL
1089 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1092 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1093 std::cerr << std::endl;
1094 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1095 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1096 std::cerr << std::endl;
1099 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1100 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1102 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1104 PyErr_Clear();//Exception during narrow. Keep ob
1105 else if(result==Py_None)
1106 Py_DECREF(result); //Can't narrow. Keep ob
1109 //Can narrow. Keep result
1111 PyObject_Print(result,stderr,Py_PRINT_RAW);
1112 std::cerr << std::endl;
1120 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1127 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1129 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1131 std::vector<PyObject*>::const_iterator iter;
1132 PyObject *pyob = PyList_New(v.size());
1134 for(iter=v.begin();iter!=v.end();iter++)
1136 PyObject* item=*iter;
1137 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1138 PyList_SetItem(pyob,i,item);
1139 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1146 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1148 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1150 PyObject *pyob = PyDict_New();
1151 std::map<std::string, PyObject*>::const_iterator pt;
1152 for(pt=m.begin();pt!=m.end();pt++)
1154 std::string name=(*pt).first;
1155 PyObject* item=(*pt).second;
1156 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1157 PyDict_SetItemString(pyob,name.c_str(),item);
1159 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1161 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1165 /* End of FromYacs Convertor for PYTHONImpl */
1167 //! ToYacs Convertor for XMLImpl
1169 * Partial specialization for XML implementation (XMLImpl)
1170 * This convertor converts xml object to YACS<TOUT> types
1172 template <ImplType IMPLOUT, class TOUT>
1173 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1175 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1178 cur = cur->xmlChildrenNode;
1181 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1183 //wait a double, got a double
1185 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1188 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1189 d=Cstr2d((const char *)s);
1194 DEBTRACE("############### workaround to improve...");
1198 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1200 //wait a double, got an int
1202 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1205 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1206 d=Cstr2d((const char *)s);
1211 DEBTRACE("############### workaround to improve...");
1218 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1219 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1220 throw YACS::ENGINE::ConversionException(msg.str());
1223 template <ImplType IMPLOUT, class TOUT>
1224 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1226 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1229 cur = cur->xmlChildrenNode;
1232 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1235 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1238 DEBTRACE( "convertToYacsInt " << (const char *)s );
1239 d=atol((const char *)s);
1244 DEBTRACE("############### workaround to improve...");
1251 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1252 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1253 throw YACS::ENGINE::ConversionException(msg.str());
1256 template <ImplType IMPLOUT, class TOUT>
1257 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1259 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1261 cur = cur->xmlChildrenNode;
1264 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1266 //wait a string, got a string
1268 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1270 DEBTRACE( "convertToYacsString " << (const char *)s );
1271 std::string mystr=std::string((const char *)s);
1278 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1279 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1280 throw YACS::ENGINE::ConversionException(msg.str());
1283 template <ImplType IMPLOUT, class TOUT>
1284 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1286 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1288 cur = cur->xmlChildrenNode;
1291 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1293 //wait a boolean, got a boolean
1295 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1299 DEBTRACE( "convertToYacsBool " << (const char *)s );
1300 ob=atoi((const char*)s)!=0;
1305 DEBTRACE("############### workaround to improve...");
1312 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1313 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1314 throw YACS::ENGINE::ConversionException(msg.str());
1317 template <ImplType IMPLOUT, class TOUT>
1318 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1320 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1322 cur = cur->xmlChildrenNode;
1325 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1327 //we wait a objref, we have got a objref
1329 std::string mystr = "";
1330 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1333 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1334 mystr = (const char *)s;
1339 DEBTRACE("############### workaround to improve...");
1341 if(strncmp(t->id(),"python",6)==0 )
1342 return FromBase64Safe(mystr);
1346 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
1348 //wait a string, got a string
1350 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1352 DEBTRACE( "convertToYacsString " << (const char *)s );
1353 std::string mystr=std::string((const char *)s);
1360 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1361 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1362 throw YACS::ENGINE::ConversionException(msg.str());
1365 template <ImplType IMPLOUT, class TOUT>
1366 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1368 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1370 cur = cur->xmlChildrenNode;
1373 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1375 DEBTRACE( "parse sequence " );
1376 xmlNodePtr cur1=cur->xmlChildrenNode;
1377 while (cur1 != NULL)
1379 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1381 DEBTRACE( "parse data " );
1382 xmlNodePtr cur2=cur1->xmlChildrenNode;
1383 while (cur2 != NULL)
1385 //collect all values
1386 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1388 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1392 } // end while value
1400 } // end while array
1403 template <ImplType IMPLOUT, class TOUT>
1404 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1406 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1408 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1409 int nMember=tst->memberCount();
1410 DEBTRACE("nMember="<<nMember);
1411 std::map<std::string,TypeCode*> mtc;
1412 for(int i=0;i<nMember;i++)
1414 mtc[tst->memberName(i)]=tst->memberType(i);
1417 cur = cur->xmlChildrenNode;
1420 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1422 DEBTRACE( "parse struct " );
1423 xmlNodePtr cur1=cur->xmlChildrenNode;
1424 while (cur1 != NULL)
1426 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1428 DEBTRACE( "parse member " );
1429 xmlNodePtr cur2=cur1->xmlChildrenNode;
1430 while (cur2 != NULL)
1433 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1436 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1437 std::string name= (char *)s;
1439 while (cur2 != NULL)
1441 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1443 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1453 } // end while member/value
1456 } // end while member
1460 } // end while struct
1463 /* End of ToYacs Convertor for XMLImpl */
1465 //! FromYacs Convertor for XMLImpl
1467 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1470 struct convertFromYacsDouble<XMLImpl,std::string>
1472 static inline std::string convert(const TypeCode *t,double o)
1475 msg << "<value><double>" << setprecision(16) << o << "</double></value>\n";
1480 struct convertFromYacsInt<XMLImpl,std::string>
1482 static inline std::string convert(const TypeCode *t,long o)
1485 msg << "<value><int>" << o << "</int></value>\n";
1490 struct convertFromYacsString<XMLImpl,std::string>
1492 static inline std::string convert(const TypeCode *t,std::string& o)
1494 std::string msg="<value><string>";
1495 return msg+o+"</string></value>\n";
1499 struct convertFromYacsBool<XMLImpl,std::string>
1501 static inline std::string convert(const TypeCode *t,bool o)
1504 msg << "<value><boolean>" << o << "</boolean></value>\n";
1509 struct convertFromYacsObjref<XMLImpl,std::string>
1511 static inline std::string convert(const TypeCode *t,std::string& o)
1513 if(strncmp(t->id(),"python",6)==0 )
1514 return "<value><objref><![CDATA[" + ToBase64(o) + "]]></objref></value>\n";
1515 else if(strncmp(t->id(),"json",4)==0)
1516 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1518 return "<value><objref>" + o + "</objref></value>\n";
1523 struct convertFromYacsSequence<XMLImpl,std::string>
1525 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1527 std::vector<std::string>::const_iterator iter;
1529 xmlob << "<value><array><data>\n";
1530 for(iter=v.begin();iter!=v.end();iter++)
1534 xmlob << "</data></array></value>\n";
1535 DEBTRACE("Sequence= " << xmlob);
1540 struct convertFromYacsStruct<XMLImpl,std::string>
1542 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1544 std::string result="<value><struct>\n";
1545 std::map<std::string, std::string>::const_iterator pt;
1546 for(pt=m.begin();pt!=m.end();pt++)
1548 std::string name=(*pt).first;
1549 std::string item=(*pt).second;
1550 result=result+"<member>\n";
1551 result=result+"<name>"+name+"</name>\n";
1553 result=result+"</member>\n";
1555 result=result+"</struct></value>\n";
1560 /* End of FromYacs Convertor for XMLImpl */
1562 //! ToYacs Convertor for NEUTRALImpl
1564 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1565 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1567 template <ImplType IMPLOUT, class TOUT>
1568 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1570 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1572 if(o->getType()->kind()==Double)
1573 return o->getDoubleValue();
1574 else if(o->getType()->kind()==Int)
1575 return o->getIntValue();
1578 msg << "Problem in conversion: a double or int is expected " ;
1579 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1580 throw YACS::ENGINE::ConversionException(msg.str());
1583 template <ImplType IMPLOUT, class TOUT>
1584 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1586 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1588 if(o->getType()->kind()==Int)
1589 return o->getIntValue();
1591 msg << "Problem in conversion: a int is expected " ;
1592 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1593 throw YACS::ENGINE::ConversionException(msg.str());
1596 template <ImplType IMPLOUT, class TOUT>
1597 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1599 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1601 if(o->getType()->kind()==String)
1602 return o->getStringValue();
1604 msg << "Problem in conversion: a string is expected " ;
1605 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1606 throw YACS::ENGINE::ConversionException(msg.str());
1609 template <ImplType IMPLOUT, class TOUT>
1610 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1612 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1614 if(o->getType()->kind()==Bool)
1615 return o->getBoolValue();
1616 else if(o->getType()->kind()==Int)
1617 return o->getIntValue() != 0;
1619 msg << "Problem in conversion: a bool or int is expected " ;
1620 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1621 throw YACS::ENGINE::ConversionException(msg.str());
1624 template <ImplType IMPLOUT, class TOUT>
1625 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1627 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1629 if(o->getType()->kind()==String || o->getType()->kind()==Objref)
1630 return o->getStringValue();
1632 msg << "Problem in conversion: a objref(string) is expected " ;
1633 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1634 throw YACS::ENGINE::ConversionException(msg.str());
1637 template <ImplType IMPLOUT, class TOUT>
1638 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1640 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1642 SequenceAny* sdata= (SequenceAny*)o;
1643 int length=sdata->size();
1645 for(int i=0;i<length;i++)
1647 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1652 template <ImplType IMPLOUT, class TOUT>
1653 struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1655 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
1657 StructAny * sdata = dynamic_cast<StructAny *>(o);
1658 YASSERT(sdata != NULL);
1659 const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
1660 YASSERT(tst != NULL);
1661 for (int i=0 ; i<tst->memberCount() ; i++)
1663 string name = tst->memberName(i);
1664 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
1669 /* End of ToYacs Convertor for NEUTRALImpl */
1671 //! FromYacs Convertor for NEUTRALImpl
1673 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1676 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1678 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1680 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1685 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1687 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1689 return YACS::ENGINE::AtomAny::New((int)o);
1693 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1695 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1697 return YACS::ENGINE::AtomAny::New(o);
1701 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1703 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1705 return YACS::ENGINE::AtomAny::New(o);
1709 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1711 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1713 //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
1714 if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
1715 return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1717 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1718 CORBA::Object_var obref;
1721 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1723 catch(CORBA::Exception& ex)
1725 throw ConversionException("Can't get reference to object");
1727 if(obref->_non_existent())
1728 throw ConversionException("non_existent object");
1729 if( CORBA::is_nil(obref) )
1730 throw ConversionException("Can't get reference to object");
1731 if(!obref->_is_a(t->id()))
1734 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1735 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1736 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1737 throw YACS::ENGINE::ConversionException(msg.str());
1740 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
1741 if(!CORBA::is_nil(gobj))
1743 DEBTRACE("It's a SALOME::GenericObj register it");
1747 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
1749 return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1754 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1756 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1758 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1759 //Objref are managed as string within YACS::ENGINE::Any objs
1761 any=SequenceAny::New(t->contentType());
1762 for(iter=v.begin();iter!=v.end();iter++)
1764 any->pushBack(*iter);
1767 DEBTRACE( "refcnt: " << any->getRefCnt() );
1773 struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
1775 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
1777 StructAny * any = StructAny::New((TypeCodeStruct *)t);
1778 std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
1779 for (it=m.begin() ; it!=m.end() ; it++)
1781 any->setEltAtRank(it->first.c_str(), it->second);
1782 it->second->decrRef();
1787 /* End of FromYacs Convertor for NEUTRALImpl */
1789 //! ToYacs Convertor for CORBAImpl
1791 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1792 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1794 template <ImplType IMPLOUT, class TOUT>
1795 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1797 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1799 CORBA::TypeCode_var tc = o->type();
1800 if (tc->equivalent(CORBA::_tc_double))
1806 if (tc->equivalent(CORBA::_tc_long))
1813 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1814 msg << " : " << __FILE__ << ":" << __LINE__;
1815 throw YACS::ENGINE::ConversionException(msg.str());
1818 template <ImplType IMPLOUT, class TOUT>
1819 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1821 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1827 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1828 msg << " : " << __FILE__ << ":" << __LINE__;
1829 throw YACS::ENGINE::ConversionException(msg.str());
1832 template <ImplType IMPLOUT, class TOUT>
1833 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1835 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1841 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1842 msg << " : " << __FILE__ << ":" << __LINE__;
1843 throw YACS::ENGINE::ConversionException(msg.str());
1846 template <ImplType IMPLOUT, class TOUT>
1847 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1849 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1852 if(*o >>= CORBA::Any::to_boolean(b))
1855 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1856 msg << " : " << __FILE__ << ":" << __LINE__;
1857 throw YACS::ENGINE::ConversionException(msg.str());
1860 template <ImplType IMPLOUT, class TOUT>
1861 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1863 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1865 char file[]="/tmp/XXXXXX";
1866 if(t->isA(Runtime::_tc_file))
1868 Engines::Salome_file_ptr sf;
1870 Salome_file_i* f=new Salome_file_i();
1872 f->setDistributedFile(file);
1878 else if(strncmp(t->id(),"python",6)==0)
1881 Engines::fileBlock * buffer;
1884 s=(const char*)buffer->get_buffer();
1888 std::string mystr(s,buffer->length());
1892 PyGILState_STATE gstate = PyGILState_Ensure();
1893 PyObject* mod=PyImport_ImportModule("pickle");
1894 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",s,buffer->length());
1895 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
1896 DEBTRACE(PyObject_Repr(pickled));
1897 std::string mystr=PyBytes_AsString(pickled);
1901 PyGILState_Release(gstate);
1906 msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1907 msg << " : " << __FILE__ << ":" << __LINE__;
1908 throw YACS::ENGINE::ConversionException(msg.str());
1910 else if(strncmp(t->id(),"json",4)==0)
1918 msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1919 msg << " : " << __FILE__ << ":" << __LINE__;
1920 throw YACS::ENGINE::ConversionException(msg.str());
1924 CORBA::Object_var ObjRef ;
1925 *o >>= CORBA::Any::to_object(ObjRef) ;
1926 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1927 return (char *)objref;
1931 template <ImplType IMPLOUT, class TOUT>
1932 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1934 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1936 CORBA::TypeCode_var tc=o->type();
1937 if (tc->kind() != CORBA::tk_sequence)
1940 msg << "Not a sequence corba type " << tc->kind();
1941 msg << " : " << __FILE__ << ":" << __LINE__;
1942 throw YACS::ENGINE::ConversionException(msg.str());
1944 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1945 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1946 CORBA::release(dynany);
1947 DynamicAny::AnySeq_var as=ds->get_elements();
1948 int len=as->length();
1950 for(int i=0;i<len;i++)
1953 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1955 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1960 for(int i=0;i<len;i++)
1963 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1968 template <ImplType IMPLOUT, class TOUT>
1969 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1971 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1973 CORBA::TypeCode_var tc=o->type();
1974 DEBTRACE(tc->kind());
1975 if (tc->kind() != CORBA::tk_struct)
1978 msg << "Not a struct corba type " << tc->kind();
1979 msg << " : " << __FILE__ << ":" << __LINE__;
1980 throw YACS::ENGINE::ConversionException(msg.str());
1982 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1983 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1984 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1985 CORBA::release(dynany);
1986 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1987 int len=as->length();
1988 for(int i=0;i<len;i++)
1990 std::string name=as[i].id.in();
1992 CORBA::Any value=as[i].value;
1994 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1996 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
2003 /* End of ToYacs Convertor for CORBAImpl */
2005 //! FromYacs Convertor for CORBAImpl
2007 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
2010 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
2012 static inline CORBA::Any* convert(const TypeCode *t,double o)
2014 CORBA::Any *any = new CORBA::Any();
2015 *any <<= (CORBA::Double)o;
2020 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
2022 static inline CORBA::Any* convert(const TypeCode *t,long o)
2024 CORBA::Any *any = new CORBA::Any();
2025 *any <<= (CORBA::Long)o;
2030 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
2032 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2034 CORBA::Any *any = new CORBA::Any();
2040 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
2042 static inline CORBA::Any* convert(const TypeCode *t,bool o)
2044 CORBA::Any *any = new CORBA::Any();
2045 *any <<= CORBA::Any::from_boolean(o);
2050 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
2052 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2054 CORBA::Object_var obref;
2056 if(t->isA(Runtime::_tc_file))
2058 //It's an objref file. Convert it specially
2059 Salome_file_i* aSalome_file = new Salome_file_i();
2062 aSalome_file->setLocalFile(o.c_str());
2063 obref = aSalome_file->_this();
2064 aSalome_file->_remove_ref();
2066 catch (const SALOME::SALOME_Exception& e)
2069 msg << e.details.text;
2070 msg << " : " << __FILE__ << ":" << __LINE__;
2071 throw YACS::ENGINE::ConversionException(msg.str());
2074 else if(strncmp(t->id(),"python",6)==0 )
2076 CORBA::Any *any = new CORBA::Any();
2077 Engines::fileBlock * buffer=new Engines::fileBlock();
2078 buffer->length(o.length());
2079 CORBA::Octet *buf=buffer->get_buffer();
2080 memcpy(buf,o.c_str(),o.length());
2084 else if(strncmp(t->id(),"json",4)==0)
2086 CORBA::Any *any = new CORBA::Any();
2094 obref=getSALOMERuntime()->getFromNS(o.c_str());
2096 catch(CORBA::Exception& ex)
2098 throw ConversionException("Can't get reference to object");
2100 if( CORBA::is_nil(obref) )
2102 throw ConversionException("Can't get reference to object");
2106 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2108 CORBA::Any *any = new CORBA::Any();
2111 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2118 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
2120 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
2122 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2123 std::vector<CORBA::Any*>::const_iterator iter;
2125 // Build an Any from vector v
2127 if(t->contentType()->kind() == Objref)
2130 CORBA::TypeCode_var tc=getCorbaTC(t);
2132 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2133 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
2134 ds->set_length(v.size());
2136 for(iter=v.begin();iter!=v.end();iter++)
2138 DynamicAny::DynAny_var temp=ds->current_component();
2139 CORBA::Any* a=*iter;
2140 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2143 CORBA::Object_var zzobj ;
2144 *a >>= CORBA::Any::to_object(zzobj) ;
2145 temp->insert_reference(zzobj);
2150 //delete intermediate any
2155 CORBA::Any *any=ds->to_any();
2161 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2163 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2165 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2167 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2168 int nMember=tst->memberCount();
2169 DEBTRACE("nMember="<<nMember);
2171 CORBA::TypeCode_var tc=getCorbaTC(t);
2172 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2173 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2175 for(int i=0;i<nMember;i++)
2177 DynamicAny::DynAny_var temp=ds->current_component();
2178 const char * name=tst->memberName(i);
2179 DEBTRACE("Member name="<<name);
2180 //do not test member presence : test has been done in ToYacs convertor
2181 CORBA::Any* a=m[name];
2182 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2183 CORBA::TypeCode_var atc = tc->member_type(i);
2184 if(atc->kind()==CORBA::tk_objref)
2186 //special treatment for objref
2187 CORBA::Object_var zzobj ;
2188 *a >>= CORBA::Any::to_object(zzobj) ;
2189 temp->insert_reference(zzobj);
2195 //delete intermediate any
2199 CORBA::Any *any=ds->to_any();
2205 /* End of FromYacs Convertor for CORBAImpl */
2207 /* Some shortcuts for CORBA to CORBA conversion */
2209 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2211 CORBA::TypeCode_var tc = o->type();
2212 if (tc->equivalent(CORBA::_tc_double))
2216 if (tc->equivalent(CORBA::_tc_long))
2220 CORBA::Any *any = new CORBA::Any();
2221 *any <<= (CORBA::Double)d;
2225 msg << "Not a double or long corba type " << tc->kind();
2226 msg << " : " << __FILE__ << ":" << __LINE__;
2227 throw YACS::ENGINE::ConversionException(msg.str());
2230 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2235 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2240 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2245 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2250 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2254 /* End of shortcuts for CORBA to CORBA conversion */
2256 //! ToYacs Convertor for CPPImpl
2258 * This convertor converts Python object to YACS<TOUT> types
2259 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2261 template <ImplType IMPLOUT, class TOUT>
2262 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2264 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2266 if(intype->kind()==YACS::ENGINE::Double)
2270 else if(intype->kind()==YACS::ENGINE::Int)
2275 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2276 msg << " : " << __FILE__ << ":" << __LINE__;
2277 throw YACS::ENGINE::ConversionException(msg.str());
2280 template <ImplType IMPLOUT, class TOUT>
2281 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2283 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2285 if(intype->kind()==YACS::ENGINE::Int)
2290 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2291 msg << " : " << __FILE__ << ":" << __LINE__;
2292 throw YACS::ENGINE::ConversionException(msg.str());
2295 /* End of ToYacs Convertor for CPPImpl */
2297 //Python conversions
2298 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2300 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2302 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2304 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2306 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2308 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2310 PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2312 return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2315 std::string convertPyObjectToString(PyObject* ob)
2318 PyGILState_STATE gstate = PyGILState_Ensure();
2319 // TODO: separate treatment for string (maybe with bad encoding?) and other types of PyObject ?
2320 // Do we need str() or repr() and what are the possible causes of failure of str() ?
2321 // not clear, needs more analysis.
2323 if (s == NULL) // for instance string with bad encoding, non utf-8
2325 s=PyObject_ASCII(ob); // escape non ASCII characters and like repr(), which is not the same as str()...
2328 const char* characters = PyUnicode_AsUTF8AndSize(s, &size);
2329 std::string ss( characters, size);
2331 PyGILState_Release(gstate);
2336 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2338 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2340 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2342 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2344 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2346 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2348 PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2353 doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2356 std::stringstream msg;
2357 msg << "Problem in conversion: XML Document not parsed successfully ";
2358 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2359 throw YACS::ENGINE::ConversionException(msg.str());
2361 cur = xmlDocGetRootElement(doc);
2365 std::stringstream msg;
2366 msg << "Problem in conversion: empty XML Document";
2367 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2368 throw YACS::ENGINE::ConversionException(msg.str());
2372 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2374 ob=convertXmlPyObject(t,doc,cur);
2382 std::stringstream msg;
2383 msg << "Problem in conversion: incorrect XML value";
2384 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2385 throw YACS::ENGINE::ConversionException(msg.str());
2389 //NEUTRAL conversions
2390 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2392 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2394 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2396 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2398 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2400 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2402 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2409 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2411 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2413 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2415 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2417 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2419 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2421 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2423 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2426 //! Basic template checker from type TIN
2428 * This checker does nothing : throws exception
2429 * It must be partially specialize for a specific type (TIN)
2431 template <ImplType IMPLIN,class TIN,class TIN2>
2432 static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2435 msg << "Check not implemented for Implementation: " << IMPLIN ;
2436 msg << " : " << __FILE__ << ":" << __LINE__;
2437 throw YACS::ENGINE::ConversionException(msg.str());
2439 template <ImplType IMPLIN,class TIN,class TIN2>
2440 static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2443 msg << "Check not implemented for Implementation: " << IMPLIN ;
2444 msg << " : " << __FILE__ << ":" << __LINE__;
2445 throw YACS::ENGINE::ConversionException(msg.str());
2447 template <ImplType IMPLIN,class TIN,class TIN2>
2448 static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2451 msg << "Check not implemented for Implementation: " << IMPLIN ;
2452 msg << " : " << __FILE__ << ":" << __LINE__;
2453 throw YACS::ENGINE::ConversionException(msg.str());
2455 template <ImplType IMPLIN,class TIN,class TIN2>
2456 static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2459 msg << "Check not implemented for Implementation: " << IMPLIN ;
2460 msg << " : " << __FILE__ << ":" << __LINE__;
2461 throw YACS::ENGINE::ConversionException(msg.str());
2463 template <ImplType IMPLIN,class TIN,class TIN2>
2464 static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2467 msg << "Check not implemented for Implementation: " << IMPLIN ;
2468 msg << " : " << __FILE__ << ":" << __LINE__;
2469 throw YACS::ENGINE::ConversionException(msg.str());
2471 template <ImplType IMPLIN,class TIN,class TIN2>
2472 static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2475 msg << "Check not implemented for Implementation: " << IMPLIN ;
2476 msg << " : " << __FILE__ << ":" << __LINE__;
2477 throw YACS::ENGINE::ConversionException(msg.str());
2479 template <ImplType IMPLIN,class TIN,class TIN2>
2480 static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2483 msg << "Check not implemented for Implementation: " << IMPLIN ;
2484 msg << " : " << __FILE__ << ":" << __LINE__;
2485 throw YACS::ENGINE::ConversionException(msg.str());
2487 template <ImplType IMPLIN,class TIN,class TIN2>
2488 static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2491 msg << "Check not implemented for Implementation: " << IMPLIN ;
2492 msg << " : " << __FILE__ << ":" << __LINE__;
2493 throw YACS::ENGINE::ConversionException(msg.str());
2496 template <ImplType IMPLIN,class TIN,class TIN2>
2497 inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2503 return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2505 return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2507 return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2509 return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2511 return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2513 return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2515 return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2517 return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2522 msg << "Check not implemented for kind= " << tk ;
2523 msg << " : " << __FILE__ << ":" << __LINE__;
2524 throw YACS::ENGINE::ConversionException(msg.str());
2527 inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2529 if (PyFloat_Check(o))
2531 else if(PyLong_Check(o))
2536 msg << "Not a python double ";
2537 throw YACS::ENGINE::ConversionException(msg.str());
2541 inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2543 if (PyLong_Check(o))
2548 msg << "Not a python integer ";
2549 throw YACS::ENGINE::ConversionException(msg.str());
2553 inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2555 if (PyBool_Check(o))
2557 else if(PyLong_Check(o))
2562 msg << "Not a python boolean " ;
2563 throw YACS::ENGINE::ConversionException(msg.str());
2568 inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2570 if (PyUnicode_Check(o))
2575 msg << "Not a python string " ;
2576 throw YACS::ENGINE::ConversionException(msg.str());
2580 inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2582 if (PyUnicode_Check(o))
2584 if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2586 else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2588 // The python object should be json compliant (to improve)
2593 // The python object should be a CORBA obj (to improve)
2598 inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2600 if(!PySequence_Check(o))
2603 msg << "python object is not a sequence " ;
2604 throw YACS::ENGINE::ConversionException(msg.str());
2606 int length=PySequence_Size(o);
2607 for(int i=0;i<length;i++)
2609 PyObject *item=PySequence_ITEM(o,i);
2612 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2614 catch(ConversionException& ex)
2617 msg << ex.what() << " for sequence element " << i;
2618 throw YACS::ENGINE::ConversionException(msg.str(),false);
2625 inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2628 if(!PyDict_Check(o))
2631 msg << "python object is not a dict " ;
2632 throw YACS::ENGINE::ConversionException(msg.str());
2634 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2635 int nMember=tst->memberCount();
2636 for(int i=0;i<nMember;i++)
2638 std::string name=tst->memberName(i);
2639 TypeCode* tm=tst->memberType(i);
2640 value=PyDict_GetItemString(o, name.c_str());
2644 msg << "member " << name << " not present " ;
2645 throw YACS::ENGINE::ConversionException(msg.str());
2649 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2651 catch(ConversionException& ex)
2653 std::string s=" for struct member "+name;
2654 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2660 bool checkPyObject(const TypeCode *t,PyObject* ob)
2662 return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);