Boost logo

Boost :

From: Neal D. Becker (ndbecker2_at_[hidden])
Date: 2004-11-12 14:42:45

George van den Driessche wrote:

>> Message: 4
>> Date: Fri, 12 Nov 2004 07:34:27 -0500
>> From: "Neal D. Becker" <ndbecker2_at_[hidden]>
>> Subject: [boost] Re: Generalised reflection
>> To: boost_at_[hidden]
>> Message-ID: <cn2akj$qij$1_at_[hidden]>
>> Content-Type: text/plain; charset=us-ascii
>> George van den Driessche wrote:
>> [...]
>> > Yes. Now imagine that I've got some helper functions for use with
>> > Boost.Python's pickling suite. But I want to use them with a C++
>> > serialisation library. I'm going to have to factor a bunch of code out
> of
>> > those helper functions so that I can invoke them without using
>> > boost::python::tuple, and then write wrappers to use the factored-out
> code
>> > with the Boost.Python pickling suite. Instead, I want to write just the
>> > C++ serialisation code, and have that automatically wrapped up for use
>> > with Boost.Python pickling. It's a similar separation of layers.
>> >
>> Incidentally, I wrote about sharing boost c++ serialization with
>> boost.python pickling, and posted code demonstrating this. Nobody seemed
>> very interested.
> I think it's this post:
> that you're referring to. In which case, I missed it, but yes, you're on
> the same topic.

That is an earlier and uglier version. I played with a few approaches. The
one I settled on is very simple and easy. It does suffer from being
serialized twice - once by boost::serialize and again by python pickle, but
in my case that's a good tradeoff for simplicity.

  Here is the current code I'm using:
(I'm just extracting the part that relates to serialization):

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;

  class_<rng_t> ("rng", "Mersenne Twister",
              (python::arg ("seed")=4357),
              "Construct with optional seed\n"
              "@param seed: initial seed\n"
    .def ("getstate", &mt_pickle_suite::getstate)
    .def ("setstate", &mt_pickle_suite::setstate)

Boost list run by bdawes at, gregod at, cpdaniel at, john at