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
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 "standart" DataArray deallocator is called.
41 void numarrdeal(void *pt, void *wron)
43 void **wronc=(void **)wron;
44 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
45 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
49 PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
50 objC->flags|=MED_NUMPY_OWNDATA;
51 Py_XDECREF(weakRefOnOwner);
56 typedef void (*MyDeallocator)(void *,void *);
57 MyDeallocator deall=(MyDeallocator)wronc[1];
59 Py_XDECREF(weakRefOnOwner);
65 template<class MCData>
66 struct PyCallBackDataArraySt {
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;
78 static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
80 static PyObject *callbackmcdataarraychar___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
82 PyCallBackDataArrayChar *self = (PyCallBackDataArrayChar *) ( type->tp_alloc(type, 0) );
83 return (PyObject *)self;
86 static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
88 PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
89 return (PyObject *)self;
92 static PyObject *callbackmcdataarrayfloat___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
94 PyCallBackDataArrayFloat *self = (PyCallBackDataArrayFloat *) ( type->tp_alloc(type, 0) );
95 return (PyObject *)self;
98 static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
100 PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
101 return (PyObject *)self;
104 static void callbackmcdataarray_dealloc(PyObject *self)
106 Py_TYPE(self)->tp_free(self);
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)
116 MEDCoupling::MemArray<char>& mma=self->_pt_mc->accessToMemArray();
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)
129 MEDCoupling::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
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)
142 MEDCoupling::MemArray<float>& mma=self->_pt_mc->accessToMemArray();
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)
155 MEDCoupling::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
163 PyTypeObject PyCallBackDataArrayChar_RefType = {
164 PyVarObject_HEAD_INIT(&PyType_Type, 0)
165 "callbackmcdataarraychar",
166 sizeof(PyCallBackDataArrayChar),
168 callbackmcdataarray_dealloc, /*tp_dealloc*/
175 0, /*tp_as_sequence*/
178 (ternaryfunc)callbackmcdataarraychar_call, /*tp_call*/
183 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
187 0, /*tp_richcompare*/
188 0, /*tp_weaklistoffset*/
199 callbackmcdataarray___init__, /*tp_init*/
200 PyType_GenericAlloc, /*tp_alloc*/
201 callbackmcdataarraychar___new__, /*tp_new*/
202 PyObject_GC_Del, /*tp_free*/
206 PyTypeObject PyCallBackDataArrayInt_RefType = {
207 PyVarObject_HEAD_INIT(&PyType_Type, 0)
208 "callbackmcdataarrayint",
209 sizeof(PyCallBackDataArrayInt),
211 callbackmcdataarray_dealloc, /*tp_dealloc*/
218 0, /*tp_as_sequence*/
221 (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/
226 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
230 0, /*tp_richcompare*/
231 0, /*tp_weaklistoffset*/
242 callbackmcdataarray___init__, /*tp_init*/
243 PyType_GenericAlloc, /*tp_alloc*/
244 callbackmcdataarrayint___new__, /*tp_new*/
245 PyObject_GC_Del, /*tp_free*/
248 PyTypeObject PyCallBackDataArrayFloat_RefType = {
249 PyVarObject_HEAD_INIT(&PyType_Type, 0)
250 "callbackmcdataarraydouble",
251 sizeof(PyCallBackDataArrayFloat),
253 callbackmcdataarray_dealloc, /*tp_dealloc*/
260 0, /*tp_as_sequence*/
263 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
272 0, /*tp_richcompare*/
273 0, /*tp_weaklistoffset*/
284 callbackmcdataarray___init__, /*tp_init*/
285 PyType_GenericAlloc, /*tp_alloc*/
286 callbackmcdataarrayfloat___new__, /*tp_new*/
287 PyObject_GC_Del, /*tp_free*/
290 PyTypeObject PyCallBackDataArrayDouble_RefType = {
291 PyVarObject_HEAD_INIT(&PyType_Type, 0)
292 "callbackmcdataarraydouble",
293 sizeof(PyCallBackDataArrayDouble),
295 callbackmcdataarray_dealloc, /*tp_dealloc*/
302 0, /*tp_as_sequence*/
305 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
310 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
314 0, /*tp_richcompare*/
315 0, /*tp_weaklistoffset*/
326 callbackmcdataarray___init__, /*tp_init*/
327 PyType_GenericAlloc, /*tp_alloc*/
328 callbackmcdataarraydouble___new__, /*tp_new*/
329 PyObject_GC_Del, /*tp_free*/
339 struct NPYTraits<double>
341 static const int NPYObjectType=NPY_DOUBLE;
342 static PyTypeObject *NPYFunc;
343 static PyObject *Array_SWIGTYPE;
347 struct NPYTraits<float>
349 static const int NPYObjectType=NPY_FLOAT;
350 static PyTypeObject *NPYFunc;
351 static PyObject *Array_SWIGTYPE;