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 - <boost/python/make_function.hpp></title> |
---|
14 | </head> |
---|
15 | |
---|
16 | <body> |
---|
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 |
---|
29 | <boost/python/make_function.hpp></h2> |
---|
30 | </td> |
---|
31 | </tr> |
---|
32 | </table> |
---|
33 | <hr> |
---|
34 | |
---|
35 | <h2>Contents</h2> |
---|
36 | |
---|
37 | <dl class="page-index"> |
---|
38 | <dt><a href="#introduction">Introduction</a></dt> |
---|
39 | |
---|
40 | <dt><a href="#functions">Functions</a></dt> |
---|
41 | |
---|
42 | <dd> |
---|
43 | <dl class="page-index"> |
---|
44 | <dt><a href="#make_function-spec">make_function</a></dt> |
---|
45 | |
---|
46 | <dt><a href="#make_constructor-spec">make_constructor</a></dt> |
---|
47 | </dl> |
---|
48 | </dd> |
---|
49 | |
---|
50 | <dt><a href="#examples">Example</a></dt> |
---|
51 | </dl> |
---|
52 | <hr> |
---|
53 | |
---|
54 | <h2><a name="introduction"></a>Introduction</h2> |
---|
55 | |
---|
56 | <p><code><a href="#make_function-spec">make_function</a>()</code> and |
---|
57 | <code><a href="#make_constructor-spec">make_constructor</a>()</code> are |
---|
58 | the functions used internally by <code><a href= |
---|
59 | "def.html#def-spec">def</a>()</code> and <code>class_<>::<a href= |
---|
60 | "class.html#class_-spec-modifiers">def</a>()</code> to produce Python |
---|
61 | callable objects which wrap C++ functions and member functions.</p> |
---|
62 | |
---|
63 | <h2><a name="functions"></a>Functions</h2> |
---|
64 | <pre> |
---|
65 | <a name="make_function-spec">template <class F></a> |
---|
66 | <a href="object.html#object-spec">object</a> make_function(F f) |
---|
67 | |
---|
68 | template <class F, class Policies> |
---|
69 | <a href= |
---|
70 | "object.html#object-spec">object</a> make_function(F f, Policies const& policies) |
---|
71 | |
---|
72 | template <class F, class Policies, class KeywordsOrSignature> |
---|
73 | <a href= |
---|
74 | "object.html#object-spec">object</a> make_function(F f, Policies const& policies, KeywordsOrSignature const& ks) |
---|
75 | |
---|
76 | template <class F, class Policies, class Keywords, class Signature> |
---|
77 | <a href= |
---|
78 | "object.html#object-spec">object</a> make_function(F f, Policies const& policies, Keywords const& kw, Signature const& sig) |
---|
79 | </pre> |
---|
80 | |
---|
81 | <dl class="function-semantics"> |
---|
82 | <dt><b>Requires:</b> <code>F</code> is a function pointer or member |
---|
83 | function pointer type. If <code>policies</code> are supplied, it must |
---|
84 | be a model of <a href="CallPolicies.html">CallPolicies</a>. If |
---|
85 | <code>kewords</code> are supplied, it must be the result of a <a href= |
---|
86 | "args.html#keyword-expression"><em>keyword-expression</em></a> |
---|
87 | specifying no more arguments than the <a href= |
---|
88 | "definitions.html#arity">arity</a> of <code>f</code>.</dt> |
---|
89 | |
---|
90 | <dt><b>Effects:</b> Creates a Python callable object which, when called |
---|
91 | from Python, converts its arguments to C++ and calls <code>f</code>. If |
---|
92 | <code>F</code> is a pointer-to-member-function type, the target |
---|
93 | object of the function call (<code>*this</code>) will be taken |
---|
94 | from the first Python argument, and subsequent Python arguments |
---|
95 | will be used as the arguments |
---|
96 | to <code>f</code>. <ul> |
---|
97 | <li> If <code>policies</code> are supplied, it |
---|
98 | will be applied to the function as described <a href= |
---|
99 | "CallPolicies.html">here</a>. |
---|
100 | <li>If <code>keywords</code> are |
---|
101 | supplied, the keywords will be applied in order to the final |
---|
102 | arguments of the resulting function. |
---|
103 | <li>If <code>Signature</code> |
---|
104 | is supplied, it should be an instance of an <a |
---|
105 | href="../../../mpl/doc/refmanual/front-extensible-sequence.html">MPL front-extensible |
---|
106 | sequence</a> representing the function's return type followed by |
---|
107 | its argument types. Pass a <code>Signature</code> when wrapping |
---|
108 | function object types whose signatures can't be deduced, or when |
---|
109 | you wish to override the types which will be passed to the |
---|
110 | wrapped function. |
---|
111 | </ul></dt> |
---|
112 | |
---|
113 | <dt><b>Returns:</b> An instance of <a href= |
---|
114 | "object.html#object-spec">object</a> which holds the new Python |
---|
115 | callable object.</dt> |
---|
116 | |
---|
117 | <dt><b>Caveats:</b> An argument of pointer type may |
---|
118 | be <code>0</code> if <code>None</code> is passed from Python. |
---|
119 | An argument type which is a constant reference may refer to a |
---|
120 | temporary which was created from the Python object for just the |
---|
121 | duration of the call to the wrapped function, for example |
---|
122 | a <code>std::vector</code> conjured up by the conversion process |
---|
123 | from a Python list. Use a non-<code>const</code> reference |
---|
124 | argument when a persistent lvalue is required. |
---|
125 | </dl> |
---|
126 | |
---|
127 | <pre> |
---|
128 | <a name= |
---|
129 | "make_constructor-spec"></a>template <class T, class ArgList, class Generator> |
---|
130 | <a href="object.html#object-spec">object</a> make_constructor(); |
---|
131 | |
---|
132 | template <class ArgList, class Generator, class Policies> |
---|
133 | <a href= |
---|
134 | "object.html#object-spec">object</a> make_constructor(Policies const& policies) |
---|
135 | </pre> |
---|
136 | |
---|
137 | <dl class="function-semantics"> |
---|
138 | <dt><b>Requires:</b> <code>T</code> is a class type. |
---|
139 | <code>Policies</code> is a model of <a href= |
---|
140 | "CallPolicies.html">CallPolicies</a>. <code>ArgList</code> is an <a |
---|
141 | href="../../../mpl/doc/refmanual/forward-sequence.html">MPL sequence</a> of C++ argument |
---|
142 | types (<i>A1, A2,... AN</i>) such that if |
---|
143 | <code>a1, a2</code>... <code>aN</code> are objects of type |
---|
144 | <i>A1, A2,... AN</i> respectively, the expression <code>new |
---|
145 | Generator::apply<T>::type(a1, a2</code>... <code>aN</code>) |
---|
146 | is valid. Generator is a model of <a href= |
---|
147 | "HolderGenerator.html">HolderGenerator</a>.</dt> |
---|
148 | |
---|
149 | <dt><b>Effects:</b> Creates a Python callable object which, when called |
---|
150 | from Python, expects its first argument to be a Boost.Python extension |
---|
151 | class object. It converts its remaining its arguments to C++ and passes |
---|
152 | them to the constructor of a dynamically-allocated |
---|
153 | <code>Generator::apply<T>::type</code> object, which is then |
---|
154 | installed in the extension class object. In the second form, the |
---|
155 | <code>policies</code> are applied to the arguments and result (<a href= |
---|
156 | "http://www.python.org/doc/current/lib/bltin-null-object.html">None</a>) |
---|
157 | of the Python callable object</dt> |
---|
158 | |
---|
159 | <dt><b>Returns:</b> An instance of <a href= |
---|
160 | "object.html#object-spec">object</a> which holds the new Python |
---|
161 | callable object.</dt> |
---|
162 | </dl> |
---|
163 | |
---|
164 | <h2><a name="examples"></a>Example</h2> |
---|
165 | |
---|
166 | <p>C++ function exposed below returns a callable object wrapping one of |
---|
167 | two functions.</p> |
---|
168 | <pre> |
---|
169 | #include <boost/python/make_function.hpp> |
---|
170 | #include <boost/python/module.hpp> |
---|
171 | |
---|
172 | char const* foo() { return "foo"; } |
---|
173 | char const* bar() { return "bar"; } |
---|
174 | |
---|
175 | using namespace boost::python; |
---|
176 | object choose_function(bool selector) |
---|
177 | { |
---|
178 | if (selector) |
---|
179 | return boost::python::make_function(foo); |
---|
180 | else |
---|
181 | return boost::python::make_function(bar); |
---|
182 | } |
---|
183 | |
---|
184 | BOOST_PYTHON_MODULE(make_function_test) |
---|
185 | { |
---|
186 | def("choose_function", choose_function); |
---|
187 | } |
---|
188 | </pre> |
---|
189 | It can be used this way in Python: |
---|
190 | <pre> |
---|
191 | >>> from make_function_test import * |
---|
192 | >>> f = choose_function(1) |
---|
193 | >>> g = choose_function(0) |
---|
194 | >>> f() |
---|
195 | 'foo' |
---|
196 | >>> g() |
---|
197 | 'bar' |
---|
198 | </pre> |
---|
199 | |
---|
200 | <p> |
---|
201 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
202 | 13 November, 2002 |
---|
203 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
204 | </p> |
---|
205 | |
---|
206 | <p><i>© Copyright <a href= |
---|
207 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
208 | </body> |
---|
209 | </html> |
---|
210 | |
---|