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