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 "TypeCode.hxx"
20 #include "YacsTrace.hxx"
29 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
32 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
34 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
37 msg << "Conversion not implemented: kind= " << t->kind();
38 msg << " : " << __FILE__ << ":" << __LINE__;
39 throw YACS::ENGINE::ConversionException(msg.str());
42 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
44 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
47 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
49 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
52 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
54 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
57 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
59 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
62 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
64 DEBTRACE( t->name() << " " << t->shortName());
65 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
67 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
72 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
74 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
75 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
77 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
78 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
83 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
85 CORBA::StructMemberSeq mseq;
86 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
87 int nMember=tst->memberCount();
89 for(int i=0;i<nMember;i++)
91 const char * name=tst->memberName(i);
92 TypeCode* tm=tst->memberType(i);
93 mseq[i].name=CORBA::string_dup(name);
94 mseq[i].type=getCorbaTC(tm);
96 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
98 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
103 getCorbaTCFn getCorbaTCFns[]=
116 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
119 return getCorbaTCFns[tk](t);
123 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
127 * Section that defines functions to check adaptation from one implementation to another
128 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
129 * can be converted to TypeCode t2 from implementation IMPLOUT
130 * IMPLIN is the implementation of an output port
131 * IMPLOUT is the implementation of an input port
132 * If the check is True, the input port can be adapted to the output port
135 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
137 template <ImplType IMPLIN,ImplType IMPLOUT>
138 struct isAdaptableDouble
140 static inline int apply(const TypeCode *t1,const TypeCode* t2)
142 if(t1->kind() == Double)return 1;
143 if(t1->kind() == Int)return 1;
147 template <ImplType IMPLIN,ImplType IMPLOUT>
148 struct isAdaptableInt
150 static inline int apply(const TypeCode *t1,const TypeCode* t2)
152 if(t1->kind() == Int)return 1;
156 template <ImplType IMPLIN,ImplType IMPLOUT>
157 struct isAdaptableString
159 static inline int apply(const TypeCode *t1,const TypeCode* t2)
161 if(t1->kind() == String)return 1;
165 template <ImplType IMPLIN,ImplType IMPLOUT>
166 struct isAdaptableBool
168 static inline int apply(const TypeCode *t1,const TypeCode* t2)
170 if(t1->kind() == Bool)return 1;
171 if(t1->kind() == Int)return 1;
175 template <ImplType IMPLIN,ImplType IMPLOUT>
176 struct isAdaptableObjref
178 static inline int apply(const TypeCode *t1,const TypeCode* t2)
180 if(t1->kind() == Objref)
182 //The inport type must be more general than outport type
183 if( t1->isA(t2->id()) )
189 template <ImplType IMPLIN,ImplType IMPLOUT>
190 struct isAdaptableSequence
192 static inline int apply(const TypeCode *t1,const TypeCode* t2)
194 if(t1->kind() == Sequence)
196 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
204 template <ImplType IMPLIN,ImplType IMPLOUT>
205 struct isAdaptableArray
207 static inline int apply(const TypeCode *t1,const TypeCode* t2)
212 template <ImplType IMPLIN,ImplType IMPLOUT>
213 struct isAdaptableStruct
215 static inline int apply(const TypeCode *t1,const TypeCode* t2)
217 if(t1->kind() == Struct)
219 if( t1->isA(t2->id()) )
227 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
228 * t1 is the IMPLIN output port type
229 * t2 is the IMPLOUT input port type
231 template <ImplType IMPLIN,ImplType IMPLOUT>
232 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
237 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
239 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
241 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
243 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
245 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
247 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
249 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
251 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
258 //xxx to Python adaptations
259 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
261 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
263 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
265 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
267 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
269 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
272 //xxx to Neutral adaptations
273 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
275 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
277 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
279 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
281 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
283 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
285 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
287 return isAdaptableNeutralCorba(t1, t2);
290 //xxx to XML adaptations
291 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
293 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
296 //xxx to Corba adaptations
297 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
299 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
301 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
303 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
305 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
307 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
309 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
311 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
314 //! Basic template convertor from type TIN to Yacs<TOUT> type
316 * This convertor does nothing : throws exception
317 * It must be partially specialize for a specific type (TIN)
319 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
320 struct convertToYacsDouble
322 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
325 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
326 msg << " : " << __FILE__ << ":" << __LINE__;
327 throw YACS::ENGINE::ConversionException(msg.str());
330 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
331 struct convertToYacsInt
333 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
336 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
337 msg << " : " << __FILE__ << ":" << __LINE__;
338 throw YACS::ENGINE::ConversionException(msg.str());
341 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
342 struct convertToYacsString
344 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
347 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
348 msg << " : " << __FILE__ << ":" << __LINE__;
349 throw YACS::ENGINE::ConversionException(msg.str());
352 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
353 struct convertToYacsBool
355 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
358 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
359 msg << " : " << __FILE__ << ":" << __LINE__;
360 throw YACS::ENGINE::ConversionException(msg.str());
363 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
364 struct convertToYacsObjref
366 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
369 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
370 msg << " : " << __FILE__ << ":" << __LINE__;
371 throw YACS::ENGINE::ConversionException(msg.str());
374 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
375 struct convertToYacsSequence
377 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
380 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
381 msg << " : " << __FILE__ << ":" << __LINE__;
382 throw YACS::ENGINE::ConversionException(msg.str());
385 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
386 struct convertToYacsArray
388 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
391 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
392 msg << " : " << __FILE__ << ":" << __LINE__;
393 throw YACS::ENGINE::ConversionException(msg.str());
396 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
397 struct convertToYacsStruct
399 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
402 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
403 msg << " : " << __FILE__ << ":" << __LINE__;
404 throw YACS::ENGINE::ConversionException(msg.str());
408 //! Basic convertor from Yacs<TOUT> type to full TOUT type
412 template <ImplType IMPLOUT, class TOUT>
413 struct convertFromYacsDouble
415 static inline TOUT convert(const TypeCode *t,double o)
418 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
419 msg << " : " << __FILE__ << ":" << __LINE__;
420 throw YACS::ENGINE::ConversionException(msg.str());
423 template <ImplType IMPLOUT, class TOUT>
424 struct convertFromYacsInt
426 static inline TOUT convert(const TypeCode *t,long o)
429 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
430 msg << " : " << __FILE__ << ":" << __LINE__;
431 throw YACS::ENGINE::ConversionException(msg.str());
434 template <ImplType IMPLOUT, class TOUT>
435 struct convertFromYacsString
437 static inline TOUT convert(const TypeCode *t,std::string o)
440 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
441 msg << " : " << __FILE__ << ":" << __LINE__;
442 throw YACS::ENGINE::ConversionException(msg.str());
445 template <ImplType IMPLOUT, class TOUT>
446 struct convertFromYacsBool
448 static inline TOUT convert(const TypeCode *t,bool o)
451 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
452 msg << " : " << __FILE__ << ":" << __LINE__;
453 throw YACS::ENGINE::ConversionException(msg.str());
456 template <ImplType IMPLOUT, class TOUT>
457 struct convertFromYacsObjref
459 static inline TOUT convert(const TypeCode *t,std::string o)
462 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
463 msg << " : " << __FILE__ << ":" << __LINE__;
464 throw YACS::ENGINE::ConversionException(msg.str());
467 template <ImplType IMPLOUT, class TOUT>
468 struct convertFromYacsSequence
470 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
473 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
474 msg << " : " << __FILE__ << ":" << __LINE__;
475 throw YACS::ENGINE::ConversionException(msg.str());
478 template <ImplType IMPLOUT, class TOUT>
479 struct convertFromYacsArray
481 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
484 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
485 msg << " : " << __FILE__ << ":" << __LINE__;
486 throw YACS::ENGINE::ConversionException(msg.str());
489 template <ImplType IMPLOUT, class TOUT>
490 struct convertFromYacsStruct
492 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
495 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
496 msg << " : " << __FILE__ << ":" << __LINE__;
497 throw YACS::ENGINE::ConversionException(msg.str());
500 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
501 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
503 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
505 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
508 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
509 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
511 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
513 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
516 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
517 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
519 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
521 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
524 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
525 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
527 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
529 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
532 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
533 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
535 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
537 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
541 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
542 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
545 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
546 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
549 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
550 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
553 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
554 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
557 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
558 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
560 std::map<std::string,TOUT> v;
561 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
562 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
566 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
567 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
573 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
575 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
577 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
579 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
581 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
583 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
585 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
587 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
592 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
593 msg << " : " << __FILE__ << ":" << __LINE__;
594 throw YACS::ENGINE::ConversionException(msg.str());
597 //! ToYacs Convertor for PYTHONImpl
599 * This convertor converts Python object to YACS<TOUT> types
600 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
602 template <ImplType IMPLOUT, class TOUT>
603 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
605 static inline double convert(const TypeCode *t,PyObject* o,void*)
608 if (PyFloat_Check(o))
609 x=PyFloat_AS_DOUBLE(o);
610 else if (PyInt_Check(o))
612 else if(PyLong_Check(o))
617 msg << "Not a python double. kind=" << t->kind() ;
618 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
619 throw YACS::ENGINE::ConversionException(msg.str());
624 template <ImplType IMPLOUT, class TOUT>
625 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
627 static inline long convert(const TypeCode *t,PyObject* o,void*)
632 else if(PyLong_Check(o))
637 msg << "Not a python integer. kind=" << t->kind() ;
638 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
639 throw YACS::ENGINE::ConversionException(msg.str());
644 template <ImplType IMPLOUT, class TOUT>
645 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
647 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
649 if (PyString_Check(o))
650 return PyString_AS_STRING(o);
654 msg << "Not a python string. kind=" << t->kind() ;
655 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
656 throw YACS::ENGINE::ConversionException(msg.str());
660 template <ImplType IMPLOUT, class TOUT>
661 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
663 static inline bool convert(const TypeCode *t,PyObject* o,void*)
668 else if (PyInt_Check(o))
669 l=(PyInt_AS_LONG(o)!=0);
670 else if(PyLong_Check(o))
671 l=(PyLong_AsLong(o)!=0);
675 msg << "Problem in Python to xml conversion: kind= " << t->kind() ;
676 msg << " : " << __FILE__ << ":" << __LINE__;
677 throw YACS::ENGINE::ConversionException(msg.str());
682 template <ImplType IMPLOUT, class TOUT>
683 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
685 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
687 if (PyString_Check(o))
689 // the objref is used by Python as a string (prefix:value) keep it as a string
690 return PyString_AS_STRING(o);
692 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"object_to_string","O",o);
693 std::string mystr=PyString_AsString(pystring);
698 template <ImplType IMPLOUT, class TOUT>
699 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
701 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
703 int length=PySequence_Size(o);
704 DEBTRACE("length: " << length );
706 for(int i=0;i<length;i++)
708 PyObject *item=PySequence_ITEM(o,i);
710 std::cerr <<"item[" << i << "]=";
711 PyObject_Print(item,stderr,Py_PRINT_RAW);
712 std::cerr << std::endl;
714 DEBTRACE( "item refcnt: " << item->ob_refcnt );
715 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
721 template <ImplType IMPLOUT, class TOUT>
722 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
724 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
726 DEBTRACE( "o refcnt: " << o->ob_refcnt );
727 PyObject *key, *value;
728 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
729 int nMember=tst->memberCount();
730 DEBTRACE("nMember="<<nMember);
731 for(int i=0;i<nMember;i++)
733 std::string name=tst->memberName(i);
734 DEBTRACE("Member name="<<name);
735 TypeCode* tm=tst->memberType(i);
736 value=PyDict_GetItemString(o, name.c_str());
739 //member name not present
740 //TODO delete all allocated objects in m
742 PyObject_Print(o,stderr,Py_PRINT_RAW);
743 std::cerr << std::endl;
746 msg << "Problem in conversion: member " << name << " not present " << endl;
747 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
748 throw YACS::ENGINE::ConversionException(msg.str());
750 DEBTRACE( "value refcnt: " << value->ob_refcnt );
751 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
756 /* End of ToYacs Convertor for PYTHONImpl */
758 //! FromYacs Convertor for PYTHONImpl
760 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
763 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
765 static inline PyObject* convert(const TypeCode *t,double o)
767 PyObject *pyob=PyFloat_FromDouble(o);
772 struct convertFromYacsInt<PYTHONImpl,PyObject*>
774 static inline PyObject* convert(const TypeCode *t,long o)
776 PyObject *pyob=PyLong_FromLong(o);
781 struct convertFromYacsString<PYTHONImpl,PyObject*>
783 static inline PyObject* convert(const TypeCode *t,std::string& o)
785 return PyString_FromString(o.c_str());
789 struct convertFromYacsBool<PYTHONImpl,PyObject*>
791 static inline PyObject* convert(const TypeCode *t,bool o)
793 return PyBool_FromLong ((long)o);
797 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
799 static inline PyObject* convert(const TypeCode *t,std::string& o)
801 std::string::size_type pos=o.find_first_of(":");
802 std::string prefix=o.substr(0,pos);
806 //It's an objref file. Convert it specially
807 return PyString_FromString(o.c_str());
810 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
811 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
815 //Objref CORBA. prefix=IOR,corbaname,corbaloc
816 CORBA::Object_var obref;
819 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
821 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
824 catch(CORBA::Exception& ex)
826 DEBTRACE( "Can't get reference to object." );
827 throw ConversionException("Can't get reference to object");
830 if( CORBA::is_nil(obref) )
832 DEBTRACE( "Can't get reference to object (or it was nil)." );
833 throw ConversionException("Can't get reference to object");
836 if(!obref->_is_a(t->id()))
839 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
840 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
841 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
842 throw YACS::ENGINE::ConversionException(msg.str());
844 //hold_lock is true: caller is supposed to hold the GIL.
845 //omniorb will not take the GIL
847 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
849 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
851 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
858 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
860 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
862 std::vector<PyObject*>::const_iterator iter;
863 PyObject *pyob = PyList_New(v.size());
865 for(iter=v.begin();iter!=v.end();iter++)
867 PyObject* item=*iter;
868 DEBTRACE( "item refcnt: " << item->ob_refcnt );
869 PyList_SetItem(pyob,i,item);
870 DEBTRACE( "item refcnt: " << item->ob_refcnt );
877 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
879 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
881 PyObject *pyob = PyDict_New();
882 std::map<std::string, PyObject*>::const_iterator pt;
883 for(pt=m.begin();pt!=m.end();pt++)
885 std::string name=(*pt).first;
886 PyObject* item=(*pt).second;
887 DEBTRACE( "item refcnt: " << item->ob_refcnt );
888 PyDict_SetItemString(pyob,name.c_str(),item);
890 DEBTRACE( "item refcnt: " << item->ob_refcnt );
892 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
896 /* End of FromYacs Convertor for PYTHONImpl */
898 //! ToYacs Convertor for XMLImpl
900 * Partial specialization for XML implementation (XMLImpl)
901 * This convertor converts xml object to YACS<TOUT> types
903 template <ImplType IMPLOUT, class TOUT>
904 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
906 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
909 cur = cur->xmlChildrenNode;
912 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
914 //wait a double, got a double
916 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
917 DEBTRACE( "convertToYacsDouble " << (const char *)s );
918 d=Cstr2d((const char *)s);
922 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
924 //wait a double, got an int
926 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
927 DEBTRACE( "convertToYacsDouble " << (const char *)s );
928 d=Cstr2d((const char *)s);
935 msg << "Problem in Xml to TOUT conversion: kind= " << t->kind() ;
936 msg << " : " << __FILE__ << ":" << __LINE__;
937 throw YACS::ENGINE::ConversionException(msg.str());
940 template <ImplType IMPLOUT, class TOUT>
941 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
943 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
946 cur = cur->xmlChildrenNode;
949 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
951 //wait a double, got an int
953 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
954 DEBTRACE( "convertToYacsInt " << (const char *)s );
955 d=atol((const char *)s);
962 msg << "Problem in Xml to TOUT conversion: kind= " << t->kind() ;
963 msg << " : " << __FILE__ << ":" << __LINE__;
964 throw YACS::ENGINE::ConversionException(msg.str());
967 template <ImplType IMPLOUT, class TOUT>
968 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
970 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
972 cur = cur->xmlChildrenNode;
975 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
977 //wait a string, got a string
979 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
981 DEBTRACE( "convertToYacsString " << (const char *)s );
982 std::string mystr=std::string((const char *)s);
989 msg << "Problem in Xml to TOUT conversion: kind= " << t->kind() ;
990 msg << " : " << __FILE__ << ":" << __LINE__;
991 throw YACS::ENGINE::ConversionException(msg.str());
994 template <ImplType IMPLOUT, class TOUT>
995 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
997 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
999 cur = cur->xmlChildrenNode;
1002 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1004 //wait a boolean, got a boolean
1006 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1007 DEBTRACE( "convertToYacsBool " << (const char *)s );
1008 bool ob=atoi((const char*)s)!=0;
1015 msg << "Problem in Xml to TOUT conversion: kind= " << t->kind() ;
1016 msg << " : " << __FILE__ << ":" << __LINE__;
1017 throw YACS::ENGINE::ConversionException(msg.str());
1020 template <ImplType IMPLOUT, class TOUT>
1021 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1023 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1025 cur = cur->xmlChildrenNode;
1028 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1030 //we wait a objref, we have got a objref
1032 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1033 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1034 std::string mystr((const char *)s);
1041 msg << "Problem in conversion: a objref is expected " ;
1042 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1043 throw YACS::ENGINE::ConversionException(msg.str());
1046 template <ImplType IMPLOUT, class TOUT>
1047 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1049 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1051 cur = cur->xmlChildrenNode;
1054 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1056 DEBTRACE( "parse sequence " );
1057 xmlNodePtr cur1=cur->xmlChildrenNode;
1058 while (cur1 != NULL)
1060 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1062 DEBTRACE( "parse data " );
1063 xmlNodePtr cur2=cur1->xmlChildrenNode;
1064 while (cur2 != NULL)
1066 //collect all values
1067 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1069 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1073 } // end while value
1081 } // end while array
1084 template <ImplType IMPLOUT, class TOUT>
1085 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1087 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1089 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1090 int nMember=tst->memberCount();
1091 DEBTRACE("nMember="<<nMember);
1092 std::map<std::string,TypeCode*> mtc;
1093 for(int i=0;i<nMember;i++)
1095 mtc[tst->memberName(i)]=tst->memberType(i);
1098 cur = cur->xmlChildrenNode;
1101 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1103 DEBTRACE( "parse struct " );
1104 xmlNodePtr cur1=cur->xmlChildrenNode;
1105 while (cur1 != NULL)
1107 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1109 DEBTRACE( "parse member " );
1110 xmlNodePtr cur2=cur1->xmlChildrenNode;
1111 while (cur2 != NULL)
1114 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1117 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1118 std::string name= (char *)s;
1120 while (cur2 != NULL)
1122 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1124 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1134 } // end while member/value
1137 } // end while member
1141 } // end while struct
1144 /* End of ToYacs Convertor for XMLImpl */
1146 //! FromYacs Convertor for XMLImpl
1148 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1151 struct convertFromYacsDouble<XMLImpl,std::string>
1153 static inline std::string convert(const TypeCode *t,double o)
1156 msg << "<value><double>" << o << "</double></value>\n";
1161 struct convertFromYacsInt<XMLImpl,std::string>
1163 static inline std::string convert(const TypeCode *t,long o)
1166 msg << "<value><int>" << o << "</int></value>\n";
1171 struct convertFromYacsString<XMLImpl,std::string>
1173 static inline std::string convert(const TypeCode *t,std::string& o)
1175 std::string msg="<value><string>";
1176 return msg+o+"</string></value>\n";
1180 struct convertFromYacsBool<XMLImpl,std::string>
1182 static inline std::string convert(const TypeCode *t,bool o)
1185 msg << "<value><boolean>" << o << "</boolean></value>\n";
1190 struct convertFromYacsObjref<XMLImpl,std::string>
1192 static inline std::string convert(const TypeCode *t,std::string& o)
1194 return "<value><objref>" + o + "</objref></value>\n";
1199 struct convertFromYacsSequence<XMLImpl,std::string>
1201 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1203 std::vector<std::string>::const_iterator iter;
1205 xmlob << "<value><array><data>\n";
1206 for(iter=v.begin();iter!=v.end();iter++)
1210 xmlob << "</data></array></value>\n";
1211 DEBTRACE("Sequence= " << xmlob);
1216 struct convertFromYacsStruct<XMLImpl,std::string>
1218 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1220 std::string result="<value><struct>\n";
1221 std::map<std::string, std::string>::const_iterator pt;
1222 for(pt=m.begin();pt!=m.end();pt++)
1224 std::string name=(*pt).first;
1225 std::string item=(*pt).second;
1226 result=result+"<member>\n";
1227 result=result+"<name>"+name+"</name>\n";
1229 result=result+"</member>\n";
1231 result=result+"</struct></value>\n";
1236 /* End of FromYacs Convertor for XMLImpl */
1238 //! ToYacs Convertor for NEUTRALImpl
1240 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1241 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1243 template <ImplType IMPLOUT, class TOUT>
1244 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1246 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1248 if(o->getType()->kind()==Double)
1249 return o->getDoubleValue();
1250 else if(o->getType()->kind()==Int)
1251 return o->getIntValue();
1254 msg << "Problem in conversion: a double or int is expected " ;
1255 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1256 throw YACS::ENGINE::ConversionException(msg.str());
1259 template <ImplType IMPLOUT, class TOUT>
1260 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1262 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1264 return o->getIntValue();
1267 template <ImplType IMPLOUT, class TOUT>
1268 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1270 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1272 return o->getStringValue();
1275 template <ImplType IMPLOUT, class TOUT>
1276 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1278 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1280 if(o->getType()->kind()==Bool)
1281 return o->getBoolValue();
1282 else if(o->getType()->kind()==Int)
1283 return o->getIntValue() != 0;
1285 msg << "Problem in conversion: a bool or int is expected " ;
1286 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1289 template <ImplType IMPLOUT, class TOUT>
1290 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1292 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1294 return o->getStringValue();
1297 template <ImplType IMPLOUT, class TOUT>
1298 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1300 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1302 SequenceAny* sdata= (SequenceAny*)o;
1303 int length=sdata->size();
1305 for(int i=0;i<length;i++)
1307 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1312 /* End of ToYacs Convertor for NEUTRALImpl */
1314 //! FromYacs Convertor for NEUTRALImpl
1316 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1319 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1321 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1323 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1328 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1330 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1332 return YACS::ENGINE::AtomAny::New((int)o);
1336 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1338 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1340 return YACS::ENGINE::AtomAny::New(o);
1344 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1346 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1348 return YACS::ENGINE::AtomAny::New(o);
1352 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1354 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1356 return YACS::ENGINE::AtomAny::New(o);
1361 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1363 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1365 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1366 //Objref are managed as string within YACS::ENGINE::Any objs
1368 any=SequenceAny::New(t->contentType());
1369 for(iter=v.begin();iter!=v.end();iter++)
1371 any->pushBack(*iter);
1374 DEBTRACE( "refcnt: " << any->getRefCnt() );
1378 /* End of FromYacs Convertor for NEUTRALImpl */
1380 //! ToYacs Convertor for CORBAImpl
1382 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1383 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1385 template <ImplType IMPLOUT, class TOUT>
1386 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1388 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1390 CORBA::TypeCode_var tc = o->type();
1391 if (tc->equivalent(CORBA::_tc_double))
1397 if (tc->equivalent(CORBA::_tc_long))
1404 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1405 msg << " : " << __FILE__ << ":" << __LINE__;
1406 throw YACS::ENGINE::ConversionException(msg.str());
1409 template <ImplType IMPLOUT, class TOUT>
1410 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1412 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1419 template <ImplType IMPLOUT, class TOUT>
1420 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1422 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1429 template <ImplType IMPLOUT, class TOUT>
1430 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1432 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1435 if(*o >>= CORBA::Any::to_boolean(b))
1438 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1439 msg << " : " << __FILE__ << ":" << __LINE__;
1440 throw YACS::ENGINE::ConversionException(msg.str());
1443 template <ImplType IMPLOUT, class TOUT>
1444 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1446 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1448 CORBA::Object_var ObjRef ;
1449 *o >>= (CORBA::Any::to_object ) ObjRef ;
1450 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1451 return (char *)objref;
1454 template <ImplType IMPLOUT, class TOUT>
1455 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1457 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1459 CORBA::TypeCode_var tc=o->type();
1460 if (tc->kind() != CORBA::tk_sequence)
1463 msg << "Not a sequence corba type " << tc->kind();
1464 msg << " : " << __FILE__ << ":" << __LINE__;
1465 throw YACS::ENGINE::ConversionException(msg.str());
1467 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1468 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1469 CORBA::release(dynany);
1470 DynamicAny::AnySeq_var as=ds->get_elements();
1471 int len=as->length();
1473 for(int i=0;i<len;i++)
1476 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1478 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1483 for(int i=0;i<len;i++)
1486 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1491 template <ImplType IMPLOUT, class TOUT>
1492 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1494 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1496 CORBA::TypeCode_var tc=o->type();
1497 DEBTRACE(tc->kind());
1498 if (tc->kind() != CORBA::tk_struct)
1501 msg << "Not a struct corba type " << tc->kind();
1502 msg << " : " << __FILE__ << ":" << __LINE__;
1503 throw YACS::ENGINE::ConversionException(msg.str());
1505 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1506 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1507 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1508 CORBA::release(dynany);
1509 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1510 int len=as->length();
1511 for(int i=0;i<len;i++)
1513 std::string name=as[i].id.in();
1515 CORBA::Any value=as[i].value;
1517 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1519 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1526 /* End of ToYacs Convertor for CORBAImpl */
1528 //! FromYacs Convertor for CORBAImpl
1530 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1533 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1535 static inline CORBA::Any* convert(const TypeCode *t,double o)
1537 CORBA::Any *any = new CORBA::Any();
1543 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
1545 static inline CORBA::Any* convert(const TypeCode *t,long o)
1547 CORBA::Any *any = new CORBA::Any();
1553 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
1555 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1557 CORBA::Any *any = new CORBA::Any();
1563 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
1565 static inline CORBA::Any* convert(const TypeCode *t,bool o)
1567 CORBA::Any *any = new CORBA::Any();
1568 *any <<= CORBA::Any::from_boolean(o);
1573 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
1575 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
1578 std::string::size_type pos=o.find_first_of(":");
1579 std::string prefix=o.substr(0,pos);
1581 if(prefix == "file")
1583 //It's an objref file. Convert it specially
1586 CORBA::Object_var obref =
1587 getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1589 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1591 CORBA::Any *any = new CORBA::Any();
1594 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
1601 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
1603 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
1606 CORBA::TypeCode_ptr seqTC;
1608 //the equivalent CORBA TypeCode
1609 seqTC=getCorbaTC(t);
1611 DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)seqTC)->pd_ref_count);
1613 DynamicAny::DynAny_ptr dynany =
1614 getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(seqTC);
1615 CORBA::release(seqTC);
1616 DynamicAny::DynSequence_ptr ds =
1617 DynamicAny::DynSequence::_narrow(dynany);
1618 CORBA::release(dynany);
1620 std::vector<CORBA::Any*>::const_iterator iter;
1621 DynamicAny::AnySeq as ;
1622 as.length(v.size());
1624 for(iter=v.begin();iter!=v.end();iter++)
1626 //Can we avoid making a copy ?
1627 CORBA::Any* a=*iter;
1630 //delete intermediate any
1635 ds->set_elements(as);
1637 catch(DynamicAny::DynAny::TypeMismatch& ex)
1639 throw YACS::ENGINE::ConversionException("type mismatch");
1641 catch(DynamicAny::DynAny::InvalidValue& ex)
1643 throw YACS::ENGINE::ConversionException("invalid value");
1649 DEBTRACE("refcount CORBA seqTC: " << ((omni::TypeCode_base*)seqTC)->pd_ref_count);
1655 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
1657 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
1660 CORBA::TypeCode_ptr structTC;
1662 //the equivalent CORBA TypeCode
1663 structTC=getCorbaTC(t);
1665 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1666 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1668 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1669 int nMember=tst->memberCount();
1670 DEBTRACE("nMember="<<nMember);
1671 DynamicAny::DynAny_ptr da=
1672 getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(structTC);
1674 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1675 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1677 CORBA::release(structTC);
1679 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1680 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1682 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(da);
1685 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1686 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1688 DynamicAny::NameValuePairSeq members;
1689 members.length(nMember);
1690 for(int i=0;i<nMember;i++)
1692 const char * name=tst->memberName(i);
1693 DEBTRACE("Member name="<<name);
1694 //TypeCode* tm=tst->memberType(i);
1695 //do not test member presence : test has been done in ToYacs convertor
1696 CORBA::Any* a=m[name];
1697 members[i].id=CORBA::string_dup(name);
1698 members[i].value=*a;
1699 //delete intermediate any
1703 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1704 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1706 ds->set_members(members);
1708 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1709 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1713 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1714 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1719 DEBTRACE("refcount CORBA structTC: " << ((omni::TypeCode_base*)structTC)->pd_ref_count);
1720 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
1725 /* End of FromYacs Convertor for CORBAImpl */
1727 /* Some shortcuts for CORBA to CORBA conversion */
1729 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1731 CORBA::TypeCode_var tc = o->type();
1732 if (tc->equivalent(CORBA::_tc_double))
1736 if (tc->equivalent(CORBA::_tc_long))
1740 CORBA::Any *any = new CORBA::Any();
1741 *any <<= (CORBA::Double)d;
1745 msg << "Not a double or long corba type " << tc->kind();
1746 msg << " : " << __FILE__ << ":" << __LINE__;
1747 throw YACS::ENGINE::ConversionException(msg.str());
1750 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1755 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1760 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1765 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1770 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
1774 /* End of shortcuts for CORBA to CORBA conversion */
1776 //! ToYacs Convertor for CPPImpl
1778 * This convertor converts Python object to YACS<TOUT> types
1779 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
1781 template <ImplType IMPLOUT, class TOUT>
1782 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
1784 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
1786 if(intype->kind()==YACS::ENGINE::Double)
1790 else if(intype->kind()==YACS::ENGINE::Int)
1795 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
1796 msg << " : " << __FILE__ << ":" << __LINE__;
1797 throw YACS::ENGINE::ConversionException(msg.str());
1800 template <ImplType IMPLOUT, class TOUT>
1801 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
1803 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
1805 if(intype->kind()==YACS::ENGINE::Int)
1810 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
1811 msg << " : " << __FILE__ << ":" << __LINE__;
1812 throw YACS::ENGINE::ConversionException(msg.str());
1815 /* End of ToYacs Convertor for CPPImpl */
1817 //Python conversions
1818 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
1820 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
1822 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
1824 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
1826 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
1828 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
1832 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1834 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
1836 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1838 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
1840 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1842 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
1844 //NEUTRAL conversions
1845 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
1847 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
1849 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
1851 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
1853 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
1855 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
1857 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
1864 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
1866 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
1868 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
1870 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
1872 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
1874 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
1876 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
1878 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);