Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-08 03:47:33


Kevin,

It's not the widget type that needs to be added to Boost, but the callback
function pointer type Fl_Callback. Boost has no way of knowing what type
such a function pointer should be converted into...

Well, technically, maybe it could know. Getting it to work would be quite a
trick. If you'd like to add a feature request to the tracker at
https://sourceforge.net/tracker/?func=add&group_id=7586&atid=357586, it
would be much appreciated.

In the meantime, you would have to create your own from_python/to_python
functions.

One approach you might consider is to have the to_python function call
boost::python::detail::new_wrapped_function_pointer(f) from
boost/python/detail/functions.hpp. Then you'll need a from_python function
which can check that its PyObject* argument has the type
boost::python::detail::function::type. This approach allows the wrapped
function pointer to be called from Python just like any other function.

Another, probably simpler way to deal with the problem would be to create a
trivial C++ class which wraps a Fl_Callback:

  struct Fl_Callback_wrapper {
    Fl_Callback callback;
  };

if you first instantiate its class_builder:

  template class boost::python::class_builder<Fl_Callback_wrapper>;

you should then be able to declare the following from_python/to_python
functions:

BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug
workaround

  Fl_Callback from_python(PyObject* o, boost::python::type<Fl_Callback>)
  {
    return from_python(PyObject* o,
        boost::python::type<Fl_Callback_wrapper>).callback;
  }

  Fl_Callback from_python(PyObject* o,
    boost::python::type<Fl_Callback const&>)
  {
    return from_python(PyObject* o,
        boost::python::type<Fl_Callback_wrapper>).callback;
  }

  PyObject* to_python(Fl_Callback cb)
  {
    Fl_Callback_wrapper w;
    w.callback = cb;
    return to_python(cb);
  }

BOOST_PYTHON_END_CONVERSION_NAMESPACE

HTH,

Dave
----- Original Message -----
From: "Kevin Dahlhausen" <kdahlhaus_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Sunday, April 08, 2001 2:00 AM
Subject: [boost] BPL - function pointers / callbacks?

> Hello,
>
> I'm looking into wrapping the Fast Light Toolkit with
> BPL. Some member functions take pointers to static
> functions which will be used as callbacks.
>
> The callback function signature is:
> void callback( Fl_Widget *, void *)
>
> This is registered with:
> typedef void (Fl_Callback)(Fl_Widget *, void *);
> void Fl_Widget::callback(Fl_Callback *c, void *p);
>
> The error this results in is:
> /home/kpd/src/boost-1.20.2/boost/python/caller.hpp:653:
> no matching function for call to `from_python
> (PyObject *&, boost::python::type<void (*)(Fl_Widget
> *, void *)>)'
> /home/kpd/src/boost-1.20.2/boost/python/conversions.hpp:80:
> candidates are: long int from_python(PyObject *,
> boost::python::type<long int>)
> .
> .
> .
>
> So, do I need to add the widget type to boost, that
> doesn't sound right.
>
> Do I need to change the interface to a Py_Object?
>
> Searching the archives did not reveal anything.
> Would someone be kind enough to post an example of
> registering a callback function?
>
> Thanks for your help!
>
>
>
> =====
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Kevin Dahlhausen kdahlhaus_at_[hidden]
>
> "Do or do not. There is no 'Try.' Yoda
>
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/
>
> To unsubscribe, send email to: <mailto:boost-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk