File: data_members.qbk

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (98 lines) | stat: -rw-r--r-- 4,118 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
[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]