| Directory: | ./ |
|---|---|
| File: | include/pinocchio/bindings/python/utils/pickle-map.hpp |
| Date: | 2025-02-12 21:03:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 25 | 26 | 96.2% |
| Branches: | 35 | 68 | 51.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019-2020 CNRS INRIA | ||
| 3 | // | ||
| 4 | |||
| 5 | #ifndef __pinocchio_python_utils_pickle_map_hpp__ | ||
| 6 | #define __pinocchio_python_utils_pickle_map_hpp__ | ||
| 7 | |||
| 8 | #include <boost/python.hpp> | ||
| 9 | #include <boost/python/tuple.hpp> | ||
| 10 | |||
| 11 | namespace pinocchio | ||
| 12 | { | ||
| 13 | namespace python | ||
| 14 | { | ||
| 15 | namespace bp = boost::python; | ||
| 16 | /// | ||
| 17 | /// \brief Create a pickle interface for the std::map and aligned map | ||
| 18 | /// | ||
| 19 | /// \tparam MapType Map Type to pickle | ||
| 20 | /// | ||
| 21 | /// \sa PickleVector | ||
| 22 | /// | ||
| 23 | template<typename MapType> | ||
| 24 | struct PickleMap : boost::python::pickle_suite | ||
| 25 | { | ||
| 26 | 1 | static boost::python::tuple getinitargs(const MapType &) | |
| 27 | { | ||
| 28 | 1 | return boost::python::make_tuple(); | |
| 29 | } | ||
| 30 | |||
| 31 | 1 | static boost::python::tuple getstate(boost::python::object op) | |
| 32 | { | ||
| 33 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | boost::python::extract<const MapType &> get_map(op); |
| 34 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (get_map.check()) |
| 35 | { | ||
| 36 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | const MapType & map = get_map(); |
| 37 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | boost::python::list list; |
| 38 |
2/2✓ Branch 3 taken 100 times.
✓ Branch 4 taken 1 times.
|
101 | for (typename MapType::const_iterator it = map.begin(); it != map.end(); ++it) |
| 39 | { | ||
| 40 |
2/4✓ Branch 3 taken 100 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 100 times.
✗ Branch 7 not taken.
|
100 | list.append(boost::python::make_tuple(it->first, it->second)); |
| 41 | } | ||
| 42 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | return boost::python::make_tuple(list); |
| 43 | 1 | } | |
| 44 | ✗ | return boost::python::make_tuple(); | |
| 45 | } | ||
| 46 | |||
| 47 | 1 | static void setstate(bp::object op, bp::tuple tup) | |
| 48 | { | ||
| 49 | typedef typename MapType::key_type key_type; | ||
| 50 | typedef typename MapType::mapped_type mapped_type; | ||
| 51 | |||
| 52 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (bp::len(tup) > 0) |
| 53 | { | ||
| 54 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | bp::extract<MapType &> get_map(op); |
| 55 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (get_map.check()) |
| 56 | { | ||
| 57 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | MapType & map = get_map(); |
| 58 |
4/8✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
|
1 | boost::python::list list = bp::extract<boost::python::list>(tup[0])(); |
| 59 |
3/4✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 100 times.
✓ Branch 5 taken 1 times.
|
101 | for (boost::python::ssize_t k = 0; k < boost::python::len(list); ++k) |
| 60 | { | ||
| 61 |
4/8✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 100 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 100 times.
✗ Branch 11 not taken.
|
100 | boost::python::tuple entry = bp::extract<boost::python::tuple>(list[k])(); |
| 62 |
5/10✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 100 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 100 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 100 times.
✗ Branch 14 not taken.
|
200 | key_type key = bp::extract<key_type>(entry[0])(); |
| 63 |
6/12✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 100 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 100 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 100 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 100 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 100 times.
✗ Branch 17 not taken.
|
100 | map[key] = bp::extract<mapped_type>(entry[1])(); |
| 64 | } | ||
| 65 | 1 | } | |
| 66 | } | ||
| 67 | 1 | } | |
| 68 | |||
| 69 | 69 | static bool getstate_manages_dict() | |
| 70 | { | ||
| 71 | 69 | return true; | |
| 72 | } | ||
| 73 | }; | ||
| 74 | } // namespace python | ||
| 75 | } // namespace pinocchio | ||
| 76 | |||
| 77 | #endif // ifndef __pinocchio_python_utils_pickle_map_hpp__ | ||
| 78 |