archive.hpp 1.83 KB
Newer Older
Guilhem Saurel's avatar
Guilhem Saurel committed
1
2
3
4
5
6
// Copyright (c) 2015-2018, CNRS
// Authors: Justin Carpentier <jcarpent@laas.fr>

#ifndef __multicontact_api_python_serialization_archive_hpp__
#define __multicontact_api_python_serialization_archive_hpp__

7
#include "multicontact-api/bindings/python/fwd.hpp"
Guilhem Saurel's avatar
Guilhem Saurel committed
8

Guilhem Saurel's avatar
Guilhem Saurel committed
9
10
11
12
13
namespace multicontact_api {
namespace python {

namespace bp = boost::python;

14
15
template <typename Derived>
struct cs_pickle_suite : bp::pickle_suite {
Pierre Fernbach's avatar
Pierre Fernbach committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  static bp::object getstate(const Derived& cs) {
    std::ostringstream os;
    boost::archive::text_oarchive oa(os);
    oa << cs;
    return bp::str(os.str());
  }

  static void setstate(Derived& cs, bp::object entries) {
    bp::str s = bp::extract<bp::str>(entries)();
    std::string st = bp::extract<std::string>(s)();
    std::istringstream is(st);
    boost::archive::text_iarchive ia(is);
    ia >> cs;
  }
30
31
};

Guilhem Saurel's avatar
Guilhem Saurel committed
32
33
34
35
36
37
38
39
40
template <typename Derived>
struct SerializableVisitor : public boost::python::def_visitor<SerializableVisitor<Derived> > {
  template <class PyClass>
  void visit(PyClass& cl) const {
    cl.def("saveAsText", &Derived::saveAsText, bp::args("filename"), "Saves *this inside a text file.")
        .def("loadFromText", &Derived::loadFromText, bp::args("filename"), "Loads *this from a text file.")
        .def("saveAsXML", &Derived::saveAsXML, bp::args("filename", "tag_name"), "Saves *this inside a XML file.")
        .def("loadFromXML", &Derived::loadFromXML, bp::args("filename", "tag_name"), "Loads *this from a XML file.")
        .def("saveAsBinary", &Derived::saveAsBinary, bp::args("filename"), "Saves *this inside a binary file.")
41
42
        .def("loadFromBinary", &Derived::loadFromBinary, bp::args("filename"), "Loads *this from a binary file.")
        .def_pickle(cs_pickle_suite<Derived>());
Guilhem Saurel's avatar
Guilhem Saurel committed
43
  }
Guilhem Saurel's avatar
Guilhem Saurel committed
44
45
46
};
}  // namespace python
}  // namespace multicontact_api
Guilhem Saurel's avatar
Guilhem Saurel committed
47

Guilhem Saurel's avatar
Guilhem Saurel committed
48
#endif  // ifndef __multicontact_api_python_serialization_archive_hpp__