]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/doc/reference/data_members.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / python / doc / reference / data_members.qbk
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 ``
7 template <class C, class D>
8 object make_getter(D C::*pm);
9
10 template <class C, class D, class Policies>
11 object 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<>
16 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.]]
17 [[Returns][An instance of object which holds the new Python callable object.]]
18 ]
19 ``
20 template <class D>
21 object make_getter(D const& d);
22 template <class D, class Policies>
23 object make_getter(D const& d, Policies const& policies);
24
25 template <class D>
26 object make_getter(D const* p);
27 template <class D, class Policies>
28 object 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 ``
36 template <class C, class D>
37 object make_setter(D C::*pm);
38
39 template <class C, class D, class Policies>
40 object 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 ``
48 template <class D>
49 object make_setter(D& d);
50 template <class D, class Policies>
51 object make_setter(D& d, Policies const& policies);
52
53 template <class D>
54 object make_setter(D* p);
55 template <class D, class Policies>
56 object 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]
65 The 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
71 struct X
72 {
73 X(int x) : y(x) {}
74 int y;
75 };
76
77 using namespace boost::python;
78
79 BOOST_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 ``
87 It can be used this way in Python:
88 ``
89 >>> from data_members_example import *
90 >>> x = X(1)
91 >>> x.get()
92 1
93 >>> x.set(2)
94 >>> x.get()
95 2
96 ``
97 [endsect]
98 [endsect]