index.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
  7. <title>The Boost Parameter Library Python Binding Documentation</title>
  8. <meta name="authors" content="David Abrahams Daniel Wallin" />
  9. <meta name="organization" content="BoostPro Computing" />
  10. <meta name="date" content="$Date$" />
  11. <meta name="copyright" content="Copyright David Abrahams, Daniel Wallin 2005-2009. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)" />
  12. <link rel="stylesheet" href="rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="the-boost-parameter-library-python-binding-documentation">
  16. <h1 class="title">The Boost Parameter Library Python Binding Documentation</h1>
  17. <table class="docinfo" frame="void" rules="none">
  18. <col class="docinfo-name" />
  19. <col class="docinfo-content" />
  20. <tbody valign="top">
  21. <tr><th class="docinfo-name">Authors:</th>
  22. <td>David Abrahams
  23. <br />Daniel Wallin</td></tr>
  24. <tr><th class="docinfo-name">Contact:</th>
  25. <td><a class="first reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="last reference external" href="mailto:daniel&#64;boostpro.com">daniel&#64;boostpro.com</a></td></tr>
  26. <tr><th class="docinfo-name">Organization:</th>
  27. <td><a class="first last reference external" href="http://www.boostpro.com">BoostPro Computing</a></td></tr>
  28. <tr><th class="docinfo-name">Date:</th>
  29. <td>$Date$</td></tr>
  30. <tr><th class="docinfo-name">Copyright:</th>
  31. <td>Copyright David Abrahams, Daniel Wallin
  32. 2005-2009. Distributed under the Boost Software License,
  33. Version 1.0. (See accompanying file LICENSE_1_0.txt
  34. or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</td></tr>
  35. </tbody>
  36. </table>
  37. <div class="abstract topic">
  38. <p class="topic-title first">Abstract</p>
  39. <p>Makes it possible to bind Boost.Parameter-enabled
  40. functions, operators and constructors to Python.</p>
  41. </div>
  42. <p><a class="reference external" href="../../../../index.htm"><img alt="Boost" src="../../../../boost.png" /></a></p>
  43. <div class="contents topic" id="contents">
  44. <p class="topic-title first">Contents</p>
  45. <ul class="simple">
  46. <li><a class="reference internal" href="#introduction" id="id7">Introduction</a></li>
  47. <li><a class="reference internal" href="#tutorial" id="id8">Tutorial</a></li>
  48. <li><a class="reference internal" href="#concept-parameterspec" id="id9">concept <span class="concept">ParameterSpec</span></a></li>
  49. <li><a class="reference internal" href="#special-keywords" id="id10"><em>special</em> keywords</a></li>
  50. <li><a class="reference internal" href="#class-template-init" id="id11">class template <tt class="docutils literal">init</tt></a></li>
  51. <li><a class="reference internal" href="#class-template-call" id="id12">class template <tt class="docutils literal">call</tt></a></li>
  52. <li><a class="reference internal" href="#class-template-function" id="id13">class template <tt class="docutils literal">function</tt></a></li>
  53. <li><a class="reference internal" href="#function-template-def" id="id14">function template <tt class="docutils literal">def</tt></a></li>
  54. <li><a class="reference internal" href="#portability" id="id15">Portability</a></li>
  55. </ul>
  56. </div>
  57. <div class="section" id="introduction">
  58. <h1><a class="toc-backref" href="#id7">Introduction</a></h1>
  59. <p><tt class="docutils literal">boost/parameter/python.hpp</tt> introduces a group of <a class="reference external" href="../../../python/doc/v2/def_visitor.html"><tt class="docutils literal">def_visitors</tt></a> that can
  60. be used to easily expose Boost.Parameter-enabled member functions to Python with
  61. Boost.Python. It also provides a function template <tt class="docutils literal">def()</tt> that can be used
  62. to expose Boost.Parameter-enabled free functions.</p>
  63. <p>When binding a Boost.Parameter enabled function, the keyword tags
  64. must be specified. Additionally, because Boost.Parameter enabled
  65. functions are templates, the desired function signature must be
  66. specified.</p>
  67. <!-- The keyword tags are specified as an `MPL Sequence`_, using the
  68. pointer qualifications described in |ParameterSpec|_ below. The
  69. signature is also specifid as an `MPL sequence`_ of parameter
  70. types. Additionally, ``boost::parameter::python::function`` and
  71. ``boost::parameter::python::def`` requires a class with forwarding
  72. overloads. We will take a closer look at how this is done in the
  73. tutorial section below. -->
  74. <p>The keyword tags and associated argument types are specified as an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL
  75. Sequence</a>, using the function type syntax described in <a class="reference internal" href="#concept-parameterspec"><span class="concept">ParameterSpec</span></a>
  76. below. Additionally, <tt class="docutils literal"><span class="pre">boost::parameter::python::function</span></tt> and
  77. <tt class="docutils literal"><span class="pre">boost::parameter::python::def</span></tt> requires a class with forwarding overloads.
  78. We will take a closer look at how this is done in the tutorial section below.</p>
  79. <!-- The last two sentences are terribly vague. Which namespace is -->
  80. <!-- ``function`` in? Isn't the return type always needed? What -->
  81. <!-- else are we going to do other than pass these sequences to -->
  82. <!-- function? -->
  83. </div>
  84. <div class="section" id="tutorial">
  85. <h1><a class="toc-backref" href="#id8">Tutorial</a></h1>
  86. <p>In this section we will outline the steps needed to bind a simple
  87. Boost.Parameter-enabled member function to Python. Knowledge of the
  88. Boost.Parameter <a class="reference external" href="index.html">macros</a> are required to understand this section.</p>
  89. <p>The class and member function we are interested in binding looks
  90. like this:</p>
  91. <pre class="literal-block">
  92. #include &lt;boost/parameter/keyword.hpp&gt;
  93. #include &lt;boost/parameter/preprocessor.hpp&gt;
  94. #include &lt;boost/parameter/python.hpp&gt;
  95. #include &lt;boost/python.hpp&gt;
  96. // First the keywords
  97. BOOST_PARAMETER_KEYWORD(tag, title)
  98. BOOST_PARAMETER_KEYWORD(tag, width)
  99. BOOST_PARAMETER_KEYWORD(tag, height)
  100. class window
  101. {
  102. public:
  103. BOOST_PARAMETER_MEMBER_FUNCTION(
  104. (void), open, tag,
  105. (required (title, (std::string)))
  106. (optional (width, (unsigned), 400)
  107. (height, (unsigned), 400))
  108. )
  109. {
  110. <em>… function implementation …</em>
  111. }
  112. };
  113. </pre>
  114. <!-- @example.prepend('#include <cassert>') -->
  115. <!-- @example.replace_emphasis('''
  116. assert(title == "foo");
  117. assert(height == 20);
  118. assert(width == 400);
  119. ''') -->
  120. <p>It defines a set of overloaded member functions called <tt class="docutils literal">open</tt> with one
  121. required parameter and two optional ones. To bind this member function to
  122. Python we use the binding utility <tt class="docutils literal"><span class="pre">boost::parameter::python::function</span></tt>.
  123. <tt class="docutils literal"><span class="pre">boost::parameter::python::function</span></tt> is a <a class="reference external" href="../../../python/doc/v2/def_visitor.html"><tt class="docutils literal">def_visitor</tt></a> that we'll instantiate
  124. and pass to <tt class="docutils literal"><span class="pre">boost::python::class_::def()</span></tt>.</p>
  125. <p>To use <tt class="docutils literal"><span class="pre">boost::parameter::python::function</span></tt> we first need to define
  126. a class with forwarding overloads. This is needed because <tt class="docutils literal"><span class="pre">window::open()</span></tt>
  127. is a function template, so we can't refer to it in any other way.</p>
  128. <pre class="literal-block">
  129. struct open_fwd
  130. {
  131. template &lt;class A0, class A1, class A2&gt;
  132. void operator()(
  133. boost::type&lt;void&gt;, window&amp; self
  134. , A0 const&amp; a0, A1 const&amp; a1, A2 const&amp; a2
  135. )
  136. {
  137. self.open(a0, a1, a2);
  138. }
  139. };
  140. </pre>
  141. <p>The first parameter, <tt class="docutils literal"><span class="pre">boost::type&lt;void&gt;</span></tt>, tells the forwarding overload
  142. what the return type should be. In this case we know that it's always void
  143. but in some cases, when we are exporting several specializations of a
  144. Boost.Parameter-enabled template, we need to use that parameter to
  145. deduce the return type.</p>
  146. <p><tt class="docutils literal"><span class="pre">window::open()</span></tt> takes a total of 3 parameters, so the forwarding function
  147. needs to take three parameters as well.</p>
  148. <div class="note">
  149. <p class="first admonition-title">Note</p>
  150. <p class="last">We only need one overload in the forwarding class, despite the
  151. fact that there are two optional parameters. There are special
  152. circumstances when several overload are needed; see
  153. <a class="reference internal" href="#special-keywords">special keywords</a>.</p>
  154. </div>
  155. <p>Next we'll define the module and export the class:</p>
  156. <pre class="literal-block">
  157. BOOST_PYTHON_MODULE(my_module)
  158. {
  159. using namespace boost::python;
  160. namespace py = boost::parameter::python;
  161. namespace mpl = boost::mpl;
  162. class_&lt;window&gt;(&quot;window&quot;)
  163. .def(
  164. &quot;open&quot;, py::function&lt;
  165. open_fwd
  166. , mpl::vector&lt;
  167. void
  168. , tag::title(std::string)
  169. , tag::width*(unsigned)
  170. , tag::height*(unsigned)
  171. &gt;
  172. &gt;()
  173. );
  174. }
  175. </pre>
  176. <!-- @jam_prefix.append('import python ;') -->
  177. <!-- @jam_prefix.append('stage . : my_module /boost/python//boost_python ;') -->
  178. <!-- @my_module = build(
  179. output = 'my_module'
  180. , target_rule = 'python-extension'
  181. , input = '/boost/python//boost_python'
  182. , howmany = 'all'
  183. ) -->
  184. <!-- @del jam_prefix[:] -->
  185. <p><tt class="docutils literal"><span class="pre">py::function</span></tt> is passed two parameters. The first one is the class with
  186. forwarding overloads that we defined earlier. The second one is an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL
  187. Sequence</a> with the keyword tag types and argument types for the function
  188. specified as function types. The pointer syntax used in <tt class="docutils literal"><span class="pre">tag::width*</span></tt> and
  189. <tt class="docutils literal"><span class="pre">tag::height*</span></tt> means that the parameter is optional. The first element of
  190. the <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL Sequence</a> is the return type of the function, in this case <tt class="docutils literal">void</tt>,
  191. which is passed as the first argument to <tt class="docutils literal">operator()</tt> in the forwarding
  192. class.</p>
  193. <!-- The
  194. pointer syntax means that the parameter is optional, so in this case
  195. ``width`` and ``height`` are optional parameters. The third parameter
  196. is an `MPL Sequence`_ with the desired function signature. The return type comes first, and
  197. then the parameter types:
  198. .. parsed-literal::
  199. mpl::vector<void, std::string, unsigned, unsigned>
  200. *return type* *title* *width* *height*
  201. .. @ignore() -->
  202. <p>That's it! This class can now be used in Python with the expected syntax:</p>
  203. <pre class="literal-block">
  204. &gt;&gt;&gt; w = my_module.window()
  205. &gt;&gt;&gt; w.open(title = &quot;foo&quot;, height = 20)
  206. </pre>
  207. <!-- @example.prepend('import my_module') -->
  208. <!-- @run_python(module_path = my_module) -->
  209. <!-- Sorry to say this at such a late date, but this syntax really -->
  210. <!-- strikes me as cumbersome. Couldn't we do something like:
  211. class_<window>("window")
  212. .def(
  213. "open",
  214. (void (*)(
  215. tag::title(std::string),
  216. tag::width*(unsigned),
  217. tag::height*(unsigned))
  218. )0
  219. );
  220. or at least:
  221. class_<window>("window")
  222. .def(
  223. "open",
  224. mpl::vector<
  225. void,
  226. tag::title(std::string),
  227. tag::width*(unsigned),
  228. tag::height*(unsigned)
  229. >()
  230. );
  231. assuming, that is, that we will have to repeat the tags (yes,
  232. users of broken compilers will have to give us function pointer
  233. types instead). -->
  234. </div>
  235. <hr class="docutils" />
  236. <div class="section" id="concept-parameterspec">
  237. <h1><a class="toc-backref" href="#id9">concept <span class="concept">ParameterSpec</span></a></h1>
  238. <p>A <span class="concept">ParameterSpec</span> is a function type <tt class="docutils literal">K(T)</tt> that describes both the keyword tag,
  239. <tt class="docutils literal">K</tt>, and the argument type, <tt class="docutils literal">T</tt>, for a parameter.</p>
  240. <p><tt class="docutils literal">K</tt> is either:</p>
  241. <ul class="simple">
  242. <li>A <em>required</em> keyword of the form <tt class="docutils literal">Tag</tt></li>
  243. <li><strong>or</strong>, an <em>optional</em> keyword of the form <tt class="docutils literal">Tag*</tt></li>
  244. <li><strong>or</strong>, a <em>special</em> keyword of the form <tt class="docutils literal">Tag**</tt></li>
  245. </ul>
  246. <p>where <tt class="docutils literal">Tag</tt> is a keyword tag type, as used in a specialization
  247. of <a class="reference external" href="../../../parameter/doc/html/reference.html#keyword"><tt class="docutils literal"><span class="pre">boost::parameter::keyword</span></tt></a>.</p>
  248. <p>The <strong>arity range</strong> for an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL Sequence</a> of <span class="concept">ParameterSpec</span>'s is
  249. defined as the closed range:</p>
  250. <pre class="literal-block">
  251. [ mpl::size&lt;S&gt; - number of <em>special</em> keyword tags in <tt class="docutils literal">S</tt>, mpl::size&lt;S&gt; ]
  252. </pre>
  253. <p>For example, the <strong>arity range</strong> of <tt class="docutils literal"><span class="pre">mpl::vector2&lt;x(int),y(int)&gt;</span></tt> is <tt class="docutils literal">[2,2]</tt>,
  254. the <strong>arity range</strong> of <tt class="docutils literal"><span class="pre">mpl::vector2&lt;x(int),y*(int)&gt;</span></tt> is <tt class="docutils literal">[2,2]</tt> and the
  255. <strong>arity range</strong> of <tt class="docutils literal"><span class="pre">mpl::vector2&lt;x(int),y**(int)&gt;</span></tt> is <tt class="docutils literal">[1,2]</tt>.</p>
  256. </div>
  257. <div class="section" id="special-keywords">
  258. <h1><a class="toc-backref" href="#id10"><em>special</em> keywords</a></h1>
  259. <p>Sometimes it is desirable to have a default value for a parameter that differ
  260. in type from the parameter. This technique is useful for doing simple tag-dispatching
  261. based on the presence of a parameter. For example:</p>
  262. <!-- An example_ of this is given in the Boost.Parameter
  263. docs. The example uses a different technique, but could also have been written like this: -->
  264. <pre class="literal-block">
  265. namespace core
  266. {
  267. template &lt;class ArgumentPack&gt;
  268. void dfs_dispatch(ArgumentPack const&amp; args, mpl::false_)
  269. {
  270. <em>…compute and use default color map…</em>
  271. }
  272. template &lt;class ArgumentPack, class ColorMap&gt;
  273. void dfs_dispatch(ArgumentPack const&amp; args, ColorMap colormap)
  274. {
  275. <em>…use colormap…</em>
  276. }
  277. }
  278. template &lt;class ArgumentPack&gt;
  279. void depth_first_search(ArgumentPack const&amp; args)
  280. {
  281. core::dfs_dispatch(args, args[color | mpl::false_()]);
  282. }
  283. </pre>
  284. <!-- @example.prepend('''
  285. #include <boost/parameter/keyword.hpp>
  286. #include <boost/parameter/parameters.hpp>
  287. #include <boost/mpl/bool.hpp>
  288. #include <cassert>
  289. BOOST_PARAMETER_KEYWORD(tag, color);
  290. typedef boost::parameter::parameters<tag::color> params;
  291. namespace mpl = boost::mpl;
  292. ''') -->
  293. <!-- @example.replace_emphasis('''
  294. assert(args[color | 1] == 1);
  295. ''') -->
  296. <!-- @example.replace_emphasis('''
  297. assert(args[color | 1] == 0);
  298. ''') -->
  299. <!-- @example.append('''
  300. int main()
  301. {
  302. depth_first_search(params()());
  303. depth_first_search(params()(color = 0));
  304. }''') -->
  305. <!-- @build() -->
  306. <!-- .. _example: index.html#dispatching-based-on-the-presence-of-a-default -->
  307. <p>In the above example the type of the default for <tt class="docutils literal">color</tt> is <tt class="docutils literal"><span class="pre">mpl::false_</span></tt>, a
  308. type that is distinct from any color map that the user might supply.</p>
  309. <p>When binding the case outlined above, the default type for <tt class="docutils literal">color</tt> will not
  310. be convertible to the parameter type. Therefore we need to tag the <tt class="docutils literal">color</tt>
  311. keyword as a <em>special</em> keyword. This is done by specifying the tag as
  312. <tt class="docutils literal"><span class="pre">tag::color**</span></tt> when binding the function (see <a class="reference internal" href="#concept-parameterspec">concept ParameterSpec</a> for
  313. more details on the tagging). By doing this we tell the binding functions that
  314. it needs to generate two overloads, one with the <tt class="docutils literal">color</tt> parameter present
  315. and one without. Had there been two <em>special</em> keywords, four overloads would
  316. need to be generated. The number of generated overloads is equal to 2<sup>N</sup>, where <tt class="docutils literal">N</tt> is the number of <em>special</em> keywords.</p>
  317. </div>
  318. <hr class="docutils" />
  319. <div class="section" id="class-template-init">
  320. <h1><a class="toc-backref" href="#id11">class template <tt class="docutils literal">init</tt></a></h1>
  321. <p>Defines a named parameter enabled constructor.</p>
  322. <pre class="literal-block">
  323. template &lt;class ParameterSpecs&gt;
  324. struct init : python::def_visitor&lt;init&lt;ParameterSpecs&gt; &gt;
  325. {
  326. template &lt;class Class&gt;
  327. void def(Class&amp; class_);
  328. template &lt;class CallPolicies&gt;
  329. <em>def_visitor</em> operator[](CallPolicies const&amp; policies) const;
  330. };
  331. </pre>
  332. <!-- @ignore() -->
  333. <div class="section" id="init-requirements">
  334. <h2><tt class="docutils literal">init</tt> requirements</h2>
  335. <ul>
  336. <li><p class="first"><tt class="docutils literal">ParameterSpecs</tt> is an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL sequence</a> where each element is a
  337. model of <span class="concept">ParameterSpec</span>.</p>
  338. </li>
  339. <li><p class="first">For every <tt class="docutils literal">N</tt> in <tt class="docutils literal">[U,V]</tt>, where <tt class="docutils literal">[U,V]</tt> is the <strong>arity
  340. range</strong> of <tt class="docutils literal">ParameterSpecs</tt>, <tt class="docutils literal">Class</tt> must support these
  341. expressions:</p>
  342. <table border="1" class="docutils">
  343. <colgroup>
  344. <col width="30%" />
  345. <col width="17%" />
  346. <col width="53%" />
  347. </colgroup>
  348. <thead valign="bottom">
  349. <tr><th class="head">Expression</th>
  350. <th class="head">Return type</th>
  351. <th class="head">Requirements</th>
  352. </tr>
  353. </thead>
  354. <tbody valign="top">
  355. <tr><td><tt class="docutils literal">Class(a0, …, aN)</tt></td>
  356. <td>-</td>
  357. <td><tt class="docutils literal">a0</tt>…<tt class="docutils literal">aN</tt> are tagged arguments.</td>
  358. </tr>
  359. </tbody>
  360. </table>
  361. </li>
  362. </ul>
  363. </div>
  364. <div class="section" id="template-class-callpolicies-operator-callpolicies-const">
  365. <h2><tt class="docutils literal">template &lt;class CallPolicies&gt; <span class="pre">operator[](CallPolicies</span> const&amp;)</tt></h2>
  366. <p>Returns a <tt class="docutils literal">def_visitor</tt> equivalent to <tt class="docutils literal">*this</tt>, except that it
  367. uses CallPolicies when creating the binding.</p>
  368. </div>
  369. <div class="section" id="example">
  370. <h2>Example</h2>
  371. <pre class="literal-block">
  372. #include &lt;boost/parameter/keyword.hpp&gt;
  373. #include &lt;boost/parameter/preprocessor.hpp&gt;
  374. #include &lt;boost/parameter/python.hpp&gt;
  375. #include &lt;boost/python.hpp&gt;
  376. #include &lt;boost/mpl/vector.hpp&gt;
  377. BOOST_PARAMETER_KEYWORD(tag, x)
  378. BOOST_PARAMETER_KEYWORD(tag, y)
  379. struct base
  380. {
  381. template &lt;class ArgumentPack&gt;
  382. base(ArgumentPack const&amp; args)
  383. {
  384. <em>… use args …</em>
  385. }
  386. };
  387. class X : base
  388. {
  389. public:
  390. BOOST_PARAMETER_CONSTRUCTOR(X, (base), tag,
  391. (required (x, *))
  392. (optional (y, *))
  393. )
  394. };
  395. BOOST_PYTHON_MODULE(<em>module name</em>)
  396. {
  397. using namespace boost::python;
  398. namespace py = boost::parameter::python;
  399. namespace mpl = boost::mpl;
  400. class_&lt;X&gt;(&quot;X&quot;, no_init)
  401. .def(
  402. py::init&lt;
  403. mpl::vector&lt;tag::x(int), tag::y*(int)&gt;
  404. &gt;()
  405. );
  406. }
  407. </pre>
  408. <!-- @example.replace_emphasis('''
  409. assert(args[x] == 0);
  410. assert(args[y | 1] == 1);
  411. ''') -->
  412. <!-- @example.replace_emphasis('my_module') -->
  413. <!-- @jam_prefix.append('import python ;') -->
  414. <!-- @jam_prefix.append('stage . : my_module /boost/python//boost_python ;') -->
  415. <!-- @my_module = build(
  416. output = 'my_module'
  417. , target_rule = 'python-extension'
  418. , input = '/boost/python//boost_python'
  419. ) -->
  420. </div>
  421. </div>
  422. <hr class="docutils" />
  423. <div class="section" id="class-template-call">
  424. <h1><a class="toc-backref" href="#id12">class template <tt class="docutils literal">call</tt></a></h1>
  425. <p>Defines a <tt class="docutils literal">__call__</tt> operator, mapped to <tt class="docutils literal">operator()</tt> in C++.</p>
  426. <pre class="literal-block">
  427. template &lt;class ParameterSpecs&gt;
  428. struct call : python::def_visitor&lt;call&lt;ParameterSpecs&gt; &gt;
  429. {
  430. template &lt;class Class&gt;
  431. void def(Class&amp; class_);
  432. template &lt;class CallPolicies&gt;
  433. <em>def_visitor</em> operator[](CallPolicies const&amp; policies) const;
  434. };
  435. </pre>
  436. <!-- @ignore() -->
  437. <div class="section" id="call-requirements">
  438. <h2><tt class="docutils literal">call</tt> requirements</h2>
  439. <ul>
  440. <li><p class="first"><tt class="docutils literal">ParameterSpecs</tt> is an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL sequence</a> where each element
  441. except the first models <span class="concept">ParameterSpec</span>. The first element
  442. is the result type of <tt class="docutils literal"><span class="pre">c(…)</span></tt>.</p>
  443. </li>
  444. <li><p class="first"><tt class="docutils literal">Class</tt> must support these expressions, where <tt class="docutils literal">c</tt> is an
  445. instance of <tt class="docutils literal">Class</tt>:</p>
  446. <table border="1" class="docutils">
  447. <colgroup>
  448. <col width="24%" />
  449. <col width="26%" />
  450. <col width="50%" />
  451. </colgroup>
  452. <thead valign="bottom">
  453. <tr><th class="head">Expression</th>
  454. <th class="head">Return type</th>
  455. <th class="head">Requirements</th>
  456. </tr>
  457. </thead>
  458. <tbody valign="top">
  459. <tr><td><tt class="docutils literal">c(a0, …, aN)</tt></td>
  460. <td>Convertible to <tt class="docutils literal">R</tt></td>
  461. <td><tt class="docutils literal">a0</tt>…<tt class="docutils literal">aN</tt> are tagged arguments.</td>
  462. </tr>
  463. </tbody>
  464. </table>
  465. <p>For every <tt class="docutils literal">N</tt> in <tt class="docutils literal">[U,V]</tt>, where <tt class="docutils literal">[U,V]</tt> is the <strong>arity range</strong> of <tt class="docutils literal">ParameterSpecs</tt>.</p>
  466. </li>
  467. </ul>
  468. </div>
  469. <div class="section" id="id3">
  470. <h2><tt class="docutils literal">template &lt;class CallPolicies&gt; <span class="pre">operator[](CallPolicies</span> const&amp;)</tt></h2>
  471. <p>Returns a <tt class="docutils literal">def_visitor</tt> equivalent to <tt class="docutils literal">*this</tt>, except that it
  472. uses CallPolicies when creating the binding.</p>
  473. </div>
  474. <div class="section" id="id4">
  475. <h2>Example</h2>
  476. <pre class="literal-block">
  477. #include &lt;boost/parameter/keyword.hpp&gt;
  478. #include &lt;boost/parameter/preprocessor.hpp&gt;
  479. #include &lt;boost/parameter/python.hpp&gt;
  480. #include &lt;boost/python.hpp&gt;
  481. #include &lt;boost/mpl/vector.hpp&gt;
  482. BOOST_PARAMETER_KEYWORD(tag, x)
  483. BOOST_PARAMETER_KEYWORD(tag, y)
  484. namespace parameter = boost::parameter;
  485. typedef parameter::parameters&lt;
  486. parameter::required&lt;tag::x&gt;
  487. , parameter::optional&lt;tag::y&gt;
  488. &gt; call_parameters;
  489. class X
  490. {
  491. public:
  492. template &lt;class ArgumentPack&gt;
  493. int call_impl(ArgumentPack const&amp; args)
  494. {
  495. <em>… use args …</em>
  496. }
  497. template &lt;class A0&gt;
  498. int operator()(A0 const&amp; a0)
  499. {
  500. return call_impl(call_parameters()(a0));
  501. }
  502. template &lt;class A0, class A1&gt;
  503. int operator()(A0 const&amp; a0, A1 const&amp; a1)
  504. {
  505. return call_impl(call_parameters()(a0,a1));
  506. }
  507. };
  508. BOOST_PYTHON_MODULE(<em>module name</em>)
  509. {
  510. using namespace boost::python;
  511. namespace py = parameter::python;
  512. namespace mpl = boost::mpl;
  513. class_&lt;X&gt;(&quot;X&quot;)
  514. .def(
  515. py::call&lt;
  516. mpl::vector&lt;int, tag::x(int), tag::y*(int)&gt;
  517. &gt;()
  518. );
  519. }
  520. </pre>
  521. <!-- @example.replace_emphasis('''
  522. assert(args[x] == 0);
  523. assert(args[y | 1] == 1);
  524. return 0;
  525. ''') -->
  526. <!-- @example.replace_emphasis('my_module') -->
  527. <!-- @my_module = build(
  528. output = 'my_module'
  529. , target_rule = 'python-extension'
  530. , input = '/boost/python//boost_python'
  531. ) -->
  532. </div>
  533. </div>
  534. <hr class="docutils" />
  535. <div class="section" id="class-template-function">
  536. <h1><a class="toc-backref" href="#id13">class template <tt class="docutils literal">function</tt></a></h1>
  537. <p>Defines a named parameter enabled member function.</p>
  538. <pre class="literal-block">
  539. template &lt;class Fwd, class ParameterSpecs&gt;
  540. struct function : python::def_visitor&lt;function&lt;Fwd, ParameterSpecs&gt; &gt;
  541. {
  542. template &lt;class Class, class Options&gt;
  543. void def(Class&amp; class_, char const* name, Options const&amp; options);
  544. };
  545. </pre>
  546. <!-- @ignore() -->
  547. <div class="section" id="function-requirements">
  548. <h2><tt class="docutils literal">function</tt> requirements</h2>
  549. <ul>
  550. <li><p class="first"><tt class="docutils literal">ParameterSpecs</tt> is an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL sequence</a> where each element
  551. except the first models <span class="concept">ParameterSpec</span>. The first element
  552. is the result type of <tt class="docutils literal"><span class="pre">c.f(…)</span></tt>, where <tt class="docutils literal">f</tt> is the member
  553. function.</p>
  554. </li>
  555. <li><p class="first">An instance of <tt class="docutils literal">Fwd</tt> must support this expression:</p>
  556. <table border="1" class="docutils">
  557. <colgroup>
  558. <col width="39%" />
  559. <col width="18%" />
  560. <col width="43%" />
  561. </colgroup>
  562. <thead valign="bottom">
  563. <tr><th class="head">Expression</th>
  564. <th class="head">Return type</th>
  565. <th class="head">Requirements</th>
  566. </tr>
  567. </thead>
  568. <tbody valign="top">
  569. <tr><td><tt class="docutils literal"><span class="pre">fwd(boost::type&lt;R&gt;(),</span> self, a0, …, aN)</tt></td>
  570. <td>Convertible to <tt class="docutils literal">R</tt></td>
  571. <td><tt class="docutils literal">self</tt> is a reference to the object on which
  572. the function should be invoked. <tt class="docutils literal">a0</tt>…<tt class="docutils literal">aN</tt>
  573. are tagged arguments.</td>
  574. </tr>
  575. </tbody>
  576. </table>
  577. <p>For every <tt class="docutils literal">N</tt> in <tt class="docutils literal">[U,V]</tt>, where <tt class="docutils literal">[U,V]</tt> is the <strong>arity range</strong> of <tt class="docutils literal">ParameterSpecs</tt>.</p>
  578. </li>
  579. </ul>
  580. </div>
  581. <div class="section" id="id5">
  582. <h2>Example</h2>
  583. <p>This example exports a member function <tt class="docutils literal">f(int x, int y = …)</tt> to Python. The
  584. sequence of <span class="concept">ParameterSpec</span>'s <tt class="docutils literal"><span class="pre">mpl::vector2&lt;tag::x(int),</span> <span class="pre">tag::y*(int)&gt;</span></tt> has
  585. an <strong>arity range</strong> of [2,2], so we only need one forwarding overload.</p>
  586. <pre class="literal-block">
  587. #include &lt;boost/parameter/keyword.hpp&gt;
  588. #include &lt;boost/parameter/preprocessor.hpp&gt;
  589. #include &lt;boost/parameter/python.hpp&gt;
  590. #include &lt;boost/python.hpp&gt;
  591. #include &lt;boost/mpl/vector.hpp&gt;
  592. BOOST_PARAMETER_KEYWORD(tag, x)
  593. BOOST_PARAMETER_KEYWORD(tag, y)
  594. class X
  595. {
  596. public:
  597. BOOST_PARAMETER_MEMBER_FUNCTION((void), f, tag,
  598. (required (x, *))
  599. (optional (y, *, 1))
  600. )
  601. {
  602. <em>…</em>
  603. }
  604. };
  605. struct f_fwd
  606. {
  607. template &lt;class A0, class A1&gt;
  608. void operator()(boost::type&lt;void&gt;, X&amp; self, A0 const&amp; a0, A1 const&amp; a1)
  609. {
  610. self.f(a0, a1);
  611. }
  612. };
  613. BOOST_PYTHON_MODULE(<em>module name</em>)
  614. {
  615. using namespace boost::python;
  616. namespace py = boost::parameter::python;
  617. namespace mpl = boost::mpl;
  618. class_&lt;X&gt;(&quot;X&quot;)
  619. .def(&quot;f&quot;,
  620. py::function&lt;
  621. f_fwd
  622. , mpl::vector&lt;void, tag::x(int), tag::y*(int)&gt;
  623. &gt;()
  624. );
  625. }
  626. </pre>
  627. <!-- @example.replace_emphasis('''
  628. assert(x == 0);
  629. assert(y == 1);
  630. ''') -->
  631. <!-- @example.replace_emphasis('my_module') -->
  632. <!-- @my_module = build(
  633. output = 'my_module'
  634. , target_rule = 'python-extension'
  635. , input = '/boost/python//boost_python'
  636. ) -->
  637. </div>
  638. </div>
  639. <hr class="docutils" />
  640. <div class="section" id="function-template-def">
  641. <h1><a class="toc-backref" href="#id14">function template <tt class="docutils literal">def</tt></a></h1>
  642. <p>Defines a named parameter enabled free function in the current Python scope.</p>
  643. <pre class="literal-block">
  644. template &lt;class Fwd, class ParameterSpecs&gt;
  645. void def(char const* name);
  646. </pre>
  647. <!-- @ignore() -->
  648. <div class="section" id="def-requirements">
  649. <h2><tt class="docutils literal">def</tt> requirements</h2>
  650. <ul>
  651. <li><p class="first"><tt class="docutils literal">ParameterSpecs</tt> is an <a class="reference external" href="../../../mpl/doc/refmanual/sequences.html">MPL sequence</a> where each element
  652. except the first models <span class="concept">ParameterSpec</span>. The first element
  653. is the result type of <tt class="docutils literal"><span class="pre">f(…)</span></tt>, where <tt class="docutils literal">f</tt> is the function.</p>
  654. </li>
  655. <li><p class="first">An instance of <tt class="docutils literal">Fwd</tt> must support this expression:</p>
  656. <table border="1" class="docutils">
  657. <colgroup>
  658. <col width="39%" />
  659. <col width="21%" />
  660. <col width="40%" />
  661. </colgroup>
  662. <thead valign="bottom">
  663. <tr><th class="head">Expression</th>
  664. <th class="head">Return type</th>
  665. <th class="head">Requirements</th>
  666. </tr>
  667. </thead>
  668. <tbody valign="top">
  669. <tr><td><tt class="docutils literal"><span class="pre">fwd(boost::type&lt;R&gt;(),</span> a0, …, aN)</tt></td>
  670. <td>Convertible to <tt class="docutils literal">R</tt></td>
  671. <td><tt class="docutils literal">a0</tt>…<tt class="docutils literal">aN</tt> are tagged arguments.</td>
  672. </tr>
  673. </tbody>
  674. </table>
  675. <p>For every <tt class="docutils literal">N</tt> in <tt class="docutils literal">[U,V]</tt>, where <tt class="docutils literal">[U,V]</tt> is the <strong>arity range</strong> of <tt class="docutils literal">ParameterSpecs</tt>.</p>
  676. </li>
  677. </ul>
  678. </div>
  679. <div class="section" id="id6">
  680. <h2>Example</h2>
  681. <p>This example exports a function <tt class="docutils literal">f(int x, int y = …)</tt> to Python. The
  682. sequence of <span class="concept">ParameterSpec</span>'s <tt class="docutils literal"><span class="pre">mpl::vector2&lt;tag::x(int),</span> <span class="pre">tag::y*(int)&gt;</span></tt> has
  683. an <strong>arity range</strong> of [2,2], so we only need one forwarding overload.</p>
  684. <pre class="literal-block">
  685. BOOST_PARAMETER_FUNCTION((void), f, tag,
  686. (required (x, *))
  687. (optional (y, *, 1))
  688. )
  689. {
  690. <em>…</em>
  691. }
  692. struct f_fwd
  693. {
  694. template &lt;class A0, class A1&gt;
  695. void operator()(boost::type&lt;void&gt;, A0 const&amp; a0, A1 const&amp; a1)
  696. {
  697. f(a0, a1);
  698. }
  699. };
  700. BOOST_PYTHON_MODULE(…)
  701. {
  702. def&lt;
  703. f_fwd
  704. , mpl::vector&lt;
  705. void, tag::x(int), tag::y*(int)
  706. &gt;
  707. &gt;(&quot;f&quot;);
  708. }
  709. </pre>
  710. <!-- @ignore() -->
  711. <!-- again, the undefined ``fwd`` identifier. -->
  712. </div>
  713. </div>
  714. <div class="section" id="portability">
  715. <h1><a class="toc-backref" href="#id15">Portability</a></h1>
  716. <p>The Boost.Parameter Python binding library requires <em>partial template
  717. specialization</em>.</p>
  718. </div>
  719. </div>
  720. <div class="footer">
  721. <hr class="footer" />
  722. Generated on: 2019-12-10 00:22 UTC.
  723. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  724. </div>
  725. </body>
  726. </html>