1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- [section boost/python/data_members.hpp]
- [section Introduction]
- `make_getter()` and `make_setter()` are the functions used internally by [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu `class_<>::def_readonly`] and [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel.class_template_class_modifier_fu `class_<>::def_readwrite`] to produce Python callable objects which wrap C++ data members.
- [endsect]
- [section Functions]
- ``
- template <class C, class D>
- object make_getter(D C::*pm);
- template <class C, class D, class Policies>
- object make_getter(D C::*pm, Policies const& policies);
- ``
- [variablelist
- [[Requires][Policies is a model of [link concepts.callpolicies `CallPolicies`].]]
- [[Effects][Creates a Python callable object which accepts a single argument that can be converted from_python to C*, and returns the corresponding member D member of the C object, converted to_python. If policies is supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whether D is a user-defined class type, and if so uses return_internal_reference<>
- for Policies. Note that this test may inappropriately choose return_internal_reference<> in some cases when D is a smart pointer type. This is a known defect.]]
- [[Returns][An instance of object which holds the new Python callable object.]]
- ]
- ``
- template <class D>
- object make_getter(D const& d);
- template <class D, class Policies>
- object make_getter(D const& d, Policies const& policies);
- template <class D>
- object make_getter(D const* p);
- template <class D, class Policies>
- object make_getter(D const* p, Policies const& policies);
- ``
- [variablelist
- [[Requires][Policies is a model of CallPolicies.]]
- [[Effects][Creates a Python callable object which accepts no arguments and returns d or *p, converted to_python on demand. If policies is supplied, it will be applied to the function as described here. Otherwise, the library attempts to determine whether D is a user-defined class type, and if so uses reference_existing_object for Policies.]]
- [[Returns][An instance of object which holds the new Python callable object.]]
- ]
- ``
- template <class C, class D>
- object make_setter(D C::*pm);
- template <class C, class D, class Policies>
- object make_setter(D C::*pm, Policies const& policies);
- ``
- [variablelist
- [[Requires][Policies is a model of CallPolicies.]]
- [[Effects][Creates a Python callable object which, when called from Python, expects two arguments which can be converted from_python to C* and D const&, respectively, and sets the corresponding D member of the C object. If policies is supplied, it will be applied to the function as described here.]]
- [[Returns][An instance of object which holds the new Python callable object.]]
- ]
- ``
- template <class D>
- object make_setter(D& d);
- template <class D, class Policies>
- object make_setter(D& d, Policies const& policies);
- template <class D>
- object make_setter(D* p);
- template <class D, class Policies>
- object make_setter(D* p, Policies const& policies);
- ``
- [variablelist
- [[Requires][Policies is a model of CallPolicies.]]
- [[Effects][Creates a Python callable object which accepts one argument, which is converted from Python to D const& and written into d or *p, respectively. If policies is supplied, it will be applied to the function as described here.]]
- [[Returns][An instance of object which holds the new Python callable object.]]
- ]
- [endsect]
- [section Example]
- The code below uses make_getter and make_setter to expose a data member as functions:
- ``
- #include <boost/python/data_members.hpp>
- #include <boost/python/module.hpp>
- #include <boost/python/class.hpp>
- struct X
- {
- X(int x) : y(x) {}
- int y;
- };
- using namespace boost::python;
- BOOST_PYTHON_MODULE_INIT(data_members_example)
- {
- class_<X>("X", init<int>())
- .def("get", make_getter(&X::y))
- .def("set", make_setter(&X::y))
- ;
- }
- ``
- It can be used this way in Python:
- ``
- >>> from data_members_example import *
- >>> x = X(1)
- >>> x.get()
- 1
- >>> x.set(2)
- >>> x.get()
- 2
- ``
- [endsect]
- [endsect]
|