source: NonGTP/Boost/boost/python/object/value_holder.hpp @ 857

Revision 857, 5.1 KB checked in by igarcia, 19 years ago (diff)
Line 
1#if !defined(BOOST_PP_IS_ITERATING)
2
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)
7
8# ifndef VALUE_HOLDER_DWA20011215_HPP
9#  define VALUE_HOLDER_DWA20011215_HPP
10
11#  include <boost/python/object/value_holder_fwd.hpp>
12
13#  include <boost/python/instance_holder.hpp>
14#  include <boost/python/type_id.hpp>
15#  include <boost/python/wrapper.hpp>
16
17#  include <boost/python/object/inheritance_query.hpp>
18#  include <boost/python/object/forward.hpp>
19
20#  include <boost/python/detail/force_instantiate.hpp>
21#  include <boost/python/detail/preprocessor.hpp>
22
23#  include <boost/preprocessor/comma_if.hpp>
24#  include <boost/preprocessor/enum_params.hpp>
25#  include <boost/preprocessor/iterate.hpp>
26#  include <boost/preprocessor/repeat.hpp>
27#  include <boost/preprocessor/debug/line.hpp>
28
29#  include <boost/preprocessor/repetition/enum_params.hpp>
30#  include <boost/preprocessor/repetition/enum_binary_params.hpp>
31
32#  include <boost/utility/addressof.hpp>
33
34namespace boost { namespace python { namespace objects {
35
36#  if BOOST_WORKAROUND(__GNUC__, == 2)
37#   define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) (typename unforward<A##n>::type)objects::do_unforward(a##n,0)
38#  else
39#   define BOOST_PYTHON_UNFORWARD_LOCAL(z, n, _) BOOST_PP_COMMA_IF(n) objects::do_unforward(a##n,0)
40#  endif
41
42template <class Value>
43struct value_holder : instance_holder
44{
45    typedef Value held_type;
46    typedef Value value_type;
47
48    // Forward construction to the held object
49#  define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 1))
50#  include BOOST_PP_ITERATE()
51
52 private: // required holder implementation
53    void* holds(type_info, bool null_ptr_only);
54   
55    template <class T>
56    inline void* holds_wrapped(type_info dst_t, wrapper<T>*,T* p)
57    {
58        return python::type_id<T>() == dst_t ? p : 0;
59    }
60   
61    inline void* holds_wrapped(type_info, ...)
62    {
63        return 0;
64    }
65 private: // data members
66    Value m_held;
67};
68
69template <class Value, class Held>
70struct value_holder_back_reference : instance_holder
71{
72    typedef Held held_type;
73    typedef Value value_type;
74   
75    // Forward construction to the held object
76#  define BOOST_PP_ITERATION_PARAMS_1 (4, (0, BOOST_PYTHON_MAX_ARITY, <boost/python/object/value_holder.hpp>, 2))
77#  include BOOST_PP_ITERATE()
78
79private: // required holder implementation
80    void* holds(type_info, bool null_ptr_only);
81
82 private: // data members
83    Held m_held;
84};
85
86#  undef BOOST_PYTHON_UNFORWARD_LOCAL
87
88template <class Value>
89void* value_holder<Value>::holds(type_info dst_t, bool null_ptr_only)
90{
91    if (void* wrapped = holds_wrapped(dst_t, boost::addressof(m_held), boost::addressof(m_held)))
92        return wrapped;
93   
94    type_info src_t = python::type_id<Value>();
95    return src_t == dst_t ? boost::addressof(m_held)
96        : find_static_type(boost::addressof(m_held), src_t, dst_t);
97}
98
99template <class Value, class Held>
100void* value_holder_back_reference<Value,Held>::holds(
101    type_info dst_t, bool null_ptr_only)
102{
103    type_info src_t = python::type_id<Value>();
104    Value* x = &m_held;
105   
106    if (dst_t == src_t)
107        return x;
108    else if (dst_t == python::type_id<Held>())
109        return &m_held;
110    else
111        return find_static_type(x, src_t, dst_t);
112}
113
114}}} // namespace boost::python::objects
115
116# endif // VALUE_HOLDER_DWA20011215_HPP
117
118// --------------- value_holder ---------------
119
120#elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 1
121# if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100)                      \
122        && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
123#  line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder))
124# endif
125
126# define N BOOST_PP_ITERATION()
127
128# if (N != 0)
129    template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
130# endif
131    value_holder(
132      PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
133        : m_held(
134            BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
135            )
136    {
137        python::detail::initialize_wrapper(self, boost::addressof(this->m_held));
138    }
139
140# undef N
141
142// --------------- value_holder_back_reference ---------------
143
144#elif BOOST_PP_ITERATION_DEPTH() == 1 && BOOST_PP_ITERATION_FLAGS() == 2
145# if !(BOOST_WORKAROUND(__MWERKS__, > 0x3100)                      \
146        && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3201)))
147#  line BOOST_PP_LINE(__LINE__, value_holder.hpp(value_holder_back_reference))
148# endif
149
150# define N BOOST_PP_ITERATION()
151
152# if (N != 0)
153    template <BOOST_PP_ENUM_PARAMS_Z(1, N, class A)>
154# endif
155    value_holder_back_reference(
156        PyObject* p BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
157        : m_held(
158            p BOOST_PP_COMMA_IF(N)
159            BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
160            )
161    {
162    }
163
164# undef N
165
166#endif
Note: See TracBrowser for help on using the repository browser.