Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/data_members.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 8.2 KB
Line 
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 - &lt;boost/python/data_members.hpp&gt;</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          &lt;boost/python/data_members.hpp&gt;</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_&lt;&gt;::<a href=
59    "class.html#class_-spec-modifiers">def_readonly</a></code> and
60    <code>class_&lt;&gt;::<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 &lt;class C, class D&gt;</a>
67<a href="object.html#object-spec">object</a> make_getter(D C::*pm);
68
69template &lt;class C, class D, class Policies&gt;
70<a href=
71"object.html#object-spec">object</a> make_getter(D C::*pm, Policies const&amp; 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>&lt;&gt;</code></dt>
87
88      <dt>for <code>Policies</code>. Note that this test may inappropriately
89      choose <code>return_internal_reference&lt;&gt;</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>
98template &lt;class D&gt;
99<a href="object.html#object-spec">object</a> make_getter(D const&amp; d);
100template &lt;class D, class Policies&gt;
101<a href=
102"object.html#object-spec">object</a> make_getter(D const&amp; d, Policies const&amp; policies);
103
104template &lt;class D&gt;
105<a href="object.html#object-spec">object</a> make_getter(D const* p);
106template &lt;class D, class Policies&gt;
107<a href=
108"object.html#object-spec">object</a> make_getter(D const* p, Policies const&amp; 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 &lt;class C, class D&gt;</a>
132<a href="object.html#object-spec">object</a> make_setter(D C::*pm);
133
134template &lt;class C, class D, class Policies&gt;
135<a href=
136"object.html#object-spec">object</a> make_setter(D C::*pm, Policies const&amp; 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&nbsp;const&amp;</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>
156template &lt;class D&gt;
157<a href="object.html#object-spec">object</a> make_setter(D&amp; d);
158template &lt;class D, class Policies&gt;
159<a href=
160"object.html#object-spec">object</a> make_setter(D&amp; d, Policies const&amp; policies);
161
162template &lt;class D&gt;
163<a href="object.html#object-spec">object</a> make_setter(D* p);
164template &lt;class D, class Policies&gt;
165<a href=
166"object.html#object-spec">object</a> make_setter(D* p, Policies const&amp; 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&amp;</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 &lt;boost/python/data_members.hpp&gt;
190#include &lt;boost/python/module.hpp&gt;
191#include &lt;boost/python/class.hpp&gt;
192
193struct X
194{
195    X(int x) : y(x) {}
196    int y;
197};
198
199using namespace boost::python;
200
201BOOST_PYTHON_MODULE_INIT(data_members_example)
202{
203    class_&lt;X&gt;("X", init&lt;int&gt;())
204       .def("get", make_getter(&amp;X::y))
205       .def("set", make_setter(&amp;X::y))
206       ;
207}
208</pre>
209    It can be used this way in Python:
210<pre>
211&gt;&gt;&gt; from data_members_example import *
212&gt;&gt;&gt; x = X(1)
213&gt;&gt;&gt; x.get()
2141
215&gt;&gt;&gt; x.set(2)
216&gt;&gt;&gt; x.get()
2172
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>&copy; Copyright <a href=
226    "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
227  </body>
228</html>
229
Note: See TracBrowser for help on using the repository browser.