• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7 
8 # include <boost/python/detail/prefix.hpp>
9 
10 # include <boost/python/handle.hpp>
11 
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>
16 
17 # include <boost/python/converter/builtin_converters.hpp>
18 
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>
23 
24 # include <boost/mpl/eval_if.hpp>
25 # include <boost/mpl/if.hpp>
26 # include <boost/mpl/vector/vector10.hpp>
27 
28 # include <boost/detail/workaround.hpp>
29 
30 namespace boost { namespace python {
31 
32 //
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.
37 //
38 
39 namespace detail
40 {
41 
42   // A small function object which handles the getting and setting of
43   // data members.
44   template <class Data, class Class>
45   struct member
46   {
47    public:
memberboost::python::detail::member48       member(Data Class::*which) : m_which(which) {}
49 
operator ()boost::python::detail::member50       Data& operator()(Class& c) const
51       {
52           return c.*m_which;
53       }
54 
operator ()boost::python::detail::member55       void operator()(Class& c, typename value_arg<Data>::type d) const
56       {
57           c.*m_which = d;
58       }
59    private:
60       Data Class::*m_which;
61   };
62 
63   // A small function object which handles the getting and setting of
64   // non-member objects.
65   template <class Data>
66   struct datum
67   {
68    public:
datumboost::python::detail::datum69       datum(Data *which) : m_which(which) {}
70 
operator ()boost::python::detail::datum71       Data& operator()() const
72       {
73           return *m_which;
74       }
75 
operator ()boost::python::detail::datum76       void operator()(typename value_arg<Data>::type d) const
77       {
78           *m_which = d;
79       }
80    private:
81       Data *m_which;
82   };
83 
84   //
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.
94   //
95   template <class T>
96   struct default_getter_by_ref
97       : mpl::and_<
98           mpl::bool_<
99               to_python_value<
100                   typename value_arg<T>::type
101               >::uses_registry
102           >
103         , indirect_traits::is_reference_to_class<
104               typename value_arg<T>::type
105           >
106        >
107   {
108   };
109 
110   // Metafunction computing the default CallPolicy to use for reading
111   // data members
112   //
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
116   //    x.y.z =  1
117   // and get the right result.
118   template <class T>
119   struct default_member_getter_policy
120     : mpl::if_<
121           default_getter_by_ref<T>
122         , return_internal_reference<>
123         , return_value_policy<return_by_value>
124       >
125   {};
126 
127   // Metafunction computing the default CallPolicy to use for reading
128   // non-member data.
129   template <class T>
130   struct default_datum_getter_policy
131     : mpl::if_<
132           default_getter_by_ref<T>
133         , return_value_policy<reference_existing_object>
134         , return_value_policy<return_by_value>
135       >
136   {};
137 
138   //
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
146   // passed 0L.
147 
148 
149 #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
150   template <class D, class P>
151   inline object make_getter(D& d, P& p, detail::false_, ...);
152 #endif
153 
154   // Handle non-member pointers with policies
155   template <class D, class Policies>
make_getter(D * d,Policies const & policies,detail::false_,int)156   inline object make_getter(D* d, Policies const& policies, detail::false_, int)
157   {
158       return python::make_function(
159           detail::datum<D>(d), policies, mpl::vector1<D&>()
160       );
161   }
162 
163   // Handle non-member pointers without policies
164   template <class D>
make_getter(D * d,not_specified,detail::false_,long)165   inline object make_getter(D* d, not_specified, detail::false_, long)
166   {
167       typedef typename default_datum_getter_policy<D>::type policies;
168       return detail::make_getter(d, policies(), detail::false_(), 0);
169   }
170 
171   // Handle pointers-to-members with policies
172   template <class C, class D, class Policies>
make_getter(D C::* pm,Policies const & policies,detail::true_,int)173   inline object make_getter(D C::*pm, Policies const& policies, detail::true_, int)
174   {
175 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
176       typedef typename detail::remove_cv<C>::type Class;
177 #else
178       typedef C Class;
179 #endif
180       return python::make_function(
181           detail::member<D,Class>(pm)
182         , policies
183         , mpl::vector2<D&,Class&>()
184       );
185   }
186 
187   // Handle pointers-to-members without policies
188   template <class C, class D>
make_getter(D C::* pm,not_specified,detail::true_,long)189   inline object make_getter(D C::*pm, not_specified, detail::true_, long)
190   {
191       typedef typename default_member_getter_policy<D>::type policies;
192       return detail::make_getter(pm, policies(), detail::true_(), 0);
193   }
194 
195   // Handle references
196   template <class D, class P>
make_getter(D & d,P & p,detail::false_,...)197   inline object make_getter(D& d, P& p, detail::false_, ...)
198   {
199       // Just dispatch to the handler for pointer types.
200       return detail::make_getter(&d, p, detail::false_(), 0L);
201   }
202 
203   //
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.
208   //
209 
210 
211   // Handle non-member pointers
212   template <class D, class Policies>
make_setter(D * p,Policies const & policies,detail::false_,int)213   inline object make_setter(D* p, Policies const& policies, detail::false_, int)
214   {
215       return python::make_function(
216           detail::datum<D>(p), policies, mpl::vector2<void,D const&>()
217       );
218   }
219 
220   // Handle pointers-to-members
221   template <class C, class D, class Policies>
make_setter(D C::* pm,Policies const & policies,detail::true_,int)222   inline object make_setter(D C::*pm, Policies const& policies, detail::true_, int)
223   {
224       return python::make_function(
225           detail::member<D,C>(pm)
226         , policies
227         , mpl::vector3<void, C&, D const&>()
228       );
229   }
230 
231   // Handle references
232   template <class D, class Policies>
make_setter(D & x,Policies const & policies,detail::false_,...)233   inline object make_setter(D& x, Policies const& policies, detail::false_, ...)
234   {
235       return detail::make_setter(&x, policies, detail::false_(), 0L);
236   }
237 }
238 
239 //
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.
245 //
246 template <class D, class Policies>
make_getter(D & d,Policies const & policies)247 inline object make_getter(D& d, Policies const& policies)
248 {
249     return detail::make_getter(d, policies, detail::is_member_pointer<D>(), 0L);
250 }
251 
252 template <class D, class Policies>
make_getter(D const & d,Policies const & policies)253 inline object make_getter(D const& d, Policies const& policies)
254 {
255     return detail::make_getter(d, policies, detail::is_member_pointer<D>(), 0L);
256 }
257 
258 template <class D>
make_getter(D & x)259 inline object make_getter(D& x)
260 {
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);
264 }
265 
266 #  if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
267 template <class D>
make_getter(D const & d)268 inline object make_getter(D const& d)
269 {
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);
273 }
274 #  endif
275 
276 //
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.
282 //
283 template <class D, class Policies>
make_setter(D & x,Policies const & policies)284 inline object make_setter(D& x, Policies const& policies)
285 {
286     return detail::make_setter(x, policies, detail::is_member_pointer<D>(), 0);
287 }
288 
289 template <class D, class Policies>
make_setter(D const & x,Policies const & policies)290 inline object make_setter(D const& x, Policies const& policies)
291 {
292     return detail::make_setter(x, policies, detail::is_member_pointer<D>(), 0);
293 }
294 
295 template <class D>
make_setter(D & x)296 inline object make_setter(D& x)
297 {
298     return detail::make_setter(x, default_call_policies(), detail::is_member_pointer<D>(), 0);
299 }
300 
301 # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238)
302 template <class D>
make_setter(D const & x)303 inline object make_setter(D const& x)
304 {
305     return detail::make_setter(x, default_call_policies(), detail::is_member_pointer<D>(), 0);
306 }
307 # endif
308 
309 }} // namespace boost::python
310 
311 #endif // DATA_MEMBERS_DWA2002328_HPP
312