1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>boost/python/overloads.hpp</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Python Reference Manual"> 8<link rel="up" href="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation"> 9<link rel="prev" href="boost_python_make_function_hpp.html" title="boost/python/make_function.hpp"> 10<link rel="next" href="boost_python_ptr_hpp.html" title="boost/python/ptr.hpp"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="boost_python_make_function_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ptr_hpp.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="function_invocation_and_creation.boost_python_overloads_hpp"></a><a class="link" href="boost_python_overloads_hpp.html" title="boost/python/overloads.hpp">boost/python/overloads.hpp</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction">Introduction</a></span></dt> 24<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept">OverloadDispatcher 25 Concept</a></span></dt> 26<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.macros">Macros</a></span></dt> 27<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.example">Example</a></span></dt> 28</dl></div> 29<div class="section"> 30<div class="titlepage"><div><div><h3 class="title"> 31<a name="function_invocation_and_creation.boost_python_overloads_hpp.introduction"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction" title="Introduction">Introduction</a> 32</h3></div></div></div> 33<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions">overload-dispatch-expressions</a></span></dt></dl></div> 34<p> 35 Defines facilities for generating families of overloaded Python functions 36 and extension class methods from C++ functions and member functions with 37 default arguments, or from similar families of C++ overloads 38 </p> 39<div class="section"> 40<div class="titlepage"><div><div><h4 class="title"> 41<a name="function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions" title="overload-dispatch-expressions">overload-dispatch-expressions</a> 42</h4></div></div></div> 43<p> 44 An overload-dispatch-expression is used to describe a family of overloaded 45 methods to be generated for an extension class. It has the following 46 properties: 47 </p> 48<div class="variablelist"> 49<p class="title"><b></b></p> 50<dl class="variablelist"> 51<dt><span class="term">docstring</span></dt> 52<dd><p> 53 An <a class="link" href="../glossary.html#ntbs">ntbs</a> whose value will bound to the 54 methods' <code class="computeroutput"><span class="identifier">__doc__</span></code> 55 attribute 56 </p></dd> 57<dt><span class="term">keywords</span></dt> 58<dd><p> 59 A <a class="link" href="../function_invocation_and_creation.html#function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions" title="keyword-expressions">keyword-expression</a> 60 which will be used to name (a trailing subsequence of) the arguments 61 to the generated methods. 62 </p></dd> 63<dt><span class="term">call policies</span></dt> 64<dd><p> 65 An instance of some type which models CallPolicies. 66 </p></dd> 67<dt><span class="term">minimum arity</span></dt> 68<dd><p> 69 The minimum number of arguments to be accepted by a generated method 70 overload. 71 </p></dd> 72<dt><span class="term">maximum arity</span></dt> 73<dd><p> 74 The maximum number of arguments to be accepted by a generated method 75 overload. 76 </p></dd> 77</dl> 78</div> 79</div> 80</div> 81<div class="section"> 82<div class="titlepage"><div><div><h3 class="title"> 83<a name="function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept" title="OverloadDispatcher Concept">OverloadDispatcher 84 Concept</a> 85</h3></div></div></div> 86<p> 87 An OverloadDispatcher X is a class which has a minimum arity and a maximum 88 arity, and for which the following following are valid overload-dispatch-expressions, 89 with the same minimum and maximum arity as the OverloadDispatcher. 90 </p> 91<pre class="programlisting"><span class="identifier">X</span><span class="special">()</span> 92<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">)</span> 93<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">,</span> <span class="identifier">keywords</span><span class="special">)</span> 94<span class="identifier">X</span><span class="special">(</span><span class="identifier">keywords</span><span class="special">,</span> <span class="identifier">docstring</span><span class="special">)</span> 95<span class="identifier">X</span><span class="special">()[</span><span class="identifier">policies</span><span class="special">]</span> 96<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span> 97<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">,</span> <span class="identifier">keywords</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span> 98<span class="identifier">X</span><span class="special">(</span><span class="identifier">keywords</span><span class="special">,</span> <span class="identifier">docstring</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span> 99</pre> 100<p> 101 * If policies are supplied, it must be an instance of a type which models 102 <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>, and will be 103 used as the result's call policies. Otherwise the result's call policies 104 will be an instance of <a class="link" href="models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_default_call_polici" title="boost/python/default_call_policies.hpp"><code class="computeroutput"><span class="identifier">default_call_policies</span></code></a>. * If docstring 105 is supplied it must be an <a class="link" href="../glossary.html#ntbs">ntbs</a>, and will be 106 used as the result's docstring. Otherwise the result has an empty docstring. 107 * If keywords is supplied it must be the result of a <a class="link" href="../function_invocation_and_creation.html#function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions" title="keyword-expressions">keyword-expression</a> 108 whose length is no greater than X's maximum arity, and will be used as 109 the result's keywords. Otherwise the result's keywords will be empty. 110 </p> 111</div> 112<div class="section"> 113<div class="titlepage"><div><div><h3 class="title"> 114<a name="function_invocation_and_creation.boost_python_overloads_hpp.macros"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.macros" title="Macros">Macros</a> 115</h3></div></div></div> 116<pre class="programlisting"><span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">func_id</span><span class="special">,</span> <span class="identifier">min_args</span><span class="special">,</span> <span class="identifier">max_args</span><span class="special">)</span> 117</pre> 118<p> 119 Expands to the definition of an OverloadDispatcher called name in the current 120 scope which can be used to generate the following function invocation: 121 </p> 122<pre class="programlisting"><span class="identifier">func_id</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">ai</span><span class="special">);</span></pre> 123<p> 124 for all <code class="computeroutput"><span class="identifier">min_args</span> <span class="special"><=</span> 125 <span class="identifier">i</span> <span class="special"><=</span> 126 <span class="identifier">max_args</span></code>. 127 </p> 128<pre class="programlisting"><span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">member_name</span><span class="special">,</span> <span class="identifier">min_args</span><span class="special">,</span> <span class="identifier">max_args</span><span class="special">)</span> 129</pre> 130<p> 131 Expands to the definition of an OverloadDispatcher called name in the current 132 scope which can be used to generate the following function invocation: 133 </p> 134<pre class="programlisting"><span class="identifier">x</span><span class="special">.</span><span class="identifier">member_name</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">ai</span><span class="special">);</span></pre> 135<p> 136 for all min_args <= i <= max_args, where x is a reference to an object 137 of class type. 138 </p> 139</div> 140<div class="section"> 141<div class="titlepage"><div><div><h3 class="title"> 142<a name="function_invocation_and_creation.boost_python_overloads_hpp.example"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.example" title="Example">Example</a> 143</h3></div></div></div> 144<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 145<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 146<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">args</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 147<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 148<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 149<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">overloads</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 150<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_internal_reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 151 152<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span> 153 154<span class="identifier">tuple</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">4.25</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">z</span> <span class="special">=</span> <span class="string">"wow"</span><span class="special">)</span> 155<span class="special">{</span> 156 <span class="keyword">return</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">);</span> 157<span class="special">}</span> 158 159<span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">f_overloads</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span> 160 161<span class="keyword">struct</span> <span class="identifier">Y</span> <span class="special">{};</span> 162<span class="keyword">struct</span> <span class="identifier">X</span> 163<span class="special">{</span> 164 <span class="identifier">Y</span><span class="special">&</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">4.25</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">z</span> <span class="special">=</span> <span class="string">"wow"</span><span class="special">)</span> 165 <span class="special">{</span> 166 <span class="keyword">return</span> <span class="identifier">inner</span><span class="special">;</span> 167 <span class="special">}</span> 168 <span class="identifier">Y</span> <span class="identifier">inner</span><span class="special">;</span> 169<span class="special">};</span> 170 171<span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">f_member_overloads</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span> 172 173<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">args_ext</span><span class="special">)</span> 174<span class="special">{</span> 175 <span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> 176 <span class="identifier">f_overloads</span><span class="special">(</span> 177 <span class="identifier">args</span><span class="special">(</span><span class="string">"x"</span><span class="special">,</span> <span class="string">"y"</span><span class="special">,</span> <span class="string">"z"</span><span class="special">),</span> <span class="string">"This is f's docstring"</span> 178 <span class="special">));</span> 179 180 181 <span class="identifier">class_</span><span class="special"><</span><span class="identifier">Y</span><span class="special">>(</span><span class="string">"Y"</span><span class="special">)</span> 182 <span class="special">;</span> 183 184 <span class="identifier">class_</span><span class="special"><</span><span class="identifier">X</span><span class="special">>(</span><span class="string">"X"</span><span class="special">,</span> <span class="string">"This is X's docstring"</span><span class="special">)</span> 185 <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f1"</span><span class="special">,</span> <span class="special">&</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span> 186 <span class="identifier">f_member_overloads</span><span class="special">(</span> 187 <span class="identifier">args</span><span class="special">(</span><span class="string">"x"</span><span class="special">,</span> <span class="string">"y"</span><span class="special">,</span> <span class="string">"z"</span><span class="special">),</span> <span class="string">"f's docstring"</span> 188 <span class="special">)[</span><span class="identifier">return_internal_reference</span><span class="special"><>()]</span> 189 <span class="special">)</span> 190 <span class="special">;</span> 191<span class="special">}</span> 192</pre> 193</div> 194</div> 195<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 196<td align="left"></td> 197<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> 198 Distributed under the Boost Software License, Version 1.0. (See accompanying 199 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a> 200 </p> 201</div></td> 202</tr></table> 203<hr> 204<div class="spirit-nav"> 205<a accesskey="p" href="boost_python_make_function_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ptr_hpp.html"><img src="../../images/next.png" alt="Next"></a> 206</div> 207</body> 208</html> 209