1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #ifndef MEDMEM_SWIG_TEMPLATES_HXX_
24 #define MEDMEM_SWIG_TEMPLATES_HXX_
26 #include "MEDMEM_Exception.hxx"
29 #include <numpy/arrayobject.h>
34 // static T Checker(PyObject *a);
35 // static T Traducer(PyObject *a);
39 struct Binding<double> {
40 //const char *TypeBuild="d";
41 static int Checker(PyObject *a) { return PyFloat_Check(a); }
42 static double Traducer(PyObject *a) { return PyFloat_AsDouble(a); }
43 static PyObject * Traducer( double value ) { return Py_BuildValue("d", value ); }
44 static double Functor(PyObject *func, double value)
45 { return Traducer( PyObject_CallFunction( func, (char *)"f", value )); }
47 static NPY_TYPES numpy_type() { return NPY_DOUBLE; }
53 //const char *TypeBuild="i";
54 static int Checker(PyObject *a) { return PyInt_Check(a); }
55 static int Traducer(PyObject *a) { return (int) PyInt_AsLong(a); }
56 static PyObject * Traducer( int value ) { return Py_BuildValue("i", value ); }
57 static int Functor(PyObject *func, int value)
58 { return Traducer( PyObject_CallFunction( func, (char *)"i", value )); }
60 static NPY_TYPES numpy_type() { return NPY_INT; }
64 template<class T, class U>
67 static PyObject *_pyFunc;
68 static int _nbOfComponent;
70 static void EvalPy2Cpp(const U *coord, T* outputValues)
73 PyObject * tuple=PyTuple_New(_spaceDim);
74 for(i=0;i<_spaceDim;i++)
76 err=PyTuple_SetItem(tuple,i,Binding<U>::Traducer(coord[i]));
78 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldDoubleFromAnalytic");
80 PyObject * function_ret = PyObject_CallObject(_pyFunc,tuple);
83 throw MEDMEM::MEDEXCEPTION(MEDMEM::STRING("Internal Error in createFieldIntFromAnalytic : the call to the user callable fonction has failed (possibly wrong nb of arguments that must be equal to space dimension = ")<< _spaceDim << ")");
85 err = PyList_Check(function_ret);
88 Py_DECREF(function_ret);
89 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldIntFromAnalytic : the call to the user callable fonction has failed (its return value must be a list");
91 int size=PyList_Size(function_ret);
92 if (size!=_nbOfComponent)
94 Py_DECREF(function_ret);
95 throw MEDMEM::MEDEXCEPTION(MEDMEM::STRING("Internal Error in createFieldIntFromAnalytic : the call to the user callable fonction has failed (its return value must be a list of size equal to _nbOfComponent = ") << _nbOfComponent << ")");
97 for(i=0;i<_nbOfComponent;i++)
99 PyObject * tmp=PyList_GetItem(function_ret,i);
100 err = Binding<T>::Checker(tmp);
103 Py_DECREF(function_ret);
104 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldDoubleFromAnalytic : the call to the user callable fonction has failed (check its return value type)");
106 outputValues[i]=Binding<T>::Traducer(tmp);
111 template<class T, class U>
112 PyObject *MyFunction<T,U>::_pyFunc=0;
114 template<class T, class U>
115 int MyFunction<T,U>::_nbOfComponent=0;
117 template<class T, class U>
118 int MyFunction<T,U>::_spaceDim=0;