1 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
2 
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 //  Authors: Douglas Gregor
8 
9 /** @file serialize.cpp
10  *
11  *  This file provides Boost.Serialization support for Python objects.
12  */
13 #include <boost/mpi/python/serialize.hpp>
14 #include <boost/mpi/python/skeleton_and_content.hpp>
15 #include <boost/mpi.hpp>
16 
17 namespace boost { namespace python {
18 
19 struct pickle::data_t {
20   object module;
21   object dumps;
22   object loads;
23 };
24 
25 
26 /// Data used for communicating with the Python `pickle' module.
27 pickle::data_t* pickle::data;
28 
dumps(object obj,int protocol)29 str pickle::dumps(object obj, int protocol)
30 {
31   if (!data) initialize_data();
32   return extract<str>((data->dumps)(obj, protocol));
33 }
34 
loads(str s)35 object pickle::loads(str s)
36 {
37   if (!data) initialize_data();
38   return ((data->loads)(s));
39 }
40 
initialize_data()41 void pickle::initialize_data()
42 {
43   data = new data_t;
44   data->module = object(handle<>(PyImport_ImportModule("pickle")));
45   data->dumps = data->module.attr("dumps");
46   data->loads = data->module.attr("loads");
47 }
48 
49 } } // end namespace boost::python
50 
51 BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(
52   ::boost::mpi::packed_iarchive,
53   ::boost::mpi::packed_oarchive)
54 
55 namespace boost { namespace mpi { namespace python { namespace detail {
56 
57   boost::python::object skeleton_proxy_base_type;
58 
59   // A map from Python type objects to skeleton/content handlers
60   typedef std::map<PyTypeObject*, skeleton_content_handler>
61     skeleton_content_handlers_type;
62 
63   BOOST_MPI_PYTHON_DECL skeleton_content_handlers_type skeleton_content_handlers;
64 
65   bool
skeleton_and_content_handler_registered(PyTypeObject * type)66   skeleton_and_content_handler_registered(PyTypeObject* type)
67   {
68     return
69       skeleton_content_handlers.find(type) != skeleton_content_handlers.end();
70   }
71 
72   void
register_skeleton_and_content_handler(PyTypeObject * type,const skeleton_content_handler & handler)73   register_skeleton_and_content_handler(PyTypeObject* type,
74                                         const skeleton_content_handler& handler)
75   {
76     skeleton_content_handlers[type] = handler;
77   }
78 
79 } } } } // end namespace boost::mpi::python::detail
80