From 8b9f918eeaef2023987edaad8be9c994fe11547f Mon Sep 17 00:00:00 2001 From: Daniel Wallin Date: Thu, 27 Apr 2006 09:42:45 +0000 Subject: [PATCH] python binding docs [SVN r33836] --- doc/python.rst | 97 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/doc/python.rst b/doc/python.rst index 4897937..05039cc 100755 --- a/doc/python.rst +++ b/doc/python.rst @@ -51,17 +51,73 @@ A |KeywordsSpec| is an MPL sequence where each element is either: * A *required* keyword tag, ``K`` * **or**, an *optional* keyword tag, ``K*`` -* **or**, a *somethingsomething* keyword tag ``K**`` +* **or**, a *special* keyword tag ``K**`` The **arity range** of a |KeywordsSpec| is determined by: .. parsed-literal:: - [ mpl::size - number of *somethingsomething* in ``S`` , mpl::size ] + [ mpl::size - number of *special* in ``S`` , mpl::size ] For example, the **arity range** of ``mpl::vector2`` is 2, the **arity range** of ``mpl::vector2`` is 2 and the **arity range** of ``mpl::vector2`` is 1. +*special* keyword tags +--------------------------------- + +If the default type for an argument is not convertible to the argument type, as +specified to the binding functions below, that argument must be specified as a +*special* argument. + +In the example below the default type for argument ``y`` is ``char const[5]``, but +the argument type is ``int``. Therefore ``y`` must be specified as a *special* +argument in the |KeywordSpec|. + +Doing this will generate N^2 overloads, where N is the number of *special* arguments. +In this case two overloads will be generated, one with ``y`` included and one without. +Having many *special* keywords will result in lots of overloads, and stress the +compiler. + +Note that this makes the *arity range* ``[1,2]``, so we'll need two forwarding overloads. + +.. parsed-literal:: + + BOOST_PARAMETER_FUNCTION((void), f, tag, + (required (x, \*)) + (optional (y, \*)) + ) + { + std::cout << args[x] << args[y | "none"] << "\n"; + } + + struct f_fwd + { + template + void operator()(boost::type, A0 const& a0) + { + f(a0); + } + + template + void operator()(boost::type, A0 const& a0, A1 const& a1) + { + f(a0, a1); + } + }; + + BOOST_PYTHON_MODULE(..) + { + class_("X") + .def("f", + function< + fwd + , mpl::vector2 + , mpl::vector3 + >() + ); + } + + ------------------------------------------------------------------------------ class template ``init`` @@ -259,7 +315,7 @@ of [2,2], so we only need one forwarding overload. { self.f(a0, a1); } - } + }; BOOST_PYTHON_MODULE(..) { @@ -302,6 +358,41 @@ Defines a named parameter enabled free function in the current Python scope. For every ``N`` in ``[U,V]``, where ``[U,V]`` is the **arity range** of ``Keywords``. +Example +~~~~~~~ + +This example exports a function ``f(int x, int y = ..)`` to Python. +The |KeywordsSpec| ``mpl::vector2`` has an **arity range** +of [2,2], so we only need one forwarding overload. + +.. parsed-literal:: + + BOOST_PARAMETER_FUNCTION((void), f, tag, + (required (x, \*)) + (optional (y, \*)) + ) + { + /\* .. \*/ + } + + struct f_fwd + { + template + void operator()(boost::type, A0 const& a0, A1 const& a1) + { + f(a0, a1); + } + }; + + BOOST_PYTHON_MODULE(..) + { + def< + fwd + , mpl::vector2 + , mpl::vector3 + >("f"); + } + Portability -----------