Boost.Serialization and Boost.Python double-sided pickle

I have a C ++ library that uses Boost.Serialization. I am creating Python bindings for this library using Boost.Python. It’s clear enough how to make a pickle set for Boost.Python that uses Boost.Serialization (save to a string using Boost.Serialization and return that string to Python).

What I want is the opposite: with boost::python::object I want to have a serialize(...) function that will call the Python pickle.dumps() function and serialize the resulting string. (Imagine a std::vector<boost::python::object> . When I serialize this vector, Boost.Serialization will call the helper function serialize() .) Is this possible? Better yet, is it possible to use cPickle and workaround while giving control to the Python interpreter?

+4
source share
1 answer

Here is the code I use to parse / spill boost instance :: mersenne_twister

 typedef boost::mt19937 rng_t; struct mt_pickle_suite : bp::pickle_suite { static bp::object getstate (const rng_t& rng) { std::ostringstream os; boost::archive::binary_oarchive oa(os); oa << rng; return bp::str (os.str()); } static void setstate(rng_t& rng, 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::binary_iarchive ia (is); ia >> rng; } }; 
0
source

All Articles