13 #include <boost/python.hpp> 32 template <
typename ResultType,
typename SourceType>
37 for (
auto obj : objects)
39 result.append(ResultType(obj));
48 std::vector<T> result;
49 std::size_t listSize = len(list);
51 for (std::size_t i = 0; i < listSize; ++i)
53 const object & element = list[i];
54 const T & value = extract<T>(element)();
55 result.push_back(value);
88 return objectConverter.
Result;
95 if (extract<std::string>(obj).
check())
97 var = extract<std::string>(obj)();
100 else if (extract<list>(obj).
check())
105 if (extract<long>(obj[0]).
check())
107 var = ToVector<long>(obj);
110 else if (extract<double>(obj[0]).check())
112 var = ToVector<double>(obj);
115 else if (extract<std::vector<std::string>>(obj).check())
117 var = extract<std::vector<std::string>>(obj)();
120 else if (extract<std::vector<NodeId>>(obj).
check())
122 var = extract<std::vector<NodeId>>(obj)();
125 else if (extract<std::vector<DateTime>>(obj).
check())
127 var = extract<std::vector<DateTime>>(obj)();
132 throw std::logic_error(
"Cannot create variant from python list. Unsupported type.");
137 else if (extract<long>(obj).
check())
139 var = extract<long>(obj)();
142 else if (extract<double>(obj).
check())
144 var = extract<double>(obj)();
147 else if (extract<NodeId>(obj).
check())
149 var = extract<NodeId>(obj)();
152 else if (extract<DateTime>(obj).
check())
154 var = extract<DateTime>(obj)();
159 throw std::logic_error(
"Cannot create variant from python object. Unsupported type.");
171 if (extract<list>(obj).
check())
183 case VariantType::BOOLEAN:
184 var = ToVector<bool>(obj);
187 case VariantType::UINT16:
188 case VariantType::UINT32:
189 var = ToVector<uint32_t>(obj);
192 case VariantType::FLOAT:
193 var = ToVector<float>(obj);
206 case VariantType::BOOLEAN:
207 var = extract<bool>(obj)();
210 case VariantType::UINT16:
211 case VariantType::UINT32:
212 var = extract<uint32_t>(obj)();
215 case VariantType::FLOAT:
216 var = extract<float>(obj)();
230 converter::registry::push_back(&convertible, &construct, type_id<Variant>());
238 static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
242 object obj = object(handle<>(borrowed(obj_ptr)));
244 void * storage = ((converter::rvalue_from_python_storage<Variant> *)data)->storage.bytes;
248 data->convertible = storage;
267 return incref(obj.ptr());
Variant ToVariant2(const object &obj, VariantType vtype)
list ToList(const std::vector< T > objects)
object ToObject(const Variant &var)
void OnScalar(const T &val)
variant_from_python_converter()
static void * convertible(PyObject *obj_ptr)
Variant ToVariant(const object &obj)
static PyObject * convert(Variant const &v)
static void construct(PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data)
Python bindings for freeopcua. GNU LGPL.
std::vector< T > ToVector(const object &list)
void OnContainer(const T &val)