source: NonGTP/Boost/boost/python/object/pickle_support.hpp @ 857

Revision 857, 3.2 KB checked in by igarcia, 19 years ago (diff)
Line 
1//  (C) Copyright R.W. Grosse-Kunstleve 2002.
2//  Distributed under the Boost Software License, Version 1.0. (See
3//  accompanying file LICENSE_1_0.txt or copy at
4//  http://www.boost.org/LICENSE_1_0.txt)
5#ifndef BOOST_PYTHON_OBJECT_PICKLE_SUPPORT_RWGK20020603_HPP
6# define BOOST_PYTHON_OBJECT_PICKLE_SUPPORT_RWGK20020603_HPP
7
8# include <boost/python/detail/prefix.hpp>
9
10namespace boost { namespace python {
11
12namespace api
13{
14  class object;
15}
16using api::object;
17class tuple;
18
19BOOST_PYTHON_DECL object const& make_instance_reduce_function();
20
21struct pickle_suite;
22
23namespace error_messages {
24
25  template <class T>
26  struct missing_pickle_suite_function_or_incorrect_signature {};
27
28  inline void must_be_derived_from_pickle_suite(pickle_suite const&) {}
29}
30
31namespace detail { struct pickle_suite_registration; }
32
33struct pickle_suite
34{
35  private:
36    struct inaccessible {};
37    friend struct detail::pickle_suite_registration;
38  public:
39    static inaccessible* getinitargs() { return 0; }
40    static inaccessible* getstate() { return 0; }
41    static inaccessible* setstate() { return 0; }
42    static bool getstate_manages_dict() { return false; }
43};
44
45namespace detail {
46
47  struct pickle_suite_registration
48  {
49    typedef pickle_suite::inaccessible inaccessible;
50
51    template <class Class_, class Tgetinitargs>
52    static
53    void
54    register_(
55      Class_& cl,
56      tuple (*getinitargs_fn)(Tgetinitargs),
57      inaccessible* (*getstate_fn)(),
58      inaccessible* (*setstate_fn)(),
59      bool)
60    {
61      cl.enable_pickling_(false);
62      cl.def("__getinitargs__", getinitargs_fn);
63    }
64
65    template <class Class_,
66              class Rgetstate, class Tgetstate,
67              class Tsetstate, class Ttuple>
68    static
69    void
70    register_(
71      Class_& cl,
72      inaccessible* (*getinitargs_fn)(),
73      Rgetstate (*getstate_fn)(Tgetstate),
74      void (*setstate_fn)(Tsetstate, Ttuple),
75      bool getstate_manages_dict)
76    {
77      cl.enable_pickling_(getstate_manages_dict);
78      cl.def("__getstate__", getstate_fn);
79      cl.def("__setstate__", setstate_fn);
80    }
81
82    template <class Class_,
83              class Tgetinitargs,
84              class Rgetstate, class Tgetstate,
85              class Tsetstate, class Ttuple>
86    static
87    void
88    register_(
89      Class_& cl,
90      tuple (*getinitargs_fn)(Tgetinitargs),
91      Rgetstate (*getstate_fn)(Tgetstate),
92      void (*setstate_fn)(Tsetstate, Ttuple),
93      bool getstate_manages_dict)
94    {
95      cl.enable_pickling_(getstate_manages_dict);
96      cl.def("__getinitargs__", getinitargs_fn);
97      cl.def("__getstate__", getstate_fn);
98      cl.def("__setstate__", setstate_fn);
99    }
100
101    template <class Class_>
102    static
103    void
104    register_(
105      Class_&,
106      ...)
107    {
108      typedef typename
109        error_messages::missing_pickle_suite_function_or_incorrect_signature<
110          Class_>::error_type error_type;
111    }
112  };
113
114  template <typename PickleSuiteType>
115  struct pickle_suite_finalize
116  : PickleSuiteType,
117    pickle_suite_registration
118  {};
119
120} // namespace detail
121
122}} // namespace boost::python
123
124#endif // BOOST_PYTHON_OBJECT_PICKLE_SUPPORT_RWGK20020603_HPP
Note: See TracBrowser for help on using the repository browser.