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

Raoul Gough raoulgough at yahoo.co.uk
Fri Mar 26 17:41:29 CET 2004


Benny Kramek benny at kramekweb.com 
Fri Mar 26 07:26:33 EST 2004 

>I'm using Boost::Signals in C++. I want to be able to attach python
>functions as slots to the C++ signals.
> Here is a very simple test that
>doesn't work. Everything compiles OK, but I get a Python error.

[snip]
>void Button::doOnClick(const boost::signal<void ()>::slot_type& slot)
>{
>    onClick.connect(slot);
>}
>
>### Python test
>
>def f():
>    print 'f'
>
>button = Button()
>button.doOnClick(f)
>
>### Python error
>
>    button.doOnClick(f)
>Boost.Python.ArgumentError: Python argument types in
>    Button.doOnClick(Button, function)
>did not match C++ signature:
>    doOnClick(Button {lvalue},
N5boost4slotINS_8functionIFvvESaIvEEEEE)
>
>The demangled name
>N5boost4slotINS_8functionIFvvESaIvEEEEE
>is
>boost::slot<boost::function<void ()(), std::allocator<void> > >
>
>I'm using gcc 3.2.3 and boost 1.31.0

When you pass the Python function f into a C++ routine I guess it is
represented as a boost::python::function object. You can't pass a
boost::python:object where a boost::slot<...> is expected, which is why
you are getting the (run-time) error.

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.

Regards,
Raoul Gough.


	
	
		
___________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html




More information about the Cplusplus-sig mailing list