00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef PICKLE_H
00022 #define PICKLE_h
00023
00024 #include <config.h>
00025
00026 #include <boost/python.hpp>
00027 #include <sstream>
00031 template <class T>
00032 struct T_pickle_suite : boost::python::pickle_suite
00033 {
00034 static
00035 std::string print_to_string(const T& t)
00036 {
00037 std::ostringstream os;
00038 t.printOn(os);
00039 os << std::ends;
00040 return os.str();
00041 }
00042
00043 static
00044 boost::python::tuple getstate(const T& t)
00045 {
00046 std::string s = print_to_string(t);
00047 return boost::python::make_tuple( boost::python::str(s));
00048 }
00049
00050 static
00051 void setstate(T& t, boost::python::tuple pickled)
00052 {
00053 std::string s = boost::python::extract<std::string>(pickled[0]);
00054 std::istringstream is(s);
00055 t.readFrom(is);
00056 }
00057 };
00058
00062 template <class Persistent, class X1, class X2, class X3>
00063 boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c)
00064 {
00065 return c.def_pickle(T_pickle_suite<Persistent>())
00066 .def("__str__", T_pickle_suite<Persistent>::print_to_string);
00067 }
00068
00069 #endif