std-map.hpp
Go to the documentation of this file.
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 
10 namespace 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  {
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__
pinocchio::python::details::overload_base_get_item_for_std_map::key_type
Container::key_type key_type
Definition: std-map.hpp:22
boost::python
pinocchio::python::details::overload_base_get_item_for_std_map::index_type
Container::key_type index_type
Definition: std-map.hpp:23
pinocchio::python::details::overload_base_get_item_for_std_map::base_get_item
static boost::python::object base_get_item(boost::python::back_reference< Container & > container, PyObject *i_)
Definition: std-map.hpp:33
inverse-kinematics.i
int i
Definition: inverse-kinematics.py:20
pinocchio::python::details::overload_base_get_item_for_std_map
Definition: std-map.hpp:17
pinocchio::python::details::overload_base_get_item_for_std_map::convert_index
static index_type convert_index(Container &, PyObject *i_)
Definition: std-map.hpp:49
details
python
pinocchio::python::details::overload_base_get_item_for_std_map::value_type
Container::value_type value_type
Definition: std-map.hpp:20
pinocchio::python::details::overload_base_get_item_for_std_map::data_type
Container::value_type::second_type data_type
Definition: std-map.hpp:21
value_type
Scalar value_type
Definition: eigen_plugin.hpp:2
pinocchio::python::details::overload_base_get_item_for_std_map::visit
void visit(Class &cl) const
Definition: std-map.hpp:26
cl
cl
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Sun Jun 16 2024 02:43:13