|
| 1 | +<html> |
| 2 | +<head> |
| 3 | +<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 4 | +<link rel="stylesheet" type="text/css" href="../boost.css"> |
| 5 | +<title>Boost.Python - <boost/python/make_function.hpp></title> |
| 6 | +</head> |
| 7 | +<body link="#0000ff" vlink="#800080"> |
| 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="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3> |
| 13 | + </td> |
| 14 | + <td valign="top"> |
| 15 | + <h1 align="center">Boost.Python</h1> |
| 16 | + <h2 align="center">Header <boost/python/make_function.hpp></h2> |
| 17 | + </td> |
| 18 | + </tr> |
| 19 | +</table> |
| 20 | +<hr> |
| 21 | +<h2>Contents</h2> |
| 22 | +<dl class="page-index"> |
| 23 | + <dt><a href="#introduction">Introduction</a></dt> |
| 24 | + |
| 25 | + <dt><a href="#functions">Functions</a></dt> |
| 26 | + <dl class="page-index"> |
| 27 | + <dt><a href="#make_function-spec">make_function</a></dt> |
| 28 | + <dt><a href="#make_constructor-spec">make_constructor</a></dt> |
| 29 | + </dl> |
| 30 | + |
| 31 | + <dt><a href="#examples">Example</a></dt> |
| 32 | + |
| 33 | +</dl> |
| 34 | +<hr> |
| 35 | +<h2><a name="introduction"></a>Introduction</h2> |
| 36 | +<p> |
| 37 | + |
| 38 | +<code><a href="#make_function-spec">make_function</a>()</code> and |
| 39 | +<code><a href="#make_constructor-spec">make_constructor</a>()</code> |
| 40 | +are the functions used internally by <code>class_<>::<a |
| 41 | +href="class.html#class_-spec-modifiers">def</a></code>, |
| 42 | +<code>class_<>::<a |
| 43 | +href="module.html#module-spec-modifiers">def</a></code>, |
| 44 | +and |
| 45 | +<code>class_<>::<a |
| 46 | +href="class.html#class_-spec-modifiers">def_init</a></code> |
| 47 | +to produce Python callable objects which wrap C++ functions and member |
| 48 | +functions. |
| 49 | +</p> |
| 50 | + |
| 51 | +<h2><a name="functions"></a>Functions</h2> |
| 52 | + |
| 53 | +<pre> |
| 54 | +<a name="make_function-spec">template <class F> |
| 55 | +objects::function* make_function(F f)</a> |
| 56 | + |
| 57 | +template <class F, class Policies> |
| 58 | +objects::function* make_function(F f, Policies const& policies) |
| 59 | +</pre> |
| 60 | +<dl class="function-semantics"> |
| 61 | + |
| 62 | + <dt><b>Requires:</b> <code>F</code> is a function pointer or member |
| 63 | + function pointer type</dt> |
| 64 | + |
| 65 | + <dt><b>Effects:</b> Creates a Python callable object which, when |
| 66 | + called from Python, converts its arguments to C++ and calls |
| 67 | + <code>f</code>. If <code>F</code> is a pointer-to-member-function |
| 68 | + type, the target object of the function call (<code>*this</code>) |
| 69 | + will be taken from the first Python argument, and subsequent Python |
| 70 | + arguments will be used as the arguments to <code>f</code>. If |
| 71 | + <code>policies</code> are supplied, it must be a model of <a |
| 72 | + href="CallPolicies.html">CallPolicies</a>, and will be applied to |
| 73 | + the function as described <a href="CallPolicies.html">here</a>.</dt> |
| 74 | + |
| 75 | + <dt><b>Returns:</b> A pointer convertible to <code>PyObject*</code> |
| 76 | + which refers to the new Python callable object.</dt> |
| 77 | +</dl> |
| 78 | + |
| 79 | +<pre> |
| 80 | +<a name="make_constructor-spec">template <class T, class ArgList, class Generator> |
| 81 | +objects::function* make_constructor();</a> |
| 82 | +</pre> |
| 83 | +<dl class="make_constructor-semantics"> |
| 84 | + |
| 85 | + <dt><b>Requires:</b> <code>T</code> is a class |
| 86 | + type. <code>ArgList</code> is an <a |
| 87 | + href="../../../mpl/doc/Sequences.html">MPL sequence</a> of C++ |
| 88 | + argument types (<i>A1, A2,... AN</i>) such that if |
| 89 | + <code>a1, a2</code>... <code>aN</code> are objects of type |
| 90 | + <i>A1, A2,... AN</i> respectively, the expression |
| 91 | + <code>new Generator::apply<T>::type(a1, a2</code>... <code>aN</code>) is |
| 92 | + valid. Generator is a model of <a |
| 93 | + href="HolderGenerator.html">HolderGenerator</a>. |
| 94 | +</dt> |
| 95 | + |
| 96 | + <dt><b>Effects:</b> Creates a Python callable object which, when |
| 97 | + called from Python, expects its first argument to be a Boost.Python |
| 98 | + extension class object. It converts its remaining its arguments to |
| 99 | + C++ and passes them to the constructor of a dynamically-allocated |
| 100 | + <code>Generator::apply<T>::type</code> object. The result is |
| 101 | + installed in the extension class object. |
| 102 | + |
| 103 | +</dt> |
| 104 | + |
| 105 | + <dt><b>Returns:</b> The new Python callable object</dt> |
| 106 | + |
| 107 | +</dl> |
| 108 | + |
| 109 | +<h2><a name="examples"></a>Example</h2> |
| 110 | + |
| 111 | +<p>C++ function exposed below returns a callable object wrapping one |
| 112 | +of two functions. |
| 113 | + |
| 114 | +<pre> |
| 115 | +#include <boost/python/make_function.hpp> |
| 116 | +#include <boost/python/module.hpp> |
| 117 | + |
| 118 | +char const* foo() { return "foo"; } |
| 119 | +char const* bar() { return "bar"; } |
| 120 | + |
| 121 | +PyObject* choose_function(bool selector) |
| 122 | +{ |
| 123 | + if (selector) |
| 124 | + return boost::python::make_function(foo); |
| 125 | + else |
| 126 | + return boost::python::make_function(bar); |
| 127 | +} |
| 128 | + |
| 129 | +BOOST_PYTHON_MODULE_INIT(make_function_test) |
| 130 | +{ |
| 131 | + module("make_function_test") |
| 132 | + .def("choose_function", choose_function); |
| 133 | +} |
| 134 | +</pre> |
| 135 | +</p> |
| 136 | +It can be used this way in Python: |
| 137 | +<pre> |
| 138 | +>>> from make_function_test import * |
| 139 | +>>> f = choose_function(1) |
| 140 | +>>> g = choose_function(0) |
| 141 | +>>> f() |
| 142 | +'foo' |
| 143 | +>>> g() |
| 144 | +'bar' |
| 145 | +</pre> |
| 146 | + |
| 147 | +<p> |
| 148 | +<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
| 149 | +14 February 2002 |
| 150 | +<!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
| 151 | +</p> |
| 152 | +<p><i>© Copyright <a href="../../../../people/dave_abrahams.htm">Dave Abrahams</a> |
| 153 | + 2002. All Rights Reserved.</i></p> |
| 154 | +</body> |
| 155 | +</html> |
0 commit comments