1 // (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>
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)
7 // Authors: Douglas Gregor
9 /** @file serialize.cpp
11 * This file provides Boost.Serialization support for Python objects.
13 #include <boost/mpi/python/serialize.hpp>
14 #include <boost/mpi/python/skeleton_and_content.hpp>
15 #include <boost/mpi.hpp>
17 namespace boost
{ namespace python
{
19 struct pickle::data_t
{
26 /// Data used for communicating with the Python `pickle' module.
27 pickle::data_t
* pickle::data
;
29 str
pickle::dumps(object obj
, int protocol
)
31 if (!data
) initialize_data();
32 return extract
<str
>((data
->dumps
)(obj
, protocol
));
35 object
pickle::loads(str s
)
37 if (!data
) initialize_data();
38 return ((data
->loads
)(s
));
41 void pickle::initialize_data()
44 data
->module
= object(handle
<>(PyImport_ImportModule("pickle")));
45 data
->dumps
= data
->module
.attr("dumps");
46 data
->loads
= data
->module
.attr("loads");
49 } } // end namespace boost::python
51 BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(
52 ::boost::mpi::packed_iarchive
,
53 ::boost::mpi::packed_oarchive
)
55 namespace boost
{ namespace mpi
{ namespace python
{ namespace detail
{
57 boost::python::object skeleton_proxy_base_type
;
59 // A map from Python type objects to skeleton/content handlers
60 typedef std::map
<PyTypeObject
*, skeleton_content_handler
>
61 skeleton_content_handlers_type
;
63 BOOST_MPI_PYTHON_DECL skeleton_content_handlers_type skeleton_content_handlers
;
66 skeleton_and_content_handler_registered(PyTypeObject
* type
)
69 skeleton_content_handlers
.find(type
) != skeleton_content_handlers
.end();
73 register_skeleton_and_content_handler(PyTypeObject
* type
,
74 const skeleton_content_handler
& handler
)
76 skeleton_content_handlers
[type
] = handler
;
79 } } } } // end namespace boost::mpi::python::detail