Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM_SWIG / MEDMEM_SWIG_Templates.hxx
1 // Copyright (C) 2007-2012  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 #ifndef MEDMEM_SWIG_TEMPLATES_HXX_
24 #define MEDMEM_SWIG_TEMPLATES_HXX_
25
26 #include "MEDMEM_Exception.hxx"
27
28 #ifdef WITH_NUMPY
29 #include <numpy/arrayobject.h>
30 #endif
31
32 template<class T>
33   struct Binding {
34 //     static T Checker(PyObject *a);
35 //     static T Traducer(PyObject *a);
36   };
37
38 template<>
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 )); }
46 #ifdef WITH_NUMPY
47   static NPY_TYPES numpy_type() { return NPY_DOUBLE; }
48 #endif
49   };
50
51 template<>
52   struct Binding<int> {
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 )); }
59 #ifdef WITH_NUMPY
60   static NPY_TYPES numpy_type() { return NPY_INT; }
61 #endif
62   };
63
64 template<class T, class U>
65   class MyFunction {
66   public:
67     static PyObject *_pyFunc;
68     static int _nbOfComponent;
69     static int _spaceDim;
70     static void EvalPy2Cpp(const U *coord, T* outputValues)
71       {
72         int i=0,err;
73         PyObject * tuple=PyTuple_New(_spaceDim);
74           for(i=0;i<_spaceDim;i++)
75             {
76               err=PyTuple_SetItem(tuple,i,Binding<U>::Traducer(coord[i]));
77               if (err != 0)
78                 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldDoubleFromAnalytic");
79             }
80           PyObject * function_ret = PyObject_CallObject(_pyFunc,tuple);
81           if ( !function_ret )
82           {
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 << ")");
84           }
85           err = PyList_Check(function_ret);
86           if (!err)
87               {
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");
90               }
91           int size=PyList_Size(function_ret);
92           if (size!=_nbOfComponent)
93             {
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 << ")");
96             }
97           for(i=0;i<_nbOfComponent;i++)
98             {
99               PyObject * tmp=PyList_GetItem(function_ret,i);
100               err = Binding<T>::Checker(tmp);
101               if (!err)
102                   {
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)");
105                   }
106               outputValues[i]=Binding<T>::Traducer(tmp);
107             }
108       }
109   };
110
111 template<class T, class U>
112 PyObject *MyFunction<T,U>::_pyFunc=0;
113
114 template<class T, class U>
115 int MyFunction<T,U>::_nbOfComponent=0;
116
117 template<class T, class U>
118 int MyFunction<T,U>::_spaceDim=0;
119
120 #endif