1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * MT safe with regards to reference counting.
30 #include "gtype-private.h"
31 #include "gvaluecollector.h"
33 #include "gparamspecs.h"
34 #include "gvaluetypes.h"
35 #include "gobject_trace.h"
36 #include "gconstructor.h"
41 * @short_description: The base object type
42 * @see_also: #GParamSpecObject, g_param_spec_object()
44 * GObject is the fundamental type providing the common attributes and
45 * methods for all object types in GTK+, Pango and other libraries
46 * based on GObject. The GObject class provides methods for object
47 * construction and destruction, property access methods, and signal
48 * support. Signals are described in detail in <xref
49 * linkend="gobject-Signals"/>.
51 * <para id="floating-ref">
52 * GInitiallyUnowned is derived from GObject. The only difference between
53 * the two is that the initial reference of a GInitiallyUnowned is flagged
54 * as a <firstterm>floating</firstterm> reference.
55 * This means that it is not specifically claimed to be "owned" by
56 * any code portion. The main motivation for providing floating references is
57 * C convenience. In particular, it allows code to be written as:
59 * container = create_container ();
60 * container_add_child (container, create_child());
62 * If <function>container_add_child()</function> will g_object_ref_sink() the
63 * passed in child, no reference of the newly created child is leaked.
64 * Without floating references, <function>container_add_child()</function>
65 * can only g_object_ref() the new child, so to implement this code without
66 * reference leaks, it would have to be written as:
69 * container = create_container ();
70 * child = create_child ();
71 * container_add_child (container, child);
72 * g_object_unref (child);
74 * The floating reference can be converted into
75 * an ordinary reference by calling g_object_ref_sink().
76 * For already sunken objects (objects that don't have a floating reference
77 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
79 * Since floating references are useful almost exclusively for C convenience,
80 * language bindings that provide automated reference and memory ownership
81 * maintenance (such as smart pointers or garbage collection) should not
82 * expose floating references in their API.
85 * Some object implementations may need to save an objects floating state
86 * across certain code portions (an example is #GtkMenu), to achieve this,
87 * the following sequence can be used:
90 * /* save floating state */
91 * gboolean was_floating = g_object_is_floating (object);
92 * g_object_ref_sink (object);
93 * /* protected code portion */
95 * /* restore floating state */
97 * g_object_force_floating (object);
99 * g_object_unref (object); /* release previously acquired reference */
105 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
106 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
108 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
109 #define OBJECT_HAS_TOGGLE_REF(object) \
110 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
111 #define OBJECT_FLOATING_FLAG 0x2
113 #define CLASS_HAS_PROPS_FLAG 0x1
114 #define CLASS_HAS_PROPS(class) \
115 ((class)->flags & CLASS_HAS_PROPS_FLAG)
116 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
117 ((class)->constructor != g_object_constructor)
118 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
119 ((class)->constructed != g_object_constructed)
121 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
122 #define CLASS_HAS_DERIVED_CLASS(class) \
123 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
125 /* --- signals --- */
132 /* --- properties --- */
138 /* --- prototypes --- */
139 static void g_object_base_class_init (GObjectClass *class);
140 static void g_object_base_class_finalize (GObjectClass *class);
141 static void g_object_do_class_init (GObjectClass *class);
142 static void g_object_init (GObject *object,
143 GObjectClass *class);
144 static GObject* g_object_constructor (GType type,
145 guint n_construct_properties,
146 GObjectConstructParam *construct_params);
147 static void g_object_constructed (GObject *object);
148 static void g_object_real_dispose (GObject *object);
149 static void g_object_finalize (GObject *object);
150 static void g_object_do_set_property (GObject *object,
154 static void g_object_do_get_property (GObject *object,
158 static void g_value_object_init (GValue *value);
159 static void g_value_object_free_value (GValue *value);
160 static void g_value_object_copy_value (const GValue *src_value,
162 static void g_value_object_transform_value (const GValue *src_value,
164 static gpointer g_value_object_peek_pointer (const GValue *value);
165 static gchar* g_value_object_collect_value (GValue *value,
166 guint n_collect_values,
167 GTypeCValue *collect_values,
168 guint collect_flags);
169 static gchar* g_value_object_lcopy_value (const GValue *value,
170 guint n_collect_values,
171 GTypeCValue *collect_values,
172 guint collect_flags);
173 static void g_object_dispatch_properties_changed (GObject *object,
175 GParamSpec **pspecs);
176 static guint object_floating_flag_handler (GObject *object,
179 static void object_interface_check_properties (gpointer func_data,
182 /* --- typedefs --- */
183 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
185 struct _GObjectNotifyQueue
189 guint16 freeze_count;
192 /* --- variables --- */
193 G_LOCK_DEFINE_STATIC (closure_array_mutex);
194 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
195 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
196 static GQuark quark_closure_array = 0;
197 static GQuark quark_weak_refs = 0;
198 static GQuark quark_toggle_refs = 0;
199 static GQuark quark_notify_queue;
200 static GParamSpecPool *pspec_pool = NULL;
201 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
202 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
203 G_LOCK_DEFINE_STATIC (construction_mutex);
204 static GSList *construction_objects = NULL;
205 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
206 static GQuark quark_weak_locations = 0;
207 static GRWLock weak_locations_lock;
209 G_LOCK_DEFINE_STATIC(notify_lock);
211 /* --- functions --- */
213 g_object_notify_queue_free (gpointer data)
215 GObjectNotifyQueue *nqueue = data;
217 g_slist_free (nqueue->pspecs);
218 g_slice_free (GObjectNotifyQueue, nqueue);
221 static GObjectNotifyQueue*
222 g_object_notify_queue_freeze (GObject *object,
223 gboolean conditional)
225 GObjectNotifyQueue *nqueue;
228 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
233 G_UNLOCK(notify_lock);
237 nqueue = g_slice_new0 (GObjectNotifyQueue);
238 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
239 nqueue, g_object_notify_queue_free);
242 if (nqueue->freeze_count >= 65535)
243 g_critical("Free queue for %s (%p) is larger than 65535,"
244 " called g_object_freeze_notify() too often."
245 " Forgot to call g_object_thaw_notify() or infinite loop",
246 G_OBJECT_TYPE_NAME (object), object);
248 nqueue->freeze_count++;
249 G_UNLOCK(notify_lock);
255 g_object_notify_queue_thaw (GObject *object,
256 GObjectNotifyQueue *nqueue)
258 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
262 g_return_if_fail (nqueue->freeze_count > 0);
263 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
267 /* Just make sure we never get into some nasty race condition */
268 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
269 G_UNLOCK(notify_lock);
270 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
271 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
275 nqueue->freeze_count--;
276 if (nqueue->freeze_count) {
277 G_UNLOCK(notify_lock);
281 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
283 for (slist = nqueue->pspecs; slist; slist = slist->next)
285 pspecs[n_pspecs++] = slist->data;
287 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
289 G_UNLOCK(notify_lock);
292 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
297 g_object_notify_queue_add (GObject *object,
298 GObjectNotifyQueue *nqueue,
303 g_return_if_fail (nqueue->n_pspecs < 65535);
305 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
307 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
311 G_UNLOCK(notify_lock);
314 #ifdef G_ENABLE_DEBUG
315 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
316 G_LOCK_DEFINE_STATIC (debug_objects);
317 static volatile GObject *g_trap_object_ref = NULL;
318 static guint debug_objects_count = 0;
319 static GHashTable *debug_objects_ht = NULL;
322 debug_objects_foreach (gpointer key,
326 GObject *object = value;
328 g_message ("[%p] stale %s\tref_count=%u",
330 G_OBJECT_TYPE_NAME (object),
334 #ifdef G_HAS_CONSTRUCTORS
335 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
336 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
338 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
339 #endif /* G_HAS_CONSTRUCTORS */
342 debug_objects_atexit (void)
346 G_LOCK (debug_objects);
347 g_message ("stale GObjects: %u", debug_objects_count);
348 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
349 G_UNLOCK (debug_objects);
352 #endif /* G_ENABLE_DEBUG */
355 _g_object_type_init (void)
357 static gboolean initialized = FALSE;
358 static const GTypeFundamentalInfo finfo = {
359 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
362 sizeof (GObjectClass),
363 (GBaseInitFunc) g_object_base_class_init,
364 (GBaseFinalizeFunc) g_object_base_class_finalize,
365 (GClassInitFunc) g_object_do_class_init,
366 NULL /* class_destroy */,
367 NULL /* class_data */,
370 (GInstanceInitFunc) g_object_init,
371 NULL, /* value_table */
373 static const GTypeValueTable value_table = {
374 g_value_object_init, /* value_init */
375 g_value_object_free_value, /* value_free */
376 g_value_object_copy_value, /* value_copy */
377 g_value_object_peek_pointer, /* value_peek_pointer */
378 "p", /* collect_format */
379 g_value_object_collect_value, /* collect_value */
380 "p", /* lcopy_format */
381 g_value_object_lcopy_value, /* lcopy_value */
385 g_return_if_fail (initialized == FALSE);
390 info.value_table = &value_table;
391 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
392 g_assert (type == G_TYPE_OBJECT);
393 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
395 #ifdef G_ENABLE_DEBUG
398 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
399 #ifndef G_HAS_CONSTRUCTORS
400 g_atexit (debug_objects_atexit);
401 #endif /* G_HAS_CONSTRUCTORS */
403 #endif /* G_ENABLE_DEBUG */
407 g_object_base_class_init (GObjectClass *class)
409 GObjectClass *pclass = g_type_class_peek_parent (class);
411 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
412 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
415 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
417 /* reset instance specific fields and methods that don't get inherited */
418 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
419 class->get_property = NULL;
420 class->set_property = NULL;
424 g_object_base_class_finalize (GObjectClass *class)
428 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
430 g_slist_free (class->construct_properties);
431 class->construct_properties = NULL;
432 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
433 for (node = list; node; node = node->next)
435 GParamSpec *pspec = node->data;
437 g_param_spec_pool_remove (pspec_pool, pspec);
438 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
439 g_param_spec_unref (pspec);
445 g_object_do_class_init (GObjectClass *class)
447 /* read the comment about typedef struct CArray; on why not to change this quark */
448 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
450 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
451 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
452 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
453 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
454 pspec_pool = g_param_spec_pool_new (TRUE);
456 class->constructor = g_object_constructor;
457 class->constructed = g_object_constructed;
458 class->set_property = g_object_do_set_property;
459 class->get_property = g_object_do_get_property;
460 class->dispose = g_object_real_dispose;
461 class->finalize = g_object_finalize;
462 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
463 class->notify = NULL;
467 * @gobject: the object which received the signal.
468 * @pspec: the #GParamSpec of the property which changed.
470 * The notify signal is emitted on an object when one of its
471 * properties has been changed. Note that getting this signal
472 * doesn't guarantee that the value of the property has actually
473 * changed, it may also be emitted when the setter for the property
474 * is called to reinstate the previous value.
476 * This signal is typically used to obtain change notification for a
477 * single property, by specifying the property name as a detail in the
478 * g_signal_connect() call, like this:
480 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
481 * G_CALLBACK (gtk_text_view_target_list_notify),
484 * It is important to note that you must use
485 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
486 * detail strings for the notify signal.
488 gobject_signals[NOTIFY] =
489 g_signal_new (g_intern_static_string ("notify"),
490 G_TYPE_FROM_CLASS (class),
491 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
492 G_STRUCT_OFFSET (GObjectClass, notify),
494 g_cclosure_marshal_VOID__PARAM,
498 /* Install a check function that we'll use to verify that classes that
499 * implement an interface implement all properties for that interface
501 g_type_add_interface_check (NULL, object_interface_check_properties);
505 install_property_internal (GType g_type,
509 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
511 g_warning ("When installing property: type `%s' already has a property named `%s'",
512 g_type_name (g_type),
517 g_param_spec_ref_sink (pspec);
518 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
519 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
523 * g_object_class_install_property:
524 * @oclass: a #GObjectClass
525 * @property_id: the id for the new property
526 * @pspec: the #GParamSpec for the new property
528 * Installs a new property. This is usually done in the class initializer.
530 * Note that it is possible to redefine a property in a derived class,
531 * by installing a property with the same name. This can be useful at times,
532 * e.g. to change the range of allowed values or the default value.
535 g_object_class_install_property (GObjectClass *class,
539 g_return_if_fail (G_IS_OBJECT_CLASS (class));
540 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
542 if (CLASS_HAS_DERIVED_CLASS (class))
543 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
545 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (class)))
546 g_error ("Attempt to add property %s::%s after class was initialised", G_OBJECT_CLASS_NAME (class), pspec->name);
548 class->flags |= CLASS_HAS_PROPS_FLAG;
550 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
551 if (pspec->flags & G_PARAM_WRITABLE)
552 g_return_if_fail (class->set_property != NULL);
553 if (pspec->flags & G_PARAM_READABLE)
554 g_return_if_fail (class->get_property != NULL);
555 g_return_if_fail (property_id > 0);
556 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
557 if (pspec->flags & G_PARAM_CONSTRUCT)
558 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
559 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
560 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
562 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
564 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
565 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
567 /* for property overrides of construct properties, we have to get rid
568 * of the overidden inherited construct property
570 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
571 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
576 * g_object_class_install_properties:
577 * @oclass: a #GObjectClass
578 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
579 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
580 * defining the new properties
582 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
583 * usually done in the class initializer.
585 * The property id of each property is the index of each #GParamSpec in
588 * The property id of 0 is treated specially by #GObject and it should not
589 * be used to store a #GParamSpec.
591 * This function should be used if you plan to use a static array of
592 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
593 * class initialization:
597 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
600 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
603 * my_object_class_init (MyObjectClass *klass)
605 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
607 * obj_properties[PROP_FOO] =
608 * g_param_spec_int ("foo", "Foo", "Foo",
611 * G_PARAM_READWRITE);
613 * obj_properties[PROP_BAR] =
614 * g_param_spec_string ("bar", "Bar", "Bar",
616 * G_PARAM_READWRITE);
618 * gobject_class->set_property = my_object_set_property;
619 * gobject_class->get_property = my_object_get_property;
620 * g_object_class_install_properties (gobject_class,
626 * allows calling g_object_notify_by_pspec() to notify of property changes:
630 * my_object_set_foo (MyObject *self, gint foo)
632 * if (self->foo != foo)
635 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
643 g_object_class_install_properties (GObjectClass *oclass,
647 GType oclass_type, parent_type;
650 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
651 g_return_if_fail (n_pspecs > 1);
652 g_return_if_fail (pspecs[0] == NULL);
654 if (CLASS_HAS_DERIVED_CLASS (oclass))
655 g_error ("Attempt to add properties to %s after it was derived",
656 G_OBJECT_CLASS_NAME (oclass));
658 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (oclass)))
659 g_error ("Attempt to add properties %s after it was initialised", G_OBJECT_CLASS_NAME (oclass));
661 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
662 parent_type = g_type_parent (oclass_type);
664 /* we skip the first element of the array as it would have a 0 prop_id */
665 for (i = 1; i < n_pspecs; i++)
667 GParamSpec *pspec = pspecs[i];
669 g_return_if_fail (pspec != NULL);
671 if (pspec->flags & G_PARAM_WRITABLE)
672 g_return_if_fail (oclass->set_property != NULL);
673 if (pspec->flags & G_PARAM_READABLE)
674 g_return_if_fail (oclass->get_property != NULL);
675 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
676 if (pspec->flags & G_PARAM_CONSTRUCT)
677 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
678 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
679 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
681 oclass->flags |= CLASS_HAS_PROPS_FLAG;
682 install_property_internal (oclass_type, i, pspec);
684 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
685 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
687 /* for property overrides of construct properties, we have to get rid
688 * of the overidden inherited construct property
690 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
691 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
692 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
697 * g_object_interface_install_property:
698 * @g_iface: any interface vtable for the interface, or the default
699 * vtable for the interface.
700 * @pspec: the #GParamSpec for the new property
702 * Add a property to an interface; this is only useful for interfaces
703 * that are added to GObject-derived types. Adding a property to an
704 * interface forces all objects classes with that interface to have a
705 * compatible property. The compatible property could be a newly
706 * created #GParamSpec, but normally
707 * g_object_class_override_property() will be used so that the object
708 * class only needs to provide an implementation and inherits the
709 * property description, default value, bounds, and so forth from the
710 * interface property.
712 * This function is meant to be called from the interface's default
713 * vtable initialization function (the @class_init member of
714 * #GTypeInfo.) It must not be called after after @class_init has
715 * been called for any object types implementing this interface.
720 g_object_interface_install_property (gpointer g_iface,
723 GTypeInterface *iface_class = g_iface;
725 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
726 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
727 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
728 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
730 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
731 if (pspec->flags & G_PARAM_CONSTRUCT)
732 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
733 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
734 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
736 install_property_internal (iface_class->g_type, 0, pspec);
740 * g_object_class_find_property:
741 * @oclass: a #GObjectClass
742 * @property_name: the name of the property to look up
744 * Looks up the #GParamSpec for a property of a class.
746 * Returns: (transfer none): the #GParamSpec for the property, or
747 * %NULL if the class doesn't have a property of that name
750 g_object_class_find_property (GObjectClass *class,
751 const gchar *property_name)
754 GParamSpec *redirect;
756 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
757 g_return_val_if_fail (property_name != NULL, NULL);
759 pspec = g_param_spec_pool_lookup (pspec_pool,
761 G_OBJECT_CLASS_TYPE (class),
765 redirect = g_param_spec_get_redirect_target (pspec);
776 * g_object_interface_find_property:
777 * @g_iface: any interface vtable for the interface, or the default
778 * vtable for the interface
779 * @property_name: name of a property to lookup.
781 * Find the #GParamSpec with the given name for an
782 * interface. Generally, the interface vtable passed in as @g_iface
783 * will be the default vtable from g_type_default_interface_ref(), or,
784 * if you know the interface has already been loaded,
785 * g_type_default_interface_peek().
789 * Returns: (transfer none): the #GParamSpec for the property of the
790 * interface with the name @property_name, or %NULL if no
791 * such property exists.
794 g_object_interface_find_property (gpointer g_iface,
795 const gchar *property_name)
797 GTypeInterface *iface_class = g_iface;
799 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
800 g_return_val_if_fail (property_name != NULL, NULL);
802 return g_param_spec_pool_lookup (pspec_pool,
809 * g_object_class_override_property:
810 * @oclass: a #GObjectClass
811 * @property_id: the new property ID
812 * @name: the name of a property registered in a parent class or
813 * in an interface of this class.
815 * Registers @property_id as referring to a property with the
816 * name @name in a parent class or in an interface implemented
817 * by @oclass. This allows this class to <firstterm>override</firstterm>
818 * a property implementation in a parent class or to provide
819 * the implementation of a property from an interface.
822 * Internally, overriding is implemented by creating a property of type
823 * #GParamSpecOverride; generally operations that query the properties of
824 * the object class, such as g_object_class_find_property() or
825 * g_object_class_list_properties() will return the overridden
826 * property. However, in one case, the @construct_properties argument of
827 * the @constructor virtual function, the #GParamSpecOverride is passed
828 * instead, so that the @param_id field of the #GParamSpec will be
829 * correct. For virtually all uses, this makes no difference. If you
830 * need to get the overridden property, you can call
831 * g_param_spec_get_redirect_target().
837 g_object_class_override_property (GObjectClass *oclass,
841 GParamSpec *overridden = NULL;
845 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
846 g_return_if_fail (property_id > 0);
847 g_return_if_fail (name != NULL);
849 /* Find the overridden property; first check parent types
851 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
852 if (parent_type != G_TYPE_NONE)
853 overridden = g_param_spec_pool_lookup (pspec_pool,
862 /* Now check interfaces
864 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
865 while (n_ifaces-- && !overridden)
867 overridden = g_param_spec_pool_lookup (pspec_pool,
878 g_warning ("%s: Can't find property to override for '%s::%s'",
879 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
883 new = g_param_spec_override (name, overridden);
884 g_object_class_install_property (oclass, property_id, new);
888 * g_object_class_list_properties:
889 * @oclass: a #GObjectClass
890 * @n_properties: (out): return location for the length of the returned array
892 * Get an array of #GParamSpec* for all properties of a class.
894 * Returns: (array length=n_properties) (transfer container): an array of
895 * #GParamSpec* which should be freed after use
897 GParamSpec** /* free result */
898 g_object_class_list_properties (GObjectClass *class,
899 guint *n_properties_p)
904 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
906 pspecs = g_param_spec_pool_list (pspec_pool,
907 G_OBJECT_CLASS_TYPE (class),
916 * g_object_interface_list_properties:
917 * @g_iface: any interface vtable for the interface, or the default
918 * vtable for the interface
919 * @n_properties_p: (out): location to store number of properties returned.
921 * Lists the properties of an interface.Generally, the interface
922 * vtable passed in as @g_iface will be the default vtable from
923 * g_type_default_interface_ref(), or, if you know the interface has
924 * already been loaded, g_type_default_interface_peek().
928 * Returns: (array length=n_properties_p) (transfer container): a
929 * pointer to an array of pointers to #GParamSpec
930 * structures. The paramspecs are owned by GLib, but the
931 * array should be freed with g_free() when you are done with
935 g_object_interface_list_properties (gpointer g_iface,
936 guint *n_properties_p)
938 GTypeInterface *iface_class = g_iface;
942 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
944 pspecs = g_param_spec_pool_list (pspec_pool,
954 g_object_init (GObject *object,
957 object->ref_count = 1;
958 object->qdata = NULL;
960 if (CLASS_HAS_PROPS (class))
962 /* freeze object's notification queue, g_object_newv() preserves pairedness */
963 g_object_notify_queue_freeze (object, FALSE);
966 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
968 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
969 G_LOCK (construction_mutex);
970 construction_objects = g_slist_prepend (construction_objects, object);
971 G_UNLOCK (construction_mutex);
974 #ifdef G_ENABLE_DEBUG
977 G_LOCK (debug_objects);
978 debug_objects_count++;
979 g_hash_table_insert (debug_objects_ht, object, object);
980 G_UNLOCK (debug_objects);
982 #endif /* G_ENABLE_DEBUG */
986 g_object_do_set_property (GObject *object,
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1000 g_object_do_get_property (GObject *object,
1005 switch (property_id)
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1014 g_object_real_dispose (GObject *object)
1016 g_signal_handlers_destroy (object);
1017 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1018 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1022 g_object_finalize (GObject *object)
1024 g_datalist_clear (&object->qdata);
1026 #ifdef G_ENABLE_DEBUG
1029 G_LOCK (debug_objects);
1030 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1031 g_hash_table_remove (debug_objects_ht, object);
1032 debug_objects_count--;
1033 G_UNLOCK (debug_objects);
1035 #endif /* G_ENABLE_DEBUG */
1040 g_object_dispatch_properties_changed (GObject *object,
1042 GParamSpec **pspecs)
1046 for (i = 0; i < n_pspecs; i++)
1047 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1051 * g_object_run_dispose:
1052 * @object: a #GObject
1054 * Releases all references to other objects. This can be used to break
1057 * This functions should only be called from object system implementations.
1060 g_object_run_dispose (GObject *object)
1062 g_return_if_fail (G_IS_OBJECT (object));
1063 g_return_if_fail (object->ref_count > 0);
1065 g_object_ref (object);
1066 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1067 G_OBJECT_GET_CLASS (object)->dispose (object);
1068 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1069 g_object_unref (object);
1073 * g_object_freeze_notify:
1074 * @object: a #GObject
1076 * Increases the freeze count on @object. If the freeze count is
1077 * non-zero, the emission of "notify" signals on @object is
1078 * stopped. The signals are queued until the freeze count is decreased
1079 * to zero. Duplicate notifications are squashed so that at most one
1080 * #GObject::notify signal is emitted for each property modified while the
1083 * This is necessary for accessors that modify multiple properties to prevent
1084 * premature notification while the object is still being modified.
1087 g_object_freeze_notify (GObject *object)
1089 g_return_if_fail (G_IS_OBJECT (object));
1091 if (g_atomic_int_get (&object->ref_count) == 0)
1094 g_object_ref (object);
1095 g_object_notify_queue_freeze (object, FALSE);
1096 g_object_unref (object);
1100 get_notify_pspec (GParamSpec *pspec)
1102 GParamSpec *redirected;
1104 /* we don't notify on non-READABLE parameters */
1105 if (~pspec->flags & G_PARAM_READABLE)
1108 /* if the paramspec is redirected, notify on the target */
1109 redirected = g_param_spec_get_redirect_target (pspec);
1110 if (redirected != NULL)
1113 /* else, notify normally */
1118 g_object_notify_by_spec_internal (GObject *object,
1121 GParamSpec *notify_pspec;
1123 notify_pspec = get_notify_pspec (pspec);
1125 if (notify_pspec != NULL)
1127 GObjectNotifyQueue *nqueue;
1129 /* conditional freeze: only increase freeze count if already frozen */
1130 nqueue = g_object_notify_queue_freeze (object, TRUE);
1134 /* we're frozen, so add to the queue and release our freeze */
1135 g_object_notify_queue_add (object, nqueue, notify_pspec);
1136 g_object_notify_queue_thaw (object, nqueue);
1139 /* not frozen, so just dispatch the notification directly */
1140 G_OBJECT_GET_CLASS (object)
1141 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1147 * @object: a #GObject
1148 * @property_name: the name of a property installed on the class of @object.
1150 * Emits a "notify" signal for the property @property_name on @object.
1152 * When possible, eg. when signaling a property change from within the class
1153 * that registered the property, you should use g_object_notify_by_pspec()
1157 g_object_notify (GObject *object,
1158 const gchar *property_name)
1162 g_return_if_fail (G_IS_OBJECT (object));
1163 g_return_if_fail (property_name != NULL);
1164 if (g_atomic_int_get (&object->ref_count) == 0)
1167 g_object_ref (object);
1168 /* We don't need to get the redirect target
1169 * (by, e.g. calling g_object_class_find_property())
1170 * because g_object_notify_queue_add() does that
1172 pspec = g_param_spec_pool_lookup (pspec_pool,
1174 G_OBJECT_TYPE (object),
1178 g_warning ("%s: object class `%s' has no property named `%s'",
1180 G_OBJECT_TYPE_NAME (object),
1183 g_object_notify_by_spec_internal (object, pspec);
1184 g_object_unref (object);
1188 * g_object_notify_by_pspec:
1189 * @object: a #GObject
1190 * @pspec: the #GParamSpec of a property installed on the class of @object.
1192 * Emits a "notify" signal for the property specified by @pspec on @object.
1194 * This function omits the property name lookup, hence it is faster than
1195 * g_object_notify().
1197 * One way to avoid using g_object_notify() from within the
1198 * class that registered the properties, and using g_object_notify_by_pspec()
1199 * instead, is to store the GParamSpec used with
1200 * g_object_class_install_property() inside a static array, e.g.:
1210 * static GParamSpec *properties[PROP_LAST];
1213 * my_object_class_init (MyObjectClass *klass)
1215 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1218 * G_PARAM_READWRITE);
1219 * g_object_class_install_property (gobject_class,
1221 * properties[PROP_FOO]);
1225 * and then notify a change on the "foo" property with:
1228 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1234 g_object_notify_by_pspec (GObject *object,
1238 g_return_if_fail (G_IS_OBJECT (object));
1239 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1241 g_object_ref (object);
1242 g_object_notify_by_spec_internal (object, pspec);
1243 g_object_unref (object);
1247 * g_object_thaw_notify:
1248 * @object: a #GObject
1250 * Reverts the effect of a previous call to
1251 * g_object_freeze_notify(). The freeze count is decreased on @object
1252 * and when it reaches zero, queued "notify" signals are emitted.
1254 * Duplicate notifications for each property are squashed so that at most one
1255 * #GObject::notify signal is emitted for each property.
1257 * It is an error to call this function when the freeze count is zero.
1260 g_object_thaw_notify (GObject *object)
1262 GObjectNotifyQueue *nqueue;
1264 g_return_if_fail (G_IS_OBJECT (object));
1265 if (g_atomic_int_get (&object->ref_count) == 0)
1268 g_object_ref (object);
1270 /* FIXME: Freezing is the only way to get at the notify queue.
1271 * So we freeze once and then thaw twice.
1273 nqueue = g_object_notify_queue_freeze (object, FALSE);
1274 g_object_notify_queue_thaw (object, nqueue);
1275 g_object_notify_queue_thaw (object, nqueue);
1277 g_object_unref (object);
1281 object_get_property (GObject *object,
1285 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1286 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1287 GParamSpec *redirect;
1291 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1292 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1296 redirect = g_param_spec_get_redirect_target (pspec);
1300 class->get_property (object, param_id, value, pspec);
1304 object_set_property (GObject *object,
1306 const GValue *value,
1307 GObjectNotifyQueue *nqueue)
1309 GValue tmp_value = G_VALUE_INIT;
1310 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1311 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1312 GParamSpec *redirect;
1313 static const gchar * enable_diagnostic = NULL;
1317 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1318 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1322 redirect = g_param_spec_get_redirect_target (pspec);
1326 if (G_UNLIKELY (!enable_diagnostic))
1328 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1329 if (!enable_diagnostic)
1330 enable_diagnostic = "0";
1333 if (enable_diagnostic[0] == '1')
1335 if (pspec->flags & G_PARAM_DEPRECATED)
1336 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1337 "anymore. It will be removed in a future version.",
1338 G_OBJECT_TYPE_NAME (object), pspec->name);
1341 /* provide a copy to work from, convert (if necessary) and validate */
1342 g_value_init (&tmp_value, pspec->value_type);
1343 if (!g_value_transform (value, &tmp_value))
1344 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1346 g_type_name (pspec->value_type),
1347 G_VALUE_TYPE_NAME (value));
1348 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1350 gchar *contents = g_strdup_value_contents (value);
1352 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1354 G_VALUE_TYPE_NAME (value),
1356 g_type_name (pspec->value_type));
1361 GParamSpec *notify_pspec;
1363 class->set_property (object, param_id, &tmp_value, pspec);
1365 notify_pspec = get_notify_pspec (pspec);
1367 if (notify_pspec != NULL)
1368 g_object_notify_queue_add (object, nqueue, notify_pspec);
1370 g_value_unset (&tmp_value);
1374 object_interface_check_properties (gpointer func_data,
1377 GTypeInterface *iface_class = g_iface;
1378 GObjectClass *class;
1379 GType iface_type = iface_class->g_type;
1380 GParamSpec **pspecs;
1383 class = g_type_class_ref (iface_class->g_instance_type);
1385 if (!G_IS_OBJECT_CLASS (class))
1388 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1392 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1394 G_OBJECT_CLASS_TYPE (class),
1399 g_critical ("Object class %s doesn't implement property "
1400 "'%s' from interface '%s'",
1401 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1403 g_type_name (iface_type));
1408 /* We do a number of checks on the properties of an interface to
1409 * make sure that all classes implementing the interface are
1410 * overriding the properties in a sane way.
1412 * We do the checks in order of importance so that we can give
1413 * more useful error messages first.
1415 * First, we check that the implementation doesn't remove the
1416 * basic functionality (readability, writability) advertised by
1417 * the interface. Next, we check that it doesn't introduce
1418 * additional restrictions (such as construct-only). Finally, we
1419 * make sure the types are compatible.
1422 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1423 /* If the property on the interface is readable then the
1424 * implementation must be readable. If the interface is writable
1425 * then the implementation must be writable.
1427 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1429 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1430 "property on interface '%s'\n", pspecs[n]->name,
1431 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1435 /* If the property on the interface is writable then we need to
1436 * make sure the implementation doesn't introduce new restrictions
1437 * on that writability (ie: construct-only).
1439 * If the interface was not writable to begin with then we don't
1440 * really have any problems here because "writable at construct
1441 * type only" is still more permissive than "read only".
1443 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1445 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1447 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1448 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1449 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1455 /* If the property on the interface is readable then we are
1456 * effectively advertising that reading the property will return a
1457 * value of a specific type. All implementations of the interface
1458 * need to return items of this type -- but may be more
1459 * restrictive. For example, it is legal to have:
1461 * GtkWidget *get_item();
1463 * that is implemented by a function that always returns a
1464 * GtkEntry. In short: readability implies that the
1465 * implementation value type must be equal or more restrictive.
1467 * Similarly, if the property on the interface is writable then
1468 * must be able to accept the property being set to any value of
1469 * that type, including subclasses. In this case, we may also be
1470 * less restrictive. For example, it is legal to have:
1472 * set_item (GtkEntry *);
1474 * that is implemented by a function that will actually work with
1475 * any GtkWidget. In short: writability implies that the
1476 * implementation value type must be equal or less restrictive.
1478 * In the case that the property is both readable and writable
1479 * then the only way that both of the above can be satisfied is
1480 * with a type that is exactly equal.
1482 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1484 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1485 /* class pspec value type must have exact equality with interface */
1486 if (pspecs[n]->value_type != class_pspec->value_type)
1487 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1488 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1489 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1490 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1493 case G_PARAM_READABLE:
1494 /* class pspec value type equal or more restrictive than interface */
1495 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1496 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1497 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1498 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1499 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1502 case G_PARAM_WRITABLE:
1503 /* class pspec value type equal or less restrictive than interface */
1504 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1505 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1506 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1507 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1508 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1512 g_assert_not_reached ();
1518 g_type_class_unref (class);
1522 g_object_get_type (void)
1524 return G_TYPE_OBJECT;
1528 * g_object_new: (skip)
1529 * @object_type: the type id of the #GObject subtype to instantiate
1530 * @first_property_name: the name of the first property
1531 * @...: the value of the first property, followed optionally by more
1532 * name/value pairs, followed by %NULL
1534 * Creates a new instance of a #GObject subtype and sets its properties.
1536 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1537 * which are not explicitly specified are set to their default values.
1539 * Returns: (transfer full): a new instance of @object_type
1542 g_object_new (GType object_type,
1543 const gchar *first_property_name,
1549 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1551 /* short circuit for calls supplying no properties */
1552 if (!first_property_name)
1553 return g_object_newv (object_type, 0, NULL);
1555 va_start (var_args, first_property_name);
1556 object = g_object_new_valist (object_type, first_property_name, var_args);
1563 slist_maybe_remove (GSList **slist,
1566 GSList *last = NULL, *node = *slist;
1569 if (node->data == data)
1572 last->next = node->next;
1574 *slist = node->next;
1575 g_slist_free_1 (node);
1584 static inline gboolean
1585 object_in_construction_list (GObject *object)
1587 gboolean in_construction;
1588 G_LOCK (construction_mutex);
1589 in_construction = g_slist_find (construction_objects, object) != NULL;
1590 G_UNLOCK (construction_mutex);
1591 return in_construction;
1596 * @object_type: the type id of the #GObject subtype to instantiate
1597 * @n_parameters: the length of the @parameters array
1598 * @parameters: (array length=n_parameters): an array of #GParameter
1600 * Creates a new instance of a #GObject subtype and sets its properties.
1602 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1603 * which are not explicitly specified are set to their default values.
1605 * Rename to: g_object_new
1606 * Returns: (type GObject.Object) (transfer full): a new instance of
1610 g_object_newv (GType object_type,
1612 GParameter *parameters)
1614 GObjectConstructParam *cparams = NULL, *oparams;
1615 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1617 GObjectClass *class, *unref_class = NULL;
1619 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1621 GList *clist = NULL;
1622 gboolean newly_constructed;
1625 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1627 class = g_type_class_peek_static (object_type);
1629 class = unref_class = g_type_class_ref (object_type);
1630 for (slist = class->construct_properties; slist; slist = slist->next)
1632 clist = g_list_prepend (clist, slist->data);
1633 n_total_cparams += 1;
1636 if (n_parameters == 0 && n_total_cparams == 0)
1638 /* This is a simple object with no construct properties, and
1639 * no properties are being set, so short circuit the parameter
1640 * handling. This speeds up simple object construction.
1643 object = class->constructor (object_type, 0, NULL);
1644 goto did_construction;
1647 /* collect parameters, sort into construction and normal ones */
1648 oparams = g_new (GObjectConstructParam, n_parameters);
1649 cparams = g_new (GObjectConstructParam, n_total_cparams);
1650 for (i = 0; i < n_parameters; i++)
1652 GValue *value = ¶meters[i].value;
1653 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1659 g_warning ("%s: object class `%s' has no property named `%s'",
1661 g_type_name (object_type),
1662 parameters[i].name);
1665 if (!(pspec->flags & G_PARAM_WRITABLE))
1667 g_warning ("%s: property `%s' of object class `%s' is not writable",
1670 g_type_name (object_type));
1673 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1675 GList *list = g_list_find (clist, pspec);
1679 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1680 G_STRFUNC, pspec->name, g_type_name (object_type));
1683 cparams[n_cparams].pspec = pspec;
1684 cparams[n_cparams].value = value;
1689 list->prev->next = list->next;
1691 list->next->prev = list->prev;
1692 g_list_free_1 (list);
1696 oparams[n_oparams].pspec = pspec;
1697 oparams[n_oparams].value = value;
1702 /* set remaining construction properties to default values */
1703 n_cvalues = n_total_cparams - n_cparams;
1704 cvalues = g_new (GValue, n_cvalues);
1707 GList *tmp = clist->next;
1708 GParamSpec *pspec = clist->data;
1709 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1712 g_value_init (value, pspec->value_type);
1713 g_param_value_set_default (pspec, value);
1715 cparams[n_cparams].pspec = pspec;
1716 cparams[n_cparams].value = value;
1719 g_list_free_1 (clist);
1723 /* construct object from construction parameters */
1724 object = class->constructor (object_type, n_total_cparams, cparams);
1725 /* free construction values */
1728 g_value_unset (cvalues + n_cvalues);
1732 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1734 /* adjust freeze_count according to g_object_init() and remaining properties */
1735 G_LOCK (construction_mutex);
1736 newly_constructed = slist_maybe_remove (&construction_objects, object);
1737 G_UNLOCK (construction_mutex);
1740 newly_constructed = TRUE;
1742 if (CLASS_HAS_PROPS (class))
1744 if (newly_constructed || n_oparams)
1745 nqueue = g_object_notify_queue_freeze (object, FALSE);
1746 if (newly_constructed)
1747 g_object_notify_queue_thaw (object, nqueue);
1750 /* run 'constructed' handler if there is a custom one */
1751 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1752 class->constructed (object);
1754 /* set remaining properties */
1755 for (i = 0; i < n_oparams; i++)
1756 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1759 if (CLASS_HAS_PROPS (class))
1761 /* release our own freeze count and handle notifications */
1762 if (newly_constructed || n_oparams)
1763 g_object_notify_queue_thaw (object, nqueue);
1767 g_type_class_unref (unref_class);
1773 * g_object_new_valist: (skip)
1774 * @object_type: the type id of the #GObject subtype to instantiate
1775 * @first_property_name: the name of the first property
1776 * @var_args: the value of the first property, followed optionally by more
1777 * name/value pairs, followed by %NULL
1779 * Creates a new instance of a #GObject subtype and sets its properties.
1781 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1782 * which are not explicitly specified are set to their default values.
1784 * Returns: a new instance of @object_type
1787 g_object_new_valist (GType object_type,
1788 const gchar *first_property_name,
1791 GObjectClass *class;
1795 guint n_params = 0, n_alloced_params = 16;
1797 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1799 if (!first_property_name)
1800 return g_object_newv (object_type, 0, NULL);
1802 class = g_type_class_ref (object_type);
1804 params = g_new0 (GParameter, n_alloced_params);
1805 name = first_property_name;
1808 gchar *error = NULL;
1809 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1815 g_warning ("%s: object class `%s' has no property named `%s'",
1817 g_type_name (object_type),
1821 if (n_params >= n_alloced_params)
1823 n_alloced_params += 16;
1824 params = g_renew (GParameter, params, n_alloced_params);
1825 memset (params + n_params, 0, 16 * (sizeof *params));
1827 params[n_params].name = name;
1828 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1829 var_args, 0, &error);
1832 g_warning ("%s: %s", G_STRFUNC, error);
1834 g_value_unset (¶ms[n_params].value);
1838 name = va_arg (var_args, gchar*);
1841 object = g_object_newv (object_type, n_params, params);
1844 g_value_unset (¶ms[n_params].value);
1847 g_type_class_unref (class);
1853 g_object_constructor (GType type,
1854 guint n_construct_properties,
1855 GObjectConstructParam *construct_params)
1860 object = (GObject*) g_type_create_instance (type);
1862 /* set construction parameters */
1863 if (n_construct_properties)
1865 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
1867 /* set construct properties */
1868 while (n_construct_properties--)
1870 GValue *value = construct_params->value;
1871 GParamSpec *pspec = construct_params->pspec;
1874 object_set_property (object, pspec, value, nqueue);
1876 g_object_notify_queue_thaw (object, nqueue);
1877 /* the notification queue is still frozen from g_object_init(), so
1878 * we don't need to handle it here, g_object_newv() takes
1887 g_object_constructed (GObject *object)
1889 /* empty default impl to allow unconditional upchaining */
1893 * g_object_set_valist: (skip)
1894 * @object: a #GObject
1895 * @first_property_name: name of the first property to set
1896 * @var_args: value for the first property, followed optionally by more
1897 * name/value pairs, followed by %NULL
1899 * Sets properties on an object.
1902 g_object_set_valist (GObject *object,
1903 const gchar *first_property_name,
1906 GObjectNotifyQueue *nqueue;
1909 g_return_if_fail (G_IS_OBJECT (object));
1911 g_object_ref (object);
1912 nqueue = g_object_notify_queue_freeze (object, FALSE);
1914 name = first_property_name;
1917 GValue value = G_VALUE_INIT;
1919 gchar *error = NULL;
1921 pspec = g_param_spec_pool_lookup (pspec_pool,
1923 G_OBJECT_TYPE (object),
1927 g_warning ("%s: object class `%s' has no property named `%s'",
1929 G_OBJECT_TYPE_NAME (object),
1933 if (!(pspec->flags & G_PARAM_WRITABLE))
1935 g_warning ("%s: property `%s' of object class `%s' is not writable",
1938 G_OBJECT_TYPE_NAME (object));
1941 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1943 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1944 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1948 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1952 g_warning ("%s: %s", G_STRFUNC, error);
1954 g_value_unset (&value);
1958 object_set_property (object, pspec, &value, nqueue);
1959 g_value_unset (&value);
1961 name = va_arg (var_args, gchar*);
1964 g_object_notify_queue_thaw (object, nqueue);
1965 g_object_unref (object);
1969 * g_object_get_valist: (skip)
1970 * @object: a #GObject
1971 * @first_property_name: name of the first property to get
1972 * @var_args: return location for the first property, followed optionally by more
1973 * name/return location pairs, followed by %NULL
1975 * Gets properties of an object.
1977 * In general, a copy is made of the property contents and the caller
1978 * is responsible for freeing the memory in the appropriate manner for
1979 * the type, for instance by calling g_free() or g_object_unref().
1981 * See g_object_get().
1984 g_object_get_valist (GObject *object,
1985 const gchar *first_property_name,
1990 g_return_if_fail (G_IS_OBJECT (object));
1992 g_object_ref (object);
1994 name = first_property_name;
1998 GValue value = G_VALUE_INIT;
2002 pspec = g_param_spec_pool_lookup (pspec_pool,
2004 G_OBJECT_TYPE (object),
2008 g_warning ("%s: object class `%s' has no property named `%s'",
2010 G_OBJECT_TYPE_NAME (object),
2014 if (!(pspec->flags & G_PARAM_READABLE))
2016 g_warning ("%s: property `%s' of object class `%s' is not readable",
2019 G_OBJECT_TYPE_NAME (object));
2023 g_value_init (&value, pspec->value_type);
2025 object_get_property (object, pspec, &value);
2027 G_VALUE_LCOPY (&value, var_args, 0, &error);
2030 g_warning ("%s: %s", G_STRFUNC, error);
2032 g_value_unset (&value);
2036 g_value_unset (&value);
2038 name = va_arg (var_args, gchar*);
2041 g_object_unref (object);
2045 * g_object_set: (skip)
2046 * @object: a #GObject
2047 * @first_property_name: name of the first property to set
2048 * @...: value for the first property, followed optionally by more
2049 * name/value pairs, followed by %NULL
2051 * Sets properties on an object.
2054 g_object_set (gpointer _object,
2055 const gchar *first_property_name,
2058 GObject *object = _object;
2061 g_return_if_fail (G_IS_OBJECT (object));
2063 va_start (var_args, first_property_name);
2064 g_object_set_valist (object, first_property_name, var_args);
2069 * g_object_get: (skip)
2070 * @object: a #GObject
2071 * @first_property_name: name of the first property to get
2072 * @...: return location for the first property, followed optionally by more
2073 * name/return location pairs, followed by %NULL
2075 * Gets properties of an object.
2077 * In general, a copy is made of the property contents and the caller
2078 * is responsible for freeing the memory in the appropriate manner for
2079 * the type, for instance by calling g_free() or g_object_unref().
2082 * <title>Using g_object_get(<!-- -->)</title>
2083 * An example of using g_object_get() to get the contents
2084 * of three properties - one of type #G_TYPE_INT,
2085 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
2091 * g_object_get (my_object,
2092 * "int-property", &intval,
2093 * "str-property", &strval,
2094 * "obj-property", &objval,
2097 * // Do something with intval, strval, objval
2100 * g_object_unref (objval);
2105 g_object_get (gpointer _object,
2106 const gchar *first_property_name,
2109 GObject *object = _object;
2112 g_return_if_fail (G_IS_OBJECT (object));
2114 va_start (var_args, first_property_name);
2115 g_object_get_valist (object, first_property_name, var_args);
2120 * g_object_set_property:
2121 * @object: a #GObject
2122 * @property_name: the name of the property to set
2125 * Sets a property on an object.
2128 g_object_set_property (GObject *object,
2129 const gchar *property_name,
2130 const GValue *value)
2132 GObjectNotifyQueue *nqueue;
2135 g_return_if_fail (G_IS_OBJECT (object));
2136 g_return_if_fail (property_name != NULL);
2137 g_return_if_fail (G_IS_VALUE (value));
2139 g_object_ref (object);
2140 nqueue = g_object_notify_queue_freeze (object, FALSE);
2142 pspec = g_param_spec_pool_lookup (pspec_pool,
2144 G_OBJECT_TYPE (object),
2147 g_warning ("%s: object class `%s' has no property named `%s'",
2149 G_OBJECT_TYPE_NAME (object),
2151 else if (!(pspec->flags & G_PARAM_WRITABLE))
2152 g_warning ("%s: property `%s' of object class `%s' is not writable",
2155 G_OBJECT_TYPE_NAME (object));
2156 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2157 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2158 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2160 object_set_property (object, pspec, value, nqueue);
2162 g_object_notify_queue_thaw (object, nqueue);
2163 g_object_unref (object);
2167 * g_object_get_property:
2168 * @object: a #GObject
2169 * @property_name: the name of the property to get
2170 * @value: return location for the property value
2172 * Gets a property of an object. @value must have been initialized to the
2173 * expected type of the property (or a type to which the expected type can be
2174 * transformed) using g_value_init().
2176 * In general, a copy is made of the property contents and the caller is
2177 * responsible for freeing the memory by calling g_value_unset().
2179 * Note that g_object_get_property() is really intended for language
2180 * bindings, g_object_get() is much more convenient for C programming.
2183 g_object_get_property (GObject *object,
2184 const gchar *property_name,
2189 g_return_if_fail (G_IS_OBJECT (object));
2190 g_return_if_fail (property_name != NULL);
2191 g_return_if_fail (G_IS_VALUE (value));
2193 g_object_ref (object);
2195 pspec = g_param_spec_pool_lookup (pspec_pool,
2197 G_OBJECT_TYPE (object),
2200 g_warning ("%s: object class `%s' has no property named `%s'",
2202 G_OBJECT_TYPE_NAME (object),
2204 else if (!(pspec->flags & G_PARAM_READABLE))
2205 g_warning ("%s: property `%s' of object class `%s' is not readable",
2208 G_OBJECT_TYPE_NAME (object));
2211 GValue *prop_value, tmp_value = G_VALUE_INIT;
2213 /* auto-conversion of the callers value type
2215 if (G_VALUE_TYPE (value) == pspec->value_type)
2217 g_value_reset (value);
2220 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2222 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2223 G_STRFUNC, pspec->name,
2224 g_type_name (pspec->value_type),
2225 G_VALUE_TYPE_NAME (value));
2226 g_object_unref (object);
2231 g_value_init (&tmp_value, pspec->value_type);
2232 prop_value = &tmp_value;
2234 object_get_property (object, pspec, prop_value);
2235 if (prop_value != value)
2237 g_value_transform (prop_value, value);
2238 g_value_unset (&tmp_value);
2242 g_object_unref (object);
2246 * g_object_connect: (skip)
2247 * @object: a #GObject
2248 * @signal_spec: the spec for the first signal
2249 * @...: #GCallback for the first signal, followed by data for the
2250 * first signal, followed optionally by more signal
2251 * spec/callback/data triples, followed by %NULL
2253 * A convenience function to connect multiple signals at once.
2255 * The signal specs expected by this function have the form
2256 * "modifier::signal_name", where modifier can be one of the following:
2259 * <term>signal</term>
2261 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2262 * </para></listitem>
2265 * <term>object_signal</term>
2266 * <term>object-signal</term>
2268 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2269 * </para></listitem>
2272 * <term>swapped_signal</term>
2273 * <term>swapped-signal</term>
2275 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2276 * </para></listitem>
2279 * <term>swapped_object_signal</term>
2280 * <term>swapped-object-signal</term>
2282 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2283 * </para></listitem>
2286 * <term>signal_after</term>
2287 * <term>signal-after</term>
2289 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2290 * </para></listitem>
2293 * <term>object_signal_after</term>
2294 * <term>object-signal-after</term>
2296 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2297 * </para></listitem>
2300 * <term>swapped_signal_after</term>
2301 * <term>swapped-signal-after</term>
2303 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2304 * </para></listitem>
2307 * <term>swapped_object_signal_after</term>
2308 * <term>swapped-object-signal-after</term>
2310 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2311 * </para></listitem>
2316 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2317 * "type", GTK_WINDOW_POPUP,
2320 * "signal::event", gtk_menu_window_event, menu,
2321 * "signal::size_request", gtk_menu_window_size_request, menu,
2322 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2326 * Returns: (transfer none): @object
2329 g_object_connect (gpointer _object,
2330 const gchar *signal_spec,
2333 GObject *object = _object;
2336 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2337 g_return_val_if_fail (object->ref_count > 0, object);
2339 va_start (var_args, signal_spec);
2342 GCallback callback = va_arg (var_args, GCallback);
2343 gpointer data = va_arg (var_args, gpointer);
2345 if (strncmp (signal_spec, "signal::", 8) == 0)
2346 g_signal_connect_data (object, signal_spec + 8,
2347 callback, data, NULL,
2349 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2350 strncmp (signal_spec, "object-signal::", 15) == 0)
2351 g_signal_connect_object (object, signal_spec + 15,
2354 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2355 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2356 g_signal_connect_data (object, signal_spec + 16,
2357 callback, data, NULL,
2359 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2360 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2361 g_signal_connect_object (object, signal_spec + 23,
2364 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2365 strncmp (signal_spec, "signal-after::", 14) == 0)
2366 g_signal_connect_data (object, signal_spec + 14,
2367 callback, data, NULL,
2369 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2370 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2371 g_signal_connect_object (object, signal_spec + 21,
2374 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2375 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2376 g_signal_connect_data (object, signal_spec + 22,
2377 callback, data, NULL,
2378 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2379 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2380 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2381 g_signal_connect_object (object, signal_spec + 29,
2383 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2386 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2389 signal_spec = va_arg (var_args, gchar*);
2397 * g_object_disconnect: (skip)
2398 * @object: a #GObject
2399 * @signal_spec: the spec for the first signal
2400 * @...: #GCallback for the first signal, followed by data for the first signal,
2401 * followed optionally by more signal spec/callback/data triples,
2404 * A convenience function to disconnect multiple signals at once.
2406 * The signal specs expected by this function have the form
2407 * "any_signal", which means to disconnect any signal with matching
2408 * callback and data, or "any_signal::signal_name", which only
2409 * disconnects the signal named "signal_name".
2412 g_object_disconnect (gpointer _object,
2413 const gchar *signal_spec,
2416 GObject *object = _object;
2419 g_return_if_fail (G_IS_OBJECT (object));
2420 g_return_if_fail (object->ref_count > 0);
2422 va_start (var_args, signal_spec);
2425 GCallback callback = va_arg (var_args, GCallback);
2426 gpointer data = va_arg (var_args, gpointer);
2427 guint sid = 0, detail = 0, mask = 0;
2429 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2430 strncmp (signal_spec, "any-signal::", 12) == 0)
2433 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2435 else if (strcmp (signal_spec, "any_signal") == 0 ||
2436 strcmp (signal_spec, "any-signal") == 0)
2439 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2443 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2447 if ((mask & G_SIGNAL_MATCH_ID) &&
2448 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2449 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2450 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2452 NULL, (gpointer)callback, data))
2453 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2454 signal_spec = va_arg (var_args, gchar*);
2465 } weak_refs[1]; /* flexible array */
2469 weak_refs_notify (gpointer data)
2471 WeakRefStack *wstack = data;
2474 for (i = 0; i < wstack->n_weak_refs; i++)
2475 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2480 * g_object_weak_ref: (skip)
2481 * @object: #GObject to reference weakly
2482 * @notify: callback to invoke before the object is freed
2483 * @data: extra data to pass to notify
2485 * Adds a weak reference callback to an object. Weak references are
2486 * used for notification when an object is finalized. They are called
2487 * "weak references" because they allow you to safely hold a pointer
2488 * to an object without calling g_object_ref() (g_object_ref() adds a
2489 * strong reference, that is, forces the object to stay alive).
2491 * Note that the weak references created by this method are not
2492 * thread-safe: they cannot safely be used in one thread if the
2493 * object's last g_object_unref() might happen in another thread.
2494 * Use #GWeakRef if thread-safety is required.
2497 g_object_weak_ref (GObject *object,
2501 WeakRefStack *wstack;
2504 g_return_if_fail (G_IS_OBJECT (object));
2505 g_return_if_fail (notify != NULL);
2506 g_return_if_fail (object->ref_count >= 1);
2508 G_LOCK (weak_refs_mutex);
2509 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2512 i = wstack->n_weak_refs++;
2513 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2517 wstack = g_renew (WeakRefStack, NULL, 1);
2518 wstack->object = object;
2519 wstack->n_weak_refs = 1;
2522 wstack->weak_refs[i].notify = notify;
2523 wstack->weak_refs[i].data = data;
2524 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2525 G_UNLOCK (weak_refs_mutex);
2529 * g_object_weak_unref: (skip)
2530 * @object: #GObject to remove a weak reference from
2531 * @notify: callback to search for
2532 * @data: data to search for
2534 * Removes a weak reference callback to an object.
2537 g_object_weak_unref (GObject *object,
2541 WeakRefStack *wstack;
2542 gboolean found_one = FALSE;
2544 g_return_if_fail (G_IS_OBJECT (object));
2545 g_return_if_fail (notify != NULL);
2547 G_LOCK (weak_refs_mutex);
2548 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2553 for (i = 0; i < wstack->n_weak_refs; i++)
2554 if (wstack->weak_refs[i].notify == notify &&
2555 wstack->weak_refs[i].data == data)
2558 wstack->n_weak_refs -= 1;
2559 if (i != wstack->n_weak_refs)
2560 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2565 G_UNLOCK (weak_refs_mutex);
2567 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2571 * g_object_add_weak_pointer: (skip)
2572 * @object: The object that should be weak referenced.
2573 * @weak_pointer_location: (inout): The memory address of a pointer.
2575 * Adds a weak reference from weak_pointer to @object to indicate that
2576 * the pointer located at @weak_pointer_location is only valid during
2577 * the lifetime of @object. When the @object is finalized,
2578 * @weak_pointer will be set to %NULL.
2580 * Note that as with g_object_weak_ref(), the weak references created by
2581 * this method are not thread-safe: they cannot safely be used in one
2582 * thread if the object's last g_object_unref() might happen in another
2583 * thread. Use #GWeakRef if thread-safety is required.
2586 g_object_add_weak_pointer (GObject *object,
2587 gpointer *weak_pointer_location)
2589 g_return_if_fail (G_IS_OBJECT (object));
2590 g_return_if_fail (weak_pointer_location != NULL);
2592 g_object_weak_ref (object,
2593 (GWeakNotify) g_nullify_pointer,
2594 weak_pointer_location);
2598 * g_object_remove_weak_pointer: (skip)
2599 * @object: The object that is weak referenced.
2600 * @weak_pointer_location: (inout): The memory address of a pointer.
2602 * Removes a weak reference from @object that was previously added
2603 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2604 * to match the one used with g_object_add_weak_pointer().
2607 g_object_remove_weak_pointer (GObject *object,
2608 gpointer *weak_pointer_location)
2610 g_return_if_fail (G_IS_OBJECT (object));
2611 g_return_if_fail (weak_pointer_location != NULL);
2613 g_object_weak_unref (object,
2614 (GWeakNotify) g_nullify_pointer,
2615 weak_pointer_location);
2619 object_floating_flag_handler (GObject *object,
2625 case +1: /* force floating if possible */
2627 oldvalue = g_atomic_pointer_get (&object->qdata);
2628 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2629 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2630 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2631 case -1: /* sink if possible */
2633 oldvalue = g_atomic_pointer_get (&object->qdata);
2634 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2635 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2636 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2637 default: /* check floating */
2638 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2643 * g_object_is_floating:
2644 * @object: (type GObject.Object): a #GObject
2646 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2651 * Returns: %TRUE if @object has a floating reference
2654 g_object_is_floating (gpointer _object)
2656 GObject *object = _object;
2657 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2658 return floating_flag_handler (object, 0);
2662 * g_object_ref_sink:
2663 * @object: (type GObject.Object): a #GObject
2665 * Increase the reference count of @object, and possibly remove the
2666 * <link linkend="floating-ref">floating</link> reference, if @object
2667 * has a floating reference.
2669 * In other words, if the object is floating, then this call "assumes
2670 * ownership" of the floating reference, converting it to a normal
2671 * reference by clearing the floating flag while leaving the reference
2672 * count unchanged. If the object is not floating, then this call
2673 * adds a new normal reference increasing the reference count by one.
2677 * Returns: (type GObject.Object) (transfer none): @object
2680 g_object_ref_sink (gpointer _object)
2682 GObject *object = _object;
2683 gboolean was_floating;
2684 g_return_val_if_fail (G_IS_OBJECT (object), object);
2685 g_return_val_if_fail (object->ref_count >= 1, object);
2686 g_object_ref (object);
2687 was_floating = floating_flag_handler (object, -1);
2689 g_object_unref (object);
2694 * g_object_force_floating:
2695 * @object: a #GObject
2697 * This function is intended for #GObject implementations to re-enforce a
2698 * <link linkend="floating-ref">floating</link> object reference.
2699 * Doing this is seldom required: all
2700 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2701 * usually just needs to be sunken by calling g_object_ref_sink().
2706 g_object_force_floating (GObject *object)
2708 g_return_if_fail (G_IS_OBJECT (object));
2709 g_return_if_fail (object->ref_count >= 1);
2711 floating_flag_handler (object, +1);
2716 guint n_toggle_refs;
2718 GToggleNotify notify;
2720 } toggle_refs[1]; /* flexible array */
2724 toggle_refs_notify (GObject *object,
2725 gboolean is_last_ref)
2727 ToggleRefStack tstack, *tstackptr;
2729 G_LOCK (toggle_refs_mutex);
2730 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2731 tstack = *tstackptr;
2732 G_UNLOCK (toggle_refs_mutex);
2734 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2735 * will only be notified when there is exactly one of them.
2737 g_assert (tstack.n_toggle_refs == 1);
2738 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2742 * g_object_add_toggle_ref: (skip)
2743 * @object: a #GObject
2744 * @notify: a function to call when this reference is the
2745 * last reference to the object, or is no longer
2746 * the last reference.
2747 * @data: data to pass to @notify
2749 * Increases the reference count of the object by one and sets a
2750 * callback to be called when all other references to the object are
2751 * dropped, or when this is already the last reference to the object
2752 * and another reference is established.
2754 * This functionality is intended for binding @object to a proxy
2755 * object managed by another memory manager. This is done with two
2756 * paired references: the strong reference added by
2757 * g_object_add_toggle_ref() and a reverse reference to the proxy
2758 * object which is either a strong reference or weak reference.
2760 * The setup is that when there are no other references to @object,
2761 * only a weak reference is held in the reverse direction from @object
2762 * to the proxy object, but when there are other references held to
2763 * @object, a strong reference is held. The @notify callback is called
2764 * when the reference from @object to the proxy object should be
2765 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2766 * true) or weak to strong (@is_last_ref false).
2768 * Since a (normal) reference must be held to the object before
2769 * calling g_object_add_toggle_ref(), the initial state of the reverse
2770 * link is always strong.
2772 * Multiple toggle references may be added to the same gobject,
2773 * however if there are multiple toggle references to an object, none
2774 * of them will ever be notified until all but one are removed. For
2775 * this reason, you should only ever use a toggle reference if there
2776 * is important state in the proxy object.
2781 g_object_add_toggle_ref (GObject *object,
2782 GToggleNotify notify,
2785 ToggleRefStack *tstack;
2788 g_return_if_fail (G_IS_OBJECT (object));
2789 g_return_if_fail (notify != NULL);
2790 g_return_if_fail (object->ref_count >= 1);
2792 g_object_ref (object);
2794 G_LOCK (toggle_refs_mutex);
2795 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2798 i = tstack->n_toggle_refs++;
2799 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2800 * in tstate->toggle_refs */
2801 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2805 tstack = g_renew (ToggleRefStack, NULL, 1);
2806 tstack->object = object;
2807 tstack->n_toggle_refs = 1;
2811 /* Set a flag for fast lookup after adding the first toggle reference */
2812 if (tstack->n_toggle_refs == 1)
2813 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2815 tstack->toggle_refs[i].notify = notify;
2816 tstack->toggle_refs[i].data = data;
2817 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2818 (GDestroyNotify)g_free);
2819 G_UNLOCK (toggle_refs_mutex);
2823 * g_object_remove_toggle_ref: (skip)
2824 * @object: a #GObject
2825 * @notify: a function to call when this reference is the
2826 * last reference to the object, or is no longer
2827 * the last reference.
2828 * @data: data to pass to @notify
2830 * Removes a reference added with g_object_add_toggle_ref(). The
2831 * reference count of the object is decreased by one.
2836 g_object_remove_toggle_ref (GObject *object,
2837 GToggleNotify notify,
2840 ToggleRefStack *tstack;
2841 gboolean found_one = FALSE;
2843 g_return_if_fail (G_IS_OBJECT (object));
2844 g_return_if_fail (notify != NULL);
2846 G_LOCK (toggle_refs_mutex);
2847 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2852 for (i = 0; i < tstack->n_toggle_refs; i++)
2853 if (tstack->toggle_refs[i].notify == notify &&
2854 tstack->toggle_refs[i].data == data)
2857 tstack->n_toggle_refs -= 1;
2858 if (i != tstack->n_toggle_refs)
2859 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2861 if (tstack->n_toggle_refs == 0)
2862 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2867 G_UNLOCK (toggle_refs_mutex);
2870 g_object_unref (object);
2872 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2877 * @object: (type GObject.Object): a #GObject
2879 * Increases the reference count of @object.
2881 * Returns: (type GObject.Object) (transfer none): the same @object
2884 g_object_ref (gpointer _object)
2886 GObject *object = _object;
2889 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2890 g_return_val_if_fail (object->ref_count > 0, NULL);
2892 #ifdef G_ENABLE_DEBUG
2893 if (g_trap_object_ref == object)
2895 #endif /* G_ENABLE_DEBUG */
2898 old_val = g_atomic_int_add (&object->ref_count, 1);
2900 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2901 toggle_refs_notify (object, FALSE);
2903 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2910 * @object: (type GObject.Object): a #GObject
2912 * Decreases the reference count of @object. When its reference count
2913 * drops to 0, the object is finalized (i.e. its memory is freed).
2916 g_object_unref (gpointer _object)
2918 GObject *object = _object;
2921 g_return_if_fail (G_IS_OBJECT (object));
2922 g_return_if_fail (object->ref_count > 0);
2924 #ifdef G_ENABLE_DEBUG
2925 if (g_trap_object_ref == object)
2927 #endif /* G_ENABLE_DEBUG */
2929 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2930 retry_atomic_decrement1:
2931 old_ref = g_atomic_int_get (&object->ref_count);
2934 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2935 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2937 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2938 goto retry_atomic_decrement1;
2940 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2942 /* if we went from 2->1 we need to notify toggle refs if any */
2943 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2944 toggle_refs_notify (object, TRUE);
2948 GSList **weak_locations;
2950 /* The only way that this object can live at this point is if
2951 * there are outstanding weak references already established
2952 * before we got here.
2954 * If there were not already weak references then no more can be
2955 * established at this time, because the other thread would have
2956 * to hold a strong ref in order to call
2957 * g_object_add_weak_pointer() and then we wouldn't be here.
2959 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
2961 if (weak_locations != NULL)
2963 g_rw_lock_writer_lock (&weak_locations_lock);
2965 /* It is possible that one of the weak references beat us to
2966 * the lock. Make sure the refcount is still what we expected
2969 old_ref = g_atomic_int_get (&object->ref_count);
2972 g_rw_lock_writer_unlock (&weak_locations_lock);
2973 goto retry_atomic_decrement1;
2976 /* We got the lock first, so the object will definitely die
2977 * now. Clear out all the weak references.
2979 while (*weak_locations)
2981 GWeakRef *weak_ref_location = (*weak_locations)->data;
2983 weak_ref_location->priv.p = NULL;
2984 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
2987 g_rw_lock_writer_unlock (&weak_locations_lock);
2990 /* we are about to remove the last reference */
2991 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2992 G_OBJECT_GET_CLASS (object)->dispose (object);
2993 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2995 /* may have been re-referenced meanwhile */
2996 retry_atomic_decrement2:
2997 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3000 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3001 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3003 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3004 goto retry_atomic_decrement2;
3006 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3008 /* if we went from 2->1 we need to notify toggle refs if any */
3009 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3010 toggle_refs_notify (object, TRUE);
3015 /* we are still in the process of taking away the last ref */
3016 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3017 g_signal_handlers_destroy (object);
3018 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3020 /* decrement the last reference */
3021 old_ref = g_atomic_int_add (&object->ref_count, -1);
3023 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3025 /* may have been re-referenced meanwhile */
3026 if (G_LIKELY (old_ref == 1))
3028 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3029 G_OBJECT_GET_CLASS (object)->finalize (object);
3031 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3033 #ifdef G_ENABLE_DEBUG
3036 /* catch objects not chaining finalize handlers */
3037 G_LOCK (debug_objects);
3038 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3039 G_UNLOCK (debug_objects);
3041 #endif /* G_ENABLE_DEBUG */
3042 g_type_free_instance ((GTypeInstance*) object);
3048 * g_clear_object: (skip)
3049 * @object_ptr: a pointer to a #GObject reference
3051 * Clears a reference to a #GObject.
3053 * @object_ptr must not be %NULL.
3055 * If the reference is %NULL then this function does nothing.
3056 * Otherwise, the reference count of the object is decreased and the
3057 * pointer is set to %NULL.
3059 * This function is threadsafe and modifies the pointer atomically,
3060 * using memory barriers where needed.
3062 * A macro is also included that allows this function to be used without
3067 #undef g_clear_object
3069 g_clear_object (volatile GObject **object_ptr)
3071 g_clear_pointer (object_ptr, g_object_unref);
3075 * g_object_get_qdata:
3076 * @object: The GObject to get a stored user data pointer from
3077 * @quark: A #GQuark, naming the user data pointer
3079 * This function gets back user data pointers stored via
3080 * g_object_set_qdata().
3082 * Returns: (transfer none): The user data pointer set, or %NULL
3085 g_object_get_qdata (GObject *object,
3088 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3090 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3094 * g_object_set_qdata: (skip)
3095 * @object: The GObject to set store a user data pointer
3096 * @quark: A #GQuark, naming the user data pointer
3097 * @data: An opaque user data pointer
3099 * This sets an opaque, named pointer on an object.
3100 * The name is specified through a #GQuark (retrived e.g. via
3101 * g_quark_from_static_string()), and the pointer
3102 * can be gotten back from the @object with g_object_get_qdata()
3103 * until the @object is finalized.
3104 * Setting a previously set user data pointer, overrides (frees)
3105 * the old pointer set, using #NULL as pointer essentially
3106 * removes the data stored.
3109 g_object_set_qdata (GObject *object,
3113 g_return_if_fail (G_IS_OBJECT (object));
3114 g_return_if_fail (quark > 0);
3116 g_datalist_id_set_data (&object->qdata, quark, data);
3120 * g_object_dup_qdata:
3121 * @object: the #GObject to store user data on
3122 * @quark: a #GQuark, naming the user data pointer
3123 * @dup_func: (allow-none): function to dup the value
3124 * @user_data: (allow-none): passed as user_data to @dup_func
3126 * This is a variant of g_object_get_qdata() which returns
3127 * a 'duplicate' of the value. @dup_func defines the
3128 * meaning of 'duplicate' in this context, it could e.g.
3129 * take a reference on a ref-counted object.
3131 * If the @quark is not set on the object then @dup_func
3132 * will be called with a %NULL argument.
3134 * Note that @dup_func is called while user data of @object
3137 * This function can be useful to avoid races when multiple
3138 * threads are using object data on the same key on the same
3141 * Returns: the result of calling @dup_func on the value
3142 * associated with @quark on @object, or %NULL if not set.
3143 * If @dup_func is %NULL, the value is returned
3149 g_object_dup_qdata (GObject *object,
3151 GDuplicateFunc dup_func,
3154 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3155 g_return_val_if_fail (quark > 0, NULL);
3157 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3161 * g_object_replace_qdata:
3162 * @object: the #GObject to store user data on
3163 * @quark: a #GQuark, naming the user data pointer
3164 * @oldval: (allow-none): the old value to compare against
3165 * @newval: (allow-none): the new value
3166 * @destroy: (allow-none): a destroy notify for the new value
3167 * @old_destroy: (allow-none): destroy notify for the existing value
3169 * Compares the user data for the key @quark on @object with
3170 * @oldval, and if they are the same, replaces @oldval with
3173 * This is like a typical atomic compare-and-exchange
3174 * operation, for user data on an object.
3176 * If the previous value was replaced then ownership of the
3177 * old value (@oldval) is passed to the caller, including
3178 * the registred destroy notify for it (passed out in @old_destroy).
3179 * Its up to the caller to free this as he wishes, which may
3180 * or may not include using @old_destroy as sometimes replacement
3181 * should not destroy the object in the normal way.
3183 * Return: %TRUE if the existing value for @quark was replaced
3184 * by @newval, %FALSE otherwise.
3189 g_object_replace_qdata (GObject *object,
3193 GDestroyNotify destroy,
3194 GDestroyNotify *old_destroy)
3196 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3197 g_return_val_if_fail (quark > 0, FALSE);
3199 return g_datalist_id_replace_data (&object->qdata, quark,
3200 oldval, newval, destroy,
3205 * g_object_set_qdata_full: (skip)
3206 * @object: The GObject to set store a user data pointer
3207 * @quark: A #GQuark, naming the user data pointer
3208 * @data: An opaque user data pointer
3209 * @destroy: Function to invoke with @data as argument, when @data
3212 * This function works like g_object_set_qdata(), but in addition,
3213 * a void (*destroy) (gpointer) function may be specified which is
3214 * called with @data as argument when the @object is finalized, or
3215 * the data is being overwritten by a call to g_object_set_qdata()
3216 * with the same @quark.
3219 g_object_set_qdata_full (GObject *object,
3222 GDestroyNotify destroy)
3224 g_return_if_fail (G_IS_OBJECT (object));
3225 g_return_if_fail (quark > 0);
3227 g_datalist_id_set_data_full (&object->qdata, quark, data,
3228 data ? destroy : (GDestroyNotify) NULL);
3232 * g_object_steal_qdata:
3233 * @object: The GObject to get a stored user data pointer from
3234 * @quark: A #GQuark, naming the user data pointer
3236 * This function gets back user data pointers stored via
3237 * g_object_set_qdata() and removes the @data from object
3238 * without invoking its destroy() function (if any was
3240 * Usually, calling this function is only required to update
3241 * user data pointers with a destroy notifier, for example:
3244 * object_add_to_user_list (GObject *object,
3245 * const gchar *new_string)
3247 * // the quark, naming the object data
3248 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3249 * // retrive the old string list
3250 * GList *list = g_object_steal_qdata (object, quark_string_list);
3252 * // prepend new string
3253 * list = g_list_prepend (list, g_strdup (new_string));
3254 * // this changed 'list', so we need to set it again
3255 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3258 * free_string_list (gpointer data)
3260 * GList *node, *list = data;
3262 * for (node = list; node; node = node->next)
3263 * g_free (node->data);
3264 * g_list_free (list);
3267 * Using g_object_get_qdata() in the above example, instead of
3268 * g_object_steal_qdata() would have left the destroy function set,
3269 * and thus the partial string list would have been freed upon
3270 * g_object_set_qdata_full().
3272 * Returns: (transfer full): The user data pointer set, or %NULL
3275 g_object_steal_qdata (GObject *object,
3278 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3279 g_return_val_if_fail (quark > 0, NULL);
3281 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3285 * g_object_get_data:
3286 * @object: #GObject containing the associations
3287 * @key: name of the key for that association
3289 * Gets a named field from the objects table of associations (see g_object_set_data()).
3291 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3294 g_object_get_data (GObject *object,
3297 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3298 g_return_val_if_fail (key != NULL, NULL);
3300 return g_datalist_get_data (&object->qdata, key);
3304 * g_object_set_data:
3305 * @object: #GObject containing the associations.
3306 * @key: name of the key
3307 * @data: data to associate with that key
3309 * Each object carries around a table of associations from
3310 * strings to pointers. This function lets you set an association.
3312 * If the object already had an association with that name,
3313 * the old association will be destroyed.
3316 g_object_set_data (GObject *object,
3320 g_return_if_fail (G_IS_OBJECT (object));
3321 g_return_if_fail (key != NULL);
3323 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3327 * g_object_dup_data:
3328 * @object: the #GObject to store user data on
3329 * @key: a string, naming the user data pointer
3330 * @dup_func: (allow-none): function to dup the value
3331 * @user_data: (allow-none): passed as user_data to @dup_func
3333 * This is a variant of g_object_get_data() which returns
3334 * a 'duplicate' of the value. @dup_func defines the
3335 * meaning of 'duplicate' in this context, it could e.g.
3336 * take a reference on a ref-counted object.
3338 * If the @key is not set on the object then @dup_func
3339 * will be called with a %NULL argument.
3341 * Note that @dup_func is called while user data of @object
3344 * This function can be useful to avoid races when multiple
3345 * threads are using object data on the same key on the same
3348 * Returns: the result of calling @dup_func on the value
3349 * associated with @key on @object, or %NULL if not set.
3350 * If @dup_func is %NULL, the value is returned
3356 g_object_dup_data (GObject *object,
3358 GDuplicateFunc dup_func,
3361 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3362 g_return_val_if_fail (key != NULL, NULL);
3364 return g_datalist_id_dup_data (&object->qdata,
3365 g_quark_from_string (key),
3366 dup_func, user_data);
3370 * g_object_replace_data:
3371 * @object: the #GObject to store user data on
3372 * @key: a string, naming the user data pointer
3373 * @oldval: (allow-none): the old value to compare against
3374 * @newval: (allow-none): the new value
3375 * @destroy: (allow-none): a destroy notify for the new value
3376 * @old_destroy: (allow-none): destroy notify for the existing value
3378 * Compares the user data for the key @key on @object with
3379 * @oldval, and if they are the same, replaces @oldval with
3382 * This is like a typical atomic compare-and-exchange
3383 * operation, for user data on an object.
3385 * If the previous value was replaced then ownership of the
3386 * old value (@oldval) is passed to the caller, including
3387 * the registred destroy notify for it (passed out in @old_destroy).
3388 * Its up to the caller to free this as he wishes, which may
3389 * or may not include using @old_destroy as sometimes replacement
3390 * should not destroy the object in the normal way.
3392 * Return: %TRUE if the existing value for @key was replaced
3393 * by @newval, %FALSE otherwise.
3398 g_object_replace_data (GObject *object,
3402 GDestroyNotify destroy,
3403 GDestroyNotify *old_destroy)
3405 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3406 g_return_val_if_fail (key != NULL, FALSE);
3408 return g_datalist_id_replace_data (&object->qdata,
3409 g_quark_from_string (key),
3410 oldval, newval, destroy,
3415 * g_object_set_data_full: (skip)
3416 * @object: #GObject containing the associations
3417 * @key: name of the key
3418 * @data: data to associate with that key
3419 * @destroy: function to call when the association is destroyed
3421 * Like g_object_set_data() except it adds notification
3422 * for when the association is destroyed, either by setting it
3423 * to a different value or when the object is destroyed.
3425 * Note that the @destroy callback is not called if @data is %NULL.
3428 g_object_set_data_full (GObject *object,
3431 GDestroyNotify destroy)
3433 g_return_if_fail (G_IS_OBJECT (object));
3434 g_return_if_fail (key != NULL);
3436 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3437 data ? destroy : (GDestroyNotify) NULL);
3441 * g_object_steal_data:
3442 * @object: #GObject containing the associations
3443 * @key: name of the key
3445 * Remove a specified datum from the object's data associations,
3446 * without invoking the association's destroy handler.
3448 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3451 g_object_steal_data (GObject *object,
3456 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3457 g_return_val_if_fail (key != NULL, NULL);
3459 quark = g_quark_try_string (key);
3461 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3465 g_value_object_init (GValue *value)
3467 value->data[0].v_pointer = NULL;
3471 g_value_object_free_value (GValue *value)
3473 if (value->data[0].v_pointer)
3474 g_object_unref (value->data[0].v_pointer);
3478 g_value_object_copy_value (const GValue *src_value,
3481 if (src_value->data[0].v_pointer)
3482 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3484 dest_value->data[0].v_pointer = NULL;
3488 g_value_object_transform_value (const GValue *src_value,
3491 if (src_value->data[0].v_pointer && g_type_is_a (G_OBJECT_TYPE (src_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
3492 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3494 dest_value->data[0].v_pointer = NULL;
3498 g_value_object_peek_pointer (const GValue *value)
3500 return value->data[0].v_pointer;
3504 g_value_object_collect_value (GValue *value,
3505 guint n_collect_values,
3506 GTypeCValue *collect_values,
3507 guint collect_flags)
3509 if (collect_values[0].v_pointer)
3511 GObject *object = collect_values[0].v_pointer;
3513 if (object->g_type_instance.g_class == NULL)
3514 return g_strconcat ("invalid unclassed object pointer for value type `",
3515 G_VALUE_TYPE_NAME (value),
3518 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3519 return g_strconcat ("invalid object type `",
3520 G_OBJECT_TYPE_NAME (object),
3521 "' for value type `",
3522 G_VALUE_TYPE_NAME (value),
3525 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3526 value->data[0].v_pointer = g_object_ref (object);
3529 value->data[0].v_pointer = NULL;
3535 g_value_object_lcopy_value (const GValue *value,
3536 guint n_collect_values,
3537 GTypeCValue *collect_values,
3538 guint collect_flags)
3540 GObject **object_p = collect_values[0].v_pointer;
3543 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3545 if (!value->data[0].v_pointer)
3547 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3548 *object_p = value->data[0].v_pointer;
3550 *object_p = g_object_ref (value->data[0].v_pointer);
3556 * g_value_set_object:
3557 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3558 * @v_object: (type GObject.Object) (allow-none): object value to be set
3560 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3562 * g_value_set_object() increases the reference count of @v_object
3563 * (the #GValue holds a reference to @v_object). If you do not wish
3564 * to increase the reference count of the object (i.e. you wish to
3565 * pass your current reference to the #GValue because you no longer
3566 * need it), use g_value_take_object() instead.
3568 * It is important that your #GValue holds a reference to @v_object (either its
3569 * own, or one it has taken) to ensure that the object won't be destroyed while
3570 * the #GValue still exists).
3573 g_value_set_object (GValue *value,
3578 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3580 old = value->data[0].v_pointer;
3584 g_return_if_fail (G_IS_OBJECT (v_object));
3585 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3587 value->data[0].v_pointer = v_object;
3588 g_object_ref (value->data[0].v_pointer);
3591 value->data[0].v_pointer = NULL;
3594 g_object_unref (old);
3598 * g_value_set_object_take_ownership: (skip)
3599 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3600 * @v_object: (allow-none): object value to be set
3602 * This is an internal function introduced mainly for C marshallers.
3604 * Deprecated: 2.4: Use g_value_take_object() instead.
3607 g_value_set_object_take_ownership (GValue *value,
3610 g_value_take_object (value, v_object);
3614 * g_value_take_object: (skip)
3615 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3616 * @v_object: (allow-none): object value to be set
3618 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3619 * and takes over the ownership of the callers reference to @v_object;
3620 * the caller doesn't have to unref it any more (i.e. the reference
3621 * count of the object is not increased).
3623 * If you want the #GValue to hold its own reference to @v_object, use
3624 * g_value_set_object() instead.
3629 g_value_take_object (GValue *value,
3632 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3634 if (value->data[0].v_pointer)
3636 g_object_unref (value->data[0].v_pointer);
3637 value->data[0].v_pointer = NULL;
3642 g_return_if_fail (G_IS_OBJECT (v_object));
3643 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3645 value->data[0].v_pointer = v_object; /* we take over the reference count */
3650 * g_value_get_object:
3651 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3653 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3655 * Returns: (type GObject.Object) (transfer none): object contents of @value
3658 g_value_get_object (const GValue *value)
3660 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3662 return value->data[0].v_pointer;
3666 * g_value_dup_object:
3667 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3669 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3670 * its reference count. If the contents of the #GValue are %NULL, then
3671 * %NULL will be returned.
3673 * Returns: (type GObject.Object) (transfer full): object content of @value,
3674 * should be unreferenced when no longer needed.
3677 g_value_dup_object (const GValue *value)
3679 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3681 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3685 * g_signal_connect_object: (skip)
3686 * @instance: the instance to connect to.
3687 * @detailed_signal: a string of the form "signal-name::detail".
3688 * @c_handler: the #GCallback to connect.
3689 * @gobject: the object to pass as data to @c_handler.
3690 * @connect_flags: a combination of #GConnectFlags.
3692 * This is similar to g_signal_connect_data(), but uses a closure which
3693 * ensures that the @gobject stays alive during the call to @c_handler
3694 * by temporarily adding a reference count to @gobject.
3696 * When the @gobject is destroyed the signal handler will be automatically
3697 * disconnected. Note that this is not currently threadsafe (ie:
3698 * emitting a signal while @gobject is being destroyed in another thread
3701 * Returns: the handler id.
3704 g_signal_connect_object (gpointer instance,
3705 const gchar *detailed_signal,
3706 GCallback c_handler,
3708 GConnectFlags connect_flags)
3710 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3711 g_return_val_if_fail (detailed_signal != NULL, 0);
3712 g_return_val_if_fail (c_handler != NULL, 0);
3718 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3720 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3722 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3725 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3731 GClosure *closures[1]; /* flexible array */
3733 /* don't change this structure without supplying an accessor for
3734 * watched closures, e.g.:
3735 * GSList* g_object_list_watched_closures (GObject *object)
3738 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3739 * carray = g_object_get_data (object, "GObject-closure-array");
3742 * GSList *slist = NULL;
3744 * for (i = 0; i < carray->n_closures; i++)
3745 * slist = g_slist_prepend (slist, carray->closures[i]);
3753 object_remove_closure (gpointer data,
3756 GObject *object = data;
3760 G_LOCK (closure_array_mutex);
3761 carray = g_object_get_qdata (object, quark_closure_array);
3762 for (i = 0; i < carray->n_closures; i++)
3763 if (carray->closures[i] == closure)
3765 carray->n_closures--;
3766 if (i < carray->n_closures)
3767 carray->closures[i] = carray->closures[carray->n_closures];
3768 G_UNLOCK (closure_array_mutex);
3771 G_UNLOCK (closure_array_mutex);
3772 g_assert_not_reached ();
3776 destroy_closure_array (gpointer data)
3778 CArray *carray = data;
3779 GObject *object = carray->object;
3780 guint i, n = carray->n_closures;
3782 for (i = 0; i < n; i++)
3784 GClosure *closure = carray->closures[i];
3786 /* removing object_remove_closure() upfront is probably faster than
3787 * letting it fiddle with quark_closure_array which is empty anyways
3789 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3790 g_closure_invalidate (closure);
3796 * g_object_watch_closure:
3797 * @object: GObject restricting lifetime of @closure
3798 * @closure: GClosure to watch
3800 * This function essentially limits the life time of the @closure to
3801 * the life time of the object. That is, when the object is finalized,
3802 * the @closure is invalidated by calling g_closure_invalidate() on
3803 * it, in order to prevent invocations of the closure with a finalized
3804 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3805 * added as marshal guards to the @closure, to ensure that an extra
3806 * reference count is held on @object during invocation of the
3807 * @closure. Usually, this function will be called on closures that
3808 * use this @object as closure data.
3811 g_object_watch_closure (GObject *object,
3817 g_return_if_fail (G_IS_OBJECT (object));
3818 g_return_if_fail (closure != NULL);
3819 g_return_if_fail (closure->is_invalid == FALSE);
3820 g_return_if_fail (closure->in_marshal == FALSE);
3821 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3823 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3824 g_closure_add_marshal_guards (closure,
3825 object, (GClosureNotify) g_object_ref,
3826 object, (GClosureNotify) g_object_unref);
3827 G_LOCK (closure_array_mutex);
3828 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3831 carray = g_renew (CArray, NULL, 1);
3832 carray->object = object;
3833 carray->n_closures = 1;
3838 i = carray->n_closures++;
3839 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3841 carray->closures[i] = closure;
3842 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3843 G_UNLOCK (closure_array_mutex);
3847 * g_closure_new_object:
3848 * @sizeof_closure: the size of the structure to allocate, must be at least
3849 * <literal>sizeof (GClosure)</literal>
3850 * @object: a #GObject pointer to store in the @data field of the newly
3851 * allocated #GClosure
3853 * A variant of g_closure_new_simple() which stores @object in the
3854 * @data field of the closure and calls g_object_watch_closure() on
3855 * @object and the created closure. This function is mainly useful
3856 * when implementing new types of closures.
3858 * Returns: (transfer full): a newly allocated #GClosure
3861 g_closure_new_object (guint sizeof_closure,
3866 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3867 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3869 closure = g_closure_new_simple (sizeof_closure, object);
3870 g_object_watch_closure (object, closure);
3876 * g_cclosure_new_object: (skip)
3877 * @callback_func: the function to invoke
3878 * @object: a #GObject pointer to pass to @callback_func
3880 * A variant of g_cclosure_new() which uses @object as @user_data and
3881 * calls g_object_watch_closure() on @object and the created
3882 * closure. This function is useful when you have a callback closely
3883 * associated with a #GObject, and want the callback to no longer run
3884 * after the object is is freed.
3886 * Returns: a new #GCClosure
3889 g_cclosure_new_object (GCallback callback_func,
3894 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3895 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3896 g_return_val_if_fail (callback_func != NULL, NULL);
3898 closure = g_cclosure_new (callback_func, object, NULL);
3899 g_object_watch_closure (object, closure);
3905 * g_cclosure_new_object_swap: (skip)
3906 * @callback_func: the function to invoke
3907 * @object: a #GObject pointer to pass to @callback_func
3909 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3910 * and calls g_object_watch_closure() on @object and the created
3911 * closure. This function is useful when you have a callback closely
3912 * associated with a #GObject, and want the callback to no longer run
3913 * after the object is is freed.
3915 * Returns: a new #GCClosure
3918 g_cclosure_new_object_swap (GCallback callback_func,
3923 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3924 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3925 g_return_val_if_fail (callback_func != NULL, NULL);
3927 closure = g_cclosure_new_swap (callback_func, object, NULL);
3928 g_object_watch_closure (object, closure);
3934 g_object_compat_control (gsize what,
3940 case 1: /* floating base type */
3941 return G_TYPE_INITIALLY_UNOWNED;
3942 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3943 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3945 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3947 *pp = floating_flag_handler;
3954 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3957 g_initially_unowned_init (GInitiallyUnowned *object)
3959 g_object_force_floating (object);
3963 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3970 * A structure containing a weak reference to a #GObject. It can either
3971 * be empty (i.e. point to %NULL), or point to an object for as long as
3972 * at least one "strong" reference to that object exists. Before the
3973 * object's #GObjectClass.dispose method is called, every #GWeakRef
3974 * associated with becomes empty (i.e. points to %NULL).
3976 * Like #GValue, #GWeakRef can be statically allocated, stack- or
3977 * heap-allocated, or embedded in larger structures.
3979 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
3980 * reference is thread-safe: converting a weak pointer to a reference is
3981 * atomic with respect to invalidation of weak pointers to destroyed
3984 * If the object's #GObjectClass.dispose method results in additional
3985 * references to the object being held, any #GWeakRef<!-- -->s taken
3986 * before it was disposed will continue to point to %NULL. If
3987 * #GWeakRef<!-- -->s are taken after the object is disposed and
3988 * re-referenced, they will continue to point to it until its refcount
3989 * goes back to zero, at which point they too will be invalidated.
3993 * g_weak_ref_init: (skip)
3994 * @weak_ref: (inout): uninitialized or empty location for a weak
3996 * @object: (allow-none): a #GObject or %NULL
3998 * Initialise a non-statically-allocated #GWeakRef.
4000 * This function also calls g_weak_ref_set() with @object on the
4001 * freshly-initialised weak reference.
4003 * This function should always be matched with a call to
4004 * g_weak_ref_clear(). It is not necessary to use this function for a
4005 * #GWeakRef in static storage because it will already be
4006 * properly initialised. Just use g_weak_ref_set() directly.
4011 g_weak_ref_init (GWeakRef *weak_ref,
4014 weak_ref->priv.p = NULL;
4016 g_weak_ref_set (weak_ref, object);
4020 * g_weak_ref_clear: (skip)
4021 * @weak_ref: (inout): location of a weak reference, which
4024 * Frees resources associated with a non-statically-allocated #GWeakRef.
4025 * After this call, the #GWeakRef is left in an undefined state.
4027 * You should only call this on a #GWeakRef that previously had
4028 * g_weak_ref_init() called on it.
4033 g_weak_ref_clear (GWeakRef *weak_ref)
4035 g_weak_ref_set (weak_ref, NULL);
4038 weak_ref->priv.p = (void *) 0xccccccccu;
4042 * g_weak_ref_get: (skip)
4043 * @weak_ref: (inout): location of a weak reference to a #GObject
4045 * If @weak_ref is not empty, atomically acquire a strong
4046 * reference to the object it points to, and return that reference.
4048 * This function is needed because of the potential race between taking
4049 * the pointer value and g_object_ref() on it, if the object was losing
4050 * its last reference at the same time in a different thread.
4052 * The caller should release the resulting reference in the usual way,
4053 * by using g_object_unref().
4055 * Returns: (transfer full) (type GObject.Object): the object pointed to
4056 * by @weak_ref, or %NULL if it was empty
4061 g_weak_ref_get (GWeakRef *weak_ref)
4063 gpointer object_or_null;
4065 g_return_val_if_fail (weak_ref!= NULL, NULL);
4067 g_rw_lock_reader_lock (&weak_locations_lock);
4069 object_or_null = weak_ref->priv.p;
4071 if (object_or_null != NULL)
4072 g_object_ref (object_or_null);
4074 g_rw_lock_reader_unlock (&weak_locations_lock);
4076 return object_or_null;
4080 * g_weak_ref_set: (skip)
4081 * @weak_ref: location for a weak reference
4082 * @object: (allow-none): a #GObject or %NULL
4084 * Change the object to which @weak_ref points, or set it to
4087 * You must own a strong reference on @object while calling this
4093 g_weak_ref_set (GWeakRef *weak_ref,
4096 GSList **weak_locations;
4097 GObject *new_object;
4098 GObject *old_object;
4100 g_return_if_fail (weak_ref != NULL);
4101 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4103 new_object = object;
4105 g_rw_lock_writer_lock (&weak_locations_lock);
4107 /* We use the extra level of indirection here so that if we have ever
4108 * had a weak pointer installed at any point in time on this object,
4109 * we can see that there is a non-NULL value associated with the
4110 * weak-pointer quark and know that this value will not change at any
4111 * point in the object's lifetime.
4113 * Both properties are important for reducing the amount of times we
4114 * need to acquire locks and for decreasing the duration of time the
4115 * lock is held while avoiding some rather tricky races.
4117 * Specifically: we can avoid having to do an extra unconditional lock
4118 * in g_object_unref() without worrying about some extremely tricky
4122 old_object = weak_ref->priv.p;
4123 if (new_object != old_object)
4125 weak_ref->priv.p = new_object;
4127 /* Remove the weak ref from the old object */
4128 if (old_object != NULL)
4130 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4131 /* for it to point to an object, the object must have had it added once */
4132 g_assert (weak_locations != NULL);
4134 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4137 /* Add the weak ref to the new object */
4138 if (new_object != NULL)
4140 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4142 if (weak_locations == NULL)
4144 weak_locations = g_new0 (GSList *, 1);
4145 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4148 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4152 g_rw_lock_writer_unlock (&weak_locations_lock);