1 // Copyright (C) 2007-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (EDF R&D)
21 #ifndef __MEDCOUPLINGDATAARRAYTRAITS_HXX__
22 #define __MEDCOUPLINGDATAARRAYTRAITS_HXX__
24 #include "MEDCouplingMemArray.hxx"
29 #include <numpy/arrayobject.h>
30 #if NPY_API_VERSION <= 0x00000006
31 # define MED_NUMPY_OWNDATA NPY_OWNDATA
33 # define MED_NUMPY_OWNDATA NPY_ARRAY_OWNDATA
37 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
38 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
39 // if no more alive the "standart" DataArray deallocator is called.
40 void numarrdeal(void *pt, void *wron)
42 void **wronc=(void **)wron;
43 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
44 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
48 PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
49 objC->flags|=MED_NUMPY_OWNDATA;
50 Py_XDECREF(weakRefOnOwner);
55 typedef void (*MyDeallocator)(void *,void *);
56 MyDeallocator deall=(MyDeallocator)wronc[1];
58 Py_XDECREF(weakRefOnOwner);
63 template<class MCData>
64 struct PyCallBackDataArraySt {
69 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayByte> PyCallBackDataArrayChar;
70 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayInt> PyCallBackDataArrayInt;
71 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayFloat> PyCallBackDataArrayFloat;
72 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayDouble> PyCallBackDataArrayDouble;
76 static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
78 static PyObject *callbackmcdataarraychar___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
80 PyCallBackDataArrayChar *self = (PyCallBackDataArrayChar *) ( type->tp_alloc(type, 0) );
81 return (PyObject *)self;
84 static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
86 PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
87 return (PyObject *)self;
90 static PyObject *callbackmcdataarrayfloat___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
92 PyCallBackDataArrayFloat *self = (PyCallBackDataArrayFloat *) ( type->tp_alloc(type, 0) );
93 return (PyObject *)self;
96 static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
98 PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
99 return (PyObject *)self;
102 static void callbackmcdataarray_dealloc(PyObject *self)
104 Py_TYPE(self)->tp_free(self);
108 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
109 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
110 static PyObject *callbackmcdataarraychar_call(PyCallBackDataArrayChar *self, PyObject *args, PyObject *kw)
114 MEDCoupling::MemArray<char>& mma=self->_pt_mc->accessToMemArray();
121 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
122 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
123 static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
127 MEDCoupling::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
134 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
135 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
136 static PyObject *callbackmcdataarrayfloat_call(PyCallBackDataArrayFloat *self, PyObject *args, PyObject *kw)
140 MEDCoupling::MemArray<float>& mma=self->_pt_mc->accessToMemArray();
147 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
148 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
149 static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
153 MEDCoupling::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
161 PyTypeObject PyCallBackDataArrayChar_RefType = {
162 PyVarObject_HEAD_INIT(&PyType_Type, 0)
163 "callbackmcdataarraychar",
164 sizeof(PyCallBackDataArrayChar),
166 callbackmcdataarray_dealloc, /*tp_dealloc*/
173 0, /*tp_as_sequence*/
176 (ternaryfunc)callbackmcdataarraychar_call, /*tp_call*/
181 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
185 0, /*tp_richcompare*/
186 0, /*tp_weaklistoffset*/
197 callbackmcdataarray___init__, /*tp_init*/
198 PyType_GenericAlloc, /*tp_alloc*/
199 callbackmcdataarraychar___new__, /*tp_new*/
200 PyObject_GC_Del, /*tp_free*/
204 PyTypeObject PyCallBackDataArrayInt_RefType = {
205 PyVarObject_HEAD_INIT(&PyType_Type, 0)
206 "callbackmcdataarrayint",
207 sizeof(PyCallBackDataArrayInt),
209 callbackmcdataarray_dealloc, /*tp_dealloc*/
216 0, /*tp_as_sequence*/
219 (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/
224 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
228 0, /*tp_richcompare*/
229 0, /*tp_weaklistoffset*/
240 callbackmcdataarray___init__, /*tp_init*/
241 PyType_GenericAlloc, /*tp_alloc*/
242 callbackmcdataarrayint___new__, /*tp_new*/
243 PyObject_GC_Del, /*tp_free*/
246 PyTypeObject PyCallBackDataArrayFloat_RefType = {
247 PyVarObject_HEAD_INIT(&PyType_Type, 0)
248 "callbackmcdataarraydouble",
249 sizeof(PyCallBackDataArrayFloat),
251 callbackmcdataarray_dealloc, /*tp_dealloc*/
258 0, /*tp_as_sequence*/
261 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
266 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
270 0, /*tp_richcompare*/
271 0, /*tp_weaklistoffset*/
282 callbackmcdataarray___init__, /*tp_init*/
283 PyType_GenericAlloc, /*tp_alloc*/
284 callbackmcdataarrayfloat___new__, /*tp_new*/
285 PyObject_GC_Del, /*tp_free*/
288 PyTypeObject PyCallBackDataArrayDouble_RefType = {
289 PyVarObject_HEAD_INIT(&PyType_Type, 0)
290 "callbackmcdataarraydouble",
291 sizeof(PyCallBackDataArrayDouble),
293 callbackmcdataarray_dealloc, /*tp_dealloc*/
300 0, /*tp_as_sequence*/
303 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
308 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
312 0, /*tp_richcompare*/
313 0, /*tp_weaklistoffset*/
324 callbackmcdataarray___init__, /*tp_init*/
325 PyType_GenericAlloc, /*tp_alloc*/
326 callbackmcdataarraydouble___new__, /*tp_new*/
327 PyObject_GC_Del, /*tp_free*/
336 struct NPYTraits<double>
338 static const int NPYObjectType=NPY_DOUBLE;
339 static PyTypeObject *NPYFunc;
340 static PyObject *Array_SWIGTYPE;
344 struct NPYTraits<float>
346 static const int NPYObjectType=NPY_FLOAT;
347 static PyTypeObject *NPYFunc;
348 static PyObject *Array_SWIGTYPE;