Nice! A little bit of cleaning up and editing and that worked like a charm,
thanks

Here is what I had to do:

struct timer_func_wrapper_t
{
    timer_func_wrapper_t( bp::object callable ) : _callable( callable ) {}

    bool operator()()
    {
        PyGILState_STATE gstate = PyGILState_Ensure();
        bool ret = _callable();
        PyGILState_Release( gstate );
        return ret;
    }

    bp::object _callable;
};

boost::int32_t createTimerWrapper( Class* class, boost::uint64_t interval,
bp::object function, bool recurring = false )
{
    return class->createTimer( interval, boost::function<bool ()>(
timer_func_wrapper_t( function ) ), recurring );
}

and then in the class def:

.def( "createTimer", &createTimerWrapper, ( bp::arg( "interval" ), bp::arg(
"function" ), bp::arg( "recurring" ) = false ) )

I think I kind of cheated with the wrapper function, the this pointer just
fills in the Class* hole.. I was actually surprised that it worked so
easily.

Anyway now I can do magic like

import MyLib
import time

def callMePls():
    print( "Hello world" )
    return True

class = MyLib.Class()

class.createTimer( 3, callMePls )

time.sleep( 1 )

Works great!

About the Py++ stuff though, I mainly use Py++ to see how I should export
something I am unsure of.  Which is why I mentioned that Py++ did not do
anything special, since I was unsure how to export this in the first place.
I type up and maintain my real export code though, so no rush on Py++
support, although I bet its as easy as generating a patch or something..
Anyway, great tool, great advice, thanks again.

On Mon, Feb 1, 2010 at 1:40 PM, Roman Yakovenko
<roman.yakove...@gmail.com>wrote:

> On Mon, Feb 1, 2010 at 5:22 PM, Charles Solar <charlesso...@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.functionobjectsabout
> > 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/
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to