1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | |
---|
3 | <!-- Copyright David Abrahams 2006. Distributed under the Boost --> |
---|
4 | <!-- Software License, Version 1.0. (See accompanying --> |
---|
5 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
6 | <html> |
---|
7 | <head> |
---|
8 | <meta name="generator" content= |
---|
9 | "HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org"> |
---|
10 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
11 | <link rel="stylesheet" type="text/css" href="../boost.css"> |
---|
12 | |
---|
13 | <title>Boost.Python - <boost/python/data_members.hpp></title> |
---|
14 | </head> |
---|
15 | |
---|
16 | <body> |
---|
17 | <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
---|
18 | "header"> |
---|
19 | <tr> |
---|
20 | <td valign="top" width="300"> |
---|
21 | <h3><a href="../../../../index.htm"><img height="86" width="277" |
---|
22 | alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
23 | </td> |
---|
24 | |
---|
25 | <td valign="top"> |
---|
26 | <h1 align="center"><a href="../index.html">Boost.Python</a></h1> |
---|
27 | |
---|
28 | <h2 align="center">Header |
---|
29 | <boost/python/data_members.hpp></h2> |
---|
30 | </td> |
---|
31 | </tr> |
---|
32 | </table> |
---|
33 | <hr> |
---|
34 | |
---|
35 | <h2>Contents</h2> |
---|
36 | |
---|
37 | <dl class="page-index"> |
---|
38 | <dt><a href="#introduction">Introduction</a></dt> |
---|
39 | |
---|
40 | <dt><a href="#functions">Functions</a></dt> |
---|
41 | |
---|
42 | <dd> |
---|
43 | <dl class="page-index"> |
---|
44 | <dt><a href="#make_getter-spec">make_getter</a></dt> |
---|
45 | |
---|
46 | <dt><a href="#make_setter-spec">make_setter</a></dt> |
---|
47 | </dl> |
---|
48 | </dd> |
---|
49 | |
---|
50 | <dt><a href="#examples">Example</a></dt> |
---|
51 | </dl> |
---|
52 | <hr> |
---|
53 | |
---|
54 | <h2><a name="introduction"></a>Introduction</h2> |
---|
55 | |
---|
56 | <p><code><a href="#make_getter-spec">make_getter</a>()</code> and |
---|
57 | <code><a href="#make_setter-spec">make_setter</a>()</code> are the |
---|
58 | functions used internally by <code>class_<>::<a href= |
---|
59 | "class.html#class_-spec-modifiers">def_readonly</a></code> and |
---|
60 | <code>class_<>::<a href= |
---|
61 | "class.html#class_-spec-modifiers">def_readwrite</a></code> to produce |
---|
62 | Python callable objects which wrap C++ data members.</p> |
---|
63 | |
---|
64 | <h2><a name="functions"></a>Functions</h2> |
---|
65 | <pre> |
---|
66 | <a name="make_getter-spec">template <class C, class D></a> |
---|
67 | <a href="object.html#object-spec">object</a> make_getter(D C::*pm); |
---|
68 | |
---|
69 | template <class C, class D, class Policies> |
---|
70 | <a href= |
---|
71 | "object.html#object-spec">object</a> make_getter(D C::*pm, Policies const& policies); |
---|
72 | </pre> |
---|
73 | |
---|
74 | <dl class="function-semantics"> |
---|
75 | <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= |
---|
76 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
77 | |
---|
78 | <dt><b>Effects:</b> Creates a Python callable object which accepts a |
---|
79 | single argument that can be converted <code>from_python</code> to |
---|
80 | <code>C*</code>, and returns the corresponding member <code>D</code> |
---|
81 | member of the <code>C</code> object, converted <code>to_python</code>. |
---|
82 | If <code>policies</code> is supplied, it will be applied to the |
---|
83 | function as described <a href="CallPolicies.html">here</a>. Otherwise, |
---|
84 | the library attempts to determine whether <code>D</code> is a |
---|
85 | user-defined class type, and if so uses <code><a href= |
---|
86 | "return_internal_reference.html#return_internal_reference-spec">return_internal_reference</a><></code></dt> |
---|
87 | |
---|
88 | <dt>for <code>Policies</code>. Note that this test may inappropriately |
---|
89 | choose <code>return_internal_reference<></code> in some cases |
---|
90 | when <code>D</code> is a smart pointer type. This is a known |
---|
91 | defect.</dt> |
---|
92 | |
---|
93 | <dt><b>Returns:</b> An instance of <a href= |
---|
94 | "object.html#object-spec">object</a> which holds the new Python |
---|
95 | callable object.</dt> |
---|
96 | </dl> |
---|
97 | <pre> |
---|
98 | template <class D> |
---|
99 | <a href="object.html#object-spec">object</a> make_getter(D const& d); |
---|
100 | template <class D, class Policies> |
---|
101 | <a href= |
---|
102 | "object.html#object-spec">object</a> make_getter(D const& d, Policies const& policies); |
---|
103 | |
---|
104 | template <class D> |
---|
105 | <a href="object.html#object-spec">object</a> make_getter(D const* p); |
---|
106 | template <class D, class Policies> |
---|
107 | <a href= |
---|
108 | "object.html#object-spec">object</a> make_getter(D const* p, Policies const& policies); |
---|
109 | </pre> |
---|
110 | |
---|
111 | <dl class="function-semantics"> |
---|
112 | <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= |
---|
113 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
114 | |
---|
115 | <dt><b>Effects:</b> Creates a Python callable object which accepts no |
---|
116 | arguments and returns <code>d</code> or <code>*p</code>, converted |
---|
117 | <code>to_python</code> on demand. If <code>policies</code> is supplied, |
---|
118 | it will be applied to the function as described <a href= |
---|
119 | "CallPolicies.html">here</a>. Otherwise, the library attempts to |
---|
120 | determine whether <code>D</code> is a user-defined class type, and if |
---|
121 | so uses <code><a href= |
---|
122 | "reference_existing_object.html#reference_existing_object-spec">reference_existing_object</a></code></dt> |
---|
123 | |
---|
124 | <dt>for <code>Policies</code>.</dt> |
---|
125 | |
---|
126 | <dt><b>Returns:</b> An instance of <a href= |
---|
127 | "object.html#object-spec">object</a> which holds the new Python |
---|
128 | callable object.</dt> |
---|
129 | </dl> |
---|
130 | <pre> |
---|
131 | <a name="make_setter-spec">template <class C, class D></a> |
---|
132 | <a href="object.html#object-spec">object</a> make_setter(D C::*pm); |
---|
133 | |
---|
134 | template <class C, class D, class Policies> |
---|
135 | <a href= |
---|
136 | "object.html#object-spec">object</a> make_setter(D C::*pm, Policies const& policies); |
---|
137 | </pre> |
---|
138 | |
---|
139 | <dl class="function*-semantics"> |
---|
140 | <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= |
---|
141 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
142 | |
---|
143 | <dt><b>Effects:</b> Creates a Python callable object which, when called |
---|
144 | from Python, expects two arguments which can be converted |
---|
145 | <code>from_python</code> to <code>C*</code> and |
---|
146 | <code>D const&</code>, respectively, and sets the |
---|
147 | corresponding <code>D</code> member of the <code>C</code> object. If |
---|
148 | <code>policies</code> is supplied, it will be applied to the function |
---|
149 | as described <a href="CallPolicies.html">here</a>.</dt> |
---|
150 | |
---|
151 | <dt><b>Returns:</b> An instance of <a href= |
---|
152 | "object.html#object-spec">object</a> which holds the new Python |
---|
153 | callable object.</dt> |
---|
154 | </dl> |
---|
155 | <pre> |
---|
156 | template <class D> |
---|
157 | <a href="object.html#object-spec">object</a> make_setter(D& d); |
---|
158 | template <class D, class Policies> |
---|
159 | <a href= |
---|
160 | "object.html#object-spec">object</a> make_setter(D& d, Policies const& policies); |
---|
161 | |
---|
162 | template <class D> |
---|
163 | <a href="object.html#object-spec">object</a> make_setter(D* p); |
---|
164 | template <class D, class Policies> |
---|
165 | <a href= |
---|
166 | "object.html#object-spec">object</a> make_setter(D* p, Policies const& policies); |
---|
167 | </pre> |
---|
168 | |
---|
169 | <dl class="function-semantics"> |
---|
170 | <dt><b>Requires:</b> <code>Policies</code> is a model of <a href= |
---|
171 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
172 | |
---|
173 | <dt><b>Effects:</b> Creates a Python callable object which accepts one |
---|
174 | argument, which is converted from Python to <code>D const&</code> |
---|
175 | and written into <code>d</code> or <code>*p</code>, respectively. If |
---|
176 | <code>policies</code> is supplied, it will be applied to the function |
---|
177 | as described <a href="CallPolicies.html">here</a>.</dt> |
---|
178 | |
---|
179 | <dt><b>Returns:</b> An instance of <a href= |
---|
180 | "object.html#object-spec">object</a> which holds the new Python |
---|
181 | callable object.</dt> |
---|
182 | </dl> |
---|
183 | |
---|
184 | <h2><a name="examples"></a>Example</h2> |
---|
185 | |
---|
186 | <p>The code below uses make_getter and make_setter to expose a data |
---|
187 | member as functions:</p> |
---|
188 | <pre> |
---|
189 | #include <boost/python/data_members.hpp> |
---|
190 | #include <boost/python/module.hpp> |
---|
191 | #include <boost/python/class.hpp> |
---|
192 | |
---|
193 | struct X |
---|
194 | { |
---|
195 | X(int x) : y(x) {} |
---|
196 | int y; |
---|
197 | }; |
---|
198 | |
---|
199 | using namespace boost::python; |
---|
200 | |
---|
201 | BOOST_PYTHON_MODULE_INIT(data_members_example) |
---|
202 | { |
---|
203 | class_<X>("X", init<int>()) |
---|
204 | .def("get", make_getter(&X::y)) |
---|
205 | .def("set", make_setter(&X::y)) |
---|
206 | ; |
---|
207 | } |
---|
208 | </pre> |
---|
209 | It can be used this way in Python: |
---|
210 | <pre> |
---|
211 | >>> from data_members_example import * |
---|
212 | >>> x = X(1) |
---|
213 | >>> x.get() |
---|
214 | 1 |
---|
215 | >>> x.set(2) |
---|
216 | >>> x.get() |
---|
217 | 2 |
---|
218 | </pre> |
---|
219 | |
---|
220 | <p> |
---|
221 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
222 | 5 August, 2003 <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
223 | </p> |
---|
224 | |
---|
225 | <p><i>© Copyright <a href= |
---|
226 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
227 | </body> |
---|
228 | </html> |
---|
229 | |
---|