pointer_holder.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 POINTER_HOLDER_DWA20011215_HPP
  7. # define POINTER_HOLDER_DWA20011215_HPP
  8. # include <boost/get_pointer.hpp>
  9. # include <boost/type.hpp>
  10. # include <boost/python/instance_holder.hpp>
  11. # include <boost/python/object/inheritance_query.hpp>
  12. # include <boost/python/object/forward.hpp>
  13. # include <boost/python/pointee.hpp>
  14. # include <boost/python/type_id.hpp>
  15. # include <boost/python/detail/wrapper_base.hpp>
  16. # include <boost/python/detail/force_instantiate.hpp>
  17. # include <boost/python/detail/preprocessor.hpp>
  18. # include <boost/python/detail/type_traits.hpp>
  19. # include <boost/mpl/if.hpp>
  20. # include <boost/mpl/apply.hpp>
  21. # include <boost/preprocessor/comma_if.hpp>
  22. # include <boost/preprocessor/iterate.hpp>
  23. # include <boost/preprocessor/repeat.hpp>
  24. # include <boost/preprocessor/debug/line.hpp>
  25. # include <boost/preprocessor/enum_params.hpp>
  26. # include <boost/preprocessor/repetition/enum_binary_params.hpp>
  27. # include <boost/detail/workaround.hpp>
  28. namespace boost { namespace python {
  29. template <class T> class wrapper;
  30. }}
  31. namespace boost { namespace python { namespace objects {
  32. #define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) objects::do_unforward(a##n,0)
  33. template <class Pointer, class Value>
  34. struct pointer_holder : instance_holder
  35. {
  36. typedef Value value_type;
  37. pointer_holder(Pointer);
  38. // Forward construction to the held object
  39. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/pointer_holder.hpp>, 1))
  40. # include BOOST_PP_ITERATE()
  41. private: // types
  42. private: // required holder implementation
  43. void* holds(type_info, bool null_ptr_only);
  44. template <class T>
  45. inline void* holds_wrapped(type_info dst_t, wrapper<T>*,T* p)
  46. {
  47. return python::type_id<T>() == dst_t ? p : 0;
  48. }
  49. inline void* holds_wrapped(type_info, ...)
  50. {
  51. return 0;
  52. }
  53. private: // data members
  54. Pointer m_p;
  55. };
  56. template <class Pointer, class Value>
  57. struct pointer_holder_back_reference : instance_holder
  58. {
  59. private:
  60. typedef typename python::pointee<Pointer>::type held_type;
  61. public:
  62. typedef Value value_type;
  63. // Not sure about this one -- can it work? The source object
  64. // undoubtedly does not carry the correct back reference pointer.
  65. pointer_holder_back_reference(Pointer);
  66. // Forward construction to the held object
  67. # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/pointer_holder.hpp>, 2))
  68. # include BOOST_PP_ITERATE()
  69. private: // required holder implementation
  70. void* holds(type_info, bool null_ptr_only);
  71. private: // data members
  72. Pointer m_p;
  73. };
  74. # undef BOOST_PYTHON_UNFORWARD_LOCAL
  75. template <class Pointer, class Value>
  76. inline pointer_holder<Pointer,Value>::pointer_holder(Pointer p)
  77. #if defined(BOOST_NO_CXX11_SMART_PTR)
  78. : m_p(p)
  79. #else
  80. : m_p(std::move(p))
  81. #endif
  82. {
  83. }
  84. template <class Pointer, class Value>
  85. inline pointer_holder_back_reference<Pointer,Value>::pointer_holder_back_reference(Pointer p)
  86. #if defined(BOOST_NO_CXX11_SMART_PTR)
  87. : m_p(p)
  88. #else
  89. : m_p(std::move(p))
  90. #endif
  91. {
  92. }
  93. template <class Pointer, class Value>
  94. void* pointer_holder<Pointer, Value>::holds(type_info dst_t, bool null_ptr_only)
  95. {
  96. typedef typename boost::python::detail::remove_const< Value >::type non_const_value;
  97. if (dst_t == python::type_id<Pointer>()
  98. && !(null_ptr_only && get_pointer(this->m_p))
  99. )
  100. return &this->m_p;
  101. Value* p0
  102. # if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  103. = static_cast<Value*>( get_pointer(this->m_p) )
  104. # else
  105. = get_pointer(this->m_p)
  106. # endif
  107. ;
  108. non_const_value* p = const_cast<non_const_value*>( p0 );
  109. if (p == 0)
  110. return 0;
  111. if (void* wrapped = holds_wrapped(dst_t, p, p))
  112. return wrapped;
  113. type_info src_t = python::type_id<non_const_value>();
  114. return src_t == dst_t ? p : find_dynamic_type(p, src_t, dst_t);
  115. }
  116. template <class Pointer, class Value>
  117. void* pointer_holder_back_reference<Pointer, Value>::holds(type_info dst_t, bool null_ptr_only)
  118. {
  119. if (dst_t == python::type_id<Pointer>()
  120. && !(null_ptr_only && get_pointer(this->m_p))
  121. )
  122. return &this->m_p;
  123. if (!get_pointer(this->m_p))
  124. return 0;
  125. Value* p = get_pointer(m_p);
  126. if (dst_t == python::type_id<held_type>())
  127. return p;
  128. type_info src_t = python::type_id<Value>();
  129. return src_t == dst_t ? p : find_dynamic_type(p, src_t, dst_t);
  130. }
  131. }}} // namespace boost::python::objects
  132. # endif // POINTER_HOLDER_DWA20011215_HPP
  133. /* --------------- pointer_holder --------------- */
  134. // For gcc 4.4 compatability, we must include the
  135. // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
  136. #else // BOOST_PP_IS_ITERATING
  137. #if BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 1
  138. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  139. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  140. # line BOOST_PP_LINE(__LINE__, pointer_holder.hpp)
  141. # endif
  142. # define N BOOST_PP_ITERATION()
  143. # if (N != 0)
  144. template< BOOST_PP_ENUM_PARAMS_Z(1, N, class A) >
  145. # endif
  146. pointer_holder(PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  147. : m_p(new Value(
  148. BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  149. ))
  150. {
  151. python::detail::initialize_wrapper(self, get_pointer(this->m_p));
  152. }
  153. # undef N
  154. /* --------------- pointer_holder_back_reference --------------- */
  155. #elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 2
  156. # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
  157. && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
  158. # line BOOST_PP_LINE(__LINE__, pointer_holder.hpp(pointer_holder_back_reference))
  159. # endif
  160. # define N BOOST_PP_ITERATION()
  161. # if (N != 0)
  162. template < BOOST_PP_ENUM_PARAMS_Z(1, N, class A) >
  163. # endif
  164. pointer_holder_back_reference(
  165. PyObject* p BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
  166. : m_p(new held_type(
  167. p BOOST_PP_COMMA_IF(N) BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
  168. ))
  169. {}
  170. # undef N
  171. #endif // BOOST_PP_ITERATION_DEPTH()
  172. #endif