5 #ifndef __pinocchio_python_utils_std_vector_hpp__ 6 #define __pinocchio_python_utils_std_vector_hpp__ 8 #include <boost/python.hpp> 9 #include <boost/python/stl_iterator.hpp> 10 #include <boost/python/suite/indexing/vector_indexing_suite.hpp> 25 template<
typename Container>
27 :
public boost::python::def_visitor< overload_base_get_item_for_std_vector<Container> >
33 template <
class Class>
42 static boost::python::object
43 base_get_item(boost::python::back_reference<Container&> container, PyObject* i_)
45 namespace bp = ::boost::python;
48 typename Container::iterator
i = container.get().begin();
50 if (i == container.get().end())
52 PyErr_SetString(PyExc_KeyError,
"Invalid index");
53 bp::throw_error_already_set();
56 typename bp::to_python_indirect<data_type&,bp::detail::make_reference_holder> convert;
57 return bp::object(bp::handle<>(convert(*i)));
64 bp::extract<long>
i(i_);
69 index += container.size();
70 if (index >=
long(container.size()) || index < 0)
72 PyErr_SetString(PyExc_IndexError,
"Index out of range");
73 bp::throw_error_already_set();
78 PyErr_SetString(PyExc_TypeError,
"Invalid index type");
79 bp::throw_error_already_set();
90 template<
typename vector_type>
101 if(!PyList_Check(obj_ptr))
return 0;
104 bp::object bp_obj(bp::handle<>(bp::borrowed(obj_ptr)));
105 bp::list bp_list(bp_obj);
106 bp::ssize_t list_size = bp::len(bp_list);
109 for(bp::ssize_t k = 0; k < list_size; ++k)
111 bp::extract<T> elt(bp_list[k]);
112 if(!elt.check())
return 0;
120 boost::python::converter::rvalue_from_python_stage1_data * memory)
125 bp::object bp_obj(bp::handle<>(bp::borrowed(obj_ptr)));
126 bp::list bp_list(bp_obj);
128 void * storage =
reinterpret_cast< bp::converter::rvalue_from_python_storage<vector_type>*
> 129 (
reinterpret_cast<void*
>(memory))->storage.bytes;
131 typedef bp::stl_input_iterator<T> iterator;
134 new (storage) vector_type(iterator(bp_list),
138 memory->convertible = storage;
143 ::boost::python::converter::registry::push_back(&convertible,
145 ::boost::python::type_id<vector_type>());
148 static ::boost::python::list
tolist(vector_type &
self)
152 typedef bp::iterator<vector_type> iterator;
153 bp::list python_list(iterator()(
self));
169 template<
class T,
class Allocator = std::allocator<T>,
bool NoProxy = false,
bool EnableFromPythonListConverter = true>
171 :
public ::boost::python::vector_indexing_suite<typename std::vector<T,Allocator>, NoProxy>
177 static ::boost::python::class_<vector_type>
expose(
const std::string & class_name,
178 const std::string & doc_string =
"")
182 bp::class_<vector_type> cl(class_name.c_str(),doc_string.c_str());
185 .def(
"tolist",&FromPythonListConverter::tolist,bp::arg(
"self"),
186 "Returns the std::vector as a Python list.")
190 if(EnableFromPythonListConverter)
191 FromPythonListConverter::register_converter();
200 #endif // ifndef __pinocchio_python_utils_std_vector_hpp__ static boost::python::object base_get_item(boost::python::back_reference< Container & > container, PyObject *i_)
::boost::python::list tolist(vector_type &self)
static void * convertible(PyObject *obj_ptr)
Check if obj_ptr can be converted.
StdContainerFromPythonList< vector_type > FromPythonListConverter
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *memory)
Allocate the std::vector and fill it with the element contained in the list.
Container::value_type value_type
Expose an std::vector from a type given as template argument.
Register the conversion from a Python list to a std::vector.
vector_type::value_type T
static index_type convert_index(Container &container, PyObject *i_)
static void register_converter()
void visit(Class &cl) const
std::vector< T, Allocator > vector_type
static::boost::python::class_< vector_type > expose(const std::string &class_name, const std::string &doc_string="")
Create a pickle interface for the std::vector and aligned vector.
Main pinocchio namespace.
Container::value_type data_type