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
|
<!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 - <wrapper.hpp></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 <wrapper.hpp></h2>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#introduction">Introduction</a></dt>
<dt><a href="#classes">Classes</a></dt>
<dd>
<dl class="page-index">
<dt><a href="#override-spec">Class template
<code>override</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#override-spec-synopsis">Class
<code>override</code> synopsis</a></dt>
<dt><a href="#override-spec-observers">Class
<code>override</code> observer functions</a></dt>
</dl>
</dd>
<dt><a href="#wrapper-spec">Class template
<code>wrapper</code></a></dt>
<dd>
<dl class="page-index">
<dt><a href="#wrapper-spec-synopsis">Class <code>wrapper</code>
synopsis</a></dt>
<dt><a href="#wrapper-spec-observers">Class
<code>wrapper</code> observer functions</a></dt>
</dl>
</dd>
</dl>
</dd>
<dt><a href="#examples">Example(s)</a></dt>
</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"—so that the corresponding method of a Python
derived class will be called when the virtual function is invoked from
C++—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.</p>
<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>.</p>
<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 <class A0>
<i>unspecified</i> operator(A0) const;
template <class A0, class A1>
<i>unspecified</i> operator(A0, A1) const;
...
template <class A0, class A1, ...class A<i>n</i>>
<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 <class A0>
<i>unspecified</i> operator(A0) const;
template <class A0, class A1>
<i>unspecified</i> operator(A0, A1) const;
...
template <class A0, class A1, ...class A<i>n</i>>
<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>
<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>
.</dt>
</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<T> makes writing that derived class easier.</p>
<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>
<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>.</dt>
</dl>
<h2><a name="examples"></a>Example</h2>
<pre>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/call.hpp>
using namespace boost::python;
// Class with one pure virtual function
struct P
{
virtual ~P(){}
virtual char const* f() = 0;
char const* g() { return "P::g()"; }
};
struct PCallback : P, wrapper<P>
{
char const* f()
{
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) // Workaround for vc6/vc7
return call<char const*>(this->get_override("f").ptr());
#else
return this->get_override("f")();
#endif
}
};
// Class with one non-pure virtual function
struct A
{
virtual ~A(){}
virtual char const* f() { return "A::f()"; }
};
struct ACallback : A, wrapper<A>
{
char const* f()
{
if (override f = this->get_override("f"))
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) // Workaround for vc6/vc7
return call<char const*>(f.ptr());
#else
return f();
#endif
return A::f();
}
char const* default_f() { return this->A::f(); }
};
BOOST_PYTHON_MODULE_INIT(polymorphism)
{
class_<PCallback,boost::noncopyable>("P")
.def("f", pure_virtual(&P::f))
;
class_<ACallback,boost::noncopyable>("A")
.def("f", &A::f, &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>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave
Abrahams</a> 2004</i>
|