pickle.hh
Go to the documentation of this file.
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  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 COAL_PYTHON_PICKLE_H
boost::python
eigenpy.hpp
coal
Main namespace.
Definition: coal/broadphase/broadphase_bruteforce.h:44
PickleObject::getinitargs
static boost::python::tuple getinitargs(const T &)
Definition: pickle.hh:20
eigenpy::Exception
PickleObject
Definition: pickle.hh:19
PickleObject::setstate
static void setstate(T &obj, boost::python::tuple tup)
Definition: pickle.hh:32
PickleObject::getstate_manages_dict
static bool getstate_manages_dict()
Definition: pickle.hh:54
str
const char * str()
Definition: doxygen_xml_parser.py:885
PickleObject::getstate
static boost::python::tuple getstate(const T &obj)
Definition: pickle.hh:24


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:58