• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[section boost/python/data_members.hpp]
2[section Introduction]
3`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.
4[endsect]
5[section Functions]
6``
7template <class C, class D>
8object make_getter(D C::*pm);
9
10template <class C, class D, class Policies>
11object make_getter(D C::*pm, Policies const& policies);
12``
13[variablelist
14[[Requires][Policies is a model of [link concepts.callpolicies `CallPolicies`].]]
15[[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<>
16for 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.]]
17[[Returns][An instance of object which holds the new Python callable object.]]
18]
19``
20template <class D>
21object make_getter(D const& d);
22template <class D, class Policies>
23object make_getter(D const& d, Policies const& policies);
24
25template <class D>
26object make_getter(D const* p);
27template <class D, class Policies>
28object make_getter(D const* p, Policies const& policies);
29``
30[variablelist
31[[Requires][Policies is a model of CallPolicies.]]
32[[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.]]
33[[Returns][An instance of object which holds the new Python callable object.]]
34]
35``
36template <class C, class D>
37object make_setter(D C::*pm);
38
39template <class C, class D, class Policies>
40object make_setter(D C::*pm, Policies const& policies);
41``
42[variablelist
43[[Requires][Policies is a model of CallPolicies.]]
44[[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.]]
45[[Returns][An instance of object which holds the new Python callable object.]]
46]
47``
48template <class D>
49object make_setter(D& d);
50template <class D, class Policies>
51object make_setter(D& d, Policies const& policies);
52
53template <class D>
54object make_setter(D* p);
55template <class D, class Policies>
56object make_setter(D* p, Policies const& policies);
57``
58[variablelist
59[[Requires][Policies is a model of CallPolicies.]]
60[[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.]]
61[[Returns][An instance of object which holds the new Python callable object.]]
62]
63[endsect]
64[section Example]
65The code below uses make_getter and make_setter to expose a data member as functions:
66``
67#include <boost/python/data_members.hpp>
68#include <boost/python/module.hpp>
69#include <boost/python/class.hpp>
70
71struct X
72{
73    X(int x) : y(x) {}
74    int y;
75};
76
77using namespace boost::python;
78
79BOOST_PYTHON_MODULE_INIT(data_members_example)
80{
81    class_<X>("X", init<int>())
82       .def("get", make_getter(&X::y))
83       .def("set", make_setter(&X::y))
84       ;
85}
86``
87It can be used this way in Python:
88``
89>>> from data_members_example import *
90>>> x = X(1)
91>>> x.get()
921
93>>> x.set(2)
94>>> x.get()
952
96``
97[endsect]
98[endsect]
99