1#include <pybind11-qt/core.h>
3#include <knowCore/BigNumber.h>
4#include <knowCore/TypeDefinitions.h>
5#include <knowCore/ValueHash.h>
6#include <knowCore/ValueList.h>
8namespace knowCore::pybind11
10 template<
typename _T_>
11 _T_ handle_result(
const cres_qresult<_T_>& _result)
13 if(_result.is_successful())
15 return _result.get_value();
19 throw std::runtime_error(_result.get_error().get_message().toStdString());
23 void handle_result(
const cres_qresult<void>& _result)
25 if(not _result.is_successful())
27 throw std::runtime_error(_result.get_error().get_message().toStdString());
31 template<
typename _T_>
32 void register_value_converter();
35 struct abstract_value_converter;
38 template<
typename _T_>
39 friend void knowCore::pybind11::register_value_converter();
40 friend struct ::pybind11::detail::type_caster<knowCore::Value>;
45 ::pybind11::return_value_policy _policy,
46 ::pybind11::handle _parent);
51 virtual bool can_cast(
const knowCore::Value& _variant,
bool _strict)
const = 0;
53 ::pybind11::return_value_policy _policy,
54 ::pybind11::handle _parent)
const
58 inline ::pybind11::handle
60 ::pybind11::return_value_policy _policy,
61 ::pybind11::handle _parent)
65 if(conv->can_cast(_variant,
true))
67 return conv->to_python(_variant, _policy, _parent);
70 for(
const abstract_value_converter* conv : converters())
72 if(conv->can_cast(_variant,
false))
74 return conv->to_python(_variant, _policy, _parent);
77 throw ::pybind11::type_error(
"No converter for type: "
78 + QString(_variant.
datatype()).toStdString()
79 +
" from knowCore::Value.");
81 inline knowCore::Value value_converter_interface::to_value(::pybind11::handle _handle)
83 for(
const abstract_value_converter* conv : converters())
87 return conv->to_value(_handle);
89 catch(
const std::exception&)
93 throw ::pybind11::type_error(
"No converter for python value '"
94 + std::string(::pybind11::repr(_handle))
95 +
"' to knowCore::Value.");
98 template<
typename _T_>
101 bool can_cast(
const knowCore::Value& _variant,
bool _strict)
const override
103 return (_strict and _variant.is<_T_>()) or (not _strict and _variant.
canConvert<_T_>());
106 ::pybind11::return_value_policy _policy,
107 ::pybind11::handle _parent)
const override
109 return ::pybind11::cast(_value.
value<_T_>(), _policy, _parent).release();
118 template<
typename _T_>
119 void register_value_converter()
125namespace pybind11::detail
138 if(int_::check_(src))
140 value = pybind11::cast<long>(src);
143 else if(float_::check_(src))
145 value = pybind11::cast<double>(src);
158 return pybind11::cast(src.toDouble(), policy, parent);
162 return pybind11::cast(knowCore::pybind11::handle_result(src.
toInt64()), policy, parent);
167 struct type_caster<knowCore::Value>
177 value = knowCore::pybind11::detail::value_converter_interface::to_value(src);
186 return knowCore::pybind11::detail::value_converter_interface::to_python(src, policy, parent);
200 value = pybind11::cast<QHash<QString, knowCore::Value>>(src);
209 return pybind11::cast(src.
hash(), policy, parent).release();
213 struct type_caster<knowCore::ValueList>
223 value = pybind11::cast<QList<knowCore::Value>>(src);
232 return pybind11::cast(src.values(), policy, parent).release();
236 struct type_caster<cres_qresult<void>>
239 PYBIND11_TYPE_CASTER(cres_qresult<void>, const_name(
"cres_qresult<void>"));
244 bool load(handle,
bool) {
return false; }
249 static handle
cast(cres_qresult<void> src, return_value_policy, handle)
251 if(src.is_successful())
253 return pybind11::none();
257 throw std::runtime_error(src.get_error().get_message().toStdString());
261 template<
typename _T_>
262 struct type_caster<cres_qresult<_T_>>
265 using t_conv = make_caster<_T_>;
266 PYBIND11_TYPE_CASTER(cres_qresult<_T_>,
267 const_name(
"cres_qresult<") + t_conv::name + const_name(
">"));
272 bool load(handle src,
bool) {
return false; }
277 static handle
cast(cres_qresult<_T_> src, return_value_policy policy, handle parent)
279 return pybind11::cast(knowCore::pybind11::handle_result(src), policy, parent).release();
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
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:50
Definition knowCorePython.h:37
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