Re: object-aware gmain functions



On Tue, 2003-02-25 at 12:09, James Willcox wrote:
> Hi,
> 
> I and I'm sure others would like some g_idle_add and g_timeout_add
> functions which would take gobjects as the user_data and remove
> themselves when the object goes away (much like
> g_signal_connect_object).  Since it would have to go in gobject, maybe
> something like the following:
> 
> guint g_object_idle_add    (GSourceFunc function, GObject *object);
> guint g_object_timeout_add (guint interval, GSourceFunc function,
>                             GObject *object);
> 
> Patch attached (if people want this, I will add docs, changelog, etc). 
> What do you think?

void g_source_set_closure (GSource  *source,
                           GClosure *closure);
GClosure*   g_cclosure_new_object (GCallback callback_func,
                                   GObject   *object);

To give an idea of how they are used, you would implement g_object_idle_add()
as:

 void
 g_object_idle_add (GObject   *object, 
                    GCallback  callback)
 {
   GSource *source = g_idle_source_new();
   g_source_set_closure (source, 
                         g_cclosure_new_object (callback, object));
   g_source_attach (source, NULL);
   g_source_unref (source);
 }

Yeah, a little bit clunky, but considering all the variants you would need for:

 - What main loop
 - priority
 - Whether you have a destroy notifier

Etc, I don't think adding convenience functions here really make sense.
Both GSource and GClosure were meant to be "scalable" interfaces ...
able to be extended without adding _full() _fuller() _fullest()

Regards,
                                                 Owen





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