gobject: Add g_set_object() convenience function to set GObject pointers

Along the same lines as g_clear_object(), g_set_object() is a
convenience function to update a GObject pointer, handling reference
counting transparently and correctly.

Specifically, it handles the case where a pointer is set to its current
value. If handled naïvely, that could result in the object instance
being finalised. In the following code, that happens when
(my_obj == new_value) and the object has a single reference:
    g_clear_object (&my_obj);
    my_obj = g_object_ref (new_value);

It also simplifies boilerplate code such as set_property()
implementations, which are otherwise long and boring.

Test cases included.

https://bugzilla.gnome.org/show_bug.cgi?id=741589
This commit is contained in:
Philip Withnall
2014-12-16 11:29:03 +00:00
parent 4f3ab40c04
commit d951db4236
3 changed files with 124 additions and 0 deletions

View File

@@ -263,6 +263,7 @@ GParameter
g_object_ref
g_object_unref
g_object_ref_sink
g_set_object
g_clear_object
GInitiallyUnowned
GInitiallyUnownedClass