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 check_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 GQuark quark_in_construction;
201 static GParamSpecPool *pspec_pool = NULL;
202 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
203 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
204 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
205 static GQuark quark_weak_locations = 0;
206 static GRWLock weak_locations_lock;
208 G_LOCK_DEFINE_STATIC(notify_lock);
210 /* --- functions --- */
212 g_object_notify_queue_free (gpointer data)
214 GObjectNotifyQueue *nqueue = data;
216 g_slist_free (nqueue->pspecs);
217 g_slice_free (GObjectNotifyQueue, nqueue);
220 static GObjectNotifyQueue*
221 g_object_notify_queue_freeze (GObject *object,
222 gboolean conditional)
224 GObjectNotifyQueue *nqueue;
227 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
232 G_UNLOCK(notify_lock);
236 nqueue = g_slice_new0 (GObjectNotifyQueue);
237 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
238 nqueue, g_object_notify_queue_free);
241 if (nqueue->freeze_count >= 65535)
242 g_critical("Free queue for %s (%p) is larger than 65535,"
243 " called g_object_freeze_notify() too often."
244 " Forgot to call g_object_thaw_notify() or infinite loop",
245 G_OBJECT_TYPE_NAME (object), object);
247 nqueue->freeze_count++;
248 G_UNLOCK(notify_lock);
254 g_object_notify_queue_thaw (GObject *object,
255 GObjectNotifyQueue *nqueue)
257 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
261 g_return_if_fail (nqueue->freeze_count > 0);
262 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
266 /* Just make sure we never get into some nasty race condition */
267 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
268 G_UNLOCK(notify_lock);
269 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
270 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
274 nqueue->freeze_count--;
275 if (nqueue->freeze_count) {
276 G_UNLOCK(notify_lock);
280 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
282 for (slist = nqueue->pspecs; slist; slist = slist->next)
284 pspecs[n_pspecs++] = slist->data;
286 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
288 G_UNLOCK(notify_lock);
291 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
296 g_object_notify_queue_add (GObject *object,
297 GObjectNotifyQueue *nqueue,
302 g_return_if_fail (nqueue->n_pspecs < 65535);
304 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
306 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
310 G_UNLOCK(notify_lock);
313 #ifdef G_ENABLE_DEBUG
314 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
315 G_LOCK_DEFINE_STATIC (debug_objects);
316 static volatile GObject *g_trap_object_ref = NULL;
317 static guint debug_objects_count = 0;
318 static GHashTable *debug_objects_ht = NULL;
321 debug_objects_foreach (gpointer key,
325 GObject *object = value;
327 g_message ("[%p] stale %s\tref_count=%u",
329 G_OBJECT_TYPE_NAME (object),
333 #ifdef G_HAS_CONSTRUCTORS
334 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
335 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
337 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
338 #endif /* G_HAS_CONSTRUCTORS */
341 debug_objects_atexit (void)
345 G_LOCK (debug_objects);
346 g_message ("stale GObjects: %u", debug_objects_count);
347 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
348 G_UNLOCK (debug_objects);
351 #endif /* G_ENABLE_DEBUG */
354 _g_object_type_init (void)
356 static gboolean initialized = FALSE;
357 static const GTypeFundamentalInfo finfo = {
358 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
361 sizeof (GObjectClass),
362 (GBaseInitFunc) g_object_base_class_init,
363 (GBaseFinalizeFunc) g_object_base_class_finalize,
364 (GClassInitFunc) g_object_do_class_init,
365 NULL /* class_destroy */,
366 NULL /* class_data */,
369 (GInstanceInitFunc) g_object_init,
370 NULL, /* value_table */
372 static const GTypeValueTable value_table = {
373 g_value_object_init, /* value_init */
374 g_value_object_free_value, /* value_free */
375 g_value_object_copy_value, /* value_copy */
376 g_value_object_peek_pointer, /* value_peek_pointer */
377 "p", /* collect_format */
378 g_value_object_collect_value, /* collect_value */
379 "p", /* lcopy_format */
380 g_value_object_lcopy_value, /* lcopy_value */
384 g_return_if_fail (initialized == FALSE);
389 info.value_table = &value_table;
390 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
391 g_assert (type == G_TYPE_OBJECT);
392 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
394 #ifdef G_ENABLE_DEBUG
397 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
398 #ifndef G_HAS_CONSTRUCTORS
399 g_atexit (debug_objects_atexit);
400 #endif /* G_HAS_CONSTRUCTORS */
402 #endif /* G_ENABLE_DEBUG */
406 g_object_base_class_init (GObjectClass *class)
408 GObjectClass *pclass = g_type_class_peek_parent (class);
410 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
411 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
414 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
416 /* reset instance specific fields and methods that don't get inherited */
417 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
418 class->get_property = NULL;
419 class->set_property = NULL;
423 g_object_base_class_finalize (GObjectClass *class)
427 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
429 g_slist_free (class->construct_properties);
430 class->construct_properties = NULL;
431 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
432 for (node = list; node; node = node->next)
434 GParamSpec *pspec = node->data;
436 g_param_spec_pool_remove (pspec_pool, pspec);
437 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
438 g_param_spec_unref (pspec);
444 g_object_do_class_init (GObjectClass *class)
446 /* read the comment about typedef struct CArray; on why not to change this quark */
447 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
449 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
450 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
451 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
452 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
453 quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
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_warning ("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_append (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_warning ("Attempt to add properties to %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_append (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,
953 static inline gboolean
954 object_in_construction (GObject *object)
956 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
960 g_object_init (GObject *object,
963 object->ref_count = 1;
964 object->qdata = NULL;
966 if (CLASS_HAS_PROPS (class))
968 /* freeze object's notification queue, g_object_newv() preserves pairedness */
969 g_object_notify_queue_freeze (object, FALSE);
972 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
974 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
975 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
978 #ifdef G_ENABLE_DEBUG
981 G_LOCK (debug_objects);
982 debug_objects_count++;
983 g_hash_table_insert (debug_objects_ht, object, object);
984 G_UNLOCK (debug_objects);
986 #endif /* G_ENABLE_DEBUG */
990 g_object_do_set_property (GObject *object,
998 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1004 g_object_do_get_property (GObject *object,
1009 switch (property_id)
1012 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1018 g_object_real_dispose (GObject *object)
1020 g_signal_handlers_destroy (object);
1021 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1022 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1026 g_object_finalize (GObject *object)
1028 if (object_in_construction (object))
1030 g_error ("object %s %p finalized while still in-construction",
1031 G_OBJECT_TYPE_NAME (object), object);
1034 g_datalist_clear (&object->qdata);
1036 #ifdef G_ENABLE_DEBUG
1039 G_LOCK (debug_objects);
1040 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1041 g_hash_table_remove (debug_objects_ht, object);
1042 debug_objects_count--;
1043 G_UNLOCK (debug_objects);
1045 #endif /* G_ENABLE_DEBUG */
1050 g_object_dispatch_properties_changed (GObject *object,
1052 GParamSpec **pspecs)
1056 for (i = 0; i < n_pspecs; i++)
1057 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1061 * g_object_run_dispose:
1062 * @object: a #GObject
1064 * Releases all references to other objects. This can be used to break
1067 * This functions should only be called from object system implementations.
1070 g_object_run_dispose (GObject *object)
1072 g_return_if_fail (G_IS_OBJECT (object));
1073 g_return_if_fail (object->ref_count > 0);
1075 g_object_ref (object);
1076 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1077 G_OBJECT_GET_CLASS (object)->dispose (object);
1078 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1079 g_object_unref (object);
1083 * g_object_freeze_notify:
1084 * @object: a #GObject
1086 * Increases the freeze count on @object. If the freeze count is
1087 * non-zero, the emission of "notify" signals on @object is
1088 * stopped. The signals are queued until the freeze count is decreased
1089 * to zero. Duplicate notifications are squashed so that at most one
1090 * #GObject::notify signal is emitted for each property modified while the
1093 * This is necessary for accessors that modify multiple properties to prevent
1094 * premature notification while the object is still being modified.
1097 g_object_freeze_notify (GObject *object)
1099 g_return_if_fail (G_IS_OBJECT (object));
1101 if (g_atomic_int_get (&object->ref_count) == 0)
1104 g_object_ref (object);
1105 g_object_notify_queue_freeze (object, FALSE);
1106 g_object_unref (object);
1110 get_notify_pspec (GParamSpec *pspec)
1112 GParamSpec *redirected;
1114 /* we don't notify on non-READABLE parameters */
1115 if (~pspec->flags & G_PARAM_READABLE)
1118 /* if the paramspec is redirected, notify on the target */
1119 redirected = g_param_spec_get_redirect_target (pspec);
1120 if (redirected != NULL)
1123 /* else, notify normally */
1128 g_object_notify_by_spec_internal (GObject *object,
1131 GParamSpec *notify_pspec;
1133 notify_pspec = get_notify_pspec (pspec);
1135 if (notify_pspec != NULL)
1137 GObjectNotifyQueue *nqueue;
1139 /* conditional freeze: only increase freeze count if already frozen */
1140 nqueue = g_object_notify_queue_freeze (object, TRUE);
1144 /* we're frozen, so add to the queue and release our freeze */
1145 g_object_notify_queue_add (object, nqueue, notify_pspec);
1146 g_object_notify_queue_thaw (object, nqueue);
1149 /* not frozen, so just dispatch the notification directly */
1150 G_OBJECT_GET_CLASS (object)
1151 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1157 * @object: a #GObject
1158 * @property_name: the name of a property installed on the class of @object.
1160 * Emits a "notify" signal for the property @property_name on @object.
1162 * When possible, eg. when signaling a property change from within the class
1163 * that registered the property, you should use g_object_notify_by_pspec()
1167 g_object_notify (GObject *object,
1168 const gchar *property_name)
1172 g_return_if_fail (G_IS_OBJECT (object));
1173 g_return_if_fail (property_name != NULL);
1174 if (g_atomic_int_get (&object->ref_count) == 0)
1177 g_object_ref (object);
1178 /* We don't need to get the redirect target
1179 * (by, e.g. calling g_object_class_find_property())
1180 * because g_object_notify_queue_add() does that
1182 pspec = g_param_spec_pool_lookup (pspec_pool,
1184 G_OBJECT_TYPE (object),
1188 g_warning ("%s: object class '%s' has no property named '%s'",
1190 G_OBJECT_TYPE_NAME (object),
1193 g_object_notify_by_spec_internal (object, pspec);
1194 g_object_unref (object);
1198 * g_object_notify_by_pspec:
1199 * @object: a #GObject
1200 * @pspec: the #GParamSpec of a property installed on the class of @object.
1202 * Emits a "notify" signal for the property specified by @pspec on @object.
1204 * This function omits the property name lookup, hence it is faster than
1205 * g_object_notify().
1207 * One way to avoid using g_object_notify() from within the
1208 * class that registered the properties, and using g_object_notify_by_pspec()
1209 * instead, is to store the GParamSpec used with
1210 * g_object_class_install_property() inside a static array, e.g.:
1220 * static GParamSpec *properties[PROP_LAST];
1223 * my_object_class_init (MyObjectClass *klass)
1225 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1228 * G_PARAM_READWRITE);
1229 * g_object_class_install_property (gobject_class,
1231 * properties[PROP_FOO]);
1235 * and then notify a change on the "foo" property with:
1238 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1244 g_object_notify_by_pspec (GObject *object,
1248 g_return_if_fail (G_IS_OBJECT (object));
1249 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1251 if (g_atomic_int_get (&object->ref_count) == 0)
1254 g_object_ref (object);
1255 g_object_notify_by_spec_internal (object, pspec);
1256 g_object_unref (object);
1260 * g_object_thaw_notify:
1261 * @object: a #GObject
1263 * Reverts the effect of a previous call to
1264 * g_object_freeze_notify(). The freeze count is decreased on @object
1265 * and when it reaches zero, queued "notify" signals are emitted.
1267 * Duplicate notifications for each property are squashed so that at most one
1268 * #GObject::notify signal is emitted for each property.
1270 * It is an error to call this function when the freeze count is zero.
1273 g_object_thaw_notify (GObject *object)
1275 GObjectNotifyQueue *nqueue;
1277 g_return_if_fail (G_IS_OBJECT (object));
1278 if (g_atomic_int_get (&object->ref_count) == 0)
1281 g_object_ref (object);
1283 /* FIXME: Freezing is the only way to get at the notify queue.
1284 * So we freeze once and then thaw twice.
1286 nqueue = g_object_notify_queue_freeze (object, FALSE);
1287 g_object_notify_queue_thaw (object, nqueue);
1288 g_object_notify_queue_thaw (object, nqueue);
1290 g_object_unref (object);
1294 object_get_property (GObject *object,
1298 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1299 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1300 GParamSpec *redirect;
1304 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1305 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1309 redirect = g_param_spec_get_redirect_target (pspec);
1313 class->get_property (object, param_id, value, pspec);
1317 object_set_property (GObject *object,
1319 const GValue *value,
1320 GObjectNotifyQueue *nqueue)
1322 GValue tmp_value = G_VALUE_INIT;
1323 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1324 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1325 GParamSpec *redirect;
1326 static const gchar * enable_diagnostic = NULL;
1330 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1331 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1335 redirect = g_param_spec_get_redirect_target (pspec);
1339 if (G_UNLIKELY (!enable_diagnostic))
1341 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1342 if (!enable_diagnostic)
1343 enable_diagnostic = "0";
1346 if (enable_diagnostic[0] == '1')
1348 if (pspec->flags & G_PARAM_DEPRECATED)
1349 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1350 "anymore. It will be removed in a future version.",
1351 G_OBJECT_TYPE_NAME (object), pspec->name);
1354 /* provide a copy to work from, convert (if necessary) and validate */
1355 g_value_init (&tmp_value, pspec->value_type);
1356 if (!g_value_transform (value, &tmp_value))
1357 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1359 g_type_name (pspec->value_type),
1360 G_VALUE_TYPE_NAME (value));
1361 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1363 gchar *contents = g_strdup_value_contents (value);
1365 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1367 G_VALUE_TYPE_NAME (value),
1369 g_type_name (pspec->value_type));
1374 GParamSpec *notify_pspec;
1376 class->set_property (object, param_id, &tmp_value, pspec);
1378 notify_pspec = get_notify_pspec (pspec);
1380 if (notify_pspec != NULL)
1381 g_object_notify_queue_add (object, nqueue, notify_pspec);
1383 g_value_unset (&tmp_value);
1387 object_interface_check_properties (gpointer check_data,
1390 GTypeInterface *iface_class = g_iface;
1391 GObjectClass *class;
1392 GType iface_type = iface_class->g_type;
1393 GParamSpec **pspecs;
1396 class = g_type_class_ref (iface_class->g_instance_type);
1398 if (!G_IS_OBJECT_CLASS (class))
1401 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1405 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1407 G_OBJECT_CLASS_TYPE (class),
1412 g_critical ("Object class %s doesn't implement property "
1413 "'%s' from interface '%s'",
1414 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1416 g_type_name (iface_type));
1421 /* We do a number of checks on the properties of an interface to
1422 * make sure that all classes implementing the interface are
1423 * overriding the properties in a sane way.
1425 * We do the checks in order of importance so that we can give
1426 * more useful error messages first.
1428 * First, we check that the implementation doesn't remove the
1429 * basic functionality (readability, writability) advertised by
1430 * the interface. Next, we check that it doesn't introduce
1431 * additional restrictions (such as construct-only). Finally, we
1432 * make sure the types are compatible.
1435 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1436 /* If the property on the interface is readable then the
1437 * implementation must be readable. If the interface is writable
1438 * then the implementation must be writable.
1440 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1442 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1443 "property on interface '%s'\n", pspecs[n]->name,
1444 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1448 /* If the property on the interface is writable then we need to
1449 * make sure the implementation doesn't introduce new restrictions
1450 * on that writability (ie: construct-only).
1452 * If the interface was not writable to begin with then we don't
1453 * really have any problems here because "writable at construct
1454 * type only" is still more permissive than "read only".
1456 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1458 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1460 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1461 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1462 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1468 /* If the property on the interface is readable then we are
1469 * effectively advertising that reading the property will return a
1470 * value of a specific type. All implementations of the interface
1471 * need to return items of this type -- but may be more
1472 * restrictive. For example, it is legal to have:
1474 * GtkWidget *get_item();
1476 * that is implemented by a function that always returns a
1477 * GtkEntry. In short: readability implies that the
1478 * implementation value type must be equal or more restrictive.
1480 * Similarly, if the property on the interface is writable then
1481 * must be able to accept the property being set to any value of
1482 * that type, including subclasses. In this case, we may also be
1483 * less restrictive. For example, it is legal to have:
1485 * set_item (GtkEntry *);
1487 * that is implemented by a function that will actually work with
1488 * any GtkWidget. In short: writability implies that the
1489 * implementation value type must be equal or less restrictive.
1491 * In the case that the property is both readable and writable
1492 * then the only way that both of the above can be satisfied is
1493 * with a type that is exactly equal.
1495 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1497 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1498 /* class pspec value type must have exact equality with interface */
1499 if (pspecs[n]->value_type != class_pspec->value_type)
1500 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1501 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1502 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1503 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1506 case G_PARAM_READABLE:
1507 /* class pspec value type equal or more restrictive than interface */
1508 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1509 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1510 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1511 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1512 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1515 case G_PARAM_WRITABLE:
1516 /* class pspec value type equal or less restrictive than interface */
1517 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1518 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1519 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1520 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1521 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1525 g_assert_not_reached ();
1531 g_type_class_unref (class);
1535 g_object_get_type (void)
1537 return G_TYPE_OBJECT;
1541 * g_object_new: (skip)
1542 * @object_type: the type id of the #GObject subtype to instantiate
1543 * @first_property_name: the name of the first property
1544 * @...: the value of the first property, followed optionally by more
1545 * name/value pairs, followed by %NULL
1547 * Creates a new instance of a #GObject subtype and sets its properties.
1549 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1550 * which are not explicitly specified are set to their default values.
1552 * Returns: (transfer full): a new instance of @object_type
1555 g_object_new (GType object_type,
1556 const gchar *first_property_name,
1562 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1564 /* short circuit for calls supplying no properties */
1565 if (!first_property_name)
1566 return g_object_newv (object_type, 0, NULL);
1568 va_start (var_args, first_property_name);
1569 object = g_object_new_valist (object_type, first_property_name, var_args);
1576 g_object_new_with_custom_constructor (GObjectClass *class,
1577 GObjectConstructParam *params,
1580 GObjectNotifyQueue *nqueue = NULL;
1581 gboolean newly_constructed;
1582 GObjectConstructParam *cparams;
1590 /* If we have ->constructed() then we have to do a lot more work.
1591 * It's possible that this is a singleton and it's also possible
1592 * that the user's constructor() will attempt to modify the values
1593 * that we pass in, so we'll need to allocate copies of them.
1594 * It's also possible that the user may attempt to call
1595 * g_object_set() from inside of their constructor, so we need to
1596 * add ourselves to a list of objects for which that is allowed
1597 * while their constructor() is running.
1600 /* Create the array of GObjectConstructParams for constructor() */
1601 n_cparams = g_slist_length (class->construct_properties);
1602 cparams = g_new (GObjectConstructParam, n_cparams);
1603 cvalues = g_new0 (GValue, n_cparams);
1607 /* As above, we may find the value in the passed-in params list.
1609 * If we have the value passed in then we can use the GValue from
1610 * it directly because it is safe to modify. If we use the
1611 * default value from the class, we had better not pass that in
1612 * and risk it being modified, so we create a new one.
1614 for (node = class->construct_properties; node; node = node->next)
1621 value = NULL; /* to silence gcc... */
1623 for (j = 0; j < n_params; j++)
1624 if (params[j].pspec == pspec)
1626 value = params[j].value;
1632 value = &cvalues[cvals_used++];
1633 g_value_init (value, pspec->value_type);
1634 g_param_value_set_default (pspec, value);
1637 cparams[i].pspec = pspec;
1638 cparams[i].value = value;
1642 /* construct object from construction parameters */
1643 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1644 /* free construction values */
1646 while (cvals_used--)
1647 g_value_unset (&cvalues[cvals_used]);
1650 /* There is code in the wild that relies on being able to return NULL
1651 * from its custom constructor. This was never a supported operation,
1652 * but since the code is already out there...
1656 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1657 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1661 /* g_object_init() will have marked the object as being in-construction.
1662 * Check if the returned object still is so marked, or if this is an
1663 * already-existing singleton (in which case we should not do 'constructed').
1665 newly_constructed = object_in_construction (object);
1666 if (newly_constructed)
1667 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1669 if (CLASS_HAS_PROPS (class))
1671 /* If this object was newly_constructed then g_object_init()
1672 * froze the queue. We need to freeze it here in order to get
1673 * the handle so that we can thaw it below (otherwise it will
1674 * be frozen forever).
1676 * We also want to do a freeze if we have any params to set,
1677 * even on a non-newly_constructed object.
1679 * It's possible that we have the case of non-newly created
1680 * singleton and all of the passed-in params were construct
1681 * properties so n_params > 0 but we will actually set no
1682 * properties. This is a pretty lame case to optimise, so
1683 * just ignore it and freeze anyway.
1685 if (newly_constructed || n_params)
1686 nqueue = g_object_notify_queue_freeze (object, FALSE);
1688 /* Remember: if it was newly_constructed then g_object_init()
1689 * already did a freeze, so we now have two. Release one.
1691 if (newly_constructed)
1692 g_object_notify_queue_thaw (object, nqueue);
1695 /* run 'constructed' handler if there is a custom one */
1696 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1697 class->constructed (object);
1699 /* set remaining properties */
1700 for (i = 0; i < n_params; i++)
1701 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1702 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1704 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1706 g_object_notify_queue_thaw (object, nqueue);
1712 g_object_new_internal (GObjectClass *class,
1713 GObjectConstructParam *params,
1716 GObjectNotifyQueue *nqueue = NULL;
1719 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1720 return g_object_new_with_custom_constructor (class, params, n_params);
1722 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1724 if (CLASS_HAS_PROPS (class))
1728 /* This will have been setup in g_object_init() */
1729 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1730 g_assert (nqueue != NULL);
1732 /* We will set exactly n_construct_properties construct
1733 * properties, but they may come from either the class default
1734 * values or the passed-in parameter list.
1736 for (node = class->construct_properties; node; node = node->next)
1738 const GValue *value;
1743 value = NULL; /* to silence gcc... */
1745 for (j = 0; j < n_params; j++)
1746 if (params[j].pspec == pspec)
1748 value = params[j].value;
1753 value = g_param_spec_get_default_value (pspec);
1755 object_set_property (object, pspec, value, nqueue);
1759 /* run 'constructed' handler if there is a custom one */
1760 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1761 class->constructed (object);
1767 /* Set remaining properties. The construct properties will
1768 * already have been taken, so set only the non-construct
1771 for (i = 0; i < n_params; i++)
1772 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1773 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1775 g_object_notify_queue_thaw (object, nqueue);
1783 * @object_type: the type id of the #GObject subtype to instantiate
1784 * @n_parameters: the length of the @parameters array
1785 * @parameters: (array length=n_parameters): an array of #GParameter
1787 * Creates a new instance of a #GObject subtype and sets its properties.
1789 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1790 * which are not explicitly specified are set to their default values.
1792 * Rename to: g_object_new
1793 * Returns: (type GObject.Object) (transfer full): a new instance of
1797 g_object_newv (GType object_type,
1799 GParameter *parameters)
1801 GObjectClass *class, *unref_class = NULL;
1804 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1805 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1807 /* Try to avoid thrashing the ref_count if we don't need to (since
1808 * it's a locked operation).
1810 class = g_type_class_peek_static (object_type);
1813 class = unref_class = g_type_class_ref (object_type);
1817 GObjectConstructParam *cparams;
1820 cparams = g_newa (GObjectConstructParam, n_parameters);
1823 for (i = 0; i < n_parameters; i++)
1828 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
1830 if G_UNLIKELY (!pspec)
1832 g_critical ("%s: object class '%s' has no property named '%s'",
1833 G_STRFUNC, g_type_name (object_type), parameters[i].name);
1837 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1839 g_critical ("%s: property '%s' of object class '%s' is not writable",
1840 G_STRFUNC, pspec->name, g_type_name (object_type));
1844 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1846 for (k = 0; k < j; k++)
1847 if (cparams[k].pspec == pspec)
1849 if G_UNLIKELY (k != j)
1851 g_critical ("%s: construct property '%s' for type '%s' cannot be set twice",
1852 G_STRFUNC, parameters[i].name, g_type_name (object_type));
1857 cparams[j].pspec = pspec;
1858 cparams[j].value = ¶meters[i].value;
1862 object = g_object_new_internal (class, cparams, j);
1865 /* Fast case: no properties passed in. */
1866 object = g_object_new_internal (class, NULL, 0);
1869 g_type_class_unref (unref_class);
1875 * g_object_new_valist: (skip)
1876 * @object_type: the type id of the #GObject subtype to instantiate
1877 * @first_property_name: the name of the first property
1878 * @var_args: the value of the first property, followed optionally by more
1879 * name/value pairs, followed by %NULL
1881 * Creates a new instance of a #GObject subtype and sets its properties.
1883 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1884 * which are not explicitly specified are set to their default values.
1886 * Returns: a new instance of @object_type
1889 g_object_new_valist (GType object_type,
1890 const gchar *first_property_name,
1893 GObjectClass *class, *unref_class = NULL;
1896 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1898 /* Try to avoid thrashing the ref_count if we don't need to (since
1899 * it's a locked operation).
1901 class = g_type_class_peek_static (object_type);
1904 class = unref_class = g_type_class_ref (object_type);
1906 if (first_property_name)
1908 GObjectConstructParam stack_params[16];
1909 GObjectConstructParam *params;
1913 name = first_property_name;
1914 params = stack_params;
1918 gchar *error = NULL;
1922 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
1924 if G_UNLIKELY (!pspec)
1926 g_critical ("%s: object class '%s' has no property named '%s'",
1927 G_STRFUNC, g_type_name (object_type), name);
1928 /* Can't continue because arg list will be out of sync. */
1932 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1934 g_critical ("%s: property '%s' of object class '%s' is not writable",
1935 G_STRFUNC, pspec->name, g_type_name (object_type));
1939 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1941 for (i = 0; i < n_params; i++)
1942 if (params[i].pspec == pspec)
1944 if G_UNLIKELY (i != n_params)
1946 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1947 G_STRFUNC, name, g_type_name (object_type));
1954 params = g_new (GObjectConstructParam, n_params + 1);
1955 memcpy (params, stack_params, sizeof stack_params);
1957 else if (n_params > 16)
1958 params = g_renew (GObjectConstructParam, params, n_params + 1);
1960 params[n_params].pspec = pspec;
1961 params[n_params].value = g_newa (GValue, 1);
1962 memset (params[n_params].value, 0, sizeof (GValue));
1964 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
1968 g_critical ("%s: %s", G_STRFUNC, error);
1969 g_value_unset (params[n_params].value);
1976 while ((name = va_arg (var_args, const gchar *)));
1978 object = g_object_new_internal (class, params, n_params);
1981 g_value_unset (params[n_params].value);
1983 if (params != stack_params)
1987 /* Fast case: no properties passed in. */
1988 object = g_object_new_internal (class, NULL, 0);
1991 g_type_class_unref (unref_class);
1997 g_object_constructor (GType type,
1998 guint n_construct_properties,
1999 GObjectConstructParam *construct_params)
2004 object = (GObject*) g_type_create_instance (type);
2006 /* set construction parameters */
2007 if (n_construct_properties)
2009 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2011 /* set construct properties */
2012 while (n_construct_properties--)
2014 GValue *value = construct_params->value;
2015 GParamSpec *pspec = construct_params->pspec;
2018 object_set_property (object, pspec, value, nqueue);
2020 g_object_notify_queue_thaw (object, nqueue);
2021 /* the notification queue is still frozen from g_object_init(), so
2022 * we don't need to handle it here, g_object_newv() takes
2031 g_object_constructed (GObject *object)
2033 /* empty default impl to allow unconditional upchaining */
2037 * g_object_set_valist: (skip)
2038 * @object: a #GObject
2039 * @first_property_name: name of the first property to set
2040 * @var_args: value for the first property, followed optionally by more
2041 * name/value pairs, followed by %NULL
2043 * Sets properties on an object.
2046 g_object_set_valist (GObject *object,
2047 const gchar *first_property_name,
2050 GObjectNotifyQueue *nqueue;
2053 g_return_if_fail (G_IS_OBJECT (object));
2055 g_object_ref (object);
2056 nqueue = g_object_notify_queue_freeze (object, FALSE);
2058 name = first_property_name;
2061 GValue value = G_VALUE_INIT;
2063 gchar *error = NULL;
2065 pspec = g_param_spec_pool_lookup (pspec_pool,
2067 G_OBJECT_TYPE (object),
2071 g_warning ("%s: object class '%s' has no property named '%s'",
2073 G_OBJECT_TYPE_NAME (object),
2077 if (!(pspec->flags & G_PARAM_WRITABLE))
2079 g_warning ("%s: property '%s' of object class '%s' is not writable",
2082 G_OBJECT_TYPE_NAME (object));
2085 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2087 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2088 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2092 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2096 g_warning ("%s: %s", G_STRFUNC, error);
2098 g_value_unset (&value);
2102 object_set_property (object, pspec, &value, nqueue);
2103 g_value_unset (&value);
2105 name = va_arg (var_args, gchar*);
2108 g_object_notify_queue_thaw (object, nqueue);
2109 g_object_unref (object);
2113 * g_object_get_valist: (skip)
2114 * @object: a #GObject
2115 * @first_property_name: name of the first property to get
2116 * @var_args: return location for the first property, followed optionally by more
2117 * name/return location pairs, followed by %NULL
2119 * Gets properties of an object.
2121 * In general, a copy is made of the property contents and the caller
2122 * is responsible for freeing the memory in the appropriate manner for
2123 * the type, for instance by calling g_free() or g_object_unref().
2125 * See g_object_get().
2128 g_object_get_valist (GObject *object,
2129 const gchar *first_property_name,
2134 g_return_if_fail (G_IS_OBJECT (object));
2136 g_object_ref (object);
2138 name = first_property_name;
2142 GValue value = G_VALUE_INIT;
2146 pspec = g_param_spec_pool_lookup (pspec_pool,
2148 G_OBJECT_TYPE (object),
2152 g_warning ("%s: object class '%s' has no property named '%s'",
2154 G_OBJECT_TYPE_NAME (object),
2158 if (!(pspec->flags & G_PARAM_READABLE))
2160 g_warning ("%s: property '%s' of object class '%s' is not readable",
2163 G_OBJECT_TYPE_NAME (object));
2167 g_value_init (&value, pspec->value_type);
2169 object_get_property (object, pspec, &value);
2171 G_VALUE_LCOPY (&value, var_args, 0, &error);
2174 g_warning ("%s: %s", G_STRFUNC, error);
2176 g_value_unset (&value);
2180 g_value_unset (&value);
2182 name = va_arg (var_args, gchar*);
2185 g_object_unref (object);
2189 * g_object_set: (skip)
2190 * @object: a #GObject
2191 * @first_property_name: name of the first property to set
2192 * @...: value for the first property, followed optionally by more
2193 * name/value pairs, followed by %NULL
2195 * Sets properties on an object.
2198 g_object_set (gpointer _object,
2199 const gchar *first_property_name,
2202 GObject *object = _object;
2205 g_return_if_fail (G_IS_OBJECT (object));
2207 va_start (var_args, first_property_name);
2208 g_object_set_valist (object, first_property_name, var_args);
2213 * g_object_get: (skip)
2214 * @object: a #GObject
2215 * @first_property_name: name of the first property to get
2216 * @...: return location for the first property, followed optionally by more
2217 * name/return location pairs, followed by %NULL
2219 * Gets properties of an object.
2221 * In general, a copy is made of the property contents and the caller
2222 * is responsible for freeing the memory in the appropriate manner for
2223 * the type, for instance by calling g_free() or g_object_unref().
2226 * <title>Using g_object_get(<!-- -->)</title>
2227 * An example of using g_object_get() to get the contents
2228 * of three properties - one of type #G_TYPE_INT,
2229 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
2235 * g_object_get (my_object,
2236 * "int-property", &intval,
2237 * "str-property", &strval,
2238 * "obj-property", &objval,
2241 * // Do something with intval, strval, objval
2244 * g_object_unref (objval);
2249 g_object_get (gpointer _object,
2250 const gchar *first_property_name,
2253 GObject *object = _object;
2256 g_return_if_fail (G_IS_OBJECT (object));
2258 va_start (var_args, first_property_name);
2259 g_object_get_valist (object, first_property_name, var_args);
2264 * g_object_set_property:
2265 * @object: a #GObject
2266 * @property_name: the name of the property to set
2269 * Sets a property on an object.
2272 g_object_set_property (GObject *object,
2273 const gchar *property_name,
2274 const GValue *value)
2276 GObjectNotifyQueue *nqueue;
2279 g_return_if_fail (G_IS_OBJECT (object));
2280 g_return_if_fail (property_name != NULL);
2281 g_return_if_fail (G_IS_VALUE (value));
2283 g_object_ref (object);
2284 nqueue = g_object_notify_queue_freeze (object, FALSE);
2286 pspec = g_param_spec_pool_lookup (pspec_pool,
2288 G_OBJECT_TYPE (object),
2291 g_warning ("%s: object class '%s' has no property named '%s'",
2293 G_OBJECT_TYPE_NAME (object),
2295 else if (!(pspec->flags & G_PARAM_WRITABLE))
2296 g_warning ("%s: property '%s' of object class '%s' is not writable",
2299 G_OBJECT_TYPE_NAME (object));
2300 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2301 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2302 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2304 object_set_property (object, pspec, value, nqueue);
2306 g_object_notify_queue_thaw (object, nqueue);
2307 g_object_unref (object);
2311 * g_object_get_property:
2312 * @object: a #GObject
2313 * @property_name: the name of the property to get
2314 * @value: return location for the property value
2316 * Gets a property of an object. @value must have been initialized to the
2317 * expected type of the property (or a type to which the expected type can be
2318 * transformed) using g_value_init().
2320 * In general, a copy is made of the property contents and the caller is
2321 * responsible for freeing the memory by calling g_value_unset().
2323 * Note that g_object_get_property() is really intended for language
2324 * bindings, g_object_get() is much more convenient for C programming.
2327 g_object_get_property (GObject *object,
2328 const gchar *property_name,
2333 g_return_if_fail (G_IS_OBJECT (object));
2334 g_return_if_fail (property_name != NULL);
2335 g_return_if_fail (G_IS_VALUE (value));
2337 g_object_ref (object);
2339 pspec = g_param_spec_pool_lookup (pspec_pool,
2341 G_OBJECT_TYPE (object),
2344 g_warning ("%s: object class '%s' has no property named '%s'",
2346 G_OBJECT_TYPE_NAME (object),
2348 else if (!(pspec->flags & G_PARAM_READABLE))
2349 g_warning ("%s: property '%s' of object class '%s' is not readable",
2352 G_OBJECT_TYPE_NAME (object));
2355 GValue *prop_value, tmp_value = G_VALUE_INIT;
2357 /* auto-conversion of the callers value type
2359 if (G_VALUE_TYPE (value) == pspec->value_type)
2361 g_value_reset (value);
2364 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2366 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2367 G_STRFUNC, pspec->name,
2368 g_type_name (pspec->value_type),
2369 G_VALUE_TYPE_NAME (value));
2370 g_object_unref (object);
2375 g_value_init (&tmp_value, pspec->value_type);
2376 prop_value = &tmp_value;
2378 object_get_property (object, pspec, prop_value);
2379 if (prop_value != value)
2381 g_value_transform (prop_value, value);
2382 g_value_unset (&tmp_value);
2386 g_object_unref (object);
2390 * g_object_connect: (skip)
2391 * @object: a #GObject
2392 * @signal_spec: the spec for the first signal
2393 * @...: #GCallback for the first signal, followed by data for the
2394 * first signal, followed optionally by more signal
2395 * spec/callback/data triples, followed by %NULL
2397 * A convenience function to connect multiple signals at once.
2399 * The signal specs expected by this function have the form
2400 * "modifier::signal_name", where modifier can be one of the following:
2403 * <term>signal</term>
2405 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2406 * </para></listitem>
2409 * <term>object_signal</term>
2410 * <term>object-signal</term>
2412 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2413 * </para></listitem>
2416 * <term>swapped_signal</term>
2417 * <term>swapped-signal</term>
2419 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2420 * </para></listitem>
2423 * <term>swapped_object_signal</term>
2424 * <term>swapped-object-signal</term>
2426 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2427 * </para></listitem>
2430 * <term>signal_after</term>
2431 * <term>signal-after</term>
2433 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2434 * </para></listitem>
2437 * <term>object_signal_after</term>
2438 * <term>object-signal-after</term>
2440 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2441 * </para></listitem>
2444 * <term>swapped_signal_after</term>
2445 * <term>swapped-signal-after</term>
2447 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2448 * </para></listitem>
2451 * <term>swapped_object_signal_after</term>
2452 * <term>swapped-object-signal-after</term>
2454 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2455 * </para></listitem>
2460 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2461 * "type", GTK_WINDOW_POPUP,
2464 * "signal::event", gtk_menu_window_event, menu,
2465 * "signal::size_request", gtk_menu_window_size_request, menu,
2466 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2470 * Returns: (transfer none): @object
2473 g_object_connect (gpointer _object,
2474 const gchar *signal_spec,
2477 GObject *object = _object;
2480 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2481 g_return_val_if_fail (object->ref_count > 0, object);
2483 va_start (var_args, signal_spec);
2486 GCallback callback = va_arg (var_args, GCallback);
2487 gpointer data = va_arg (var_args, gpointer);
2489 if (strncmp (signal_spec, "signal::", 8) == 0)
2490 g_signal_connect_data (object, signal_spec + 8,
2491 callback, data, NULL,
2493 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2494 strncmp (signal_spec, "object-signal::", 15) == 0)
2495 g_signal_connect_object (object, signal_spec + 15,
2498 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2499 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2500 g_signal_connect_data (object, signal_spec + 16,
2501 callback, data, NULL,
2503 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2504 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2505 g_signal_connect_object (object, signal_spec + 23,
2508 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2509 strncmp (signal_spec, "signal-after::", 14) == 0)
2510 g_signal_connect_data (object, signal_spec + 14,
2511 callback, data, NULL,
2513 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2514 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2515 g_signal_connect_object (object, signal_spec + 21,
2518 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2519 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2520 g_signal_connect_data (object, signal_spec + 22,
2521 callback, data, NULL,
2522 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2523 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2524 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2525 g_signal_connect_object (object, signal_spec + 29,
2527 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2530 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2533 signal_spec = va_arg (var_args, gchar*);
2541 * g_object_disconnect: (skip)
2542 * @object: a #GObject
2543 * @signal_spec: the spec for the first signal
2544 * @...: #GCallback for the first signal, followed by data for the first signal,
2545 * followed optionally by more signal spec/callback/data triples,
2548 * A convenience function to disconnect multiple signals at once.
2550 * The signal specs expected by this function have the form
2551 * "any_signal", which means to disconnect any signal with matching
2552 * callback and data, or "any_signal::signal_name", which only
2553 * disconnects the signal named "signal_name".
2556 g_object_disconnect (gpointer _object,
2557 const gchar *signal_spec,
2560 GObject *object = _object;
2563 g_return_if_fail (G_IS_OBJECT (object));
2564 g_return_if_fail (object->ref_count > 0);
2566 va_start (var_args, signal_spec);
2569 GCallback callback = va_arg (var_args, GCallback);
2570 gpointer data = va_arg (var_args, gpointer);
2571 guint sid = 0, detail = 0, mask = 0;
2573 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2574 strncmp (signal_spec, "any-signal::", 12) == 0)
2577 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2579 else if (strcmp (signal_spec, "any_signal") == 0 ||
2580 strcmp (signal_spec, "any-signal") == 0)
2583 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2587 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2591 if ((mask & G_SIGNAL_MATCH_ID) &&
2592 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2593 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2594 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2596 NULL, (gpointer)callback, data))
2597 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2598 signal_spec = va_arg (var_args, gchar*);
2609 } weak_refs[1]; /* flexible array */
2613 weak_refs_notify (gpointer data)
2615 WeakRefStack *wstack = data;
2618 for (i = 0; i < wstack->n_weak_refs; i++)
2619 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2624 * g_object_weak_ref: (skip)
2625 * @object: #GObject to reference weakly
2626 * @notify: callback to invoke before the object is freed
2627 * @data: extra data to pass to notify
2629 * Adds a weak reference callback to an object. Weak references are
2630 * used for notification when an object is finalized. They are called
2631 * "weak references" because they allow you to safely hold a pointer
2632 * to an object without calling g_object_ref() (g_object_ref() adds a
2633 * strong reference, that is, forces the object to stay alive).
2635 * Note that the weak references created by this method are not
2636 * thread-safe: they cannot safely be used in one thread if the
2637 * object's last g_object_unref() might happen in another thread.
2638 * Use #GWeakRef if thread-safety is required.
2641 g_object_weak_ref (GObject *object,
2645 WeakRefStack *wstack;
2648 g_return_if_fail (G_IS_OBJECT (object));
2649 g_return_if_fail (notify != NULL);
2650 g_return_if_fail (object->ref_count >= 1);
2652 G_LOCK (weak_refs_mutex);
2653 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2656 i = wstack->n_weak_refs++;
2657 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2661 wstack = g_renew (WeakRefStack, NULL, 1);
2662 wstack->object = object;
2663 wstack->n_weak_refs = 1;
2666 wstack->weak_refs[i].notify = notify;
2667 wstack->weak_refs[i].data = data;
2668 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2669 G_UNLOCK (weak_refs_mutex);
2673 * g_object_weak_unref: (skip)
2674 * @object: #GObject to remove a weak reference from
2675 * @notify: callback to search for
2676 * @data: data to search for
2678 * Removes a weak reference callback to an object.
2681 g_object_weak_unref (GObject *object,
2685 WeakRefStack *wstack;
2686 gboolean found_one = FALSE;
2688 g_return_if_fail (G_IS_OBJECT (object));
2689 g_return_if_fail (notify != NULL);
2691 G_LOCK (weak_refs_mutex);
2692 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2697 for (i = 0; i < wstack->n_weak_refs; i++)
2698 if (wstack->weak_refs[i].notify == notify &&
2699 wstack->weak_refs[i].data == data)
2702 wstack->n_weak_refs -= 1;
2703 if (i != wstack->n_weak_refs)
2704 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2709 G_UNLOCK (weak_refs_mutex);
2711 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2715 * g_object_add_weak_pointer: (skip)
2716 * @object: The object that should be weak referenced.
2717 * @weak_pointer_location: (inout): The memory address of a pointer.
2719 * Adds a weak reference from weak_pointer to @object to indicate that
2720 * the pointer located at @weak_pointer_location is only valid during
2721 * the lifetime of @object. When the @object is finalized,
2722 * @weak_pointer will be set to %NULL.
2724 * Note that as with g_object_weak_ref(), the weak references created by
2725 * this method are not thread-safe: they cannot safely be used in one
2726 * thread if the object's last g_object_unref() might happen in another
2727 * thread. Use #GWeakRef if thread-safety is required.
2730 g_object_add_weak_pointer (GObject *object,
2731 gpointer *weak_pointer_location)
2733 g_return_if_fail (G_IS_OBJECT (object));
2734 g_return_if_fail (weak_pointer_location != NULL);
2736 g_object_weak_ref (object,
2737 (GWeakNotify) g_nullify_pointer,
2738 weak_pointer_location);
2742 * g_object_remove_weak_pointer: (skip)
2743 * @object: The object that is weak referenced.
2744 * @weak_pointer_location: (inout): The memory address of a pointer.
2746 * Removes a weak reference from @object that was previously added
2747 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2748 * to match the one used with g_object_add_weak_pointer().
2751 g_object_remove_weak_pointer (GObject *object,
2752 gpointer *weak_pointer_location)
2754 g_return_if_fail (G_IS_OBJECT (object));
2755 g_return_if_fail (weak_pointer_location != NULL);
2757 g_object_weak_unref (object,
2758 (GWeakNotify) g_nullify_pointer,
2759 weak_pointer_location);
2763 object_floating_flag_handler (GObject *object,
2769 case +1: /* force floating if possible */
2771 oldvalue = g_atomic_pointer_get (&object->qdata);
2772 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2773 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2774 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2775 case -1: /* sink if possible */
2777 oldvalue = g_atomic_pointer_get (&object->qdata);
2778 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2779 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2780 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2781 default: /* check floating */
2782 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2787 * g_object_is_floating:
2788 * @object: (type GObject.Object): a #GObject
2790 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2795 * Returns: %TRUE if @object has a floating reference
2798 g_object_is_floating (gpointer _object)
2800 GObject *object = _object;
2801 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2802 return floating_flag_handler (object, 0);
2806 * g_object_ref_sink:
2807 * @object: (type GObject.Object): a #GObject
2809 * Increase the reference count of @object, and possibly remove the
2810 * <link linkend="floating-ref">floating</link> reference, if @object
2811 * has a floating reference.
2813 * In other words, if the object is floating, then this call "assumes
2814 * ownership" of the floating reference, converting it to a normal
2815 * reference by clearing the floating flag while leaving the reference
2816 * count unchanged. If the object is not floating, then this call
2817 * adds a new normal reference increasing the reference count by one.
2821 * Returns: (type GObject.Object) (transfer none): @object
2824 g_object_ref_sink (gpointer _object)
2826 GObject *object = _object;
2827 gboolean was_floating;
2828 g_return_val_if_fail (G_IS_OBJECT (object), object);
2829 g_return_val_if_fail (object->ref_count >= 1, object);
2830 g_object_ref (object);
2831 was_floating = floating_flag_handler (object, -1);
2833 g_object_unref (object);
2838 * g_object_force_floating:
2839 * @object: a #GObject
2841 * This function is intended for #GObject implementations to re-enforce a
2842 * <link linkend="floating-ref">floating</link> object reference.
2843 * Doing this is seldom required: all
2844 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2845 * usually just needs to be sunken by calling g_object_ref_sink().
2850 g_object_force_floating (GObject *object)
2852 g_return_if_fail (G_IS_OBJECT (object));
2853 g_return_if_fail (object->ref_count >= 1);
2855 floating_flag_handler (object, +1);
2860 guint n_toggle_refs;
2862 GToggleNotify notify;
2864 } toggle_refs[1]; /* flexible array */
2868 toggle_refs_notify (GObject *object,
2869 gboolean is_last_ref)
2871 ToggleRefStack tstack, *tstackptr;
2873 G_LOCK (toggle_refs_mutex);
2874 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2875 tstack = *tstackptr;
2876 G_UNLOCK (toggle_refs_mutex);
2878 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2879 * will only be notified when there is exactly one of them.
2881 g_assert (tstack.n_toggle_refs == 1);
2882 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2886 * g_object_add_toggle_ref: (skip)
2887 * @object: a #GObject
2888 * @notify: a function to call when this reference is the
2889 * last reference to the object, or is no longer
2890 * the last reference.
2891 * @data: data to pass to @notify
2893 * Increases the reference count of the object by one and sets a
2894 * callback to be called when all other references to the object are
2895 * dropped, or when this is already the last reference to the object
2896 * and another reference is established.
2898 * This functionality is intended for binding @object to a proxy
2899 * object managed by another memory manager. This is done with two
2900 * paired references: the strong reference added by
2901 * g_object_add_toggle_ref() and a reverse reference to the proxy
2902 * object which is either a strong reference or weak reference.
2904 * The setup is that when there are no other references to @object,
2905 * only a weak reference is held in the reverse direction from @object
2906 * to the proxy object, but when there are other references held to
2907 * @object, a strong reference is held. The @notify callback is called
2908 * when the reference from @object to the proxy object should be
2909 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2910 * true) or weak to strong (@is_last_ref false).
2912 * Since a (normal) reference must be held to the object before
2913 * calling g_object_add_toggle_ref(), the initial state of the reverse
2914 * link is always strong.
2916 * Multiple toggle references may be added to the same gobject,
2917 * however if there are multiple toggle references to an object, none
2918 * of them will ever be notified until all but one are removed. For
2919 * this reason, you should only ever use a toggle reference if there
2920 * is important state in the proxy object.
2925 g_object_add_toggle_ref (GObject *object,
2926 GToggleNotify notify,
2929 ToggleRefStack *tstack;
2932 g_return_if_fail (G_IS_OBJECT (object));
2933 g_return_if_fail (notify != NULL);
2934 g_return_if_fail (object->ref_count >= 1);
2936 g_object_ref (object);
2938 G_LOCK (toggle_refs_mutex);
2939 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2942 i = tstack->n_toggle_refs++;
2943 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2944 * in tstate->toggle_refs */
2945 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2949 tstack = g_renew (ToggleRefStack, NULL, 1);
2950 tstack->object = object;
2951 tstack->n_toggle_refs = 1;
2955 /* Set a flag for fast lookup after adding the first toggle reference */
2956 if (tstack->n_toggle_refs == 1)
2957 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2959 tstack->toggle_refs[i].notify = notify;
2960 tstack->toggle_refs[i].data = data;
2961 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2962 (GDestroyNotify)g_free);
2963 G_UNLOCK (toggle_refs_mutex);
2967 * g_object_remove_toggle_ref: (skip)
2968 * @object: a #GObject
2969 * @notify: a function to call when this reference is the
2970 * last reference to the object, or is no longer
2971 * the last reference.
2972 * @data: data to pass to @notify
2974 * Removes a reference added with g_object_add_toggle_ref(). The
2975 * reference count of the object is decreased by one.
2980 g_object_remove_toggle_ref (GObject *object,
2981 GToggleNotify notify,
2984 ToggleRefStack *tstack;
2985 gboolean found_one = FALSE;
2987 g_return_if_fail (G_IS_OBJECT (object));
2988 g_return_if_fail (notify != NULL);
2990 G_LOCK (toggle_refs_mutex);
2991 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2996 for (i = 0; i < tstack->n_toggle_refs; i++)
2997 if (tstack->toggle_refs[i].notify == notify &&
2998 tstack->toggle_refs[i].data == data)
3001 tstack->n_toggle_refs -= 1;
3002 if (i != tstack->n_toggle_refs)
3003 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3005 if (tstack->n_toggle_refs == 0)
3006 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3011 G_UNLOCK (toggle_refs_mutex);
3014 g_object_unref (object);
3016 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3021 * @object: (type GObject.Object): a #GObject
3023 * Increases the reference count of @object.
3025 * Returns: (type GObject.Object) (transfer none): the same @object
3028 g_object_ref (gpointer _object)
3030 GObject *object = _object;
3033 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3034 g_return_val_if_fail (object->ref_count > 0, NULL);
3036 #ifdef G_ENABLE_DEBUG
3037 if (g_trap_object_ref == object)
3039 #endif /* G_ENABLE_DEBUG */
3042 old_val = g_atomic_int_add (&object->ref_count, 1);
3044 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3045 toggle_refs_notify (object, FALSE);
3047 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3054 * @object: (type GObject.Object): a #GObject
3056 * Decreases the reference count of @object. When its reference count
3057 * drops to 0, the object is finalized (i.e. its memory is freed).
3060 g_object_unref (gpointer _object)
3062 GObject *object = _object;
3065 g_return_if_fail (G_IS_OBJECT (object));
3066 g_return_if_fail (object->ref_count > 0);
3068 #ifdef G_ENABLE_DEBUG
3069 if (g_trap_object_ref == object)
3071 #endif /* G_ENABLE_DEBUG */
3073 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3074 retry_atomic_decrement1:
3075 old_ref = g_atomic_int_get (&object->ref_count);
3078 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3079 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3081 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3082 goto retry_atomic_decrement1;
3084 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3086 /* if we went from 2->1 we need to notify toggle refs if any */
3087 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3088 toggle_refs_notify (object, TRUE);
3092 GSList **weak_locations;
3094 /* The only way that this object can live at this point is if
3095 * there are outstanding weak references already established
3096 * before we got here.
3098 * If there were not already weak references then no more can be
3099 * established at this time, because the other thread would have
3100 * to hold a strong ref in order to call
3101 * g_object_add_weak_pointer() and then we wouldn't be here.
3103 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3105 if (weak_locations != NULL)
3107 g_rw_lock_writer_lock (&weak_locations_lock);
3109 /* It is possible that one of the weak references beat us to
3110 * the lock. Make sure the refcount is still what we expected
3113 old_ref = g_atomic_int_get (&object->ref_count);
3116 g_rw_lock_writer_unlock (&weak_locations_lock);
3117 goto retry_atomic_decrement1;
3120 /* We got the lock first, so the object will definitely die
3121 * now. Clear out all the weak references.
3123 while (*weak_locations)
3125 GWeakRef *weak_ref_location = (*weak_locations)->data;
3127 weak_ref_location->priv.p = NULL;
3128 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3131 g_rw_lock_writer_unlock (&weak_locations_lock);
3134 /* we are about to remove the last reference */
3135 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3136 G_OBJECT_GET_CLASS (object)->dispose (object);
3137 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3139 /* may have been re-referenced meanwhile */
3140 retry_atomic_decrement2:
3141 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3144 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3145 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3147 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3148 goto retry_atomic_decrement2;
3150 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3152 /* if we went from 2->1 we need to notify toggle refs if any */
3153 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3154 toggle_refs_notify (object, TRUE);
3159 /* we are still in the process of taking away the last ref */
3160 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3161 g_signal_handlers_destroy (object);
3162 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3164 /* decrement the last reference */
3165 old_ref = g_atomic_int_add (&object->ref_count, -1);
3167 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3169 /* may have been re-referenced meanwhile */
3170 if (G_LIKELY (old_ref == 1))
3172 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3173 G_OBJECT_GET_CLASS (object)->finalize (object);
3175 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3177 #ifdef G_ENABLE_DEBUG
3180 /* catch objects not chaining finalize handlers */
3181 G_LOCK (debug_objects);
3182 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3183 G_UNLOCK (debug_objects);
3185 #endif /* G_ENABLE_DEBUG */
3186 g_type_free_instance ((GTypeInstance*) object);
3192 * g_clear_object: (skip)
3193 * @object_ptr: a pointer to a #GObject reference
3195 * Clears a reference to a #GObject.
3197 * @object_ptr must not be %NULL.
3199 * If the reference is %NULL then this function does nothing.
3200 * Otherwise, the reference count of the object is decreased and the
3201 * pointer is set to %NULL.
3203 * This function is threadsafe and modifies the pointer atomically,
3204 * using memory barriers where needed.
3206 * A macro is also included that allows this function to be used without
3211 #undef g_clear_object
3213 g_clear_object (volatile GObject **object_ptr)
3215 g_clear_pointer (object_ptr, g_object_unref);
3219 * g_object_get_qdata:
3220 * @object: The GObject to get a stored user data pointer from
3221 * @quark: A #GQuark, naming the user data pointer
3223 * This function gets back user data pointers stored via
3224 * g_object_set_qdata().
3226 * Returns: (transfer none): The user data pointer set, or %NULL
3229 g_object_get_qdata (GObject *object,
3232 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3234 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3238 * g_object_set_qdata: (skip)
3239 * @object: The GObject to set store a user data pointer
3240 * @quark: A #GQuark, naming the user data pointer
3241 * @data: An opaque user data pointer
3243 * This sets an opaque, named pointer on an object.
3244 * The name is specified through a #GQuark (retrived e.g. via
3245 * g_quark_from_static_string()), and the pointer
3246 * can be gotten back from the @object with g_object_get_qdata()
3247 * until the @object is finalized.
3248 * Setting a previously set user data pointer, overrides (frees)
3249 * the old pointer set, using #NULL as pointer essentially
3250 * removes the data stored.
3253 g_object_set_qdata (GObject *object,
3257 g_return_if_fail (G_IS_OBJECT (object));
3258 g_return_if_fail (quark > 0);
3260 g_datalist_id_set_data (&object->qdata, quark, data);
3264 * g_object_dup_qdata:
3265 * @object: the #GObject to store user data on
3266 * @quark: a #GQuark, naming the user data pointer
3267 * @dup_func: (allow-none): function to dup the value
3268 * @user_data: (allow-none): passed as user_data to @dup_func
3270 * This is a variant of g_object_get_qdata() which returns
3271 * a 'duplicate' of the value. @dup_func defines the
3272 * meaning of 'duplicate' in this context, it could e.g.
3273 * take a reference on a ref-counted object.
3275 * If the @quark is not set on the object then @dup_func
3276 * will be called with a %NULL argument.
3278 * Note that @dup_func is called while user data of @object
3281 * This function can be useful to avoid races when multiple
3282 * threads are using object data on the same key on the same
3285 * Returns: the result of calling @dup_func on the value
3286 * associated with @quark on @object, or %NULL if not set.
3287 * If @dup_func is %NULL, the value is returned
3293 g_object_dup_qdata (GObject *object,
3295 GDuplicateFunc dup_func,
3298 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3299 g_return_val_if_fail (quark > 0, NULL);
3301 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3305 * g_object_replace_qdata:
3306 * @object: the #GObject to store user data on
3307 * @quark: a #GQuark, naming the user data pointer
3308 * @oldval: (allow-none): the old value to compare against
3309 * @newval: (allow-none): the new value
3310 * @destroy: (allow-none): a destroy notify for the new value
3311 * @old_destroy: (allow-none): destroy notify for the existing value
3313 * Compares the user data for the key @quark on @object with
3314 * @oldval, and if they are the same, replaces @oldval with
3317 * This is like a typical atomic compare-and-exchange
3318 * operation, for user data on an object.
3320 * If the previous value was replaced then ownership of the
3321 * old value (@oldval) is passed to the caller, including
3322 * the registered destroy notify for it (passed out in @old_destroy).
3323 * Its up to the caller to free this as he wishes, which may
3324 * or may not include using @old_destroy as sometimes replacement
3325 * should not destroy the object in the normal way.
3327 * Return: %TRUE if the existing value for @quark was replaced
3328 * by @newval, %FALSE otherwise.
3333 g_object_replace_qdata (GObject *object,
3337 GDestroyNotify destroy,
3338 GDestroyNotify *old_destroy)
3340 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3341 g_return_val_if_fail (quark > 0, FALSE);
3343 return g_datalist_id_replace_data (&object->qdata, quark,
3344 oldval, newval, destroy,
3349 * g_object_set_qdata_full: (skip)
3350 * @object: The GObject to set store a user data pointer
3351 * @quark: A #GQuark, naming the user data pointer
3352 * @data: An opaque user data pointer
3353 * @destroy: Function to invoke with @data as argument, when @data
3356 * This function works like g_object_set_qdata(), but in addition,
3357 * a void (*destroy) (gpointer) function may be specified which is
3358 * called with @data as argument when the @object is finalized, or
3359 * the data is being overwritten by a call to g_object_set_qdata()
3360 * with the same @quark.
3363 g_object_set_qdata_full (GObject *object,
3366 GDestroyNotify destroy)
3368 g_return_if_fail (G_IS_OBJECT (object));
3369 g_return_if_fail (quark > 0);
3371 g_datalist_id_set_data_full (&object->qdata, quark, data,
3372 data ? destroy : (GDestroyNotify) NULL);
3376 * g_object_steal_qdata:
3377 * @object: The GObject to get a stored user data pointer from
3378 * @quark: A #GQuark, naming the user data pointer
3380 * This function gets back user data pointers stored via
3381 * g_object_set_qdata() and removes the @data from object
3382 * without invoking its destroy() function (if any was
3384 * Usually, calling this function is only required to update
3385 * user data pointers with a destroy notifier, for example:
3388 * object_add_to_user_list (GObject *object,
3389 * const gchar *new_string)
3391 * // the quark, naming the object data
3392 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3393 * // retrive the old string list
3394 * GList *list = g_object_steal_qdata (object, quark_string_list);
3396 * // prepend new string
3397 * list = g_list_prepend (list, g_strdup (new_string));
3398 * // this changed 'list', so we need to set it again
3399 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3402 * free_string_list (gpointer data)
3404 * GList *node, *list = data;
3406 * for (node = list; node; node = node->next)
3407 * g_free (node->data);
3408 * g_list_free (list);
3411 * Using g_object_get_qdata() in the above example, instead of
3412 * g_object_steal_qdata() would have left the destroy function set,
3413 * and thus the partial string list would have been freed upon
3414 * g_object_set_qdata_full().
3416 * Returns: (transfer full): The user data pointer set, or %NULL
3419 g_object_steal_qdata (GObject *object,
3422 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3423 g_return_val_if_fail (quark > 0, NULL);
3425 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3429 * g_object_get_data:
3430 * @object: #GObject containing the associations
3431 * @key: name of the key for that association
3433 * Gets a named field from the objects table of associations (see g_object_set_data()).
3435 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3438 g_object_get_data (GObject *object,
3441 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3442 g_return_val_if_fail (key != NULL, NULL);
3444 return g_datalist_get_data (&object->qdata, key);
3448 * g_object_set_data:
3449 * @object: #GObject containing the associations.
3450 * @key: name of the key
3451 * @data: data to associate with that key
3453 * Each object carries around a table of associations from
3454 * strings to pointers. This function lets you set an association.
3456 * If the object already had an association with that name,
3457 * the old association will be destroyed.
3460 g_object_set_data (GObject *object,
3464 g_return_if_fail (G_IS_OBJECT (object));
3465 g_return_if_fail (key != NULL);
3467 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3471 * g_object_dup_data:
3472 * @object: the #GObject to store user data on
3473 * @key: a string, naming the user data pointer
3474 * @dup_func: (allow-none): function to dup the value
3475 * @user_data: (allow-none): passed as user_data to @dup_func
3477 * This is a variant of g_object_get_data() which returns
3478 * a 'duplicate' of the value. @dup_func defines the
3479 * meaning of 'duplicate' in this context, it could e.g.
3480 * take a reference on a ref-counted object.
3482 * If the @key is not set on the object then @dup_func
3483 * will be called with a %NULL argument.
3485 * Note that @dup_func is called while user data of @object
3488 * This function can be useful to avoid races when multiple
3489 * threads are using object data on the same key on the same
3492 * Returns: the result of calling @dup_func on the value
3493 * associated with @key on @object, or %NULL if not set.
3494 * If @dup_func is %NULL, the value is returned
3500 g_object_dup_data (GObject *object,
3502 GDuplicateFunc dup_func,
3505 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3506 g_return_val_if_fail (key != NULL, NULL);
3508 return g_datalist_id_dup_data (&object->qdata,
3509 g_quark_from_string (key),
3510 dup_func, user_data);
3514 * g_object_replace_data:
3515 * @object: the #GObject to store user data on
3516 * @key: a string, naming the user data pointer
3517 * @oldval: (allow-none): the old value to compare against
3518 * @newval: (allow-none): the new value
3519 * @destroy: (allow-none): a destroy notify for the new value
3520 * @old_destroy: (allow-none): destroy notify for the existing value
3522 * Compares the user data for the key @key on @object with
3523 * @oldval, and if they are the same, replaces @oldval with
3526 * This is like a typical atomic compare-and-exchange
3527 * operation, for user data on an object.
3529 * If the previous value was replaced then ownership of the
3530 * old value (@oldval) is passed to the caller, including
3531 * the registered destroy notify for it (passed out in @old_destroy).
3532 * Its up to the caller to free this as he wishes, which may
3533 * or may not include using @old_destroy as sometimes replacement
3534 * should not destroy the object in the normal way.
3536 * Return: %TRUE if the existing value for @key was replaced
3537 * by @newval, %FALSE otherwise.
3542 g_object_replace_data (GObject *object,
3546 GDestroyNotify destroy,
3547 GDestroyNotify *old_destroy)
3549 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3550 g_return_val_if_fail (key != NULL, FALSE);
3552 return g_datalist_id_replace_data (&object->qdata,
3553 g_quark_from_string (key),
3554 oldval, newval, destroy,
3559 * g_object_set_data_full: (skip)
3560 * @object: #GObject containing the associations
3561 * @key: name of the key
3562 * @data: data to associate with that key
3563 * @destroy: function to call when the association is destroyed
3565 * Like g_object_set_data() except it adds notification
3566 * for when the association is destroyed, either by setting it
3567 * to a different value or when the object is destroyed.
3569 * Note that the @destroy callback is not called if @data is %NULL.
3572 g_object_set_data_full (GObject *object,
3575 GDestroyNotify destroy)
3577 g_return_if_fail (G_IS_OBJECT (object));
3578 g_return_if_fail (key != NULL);
3580 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3581 data ? destroy : (GDestroyNotify) NULL);
3585 * g_object_steal_data:
3586 * @object: #GObject containing the associations
3587 * @key: name of the key
3589 * Remove a specified datum from the object's data associations,
3590 * without invoking the association's destroy handler.
3592 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3595 g_object_steal_data (GObject *object,
3600 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3601 g_return_val_if_fail (key != NULL, NULL);
3603 quark = g_quark_try_string (key);
3605 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3609 g_value_object_init (GValue *value)
3611 value->data[0].v_pointer = NULL;
3615 g_value_object_free_value (GValue *value)
3617 if (value->data[0].v_pointer)
3618 g_object_unref (value->data[0].v_pointer);
3622 g_value_object_copy_value (const GValue *src_value,
3625 if (src_value->data[0].v_pointer)
3626 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3628 dest_value->data[0].v_pointer = NULL;
3632 g_value_object_transform_value (const GValue *src_value,
3635 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)))
3636 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3638 dest_value->data[0].v_pointer = NULL;
3642 g_value_object_peek_pointer (const GValue *value)
3644 return value->data[0].v_pointer;
3648 g_value_object_collect_value (GValue *value,
3649 guint n_collect_values,
3650 GTypeCValue *collect_values,
3651 guint collect_flags)
3653 if (collect_values[0].v_pointer)
3655 GObject *object = collect_values[0].v_pointer;
3657 if (object->g_type_instance.g_class == NULL)
3658 return g_strconcat ("invalid unclassed object pointer for value type '",
3659 G_VALUE_TYPE_NAME (value),
3662 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3663 return g_strconcat ("invalid object type '",
3664 G_OBJECT_TYPE_NAME (object),
3665 "' for value type '",
3666 G_VALUE_TYPE_NAME (value),
3669 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3670 value->data[0].v_pointer = g_object_ref (object);
3673 value->data[0].v_pointer = NULL;
3679 g_value_object_lcopy_value (const GValue *value,
3680 guint n_collect_values,
3681 GTypeCValue *collect_values,
3682 guint collect_flags)
3684 GObject **object_p = collect_values[0].v_pointer;
3687 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3689 if (!value->data[0].v_pointer)
3691 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3692 *object_p = value->data[0].v_pointer;
3694 *object_p = g_object_ref (value->data[0].v_pointer);
3700 * g_value_set_object:
3701 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3702 * @v_object: (type GObject.Object) (allow-none): object value to be set
3704 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3706 * g_value_set_object() increases the reference count of @v_object
3707 * (the #GValue holds a reference to @v_object). If you do not wish
3708 * to increase the reference count of the object (i.e. you wish to
3709 * pass your current reference to the #GValue because you no longer
3710 * need it), use g_value_take_object() instead.
3712 * It is important that your #GValue holds a reference to @v_object (either its
3713 * own, or one it has taken) to ensure that the object won't be destroyed while
3714 * the #GValue still exists).
3717 g_value_set_object (GValue *value,
3722 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3724 old = value->data[0].v_pointer;
3728 g_return_if_fail (G_IS_OBJECT (v_object));
3729 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3731 value->data[0].v_pointer = v_object;
3732 g_object_ref (value->data[0].v_pointer);
3735 value->data[0].v_pointer = NULL;
3738 g_object_unref (old);
3742 * g_value_set_object_take_ownership: (skip)
3743 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3744 * @v_object: (allow-none): object value to be set
3746 * This is an internal function introduced mainly for C marshallers.
3748 * Deprecated: 2.4: Use g_value_take_object() instead.
3751 g_value_set_object_take_ownership (GValue *value,
3754 g_value_take_object (value, v_object);
3758 * g_value_take_object: (skip)
3759 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3760 * @v_object: (allow-none): object value to be set
3762 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3763 * and takes over the ownership of the callers reference to @v_object;
3764 * the caller doesn't have to unref it any more (i.e. the reference
3765 * count of the object is not increased).
3767 * If you want the #GValue to hold its own reference to @v_object, use
3768 * g_value_set_object() instead.
3773 g_value_take_object (GValue *value,
3776 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3778 if (value->data[0].v_pointer)
3780 g_object_unref (value->data[0].v_pointer);
3781 value->data[0].v_pointer = NULL;
3786 g_return_if_fail (G_IS_OBJECT (v_object));
3787 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3789 value->data[0].v_pointer = v_object; /* we take over the reference count */
3794 * g_value_get_object:
3795 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3797 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3799 * Returns: (type GObject.Object) (transfer none): object contents of @value
3802 g_value_get_object (const GValue *value)
3804 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3806 return value->data[0].v_pointer;
3810 * g_value_dup_object:
3811 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3813 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3814 * its reference count. If the contents of the #GValue are %NULL, then
3815 * %NULL will be returned.
3817 * Returns: (type GObject.Object) (transfer full): object content of @value,
3818 * should be unreferenced when no longer needed.
3821 g_value_dup_object (const GValue *value)
3823 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3825 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3829 * g_signal_connect_object: (skip)
3830 * @instance: the instance to connect to.
3831 * @detailed_signal: a string of the form "signal-name::detail".
3832 * @c_handler: the #GCallback to connect.
3833 * @gobject: the object to pass as data to @c_handler.
3834 * @connect_flags: a combination of #GConnectFlags.
3836 * This is similar to g_signal_connect_data(), but uses a closure which
3837 * ensures that the @gobject stays alive during the call to @c_handler
3838 * by temporarily adding a reference count to @gobject.
3840 * When the @gobject is destroyed the signal handler will be automatically
3841 * disconnected. Note that this is not currently threadsafe (ie:
3842 * emitting a signal while @gobject is being destroyed in another thread
3845 * Returns: the handler id.
3848 g_signal_connect_object (gpointer instance,
3849 const gchar *detailed_signal,
3850 GCallback c_handler,
3852 GConnectFlags connect_flags)
3854 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3855 g_return_val_if_fail (detailed_signal != NULL, 0);
3856 g_return_val_if_fail (c_handler != NULL, 0);
3862 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3864 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3866 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3869 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3875 GClosure *closures[1]; /* flexible array */
3877 /* don't change this structure without supplying an accessor for
3878 * watched closures, e.g.:
3879 * GSList* g_object_list_watched_closures (GObject *object)
3882 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3883 * carray = g_object_get_data (object, "GObject-closure-array");
3886 * GSList *slist = NULL;
3888 * for (i = 0; i < carray->n_closures; i++)
3889 * slist = g_slist_prepend (slist, carray->closures[i]);
3897 object_remove_closure (gpointer data,
3900 GObject *object = data;
3904 G_LOCK (closure_array_mutex);
3905 carray = g_object_get_qdata (object, quark_closure_array);
3906 for (i = 0; i < carray->n_closures; i++)
3907 if (carray->closures[i] == closure)
3909 carray->n_closures--;
3910 if (i < carray->n_closures)
3911 carray->closures[i] = carray->closures[carray->n_closures];
3912 G_UNLOCK (closure_array_mutex);
3915 G_UNLOCK (closure_array_mutex);
3916 g_assert_not_reached ();
3920 destroy_closure_array (gpointer data)
3922 CArray *carray = data;
3923 GObject *object = carray->object;
3924 guint i, n = carray->n_closures;
3926 for (i = 0; i < n; i++)
3928 GClosure *closure = carray->closures[i];
3930 /* removing object_remove_closure() upfront is probably faster than
3931 * letting it fiddle with quark_closure_array which is empty anyways
3933 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3934 g_closure_invalidate (closure);
3940 * g_object_watch_closure:
3941 * @object: GObject restricting lifetime of @closure
3942 * @closure: GClosure to watch
3944 * This function essentially limits the life time of the @closure to
3945 * the life time of the object. That is, when the object is finalized,
3946 * the @closure is invalidated by calling g_closure_invalidate() on
3947 * it, in order to prevent invocations of the closure with a finalized
3948 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3949 * added as marshal guards to the @closure, to ensure that an extra
3950 * reference count is held on @object during invocation of the
3951 * @closure. Usually, this function will be called on closures that
3952 * use this @object as closure data.
3955 g_object_watch_closure (GObject *object,
3961 g_return_if_fail (G_IS_OBJECT (object));
3962 g_return_if_fail (closure != NULL);
3963 g_return_if_fail (closure->is_invalid == FALSE);
3964 g_return_if_fail (closure->in_marshal == FALSE);
3965 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3967 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3968 g_closure_add_marshal_guards (closure,
3969 object, (GClosureNotify) g_object_ref,
3970 object, (GClosureNotify) g_object_unref);
3971 G_LOCK (closure_array_mutex);
3972 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3975 carray = g_renew (CArray, NULL, 1);
3976 carray->object = object;
3977 carray->n_closures = 1;
3982 i = carray->n_closures++;
3983 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3985 carray->closures[i] = closure;
3986 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3987 G_UNLOCK (closure_array_mutex);
3991 * g_closure_new_object:
3992 * @sizeof_closure: the size of the structure to allocate, must be at least
3993 * <literal>sizeof (GClosure)</literal>
3994 * @object: a #GObject pointer to store in the @data field of the newly
3995 * allocated #GClosure
3997 * A variant of g_closure_new_simple() which stores @object in the
3998 * @data field of the closure and calls g_object_watch_closure() on
3999 * @object and the created closure. This function is mainly useful
4000 * when implementing new types of closures.
4002 * Returns: (transfer full): a newly allocated #GClosure
4005 g_closure_new_object (guint sizeof_closure,
4010 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4011 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4013 closure = g_closure_new_simple (sizeof_closure, object);
4014 g_object_watch_closure (object, closure);
4020 * g_cclosure_new_object: (skip)
4021 * @callback_func: the function to invoke
4022 * @object: a #GObject pointer to pass to @callback_func
4024 * A variant of g_cclosure_new() which uses @object as @user_data and
4025 * calls g_object_watch_closure() on @object and the created
4026 * closure. This function is useful when you have a callback closely
4027 * associated with a #GObject, and want the callback to no longer run
4028 * after the object is is freed.
4030 * Returns: a new #GCClosure
4033 g_cclosure_new_object (GCallback callback_func,
4038 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4039 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4040 g_return_val_if_fail (callback_func != NULL, NULL);
4042 closure = g_cclosure_new (callback_func, object, NULL);
4043 g_object_watch_closure (object, closure);
4049 * g_cclosure_new_object_swap: (skip)
4050 * @callback_func: the function to invoke
4051 * @object: a #GObject pointer to pass to @callback_func
4053 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4054 * and calls g_object_watch_closure() on @object and the created
4055 * closure. This function is useful when you have a callback closely
4056 * associated with a #GObject, and want the callback to no longer run
4057 * after the object is is freed.
4059 * Returns: a new #GCClosure
4062 g_cclosure_new_object_swap (GCallback callback_func,
4067 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4068 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4069 g_return_val_if_fail (callback_func != NULL, NULL);
4071 closure = g_cclosure_new_swap (callback_func, object, NULL);
4072 g_object_watch_closure (object, closure);
4078 g_object_compat_control (gsize what,
4084 case 1: /* floating base type */
4085 return G_TYPE_INITIALLY_UNOWNED;
4086 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4087 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4089 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4091 *pp = floating_flag_handler;
4098 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
4101 g_initially_unowned_init (GInitiallyUnowned *object)
4103 g_object_force_floating (object);
4107 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4114 * A structure containing a weak reference to a #GObject. It can either
4115 * be empty (i.e. point to %NULL), or point to an object for as long as
4116 * at least one "strong" reference to that object exists. Before the
4117 * object's #GObjectClass.dispose method is called, every #GWeakRef
4118 * associated with becomes empty (i.e. points to %NULL).
4120 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4121 * heap-allocated, or embedded in larger structures.
4123 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4124 * reference is thread-safe: converting a weak pointer to a reference is
4125 * atomic with respect to invalidation of weak pointers to destroyed
4128 * If the object's #GObjectClass.dispose method results in additional
4129 * references to the object being held, any #GWeakRef<!-- -->s taken
4130 * before it was disposed will continue to point to %NULL. If
4131 * #GWeakRef<!-- -->s are taken after the object is disposed and
4132 * re-referenced, they will continue to point to it until its refcount
4133 * goes back to zero, at which point they too will be invalidated.
4137 * g_weak_ref_init: (skip)
4138 * @weak_ref: (inout): uninitialized or empty location for a weak
4140 * @object: (allow-none): a #GObject or %NULL
4142 * Initialise a non-statically-allocated #GWeakRef.
4144 * This function also calls g_weak_ref_set() with @object on the
4145 * freshly-initialised weak reference.
4147 * This function should always be matched with a call to
4148 * g_weak_ref_clear(). It is not necessary to use this function for a
4149 * #GWeakRef in static storage because it will already be
4150 * properly initialised. Just use g_weak_ref_set() directly.
4155 g_weak_ref_init (GWeakRef *weak_ref,
4158 weak_ref->priv.p = NULL;
4160 g_weak_ref_set (weak_ref, object);
4164 * g_weak_ref_clear: (skip)
4165 * @weak_ref: (inout): location of a weak reference, which
4168 * Frees resources associated with a non-statically-allocated #GWeakRef.
4169 * After this call, the #GWeakRef is left in an undefined state.
4171 * You should only call this on a #GWeakRef that previously had
4172 * g_weak_ref_init() called on it.
4177 g_weak_ref_clear (GWeakRef *weak_ref)
4179 g_weak_ref_set (weak_ref, NULL);
4182 weak_ref->priv.p = (void *) 0xccccccccu;
4186 * g_weak_ref_get: (skip)
4187 * @weak_ref: (inout): location of a weak reference to a #GObject
4189 * If @weak_ref is not empty, atomically acquire a strong
4190 * reference to the object it points to, and return that reference.
4192 * This function is needed because of the potential race between taking
4193 * the pointer value and g_object_ref() on it, if the object was losing
4194 * its last reference at the same time in a different thread.
4196 * The caller should release the resulting reference in the usual way,
4197 * by using g_object_unref().
4199 * Returns: (transfer full) (type GObject.Object): the object pointed to
4200 * by @weak_ref, or %NULL if it was empty
4205 g_weak_ref_get (GWeakRef *weak_ref)
4207 gpointer object_or_null;
4209 g_return_val_if_fail (weak_ref!= NULL, NULL);
4211 g_rw_lock_reader_lock (&weak_locations_lock);
4213 object_or_null = weak_ref->priv.p;
4215 if (object_or_null != NULL)
4216 g_object_ref (object_or_null);
4218 g_rw_lock_reader_unlock (&weak_locations_lock);
4220 return object_or_null;
4224 * g_weak_ref_set: (skip)
4225 * @weak_ref: location for a weak reference
4226 * @object: (allow-none): a #GObject or %NULL
4228 * Change the object to which @weak_ref points, or set it to
4231 * You must own a strong reference on @object while calling this
4237 g_weak_ref_set (GWeakRef *weak_ref,
4240 GSList **weak_locations;
4241 GObject *new_object;
4242 GObject *old_object;
4244 g_return_if_fail (weak_ref != NULL);
4245 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4247 new_object = object;
4249 g_rw_lock_writer_lock (&weak_locations_lock);
4251 /* We use the extra level of indirection here so that if we have ever
4252 * had a weak pointer installed at any point in time on this object,
4253 * we can see that there is a non-NULL value associated with the
4254 * weak-pointer quark and know that this value will not change at any
4255 * point in the object's lifetime.
4257 * Both properties are important for reducing the amount of times we
4258 * need to acquire locks and for decreasing the duration of time the
4259 * lock is held while avoiding some rather tricky races.
4261 * Specifically: we can avoid having to do an extra unconditional lock
4262 * in g_object_unref() without worrying about some extremely tricky
4266 old_object = weak_ref->priv.p;
4267 if (new_object != old_object)
4269 weak_ref->priv.p = new_object;
4271 /* Remove the weak ref from the old object */
4272 if (old_object != NULL)
4274 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4275 /* for it to point to an object, the object must have had it added once */
4276 g_assert (weak_locations != NULL);
4278 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4281 /* Add the weak ref to the new object */
4282 if (new_object != NULL)
4284 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4286 if (weak_locations == NULL)
4288 weak_locations = g_new0 (GSList *, 1);
4289 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4292 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4296 g_rw_lock_writer_unlock (&weak_locations_lock);