summaryrefslogtreecommitdiff
path: root/libs/mpi/src/python/serialize.cpp
blob: 92004a3405a52bff02d8c1605f8c2f8beecb196c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// (C) Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Authors: Douglas Gregor

/** @file serialize.cpp
 *
 *  This file provides Boost.Serialization support for Python objects.
 */
#include <boost/mpi/python/serialize.hpp>
#include <boost/mpi/python/skeleton_and_content.hpp>
#include <boost/mpi.hpp>

namespace boost { namespace python {

struct pickle::data_t {
  object module;
  object dumps;
  object loads;
};


/// Data used for communicating with the Python `pickle' module.
pickle::data_t* pickle::data;

str pickle::dumps(object obj, int protocol)
{
  if (!data) initialize_data();
  return extract<str>((data->dumps)(obj, protocol));
}

object pickle::loads(str s)
{
  if (!data) initialize_data();
  return ((data->loads)(s));
}

void pickle::initialize_data()
{
  data = new data_t;
  data->module = object(handle<>(PyImport_ImportModule("pickle")));
  data->dumps = data->module.attr("dumps");
  data->loads = data->module.attr("loads");
}

} } // end namespace boost::python

BOOST_PYTHON_DIRECT_SERIALIZATION_ARCHIVE_IMPL(
  ::boost::mpi::packed_iarchive,
  ::boost::mpi::packed_oarchive)

namespace boost { namespace mpi { namespace python { namespace detail {

  boost::python::object skeleton_proxy_base_type; 

  // A map from Python type objects to skeleton/content handlers
  typedef std::map<PyTypeObject*, skeleton_content_handler>
    skeleton_content_handlers_type;

  BOOST_MPI_PYTHON_DECL skeleton_content_handlers_type skeleton_content_handlers;

  bool
  skeleton_and_content_handler_registered(PyTypeObject* type)
  {
    return 
      skeleton_content_handlers.find(type) != skeleton_content_handlers.end();
  }

  void 
  register_skeleton_and_content_handler(PyTypeObject* type, 
                                        const skeleton_content_handler& handler)
  {
    skeleton_content_handlers[type] = handler;
  }

} } } } // end namespace boost::mpi::python::detail