serialize.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // Copyright (C) 2006 Douglas Gregor <doug.gregor -at- gmail.com>
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. /** @file serialize.hpp
  7. *
  8. * This file provides Boost.Serialization support for Python objects
  9. * within Boost.MPI. Python objects can be serialized in one of two
  10. * ways. The default serialization method involves using the Python
  11. * "pickle" module to pickle the Python objects, transmits the
  12. * pickled representation, and unpickles the result when
  13. * received. For C++ types that have been exposed to Python and
  14. * registered with register_serialized(), objects are directly
  15. * serialized for transmissing, skipping the pickling step.
  16. */
  17. #ifndef BOOST_MPI_PYTHON_SERIALIZE_HPP
  18. #define BOOST_MPI_PYTHON_SERIALIZE_HPP
  19. #include <boost/mpi/python/config.hpp>
  20. #include <boost/python/object.hpp>
  21. #include <boost/python/str.hpp>
  22. #include <boost/python/extract.hpp>
  23. #include <map>
  24. #include <boost/function/function3.hpp>
  25. #include <boost/mpl/bool.hpp>
  26. #include <boost/mpl/if.hpp>
  27. #include <boost/serialization/split_free.hpp>
  28. #include <boost/serialization/array.hpp>
  29. #include <boost/serialization/array_wrapper.hpp>
  30. #include <boost/smart_ptr/scoped_array.hpp>
  31. #include <boost/assert.hpp>
  32. #include <boost/type_traits/is_fundamental.hpp>
  33. #define BOOST_MPI_PYTHON_FORWARD_ONLY
  34. #include <boost/mpi/python.hpp>
  35. /************************************************************************
  36. * Boost.Python Serialization Section *
  37. ************************************************************************/
  38. #if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_IS_CONVERTIBLE)
  39. /**
  40. * @brief Declare IArchive and OArchive as a Boost.Serialization
  41. * archives that can be used for Python objects.
  42. *
  43. * This macro can only be expanded from the global namespace. It only
  44. * requires that Archiver be forward-declared. IArchiver and OArchiver
  45. * will only support Serialization of Python objects by pickling
  46. * them. If the Archiver type should also support "direct"
  47. * serialization (for C++ types), use
  48. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE instead.
  49. */
  50. # define BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
  51. namespace boost { namespace python { namespace api { \
  52. template<typename R, typename T> \
  53. struct enable_binary< IArchiver , R, T> {}; \
  54. \
  55. template<typename R, typename T> \
  56. struct enable_binary< OArchiver , R, T> {}; \
  57. } } }
  58. # else
  59. # define BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver)
  60. #endif
  61. /**
  62. * @brief Declare IArchiver and OArchiver as a Boost.Serialization
  63. * archives that can be used for Python objects and C++ objects
  64. * wrapped in Python.
  65. *
  66. * This macro can only be expanded from the global namespace. It only
  67. * requires that IArchiver and OArchiver be forward-declared. However,
  68. * note that you will also need to write
  69. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(IArchiver,
  70. * OArchiver) in one of your translation units.
  71. DPG PICK UP HERE
  72. */
  73. #define BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
  74. BOOST_PYTHON_SERIALIZATION_ARCHIVE(IArchiver, OArchiver) \
  75. namespace boost { namespace python { namespace detail { \
  76. template<> \
  77. BOOST_MPI_PYTHON_DECL direct_serialization_table< IArchiver , OArchiver >& \
  78. get_direct_serialization_table< IArchiver , OArchiver >(); \
  79. } \
  80. \
  81. template<> \
  82. struct has_direct_serialization< IArchiver , OArchiver> : mpl::true_ { }; \
  83. \
  84. template<> \
  85. struct output_archiver< IArchiver > { typedef OArchiver type; }; \
  86. \
  87. template<> \
  88. struct input_archiver< OArchiver > { typedef IArchiver type; }; \
  89. } }
  90. /**
  91. * @brief Define the implementation for Boost.Serialization archivers
  92. * that can be used for Python objects and C++ objects wrapped in
  93. * Python.
  94. *
  95. * This macro can only be expanded from the global namespace. It only
  96. * requires that IArchiver and OArchiver be forward-declared. Before
  97. * using this macro, you will need to declare IArchiver and OArchiver
  98. * as direct serialization archives with
  99. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE(IArchiver, OArchiver).
  100. */
  101. #define BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(IArchiver, OArchiver) \
  102. namespace boost { namespace python { namespace detail { \
  103. template \
  104. class BOOST_MPI_PYTHON_DECL direct_serialization_table< IArchiver , OArchiver >; \
  105. \
  106. template<> \
  107. BOOST_MPI_PYTHON_DECL \
  108. direct_serialization_table< IArchiver , OArchiver >& \
  109. get_direct_serialization_table< IArchiver , OArchiver >( ) \
  110. { \
  111. static direct_serialization_table< IArchiver, OArchiver > table; \
  112. return table; \
  113. } \
  114. } } }
  115. namespace boost { namespace python {
  116. /**
  117. * INTERNAL ONLY
  118. *
  119. * Provides access to the Python "pickle" module from within C++.
  120. */
  121. class BOOST_MPI_PYTHON_DECL pickle {
  122. struct data_t;
  123. public:
  124. static str dumps(object obj, int protocol = -1);
  125. static object loads(str s);
  126. private:
  127. static void initialize_data();
  128. static data_t* data;
  129. };
  130. /**
  131. * @brief Whether the input/output archiver pair has "direct"
  132. * serialization for C++ objects exposed in Python.
  133. *
  134. * Users do not typically need to specialize this trait, as it will be
  135. * specialized as part of the macro
  136. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE.
  137. */
  138. template<typename IArchiver, typename OArchiver>
  139. struct has_direct_serialization : mpl::false_ { };
  140. /**
  141. * @brief A metafunction that determines the output archiver for the
  142. * given input archiver.
  143. *
  144. * Users do not typically need to specialize this trait, as it will be
  145. * specialized as part of the macro
  146. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE.
  147. */
  148. template<typename IArchiver> struct output_archiver { };
  149. /**
  150. * @brief A metafunction that determines the input archiver for the
  151. * given output archiver.
  152. *
  153. * Users do not typically need to specialize this trait, as it will be
  154. * specialized as part of the macro
  155. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE.
  156. *
  157. */
  158. template<typename OArchiver> struct input_archiver { };
  159. namespace detail {
  160. /**
  161. * INTERNAL ONLY
  162. *
  163. * This class contains the direct-serialization code for the given
  164. * IArchiver/OArchiver pair. It is intended to be used as a
  165. * singleton class, and will be accessed when (de-)serializing a
  166. * Boost.Python object with an archiver that supports direct
  167. * serializations. Do not create instances of this class directly:
  168. * instead, use get_direct_serialization_table.
  169. */
  170. template<typename IArchiver, typename OArchiver>
  171. class BOOST_MPI_PYTHON_DECL direct_serialization_table
  172. {
  173. public:
  174. typedef boost::function3<void, OArchiver&, const object&, const unsigned int>
  175. saver_t;
  176. typedef boost::function3<void, IArchiver&, object&, const unsigned int>
  177. loader_t;
  178. typedef std::map<PyTypeObject*, std::pair<int, saver_t> > savers_t;
  179. typedef std::map<int, loader_t> loaders_t;
  180. /**
  181. * Retrieve the saver (serializer) associated with the Python
  182. * object @p obj.
  183. *
  184. * @param obj The object we want to save. Only its (Python) type
  185. * is important.
  186. *
  187. * @param descriptor The value of the descriptor associated to
  188. * the returned saver. Will be set to zero if no saver was found
  189. * for @p obj.
  190. *
  191. * @returns a function object that can be used to serialize this
  192. * object (and other objects of the same type), if possible. If
  193. * no saver can be found, returns an empty function object..
  194. */
  195. saver_t saver(const object& obj, int& descriptor)
  196. {
  197. typename savers_t::iterator pos = savers.find(obj.ptr()->ob_type);
  198. if (pos != savers.end()) {
  199. descriptor = pos->second.first;
  200. return pos->second.second;
  201. }
  202. else {
  203. descriptor = 0;
  204. return saver_t();
  205. }
  206. }
  207. /**
  208. * Retrieve the loader (deserializer) associated with the given
  209. * descriptor.
  210. *
  211. * @param descriptor The descriptor number provided by saver()
  212. * when determining the saver for this type.
  213. *
  214. * @returns a function object that can be used to deserialize an
  215. * object whose type is the same as that corresponding to the
  216. * descriptor. If the descriptor is unknown, the return value
  217. * will be an empty function object.
  218. */
  219. loader_t loader(int descriptor)
  220. {
  221. typename loaders_t::iterator pos = loaders.find(descriptor);
  222. if (pos != loaders.end())
  223. return pos->second;
  224. else
  225. return loader_t();
  226. }
  227. /**
  228. * Register the type T for direct serialization.
  229. *
  230. * @param value A sample value of the type @c T. This may be used
  231. * to compute the Python type associated with the C++ type @c T.
  232. *
  233. * @param type The Python type associated with the C++ type @c
  234. * T. If not provided, it will be computed from the same value @p
  235. * value.
  236. */
  237. template<typename T>
  238. void register_type(const T& value = T(), PyTypeObject* type = 0)
  239. {
  240. // If the user did not provide us with a Python type, figure it
  241. // out for ourselves.
  242. if (!type) {
  243. object obj(value);
  244. type = obj.ptr()->ob_type;
  245. }
  246. register_type(default_saver<T>(), default_loader<T>(type), value, type);
  247. }
  248. /**
  249. * Register the type T for direct serialization.
  250. *
  251. * @param saver A function object that will serialize a
  252. * Boost.Python object (that represents a C++ object of type @c
  253. * T) to an @c OArchive.
  254. *
  255. * @param loader A function object that will deserialize from an
  256. * @c IArchive into a Boost.Python object that represents a C++
  257. * object of type @c T.
  258. *
  259. * @param value A sample value of the type @c T. This may be used
  260. * to compute the Python type associated with the C++ type @c T.
  261. *
  262. * @param type The Python type associated with the C++ type @c
  263. * T. If not provided, it will be computed from the same value @p
  264. * value.
  265. */
  266. template<typename T>
  267. void register_type(const saver_t& saver, const loader_t& loader,
  268. const T& value = T(), PyTypeObject* type = 0)
  269. {
  270. // If the user did not provide us with a Python type, figure it
  271. // out for ourselves.
  272. if (!type) {
  273. object obj(value);
  274. type = obj.ptr()->ob_type;
  275. }
  276. int descriptor = savers.size() + 1;
  277. if (savers.find(type) != savers.end())
  278. return;
  279. savers[type] = std::make_pair(descriptor, saver);
  280. loaders[descriptor] = loader;
  281. }
  282. protected:
  283. template<typename T>
  284. struct default_saver {
  285. void operator()(OArchiver& ar, const object& obj, const unsigned int) {
  286. T value = extract<T>(obj)();
  287. ar << value;
  288. }
  289. };
  290. template<typename T>
  291. struct default_loader {
  292. default_loader(PyTypeObject* type) : type(type) { }
  293. void operator()(IArchiver& ar, object& obj, const unsigned int) {
  294. // If we can, extract the object in place.
  295. if (!is_fundamental<T>::value && obj && obj.ptr()->ob_type == type) {
  296. ar >> extract<T&>(obj)();
  297. } else {
  298. T value;
  299. ar >> value;
  300. obj = object(value);
  301. }
  302. }
  303. private:
  304. PyTypeObject* type;
  305. };
  306. savers_t savers;
  307. loaders_t loaders;
  308. };
  309. /**
  310. * @brief Retrieve the direct-serialization table for an
  311. * IArchiver/OArchiver pair.
  312. *
  313. * This function is responsible for returning a reference to the
  314. * singleton direct-serialization table. Its primary template is
  315. * left undefined, to force the use of an explicit specialization
  316. * with a definition in a single translation unit. Use the macro
  317. * BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL to define this
  318. * explicit specialization.
  319. */
  320. template<typename IArchiver, typename OArchiver>
  321. direct_serialization_table<IArchiver, OArchiver>&
  322. get_direct_serialization_table();
  323. } // end namespace detail
  324. /**
  325. * @brief Register the type T for direct serialization.
  326. *
  327. * The @c register_serialized function registers a C++ type for direct
  328. * serialization with the given @c IArchiver/@c OArchiver pair. Direct
  329. * serialization elides the use of the Python @c pickle package when
  330. * serializing Python objects that represent C++ values. Direct
  331. * serialization can be beneficial both to improve serialization
  332. * performance (Python pickling can be very inefficient) and to permit
  333. * serialization for Python-wrapped C++ objects that do not support
  334. * pickling.
  335. *
  336. * @param value A sample value of the type @c T. This may be used
  337. * to compute the Python type associated with the C++ type @c T.
  338. *
  339. * @param type The Python type associated with the C++ type @c
  340. * T. If not provided, it will be computed from the same value @p
  341. * value.
  342. */
  343. template<typename IArchiver, typename OArchiver, typename T>
  344. void
  345. register_serialized(const T& value = T(), PyTypeObject* type = 0)
  346. {
  347. detail::direct_serialization_table<IArchiver, OArchiver>& table =
  348. detail::get_direct_serialization_table<IArchiver, OArchiver>();
  349. table.register_type(value, type);
  350. }
  351. namespace detail {
  352. /// Save a Python object by pickling it.
  353. template<typename Archiver>
  354. void
  355. save_impl(Archiver& ar, const boost::python::object& obj,
  356. const unsigned int /*version*/,
  357. mpl::false_ /*has_direct_serialization*/)
  358. {
  359. boost::python::str py_string = boost::python::pickle::dumps(obj);
  360. int len = boost::python::extract<int>(py_string.attr("__len__")());
  361. const char* string = boost::python::extract<const char*>(py_string);
  362. ar << len << boost::serialization::make_array(string, len);
  363. }
  364. /// Try to save a Python object by directly serializing it; fall back
  365. /// on pickling if required.
  366. template<typename Archiver>
  367. void
  368. save_impl(Archiver& ar, const boost::python::object& obj,
  369. const unsigned int version,
  370. mpl::true_ /*has_direct_serialization*/)
  371. {
  372. typedef Archiver OArchiver;
  373. typedef typename input_archiver<OArchiver>::type IArchiver;
  374. typedef typename direct_serialization_table<IArchiver, OArchiver>::saver_t
  375. saver_t;
  376. direct_serialization_table<IArchiver, OArchiver>& table =
  377. get_direct_serialization_table<IArchiver, OArchiver>();
  378. int descriptor = 0;
  379. if (saver_t saver = table.saver(obj, descriptor)) {
  380. ar << descriptor;
  381. saver(ar, obj, version);
  382. } else {
  383. // Pickle it
  384. ar << descriptor;
  385. detail::save_impl(ar, obj, version, mpl::false_());
  386. }
  387. }
  388. /// Load a Python object by unpickling it
  389. template<typename Archiver>
  390. void
  391. load_impl(Archiver& ar, boost::python::object& obj,
  392. const unsigned int /*version*/,
  393. mpl::false_ /*has_direct_serialization*/)
  394. {
  395. int len;
  396. ar >> len;
  397. boost::scoped_array<char> string(new char[len]);
  398. ar >> boost::serialization::make_array(string.get(), len);
  399. boost::python::str py_string(string.get(), len);
  400. obj = boost::python::pickle::loads(py_string);
  401. }
  402. /// Try to load a Python object by directly deserializing it; fall back
  403. /// on unpickling if required.
  404. template<typename Archiver>
  405. void
  406. load_impl(Archiver& ar, boost::python::object& obj,
  407. const unsigned int version,
  408. mpl::true_ /*has_direct_serialization*/)
  409. {
  410. typedef Archiver IArchiver;
  411. typedef typename output_archiver<IArchiver>::type OArchiver;
  412. typedef typename direct_serialization_table<IArchiver, OArchiver>::loader_t
  413. loader_t;
  414. direct_serialization_table<IArchiver, OArchiver>& table =
  415. get_direct_serialization_table<IArchiver, OArchiver>();
  416. int descriptor;
  417. ar >> descriptor;
  418. if (descriptor) {
  419. loader_t loader = table.loader(descriptor);
  420. BOOST_ASSERT(loader);
  421. loader(ar, obj, version);
  422. } else {
  423. // Unpickle it
  424. detail::load_impl(ar, obj, version, mpl::false_());
  425. }
  426. }
  427. } // end namespace detail
  428. template<typename Archiver>
  429. void
  430. save(Archiver& ar, const boost::python::object& obj,
  431. const unsigned int version)
  432. {
  433. typedef Archiver OArchiver;
  434. typedef typename input_archiver<OArchiver>::type IArchiver;
  435. detail::save_impl(ar, obj, version,
  436. has_direct_serialization<IArchiver, OArchiver>());
  437. }
  438. template<typename Archiver>
  439. void
  440. load(Archiver& ar, boost::python::object& obj,
  441. const unsigned int version)
  442. {
  443. typedef Archiver IArchiver;
  444. typedef typename output_archiver<IArchiver>::type OArchiver;
  445. detail::load_impl(ar, obj, version,
  446. has_direct_serialization<IArchiver, OArchiver>());
  447. }
  448. template<typename Archive>
  449. inline void
  450. serialize(Archive& ar, boost::python::object& obj, const unsigned int version)
  451. {
  452. boost::serialization::split_free(ar, obj, version);
  453. }
  454. } } // end namespace boost::python
  455. /************************************************************************
  456. * Boost.MPI-Specific Section *
  457. ************************************************************************/
  458. namespace boost { namespace mpi {
  459. class packed_iarchive;
  460. class packed_oarchive;
  461. } } // end namespace boost::mpi
  462. BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE(
  463. ::boost::mpi::packed_iarchive,
  464. ::boost::mpi::packed_oarchive)
  465. namespace boost { namespace mpi { namespace python {
  466. template<typename T>
  467. void
  468. register_serialized(const T& value, PyTypeObject* type)
  469. {
  470. using boost::python::register_serialized;
  471. register_serialized<packed_iarchive, packed_oarchive>(value, type);
  472. }
  473. } } } // end namespace boost::mpi::python
  474. #endif // BOOST_MPI_PYTHON_SERIALIZE_HPP