pickle.hh
Go to the documentation of this file.
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  static boost::python::tuple getinitargs(const T&) {
21  return boost::python::make_tuple();
22  }
23 
24  static boost::python::tuple getstate(const T& obj) {
25  std::stringstream ss;
26  boost::archive::text_oarchive oa(ss);
27  oa& obj;
28 
29  return boost::python::make_tuple(boost::python::str(ss.str()));
30  }
31 
32  static void setstate(T& obj, boost::python::tuple tup) {
33  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  boost::python::object py_obj = tup[0];
41  boost::python::extract<std::string> obj_as_string(py_obj.ptr());
42  if (obj_as_string.check()) {
43  const std::string str = obj_as_string;
44  std::istringstream is(str);
45  boost::archive::text_iarchive ia(is, boost::archive::no_codecvt);
46  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  static bool getstate_manages_dict() { return false; }
55 };
56 
57 #endif // ifndef HPP_FCL_PYTHON_PICKLE_H
static void setstate(T &obj, boost::python::tuple tup)
Definition: pickle.hh:32
const char * str()
static bool getstate_manages_dict()
Definition: pickle.hh:54
static boost::python::tuple getstate(const T &obj)
Definition: pickle.hh:24
static boost::python::tuple getinitargs(const T &)
Definition: pickle.hh:20


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:01