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(); |
39 |
|
|
it != map.end(); |
40 |
|
|
++it) |
41 |
|
|
{ |
42 |
|
|
list.append(boost::python::make_tuple(it->first,it->second)); |
43 |
|
|
} |
44 |
|
|
return boost::python::make_tuple(list); |
45 |
|
|
} |
46 |
|
|
return boost::python::make_tuple(); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
static void setstate(bp::object op, bp::tuple tup) |
50 |
|
|
{ |
51 |
|
|
typedef typename MapType::key_type key_type; |
52 |
|
|
typedef typename MapType::mapped_type mapped_type; |
53 |
|
|
|
54 |
|
|
if(bp::len(tup) > 0) |
55 |
|
|
{ |
56 |
|
|
bp::extract<MapType&> get_map(op); |
57 |
|
|
if(get_map.check()) |
58 |
|
|
{ |
59 |
|
|
MapType & map = get_map(); |
60 |
|
|
boost::python::list list = bp::extract<boost::python::list>(tup[0])(); |
61 |
|
|
for(boost::python::ssize_t k = 0; k < boost::python::len(list); ++k) |
62 |
|
|
{ |
63 |
|
|
boost::python::tuple entry = bp::extract<boost::python::tuple>(list[k])(); |
64 |
|
|
key_type key = bp::extract<key_type>(entry[0])(); |
65 |
|
|
map[key] = bp::extract<mapped_type>(entry[1])(); |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
19 |
static bool getstate_manages_dict() { return true; } |
72 |
|
|
}; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
#endif // ifndef __pinocchio_python_utils_pickle_map_hpp__ |