Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/ptr.html @ 45

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

updated boost from 1_33_1 to 1_34_1

File size: 8.6 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2    <meta name="generator" content="HTML Tidy, see www.w3.org">
3    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4    <link rel="stylesheet" type="text/css" href="../boost.css">
5
6    <title>Boost.Python - &lt;boost/python/ptr.hpp&gt;</title>
7
8    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
9    "header">
10      <tr>
11        <td valign="top" width="300">
12          <h3><a href="../../../../index.htm"><img height="86" width="277" alt=
13          "C++ Boost" src="../../../../boost.png" border="0"></a></h3>
14
15        <td valign="top">
16          <h1 align="center"><a href="../index.html">Boost.Python</a></h1>
17
18          <h2 align="center">Header &lt;boost/python/ptr.hpp&gt;</h2>
19    </table>
20    <hr>
21
22    <h2>Contents</h2>
23
24    <dl class="page-index">
25      <dt><a href="#introduction">Introduction</a>
26
27      <dt><a href="#functions">Functions</a>
28      <dd>
29        <dl class="page-index">
30          <dt><a href="#ptr-spec">ptr</a>
31        </dl>
32
33      <dt><a href="#classes">Classes</a>
34      <dd>
35        <dl class="page-index">
36          <dt><a href="#pointer_wrapper-spec">Class template <code>pointer_wrapper</code></a>
37
38          <dd>
39            <dl class="page-index">
40              <dt><a href="#pointer_wrapper-spec-synopsis">Class template <code>pointer_wrapper</code> synopsis</a>
41
42              <dt><a href="#pointer_wrapper-spec-types">Class
43              <code>pointer_wrapper</code> types</a>
44
45              <dt><a href="#pointer_wrapper-spec-ctors">Class
46              <code>pointer_wrapper</code> constructors and destructor</a>
47
48              <dt><a href="#pointer_wrapper-spec-observers">Class
49              <code>pointer_wrapper</code> observer functions</a>
50
51            </dl>
52         </dl>
53
54      <dt><a href="#metafunctions">Metafunctions</a>
55      <dd>
56        <dl class="page-index">
57          <dt><a href="#is_pointer_wrapper-spec">Class template <code>is_pointer_wrapper</code></a>
58
59          <dd>
60            <dl class="page-index">
61              <dt><a href="#is_pointer_wrapper-spec-synopsis">Class template <code>is_pointer_wrapper</code> synopsis</a>
62            </dl>
63
64
65          <dt><a href="#unwrap_pointer-spec">Class template <code>unwrap_pointer</code></a>
66
67          <dd>
68            <dl class="page-index">
69              <dt><a href="#unwrap_pointer-spec-synopsis">Class template <code>unwrap_pointer</code> synopsis</a>
70            </dl>
71
72        </dl>
73
74
75      <dt><a href="#examples">Example(s)</a>
76    </dl>
77    <hr>
78
79    <h2><a name="introduction"></a>Introduction</h2>
80
81    <p><code>&lt;boost/python/ptr.hpp&gt;</code> defines the
82    <code>ptr()</code> function template, which allows users to
83    specify how to convert C++ pointer values to python in the context
84    of implementing overridable virtual functions, invoking Python
85    callable objects, or explicitly converting C++ objects to
86    Python. Normally, when passing pointers to Python callbacks, the
87    pointee is copied to ensure that the Python object
88    never holds a dangling reference. To specify that the new Python
89    object should merely contain a copy of a pointer <code>p</code>,
90    the user can pass <code><a href="#ptr-spec">ptr</a>(p)</code> instead of passing
91    <code>p</code> directly. This interface is meant to mirror the use
92    of <a href="../../../bind/ref.html"><code>boost::ref()</code></a>,
93    which can be similarly used to prevent copying of referents.
94
95    <p><code>ptr(p)</code> returns an instance of <code><a
96    href="#pointer_wrapper-spec">pointer_wrapper&lt;&gt;</a></code>, which
97    can be detected using the <code><a
98    href="#is_pointer_wrapper-spec">is_pointer_wrapper&lt;&gt;</a></code>
99    metafunction; <code><a
100    href="#unwrap_pointer-spec">unwrap_pointer&lt;&gt;</a></code> is a
101    metafunction which extracts the original pointer type from a
102    <code>pointer_wrapper&lt;&gt;</code>. These classes can be thought
103    of as implementation details.
104
105    <h2><a name="functions"></a>Functions</h2>
106<pre>
107
108<a name="ptr-spec">template &lt;class T&gt;</a>
109pointer_wrapper&lt;T&gt; ptr(T x);
110</pre>
111
112    <dl class="ptr-semantics">
113      <dt><b>Requires:</b> <code>T</code> is a pointer type.
114
115      <dt><b>Returns:</b> <code><a href="#pointer_wrapper-spec">pointer_wrapper</a>&lt;T&gt;(x)</code>
116
117      <dt><b>Throws:</b> nothing.
118    </dl>
119
120    <h2><a name="classes"></a>Classes</h2>
121
122    <h3><a name="pointer_wrapper-spec"></a>Class template <code>pointer_wrapper</code></h3>
123
124    <p>A &quot;type envelope&quot; which is returned by <a
125    href="#ptr-spec">ptr()</a>, used to indicate reference semantics
126    for pointers passed to Python callbacks.
127
128    <h4><a name="pointer_wrapper-spec-synopsis"></a>Class
129    <code>pointer_wrapper</code> synopsis</h4>
130<pre>
131namespace boost { namespace python
132{
133    template&lt;class Ptr&gt; class pointer_wrapper
134    {
135     public:
136        typedef Ptr type;
137
138        explicit pointer_wrapper(Ptr x);
139        operator Ptr() const;
140        Ptr get() const;
141    };
142}}
143</pre>
144
145    <h4><a name="pointer_wrapper-spec-types"></a>Class template <code>pointer_wrapper</code> types</h4>
146<pre>
147typedef Ptr type;
148</pre>
149The type of the pointer being wrapped.
150
151    <h4><a name="pointer_wrapper-spec-ctors"></a>Class template <code>pointer_wrapper</code> constructors and
152    destructor</h4>
153<pre>
154explicit pointer_wrapper(Ptr x);
155</pre>
156
157    <dl class="function-semantics">
158      <dt><b>Requires:</b> <code>Ptr</code> is a pointer type.
159
160      <dt><b>Effects:</b> Stores <code>x</code> in a the <code>pointer_wrapper&lt;&gt;</code>.
161      <dt><b>Throws:</b> nothing.
162    </dl>
163
164    <h4><a name="pointer_wrapper-spec-observers"></a>Class template <code>pointer_wrapper</code> observer
165    functions</h4>
166<pre>
167operator Ptr() const;
168Ptr get() const;
169</pre>
170
171    <dl class="function-semantics">
172      <dt><b>Returns:</b> a copy of the stored pointer.
173      <dt><b>Rationale:</b> <code>pointer_wrapper</code> is intended
174      to be a stand-in for the actual pointer type, but sometimes it's
175      better to have an explicit way to retrieve the pointer.
176    </dl>
177
178    <h2><a name="metafunctions"></a>Metafunctions</h2>
179
180    <h3><a name="is_pointer_wrapper-spec"></a>Class template <code>is_pointer_wrapper</code></h3>
181
182    <p>A unary metafunction whose <code>value</code> is true iff its
183    argument is a <code>pointer_wrapper&lt;&gt;</code>.
184
185    <h4><a name="is_pointer_wrapper-spec-synopsis"></a>Class template <code>is_pointer_wrapper</code> synopsis</h4>
186<pre>
187namespace boost { namespace python
188{
189    template&lt;class T&gt; class is_pointer_wrapper
190    {
191        static <i>unspecified</i> value = ...;
192    };
193}}
194</pre>
195
196
197    <dl class="metafunction-semantics">
198      <dt><b>Returns:</b> <code>true</code> iff <code>T</code> is a
199      specialization of
200<code>pointer_wrapper&lt;&gt;</code>.
201<dt><code>value</code> is an integral constant convertible to bool of
202unspecified type
203
204    </dl>
205
206<h3><a name="unwrap_pointer-spec"></a>Class template <code>unwrap_pointer</code></h3>
207
208A unary metafunction which extracts the wrapped pointer type from a
209specialization of <code>pointer_wrapper&lt;&gt;</code>.
210
211    <h4><a name="unwrap_pointer-spec-synopsis"></a>Class template <code>unwrap_pointer</code> synopsis</h4>
212<pre>
213namespace boost { namespace python
214{
215    template&lt;class T&gt; class unwrap_pointer
216    {
217        typedef <i>unspecified</i> type;
218    };
219}}
220</pre>
221
222    <dl class="metafunction-semantics">
223      <dt><b>Returns:</b> <code>T::type</code> if <code>T</code> is a
224      specialization of
225<code>pointer_wrapper&lt;&gt;</code>, <code>T</code> otherwise
226    </dl>
227
228
229    <h2><a name="examples"></a>Example(s)</h2>
230
231This example illustrates the use of <code>ptr()</code> to prevent an
232object from being copied:
233<pre>
234#include &lt;boost/python/call.hpp&gt;
235#include &lt;boost/python/ptr.hpp&gt;
236
237class expensive_to_copy
238{
239   ...
240};
241
242void pass_as_arg(expensive_to_copy* x, PyObject* f)
243{
244   // call the Python function f, passing a Python object built around
245   // which refers to *x by-pointer.
246   //
247   // *** Note: ensuring that *x outlives the argument to f() is    ***
248   // *** up to the user! Failure to do so could result in a crash! ***
249
250   boost::python::call&lt;void&gt;(f, ptr(x));
251}
252...
253</pre>
254
255    <p>Revised
256    <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
257  13 November, 2002
258  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
259
260
261    <p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave
262    Abrahams</a> 2002. </i> Distributed
263  under the Boost Software License, Version 1.0. (See accompanying file
264  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>
265
Note: See TracBrowser for help on using the repository browser.