Back to index

salome-med  6.5.0
MEDMEM_SWIG_Templates.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 //     static T Checker(PyObject *a);
00035 //     static T Traducer(PyObject *a);
00036   };
00037 
00038 template<>
00039   struct Binding<double> {
00040     //const char *TypeBuild="d";
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     //const char *TypeBuild="i";
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