|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#ifndef DATA_MEMBERS_DWA2002328_HPP |
| 7 | +# define DATA_MEMBERS_DWA2002328_HPP |
| 8 | + |
| 9 | +# include <boost/python/detail/config.hpp> |
| 10 | +# include <boost/python/detail/wrap_python.hpp> |
| 11 | +# include <boost/python/object/function.hpp> |
| 12 | +# include <boost/type_traits/transform_traits.hpp> |
| 13 | +# include <boost/type_traits/cv_traits.hpp> |
| 14 | +# include <boost/python/return_value_policy.hpp> |
| 15 | +# include <boost/python/copy_mutable_reference.hpp> |
| 16 | + |
| 17 | +namespace boost { namespace python { |
| 18 | + |
| 19 | +namespace detail |
| 20 | +{ |
| 21 | + template <class Data, class Class, class Policies> |
| 22 | + struct member |
| 23 | + { |
| 24 | + static PyObject* get(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies) |
| 25 | + { |
| 26 | + from_python<Class*> c0(PyTuple_GET_ITEM(args_, 0)); |
| 27 | + if (!c0.convertible()) return 0; |
| 28 | + |
| 29 | + // find the result converter |
| 30 | + typedef typename Policies::result_converter result_converter; |
| 31 | + typedef typename boost::add_reference<Data>::type source; |
| 32 | + typename mpl::apply1<result_converter,source>::type cr; |
| 33 | + if (!cr.convertible()) return 0; |
| 34 | + |
| 35 | + if (!policies.precall(args_)) return 0; |
| 36 | + |
| 37 | + PyObject* result = cr( (c0(PyTuple_GET_ITEM(args_, 0)))->*pm ); |
| 38 | + |
| 39 | + return policies.postcall(args_, result); |
| 40 | + } |
| 41 | + |
| 42 | + static PyObject* set(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies) |
| 43 | + { |
| 44 | + // check that each of the arguments is convertible |
| 45 | + from_python<Class*> c0(PyTuple_GET_ITEM(args_, 0)); |
| 46 | + if (!c0.convertible()) return 0; |
| 47 | + |
| 48 | + typedef typename add_const<Data>::type target1; |
| 49 | + typedef typename add_reference<target1>::type target; |
| 50 | + from_python<target> c1(PyTuple_GET_ITEM(args_, 1)); |
| 51 | + |
| 52 | + if (!c1.convertible()) return 0; |
| 53 | + |
| 54 | + if (!policies.precall(args_)) return 0; |
| 55 | + |
| 56 | + (c0(PyTuple_GET_ITEM(args_, 0)))->*pm = c1(PyTuple_GET_ITEM(args_, 1)); |
| 57 | + |
| 58 | + return policies.postcall(args_, detail::none()); |
| 59 | + } |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +template <class C, class D> |
| 64 | +objects::function* make_getter(D C::*pm) |
| 65 | +{ |
| 66 | + typedef return_value_policy<copy_mutable_reference> default_policy; |
| 67 | + return new objects::function( |
| 68 | + objects::py_function( |
| 69 | + ::boost::bind( |
| 70 | + &detail::member<D,C,default_policy>::get, pm, _1, _2 |
| 71 | + , default_policy())) |
| 72 | + , 1); |
| 73 | +} |
| 74 | + |
| 75 | +template <class C, class D, class Policies> |
| 76 | +objects::function* make_getter(D C::*pm, Policies const& policies) |
| 77 | +{ |
| 78 | + return new objects::function( |
| 79 | + objects::py_function( |
| 80 | + ::boost::bind( |
| 81 | + &detail::member<D,C,Policies>::get, pm, _1, _2 |
| 82 | + , policies)) |
| 83 | + , 1); |
| 84 | +} |
| 85 | + |
| 86 | +template <class C, class D> |
| 87 | +objects::function* make_setter(D C::*pm) |
| 88 | +{ |
| 89 | + return new objects::function( |
| 90 | + objects::py_function( |
| 91 | + ::boost::bind( |
| 92 | + &detail::member<D,C,default_call_policies>::set, pm, _1, _2 |
| 93 | + , default_call_policies())) |
| 94 | + , 1); |
| 95 | +} |
| 96 | + |
| 97 | +template <class C, class D, class Policies> |
| 98 | +objects::function* make_setter(D C::*pm, Policies const& policies) |
| 99 | +{ |
| 100 | + return new objects::function( |
| 101 | + objects::py_function( |
| 102 | + ::boost::bind( |
| 103 | + &detail::member<D,C,Policies>::set, pm, _1, _2 |
| 104 | + , policies)) |
| 105 | + , 1); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +}} // namespace boost::python |
| 110 | + |
| 111 | +#endif // DATA_MEMBERS_DWA2002328_HPP |
0 commit comments