knowL: Knowledge Libraries
Loading...
Searching...
No Matches
knowCorePython.h
1#include <pybind11-qt/core.h>
2
3#include <knowCore/BigNumber.h>
4#include <knowCore/TypeDefinitions.h>
5#include <knowCore/ValueHash.h>
6#include <knowCore/ValueList.h>
7
8namespace knowCore::pybind11
9{
10 template<typename _T_>
11 _T_ handle_result(const cres_qresult<_T_>& _result)
12 {
13 if(_result.is_successful())
14 {
15 return _result.get_value();
16 }
17 else
18 {
19 throw std::runtime_error(_result.get_error().get_message().toStdString());
20 }
21 }
22 template<>
23 void handle_result(const cres_qresult<void>& _result)
24 {
25 if(not _result.is_successful())
26 {
27 throw std::runtime_error(_result.get_error().get_message().toStdString());
28 }
29 }
30
31 template<typename _T_>
32 void register_value_converter();
33 namespace detail
34 {
35 struct abstract_value_converter;
37 {
38 template<typename _T_>
39 friend void knowCore::pybind11::register_value_converter();
40 friend struct ::pybind11::detail::type_caster<knowCore::Value>;
41 private:
42 static void register_value_converter(abstract_value_converter* _converter);
44 static ::pybind11::handle to_python(const knowCore::Value& _variant,
45 ::pybind11::return_value_policy _policy,
46 ::pybind11::handle _parent);
47 static knowCore::Value to_value(::pybind11::handle _handle);
48 };
50 {
51 virtual bool can_cast(const knowCore::Value& _variant, bool _strict) const = 0;
52 virtual ::pybind11::handle to_python(const knowCore::Value& _variant,
53 ::pybind11::return_value_policy _policy,
54 ::pybind11::handle _parent) const
55 = 0;
56 virtual knowCore::Value to_value(::pybind11::handle _handle) const = 0;
57 };
58 inline ::pybind11::handle
59 value_converter_interface::to_python(const knowCore::Value& _variant,
60 ::pybind11::return_value_policy _policy,
61 ::pybind11::handle _parent)
62 {
63 for(const abstract_value_converter* conv : converters())
64 {
65 if(conv->can_cast(_variant, true))
66 {
67 return conv->to_python(_variant, _policy, _parent);
68 }
69 }
70 for(const abstract_value_converter* conv : converters())
71 {
72 if(conv->can_cast(_variant, false))
73 {
74 return conv->to_python(_variant, _policy, _parent);
75 }
76 }
77 throw ::pybind11::type_error("No converter for type: "
78 + QString(_variant.datatype()).toStdString()
79 + " from knowCore::Value.");
80 }
81 inline knowCore::Value value_converter_interface::to_value(::pybind11::handle _handle)
82 {
83 for(const abstract_value_converter* conv : converters())
84 {
85 try
86 {
87 return conv->to_value(_handle);
88 }
89 catch(const std::exception&)
90 {
91 }
92 }
93 throw ::pybind11::type_error("No converter for python value '"
94 + std::string(::pybind11::repr(_handle))
95 + "' to knowCore::Value.");
96 }
97
98 template<typename _T_>
100 {
101 bool can_cast(const knowCore::Value& _variant, bool _strict) const override
102 {
103 return (_strict and _variant.is<_T_>()) or (not _strict and _variant.canConvert<_T_>());
104 }
105 ::pybind11::handle to_python(const knowCore::Value& _value,
106 ::pybind11::return_value_policy _policy,
107 ::pybind11::handle _parent) const override
108 {
109 return ::pybind11::cast(_value.value<_T_>(), _policy, _parent).release();
110 }
111 knowCore::Value to_value(::pybind11::handle _handle) const override
112 {
113 return knowCore::Value::fromValue(::pybind11::cast<_T_>(_handle));
114 }
115 };
116
117 } // namespace detail
118 template<typename _T_>
119 void register_value_converter()
120 {
121 detail::value_converter_interface::register_value_converter(new detail::value_converter<_T_>());
122 }
123} // namespace knowCore::pybind11
124
125namespace pybind11::detail
126{
127 template<>
128 struct type_caster<knowCore::BigNumber>
129 {
130 public:
131 PYBIND11_TYPE_CASTER(knowCore::BigNumber, const_name("knowCore::BigNumber"));
132
136 bool load(handle src, bool)
137 {
138 if(int_::check_(src))
139 {
140 value = pybind11::cast<long>(src);
141 return true;
142 }
143 else if(float_::check_(src))
144 {
145 value = pybind11::cast<double>(src);
146 return true;
147 }
148 return false;
149 }
150
154 static handle cast(knowCore::BigNumber src, return_value_policy policy, handle parent)
155 {
156 if(src.isFloating())
157 {
158 return pybind11::cast(src.toDouble(), policy, parent);
159 }
160 else
161 {
162 return pybind11::cast(knowCore::pybind11::handle_result(src.toInt64()), policy, parent);
163 }
164 }
165 };
166 template<>
167 struct type_caster<knowCore::Value>
168 {
169 public:
170 PYBIND11_TYPE_CASTER(knowCore::Value, const_name("knowCore::Value"));
171
175 bool load(handle src, bool)
176 {
177 value = knowCore::pybind11::detail::value_converter_interface::to_value(src);
178 return true;
179 }
180
184 static handle cast(knowCore::Value src, return_value_policy policy, handle parent)
185 {
186 return knowCore::pybind11::detail::value_converter_interface::to_python(src, policy, parent);
187 }
188 };
189 template<>
190 struct type_caster<knowCore::ValueHash>
191 {
192 public:
193 PYBIND11_TYPE_CASTER(knowCore::ValueHash, const_name("knowCore::ValueHash"));
194
198 bool load(handle src, bool)
199 {
200 value = pybind11::cast<QHash<QString, knowCore::Value>>(src);
201 return true;
202 }
203
207 static handle cast(knowCore::ValueHash src, return_value_policy policy, handle parent)
208 {
209 return pybind11::cast(src.hash(), policy, parent).release();
210 }
211 };
212 template<>
213 struct type_caster<knowCore::ValueList>
214 {
215 public:
216 PYBIND11_TYPE_CASTER(knowCore::ValueList, const_name("knowCore::ValueList"));
217
221 bool load(handle src, bool)
222 {
223 value = pybind11::cast<QList<knowCore::Value>>(src);
224 return true;
225 }
226
230 static handle cast(knowCore::ValueList src, return_value_policy policy, handle parent)
231 {
232 return pybind11::cast(src.values(), policy, parent).release();
233 }
234 };
235 template<>
236 struct type_caster<cres_qresult<void>>
237 {
238 public:
239 PYBIND11_TYPE_CASTER(cres_qresult<void>, const_name("cres_qresult<void>"));
240
244 bool load(handle, bool) { return false; }
245
249 static handle cast(cres_qresult<void> src, return_value_policy, handle)
250 {
251 if(src.is_successful())
252 {
253 return pybind11::none();
254 }
255 else
256 {
257 throw std::runtime_error(src.get_error().get_message().toStdString());
258 }
259 }
260 };
261 template<typename _T_>
262 struct type_caster<cres_qresult<_T_>>
263 {
264 public:
265 using t_conv = make_caster<_T_>;
266 PYBIND11_TYPE_CASTER(cres_qresult<_T_>,
267 const_name("cres_qresult<") + t_conv::name + const_name(">"));
268
272 bool load(handle src, bool) { return false; }
273
277 static handle cast(cres_qresult<_T_> src, return_value_policy policy, handle parent)
278 {
279 return pybind11::cast(knowCore::pybind11::handle_result(src), policy, parent).release();
280 }
281 };
282
283} // namespace pybind11::detail
Definition Forward.h:14
Class that can contains large numeric value.
Definition BigNumber.h:51
bool isFloating() const
Definition BigNumber.cpp:248
cres_qresult< qint64 > toInt64(bool _truncate=false) const
Definition BigNumber.cpp:288
Definition ValueHash.h:13
cres_qresult< QHash< QString, _T_ > > hash() const
Definition ValueHash.h:252
Definition ValueList.h:8
Definition Value.h:21
Uri datatype() const
Definition Value.cpp:230
static Value fromValue(const _T_ &_value)
Definition Value.h:241
cres_qresult< _T_ > value(TypeCheckingMode _conversion=TypeCheckingMode::Safe) const
Definition Value.h:353
bool canConvert(TypeCheckingMode _conversion=TypeCheckingMode::Safe) const
Definition Value.h:364
Definition knowCorePython.h:100
bool load(handle src, bool)
Definition knowCorePython.h:272
static handle cast(cres_qresult< _T_ > src, return_value_policy policy, handle parent)
Definition knowCorePython.h:277
bool load(handle, bool)
Definition knowCorePython.h:244
static handle cast(cres_qresult< void > src, return_value_policy, handle)
Definition knowCorePython.h:249
bool load(handle src, bool)
Definition knowCorePython.h:136
static handle cast(knowCore::BigNumber src, return_value_policy policy, handle parent)
Definition knowCorePython.h:154
bool load(handle src, bool)
Definition knowCorePython.h:198
static handle cast(knowCore::ValueHash src, return_value_policy policy, handle parent)
Definition knowCorePython.h:207
static handle cast(knowCore::ValueList src, return_value_policy policy, handle parent)
Definition knowCorePython.h:230
bool load(handle src, bool)
Definition knowCorePython.h:221
static handle cast(knowCore::Value src, return_value_policy policy, handle parent)
Definition knowCorePython.h:184
bool load(handle src, bool)
Definition knowCorePython.h:175