1 // Copyright (C) 2019 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 #ifndef PY2CPP_FUNCTIONCALLS_HXX
20 #define PY2CPP_FUNCTIONCALLS_HXX
23 #include "TypeConversions.hxx"
26 class PyFunction : public PyPtr
29 /*! Load a callable object from a python module.*/
30 bool load(const std::string& module, const std::string& function);
31 /*! Load a callable object member of a python object.*/
32 bool load(const PyPtr& obj, const std::string& function);
33 bool load(PyObject* obj, const std::string& function);
36 * The evaluation returns nullptr if the python function throws an exception.
37 * See PyObject_CallObject documentation.
38 * You can use getLastPyError in order to get the last python error.
40 template <class ...Ts>
41 PyPtr operator()(const Ts&... args)
43 PyObject * result = nullptr;
44 PyObject * myFunc = get();
45 if(myFunc && PyCallable_Check(myFunc))
47 std::tuple<const Ts&...> tupleArgs(args...);
48 PyPtr pyArgs(toPy(tupleArgs));
49 result = PyObject_CallObject(myFunc, pyArgs.get());
56 #endif //PY2CPP_FUNCTIONCALLS_HXX