blob: 4aca5391581b6d06a3a4dc84bd2e828d9568b0b8 [file] [log] [blame]
<!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 - &lt;boost/python/ptr.hpp&gt;</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 &lt;boost/python/ptr.hpp&gt;</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>&lt;boost/python/ptr.hpp&gt;</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&lt;&gt;</a></code>, which
can be detected using the <code><a
href="#is_pointer_wrapper-spec">is_pointer_wrapper&lt;&gt;</a></code>
metafunction; <code><a
href="#unwrap_pointer-spec">unwrap_pointer&lt;&gt;</a></code> is a
metafunction which extracts the original pointer type from a
<code>pointer_wrapper&lt;&gt;</code>. These classes can be thought
of as implementation details.
<h2><a name="functions"></a>Functions</h2>
<pre>
<a name="ptr-spec">template &lt;class T&gt;</a>
pointer_wrapper&lt;T&gt; 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>&lt;T&gt;(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 &quot;type envelope&quot; 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&lt;class Ptr&gt; 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&lt;&gt;</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&lt;&gt;</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&lt;class T&gt; 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&lt;&gt;</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&lt;&gt;</code>.
<h4><a name="unwrap_pointer-spec-synopsis"></a>Class template <code>unwrap_pointer</code> synopsis</h4>
<pre>
namespace boost { namespace python
{
template&lt;class T&gt; 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&lt;&gt;</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 &lt;boost/python/call.hpp&gt;
#include &lt;boost/python/ptr.hpp&gt;
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&lt;void&gt;(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>&copy; 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>