value_holder.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. # if BOOST_WORKAROUND(__GNUC__, == 2)
  26. # define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) (typename unforward<A##n>::type)objects::do_unforward(a##n,0)
  27. # else
  28. # define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) objects::do_unforward(a##n,0)
  29. # endif
  30. template <class Value>
  31. struct value_holder : instance_holder
  32. {
  33. typedef Value held_type;
  34. typedef Value value_type;
  35. // Forward construction to the held object
  36. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 1))
  37. # include BOOST_PP_ITERATE()
  38. private: // required holder implementation
  39. void* holds(type_info, bool null_ptr_only);
  40. template <class T>
  41. inline void* holds_wrapped(type_info dst_t, wrapper<T>*,T* p)
  42. {
  43. return python::type_id<T>() == dst_t ? p : 0;
  44. }
  45. inline void* holds_wrapped(type_info, ...)
  46. {
  47. return 0;
  48. }
  49. private: // data members
  50. Value m_held;
  51. };
  52. template <class Value, class Held>
  53. struct value_holder_back_reference : instance_holder
  54. {
  55. typedef Held held_type;
  56. typedef Value value_type;
  57. // Forward construction to the held object
  58. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 2))
  59. # include BOOST_PP_ITERATE()
  60. private: // required holder implementation
  61. void* holds(type_info, bool null_ptr_only);
  62. private: // data members
  63. Held m_held;
  64. };
  65. # undef BOOST_PYTHON_UNFORWARD_LOCAL
  66. template <class Value>
  67. void* value_holder<Value>::holds(type_info dst_t, bool /*null_ptr_only*/)
  68. {
  69. if (void* wrapped = holds_wrapped(dst_t, boost::addressof(m_held), boost::addressof(m_held)))
  70. return wrapped;
  71. type_info src_t = python::type_id<Value>();
  72. return src_t == dst_t ? boost::addressof(m_held)
  73. : find_static_type(boost::addressof(m_held), src_t, dst_t);
  74. }
  75. template <class Value, class Held>
  76. void* value_holder_back_reference<Value,Held>::holds(
  77. type_info dst_t, bool /*null_ptr_only*/)
  78. {
  79. type_info src_t = python::type_id<Value>();
  80. Value* x = &m_held;
  81. if (dst_t == src_t)
  82. return x;
  83. else if (dst_t == python::type_id<Held>())
  84. return &m_held;
  85. else
  86. return find_static_type(x, src_t, dst_t);
  87. }
  88. }}} // namespace boost::python::objects
  89. # endif // VALUE_HOLDER_DWA20011215_HPP
  90. // --------------- value_holder ---------------
  91. // For gcc 4.4 compatability, we must include the
  92. // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
  93. #else // BOOST_PP_IS_ITERATING
  94. #if BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 1
  95. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  96. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  97. # line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder))
  98. # endif
  99. # define N BOOST_PP_ITERATION()
  100. # if (N != 0)
  101. template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
  102. # endif
  103. value_holder(
  104. PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  105. : m_held(
  106. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  107. )
  108. {
  109. python::detail::initialize_wrapper(self, boost::addressof(this->m_held));
  110. }
  111. # undef N
  112. // --------------- value_holder_back_reference ---------------
  113. #elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 2
  114. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  115. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  116. # line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder_back_reference))
  117. # endif
  118. # define N BOOST_PP_ITERATION()
  119. # if (N != 0)
  120. template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
  121. # endif
  122. value_holder_back_reference(
  123. PyObject* p BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  124. : m_held(
  125. p BOOST_PP_COMMA_IF(N)
  126. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  127. )
  128. {
  129. }
  130. # undef N
  131. #endif // BOOST_PP_ITERATION_DEPTH()
  132. #endif