GCC Code Coverage Report


Directory: ./
File: include/pinocchio/bindings/python/utils/pickle-map.hpp
Date: 2024-08-27 18:20:05
Exec Total Coverage
Lines: 2 23 8.7%
Branches: 0 68 0.0%

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 static boost::python::tuple getinitargs(const MapType &)
27 {
28 return boost::python::make_tuple();
29 }
30
31 static boost::python::tuple getstate(boost::python::object op)
32 {
33 boost::python::extract<const MapType &> get_map(op);
34 if (get_map.check())
35 {
36 const MapType & map = get_map();
37 boost::python::list list;
38 for (typename MapType::const_iterator it = map.begin(); it != map.end(); ++it)
39 {
40 list.append(boost::python::make_tuple(it->first, it->second));
41 }
42 return boost::python::make_tuple(list);
43 }
44 return boost::python::make_tuple();
45 }
46
47 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 if (bp::len(tup) > 0)
53 {
54 bp::extract<MapType &> get_map(op);
55 if (get_map.check())
56 {
57 MapType & map = get_map();
58 boost::python::list list = bp::extract<boost::python::list>(tup[0])();
59 for (boost::python::ssize_t k = 0; k < boost::python::len(list); ++k)
60 {
61 boost::python::tuple entry = bp::extract<boost::python::tuple>(list[k])();
62 key_type key = bp::extract<key_type>(entry[0])();
63 map[key] = bp::extract<mapped_type>(entry[1])();
64 }
65 }
66 }
67 }
68
69 20 static bool getstate_manages_dict()
70 {
71 20 return true;
72 }
73 };
74 } // namespace python
75 } // namespace pinocchio
76
77 #endif // ifndef __pinocchio_python_utils_pickle_map_hpp__
78