Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef MEDMEM_SWIG_TEMPLATES_HXX_
00024 #define MEDMEM_SWIG_TEMPLATES_HXX_
00025
00026 #include "MEDMEM_Exception.hxx"
00027
00028 #ifdef WITH_NUMPY
00029 #include <numpy/arrayobject.h>
00030 #endif
00031
00032 template<class T>
00033 struct Binding {
00034
00035
00036 };
00037
00038 template<>
00039 struct Binding<double> {
00040
00041 static int Checker(PyObject *a) { return PyFloat_Check(a); }
00042 static double Traducer(PyObject *a) { return PyFloat_AsDouble(a); }
00043 static PyObject * Traducer( double value ) { return Py_BuildValue("d", value ); }
00044 static double Functor(PyObject *func, double value)
00045 { return Traducer( PyObject_CallFunction( func, (char *)"f", value )); }
00046 #ifdef WITH_NUMPY
00047 static NPY_TYPES numpy_type() { return NPY_DOUBLE; }
00048 #endif
00049 };
00050
00051 template<>
00052 struct Binding<int> {
00053
00054 static int Checker(PyObject *a) { return PyInt_Check(a); }
00055 static int Traducer(PyObject *a) { return (int) PyInt_AsLong(a); }
00056 static PyObject * Traducer( int value ) { return Py_BuildValue("i", value ); }
00057 static int Functor(PyObject *func, int value)
00058 { return Traducer( PyObject_CallFunction( func, (char *)"i", value )); }
00059 #ifdef WITH_NUMPY
00060 static NPY_TYPES numpy_type() { return NPY_INT; }
00061 #endif
00062 };
00063
00064 template<class T, class U>
00065 class MyFunction {
00066 public:
00067 static PyObject *_pyFunc;
00068 static int _nbOfComponent;
00069 static int _spaceDim;
00070 static void EvalPy2Cpp(const U *coord, T* outputValues)
00071 {
00072 int i=0,err;
00073 PyObject * tuple=PyTuple_New(_spaceDim);
00074 for(i=0;i<_spaceDim;i++)
00075 {
00076 err=PyTuple_SetItem(tuple,i,Binding<U>::Traducer(coord[i]));
00077 if (err != 0)
00078 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldDoubleFromAnalytic");
00079 }
00080 PyObject * function_ret = PyObject_CallObject(_pyFunc,tuple);
00081 if ( !function_ret )
00082 {
00083 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 << ")");
00084 }
00085 err = PyList_Check(function_ret);
00086 if (!err)
00087 {
00088 Py_DECREF(function_ret);
00089 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldIntFromAnalytic : the call to the user callable fonction has failed (its return value must be a list");
00090 }
00091 int size=PyList_Size(function_ret);
00092 if (size!=_nbOfComponent)
00093 {
00094 Py_DECREF(function_ret);
00095 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 << ")");
00096 }
00097 for(i=0;i<_nbOfComponent;i++)
00098 {
00099 PyObject * tmp=PyList_GetItem(function_ret,i);
00100 err = Binding<T>::Checker(tmp);
00101 if (!err)
00102 {
00103 Py_DECREF(function_ret);
00104 throw MEDMEM::MEDEXCEPTION("Internal Error in createFieldDoubleFromAnalytic : the call to the user callable fonction has failed (check its return value type)");
00105 }
00106 outputValues[i]=Binding<T>::Traducer(tmp);
00107 }
00108 }
00109 };
00110
00111 template<class T, class U>
00112 PyObject *MyFunction<T,U>::_pyFunc=0;
00113
00114 template<class T, class U>
00115 int MyFunction<T,U>::_nbOfComponent=0;
00116
00117 template<class T, class U>
00118 int MyFunction<T,U>::_spaceDim=0;
00119
00120 #endif