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 #ifndef PY2CPP_RESULT_HXX
20 #define PY2CPP_RESULT_HXX
21 #include "py2cppExports.hxx"
24 #include "TypeConversions.hxx"
33 /*! class Result is used by pyResult function for syntax sugar purpose.
37 * py2cpp::PyFunction fn;
38 * fn.load("mymodule", "myfunction");
40 * py2cpp::pyResult(d, str) = fn(42);
41 * catch (const py2cpp::Exception& err)
42 * std::cerr << err.what();
47 * py2cpp::PyFunction fn;
48 * fn.load("mymodule", "myfunction");
49 * py2cpp::PyPtr fn_result = fn(42);
52 * std::tuple<double&, std::string&> cpp_result(d, str);
53 * ConversionCheck check = fromPyPtr(fn_result, cpp_result);
55 * std::cerr << err.getMessage();
58 * std::cerr << py2cpp::getLastPyError();
64 void operator=(PyObject * po)
67 throw ExecutionException(getLastPyError());
69 void operator=(const PyPtr& po){*this = po.get();}
77 Result(T& v):_data(v){}
78 void operator=(PyObject *po)
81 throw ExecutionException(getLastPyError());
82 ConversionCheck check;
83 check = fromPy(po, _data);
86 throw ConversionException(check.getMessage());
89 void operator=(const PyPtr& po){ *this = po.get();}
99 Result(Ts&...args):_data(args...){}
100 void operator=(PyObject *po)
103 throw ExecutionException(getLastPyError());
104 ConversionCheck check;
105 check = fromPy(po, _data);
107 throw ConversionException(check.getMessage());
109 void operator=(const PyPtr& po){ *this = po.get();};
111 std::tuple<Ts&...> _data;
114 template<class ...Ts>
115 Result<Ts...> pyResult(Ts&... args)
117 return Result<Ts...>(args...);
122 #endif //PY2CPP_RESULT_HXX