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 Windows (vers 1st August 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/handle.hpp></title> |
---|
14 | |
---|
15 | <style type="text/css"> |
---|
16 | p.c4 {font-style: italic} |
---|
17 | span.c3 {color: #ff0000} |
---|
18 | h2.c2 {text-align: center} |
---|
19 | h1.c1 {text-align: center} |
---|
20 | </style> |
---|
21 | </head> |
---|
22 | |
---|
23 | <body> |
---|
24 | <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
---|
25 | "header"> |
---|
26 | <tr> |
---|
27 | <td valign="top" width="300"> |
---|
28 | <h3><a href="../../../../index.htm"><img height="86" width="277" |
---|
29 | alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
30 | </td> |
---|
31 | |
---|
32 | <td valign="top"> |
---|
33 | <h1 class="c1"><a href="../index.html">Boost.Python</a></h1> |
---|
34 | |
---|
35 | <h2 class="c2">Header <boost/python/handle.hpp></h2> |
---|
36 | </td> |
---|
37 | </tr> |
---|
38 | </table> |
---|
39 | <hr> |
---|
40 | |
---|
41 | <h2>Contents</h2> |
---|
42 | |
---|
43 | <dl class="page-index"> |
---|
44 | <dt><a href="#introduction">Introduction</a></dt> |
---|
45 | |
---|
46 | <dt><a href="#classes">Classes</a></dt> |
---|
47 | |
---|
48 | <dd> |
---|
49 | <dl class="page-index"> |
---|
50 | <dt><a href="#handle-spec">Class template |
---|
51 | <code>handle</code></a></dt> |
---|
52 | |
---|
53 | <dd> |
---|
54 | <dl class="page-index"> |
---|
55 | <dt><a href="#handle-spec-synopsis">Class <code>handle</code> |
---|
56 | synopsis</a></dt> |
---|
57 | |
---|
58 | <dt><a href="#handle-spec-ctors">Class <code>handle</code> |
---|
59 | constructors and destructor</a></dt> |
---|
60 | |
---|
61 | <dt><a href="#handle-spec-modifiers">Class <code>handle</code> |
---|
62 | modifier functions</a></dt> |
---|
63 | |
---|
64 | <dt><a href="#handle-spec-observers">Class <code>handle</code> |
---|
65 | observer functions</a></dt> |
---|
66 | </dl> |
---|
67 | </dd> |
---|
68 | </dl> |
---|
69 | </dd> |
---|
70 | |
---|
71 | <dt><a href="#functions">Functions</a></dt> |
---|
72 | |
---|
73 | <dd> |
---|
74 | <dl class="page-index"> |
---|
75 | <dt><a href="#borrowed-spec"><code>borrowed</code></a></dt> |
---|
76 | |
---|
77 | <dt><a href="#allow_null-spec"><code>allow_null</code></a></dt> |
---|
78 | </dl> |
---|
79 | </dd> |
---|
80 | </dl> |
---|
81 | <hr> |
---|
82 | |
---|
83 | <h2><a name="introduction"></a>Introduction</h2> |
---|
84 | |
---|
85 | <p><code><boost/python/handle.hpp></code> provides |
---|
86 | <code>class template handle</code>, a smart pointer for |
---|
87 | managing reference-counted Python objects.</p> |
---|
88 | |
---|
89 | <h2><a name="classes"></a>Classes</h2> |
---|
90 | |
---|
91 | <h3><a name="handle-spec"></a>Class template <code>handle</code></h3> |
---|
92 | |
---|
93 | <p><code>handle</code> is a smart pointer to a Python object type; it |
---|
94 | holds a pointer of type <code>T*</code>, where T is its template |
---|
95 | parameter. <code>T</code> must be either a type derived from |
---|
96 | <code>PyObject</code> or a <a href="definitions.html#POD">POD</a> type |
---|
97 | whose initial <code>sizeof(PyObject)</code> bytes are layout-compatible |
---|
98 | with <code>PyObject</code>. Use <code>handle<></code> at the |
---|
99 | boundary between the Python/'C' API and high-level code; prefer <code><a |
---|
100 | href="object.html#object-spec">object</a></code> for a generalized |
---|
101 | interface to Python objects.</p> |
---|
102 | |
---|
103 | <p><a name="upcast"></a>In this document, the term "upcast" refers to an |
---|
104 | operation which converts a pointer <code>Y*</code> to a base class |
---|
105 | pointer <code>T*</code> via <code>static_cast<T*></code> if |
---|
106 | <code>Y</code> is derived from <code>T</code>, or via C-style cast |
---|
107 | <code>(T*)</code> if it is not. However, in the latter case the "upcast" |
---|
108 | is ill-formed if the initial <code>sizeof(PyObject)</code> bytes of |
---|
109 | <code>Y</code> are not layout-compatible with <code>PyObject</code>.</p> |
---|
110 | |
---|
111 | <h4><a name="handle-spec-synopsis"></a>Class template handle |
---|
112 | synopsis</h4> |
---|
113 | <pre> |
---|
114 | namespace boost { namespace python |
---|
115 | { |
---|
116 | template <class T> |
---|
117 | class handle |
---|
118 | { |
---|
119 | typedef <i>unspecified-member-function-pointer</i> bool_type; |
---|
120 | |
---|
121 | public: // types |
---|
122 | typedef T element_type; |
---|
123 | |
---|
124 | public: // member functions |
---|
125 | ~handle(); |
---|
126 | |
---|
127 | template <class Y> |
---|
128 | explicit handle(detail::borrowed<null_ok<Y> >* p); |
---|
129 | |
---|
130 | template <class Y> |
---|
131 | explicit handle(null_ok<detail::borrowed<Y> >* p); |
---|
132 | |
---|
133 | template <class Y> |
---|
134 | explicit handle(detail::borrowed<Y>* p); |
---|
135 | |
---|
136 | template <class Y> |
---|
137 | explicit handle(null_ok<Y>* p); |
---|
138 | |
---|
139 | template <class Y> |
---|
140 | explicit handle(Y* p); |
---|
141 | |
---|
142 | handle(); |
---|
143 | |
---|
144 | handle& operator=(handle const& r); |
---|
145 | |
---|
146 | template<typename Y> |
---|
147 | handle& operator=(handle<Y> const & r); // never throws |
---|
148 | |
---|
149 | |
---|
150 | template <typename Y> |
---|
151 | handle(handle<Y> const& r); |
---|
152 | |
---|
153 | handle(handle const& r); |
---|
154 | |
---|
155 | T* operator-> () const; |
---|
156 | T& operator* () const; |
---|
157 | T* get() const; |
---|
158 | void reset(); |
---|
159 | T* release(); |
---|
160 | |
---|
161 | operator bool_type() const; // never throws |
---|
162 | private: |
---|
163 | T* m_p; |
---|
164 | }; |
---|
165 | |
---|
166 | template <class T> struct null_ok; |
---|
167 | namespace detail { template <class T> struct borrowed; } |
---|
168 | }} |
---|
169 | </pre> |
---|
170 | |
---|
171 | <h4><a name="handle-spec-ctors">Class <code>handle</code> constructors |
---|
172 | and destructor</a></h4> |
---|
173 | <pre> |
---|
174 | virtual ~handle(); |
---|
175 | </pre> |
---|
176 | |
---|
177 | <dl class="function-semantics"> |
---|
178 | <dt><b>Effects:</b> |
---|
179 | <code>Py_XDECREF(</code><i>upcast</i><code><PyObject*>(m_p))</code></dt> |
---|
180 | </dl> |
---|
181 | <pre> |
---|
182 | template <class Y> |
---|
183 | explicit handle(detail::borrowed<null_ok<Y> >* p); |
---|
184 | </pre> |
---|
185 | |
---|
186 | <dl class="function-semantics"> |
---|
187 | <dt><b>Effects:</b> |
---|
188 | <code>Py_XINCREF(</code><i>upcast</i><code><PyObject*>(p)); |
---|
189 | m_p = </code><i>upcast</i><code><T*>(p);</code></dt> |
---|
190 | </dl> |
---|
191 | <pre> |
---|
192 | template <class Y> |
---|
193 | explicit handle(null_ok<detail::borrowed<Y> >* p); |
---|
194 | </pre> |
---|
195 | |
---|
196 | <dl class="function-semantics"> |
---|
197 | <dt><b>Effects:</b> |
---|
198 | <code>Py_XINCREF(</code><i>upcast</i><code><PyObject*>(p)); |
---|
199 | m_p = </code><i>upcast</i><code><T*>(p);</code></dt> |
---|
200 | </dl> |
---|
201 | <pre> |
---|
202 | template <class Y> |
---|
203 | explicit handle(detail::borrowed<Y>* p); |
---|
204 | </pre> |
---|
205 | |
---|
206 | <dl class="function-semantics"> |
---|
207 | <dt><b>Effects:</b> |
---|
208 | <code>Py_XINCREF(</code><i>upcast</i><code><PyObject*>(p)); |
---|
209 | m_p = </code><i>upcast</i><code><T*>(<a href= |
---|
210 | "errors.html#expect_non_null-spec">expect_non_null</a>(p));</code></dt> |
---|
211 | </dl> |
---|
212 | <pre> |
---|
213 | template <class Y> |
---|
214 | explicit handle(null_ok<Y>* p); |
---|
215 | </pre> |
---|
216 | |
---|
217 | <dl class="function-semantics"> |
---|
218 | <dt><b>Effects:</b> |
---|
219 | <code>m_p = </code><i>upcast</i><code><T*>(p);</code></dt> |
---|
220 | </dl> |
---|
221 | <pre> |
---|
222 | template <class Y> |
---|
223 | explicit handle(Y* p); |
---|
224 | </pre> |
---|
225 | |
---|
226 | <dl class="function-semantics"> |
---|
227 | <dt><b>Effects:</b> |
---|
228 | <code>m_p = </code><i>upcast</i><code><T*>(<a href= |
---|
229 | "errors.html#expect_non_null-spec">expect_non_null</a>(p));</code></dt> |
---|
230 | </dl> |
---|
231 | <pre> |
---|
232 | handle(); |
---|
233 | </pre> |
---|
234 | |
---|
235 | <dl class="function-semantics"> |
---|
236 | <dt><b>Effects:</b> <code>m_p = 0;</code></dt> |
---|
237 | </dl> |
---|
238 | <pre> |
---|
239 | template <typename Y> |
---|
240 | handle(handle<Y> const& r); |
---|
241 | handle(handle const& r); |
---|
242 | </pre> |
---|
243 | |
---|
244 | <dl class="function-semantics"> |
---|
245 | <dt><b>Effects:</b> |
---|
246 | <code>m_p = r.m_p; Py_XINCREF(</code><i>upcast</i><code><PyObject*>(m_p));</code></dt> |
---|
247 | </dl> |
---|
248 | |
---|
249 | <h4><a name="handle-spec-modifiers">Class <code>handle</code> |
---|
250 | modifiers</a></h4> |
---|
251 | <pre> |
---|
252 | handle& operator=(handle const& r); |
---|
253 | template<typename Y> |
---|
254 | handle& operator=(handle<Y> const & r); // never throws |
---|
255 | </pre> |
---|
256 | |
---|
257 | <dl class="function-semantics"> |
---|
258 | <dt><b>Effects:</b> |
---|
259 | <code>Py_XINCREF(</code><i>upcast</i><code><PyObject*>(r.m_p)); Py_XDECREF(</code><i> |
---|
260 | upcast</i><code><PyObject*>(m_p)); m_p = r.m_p;</code></dt> |
---|
261 | </dl> |
---|
262 | <pre> |
---|
263 | T* release(); |
---|
264 | </pre> |
---|
265 | |
---|
266 | <dl class="function-semantics"> |
---|
267 | <dt><b>Effects:</b> <code>T* x = m_p; m_p = 0;return |
---|
268 | x;</code></dt> |
---|
269 | </dl> |
---|
270 | <pre> |
---|
271 | void reset(); |
---|
272 | </pre> |
---|
273 | |
---|
274 | <dl class="function-semantics"> |
---|
275 | <dt><b>Effects:</b> |
---|
276 | <code>*this = handle<T>();</code></dt> |
---|
277 | </dl> |
---|
278 | |
---|
279 | <h4><a name="handle-spec-observers">Class <code>handle</code> |
---|
280 | observers</a></h4> |
---|
281 | <pre> |
---|
282 | T* operator-> () const; |
---|
283 | T* get() const; |
---|
284 | </pre> |
---|
285 | |
---|
286 | <dl class="function-semantics"> |
---|
287 | <dt><b>Returns:</b> <code>m_p;</code></dt> |
---|
288 | </dl> |
---|
289 | <pre> |
---|
290 | T& operator* () const; |
---|
291 | </pre> |
---|
292 | |
---|
293 | <dl class="function-semantics"> |
---|
294 | <dt><b>Returns:</b> <code>*m_p;</code></dt> |
---|
295 | </dl> |
---|
296 | <pre> |
---|
297 | operator bool_type() const; // never throws |
---|
298 | </pre> |
---|
299 | |
---|
300 | <dl class="function-semantics"> |
---|
301 | <dt><b>Returns:</b> 0 if <code>m_p == 0</code>, a pointer |
---|
302 | convertible to <code>true</code> otherwise.</dt> |
---|
303 | </dl> |
---|
304 | |
---|
305 | <h2><a name="functions"></a>Functions</h2> |
---|
306 | |
---|
307 | <h3><a name="borrowed-spec"></a><code>borrowed</code></h3> |
---|
308 | <pre> |
---|
309 | template <class T> |
---|
310 | detail::borrowed<T>* borrowed(T* p) |
---|
311 | { |
---|
312 | return (detail::borrowed<T>*)p; |
---|
313 | } |
---|
314 | </pre> |
---|
315 | |
---|
316 | <h3><a name="allow_null-spec"></a><code>allow_null</code></h3> |
---|
317 | <pre> |
---|
318 | template <class T> |
---|
319 | null_ok<T>* allow_null(T* p) |
---|
320 | { |
---|
321 | return (null_ok<T>*)p; |
---|
322 | } |
---|
323 | </pre> |
---|
324 | |
---|
325 | <p>Revised |
---|
326 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
327 | 13 November, 2002 |
---|
328 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
329 | </p> |
---|
330 | |
---|
331 | <p class="c4">© Copyright <a href= |
---|
332 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002 |
---|
333 | .</p> |
---|
334 | </body> |
---|
335 | </html> |
---|
336 | |
---|