diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-06-25 22:59:01 +0000 |
---|---|---|
committer | <> | 2013-09-27 11:49:28 +0000 |
commit | 8c4528713d907ee2cfd3bfcbbad272c749867f84 (patch) | |
tree | c09e2ce80f47b90c85cc720f5139089ad9c8cfff /libs/python/doc/v2/call_method.html | |
download | boost-tarball-baserock/morph.tar.gz |
Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_54_0.tar.bz2.boost_1_54_0baserock/morph
Diffstat (limited to 'libs/python/doc/v2/call_method.html')
-rw-r--r-- | libs/python/doc/v2/call_method.html | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/libs/python/doc/v2/call_method.html b/libs/python/doc/v2/call_method.html new file mode 100644 index 000000000..e54ffb26a --- /dev/null +++ b/libs/python/doc/v2/call_method.html @@ -0,0 +1,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> + |