pinocchio  3.7.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
 
Loading...
Searching...
No Matches
std-map.hpp
1//
2// Copyright (c) 2020 INRIA
3//
4
5#ifndef __pinocchio_python_utils_map_hpp__
6#define __pinocchio_python_utils_map_hpp__
7
8#include <boost/python/suite/indexing/map_indexing_suite.hpp>
9
10namespace pinocchio
11{
12 namespace python
13 {
14 namespace details
15 {
16 template<typename Container>
18 : public boost::python::def_visitor<overload_base_get_item_for_std_map<Container>>
19 {
20 typedef typename Container::value_type value_type;
21 typedef typename Container::value_type::second_type data_type;
22 typedef typename Container::key_type key_type;
23 typedef typename Container::key_type index_type;
24
25 template<class Class>
26 void visit(Class & cl) const
27 {
28 cl.def("__getitem__", &base_get_item);
29 }
30
31 private:
32 static boost::python::object
33 base_get_item(boost::python::back_reference<Container &> container, PyObject * i_)
34 {
35 namespace bp = ::boost::python;
36
37 index_type idx = convert_index(container.get(), i_);
38 typename Container::iterator i = container.get().find(idx);
39 if (i == container.get().end())
40 {
41 PyErr_SetString(PyExc_KeyError, "Invalid key");
42 bp::throw_error_already_set();
43 }
44
45 typename bp::to_python_indirect<data_type &, bp::detail::make_reference_holder> convert;
46 return bp::object(bp::handle<>(convert(i->second)));
47 }
48
49 static index_type convert_index(Container & /*container*/, PyObject * i_)
50 {
51 namespace bp = ::boost::python;
52 bp::extract<key_type const &> i(i_);
53 if (i.check())
54 {
55 return i();
56 }
57 else
58 {
59 bp::extract<key_type> i(i_);
60 if (i.check())
61 return i();
62 }
63
64 PyErr_SetString(PyExc_TypeError, "Invalid index type");
65 bp::throw_error_already_set();
66 return index_type();
67 }
68 };
69
70 } // namespace details
71 } // namespace python
72} // namespace pinocchio
73
74#endif // ifndef __pinocchio_python_utils_map_hpp__
Main pinocchio namespace.
Definition treeview.dox:11