Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/call_method.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.7 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3<!-- Copyright David Abrahams 2006. Distributed under the Boost -->
4<!-- Software License, Version 1.0. (See accompanying -->
5<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
6<html>
7  <head>
8    <meta name="generator" content=
9    "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
10    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
11    <link rel="stylesheet" type="text/css" href="../boost.css">
12
13    <title>Boost.Python - &lt;call_method.hpp&gt;</title>
14  </head>
15
16  <body link="#0000ff" vlink="#800080">
17    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
18    "header">
19      <tr>
20        <td valign="top" width="300">
21          <h3><a href="../../../../index.htm"><img height="86" width="277"
22          alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3>
23        </td>
24
25        <td valign="top">
26          <h1 align="center"><a href="../index.html">Boost.Python</a></h1>
27
28          <h2 align="center">Header &lt;call_method.hpp&gt;</h2>
29        </td>
30      </tr>
31    </table>
32    <hr>
33
34    <h2>Contents</h2>
35
36    <dl class="page-index">
37      <dt><a href="#introduction">Introduction</a></dt>
38
39      <dt><a href="#functions">Functions</a></dt>
40
41      <dd>
42        <dl class="page-index">
43          <dt><a href="#call_method-spec">call_method</a></dt>
44        </dl>
45      </dd>
46
47      <dt><a href="#examples">Example(s)</a></dt>
48    </dl>
49    <hr>
50
51    <h2><a name="introduction"></a>Introduction</h2>
52
53    <p><code>&lt;boost/python/call_method.hpp&gt;</code> defines the <a href=
54    "#call_method-spec"><code>call_method</code></a> family of overloaded
55    function templates, used to invoke callable attributes of Python objects
56    from C++.</p>
57
58    <h2><a name="functions"></a>Functions</h2>
59<pre>
60<a name=
61"call_method-spec">template &lt;class R, class A1, class A2, ... class A<i>n</i>&gt;</a>
62R call_method(PyObject* self, char const* method, A1 const&amp;, A2 const&amp;, ... A<i>n</i> const&amp;)
63</pre>
64
65    <dl class="function-semantics">
66      <dt><b>Requires:</b> <code>R</code> is a pointer type, reference type,
67      or a complete type with an accessible copy constructor</dt>
68
69      <dt><b>Effects:</b> Invokes
70      <code>self.<i>method</i>(a1,&nbsp;a2,&nbsp;...a<i>n</i>)</code> in
71      Python, where <code>a1</code>...<code>a<i>n</i></code> are the
72      arguments to <code>call_method()</code>, converted to Python objects.
73      For a complete semantic description, see <a href="callbacks.html">this
74      page</a>.</dt>
75
76      <dt><b>Returns:</b> The result of the Python call, converted to the C++
77      type <code>R</code>.</dt>
78
79      <dt><b>Rationale:</b> <code>call_method</code> is critical to
80      implementing C++ virtual functions which are overridable in Python, as
81      shown by the example below.</dt>
82    </dl>
83
84    <h2><a name="examples"></a>Example(s)</h2>
85    The following C++ illustrates the use of <code>call_method</code> in
86    wrapping a class with a virtual function that can be overridden in
87    Python:
88
89    <h3>C++ Module Definition</h3>
90<pre>
91#include &lt;boost/python/module.hpp&gt;
92#include &lt;boost/python/class.hpp&gt;
93#include &lt;boost/utility.hpp&gt;
94#include &lt;cstring&gt;
95
96// class to be wrapped
97class Base
98{
99 public:
100   virtual char const* class_name() const { return "Base"; }
101   virtual ~Base();
102};
103
104bool is_base(Base* b)
105{
106   return !std::strcmp(b-&gt;class_name(), "Base");
107}
108
109// Wrapper code begins here
110using namespace boost::python;
111
112// Callback class
113class Base_callback : public Base
114{
115 public:
116   Base_callback(PyObject* self) : m_self(self) {}
117
118   char const* class_name() const { return <b>call_method</b>&lt;char const*&gt;(m_self, "class_name"); }
119   char const* Base_name() const { return Base::class_name(); }
120 private:
121   PyObject* const m_self;
122};
123
124using namespace boost::python;
125BOOST_PYTHON_MODULE(my_module)
126{
127    def("is_base", is_base);
128
129    class_&lt;Base,Base_callback, noncopyable&gt;("Base")
130        .def("class_name", &amp;Base_callback::Base_name)
131        ;
132
133}
134</pre>
135
136    <h3>Python Code</h3>
137<pre>
138&gt;&gt;&gt; from my_module import *
139&gt;&gt;&gt; class Derived(Base):
140...    def __init__(self):
141...       Base.__init__(self)
142...    def class_name(self):
143...       return self.__class__.__name__
144...
145&gt;&gt;&gt; is_base(Base()) # calls the class_name() method from C++
1461
147&gt;&gt;&gt; is_base(Derived())
1480
149</pre>
150
151    <p>Revised
152    <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
153  13 November, 2002
154  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
155    </p>
156
157    <p><i>&copy; Copyright <a href=
158    "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p>
159  </body>
160</html>
161
Note: See TracBrowser for help on using the repository browser.