Salome HOME
972c540f6830a0419c6292ba67a0b84120ca6026
[modules/yacs.git] / src / runtime / TypeConversions.cxx
1 // Copyright (C) 2006-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //#define REFCNT
21 //
22 #ifdef REFCNT
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
27 #endif
28
29 #include "TypeConversions.hxx"
30 #include "ConversionException.hxx"
31 #include "RuntimeSALOME.hxx"
32 #include "Salome_file_i.hxx"
33 #include "TypeCode.hxx"
34 #include "Cstr2d.hxx"
35 #include "SALOME_GenericObj.hh"
36 #include "PythonNode.hxx"
37
38 #include <iostream>
39 #include <iomanip>
40 #include <sstream>
41
42 #ifdef WIN32
43 #include <fcntl.h>
44 #define _S_IREAD 256
45 #define _S_IWRITE 128
46 int mkstemp(char *tmpl)
47 {
48   int ret=-1;
49   mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
50   return ret;
51 }
52 #endif
53
54 //#define _DEVDEBUG_
55 #include "YacsTrace.hxx"
56
57 using namespace std;
58
59 namespace YACS
60 {
61   namespace ENGINE
62   {
63     std::string getImplName(ImplType impl)
64       {
65          switch(impl)
66            {
67            case CORBAImpl:
68              return "CORBA";
69            case PYTHONImpl:
70              return "PYTHON";
71            case NEUTRALImpl:
72              return "NEUTRAL";
73            case XMLImpl:
74              return "XML";
75            case CPPImpl:
76              return "CPP";
77            default:
78              return "UNKNOWN";
79            }
80       }
81     /*
82      * Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
83      */
84
85     typedef CORBA::TypeCode_ptr (*getCorbaTCFn)(const TypeCode *);
86
87     CORBA::TypeCode_ptr getCorbaTCNull(const TypeCode *t)
88       {
89         stringstream msg;
90         msg << "Conversion not implemented: kind= " << t->kind();
91         msg << " : " << __FILE__ << ":" << __LINE__;
92         throw YACS::ENGINE::ConversionException(msg.str());
93       }
94
95     CORBA::TypeCode_ptr getCorbaTCDouble(const TypeCode *t)
96     {
97       return CORBA::TypeCode::_duplicate(CORBA::_tc_double);
98     }
99
100     CORBA::TypeCode_ptr getCorbaTCInt(const TypeCode *t)
101     {
102       return CORBA::TypeCode::_duplicate(CORBA::_tc_long);
103     }
104
105     CORBA::TypeCode_ptr getCorbaTCString(const TypeCode *t)
106     {
107       return CORBA::TypeCode::_duplicate(CORBA::_tc_string);
108     }
109
110     CORBA::TypeCode_ptr getCorbaTCBool(const TypeCode *t)
111     {
112       return CORBA::TypeCode::_duplicate(CORBA::_tc_boolean);
113     }
114
115     CORBA::TypeCode_ptr getCorbaTCObjref(const TypeCode *t)
116     {
117       DEBTRACE( t->name() << " " << t->shortName() << " " << t->id());
118       CORBA::TypeCode_ptr tc;
119       if(strncmp(t->id(),"python",6)==0 )
120         tc= CORBA::TypeCode::_duplicate(Engines::_tc_fileBlock);
121       else if(strncmp(t->id(),"json",4)==0)
122         tc= CORBA::TypeCode::_duplicate(CORBA::_tc_string);
123       else
124         tc= getSALOMERuntime()->getOrb()->create_interface_tc(t->id(),t->shortName());
125 #ifdef REFCNT
126       DEBTRACE("refcount CORBA tc Objref: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
127 #endif
128       return tc;
129     }
130
131     CORBA::TypeCode_ptr getCorbaTCSequence(const TypeCode *t)
132     {
133       CORBA::TypeCode_var content_type=getCorbaTC(t->contentType());
134       CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_sequence_tc(0,content_type);
135 #ifdef REFCNT
136       DEBTRACE("refcount CORBA content_type: " << ((omni::TypeCode_base*)content_type.in())->pd_ref_count);
137       DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
138 #endif
139       return tc;
140     }
141
142     CORBA::TypeCode_ptr getCorbaTCStruct(const TypeCode *t)
143     {
144       CORBA::StructMemberSeq mseq;
145       YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
146       int nMember=tst->memberCount();
147       mseq.length(nMember);
148       for(int i=0;i<nMember;i++)
149         {
150           const char * name=tst->memberName(i);
151           TypeCode* tm=tst->memberType(i);
152           mseq[i].name=CORBA::string_dup(name);
153           mseq[i].type=getCorbaTC(tm);
154         }
155       CORBA::TypeCode_ptr tc= getSALOMERuntime()->getOrb()->create_struct_tc(t->id(),t->shortName(),mseq);
156 #ifdef REFCNT
157       DEBTRACE("refcount CORBA tc: " << ((omni::TypeCode_base*)tc)->pd_ref_count);
158 #endif
159       return tc;
160     }
161
162     getCorbaTCFn getCorbaTCFns[]=
163       {
164         getCorbaTCNull,
165         getCorbaTCDouble,
166         getCorbaTCInt,
167         getCorbaTCString,
168         getCorbaTCBool,
169         getCorbaTCObjref,
170         getCorbaTCSequence,
171         getCorbaTCNull,
172         getCorbaTCStruct,
173       };
174
175     CORBA::TypeCode_ptr getCorbaTC(const TypeCode *t)
176     {
177       int tk=t->kind();
178       return getCorbaTCFns[tk](t);
179     }
180
181     /*
182      * End of Functions to return a CORBA TypeCode equivalent to a YACS TypeCode
183      */
184
185     /*
186      * Section that defines functions to check adaptation from one implementation to another
187      * isAdaptable is template function that checks if TypeCode t1 from implementation IMPLIN
188      * can be converted to TypeCode t2 from implementation IMPLOUT
189      * IMPLIN is the implementation of an output port
190      * IMPLOUT is the implementation of an input port
191      * If the check is True, the input port can be adapted to the output port
192      */
193
194     template <ImplType IMPLIN,ImplType IMPLOUT> inline int isAdaptable(const TypeCode *t1,const TypeCode* t2);
195
196     template <ImplType IMPLIN,ImplType IMPLOUT>
197     struct isAdaptableDouble
198       {
199         static inline int apply(const TypeCode *t1,const TypeCode* t2)
200           {
201             if(t1->kind() == Double)return 1;
202             if(t1->kind() == Int)return 1;
203             return 0;
204           }
205       };
206     template <ImplType IMPLIN,ImplType IMPLOUT>
207     struct isAdaptableInt
208       {
209         static inline int apply(const TypeCode *t1,const TypeCode* t2)
210           {
211             if(t1->kind() == Int)return 1;
212             return 0;
213           }
214       };
215     template <ImplType IMPLIN,ImplType IMPLOUT>
216     struct isAdaptableString
217       {
218         static inline int apply(const TypeCode *t1,const TypeCode* t2)
219           {
220             if(t1->kind() == String)return 1;
221             return 0;
222           }
223       };
224     template <ImplType IMPLIN,ImplType IMPLOUT>
225     struct isAdaptableBool
226       {
227         static inline int apply(const TypeCode *t1,const TypeCode* t2)
228           {
229             if(t1->kind() == Bool)return 1;
230             if(t1->kind() == Int)return 1;
231             return 0;
232           }
233       };
234     template <ImplType IMPLIN,ImplType IMPLOUT>
235     struct isAdaptableObjref
236       {
237         static inline int apply(const TypeCode *t1,const TypeCode* t2)
238           {
239             if(t1->kind() == Objref)
240               {
241                 //The inport type must be more general than outport type
242                 if( t1->isA(t2->id()) )
243                   return 1;
244               }
245             else if(t1->kind() == Sequence)
246               {
247                 const TypeCodeSeq *t1c(dynamic_cast<const TypeCodeSeq *>(t1));
248                 if(!t1c)
249                   return 0;
250                 const TypeCode *t1cc(t1c->contentType());
251                 if(t1cc==t2)
252                   return 1;
253                 if(t1cc->kind() == Objref && std::string(t1cc->id())==std::string(t2->id()))
254                   return 1;
255               }
256             return 0;
257           }
258       };
259     template <ImplType IMPLIN,ImplType IMPLOUT>
260     struct isAdaptableSequence
261       {
262         static inline int apply(const TypeCode *t1,const TypeCode* t2)
263           {
264             if(t1->kind() == Sequence)
265               {
266                 if(isAdaptable<IMPLIN,IMPLOUT>(t1->contentType(),t2->contentType()))
267                   {
268                     return 1;
269                   }
270               }
271             return 0;
272           }
273       };
274     template <ImplType IMPLIN,ImplType IMPLOUT>
275     struct isAdaptableArray
276       {
277         static inline int apply(const TypeCode *t1,const TypeCode* t2)
278           {
279             return 0;
280           }
281       };
282     template <ImplType IMPLIN,ImplType IMPLOUT>
283     struct isAdaptableStruct
284       {
285         static inline int apply(const TypeCode *t1,const TypeCode* t2)
286           {
287             if(t1->kind() == Struct)
288               {
289                 if( t1->isA(t2) )
290                   return 1;
291               }
292             return 0;
293           }
294       };
295
296     /*
297      * Function to check adaptation from implementation 1 (IMPLIN,t1) to implementation 2 (IMPLOUT,t2)
298      * t1 is the IMPLIN output port type
299      * t2 is the IMPLOUT input port type
300      */
301     template <ImplType IMPLIN,ImplType IMPLOUT>
302     inline int isAdaptable(const TypeCode *t1,const TypeCode* t2)
303       {
304          switch(t2->kind())
305            {
306            case Double:
307              return isAdaptableDouble<IMPLIN,IMPLOUT>::apply(t1,t2);
308            case Int:
309              return isAdaptableInt<IMPLIN,IMPLOUT>::apply(t1,t2);
310            case String:
311              return isAdaptableString<IMPLIN,IMPLOUT>::apply(t1,t2);
312            case Bool:
313              return isAdaptableBool<IMPLIN,IMPLOUT>::apply(t1,t2);
314            case Objref:
315              return isAdaptableObjref<IMPLIN,IMPLOUT>::apply(t1,t2);
316            case Sequence:
317              return isAdaptableSequence<IMPLIN,IMPLOUT>::apply(t1,t2);
318            case Array:
319              return isAdaptableArray<IMPLIN,IMPLOUT>::apply(t1,t2);
320            case Struct:
321              return isAdaptableStruct<IMPLIN,IMPLOUT>::apply(t1,t2);
322            default:
323              break;
324            }
325          return 0;
326       }
327
328     //xxx to Python adaptations
329     int isAdaptableCorbaPyObject(const TypeCode *t1,const TypeCode *t2)
330     {
331       return isAdaptable<PYTHONImpl,CORBAImpl>(t1,t2);
332     }
333     int isAdaptableNeutralPyObject(const TypeCode * t1, const TypeCode * t2)
334     {
335       return isAdaptable<PYTHONImpl,NEUTRALImpl>(t1,t2);
336     }
337     int isAdaptablePyObjectPyObject(const TypeCode *t1,const TypeCode *t2)
338     {
339       return isAdaptable<PYTHONImpl,PYTHONImpl>(t1,t2);
340     }
341
342     //xxx to Neutral adaptations
343     int isAdaptableCorbaNeutral(const TypeCode *t1,const TypeCode *t2)
344     {
345       return isAdaptable<NEUTRALImpl,CORBAImpl>(t1,t2);
346     }
347     int isAdaptablePyObjectNeutral(const TypeCode *t1,const TypeCode *t2)
348     {
349       return isAdaptable<NEUTRALImpl,PYTHONImpl>(t1,t2);
350     }
351     int isAdaptableXmlNeutral(const TypeCode *t1,const TypeCode *t2)
352     {
353       return isAdaptable<NEUTRALImpl,XMLImpl>(t1,t2);
354     }
355     int isAdaptableNeutralNeutral(const TypeCode *t1, const TypeCode *t2)
356     {
357       return isAdaptableNeutralCorba(t1, t2);
358     }
359
360     //xxx to XML adaptations
361     int isAdaptableNeutralXml(const TypeCode * t1, const TypeCode * t2)
362     {
363       return isAdaptable<XMLImpl,NEUTRALImpl>(t1,t2);
364     }
365
366     //xxx to Corba adaptations
367     int isAdaptableNeutralCorba(const TypeCode *t1,const TypeCode *t2)
368     {
369       return isAdaptable<CORBAImpl,NEUTRALImpl>(t1,t2);
370     }
371     int isAdaptableXmlCorba(const TypeCode *t1,const TypeCode *t2)
372     {
373       return isAdaptable<CORBAImpl,XMLImpl>(t1,t2);
374     }
375     int isAdaptableCorbaCorba(const TypeCode *t1,const TypeCode *t2)
376     {
377       return isAdaptable<CORBAImpl,CORBAImpl>(t1,t2);
378     }
379     int isAdaptablePyObjectCorba(const TypeCode *t1,const TypeCode *t2)
380     {
381       return isAdaptable<CORBAImpl,PYTHONImpl>(t1,t2);
382     }
383
384     //! Basic template convertor from type TIN to Yacs<TOUT> type
385     /*!
386      * This convertor does nothing : throws exception
387      * It must be partially specialize for a specific type (TIN)
388      */
389     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
390     struct convertToYacsDouble
391     {
392       static inline double convert(const TypeCode *t,TIN o,TIN2 aux)
393         {
394           stringstream msg;
395           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
396           msg << " : " << __FILE__ << ":" << __LINE__;
397           throw YACS::ENGINE::ConversionException(msg.str());
398         }
399     };
400     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
401     struct convertToYacsInt
402     {
403       static inline long convert(const TypeCode *t,TIN o,TIN2 aux)
404         {
405           stringstream msg;
406           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
407           msg << " : " << __FILE__ << ":" << __LINE__;
408           throw YACS::ENGINE::ConversionException(msg.str());
409         }
410     };
411     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
412     struct convertToYacsString
413     {
414       static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux)
415         {
416           stringstream msg;
417           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
418           msg << " : " << __FILE__ << ":" << __LINE__;
419           throw YACS::ENGINE::ConversionException(msg.str());
420         }
421     };
422     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
423     struct convertToYacsBool
424     {
425       static inline bool convert(const TypeCode *t,TIN o,TIN2 aux)
426         {
427           stringstream msg;
428           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
429           msg << " : " << __FILE__ << ":" << __LINE__;
430           throw YACS::ENGINE::ConversionException(msg.str());
431         }
432     };
433     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
434     struct convertToYacsObjref
435     {
436       static inline std::string convert(const TypeCode *t,TIN o,TIN2 aux,int protocol)
437         {
438           stringstream msg;
439           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
440           msg << " : " << __FILE__ << ":" << __LINE__;
441           throw YACS::ENGINE::ConversionException(msg.str());
442         }
443     };
444     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
445     struct convertToYacsSequence
446     {
447       static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
448         {
449           stringstream msg;
450           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
451           msg << " : " << __FILE__ << ":" << __LINE__;
452           throw YACS::ENGINE::ConversionException(msg.str());
453         }
454     };
455     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
456     struct convertToYacsArray
457     {
458       static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::vector<TOUT>& v)
459         {
460           stringstream msg;
461           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
462           msg << " : " << __FILE__ << ":" << __LINE__;
463           throw YACS::ENGINE::ConversionException(msg.str());
464         }
465     };
466     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
467     struct convertToYacsStruct
468     {
469       static inline void convert(const TypeCode *t,TIN o,TIN2 aux,std::map<std::string,TOUT>& v)
470         {
471           stringstream msg;
472           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLIN << " to: " << IMPLOUT;
473           msg << " : " << __FILE__ << ":" << __LINE__;
474           throw YACS::ENGINE::ConversionException(msg.str());
475         }
476     };
477
478     //! Basic convertor from Yacs<TOUT> type to full TOUT type
479     /*!
480      *
481      */
482     template <ImplType IMPLOUT, class TOUT>
483     struct convertFromYacsDouble
484     {
485       static inline TOUT convert(const TypeCode *t,double o)
486         {
487           stringstream msg;
488           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
489           msg << " : " << __FILE__ << ":" << __LINE__;
490           throw YACS::ENGINE::ConversionException(msg.str());
491         }
492     };
493     template <ImplType IMPLOUT, class TOUT>
494     struct convertFromYacsInt
495     {
496       static inline TOUT convert(const TypeCode *t,long o)
497         {
498           stringstream msg;
499           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
500           msg << " : " << __FILE__ << ":" << __LINE__;
501           throw YACS::ENGINE::ConversionException(msg.str());
502         }
503     };
504     template <ImplType IMPLOUT, class TOUT>
505     struct convertFromYacsString
506     {
507       static inline TOUT convert(const TypeCode *t,std::string o)
508         {
509           stringstream msg;
510           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
511           msg << " : " << __FILE__ << ":" << __LINE__;
512           throw YACS::ENGINE::ConversionException(msg.str());
513         }
514     };
515     template <ImplType IMPLOUT, class TOUT>
516     struct convertFromYacsBool
517     {
518       static inline TOUT convert(const TypeCode *t,bool o)
519         {
520           stringstream msg;
521           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
522           msg << " : " << __FILE__ << ":" << __LINE__;
523           throw YACS::ENGINE::ConversionException(msg.str());
524         }
525     };
526     template <ImplType IMPLOUT, class TOUT>
527     struct convertFromYacsObjref
528     {
529       static inline TOUT convert(const TypeCode *t,std::string o)
530         {
531           stringstream msg;
532           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
533           msg << " : " << __FILE__ << ":" << __LINE__;
534           throw YACS::ENGINE::ConversionException(msg.str());
535         }
536     };
537     template <ImplType IMPLOUT, class TOUT>
538     struct convertFromYacsSequence
539     {
540       static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
541         {
542           stringstream msg;
543           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
544           msg << " : " << __FILE__ << ":" << __LINE__;
545           throw YACS::ENGINE::ConversionException(msg.str());
546         }
547     };
548     template <ImplType IMPLOUT, class TOUT>
549     struct convertFromYacsArray
550     {
551       static inline TOUT convert(const TypeCode *t,std::vector<TOUT>& v)
552         {
553           stringstream msg;
554           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
555           msg << " : " << __FILE__ << ":" << __LINE__;
556           throw YACS::ENGINE::ConversionException(msg.str());
557         }
558     };
559     template <ImplType IMPLOUT, class TOUT>
560     struct convertFromYacsStruct
561     {
562       static inline TOUT convert(const TypeCode *t,std::map<std::string,TOUT>& v)
563         {
564           stringstream msg;
565           msg << "Conversion not implemented: kind= " << t->kind() << " Implementation: " << IMPLOUT;
566           msg << " : " << __FILE__ << ":" << __LINE__;
567           throw YACS::ENGINE::ConversionException(msg.str());
568         }
569     };
570     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
571     inline TOUT convertDouble(const TypeCode *t,TIN o,TIN2 aux)
572     {
573       double d=convertToYacsDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
574       DEBTRACE( d );
575       TOUT r=convertFromYacsDouble<IMPLOUT,TOUT>::convert(t,d);
576       return r;
577     }
578     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
579     inline TOUT convertInt(const TypeCode *t,TIN o,TIN2 aux)
580     {
581       long d=convertToYacsInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
582       DEBTRACE( d );
583       TOUT r=convertFromYacsInt<IMPLOUT,TOUT>::convert(t,d);
584       return r;
585     }
586     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
587     inline TOUT convertString(const TypeCode *t,TIN o,TIN2 aux)
588     {
589       std::string d=convertToYacsString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
590       DEBTRACE( d );
591       TOUT r=convertFromYacsString<IMPLOUT,TOUT>::convert(t,d);
592       return r;
593     }
594     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
595     inline TOUT convertBool(const TypeCode *t,TIN o,TIN2 aux)
596     {
597       double d=convertToYacsBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux);
598       DEBTRACE( d );
599       TOUT r=convertFromYacsBool<IMPLOUT,TOUT>::convert(t,d);
600       return r;
601     }
602     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
603     inline TOUT convertObjref(const TypeCode *t,TIN o,TIN2 aux)
604     {
605       int protocol=-1;
606       if(IMPLOUT==XMLImpl)
607         protocol=0;//to avoid presence of \0 into XML generated file
608       if(IMPLOUT==NEUTRALImpl)
609         protocol=4;
610       std::string d=convertToYacsObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,protocol);
611       DEBTRACE( d );
612       TOUT r=convertFromYacsObjref<IMPLOUT,TOUT>::convert(t,d);
613       return r;
614     }
615
616     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
617     inline TOUT convertSequence(const TypeCode *t,TIN o,TIN2 aux)
618     {
619       std::vector<TOUT> v;
620       convertToYacsSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
621       TOUT r=convertFromYacsSequence<IMPLOUT,TOUT>::convert(t,v);
622       return r;
623     }
624     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
625     inline TOUT convertArray(const TypeCode *t,TIN o,TIN2 aux)
626     {
627       std::vector<TOUT> v;
628       convertToYacsArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
629       TOUT r=convertFromYacsArray<IMPLOUT,TOUT>::convert(t,v);
630       return r;
631     }
632     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
633     inline TOUT convertStruct(const TypeCode *t,TIN o,TIN2 aux)
634     {
635       std::map<std::string,TOUT> v;
636       convertToYacsStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>::convert(t,o,aux,v);
637       TOUT r=convertFromYacsStruct<IMPLOUT,TOUT>::convert(t,v);
638       return r;
639     }
640
641     template <ImplType IMPLIN,class TIN,class TIN2,ImplType IMPLOUT, class TOUT>
642     inline TOUT YacsConvertor(const TypeCode *t,TIN o,TIN2 aux)
643       {
644          int tk=t->kind();
645          switch(t->kind())
646            {
647            case Double:
648              return convertDouble<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
649            case Int:
650              return convertInt<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
651            case String:
652              return convertString<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
653            case Bool:
654              return convertBool<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
655            case Objref:
656              return convertObjref<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
657            case Sequence:
658              return convertSequence<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
659            case Array:
660              return convertArray<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
661            case Struct:
662              return convertStruct<IMPLIN,TIN,TIN2,IMPLOUT,TOUT>(t,o,aux);
663            default:
664              break;
665            }
666          stringstream msg;
667          msg << "Conversion not implemented: kind= " << tk << " Implementation: " << IMPLOUT;
668          msg << " : " << __FILE__ << ":" << __LINE__;
669          throw YACS::ENGINE::ConversionException(msg.str());
670       }
671
672     //! ToYacs Convertor for PYTHONImpl
673     /*!
674      * This convertor converts Python object to YACS<TOUT> types
675      * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
676      */
677     template <ImplType IMPLOUT, class TOUT>
678     struct convertToYacsDouble<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
679     {
680       static inline double convert(const TypeCode *t,PyObject* o,void*)
681         {
682           double x;
683           x=PyFloat_AsDouble(o);
684           if( PyErr_Occurred() )
685             {
686               PyErr_Restore(nullptr,nullptr,nullptr);
687               stringstream msg;
688               msg << "Not a python double. ";
689 #ifdef _DEVDEBUG_
690               msg << "kind=" << t->kind() ;
691               msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
692 #endif
693               throw YACS::ENGINE::ConversionException(msg.str());
694             }
695           return x;
696         }
697     };
698     template <ImplType IMPLOUT, class TOUT>
699     struct convertToYacsInt<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
700     {
701       static inline long convert(const TypeCode *t,PyObject* o,void*)
702         {
703           long l;
704           l=PyLong_AsLong(o);
705           if( PyErr_Occurred() )
706             {
707               PyErr_Restore(nullptr,nullptr,nullptr);
708               stringstream msg;
709               msg << "Not a python integer. ";
710 #ifdef _DEVDEBUG_
711               msg << "kind=" << t->kind() ;
712               msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
713 #endif
714               throw YACS::ENGINE::ConversionException(msg.str());
715             }
716           return l;
717         }
718     };
719     template <ImplType IMPLOUT, class TOUT>
720     struct convertToYacsString<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
721     {
722       static inline std::string convert(const TypeCode *t,PyObject* o,void*)
723         {
724           std::string s;
725           if (PyUnicode_Check(o))
726             {
727               Py_ssize_t size;
728               const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
729               if (!ptr)
730                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
731               s.assign(ptr, size);
732             }
733           else
734             {
735               stringstream msg;
736               msg << "Not a python string. ";
737 #ifdef _DEVDEBUG_
738               msg << "kind=" << t->kind() ;
739               msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
740 #endif
741               throw YACS::ENGINE::ConversionException(msg.str());
742             }
743           return s;
744         }
745     };
746     template <ImplType IMPLOUT, class TOUT>
747     struct convertToYacsBool<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
748     {
749       static inline bool convert(const TypeCode *t,PyObject* o,void*)
750         {
751           bool l;
752           if (PyBool_Check(o))
753               l=(o==Py_True);
754           else if(PyLong_Check(o))
755               l=(PyLong_AsLong(o)!=0);
756           else
757             {
758               stringstream msg;
759               msg << "Not a python boolean. ";
760 #ifdef _DEVDEBUG_
761               msg << "kind=" << t->kind() ;
762               msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
763 #endif
764               throw YACS::ENGINE::ConversionException(msg.str());
765             }
766           return l;
767         }
768     };
769     template <ImplType IMPLOUT, class TOUT>
770     struct convertToYacsObjref<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
771     {
772       static inline std::string convert(const TypeCode *t,PyObject* o,void*,int protocol)
773         {
774           if (PyUnicode_Check(o) && strncmp(t->id(),"python",6)!=0)
775             {
776               // the objref is used by Python as a string (prefix:value) keep it as a string
777               Py_ssize_t size;
778               std::string s;
779               const char *ptr = PyUnicode_AsUTF8AndSize(o, &size);
780               if (!ptr)
781                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
782               s.assign(ptr, size);
783               return s;
784             }
785           if(strncmp(t->id(),"python",6)==0)
786             {
787               bool somthingToDo = YACS::ENGINE::PythonEntry::GetDestroyStatus(o);
788               if( somthingToDo )
789                 YACS::ENGINE::PythonEntry::DoNotTouchFileIfProxy(o);
790               // It's a native Python object pickle it
791               PyObject* mod=PyImport_ImportModule("pickle");
792               PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",o,protocol);
793               if( somthingToDo )
794               {
795                 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(o);
796                 YACS::ENGINE::PythonEntry::IfProxyDoSomething(o,"incrRef");
797               }
798               DEBTRACE(PyObject_Repr(pickled) );
799               Py_DECREF(mod);
800               if(pickled==NULL)
801                 {
802                   PyErr_Print();
803                   throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
804                 }
805               std::string mystr(PyBytes_AsString(pickled),PyBytes_Size(pickled));
806               Py_DECREF(pickled);
807               return mystr;
808             }
809           else if(strncmp(t->id(),"json",4)==0)
810             {
811               // It's a Python  object convert it to json 
812               PyObject* mod=PyImport_ImportModule("simplejson");
813               if(mod==NULL)
814                 {
815                   PyErr_Print();
816                   throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
817                 }
818               PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"O",o);
819               Py_DECREF(mod);
820               if(pickled==NULL)
821                 {
822                   PyErr_Print();
823                   throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
824                 }
825               std::string mystr=PyBytes_AsString(pickled);
826               Py_DECREF(pickled);
827               return mystr;
828             }
829           else
830             {
831               // It should be a CORBA Object convert it to an IOR string
832               PyObject *pystring=PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),(char *)"object_to_string",(char *)"O",o);
833               if(pystring==NULL)
834                 {
835                   PyErr_Print();
836                   throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl");
837                 }
838               Py_ssize_t size;
839               std::string mystr;
840               const char *ptr = PyUnicode_AsUTF8AndSize(pystring, &size);
841               if (!ptr)
842                 throw YACS::ENGINE::ConversionException("Conversion from PyUnicode to string failed");
843               mystr.assign(ptr, size);
844               Py_DECREF(pystring);
845               return mystr;
846             }
847         }
848     };
849     template <ImplType IMPLOUT, class TOUT>
850     struct convertToYacsSequence<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
851     {
852       static inline void convert(const TypeCode *t,PyObject* o,void*,std::vector<TOUT>& v)
853         {
854           if(!PySequence_Check(o))
855             {
856               stringstream msg;
857               msg << "Problem in conversion: the python object is not a sequence " << std::endl;
858 #ifdef _DEVDEBUG_
859               msg << " ( " << __FILE__ << ":" << __LINE__ << ")";
860 #endif
861               throw YACS::ENGINE::ConversionException(msg.str());
862             }
863           int length=PySequence_Size(o);
864           GURU_YACSTRACE("convertToYacsSequence : length = " << length);
865           v.resize(length);
866           for(int i=0;i<length;i++)
867             {
868               PyObject *item=PySequence_ITEM(o,i);
869               GURU_YACSTRACE("convertToYacsSequence : analyze if proxy");
870               bool somthingToDo = YACS::ENGINE::PythonEntry::IsProxy(item);
871               if( somthingToDo )
872               {
873                 GURU_YACSTRACE("convertToYacsSequence : it s proxy -> unlink");
874                 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(item);
875                 GURU_YACSTRACE("convertToYacsSequence : it s proxy -> incrRef");
876                 YACS::ENGINE::PythonEntry::IfProxyDoSomething(item,"incrRef");
877               }
878 #ifdef _DEVDEBUG_
879               std::cerr <<"item[" << i << "]=";
880               PyObject_Print(item,stderr,Py_PRINT_RAW);
881               std::cerr << std::endl;
882 #endif
883               DEBTRACE( "item refcnt: " << item->ob_refcnt );
884               try
885                 {
886                   TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(t->contentType(),item,0);
887                   v[i]=ro;
888                   Py_DECREF(item);
889                 }
890               catch(ConversionException& ex)
891                 {
892                   stringstream msg;
893                   msg << ex.what() << " for sequence element " << i;
894                   throw YACS::ENGINE::ConversionException(msg.str(),false);
895                 }
896             }
897         }
898     };
899     template <ImplType IMPLOUT, class TOUT>
900     struct convertToYacsStruct<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>
901     {
902       static inline void convert(const TypeCode *t,PyObject* o,void*,std::map<std::string,TOUT>& m)
903         {
904           DEBTRACE( "o refcnt: " << o->ob_refcnt );
905           PyObject *key, *value;
906           YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
907           int nMember=tst->memberCount();
908           DEBTRACE("nMember="<<nMember);
909           for(int i=0;i<nMember;i++)
910             {
911               std::string name=tst->memberName(i);
912               DEBTRACE("Member name="<<name);
913               TypeCode* tm=tst->memberType(i);
914               value=PyDict_GetItemString(o, name.c_str());
915               if(value==NULL)
916                 {
917                   //member name not present
918                   //TODO delete all allocated objects in m
919 #ifdef _DEVDEBUG_
920                   PyObject_Print(o,stderr,Py_PRINT_RAW);
921                   std::cerr << std::endl;
922 #endif
923                   stringstream msg;
924                   msg << "member " << name << " not present " ;
925                   throw YACS::ENGINE::ConversionException(msg.str());
926                 }
927               DEBTRACE( "value refcnt: " << value->ob_refcnt );
928               try
929                 {
930                   TOUT ro=YacsConvertor<PYTHONImpl,PyObject*,void*,IMPLOUT,TOUT>(tm,value,0);
931                   m[name]=ro;
932                 }
933               catch(ConversionException& ex)
934                 {
935                   std::string s=" for struct member "+name;
936                   throw YACS::ENGINE::ConversionException(ex.what()+s,false);
937                 }
938             }
939         }
940     };
941     /* End of ToYacs Convertor for PYTHONImpl */
942
943     //! FromYacs Convertor for PYTHONImpl
944     /*!
945      * Convert YACS<PyObject*> intermediate types to PyObject* types (PYTHONImpl)
946      */
947     template <>
948     struct convertFromYacsDouble<PYTHONImpl,PyObject*>
949     {
950       static inline PyObject* convert(const TypeCode *t,double o)
951         {
952           PyObject *pyob=PyFloat_FromDouble(o);
953           return pyob;
954         }
955     };
956     template <>
957     struct convertFromYacsInt<PYTHONImpl,PyObject*>
958     {
959       static inline PyObject* convert(const TypeCode *t,long o)
960         {
961           PyObject *pyob=PyLong_FromLong(o);
962           return pyob;
963         }
964     };
965     template <>
966     struct convertFromYacsString<PYTHONImpl,PyObject*>
967     {
968       static inline PyObject* convert(const TypeCode *t,std::string& o)
969         {
970           return PyUnicode_FromString(o.c_str());
971         }
972     };
973     template <>
974     struct convertFromYacsBool<PYTHONImpl,PyObject*>
975     {
976       static inline PyObject* convert(const TypeCode *t,bool o)
977         {
978           return PyBool_FromLong ((long)o);
979         }
980     };
981     template <>
982     struct convertFromYacsObjref<PYTHONImpl,PyObject*>
983     {
984       static inline PyObject* convert(const TypeCode *t,std::string& o)
985         {
986           if(o=="")
987             {
988               Py_INCREF(Py_None);
989               return Py_None;
990             }
991           if(t->isA(Runtime::_tc_file))
992             {
993               //It's an objref file. Convert it specially
994               return PyUnicode_FromString(o.c_str());
995             }
996           if(strncmp(t->id(),"python",6)==0) //ex: "python:obj:1.0"
997             {
998               //It's a python pickled object, unpickled it
999               PyObject* mod=PyImport_ImportModule("pickle");
1000               GURU_YACSTRACE("convertFromYacsObjref : unpickling...");
1001               PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",o.c_str(),o.length());
1002               GURU_YACSTRACE("convertFromYacsObjref : unpickling done...");
1003               DEBTRACE(PyObject_Repr(ob));
1004               Py_DECREF(mod);
1005               GURU_YACSTRACE("convertFromYacsObjref : Analyze if it's a proxy");
1006               bool somthingToDo = YACS::ENGINE::PythonEntry::IsProxy(ob);
1007               if( somthingToDo )
1008               {
1009                 // no incrRef here because the incrRef has been done before dumps that construct o.
1010                 GURU_YACSTRACE("convertFromYacsObjref : It's a proxy -> activate on deletion");
1011                 YACS::ENGINE::PythonEntry::UnlinkOnDestructorIfProxy(ob);
1012               }
1013               if(ob==NULL)
1014                 {
1015                   PyErr_Print();
1016                   throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1017                 }
1018               return ob;
1019             }
1020           if(strncmp(t->id(),"json",4)==0)
1021             {
1022               // It's a json object unpack it
1023               PyObject* mod=PyImport_ImportModule("simplejson");
1024               if(mod==NULL)
1025                 {
1026                   PyErr_Print();
1027                   throw YACS::ENGINE::ConversionException("Problem in convertToYacsObjref<PYTHONImpl: no simplejson module");
1028                 }
1029               PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y",o.c_str());
1030               Py_DECREF(mod);
1031               if(ob==NULL)
1032                 {
1033                   PyErr_Print();
1034                   throw YACS::ENGINE::ConversionException("Problem in convertFromYacsObjref<PYTHONImpl");
1035                 }
1036               return ob;
1037             }
1038
1039           /* another way to convert IOR string to CORBA PyObject 
1040           PyObject* ob= PyObject_CallMethod(getSALOMERuntime()->getPyOrb(),"string_to_object","s",o.c_str());
1041           DEBTRACE( "Objref python refcnt: " << ob->ob_refcnt );
1042           return ob;
1043           */
1044
1045           //Objref CORBA. prefix=IOR,corbaname,corbaloc
1046           CORBA::Object_var obref;
1047           try
1048             {
1049               obref = getSALOMERuntime()->getFromNS(o.c_str());
1050 #ifdef REFCNT
1051               DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1052 #endif
1053             }
1054           catch(CORBA::Exception& ex) 
1055             {
1056               DEBTRACE( "Can't get reference to object." );
1057               throw ConversionException("Can't get reference to object");
1058             }
1059
1060           if(obref->_non_existent())
1061             {
1062               throw ConversionException("non_existent object");
1063             }
1064
1065           if( CORBA::is_nil(obref) )
1066             {
1067               DEBTRACE( "Can't get reference to object (or it was nil)." );
1068               throw ConversionException("Can't get reference to object");
1069             }
1070
1071           if(!obref->_is_a(t->id()))
1072             {
1073               stringstream msg;
1074               msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1075               msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1076               msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1077               throw YACS::ENGINE::ConversionException(msg.str());
1078             }
1079 #ifdef REFCNT
1080           DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1081 #endif
1082 #ifdef _DEVDEBUG_
1083           std::cerr << "_PD_repoId: " << obref->_PD_repoId << std::endl;
1084           std::cerr << "_mostDerivedRepoId: " << obref->_PR_getobj()->_mostDerivedRepoId()  << std::endl;
1085 #endif
1086
1087           //hold_lock is true: caller is supposed to hold the GIL.
1088           //omniorb will not take the GIL
1089           PyObject* ob= getSALOMERuntime()->getApi()->cxxObjRefToPyObjRef(obref, 1);
1090
1091 #ifdef _DEVDEBUG_
1092           PyObject_Print(ob,stderr,Py_PRINT_RAW);
1093           std::cerr << std::endl;
1094           std::cerr << "obref is a generic: " << obref->_is_a("IDL:SALOME/GenericObj:1.0") << std::endl;
1095           PyObject_Print(getSALOMERuntime()->get_omnipy(),stderr,Py_PRINT_RAW);
1096           std::cerr << std::endl;
1097 #endif
1098
1099           //ob is a CORBA::Object. Try to convert it to more specific type SALOME/GenericObj
1100           if(obref->_is_a("IDL:SALOME/GenericObj:1.0"))
1101             {
1102               PyObject *result = PyObject_CallMethod(getSALOMERuntime()->get_omnipy(), (char *)"narrow", (char *)"Osi",ob,"IDL:SALOME/GenericObj:1.0",1);
1103               if(result==NULL)
1104                 PyErr_Clear();//Exception during narrow. Keep ob
1105               else if(result==Py_None)
1106                 Py_DECREF(result); //Can't narrow. Keep ob
1107               else
1108                 {
1109                   //Can narrow. Keep result
1110 #ifdef _DEVDEBUG_
1111                   PyObject_Print(result,stderr,Py_PRINT_RAW);
1112                   std::cerr << std::endl;
1113 #endif
1114                   Py_DECREF(ob);
1115                   ob=result;
1116                 }
1117             }
1118
1119 #ifdef REFCNT
1120           DEBTRACE("obref refCount: " << obref->_PR_getobj()->pd_refCount);
1121 #endif
1122           return ob;
1123         }
1124     };
1125
1126     template <>
1127     struct convertFromYacsSequence<PYTHONImpl,PyObject*>
1128     {
1129       static inline PyObject* convert(const TypeCode *t,std::vector<PyObject*>& v)
1130         {
1131           std::vector<PyObject*>::const_iterator iter;
1132           PyObject *pyob = PyList_New(v.size());
1133           int i=0;
1134           for(iter=v.begin();iter!=v.end();iter++)
1135             {
1136               PyObject* item=*iter;
1137               DEBTRACE( "item refcnt: " << item->ob_refcnt );
1138               PyList_SetItem(pyob,i,item);
1139               DEBTRACE( "item refcnt: " << item->ob_refcnt );
1140               i++;
1141             }
1142           return pyob;
1143         }
1144     };
1145     template <>
1146     struct convertFromYacsStruct<PYTHONImpl,PyObject*>
1147     {
1148       static inline PyObject* convert(const TypeCode *t,std::map<std::string,PyObject*>& m)
1149         {
1150           PyObject *pyob = PyDict_New();
1151           std::map<std::string, PyObject*>::const_iterator pt;
1152           for(pt=m.begin();pt!=m.end();pt++)
1153             {
1154               std::string name=(*pt).first;
1155               PyObject* item=(*pt).second;
1156               DEBTRACE( "item refcnt: " << item->ob_refcnt );
1157               PyDict_SetItemString(pyob,name.c_str(),item);
1158               Py_DECREF(item);
1159               DEBTRACE( "item refcnt: " << item->ob_refcnt );
1160             }
1161           DEBTRACE( "pyob refcnt: " << pyob->ob_refcnt );
1162           return pyob;
1163         }
1164     };
1165     /* End of FromYacs Convertor for PYTHONImpl */
1166
1167     //! ToYacs Convertor for XMLImpl
1168     /*!
1169      * Partial specialization for XML implementation (XMLImpl)
1170      * This convertor converts xml object to YACS<TOUT> types
1171      */
1172     template <ImplType IMPLOUT, class TOUT>
1173     struct convertToYacsDouble<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1174     {
1175       static inline double convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1176         {
1177           double d=0;
1178           cur = cur->xmlChildrenNode;
1179           while (cur != NULL)
1180             {
1181               if ((!xmlStrcmp(cur->name, (const xmlChar *)"double")))
1182                 {
1183                   //wait a double, got a double
1184                   xmlChar * s = NULL;
1185                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1186                   if (s)
1187                     {
1188                       DEBTRACE( "convertToYacsDouble " << (const char *)s );
1189                       d=Cstr2d((const char *)s);
1190                       xmlFree(s);
1191                     }
1192                   else
1193                     {
1194                       DEBTRACE("############### workaround to improve...");
1195                     }
1196                   return d;
1197                 }
1198               else if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1199                 {
1200                   //wait a double, got an int
1201                   xmlChar * s = NULL;
1202                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1203                   if (s)
1204                     {
1205                       DEBTRACE( "convertToYacsDouble " << (const char *)s );
1206                       d=Cstr2d((const char *)s);
1207                       xmlFree(s);
1208                     }
1209                   else
1210                     {
1211                       DEBTRACE("############### workaround to improve...");
1212                     }
1213                   return d;
1214                 }
1215               cur = cur->next;
1216             }
1217           stringstream msg;
1218           msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type:  " << t->id() ;
1219           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1220           throw YACS::ENGINE::ConversionException(msg.str());
1221         }
1222     };
1223     template <ImplType IMPLOUT, class TOUT>
1224     struct convertToYacsInt<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1225     {
1226       static inline long convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1227         {
1228           long d=0;
1229           cur = cur->xmlChildrenNode;
1230           while (cur != NULL)
1231             {
1232               if ((!xmlStrcmp(cur->name, (const xmlChar *)"int")))
1233                 {
1234                   xmlChar * s = NULL;
1235                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1236                   if (s)
1237                     {
1238                       DEBTRACE( "convertToYacsInt " << (const char *)s );
1239                       d=atol((const char *)s);
1240                       xmlFree(s);
1241                     }
1242                   else
1243                     {
1244                       DEBTRACE("############### workaround to improve...");
1245                     }
1246                   return d;
1247                 }
1248               cur = cur->next;
1249             }
1250           stringstream msg;
1251           msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type:  " << t->id() ;
1252           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1253           throw YACS::ENGINE::ConversionException(msg.str());
1254         }
1255     };
1256     template <ImplType IMPLOUT, class TOUT>
1257     struct convertToYacsString<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1258     {
1259       static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1260         {
1261           cur = cur->xmlChildrenNode;
1262           while (cur != NULL)
1263             {
1264               if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))
1265                 {
1266                   //wait a string, got a string
1267                   xmlChar * s = NULL;
1268                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1269                   if(s==0)return "";
1270                   DEBTRACE( "convertToYacsString " << (const char *)s );
1271                   std::string mystr=std::string((const char *)s);
1272                   xmlFree(s);
1273                   return mystr;
1274                 }
1275               cur = cur->next;
1276             }
1277           stringstream msg;
1278           msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type:  " << t->id() ;
1279           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1280           throw YACS::ENGINE::ConversionException(msg.str());
1281         }
1282     };
1283     template <ImplType IMPLOUT, class TOUT>
1284     struct convertToYacsBool<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1285     {
1286       static inline bool convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
1287         {
1288           cur = cur->xmlChildrenNode;
1289           while (cur != NULL)
1290             {
1291               if ((!xmlStrcmp(cur->name, (const xmlChar *)"boolean")))
1292                 {
1293                   //wait a boolean, got a boolean
1294                   xmlChar * s = NULL;
1295                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1296                   bool ob =false;
1297                   if (s)
1298                     {
1299                       DEBTRACE( "convertToYacsBool " << (const char *)s );
1300                       ob=atoi((const char*)s)!=0;
1301                       xmlFree(s);
1302                     }
1303                   else
1304                     {
1305                       DEBTRACE("############### workaround to improve...");
1306                     }
1307                   return ob;
1308                 }
1309               cur = cur->next;
1310             }
1311           stringstream msg;
1312           msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type:  " << t->id() ;
1313           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1314           throw YACS::ENGINE::ConversionException(msg.str());
1315         }
1316     };
1317     template <ImplType IMPLOUT, class TOUT>
1318     struct convertToYacsObjref<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1319     {
1320       static inline std::string convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,int protocol)
1321         {
1322           cur = cur->xmlChildrenNode;
1323           while (cur != NULL)
1324             {
1325               if ((!xmlStrcmp(cur->name, (const xmlChar *)"objref")))
1326                 {
1327                   //we wait a objref, we have got a objref
1328                   xmlChar * s = NULL;
1329                   std::string mystr = "";
1330                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1331                   if (s)
1332                     {
1333                       DEBTRACE( "convertToYacsObjref " << (const char *)s );
1334                       mystr = (const char *)s;
1335                       xmlFree(s);
1336                     }
1337                   else
1338                     {
1339                       DEBTRACE("############### workaround to improve...");
1340                     }
1341                   if(strncmp(t->id(),"python",6)==0 )
1342                     return FromBase64Safe(mystr);
1343                   else
1344                     return mystr;
1345                 }
1346               else if ((!xmlStrcmp(cur->name, (const xmlChar *)"string")))// <- here case where pyobj value has been stored in XML. pyobj has kind==ObjRef. And the stored format is String ! EDF11027
1347                 {
1348                   //wait a string, got a string
1349                   xmlChar * s = NULL;
1350                   s = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
1351                   if(s==0)return "";
1352                   DEBTRACE( "convertToYacsString " << (const char *)s );
1353                   std::string mystr=std::string((const char *)s);
1354                   xmlFree(s);
1355                   return mystr;
1356                 }
1357               cur = cur->next;
1358             }
1359           stringstream msg;
1360           msg << "Problem in conversion from Xml to " << getImplName(IMPLOUT) << " with type:  " << t->id() ;
1361           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1362           throw YACS::ENGINE::ConversionException(msg.str());
1363         }
1364     };
1365     template <ImplType IMPLOUT, class TOUT>
1366     struct convertToYacsSequence<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1367     {
1368       static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::vector<TOUT>& v)
1369         {
1370           cur = cur->xmlChildrenNode;
1371           while (cur != NULL)
1372             {
1373               if ((!xmlStrcmp(cur->name, (const xmlChar *)"array")))
1374                 {
1375                   DEBTRACE( "parse sequence " );
1376                   xmlNodePtr cur1=cur->xmlChildrenNode;
1377                   while (cur1 != NULL)
1378                     {
1379                       if ((!xmlStrcmp(cur1->name, (const xmlChar *)"data")))
1380                         {
1381                           DEBTRACE( "parse data " );
1382                           xmlNodePtr cur2=cur1->xmlChildrenNode;
1383                           while (cur2 != NULL)
1384                             {
1385                               //collect all values
1386                               if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1387                                 {
1388                                   TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(t->contentType(),doc,cur2);
1389                                   v.push_back(ro);
1390                                 }
1391                               cur2 = cur2->next;
1392                             } // end while value
1393                           break;
1394                         }
1395                       cur1 = cur1->next;
1396                     } // end while data
1397                   break;
1398                 }
1399               cur = cur->next;
1400             } // end while array
1401         }
1402     };
1403     template <ImplType IMPLOUT, class TOUT>
1404     struct convertToYacsStruct<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>
1405     {
1406       static inline void convert(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur,std::map<std::string,TOUT>& m)
1407         {
1408           YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1409           int nMember=tst->memberCount();
1410           DEBTRACE("nMember="<<nMember);
1411           std::map<std::string,TypeCode*> mtc;
1412           for(int i=0;i<nMember;i++)
1413             {
1414               mtc[tst->memberName(i)]=tst->memberType(i);
1415             }
1416
1417           cur = cur->xmlChildrenNode;
1418           while (cur != NULL)
1419             {
1420               if ((!xmlStrcmp(cur->name, (const xmlChar *)"struct")))
1421                 {
1422                   DEBTRACE( "parse struct " );
1423                   xmlNodePtr cur1=cur->xmlChildrenNode;
1424                   while (cur1 != NULL)
1425                     {
1426                       if ((!xmlStrcmp(cur1->name, (const xmlChar *)"member")))
1427                         {
1428                           DEBTRACE( "parse member " );
1429                           xmlNodePtr cur2=cur1->xmlChildrenNode;
1430                           while (cur2 != NULL)
1431                             {
1432                               //member name
1433                               if ((!xmlStrcmp(cur2->name, (const xmlChar *)"name")))
1434                                 {
1435                                   xmlChar * s = NULL;
1436                                   s = xmlNodeListGetString(doc, cur2->xmlChildrenNode, 1);
1437                                   std::string name= (char *)s;
1438                                   cur2 = cur2->next;
1439                                   while (cur2 != NULL)
1440                                     {
1441                                       if ((!xmlStrcmp(cur2->name, (const xmlChar *)"value")))
1442                                         {
1443                                           TOUT ro=YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,IMPLOUT,TOUT>(mtc[name],doc,cur2);
1444                                           m[name]=ro;
1445                                           break;
1446                                         }
1447                                       cur2 = cur2->next;
1448                                     }
1449                                   xmlFree(s);
1450                                   break;
1451                                 }
1452                               cur2 = cur2->next;
1453                             } // end while member/value
1454                         }
1455                       cur1 = cur1->next;
1456                     } // end while member
1457                   break;
1458                 }
1459               cur = cur->next;
1460             } // end while struct
1461         }
1462     };
1463     /* End of ToYacs Convertor for XMLImpl */
1464
1465     //! FromYacs Convertor for XMLImpl
1466     /*!
1467      * Convert YACS<std::string> intermediate types to std::string types (XMLImpl)
1468      */
1469     template <>
1470     struct convertFromYacsDouble<XMLImpl,std::string>
1471     {
1472       static inline std::string convert(const TypeCode *t,double o)
1473         {
1474           stringstream msg ;
1475           msg << "<value><double>" << setprecision(16) << o << "</double></value>\n";
1476           return msg.str();
1477         }
1478     };
1479     template <>
1480     struct convertFromYacsInt<XMLImpl,std::string>
1481     {
1482       static inline std::string convert(const TypeCode *t,long o)
1483         {
1484           stringstream msg ;
1485           msg << "<value><int>" << o << "</int></value>\n";
1486           return msg.str();
1487         }
1488     };
1489     template <>
1490     struct convertFromYacsString<XMLImpl,std::string>
1491     {
1492       static inline std::string convert(const TypeCode *t,std::string& o)
1493         {
1494           std::string msg="<value><string>";
1495           return msg+o+"</string></value>\n";
1496         }
1497     };
1498     template <>
1499     struct convertFromYacsBool<XMLImpl,std::string>
1500     {
1501       static inline std::string convert(const TypeCode *t,bool o)
1502         {
1503           stringstream msg ;
1504           msg << "<value><boolean>" << o << "</boolean></value>\n";
1505           return msg.str();
1506         }
1507     };
1508     template <>
1509     struct convertFromYacsObjref<XMLImpl,std::string>
1510     {
1511       static inline std::string convert(const TypeCode *t,std::string& o)
1512         {
1513           if(strncmp(t->id(),"python",6)==0 )
1514             return "<value><objref><![CDATA[" + ToBase64(o) + "]]></objref></value>\n";
1515           else if(strncmp(t->id(),"json",4)==0)
1516             return "<value><objref><![CDATA[" + o + "]]></objref></value>\n";
1517           else
1518             return "<value><objref>" + o + "</objref></value>\n";
1519         }
1520     };
1521
1522     template <>
1523     struct convertFromYacsSequence<XMLImpl,std::string>
1524     {
1525       static inline std::string convert(const TypeCode *t,std::vector<std::string>& v)
1526         {
1527           std::vector<std::string>::const_iterator iter;
1528           stringstream xmlob;
1529           xmlob << "<value><array><data>\n";
1530           for(iter=v.begin();iter!=v.end();iter++)
1531             {
1532               xmlob << *iter;
1533             }
1534           xmlob << "</data></array></value>\n";
1535           DEBTRACE("Sequence= " << xmlob);
1536           return xmlob.str();
1537         }
1538     };
1539     template <>
1540     struct convertFromYacsStruct<XMLImpl,std::string>
1541     {
1542       static inline std::string convert(const TypeCode *t,std::map<std::string,std::string>& m)
1543         {
1544           std::string result="<value><struct>\n";
1545           std::map<std::string, std::string>::const_iterator pt;
1546           for(pt=m.begin();pt!=m.end();pt++)
1547             {
1548               std::string name=(*pt).first;
1549               std::string item=(*pt).second;
1550               result=result+"<member>\n";
1551               result=result+"<name>"+name+"</name>\n";
1552               result=result+item;
1553               result=result+"</member>\n";
1554             }
1555           result=result+"</struct></value>\n";
1556           return result;
1557         }
1558     };
1559
1560     /* End of FromYacs Convertor for XMLImpl */
1561
1562     //! ToYacs Convertor for NEUTRALImpl
1563     /*!
1564      * This convertor converts Neutral objects to intermediate YACS<TOUT> types
1565      * Template : Partial specialization for Neutral implementation with types YACS::ENGINE::Any*
1566      */
1567     template <ImplType IMPLOUT, class TOUT>
1568     struct convertToYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1569     {
1570       static inline double convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1571         {
1572           if(o->getType()->kind()==Double)
1573             return o->getDoubleValue();
1574           else if(o->getType()->kind()==Int)
1575             return o->getIntValue();
1576
1577           stringstream msg;
1578           msg << "Problem in conversion: a double or int is expected " ;
1579           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1580           throw YACS::ENGINE::ConversionException(msg.str());
1581         }
1582     };
1583     template <ImplType IMPLOUT, class TOUT>
1584     struct convertToYacsInt<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1585     {
1586       static inline long convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1587         {
1588           if(o->getType()->kind()==Int)
1589             return o->getIntValue();
1590           stringstream msg;
1591           msg << "Problem in conversion: a int is expected " ;
1592           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1593           throw YACS::ENGINE::ConversionException(msg.str());
1594         }
1595     };
1596     template <ImplType IMPLOUT, class TOUT>
1597     struct convertToYacsString<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1598     {
1599       static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1600         {
1601           if(o->getType()->kind()==String)
1602             return o->getStringValue();
1603           stringstream msg;
1604           msg << "Problem in conversion: a string is expected " ;
1605           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1606           throw YACS::ENGINE::ConversionException(msg.str());
1607         }
1608     };
1609     template <ImplType IMPLOUT, class TOUT>
1610     struct convertToYacsBool<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1611     {
1612       static inline bool convert(const TypeCode *t,YACS::ENGINE::Any* o,void*)
1613         {
1614           if(o->getType()->kind()==Bool)
1615             return o->getBoolValue();
1616           else if(o->getType()->kind()==Int)
1617             return o->getIntValue() != 0;
1618           stringstream msg;
1619           msg << "Problem in conversion: a bool or int is expected " ;
1620           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1621           throw YACS::ENGINE::ConversionException(msg.str());
1622         }
1623     };
1624     template <ImplType IMPLOUT, class TOUT>
1625     struct convertToYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1626     {
1627       static inline std::string convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,int protocol)
1628         {
1629           if(o->getType()->kind()==String || o->getType()->kind()==Objref)
1630             return o->getStringValue();
1631           stringstream msg;
1632           msg << "Problem in conversion: a objref(string) is expected " ;
1633           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1634           throw YACS::ENGINE::ConversionException(msg.str());
1635         }
1636     };
1637     template <ImplType IMPLOUT, class TOUT>
1638     struct convertToYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1639     {
1640       static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::vector<TOUT>& v)
1641         {
1642           SequenceAny* sdata= (SequenceAny*)o;
1643           int length=sdata->size();
1644           v.resize(length);
1645           for(int i=0;i<length;i++)
1646             {
1647               TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(t->contentType(),(*sdata)[i],0);
1648               v[i]=ro;
1649             }
1650         }
1651     };
1652     template <ImplType IMPLOUT, class TOUT>
1653     struct convertToYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>
1654     {
1655       static inline void convert(const TypeCode *t,YACS::ENGINE::Any* o,void*,std::map<std::string,TOUT>& m)
1656         {
1657           StructAny * sdata = dynamic_cast<StructAny *>(o);
1658           YASSERT(sdata != NULL);
1659           const TypeCodeStruct * tst = dynamic_cast<const TypeCodeStruct *>(t);
1660           YASSERT(tst != NULL);
1661           for (int i=0 ; i<tst->memberCount() ; i++)
1662             {
1663               string name = tst->memberName(i);
1664               TOUT ro=YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),(*sdata)[name.c_str()],0);
1665               m[name]=ro;
1666             }
1667         }
1668     };
1669     /* End of ToYacs Convertor for NEUTRALImpl */
1670
1671     //! FromYacs Convertor for NEUTRALImpl
1672     /*!
1673      * Convert YACS<YACS::ENGINE::Any*> intermediate types to YACS::ENGINE::Any* types (NEUTRALImpl)
1674      */
1675     template <>
1676     struct convertFromYacsDouble<NEUTRALImpl,YACS::ENGINE::Any*>
1677     {
1678       static inline YACS::ENGINE::Any* convert(const TypeCode *t,double o)
1679         {
1680           YACS::ENGINE::Any *ob=YACS::ENGINE::AtomAny::New(o);
1681           return ob;
1682         }
1683     };
1684     template <>
1685     struct convertFromYacsInt<NEUTRALImpl,YACS::ENGINE::Any*>
1686     {
1687       static inline YACS::ENGINE::Any* convert(const TypeCode *t,long o)
1688         {
1689           return YACS::ENGINE::AtomAny::New((int)o);
1690         }
1691     };
1692     template <>
1693     struct convertFromYacsString<NEUTRALImpl,YACS::ENGINE::Any*>
1694     {
1695       static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1696         {
1697           return YACS::ENGINE::AtomAny::New(o);
1698         }
1699     };
1700     template <>
1701     struct convertFromYacsBool<NEUTRALImpl,YACS::ENGINE::Any*>
1702     {
1703       static inline YACS::ENGINE::Any* convert(const TypeCode *t,bool o)
1704         {
1705           return YACS::ENGINE::AtomAny::New(o);
1706         }
1707     };
1708     template <>
1709     struct convertFromYacsObjref<NEUTRALImpl,YACS::ENGINE::Any*>
1710     {
1711       static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::string& o)
1712         {
1713           //Check if objref is a GenericObj and register it if it is the case (workaround for bad management of GenericObj)
1714           if(o=="" || (t->isA(Runtime::_tc_file)) || (strncmp(t->id(),"python",6)==0) || (strncmp(t->id(),"json",4)==0))
1715             return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1716
1717           //Objref CORBA. prefix=IOR,corbaname,corbaloc
1718           CORBA::Object_var obref;
1719           try
1720             {
1721               obref = getSALOMERuntime()->getOrb()->string_to_object(o.c_str());
1722             }
1723           catch(CORBA::Exception& ex)
1724             {
1725               throw ConversionException("Can't get reference to object");
1726             }
1727           if(obref->_non_existent())
1728             throw ConversionException("non_existent object");
1729           if( CORBA::is_nil(obref) )
1730             throw ConversionException("Can't get reference to object");
1731           if(!obref->_is_a(t->id()))
1732             {
1733               stringstream msg;
1734               msg << "Problem in conversion: an objref " << t->id() << " is expected " << endl;
1735               msg << "An objref of type " << obref->_PD_repoId << " is given " << endl;
1736               msg << " (" << __FILE__ << ":" << __LINE__ << ")";
1737               throw YACS::ENGINE::ConversionException(msg.str());
1738             }
1739
1740           SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obref);
1741           if(!CORBA::is_nil(gobj))
1742             {
1743               DEBTRACE("It's a SALOME::GenericObj register it");
1744               gobj->Register();
1745             }
1746           else
1747               DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
1748
1749           return YACS::ENGINE::AtomAny::New(o, const_cast<TypeCode *>(t));
1750         }
1751     };
1752
1753     template <>
1754     struct convertFromYacsSequence<NEUTRALImpl,YACS::ENGINE::Any*>
1755     {
1756       static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::vector<YACS::ENGINE::Any*>& v)
1757         {
1758           std::vector<YACS::ENGINE::Any*>::const_iterator iter;
1759           //Objref are managed as string within YACS::ENGINE::Any objs
1760           SequenceAny* any;
1761           any=SequenceAny::New(t->contentType());
1762           for(iter=v.begin();iter!=v.end();iter++)
1763             {
1764               any->pushBack(*iter);
1765               (*iter)->decrRef();
1766             }
1767           DEBTRACE( "refcnt: " << any->getRefCnt() );
1768           return any;
1769         }
1770     };
1771
1772     template <>
1773     struct convertFromYacsStruct<NEUTRALImpl,YACS::ENGINE::Any*>
1774     {
1775       static inline YACS::ENGINE::Any* convert(const TypeCode *t,std::map<std::string,YACS::ENGINE::Any*>& m)
1776         {
1777           StructAny * any = StructAny::New((TypeCodeStruct *)t);
1778           std::map<std::string,YACS::ENGINE::Any*>::const_iterator it;
1779           for (it=m.begin() ; it!=m.end() ; it++)
1780             {
1781               any->setEltAtRank(it->first.c_str(), it->second);
1782               it->second->decrRef();
1783             }
1784           return any;
1785         }
1786     };
1787     /* End of FromYacs Convertor for NEUTRALImpl */
1788
1789     //! ToYacs Convertor for CORBAImpl
1790     /*!
1791      * This convertor converts Corba objects to intermediate YACS<TOUT> types
1792      * Template : Partial specialization for CORBA implementation with types CORBA::Any*
1793      */
1794     template <ImplType IMPLOUT, class TOUT>
1795     struct convertToYacsDouble<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1796     {
1797       static inline double convert(const TypeCode *t,CORBA::Any* o,void*)
1798         {
1799           CORBA::TypeCode_var tc = o->type();
1800           if (tc->equivalent(CORBA::_tc_double))
1801             {
1802               CORBA::Double d;
1803               *o >>= d;
1804               return d;
1805             }
1806           if (tc->equivalent(CORBA::_tc_long))
1807             {
1808               CORBA::Long d;
1809               *o >>= d;
1810               return d;
1811             }
1812           stringstream msg;
1813           msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1814           msg << " : " << __FILE__ << ":" << __LINE__;
1815           throw YACS::ENGINE::ConversionException(msg.str());
1816         }
1817     };
1818     template <ImplType IMPLOUT, class TOUT>
1819     struct convertToYacsInt<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1820     {
1821       static inline long convert(const TypeCode *t,CORBA::Any* o,void*)
1822         {
1823           CORBA::Long d;
1824           if(*o >>= d)
1825             return d;
1826           stringstream msg;
1827           msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1828           msg << " : " << __FILE__ << ":" << __LINE__;
1829           throw YACS::ENGINE::ConversionException(msg.str());
1830         }
1831     };
1832     template <ImplType IMPLOUT, class TOUT>
1833     struct convertToYacsString<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1834     {
1835       static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*)
1836         {
1837           const char *s;
1838           if(*o >>=s)
1839             return s;
1840           stringstream msg;
1841           msg << "Problem in CORBA to TOUT conversion: kind= " << t->kind() ;
1842           msg << " : " << __FILE__ << ":" << __LINE__;
1843           throw YACS::ENGINE::ConversionException(msg.str());
1844         }
1845     };
1846     template <ImplType IMPLOUT, class TOUT>
1847     struct convertToYacsBool<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1848     {
1849       static inline bool convert(const TypeCode *t,CORBA::Any* o,void*)
1850         {
1851           CORBA::Boolean b;
1852           if(*o >>= CORBA::Any::to_boolean(b))
1853             return b;
1854           stringstream msg;
1855           msg << "Problem in Corba to TOUT conversion: kind= " << t->kind() ;
1856           msg << " : " << __FILE__ << ":" << __LINE__;
1857           throw YACS::ENGINE::ConversionException(msg.str());
1858         }
1859     };
1860     template <ImplType IMPLOUT, class TOUT>
1861     struct convertToYacsObjref<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1862     {
1863       static inline std::string convert(const TypeCode *t,CORBA::Any* o,void*,int protocol)
1864         {
1865           char file[]="/tmp/XXXXXX";
1866           if(t->isA(Runtime::_tc_file))
1867             {
1868               Engines::Salome_file_ptr sf;
1869               *o >>= sf;
1870               Salome_file_i* f=new Salome_file_i();
1871               mkstemp(file);
1872               f->setDistributedFile(file);
1873               f->connect(sf);
1874               f->recvFiles();
1875               delete f;
1876               return file;
1877             }
1878           else if(strncmp(t->id(),"python",6)==0)
1879             {
1880               const char *s;
1881               Engines::fileBlock * buffer;
1882               if(*o >>=buffer)
1883                 {
1884                   s=(const char*)buffer->get_buffer();
1885
1886                   if(protocol !=4)
1887                     {
1888                       std::string mystr(s,buffer->length());
1889                       return mystr;
1890                     }
1891
1892                   PyGILState_STATE gstate = PyGILState_Ensure(); 
1893                   PyObject* mod=PyImport_ImportModule("pickle");
1894                   PyObject *ob=PyObject_CallMethod(mod,(char *)"loads",(char *)"y#",s,buffer->length());
1895                   PyObject *pickled=PyObject_CallMethod(mod,(char *)"dumps",(char *)"Oi",ob,protocol);
1896                   DEBTRACE(PyObject_Repr(pickled));
1897                   std::string mystr=PyBytes_AsString(pickled);
1898                   Py_DECREF(mod);
1899                   Py_DECREF(ob);
1900                   Py_DECREF(pickled);
1901                   PyGILState_Release(gstate);
1902
1903                   return mystr;
1904                 }
1905               stringstream msg;
1906               msg << "Problem in CORBA (protocol python) to TOUT conversion: kind= " << t->kind() ;
1907               msg << " : " << __FILE__ << ":" << __LINE__;
1908               throw YACS::ENGINE::ConversionException(msg.str());
1909             }
1910           else if(strncmp(t->id(),"json",4)==0)
1911             {
1912               const char *s;
1913               if(*o >>=s)
1914                 {
1915                   return s;
1916                 }
1917               stringstream msg;
1918               msg << "Problem in CORBA (protocol json) to TOUT conversion: kind= " << t->kind() ;
1919               msg << " : " << __FILE__ << ":" << __LINE__;
1920               throw YACS::ENGINE::ConversionException(msg.str());
1921             }
1922           else
1923             {
1924               CORBA::Object_var ObjRef ;
1925               *o >>= CORBA::Any::to_object(ObjRef) ;
1926               CORBA::String_var objref = getSALOMERuntime()->getOrb()->object_to_string(ObjRef);
1927               return (char *)objref;
1928             }
1929         }
1930     };
1931     template <ImplType IMPLOUT, class TOUT>
1932     struct convertToYacsSequence<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1933     {
1934       static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::vector<TOUT>& v)
1935         {
1936           CORBA::TypeCode_var tc=o->type();
1937           if (tc->kind() != CORBA::tk_sequence)
1938             {
1939               stringstream msg;
1940               msg << "Not a sequence corba type " << tc->kind();
1941               msg << " : " << __FILE__ << ":" << __LINE__;
1942               throw YACS::ENGINE::ConversionException(msg.str());
1943             }
1944           DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1945           DynamicAny::DynSequence_ptr ds=DynamicAny::DynSequence::_narrow(dynany);
1946           CORBA::release(dynany);
1947           DynamicAny::AnySeq_var as=ds->get_elements();
1948           int len=as->length();
1949           v.resize(len);
1950           for(int i=0;i<len;i++)
1951             {
1952 #ifdef REFCNT
1953               DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1954 #endif
1955               TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(t->contentType(),&as[i],0);
1956               v[i]=ro;
1957             }
1958           ds->destroy();
1959           CORBA::release(ds);
1960           for(int i=0;i<len;i++)
1961             {
1962 #ifdef REFCNT
1963               DEBTRACE("refcount CORBA as[i]: " << ((omni::TypeCode_base*)as[i].pd_tc.in())->pd_ref_count);
1964 #endif
1965             }
1966         }
1967     };
1968     template <ImplType IMPLOUT, class TOUT>
1969     struct convertToYacsStruct<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>
1970     {
1971       static inline void convert(const TypeCode *t,CORBA::Any* o,void*,std::map<std::string,TOUT>& m)
1972         {
1973           CORBA::TypeCode_var tc=o->type();
1974           DEBTRACE(tc->kind());
1975           if (tc->kind() != CORBA::tk_struct)
1976             {
1977               stringstream msg;
1978               msg << "Not a struct corba type " << tc->kind();
1979               msg << " : " << __FILE__ << ":" << __LINE__;
1980               throw YACS::ENGINE::ConversionException(msg.str());
1981             }
1982           YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
1983           DynamicAny::DynAny_ptr dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any(*o);
1984           DynamicAny::DynStruct_ptr ds=DynamicAny::DynStruct::_narrow(dynany);
1985           CORBA::release(dynany);
1986           DynamicAny::NameValuePairSeq_var as=ds->get_members();
1987           int len=as->length();
1988           for(int i=0;i<len;i++)
1989             {
1990               std::string name=as[i].id.in();
1991               DEBTRACE(name);
1992               CORBA::Any value=as[i].value;
1993 #ifdef REFCNT
1994               DEBTRACE("refcount CORBA value: " << ((omni::TypeCode_base*)value.pd_tc.in())->pd_ref_count);
1995 #endif
1996               TOUT ro=YacsConvertor<CORBAImpl,CORBA::Any*,void*,IMPLOUT,TOUT>(tst->memberType(i),&value,0);
1997               m[name]=ro;
1998             }
1999           ds->destroy();
2000           CORBA::release(ds);
2001         }
2002     };
2003     /* End of ToYacs Convertor for CORBAImpl */
2004
2005     //! FromYacs Convertor for CORBAImpl
2006     /*!
2007      * Convert YACS<CORBA::Any*> intermediate types to CORBA::Any* types (CORBAImpl)
2008      */
2009     template <>
2010     struct convertFromYacsDouble<CORBAImpl,CORBA::Any*>
2011     {
2012       static inline CORBA::Any* convert(const TypeCode *t,double o)
2013         {
2014           CORBA::Any *any = new CORBA::Any();
2015           *any <<= (CORBA::Double)o;
2016           return any;
2017         }
2018     };
2019     template <>
2020     struct convertFromYacsInt<CORBAImpl,CORBA::Any*>
2021     {
2022       static inline CORBA::Any* convert(const TypeCode *t,long o)
2023         {
2024           CORBA::Any *any = new CORBA::Any();
2025           *any <<= (CORBA::Long)o;
2026           return any;
2027         }
2028     };
2029     template <>
2030     struct convertFromYacsString<CORBAImpl,CORBA::Any*>
2031     {
2032       static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2033         {
2034           CORBA::Any *any = new CORBA::Any();
2035           *any <<= o.c_str();
2036           return any;
2037         }
2038     };
2039     template <>
2040     struct convertFromYacsBool<CORBAImpl,CORBA::Any*>
2041     {
2042       static inline CORBA::Any* convert(const TypeCode *t,bool o)
2043         {
2044           CORBA::Any *any = new CORBA::Any();
2045           *any <<= CORBA::Any::from_boolean(o);
2046           return any;
2047         }
2048     };
2049     template <>
2050     struct convertFromYacsObjref<CORBAImpl,CORBA::Any*>
2051     {
2052       static inline CORBA::Any* convert(const TypeCode *t,std::string& o)
2053         {
2054           CORBA::Object_var obref;
2055
2056           if(t->isA(Runtime::_tc_file))
2057             {
2058               //It's an objref file. Convert it specially
2059               Salome_file_i* aSalome_file = new Salome_file_i();
2060               try
2061                 {
2062                   aSalome_file->setLocalFile(o.c_str());
2063                   obref = aSalome_file->_this();
2064                   aSalome_file->_remove_ref();
2065                 }
2066               catch (const SALOME::SALOME_Exception& e)
2067                 {
2068                   stringstream msg;
2069                   msg << e.details.text;
2070                   msg << " : " << __FILE__ << ":" << __LINE__;
2071                   throw YACS::ENGINE::ConversionException(msg.str());
2072                 }
2073             }
2074           else if(strncmp(t->id(),"python",6)==0 )
2075             {
2076               CORBA::Any *any = new CORBA::Any();
2077               Engines::fileBlock * buffer=new Engines::fileBlock();
2078               buffer->length(o.length());
2079               CORBA::Octet *buf=buffer->get_buffer();
2080               memcpy(buf,o.c_str(),o.length());
2081               *any <<= buffer;
2082               return any;
2083             }
2084           else if(strncmp(t->id(),"json",4)==0)
2085             {
2086               CORBA::Any *any = new CORBA::Any();
2087               *any <<= o.c_str();
2088               return any;
2089             }
2090           else
2091             {
2092               try
2093                 {
2094                   obref=getSALOMERuntime()->getFromNS(o.c_str());
2095                 }
2096               catch(CORBA::Exception& ex)
2097                 {
2098                   throw ConversionException("Can't get reference to object");
2099                 }
2100               if( CORBA::is_nil(obref) )
2101                 {
2102                   throw ConversionException("Can't get reference to object");
2103                 }
2104             }
2105 #ifdef REFCNT
2106           DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2107 #endif
2108           CORBA::Any *any = new CORBA::Any();
2109           *any <<= obref;
2110 #ifdef REFCNT
2111           DEBTRACE("ObjRef refCount: " << obref->_PR_getobj()->pd_refCount);
2112 #endif
2113           return any;
2114         }
2115     };
2116
2117     template <>
2118     struct convertFromYacsSequence<CORBAImpl,CORBA::Any*>
2119     {
2120       static inline CORBA::Any* convert(const TypeCode *t,std::vector<CORBA::Any*>& v)
2121         {
2122           CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2123           std::vector<CORBA::Any*>::const_iterator iter;
2124
2125           // Build an Any from vector v
2126           int isObjref=0;
2127           if(t->contentType()->kind() == Objref)
2128             isObjref=1;
2129
2130           CORBA::TypeCode_var tc=getCorbaTC(t);
2131
2132           DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2133           DynamicAny::DynSequence_var ds = DynamicAny::DynSequence::_narrow(dynany);
2134           ds->set_length(v.size());
2135
2136           for(iter=v.begin();iter!=v.end();iter++)
2137             {
2138               DynamicAny::DynAny_var temp=ds->current_component();
2139               CORBA::Any* a=*iter;
2140               //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2141               if(isObjref)
2142                 {
2143                   CORBA::Object_var zzobj ;
2144                   *a >>= CORBA::Any::to_object(zzobj) ;
2145                   temp->insert_reference(zzobj);
2146                 }
2147               else
2148                 temp->from_any(*a);
2149
2150               //delete intermediate any
2151               delete a;
2152               ds->next();
2153             }
2154
2155           CORBA::Any *any=ds->to_any();
2156           ds->destroy();
2157           return any;
2158         }
2159     };
2160     template <>
2161     struct convertFromYacsStruct<CORBAImpl,CORBA::Any*>
2162     {
2163       static inline CORBA::Any* convert(const TypeCode *t,std::map<std::string,CORBA::Any*>& m)
2164         {
2165           CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
2166
2167           YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2168           int nMember=tst->memberCount();
2169           DEBTRACE("nMember="<<nMember);
2170
2171           CORBA::TypeCode_var tc=getCorbaTC(t);
2172           DynamicAny::DynAny_var dynany=getSALOMERuntime()->getDynFactory()->create_dyn_any_from_type_code(tc);
2173           DynamicAny::DynStruct_var ds = DynamicAny::DynStruct::_narrow(dynany);
2174
2175           for(int i=0;i<nMember;i++)
2176             {
2177               DynamicAny::DynAny_var temp=ds->current_component();
2178               const char * name=tst->memberName(i);
2179               DEBTRACE("Member name="<<name);
2180               //do not test member presence : test has been done in ToYacs convertor
2181               CORBA::Any* a=m[name];
2182               //It seems that from_any does not support inherited objref: convert to CORBA::Object and insert reference
2183               CORBA::TypeCode_var atc = tc->member_type(i);
2184               if(atc->kind()==CORBA::tk_objref)
2185                 {
2186                   //special treatment for objref
2187                   CORBA::Object_var zzobj ;
2188                   *a >>= CORBA::Any::to_object(zzobj) ;
2189                   temp->insert_reference(zzobj);
2190                 }
2191               else
2192                 {
2193                   temp->from_any(*a);
2194                 }
2195               //delete intermediate any
2196               delete a;
2197               ds->next();
2198             }
2199           CORBA::Any *any=ds->to_any();
2200           ds->destroy();
2201
2202           return any;
2203         }
2204     };
2205     /* End of FromYacs Convertor for CORBAImpl */
2206
2207     /* Some shortcuts for CORBA to CORBA conversion */
2208     template <>
2209     inline CORBA::Any* convertDouble<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2210     {
2211       CORBA::TypeCode_var tc = o->type();
2212       if (tc->equivalent(CORBA::_tc_double))
2213         {
2214           return o;
2215         }
2216       if (tc->equivalent(CORBA::_tc_long))
2217         {
2218           CORBA::Long d;
2219           *o >>= d;
2220           CORBA::Any *any = new CORBA::Any();
2221           *any <<= (CORBA::Double)d;
2222           return any;
2223         }
2224       stringstream msg;
2225       msg << "Not a double or long corba type " << tc->kind();
2226       msg << " : " << __FILE__ << ":" << __LINE__;
2227       throw YACS::ENGINE::ConversionException(msg.str());
2228     }
2229     template <>
2230     inline CORBA::Any* convertInt<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2231     {
2232       return o;
2233     }
2234     template <>
2235     inline CORBA::Any* convertString<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2236     {
2237       return o;
2238     }
2239     template <>
2240     inline CORBA::Any* convertBool<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2241     {
2242       return o;
2243     }
2244     template <>
2245     inline CORBA::Any* convertObjref<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2246     {
2247       return o;
2248     }
2249     template <>
2250     inline CORBA::Any* convertStruct<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(const TypeCode *t,CORBA::Any* o,void* aux)
2251     {
2252       return o;
2253     }
2254     /* End of shortcuts for CORBA to CORBA conversion */
2255
2256     //! ToYacs Convertor for CPPImpl
2257     /*!
2258      * This convertor converts Python object to YACS<TOUT> types
2259      * Partial specialization for Python implementation with type PyObject* (PYTHONImpl)
2260      */
2261     template <ImplType IMPLOUT, class TOUT>
2262     struct convertToYacsDouble<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2263     {
2264       static inline double convert(const TypeCode *t,void* o,const TypeCode* intype)
2265         {
2266           if(intype->kind()==YACS::ENGINE::Double)
2267             {
2268               return *(double*)o;
2269             }
2270           else if(intype->kind()==YACS::ENGINE::Int)
2271             {
2272               return *(long*)o;
2273             }
2274           stringstream msg;
2275           msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2276           msg << " : " << __FILE__ << ":" << __LINE__;
2277           throw YACS::ENGINE::ConversionException(msg.str());
2278         }
2279     };
2280     template <ImplType IMPLOUT, class TOUT>
2281     struct convertToYacsInt<CPPImpl,void*,const TypeCode*,IMPLOUT,TOUT>
2282     {
2283       static inline long convert(const TypeCode *t,void* o,const TypeCode* intype)
2284         {
2285           if(intype->kind()==YACS::ENGINE::Int)
2286             {
2287               return *(long*)o;
2288             }
2289           stringstream msg;
2290           msg << "Problem in Cpp to TOUT conversion: kind= " << t->kind() ;
2291           msg << " : " << __FILE__ << ":" << __LINE__;
2292           throw YACS::ENGINE::ConversionException(msg.str());
2293         }
2294     };
2295     /* End of ToYacs Convertor for CPPImpl */
2296
2297     //Python conversions
2298     std::string convertPyObjectXml(const TypeCode *t,PyObject *data)
2299       {
2300         return YacsConvertor<PYTHONImpl,PyObject*,void*,XMLImpl,std::string>(t,data,0);
2301       }
2302     YACS::ENGINE::Any* convertPyObjectNeutral(const TypeCode *t,PyObject *data)
2303       {
2304         return YacsConvertor<PYTHONImpl,PyObject*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2305       }
2306     CORBA::Any* convertPyObjectCorba(const TypeCode *t,PyObject *data)
2307       {
2308         return YacsConvertor<PYTHONImpl,PyObject*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2309       }
2310     PyObject* convertPyObjectPyObject(const TypeCode *t,PyObject *data)
2311       {
2312         return YacsConvertor<PYTHONImpl,PyObject*,void*,PYTHONImpl,PyObject*>(t,data,0);
2313       }
2314
2315     std::string convertPyObjectToString(PyObject* ob)
2316     {
2317       PyObject *s;
2318       PyGILState_STATE gstate = PyGILState_Ensure(); 
2319       // TODO: separate treatment for string (maybe with bad encoding?) and other types of PyObject ?
2320       // Do we need str() or repr() and what are the possible causes of failure of str() ?
2321       // not clear, needs more analysis.
2322       s=PyObject_Str(ob);
2323       if (s == NULL) // for instance string with bad encoding, non utf-8
2324       {
2325         s=PyObject_ASCII(ob); // escape non ASCII characters and like repr(), which is not the same as str()...
2326       }
2327       Py_ssize_t size;
2328       const char* characters =  PyUnicode_AsUTF8AndSize(s, &size);
2329       std::string ss( characters, size);
2330       Py_DECREF(s);
2331       PyGILState_Release(gstate);
2332       return ss;
2333     }
2334
2335     //XML conversions
2336     PyObject* convertXmlPyObject(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2337       {
2338         return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,PYTHONImpl,PyObject*>(t,doc,cur);
2339       }
2340     YACS::ENGINE::Any* convertXmlNeutral(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2341       {
2342         return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,NEUTRALImpl,YACS::ENGINE::Any*>(t,doc,cur);
2343       }
2344     CORBA::Any* convertXmlCorba(const TypeCode *t,xmlDocPtr doc,xmlNodePtr cur)
2345       {
2346         return YacsConvertor<XMLImpl,xmlDocPtr,xmlNodePtr,CORBAImpl,CORBA::Any*>(t,doc,cur);
2347       }
2348     PyObject* convertXmlStrPyObject(const TypeCode *t,std::string data)
2349       {
2350         xmlDocPtr doc;
2351         xmlNodePtr cur;
2352         PyObject *ob=NULL;
2353         doc = xmlParseMemory(data.c_str(), strlen(data.c_str()));
2354         if (doc == NULL )
2355         {
2356           std::stringstream msg;
2357           msg << "Problem in conversion: XML Document not parsed successfully ";
2358           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2359           throw YACS::ENGINE::ConversionException(msg.str());
2360         }
2361         cur = xmlDocGetRootElement(doc);
2362         if (cur == NULL)
2363         {
2364           xmlFreeDoc(doc);
2365           std::stringstream msg;
2366           msg << "Problem in conversion: empty XML Document";
2367           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2368           throw YACS::ENGINE::ConversionException(msg.str());
2369         }
2370         while (cur != NULL)
2371         {
2372           if ((!xmlStrcmp(cur->name, (const xmlChar *)"value")))
2373           {
2374             ob=convertXmlPyObject(t,doc,cur);
2375             break;
2376           }
2377           cur = cur->next;
2378         }
2379         xmlFreeDoc(doc);
2380         if(ob==NULL)
2381         {
2382           std::stringstream msg;
2383           msg << "Problem in conversion: incorrect XML value";
2384           msg << " (" << __FILE__ << ":" << __LINE__ << ")";
2385           throw YACS::ENGINE::ConversionException(msg.str());
2386         }
2387         return ob;
2388       }
2389     //NEUTRAL conversions
2390     PyObject* convertNeutralPyObject(const TypeCode *t,YACS::ENGINE::Any* data)
2391       {
2392         return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2393       }
2394     std::string convertNeutralXml(const TypeCode *t,YACS::ENGINE::Any* data)
2395       {
2396         return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,XMLImpl,std::string>(t,data,0);
2397       }
2398     CORBA::Any* convertNeutralCorba(const TypeCode *t,YACS::ENGINE::Any* data)
2399       {
2400         return YacsConvertor<NEUTRALImpl,YACS::ENGINE::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2401       }
2402     YACS::ENGINE::Any *convertNeutralNeutral(const TypeCode *t, YACS::ENGINE::Any* data)
2403       {
2404         data->incrRef();
2405         return data;
2406       }
2407
2408     //CORBA conversions
2409     PyObject* convertCorbaPyObject(const TypeCode *t,CORBA::Any* data)
2410       {
2411         return YacsConvertor<CORBAImpl,CORBA::Any*,void*,PYTHONImpl,PyObject*>(t,data,0);
2412       }
2413     std::string convertCorbaXml(const TypeCode *t,CORBA::Any* data)
2414       {
2415         return YacsConvertor<CORBAImpl,CORBA::Any*,void*,XMLImpl,std::string>(t,data,0);
2416       }
2417     YACS::ENGINE::Any* convertCorbaNeutral(const TypeCode *t,CORBA::Any* data)
2418       {
2419         return YacsConvertor<CORBAImpl,CORBA::Any*,void*,NEUTRALImpl,YACS::ENGINE::Any*>(t,data,0);
2420       }
2421     CORBA::Any *convertCorbaCorba(const TypeCode *t,CORBA::Any *data)
2422       {
2423         return YacsConvertor<CORBAImpl,CORBA::Any*,void*,CORBAImpl,CORBA::Any*>(t,data,0);
2424       }
2425
2426     //! Basic template checker from type TIN 
2427     /*!
2428      * This checker does nothing : throws exception
2429      * It must be partially specialize for a specific type (TIN)
2430      */
2431     template <ImplType IMPLIN,class TIN,class TIN2>
2432     static inline bool checkDouble(const TypeCode *t,TIN o,TIN2 aux)
2433         {
2434           stringstream msg;
2435           msg << "Check not implemented for Implementation: " << IMPLIN ;
2436           msg << " : " << __FILE__ << ":" << __LINE__;
2437           throw YACS::ENGINE::ConversionException(msg.str());
2438         }
2439     template <ImplType IMPLIN,class TIN,class TIN2>
2440     static inline bool checkInt(const TypeCode *t,TIN o,TIN2 aux)
2441         {
2442           stringstream msg;
2443           msg << "Check not implemented for Implementation: " << IMPLIN ;
2444           msg << " : " << __FILE__ << ":" << __LINE__;
2445           throw YACS::ENGINE::ConversionException(msg.str());
2446         }
2447     template <ImplType IMPLIN,class TIN,class TIN2>
2448     static inline bool checkBool(const TypeCode *t,TIN o,TIN2 aux)
2449         {
2450           stringstream msg;
2451           msg << "Check not implemented for Implementation: " << IMPLIN ;
2452           msg << " : " << __FILE__ << ":" << __LINE__;
2453           throw YACS::ENGINE::ConversionException(msg.str());
2454         }
2455     template <ImplType IMPLIN,class TIN,class TIN2>
2456     static inline bool checkString(const TypeCode *t,TIN o,TIN2 aux)
2457         {
2458           stringstream msg;
2459           msg << "Check not implemented for Implementation: " << IMPLIN ;
2460           msg << " : " << __FILE__ << ":" << __LINE__;
2461           throw YACS::ENGINE::ConversionException(msg.str());
2462         }
2463     template <ImplType IMPLIN,class TIN,class TIN2>
2464     static inline bool checkObjref(const TypeCode *t,TIN o,TIN2 aux)
2465         {
2466           stringstream msg;
2467           msg << "Check not implemented for Implementation: " << IMPLIN ;
2468           msg << " : " << __FILE__ << ":" << __LINE__;
2469           throw YACS::ENGINE::ConversionException(msg.str());
2470         }
2471     template <ImplType IMPLIN,class TIN,class TIN2>
2472     static inline bool checkSequence(const TypeCode *t,TIN o,TIN2 aux)
2473         {
2474           stringstream msg;
2475           msg << "Check not implemented for Implementation: " << IMPLIN ;
2476           msg << " : " << __FILE__ << ":" << __LINE__;
2477           throw YACS::ENGINE::ConversionException(msg.str());
2478         }
2479     template <ImplType IMPLIN,class TIN,class TIN2>
2480     static inline bool checkStruct(const TypeCode *t,TIN o,TIN2 aux)
2481         {
2482           stringstream msg;
2483           msg << "Check not implemented for Implementation: " << IMPLIN ;
2484           msg << " : " << __FILE__ << ":" << __LINE__;
2485           throw YACS::ENGINE::ConversionException(msg.str());
2486         }
2487     template <ImplType IMPLIN,class TIN,class TIN2>
2488     static inline bool checkArray(const TypeCode *t,TIN o,TIN2 aux)
2489         {
2490           stringstream msg;
2491           msg << "Check not implemented for Implementation: " << IMPLIN ;
2492           msg << " : " << __FILE__ << ":" << __LINE__;
2493           throw YACS::ENGINE::ConversionException(msg.str());
2494         }
2495
2496     template <ImplType IMPLIN,class TIN,class TIN2>
2497     inline bool YacsChecker(const TypeCode *t,TIN o,TIN2 aux)
2498       {
2499          int tk=t->kind();
2500          switch(t->kind())
2501            {
2502            case Double:
2503              return checkDouble<IMPLIN,TIN,TIN2>(t,o,aux);
2504            case Int:
2505              return checkInt<IMPLIN,TIN,TIN2>(t,o,aux);
2506            case String:
2507              return checkString<IMPLIN,TIN,TIN2>(t,o,aux);
2508            case Bool:
2509              return checkBool<IMPLIN,TIN,TIN2>(t,o,aux);
2510            case Objref:
2511              return checkObjref<IMPLIN,TIN,TIN2>(t,o,aux);
2512            case Sequence:
2513              return checkSequence<IMPLIN,TIN,TIN2>(t,o,aux);
2514            case Array:
2515              return checkArray<IMPLIN,TIN,TIN2>(t,o,aux);
2516            case Struct:
2517              return checkStruct<IMPLIN,TIN,TIN2>(t,o,aux);
2518            default:
2519              break;
2520            }
2521          stringstream msg;
2522          msg << "Check not implemented for kind= " << tk ;
2523          msg << " : " << __FILE__ << ":" << __LINE__;
2524          throw YACS::ENGINE::ConversionException(msg.str());
2525       }
2526     template<>
2527     inline bool checkDouble<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2528       {
2529         if (PyFloat_Check(o))
2530           return true;
2531         else if(PyLong_Check(o))
2532           return true;
2533         else
2534           {
2535             stringstream msg;
2536             msg << "Not a python double ";
2537             throw YACS::ENGINE::ConversionException(msg.str());
2538           }
2539       }
2540     template<>
2541     inline bool checkInt<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2542       {
2543           if (PyLong_Check(o))
2544             return true;
2545           else
2546             {
2547               stringstream msg;
2548               msg << "Not a python integer ";
2549               throw YACS::ENGINE::ConversionException(msg.str());
2550             }
2551       }
2552     template<>
2553     inline bool checkBool<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2554       {
2555           if (PyBool_Check(o))
2556               return true;
2557           else if(PyLong_Check(o))
2558               return true;
2559           else
2560             {
2561               stringstream msg;
2562               msg << "Not a python boolean " ;
2563               throw YACS::ENGINE::ConversionException(msg.str());
2564             }
2565
2566       }
2567     template<>
2568     inline bool checkString<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2569       {
2570           if (PyUnicode_Check(o))
2571             return true;
2572           else
2573             {
2574               stringstream msg;
2575               msg << "Not a python string " ;
2576               throw YACS::ENGINE::ConversionException(msg.str());
2577             }
2578       }
2579     template<>
2580     inline bool checkObjref<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2581       {
2582           if (PyUnicode_Check(o))
2583             return true;
2584           if(strncmp(t->id(),"python",6)==0) // a Python object is expected (it's always true)
2585             return true;
2586           else if(strncmp(t->id(),"json",4)==0) // The python object must be json pickable
2587             {
2588                // The python object should be json compliant (to improve)
2589                return true;
2590             }
2591           else
2592             {
2593               // The python object should be a CORBA obj (to improve)
2594                return true;
2595             }
2596       }
2597     template<>
2598     inline bool checkSequence<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2599       {
2600         if(!PySequence_Check(o))
2601           {
2602             stringstream msg;
2603             msg << "python object is not a sequence " ;
2604             throw YACS::ENGINE::ConversionException(msg.str());
2605           }
2606         int length=PySequence_Size(o);
2607         for(int i=0;i<length;i++)
2608           {
2609             PyObject *item=PySequence_ITEM(o,i);
2610             try
2611               {
2612                 YacsChecker<PYTHONImpl,PyObject*,void*>(t->contentType(),item,0);
2613               }
2614             catch(ConversionException& ex)
2615               {
2616                 stringstream msg;
2617                 msg << ex.what() << " for sequence element " << i;
2618                 throw YACS::ENGINE::ConversionException(msg.str(),false);
2619               }
2620             Py_DECREF(item);
2621           }
2622         return true;
2623       }
2624     template<>
2625     inline bool checkStruct<PYTHONImpl,PyObject*,void*>(const TypeCode *t,PyObject* o,void* aux)
2626       {
2627         PyObject *value;
2628         if(!PyDict_Check(o))
2629           {
2630             stringstream msg;
2631             msg << "python object is not a dict " ;
2632             throw YACS::ENGINE::ConversionException(msg.str());
2633           }
2634         YACS::ENGINE::TypeCodeStruct* tst=(YACS::ENGINE::TypeCodeStruct*)t;
2635         int nMember=tst->memberCount();
2636         for(int i=0;i<nMember;i++)
2637           {
2638             std::string name=tst->memberName(i);
2639             TypeCode* tm=tst->memberType(i);
2640             value=PyDict_GetItemString(o, name.c_str());
2641             if(value==NULL)
2642               {
2643                 stringstream msg;
2644                 msg << "member " << name << " not present " ;
2645                 throw YACS::ENGINE::ConversionException(msg.str());
2646               }
2647             try
2648               {
2649                 YacsChecker<PYTHONImpl,PyObject*,void*>(tm,value,0);
2650               }
2651             catch(ConversionException& ex)
2652               {
2653                 std::string s=" for struct member "+name;
2654                 throw YACS::ENGINE::ConversionException(ex.what()+s,false);
2655               }
2656           }
2657         return true;
2658       }
2659
2660     bool checkPyObject(const TypeCode *t,PyObject* ob)
2661       {
2662         return YacsChecker<PYTHONImpl,PyObject*,void*>(t,ob,0);
2663       }
2664   }
2665 }