1 // Copyright (C) 2019-2023 EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "TypeConversions.hxx"
23 ConversionCheck fromPy(PyObject * po, int& result)
25 ConversionCheck check;
26 if(po && PyLong_Check(po))
27 result = PyLong_AsLong(po);
29 check.addError("int", po);
33 PyObject * toPy(int val)
35 return PyLong_FromLong(val);
38 ConversionCheck fromPy(PyObject * po, unsigned int& result)
40 ConversionCheck check;
41 if(po && PyLong_Check(po))
42 result = PyLong_AsUnsignedLong(po);
44 check.addError("int", po);
48 PyObject * toPy(unsigned int val)
50 return PyLong_FromUnsignedLong(val);
53 ConversionCheck fromPy(PyObject * po, bool& result)
55 ConversionCheck check;
56 if(po && PyBool_Check(po))
57 result = (Py_True == po);
59 check.addError("bool", po);
63 PyObject * toPy(bool val)
65 return PyBool_FromLong(val);
68 ConversionCheck fromPy(PyObject * po, double& result)
70 ConversionCheck check;
71 if(po && PyFloat_Check(po))
72 result = PyFloat_AsDouble(po);
73 else if(po && PyLong_Check(po))
75 int v = PyLong_AsLong(po);
79 check.addError("double", po);
83 PyObject * toPy(double val)
85 return PyFloat_FromDouble(val);
88 ConversionCheck fromPy(PyObject * po, std::string& result)
90 ConversionCheck check;
91 if(po && PyUnicode_Check(po))
93 const char* charstr = PyUnicode_AsUTF8(po);
94 if(charstr != nullptr)
100 check.addError("std::string", po);
104 PyObject * toPy(const std::string& val)
106 return PyUnicode_FromString(val.c_str());
109 PyObject * toPy(const char* val)
111 return PyUnicode_FromString(val);
114 ConversionCheck fromPy( PyObject *po, PyObject *& result)
118 return ConversionCheck();
121 PyObject * toPy(PyObject * obj)
127 ConversionCheck fromPy( PyObject *po, PyPtr& result)
131 return ConversionCheck();
134 PyObject * toPy(const PyPtr& obj)
136 PyObject * result = obj.get();
142 PyPtr fromPy<PyPtr>( PyObject *po)