1 // Copyright (C) 2006-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
29 #include "TypeConversions.hxx"
30 #include "ConversionException.hxx"
31 #include "RuntimeSALOME.hxx"
32 #include "Salome_file_i.hxx"
33 #include "TypeCode.hxx"
35 #include "SALOME_GenericObj.hh"
45 int mkstemp(char *tmpl)
48 mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
54 #include "YacsTrace.hxx"
62 void printbin(const std::string& bin)
65 for(int i=0;i<bin.length();i++)
68 if (c < ' ' || c >= 0x7f)
70 fprintf(stderr,"\\x%02x",c & 0xff);
73 fprintf(stderr,"%c",c);
78 std::string getImplName(ImplType impl)
97 * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
100 typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
102 CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
105 msg << "Conversion not implemented: kind= " << t->kind();
106 msg << " : " << __FILE__ << ":" << __LINE__;
107 throw YACS::ENGINE::ConversionException(msg.str());
110 CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
112 return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
115 CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
117 return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
120 CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
122 return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
125 CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
127 return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
130 CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
132 DEBTRACE( t->name() << " " << t->shortName() << " " << t->id());
133 CORBA::TypeCode_ptr tc;
134 if(strncmp(t->id(),"python",6)==0 )
135 tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
136 else if(strncmp(t->id(),"json",4)==0)
137 tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
139 tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
141 DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
146 CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
148 CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
149 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
151 DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
152 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
157 CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
159 CORBA::StructMemberSeq mseq;
160 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
161 int nMember=tst->memberCount();
162 mseq.length(nMember);
163 for(int i=0;i<nMember;i++)
165 const char * name=tst->memberName(i);
166 TypeCode* tm=tst->memberType(i);
167 mseq[i].name=CORBA::string_dup(name);
168 mseq[i].type=getCorbaTC(tm);
170 CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
172 DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
177 getCorbaTCFn getCorbaTCFns[]=
190 CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
193 return getCorbaTCFns[tk](t);
197 * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
201 * Section that defines functions to check adaptation from one implementation to another
202 * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
203 * can be converted to TypeCode t2 from implementation IMPLOUT
204 * IMPLIN is the implementation of an output port
205 * IMPLOUT is the implementation of an input port
206 * If the check is True, the input port can be adapted to the output port
209 template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
211 template <ImplType IMPLIN,ImplType IMPLOUT>
212 struct isAdaptableDouble
214 static inline int apply(const TypeCode *t1,const TypeCode* t2)
216 if(t1->kind() == Double)return 1;
217 if(t1->kind() == Int)return 1;
221 template <ImplType IMPLIN,ImplType IMPLOUT>
222 struct isAdaptableInt
224 static inline int apply(const TypeCode *t1,const TypeCode* t2)
226 if(t1->kind() == Int)return 1;
230 template <ImplType IMPLIN,ImplType IMPLOUT>
231 struct isAdaptableString
233 static inline int apply(const TypeCode *t1,const TypeCode* t2)
235 if(t1->kind() == String)return 1;
239 template <ImplType IMPLIN,ImplType IMPLOUT>
240 struct isAdaptableBool
242 static inline int apply(const TypeCode *t1,const TypeCode* t2)
244 if(t1->kind() == Bool)return 1;
245 if(t1->kind() == Int)return 1;
249 template <ImplType IMPLIN,ImplType IMPLOUT>
250 struct isAdaptableObjref
252 static inline int apply(const TypeCode *t1,const TypeCode* t2)
254 if(t1->kind() == Objref)
256 //The inport type must be more general than outport type
257 if( t1->isA(t2->id()) )
260 else if(t1->kind() == Sequence)
262 const TypeCodeSeq *t1c(dynamic_cast<const TypeCodeSeq *>(t1));
265 const TypeCode *t1cc(t1c->contentType());
268 if(t1cc->kind() == Objref && std::string(t1cc->id())==std::string(t2->id()))
274 template <ImplType IMPLIN,ImplType IMPLOUT>
275 struct isAdaptableSequence
277 static inline int apply(const TypeCode *t1,const TypeCode* t2)
279 if(t1->kind() == Sequence)
281 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
289 template <ImplType IMPLIN,ImplType IMPLOUT>
290 struct isAdaptableArray
292 static inline int apply(const TypeCode *t1,const TypeCode* t2)
297 template <ImplType IMPLIN,ImplType IMPLOUT>
298 struct isAdaptableStruct
300 static inline int apply(const TypeCode *t1,const TypeCode* t2)
302 if(t1->kind() == Struct)
312 * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
313 * t1 is the IMPLIN output port type
314 * t2 is the IMPLOUT input port type
316 template <ImplType IMPLIN,ImplType IMPLOUT>
317 inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
322 return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
324 return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
326 return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
328 return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
330 return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
332 return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
334 return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
336 return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
343 //xxx to Python adaptations
344 int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
346 return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
348 int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
350 return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
352 int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
354 return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
357 //xxx to Neutral adaptations
358 int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
360 return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
362 int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
364 return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
366 int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
368 return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
370 int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
372 return isAdaptableNeutralCorba(t1, t2);
375 //xxx to XML adaptations
376 int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
378 return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
381 //xxx to Corba adaptations
382 int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
384 return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
386 int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
388 return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
390 int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
392 return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
394 int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
396 return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
399 //! Basic template convertor from type TIN to Yacs<TOUT> type
401 * This convertor does nothing : throws exception
402 * It must be partially specialize for a specific type (TIN)
404 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
405 struct convertToYacsDouble
407 static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
410 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
411 msg << " : " << __FILE__ << ":" << __LINE__;
412 throw YACS::ENGINE::ConversionException(msg.str());
415 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
416 struct convertToYacsInt
418 static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
421 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
422 msg << " : " << __FILE__ << ":" << __LINE__;
423 throw YACS::ENGINE::ConversionException(msg.str());
426 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
427 struct convertToYacsString
429 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
432 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
433 msg << " : " << __FILE__ << ":" << __LINE__;
434 throw YACS::ENGINE::ConversionException(msg.str());
437 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
438 struct convertToYacsBool
440 static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
443 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
444 msg << " : " << __FILE__ << ":" << __LINE__;
445 throw YACS::ENGINE::ConversionException(msg.str());
448 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
449 struct convertToYacsObjref
451 static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
454 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
455 msg << " : " << __FILE__ << ":" << __LINE__;
456 throw YACS::ENGINE::ConversionException(msg.str());
459 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
460 struct convertToYacsSequence
462 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
465 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
466 msg << " : " << __FILE__ << ":" << __LINE__;
467 throw YACS::ENGINE::ConversionException(msg.str());
470 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
471 struct convertToYacsArray
473 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
476 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
477 msg << " : " << __FILE__ << ":" << __LINE__;
478 throw YACS::ENGINE::ConversionException(msg.str());
481 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
482 struct convertToYacsStruct
484 static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
487 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
488 msg << " : " << __FILE__ << ":" << __LINE__;
489 throw YACS::ENGINE::ConversionException(msg.str());
493 //! Basic convertor from Yacs<TOUT> type to full TOUT type
497 template <ImplType IMPLOUT, class TOUT>
498 struct convertFromYacsDouble
500 static inline TOUT convert(const TypeCode *t,double o)
503 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
504 msg << " : " << __FILE__ << ":" << __LINE__;
505 throw YACS::ENGINE::ConversionException(msg.str());
508 template <ImplType IMPLOUT, class TOUT>
509 struct convertFromYacsInt
511 static inline TOUT convert(const TypeCode *t,long o)
514 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
515 msg << " : " << __FILE__ << ":" << __LINE__;
516 throw YACS::ENGINE::ConversionException(msg.str());
519 template <ImplType IMPLOUT, class TOUT>
520 struct convertFromYacsString
522 static inline TOUT convert(const TypeCode *t,std::string o)
525 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
526 msg << " : " << __FILE__ << ":" << __LINE__;
527 throw YACS::ENGINE::ConversionException(msg.str());
530 template <ImplType IMPLOUT, class TOUT>
531 struct convertFromYacsBool
533 static inline TOUT convert(const TypeCode *t,bool o)
536 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
537 msg << " : " << __FILE__ << ":" << __LINE__;
538 throw YACS::ENGINE::ConversionException(msg.str());
541 template <ImplType IMPLOUT, class TOUT>
542 struct convertFromYacsObjref
544 static inline TOUT convert(const TypeCode *t,std::string o)
547 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
548 msg << " : " << __FILE__ << ":" << __LINE__;
549 throw YACS::ENGINE::ConversionException(msg.str());
552 template <ImplType IMPLOUT, class TOUT>
553 struct convertFromYacsSequence
555 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
558 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
559 msg << " : " << __FILE__ << ":" << __LINE__;
560 throw YACS::ENGINE::ConversionException(msg.str());
563 template <ImplType IMPLOUT, class TOUT>
564 struct convertFromYacsArray
566 static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
569 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
570 msg << " : " << __FILE__ << ":" << __LINE__;
571 throw YACS::ENGINE::ConversionException(msg.str());
574 template <ImplType IMPLOUT, class TOUT>
575 struct convertFromYacsStruct
577 static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
580 msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
581 msg << " : " << __FILE__ << ":" << __LINE__;
582 throw YACS::ENGINE::ConversionException(msg.str());
585 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
586 inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
588 double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
590 TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
593 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
594 inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
596 long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
598 TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
601 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
602 inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
604 std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
606 TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
609 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
610 inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
612 double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
614 TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
617 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
618 inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
621 if(IMPLOUT==XMLImpl || IMPLOUT==NEUTRALImpl)
623 std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
625 TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
629 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
630 inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
633 convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
634 TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
637 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
638 inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
641 convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
642 TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
645 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
646 inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
648 std::map<std::string,TOUT> v;
649 convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
650 TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
654 template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
655 inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
661 return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
663 return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
665 return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
667 return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
669 return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
671 return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
673 return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
675 return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
680 msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
681 msg << " : " << __FILE__ << ":" << __LINE__;
682 throw YACS::ENGINE::ConversionException(msg.str());
685 //! ToYacs Convertor for PYTHONImpl
687 * This convertor converts Python object to YACS<TOUT> types
688 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
690 template <ImplType IMPLOUT, class TOUT>
691 struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
693 static inline double convert(const TypeCode *t,PyObject* o,void*)
696 if (PyFloat_Check(o))
697 x=PyFloat_AS_DOUBLE(o);
698 else if (PyInt_Check(o))
700 else if(PyLong_Check(o))
705 msg << "Not a python double. ";
707 msg << "kind=" << t->kind() ;
708 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
710 throw YACS::ENGINE::ConversionException(msg.str());
715 template <ImplType IMPLOUT, class TOUT>
716 struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
718 static inline long convert(const TypeCode *t,PyObject* o,void*)
723 else if(PyLong_Check(o))
728 msg << "Not a python integer. ";
730 msg << "kind=" << t->kind() ;
731 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
733 throw YACS::ENGINE::ConversionException(msg.str());
738 template <ImplType IMPLOUT, class TOUT>
739 struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
741 static inline std::string convert(const TypeCode *t,PyObject* o,void*)
744 if (PyString_Check(o))
745 s= PyString_AS_STRING(o);
749 msg << "Not a python string. ";
751 msg << "kind=" << t->kind() ;
752 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
754 throw YACS::ENGINE::ConversionException(msg.str());
759 template <ImplType IMPLOUT, class TOUT>
760 struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
762 static inline bool convert(const TypeCode *t,PyObject* o,void*)
767 else if (PyInt_Check(o))
768 l=(PyInt_AS_LONG(o)!=0);
769 else if(PyLong_Check(o))
770 l=(PyLong_AsLong(o)!=0);
774 msg << "Not a python boolean. ";
776 msg << "kind=" << t->kind() ;
777 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
779 throw YACS::ENGINE::ConversionException(msg.str());
784 template <ImplType IMPLOUT, class TOUT>
785 struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
787 static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
789 if (PyString_Check(o) && strncmp(t->id(),"python",6)!=0)
791 // the objref is used by Python as a string (prefix:value) keep it as a string
792 return PyString_AS_STRING(o);
794 if(strncmp(t->id(),"python",6)==0)
796 // It's a native Python object pickle it
797 PyObject* mod=PyImport_ImportModule("cPickle");
798 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
799 DEBTRACE(PyObject_REPR(pickled) );
804 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
806 std::string mystr(PyString_AsString(pickled),PyString_Size(pickled));
810 else if(strncmp(t->id(),"json",4)==0)
812 // It's a Python object convert it to json
813 PyObject* mod=PyImport_ImportModule("simplejson");
817 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
819 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
824 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
826 std::string mystr=PyString_AsString(pickled);
832 // It should be a CORBA Object convert it to an IOR string
833 PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
837 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
839 std::string mystr=PyString_AsString(pystring);
845 template <ImplType IMPLOUT, class TOUT>
846 struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
848 static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
850 if(!PySequence_Check(o))
853 msg << "Problem in conversion: the python object is not a sequence " << std::endl;
855 msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
857 throw YACS::ENGINE::ConversionException(msg.str());
859 int length=PySequence_Size(o);
860 DEBTRACE("length: " << length );
862 for(int i=0;i<length;i++)
864 PyObject *item=PySequence_ITEM(o,i);
866 std::cerr <<"item[" << i << "]=";
867 PyObject_Print(item,stderr,Py_PRINT_RAW);
868 std::cerr << std::endl;
870 DEBTRACE( "item refcnt: " << item->ob_refcnt );
873 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
877 catch(ConversionException& ex)
880 msg << ex.what() << " for sequence element " << i;
881 throw YACS::ENGINE::ConversionException(msg.str(),false);
886 template <ImplType IMPLOUT, class TOUT>
887 struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
889 static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
891 DEBTRACE( "o refcnt: " << o->ob_refcnt );
892 PyObject *key, *value;
893 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
894 int nMember=tst->memberCount();
895 DEBTRACE("nMember="<<nMember);
896 for(int i=0;i<nMember;i++)
898 std::string name=tst->memberName(i);
899 DEBTRACE("Member name="<<name);
900 TypeCode* tm=tst->memberType(i);
901 value=PyDict_GetItemString(o, name.c_str());
904 //member name not present
905 //TODO delete all allocated objects in m
907 PyObject_Print(o,stderr,Py_PRINT_RAW);
908 std::cerr << std::endl;
911 msg << "member " << name << " not present " ;
912 throw YACS::ENGINE::ConversionException(msg.str());
914 DEBTRACE( "value refcnt: " << value->ob_refcnt );
917 TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
920 catch(ConversionException& ex)
922 std::string s=" for struct member "+name;
923 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
928 /* End of ToYacs Convertor for PYTHONImpl */
930 //! FromYacs Convertor for PYTHONImpl
932 * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
935 struct convertFromYacsDouble<PYTHONImpl,PyObject*>
937 static inline PyObject* convert(const TypeCode *t,double o)
939 PyObject *pyob=PyFloat_FromDouble(o);
944 struct convertFromYacsInt<PYTHONImpl,PyObject*>
946 static inline PyObject* convert(const TypeCode *t,long o)
948 PyObject *pyob=PyLong_FromLong(o);
953 struct convertFromYacsString<PYTHONImpl,PyObject*>
955 static inline PyObject* convert(const TypeCode *t,std::string& o)
957 return PyString_FromString(o.c_str());
961 struct convertFromYacsBool<PYTHONImpl,PyObject*>
963 static inline PyObject* convert(const TypeCode *t,bool o)
965 return PyBool_FromLong ((long)o);
969 struct convertFromYacsObjref<PYTHONImpl,PyObject*>
971 static inline PyObject* convert(const TypeCode *t,std::string& o)
978 if(t->isA(Runtime::_tc_file))
980 //It's an objref file. Convert it specially
981 return PyString_FromString(o.c_str());
983 if(strncmp(t->id(),"python",6)==0)
985 //It's a python pickled object, unpickled it
986 PyObject* mod=PyImport_ImportModule("cPickle");
987 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",o.c_str(),o.length());
988 DEBTRACE(PyObject_REPR(ob));
993 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
997 if(strncmp(t->id(),"json",4)==0)
999 // It's a json object unpack it
1000 PyObject* mod=PyImport_ImportModule("simplejson");
1004 throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
1006 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s",o.c_str());
1011 throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1016 /* another way to convert IOR string to CORBA PyObject
1017 PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
1018 DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
1022 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1023 CORBA::Object_var obref;
1026 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1028 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1031 catch(CORBA::Exception& ex)
1033 DEBTRACE( "Can't get reference to object." );
1034 throw ConversionException("Can't get reference to object");
1037 if(obref->_non_existent())
1039 throw ConversionException("non_existent object");
1042 if( CORBA::is_nil(obref) )
1044 DEBTRACE( "Can't get reference to object (or it was nil)." );
1045 throw ConversionException("Can't get reference to object");
1048 if(!obref->_is_a(t->id()))
1051 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1052 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1053 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1054 throw YACS::ENGINE::ConversionException(msg.str());
1057 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1060 std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1061 std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId() << std::endl;
1064 //hold_lock is true: caller is supposed to hold the GIL.
1065 //omniorb will not take the GIL
1066 PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1069 PyObject_Print(ob,stderr,Py_PRINT_RAW);
1070 std::cerr << std::endl;
1071 std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1072 PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1073 std::cerr << std::endl;
1076 //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1077 if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1079 PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1081 PyErr_Clear();//Exception during narrow. Keep ob
1082 else if(result==Py_None)
1083 Py_DECREF(result); //Can't narrow. Keep ob
1086 //Can narrow. Keep result
1088 PyObject_Print(result,stderr,Py_PRINT_RAW);
1089 std::cerr << std::endl;
1097 DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1104 struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1106 static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1108 std::vector<PyObject*>::const_iterator iter;
1109 PyObject *pyob = PyList_New(v.size());
1111 for(iter=v.begin();iter!=v.end();iter++)
1113 PyObject* item=*iter;
1114 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1115 PyList_SetItem(pyob,i,item);
1116 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1123 struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1125 static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1127 PyObject *pyob = PyDict_New();
1128 std::map<std::string, PyObject*>::const_iterator pt;
1129 for(pt=m.begin();pt!=m.end();pt++)
1131 std::string name=(*pt).first;
1132 PyObject* item=(*pt).second;
1133 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1134 PyDict_SetItemString(pyob,name.c_str(),item);
1136 DEBTRACE( "item refcnt: " << item->ob_refcnt );
1138 DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1142 /* End of FromYacs Convertor for PYTHONImpl */
1144 //! ToYacs Convertor for XMLImpl
1146 * Partial specialization for XML implementation (XMLImpl)
1147 * This convertor converts xml object to YACS<TOUT> types
1149 template <ImplType IMPLOUT, class TOUT>
1150 struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1152 static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1155 cur = cur->xmlChildrenNode;
1158 if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1160 //wait a double, got a double
1162 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1165 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1166 d=Cstr2d((const char *)s);
1171 DEBTRACE("############### workaround to improve...");
1175 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1177 //wait a double, got an int
1179 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1182 DEBTRACE( "convertToYacsDouble " << (const char *)s );
1183 d=Cstr2d((const char *)s);
1188 DEBTRACE("############### workaround to improve...");
1195 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1196 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1197 throw YACS::ENGINE::ConversionException(msg.str());
1200 template <ImplType IMPLOUT, class TOUT>
1201 struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1203 static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1206 cur = cur->xmlChildrenNode;
1209 if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1212 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1215 DEBTRACE( "convertToYacsInt " << (const char *)s );
1216 d=atol((const char *)s);
1221 DEBTRACE("############### workaround to improve...");
1228 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1229 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1230 throw YACS::ENGINE::ConversionException(msg.str());
1233 template <ImplType IMPLOUT, class TOUT>
1234 struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1236 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1238 cur = cur->xmlChildrenNode;
1241 if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1243 //wait a string, got a string
1245 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1247 DEBTRACE( "convertToYacsString " << (const char *)s );
1248 std::string mystr=std::string((const char *)s);
1255 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1256 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1257 throw YACS::ENGINE::ConversionException(msg.str());
1260 template <ImplType IMPLOUT, class TOUT>
1261 struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1263 static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1265 cur = cur->xmlChildrenNode;
1268 if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1270 //wait a boolean, got a boolean
1272 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1276 DEBTRACE( "convertToYacsBool " << (const char *)s );
1277 ob=atoi((const char*)s)!=0;
1282 DEBTRACE("############### workaround to improve...");
1289 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1290 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1291 throw YACS::ENGINE::ConversionException(msg.str());
1294 template <ImplType IMPLOUT, class TOUT>
1295 struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1297 static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1299 cur = cur->xmlChildrenNode;
1302 if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1304 //we wait a objref, we have got a objref
1306 std::string mystr = "";
1307 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1310 DEBTRACE( "convertToYacsObjref " << (const char *)s );
1311 mystr = (const char *)s;
1316 DEBTRACE("############### workaround to improve...");
1320 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))// <- here case where pyobj value has been stored in XML. pyobj has kind==ObjRef. And the stored format is String ! EDF11027
1322 //wait a string, got a string
1324 s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1326 DEBTRACE( "convertToYacsString " << (const char *)s );
1327 std::string mystr=std::string((const char *)s);
1334 msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type: " << t->id() ;
1335 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1336 throw YACS::ENGINE::ConversionException(msg.str());
1339 template <ImplType IMPLOUT, class TOUT>
1340 struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1342 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1344 cur = cur->xmlChildrenNode;
1347 if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1349 DEBTRACE( "parse sequence " );
1350 xmlNodePtr cur1=cur->xmlChildrenNode;
1351 while (cur1 != NULL)
1353 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1355 DEBTRACE( "parse data " );
1356 xmlNodePtr cur2=cur1->xmlChildrenNode;
1357 while (cur2 != NULL)
1359 //collect all values
1360 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1362 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1366 } // end while value
1374 } // end while array
1377 template <ImplType IMPLOUT, class TOUT>
1378 struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1380 static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1382 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1383 int nMember=tst->memberCount();
1384 DEBTRACE("nMember="<<nMember);
1385 std::map<std::string,TypeCode*> mtc;
1386 for(int i=0;i<nMember;i++)
1388 mtc[tst->memberName(i)]=tst->memberType(i);
1391 cur = cur->xmlChildrenNode;
1394 if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1396 DEBTRACE( "parse struct " );
1397 xmlNodePtr cur1=cur->xmlChildrenNode;
1398 while (cur1 != NULL)
1400 if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1402 DEBTRACE( "parse member " );
1403 xmlNodePtr cur2=cur1->xmlChildrenNode;
1404 while (cur2 != NULL)
1407 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1410 s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1411 std::string name= (char *)s;
1413 while (cur2 != NULL)
1415 if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1417 TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1427 } // end while member/value
1430 } // end while member
1434 } // end while struct
1437 /* End of ToYacs Convertor for XMLImpl */
1439 //! FromYacs Convertor for XMLImpl
1441 * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1444 struct convertFromYacsDouble<XMLImpl,std::string>
1446 static inline std::string convert(const TypeCode *t,double o)
1449 msg << "<value><double>" << setprecision(16) << o << "</double></value>\n";
1454 struct convertFromYacsInt<XMLImpl,std::string>
1456 static inline std::string convert(const TypeCode *t,long o)
1459 msg << "<value><int>" << o << "</int></value>\n";
1464 struct convertFromYacsString<XMLImpl,std::string>
1466 static inline std::string convert(const TypeCode *t,std::string& o)
1468 std::string msg="<value><string>";
1469 return msg+o+"</string></value>\n";
1473 struct convertFromYacsBool<XMLImpl,std::string>
1475 static inline std::string convert(const TypeCode *t,bool o)
1478 msg << "<value><boolean>" << o << "</boolean></value>\n";
1483 struct convertFromYacsObjref<XMLImpl,std::string>
1485 static inline std::string convert(const TypeCode *t,std::string& o)
1487 if(strncmp(t->id(),"python",6)==0 )
1488 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1489 else if(strncmp(t->id(),"json",4)==0)
1490 return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1492 return "<value><objref>" + o + "</objref></value>\n";
1497 struct convertFromYacsSequence<XMLImpl,std::string>
1499 static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1501 std::vector<std::string>::const_iterator iter;
1503 xmlob << "<value><array><data>\n";
1504 for(iter=v.begin();iter!=v.end();iter++)
1508 xmlob << "</data></array></value>\n";
1509 DEBTRACE("Sequence= " << xmlob);
1514 struct convertFromYacsStruct<XMLImpl,std::string>
1516 static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1518 std::string result="<value><struct>\n";
1519 std::map<std::string, std::string>::const_iterator pt;
1520 for(pt=m.begin();pt!=m.end();pt++)
1522 std::string name=(*pt).first;
1523 std::string item=(*pt).second;
1524 result=result+"<member>\n";
1525 result=result+"<name>"+name+"</name>\n";
1527 result=result+"</member>\n";
1529 result=result+"</struct></value>\n";
1534 /* End of FromYacs Convertor for XMLImpl */
1536 //! ToYacs Convertor for NEUTRALImpl
1538 * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1539 * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1541 template <ImplType IMPLOUT, class TOUT>
1542 struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1544 static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1546 if(o->getType()->kind()==Double)
1547 return o->getDoubleValue();
1548 else if(o->getType()->kind()==Int)
1549 return o->getIntValue();
1552 msg << "Problem in conversion: a double or int is expected " ;
1553 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1554 throw YACS::ENGINE::ConversionException(msg.str());
1557 template <ImplType IMPLOUT, class TOUT>
1558 struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1560 static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1562 if(o->getType()->kind()==Int)
1563 return o->getIntValue();
1565 msg << "Problem in conversion: a int is expected " ;
1566 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1567 throw YACS::ENGINE::ConversionException(msg.str());
1570 template <ImplType IMPLOUT, class TOUT>
1571 struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1573 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1575 if(o->getType()->kind()==String)
1576 return o->getStringValue();
1578 msg << "Problem in conversion: a string is expected " ;
1579 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1580 throw YACS::ENGINE::ConversionException(msg.str());
1583 template <ImplType IMPLOUT, class TOUT>
1584 struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1586 static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1588 if(o->getType()->kind()==Bool)
1589 return o->getBoolValue();
1590 else if(o->getType()->kind()==Int)
1591 return o->getIntValue() != 0;
1593 msg << "Problem in conversion: a bool or int is expected " ;
1594 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1595 throw YACS::ENGINE::ConversionException(msg.str());
1598 template <ImplType IMPLOUT, class TOUT>
1599 struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1601 static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1603 if(o->getType()->kind()==String)
1604 return o->getStringValue();
1606 msg << "Problem in conversion: a objref(string) is expected " ;
1607 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1608 throw YACS::ENGINE::ConversionException(msg.str());
1611 template <ImplType IMPLOUT, class TOUT>
1612 struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1614 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1616 SequenceAny* sdata= (SequenceAny*)o;
1617 int length=sdata->size();
1619 for(int i=0;i<length;i++)
1621 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1626 template <ImplType IMPLOUT, class TOUT>
1627 struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1629 static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
1631 StructAny * sdata = dynamic_cast<StructAny *>(o);
1632 YASSERT(sdata != NULL);
1633 const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
1634 YASSERT(tst != NULL);
1635 for (int i=0 ; i<tst->memberCount() ; i++)
1637 string name = tst->memberName(i);
1638 TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
1643 /* End of ToYacs Convertor for NEUTRALImpl */
1645 //! FromYacs Convertor for NEUTRALImpl
1647 * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1650 struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1652 static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1654 YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1659 struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1661 static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1663 return YACS::ENGINE::AtomAny::New((int)o);
1667 struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1669 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1671 return YACS::ENGINE::AtomAny::New(o);
1675 struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1677 static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1679 return YACS::ENGINE::AtomAny::New(o);
1683 struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1685 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1687 //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
1688 if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
1689 return YACS::ENGINE::AtomAny::New(o);
1691 //Objref CORBA. prefix=IOR,corbaname,corbaloc
1692 CORBA::Object_var obref;
1695 obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1697 catch(CORBA::Exception& ex)
1699 throw ConversionException("Can't get reference to object");
1701 if(obref->_non_existent())
1702 throw ConversionException("non_existent object");
1703 if( CORBA::is_nil(obref) )
1704 throw ConversionException("Can't get reference to object");
1705 if(!obref->_is_a(t->id()))
1708 msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1709 msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1710 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1711 throw YACS::ENGINE::ConversionException(msg.str());
1714 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
1715 if(!CORBA::is_nil(gobj))
1717 DEBTRACE("It's a SALOME::GenericObj register it");
1721 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
1723 return YACS::ENGINE::AtomAny::New(o);
1728 struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1730 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1732 std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1733 //Objref are managed as string within YACS::ENGINE::Any objs
1735 any=SequenceAny::New(t->contentType());
1736 for(iter=v.begin();iter!=v.end();iter++)
1738 any->pushBack(*iter);
1741 DEBTRACE( "refcnt: " << any->getRefCnt() );
1747 struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
1749 static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
1751 StructAny * any = StructAny::New((TypeCodeStruct *)t);
1752 std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
1753 for (it=m.begin() ; it!=m.end() ; it++)
1755 any->setEltAtRank(it->first.c_str(), it->second);
1756 it->second->decrRef();
1761 /* End of FromYacs Convertor for NEUTRALImpl */
1763 //! ToYacs Convertor for CORBAImpl
1765 * This convertor converts Corba objects to intermediate YACS<TOUT> types
1766 * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1768 template <ImplType IMPLOUT, class TOUT>
1769 struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1771 static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1773 CORBA::TypeCode_var tc = o->type();
1774 if (tc->equivalent(CORBA::_tc_double))
1780 if (tc->equivalent(CORBA::_tc_long))
1787 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1788 msg << " : " << __FILE__ << ":" << __LINE__;
1789 throw YACS::ENGINE::ConversionException(msg.str());
1792 template <ImplType IMPLOUT, class TOUT>
1793 struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1795 static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1801 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1802 msg << " : " << __FILE__ << ":" << __LINE__;
1803 throw YACS::ENGINE::ConversionException(msg.str());
1806 template <ImplType IMPLOUT, class TOUT>
1807 struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1809 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1815 msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1816 msg << " : " << __FILE__ << ":" << __LINE__;
1817 throw YACS::ENGINE::ConversionException(msg.str());
1820 template <ImplType IMPLOUT, class TOUT>
1821 struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1823 static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1826 if(*o >>= CORBA::Any::to_boolean(b))
1829 msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1830 msg << " : " << __FILE__ << ":" << __LINE__;
1831 throw YACS::ENGINE::ConversionException(msg.str());
1834 template <ImplType IMPLOUT, class TOUT>
1835 struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1837 static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1839 char file[]="/tmp/XXXXXX";
1840 if(t->isA(Runtime::_tc_file))
1842 Engines::Salome_file_ptr sf;
1844 Salome_file_i* f=new Salome_file_i();
1846 f->setDistributedFile(file);
1852 else if(strncmp(t->id(),"python",6)==0)
1855 Engines::fileBlock * buffer;
1858 s=(const char*)buffer->get_buffer();
1862 std::string mystr(s,buffer->length());
1866 PyGILState_STATE gstate = PyGILState_Ensure();
1867 PyObject* mod=PyImport_ImportModule("cPickle");
1868 PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"s#",s,buffer->length());
1869 PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
1870 DEBTRACE(PyObject_REPR(pickled));
1871 std::string mystr=PyString_AsString(pickled);
1875 PyGILState_Release(gstate);
1880 msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1881 msg << " : " << __FILE__ << ":" << __LINE__;
1882 throw YACS::ENGINE::ConversionException(msg.str());
1884 else if(strncmp(t->id(),"json",4)==0)
1892 msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1893 msg << " : " << __FILE__ << ":" << __LINE__;
1894 throw YACS::ENGINE::ConversionException(msg.str());
1898 CORBA::Object_var ObjRef ;
1899 *o >>= CORBA::Any::to_object(ObjRef) ;
1900 CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1901 return (char *)objref;
1905 template <ImplType IMPLOUT, class TOUT>
1906 struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1908 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1910 CORBA::TypeCode_var tc=o->type();
1911 if (tc->kind() != CORBA::tk_sequence)
1914 msg << "Not a sequence corba type " << tc->kind();
1915 msg << " : " << __FILE__ << ":" << __LINE__;
1916 throw YACS::ENGINE::ConversionException(msg.str());
1918 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1919 DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1920 CORBA::release(dynany);
1921 DynamicAny::AnySeq_var as=ds->get_elements();
1922 int len=as->length();
1924 for(int i=0;i<len;i++)
1927 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1929 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1934 for(int i=0;i<len;i++)
1937 DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1942 template <ImplType IMPLOUT, class TOUT>
1943 struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1945 static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1947 CORBA::TypeCode_var tc=o->type();
1948 DEBTRACE(tc->kind());
1949 if (tc->kind() != CORBA::tk_struct)
1952 msg << "Not a struct corba type " << tc->kind();
1953 msg << " : " << __FILE__ << ":" << __LINE__;
1954 throw YACS::ENGINE::ConversionException(msg.str());
1956 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1957 DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1958 DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1959 CORBA::release(dynany);
1960 DynamicAny::NameValuePairSeq_var as=ds->get_members();
1961 int len=as->length();
1962 for(int i=0;i<len;i++)
1964 std::string name=as[i].id.in();
1966 CORBA::Any value=as[i].value;
1968 DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1970 TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1977 /* End of ToYacs Convertor for CORBAImpl */
1979 //! FromYacs Convertor for CORBAImpl
1981 * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
1984 struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
1986 static inline CORBA::Any* convert(const TypeCode *t,double o)
1988 CORBA::Any *any = new CORBA::Any();
1989 *any <<= (CORBA::Double)o;
1994 struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
1996 static inline CORBA::Any* convert(const TypeCode *t,long o)
1998 CORBA::Any *any = new CORBA::Any();
1999 *any <<= (CORBA::Long)o;
2004 struct convertFromYacsString<CORBAImpl,CORBA::Any*>
2006 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2008 CORBA::Any *any = new CORBA::Any();
2014 struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
2016 static inline CORBA::Any* convert(const TypeCode *t,bool o)
2018 CORBA::Any *any = new CORBA::Any();
2019 *any <<= CORBA::Any::from_boolean(o);
2024 struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
2026 static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2028 CORBA::Object_var obref;
2030 if(t->isA(Runtime::_tc_file))
2032 //It's an objref file. Convert it specially
2033 Salome_file_i* aSalome_file = new Salome_file_i();
2036 aSalome_file->setLocalFile(o.c_str());
2037 obref = aSalome_file->_this();
2038 aSalome_file->_remove_ref();
2040 catch (const SALOME::SALOME_Exception& e)
2043 msg << e.details.text;
2044 msg << " : " << __FILE__ << ":" << __LINE__;
2045 throw YACS::ENGINE::ConversionException(msg.str());
2048 else if(strncmp(t->id(),"python",6)==0 )
2050 CORBA::Any *any = new CORBA::Any();
2051 Engines::fileBlock * buffer=new Engines::fileBlock();
2052 buffer->length(o.length());
2053 CORBA::Octet *buf=buffer->get_buffer();
2054 memcpy(buf,o.c_str(),o.length());
2058 else if(strncmp(t->id(),"json",4)==0)
2060 CORBA::Any *any = new CORBA::Any();
2068 obref=getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
2070 catch(CORBA::Exception& ex)
2072 throw ConversionException("Can't get reference to object");
2074 if( CORBA::is_nil(obref) )
2076 throw ConversionException("Can't get reference to object");
2080 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2082 CORBA::Any *any = new CORBA::Any();
2085 DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2092 struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
2094 static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
2096 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2097 std::vector<CORBA::Any*>::const_iterator iter;
2099 // Build an Any from vector v
2101 if(t->contentType()->kind() == Objref)
2104 CORBA::TypeCode_var tc=getCorbaTC(t);
2106 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2107 DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
2108 ds->set_length(v.size());
2110 for(iter=v.begin();iter!=v.end();iter++)
2112 DynamicAny::DynAny_var temp=ds->current_component();
2113 CORBA::Any* a=*iter;
2114 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2117 CORBA::Object_var zzobj ;
2118 *a >>= CORBA::Any::to_object(zzobj) ;
2119 temp->insert_reference(zzobj);
2124 //delete intermediate any
2129 CORBA::Any *any=ds->to_any();
2135 struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2137 static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2139 CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2141 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2142 int nMember=tst->memberCount();
2143 DEBTRACE("nMember="<<nMember);
2145 CORBA::TypeCode_var tc=getCorbaTC(t);
2146 DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2147 DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2149 for(int i=0;i<nMember;i++)
2151 DynamicAny::DynAny_var temp=ds->current_component();
2152 const char * name=tst->memberName(i);
2153 DEBTRACE("Member name="<<name);
2154 //do not test member presence : test has been done in ToYacs convertor
2155 CORBA::Any* a=m[name];
2156 //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2157 CORBA::TypeCode_var atc = tc->member_type(i);
2158 if(atc->kind()==CORBA::tk_objref)
2160 //special treatment for objref
2161 CORBA::Object_var zzobj ;
2162 *a >>= CORBA::Any::to_object(zzobj) ;
2163 temp->insert_reference(zzobj);
2169 //delete intermediate any
2173 CORBA::Any *any=ds->to_any();
2179 /* End of FromYacs Convertor for CORBAImpl */
2181 /* Some shortcuts for CORBA to CORBA conversion */
2183 inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2185 CORBA::TypeCode_var tc = o->type();
2186 if (tc->equivalent(CORBA::_tc_double))
2190 if (tc->equivalent(CORBA::_tc_long))
2194 CORBA::Any *any = new CORBA::Any();
2195 *any <<= (CORBA::Double)d;
2199 msg << "Not a double or long corba type " << tc->kind();
2200 msg << " : " << __FILE__ << ":" << __LINE__;
2201 throw YACS::ENGINE::ConversionException(msg.str());
2204 inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2209 inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2214 inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2219 inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2224 inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2228 /* End of shortcuts for CORBA to CORBA conversion */
2230 //! ToYacs Convertor for CPPImpl
2232 * This convertor converts Python object to YACS<TOUT> types
2233 * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2235 template <ImplType IMPLOUT, class TOUT>
2236 struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2238 static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2240 if(intype->kind()==YACS::ENGINE::Double)
2244 else if(intype->kind()==YACS::ENGINE::Int)
2249 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2250 msg << " : " << __FILE__ << ":" << __LINE__;
2251 throw YACS::ENGINE::ConversionException(msg.str());
2254 template <ImplType IMPLOUT, class TOUT>
2255 struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2257 static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2259 if(intype->kind()==YACS::ENGINE::Int)
2264 msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2265 msg << " : " << __FILE__ << ":" << __LINE__;
2266 throw YACS::ENGINE::ConversionException(msg.str());
2269 /* End of ToYacs Convertor for CPPImpl */
2271 //Python conversions
2272 std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2274 return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2276 YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2278 return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2280 CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2282 return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2284 PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2286 return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2289 std::string convertPyObjectToString(PyObject* ob)
2292 PyGILState_STATE gstate = PyGILState_Ensure();
2294 std::string ss(PyString_AsString(s),PyString_Size(s));
2296 PyGILState_Release(gstate);
2301 PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2303 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2305 YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2307 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2309 CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2311 return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2313 PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2318 doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2321 std::stringstream msg;
2322 msg << "Problem in conversion: XML Document not parsed successfully ";
2323 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2324 throw YACS::ENGINE::ConversionException(msg.str());
2326 cur = xmlDocGetRootElement(doc);
2330 std::stringstream msg;
2331 msg << "Problem in conversion: empty XML Document";
2332 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2333 throw YACS::ENGINE::ConversionException(msg.str());
2337 if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2339 ob=convertXmlPyObject(t,doc,cur);
2347 std::stringstream msg;
2348 msg << "Problem in conversion: incorrect XML value";
2349 msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2350 throw YACS::ENGINE::ConversionException(msg.str());
2354 //NEUTRAL conversions
2355 PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2357 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2359 std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2361 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2363 CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2365 return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2367 YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2374 PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2376 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2378 std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2380 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2382 YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2384 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2386 CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2388 return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2391 //! Basic template checker from type TIN
2393 * This checker does nothing : throws exception
2394 * It must be partially specialize for a specific type (TIN)
2396 template <ImplType IMPLIN,class TIN,class TIN2>
2397 static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2400 msg << "Check not implemented for Implementation: " << IMPLIN ;
2401 msg << " : " << __FILE__ << ":" << __LINE__;
2402 throw YACS::ENGINE::ConversionException(msg.str());
2404 template <ImplType IMPLIN,class TIN,class TIN2>
2405 static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2408 msg << "Check not implemented for Implementation: " << IMPLIN ;
2409 msg << " : " << __FILE__ << ":" << __LINE__;
2410 throw YACS::ENGINE::ConversionException(msg.str());
2412 template <ImplType IMPLIN,class TIN,class TIN2>
2413 static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2416 msg << "Check not implemented for Implementation: " << IMPLIN ;
2417 msg << " : " << __FILE__ << ":" << __LINE__;
2418 throw YACS::ENGINE::ConversionException(msg.str());
2420 template <ImplType IMPLIN,class TIN,class TIN2>
2421 static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2424 msg << "Check not implemented for Implementation: " << IMPLIN ;
2425 msg << " : " << __FILE__ << ":" << __LINE__;
2426 throw YACS::ENGINE::ConversionException(msg.str());
2428 template <ImplType IMPLIN,class TIN,class TIN2>
2429 static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2432 msg << "Check not implemented for Implementation: " << IMPLIN ;
2433 msg << " : " << __FILE__ << ":" << __LINE__;
2434 throw YACS::ENGINE::ConversionException(msg.str());
2436 template <ImplType IMPLIN,class TIN,class TIN2>
2437 static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2440 msg << "Check not implemented for Implementation: " << IMPLIN ;
2441 msg << " : " << __FILE__ << ":" << __LINE__;
2442 throw YACS::ENGINE::ConversionException(msg.str());
2444 template <ImplType IMPLIN,class TIN,class TIN2>
2445 static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2448 msg << "Check not implemented for Implementation: " << IMPLIN ;
2449 msg << " : " << __FILE__ << ":" << __LINE__;
2450 throw YACS::ENGINE::ConversionException(msg.str());
2452 template <ImplType IMPLIN,class TIN,class TIN2>
2453 static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2456 msg << "Check not implemented for Implementation: " << IMPLIN ;
2457 msg << " : " << __FILE__ << ":" << __LINE__;
2458 throw YACS::ENGINE::ConversionException(msg.str());
2461 template <ImplType IMPLIN,class TIN,class TIN2>
2462 inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2468 return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2470 return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2472 return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2474 return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2476 return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2478 return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2480 return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2482 return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2487 msg << "Check not implemented for kind= " << tk ;
2488 msg << " : " << __FILE__ << ":" << __LINE__;
2489 throw YACS::ENGINE::ConversionException(msg.str());
2492 inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2494 if (PyFloat_Check(o))
2496 else if (PyInt_Check(o))
2498 else if(PyLong_Check(o))
2503 msg << "Not a python double ";
2504 throw YACS::ENGINE::ConversionException(msg.str());
2508 inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2510 if (PyInt_Check(o) || PyLong_Check(o))
2515 msg << "Not a python integer ";
2516 throw YACS::ENGINE::ConversionException(msg.str());
2520 inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2522 if (PyBool_Check(o))
2524 else if (PyInt_Check(o))
2526 else if(PyLong_Check(o))
2531 msg << "Not a python boolean " ;
2532 throw YACS::ENGINE::ConversionException(msg.str());
2537 inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2539 if (PyString_Check(o))
2544 msg << "Not a python string " ;
2545 throw YACS::ENGINE::ConversionException(msg.str());
2549 inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2551 if (PyString_Check(o))
2553 if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2555 else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2557 // The python object should be json compliant (to improve)
2562 // The python object should be a CORBA obj (to improve)
2567 inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2569 if(!PySequence_Check(o))
2572 msg << "python object is not a sequence " ;
2573 throw YACS::ENGINE::ConversionException(msg.str());
2575 int length=PySequence_Size(o);
2576 for(int i=0;i<length;i++)
2578 PyObject *item=PySequence_ITEM(o,i);
2581 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2583 catch(ConversionException& ex)
2586 msg << ex.what() << " for sequence element " << i;
2587 throw YACS::ENGINE::ConversionException(msg.str(),false);
2594 inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2597 if(!PyDict_Check(o))
2600 msg << "python object is not a dict " ;
2601 throw YACS::ENGINE::ConversionException(msg.str());
2603 YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2604 int nMember=tst->memberCount();
2605 for(int i=0;i<nMember;i++)
2607 std::string name=tst->memberName(i);
2608 TypeCode* tm=tst->memberType(i);
2609 value=PyDict_GetItemString(o, name.c_str());
2613 msg << "member " << name << " not present " ;
2614 throw YACS::ENGINE::ConversionException(msg.str());
2618 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2620 catch(ConversionException& ex)
2622 std::string s=" for struct member "+name;
2623 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2629 bool checkPyObject(const TypeCode *t,PyObject* ob)
2631 return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);