Salome HOME
f71939e907548ff9d58f904055fb0ecd07e6ae35
[modules/med.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTypemaps.i
1 // Copyright (C) 2007-2013  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.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "InterpKernelAutoPtr.hxx"
22
23 #ifdef WITH_NUMPY
24 #include <numpy/arrayobject.h>
25
26 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
27 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
28 // if no more alive the "standart" DataArray deallocator is called.
29 void numarrdeal(void *pt, void *wron)
30 {
31   void **wronc=(void **)wron;
32   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
33   PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
34   if(obj!=Py_None)
35     {
36       Py_XINCREF(obj);
37       PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
38       objC->flags|=NPY_OWNDATA;
39       Py_XDECREF(weakRefOnOwner);
40       Py_XDECREF(obj);
41     }
42   else
43     {
44       typedef void (*MyDeallocator)(void *,void *);
45       MyDeallocator deall=(MyDeallocator)wronc[1];
46       deall(pt,NULL);
47       Py_XDECREF(weakRefOnOwner);
48     }
49   delete [] wronc;
50 }
51
52 template<class MCData>
53 struct PyCallBackDataArraySt {
54     PyObject_HEAD
55     MCData *_pt_mc;
56 };
57
58 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayInt> PyCallBackDataArrayInt;
59 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayDouble> PyCallBackDataArrayDouble;
60
61 extern "C"
62 {
63   static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
64   
65   static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
66   {
67     PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
68     return (PyObject *)self;
69   }
70
71   static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
72   {
73     PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
74     return (PyObject *)self;
75   }
76   
77   static void callbackmcdataarray_dealloc(PyObject *self)
78   {
79     Py_TYPE(self)->tp_free(self);
80   }
81   
82   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
83   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
84   static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
85   {
86     if(self->_pt_mc)
87       {
88         ParaMEDMEM::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
89         mma.destroy();
90       }
91     Py_XINCREF(Py_None);
92     return Py_None;
93   }
94   
95   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
96   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
97   static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
98   {
99     if(self->_pt_mc)
100       {
101         ParaMEDMEM::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
102         mma.destroy();
103       }
104     Py_XINCREF(Py_None);
105     return Py_None;
106   }
107 }
108
109 PyTypeObject PyCallBackDataArrayInt_RefType = {
110   PyVarObject_HEAD_INIT(&PyType_Type, 0)
111   "callbackmcdataarrayint",
112   sizeof(PyCallBackDataArrayInt),
113   0,
114   callbackmcdataarray_dealloc,            /*tp_dealloc*/
115   0,                          /*tp_print*/
116   0,                          /*tp_getattr*/
117   0,                          /*tp_setattr*/
118   0,                          /*tp_compare*/
119   0,                          /*tp_repr*/
120   0,                          /*tp_as_number*/
121   0,                          /*tp_as_sequence*/
122   0,                          /*tp_as_mapping*/
123   0,                          /*tp_hash*/
124   (ternaryfunc)callbackmcdataarrayint_call,  /*tp_call*/
125   0,                          /*tp_str*/
126   0,                          /*tp_getattro*/
127   0,                          /*tp_setattro*/
128   0,                          /*tp_as_buffer*/
129   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
130   0,                          /*tp_doc*/
131   0,                          /*tp_traverse*/
132   0,                          /*tp_clear*/
133   0,                          /*tp_richcompare*/
134   0,                          /*tp_weaklistoffset*/
135   0,                          /*tp_iter*/
136   0,                          /*tp_iternext*/
137   0,                          /*tp_methods*/
138   0,                          /*tp_members*/
139   0,                          /*tp_getset*/
140   0,                          /*tp_base*/
141   0,                          /*tp_dict*/
142   0,                          /*tp_descr_get*/
143   0,                          /*tp_descr_set*/
144   0,                          /*tp_dictoffset*/
145   callbackmcdataarray___init__,           /*tp_init*/
146   PyType_GenericAlloc,        /*tp_alloc*/
147   callbackmcdataarrayint___new__,            /*tp_new*/
148   PyObject_GC_Del,            /*tp_free*/
149 };
150
151 PyTypeObject PyCallBackDataArrayDouble_RefType = {
152   PyVarObject_HEAD_INIT(&PyType_Type, 0)
153   "callbackmcdataarraydouble",
154   sizeof(PyCallBackDataArrayDouble),
155   0,
156   callbackmcdataarray_dealloc,            /*tp_dealloc*/
157   0,                          /*tp_print*/
158   0,                          /*tp_getattr*/
159   0,                          /*tp_setattr*/
160   0,                          /*tp_compare*/
161   0,                          /*tp_repr*/
162   0,                          /*tp_as_number*/
163   0,                          /*tp_as_sequence*/
164   0,                          /*tp_as_mapping*/
165   0,                          /*tp_hash*/
166   (ternaryfunc)callbackmcdataarraydouble_call,  /*tp_call*/
167   0,                          /*tp_str*/
168   0,                          /*tp_getattro*/
169   0,                          /*tp_setattro*/
170   0,                          /*tp_as_buffer*/
171   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
172   0,                          /*tp_doc*/
173   0,                          /*tp_traverse*/
174   0,                          /*tp_clear*/
175   0,                          /*tp_richcompare*/
176   0,                          /*tp_weaklistoffset*/
177   0,                          /*tp_iter*/
178   0,                          /*tp_iternext*/
179   0,                          /*tp_methods*/
180   0,                          /*tp_members*/
181   0,                          /*tp_getset*/
182   0,                          /*tp_base*/
183   0,                          /*tp_dict*/
184   0,                          /*tp_descr_get*/
185   0,                          /*tp_descr_set*/
186   0,                          /*tp_dictoffset*/
187   callbackmcdataarray___init__,           /*tp_init*/
188   PyType_GenericAlloc,        /*tp_alloc*/
189   callbackmcdataarraydouble___new__,            /*tp_new*/
190   PyObject_GC_Del,            /*tp_free*/
191 };
192
193 // this is the second type of specific deallocator, only valid for the constructor of DataArrays taking numpy array
194 // in input when an another DataArray is already client of this.
195 template<class MCData>
196 void numarrdeal2(void *pt, void *obj)
197 {
198   typedef struct PyCallBackDataArraySt<MCData> PyCallBackDataArray;
199   void **obj1=(void **)obj;
200   PyCallBackDataArray *cbdaic=reinterpret_cast<PyCallBackDataArray *>(obj1[0]);
201   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(obj1[1]);
202   cbdaic->_pt_mc=0;
203   Py_XDECREF(weakRefOnOwner);
204   Py_XDECREF(cbdaic);
205   delete [] obj1;
206 }
207
208 template<class MCData, class T>
209 MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype, const char *msg)
210 {
211   int ndim=PyArray_NDIM(elt0);
212   if(ndim!=1 && ndim!=2)
213     throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !");
214   if(PyArray_DESCR(elt0)->type_num != npyObjectType)
215     {
216       std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!";
217       throw INTERP_KERNEL::Exception(oss.str().c_str());
218     }
219   npy_intp sz0=PyArray_DIM(elt0,0);
220   npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1;
221   //
222   int itemSize=PyArray_ITEMSIZE(elt0);
223   if(itemSize!=sizeof(T))
224     {
225       std::ostringstream oss; oss << "Input numpy array has not itemSize set to " << sizeof(T) << " !";
226       throw INTERP_KERNEL::Exception(oss.str().c_str());
227     }
228   if(itemSize*sz1!=PyArray_STRIDE(elt0,0))
229     throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays !");
230   if(ndim==2)
231     if(itemSize!=PyArray_STRIDE(elt0,1))
232       throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays for component #1 !");
233   const char *data=PyArray_BYTES(elt0);
234   typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<MCData> ret=MCData::New();
235   if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
236     {
237       PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
238       PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_OWNDATA)?elt0C:NULL;
239       int mask=NPY_OWNDATA; mask=~mask;
240       elt0C->flags&=mask;
241       PyObject *deepestObj=elt0;
242       PyObject *base=elt0C->base;
243       if(base) deepestObj=base;
244       while(base)
245         {
246           if(PyArray_Check(base))
247             {
248               PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
249               eltOwning=(PyArray_FLAGS(baseC) & NPY_OWNDATA)?baseC:eltOwning;
250               baseC->flags&=mask;
251               base=baseC->base;
252               if(base) deepestObj=base;
253             }
254           else
255             break;
256         }
257       typename ParaMEDMEM::MemArray<T>& mma=ret->accessToMemArray();
258       if(eltOwning==NULL)
259         {
260           PyCallBackDataArraySt<MCData> *cb=PyObject_GC_New(PyCallBackDataArraySt<MCData>,pytype);
261           cb->_pt_mc=ret;
262           ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
263           PyObject *ref=PyWeakref_NewRef(deepestObj,(PyObject *)cb);
264           void **objs=new void *[2]; objs[0]=cb; objs[1]=ref;
265           mma.setParameterForDeallocator(objs);
266           mma.setSpecificDeallocator(numarrdeal2<MCData>);
267           //"Impossible to share this numpy array chunk of data, because already shared by an another non numpy array object (maybe an another DataArrayInt instance) ! Release it, or perform a copy on the input array !");
268         }
269       else
270         {
271           ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
272           PyObject *ref=PyWeakref_NewRef(reinterpret_cast<PyObject *>(eltOwning),NULL);
273           void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) ParaMEDMEM::MemArray<T>::CDeallocator;
274           mma.setParameterForDeallocator(objs);
275           mma.setSpecificDeallocator(numarrdeal);
276         }
277     }
278   else if(PyArray_ISBEHAVED_RO(elt0))
279     ret->useArray(reinterpret_cast<const T *>(data),false,ParaMEDMEM::CPP_DEALLOC,sz0,sz1);
280   return ret.retn();
281 }
282
283
284 int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
285 {
286     if (obj == NULL) {
287         PyErr_SetString(PyExc_ValueError,
288                 "Cannot set the NumPy array 'base' "
289                 "dependency to NULL after initialization");
290         return -1;
291     }
292     /*
293      * Allow the base to be set only once. Once the object which
294      * owns the data is set, it doesn't make sense to change it.
295      */
296     if (PyArray_BASE(arr) != NULL) {
297         Py_DECREF(obj);
298         PyErr_SetString(PyExc_ValueError,
299                 "Cannot set the NumPy array 'base' "
300                 "dependency more than once");
301         return -1;
302     }
303
304     /*
305      * Don't allow infinite chains of views, always set the base
306      * to the first owner of the data.  
307      * That is, either the first object which isn't an array, 
308      * or the first object which owns its own data.
309      */
310
311     while (PyArray_Check(obj) && (PyObject *)arr != obj) {
312         PyArrayObject *obj_arr = (PyArrayObject *)obj;
313         PyObject *tmp;
314  
315
316         /* If this array owns its own data, stop collapsing */
317         if (PyArray_CHKFLAGS(obj_arr, NPY_OWNDATA)) {
318             break;
319         }   
320
321         tmp = PyArray_BASE(obj_arr);
322         /* If there's no base, stop collapsing */
323         if (tmp == NULL) {
324             break;
325         }
326         /* Stop the collapse new base when the would not be of the same 
327          * type (i.e. different subclass).
328          */
329         if (Py_TYPE(tmp) != Py_TYPE(arr)) {
330             break;
331         }
332
333
334         Py_INCREF(tmp);
335         Py_DECREF(obj);
336         obj = tmp;
337     }
338
339     /* Disallow circular references */
340     if ((PyObject *)arr == obj) {
341         Py_DECREF(obj);
342         PyErr_SetString(PyExc_ValueError,
343                 "Cannot create a circular NumPy array 'base' dependency");
344         return -1;
345     }
346
347     arr->base = obj;
348
349     return 0;
350 }
351
352 template<class MCData, class T>
353 PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
354 {
355   if(!self->isAllocated())
356     {
357       std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : this is not allocated !";
358       throw INTERP_KERNEL::Exception(oss.str().c_str());
359     }
360   ParaMEDMEM::MemArray<T>& mem=self->accessToMemArray();
361   int nbComp=self->getNumberOfComponents();
362   if(nbComp==0)
363     {
364       std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !"; 
365       throw INTERP_KERNEL::Exception(oss.str().c_str());
366     }
367   int nbDims=nbComp==1?1:2;
368   npy_intp dim[2];
369   dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp;
370   const T *bg=self->getConstPointer();
371   PyObject *ret=PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg));
372   if(mem.isDeallocatorCalled())
373     {
374       if(mem.getDeallocator()!=numarrdeal)
375         {// case for the first call of toNumPyArray
376           PyObject *ref=PyWeakref_NewRef(ret,NULL);
377           void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) mem.getDeallocator();
378           mem.setParameterForDeallocator(objs);
379           mem.setSpecificDeallocator(numarrdeal);
380           return ret;
381         }
382       else
383         {// case for the second and other call of toNumPyArray
384           void **objs=(void **)mem.getParameterForDeallocator();
385           PyObject *weakRefOnOwner=(PyObject *)objs[0];
386           PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
387           if(obj!=Py_None)
388             {//the previous numArray exists let numpy deals the numpy array each other by declaring the still alive instance as base
389               Py_XINCREF(obj);
390               NumpyArrSetBaseObjectExt((PyArrayObject*)ret,obj);
391             }
392           else
393             {//the previous numArray no more exists -> declare the newly created numpy array as the first one.
394               Py_XDECREF(weakRefOnOwner);
395               PyObject *ref=PyWeakref_NewRef(ret,NULL);
396               objs[0]=ref;
397             }
398         }
399     }
400   return ret;
401 }
402
403 SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
404 SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
405
406 PyObject *ToCSRMatrix(const std::vector<std::map<int,double> >& m, int nbCols) throw(INTERP_KERNEL::Exception)
407 {
408   int nbRows((int)m.size());
409   ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayInt> indPtr(ParaMEDMEM::DataArrayInt::New()),indices(ParaMEDMEM::DataArrayInt::New());
410   ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> data(ParaMEDMEM::DataArrayDouble::New());
411   indPtr->alloc(nbRows+1,1);
412   int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++;
413   int sz2(0);
414   for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++)
415     {
416       sz2+=(int)(*it0).size();
417       *intPtr_ptr=sz2;
418     }
419   indices->alloc(sz2,1); data->alloc(sz2,1);
420   int *indices_ptr(indices->getPointer());
421   double *data_ptr(data->getPointer());
422   for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++)
423     for(std::map<int,double>::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++)
424       {
425         *indices_ptr=(*it1).first;
426         *data_ptr=(*it1).second;
427       }
428   PyObject *a(ParaMEDMEM_DataArrayDouble_toNumPyArray(data)),*b(ParaMEDMEM_DataArrayInt_toNumPyArray(indices)),*c(ParaMEDMEM_DataArrayInt_toNumPyArray(indPtr));
429   //
430   PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2));
431   PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0);
432   PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols));
433   PyObject *tmp1(PyString_FromString("shape"));
434   PyDict_SetItem(kw,tmp1,kw1); Py_DECREF(tmp1); Py_DECREF(kw1);
435   PyObject* pdict=PyDict_New();
436   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
437   PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict));
438   if(!tmp)
439     throw INTERP_KERNEL::Exception("Problem during loading csr_matrix in scipy.sparse ! Is Scipy module available in present ?");
440   PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
441   if(!csrMatrixCls)
442     throw INTERP_KERNEL::Exception("csr_matrix not found in scipy.sparse ! Is Scipy module available in present ?");
443   PyObject *ret(PyObject_Call(csrMatrixCls,args,kw));
444   Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw);
445   return ret;
446 }
447
448 #endif
449
450 static PyObject *convertDataArrayChar(ParaMEDMEM::DataArrayChar *dac, int owner) throw(INTERP_KERNEL::Exception)
451 {
452   PyObject *ret=0;
453   if(!dac)
454     {
455       Py_XINCREF(Py_None);
456       return Py_None;
457     }
458   if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
459     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
460   if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
461     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
462   if(!ret)
463     throw INTERP_KERNEL::Exception("Not recognized type of DataArrayChar on downcast !");
464   return ret;
465 }
466
467 static PyObject *convertDataArray(ParaMEDMEM::DataArray *dac, int owner) throw(INTERP_KERNEL::Exception)
468 {
469   PyObject *ret=0;
470   if(!dac)
471     {
472       Py_XINCREF(Py_None);
473       return Py_None;
474     }
475   if(dynamic_cast<ParaMEDMEM::DataArrayDouble *>(dac))
476     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,owner);
477   if(dynamic_cast<ParaMEDMEM::DataArrayInt *>(dac))
478     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,owner);
479   if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
480     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
481   if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
482     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
483   if(!ret)
484     throw INTERP_KERNEL::Exception("Not recognized type of DataArray on downcast !");
485   return ret;
486 }
487
488 static PyObject *convertIntArrToPyList(const int *ptr, int size) throw(INTERP_KERNEL::Exception)
489 {
490   PyObject *ret=PyList_New(size);
491   for(int i=0;i<size;i++)
492     PyList_SetItem(ret,i,PyInt_FromLong(ptr[i]));
493   return ret;
494 }
495
496 static PyObject *convertIntArrToPyList2(const std::vector<int>& v) throw(INTERP_KERNEL::Exception)
497 {
498   int size=v.size();
499   PyObject *ret=PyList_New(size);
500   for(int i=0;i<size;i++)
501     PyList_SetItem(ret,i,PyInt_FromLong(v[i]));
502   return ret;
503 }
504
505 static PyObject *convertIntArrToPyList3(const std::set<int>& v) throw(INTERP_KERNEL::Exception)
506 {
507   int size=v.size();
508   PyObject *ret=PyList_New(size);
509   std::set<int>::const_iterator it=v.begin();
510   for(int i=0;i<size;i++,it++)
511     PyList_SetItem(ret,i,PyInt_FromLong(*it));
512   return ret;
513 }
514
515 static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
516 {
517   PyObject *ret=PyList_New(nbOfTuples);
518   for(int i=0;i<nbOfTuples;i++)
519     {
520       PyObject *t=PyTuple_New(nbOfComp);
521       for(int j=0;j<nbOfComp;j++)
522         PyTuple_SetItem(t,j,PyInt_FromLong(vals[i*nbOfComp+j]));
523       PyList_SetItem(ret,i,t);
524     }
525   return ret;
526 }
527
528 static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
529 {
530   if(PyList_Check(pyLi))
531     {
532       *size=PyList_Size(pyLi);
533       int *tmp=new int[*size];
534       for(int i=0;i<*size;i++)
535         {
536           PyObject *o=PyList_GetItem(pyLi,i);
537           if(PyInt_Check(o))
538             {
539               int val=(int)PyInt_AS_LONG(o);
540               tmp[i]=val;
541             }
542           else
543             {
544               delete [] tmp;
545               throw INTERP_KERNEL::Exception("list must contain integers only");
546             }
547         }
548       return tmp;
549     }
550   else if(PyTuple_Check(pyLi))
551     {
552       *size=PyTuple_Size(pyLi);
553       int *tmp=new int[*size];
554       for(int i=0;i<*size;i++)
555         {
556           PyObject *o=PyTuple_GetItem(pyLi,i);
557           if(PyInt_Check(o))
558             {
559               int val=(int)PyInt_AS_LONG(o);
560               tmp[i]=val;
561             }
562           else
563             {
564               delete [] tmp;
565               throw INTERP_KERNEL::Exception("tuple must contain integers only");
566             }
567         }
568       return tmp;
569     }
570   else
571     {
572       throw INTERP_KERNEL::Exception("convertPyToNewIntArr2 : not a list");
573     }
574 }
575
576 static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
577 {
578   const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
579   if(PyList_Check(pyLi))
580     {
581       int size=PyList_Size(pyLi);
582       arr.resize(size);
583       for(int i=0;i<size;i++)
584         {
585           PyObject *o=PyList_GetItem(pyLi,i);
586           if(PyTuple_Check(o))
587             {
588               int sz2=PyTuple_Size(o);
589               if(sz2!=2)
590                 throw INTERP_KERNEL::Exception(msg);
591               PyObject *o_0=PyTuple_GetItem(o,0);
592               if(!PyInt_Check(o_0))
593                 throw INTERP_KERNEL::Exception(msg);
594               PyObject *o_1=PyTuple_GetItem(o,1);
595               if(!PyInt_Check(o_1))
596                 throw INTERP_KERNEL::Exception(msg);
597               arr[i].first=(int)PyInt_AS_LONG(o_0);
598               arr[i].second=(int)PyInt_AS_LONG(o_1);
599             }
600           else
601             throw INTERP_KERNEL::Exception(msg);
602         }
603     }
604   else if(PyTuple_Check(pyLi))
605     {
606       int size=PyTuple_Size(pyLi);
607       arr.resize(size);
608       for(int i=0;i<size;i++)
609         {
610           PyObject *o=PyTuple_GetItem(pyLi,i);
611           if(PyTuple_Check(o))
612             {
613               int sz2=PyTuple_Size(o);
614               if(sz2!=2)
615                 throw INTERP_KERNEL::Exception(msg);
616               PyObject *o_0=PyTuple_GetItem(o,0);
617               if(!PyInt_Check(o_0))
618                 throw INTERP_KERNEL::Exception(msg);
619               PyObject *o_1=PyTuple_GetItem(o,1);
620               if(!PyInt_Check(o_1))
621                 throw INTERP_KERNEL::Exception(msg);
622               arr[i].first=(int)PyInt_AS_LONG(o_0);
623               arr[i].second=(int)PyInt_AS_LONG(o_1);
624             }
625           else
626             throw INTERP_KERNEL::Exception(msg);
627         }
628     }
629   else
630     throw INTERP_KERNEL::Exception(msg);
631 }
632
633 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
634 {
635   if(PyList_Check(pyLi))
636     {
637       int size=PyList_Size(pyLi);
638       arr.resize(size);
639       for(int i=0;i<size;i++)
640         {
641           PyObject *o=PyList_GetItem(pyLi,i);
642           if(PyInt_Check(o))
643             {
644               int val=(int)PyInt_AS_LONG(o);
645               arr[i]=val;
646             }
647           else
648             throw INTERP_KERNEL::Exception("list must contain integers only");
649         }
650     }
651   else if(PyTuple_Check(pyLi))
652     {
653       int size=PyTuple_Size(pyLi);
654       arr.resize(size);
655       for(int i=0;i<size;i++)
656         {
657           PyObject *o=PyTuple_GetItem(pyLi,i);
658           if(PyInt_Check(o))
659             {
660               int val=(int)PyInt_AS_LONG(o);
661               arr[i]=val;
662             }
663           else
664             throw INTERP_KERNEL::Exception("tuple must contain integers only");
665         }
666     }
667   else
668     {
669       throw INTERP_KERNEL::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
670     }
671 }
672
673 static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
674 {
675   if(recurseLev<0)
676     throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : invalid list of integers level of recursion !");
677   arr.clear();
678   if(PyList_Check(pyLi))
679     {
680       int size=PyList_Size(pyLi);
681       for(int i=0;i<size;i++)
682         {
683           PyObject *o=PyList_GetItem(pyLi,i);
684           if(PyInt_Check(o))
685             {
686               int val=(int)PyInt_AS_LONG(o);
687               arr.push_back(val);
688             }
689           else
690             {
691               std::vector<int> arr2;
692               convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
693               if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
694                   {
695                     std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " <<  recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
696                     throw INTERP_KERNEL::Exception(oss.str().c_str());
697                   }
698               arr.insert(arr.end(),arr2.begin(),arr2.end());
699             }
700         }
701     }
702   else if(PyTuple_Check(pyLi))
703     {
704       int size=PyTuple_Size(pyLi);
705       for(int i=0;i<size;i++)
706         {
707           PyObject *o=PyTuple_GetItem(pyLi,i);
708           if(PyInt_Check(o))
709             {
710               int val=(int)PyInt_AS_LONG(o);
711               arr.push_back(val);
712             }
713           else
714             {
715               std::vector<int> arr2;
716               convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
717               if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
718                   {
719                     std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " <<  recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
720                     throw INTERP_KERNEL::Exception(oss.str().c_str());
721                   }
722               arr.insert(arr.end(),arr2.begin(),arr2.end());
723             }
724         }
725     }
726   else
727     throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : not a list nor a tuple recursively !");
728 }
729
730 static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
731 {
732   if(nbOfTuples==-1)
733     {
734       if(nbOfComp==-1) { nbOfTuples=size1; nbOfComp=size2; }
735       else { if(nbOfComp==size2) { nbOfTuples=size1; } else
736           {
737             std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
738             oss << " whereas nb of components expected is " << nbOfComp << " !";
739             throw INTERP_KERNEL::Exception(oss.str().c_str());
740           } }
741     }
742   else
743     {
744       if(nbOfComp!=-1)
745         {
746           if((nbOfTuples!=size1 || nbOfComp!=size2))
747             {
748               if(size2!=1 || size1!=nbOfComp*nbOfTuples)
749                 {
750                   std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
751                   oss << " whereas nb of tuples expected is " << nbOfTuples << " and number of components expected is " << nbOfComp << " !";
752                   throw INTERP_KERNEL::Exception(oss.str().c_str());
753                 }
754             }
755         }
756       else
757         {
758           if(nbOfTuples==size1)
759             nbOfComp=size2;
760           else
761             {
762               std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
763               oss << " whereas nb of tuples expected is " << nbOfTuples << " !";
764               throw INTERP_KERNEL::Exception(oss.str().c_str());
765             }
766         }
767     }
768 }
769
770 static void fillArrayWithPyListInt3(PyObject *pyLi, int& nbOfElt, std::vector<int>& ret)
771 {
772   static const char MSG[]="fillArrayWithPyListInt3 : It appears that the input list or tuple is composed by elts having different sizes !";
773   if(PyInt_Check(pyLi))
774     {
775       long val=PyInt_AS_LONG(pyLi);
776       if(nbOfElt==-1)
777         nbOfElt=1;
778       else
779         if(nbOfElt!=1)
780           throw INTERP_KERNEL::Exception(MSG);
781       ret.push_back(val);
782     }
783   else if(PyList_Check(pyLi))
784     {
785       int size=PyList_Size(pyLi);
786       int tmp=0;
787       for(int i=0;i<size;i++)
788         {
789           PyObject *o=PyList_GetItem(pyLi,i);
790           int tmp1=-1;
791           fillArrayWithPyListInt3(o,tmp1,ret);
792           tmp+=tmp1;
793         }
794       if(nbOfElt==-1)
795         nbOfElt=tmp;
796       else
797         {
798           if(nbOfElt!=tmp)
799             throw INTERP_KERNEL::Exception(MSG);
800         }
801     }
802   else if(PyTuple_Check(pyLi))
803     {
804       int size=PyTuple_Size(pyLi);
805       int tmp=0;
806       for(int i=0;i<size;i++)
807         {
808           PyObject *o=PyTuple_GetItem(pyLi,i);
809           int tmp1=-1;
810           fillArrayWithPyListInt3(o,tmp1,ret);
811           tmp+=tmp1;
812         }
813       if(nbOfElt==-1)
814         nbOfElt=tmp;
815       else
816         {
817           if(nbOfElt!=tmp)
818             throw INTERP_KERNEL::Exception(MSG);
819         }
820     }
821   else
822     throw INTERP_KERNEL::Exception("fillArrayWithPyListInt3 : Unrecognized type ! Should be a composition of tuple,list,int !");
823 }
824
825 static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
826 {
827   std::vector<int> ret;
828   int size1=-1,size2=-1;
829   if(PyList_Check(pyLi))
830     {
831       size1=PyList_Size(pyLi);
832       for(int i=0;i<size1;i++)
833         {
834           PyObject *o=PyList_GetItem(pyLi,i);
835           fillArrayWithPyListInt3(o,size2,ret);
836         }
837       if(size1==0)
838         size2=1;
839     }
840   else if(PyTuple_Check(pyLi))
841     {
842       size1=PyTuple_Size(pyLi);
843       for(int i=0;i<size1;i++)
844         {
845           PyObject *o=PyTuple_GetItem(pyLi,i);
846           fillArrayWithPyListInt3(o,size2,ret);
847         }
848       if(size1==0)
849         size2=1;
850     }
851   else
852     throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !");
853   //
854   checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
855   return ret;
856 }
857
858 static bool fillStringVector(PyObject *pyLi, std::vector<std::string>& vec) throw(INTERP_KERNEL::Exception)
859 {
860   if(PyList_Check(pyLi))
861     {
862       Py_ssize_t sz=PyList_Size(pyLi);
863       vec.resize(sz);
864       for(int i=0;i<sz;i++)
865         {
866           PyObject *o=PyList_GetItem(pyLi,i);
867           if(PyString_Check(o))
868             vec[i]=PyString_AsString(o);
869           else
870             return false;
871         }
872       return true;
873     }
874   else if(PyTuple_Check(pyLi))
875     {
876       Py_ssize_t sz=PyTuple_Size(pyLi);
877       vec.resize(sz);
878       for(int i=0;i<sz;i++)
879         {
880           PyObject *o=PyTuple_GetItem(pyLi,i);
881           if(PyString_Check(o))
882             vec[i]=PyString_AsString(o);
883           else
884             return false;
885         }
886       return true;
887     }
888   else
889     return false;
890 }
891
892 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
893 {
894   PyObject *ret=PyList_New(size);
895   for(int i=0;i<size;i++)
896     PyList_SetItem(ret,i,PyFloat_FromDouble(ptr[i]));
897   return ret;
898 }
899
900 static PyObject *convertDblArrToPyList2(const std::vector<double>& v) throw(INTERP_KERNEL::Exception)
901 {
902   int size=v.size();
903   PyObject *ret=PyList_New(size);
904   for(int i=0;i<size;i++)
905     PyList_SetItem(ret,i,PyFloat_FromDouble(v[i]));
906   return ret;
907 }
908
909 static PyObject *convertDblArrToPyListOfTuple(const double *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
910 {
911   PyObject *ret=PyList_New(nbOfTuples);
912   for(int i=0;i<nbOfTuples;i++)
913     {
914       PyObject *t=PyTuple_New(nbOfComp);
915       for(int j=0;j<nbOfComp;j++)
916         PyTuple_SetItem(t,j,PyFloat_FromDouble(vals[i*nbOfComp+j]));
917       PyList_SetItem(ret,i,t);
918     }
919   return ret;
920 }
921
922 static PyObject *convertCharArrToPyListOfTuple(const char *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
923 {
924   PyObject *ret=PyList_New(nbOfTuples);
925   INTERP_KERNEL::AutoPtr<char> tmp=new char[nbOfComp+1]; tmp[nbOfComp]='\0';
926   for(int i=0;i<nbOfTuples;i++)
927     {
928       std::copy(vals+i*nbOfComp,vals+(i+1)*nbOfComp,(char *)tmp);
929       PyList_SetItem(ret,i,PyString_FromString(tmp));
930     }
931   return ret;
932 }
933
934 static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
935 {
936   if(PyList_Check(pyLi))
937     {
938       *size=PyList_Size(pyLi);
939       double *tmp=(double *)malloc((*size)*sizeof(double));
940       for(int i=0;i<*size;i++)
941         {
942           PyObject *o=PyList_GetItem(pyLi,i);
943           if(PyFloat_Check(o))
944             {
945               double val=PyFloat_AS_DOUBLE(o);
946               tmp[i]=val;
947             }
948           else if(PyInt_Check(o))
949             {
950               long val0=PyInt_AS_LONG(o);
951               double val=val0;
952               tmp[i]=val;
953             }
954           else
955             {
956               free(tmp);
957               throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : list must contain floats/integers only");
958             }
959         }
960       return tmp;
961     }
962   else if(PyTuple_Check(pyLi))
963     {
964       *size=PyTuple_Size(pyLi);
965       double *tmp=(double *)malloc((*size)*sizeof(double));
966       for(int i=0;i<*size;i++)
967         {
968           PyObject *o=PyTuple_GetItem(pyLi,i);
969           if(PyFloat_Check(o))
970             {
971               double val=PyFloat_AS_DOUBLE(o);
972               tmp[i]=val;
973             }
974           else if(PyInt_Check(o))
975             {
976               long val0=PyInt_AS_LONG(o);
977               double val=val0;
978               tmp[i]=val;
979             }
980           else
981             {
982               free(tmp);
983               throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : tuple must contain floats/integers only");
984             }
985         }
986       return tmp;
987     }
988   else
989     throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : not a list");
990 }
991
992 static void fillArrayWithPyListDbl3(PyObject *pyLi, int& nbOfElt, std::vector<double>& ret)
993 {
994   static const char MSG[]="fillArrayWithPyListDbl3 : It appears that the input list or tuple is composed by elts having different sizes !";
995   if(PyFloat_Check(pyLi))
996     {
997       if(nbOfElt==-1)
998         nbOfElt=1;
999       else
1000         if(nbOfElt!=1)
1001           throw INTERP_KERNEL::Exception(MSG);
1002       double val=PyFloat_AS_DOUBLE(pyLi);
1003       ret.push_back(val);
1004     }
1005   else if(PyInt_Check(pyLi))
1006     {
1007       long val0=PyInt_AS_LONG(pyLi);
1008       double val=val0;
1009       if(nbOfElt==-1)
1010         nbOfElt=1;
1011       else
1012         if(nbOfElt!=1)
1013           throw INTERP_KERNEL::Exception(MSG);
1014       ret.push_back(val);
1015     }
1016   else if(PyList_Check(pyLi))
1017     {
1018       int size=PyList_Size(pyLi);
1019       int tmp=0;
1020       for(int i=0;i<size;i++)
1021         {
1022           PyObject *o=PyList_GetItem(pyLi,i);
1023           int tmp1=-1;
1024           fillArrayWithPyListDbl3(o,tmp1,ret);
1025           tmp+=tmp1;
1026         }
1027       if(nbOfElt==-1)
1028         nbOfElt=tmp;
1029       else
1030         {
1031           if(nbOfElt!=tmp)
1032             throw INTERP_KERNEL::Exception(MSG);
1033         }
1034     }
1035   else if(PyTuple_Check(pyLi))
1036     {
1037       int size=PyTuple_Size(pyLi);
1038       int tmp=0;
1039       for(int i=0;i<size;i++)
1040         {
1041           PyObject *o=PyTuple_GetItem(pyLi,i);
1042           int tmp1=-1;
1043           fillArrayWithPyListDbl3(o,tmp1,ret);
1044           tmp+=tmp1;
1045         }
1046       if(nbOfElt==-1)
1047         nbOfElt=tmp;
1048       else
1049         {
1050           if(nbOfElt!=tmp)
1051             throw INTERP_KERNEL::Exception(MSG);
1052         }
1053     }
1054   else
1055     throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl3 : Unrecognized type ! Should be a composition of tuple,list,int and float !");
1056 }
1057
1058 static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
1059 {
1060   std::vector<double> ret;
1061   int size1=-1,size2=-1;
1062   if(PyList_Check(pyLi))
1063     {
1064       size1=PyList_Size(pyLi);
1065       for(int i=0;i<size1;i++)
1066         {
1067           PyObject *o=PyList_GetItem(pyLi,i);
1068           fillArrayWithPyListDbl3(o,size2,ret);
1069         }
1070       if(size1==0)
1071         size2=1;
1072     }
1073   else if(PyTuple_Check(pyLi))
1074     {
1075       size1=PyTuple_Size(pyLi);
1076       for(int i=0;i<size1;i++)
1077         {
1078           PyObject *o=PyTuple_GetItem(pyLi,i);
1079           fillArrayWithPyListDbl3(o,size2,ret);
1080         }
1081       if(size1==0)
1082         size2=1;
1083     }
1084   else
1085     throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !");
1086   //
1087   checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
1088   return ret;
1089 }
1090
1091 //convertFromPyObjVectorOfObj<const ParaMEDMEM::MEDCouplingUMesh *>(pyLi,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,"MEDCouplingUMesh")
1092 template<class T>
1093 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
1094 {
1095   void *argp=0;
1096   if(PyList_Check(pyLi))
1097     {
1098       int size=PyList_Size(pyLi);
1099       ret.resize(size);
1100       for(int i=0;i<size;i++)
1101         {
1102           PyObject *obj=PyList_GetItem(pyLi,i);
1103           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1104           if(!SWIG_IsOK(status))
1105             {
1106               std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
1107               throw INTERP_KERNEL::Exception(oss.str().c_str());
1108             }
1109           T arg=reinterpret_cast< T >(argp);
1110           ret[i]=arg;
1111         }
1112     }
1113   else if(PyTuple_Check(pyLi))
1114     {
1115       int size=PyTuple_Size(pyLi);
1116       ret.resize(size);
1117       for(int i=0;i<size;i++)
1118         {
1119           PyObject *obj=PyTuple_GetItem(pyLi,i);
1120           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1121           if(!SWIG_IsOK(status))
1122             {
1123               std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
1124               throw INTERP_KERNEL::Exception(oss.str().c_str());
1125             }
1126           T arg=reinterpret_cast< T >(argp);
1127           ret[i]=arg;
1128         }
1129     }
1130   else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
1131     {
1132       ret.resize(1);
1133       T arg=reinterpret_cast< T >(argp);
1134       ret[0]=arg;
1135     }
1136   else
1137     throw INTERP_KERNEL::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
1138 }
1139
1140 /*!
1141  * if python int -> cpp int sw=1
1142  * if python list[int] -> cpp vector<int> sw=2
1143  * if python tuple[int] -> cpp vector<int> sw=2
1144  * if python DataArrayInt -> cpp DataArrayInt sw=3
1145  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
1146  *
1147  * switch between (int,vector<int>,DataArrayInt)
1148  */
1149 static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, ParaMEDMEM::DataArrayInt *& daIntTyypp, ParaMEDMEM::DataArrayIntTuple *&daIntTuple) throw(INTERP_KERNEL::Exception)
1150 {
1151   sw=-1;
1152   if(PyInt_Check(value))
1153     {
1154       iTyypp=(int)PyInt_AS_LONG(value);
1155       sw=1;
1156       return;
1157     }
1158   if(PyTuple_Check(value))
1159     {
1160       int size=PyTuple_Size(value);
1161       stdvecTyypp.resize(size);
1162       for(int i=0;i<size;i++)
1163         {
1164           PyObject *o=PyTuple_GetItem(value,i);
1165           if(PyInt_Check(o))
1166             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1167           else
1168             {
1169               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1170               throw INTERP_KERNEL::Exception(oss.str().c_str());
1171             }
1172         }
1173       sw=2;
1174       return;
1175     }
1176   if(PyList_Check(value))
1177     {
1178       int size=PyList_Size(value);
1179       stdvecTyypp.resize(size);
1180       for(int i=0;i<size;i++)
1181         {
1182           PyObject *o=PyList_GetItem(value,i);
1183           if(PyInt_Check(o))
1184             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1185           else
1186             {
1187               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1188               throw INTERP_KERNEL::Exception(oss.str().c_str());
1189             }
1190         }
1191       sw=2;
1192       return;
1193     }
1194   void *argp;
1195   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1196   if(SWIG_IsOK(status))
1197     {
1198       daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1199       sw=3;
1200       return;
1201     }
1202   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1203   if(SWIG_IsOK(status))
1204     {  
1205       daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1206       sw=4;
1207       return ;
1208     }
1209   throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
1210 }
1211
1212 /*!
1213  * if python double -> cpp double sw=1
1214  * if python int -> cpp double sw=1
1215  * if python list[double] -> cpp vector<double> sw=2
1216  * if python list[int] -> cpp vector<double> sw=2
1217  * if python tuple[double] -> cpp vector<double> sw=2
1218  * if python tuple[int] -> cpp vector<double> sw=2
1219  * if python DataArrayDouble -> cpp DataArrayDouble sw=3
1220  *
1221  * switch between (int,vector<int>,DataArrayInt)
1222  */
1223 static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDouble *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1224 {
1225   sw=-1;
1226   if(PyFloat_Check(value))
1227     {
1228       iTyypp=PyFloat_AS_DOUBLE(value);
1229       sw=1;
1230       return;
1231     }
1232   if(PyInt_Check(value))
1233     {
1234       iTyypp=(double)PyInt_AS_LONG(value);
1235       sw=1;
1236       return;
1237     }
1238   if(PyTuple_Check(value))
1239     {
1240       int size=PyTuple_Size(value);
1241       stdvecTyypp.resize(size);
1242       for(int i=0;i<size;i++)
1243         {
1244           PyObject *o=PyTuple_GetItem(value,i);
1245           if(PyFloat_Check(o))
1246             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1247           else if(PyInt_Check(o))
1248             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1249           else
1250             {
1251               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1252               throw INTERP_KERNEL::Exception(oss.str().c_str());
1253             }
1254         }
1255       sw=2;
1256       return;
1257     }
1258   if(PyList_Check(value))
1259     {
1260       int size=PyList_Size(value);
1261       stdvecTyypp.resize(size);
1262       for(int i=0;i<size;i++)
1263         {
1264           PyObject *o=PyList_GetItem(value,i);
1265           if(PyFloat_Check(o))
1266             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1267           else if(PyInt_Check(o))
1268             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1269           else
1270             {
1271               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1272               throw INTERP_KERNEL::Exception(oss.str().c_str());
1273             }
1274         }
1275       sw=2;
1276       return;
1277     }
1278   void *argp;
1279   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1280   if(!SWIG_IsOK(status))
1281     throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDouble");
1282   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1283   sw=3;
1284 }
1285
1286 /*!
1287  * if python double -> cpp double sw=1
1288  * if python int -> cpp double sw=1
1289  * if python list[double] -> cpp vector<double> sw=2
1290  * if python list[int] -> cpp vector<double> sw=2
1291  * if python tuple[double] -> cpp vector<double> sw=2
1292  * if python tuple[int] -> cpp vector<double> sw=2
1293  * if python DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1294  *
1295  * switch between (int,vector<int>,DataArrayInt)
1296  */
1297 static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1298 {
1299   sw=-1;
1300   if(PyFloat_Check(value))
1301     {
1302       iTyypp=PyFloat_AS_DOUBLE(value);
1303       sw=1;
1304       return;
1305     }
1306   if(PyInt_Check(value))
1307     {
1308       iTyypp=(double)PyInt_AS_LONG(value);
1309       sw=1;
1310       return;
1311     }
1312   if(PyTuple_Check(value))
1313     {
1314       int size=PyTuple_Size(value);
1315       stdvecTyypp.resize(size);
1316       for(int i=0;i<size;i++)
1317         {
1318           PyObject *o=PyTuple_GetItem(value,i);
1319           if(PyFloat_Check(o))
1320             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1321           else if(PyInt_Check(o))
1322             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1323           else
1324             {
1325               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1326               throw INTERP_KERNEL::Exception(oss.str().c_str());
1327             }
1328         }
1329       sw=2;
1330       return;
1331     }
1332   if(PyList_Check(value))
1333     {
1334       int size=PyList_Size(value);
1335       stdvecTyypp.resize(size);
1336       for(int i=0;i<size;i++)
1337         {
1338           PyObject *o=PyList_GetItem(value,i);
1339           if(PyFloat_Check(o))
1340             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1341           else if(PyInt_Check(o))
1342             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1343           else
1344             {
1345               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1346               throw INTERP_KERNEL::Exception(oss.str().c_str());
1347             }
1348         }
1349       sw=2;
1350       return;
1351     }
1352   void *argp;
1353   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1354   if(!SWIG_IsOK(status))
1355     throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDoubleTuple");
1356   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1357   sw=3;
1358 }
1359
1360 /*!
1361  * if python int -> cpp int sw=1
1362  * if python list[int] -> cpp vector<int> sw=2
1363  * if python tuple[int] -> cpp vector<int> sw=2
1364  * if python slicp -> cpp pair sw=3 (begin,end,step)
1365  * if python DataArrayInt -> cpp DataArrayInt sw=4 . The returned pointer cannot be the null pointer ! If null an exception is thrown.
1366  *
1367  * switch between (int,vector<int>,DataArrayInt)
1368  */
1369 static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1370 {
1371   const char *msg="5 types accepted : integer, tuple of integer, list of integer, slice, DataArrayInt, DataArrayIntTuple";
1372   sw=-1;
1373   if(PyInt_Check(value))
1374     {
1375       iTyypp=(int)PyInt_AS_LONG(value);
1376       sw=1;
1377       return;
1378     }
1379   if(PyTuple_Check(value))
1380     {
1381       int size=PyTuple_Size(value);
1382       stdvecTyypp.resize(size);
1383       for(int i=0;i<size;i++)
1384         {
1385           PyObject *o=PyTuple_GetItem(value,i);
1386           if(PyInt_Check(o))
1387             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1388           else
1389             {
1390               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1391               throw INTERP_KERNEL::Exception(oss.str().c_str());
1392             }
1393         }
1394       sw=2;
1395       return;
1396     }
1397   if(PyList_Check(value))
1398     {
1399       int size=PyList_Size(value);
1400       stdvecTyypp.resize(size);
1401       for(int i=0;i<size;i++)
1402         {
1403           PyObject *o=PyList_GetItem(value,i);
1404           if(PyInt_Check(o))
1405             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1406           else
1407             {
1408               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1409               throw INTERP_KERNEL::Exception(oss.str().c_str());
1410             }
1411         }
1412       sw=2;
1413       return;
1414     }
1415   if(PySlice_Check(value))
1416     {
1417       Py_ssize_t strt=2,stp=2,step=2;
1418       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1419       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1420         if(nbelem!=0 || strt!=0 || stp!=0)
1421           {
1422             std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1423             throw INTERP_KERNEL::Exception(oss.str().c_str());
1424           }
1425       p.first=strt;
1426       p.second.first=stp;
1427       p.second.second=step;
1428       sw=3;
1429       return ;
1430     }
1431   void *argp;
1432   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1433   if(SWIG_IsOK(status))
1434     {
1435       daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1436       if(!daIntTyypp)
1437         {
1438           std::ostringstream oss; oss << msg << " Instance in null !";
1439           throw INTERP_KERNEL::Exception(oss.str().c_str());
1440         }
1441       sw=4;
1442       return ;
1443     }
1444   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1445   if(SWIG_IsOK(status))
1446     {
1447       ParaMEDMEM::DataArrayIntTuple *tmp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1448       if(!tmp)
1449         {
1450           std::ostringstream oss; oss << msg << " Instance in null !";
1451           throw INTERP_KERNEL::Exception(oss.str().c_str());
1452         }
1453       stdvecTyypp.resize(tmp->getNumberOfCompo());
1454       std::copy(tmp->getConstPointer(),tmp->getConstPointer()+tmp->getNumberOfCompo(),stdvecTyypp.begin());
1455       sw=2;
1456       return ;
1457     }
1458   throw INTERP_KERNEL::Exception(msg);
1459 }
1460
1461 /*!
1462  * if python int -> cpp int sw=1
1463  * if python tuple[int] -> cpp vector<int> sw=2
1464  * if python list[int] -> cpp vector<int> sw=2
1465  * if python slice -> cpp pair sw=3
1466  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 . WARNING The returned pointer can be the null pointer !
1467  */
1468 static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayIntTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1469 {
1470   sw=-1;
1471   if(PyInt_Check(value))
1472     {
1473       iTyypp=(int)PyInt_AS_LONG(value);
1474       sw=1;
1475       return;
1476     }
1477   if(PyTuple_Check(value))
1478     {
1479       int size=PyTuple_Size(value);
1480       stdvecTyypp.resize(size);
1481       for(int i=0;i<size;i++)
1482         {
1483           PyObject *o=PyTuple_GetItem(value,i);
1484           if(PyInt_Check(o))
1485             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1486           else
1487             {
1488               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1489               throw INTERP_KERNEL::Exception(oss.str().c_str());
1490             }
1491         }
1492       sw=2;
1493       return;
1494     }
1495   if(PyList_Check(value))
1496     {
1497       int size=PyList_Size(value);
1498       stdvecTyypp.resize(size);
1499       for(int i=0;i<size;i++)
1500         {
1501           PyObject *o=PyList_GetItem(value,i);
1502           if(PyInt_Check(o))
1503             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1504           else
1505             {
1506               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1507               throw INTERP_KERNEL::Exception(oss.str().c_str());
1508             }
1509         }
1510       sw=2;
1511       return;
1512     }
1513   if(PySlice_Check(value))
1514     {
1515       Py_ssize_t strt=2,stp=2,step=2;
1516       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1517       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1518         if(nbelem!=0 || strt!=0 || stp!=0)
1519           {
1520             std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1521             throw INTERP_KERNEL::Exception(oss.str().c_str());
1522           }
1523       p.first=strt;
1524       p.second.first=stp;
1525       p.second.second=step;
1526       sw=3;
1527       return ;
1528     }
1529   void *argp;
1530   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1531   if(!SWIG_IsOK(status))
1532     throw INTERP_KERNEL::Exception("4 types accepted : integer, tuple of integer, list of integer, slice, DataArrayIntTuple");
1533   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1534   sw=4;
1535 }
1536
1537 /*!
1538  * if python string with size one -> cpp char sw=1
1539  * if python string with size different from one -> cpp string sw=2
1540  * if python tuple[string] or list[string] -> vector<string> sw=3
1541  * if python not null pointer of DataArrayChar -> cpp DataArrayChar sw=4
1542  * switch between (int,string,vector<string>,DataArrayChar)
1543  */
1544 static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector<std::string>& vsType, ParaMEDMEM::DataArrayChar *& dacType) throw(INTERP_KERNEL::Exception)
1545 {
1546   const char *msg="4 types accepted : string, list or tuple of strings having same size, not null DataArrayChar instance.";
1547   sw=-1;
1548   if(PyString_Check(value))
1549     {
1550       const char *pt=PyString_AsString(value);
1551       Py_ssize_t sz=PyString_Size(value);
1552       if(sz==1)
1553         {
1554           cTyp=pt[0];
1555           sw=1;
1556           return;
1557         }
1558       else
1559         {
1560           sType=pt;
1561           sw=2;
1562           return;
1563         }
1564     }
1565   if(PyTuple_Check(value))
1566     {
1567       int size=PyTuple_Size(value);
1568       vsType.resize(size);
1569       for(int i=0;i<size;i++)
1570         {
1571           PyObject *o=PyTuple_GetItem(value,i);
1572           if(PyString_Check(o))
1573             vsType[i]=PyString_AsString(o);
1574           else
1575             {
1576               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not a string ! only tuples of strings accepted !";
1577               throw INTERP_KERNEL::Exception(oss.str().c_str());
1578             }
1579         }
1580       sw=3;
1581       return;
1582     }
1583   if(PyList_Check(value))
1584     {
1585       int size=PyList_Size(value);
1586       vsType.resize(size);
1587       for(int i=0;i<size;i++)
1588         {
1589           PyObject *o=PyList_GetItem(value,i);
1590           if(PyString_Check(o))
1591             vsType[i]=PyString_AsString(o);
1592           else
1593             {
1594               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not string ! only lists of strings accepted !";
1595               throw INTERP_KERNEL::Exception(oss.str().c_str());
1596             }
1597         }
1598       sw=3;
1599       return;
1600     }
1601   void *argp;
1602   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayChar,0|0);
1603   if(SWIG_IsOK(status))
1604     {
1605       dacType=reinterpret_cast< ParaMEDMEM::DataArrayChar * >(argp);
1606       if(!dacType)
1607         {
1608           std::ostringstream oss; oss << msg << " Instance in null !";
1609           throw INTERP_KERNEL::Exception(oss.str().c_str());
1610         }
1611       sw=4;
1612       return ;
1613     }
1614   throw INTERP_KERNEL::Exception(msg);
1615 }
1616
1617 /*!
1618  * if value int -> cpp it sw=1
1619  * if value list[int] -> vt sw=2
1620  * if value tuple[int] -> vt sw=2
1621  * if value slice -> pt sw=3
1622  * if value DataArrayInt -> dt sw=4
1623  * if value tuple [int,int] -> cpp it,ip sw=5
1624  * if value tuple [list[int],int] -> cpp vt,ip sw=6
1625  * if value tuple [tuple[int],int] -> cpp vt,ip sw=6
1626  * if value tuple [slice,int] -> cpp pt,ip sw=7
1627  * if value tuple [DaI,int] -> cpp dt,ip sw=8
1628  * if value tuple [int,list[int]] -> cpp it,vc sw=9
1629  * if value tuple [list[int],list[int]] -> cpp vt,vc sw=10
1630  * if value tuple [tuple[int],list[int]] -> cpp vt,vc sw=10
1631  * if value tuple [slice,list[int]] -> cpp pt,vc sw=11
1632  * if value tuple [DaI,list[int]] -> cpp dt,vc sw=12
1633  * if value tuple [int,tuple[int]] -> cpp it,vc sw=9
1634  * if value tuple [list[int],tuple[int]] -> cpp vt,vc sw=10
1635  * if value tuple [tuple[int],tuple[int]] -> cpp vt,vc sw=10
1636  * if value tuple [slice,tuple[int]] -> cpp pt,vc sw=11
1637  * if value tuple [DaI,tuple[int]] -> cpp dt,vc sw=12
1638  * if value tuple [int,slice] -> cpp it,pc sw=13
1639  * if value tuple [list[int],slice] -> cpp vt,pc sw=14
1640  * if value tuple [tuple[int],slice] -> cpp vt,pc sw=14
1641  * if value tuple [slice,slice] -> cpp pt,pc sw=15
1642  * if value tuple [DaI,slice] -> cpp dt,pc sw=16
1643  *
1644  * switch between (int,vector<int>,DataArrayInt)
1645  */
1646 static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, int& sw, int& it, int& ic, std::vector<int>& vt, std::vector<int>& vc,
1647                                      std::pair<int, std::pair<int,int> >& pt, std::pair<int, std::pair<int,int> >& pc,
1648                                      ParaMEDMEM::DataArrayInt *&dt, ParaMEDMEM::DataArrayInt *&dc) throw(INTERP_KERNEL::Exception)
1649 {
1650   if(!PyTuple_Check(value))
1651     {
1652       convertObjToPossibleCpp2(value,nbTuple,sw,it,vt,pt,dt);
1653       return ;
1654     }
1655   else
1656     {
1657       int sz=PyTuple_Size(value);
1658       if(sz!=2)
1659         throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !");
1660       PyObject *ob0=PyTuple_GetItem(value,0);
1661       int sw1,sw2;
1662       convertObjToPossibleCpp2(ob0,nbTuple,sw1,it,vt,pt,dt);
1663       PyObject *ob1=PyTuple_GetItem(value,1);
1664       convertObjToPossibleCpp2(ob1,nbCompo,sw2,ic,vc,pc,dc);
1665       sw=4*sw2+sw1;
1666     }
1667 }
1668
1669 /*!
1670  * if value int -> cpp val sw=1
1671  * if value double -> cpp val sw=1
1672  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1673  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1674  * if value list[int,double] -> cpp std::vector<double> sw=4
1675  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1676  */
1677 static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f)
1678 {
1679   sw=-1;
1680   if(PyFloat_Check(value))
1681     {
1682       val=PyFloat_AS_DOUBLE(value);
1683       sw=1;
1684       return;
1685     }
1686   if(PyInt_Check(value))
1687     {
1688       val=(double)PyInt_AS_LONG(value);
1689       sw=1;
1690       return;
1691     }
1692   if(PyTuple_Check(value))
1693     {
1694       int size=PyTuple_Size(value);
1695       f.resize(size);
1696       for(int i=0;i<size;i++)
1697         {
1698           PyObject *o=PyTuple_GetItem(value,i);
1699           if(PyFloat_Check(o))
1700             f[i]=PyFloat_AS_DOUBLE(o);
1701           else if(PyInt_Check(o))
1702             f[i]=(double)PyInt_AS_LONG(o);
1703           else
1704             {
1705               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1706               throw INTERP_KERNEL::Exception(oss.str().c_str());
1707             }
1708         }
1709       sw=4;
1710       return;
1711     }
1712   if(PyList_Check(value))
1713     {
1714       int size=PyList_Size(value);
1715       f.resize(size);
1716       for(int i=0;i<size;i++)
1717         {
1718           PyObject *o=PyList_GetItem(value,i);
1719           if(PyFloat_Check(o))
1720             f[i]=PyFloat_AS_DOUBLE(o);
1721           else if(PyInt_Check(o))
1722             f[i]=(double)PyInt_AS_LONG(o);
1723           else
1724             {
1725               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1726               throw INTERP_KERNEL::Exception(oss.str().c_str());
1727             }
1728         }
1729       sw=4;
1730       return;
1731     }
1732   void *argp;
1733   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1734   if(SWIG_IsOK(status))
1735     {  
1736       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1737       sw=2;
1738       return ;
1739     }
1740   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1741   if(SWIG_IsOK(status))
1742     {  
1743       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1744       sw=3;
1745       return ;
1746     }
1747   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1748 }
1749
1750 /*!
1751  * if value int -> cpp val sw=1
1752  * if value double -> cpp val sw=1
1753  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1754  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1755  * if value list[int,double] -> cpp std::vector<double> sw=4
1756  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1757  */
1758 static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1759                                                    const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception)
1760 {
1761   sw=-1;
1762   if(PyFloat_Check(value))
1763     {
1764       val=PyFloat_AS_DOUBLE(value);
1765       sw=1;
1766       if(nbTuplesExpected*nbCompExpected!=1)
1767         {
1768           std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; 
1769           throw INTERP_KERNEL::Exception(oss.str().c_str());
1770         }
1771       return &val;
1772     }
1773   if(PyInt_Check(value))
1774     {
1775       val=(double)PyInt_AS_LONG(value);
1776       sw=1;
1777       if(nbTuplesExpected*nbCompExpected!=1)
1778         {
1779           std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; 
1780           throw INTERP_KERNEL::Exception(oss.str().c_str());
1781         }
1782       return &val;
1783     }
1784   if(PyTuple_Check(value) || PyList_Check(value))
1785     {
1786       try
1787         {
1788           int tmp1=nbTuplesExpected,tmp2=nbCompExpected;
1789           std::vector<double> ret=fillArrayWithPyListDbl2(value,tmp1,tmp2);
1790           sw=4;
1791           f=ret;
1792           return &f[0];
1793         }
1794       catch(INTERP_KERNEL::Exception& exc) { throw exc; }
1795     }
1796   void *argp;
1797   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1798   if(SWIG_IsOK(status))
1799     {  
1800       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1801       sw=2;
1802       if(d)
1803         {
1804           if(d->getNumberOfTuples()==nbTuplesExpected)
1805             {
1806               if(d->getNumberOfComponents()==nbCompExpected)
1807                 {
1808                   return d->getConstPointer();
1809                 }
1810               else
1811                 {
1812                   std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
1813                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1814                 }
1815             }
1816           else
1817             {
1818               std::ostringstream oss; oss << msg << " input DataArrayDouble should have a number of tuples equal to " << nbTuplesExpected << " and there are " << d->getNumberOfTuples() << " tuples !";
1819               throw INTERP_KERNEL::Exception(oss.str().c_str());
1820             }
1821         }
1822       else
1823         {
1824           if(throwIfNullPt)
1825             {
1826               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1827               throw INTERP_KERNEL::Exception(oss.str().c_str());
1828             }
1829           else
1830             return 0;
1831         }
1832     }
1833   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1834   if(SWIG_IsOK(status))
1835     {  
1836       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1837       sw=3;
1838       if(e->getNumberOfCompo()==nbCompExpected)
1839         {
1840           if(nbTuplesExpected==1)
1841             return e->getConstPointer();
1842           else
1843             {
1844               std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !";
1845               throw INTERP_KERNEL::Exception(oss.str().c_str());
1846             }
1847         }
1848       else
1849         {
1850           std::ostringstream oss; oss << msg << "nb of components expected to be " <<  nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
1851           throw INTERP_KERNEL::Exception(oss.str().c_str());
1852         }
1853     }
1854   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1855 }
1856
1857 /*!
1858  * if value int -> cpp val sw=1
1859  * if value double -> cpp val sw=1
1860  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1861  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1862  * if value list[int,double] -> cpp std::vector<double> sw=4
1863  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1864  */
1865 static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1866                                                     const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
1867 {
1868   sw=-1;
1869   if(PyFloat_Check(value))
1870     {
1871       val=PyFloat_AS_DOUBLE(value);
1872       sw=1;
1873       if(nbCompExpected!=1)
1874         {
1875           std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; 
1876           throw INTERP_KERNEL::Exception(oss.str().c_str());
1877         }
1878       nbTuples=1;
1879       return &val;
1880     }
1881   if(PyInt_Check(value))
1882     {
1883       val=(double)PyInt_AS_LONG(value);
1884       sw=1;
1885       if(nbCompExpected!=1)
1886         {
1887           std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; 
1888           throw INTERP_KERNEL::Exception(oss.str().c_str());
1889         }
1890       nbTuples=1;
1891       return &val;
1892     }
1893   if(PyTuple_Check(value))
1894     {
1895       int size=PyTuple_Size(value);
1896       f.resize(size);
1897       for(int i=0;i<size;i++)
1898         {
1899           PyObject *o=PyTuple_GetItem(value,i);
1900           if(PyFloat_Check(o))
1901             f[i]=PyFloat_AS_DOUBLE(o);
1902           else if(PyInt_Check(o))
1903             f[i]=(double)PyInt_AS_LONG(o);
1904           else
1905             {
1906               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1907               throw INTERP_KERNEL::Exception(oss.str().c_str());
1908             }
1909         }
1910       sw=4;
1911       if(size%nbCompExpected!=0)
1912         {
1913           std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
1914           throw INTERP_KERNEL::Exception(oss.str().c_str());
1915         }
1916       nbTuples=size/nbCompExpected;
1917       return &f[0];
1918     }
1919   if(PyList_Check(value))
1920     {
1921       int size=PyList_Size(value);
1922       f.resize(size);
1923       for(int i=0;i<size;i++)
1924         {
1925           PyObject *o=PyList_GetItem(value,i);
1926           if(PyFloat_Check(o))
1927             f[i]=PyFloat_AS_DOUBLE(o);
1928           else if(PyInt_Check(o))
1929             f[i]=(double)PyInt_AS_LONG(o);
1930           else
1931             {
1932               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1933               throw INTERP_KERNEL::Exception(oss.str().c_str());
1934             }
1935         }
1936       sw=4;
1937       if(size%nbCompExpected!=0)
1938         {
1939           std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
1940           throw INTERP_KERNEL::Exception(oss.str().c_str());
1941         }
1942       nbTuples=size/nbCompExpected;
1943       return &f[0];
1944     }
1945   void *argp;
1946   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1947   if(SWIG_IsOK(status))
1948     {  
1949       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1950       sw=2;
1951       if(d)
1952         {
1953           if(d->getNumberOfComponents()==nbCompExpected)
1954             {
1955               nbTuples=d->getNumberOfTuples();
1956               return d->getConstPointer();
1957             }
1958           else
1959             {
1960               std::ostringstream oss; oss << msg << "nb of components expected to be a multiple of " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
1961               throw INTERP_KERNEL::Exception(oss.str().c_str());
1962             }
1963         }
1964       else
1965         {
1966           if(throwIfNullPt)
1967             {
1968               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1969               throw INTERP_KERNEL::Exception(oss.str().c_str());
1970             }
1971           else
1972             { nbTuples=0; return 0; }
1973         }
1974     }
1975   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1976   if(SWIG_IsOK(status))
1977     {  
1978       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1979       sw=3;
1980       if(e)
1981         {
1982           if(e->getNumberOfCompo()==nbCompExpected)
1983             {
1984               nbTuples=1;
1985               return e->getConstPointer();
1986             }
1987           else
1988             {
1989               std::ostringstream oss; oss << msg << "nb of components expected to be " <<  nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
1990               throw INTERP_KERNEL::Exception(oss.str().c_str());
1991             }
1992         }
1993       else
1994         {
1995           if(throwIfNullPt)
1996             {
1997               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1998               throw INTERP_KERNEL::Exception(oss.str().c_str());
1999             }
2000           else
2001             { nbTuples=0; return 0; }
2002         }
2003     }
2004   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2005 }
2006
2007 /*!
2008  * if value int -> cpp val sw=1
2009  * if value double -> cpp val sw=1
2010  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2011  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2012  * if value list[int,double] -> cpp std::vector<double> sw=4
2013  * if value tuple[int,double] -> cpp std::vector<double> sw=4
2014  */
2015 static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& sw, double& val, std::vector<double>& f,
2016                                                           const char *msg, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
2017 {
2018   ParaMEDMEM::DataArrayDouble *d=0;
2019   ParaMEDMEM::DataArrayDoubleTuple *e=0;
2020   sw=-1;
2021   if(PyFloat_Check(value))
2022     {
2023       val=PyFloat_AS_DOUBLE(value);
2024       sw=1;
2025       nbTuples=1;
2026       return &val;
2027     }
2028   if(PyInt_Check(value))
2029     {
2030       val=(double)PyInt_AS_LONG(value);
2031       sw=1;
2032       nbTuples=1;
2033       return &val;
2034     }
2035   if(PyTuple_Check(value))
2036     {
2037       int size=PyTuple_Size(value);
2038       f.resize(size);
2039       for(int i=0;i<size;i++)
2040         {
2041           PyObject *o=PyTuple_GetItem(value,i);
2042           if(PyFloat_Check(o))
2043             f[i]=PyFloat_AS_DOUBLE(o);
2044           else if(PyInt_Check(o))
2045             f[i]=(double)PyInt_AS_LONG(o);
2046           else
2047             {
2048               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2049               throw INTERP_KERNEL::Exception(oss.str().c_str());
2050             }
2051         }
2052       sw=4;
2053       nbTuples=size;
2054       return &f[0];
2055     }
2056   if(PyList_Check(value))
2057     {
2058       int size=PyList_Size(value);
2059       f.resize(size);
2060       for(int i=0;i<size;i++)
2061         {
2062           PyObject *o=PyList_GetItem(value,i);
2063           if(PyFloat_Check(o))
2064             f[i]=PyFloat_AS_DOUBLE(o);
2065           else if(PyInt_Check(o))
2066             f[i]=(double)PyInt_AS_LONG(o);
2067           else
2068             {
2069               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2070               throw INTERP_KERNEL::Exception(oss.str().c_str());
2071             }
2072         }
2073       sw=4;
2074       nbTuples=size;
2075       return &f[0];
2076     }
2077   void *argp;
2078   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2079   if(SWIG_IsOK(status))
2080     {  
2081       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
2082       sw=2;
2083       if(d)
2084         {
2085           if(d->getNumberOfComponents()==1)
2086             {
2087               nbTuples=d->getNumberOfTuples();
2088               return d->getConstPointer();
2089             }
2090           else
2091             {
2092               std::ostringstream oss; oss << msg << "nb of components expected to be one, and input has " << d->getNumberOfComponents() << " components !";
2093               throw INTERP_KERNEL::Exception(oss.str().c_str());
2094             }
2095         }
2096       else
2097         {
2098           if(throwIfNullPt)
2099             {
2100               std::ostringstream oss; oss << msg << " null pointer not accepted!";
2101               throw INTERP_KERNEL::Exception(oss.str().c_str());
2102             }
2103           else
2104             { nbTuples=0; return 0; }
2105         }
2106     }
2107   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
2108   if(SWIG_IsOK(status))
2109     {  
2110       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
2111       sw=3;
2112       if(e)
2113         {
2114           nbTuples=e->getNumberOfCompo();
2115           return e->getConstPointer();
2116         }
2117       else
2118         {
2119           if(throwIfNullPt)
2120             {
2121               std::ostringstream oss; oss << msg << " null pointer not accepted!";
2122               throw INTERP_KERNEL::Exception(oss.str().c_str());
2123             }
2124           else
2125             { nbTuples=0; return 0; }
2126         }
2127     }
2128   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2129 }
2130
2131 /*!
2132  * if python int -> cpp int sw=1
2133  * if python list[int] -> cpp vector<int> sw=2
2134  * if python tuple[int] -> cpp vector<int> sw=2
2135  * if python DataArrayInt -> cpp DataArrayInt sw=3
2136  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
2137  *
2138  * switch between (int,vector<int>,DataArrayInt)
2139  */
2140 static const int *convertObjToPossibleCpp1_Safe(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector<int>& stdvecTyypp) throw(INTERP_KERNEL::Exception)
2141 {
2142   sw=-1;
2143   if(PyInt_Check(value))
2144     {
2145       iTyypp=(int)PyInt_AS_LONG(value);
2146       sw=1; sz=1;
2147       return &iTyypp;
2148     }
2149   if(PyTuple_Check(value))
2150     {
2151       int size=PyTuple_Size(value);
2152       stdvecTyypp.resize(size);
2153       for(int i=0;i<size;i++)
2154         {
2155           PyObject *o=PyTuple_GetItem(value,i);
2156           if(PyInt_Check(o))
2157             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2158           else
2159             {
2160               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
2161               throw INTERP_KERNEL::Exception(oss.str().c_str());
2162             }
2163         }
2164       sw=2; sz=size;
2165       return &stdvecTyypp[0];
2166     }
2167   if(PyList_Check(value))
2168     {
2169       int size=PyList_Size(value);
2170       stdvecTyypp.resize(size);
2171       for(int i=0;i<size;i++)
2172         {
2173           PyObject *o=PyList_GetItem(value,i);
2174           if(PyInt_Check(o))
2175             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2176           else
2177             {
2178               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
2179               throw INTERP_KERNEL::Exception(oss.str().c_str());
2180             }
2181         }
2182       sw=2; sz=size;
2183       return &stdvecTyypp[0];
2184     }
2185   void *argp;
2186   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2187   if(SWIG_IsOK(status))
2188     {
2189       ParaMEDMEM::DataArrayInt *daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
2190       if(daIntTyypp)
2191         {
2192           sw=3; sz=daIntTyypp->getNbOfElems();
2193           return daIntTyypp->begin();
2194         }
2195       else
2196         {
2197           sz=0;
2198           return 0;
2199         }
2200     }
2201   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
2202   if(SWIG_IsOK(status))
2203     {  
2204       ParaMEDMEM::DataArrayIntTuple *daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
2205       sw=4; sz=daIntTuple->getNumberOfCompo();
2206       return daIntTuple->getConstPointer();
2207     }
2208   throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
2209 }
2210
2211 static ParaMEDMEM::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg)
2212 {
2213   void *aBasePtrVS=0;
2214   int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArray,0|0);
2215   if(!SWIG_IsOK(status))
2216     {
2217       status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2218       if(!SWIG_IsOK(status))
2219         {
2220           status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2221           if(!SWIG_IsOK(status))
2222             {
2223               status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,0|0);
2224               if(!SWIG_IsOK(status))
2225                 {
2226                   status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,0|0);
2227                   std::ostringstream oss; oss << msg << " ! Accepted instances are DataArrayDouble, DataArrayInt, DataArrayAsciiChar, DataArrayByte !";
2228                   throw INTERP_KERNEL::Exception(oss.str().c_str());
2229                 }
2230             }
2231         }
2232     }
2233   return reinterpret_cast< ParaMEDMEM::DataArray * >(aBasePtrVS);
2234 }