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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Copyright David Abrahams 2006. 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) -->
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Windows (vers 1st August 2002), 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 - <call_method.hpp></title>
</head>
<body link="#0000ff" vlink="#800080">
<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>
<td valign="top">
<h1 align="center"><a href="../index.html">Boost.Python</a></h1>
<h2 align="center">Header <call_method.hpp></h2>
</td>
</tr>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#introduction">Introduction</a></dt>
<dt><a href="#functions">Functions</a></dt>
<dd>
<dl class="page-index">
<dt><a href="#call_method-spec">call_method</a></dt>
</dl>
</dd>
<dt><a href="#examples">Example(s)</a></dt>
</dl>
<hr>
<h2><a name="introduction"></a>Introduction</h2>
<p><code><boost/python/call_method.hpp></code> defines the <a href=
"#call_method-spec"><code>call_method</code></a> family of overloaded
function templates, used to invoke callable attributes of Python objects
from C++.</p>
<h2><a name="functions"></a>Functions</h2>
<pre>
<a name=
"call_method-spec">template <class R, class A1, class A2, ... class A<i>n</i>></a>
R call_method(PyObject* self, char const* method, A1 const&, A2 const&, ... A<i>n</i> const&)
</pre>
<dl class="function-semantics">
<dt><b>Requires:</b> <code>R</code> is a pointer type, reference type,
or a complete type with an accessible copy constructor</dt>
<dt><b>Effects:</b> Invokes
<code>self.<i>method</i>(a1, a2, ...a<i>n</i>)</code> in
Python, where <code>a1</code>...<code>a<i>n</i></code> are the
arguments to <code>call_method()</code>, converted to Python objects.
For a complete semantic description, see <a href="callbacks.html">this
page</a>.</dt>
<dt><b>Returns:</b> The result of the Python call, converted to the C++
type <code>R</code>.</dt>
<dt><b>Rationale:</b> <code>call_method</code> is critical to
implementing C++ virtual functions which are overridable in Python, as
shown by the example below.</dt>
</dl>
<h2><a name="examples"></a>Example(s)</h2>
The following C++ illustrates the use of <code>call_method</code> in
wrapping a class with a virtual function that can be overridden in
Python:
<h3>C++ Module Definition</h3>
<pre>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/utility.hpp>
#include <cstring>
// class to be wrapped
class Base
{
public:
virtual char const* class_name() const { return "Base"; }
virtual ~Base();
};
bool is_base(Base* b)
{
return !std::strcmp(b->class_name(), "Base");
}
// Wrapper code begins here
using namespace boost::python;
// Callback class
class Base_callback : public Base
{
public:
Base_callback(PyObject* self) : m_self(self) {}
char const* class_name() const { return <b>call_method</b><char const*>(m_self, "class_name"); }
char const* Base_name() const { return Base::class_name(); }
private:
PyObject* const m_self;
};
using namespace boost::python;
BOOST_PYTHON_MODULE(my_module)
{
def("is_base", is_base);
class_<Base,Base_callback, noncopyable>("Base")
.def("class_name", &Base_callback::Base_name)
;
}
</pre>
<h3>Python Code</h3>
<pre>
>>> from my_module import *
>>> class Derived(Base):
... def __init__(self):
... Base.__init__(self)
... def class_name(self):
... return self.__class__.__name__
...
>>> is_base(Base()) # calls the class_name() method from C++
1
>>> is_base(Derived())
0
</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>
<p><i>© Copyright <a href=
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
</body>
</html>
|