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/overloads.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/overloads.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= |
---|
40 | "#overload-dispatch-expression"><i>overload-dispatch-expressions</i></a></dt> |
---|
41 | |
---|
42 | <dt><a href= "#OverloadDispatcher-concept">OverloadDispatcher</a> concept</dt> |
---|
43 | |
---|
44 | <dt><a href="#macros">Macros</a></dt> |
---|
45 | |
---|
46 | <dd> |
---|
47 | <dl class="page-index"> |
---|
48 | <dt><a href= |
---|
49 | "#BOOST_PYTHON_FUNCTION_OVERLOADS-spec">BOOST_PYTHON_FUNCTION_OVERLOADS</a></dt> |
---|
50 | |
---|
51 | <dt><a href= |
---|
52 | "#BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS-spec">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</a></dt> |
---|
53 | </dl> |
---|
54 | </dd> |
---|
55 | |
---|
56 | <dt><a href="#examples">Example(s)</a></dt> |
---|
57 | </dl> |
---|
58 | <hr> |
---|
59 | |
---|
60 | <h2><a name="introduction"></a>Introduction</h2> |
---|
61 | |
---|
62 | <p>Defines facilities for generating families of overloaded Python |
---|
63 | functions and extension class methods from C++ functions and |
---|
64 | member functions with default arguments, or from similar families |
---|
65 | of C++ overloads</p> |
---|
66 | |
---|
67 | <h2><a name= |
---|
68 | "overload-dispatch-expression"></a><i>overload-dispatch-expressions</i></h2> |
---|
69 | |
---|
70 | <p> |
---|
71 | An <em>overload-dispatch-expression</em> is used to describe a |
---|
72 | family of overloaded methods to be generated for an extension |
---|
73 | class. It has the following properties: |
---|
74 | |
---|
75 | <blockquote> |
---|
76 | <dl class="properties"> |
---|
77 | <dt><b>docstring:</b> An <a href="definitions.html#ntbs">ntbs</a> |
---|
78 | whose value will bound to the methods' <code>__doc__</code> |
---|
79 | attribute</dt> |
---|
80 | |
---|
81 | <dt><b>keywords:</b> A <a href= |
---|
82 | "args.html#keyword-expression">keyword-expression</a> which |
---|
83 | will be used to name (a trailing subsequence of) the arguments |
---|
84 | to the generated methods.</dt> |
---|
85 | |
---|
86 | <dt><b>call policies:</b> An instance of some type which models <a href= |
---|
87 | "CallPolicies.html">CallPolicies</a>.</dt> |
---|
88 | |
---|
89 | <dt><b>minimum <a href="definitions.html#arity">arity</a></b> |
---|
90 | The minimum number of arguments to be accepted by a generated |
---|
91 | method overload.</dt> |
---|
92 | |
---|
93 | <dt><b>maximum <a href="definitions.html#arity">arity</a></b> |
---|
94 | The maximum number of arguments to be accepted by a generated |
---|
95 | method overload.</dt> |
---|
96 | </dl> |
---|
97 | </blockquote> |
---|
98 | |
---|
99 | <h2><a name="OverloadDispatcher-concept"></a>OverloadDispatcher Concept</h2> |
---|
100 | |
---|
101 | An OverloadDispatcher <code>X</code> is a class which has a |
---|
102 | <em>minimum arity</em> and a <em>maximum arity</em>, and for which |
---|
103 | the following following are valid <a |
---|
104 | href="#overload-dispatch-expression"><em>overload-dispatch-expression</em></a>s, |
---|
105 | with the same minimum and maximum arity as the OverloadDispatcher. |
---|
106 | |
---|
107 | <pre> |
---|
108 | X() |
---|
109 | X(docstring) |
---|
110 | X(docstring, keywords) |
---|
111 | X(keywords, docstring) |
---|
112 | X()[policies] |
---|
113 | X(docstring)[policies] |
---|
114 | X(docstring, keywords)[policies] |
---|
115 | X(keywords, docstring)[policies] |
---|
116 | </pre> |
---|
117 | |
---|
118 | <ul> |
---|
119 | <li>If <code>policies</code> are supplied, it must be an instance of a |
---|
120 | type which models <a |
---|
121 | href="CallPolicies.html#CallPolicies-concept">CallPolicies</a>, and |
---|
122 | will be used as the result's call policies. Otherwise the result's |
---|
123 | call policies will be an instance of <a |
---|
124 | href="default_call_policies.html#default_call_policies-spec">default_call_policies</a>. |
---|
125 | |
---|
126 | <li>If <code>docstring</code> is supplied it must be an <a |
---|
127 | href="definitions.html#ntbs">ntbs</a>, and will be used as the result's docstring. Otherwise the result has an empty docstring. |
---|
128 | |
---|
129 | <li>If <code>keywords</code> is supplied it must be the result of a <a |
---|
130 | href= "args.html#keyword-expression">keyword-expression</a> |
---|
131 | whose length is no greater than <code>X</code>'s maximum |
---|
132 | arity, and will be used as the result's keywords. Otherwise |
---|
133 | the result's keywords will be empty. |
---|
134 | </ul> |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | <h2><a name="macros"></a>Macros</h2> |
---|
140 | |
---|
141 | <h3><a name= |
---|
142 | "BOOST_PYTHON_FUNCTION_OVERLOADS-spec">BOOST_PYTHON_FUNCTION_OVERLOADS(name, func_id, min_args, max_args)</a></h3> |
---|
143 | Expands to the definition of an OverloadDispatcher called |
---|
144 | <code>name</code> in the current scope which can be used to |
---|
145 | generate the following function invocation: |
---|
146 | <pre> |
---|
147 | func_id(a<small><i>1</i></small>, a<small><i>2</i></small>,...a<small><i>i</i></small>); |
---|
148 | </pre> |
---|
149 | |
---|
150 | for all <code>min_args</code> <= <i>i</i> <= <code>max_args</code>. |
---|
151 | |
---|
152 | <h3><a name= |
---|
153 | "BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS-spec">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(name, member_name, min_args, max_args)</a></h3> |
---|
154 | |
---|
155 | Expands to the definition of an OverloadDispatcher called |
---|
156 | <code>name</code> in the current scope which can be used to |
---|
157 | generate the following function invocation: |
---|
158 | <pre> |
---|
159 | x.member_name(a<small><i>1</i></small>, a<small><i>2</i></small>,...a<small><i>i</i></small>); |
---|
160 | </pre> |
---|
161 | |
---|
162 | for all <code>min_args</code> <= <i>i</i> <= |
---|
163 | <code>max_args</code>, where <code>x</code> is a reference to an |
---|
164 | object of class type. |
---|
165 | |
---|
166 | <h2><a name="examples"></a>Example(s)</h2> |
---|
167 | |
---|
168 | <pre> |
---|
169 | #include <boost/python/module.hpp> |
---|
170 | #include <boost/python/def.hpp> |
---|
171 | #include <boost/python/args.hpp> |
---|
172 | #include <boost/python/tuple.hpp> |
---|
173 | #include <boost/python/class.hpp> |
---|
174 | #include <boost/python/overloads.hpp> |
---|
175 | #include <boost/python/return_internal_reference.hpp> |
---|
176 | |
---|
177 | using namespace boost::python; |
---|
178 | |
---|
179 | tuple f(int x = 1, double y = 4.25, char const* z = "wow") |
---|
180 | { |
---|
181 | return make_tuple(x, y, z); |
---|
182 | } |
---|
183 | |
---|
184 | BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads, f, 0, 3) |
---|
185 | |
---|
186 | struct Y {}; |
---|
187 | struct X |
---|
188 | { |
---|
189 | Y& f(int x, double y = 4.25, char const* z = "wow") |
---|
190 | { |
---|
191 | return inner; |
---|
192 | } |
---|
193 | Y inner; |
---|
194 | }; |
---|
195 | |
---|
196 | BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(f_member_overloads, f, 1, 3) |
---|
197 | |
---|
198 | BOOST_PYTHON_MODULE(args_ext) |
---|
199 | { |
---|
200 | def("f", f, |
---|
201 | f_overloads( |
---|
202 | args("x", "y", "z"), "This is f's docstring" |
---|
203 | )); |
---|
204 | |
---|
205 | |
---|
206 | class_<Y>("Y") |
---|
207 | ; |
---|
208 | |
---|
209 | class_<X>("X", "This is X's docstring") |
---|
210 | .def("f1", &X::f, |
---|
211 | f_member_overloads( |
---|
212 | args("x", "y", "z"), "f's docstring" |
---|
213 | )[return_internal_reference<>()] |
---|
214 | ) |
---|
215 | ; |
---|
216 | } |
---|
217 | </pre> |
---|
218 | |
---|
219 | <p>Revised |
---|
220 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
221 | 15 April, 2003 |
---|
222 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
223 | </p> |
---|
224 | |
---|
225 | <p><i>© Copyright <a href= |
---|
226 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
227 | </body> |
---|
228 | </html> |
---|
229 | |
---|