[C++-sig] Re: Boost::Signals - Python functions as slots

David Abrahams dave at boost-consulting.com
Sun Mar 28 00:15:33 CET 2004


Raoul Gough <raoulgough at yahoo.co.uk> writes:

> I think you will have to modify your doOnClick member function to
> accept a boost::python::function (or maybe boost::python::object)
> instead, and use this object to handle the signal. I assume that the
> slots/signals code can handle functors (i.e. objects with an
> operator()). I'm not sure if you will able to use the Python object
> directly for this, you might have to write a thin wrapper like this:
>
> struct python_function {
>   boost::python::object my_callable_object;
>
>   python_function (boost::python::object const &obj)
>     : my_callable_object (obj)
>   {
>   }
>
>   void operator()() {
>     my_callable_object();
>   }
> };
>
> Untested code! I'd be more definite about this, but I don't have access
> to the Boost.Python code at the moment.

     boost::python::object instances are callable directly as
     functions, and therefore there's an implicit conversion from
     boost::python::object instances to the boost::function instances
     used by Boost.Signals.  There's probably an easy way to take
     advantage of that fact to avoid writing so much extra
     code... but I'm speculating.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list