1 #if !defined(BOOST_PP_IS_ITERATING)
3 // Copyright David Abrahams 2001.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 # ifndef POINTER_HOLDER_DWA20011215_HPP
9 # define POINTER_HOLDER_DWA20011215_HPP
11 # include <boost/get_pointer.hpp>
12 # include <boost/type.hpp>
14 # include <boost/python/instance_holder.hpp>
15 # include <boost/python/object/inheritance_query.hpp>
16 # include <boost/python/object/forward.hpp>
18 # include <boost/python/pointee.hpp>
19 # include <boost/python/type_id.hpp>
21 # include <boost/python/detail/wrapper_base.hpp>
22 # include <boost/python/detail/force_instantiate.hpp>
23 # include <boost/python/detail/preprocessor.hpp>
26 # include <boost/mpl/if.hpp>
27 # include <boost/mpl/apply.hpp>
29 # include <boost/preprocessor/comma_if.hpp>
30 # include <boost/preprocessor/iterate.hpp>
31 # include <boost/preprocessor/repeat.hpp>
32 # include <boost/preprocessor/debug/line.hpp>
33 # include <boost/preprocessor/enum_params.hpp>
34 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
36 # include <boost/detail/workaround.hpp>
38 # include <boost/type_traits/remove_const.hpp>
40 namespace boost { namespace python {
42 template <class T> class wrapper;
47 namespace boost { namespace python { namespace objects {
49 #define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) objects::do_unforward(a##n,0)
51 template <class Pointer, class Value>
52 struct pointer_holder : instance_holder
54 typedef Value value_type;
56 pointer_holder(Pointer);
58 // Forward construction to the held object
60 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/pointer_holder.hpp>, 1))
61 # include BOOST_PP_ITERATE()
65 private: // required holder implementation
66 void* holds(type_info, bool null_ptr_only);
69 inline void* holds_wrapped(type_info dst_t, wrapper<T>*,T* p)
71 return python::type_id<T>() == dst_t ? p : 0;
74 inline void* holds_wrapped(type_info, ...)
79 private: // data members
83 template <class Pointer, class Value>
84 struct pointer_holder_back_reference : instance_holder
87 typedef typename python::pointee<Pointer>::type held_type;
89 typedef Value value_type;
91 // Not sure about this one -- can it work? The source object
92 // undoubtedly does not carry the correct back reference pointer.
93 pointer_holder_back_reference(Pointer);
95 // Forward construction to the held object
96 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/pointer_holder.hpp>, 2))
97 # include BOOST_PP_ITERATE()
99 private: // required holder implementation
100 void* holds(type_info, bool null_ptr_only);
102 private: // data members
106 # undef BOOST_PYTHON_UNFORWARD_LOCAL
108 template <class Pointer, class Value>
109 inline pointer_holder<Pointer,Value>::pointer_holder(Pointer p)
110 #if __cplusplus < 201103L
118 template <class Pointer, class Value>
119 inline pointer_holder_back_reference<Pointer,Value>::pointer_holder_back_reference(Pointer p)
120 #if __cplusplus < 201103L
128 template <class Pointer, class Value>
129 void* pointer_holder<Pointer, Value>::holds(type_info dst_t, bool null_ptr_only)
131 typedef typename boost::remove_const< Value >::type non_const_value;
133 if (dst_t == python::type_id<Pointer>()
134 && !(null_ptr_only && get_pointer(this->m_p))
139 # if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
140 = static_cast<Value*>( get_pointer(this->m_p) )
142 = get_pointer(this->m_p)
145 non_const_value* p = const_cast<non_const_value*>( p0 );
150 if (void* wrapped = holds_wrapped(dst_t, p, p))
153 type_info src_t = python::type_id<non_const_value>();
154 return src_t == dst_t ? p : find_dynamic_type(p, src_t, dst_t);
157 template <class Pointer, class Value>
158 void* pointer_holder_back_reference<Pointer, Value>::holds(type_info dst_t, bool null_ptr_only)
160 if (dst_t == python::type_id<Pointer>()
161 && !(null_ptr_only && get_pointer(this->m_p))
165 if (!get_pointer(this->m_p))
168 Value* p = get_pointer(m_p);
170 if (dst_t == python::type_id<held_type>())
173 type_info src_t = python::type_id<Value>();
174 return src_t == dst_t ? p : find_dynamic_type(p, src_t, dst_t);
177 }}} // namespace boost::python::objects
179 # endif // POINTER_HOLDER_DWA20011215_HPP
181 /* --------------- pointer_holder --------------- */
182 // For gcc 4.4 compatability, we must include the
183 // BOOST_PP_ITERATION_DEPTH test inside an #else clause.
184 #else // BOOST_PP_IS_ITERATING
185 #if BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 1
186 # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
187 && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
188 # line BOOST_PP_LINE(__LINE__, pointer_holder.hpp)
191 # define N BOOST_PP_ITERATION()
194 template< BOOST_PP_ENUM_PARAMS_Z(1, N, class A) >
196 pointer_holder(PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
198 BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
201 python::detail::initialize_wrapper(self, get_pointer(this->m_p));
206 /* --------------- pointer_holder_back_reference --------------- */
207 #elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 2
208 # if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100) \
209 && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
210 # line BOOST_PP_LINE(__LINE__, pointer_holder.hpp(pointer_holder_back_reference))
213 # define N BOOST_PP_ITERATION()
216 template < BOOST_PP_ENUM_PARAMS_Z(1, N, class A) >
218 pointer_holder_back_reference(
219 PyObject* p BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
221 p BOOST_PP_COMMA_IF(N) BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
227 #endif // BOOST_PP_ITERATION_DEPTH()