123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- #ifndef BOOST_MPI_PYTHON_SERIALIZE_HPP
- #define BOOST_MPI_PYTHON_SERIALIZE_HPP
- #include <boost/mpi/python/config.hpp>
- #include <boost/python/object.hpp>
- #include <boost/python/str.hpp>
- #include <boost/python/extract.hpp>
- #include <memory>
- #include <map>
- #include <boost/function/function3.hpp>
- #include <boost/mpl/bool.hpp>
- #include <boost/mpl/if.hpp>
- #include <boost/serialization/split_free.hpp>
- #include <boost/serialization/array.hpp>
- #include <boost/assert.hpp>
- #include <boost/type_traits/is_fundamental.hpp>
- #define BOOST_MPI_PYTHON_FORWARD_ONLY
- #include <boost/mpi/python.hpp>
- #if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_IS_CONVERTIBLE)
- # define BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
- namespace boost { namespace python { namespace api { \
- template<typename R, typename T> \
- struct enable_binary< IArchiver , R, T> {}; \
- \
- template<typename R, typename T> \
- struct enable_binary< OArchiver , R, T> {}; \
- } } }
- # else
- # define BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver)
- #endif
- #define BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
- BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
- namespace boost { namespace python { namespace detail { \
- template<> \
- BOOST_MPI_PYTHON_DECL direct_serialization_table< IArchiver , OArchiver >& \
- get_direct_serialization_table< IArchiver , OArchiver >(); \
- } \
- \
- template<> \
- struct has_direct_serialization< IArchiver , OArchiver> : mpl::true_ { }; \
- \
- template<> \
- struct output_archiver< IArchiver > { typedef OArchiver type; }; \
- \
- template<> \
- struct input_archiver< OArchiver > { typedef IArchiver type; }; \
- } }
- #define BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(IArchiver, OArchiver) \
- namespace boost { namespace python { namespace detail { \
- template \
- class BOOST_MPI_PYTHON_DECL direct_serialization_table< IArchiver , OArchiver >; \
- \
- template<> \
- BOOST_MPI_PYTHON_DECL \
- direct_serialization_table< IArchiver , OArchiver >& \
- get_direct_serialization_table< IArchiver , OArchiver >( ) \
- { \
- static direct_serialization_table< IArchiver, OArchiver > table; \
- return table; \
- } \
- } } }
- namespace boost { namespace python {
- class BOOST_MPI_PYTHON_DECL pickle {
- struct data_t;
- public:
- static str dumps(object obj, int protocol = -1);
- static object loads(str s);
-
- private:
- static void initialize_data();
- static data_t* data;
- };
- template<typename IArchiver, typename OArchiver>
- struct has_direct_serialization : mpl::false_ { };
- template<typename IArchiver> struct output_archiver { };
- template<typename OArchiver> struct input_archiver { };
- namespace detail {
-
- template<typename IArchiver, typename OArchiver>
- class BOOST_MPI_PYTHON_DECL direct_serialization_table
- {
- public:
- typedef boost::function3<void, OArchiver&, const object&, const unsigned int>
- saver_t;
- typedef boost::function3<void, IArchiver&, object&, const unsigned int>
- loader_t;
- typedef std::map<PyTypeObject*, std::pair<int, saver_t> > savers_t;
- typedef std::map<int, loader_t> loaders_t;
-
- saver_t saver(const object& obj, int& descriptor)
- {
- typename savers_t::iterator pos = savers.find(obj.ptr()->ob_type);
- if (pos != savers.end()) {
- descriptor = pos->second.first;
- return pos->second.second;
- }
- else {
- descriptor = 0;
- return saver_t();
- }
- }
-
- loader_t loader(int descriptor)
- {
- typename loaders_t::iterator pos = loaders.find(descriptor);
- if (pos != loaders.end())
- return pos->second;
- else
- return loader_t();
- }
-
- template<typename T>
- void register_type(const T& value = T(), PyTypeObject* type = 0)
- {
-
-
- if (!type) {
- object obj(value);
- type = obj.ptr()->ob_type;
- }
- register_type(default_saver<T>(), default_loader<T>(type), value, type);
- }
-
- template<typename T>
- void register_type(const saver_t& saver, const loader_t& loader,
- const T& value = T(), PyTypeObject* type = 0)
- {
-
-
- if (!type) {
- object obj(value);
- type = obj.ptr()->ob_type;
- }
- int descriptor = savers.size() + 1;
- if (savers.find(type) != savers.end())
- return;
- savers[type] = std::make_pair(descriptor, saver);
- loaders[descriptor] = loader;
- }
- protected:
- template<typename T>
- struct default_saver {
- void operator()(OArchiver& ar, const object& obj, const unsigned int) {
- T value = extract<T>(obj)();
- ar << value;
- }
- };
- template<typename T>
- struct default_loader {
- default_loader(PyTypeObject* type) : type(type) { }
- void operator()(IArchiver& ar, object& obj, const unsigned int) {
-
- if (!is_fundamental<T>::value && obj && obj.ptr()->ob_type == type) {
- ar >> extract<T&>(obj)();
- } else {
- T value;
- ar >> value;
- obj = object(value);
- }
- }
- private:
- PyTypeObject* type;
- };
- savers_t savers;
- loaders_t loaders;
- };
-
- template<typename IArchiver, typename OArchiver>
- direct_serialization_table<IArchiver, OArchiver>&
- get_direct_serialization_table();
- }
- template<typename IArchiver, typename OArchiver, typename T>
- void
- register_serialized(const T& value = T(), PyTypeObject* type = 0)
- {
- detail::direct_serialization_table<IArchiver, OArchiver>& table =
- detail::get_direct_serialization_table<IArchiver, OArchiver>();
- table.register_type(value, type);
- }
- namespace detail {
- template<typename Archiver>
- void
- save_impl(Archiver& ar, const boost::python::object& obj,
- const unsigned int ,
- mpl::false_ )
- {
- boost::python::str py_string = boost::python::pickle::dumps(obj);
- int len = boost::python::extract<int>(py_string.attr("__len__")());
- const char* string = boost::python::extract<const char*>(py_string);
- ar << len << boost::serialization::make_array(string, len);
- }
- template<typename Archiver>
- void
- save_impl(Archiver& ar, const boost::python::object& obj,
- const unsigned int version,
- mpl::true_ )
- {
- typedef Archiver OArchiver;
- typedef typename input_archiver<OArchiver>::type IArchiver;
- typedef typename direct_serialization_table<IArchiver, OArchiver>::saver_t
- saver_t;
- direct_serialization_table<IArchiver, OArchiver>& table =
- get_direct_serialization_table<IArchiver, OArchiver>();
- int descriptor = 0;
- if (saver_t saver = table.saver(obj, descriptor)) {
- ar << descriptor;
- saver(ar, obj, version);
- } else {
-
- ar << descriptor;
- detail::save_impl(ar, obj, version, mpl::false_());
- }
- }
- template<typename Archiver>
- void
- load_impl(Archiver& ar, boost::python::object& obj,
- const unsigned int ,
- mpl::false_ )
- {
- int len;
- ar >> len;
- std::auto_ptr<char> string(new char[len]);
- ar >> boost::serialization::make_array(string.get(), len);
- boost::python::str py_string(string.get(), len);
- obj = boost::python::pickle::loads(py_string);
- }
- template<typename Archiver>
- void
- load_impl(Archiver& ar, boost::python::object& obj,
- const unsigned int version,
- mpl::true_ )
- {
- typedef Archiver IArchiver;
- typedef typename output_archiver<IArchiver>::type OArchiver;
- typedef typename direct_serialization_table<IArchiver, OArchiver>::loader_t
- loader_t;
- direct_serialization_table<IArchiver, OArchiver>& table =
- get_direct_serialization_table<IArchiver, OArchiver>();
- int descriptor;
- ar >> descriptor;
- if (descriptor) {
- loader_t loader = table.loader(descriptor);
- BOOST_ASSERT(loader);
- loader(ar, obj, version);
- } else {
-
- detail::load_impl(ar, obj, version, mpl::false_());
- }
- }
- }
- template<typename Archiver>
- void
- save(Archiver& ar, const boost::python::object& obj,
- const unsigned int version)
- {
- typedef Archiver OArchiver;
- typedef typename input_archiver<OArchiver>::type IArchiver;
- detail::save_impl(ar, obj, version,
- has_direct_serialization<IArchiver, OArchiver>());
- }
- template<typename Archiver>
- void
- load(Archiver& ar, boost::python::object& obj,
- const unsigned int version)
- {
- typedef Archiver IArchiver;
- typedef typename output_archiver<IArchiver>::type OArchiver;
- detail::load_impl(ar, obj, version,
- has_direct_serialization<IArchiver, OArchiver>());
- }
- template<typename Archive>
- inline void
- serialize(Archive& ar, boost::python::object& obj, const unsigned int version)
- {
- boost::serialization::split_free(ar, obj, version);
- }
- } }
- namespace boost { namespace mpi {
- class packed_iarchive;
- class packed_oarchive;
- } } // end namespace boost::mpi
- BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE(
- ::boost::mpi::packed_iarchive,
- ::boost::mpi::packed_oarchive)
- namespace boost { namespace mpi { namespace python {
- template<typename T>
- void
- register_serialized(const T& value, PyTypeObject* type)
- {
- using boost::python::register_serialized;
- register_serialized<packed_iarchive, packed_oarchive>(value, type);
- }
- } } }
- #endif
|