mirror of
https://github.com/boostorg/python.git
synced 2026-01-24 18:12:43 +00:00
Tutorial updates
[SVN r17598]
This commit is contained in:
@@ -76,10 +76,10 @@ automatic wrapping through <tt>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> and
|
||||
its sister, <tt>BOOST_PYTHON_FUNCTION_OVERLOADS</tt>. Following up on our example
|
||||
presented in the section <a href="overloading.html">
|
||||
on overloading</a>, since the
|
||||
first overload has default arguments, we can use
|
||||
<tt>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> to automatically wrap the first
|
||||
three of the <tt>def</tt>s above and manually wrap just the last. Here's how
|
||||
we'll do this:</p>
|
||||
first 4 overload functins have a common sequence of initial arguments, we
|
||||
can use <tt>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</tt> to automatically wrap the
|
||||
first three of the <tt>def</tt>s and manually wrap just the last. Here's
|
||||
how we'll do this:</p>
|
||||
<code><pre>
|
||||
<span class=identifier>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class=special>(</span><span class=identifier>xf_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>4</span><span class=special>)
|
||||
</span></pre></code>
|
||||
|
||||
@@ -25,8 +25,9 @@
|
||||
</tr>
|
||||
</table>
|
||||
<p>
|
||||
Continuing, now, at last, we can even derive from our base class Base in
|
||||
Python. Before we can do that, we have to set up our <tt>class_</tt> wrapper as:</p>
|
||||
Continuing, we can derive from our base class Base in Python and override
|
||||
the virtual function in Python. Before we can do that, we have to set up
|
||||
our <tt>class_</tt> wrapper as:</p>
|
||||
<code><pre>
|
||||
<span class=identifier>class_</span><span class=special><</span><span class=identifier>Base</span><span class=special>, </span><span class=identifier>BaseWrap</span><span class=special>, </span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>noncopyable</span><span class=special>>(</span><span class=string>"Base"</span><span class=special>)
|
||||
;
|
||||
|
||||
@@ -26,16 +26,24 @@
|
||||
</table>
|
||||
<p>
|
||||
The following illustrates a scheme for manually wrapping an overloaded
|
||||
member function or. Obviously, the same technique can be applied to
|
||||
wrapping overloaded non- member functions. Take note that this scheme
|
||||
applies to actual overloaded (member, non-member) functions as well as
|
||||
(member, non-member) functions with default arguments.</p>
|
||||
member functions. Of course, the same technique can be applied to wrapping
|
||||
overloaded non-member functions.</p>
|
||||
<p>
|
||||
We have here our C++ classes:</p>
|
||||
We have here our C++ class:</p>
|
||||
<code><pre>
|
||||
<span class=keyword>struct </span><span class=identifier>X
|
||||
</span><span class=special>{
|
||||
</span><span class=keyword>bool </span><span class=identifier>f</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>a</span><span class=special>, </span><span class=keyword>double </span><span class=identifier>b </span><span class=special>= </span><span class=number>0</span><span class=special>, </span><span class=keyword>char </span><span class=identifier>c </span><span class=special>= </span><span class=literal>'x'</span><span class=special>)
|
||||
</span><span class=keyword>bool </span><span class=identifier>f</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>a</span><span class=special>)
|
||||
{
|
||||
</span><span class=keyword>return </span><span class=keyword>true</span><span class=special>;
|
||||
}
|
||||
|
||||
</span><span class=keyword>bool </span><span class=identifier>f</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>a</span><span class=special>, </span><span class=keyword>double </span><span class=identifier>b</span><span class=special>)
|
||||
{
|
||||
</span><span class=keyword>return </span><span class=keyword>true</span><span class=special>;
|
||||
}
|
||||
|
||||
</span><span class=keyword>bool </span><span class=identifier>f</span><span class=special>(</span><span class=keyword>int </span><span class=identifier>a</span><span class=special>, </span><span class=keyword>double </span><span class=identifier>b</span><span class=special>, </span><span class=keyword>char </span><span class=identifier>c</span><span class=special>)
|
||||
{
|
||||
</span><span class=keyword>return </span><span class=keyword>true</span><span class=special>;
|
||||
}
|
||||
@@ -47,11 +55,8 @@ We have here our C++ classes:</p>
|
||||
};
|
||||
</span></pre></code>
|
||||
<p>
|
||||
Notice that class X has two overloaded functions with different signatures.
|
||||
The types of the arguments, and the return are totally different, unlike
|
||||
above where we have a common sequence of initial arguments.</p>
|
||||
<p>
|
||||
We shall start by introducing some member function pointer variables:</p>
|
||||
Class X has 4 overloaded functions. We shall start by introducing some
|
||||
member function pointer variables:</p>
|
||||
<code><pre>
|
||||
<span class=keyword>bool </span><span class=special>(</span><span class=identifier>X</span><span class=special>::*</span><span class=identifier>fx1</span><span class=special>)(</span><span class=keyword>int</span><span class=special>) = &</span><span class=identifier>X</span><span class=special>::</span><span class=identifier>f</span><span class=special>;
|
||||
</span><span class=keyword>bool </span><span class=special>(</span><span class=identifier>X</span><span class=special>::*</span><span class=identifier>fx2</span><span class=special>)(</span><span class=keyword>int</span><span class=special>, </span><span class=keyword>double</span><span class=special>) = &</span><span class=identifier>X</span><span class=special>::</span><span class=identifier>f</span><span class=special>;
|
||||
@@ -59,10 +64,6 @@ We shall start by introducing some member function pointer variables:</p>
|
||||
</span><span class=keyword>int </span><span class=special>(</span><span class=identifier>X</span><span class=special>::*</span><span class=identifier>fx4</span><span class=special>)(</span><span class=keyword>int</span><span class=special>, </span><span class=keyword>int</span><span class=special>, </span><span class=keyword>int</span><span class=special>) = &</span><span class=identifier>X</span><span class=special>::</span><span class=identifier>f</span><span class=special>;
|
||||
</span></pre></code>
|
||||
<p>
|
||||
The first three member function pointers take care of the first X::f
|
||||
overload. The one with default arguments. The last member function pointer
|
||||
takes care of the second X::f overload.</p>
|
||||
<p>
|
||||
With these in hand, we can proceed to define and wrap this for Python:</p>
|
||||
<code><pre>
|
||||
<span class=special>.</span><span class=identifier>def</span><span class=special>(</span><span class=string>"f"</span><span class=special>, </span><span class=identifier>fx1</span><span class=special>)
|
||||
|
||||
@@ -529,8 +529,9 @@ so will disallow abstract base classes such as [^Base] to be instantiated.
|
||||
|
||||
[page:1 Deriving a Python Class]
|
||||
|
||||
Continuing, now, at last, we can even derive from our base class Base in
|
||||
Python. Before we can do that, we have to set up our [^class_] wrapper as:
|
||||
Continuing, we can derive from our base class Base in Python and override
|
||||
the virtual function in Python. Before we can do that, we have to set up
|
||||
our [^class_] wrapper as:
|
||||
|
||||
class_<Base, BaseWrap, boost::noncopyable>("Base")
|
||||
;
|
||||
@@ -892,16 +893,24 @@ these can be found [@../../v2/reference.html#models_of_call_policies here].
|
||||
[page:1 Overloading]
|
||||
|
||||
The following illustrates a scheme for manually wrapping an overloaded
|
||||
member function or. Obviously, the same technique can be applied to
|
||||
wrapping overloaded non- member functions. Take note that this scheme
|
||||
applies to actual overloaded (member, non-member) functions as well as
|
||||
(member, non-member) functions with default arguments.
|
||||
member functions. Of course, the same technique can be applied to wrapping
|
||||
overloaded non-member functions.
|
||||
|
||||
We have here our C++ classes:
|
||||
We have here our C++ class:
|
||||
|
||||
struct X
|
||||
{
|
||||
bool f(int a, double b = 0, char c = 'x')
|
||||
bool f(int a)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool f(int a, double b)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool f(int a, double b, char c)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -912,21 +921,14 @@ We have here our C++ classes:
|
||||
};
|
||||
};
|
||||
|
||||
Notice that class X has two overloaded functions with different signatures.
|
||||
The types of the arguments, and the return are totally different, unlike
|
||||
above where we have a common sequence of initial arguments.
|
||||
|
||||
We shall start by introducing some member function pointer variables:
|
||||
Class X has 4 overloaded functions. We shall start by introducing some
|
||||
member function pointer variables:
|
||||
|
||||
bool (X::*fx1)(int) = &X::f;
|
||||
bool (X::*fx2)(int, double) = &X::f;
|
||||
bool (X::*fx3)(int, double, char)= &X::f;
|
||||
int (X::*fx4)(int, int, int) = &X::f;
|
||||
|
||||
The first three member function pointers take care of the first X::f
|
||||
overload. The one with default arguments. The last member function pointer
|
||||
takes care of the second X::f overload.
|
||||
|
||||
With these in hand, we can proceed to define and wrap this for Python:
|
||||
|
||||
.def("f", fx1)
|
||||
@@ -1098,10 +1100,10 @@ Actually, we can mix and match manual wrapping of overloaded functions and
|
||||
automatic wrapping through [^BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS] and
|
||||
its sister, [^BOOST_PYTHON_FUNCTION_OVERLOADS]. Following up on our example
|
||||
presented in the section [@overloading.html on overloading], since the
|
||||
first overload has default arguments, we can use
|
||||
[^BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS] to automatically wrap the first
|
||||
three of the [^def]s above and manually wrap just the last. Here's how
|
||||
we'll do this:
|
||||
first 4 overload functins have a common sequence of initial arguments, we
|
||||
can use [^BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS] to automatically wrap the
|
||||
first three of the [^def]s and manually wrap just the last. Here's
|
||||
how we'll do this:
|
||||
|
||||
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(xf_overloads, f, 1, 4)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user