Re: patch for g_nullify_pointer() and friends
- From: Sven Neumann <sven gimp org>
- To: Tim Janik <timj gtk org>
- Cc: Gtk+ Developers <gtk-devel-list gnome org>
- Subject: Re: patch for g_nullify_pointer() and friends
- Date: 06 Aug 2001 20:47:16 +0200
Hi,
here's a new patch. I stayed with g_nullify_pointer() since I agree
with Tim on this subject. The GObject additions are implemented as
functions now and are documented inline. Someone should please check
that I found the correct words. The patch also adds a simple test
case to testgruntime. There should probably be more tests there...
Salut, Sven
Index: glib/gutils.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gutils.c,v
retrieving revision 1.97
diff -u -r1.97 gutils.c
--- glib/gutils.c 2001/06/30 16:54:32 1.97
+++ glib/gutils.c 2001/08/06 18:42:05
@@ -1046,6 +1046,20 @@
}
/**
+ * g_nullify_pointer:
+ * @nullify_location: the memory address of the pointer.
+ *
+ * Set the pointer at the specified location to %NULL.
+ **/
+void
+g_nullify_pointer (gpointer *nullify_location)
+{
+ g_return_if_fail (nullify_location != NULL);
+
+ *nullify_location = NULL;
+}
+
+/**
* g_get_codeset:
*
* Get the codeset for the current locale.
Index: glib/gutils.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gutils.h,v
retrieving revision 1.10
diff -u -r1.10 gutils.h
--- glib/gutils.h 2001/07/10 22:37:07 1.10
+++ glib/gutils.h 2001/08/06 18:42:05
@@ -165,6 +165,10 @@
gchar* g_path_get_basename (const gchar *file_name);
gchar* g_path_get_dirname (const gchar *file_name);
+
+/* Set the pointer at the specified location to NULL */
+void g_nullify_pointer (gpointer *nullify_location);
+
/* Get the codeset for the current locale */
/* gchar * g_get_codeset (void); */
Index: gobject/gobject.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gobject.c,v
retrieving revision 1.39
diff -u -r1.39 gobject.c
--- gobject/gobject.c 2001/07/02 05:15:29 1.39
+++ gobject/gobject.c 2001/08/06 18:42:05
@@ -1262,6 +1262,49 @@
g_warning (G_STRLOC ": couldn't find weak ref %p(%p)", notify, data);
}
+/**
+ * g_object_add_weak_pointer:
+ * @object: the object that should be weak referenced.
+ * @weak_pointer_location: the memory address of a pointer.
+ *
+ * Adds a weak reference from weak_pointer to @object to indicate that
+ * the pointer located at @weak_pointer_location is only valid during the
+ * lifetime of @object. When the @object is finalized, @weak_pointer will
+ * be set to %NULL.
+ **/
+void
+g_object_add_weak_pointer (GObject *object,
+ gpointer *weak_pointer_location)
+{
+ g_return_if_fail (G_IS_OBJECT (object));
+ g_return_if_fail (weak_pointer_location != NULL);
+
+ g_object_weak_ref (object,
+ (GWeakNotify) g_nullify_pointer,
+ weak_pointer_location);
+}
+
+/**
+ * g_object_remove_weak_pointer:
+ * @object: the object that is weak referenced.
+ * @weak_pointer_location: the memory address of a pointer.
+ *
+ * Removes a weak reference from @object that was previously added
+ * using g_object_add_weak_pointer(). The @weak_pointer_location has
+ * to match the one used with g_object_add_weak_pointer().
+ **/
+void
+g_object_remove_weak_pointer (GObject *object,
+ gpointer *weak_pointer_location)
+{
+ g_return_if_fail (G_IS_OBJECT (object));
+ g_return_if_fail (weak_pointer_location != NULL);
+
+ g_object_weak_unref (object,
+ (GWeakNotify) g_nullify_pointer,
+ weak_pointer_location);
+}
+
gpointer
g_object_ref (gpointer _object)
{
Index: gobject/gobject.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gobject.h,v
retrieving revision 1.16
diff -u -r1.16 gobject.h
--- gobject/gobject.h 2001/07/02 05:02:13 1.16
+++ gobject/gobject.h 2001/08/06 18:42:05
@@ -154,6 +154,10 @@
void g_object_weak_unref (GObject *object,
GWeakNotify notify,
gpointer data);
+void g_object_add_weak_pointer (GObject *object,
+ gpointer *weak_pointer_location);
+void g_object_remove_weak_pointer (GObject *object,
+ gpointer *weak_pointer_location);
gpointer g_object_get_qdata (GObject *object,
GQuark quark);
void g_object_set_qdata (GObject *object,
Index: gobject/testgruntime.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/testgruntime.c,v
retrieving revision 1.3
diff -u -r1.3 testgruntime.c
--- gobject/testgruntime.c 2001/06/30 17:17:41 1.3
+++ gobject/testgruntime.c 2001/08/06 18:42:05
@@ -161,8 +161,10 @@
main (int argc,
char *argv[])
{
- TestObject *tobject, *sigarg;
- gchar *string = NULL;
+ TestObject *tobject;
+ TestObject *sigarg;
+ gpointer tobject_address;
+ gchar *string = NULL;
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
@@ -170,13 +172,21 @@
g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
tobject = g_object_new (TEST_TYPE_OBJECT, NULL);
+ tobject_address = tobject;
+ g_object_add_weak_pointer (G_OBJECT (tobject), &tobject_address);
+
sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
+
g_signal_emit_by_name (tobject, "test-signal", sigarg, NULL, &string);
g_message ("signal return: \"%s\"", string);
g_free (string);
g_object_unref (sigarg);
g_object_unref (tobject);
+
+ if (tobject_address != NULL)
+ g_message ("g_object_add_weak_pointer() failed, "
+ "tobject_address should be NULL.");
g_message ("%s done", argv[0]);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]