[C++-sig] serialization
Neal D. Becker
ndbecker2 at verizon.net
Wed Sep 8 14:55:45 CEST 2004
Now serialization will hopefully be part of boost soon. It would be good to
share a single serialization implementation, so that a class could be
serialized in c++ using boost or by python.
Here is one way to achieve this. Assume
template<class Archive>
void serialize(Archive & ar, const unsigned int version);
is added to your class. Then an easy way to use this from boost::python is
to serialize to a stringstream, and pickle the resulting string. Here is
an example:
struct mt_pickle_suite : python::pickle_suite {
static python::object getstate (rng_t& rng) {
std::ostringstream os;
boost::archive::binary_oarchive oa(os);
oa << rng;
return python::str (os.str());
}
static void
setstate(rng_t& rng, python::object entries) {
python::str s = python::extract<python::str> (entries)();
std::string st = python::extract<std::string> (s)();
std::istringstream is (st);
boost::archive::binary_iarchive ia (is);
ia >> rng;
}
};
More information about the Cplusplus-sig
mailing list