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