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_RESULT_HXX
20 #define PY2CPP_RESULT_HXX
23 #include "TypeConversions.hxx"
32 /*! class Result is used by pyResult function for syntax sugar purpose.
36 * py2cpp::PyFunction fn;
37 * fn.load("mymodule", "myfunction");
39 * py2cpp::pyResult(d, str) = fn(42);
40 * catch (const py2cpp::Exception& err)
41 * std::cerr << err.what();
46 * py2cpp::PyFunction fn;
47 * fn.load("mymodule", "myfunction");
48 * py2cpp::PyPtr fn_result = fn(42);
51 * std::tuple<double&, std::string&> cpp_result(d, str);
52 * ConversionCheck check = fromPyPtr(fn_result, cpp_result);
54 * std::cerr << err.getMessage();
57 * std::cerr << py2cpp::getLastPyError();
63 void operator=(PyObject * po)
66 throw ExecutionException(getLastPyError());
68 void operator=(const PyPtr& po){*this = po.get();}
76 Result(T& v):_data(v){}
77 void operator=(PyObject *po)
80 throw ExecutionException(getLastPyError());
81 ConversionCheck check;
82 check = fromPy(po, _data);
85 throw ConversionException(check.getMessage());
88 void operator=(const PyPtr& po){ *this = po.get();}
98 Result(Ts&...args):_data(args...){}
99 void operator=(PyObject *po)
102 throw ExecutionException(getLastPyError());
103 ConversionCheck check;
104 check = fromPy(po, _data);
106 throw ConversionException(check.getMessage());
108 void operator=(const PyPtr& po){ *this = po.get();};
110 std::tuple<Ts&...> _data;
113 template<class ...Ts>
114 Result<Ts...> pyResult(Ts&... args)
116 return Result<Ts...>(args...);
121 #endif //PY2CPP_RESULT_HXX