Commit 3bb60a04 authored by Pierre Fernbach's avatar Pierre Fernbach
Browse files

[Python] factorize pickle code for all classes

parent e7b67d10
......@@ -229,8 +229,7 @@ struct ContactSequencePythonVisitor : public bp::def_visitor<ContactSequencePyth
"if t equal to the last phase timeFinal, this index is returned.")
.def(bp::self == bp::self)
.def(bp::self != bp::self)
.def("copy", &copy, "Returns a copy of *this.")
.def_pickle(cs_pickle_suite());
.def("copy", &copy, "Returns a copy of *this.");
}
static void expose(const std::string& class_name) {
......@@ -262,24 +261,6 @@ struct ContactSequencePythonVisitor : public bp::def_visitor<ContactSequencePyth
return toPythonList<std::string>(self.getAllEffectorsInContact());
}
struct cs_pickle_suite : bp::pickle_suite {
static bp::object getstate (const CS& cs) {
std::ostringstream os;
boost::archive::text_oarchive oa(os);
oa << cs;
return bp::str(os.str());
}
static void
setstate(CS& 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;
}
};
};
} // namespace python
} // namespace multicontact_api
......
......@@ -11,6 +11,26 @@ namespace python {
namespace bp = boost::python;
template <typename Derived>
struct cs_pickle_suite : bp::pickle_suite {
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;
}
};
template <typename Derived>
struct SerializableVisitor : public boost::python::def_visitor<SerializableVisitor<Derived> > {
template <class PyClass>
......@@ -20,7 +40,8 @@ struct SerializableVisitor : public boost::python::def_visitor<SerializableVisit
.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.")
.def("loadFromBinary", &Derived::loadFromBinary, bp::args("filename"), "Loads *this from a binary file.");
.def("loadFromBinary", &Derived::loadFromBinary, bp::args("filename"), "Loads *this from a binary file.")
.def_pickle(cs_pickle_suite<Derived>());
}
};
} // namespace python
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment