archive.hpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018, CNRS
2 // Authors: Justin Carpentier <jcarpent@laas.fr>
3 
4 #ifndef __multicontact_api_python_serialization_archive_hpp__
5 #define __multicontact_api_python_serialization_archive_hpp__
6 
7 #include <boost/archive/text_iarchive.hpp>
8 #include <boost/archive/text_oarchive.hpp>
9 
11 
12 namespace multicontact_api {
13 namespace python {
14 
15 namespace bp = boost::python;
16 
17 template <typename Derived>
18 struct cs_pickle_suite : bp::pickle_suite {
19  static bp::object getstate(const Derived& cs) {
20  std::ostringstream os;
21  boost::archive::text_oarchive oa(os);
22  oa << cs;
23  return bp::str(os.str());
24  }
25 
26  static void setstate(Derived& cs, bp::object entries) {
27  bp::str s = bp::extract<bp::str>(entries)();
28  std::string st = bp::extract<std::string>(s)();
29  std::istringstream is(st);
30  boost::archive::text_iarchive ia(is);
31  ia >> cs;
32  }
33 };
34 
35 template <typename Derived>
37  : public boost::python::def_visitor<SerializableVisitor<Derived> > {
38  template <class PyClass>
39  void visit(PyClass& cl) const {
40  cl.def("saveAsText", &Derived::saveAsText, bp::args("filename"),
41  "Saves *this inside a text file.")
42  .def("loadFromText", &Derived::loadFromText, bp::args("filename"),
43  "Loads *this from a text file.")
44  .def("saveAsXML", &Derived::saveAsXML, bp::args("filename", "tag_name"),
45  "Saves *this inside a XML file.")
46  .def("loadFromXML", &Derived::loadFromXML,
47  bp::args("filename", "tag_name"), "Loads *this from a XML file.")
48  .def("saveAsBinary", &Derived::saveAsBinary, bp::args("filename"),
49  "Saves *this inside a binary file.")
50  .def("loadFromBinary", &Derived::loadFromBinary, bp::args("filename"),
51  "Loads *this from a binary file.")
52  .def_pickle(cs_pickle_suite<Derived>());
53  }
54 };
55 } // namespace python
56 } // namespace multicontact_api
57 
58 #endif // ifndef __multicontact_api_python_serialization_archive_hpp__
multicontact_api::python::cs_pickle_suite::getstate
static bp::object getstate(const Derived &cs)
Definition: archive.hpp:19
multicontact_api::python::SerializableVisitor
Definition: archive.hpp:36
multicontact_api
Definition: ellipsoid.hpp:12
multicontact_api::python::cs_pickle_suite
Definition: archive.hpp:18
multicontact_api::python::cs_pickle_suite::setstate
static void setstate(Derived &cs, bp::object entries)
Definition: archive.hpp:26
multicontact_api::python::SerializableVisitor::visit
void visit(PyClass &cl) const
Definition: archive.hpp:39
fwd.hpp