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 Cygwin (vers 1st April 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/def.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 <boost/python/def.hpp></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="#def-spec">def</a></dt> |
---|
44 | </dl> |
---|
45 | </dd> |
---|
46 | |
---|
47 | <dt><a href="#examples">Example</a></dt> |
---|
48 | </dl> |
---|
49 | <hr> |
---|
50 | |
---|
51 | <h2><a name="introduction"></a>Introduction</h2> |
---|
52 | |
---|
53 | <p><code><a href="#def-spec">def</a>()</code> is the function which can |
---|
54 | be used to expose C++ functions and callable objects as Python functions |
---|
55 | in the current <code><a href="scope.html">scope</a></code>.</p> |
---|
56 | |
---|
57 | <h2><a name="functions"></a>Functions</h2> |
---|
58 | <a name="def-spec"></a>def |
---|
59 | <pre> |
---|
60 | template <class F> |
---|
61 | void def(char const* name, F f); |
---|
62 | |
---|
63 | template <class Fn, class A1> |
---|
64 | void def(char const* name, Fn fn, A1 const&); |
---|
65 | |
---|
66 | template <class Fn, class A1, class A2> |
---|
67 | void def(char const* name, Fn fn, A1 const&, A2 const&); |
---|
68 | |
---|
69 | template <class Fn, class A1, class A2, class A3> |
---|
70 | void def(char const* name, Fn fn, A1 const&, A2 const&, A3 const&); |
---|
71 | </pre> |
---|
72 | |
---|
73 | <dl class="function-semantics"> |
---|
74 | <dt><b>Requires:</b> <code>name</code> is an <a href= |
---|
75 | "definitions.html#ntbs">ntbs</a> which conforms to Python's <a href= |
---|
76 | "http://www.python.org/doc/current/ref/identifiers.html">identifier |
---|
77 | naming rules</a>.</dt> |
---|
78 | |
---|
79 | <dd> |
---|
80 | <ul> |
---|
81 | <li>If <code>Fn</code> is [derived from] <code><a href= |
---|
82 | "object.html#object-spec">object</a></code>, it will be added to |
---|
83 | the current scope as a single overload. To be useful, |
---|
84 | <code>fn</code> should be <a href= |
---|
85 | "http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6">callable</a>.</li> |
---|
86 | |
---|
87 | <li> |
---|
88 | If <code>a1</code> is the result of an <a href= |
---|
89 | "overloads.html#overload-dispatch-expression"><em>overload-dispatch-expression</em></a>, |
---|
90 | only the second form is allowed and fn must be a pointer to |
---|
91 | function or pointer to member function whose <a href= |
---|
92 | "definitions.html#arity">arity</a> is the same as A1's <a href= |
---|
93 | "overloads.html#overload-dispatch-expression"><em>maximum |
---|
94 | arity</em></a>. |
---|
95 | |
---|
96 | <dl> |
---|
97 | <dt><b>Effects:</b> For each prefix <em>P</em> of |
---|
98 | <code>Fn</code>'s sequence of argument types, beginning with |
---|
99 | the one whose length is <code>A1</code>'s <a href= |
---|
100 | "overloads.html#overload-dispatch-expression"><em>minimum |
---|
101 | arity</em></a>, adds a |
---|
102 | <code><em>name</em>(</code>...<code>)</code> function overload |
---|
103 | to the <a href="scope.html">current scope</a>. Each overload |
---|
104 | generated invokes <code>a1</code>'s call-expression with |
---|
105 | <em>P</em>, using a copy of <code>a1</code>'s <a href= |
---|
106 | "CallPolicies.html">call policies</a>. If the longest valid |
---|
107 | prefix of <code>A1</code> contains <em>N</em> types and |
---|
108 | <code>a1</code> holds <em>M</em> keywords, an initial sequence |
---|
109 | of the keywords are used for all but the first |
---|
110 | <em>N</em> - <em>M</em> arguments of each |
---|
111 | overload.<br> |
---|
112 | </dt> |
---|
113 | </dl> |
---|
114 | </li> |
---|
115 | |
---|
116 | <li>Otherwise, fn must be a non-null function or member function |
---|
117 | pointer, and a single function overload built around fn is added to |
---|
118 | the <a href="scope.html">current scope</a>. If any of |
---|
119 | <code>a1</code>-<code>a3</code> are supplied, they may be selected |
---|
120 | in any order from the table below.</li> |
---|
121 | </ul> |
---|
122 | |
---|
123 | <table border="1" summary="def() optional arguments"> |
---|
124 | <tr> |
---|
125 | <th>Memnonic Name</th> |
---|
126 | |
---|
127 | <th>Requirements/Type properties</th> |
---|
128 | |
---|
129 | <th>Effects</th> |
---|
130 | </tr> |
---|
131 | |
---|
132 | <tr> |
---|
133 | <td>docstring</td> |
---|
134 | |
---|
135 | <td>Any <a href="definitions.html#ntbs">ntbs</a>.</td> |
---|
136 | |
---|
137 | <td>Value will be bound to the <code>__doc__</code> attribute of |
---|
138 | the resulting method overload.</td> |
---|
139 | </tr> |
---|
140 | |
---|
141 | <tr> |
---|
142 | <td>policies</td> |
---|
143 | |
---|
144 | <td>A model of <a href="CallPolicies.html">CallPolicies</a></td> |
---|
145 | |
---|
146 | <td>A copy will be used as the call policies of the resulting |
---|
147 | method overload.</td> |
---|
148 | </tr> |
---|
149 | |
---|
150 | <tr> |
---|
151 | <td>keywords</td> |
---|
152 | |
---|
153 | <td>The result of a <a href= |
---|
154 | "args.html#keyword-expression"><em>keyword-expression</em></a> |
---|
155 | specifying no more arguments than the <a href= |
---|
156 | "definitions.html#arity">arity</a> of <code>fn</code>.</td> |
---|
157 | |
---|
158 | <td>A copy will be used as the call policies of the resulting |
---|
159 | method overload.</td> |
---|
160 | </tr> |
---|
161 | </table> |
---|
162 | </dd> |
---|
163 | </dl> |
---|
164 | |
---|
165 | <h2><a name="examples"></a>Example</h2> |
---|
166 | <pre> |
---|
167 | #include <boost/python/def.hpp> |
---|
168 | #include <boost/python/module.hpp> |
---|
169 | #include <boost/python/args.hpp> |
---|
170 | |
---|
171 | using namespace boost::python; |
---|
172 | |
---|
173 | char const* foo(int x, int y) { return "foo"; } |
---|
174 | |
---|
175 | BOOST_PYTHON_MODULE(def_test) |
---|
176 | { |
---|
177 | def("foo", foo, args("x", "y"), "foo's docstring"); |
---|
178 | } |
---|
179 | </pre> |
---|
180 | |
---|
181 | <p> |
---|
182 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
183 | 7 March, 2003 |
---|
184 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
185 | </p> |
---|
186 | |
---|
187 | <p><i>© Copyright <a href= |
---|
188 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
189 | </body> |
---|
190 | </html> |
---|
191 | |
---|