2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 06:42:27 +00:00

add wrapper<T>

[SVN r26025]
This commit is contained in:
Dave Abrahams
2004-11-01 00:14:18 +00:00
parent a9cddc10a9
commit 601fd16cba
2 changed files with 245 additions and 0 deletions

View File

@@ -247,6 +247,20 @@
</dd>
</dl>
</dd>
<dt><a href="wrapper.html">wrapper.hpp</a></dt>
<dd>
<dl class="index">
<dt><a href="wrapper.html#classes">Classes</a></dt>
<dd>
<dl class="index">
<dt><a href="wrapper.html#override-spec">override</a></dt>
<dt><a href="wrapper.html#wrapper-spec">wrapper</a></dt>
</dl>
</dd>
</dl>
</dd>
</dl>
<h2><a name="object_wrappers">Object Wrappers</a></h2>

231
doc/v2/wrapper.html Executable file
View File

@@ -0,0 +1,231 @@
<!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=utf-8">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python - &lt;wrapper.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;wrapper.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="#override-spec">Class template <code>override</code></a>
<dd>
<dl class="page-index">
<dt><a href="#override-spec-synopsis">Class <code>override</code> synopsis</a>
<dt><a href="#override-spec-observers">Class <code>override</code> observer functions</a>
</dl>
<dt><a href="#wrapper-spec">Class template <code>wrapper</code></a>
<dd>
<dl class="page-index">
<dt><a href="#wrapper-spec-synopsis">Class <code>wrapper</code> synopsis</a>
<dt><a href="#wrapper-spec-observers">Class <code>wrapper</code> observer functions</a>
</dl>
</dl>
<dt><a href="#examples">Example(s)</a>
</dl>
<hr>
<h2><a name="introduction"></a>Introduction</h2>
<p>To wrap a class <code>T</code> such that its virtual functions
can be "overridden in Python"&#8212;so that the corresponding
method of a Python derived class will be called when the virtual
function is invoked from C++&#8212;you must create a C++ wrapper
class derived from ``T`` that overrides those virtual functions so
that they call into Python. This header contains classes that can
be used to make that job easier.
<h2><a name="classes"></a>Classes</h2>
<h3><a name="override-spec"></a>Class <code>override</code></h3>
<p>Encapsulates a Python override of a C++ virtual function.
An <code>override</code> object either holds a callable Python
object or <code>None</code>.
<h4><a name="override-spec-synopsis"></a>Class <code>override</code> synopsis</h4>
<pre>
namespace boost
{
class override : object
{
public:
<i>unspecified</i> operator() const;
template &lt;class A0&gt;
<i>unspecified</i> operator(A0) const;
template &lt;class A0, class A1&gt;
<i>unspecified</i> operator(A0, A1) const;
...
template &lt;class A0, class A1, ...class A<i>n</i>&gt;
<i>unspecified</i> operator(A0, A1, ...A<i>n</i>) const;
};
};
</pre>
<h4><a name="override-spec-observers"></a>Class <code>override</code> observer
functions</h4>
<pre>
<i>unspecified</i> operator() const;
template &lt;class A0&gt;
<i>unspecified</i> operator(A0) const;
template &lt;class A0, class A1&gt;
<i>unspecified</i> operator(A0, A1) const;
...
template &lt;class A0, class A1, ...class A<i>n</i>&gt;
<i>unspecified</i> operator(A0, A1, ...A<i>n</i>) const;
</pre>
<dl class="function-semantics">
<dt><b>Effects:</b> If <code>*this</code> holds a callable
Python object, it is invoked with the specified arguments in
the manner specified <a href="callbacks.html">here</a>. Otherwise, throws <code>
<a href="errors.html#error_already_set-spec">error_already_set</a>
</code>.
<dt><b>Returns:</b> An object of unspecified type that holds the
Python result of the invocation and, when converted to a C++
type <code>R</code>, attempts to convert that result object
to <code>R</code>. If that conversion fails, throws <code>
<a href="errors.html#error_already_set-spec">error_already_set</a>
</code>.
</dl>
<h3><a name="wrapper-spec"></a>Class template <code>wrapper</code></h3>
<p>Deriving your wrapper class from both ``T`` <i>and</i>
``wrapper&lt;T&gt; makes writing that derived class easier.
<h4><a name="wrapper-spec-synopsis"></a>Class template <code>wrapper</code> synopsis</h4>
<pre>
namespace boost
{
class wrapper
{
protected:
override get_override(char const* name) const;
};
};
</pre>
<h4><a name="wrapper-spec-observers"></a>Class <code>wrapper</code> observer
functions</h4>
<pre>
override get_override(char const* name) const;
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>name</code> is a <a href="definitions.html#ntbs">ntbs</a>.
<dt><b>Returns:</b> If <code>*this</code> is the C++ base class
subobject of a Python derived class instance that overrides
the named function, returns an <code>override</code> object
that delegates to the Python override. Otherwise,
returns an <code>
override
</code>
object that holds <code>None</code>.
</dl>
<h2><a name="examples"></a>Example</h2>
<pre>
#include &lt;boost/python/module.hpp&gt;
#include &lt;boost/python/class.hpp&gt;
#include &lt;boost/python/wrapper.hpp&gt;
#include &lt;boost/python/call.hpp&gt;
using namespace boost::python;
// Class with one pure virtual function
struct P
{
virtual ~P(){}
virtual char const* f() = 0;
char const* g() { return &quot;P::g()&quot;; }
};
struct PCallback : P, wrapper&lt;P&gt;
{
char const* f()
{
#if BOOST_WORKAROUND(BOOST_MSVC, &lt;= 1300) // Workaround for vc6/vc7
return call&lt;char const*&gt;(this-&gt;get_override(&quot;f&quot;).ptr());
#else
return this-&gt;get_override(&quot;f&quot;)();
#endif
}
};
// Class with one non-pure virtual function
struct A
{
virtual ~A(){}
virtual char const* f() { return &quot;A::f()&quot;; }
};
struct ACallback : A, wrapper&lt;A&gt;
{
char const* f()
{
if (override f = this-&gt;get_override(&quot;f&quot;))
#if BOOST_WORKAROUND(BOOST_MSVC, &lt;= 1300) // Workaround for vc6/vc7
return call&lt;char const*&gt;(f.ptr());
#else
return f();
#endif
return A::f();
}
char const* default_f() { return this-&gt;A::f(); }
};
BOOST_PYTHON_MODULE_INIT(polymorphism)
{
class_&lt;PCallback,boost::noncopyable&gt;(&quot;P&quot;)
.def(&quot;f&quot;, pure_virtual(&amp;P::f))
;
class_&lt;ACallback,boost::noncopyable&gt;(&quot;A&quot;)
.def(&quot;f&quot;, &amp;A::f, &amp;ACallback::default_f)
;
}
</pre>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
31 October, 2004
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
<p><i>&copy; Copyright <a href="../../../../people/dave_abrahams.htm">Dave
Abrahams</a> 2004</i>