Chris@16: // Copyright David Abrahams 2002. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: #ifndef DATA_MEMBERS_DWA2002328_HPP Chris@16: # define DATA_MEMBERS_DWA2002328_HPP Chris@16: Chris@16: # include Chris@16: Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) Chris@16: # include Chris@16: # endif Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: Chris@16: namespace boost { namespace python { Chris@16: Chris@16: // Chris@16: // This file defines the make_getter and make_setter function Chris@16: // families, which are responsible for turning pointers, references, Chris@16: // and pointers-to-data-members into callable Python objects which Chris@16: // can be used for attribute access on wrapped classes. Chris@16: // Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: Chris@16: // A small function object which handles the getting and setting of Chris@16: // data members. Chris@16: template Chris@16: struct member Chris@16: { Chris@16: public: Chris@16: member(Data Class::*which) : m_which(which) {} Chris@16: Chris@16: Data& operator()(Class& c) const Chris@16: { Chris@16: return c.*m_which; Chris@16: } Chris@16: Chris@16: void operator()(Class& c, typename value_arg::type d) const Chris@16: { Chris@16: c.*m_which = d; Chris@16: } Chris@16: private: Chris@16: Data Class::*m_which; Chris@16: }; Chris@16: Chris@16: // A small function object which handles the getting and setting of Chris@16: // non-member objects. Chris@16: template Chris@16: struct datum Chris@16: { Chris@16: public: Chris@16: datum(Data *which) : m_which(which) {} Chris@16: Chris@16: Data& operator()() const Chris@16: { Chris@16: return *m_which; Chris@16: } Chris@16: Chris@16: void operator()(typename value_arg::type d) const Chris@16: { Chris@16: *m_which = d; Chris@16: } Chris@16: private: Chris@16: Data *m_which; Chris@16: }; Chris@16: Chris@16: // Chris@16: // Helper metafunction for determining the default CallPolicy to use Chris@16: // for attribute access. If T is a [reference to a] class type X Chris@16: // whose conversion to python would normally produce a new copy of X Chris@16: // in a wrapped X class instance (as opposed to types such as Chris@16: // std::string, which are converted to native Python types, and Chris@16: // smart pointer types which produce a wrapped class instance of the Chris@16: // pointee type), to-python conversions will attempt to produce an Chris@16: // object which refers to the original C++ object, rather than a Chris@16: // copy. See default_member_getter_policy for rationale. Chris@16: // Chris@16: template Chris@16: struct default_getter_by_ref Chris@16: : mpl::and_< Chris@16: mpl::bool_< Chris@16: to_python_value< Chris@16: typename value_arg::type Chris@16: >::uses_registry Chris@16: > Chris@16: , indirect_traits::is_reference_to_class< Chris@16: typename value_arg::type Chris@16: > Chris@16: > Chris@16: { Chris@16: }; Chris@16: Chris@16: // Metafunction computing the default CallPolicy to use for reading Chris@16: // data members Chris@16: // Chris@16: // If it's a regular class type (not an object manager or other Chris@16: // type for which we have to_python specializations, use Chris@16: // return_internal_reference so that we can do things like Chris@16: // x.y.z = 1 Chris@16: // and get the right result. Chris@16: template Chris@16: struct default_member_getter_policy Chris@16: : mpl::if_< Chris@16: default_getter_by_ref Chris@16: , return_internal_reference<> Chris@16: , return_value_policy Chris@16: > Chris@16: {}; Chris@16: Chris@16: // Metafunction computing the default CallPolicy to use for reading Chris@16: // non-member data. Chris@16: template Chris@16: struct default_datum_getter_policy Chris@16: : mpl::if_< Chris@16: default_getter_by_ref Chris@16: , return_value_policy Chris@16: , return_value_policy Chris@16: > Chris@16: {}; Chris@16: Chris@16: // Chris@16: // make_getter helper function family -- These helpers to Chris@16: // boost::python::make_getter are used to dispatch behavior. The Chris@16: // third argument is a workaround for a CWPro8 partial ordering bug Chris@16: // with pointers to data members. It should be convertible to Chris@16: // mpl::true_ iff the first argument is a pointer-to-member, and Chris@16: // mpl::false_ otherwise. The fourth argument is for compilers Chris@16: // which don't support partial ordering at all and should always be Chris@16: // passed 0L. Chris@16: // Chris@16: Chris@16: #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238) Chris@16: template Chris@16: inline object make_getter(D& d, P& p, mpl::false_, ...); Chris@16: #endif Chris@16: Chris@16: // Handle non-member pointers with policies Chris@16: template Chris@16: inline object make_getter(D* d, Policies const& policies, mpl::false_, int) Chris@16: { Chris@16: return python::make_function( Chris@16: detail::datum(d), policies, mpl::vector1() Chris@16: ); Chris@16: } Chris@16: Chris@16: // Handle non-member pointers without policies Chris@16: template Chris@16: inline object make_getter(D* d, not_specified, mpl::false_, long) Chris@16: { Chris@16: typedef typename default_datum_getter_policy::type policies; Chris@16: return detail::make_getter(d, policies(), mpl::false_(), 0); Chris@16: } Chris@16: Chris@16: // Handle pointers-to-members with policies Chris@16: template Chris@16: inline object make_getter(D C::*pm, Policies const& policies, mpl::true_, int) Chris@16: { Chris@16: #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003)) Chris@16: typedef typename remove_cv::type Class; Chris@16: #else Chris@16: typedef C Class; Chris@16: #endif Chris@16: return python::make_function( Chris@16: detail::member(pm) Chris@16: , policies Chris@16: , mpl::vector2() Chris@16: ); Chris@16: } Chris@16: Chris@16: // Handle pointers-to-members without policies Chris@16: template Chris@16: inline object make_getter(D C::*pm, not_specified, mpl::true_, long) Chris@16: { Chris@16: typedef typename default_member_getter_policy::type policies; Chris@16: return detail::make_getter(pm, policies(), mpl::true_(), 0); Chris@16: } Chris@16: Chris@16: // Handle references Chris@16: template Chris@16: inline object make_getter(D& d, P& p, mpl::false_, ...) Chris@16: { Chris@16: // Just dispatch to the handler for pointer types. Chris@16: return detail::make_getter(&d, p, mpl::false_(), 0L); Chris@16: } Chris@16: Chris@16: // Chris@16: // make_setter helper function family -- These helpers to Chris@16: // boost::python::make_setter are used to dispatch behavior. The Chris@16: // third argument is for compilers which don't support partial Chris@16: // ordering at all and should always be passed 0. Chris@16: // Chris@16: Chris@16: Chris@16: // Handle non-member pointers Chris@16: template Chris@16: inline object make_setter(D* p, Policies const& policies, mpl::false_, int) Chris@16: { Chris@16: return python::make_function( Chris@16: detail::datum(p), policies, mpl::vector2() Chris@16: ); Chris@16: } Chris@16: Chris@16: // Handle pointers-to-members Chris@16: template Chris@16: inline object make_setter(D C::*pm, Policies const& policies, mpl::true_, int) Chris@16: { Chris@16: return python::make_function( Chris@16: detail::member(pm) Chris@16: , policies Chris@16: , mpl::vector3() Chris@16: ); Chris@16: } Chris@16: Chris@16: // Handle references Chris@16: template Chris@16: inline object make_setter(D& x, Policies const& policies, mpl::false_, ...) Chris@16: { Chris@16: return detail::make_setter(&x, policies, mpl::false_(), 0L); Chris@16: } Chris@16: } Chris@16: Chris@16: // Chris@16: // make_getter function family -- build a callable object which Chris@16: // retrieves data through the first argument and is appropriate for Chris@16: // use as the `get' function in Python properties . The second, Chris@16: // policies argument, is optional. We need both D& and D const& Chris@16: // overloads in order be able to handle rvalues. Chris@16: // Chris@16: template Chris@16: inline object make_getter(D& d, Policies const& policies) Chris@16: { Chris@16: return detail::make_getter(d, policies, is_member_pointer(), 0L); Chris@16: } Chris@16: Chris@16: template Chris@16: inline object make_getter(D const& d, Policies const& policies) Chris@16: { Chris@16: return detail::make_getter(d, policies, is_member_pointer(), 0L); Chris@16: } Chris@16: Chris@16: template Chris@16: inline object make_getter(D& x) Chris@16: { Chris@16: detail::not_specified policy Chris@16: = detail::not_specified(); // suppress a SunPro warning Chris@16: return detail::make_getter(x, policy, is_member_pointer(), 0L); Chris@16: } Chris@16: Chris@16: # if !BOOST_WORKAROUND(__EDG_VERSION__, <= 238) && !BOOST_WORKAROUND(BOOST_MSVC, <= 1300) Chris@16: template Chris@16: inline object make_getter(D const& d) Chris@16: { Chris@16: detail::not_specified policy Chris@16: = detail::not_specified(); // Suppress a SunPro warning Chris@16: return detail::make_getter(d, policy, is_member_pointer(), 0L); Chris@16: } Chris@16: # endif Chris@16: Chris@16: // Chris@16: // make_setter function family -- build a callable object which Chris@16: // writes data through the first argument and is appropriate for Chris@16: // use as the `set' function in Python properties . The second, Chris@16: // policies argument, is optional. We need both D& and D const& Chris@16: // overloads in order be able to handle rvalues. Chris@16: // Chris@16: template Chris@16: inline object make_setter(D& x, Policies const& policies) Chris@16: { Chris@16: return detail::make_setter(x, policies, is_member_pointer(), 0); Chris@16: } Chris@16: Chris@16: template Chris@16: inline object make_setter(D const& x, Policies const& policies) Chris@16: { Chris@16: return detail::make_setter(x, policies, is_member_pointer(), 0); Chris@16: } Chris@16: Chris@16: template Chris@16: inline object make_setter(D& x) Chris@16: { Chris@16: return detail::make_setter(x, default_call_policies(), is_member_pointer(), 0); Chris@16: } Chris@16: Chris@16: # if !(BOOST_WORKAROUND(BOOST_MSVC, <= 1300) || BOOST_WORKAROUND(__EDG_VERSION__, <= 238)) Chris@16: template Chris@16: inline object make_setter(D const& x) Chris@16: { Chris@16: return detail::make_setter(x, default_call_policies(), is_member_pointer(), 0); Chris@16: } Chris@16: # endif Chris@16: Chris@16: }} // namespace boost::python Chris@16: Chris@16: #endif // DATA_MEMBERS_DWA2002328_HPP