• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>boost/python/def.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="../high_level_components.html" title="Chapter 2. High Level Components">
9<link rel="prev" href="../high_level_components.html" title="Chapter 2. High Level Components">
10<link rel="next" href="boost_python_def_visitor_hpp.html" title="boost/python/def_visitor.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="../high_level_components.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.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_def_visitor_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="high_level_components.boost_python_def_hpp"></a><a class="link" href="boost_python_def_hpp.html" title="boost/python/def.hpp">boost/python/def.hpp</a>
21</h2></div></div></div>
22<div class="toc"><dl class="toc">
23<dt><span class="section"><a href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.introduction">Introduction</a></span></dt>
24<dt><span class="section"><a href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.functions">Functions</a></span></dt>
25<dt><span class="section"><a href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.example">Example</a></span></dt>
26</dl></div>
27<div class="section">
28<div class="titlepage"><div><div><h3 class="title">
29<a name="high_level_components.boost_python_def_hpp.introduction"></a><a class="link" href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.introduction" title="Introduction">Introduction</a>
30</h3></div></div></div>
31<p>
32          <code class="computeroutput"><span class="identifier">def</span><span class="special">()</span></code>
33          is the function which can be used to expose C++ functions and callable
34          objects as Python functions in the <a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.introduction" title="Introduction">current
35          scope</a>.
36        </p>
37</div>
38<div class="section">
39<div class="titlepage"><div><div><h3 class="title">
40<a name="high_level_components.boost_python_def_hpp.functions"></a><a class="link" href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.functions" title="Functions">Functions</a>
41</h3></div></div></div>
42<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">&gt;</span>
43<span class="keyword">void</span> <span class="identifier">def</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">F</span> <span class="identifier">f</span><span class="special">);</span>
44
45<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A1</span><span class="special">&gt;</span>
46<span class="keyword">void</span> <span class="identifier">def</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">A1</span> <span class="keyword">const</span><span class="special">&amp;);</span>
47
48<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A2</span><span class="special">&gt;</span>
49<span class="keyword">void</span> <span class="identifier">def</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">A1</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">A2</span> <span class="keyword">const</span><span class="special">&amp;);</span>
50
51<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Fn</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A1</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A2</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A3</span><span class="special">&gt;</span>
52<span class="keyword">void</span> <span class="identifier">def</span><span class="special">(</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">name</span><span class="special">,</span> <span class="identifier">Fn</span> <span class="identifier">fn</span><span class="special">,</span> <span class="identifier">A1</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">A2</span> <span class="keyword">const</span><span class="special">&amp;,</span> <span class="identifier">A3</span> <span class="keyword">const</span><span class="special">&amp;);</span>
53</pre>
54<div class="variablelist">
55<p class="title"><b></b></p>
56<dl class="variablelist">
57<dt><span class="term">Requires</span></dt>
58<dd>
59<p>
60                name is an <a class="link" href="../glossary.html#ntbs">ntbs</a> which conforms to Python's
61                <a href="http://www.python.org/doc/current/ref/identifiers.html" target="_top">identifier
62                naming rules</a>.
63              </p>
64<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
65<li class="listitem">
66                    If <code class="computeroutput"><span class="identifier">Fn</span></code> is [derived
67                    from] <a class="link" href="../object_wrappers/boost_python_object_hpp.html#object_wrappers.boost_python_object_hpp.class_object" title="Class object">object</a>,
68                    it will be added to the <a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.introduction" title="Introduction">current
69                    scope</a> as a single overload. To be useful, <code class="computeroutput"><span class="identifier">fn</span></code> should be <a href="http://www.python.org/doc/current/lib/built-in-funcs.html#l2h-6" target="_top">callable</a>.
70                  </li>
71<li class="listitem">
72<p class="simpara">
73                    If <code class="computeroutput"><span class="identifier">a1</span></code> is the
74                    result of an <a class="link" href="../function_invocation_and_creation/boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions" title="overload-dispatch-expressions">overload-dispatch-expression</a>,
75                    only the second form is allowed and <code class="computeroutput"><span class="identifier">fn</span></code>
76                    must be a pointer to function or pointer to member function whose
77                    <a class="link" href="../glossary.html#arity">arity</a> is the same as A1's <a class="link" href="../function_invocation_and_creation/boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions" title="overload-dispatch-expressions">maximum
78                    arity</a>.
79                  </p>
80<p class="simpara">
81                    <span class="bold"><strong>Effects:</strong></span> For each prefix <code class="computeroutput"><span class="identifier">P</span></code> of <code class="computeroutput"><span class="identifier">Fn</span></code>'s
82                    sequence of argument types, beginning with the one whose length
83                    is <code class="computeroutput"><span class="identifier">A1</span></code>'s <a class="link" href="../function_invocation_and_creation/boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions" title="overload-dispatch-expressions">minimum
84                    arity</a>, adds a <code class="computeroutput"><span class="identifier">name</span><span class="special">(...)</span></code> function overload to the
85                    <a class="link" href="boost_python_scope_hpp.html#high_level_components.boost_python_scope_hpp.introduction" title="Introduction">current
86                    scope</a>. Each overload generated invokes a1's call-expression
87                    with P, using a copy of a1's call policies. If the longest valid
88                    prefix of A1 contains N types and a1 holds M keywords, an initial
89                    sequence of the keywords are used for all but the first N - M
90                    arguments of each overload.
91                  </p>
92</li>
93<li class="listitem">
94<p class="simpara">
95                    Otherwise, fn must be a non-null function or member function
96                    pointer, and a single function overload built around fn is added
97                    to the current scope. If any of a1-a3 are supplied, they may
98                    be selected in any order from the table below.
99                  </p>
100<div class="informaltable"><table class="table">
101<colgroup>
102<col>
103<col>
104<col>
105</colgroup>
106<thead><tr>
107<th>
108                            <p>
109                              Mnemonic Name
110                            </p>
111                          </th>
112<th>
113                            <p>
114                              Requirements/Type properties
115                            </p>
116                          </th>
117<th>
118                            <p>
119                              Effects
120                            </p>
121                          </th>
122</tr></thead>
123<tbody>
124<tr>
125<td>
126                            <p>
127                              docstring
128                            </p>
129                          </td>
130<td>
131                            <p>
132                              Any <a class="link" href="../glossary.html#ntbs">ntbs</a>
133                            </p>
134                          </td>
135<td>
136                            <p>
137                              Value will be bound to the <code class="computeroutput"><span class="identifier">__doc__</span></code>
138                              attribute of the resulting method overload.
139                            </p>
140                          </td>
141</tr>
142<tr>
143<td>
144                            <p>
145                              policies
146                            </p>
147                          </td>
148<td>
149                            <p>
150                              A model of <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>
151                            </p>
152                          </td>
153<td>
154                            <p>
155                              A copy will be used as the call policies of the resulting
156                              method overload.
157                            </p>
158                          </td>
159</tr>
160<tr>
161<td>
162                            <p>
163                              keywords
164                            </p>
165                          </td>
166<td>
167                            <p>
168                              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>
169                              specifying no more arguments than the <a class="link" href="../glossary.html#arity">arity</a>
170                              of <code class="computeroutput"><span class="identifier">fn</span></code>.
171                            </p>
172                          </td>
173<td>
174                            <p>
175                              A copy will be used as the call policies of the resulting
176                              method overload.
177                            </p>
178                          </td>
179</tr>
180</tbody>
181</table></div>
182</li>
183</ul></div>
184</dd>
185</dl>
186</div>
187</div>
188<div class="section">
189<div class="titlepage"><div><div><h3 class="title">
190<a name="high_level_components.boost_python_def_hpp.example"></a><a class="link" href="boost_python_def_hpp.html#high_level_components.boost_python_def_hpp.example" title="Example">Example</a>
191</h3></div></div></div>
192<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
193<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
194<span class="preprocessor">#include</span> <span class="special">&lt;</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">&gt;</span>
195
196<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>
197
198<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">foo</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"foo"</span><span class="special">;</span> <span class="special">}</span>
199
200<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">def_test</span><span class="special">)</span>
201<span class="special">{</span>
202    <span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span> <span class="identifier">foo</span><span class="special">,</span> <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">"foo's docstring"</span><span class="special">);</span>
203<span class="special">}</span>
204</pre>
205</div>
206</div>
207<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
208<td align="left"></td>
209<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
210        Distributed under the Boost Software License, Version 1.0. (See accompanying
211        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>
212      </p>
213</div></td>
214</tr></table>
215<hr>
216<div class="spirit-nav">
217<a accesskey="p" href="../high_level_components.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_level_components.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_def_visitor_hpp.html"><img src="../../images/next.png" alt="Next"></a>
218</div>
219</body>
220</html>
221