value_holder.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if !defined(BOOST_PP_IS_ITERATING)
  2. // Copyright David Abrahams 2001.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. # ifndef VALUE_HOLDER_DWA20011215_HPP
  7. # define VALUE_HOLDER_DWA20011215_HPP
  8. # include <boost/python/object/value_holder_fwd.hpp>
  9. # include <boost/python/instance_holder.hpp>
  10. # include <boost/python/type_id.hpp>
  11. # include <boost/python/wrapper.hpp>
  12. # include <boost/python/object/inheritance_query.hpp>
  13. # include <boost/python/object/forward.hpp>
  14. # include <boost/python/detail/force_instantiate.hpp>
  15. # include <boost/python/detail/preprocessor.hpp>
  16. # include <boost/preprocessor/comma_if.hpp>
  17. # include <boost/preprocessor/enum_params.hpp>
  18. # include <boost/preprocessor/iterate.hpp>
  19. # include <boost/preprocessor/repeat.hpp>
  20. # include <boost/preprocessor/debug/line.hpp>
  21. # include <boost/preprocessor/repetition/enum_params.hpp>
  22. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  23. # include <boost/utility/addressof.hpp>
  24. namespace boost { namespace python { namespace objects {
  25. #define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) objects::do_unforward(a##n,0)
  26. template <class Value>
  27. struct value_holder : instance_holder
  28. {
  29. typedef Value held_type;
  30. typedef Value value_type;
  31. // Forward construction to the held object
  32. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 1))
  33. # include BOOST_PP_ITERATE()
  34. private: // required holder implementation
  35. void* holds(type_info, bool null_ptr_only);
  36. template <class T>
  37. inline void* holds_wrapped(type_info dst_t, wrapper<T>*,T* p)
  38. {
  39. return python::type_id<T>() == dst_t ? p : 0;
  40. }
  41. inline void* holds_wrapped(type_info, ...)
  42. {
  43. return 0;
  44. }
  45. private: // data members
  46. Value m_held;
  47. };
  48. template <class Value, class Held>
  49. struct value_holder_back_reference : instance_holder
  50. {
  51. typedef Held held_type;
  52. typedef Value value_type;
  53. // Forward construction to the held object
  54. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 2))
  55. # include BOOST_PP_ITERATE()
  56. private: // required holder implementation
  57. void* holds(type_info, bool null_ptr_only);
  58. private: // data members
  59. Held m_held;
  60. };
  61. # undef BOOST_PYTHON_UNFORWARD_LOCAL
  62. template <class Value>
  63. void* value_holder<Value>::holds(type_info dst_t, bool /*null_ptr_only*/)
  64. {
  65. if (void* wrapped = holds_wrapped(dst_t, boost::addressof(m_held), boost::addressof(m_held)))
  66. return wrapped;
  67. type_info src_t = python::type_id<Value>();
  68. return src_t == dst_t ? boost::addressof(m_held)
  69. : find_static_type(boost::addressof(m_held), src_t, dst_t);
  70. }
  71. template <class Value, class Held>
  72. void* value_holder_back_reference<Value,Held>::holds(
  73. type_info dst_t, bool /*null_ptr_only*/)
  74. {
  75. type_info src_t = python::type_id<Value>();
  76. Value* x = &m_held;
  77. if (dst_t == src_t)
  78. return x;
  79. else if (dst_t == python::type_id<Held>())
  80. return &m_held;
  81. else
  82. return find_static_type(x, src_t, dst_t);
  83. }
  84. }}} // namespace boost::python::objects
  85. # endif // VALUE_HOLDER_DWA20011215_HPP
  86. // --------------- value_holder ---------------
  87. // For gcc 4.4 compatability, we must include the
  88. // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
  89. #else // BOOST_PP_IS_ITERATING
  90. #if BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 1
  91. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  92. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  93. # line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder))
  94. # endif
  95. # define N BOOST_PP_ITERATION()
  96. # if (N != 0)
  97. template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
  98. # endif
  99. value_holder(
  100. PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  101. : m_held(
  102. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  103. )
  104. {
  105. python::detail::initialize_wrapper(self, boost::addressof(this->m_held));
  106. }
  107. # undef N
  108. // --------------- value_holder_back_reference ---------------
  109. #elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 2
  110. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  111. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  112. # line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder_back_reference))
  113. # endif
  114. # define N BOOST_PP_ITERATION()
  115. # if (N != 0)
  116. template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
  117. # endif
  118. value_holder_back_reference(
  119. PyObject* p BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  120. : m_held(
  121. p BOOST_PP_COMMA_IF(N)
  122. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  123. )
  124. {
  125. }
  126. # undef N
  127. #endif // BOOST_PP_ITERATION_DEPTH()
  128. #endif