Directory: | ./ |
---|---|
File: | python/pickle.hh |
Date: | 2025-04-01 09:23:31 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 18 | 20 | 90.0% |
Branches: | 20 | 48 | 41.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2022 INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef COAL_PYTHON_PICKLE_H | ||
6 | #define COAL_PYTHON_PICKLE_H | ||
7 | |||
8 | #include <boost/python.hpp> | ||
9 | #include <eigenpy/eigenpy.hpp> | ||
10 | |||
11 | #include <boost/archive/text_oarchive.hpp> | ||
12 | #include <boost/archive/text_iarchive.hpp> | ||
13 | #include <sstream> | ||
14 | |||
15 | using namespace boost::python; | ||
16 | using namespace coal; | ||
17 | // | ||
18 | template <typename T> | ||
19 | struct PickleObject : boost::python::pickle_suite { | ||
20 | 16 | static boost::python::tuple getinitargs(const T&) { | |
21 | 16 | return boost::python::make_tuple(); | |
22 | } | ||
23 | |||
24 | 16 | static boost::python::tuple getstate(const T& obj) { | |
25 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | std::stringstream ss; |
26 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | boost::archive::text_oarchive oa(ss); |
27 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | oa & obj; |
28 | |||
29 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8 times.
✗ Branch 8 not taken.
|
48 | return boost::python::make_tuple(boost::python::str(ss.str())); |
30 | } | ||
31 | |||
32 | 16 | static void setstate(T& obj, boost::python::tuple tup) { | |
33 |
5/10✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 8 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 8 times.
|
16 | if (boost::python::len(tup) == 0 || boost::python::len(tup) > 1) { |
34 | ✗ | throw eigenpy::Exception( | |
35 | "Pickle was not able to reconstruct the object from the loaded " | ||
36 | "data.\n" | ||
37 | "The pickle data structure contains too many elements."); | ||
38 | } | ||
39 | |||
40 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | boost::python::object py_obj = tup[0]; |
41 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
16 | boost::python::extract<std::string> obj_as_string(py_obj.ptr()); |
42 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | if (obj_as_string.check()) { |
43 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
16 | const std::string str = obj_as_string; |
44 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | std::istringstream is(str); |
45 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | boost::archive::text_iarchive ia(is, boost::archive::no_codecvt); |
46 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | ia >> obj; |
47 | 16 | } else { | |
48 | ✗ | throw eigenpy::Exception( | |
49 | "Pickle was not able to reconstruct the model from the loaded data.\n" | ||
50 | "The entry is not a string."); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | 155 | static bool getstate_manages_dict() { return false; } | |
55 | }; | ||
56 | |||
57 | #endif // ifndef COAL_PYTHON_PICKLE_H | ||
58 |