| Directory: | ./ |
|---|---|
| File: | include/pinocchio/bindings/python/utils/std-map.hpp |
| Date: | 2025-02-12 21:03:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 16 | 22 | 72.7% |
| Branches: | 13 | 38 | 34.2% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 69 | void visit(Class & cl) const | |
| 27 | { | ||
| 28 | 69 | cl.def("__getitem__", &base_get_item); | |
| 29 | 69 | } | |
| 30 | |||
| 31 | private: | ||
| 32 | static boost::python::object | ||
| 33 | 204 | base_get_item(boost::python::back_reference<Container &> container, PyObject * i_) | |
| 34 | { | ||
| 35 | namespace bp = ::boost::python; | ||
| 36 | |||
| 37 |
1/2✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
|
204 | index_type idx = convert_index(container.get(), i_); |
| 38 |
1/2✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
|
204 | typename Container::iterator i = container.get().find(idx); |
| 39 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 204 times.
|
204 | 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 |
3/6✓ Branch 2 taken 204 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 204 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 204 times.
✗ Branch 9 not taken.
|
612 | return bp::object(bp::handle<>(convert(i->second))); |
| 47 | 204 | } | |
| 48 | |||
| 49 | 204 | static index_type convert_index(Container & /*container*/, PyObject * i_) | |
| 50 | { | ||
| 51 | namespace bp = ::boost::python; | ||
| 52 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
204 | bp::extract<key_type const &> i(i_); |
| 53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
|
204 | if (i.check()) |
| 54 | { | ||
| 55 | ✗ | return i(); | |
| 56 | } | ||
| 57 | else | ||
| 58 | { | ||
| 59 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
204 | bp::extract<key_type> i(i_); |
| 60 |
1/2✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
|
204 | if (i.check()) |
| 61 |
2/4✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 204 times.
✗ Branch 5 not taken.
|
204 | return i(); |
| 62 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 204 times.
|
204 | } |
| 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__ | ||
| 75 |