knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
vector.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_PY_CONVERTER_VECTOR_H
7 #define KNOWROB_PY_CONVERTER_VECTOR_H
8 
9 namespace knowrob::py {
10  /*** python-list to c++-list */
11  template<typename containedType>
12  struct custom_vector_from_seq {
14  boost::python::converter::registry::push_back(&convertible,
15  &construct,
16  boost::python::type_id<std::vector<containedType> >());
17  }
18 
19  static void *convertible(PyObject *obj_ptr) {
20  // the second condition is important, for some reason otherwise there were attempted conversions
21  // of Body to list which failed afterwards.
22  if (!PySequence_Check(obj_ptr) || !PyObject_HasAttrString(obj_ptr, "__len__")) return nullptr;
23  return obj_ptr;
24  }
25 
26  static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data) {
27  void *storage = ((boost::python::converter::rvalue_from_python_storage<std::vector<containedType> > *) (data))->storage.bytes;
28  new(storage) std::vector<containedType>();
29  auto v = (std::vector<containedType> *) (storage);
30  auto l = PySequence_Size(obj_ptr);
31  if (l < 0) abort();
32  v->reserve(l);
33  for (int i = 0; i < l; i++) {
34  v->push_back(boost::python::extract<containedType>(PySequence_GetItem(obj_ptr, i)));
35  }
36  data->convertible = storage;
37  }
38  };
39 }
40 
41 #endif //KNOWROB_PY_CONVERTER_VECTOR_H
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Definition: vector.h:26
static void * convertible(PyObject *obj_ptr)
Definition: vector.h:19