[C++-sig] overloaded member functions in boost.python

Dave Abrahams dave at boost-consulting.com
Thu Feb 6 14:23:30 CET 2003


On Thursday, February 06, 2003 2:45 AM [GMT+1=CET],
Anthony Baxter <anthony at interlink.com.au> wrote:

> [I initially sent this to python-list - I completely forgot about
> c++-sig. Any help would be greatly appreciated...]
>
>
> Does anyone have any examples using Boost::Python that are more complex
> than the simple ones in the tutorial? I'm trying to wrap a large class
> library (OpenH323) and I'm constantly banging my head against weird
> errors that are more or less trial and error to figure out. The only
> chunks of code I can find are the bits in the "test" directory of the
> distribution, and they're all a bit... artificial.
>
> The specific case I'm trying to solve at the moment is that I have
> a class (H323Connection) that defines a number of variants of a member
> function MakeCall:
>
>     H323Connection * MakeCall(
>       const PString & remoteParty,  /// Remote party to call
>       PString & token,              /// String to receive token for
>       connection void * userData = NULL        /// user data to pass to
>     CreateConnection );
>
>     H323Connection * MakeCall(
>       const PString & remoteParty,  /// Remote party to call
>       H323Transport * transport,    /// Transport to use for call.
>       PString & token,              /// String to receive token for
>       connection void * userData = NULL        /// user data to pass to
>     CreateConnection );

What is "connection void*"?  That doesn't look like a legal type to me.

> PString is the class library's own String class - I've already got
> functions working that accept them, and return them, fine (using
> converters).
>
> It's really not clear to me how I'm supposed to specify which of these
> methods I want to call. The BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro
> seems to be more about the numbers of arguments, rather than choosing
> from methods with different argument types.

Exactly.  I keep thinking we should rename those macros somehow to indicate
that.

Dealing with a pair of overloaded functions like that is simple, yet
tedious.  You need to get a pointer to each of them.  The only way to do
that is to do something which discriminates the type.  The safest way is:

  H323Connect* (H323EndPoint::*f1)(const PString&,PString&,connection void*)
= MakeCall;
  H323Connect* (H323EndPoint::*f2)(const PString&,H323Transport*
PString&,connection void*) = MakeCall;

    ...

    class_<H323EndPoint>("H323EndPoint")
           .def("MakeCall", f1)
           .def("MakeCall", f2)
           ;

> I've tried reading the reference documentation, to no avail. Is there
> another source of information out there that I'm missing?

I'm going to try to get Joel to put this in the tutorial, since everyone
seems to ask all the time.

There are some Wiki pages here:
http://www.python.org/cgi-bin/moinmoin/boost_2epython, though I'm not sure
what condition they're in.

The latest on-line docs are always at:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/python/doc/index.html

HTH,
-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list