pickle_support.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # include <boost/python/detail/prefix.hpp>
  8. namespace boost { namespace python {
  9. namespace api
  10. {
  11. class object;
  12. }
  13. using api::object;
  14. class tuple;
  15. BOOST_PYTHON_DECL object const& make_instance_reduce_function();
  16. struct pickle_suite;
  17. namespace error_messages {
  18. template <class T>
  19. struct missing_pickle_suite_function_or_incorrect_signature {};
  20. inline void must_be_derived_from_pickle_suite(pickle_suite const&) {}
  21. }
  22. namespace detail { struct pickle_suite_registration; }
  23. struct pickle_suite
  24. {
  25. private:
  26. struct inaccessible {};
  27. friend struct detail::pickle_suite_registration;
  28. public:
  29. static inaccessible* getinitargs() { return 0; }
  30. static inaccessible* getstate() { return 0; }
  31. static inaccessible* setstate() { return 0; }
  32. static bool getstate_manages_dict() { return false; }
  33. };
  34. namespace detail {
  35. struct pickle_suite_registration
  36. {
  37. typedef pickle_suite::inaccessible inaccessible;
  38. template <class Class_, class Tgetinitargs>
  39. static
  40. void
  41. register_(
  42. Class_& cl,
  43. tuple (*getinitargs_fn)(Tgetinitargs),
  44. inaccessible* (* /*getstate_fn*/)(),
  45. inaccessible* (* /*setstate_fn*/)(),
  46. bool)
  47. {
  48. cl.enable_pickling_(false);
  49. cl.def("__getinitargs__", getinitargs_fn);
  50. }
  51. template <class Class_,
  52. class Rgetstate, class Tgetstate,
  53. class Tsetstate, class Ttuple>
  54. static
  55. void
  56. register_(
  57. Class_& cl,
  58. inaccessible* (* /*getinitargs_fn*/)(),
  59. Rgetstate (*getstate_fn)(Tgetstate),
  60. void (*setstate_fn)(Tsetstate, Ttuple),
  61. bool getstate_manages_dict)
  62. {
  63. cl.enable_pickling_(getstate_manages_dict);
  64. cl.def("__getstate__", getstate_fn);
  65. cl.def("__setstate__", setstate_fn);
  66. }
  67. template <class Class_,
  68. class Tgetinitargs,
  69. class Rgetstate, class Tgetstate,
  70. class Tsetstate, class Ttuple>
  71. static
  72. void
  73. register_(
  74. Class_& cl,
  75. tuple (*getinitargs_fn)(Tgetinitargs),
  76. Rgetstate (*getstate_fn)(Tgetstate),
  77. void (*setstate_fn)(Tsetstate, Ttuple),
  78. bool getstate_manages_dict)
  79. {
  80. cl.enable_pickling_(getstate_manages_dict);
  81. cl.def("__getinitargs__", getinitargs_fn);
  82. cl.def("__getstate__", getstate_fn);
  83. cl.def("__setstate__", setstate_fn);
  84. }
  85. template <class Class_>
  86. static
  87. void
  88. register_(
  89. Class_&,
  90. ...)
  91. {
  92. typedef typename
  93. error_messages::missing_pickle_suite_function_or_incorrect_signature<
  94. Class_>::error_type error_type BOOST_ATTRIBUTE_UNUSED;
  95. }
  96. };
  97. template <typename PickleSuiteType>
  98. struct pickle_suite_finalize
  99. : PickleSuiteType,
  100. pickle_suite_registration
  101. {};
  102. } // namespace detail
  103. }} // namespace boost::python
  104. #endif // BOOST_PYTHON_OBJECT_PICKLE_SUPPORT_RWGK20020603_HPP