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 #ifndef WIN32
00025 #include <config.h>
00026 #endif
00027
00028 #include <boost/python.hpp>
00029 #include <sstream>
00033 template <class T>
00034 struct T_pickle_suite : boost::python::pickle_suite
00035 {
00036 static
00037 std::string print_to_string(const T& t)
00038 {
00039 std::ostringstream os;
00040 t.printOn(os);
00041 os << std::ends;
00042 return os.str();
00043 }
00044
00045 static
00046 boost::python::tuple getstate(const T& t)
00047 {
00048 std::string s = print_to_string(t);
00049 return boost::python::make_tuple( boost::python::str(s));
00050 }
00051
00052 static
00053 void setstate(T& t, boost::python::tuple pickled)
00054 {
00055 std::string s = boost::python::extract<std::string>(pickled[0]);
00056 std::istringstream is(s);
00057 t.readFrom(is);
00058 }
00059 };
00060
00064 template <class Persistent, class X1, class X2, class X3>
00065 boost::python::class_<Persistent, X1, X2, X3>& pickle(boost::python::class_<Persistent, X1, X2, X3>& c)
00066 {
00067 return c.def_pickle(T_pickle_suite<Persistent>())
00068 .def("__str__", T_pickle_suite<Persistent>::print_to_string);
00069 }
00070
00071 #endif