mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 05:42:30 +00:00
156 lines
5.0 KiB
HTML
156 lines
5.0 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link rel="stylesheet" type="text/css" href="../boost.css">
|
|
<title>Boost.Python - <boost/python/make_function.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="../../../../c++boost.gif" border="0"></a></h3>
|
|
</td>
|
|
<td valign="top">
|
|
<h1 align="center">Boost.Python</h1>
|
|
<h2 align="center">Header <boost/python/make_function.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>
|
|
<dl class="page-index">
|
|
<dt><a href="#make_function-spec">make_function</a></dt>
|
|
<dt><a href="#make_constructor-spec">make_constructor</a></dt>
|
|
</dl>
|
|
|
|
<dt><a href="#examples">Example</a></dt>
|
|
|
|
</dl>
|
|
<hr>
|
|
<h2><a name="introduction"></a>Introduction</h2>
|
|
<p>
|
|
|
|
<code><a href="#make_function-spec">make_function</a>()</code> and
|
|
<code><a href="#make_constructor-spec">make_constructor</a>()</code>
|
|
are the functions used internally by <code>class_<>::<a
|
|
href="class.html#class_-spec-modifiers">def</a></code>,
|
|
<code>class_<>::<a
|
|
href="module.html#module-spec-modifiers">def</a></code>,
|
|
and
|
|
<code>class_<>::<a
|
|
href="class.html#class_-spec-modifiers">def_init</a></code>
|
|
to produce Python callable objects which wrap C++ functions and member
|
|
functions.
|
|
</p>
|
|
|
|
<h2><a name="functions"></a>Functions</h2>
|
|
|
|
<pre>
|
|
<a name="make_function-spec">template <class F>
|
|
objects::function* make_function(F f)</a>
|
|
|
|
template <class F, class Policies>
|
|
objects::function* make_function(F f, Policies const& policies)
|
|
</pre>
|
|
<dl class="function-semantics">
|
|
|
|
<dt><b>Requires:</b> <code>F</code> is a function pointer or member
|
|
function pointer type</dt>
|
|
|
|
<dt><b>Effects:</b> Creates a Python callable object which, when
|
|
called from Python, converts its arguments to C++ and calls
|
|
<code>f</code>. If <code>F</code> is a pointer-to-member-function
|
|
type, the target object of the function call (<code>*this</code>)
|
|
will be taken from the first Python argument, and subsequent Python
|
|
arguments will be used as the arguments to <code>f</code>. If
|
|
<code>policies</code> are supplied, it must be a model of <a
|
|
href="CallPolicies.html">CallPolicies</a>, and will be applied to
|
|
the function as described <a href="CallPolicies.html">here</a>.</dt>
|
|
|
|
<dt><b>Returns:</b> A pointer convertible to <code>PyObject*</code>
|
|
which refers to the new Python callable object.</dt>
|
|
</dl>
|
|
|
|
<pre>
|
|
<a name="make_constructor-spec">template <class T, class ArgList, class Generator>
|
|
objects::function* make_constructor();</a>
|
|
</pre>
|
|
<dl class="make_constructor-semantics">
|
|
|
|
<dt><b>Requires:</b> <code>T</code> is a class
|
|
type. <code>ArgList</code> is an <a
|
|
href="../../../mpl/doc/Sequences.html">MPL sequence</a> of C++
|
|
argument types (<i>A1, A2,... AN</i>) such that if
|
|
<code>a1, a2</code>... <code>aN</code> are objects of type
|
|
<i>A1, A2,... AN</i> respectively, the expression
|
|
<code>new Generator::apply<T>::type(a1, a2</code>... <code>aN</code>) is
|
|
valid. Generator is a model of <a
|
|
href="HolderGenerator.html">HolderGenerator</a>.
|
|
</dt>
|
|
|
|
<dt><b>Effects:</b> Creates a Python callable object which, when
|
|
called from Python, expects its first argument to be a Boost.Python
|
|
extension class object. It converts its remaining its arguments to
|
|
C++ and passes them to the constructor of a dynamically-allocated
|
|
<code>Generator::apply<T>::type</code> object. The result is
|
|
installed in the extension class object.
|
|
|
|
</dt>
|
|
|
|
<dt><b>Returns:</b> The new Python callable object</dt>
|
|
|
|
</dl>
|
|
|
|
<h2><a name="examples"></a>Example</h2>
|
|
|
|
<p>C++ function exposed below returns a callable object wrapping one
|
|
of two functions.
|
|
|
|
<pre>
|
|
#include <boost/python/make_function.hpp>
|
|
#include <boost/python/module.hpp>
|
|
|
|
char const* foo() { return "foo"; }
|
|
char const* bar() { return "bar"; }
|
|
|
|
PyObject* choose_function(bool selector)
|
|
{
|
|
if (selector)
|
|
return boost::python::make_function(foo);
|
|
else
|
|
return boost::python::make_function(bar);
|
|
}
|
|
|
|
BOOST_PYTHON_MODULE_INIT(make_function_test)
|
|
{
|
|
module("make_function_test")
|
|
.def("choose_function", choose_function);
|
|
}
|
|
</pre>
|
|
</p>
|
|
It can be used this way in Python:
|
|
<pre>
|
|
>>> from make_function_test import *
|
|
>>> f = choose_function(1)
|
|
>>> g = choose_function(0)
|
|
>>> f()
|
|
'foo'
|
|
>>> g()
|
|
'bar'
|
|
</pre>
|
|
|
|
<p>
|
|
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
|
14 February 2002
|
|
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
|
</p>
|
|
<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a>
|
|
2002. All Rights Reserved.</i></p>
|
|
</body>
|
|
</html>
|