Re: Marshalling CClosures



On Sun, 29 Jul 2001, ERDI Gergo wrote:

> On 29 Jul 2001, Havoc Pennington wrote:
> 
> > This still makes no sense to me, but perhaps you can ask him to
> > explain it, or maybe you can figure it out.
> 
> I want to use GClosures like SigC::Slots from the great C++ library
> libsigc++: `callback function objects' that I can pass around and call
> when I feel like it. So not tied to signals at all.
> 
> I see two workarounds to this current limitation:
> 	1, Creating a dummy GSignal, connecting the closure to it,
> emitting the singal and then distroying it
> 	2, touching the GClosure's marshal field (yes I know this is
> evil). 
> 
> In the hope of the current situation being only a temporary limitation,
> I've decided to go with #2, so the following works currently:
> 
> 	listener->priv->event_callback->marshal =
> 		bonobo_marshal_VOID__STRING_BOXED_BOXED;
> 	
> 	g_closure_invoke (listener->priv->event_callback,
> 			  NULL,
> 			  4,
> 			  arguments,
> 			  NULL);
> 
> but I feel dirty after using it.

and rightly so, you can have closure memory management messed up due to that
(probably not currently but possibly in the future).

supposing you have an interface like:

typedef void (*FooSIg) (gpointer, .../*foo args*/..., gpointer);
add_foo_notifier(GClosure*cl);

that is, closures passed in to add_foo_notifier() are always invoked with
argument lists of the same type, you can (should) do the following to
support C closures (non-C closures, e.g. from language bindings,  usually
have their own marshallers set already):

add_foo_notifier(GClosure*cl)
{
  g_closure_ref (cl);
  g_closure_sink (cl); /* all functions that take a closure need to sink it */
  if (G_CLOSURE_NEEDS_MARSHAL (cl))
    g_closure_set_marshal (cl, foo_marshal_VOID__FOO_ARGS);
}

where foo_marshal_VOID__FOO_ARGS() should be generated by glib-genmarshal.

> 
> Tim, will we ever get invokeable standalone C closures for GLIB 2?
> 

havoc gave the wrong impression by indicating that closures are only
usable from signals, we already support what you want.

---
ciaoTJ





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