Update contrib.
1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef DATA_MEMBERS_DWA2002328_HPP
6 # define DATA_MEMBERS_DWA2002328_HPP
8 # include <boost/python/detail/prefix.hpp>
10 # include <boost/python/handle.hpp>
12 # include <boost/python/return_value_policy.hpp>
13 # include <boost/python/return_by_value.hpp>
14 # include <boost/python/return_internal_reference.hpp>
15 # include <boost/python/make_function.hpp>
17 # include <boost/python/converter/builtin_converters.hpp>
19 # include <boost/python/detail/indirect_traits.hpp>
20 # include <boost/python/detail/not_specified.hpp>
21 # include <boost/python/detail/value_arg.hpp>
23 # include <boost/type_traits/add_const.hpp>
24 # include <boost/type_traits/add_reference.hpp>
25 # include <boost/type_traits/is_member_pointer.hpp>
27 # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
28 # include <boost/type_traits/remove_cv.hpp>
31 # include <boost/mpl/eval_if.hpp>
32 # include <boost/mpl/if.hpp>
33 # include <boost/mpl/vector/vector10.hpp>
35 # include <boost/detail/workaround.hpp>
37 namespace boost { namespace python {
40 // This file defines the make_getter and make_setter function
41 // families, which are responsible for turning pointers, references,
42 // and pointers-to-data-members into callable Python objects which
43 // can be used for attribute access on wrapped classes.
49 // A small function object which handles the getting and setting of
51 template <class Data, class Class>
55 member(Data Class::*which) : m_which(which) {}
57 Data& operator()(Class& c) const
62 void operator()(Class& c, typename value_arg<Data>::type d) const
70 // A small function object which handles the getting and setting of
71 // non-member objects.
76 datum(Data *which) : m_which(which) {}
78 Data& operator()() const
83 void operator()(typename value_arg<Data>::type d) const
92 // Helper metafunction for determining the default CallPolicy to use
93 // for attribute access. If T is a [reference to a] class type X
94 // whose conversion to python would normally produce a new copy of X
95 // in a wrapped X class instance (as opposed to types such as
96 // std::string, which are converted to native Python types, and
97 // smart pointer types which produce a wrapped class instance of the
98 // pointee type), to-python conversions will attempt to produce an
99 // object which refers to the original C++ object, rather than a
100 // copy. See default_member_getter_policy for rationale.
103 struct default_getter_by_ref
107 typename value_arg<T>::type
110 , indirect_traits::is_reference_to_class<
111 typename value_arg<T>::type
117 // Metafunction computing the default CallPolicy to use for reading
120 // If it's a regular class type (not an object manager or other
121 // type for which we have to_python specializations, use
122 // return_internal_reference so that we can do things like
124 // and get the right result.
126 struct default_member_getter_policy
128 default_getter_by_ref<T>
129 , return_internal_reference<>
130 , return_value_policy<return_by_value>
134 // Metafunction computing the default CallPolicy to use for reading
137 struct default_datum_getter_policy
139 default_getter_by_ref<T>
140 , return_value_policy<reference_existing_object>
141 , return_value_policy<return_by_value>
146 // make_getter helper function family -- These helpers to
147 // boost::python::make_getter are used to dispatch behavior. The
148 // third argument is a workaround for a CWPro8 partial ordering bug
149 // with pointers to data members. It should be convertible to
150 // mpl::true_ iff the first argument is a pointer-to-member, and
151 // mpl::false_ otherwise. The fourth argument is for compilers
152 // which don't support partial ordering at all and should always be
156 #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
157 template <class D, class P>
158 inline object make_getter(D& d, P& p, mpl::false_, ...);
161 // Handle non-member pointers with policies
162 template <class D, class Policies>
163 inline object make_getter(D* d, Policies const& policies, mpl::false_, int)
165 return python::make_function(
166 detail::datum<D>(d), policies, mpl::vector1<D&>()
170 // Handle non-member pointers without policies
172 inline object make_getter(D* d, not_specified, mpl::false_, long)
174 typedef typename default_datum_getter_policy<D>::type policies;
175 return detail::make_getter(d, policies(), mpl::false_(), 0);
178 // Handle pointers-to-members with policies
179 template <class C, class D, class Policies>
180 inline object make_getter(D C::*pm, Policies const& policies, mpl::true_, int)
182 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
183 typedef typename remove_cv<C>::type Class;
187 return python::make_function(
188 detail::member<D,Class>(pm)
190 , mpl::vector2<D&,Class&>()
194 // Handle pointers-to-members without policies
195 template <class C, class D>
196 inline object make_getter(D C::*pm, not_specified, mpl::true_, long)
198 typedef typename default_member_getter_policy<D>::type policies;
199 return detail::make_getter(pm, policies(), mpl::true_(), 0);
203 template <class D, class P>
204 inline object make_getter(D& d, P& p, mpl::false_, ...)
206 // Just dispatch to the handler for pointer types.
207 return detail::make_getter(&d, p, mpl::false_(), 0L);
211 // make_setter helper function family -- These helpers to
212 // boost::python::make_setter are used to dispatch behavior. The
213 // third argument is for compilers which don't support partial
214 // ordering at all and should always be passed 0.
218 // Handle non-member pointers
219 template <class D, class Policies>
220 inline object make_setter(D* p, Policies const& policies, mpl::false_, int)
222 return python::make_function(
223 detail::datum<D>(p), policies, mpl::vector2<void,D const&>()
227 // Handle pointers-to-members
228 template <class C, class D, class Policies>
229 inline object make_setter(D C::*pm, Policies const& policies, mpl::true_, int)
231 return python::make_function(
232 detail::member<D,C>(pm)
234 , mpl::vector3<void, C&, D const&>()
239 template <class D, class Policies>
240 inline object make_setter(D& x, Policies const& policies, mpl::false_, ...)
242 return detail::make_setter(&x, policies, mpl::false_(), 0L);
247 // make_getter function family -- build a callable object which
248 // retrieves data through the first argument and is appropriate for
249 // use as the `get' function in Python properties . The second,
250 // policies argument, is optional. We need both D& and D const&
251 // overloads in order be able to handle rvalues.
253 template <class D, class Policies>
254 inline object make_getter(D& d, Policies const& policies)
256 return detail::make_getter(d, policies, is_member_pointer<D>(), 0L);
259 template <class D, class Policies>
260 inline object make_getter(D const& d, Policies const& policies)
262 return detail::make_getter(d, policies, is_member_pointer<D>(), 0L);
266 inline object make_getter(D& x)
268 detail::not_specified policy;
269 return detail::make_getter(x, policy, is_member_pointer<D>(), 0L);
272 # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238) && !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
274 inline object make_getter(D const& d)
276 detail::not_specified policy;
277 return detail::make_getter(d, policy, is_member_pointer<D>(), 0L);
282 // make_setter function family -- build a callable object which
283 // writes data through the first argument and is appropriate for
284 // use as the `set' function in Python properties . The second,
285 // policies argument, is optional. We need both D& and D const&
286 // overloads in order be able to handle rvalues.
288 template <class D, class Policies>
289 inline object make_setter(D& x, Policies const& policies)
291 return detail::make_setter(x, policies, is_member_pointer<D>(), 0);
294 template <class D, class Policies>
295 inline object make_setter(D const& x, Policies const& policies)
297 return detail::make_setter(x, policies, is_member_pointer<D>(), 0);
301 inline object make_setter(D& x)
303 return detail::make_setter(x, default_call_policies(), is_member_pointer<D>(), 0);
306 # if !(BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(__EDG_VERSION__, <= 238))
308 inline object make_setter(D const& x)
310 return detail::make_setter(x, default_call_policies(), is_member_pointer<D>(), 0);
314 }} // namespace boost::python
316 #endif // DATA_MEMBERS_DWA2002328_HPP