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>
22 # include <boost/python/detail/type_traits.hpp>
24 # include <boost/mpl/eval_if.hpp>
25 # include <boost/mpl/if.hpp>
26 # include <boost/mpl/vector/vector10.hpp>
28 # include <boost/detail/workaround.hpp>
30 namespace boost { namespace python {
33 // This file defines the make_getter and make_setter function
34 // families, which are responsible for turning pointers, references,
35 // and pointers-to-data-members into callable Python objects which
36 // can be used for attribute access on wrapped classes.
42 // A small function object which handles the getting and setting of
44 template <class Data, class Class>
48 member(Data Class::*which) : m_which(which) {}
50 Data& operator()(Class& c) const
55 void operator()(Class& c, typename value_arg<Data>::type d) const
63 // A small function object which handles the getting and setting of
64 // non-member objects.
69 datum(Data *which) : m_which(which) {}
71 Data& operator()() const
76 void operator()(typename value_arg<Data>::type d) const
85 // Helper metafunction for determining the default CallPolicy to use
86 // for attribute access. If T is a [reference to a] class type X
87 // whose conversion to python would normally produce a new copy of X
88 // in a wrapped X class instance (as opposed to types such as
89 // std::string, which are converted to native Python types, and
90 // smart pointer types which produce a wrapped class instance of the
91 // pointee type), to-python conversions will attempt to produce an
92 // object which refers to the original C++ object, rather than a
93 // copy. See default_member_getter_policy for rationale.
96 struct default_getter_by_ref
100 typename value_arg<T>::type
103 , indirect_traits::is_reference_to_class<
104 typename value_arg<T>::type
110 // Metafunction computing the default CallPolicy to use for reading
113 // If it's a regular class type (not an object manager or other
114 // type for which we have to_python specializations, use
115 // return_internal_reference so that we can do things like
117 // and get the right result.
119 struct default_member_getter_policy
121 default_getter_by_ref<T>
122 , return_internal_reference<>
123 , return_value_policy<return_by_value>
127 // Metafunction computing the default CallPolicy to use for reading
130 struct default_datum_getter_policy
132 default_getter_by_ref<T>
133 , return_value_policy<reference_existing_object>
134 , return_value_policy<return_by_value>
139 // make_getter helper function family -- These helpers to
140 // boost::python::make_getter are used to dispatch behavior. The
141 // third argument is a workaround for a CWPro8 partial ordering bug
142 // with pointers to data members. It should be convertible to
143 // detail::true_ iff the first argument is a pointer-to-member, and
144 // detail::false_ otherwise. The fourth argument is for compilers
145 // which don't support partial ordering at all and should always be
149 #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
150 template <class D, class P>
151 inline object make_getter(D& d, P& p, detail::false_, ...);
154 // Handle non-member pointers with policies
155 template <class D, class Policies>
156 inline object make_getter(D* d, Policies const& policies, detail::false_, int)
158 return python::make_function(
159 detail::datum<D>(d), policies, mpl::vector1<D&>()
163 // Handle non-member pointers without policies
165 inline object make_getter(D* d, not_specified, detail::false_, long)
167 typedef typename default_datum_getter_policy<D>::type policies;
168 return detail::make_getter(d, policies(), detail::false_(), 0);
171 // Handle pointers-to-members with policies
172 template <class C, class D, class Policies>
173 inline object make_getter(D C::*pm, Policies const& policies, detail::true_, int)
175 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
176 typedef typename detail::remove_cv<C>::type Class;
180 return python::make_function(
181 detail::member<D,Class>(pm)
183 , mpl::vector2<D&,Class&>()
187 // Handle pointers-to-members without policies
188 template <class C, class D>
189 inline object make_getter(D C::*pm, not_specified, detail::true_, long)
191 typedef typename default_member_getter_policy<D>::type policies;
192 return detail::make_getter(pm, policies(), detail::true_(), 0);
196 template <class D, class P>
197 inline object make_getter(D& d, P& p, detail::false_, ...)
199 // Just dispatch to the handler for pointer types.
200 return detail::make_getter(&d, p, detail::false_(), 0L);
204 // make_setter helper function family -- These helpers to
205 // boost::python::make_setter are used to dispatch behavior. The
206 // third argument is for compilers which don't support partial
207 // ordering at all and should always be passed 0.
211 // Handle non-member pointers
212 template <class D, class Policies>
213 inline object make_setter(D* p, Policies const& policies, detail::false_, int)
215 return python::make_function(
216 detail::datum<D>(p), policies, mpl::vector2<void,D const&>()
220 // Handle pointers-to-members
221 template <class C, class D, class Policies>
222 inline object make_setter(D C::*pm, Policies const& policies, detail::true_, int)
224 return python::make_function(
225 detail::member<D,C>(pm)
227 , mpl::vector3<void, C&, D const&>()
232 template <class D, class Policies>
233 inline object make_setter(D& x, Policies const& policies, detail::false_, ...)
235 return detail::make_setter(&x, policies, detail::false_(), 0L);
240 // make_getter function family -- build a callable object which
241 // retrieves data through the first argument and is appropriate for
242 // use as the `get' function in Python properties . The second,
243 // policies argument, is optional. We need both D& and D const&
244 // overloads in order be able to handle rvalues.
246 template <class D, class Policies>
247 inline object make_getter(D& d, Policies const& policies)
249 return detail::make_getter(d, policies, detail::is_member_pointer<D>(), 0L);
252 template <class D, class Policies>
253 inline object make_getter(D const& d, Policies const& policies)
255 return detail::make_getter(d, policies, detail::is_member_pointer<D>(), 0L);
259 inline object make_getter(D& x)
261 detail::not_specified policy
262 = detail::not_specified(); // suppress a SunPro warning
263 return detail::make_getter(x, policy, detail::is_member_pointer<D>(), 0L);
266 # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
268 inline object make_getter(D const& d)
270 detail::not_specified policy
271 = detail::not_specified(); // Suppress a SunPro warning
272 return detail::make_getter(d, policy, detail::is_member_pointer<D>(), 0L);
277 // make_setter function family -- build a callable object which
278 // writes data through the first argument and is appropriate for
279 // use as the `set' function in Python properties . The second,
280 // policies argument, is optional. We need both D& and D const&
281 // overloads in order be able to handle rvalues.
283 template <class D, class Policies>
284 inline object make_setter(D& x, Policies const& policies)
286 return detail::make_setter(x, policies, detail::is_member_pointer<D>(), 0);
289 template <class D, class Policies>
290 inline object make_setter(D const& x, Policies const& policies)
292 return detail::make_setter(x, policies, detail::is_member_pointer<D>(), 0);
296 inline object make_setter(D& x)
298 return detail::make_setter(x, default_call_policies(), detail::is_member_pointer<D>(), 0);
301 # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
303 inline object make_setter(D const& x)
305 return detail::make_setter(x, default_call_policies(), detail::is_member_pointer<D>(), 0);
309 }} // namespace boost::python
311 #endif // DATA_MEMBERS_DWA2002328_HPP