summaryrefslogtreecommitdiff
path: root/libs/python/doc/html/reference/function_invocation_and_creation/boost_python_ptr_hpp.html
blob: 706774c11d2210f603be97e141619fd72c037bba (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>boost/python/ptr.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../function_invocation_and_creation.html" title="Chapter&#160;4.&#160;Function Invocation and Creation">
<link rel="prev" href="boost_python_overloads_hpp.html" title="boost/python/overloads.hpp">
<link rel="next" href="boost_python_raw_function_hpp.html" title="boost/python/raw_function.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_overloads_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_raw_function_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="function_invocation_and_creation.boost_python_ptr_hpp"></a><a class="link" href="boost_python_ptr_hpp.html" title="boost/python/ptr.hpp">boost/python/ptr.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.functions">Functions</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_t">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        types</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_c">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        constructors and destructor</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_o">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        observer functions</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions">Metafunctions</a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.introduction"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          &lt;boost/python/ptr.hpp&gt; defines the ptr() 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 p, the
          user can pass ptr(p) instead of passing p directly. This interface is meant
          to mirror the use of boost::ref(), which can be similarly used to prevent
          copying of referents.
        </p>
<p>
          ptr(p) returns an instance of <a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper" title="Class template pointer_wrapper"><code class="computeroutput"><span class="identifier">pointer_wrapper</span><span class="special">&lt;&gt;</span></code></a>,
          which can be detected using the <a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_is_pointer_wrappe" title="Class template is_pointer_wrapper"><code class="computeroutput"><span class="identifier">is_pointer_wrapper</span><span class="special">&lt;&gt;</span></code></a>
          metafunction; <a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_unwrap_pointer" title="Class template unwrap_pointer"><code class="computeroutput"><span class="identifier">unwrap_pointer</span><span class="special">&lt;&gt;</span></code></a>
          is a metafunction which extracts the original pointer type from a <code class="computeroutput"><span class="identifier">pointer_wrapper</span><span class="special">&lt;&gt;</span></code>.
          These classes can be thought of as implementation details.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.functions"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.functions" title="Functions">Functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">pointer_wrapper</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">x</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                T is a pointer type.
              </p></dd>
<dt><span class="term">Returns</span></dt>
<dd><p>
                pointer_wrapper&lt;T&gt;(x)
              </p></dd>
<dt><span class="term">Throws</span></dt>
<dd><p>
                nothing.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper" title="Class template pointer_wrapper">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code></a>
</h3></div></div></div>
<p>
          A "type envelope" which is returned by <code class="computeroutput"><span class="identifier">ptr</span><span class="special">()</span></code>, used to indicate reference semantics
          for pointers passed to Python callbacks.
        </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Ptr</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">pointer_wrapper</span>
    <span class="special">{</span>
     <span class="keyword">public</span><span class="special">:</span>
        <span class="keyword">typedef</span> <span class="identifier">Ptr</span> <span class="identifier">type</span><span class="special">;</span>

        <span class="keyword">explicit</span> <span class="identifier">pointer_wrapper</span><span class="special">(</span><span class="identifier">Ptr</span> <span class="identifier">x</span><span class="special">);</span>
        <span class="keyword">operator</span> <span class="identifier">Ptr</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
        <span class="identifier">Ptr</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_t"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_t" title="Class template pointer_wrapper types">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        types</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">Ptr</span> <span class="identifier">type</span><span class="special">;</span>
</pre>
<p>
          The type of the pointer being wrapped.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_c"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_c" title="Class template pointer_wrapper constructors and destructor">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        constructors and destructor</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">explicit</span> <span class="identifier">pointer_wrapper</span><span class="special">(</span><span class="identifier">Ptr</span> <span class="identifier">x</span><span class="special">);</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                <code class="computeroutput"><span class="identifier">Ptr</span></code> is a pointer
                type
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                Stores <code class="computeroutput"><span class="identifier">x</span></code> in a the
                <code class="computeroutput"><span class="identifier">pointer_wrapper</span><span class="special">&lt;&gt;</span></code>.
              </p></dd>
<dt><span class="term">Throws</span></dt>
<dd><p>
                nothing.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_o"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper_o" title="Class template pointer_wrapper observer functions">Class
        template <code class="computeroutput"><span class="identifier">pointer_wrapper</span></code>
        observer functions</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">operator</span> <span class="identifier">Ptr</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
<span class="identifier">Ptr</span> <span class="identifier">get</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                a copy of the stored pointer.
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                pointer_wrapper 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.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.metafunctions"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions" title="Metafunctions">Metafunctions</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_is_pointer_wrappe">Class
          template <code class="computeroutput"><span class="identifier">is_pointer_wrapper</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_unwrap_pointer">Class
          template <code class="computeroutput"><span class="identifier">unwrap_pointer</span></code></a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_is_pointer_wrappe"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_is_pointer_wrappe" title="Class template is_pointer_wrapper">Class
          template <code class="computeroutput"><span class="identifier">is_pointer_wrapper</span></code></a>
</h4></div></div></div>
<p>
            A unary metafunction whose value is true iff its argument is a pointer_wrapper&lt;&gt;.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">is_pointer_wrapper</span>
    <span class="special">{</span>
        <span class="keyword">static</span> <span class="identifier">unspecified</span> <span class="identifier">value</span> <span class="special">=</span> <span class="special">...;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="keyword">true</span></code> iff <code class="computeroutput"><span class="identifier">T</span></code> is a specialization of <code class="computeroutput"><span class="identifier">pointer_wrapper</span><span class="special">&lt;&gt;</span></code>.
                  value is an integral constant convertible to bool of unspecified
                  type
                </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_unwrap_pointer"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_unwrap_pointer" title="Class template unwrap_pointer">Class
          template <code class="computeroutput"><span class="identifier">unwrap_pointer</span></code></a>
</h4></div></div></div>
<p>
            A unary metafunction which extracts the wrapped pointer type from a specialization
            of pointer_wrapper&lt;&gt;.
          </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
<span class="special">{</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">class</span> <span class="identifier">unwrap_pointer</span>
    <span class="special">{</span>
        <span class="keyword">typedef</span> <span class="identifier">unspecified</span> <span class="identifier">type</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Returns</span></dt>
<dd><p>
                  <code class="computeroutput"><span class="identifier">T</span><span class="special">::</span><span class="identifier">type</span></code> if <code class="computeroutput"><span class="identifier">T</span></code>
                  is a specialization of <code class="computeroutput"><span class="identifier">pointer_wrapper</span><span class="special">&lt;&gt;</span></code>, <code class="computeroutput"><span class="identifier">T</span></code>
                  otherwise
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_ptr_hpp.example"></a><a class="link" href="boost_python_ptr_hpp.html#function_invocation_and_creation.boost_python_ptr_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          This example illustrates the use of ptr() to prevent an object from being
          copied:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">call</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">ptr</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">class</span> <span class="identifier">expensive_to_copy</span>
<span class="special">{</span>
   <span class="special">...</span>
<span class="special">};</span>

<span class="keyword">void</span> <span class="identifier">pass_as_arg</span><span class="special">(</span><span class="identifier">expensive_to_copy</span><span class="special">*</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">f</span><span class="special">)</span>
<span class="special">{</span>
   <span class="comment">// call the Python function f, passing a Python object built around</span>
   <span class="comment">// which refers to *x by-pointer.</span>
   <span class="comment">//</span>
   <span class="comment">// *** Note: ensuring that *x outlives the argument to f() is    ***</span>
   <span class="comment">// *** up to the user! Failure to do so could result in a crash! ***</span>

   <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">call</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;(</span><span class="identifier">f</span><span class="special">,</span> <span class="identifier">ptr</span><span class="special">(</span><span class="identifier">x</span><span class="special">));</span>
<span class="special">}</span>
<span class="special">...</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_overloads_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_raw_function_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>