5 #define protected public
6 #include <omniORB4/CORBA.h>
7 #include <omniORB4/internal/typecode.h>
10 #include "TypeConversions.hxx"
11 #include "ConversionException.hxx"
12 #include "RuntimeSALOME.hxx"
13 #include "Salome_file_i.hxx"
14 #include "TypeCode.hxx"
16 #include "SALOME_GenericObj.hh"
22 #include "YacsTrace.hxx"
30 std::string getImplName(ImplType impl)
49 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
52 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
54 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
57 msg << "Conversion not implemented: kind= " << t->kind();
58 msg << " : " << __FILE__ << ":" << __LINE__;
59 throw YACS::ENGINE::ConversionException(msg.str());
62 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
64 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
67 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
69 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
72 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
74 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
77 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
79 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
82 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
84 DEBTRACE( t->name() << " " << t->shortName());
85 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
87 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
92 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
94 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
95 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
97 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
98 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
103 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
105 CORBA::StructMemberSeq mseq;
106 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
107 int nMember=tst->memberCount();
108 mseq.length(nMember);
109 for(int i=0;i<nMember;i++)
111 const char * name=tst->memberName(i);
112 TypeCode* tm=tst->memberType(i);
113 mseq[i].name=CORBA::string_dup(name);
114 mseq[i].type=getCorbaTC(tm);
116 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
118 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
123 getCorbaTCFn getCorbaTCFns[]=
136 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
139 return getCorbaTCFns[tk](t);
143 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
147 * Section that defines functions to check adaptation from one implementation to another
148 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
149 * can be converted to TypeCode t2 from implementation IMPLOUT
150 * IMPLIN is the implementation of an output port
151 * IMPLOUT is the implementation of an input port
152 * If the check is True, the input port can be adapted to the output port
155 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
157 template <ImplType IMPLIN,ImplType IMPLOUT>
158 struct isAdaptableDouble
160 static inline int apply(const TypeCode *t1,const TypeCode* t2)
162 if(t1->kind() == Double)return 1;
163 if(t1->kind() == Int)return 1;
167 template <ImplType IMPLIN,ImplType IMPLOUT>
168 struct isAdaptableInt
170 static inline int apply(const TypeCode *t1,const TypeCode* t2)
172 if(t1->kind() == Int)return 1;
176 template <ImplType IMPLIN,ImplType IMPLOUT>
177 struct isAdaptableString
179 static inline int apply(const TypeCode *t1,const TypeCode* t2)
181 if(t1->kind() == String)return 1;
185 template <ImplType IMPLIN,ImplType IMPLOUT>
186 struct isAdaptableBool
188 static inline int apply(const TypeCode *t1,const TypeCode* t2)
190 if(t1->kind() == Bool)return 1;
191 if(t1->kind() == Int)return 1;
195 template <ImplType IMPLIN,ImplType IMPLOUT>
196 struct isAdaptableObjref
198 static inline int apply(const TypeCode *t1,const TypeCode* t2)
200 if(t1->kind() == Objref)
202 //The inport type must be more general than outport type
203 if( t1->isA(t2->id()) )
209 template <ImplType IMPLIN,ImplType IMPLOUT>
210 struct isAdaptableSequence
212 static inline int apply(const TypeCode *t1,const TypeCode* t2)
214 if(t1->kind() == Sequence)
216 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
224 template <ImplType IMPLIN,ImplType IMPLOUT>
225 struct isAdaptableArray
227 static inline int apply(const TypeCode *t1,const TypeCode* t2)
232 template <ImplType IMPLIN,ImplType IMPLOUT>
233 struct isAdaptableStruct
235 static inline int apply(const TypeCode *t1,const TypeCode* t2)
237 if(t1->kind() == Struct)
247 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
248 * t1 is the IMPLIN output port type
249 * t2 is the IMPLOUT input port type
251 template <ImplType IMPLIN,ImplType IMPLOUT>
252 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
257 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
259 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
261 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
263 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
265 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
267 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
269 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
271 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
278 //xxx to Python adaptations
279 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
281 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
283 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
285 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
287 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
289 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
292 //xxx to Neutral adaptations
293 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
295 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
297 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
299 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
301 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
303 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
305 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
307 return isAdaptableNeutralCorba(t1, t2);
310 //xxx to XML adaptations
311 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
313 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
316 //xxx to Corba adaptations
317 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
319 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
321 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
323 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
325 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
327 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
329 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
331 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
334 //! Basic template convertor from type TIN to Yacs<TOUT> type
336 * This convertor does nothing : throws exception
337 * It must be partially specialize for a specific type (TIN)
339 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
340 struct convertToYacsDouble
342 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
345 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
346 msg << " : " << __FILE__ << ":" << __LINE__;
347 throw YACS::ENGINE::ConversionException(msg.str());
350 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
351 struct convertToYacsInt
353 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
356 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
357 msg << " : " << __FILE__ << ":" << __LINE__;
358 throw YACS::ENGINE::ConversionException(msg.str());
361 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
362 struct convertToYacsString
364 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
367 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
368 msg << " : " << __FILE__ << ":" << __LINE__;
369 throw YACS::ENGINE::ConversionException(msg.str());
372 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
373 struct convertToYacsBool
375 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
378 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
379 msg << " : " << __FILE__ << ":" << __LINE__;
380 throw YACS::ENGINE::ConversionException(msg.str());
383 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
384 struct convertToYacsObjref
386 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
389 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
390 msg << " : " << __FILE__ << ":" << __LINE__;
391 throw YACS::ENGINE::ConversionException(msg.str());
394 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
395 struct convertToYacsSequence
397 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
400 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
401 msg << " : " << __FILE__ << ":" << __LINE__;
402 throw YACS::ENGINE::ConversionException(msg.str());
405 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
406 struct convertToYacsArray
408 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
411 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
412 msg << " : " << __FILE__ << ":" << __LINE__;
413 throw YACS::ENGINE::ConversionException(msg.str());
416 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
417 struct convertToYacsStruct
419 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
422 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
423 msg << " : " << __FILE__ << ":" << __LINE__;
424 throw YACS::ENGINE::ConversionException(msg.str());
428 //! Basic convertor from Yacs<TOUT> type to full TOUT type
432 template <ImplType IMPLOUT, class TOUT>
433 struct convertFromYacsDouble
435 static inline TOUT convert(const TypeCode *t,double o)
438 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
439 msg << " : " << __FILE__ << ":" << __LINE__;
440 throw YACS::ENGINE::ConversionException(msg.str());
443 template <ImplType IMPLOUT, class TOUT>
444 struct convertFromYacsInt
446 static inline TOUT convert(const TypeCode *t,long o)
449 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
450 msg << " : " << __FILE__ << ":" << __LINE__;
451 throw YACS::ENGINE::ConversionException(msg.str());
454 template <ImplType IMPLOUT, class TOUT>
455 struct convertFromYacsString
457 static inline TOUT convert(const TypeCode *t,std::string o)
460 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
461 msg << " : " << __FILE__ << ":" << __LINE__;
462 throw YACS::ENGINE::ConversionException(msg.str());
465 template <ImplType IMPLOUT, class TOUT>
466 struct convertFromYacsBool
468 static inline TOUT convert(const TypeCode *t,bool o)
471 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
472 msg << " : " << __FILE__ << ":" << __LINE__;
473 throw YACS::ENGINE::ConversionException(msg.str());
476 template <ImplType IMPLOUT, class TOUT>
477 struct convertFromYacsObjref
479 static inline TOUT convert(const TypeCode *t,std::string o)
482 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
483 msg << " : " << __FILE__ << ":" << __LINE__;
484 throw YACS::ENGINE::ConversionException(msg.str());
487 template <ImplType IMPLOUT, class TOUT>
488 struct convertFromYacsSequence
490 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
493 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
494 msg << " : " << __FILE__ << ":" << __LINE__;
495 throw YACS::ENGINE::ConversionException(msg.str());
498 template <ImplType IMPLOUT, class TOUT>
499 struct convertFromYacsArray
501 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
504 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
505 msg << " : " << __FILE__ << ":" << __LINE__;
506 throw YACS::ENGINE::ConversionException(msg.str());
509 template <ImplType IMPLOUT, class TOUT>
510 struct convertFromYacsStruct
512 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
515 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
516 msg << " : " << __FILE__ << ":" << __LINE__;
517 throw YACS::ENGINE::ConversionException(msg.str());
520 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
521 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
523 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
525 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
528 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
529 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
531 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
533 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
536 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
537 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
539 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
541 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
544 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
545 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
547 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
549 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
552 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
553 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
555 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
557 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
561 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
562 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
565 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
566 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
569 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
570 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
573 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
574 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
577 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
578 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
580 std::map<std::string,TOUT> v;
581 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
582 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
586 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
587 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
593 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
595 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
597 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
599 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
601 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
603 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
605 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
607 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
612 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
613 msg << " : " << __FILE__ << ":" << __LINE__;
614 throw YACS::ENGINE::ConversionException(msg.str());
617 //! ToYacs Convertor for PYTHONImpl
619 * This convertor converts Python object to YACS<TOUT> types
620 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
622 template <ImplType IMPLOUT, class TOUT>
623 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
625 static inline double convert(const TypeCode *t,PyObject* o,void*)
628 if (PyFloat_Check(o))
629 x=PyFloat_AS_DOUBLE(o);
630 else if (PyInt_Check(o))
632 else if(PyLong_Check(o))
637 msg << "Not a python double. kind=" << t->kind() ;
638 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
639 throw YACS::ENGINE::ConversionException(msg.str());
644 template <ImplType IMPLOUT, class TOUT>
645 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
647 static inline long convert(const TypeCode *t,PyObject* o,void*)
652 else if(PyLong_Check(o))
657 msg << "Not a python integer. kind=" << t->kind() ;
658 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
659 throw YACS::ENGINE::ConversionException(msg.str());
664 template <ImplType IMPLOUT, class TOUT>
665 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
667 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
669 if (PyString_Check(o))
670 return PyString_AS_STRING(o);
674 msg << "Not a python string. kind=" << t->kind() ;
675 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
676 throw YACS::ENGINE::ConversionException(msg.str());
680 template <ImplType IMPLOUT, class TOUT>
681 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
683 static inline bool convert(const TypeCode *t,PyObject* o,void*)
688 else if (PyInt_Check(o))
689 l=(PyInt_AS_LONG(o)!=0);
690 else if(PyLong_Check(o))
691 l=(PyLong_AsLong(o)!=0);
695 msg << "Not a python boolean. kind=" << t->kind() ;
696 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
697 throw YACS::ENGINE::ConversionException(msg.str());
702 template <ImplType IMPLOUT, class TOUT>
703 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
705 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
707 if (PyString_Check(o))
709 // the objref is used by Python as a string (prefix:value) keep it as a string
710 return PyString_AS_STRING(o);
712 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"object_to_string","O",o);
716 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
718 std::string mystr=PyString_AsString(pystring);
723 template <ImplType IMPLOUT, class TOUT>
724 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
726 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
728 if(!PySequence_Check(o))
731 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
732 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
733 throw YACS::ENGINE::ConversionException(msg.str());
735 int length=PySequence_Size(o);
736 DEBTRACE("length: " << length );
738 for(int i=0;i<length;i++)
740 PyObject *item=PySequence_ITEM(o,i);
742 std::cerr <<"item[" << i << "]=";
743 PyObject_Print(item,stderr,Py_PRINT_RAW);
744 std::cerr << std::endl;
746 DEBTRACE( "item refcnt: " << item->ob_refcnt );
747 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
753 template <ImplType IMPLOUT, class TOUT>
754 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
756 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
758 DEBTRACE( "o refcnt: " << o->ob_refcnt );
759 PyObject *key, *value;
760 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
761 int nMember=tst->memberCount();
762 DEBTRACE("nMember="<<nMember);
763 for(int i=0;i<nMember;i++)
765 std::string name=tst->memberName(i);
766 DEBTRACE("Member name="<<name);
767 TypeCode* tm=tst->memberType(i);
768 value=PyDict_GetItemString(o, name.c_str());
771 //member name not present
772 //TODO delete all allocated objects in m
774 PyObject_Print(o,stderr,Py_PRINT_RAW);
775 std::cerr << std::endl;
778 msg << "Problem in conversion: member " << name << " not present " << endl;
779 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
780 throw YACS::ENGINE::ConversionException(msg.str());
782 DEBTRACE( "value refcnt: " << value->ob_refcnt );
783 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
788 /* End of ToYacs Convertor for PYTHONImpl */
790 //! FromYacs Convertor for PYTHONImpl
792 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
795 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
797 static inline PyObject* convert(const TypeCode *t,double o)
799 PyObject *pyob=PyFloat_FromDouble(o);
804 struct convertFromYacsInt<PYTHONImpl,PyObject*>
806 static inline PyObject* convert(const TypeCode *t,long o)
808 PyObject *pyob=PyLong_FromLong(o);
813 struct convertFromYacsString<PYTHONImpl,PyObject*>
815 static inline PyObject* convert(const TypeCode *t,std::string& o)
817 return PyString_FromString(o.c_str());
821 struct convertFromYacsBool<PYTHONImpl,PyObject*>
823 static inline PyObject* convert(const TypeCode *t,bool o)
825 return PyBool_FromLong ((long)o);
829 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
831 static inline PyObject* convert(const TypeCode *t,std::string& o)
833 if(t->isA(Runtime::_tc_file))
835 //It's an objref file. Convert it specially
836 return PyString_FromString(o.c_str());
839 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
840 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
844 //Objref CORBA. prefix=IOR,corbaname,corbaloc
845 CORBA::Object_var obref;
848 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
850 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
853 catch(CORBA::Exception& ex)
855 DEBTRACE( "Can't get reference to object." );
856 throw ConversionException("Can't get reference to object");
859 if( CORBA::is_nil(obref) )
861 DEBTRACE( "Can't get reference to object (or it was nil)." );
862 throw ConversionException("Can't get reference to object");
865 if(!obref->_is_a(t->id()))
868 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
869 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
870 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
871 throw YACS::ENGINE::ConversionException(msg.str());
874 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
877 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
878 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
881 //hold_lock is true: caller is supposed to hold the GIL.
882 //omniorb will not take the GIL
883 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
886 PyObject_Print(ob,stderr,Py_PRINT_RAW);
887 std::cerr << std::endl;
888 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
889 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
890 std::cerr << std::endl;
893 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
894 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
896 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), "narrow", "Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
898 PyErr_Clear();//Exception during narrow. Keep ob
899 else if(result==Py_None)
900 Py_DECREF(result); //Can't narrow. Keep ob
903 //Can narrow. Keep result
905 PyObject_Print(result,stderr,Py_PRINT_RAW);
906 std::cerr << std::endl;
914 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
921 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
923 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
925 std::vector<PyObject*>::const_iterator iter;
926 PyObject *pyob = PyList_New(v.size());
928 for(iter=v.begin();iter!=v.end();iter++)
930 PyObject* item=*iter;
931 DEBTRACE( "item refcnt: " << item->ob_refcnt );
932 PyList_SetItem(pyob,i,item);
933 DEBTRACE( "item refcnt: " << item->ob_refcnt );
940 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
942 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
944 PyObject *pyob = PyDict_New();
945 std::map<std::string, PyObject*>::const_iterator pt;
946 for(pt=m.begin();pt!=m.end();pt++)
948 std::string name=(*pt).first;
949 PyObject* item=(*pt).second;
950 DEBTRACE( "item refcnt: " << item->ob_refcnt );
951 PyDict_SetItemString(pyob,name.c_str(),item);
953 DEBTRACE( "item refcnt: " << item->ob_refcnt );
955 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
959 /* End of FromYacs Convertor for PYTHONImpl */
961 //! ToYacs Convertor for XMLImpl
963 * Partial specialization for XML implementation (XMLImpl)
964 * This convertor converts xml object to YACS<TOUT> types
966 template <ImplType IMPLOUT, class TOUT>
967 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
969 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
972 cur = cur->xmlChildrenNode;
975 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
977 //wait a double, got a double
979 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
982 DEBTRACE( "convertToYacsDouble " << (const char *)s );
983 d=Cstr2d((const char *)s);
988 DEBTRACE("############### workaround to improve...");
992 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
994 //wait a double, got an int
996 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
999 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1000 d=Cstr2d((const char *)s);
1005 DEBTRACE("############### workaround to improve...");
1012 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1013 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1014 throw YACS::ENGINE::ConversionException(msg.str());
1017 template <ImplType IMPLOUT, class TOUT>
1018 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1020 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1023 cur = cur->xmlChildrenNode;
1026 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1029 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1032 DEBTRACE( "convertToYacsInt " << (const char *)s );
1033 d=atol((const char *)s);
1038 DEBTRACE("############### workaround to improve...");
1045 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1046 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1047 throw YACS::ENGINE::ConversionException(msg.str());
1050 template <ImplType IMPLOUT, class TOUT>
1051 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1053 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1055 cur = cur->xmlChildrenNode;
1058 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1060 //wait a string, got a string
1062 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1064 DEBTRACE( "convertToYacsString " << (const char *)s );
1065 std::string mystr=std::string((const char *)s);
1072 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1073 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1074 throw YACS::ENGINE::ConversionException(msg.str());
1077 template <ImplType IMPLOUT, class TOUT>
1078 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1080 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1082 cur = cur->xmlChildrenNode;
1085 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1087 //wait a boolean, got a boolean
1089 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1093 DEBTRACE( "convertToYacsBool " << (const char *)s );
1094 ob=atoi((const char*)s)!=0;
1099 DEBTRACE("############### workaround to improve...");
1106 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1107 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1108 throw YACS::ENGINE::ConversionException(msg.str());
1111 template <ImplType IMPLOUT, class TOUT>
1112 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1114 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1116 cur = cur->xmlChildrenNode;
1119 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1121 //we wait a objref, we have got a objref
1123 std::string mystr = "";
1124 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1127 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1128 mystr = (const char *)s;
1133 DEBTRACE("############### workaround to improve...");
1140 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1141 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1142 throw YACS::ENGINE::ConversionException(msg.str());
1145 template <ImplType IMPLOUT, class TOUT>
1146 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1148 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1150 cur = cur->xmlChildrenNode;
1153 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1155 DEBTRACE( "parse sequence " );
1156 xmlNodePtr cur1=cur->xmlChildrenNode;
1157 while (cur1 != NULL)
1159 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1161 DEBTRACE( "parse data " );
1162 xmlNodePtr cur2=cur1->xmlChildrenNode;
1163 while (cur2 != NULL)
1165 //collect all values
1166 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1168 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1172 } // end while value
1180 } // end while array
1183 template <ImplType IMPLOUT, class TOUT>
1184 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1186 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1188 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1189 int nMember=tst->memberCount();
1190 DEBTRACE("nMember="<<nMember);
1191 std::map<std::string,TypeCode*> mtc;
1192 for(int i=0;i<nMember;i++)
1194 mtc[tst->memberName(i)]=tst->memberType(i);
1197 cur = cur->xmlChildrenNode;
1200 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1202 DEBTRACE( "parse struct " );
1203 xmlNodePtr cur1=cur->xmlChildrenNode;
1204 while (cur1 != NULL)
1206 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1208 DEBTRACE( "parse member " );
1209 xmlNodePtr cur2=cur1->xmlChildrenNode;
1210 while (cur2 != NULL)
1213 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1216 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1217 std::string name= (char *)s;
1219 while (cur2 != NULL)
1221 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1223 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1233 } // end while member/value
1236 } // end while member
1240 } // end while struct
1243 /* End of ToYacs Convertor for XMLImpl */
1245 //! FromYacs Convertor for XMLImpl
1247 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1250 struct convertFromYacsDouble<XMLImpl,std::string>
1252 static inline std::string convert(const TypeCode *t,double o)
1255 msg << "<value><double>" << o << "</double></value>\n";
1260 struct convertFromYacsInt<XMLImpl,std::string>
1262 static inline std::string convert(const TypeCode *t,long o)
1265 msg << "<value><int>" << o << "</int></value>\n";
1270 struct convertFromYacsString<XMLImpl,std::string>
1272 static inline std::string convert(const TypeCode *t,std::string& o)
1274 std::string msg="<value><string>";
1275 return msg+o+"</string></value>\n";
1279 struct convertFromYacsBool<XMLImpl,std::string>
1281 static inline std::string convert(const TypeCode *t,bool o)
1284 msg << "<value><boolean>" << o << "</boolean></value>\n";
1289 struct convertFromYacsObjref<XMLImpl,std::string>
1291 static inline std::string convert(const TypeCode *t,std::string& o)
1293 return "<value><objref>" + o + "</objref></value>\n";
1298 struct convertFromYacsSequence<XMLImpl,std::string>
1300 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1302 std::vector<std::string>::const_iterator iter;
1304 xmlob << "<value><array><data>\n";
1305 for(iter=v.begin();iter!=v.end();iter++)
1309 xmlob << "</data></array></value>\n";
1310 DEBTRACE("Sequence= " << xmlob);
1315 struct convertFromYacsStruct<XMLImpl,std::string>
1317 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1319 std::string result="<value><struct>\n";
1320 std::map<std::string, std::string>::const_iterator pt;
1321 for(pt=m.begin();pt!=m.end();pt++)
1323 std::string name=(*pt).first;
1324 std::string item=(*pt).second;
1325 result=result+"<member>\n";
1326 result=result+"<name>"+name+"</name>\n";
1328 result=result+"</member>\n";
1330 result=result+"</struct></value>\n";
1335 /* End of FromYacs Convertor for XMLImpl */
1337 //! ToYacs Convertor for NEUTRALImpl
1339 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1340 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1342 template <ImplType IMPLOUT, class TOUT>
1343 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1345 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1347 if(o->getType()->kind()==Double)
1348 return o->getDoubleValue();
1349 else if(o->getType()->kind()==Int)
1350 return o->getIntValue();
1353 msg << "Problem in conversion: a double or int is expected " ;
1354 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1355 throw YACS::ENGINE::ConversionException(msg.str());
1358 template <ImplType IMPLOUT, class TOUT>
1359 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1361 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1363 if(o->getType()->kind()==Int)
1364 return o->getIntValue();
1366 msg << "Problem in conversion: a int is expected " ;
1367 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1368 throw YACS::ENGINE::ConversionException(msg.str());
1371 template <ImplType IMPLOUT, class TOUT>
1372 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1374 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1376 if(o->getType()->kind()==String)
1377 return o->getStringValue();
1379 msg << "Problem in conversion: a string is expected " ;
1380 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1381 throw YACS::ENGINE::ConversionException(msg.str());
1384 template <ImplType IMPLOUT, class TOUT>
1385 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1387 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1389 if(o->getType()->kind()==Bool)
1390 return o->getBoolValue();
1391 else if(o->getType()->kind()==Int)
1392 return o->getIntValue() != 0;
1394 msg << "Problem in conversion: a bool or int is expected " ;
1395 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1396 throw YACS::ENGINE::ConversionException(msg.str());
1399 template <ImplType IMPLOUT, class TOUT>
1400 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1402 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1404 if(o->getType()->kind()==String)
1405 return o->getStringValue();
1407 msg << "Problem in conversion: a objref(string) is expected " ;
1408 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1409 throw YACS::ENGINE::ConversionException(msg.str());
1412 template <ImplType IMPLOUT, class TOUT>
1413 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1415 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1417 SequenceAny* sdata= (SequenceAny*)o;
1418 int length=sdata->size();
1420 for(int i=0;i<length;i++)
1422 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1427 /* End of ToYacs Convertor for NEUTRALImpl */
1429 //! FromYacs Convertor for NEUTRALImpl
1431 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1434 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1436 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1438 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1443 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1445 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1447 return YACS::ENGINE::AtomAny::New((int)o);
1451 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1453 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1455 return YACS::ENGINE::AtomAny::New(o);
1459 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1461 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1463 return YACS::ENGINE::AtomAny::New(o);
1467 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1469 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1471 return YACS::ENGINE::AtomAny::New(o);
1476 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1478 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1480 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1481 //Objref are managed as string within YACS::ENGINE::Any objs
1483 any=SequenceAny::New(t->contentType());
1484 for(iter=v.begin();iter!=v.end();iter++)
1486 any->pushBack(*iter);
1489 DEBTRACE( "refcnt: " << any->getRefCnt() );
1493 /* End of FromYacs Convertor for NEUTRALImpl */
1495 //! ToYacs Convertor for CORBAImpl
1497 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1498 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1500 template <ImplType IMPLOUT, class TOUT>
1501 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1503 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1505 CORBA::TypeCode_var tc = o->type();
1506 if (tc->equivalent(CORBA::_tc_double))
1512 if (tc->equivalent(CORBA::_tc_long))
1519 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1520 msg << " : " << __FILE__ << ":" << __LINE__;
1521 throw YACS::ENGINE::ConversionException(msg.str());
1524 template <ImplType IMPLOUT, class TOUT>
1525 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1527 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1533 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1534 msg << " : " << __FILE__ << ":" << __LINE__;
1535 throw YACS::ENGINE::ConversionException(msg.str());
1538 template <ImplType IMPLOUT, class TOUT>
1539 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1541 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1547 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1548 msg << " : " << __FILE__ << ":" << __LINE__;
1549 throw YACS::ENGINE::ConversionException(msg.str());
1552 template <ImplType IMPLOUT, class TOUT>
1553 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1555 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1558 if(*o >>= CORBA::Any::to_boolean(b))
1561 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1562 msg << " : " << __FILE__ << ":" << __LINE__;
1563 throw YACS::ENGINE::ConversionException(msg.str());
1566 template <ImplType IMPLOUT, class TOUT>
1567 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1569 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1571 char file[]="/tmp/XXXXXX";
1572 if(t->isA(Runtime::_tc_file))
1574 Engines::Salome_file_ptr sf;
1576 Salome_file_i* f=new Salome_file_i();
1578 f->setDistributedFile(file);
1586 CORBA::Object_var ObjRef ;
1587 *o >>= CORBA::Any::to_object(ObjRef) ;
1588 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1589 return (char *)objref;
1593 template <ImplType IMPLOUT, class TOUT>
1594 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1596 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1598 CORBA::TypeCode_var tc=o->type();
1599 if (tc->kind() != CORBA::tk_sequence)
1602 msg << "Not a sequence corba type " << tc->kind();
1603 msg << " : " << __FILE__ << ":" << __LINE__;
1604 throw YACS::ENGINE::ConversionException(msg.str());
1606 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1607 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1608 CORBA::release(dynany);
1609 DynamicAny::AnySeq_var as=ds->get_elements();
1610 int len=as->length();
1612 for(int i=0;i<len;i++)
1615 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1617 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1622 for(int i=0;i<len;i++)
1625 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1630 template <ImplType IMPLOUT, class TOUT>
1631 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1633 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1635 CORBA::TypeCode_var tc=o->type();
1636 DEBTRACE(tc->kind());
1637 if (tc->kind() != CORBA::tk_struct)
1640 msg << "Not a struct corba type " << tc->kind();
1641 msg << " : " << __FILE__ << ":" << __LINE__;
1642 throw YACS::ENGINE::ConversionException(msg.str());
1644 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1645 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1646 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1647 CORBA::release(dynany);
1648 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1649 int len=as->length();
1650 for(int i=0;i<len;i++)
1652 std::string name=as[i].id.in();
1654 CORBA::Any value=as[i].value;
1656 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1658 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1665 /* End of ToYacs Convertor for CORBAImpl */
1667 //! FromYacs Convertor for CORBAImpl
1669 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1672 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1674 static inline CORBA::Any* convert(const TypeCode *t,double o)
1676 CORBA::Any *any = new CORBA::Any();
1682 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
1684 static inline CORBA::Any* convert(const TypeCode *t,long o)
1686 CORBA::Any *any = new CORBA::Any();
1692 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
1694 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1696 CORBA::Any *any = new CORBA::Any();
1702 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
1704 static inline CORBA::Any* convert(const TypeCode *t,bool o)
1706 CORBA::Any *any = new CORBA::Any();
1707 *any <<= CORBA::Any::from_boolean(o);
1712 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
1714 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1716 CORBA::Object_var obref;
1717 if(t->isA(Runtime::_tc_file))
1719 //It's an objref file. Convert it specially
1720 Salome_file_i* aSalome_file = new Salome_file_i();
1723 aSalome_file->setLocalFile(o.c_str());
1724 obref = aSalome_file->_this();
1725 aSalome_file->_remove_ref();
1727 catch (const SALOME::SALOME_Exception& e)
1730 msg << e.details.text;
1731 msg << " : " << __FILE__ << ":" << __LINE__;
1732 throw YACS::ENGINE::ConversionException(msg.str());
1739 obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1741 catch(CORBA::Exception& ex)
1743 throw ConversionException("Can't get reference to object");
1745 if( CORBA::is_nil(obref) )
1747 throw ConversionException("Can't get reference to object");
1751 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1753 CORBA::Any *any = new CORBA::Any();
1756 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1763 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
1765 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
1767 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
1768 std::vector<CORBA::Any*>::const_iterator iter;
1770 // Build an Any from vector v
1772 if(t->contentType()->kind() == Objref)
1775 CORBA::TypeCode_var tc=getCorbaTC(t);
1777 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
1778 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
1779 ds->set_length(v.size());
1781 for(iter=v.begin();iter!=v.end();iter++)
1783 DynamicAny::DynAny_var temp=ds->current_component();
1784 CORBA::Any* a=*iter;
1787 CORBA::Object_var zzobj ;
1788 *a >>= CORBA::Any::to_object(zzobj) ;
1789 temp->insert_reference(zzobj);
1794 //delete intermediate any
1799 CORBA::Any *any=ds->to_any();
1805 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
1807 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
1809 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
1811 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1812 int nMember=tst->memberCount();
1813 DEBTRACE("nMember="<<nMember);
1815 CORBA::StructMemberSeq mseq;
1816 mseq.length(nMember);
1817 for(int i=0;i<nMember;i++)
1819 const char * name=tst->memberName(i);
1820 if(m.count(name) !=0)
1822 mseq[i].type=m[name]->type();
1825 CORBA::TypeCode_var tc= orb->create_struct_tc("","",mseq);
1826 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
1827 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
1829 for(int i=0;i<nMember;i++)
1831 DynamicAny::DynAny_var temp=ds->current_component();
1832 const char * name=tst->memberName(i);
1833 DEBTRACE("Member name="<<name);
1834 //do not test member presence : test has been done in ToYacs convertor
1835 CORBA::Any* a=m[name];
1837 //delete intermediate any
1841 CORBA::Any *any=ds->to_any();
1847 /* End of FromYacs Convertor for CORBAImpl */
1849 /* Some shortcuts for CORBA to CORBA conversion */
1851 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1853 CORBA::TypeCode_var tc = o->type();
1854 if (tc->equivalent(CORBA::_tc_double))
1858 if (tc->equivalent(CORBA::_tc_long))
1862 CORBA::Any *any = new CORBA::Any();
1863 *any <<= (CORBA::Double)d;
1867 msg << "Not a double or long corba type " << tc->kind();
1868 msg << " : " << __FILE__ << ":" << __LINE__;
1869 throw YACS::ENGINE::ConversionException(msg.str());
1872 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1877 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1882 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1887 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1892 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1896 /* End of shortcuts for CORBA to CORBA conversion */
1898 //! ToYacs Convertor for CPPImpl
1900 * This convertor converts Python object to YACS<TOUT> types
1901 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
1903 template <ImplType IMPLOUT, class TOUT>
1904 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
1906 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
1908 if(intype->kind()==YACS::ENGINE::Double)
1912 else if(intype->kind()==YACS::ENGINE::Int)
1917 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
1918 msg << " : " << __FILE__ << ":" << __LINE__;
1919 throw YACS::ENGINE::ConversionException(msg.str());
1922 template <ImplType IMPLOUT, class TOUT>
1923 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
1925 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
1927 if(intype->kind()==YACS::ENGINE::Int)
1932 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
1933 msg << " : " << __FILE__ << ":" << __LINE__;
1934 throw YACS::ENGINE::ConversionException(msg.str());
1937 /* End of ToYacs Convertor for CPPImpl */
1939 //Python conversions
1940 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
1942 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
1944 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
1946 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
1948 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
1950 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
1954 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1956 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
1958 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1960 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
1962 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1964 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
1966 //NEUTRAL conversions
1967 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
1969 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
1971 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
1973 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
1975 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
1977 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
1979 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
1986 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
1988 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
1990 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
1992 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
1994 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
1996 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
1998 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2000 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);