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> |
17 |
|
|
struct overload_base_get_item_for_std_map |
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 |
|
19 |
void visit(Class& cl) const |
27 |
|
|
{ |
28 |
|
19 |
cl |
29 |
|
|
.def("__getitem__", &base_get_item); |
30 |
|
19 |
} |
31 |
|
|
|
32 |
|
|
private: |
33 |
|
|
|
34 |
|
|
static boost::python::object |
35 |
|
1 |
base_get_item(boost::python::back_reference<Container&> container, PyObject* i_) |
36 |
|
|
{ |
37 |
|
|
namespace bp = ::boost::python; |
38 |
|
|
|
39 |
✓✗ |
1 |
index_type idx = convert_index(container.get(), i_); |
40 |
✓✗ |
1 |
typename Container::iterator i = container.get().find(idx); |
41 |
✗✓ |
1 |
if (i == container.get().end()) |
42 |
|
|
{ |
43 |
|
|
PyErr_SetString(PyExc_KeyError, "Invalid key"); |
44 |
|
|
bp::throw_error_already_set(); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
typename bp::to_python_indirect<data_type&,bp::detail::make_reference_holder> convert; |
48 |
✓✗✓✗ ✓✗ |
2 |
return bp::object(bp::handle<>(convert(i->second))); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
static index_type |
52 |
|
1 |
convert_index(Container& /*container*/, PyObject* i_) |
53 |
|
|
{ |
54 |
|
|
namespace bp = ::boost::python; |
55 |
✓✗ |
1 |
bp::extract<key_type const&> i(i_); |
56 |
✗✓ |
1 |
if (i.check()) |
57 |
|
|
{ |
58 |
|
|
return i(); |
59 |
|
|
} |
60 |
|
|
else |
61 |
|
|
{ |
62 |
✓✗ |
1 |
bp::extract<key_type> i(i_); |
63 |
✓✗ |
1 |
if (i.check()) |
64 |
✓✗✓✗
|
1 |
return i(); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
PyErr_SetString(PyExc_TypeError, "Invalid index type"); |
68 |
|
|
bp::throw_error_already_set(); |
69 |
|
|
return index_type(); |
70 |
|
|
} |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
#endif // ifndef __pinocchio_python_utils_map_hpp__ |