[C++-sig] Python Exported Method that Takes a Boost::Function Object

Roman Yakovenko roman.yakovenko at gmail.com
Mon Feb 1 20:40:56 CET 2010


On Mon, Feb 1, 2010 at 5:22 PM, Charles Solar <charlessolar at gmail.com> wrote:
> I have a method that takes a boost function object as so
>
> bool createTimer( boost::int64_t interval, boost::function< bool() >
> function, bool recurring = false )
>
> that I would like to call in python.  I tried exporting the class in Py++
> but it did not look like it does anything special with that argument.

Py++ generates the wrong code in this situation. It definitely could
do better job.

>
> which btw there is an error here in Py++, ::boost::function< bool ()() >
> should be ::boost::function< bool () >

Hmm. This is how gccxml reports the class name. I will take a look on this bug.

> that code also fails in python with the same error.
>
> I have seen the page here
> http://wiki.python.org/moin/boost.python/HowTo#boost.functionobjects about
> boost function objects but I do not think that is exactly what I am looking
> for.
>
> If there an easy way to get python to work with this function definition?
> Or am I trying to do something stupid?

No, I would call this a "challenge".

The following is just an untested idea:

1. create the following class:

class callback_wrapper_t{
    callback_wrapper_t( boost::python::object callable ){ store it in the class}

   bool operator()( bool ){
        call the function
   }
}

2. create a wrapper

bool createTimerWrapper( boost::int64_t interval,
boost::python::object function, bool recurring = false ){
    createTimer( interval, boost::function<bool>( callback_wrapper_t(
function ) ), recurring )
}

3. Register the createTimerWrapper

I think this could work. May be there is a better way. If you have a
lot of functions like this and the idea works, I can help you to
generate it with Py++.


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list