1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <meta name="generator" content="HTML Tidy, see www.w3.org"> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
---|
4 | <link rel="stylesheet" type="text/css" href="../boost.css"> |
---|
5 | |
---|
6 | <title>Boost.Python - <wrapper.hpp></title> |
---|
7 | |
---|
8 | <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
---|
9 | "header"> |
---|
10 | <tr> |
---|
11 | <td valign="top" width="300"> |
---|
12 | <h3><a href="../../../../index.htm"><img height="86" width="277" alt= |
---|
13 | "C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
14 | |
---|
15 | <td valign="top"> |
---|
16 | <h1 align="center"><a href="../index.html">Boost.Python</a></h1> |
---|
17 | |
---|
18 | <h2 align="center">Header <wrapper.hpp></h2> |
---|
19 | </table> |
---|
20 | <hr> |
---|
21 | |
---|
22 | <h2>Contents</h2> |
---|
23 | |
---|
24 | <dl class="page-index"> |
---|
25 | <dt><a href="#introduction">Introduction</a></dt> |
---|
26 | |
---|
27 | <dt><a href="#classes">Classes</a></dt> |
---|
28 | |
---|
29 | <dd> |
---|
30 | <dl class="page-index"> |
---|
31 | <dt><a href="#override-spec">Class template |
---|
32 | <code>override</code></a></dt> |
---|
33 | |
---|
34 | <dd> |
---|
35 | <dl class="page-index"> |
---|
36 | <dt><a href="#override-spec-synopsis">Class |
---|
37 | <code>override</code> synopsis</a></dt> |
---|
38 | |
---|
39 | <dt><a href="#override-spec-observers">Class |
---|
40 | <code>override</code> observer functions</a></dt> |
---|
41 | </dl> |
---|
42 | </dd> |
---|
43 | |
---|
44 | <dt><a href="#wrapper-spec">Class template |
---|
45 | <code>wrapper</code></a></dt> |
---|
46 | |
---|
47 | <dd> |
---|
48 | <dl class="page-index"> |
---|
49 | <dt><a href="#wrapper-spec-synopsis">Class <code>wrapper</code> |
---|
50 | synopsis</a></dt> |
---|
51 | |
---|
52 | <dt><a href="#wrapper-spec-observers">Class |
---|
53 | <code>wrapper</code> observer functions</a></dt> |
---|
54 | </dl> |
---|
55 | </dd> |
---|
56 | </dl> |
---|
57 | </dd> |
---|
58 | |
---|
59 | <dt><a href="#examples">Example(s)</a></dt> |
---|
60 | </dl> |
---|
61 | <hr> |
---|
62 | |
---|
63 | <h2><a name="introduction"></a>Introduction</h2> |
---|
64 | |
---|
65 | <p>To wrap a class <code>T</code> such that its virtual functions can be |
---|
66 | "overridden in Python"—so that the corresponding method of a Python |
---|
67 | derived class will be called when the virtual function is invoked from |
---|
68 | C++—you must create a C++ wrapper class derived from ``T`` that |
---|
69 | overrides those virtual functions so that they call into Python. This |
---|
70 | header contains classes that can be used to make that job easier.</p> |
---|
71 | |
---|
72 | <h2><a name="classes"></a>Classes</h2> |
---|
73 | |
---|
74 | <h3><a name="override-spec"></a>Class <code>override</code></h3> |
---|
75 | |
---|
76 | <p>Encapsulates a Python override of a C++ virtual function. An |
---|
77 | <code>override</code> object either holds a callable Python object or |
---|
78 | <code>None</code>.</p> |
---|
79 | |
---|
80 | <h4><a name="override-spec-synopsis"></a>Class <code>override</code> |
---|
81 | synopsis</h4> |
---|
82 | <pre> |
---|
83 | namespace boost |
---|
84 | { |
---|
85 | class override : object |
---|
86 | { |
---|
87 | public: |
---|
88 | <i>unspecified</i> operator() const; |
---|
89 | template <class A0> |
---|
90 | <i>unspecified</i> operator(A0) const; |
---|
91 | template <class A0, class A1> |
---|
92 | <i>unspecified</i> operator(A0, A1) const; |
---|
93 | ... |
---|
94 | template <class A0, class A1, ...class A<i>n</i>> |
---|
95 | <i>unspecified</i> operator(A0, A1, ...A<i>n</i>) const; |
---|
96 | }; |
---|
97 | }; |
---|
98 | </pre> |
---|
99 | |
---|
100 | <h4><a name="override-spec-observers"></a>Class <code>override</code> |
---|
101 | observer functions</h4> |
---|
102 | <pre> |
---|
103 | <i>unspecified</i> operator() const; |
---|
104 | template <class A0> |
---|
105 | <i>unspecified</i> operator(A0) const; |
---|
106 | template <class A0, class A1> |
---|
107 | <i>unspecified</i> operator(A0, A1) const; |
---|
108 | ... |
---|
109 | template <class A0, class A1, ...class A<i>n</i>> |
---|
110 | <i>unspecified</i> operator(A0, A1, ...A<i>n</i>) const; |
---|
111 | </pre> |
---|
112 | |
---|
113 | <dl class="function-semantics"> |
---|
114 | <dt><b>Effects:</b> If <code>*this</code> holds a callable Python |
---|
115 | object, it is invoked with the specified arguments in the manner |
---|
116 | specified <a href="callbacks.html">here</a>. Otherwise, throws <code><a |
---|
117 | href="errors.html#error_already_set-spec">error_already_set</a></code> |
---|
118 | .</dt> |
---|
119 | |
---|
120 | <dt><b>Returns:</b> An object of unspecified type that holds the Python |
---|
121 | result of the invocation and, when converted to a C++ type |
---|
122 | <code>R</code>, attempts to convert that result object to |
---|
123 | <code>R</code>. If that conversion fails, throws <code><a href= |
---|
124 | "errors.html#error_already_set-spec">error_already_set</a></code> |
---|
125 | .</dt> |
---|
126 | </dl> |
---|
127 | |
---|
128 | <h3><a name="wrapper-spec"></a>Class template <code>wrapper</code></h3> |
---|
129 | |
---|
130 | <p>Deriving your wrapper class from both ``T`` <i>and</i> |
---|
131 | ``wrapper<T> makes writing that derived class easier.</p> |
---|
132 | |
---|
133 | <h4><a name="wrapper-spec-synopsis"></a>Class template |
---|
134 | <code>wrapper</code> synopsis</h4> |
---|
135 | <pre> |
---|
136 | namespace boost |
---|
137 | { |
---|
138 | class wrapper |
---|
139 | { |
---|
140 | protected: |
---|
141 | override get_override(char const* name) const; |
---|
142 | }; |
---|
143 | }; |
---|
144 | </pre> |
---|
145 | |
---|
146 | <h4><a name="wrapper-spec-observers"></a>Class <code>wrapper</code> |
---|
147 | observer functions</h4> |
---|
148 | <pre> |
---|
149 | override get_override(char const* name) const; |
---|
150 | </pre> |
---|
151 | |
---|
152 | <dl class="function-semantics"> |
---|
153 | <dt><b>Requires:</b> <code>name</code> is a <a href= |
---|
154 | "definitions.html#ntbs">ntbs</a>.</dt> |
---|
155 | |
---|
156 | <dt><b>Returns:</b> If <code>*this</code> is the C++ base class |
---|
157 | subobject of a Python derived class instance that overrides the named |
---|
158 | function, returns an <code>override</code> object that delegates to the |
---|
159 | Python override. Otherwise, returns an <code>override</code> object |
---|
160 | that holds <code>None</code>.</dt> |
---|
161 | </dl> |
---|
162 | |
---|
163 | <h2><a name="examples"></a>Example</h2> |
---|
164 | <pre> |
---|
165 | #include <boost/python/module.hpp> |
---|
166 | #include <boost/python/class.hpp> |
---|
167 | #include <boost/python/wrapper.hpp> |
---|
168 | #include <boost/python/call.hpp> |
---|
169 | |
---|
170 | using namespace boost::python; |
---|
171 | |
---|
172 | // Class with one pure virtual function |
---|
173 | struct P |
---|
174 | { |
---|
175 | virtual ~P(){} |
---|
176 | virtual char const* f() = 0; |
---|
177 | char const* g() { return "P::g()"; } |
---|
178 | }; |
---|
179 | |
---|
180 | struct PCallback : P, wrapper<P> |
---|
181 | { |
---|
182 | char const* f() |
---|
183 | { |
---|
184 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) // Workaround for vc6/vc7 |
---|
185 | return call<char const*>(this->get_override("f").ptr()); |
---|
186 | #else |
---|
187 | return this->get_override("f")(); |
---|
188 | #endif |
---|
189 | } |
---|
190 | }; |
---|
191 | |
---|
192 | // Class with one non-pure virtual function |
---|
193 | struct A |
---|
194 | { |
---|
195 | virtual ~A(){} |
---|
196 | virtual char const* f() { return "A::f()"; } |
---|
197 | }; |
---|
198 | |
---|
199 | struct ACallback : A, wrapper<A> |
---|
200 | { |
---|
201 | char const* f() |
---|
202 | { |
---|
203 | if (override f = this->get_override("f")) |
---|
204 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) // Workaround for vc6/vc7 |
---|
205 | return call<char const*>(f.ptr()); |
---|
206 | #else |
---|
207 | return f(); |
---|
208 | #endif |
---|
209 | |
---|
210 | return A::f(); |
---|
211 | } |
---|
212 | |
---|
213 | char const* default_f() { return this->A::f(); } |
---|
214 | }; |
---|
215 | |
---|
216 | BOOST_PYTHON_MODULE_INIT(polymorphism) |
---|
217 | { |
---|
218 | class_<PCallback,boost::noncopyable>("P") |
---|
219 | .def("f", pure_virtual(&P::f)) |
---|
220 | ; |
---|
221 | |
---|
222 | class_<ACallback,boost::noncopyable>("A") |
---|
223 | .def("f", &A::f, &ACallback::default_f) |
---|
224 | ; |
---|
225 | } |
---|
226 | </pre> |
---|
227 | |
---|
228 | <p>Revised |
---|
229 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
230 | 31 October, 2004 |
---|
231 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
232 | |
---|
233 | |
---|
234 | <p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave |
---|
235 | Abrahams</a> 2004</i> Distributed under the Boost Software License, |
---|
236 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
237 | http://www.boost.org/LICENSE_1_0.txt)</p> |
---|
238 | |
---|