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