Salome HOME
MEDMEM suppression
[modules/med.git] / src / MEDMEM_SWIG / my_typemap.i
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifdef WITH_NUMPY
24 %init %{
25   import_array();
26 %}
27 #endif
28
29 %{
30 #include <stdio.h>
31
32 #ifdef WITH_NUMPY
33 #include <numpy/arrayobject.h>
34 #endif
35 %}
36
37 #if defined(SWIGPYTHON)
38 %typemap(in) std::string * , const std::string * , const std::string * const {
39   /* typemap in for string * , const string * , const string * const */
40   /* Check if is a list */
41   if (PyList_Check($input)) {
42     int size = PyList_Size($input);
43     int i = 0;
44     $1 = new string[size];
45     for (i = 0; i < size; i++) {
46       PyObject *o = PyList_GetItem($input,i);
47       if (PyString_Check(o))
48         $1[i] = string(PyString_AsString(PyList_GetItem($input,i)));
49       else {
50         PyErr_SetString(PyExc_TypeError,"list must contain strings");
51         delete [] ($1);
52         return NULL;
53       }
54     }
55   }
56   else
57     {
58       PyErr_SetString(PyExc_TypeError,"not a list");
59       return NULL;
60     }
61 }
62 %typemap(freearg) string * , const string * , const string * const {
63    delete [] ($1);
64 }
65 #endif
66
67 #if defined(SWIGPYTHON)
68 %typemap(in) double * , const double * , const double * const
69 {
70   /* typemap in for double * , const double * , const double * const */
71   /* Check if $input is a list */
72   bool ok = false;
73   if (!ok && PyList_Check($input))
74   { 
75     int size = PyList_Size($input);
76     int i = 0; 
77     $1 = (double *) malloc(size*sizeof(double));
78     for (i = 0; i < size; i++) {
79       PyObject *o = PyList_GetItem($input,i);
80       if (PyFloat_Check(o))
81         $1[i] = PyFloat_AsDouble(PyList_GetItem($input,i));
82       else { 
83         PyErr_SetString(PyExc_TypeError,"list must contain floats");
84         free($1);
85         return NULL;
86       }
87     }
88     ok = true;
89   }
90 #ifdef WITH_NUMPY
91   /* Check if $input is a ndarray */
92   if ( !ok && PyArray_Check($input))
93   {
94     if ( !PyArray_ISFLOAT ($input))
95     {
96       PyErr_SetString(PyExc_TypeError,"ndarray must contain floats");
97       return NULL;
98     }
99   
100     npy_intp size = PyArray_SIZE($input);
101     double* workPtr = $1 = (double *) malloc(size*sizeof(double));
102
103     if ( PyArray_ISCONTIGUOUS( $input )) // the data is in a single C-style contiguous segment
104     {
105       const char * dataPtr = PyArray_BYTES( $input );
106       int step = PyArray_ITEMSIZE( $input );
107       while(size--)
108       {
109         *workPtr++ = *((const double*) dataPtr);
110         dataPtr += step;
111       }
112     }
113     else
114     {
115       PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew($input);
116       if ( !iter )
117       {
118         PyErr_SetString(PyExc_RuntimeError,"can't iterate over a ndarray");
119         free($1);
120         return NULL;
121       }
122       while (iter->index < iter->size)
123       {
124         *workPtr++ = *((const double*) iter->dataptr);
125         PyArray_ITER_NEXT(iter);
126       }
127     }
128     ok = true;
129   }
130 #endif
131   if ( !ok )
132   {
133     PyErr_SetString(PyExc_TypeError,"not a list nor a ndarray");
134     return NULL;
135   }
136 }
137 #endif
138
139 #if defined(SWIGPYTHON)
140 %typemap(in) int * , const int * , const int * const
141 {
142   /* typemap in for int * , const int * , const int * const */
143   /* Check if is a list */
144   bool ok = false;
145   if (PyList_Check($input))
146   { 
147     int size = PyList_Size($input);
148     int i = 0; 
149     $1 = (int *) malloc(size*sizeof(int));
150     for (i = 0; i < size; i++)
151     {
152       PyObject *o = PyList_GetItem($input,i);
153       if (PyInt_Check(o))
154         $1[i] = PyInt_AsLong(PyList_GetItem($input,i));
155       else { 
156         PyErr_SetString(PyExc_TypeError,"list must contain integers");
157         free($1);
158         return NULL;
159       }
160     }
161     ok = true;
162   } 
163 #ifdef WITH_NUMPY
164   /* Check if $input is a ndarray */
165   if ( !ok && PyArray_Check($input))
166   {
167     if ( !PyArray_ISINTEGER ($input))
168     {
169       PyErr_SetString(PyExc_TypeError,"ndarray must contain integers");
170       return NULL;
171     }
172   
173     npy_intp size = PyArray_SIZE($input);
174     int* workPtr = $1 = (int *) malloc(size*sizeof(int));
175
176     if ( PyArray_ISCONTIGUOUS( $input )) // the data is in a single C-style contiguous segment
177     {
178       const char * dataPtr = PyArray_BYTES( $input );
179       int step = PyArray_ITEMSIZE( $input );
180       while(size--)
181       {
182         *workPtr++ = *((const int*) dataPtr);
183         dataPtr += step;
184       }
185     }
186     else
187     {
188       PyArrayIterObject *iter = (PyArrayIterObject *)PyArray_IterNew($input);
189       if ( !iter )
190       {
191         PyErr_SetString(PyExc_RuntimeError,"can't iterate over a ndarray");
192         free($1);
193         return NULL;
194       }
195       while (iter->index < iter->size)
196       {
197         *workPtr++ = *((const int*) iter->dataptr);
198         PyArray_ITER_NEXT(iter);
199       }
200     }
201     ok = true;
202   }
203 #endif
204   if ( !ok )
205   { 
206     PyErr_SetString(PyExc_TypeError,"not a list nor a ndarray");
207     return NULL;
208   }
209
210 #endif
211
212 #if defined(SWIGPYTHON)
213 %typemap(in) medGeometryElement * , const  medGeometryElement * , const  medGeometryElement * const 
214 {
215   /* typemap in for medGeometryElement * , const  medGeometryElement * , const  medGeometryElement * const */
216   /* Check if is a list */
217   if (PyList_Check($input)) { 
218     int size = PyList_Size($input);
219     int i = 0; 
220     $1 = (medGeometryElement *) malloc(size*sizeof(medGeometryElement));
221     for (i = 0; i < size; i++) {
222       PyObject *o = PyList_GetItem($input,i);
223       if (PyInt_Check(o))
224         $1[i] = (medGeometryElement) PyInt_AsLong(PyList_GetItem($input,i));
225       else { 
226         PyErr_SetString(PyExc_TypeError,"list must contain integers");
227         free($1);
228         return NULL;
229       }
230     }
231   } 
232   else
233     { 
234       PyErr_SetString(PyExc_TypeError,"not a list");
235       return NULL;
236     }
237
238 #endif
239
240 #if defined(SWIGPYTHON)
241 %typemap(out) list<string> {
242   int i;
243   list<string>::iterator iL;
244
245   $result = PyList_New($1->size());
246   for (i=0, iL=$1->begin(); iL!=$1->end(); i++, iL++)
247     PyList_SetItem($result,i,PyString_FromString((*iL).c_str())); 
248 }
249 #endif
250
251 %typemap(freearg) int * , const int * , const int * const {
252   /* free the memory allocated in the typemap in for int * , const int * , const int * const */
253   free($1);
254 }
255
256 %typemap(freearg) double * , const double * , const double * const {
257   /* free the memory allocated in the typemap in for double * , const double * , const double * const */
258   free($1);
259 }
260
261 %typemap(freearg) medGeometryElement * , const medGeometryElement * , const medGeometryElement * const {
262   /* free the memory allocated in the typemap in for medGeometryElement * , const medGeometryElement * , const medGeometryElement * const */
263   free($1);
264 }