Salome HOME
Copyright update 2020
[modules/med.git] / src / MEDCalculator / Swig / MEDCalculatorTypemaps.i
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include <limits>
22
23 #if PY_VERSION_HEX < 0x03050000
24 static char*
25 Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
26 {
27    return _Py_wchar2char(text, error_pos);
28 }
29 #endif
30
31 static PyObject* convertMEDCalculatorDBField(MEDCoupling::MEDCalculatorDBField *f, int owner)
32 {
33   PyObject *ret=0;
34   if(dynamic_cast<MEDCoupling::MEDCalculatorDBFieldReal *>(f))
35     ret=SWIG_NewPointerObj((void*)f,SWIGTYPE_p_MEDCoupling__MEDCalculatorDBFieldReal,owner);
36   if(dynamic_cast<MEDCoupling::MEDCalculatorDBFieldCst *>(f))
37     ret=SWIG_NewPointerObj((void*)f,SWIGTYPE_p_MEDCoupling__MEDCalculatorDBFieldCst,owner);
38   return ret;
39 }
40
41 void convertPyObjToRS(PyObject *o, MEDCoupling::MEDCalculatorDBRangeSelection& rs)
42 {
43   if(PyLong_Check(o))
44     {
45       int val=(int)PyLong_AS_LONG(o);
46       rs=val;
47       return ;
48     }
49   if(PyString_Check(o))
50     {
51       char *s=Py_EncodeLocale(PyUnicode_AS_UNICODE(o), NULL);
52       rs=s;
53       return ;
54     }
55   PyErr_SetString(PyExc_TypeError,"Range selection must be an integer or a string !");
56 }
57
58 void convertPyObjToRS2(PyObject *o, MEDCoupling::MEDCalculatorDBRangeSelection& rs, const char *msg)
59 {
60   if(PyLong_Check(o))
61     {
62       int val=(int)PyLong_AS_LONG(o);
63       rs=val;
64       return ;
65     }
66   if(!PySlice_Check(o))
67     {
68       std::ostringstream oss;
69       oss << "Expecting a slice or an integer for " << msg << " !";
70       throw INTERP_KERNEL::Exception(oss.str().c_str());
71     }
72   Py_ssize_t strt,stp,step;
73   PySlice_GetIndices(o,std::numeric_limits<int>::max(),&strt,&stp,&step);
74   rs.setPyStart(strt);
75   rs.setPyEnd(stp);
76 }