object-aware gmain functions



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?

Thanks,
James
? glib_jwillcox_gobject_timeout_v1.diff
Index: gobject/gobject.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gobject.c,v
retrieving revision 1.57
diff -u -r1.57 gobject.c
--- gobject/gobject.c	7 Feb 2003 22:04:24 -0000	1.57
+++ gobject/gobject.c	25 Feb 2003 17:04:23 -0000
@@ -1731,3 +1731,47 @@
 
   return closure;
 }
+
+static void
+weak_notify_cb (gpointer data, GObject *old_object)
+{
+  guint id = GPOINTER_TO_UINT (data);
+    
+  g_source_remove (id);
+}
+
+guint
+g_object_timeout_add (guint32 interval,
+    		      GSourceFunc function,
+		      GObject *object)
+{
+  guint id;
+
+  g_return_val_if_fail (function != NULL, 0);
+  g_return_val_if_fail (object != NULL, 0);
+  g_return_val_if_fail (G_IS_OBJECT (object), 0);
+
+  id = g_timeout_add (interval, function, object);
+
+  g_object_weak_ref (object, weak_notify_cb,
+      		     GUINT_TO_POINTER (id));
+
+  return id;
+}
+
+guint
+g_object_idle_add (GSourceFunc function, GObject *object)
+{
+  guint id;
+
+  g_return_val_if_fail (function != NULL, 0);
+  g_return_val_if_fail (object != NULL, 0);
+  g_return_val_if_fail (G_IS_OBJECT (object), 0);
+
+  id = g_idle_add (function, object);
+
+  g_object_weak_ref (object, weak_notify_cb,
+      		     GUINT_TO_POINTER (id));
+  
+  return id;
+}
Index: gobject/gobject.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gobject.h,v
retrieving revision 1.26
diff -u -r1.26 gobject.h
--- gobject/gobject.h	21 Mar 2002 00:34:04 -0000	1.26
+++ gobject/gobject.h	25 Feb 2003 17:04:23 -0000
@@ -205,6 +205,11 @@
 					       gpointer	       gobject,
 					       GConnectFlags   connect_flags);
 
+guint       g_object_idle_add                 (GSourceFunc     function,
+					       GObject        *object);
+guint       g_object_timeout_add              (guint32         interval,
+					       GSourceFunc     function,
+					       GObject        *object);
 
 /*< protected >*/
 void        g_object_run_dispose	      (GObject	      *object);


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