Salome HOME
typo-fix by Kunda
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTraits.hxx
1 // Copyright (C) 2007-2017  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 // Author : Anthony Geay (EDF R&D)
20
21 #ifndef __MEDCOUPLINGDATAARRAYTRAITS_HXX__
22 #define __MEDCOUPLINGDATAARRAYTRAITS_HXX__
23
24 #include "MEDCouplingMemArray.hxx"
25
26 #include <Python.h>
27
28 #ifdef WITH_NUMPY
29 #include <numpy/arrayobject.h>
30 #if NPY_API_VERSION <= 0x00000006
31 #  define MED_NUMPY_OWNDATA NPY_OWNDATA
32 #else
33 #  define MED_NUMPY_OWNDATA NPY_ARRAY_OWNDATA
34 #endif
35 #endif
36
37 #ifdef WITH_NUMPY
38 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
39 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
40 // if no more alive the "standard" DataArray deallocator is called.
41 void numarrdeal(void *pt, void *wron)
42 {
43   void **wronc=(void **)wron;
44   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
45   PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
46   if(obj!=Py_None)
47     {
48       Py_XINCREF(obj);
49       PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
50       objC->flags|=MED_NUMPY_OWNDATA;
51       Py_XDECREF(weakRefOnOwner);
52       Py_XDECREF(obj);
53     }
54   else
55     {
56       typedef void (*MyDeallocator)(void *,void *);
57       MyDeallocator deall=(MyDeallocator)wronc[1];
58       deall(pt,NULL);
59       Py_XDECREF(weakRefOnOwner);
60     }
61   delete [] wronc;
62 }
63 #endif
64
65 template<class MCData>
66 struct PyCallBackDataArraySt {
67     PyObject_HEAD
68     MCData *_pt_mc;
69 };
70
71 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayByte> PyCallBackDataArrayChar;
72 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayInt> PyCallBackDataArrayInt;
73 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayFloat> PyCallBackDataArrayFloat;
74 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayDouble> PyCallBackDataArrayDouble;
75
76 extern "C"
77 {
78   static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
79   
80   static PyObject *callbackmcdataarraychar___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
81   {
82     PyCallBackDataArrayChar *self = (PyCallBackDataArrayChar *) ( type->tp_alloc(type, 0) );
83     return (PyObject *)self;
84   }
85
86   static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
87   {
88     PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
89     return (PyObject *)self;
90   }
91   
92   static PyObject *callbackmcdataarrayfloat___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
93   {
94     PyCallBackDataArrayFloat *self = (PyCallBackDataArrayFloat *) ( type->tp_alloc(type, 0) );
95     return (PyObject *)self;
96   }
97   
98   static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
99   {
100     PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
101     return (PyObject *)self;
102   }
103   
104   static void callbackmcdataarray_dealloc(PyObject *self)
105   {
106     Py_TYPE(self)->tp_free(self);
107   }
108
109   
110   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
111   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
112   static PyObject *callbackmcdataarraychar_call(PyCallBackDataArrayChar *self, PyObject *args, PyObject *kw)
113   {
114     if(self->_pt_mc)
115       {
116         MEDCoupling::MemArray<char>& mma=self->_pt_mc->accessToMemArray();
117         mma.destroy();
118       }
119     Py_XINCREF(Py_None);
120     return Py_None;
121   }
122
123   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
124   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
125   static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
126   {
127     if(self->_pt_mc)
128       {
129         MEDCoupling::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
130         mma.destroy();
131       }
132     Py_XINCREF(Py_None);
133     return Py_None;
134   }
135
136   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
137   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
138   static PyObject *callbackmcdataarrayfloat_call(PyCallBackDataArrayFloat *self, PyObject *args, PyObject *kw)
139   {
140     if(self->_pt_mc)
141       {
142         MEDCoupling::MemArray<float>& mma=self->_pt_mc->accessToMemArray();
143         mma.destroy();
144       }
145     Py_XINCREF(Py_None);
146     return Py_None;
147   }
148   
149   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
150   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
151   static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
152   {
153     if(self->_pt_mc)
154       {
155         MEDCoupling::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
156         mma.destroy();
157       }
158     Py_XINCREF(Py_None);
159     return Py_None;
160   }
161 }
162
163 PyTypeObject PyCallBackDataArrayChar_RefType = {
164   PyVarObject_HEAD_INIT(&PyType_Type, 0)
165   "callbackmcdataarraychar",
166   sizeof(PyCallBackDataArrayChar),
167   0,
168   callbackmcdataarray_dealloc,            /*tp_dealloc*/
169   0,                          /*tp_print*/
170   0,                          /*tp_getattr*/
171   0,                          /*tp_setattr*/
172   0,                          /*tp_compare*/
173   0,                          /*tp_repr*/
174   0,                          /*tp_as_number*/
175   0,                          /*tp_as_sequence*/
176   0,                          /*tp_as_mapping*/
177   0,                          /*tp_hash*/
178   (ternaryfunc)callbackmcdataarraychar_call,  /*tp_call*/
179   0,                          /*tp_str*/
180   0,                          /*tp_getattro*/
181   0,                          /*tp_setattro*/
182   0,                          /*tp_as_buffer*/
183   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
184   0,                          /*tp_doc*/
185   0,                          /*tp_traverse*/
186   0,                          /*tp_clear*/
187   0,                          /*tp_richcompare*/
188   0,                          /*tp_weaklistoffset*/
189   0,                          /*tp_iter*/
190   0,                          /*tp_iternext*/
191   0,                          /*tp_methods*/
192   0,                          /*tp_members*/
193   0,                          /*tp_getset*/
194   0,                          /*tp_base*/
195   0,                          /*tp_dict*/
196   0,                          /*tp_descr_get*/
197   0,                          /*tp_descr_set*/
198   0,                          /*tp_dictoffset*/
199   callbackmcdataarray___init__,           /*tp_init*/
200   PyType_GenericAlloc,        /*tp_alloc*/
201   callbackmcdataarraychar___new__,            /*tp_new*/
202   PyObject_GC_Del,            /*tp_free*/
203 };
204
205
206 PyTypeObject PyCallBackDataArrayInt_RefType = {
207   PyVarObject_HEAD_INIT(&PyType_Type, 0)
208   "callbackmcdataarrayint",
209   sizeof(PyCallBackDataArrayInt),
210   0,
211   callbackmcdataarray_dealloc,            /*tp_dealloc*/
212   0,                          /*tp_print*/
213   0,                          /*tp_getattr*/
214   0,                          /*tp_setattr*/
215   0,                          /*tp_compare*/
216   0,                          /*tp_repr*/
217   0,                          /*tp_as_number*/
218   0,                          /*tp_as_sequence*/
219   0,                          /*tp_as_mapping*/
220   0,                          /*tp_hash*/
221   (ternaryfunc)callbackmcdataarrayint_call,  /*tp_call*/
222   0,                          /*tp_str*/
223   0,                          /*tp_getattro*/
224   0,                          /*tp_setattro*/
225   0,                          /*tp_as_buffer*/
226   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
227   0,                          /*tp_doc*/
228   0,                          /*tp_traverse*/
229   0,                          /*tp_clear*/
230   0,                          /*tp_richcompare*/
231   0,                          /*tp_weaklistoffset*/
232   0,                          /*tp_iter*/
233   0,                          /*tp_iternext*/
234   0,                          /*tp_methods*/
235   0,                          /*tp_members*/
236   0,                          /*tp_getset*/
237   0,                          /*tp_base*/
238   0,                          /*tp_dict*/
239   0,                          /*tp_descr_get*/
240   0,                          /*tp_descr_set*/
241   0,                          /*tp_dictoffset*/
242   callbackmcdataarray___init__,           /*tp_init*/
243   PyType_GenericAlloc,        /*tp_alloc*/
244   callbackmcdataarrayint___new__,            /*tp_new*/
245   PyObject_GC_Del,            /*tp_free*/
246 };
247
248 PyTypeObject PyCallBackDataArrayFloat_RefType = {
249   PyVarObject_HEAD_INIT(&PyType_Type, 0)
250   "callbackmcdataarraydouble",
251   sizeof(PyCallBackDataArrayFloat),
252   0,
253   callbackmcdataarray_dealloc,            /*tp_dealloc*/
254   0,                          /*tp_print*/
255   0,                          /*tp_getattr*/
256   0,                          /*tp_setattr*/
257   0,                          /*tp_compare*/
258   0,                          /*tp_repr*/
259   0,                          /*tp_as_number*/
260   0,                          /*tp_as_sequence*/
261   0,                          /*tp_as_mapping*/
262   0,                          /*tp_hash*/
263   (ternaryfunc)callbackmcdataarraydouble_call,  /*tp_call*/
264   0,                          /*tp_str*/
265   0,                          /*tp_getattro*/
266   0,                          /*tp_setattro*/
267   0,                          /*tp_as_buffer*/
268   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
269   0,                          /*tp_doc*/
270   0,                          /*tp_traverse*/
271   0,                          /*tp_clear*/
272   0,                          /*tp_richcompare*/
273   0,                          /*tp_weaklistoffset*/
274   0,                          /*tp_iter*/
275   0,                          /*tp_iternext*/
276   0,                          /*tp_methods*/
277   0,                          /*tp_members*/
278   0,                          /*tp_getset*/
279   0,                          /*tp_base*/
280   0,                          /*tp_dict*/
281   0,                          /*tp_descr_get*/
282   0,                          /*tp_descr_set*/
283   0,                          /*tp_dictoffset*/
284   callbackmcdataarray___init__,           /*tp_init*/
285   PyType_GenericAlloc,        /*tp_alloc*/
286   callbackmcdataarrayfloat___new__,            /*tp_new*/
287   PyObject_GC_Del,            /*tp_free*/
288 };
289
290 PyTypeObject PyCallBackDataArrayDouble_RefType = {
291   PyVarObject_HEAD_INIT(&PyType_Type, 0)
292   "callbackmcdataarraydouble",
293   sizeof(PyCallBackDataArrayDouble),
294   0,
295   callbackmcdataarray_dealloc,            /*tp_dealloc*/
296   0,                          /*tp_print*/
297   0,                          /*tp_getattr*/
298   0,                          /*tp_setattr*/
299   0,                          /*tp_compare*/
300   0,                          /*tp_repr*/
301   0,                          /*tp_as_number*/
302   0,                          /*tp_as_sequence*/
303   0,                          /*tp_as_mapping*/
304   0,                          /*tp_hash*/
305   (ternaryfunc)callbackmcdataarraydouble_call,  /*tp_call*/
306   0,                          /*tp_str*/
307   0,                          /*tp_getattro*/
308   0,                          /*tp_setattro*/
309   0,                          /*tp_as_buffer*/
310   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
311   0,                          /*tp_doc*/
312   0,                          /*tp_traverse*/
313   0,                          /*tp_clear*/
314   0,                          /*tp_richcompare*/
315   0,                          /*tp_weaklistoffset*/
316   0,                          /*tp_iter*/
317   0,                          /*tp_iternext*/
318   0,                          /*tp_methods*/
319   0,                          /*tp_members*/
320   0,                          /*tp_getset*/
321   0,                          /*tp_base*/
322   0,                          /*tp_dict*/
323   0,                          /*tp_descr_get*/
324   0,                          /*tp_descr_set*/
325   0,                          /*tp_dictoffset*/
326   callbackmcdataarray___init__,           /*tp_init*/
327   PyType_GenericAlloc,        /*tp_alloc*/
328   callbackmcdataarraydouble___new__,            /*tp_new*/
329   PyObject_GC_Del,            /*tp_free*/
330 };
331
332 #ifdef WITH_NUMPY
333 template<class T>
334 struct NPYTraits
335 {
336 };
337
338 template<>
339 struct NPYTraits<double>
340 {
341   static const int NPYObjectType=NPY_DOUBLE;
342   static PyTypeObject *NPYFunc;
343   static PyObject *Array_SWIGTYPE;
344 };
345
346 template<>
347 struct NPYTraits<float>
348 {
349   static const int NPYObjectType=NPY_FLOAT;
350   static PyTypeObject *NPYFunc;
351   static PyObject *Array_SWIGTYPE;
352 };
353 #endif
354
355 #endif