py_opcua_helpers.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <vector>
14 #include <boost/python.hpp>
15 
16 using namespace boost::python;
17 
18 //
19 // vector<T> to python []
20 //
21 
22 template<class T>
24 {
25 
26  static PyObject * convert(const std::vector<T> & vec)
27  {
28  list result;
29 
30  for (auto e : vec)
31  { result.append(e); }
32 
33  return incref(result.ptr());
34  }
35 };
36 
37 
38 //
39 // def_readwrite + conversion
40 //
41 
42 #define def_readwrite_vector(NAME, FIELDREF) \
43 add_property(NAME, make_getter(FIELDREF, return_value_policy<return_by_value>()), \
44  make_setter(FIELDREF, return_value_policy<return_by_value>()))
45 
46 //
47 // python [] to vector<T>
48 //
49 
50 template<typename T>
52 {
53 
55  {
56  converter::registry::push_back(&convertible, &construct, type_id<std::vector<T>>());
57  }
58 
59  static void * convertible(PyObject * obj_ptr)
60  {
61  if (PySequence_Check(obj_ptr))
62  {
63  for (Py_ssize_t i = 0; i < PySequence_Length(obj_ptr); i++)
64  {
65  if (!extract<T>(object(handle<>(PySequence_GetItem(obj_ptr, i)))).check())
66  {
67  return NULL;
68  }
69  }
70 
71  return obj_ptr;
72  }
73 
74  else
75  {
76  return NULL;
77  }
78  }
79 
80  static void construct(PyObject * obj_ptr, converter::rvalue_from_python_stage1_data * data)
81  {
82 
83  std::vector<T> vs;
84 
85  for (Py_ssize_t i = 0; i < PySequence_Length(obj_ptr); i++)
86  {
87  PyObject * elt = PySequence_GetItem(obj_ptr, i);
88  vs.push_back(extract<T>(object(handle<>(borrowed(elt)))));
89  }
90 
91  void * storage = ((converter::rvalue_from_python_storage<std::vector<T>> *)data)->storage.bytes;
92 
93  new(storage) std::vector<T>(vs);
94 
95  data->convertible = storage;
96 
97  }
98 
99 };
100 
static void construct(PyObject *obj_ptr, converter::rvalue_from_python_stage1_data *data)
static PyObject * convert(const std::vector< T > &vec)
Python bindings for freeopcua. GNU LGPL.
static void * convertible(PyObject *obj_ptr)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:07