blob: 17e0a826c4012b85256455774a026c4eb775f29a [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/to_python_indirect.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/to_python_indirect.hpp&gt;</h2>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#introduction">Introduction</a>
<dt><a href="#classes">Classes</a>
<dd>
<dl class="page-index">
<dt><a href="#to_python_indirect-spec">Class Template <code>to_python_indirect</code></a>
<dd>
<dl class="page-index">
<dt><a href="#to_python_indirect-spec-synopsis">Class Template
<code>to_python_indirect</code> synopsis</a>
<dt><a href="#to_python_indirect-spec-observers">Class Template
<code>to_python_indirect</code> observer functions</a>
<dt><a href="#to_python_indirect-spec-statics">Class Template
<code>to_python_indirect</code> static functions</a>
</dl>
</dl>
<dt><a href="#examples">Example</a>
</dl>
<hr>
<h2><a name="introduction"></a>Introduction</h2>
<code>&lt;boost/python/to_python_indirect.hpp&gt;</code> supplies
a way to construct new Python objects that hold wrapped C++ class
instances via a pointer or smart pointer.
<h2><a name="classes"></a>Classes</h2>
<h3><a name="to_python_indirect-spec"></a>Class template <code>to_python_indirect</code></h3>
<p>Class template <code>to_python_indirect</code> converts objects
of its first argument type to python as extension class instances, using the ownership policy provided by its 2nd argument.
<p>
<table border="1" summary="to_python_indirect template parameters">
<caption>
<b><code>to_python_indirect</code> Requirements</b><br>
In the table below, <b><code>x</code></b> denotes an object of
type <code>T</code>, <b><code>h</code></b> denotes an
object of type
<code>boost::python::objects::instance_holder*</code>, and
<b><code>p</code></b> denotes an object of type
<code>U*</code>.
</caption>
<tr>
<th>Parameter
<th>Requirements
<th>Description
<tr>
<td><code>T</code>
<td>Either <code>U</code>&nbsp;<i>cv</i><code>&amp;</code>
(where <i>cv</i> is any optional cv-qualification) or a <a
href="Dereferenceable.html">Dereferenceable</a> type such that
<code>*x</code> is convertible to <code>U const&amp;</code>, where
<code>U</code> is a class type.
<td>A type deferencing a C++ class exposed to Python using
class template <code><a
href="class.html#class_-spec">class_</a></code>.
<tr>
<td><code>MakeHolder</code>
<td>h = MakeHolder::execute(p);
<td>A class whose static <code>execute()</code> creates an
<code>instance_holder</code>.
</table>
Instantiations of <code>to_python_indirect</code> are models of <a
href="ResultConverter.html#ResultConverter-concept">ResultConverter</a>.
<h4><a name="to_python_indirect-spec-synopsis"></a>Class template <code>to_python_indirect</code> synopsis</h4>
<pre>
namespace boost { namespace python
{
template &lt;class T, class MakeHolder&gt;
struct to_python_indirect
{
static bool convertible();
PyObject* operator()(T ptr_or_reference) const;
private:
static PyTypeObject* type();
};
}}
</pre>
<h4><a name="to_python_indirect-spec-observers"></a>Class template <code>to_python_indirect</code> observers</h4>
<pre>
PyObject* operator()(T x) const;
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>x</code> refers to an object (if it
is a pointer type, it is non-null). <code>convertible() ==
true</code>.
<dt><b>Effects:</b> Creates an appropriately-typed Boost.Python
extension class instance, uses <code>MakeHolder</code> to create
an <code>instance_holder</code> from <code>x</code>, installs
the <code>instance_holder</code> in the new extension class
instance, and returns a pointer to it.
</dl>
<h4><a name="to_python_indirect-spec-statics"></a>Class template <code>to_python_indirect</code> statics</h4>
<pre>
bool convertible();
</pre>
<dt><b>Effects:</b> Returns <code>true</code> iff any module has
registered a Python type corresponding to <code>U</code>.
<h2><a name="examples"></a>Example</h2>
This example replicates the functionality of <a
href="reference_existing_object.html#reference_existing_object-spec">reference_existing_object</a>,
but without some of the compile-time error checking.
<pre>
struct make_reference_holder
{
typedef boost::python::objects::instance_holder* result_type;
template &lt;class T&gt;
static result_type execute(T* p)
{
return new boost::python::objects::pointer_holder&lt;T*, T&gt;(p);
}
};
struct reference_existing_object
{
// metafunction returning the <a href="ResultConverter.html#ResultConverter-concept">ResultConverter</a>
template &lt;class T&gt;
struct apply
{
typedef boost::python::to_python_indirect&lt;T,make_reference_holder&gt; type;
};
};
</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>