GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: python/pickle.hh Lines: 17 19 89.5 %
Date: 2024-02-09 12:57:42 Branches: 20 48 41.7 %

Line Branch Exec Source
1
//
2
// Copyright (c) 2022 INRIA
3
//
4
5
#ifndef HPP_FCL_PYTHON_PICKLE_H
6
#define HPP_FCL_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 hpp::fcl;
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
32
    std::stringstream ss;
26
16
    boost::archive::text_oarchive oa(ss);
27
16
    oa & obj;
28
29

32
    return boost::python::make_tuple(boost::python::str(ss.str()));
30
  }
31
32
16
  static void setstate(T& obj, boost::python::tuple tup) {
33


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

32
    boost::python::object py_obj = tup[0];
41
32
    boost::python::extract<std::string> obj_as_string(py_obj.ptr());
42
16
    if (obj_as_string.check()) {
43

32
      const std::string str = obj_as_string;
44
32
      std::istringstream is(str);
45
32
      boost::archive::text_iarchive ia(is, boost::archive::no_codecvt);
46
16
      ia >> obj;
47
    } 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
150
  static bool getstate_manages_dict() { return false; }
55
};
56
57
#endif  // ifndef HPP_FCL_PYTHON_PICKLE_H