Chris@16
|
1 // Copyright David Abrahams 2002.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
3 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 #ifndef DATA_MEMBERS_DWA2002328_HPP
|
Chris@16
|
6 # define DATA_MEMBERS_DWA2002328_HPP
|
Chris@16
|
7
|
Chris@16
|
8 # include <boost/python/detail/prefix.hpp>
|
Chris@16
|
9
|
Chris@16
|
10 # include <boost/python/handle.hpp>
|
Chris@16
|
11
|
Chris@16
|
12 # include <boost/python/return_value_policy.hpp>
|
Chris@16
|
13 # include <boost/python/return_by_value.hpp>
|
Chris@16
|
14 # include <boost/python/return_internal_reference.hpp>
|
Chris@16
|
15 # include <boost/python/make_function.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 # include <boost/python/converter/builtin_converters.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 # include <boost/python/detail/indirect_traits.hpp>
|
Chris@16
|
20 # include <boost/python/detail/not_specified.hpp>
|
Chris@16
|
21 # include <boost/python/detail/value_arg.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 # include <boost/type_traits/add_const.hpp>
|
Chris@16
|
24 # include <boost/type_traits/add_reference.hpp>
|
Chris@16
|
25 # include <boost/type_traits/is_member_pointer.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
|
Chris@16
|
28 # include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
29 # endif
|
Chris@16
|
30
|
Chris@16
|
31 # include <boost/mpl/eval_if.hpp>
|
Chris@16
|
32 # include <boost/mpl/if.hpp>
|
Chris@16
|
33 # include <boost/mpl/vector/vector10.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 # include <boost/detail/workaround.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace python {
|
Chris@16
|
38
|
Chris@16
|
39 //
|
Chris@16
|
40 // This file defines the make_getter and make_setter function
|
Chris@16
|
41 // families, which are responsible for turning pointers, references,
|
Chris@16
|
42 // and pointers-to-data-members into callable Python objects which
|
Chris@16
|
43 // can be used for attribute access on wrapped classes.
|
Chris@16
|
44 //
|
Chris@16
|
45
|
Chris@16
|
46 namespace detail
|
Chris@16
|
47 {
|
Chris@16
|
48
|
Chris@16
|
49 // A small function object which handles the getting and setting of
|
Chris@16
|
50 // data members.
|
Chris@16
|
51 template <class Data, class Class>
|
Chris@16
|
52 struct member
|
Chris@16
|
53 {
|
Chris@16
|
54 public:
|
Chris@16
|
55 member(Data Class::*which) : m_which(which) {}
|
Chris@16
|
56
|
Chris@16
|
57 Data& operator()(Class& c) const
|
Chris@16
|
58 {
|
Chris@16
|
59 return c.*m_which;
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 void operator()(Class& c, typename value_arg<Data>::type d) const
|
Chris@16
|
63 {
|
Chris@16
|
64 c.*m_which = d;
|
Chris@16
|
65 }
|
Chris@16
|
66 private:
|
Chris@16
|
67 Data Class::*m_which;
|
Chris@16
|
68 };
|
Chris@16
|
69
|
Chris@16
|
70 // A small function object which handles the getting and setting of
|
Chris@16
|
71 // non-member objects.
|
Chris@16
|
72 template <class Data>
|
Chris@16
|
73 struct datum
|
Chris@16
|
74 {
|
Chris@16
|
75 public:
|
Chris@16
|
76 datum(Data *which) : m_which(which) {}
|
Chris@16
|
77
|
Chris@16
|
78 Data& operator()() const
|
Chris@16
|
79 {
|
Chris@16
|
80 return *m_which;
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 void operator()(typename value_arg<Data>::type d) const
|
Chris@16
|
84 {
|
Chris@16
|
85 *m_which = d;
|
Chris@16
|
86 }
|
Chris@16
|
87 private:
|
Chris@16
|
88 Data *m_which;
|
Chris@16
|
89 };
|
Chris@16
|
90
|
Chris@16
|
91 //
|
Chris@16
|
92 // Helper metafunction for determining the default CallPolicy to use
|
Chris@16
|
93 // for attribute access. If T is a [reference to a] class type X
|
Chris@16
|
94 // whose conversion to python would normally produce a new copy of X
|
Chris@16
|
95 // in a wrapped X class instance (as opposed to types such as
|
Chris@16
|
96 // std::string, which are converted to native Python types, and
|
Chris@16
|
97 // smart pointer types which produce a wrapped class instance of the
|
Chris@16
|
98 // pointee type), to-python conversions will attempt to produce an
|
Chris@16
|
99 // object which refers to the original C++ object, rather than a
|
Chris@16
|
100 // copy. See default_member_getter_policy for rationale.
|
Chris@16
|
101 //
|
Chris@16
|
102 template <class T>
|
Chris@16
|
103 struct default_getter_by_ref
|
Chris@16
|
104 : mpl::and_<
|
Chris@16
|
105 mpl::bool_<
|
Chris@16
|
106 to_python_value<
|
Chris@16
|
107 typename value_arg<T>::type
|
Chris@16
|
108 >::uses_registry
|
Chris@16
|
109 >
|
Chris@16
|
110 , indirect_traits::is_reference_to_class<
|
Chris@16
|
111 typename value_arg<T>::type
|
Chris@16
|
112 >
|
Chris@16
|
113 >
|
Chris@16
|
114 {
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 // Metafunction computing the default CallPolicy to use for reading
|
Chris@16
|
118 // data members
|
Chris@16
|
119 //
|
Chris@16
|
120 // If it's a regular class type (not an object manager or other
|
Chris@16
|
121 // type for which we have to_python specializations, use
|
Chris@16
|
122 // return_internal_reference so that we can do things like
|
Chris@16
|
123 // x.y.z = 1
|
Chris@16
|
124 // and get the right result.
|
Chris@16
|
125 template <class T>
|
Chris@16
|
126 struct default_member_getter_policy
|
Chris@16
|
127 : mpl::if_<
|
Chris@16
|
128 default_getter_by_ref<T>
|
Chris@16
|
129 , return_internal_reference<>
|
Chris@16
|
130 , return_value_policy<return_by_value>
|
Chris@16
|
131 >
|
Chris@16
|
132 {};
|
Chris@16
|
133
|
Chris@16
|
134 // Metafunction computing the default CallPolicy to use for reading
|
Chris@16
|
135 // non-member data.
|
Chris@16
|
136 template <class T>
|
Chris@16
|
137 struct default_datum_getter_policy
|
Chris@16
|
138 : mpl::if_<
|
Chris@16
|
139 default_getter_by_ref<T>
|
Chris@16
|
140 , return_value_policy<reference_existing_object>
|
Chris@16
|
141 , return_value_policy<return_by_value>
|
Chris@16
|
142 >
|
Chris@16
|
143 {};
|
Chris@16
|
144
|
Chris@16
|
145 //
|
Chris@16
|
146 // make_getter helper function family -- These helpers to
|
Chris@16
|
147 // boost::python::make_getter are used to dispatch behavior. The
|
Chris@16
|
148 // third argument is a workaround for a CWPro8 partial ordering bug
|
Chris@16
|
149 // with pointers to data members. It should be convertible to
|
Chris@16
|
150 // mpl::true_ iff the first argument is a pointer-to-member, and
|
Chris@16
|
151 // mpl::false_ otherwise. The fourth argument is for compilers
|
Chris@16
|
152 // which don't support partial ordering at all and should always be
|
Chris@16
|
153 // passed 0L.
|
Chris@16
|
154 //
|
Chris@16
|
155
|
Chris@16
|
156 #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
|
Chris@16
|
157 template <class D, class P>
|
Chris@16
|
158 inline object make_getter(D& d, P& p, mpl::false_, ...);
|
Chris@16
|
159 #endif
|
Chris@16
|
160
|
Chris@16
|
161 // Handle non-member pointers with policies
|
Chris@16
|
162 template <class D, class Policies>
|
Chris@16
|
163 inline object make_getter(D* d, Policies const& policies, mpl::false_, int)
|
Chris@16
|
164 {
|
Chris@16
|
165 return python::make_function(
|
Chris@16
|
166 detail::datum<D>(d), policies, mpl::vector1<D&>()
|
Chris@16
|
167 );
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 // Handle non-member pointers without policies
|
Chris@16
|
171 template <class D>
|
Chris@16
|
172 inline object make_getter(D* d, not_specified, mpl::false_, long)
|
Chris@16
|
173 {
|
Chris@16
|
174 typedef typename default_datum_getter_policy<D>::type policies;
|
Chris@16
|
175 return detail::make_getter(d, policies(), mpl::false_(), 0);
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 // Handle pointers-to-members with policies
|
Chris@16
|
179 template <class C, class D, class Policies>
|
Chris@16
|
180 inline object make_getter(D C::*pm, Policies const& policies, mpl::true_, int)
|
Chris@16
|
181 {
|
Chris@16
|
182 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
|
Chris@16
|
183 typedef typename remove_cv<C>::type Class;
|
Chris@16
|
184 #else
|
Chris@16
|
185 typedef C Class;
|
Chris@16
|
186 #endif
|
Chris@16
|
187 return python::make_function(
|
Chris@16
|
188 detail::member<D,Class>(pm)
|
Chris@16
|
189 , policies
|
Chris@16
|
190 , mpl::vector2<D&,Class&>()
|
Chris@16
|
191 );
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 // Handle pointers-to-members without policies
|
Chris@16
|
195 template <class C, class D>
|
Chris@16
|
196 inline object make_getter(D C::*pm, not_specified, mpl::true_, long)
|
Chris@16
|
197 {
|
Chris@16
|
198 typedef typename default_member_getter_policy<D>::type policies;
|
Chris@16
|
199 return detail::make_getter(pm, policies(), mpl::true_(), 0);
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 // Handle references
|
Chris@16
|
203 template <class D, class P>
|
Chris@16
|
204 inline object make_getter(D& d, P& p, mpl::false_, ...)
|
Chris@16
|
205 {
|
Chris@16
|
206 // Just dispatch to the handler for pointer types.
|
Chris@16
|
207 return detail::make_getter(&d, p, mpl::false_(), 0L);
|
Chris@16
|
208 }
|
Chris@16
|
209
|
Chris@16
|
210 //
|
Chris@16
|
211 // make_setter helper function family -- These helpers to
|
Chris@16
|
212 // boost::python::make_setter are used to dispatch behavior. The
|
Chris@16
|
213 // third argument is for compilers which don't support partial
|
Chris@16
|
214 // ordering at all and should always be passed 0.
|
Chris@16
|
215 //
|
Chris@16
|
216
|
Chris@16
|
217
|
Chris@16
|
218 // Handle non-member pointers
|
Chris@16
|
219 template <class D, class Policies>
|
Chris@16
|
220 inline object make_setter(D* p, Policies const& policies, mpl::false_, int)
|
Chris@16
|
221 {
|
Chris@16
|
222 return python::make_function(
|
Chris@16
|
223 detail::datum<D>(p), policies, mpl::vector2<void,D const&>()
|
Chris@16
|
224 );
|
Chris@16
|
225 }
|
Chris@16
|
226
|
Chris@16
|
227 // Handle pointers-to-members
|
Chris@16
|
228 template <class C, class D, class Policies>
|
Chris@16
|
229 inline object make_setter(D C::*pm, Policies const& policies, mpl::true_, int)
|
Chris@16
|
230 {
|
Chris@16
|
231 return python::make_function(
|
Chris@16
|
232 detail::member<D,C>(pm)
|
Chris@16
|
233 , policies
|
Chris@16
|
234 , mpl::vector3<void, C&, D const&>()
|
Chris@16
|
235 );
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 // Handle references
|
Chris@16
|
239 template <class D, class Policies>
|
Chris@16
|
240 inline object make_setter(D& x, Policies const& policies, mpl::false_, ...)
|
Chris@16
|
241 {
|
Chris@16
|
242 return detail::make_setter(&x, policies, mpl::false_(), 0L);
|
Chris@16
|
243 }
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 //
|
Chris@16
|
247 // make_getter function family -- build a callable object which
|
Chris@16
|
248 // retrieves data through the first argument and is appropriate for
|
Chris@16
|
249 // use as the `get' function in Python properties . The second,
|
Chris@16
|
250 // policies argument, is optional. We need both D& and D const&
|
Chris@16
|
251 // overloads in order be able to handle rvalues.
|
Chris@16
|
252 //
|
Chris@16
|
253 template <class D, class Policies>
|
Chris@16
|
254 inline object make_getter(D& d, Policies const& policies)
|
Chris@16
|
255 {
|
Chris@16
|
256 return detail::make_getter(d, policies, is_member_pointer<D>(), 0L);
|
Chris@16
|
257 }
|
Chris@16
|
258
|
Chris@16
|
259 template <class D, class Policies>
|
Chris@16
|
260 inline object make_getter(D const& d, Policies const& policies)
|
Chris@16
|
261 {
|
Chris@16
|
262 return detail::make_getter(d, policies, is_member_pointer<D>(), 0L);
|
Chris@16
|
263 }
|
Chris@16
|
264
|
Chris@16
|
265 template <class D>
|
Chris@16
|
266 inline object make_getter(D& x)
|
Chris@16
|
267 {
|
Chris@16
|
268 detail::not_specified policy
|
Chris@16
|
269 = detail::not_specified(); // suppress a SunPro warning
|
Chris@16
|
270 return detail::make_getter(x, policy, is_member_pointer<D>(), 0L);
|
Chris@16
|
271 }
|
Chris@16
|
272
|
Chris@16
|
273 # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238) && !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
|
Chris@16
|
274 template <class D>
|
Chris@16
|
275 inline object make_getter(D const& d)
|
Chris@16
|
276 {
|
Chris@16
|
277 detail::not_specified policy
|
Chris@16
|
278 = detail::not_specified(); // Suppress a SunPro warning
|
Chris@16
|
279 return detail::make_getter(d, policy, is_member_pointer<D>(), 0L);
|
Chris@16
|
280 }
|
Chris@16
|
281 # endif
|
Chris@16
|
282
|
Chris@16
|
283 //
|
Chris@16
|
284 // make_setter function family -- build a callable object which
|
Chris@16
|
285 // writes data through the first argument and is appropriate for
|
Chris@16
|
286 // use as the `set' function in Python properties . The second,
|
Chris@16
|
287 // policies argument, is optional. We need both D& and D const&
|
Chris@16
|
288 // overloads in order be able to handle rvalues.
|
Chris@16
|
289 //
|
Chris@16
|
290 template <class D, class Policies>
|
Chris@16
|
291 inline object make_setter(D& x, Policies const& policies)
|
Chris@16
|
292 {
|
Chris@16
|
293 return detail::make_setter(x, policies, is_member_pointer<D>(), 0);
|
Chris@16
|
294 }
|
Chris@16
|
295
|
Chris@16
|
296 template <class D, class Policies>
|
Chris@16
|
297 inline object make_setter(D const& x, Policies const& policies)
|
Chris@16
|
298 {
|
Chris@16
|
299 return detail::make_setter(x, policies, is_member_pointer<D>(), 0);
|
Chris@16
|
300 }
|
Chris@16
|
301
|
Chris@16
|
302 template <class D>
|
Chris@16
|
303 inline object make_setter(D& x)
|
Chris@16
|
304 {
|
Chris@16
|
305 return detail::make_setter(x, default_call_policies(), is_member_pointer<D>(), 0);
|
Chris@16
|
306 }
|
Chris@16
|
307
|
Chris@16
|
308 # if !(BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(__EDG_VERSION__, <= 238))
|
Chris@16
|
309 template <class D>
|
Chris@16
|
310 inline object make_setter(D const& x)
|
Chris@16
|
311 {
|
Chris@16
|
312 return detail::make_setter(x, default_call_policies(), is_member_pointer<D>(), 0);
|
Chris@16
|
313 }
|
Chris@16
|
314 # endif
|
Chris@16
|
315
|
Chris@16
|
316 }} // namespace boost::python
|
Chris@16
|
317
|
Chris@16
|
318 #endif // DATA_MEMBERS_DWA2002328_HPP
|