]> SALOME platform Git repositories - tools/py2cpp.git/blob - src/Result.hxx
Salome HOME
37c9535026132347b8f1f89482ca6563a00bfa15
[tools/py2cpp.git] / src / Result.hxx
1 // Copyright (C) 2019-2023 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 #ifndef PY2CPP_RESULT_HXX
20 #define PY2CPP_RESULT_HXX
21 #include "py2cppExports.hxx"
22 #include <Python.h>
23 #include <tuple>
24 #include "TypeConversions.hxx"
25 #include "Errors.hxx"
26
27 namespace py2cpp
28 {
29
30 template<class ...Ts>
31 class Result;
32
33 /*! class Result is used by pyResult function for syntax sugar purpose.
34  * You can write this:
35  *   double d;
36  *   std::string str;
37  *   py2cpp::PyFunction fn;
38  *   fn.load("mymodule", "myfunction");
39  *   try
40  *     py2cpp::pyResult(d, str) = fn(42);
41  *   catch (const py2cpp::Exception& err)
42  *     std::cerr << err.what();
43  *
44  * Instead of that:
45  *   double d;
46  *   std::string str;
47  *   py2cpp::PyFunction fn;
48  *   fn.load("mymodule", "myfunction");
49  *   py2cpp::PyPtr fn_result = fn(42);
50  *   if(fn_result)
51  *   {
52  *     std::tuple<double&, std::string&> cpp_result(d, str);
53  *     ConversionCheck check = fromPyPtr(fn_result, cpp_result);
54  *     if(!check)
55  *       std::cerr << err.getMessage();
56  *   }
57  *   else
58  *     std::cerr << py2cpp::getLastPyError();
59  **/
60 template<>
61 class Result<>
62 {
63 public:
64   void operator=(PyObject * po)
65   {
66     if(!po)
67       throw ExecutionException(getLastPyError());
68   }
69   void operator=(const PyPtr& po){*this = po.get();}
70 };
71
72 template<class T>
73 class Result<T>
74 {
75 public:
76   Result() = delete;
77   Result(T& v):_data(v){}
78   void operator=(PyObject *po)
79   {
80     if(!po)
81       throw ExecutionException(getLastPyError());
82     ConversionCheck check;
83     check = fromPy(po, _data);
84     if(!check)
85     {
86       throw ConversionException(check.getMessage());
87     }
88   }
89   void operator=(const PyPtr& po){ *this = po.get();}
90 private:
91   T& _data;
92 };
93
94 template<class ...Ts>
95 class Result
96 {
97 public:
98   Result() = delete;
99   Result(Ts&...args):_data(args...){}
100   void operator=(PyObject *po)
101   {
102     if(!po)
103       throw ExecutionException(getLastPyError());
104     ConversionCheck check;
105     check = fromPy(po, _data);
106     if(!check)
107       throw ConversionException(check.getMessage());
108   }
109   void operator=(const PyPtr& po){ *this = po.get();};
110 private:
111   std::tuple<Ts&...> _data;
112 };
113
114 template<class ...Ts>
115 Result<Ts...> pyResult(Ts&... args)
116 {
117   return Result<Ts...>(args...);
118 }
119
120 }
121
122 #endif //PY2CPP_RESULT_HXX