Re: Using (or not using) sigc::mem_fun with the G_CALLBACK macro



On Thursday 16 June 2005 12:50, Paul Davis wrote:
> On Thu, 2005-06-16 at 06:08 -0500, Bob Caryl wrote:
> > Murray Cumming wrote:
> > >And if you must provided a C function pointer, you'll need to provide a
> > >pointer to a static method (or global function) and the state
> > >information as user_data. See the various SignalProxy classes, for
> > >instance, in TreeSelection.
> >
> > In other words, the way I've done it (separate executable invoked via
> > fork, execv) is the only thread safe way to do this until GtkHTML has a
> > C++ wrapper.
>
> what are you thinking? this has nothing to do with threads. it doesn't
> even have a *lot* to do with language, except for the fact that you
> can't use a non-static C++ member function as a C callback. if you
> really want to use C++ objects with GTK widgets, then do this:
>
>  class Foo {
>             public:
>         static gint my_c_proxy_for_bar (arglist...);
>         gint my_handler_for_bar (otherarglist...);
>         };
>
>  gint
>         Foo::my_c_proxy_for_bar (..., gptr user_data, ...)
>         {
>      return reinterpret_cast<Foo*>(user_data)->my_handler_for_bar
> (relevant_args...);
>  }
>
>  gint
>  Foo::my_handler_for_bar (otherarglist...)
>  {
>  }
>
> and then somewhere, connect
>
>  gtk_signal_connect (widget, "signal", ...., SomePtrToFoo);
>
> I have done this a lot with the GtkCanvas widget which has never been
> wrapped for C++ (and never will be; we're migrating to GNOME::Canvas
> ASAP)

You are not even guaranteed that static C++ member functions can be used as a 
C callback.

You should really use a function with C linkage as the proxy (probably in 
anonymous namespace so it is not exported).  Static member functions are not 
guaranteed to have the C linkage.  On g++ they do, but I believe on the Intel 
compiler as an example they do not, and your example will not compile.  (In 
your example the C++ handler is public, but if it were private the proxy 
function would also need to be a friend.)

Chris



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]