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, see <http://www.gnu.org/licenses/>.
19 * MT safe with regards to reference counting.
28 #include "gtype-private.h"
29 #include "gvaluecollector.h"
31 #include "gparamspecs.h"
32 #include "gvaluetypes.h"
33 #include "gobject_trace.h"
34 #include "gconstructor.h"
39 * @short_description: The base object type
40 * @see_also: #GParamSpecObject, g_param_spec_object()
42 * GObject is the fundamental type providing the common attributes and
43 * methods for all object types in GTK+, Pango and other libraries
44 * based on GObject. The GObject class provides methods for object
45 * construction and destruction, property access methods, and signal
46 * support. Signals are described in detail [here][gobject-Signals].
48 * ## Floating references # {#floating-ref}
50 * GInitiallyUnowned is derived from GObject. The only difference between
51 * the two is that the initial reference of a GInitiallyUnowned is flagged
52 * as a "floating" reference. This means that it is not specifically
53 * claimed to be "owned" by any code portion. The main motivation for
54 * providing floating references is C convenience. In particular, it
55 * allows code to be written as:
56 * |[<!-- language="C" -->
57 * container = create_container ();
58 * container_add_child (container, create_child());
60 * If container_add_child() calls g_object_ref_sink() on the passed-in child,
61 * no reference of the newly created child is leaked. Without floating
62 * references, container_add_child() can only g_object_ref() the new child,
63 * so to implement this code without reference leaks, it would have to be
65 * |[<!-- language="C" -->
67 * container = create_container ();
68 * child = create_child ();
69 * container_add_child (container, child);
70 * g_object_unref (child);
72 * The floating reference can be converted into an ordinary reference by
73 * calling g_object_ref_sink(). For already sunken objects (objects that
74 * don't have a floating reference anymore), g_object_ref_sink() is equivalent
75 * to g_object_ref() and returns a new reference.
77 * Since floating references are useful almost exclusively for C convenience,
78 * language bindings that provide automated reference and memory ownership
79 * maintenance (such as smart pointers or garbage collection) should not
80 * expose floating references in their API.
82 * Some object implementations may need to save an objects floating state
83 * across certain code portions (an example is #GtkMenu), to achieve this,
84 * the following sequence can be used:
86 * |[<!-- language="C" -->
87 * // save floating state
88 * gboolean was_floating = g_object_is_floating (object);
89 * g_object_ref_sink (object);
90 * // protected code portion
94 * // restore floating state
96 * g_object_force_floating (object);
98 * g_object_unref (object); // release previously acquired reference
104 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
105 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
107 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
108 #define OBJECT_HAS_TOGGLE_REF(object) \
109 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
110 #define OBJECT_FLOATING_FLAG 0x2
112 #define CLASS_HAS_PROPS_FLAG 0x1
113 #define CLASS_HAS_PROPS(class) \
114 ((class)->flags & CLASS_HAS_PROPS_FLAG)
115 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
116 ((class)->constructor != g_object_constructor)
117 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
118 ((class)->constructed != g_object_constructed)
120 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
121 #define CLASS_HAS_DERIVED_CLASS(class) \
122 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
124 /* --- signals --- */
131 /* --- properties --- */
137 /* --- prototypes --- */
138 static void g_object_base_class_init (GObjectClass *class);
139 static void g_object_base_class_finalize (GObjectClass *class);
140 static void g_object_do_class_init (GObjectClass *class);
141 static void g_object_init (GObject *object,
142 GObjectClass *class);
143 static GObject* g_object_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void g_object_constructed (GObject *object);
147 static void g_object_real_dispose (GObject *object);
148 static void g_object_finalize (GObject *object);
149 static void g_object_do_set_property (GObject *object,
153 static void g_object_do_get_property (GObject *object,
157 static void g_value_object_init (GValue *value);
158 static void g_value_object_free_value (GValue *value);
159 static void g_value_object_copy_value (const GValue *src_value,
161 static void g_value_object_transform_value (const GValue *src_value,
163 static gpointer g_value_object_peek_pointer (const GValue *value);
164 static gchar* g_value_object_collect_value (GValue *value,
165 guint n_collect_values,
166 GTypeCValue *collect_values,
167 guint collect_flags);
168 static gchar* g_value_object_lcopy_value (const GValue *value,
169 guint n_collect_values,
170 GTypeCValue *collect_values,
171 guint collect_flags);
172 static void g_object_dispatch_properties_changed (GObject *object,
174 GParamSpec **pspecs);
175 static guint object_floating_flag_handler (GObject *object,
178 static void object_interface_check_properties (gpointer check_data,
181 /* --- typedefs --- */
182 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
184 struct _GObjectNotifyQueue
188 guint16 freeze_count;
191 /* --- variables --- */
192 G_LOCK_DEFINE_STATIC (closure_array_mutex);
193 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
194 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
195 static GQuark quark_closure_array = 0;
196 static GQuark quark_weak_refs = 0;
197 static GQuark quark_toggle_refs = 0;
198 static GQuark quark_notify_queue;
199 static GQuark quark_in_construction;
200 static GParamSpecPool *pspec_pool = NULL;
201 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
202 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
203 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
204 static GQuark quark_weak_locations = 0;
205 static GRWLock weak_locations_lock;
207 G_LOCK_DEFINE_STATIC(notify_lock);
209 /* --- functions --- */
211 g_object_notify_queue_free (gpointer data)
213 GObjectNotifyQueue *nqueue = data;
215 g_slist_free (nqueue->pspecs);
216 g_slice_free (GObjectNotifyQueue, nqueue);
219 static GObjectNotifyQueue*
220 g_object_notify_queue_freeze (GObject *object,
221 gboolean conditional)
223 GObjectNotifyQueue *nqueue;
226 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
231 G_UNLOCK(notify_lock);
235 nqueue = g_slice_new0 (GObjectNotifyQueue);
236 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
237 nqueue, g_object_notify_queue_free);
240 if (nqueue->freeze_count >= 65535)
241 g_critical("Free queue for %s (%p) is larger than 65535,"
242 " called g_object_freeze_notify() too often."
243 " Forgot to call g_object_thaw_notify() or infinite loop",
244 G_OBJECT_TYPE_NAME (object), object);
246 nqueue->freeze_count++;
247 G_UNLOCK(notify_lock);
253 g_object_notify_queue_thaw (GObject *object,
254 GObjectNotifyQueue *nqueue)
256 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
260 g_return_if_fail (nqueue->freeze_count > 0);
261 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
265 /* Just make sure we never get into some nasty race condition */
266 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
267 G_UNLOCK(notify_lock);
268 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
269 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
273 nqueue->freeze_count--;
274 if (nqueue->freeze_count) {
275 G_UNLOCK(notify_lock);
279 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
281 for (slist = nqueue->pspecs; slist; slist = slist->next)
283 pspecs[n_pspecs++] = slist->data;
285 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
287 G_UNLOCK(notify_lock);
290 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
295 g_object_notify_queue_add (GObject *object,
296 GObjectNotifyQueue *nqueue,
301 g_return_if_fail (nqueue->n_pspecs < 65535);
303 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
305 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
309 G_UNLOCK(notify_lock);
312 #ifdef G_ENABLE_DEBUG
313 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
314 G_LOCK_DEFINE_STATIC (debug_objects);
315 static guint debug_objects_count = 0;
316 static GHashTable *debug_objects_ht = NULL;
319 debug_objects_foreach (gpointer key,
323 GObject *object = value;
325 g_message ("[%p] stale %s\tref_count=%u",
327 G_OBJECT_TYPE_NAME (object),
331 #ifdef G_HAS_CONSTRUCTORS
332 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
333 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
335 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
336 #endif /* G_HAS_CONSTRUCTORS */
339 debug_objects_atexit (void)
343 G_LOCK (debug_objects);
344 g_message ("stale GObjects: %u", debug_objects_count);
345 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
346 G_UNLOCK (debug_objects);
349 #endif /* G_ENABLE_DEBUG */
352 _g_object_type_init (void)
354 static gboolean initialized = FALSE;
355 static const GTypeFundamentalInfo finfo = {
356 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
359 sizeof (GObjectClass),
360 (GBaseInitFunc) g_object_base_class_init,
361 (GBaseFinalizeFunc) g_object_base_class_finalize,
362 (GClassInitFunc) g_object_do_class_init,
363 NULL /* class_destroy */,
364 NULL /* class_data */,
367 (GInstanceInitFunc) g_object_init,
368 NULL, /* value_table */
370 static const GTypeValueTable value_table = {
371 g_value_object_init, /* value_init */
372 g_value_object_free_value, /* value_free */
373 g_value_object_copy_value, /* value_copy */
374 g_value_object_peek_pointer, /* value_peek_pointer */
375 "p", /* collect_format */
376 g_value_object_collect_value, /* collect_value */
377 "p", /* lcopy_format */
378 g_value_object_lcopy_value, /* lcopy_value */
382 g_return_if_fail (initialized == FALSE);
387 info.value_table = &value_table;
388 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
389 g_assert (type == G_TYPE_OBJECT);
390 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
392 #ifdef G_ENABLE_DEBUG
395 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
396 #ifndef G_HAS_CONSTRUCTORS
397 g_atexit (debug_objects_atexit);
398 #endif /* G_HAS_CONSTRUCTORS */
400 #endif /* G_ENABLE_DEBUG */
404 g_object_base_class_init (GObjectClass *class)
406 GObjectClass *pclass = g_type_class_peek_parent (class);
408 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
409 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
412 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
414 /* reset instance specific fields and methods that don't get inherited */
415 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
416 class->get_property = NULL;
417 class->set_property = NULL;
421 g_object_base_class_finalize (GObjectClass *class)
425 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
427 g_slist_free (class->construct_properties);
428 class->construct_properties = NULL;
429 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
430 for (node = list; node; node = node->next)
432 GParamSpec *pspec = node->data;
434 g_param_spec_pool_remove (pspec_pool, pspec);
435 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
436 g_param_spec_unref (pspec);
442 g_object_do_class_init (GObjectClass *class)
444 /* read the comment about typedef struct CArray; on why not to change this quark */
445 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
447 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
448 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
449 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
450 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
451 quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
452 pspec_pool = g_param_spec_pool_new (TRUE);
454 class->constructor = g_object_constructor;
455 class->constructed = g_object_constructed;
456 class->set_property = g_object_do_set_property;
457 class->get_property = g_object_do_get_property;
458 class->dispose = g_object_real_dispose;
459 class->finalize = g_object_finalize;
460 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
461 class->notify = NULL;
465 * @gobject: the object which received the signal.
466 * @pspec: the #GParamSpec of the property which changed.
468 * The notify signal is emitted on an object when one of its
469 * properties has been changed. Note that getting this signal
470 * doesn't guarantee that the value of the property has actually
471 * changed, it may also be emitted when the setter for the property
472 * is called to reinstate the previous value.
474 * This signal is typically used to obtain change notification for a
475 * single property, by specifying the property name as a detail in the
476 * g_signal_connect() call, like this:
477 * |[<!-- language="C" -->
478 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
479 * G_CALLBACK (gtk_text_view_target_list_notify),
482 * It is important to note that you must use
483 * [canonical][canonical-parameter-name] parameter names as
484 * detail strings for the notify signal.
486 gobject_signals[NOTIFY] =
487 g_signal_new (g_intern_static_string ("notify"),
488 G_TYPE_FROM_CLASS (class),
489 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
490 G_STRUCT_OFFSET (GObjectClass, notify),
492 g_cclosure_marshal_VOID__PARAM,
496 /* Install a check function that we'll use to verify that classes that
497 * implement an interface implement all properties for that interface
499 g_type_add_interface_check (NULL, object_interface_check_properties);
503 install_property_internal (GType g_type,
507 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
509 g_warning ("When installing property: type '%s' already has a property named '%s'",
510 g_type_name (g_type),
515 g_param_spec_ref_sink (pspec);
516 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
517 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
521 * g_object_class_install_property:
522 * @oclass: a #GObjectClass
523 * @property_id: the id for the new property
524 * @pspec: the #GParamSpec for the new property
526 * Installs a new property. This is usually done in the class initializer.
528 * Note that it is possible to redefine a property in a derived class,
529 * by installing a property with the same name. This can be useful at times,
530 * e.g. to change the range of allowed values or the default value.
533 g_object_class_install_property (GObjectClass *class,
537 g_return_if_fail (G_IS_OBJECT_CLASS (class));
538 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
540 if (CLASS_HAS_DERIVED_CLASS (class))
541 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
543 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (class)))
544 g_warning ("Attempt to add property %s::%s after class was initialised", G_OBJECT_CLASS_NAME (class), pspec->name);
546 class->flags |= CLASS_HAS_PROPS_FLAG;
548 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
549 if (pspec->flags & G_PARAM_WRITABLE)
550 g_return_if_fail (class->set_property != NULL);
551 if (pspec->flags & G_PARAM_READABLE)
552 g_return_if_fail (class->get_property != NULL);
553 g_return_if_fail (property_id > 0);
554 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
555 if (pspec->flags & G_PARAM_CONSTRUCT)
556 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
557 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
558 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
560 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
562 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
563 class->construct_properties = g_slist_append (class->construct_properties, pspec);
565 /* for property overrides of construct properties, we have to get rid
566 * of the overidden inherited construct property
568 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
569 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
570 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
574 * g_object_class_install_properties:
575 * @oclass: a #GObjectClass
576 * @n_pspecs: the length of the #GParamSpecs array
577 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
578 * defining the new properties
580 * Installs new properties from an array of #GParamSpecs. This is
581 * usually done in the class initializer.
583 * The property id of each property is the index of each #GParamSpec in
586 * The property id of 0 is treated specially by #GObject and it should not
587 * be used to store a #GParamSpec.
589 * This function should be used if you plan to use a static array of
590 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
591 * class initialization:
593 * |[<!-- language="C" -->
595 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
598 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
601 * my_object_class_init (MyObjectClass *klass)
603 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
605 * obj_properties[PROP_FOO] =
606 * g_param_spec_int ("foo", "Foo", "Foo",
609 * G_PARAM_READWRITE);
611 * obj_properties[PROP_BAR] =
612 * g_param_spec_string ("bar", "Bar", "Bar",
614 * G_PARAM_READWRITE);
616 * gobject_class->set_property = my_object_set_property;
617 * gobject_class->get_property = my_object_get_property;
618 * g_object_class_install_properties (gobject_class,
624 * allows calling g_object_notify_by_pspec() to notify of property changes:
626 * |[<!-- language="C" -->
628 * my_object_set_foo (MyObject *self, gint foo)
630 * if (self->foo != foo)
633 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
641 g_object_class_install_properties (GObjectClass *oclass,
645 GType oclass_type, parent_type;
648 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
649 g_return_if_fail (n_pspecs > 1);
650 g_return_if_fail (pspecs[0] == NULL);
652 if (CLASS_HAS_DERIVED_CLASS (oclass))
653 g_error ("Attempt to add properties to %s after it was derived",
654 G_OBJECT_CLASS_NAME (oclass));
656 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (oclass)))
657 g_warning ("Attempt to add properties to %s after it was initialised", G_OBJECT_CLASS_NAME (oclass));
659 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
660 parent_type = g_type_parent (oclass_type);
662 /* we skip the first element of the array as it would have a 0 prop_id */
663 for (i = 1; i < n_pspecs; i++)
665 GParamSpec *pspec = pspecs[i];
667 g_return_if_fail (pspec != NULL);
669 if (pspec->flags & G_PARAM_WRITABLE)
670 g_return_if_fail (oclass->set_property != NULL);
671 if (pspec->flags & G_PARAM_READABLE)
672 g_return_if_fail (oclass->get_property != NULL);
673 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
674 if (pspec->flags & G_PARAM_CONSTRUCT)
675 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
676 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
677 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
679 oclass->flags |= CLASS_HAS_PROPS_FLAG;
680 install_property_internal (oclass_type, i, pspec);
682 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
683 oclass->construct_properties = g_slist_append (oclass->construct_properties, pspec);
685 /* for property overrides of construct properties, we have to get rid
686 * of the overidden inherited construct property
688 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
689 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
690 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
695 * g_object_interface_install_property:
696 * @g_iface: any interface vtable for the interface, or the default
697 * vtable for the interface.
698 * @pspec: the #GParamSpec for the new property
700 * Add a property to an interface; this is only useful for interfaces
701 * that are added to GObject-derived types. Adding a property to an
702 * interface forces all objects classes with that interface to have a
703 * compatible property. The compatible property could be a newly
704 * created #GParamSpec, but normally
705 * g_object_class_override_property() will be used so that the object
706 * class only needs to provide an implementation and inherits the
707 * property description, default value, bounds, and so forth from the
708 * interface property.
710 * This function is meant to be called from the interface's default
711 * vtable initialization function (the @class_init member of
712 * #GTypeInfo.) It must not be called after after @class_init has
713 * been called for any object types implementing this interface.
718 g_object_interface_install_property (gpointer g_iface,
721 GTypeInterface *iface_class = g_iface;
723 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
724 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
725 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
726 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
728 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
729 if (pspec->flags & G_PARAM_CONSTRUCT)
730 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
731 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
732 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
734 install_property_internal (iface_class->g_type, 0, pspec);
738 * g_object_class_find_property:
739 * @oclass: a #GObjectClass
740 * @property_name: the name of the property to look up
742 * Looks up the #GParamSpec for a property of a class.
744 * Returns: (transfer none): the #GParamSpec for the property, or
745 * %NULL if the class doesn't have a property of that name
748 g_object_class_find_property (GObjectClass *class,
749 const gchar *property_name)
752 GParamSpec *redirect;
754 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
755 g_return_val_if_fail (property_name != NULL, NULL);
757 pspec = g_param_spec_pool_lookup (pspec_pool,
759 G_OBJECT_CLASS_TYPE (class),
763 redirect = g_param_spec_get_redirect_target (pspec);
774 * g_object_interface_find_property:
775 * @g_iface: any interface vtable for the interface, or the default
776 * vtable for the interface
777 * @property_name: name of a property to lookup.
779 * Find the #GParamSpec with the given name for an
780 * interface. Generally, the interface vtable passed in as @g_iface
781 * will be the default vtable from g_type_default_interface_ref(), or,
782 * if you know the interface has already been loaded,
783 * g_type_default_interface_peek().
787 * Returns: (transfer none): the #GParamSpec for the property of the
788 * interface with the name @property_name, or %NULL if no
789 * such property exists.
792 g_object_interface_find_property (gpointer g_iface,
793 const gchar *property_name)
795 GTypeInterface *iface_class = g_iface;
797 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
798 g_return_val_if_fail (property_name != NULL, NULL);
800 return g_param_spec_pool_lookup (pspec_pool,
807 * g_object_class_override_property:
808 * @oclass: a #GObjectClass
809 * @property_id: the new property ID
810 * @name: the name of a property registered in a parent class or
811 * in an interface of this class.
813 * Registers @property_id as referring to a property with the name
814 * @name in a parent class or in an interface implemented by @oclass.
815 * This allows this class to "override" a property implementation in
816 * a parent class or to provide the implementation of a property from
819 * Internally, overriding is implemented by creating a property of type
820 * #GParamSpecOverride; generally operations that query the properties of
821 * the object class, such as g_object_class_find_property() or
822 * g_object_class_list_properties() will return the overridden
823 * property. However, in one case, the @construct_properties argument of
824 * the @constructor virtual function, the #GParamSpecOverride is passed
825 * instead, so that the @param_id field of the #GParamSpec will be
826 * correct. For virtually all uses, this makes no difference. If you
827 * need to get the overridden property, you can call
828 * g_param_spec_get_redirect_target().
833 g_object_class_override_property (GObjectClass *oclass,
837 GParamSpec *overridden = NULL;
841 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
842 g_return_if_fail (property_id > 0);
843 g_return_if_fail (name != NULL);
845 /* Find the overridden property; first check parent types
847 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
848 if (parent_type != G_TYPE_NONE)
849 overridden = g_param_spec_pool_lookup (pspec_pool,
858 /* Now check interfaces
860 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
861 while (n_ifaces-- && !overridden)
863 overridden = g_param_spec_pool_lookup (pspec_pool,
874 g_warning ("%s: Can't find property to override for '%s::%s'",
875 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
879 new = g_param_spec_override (name, overridden);
880 g_object_class_install_property (oclass, property_id, new);
884 * g_object_class_list_properties:
885 * @oclass: a #GObjectClass
886 * @n_properties: (out): return location for the length of the returned array
888 * Get an array of #GParamSpec* for all properties of a class.
890 * Returns: (array length=n_properties) (transfer container): an array of
891 * #GParamSpec* which should be freed after use
893 GParamSpec** /* free result */
894 g_object_class_list_properties (GObjectClass *class,
895 guint *n_properties_p)
900 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
902 pspecs = g_param_spec_pool_list (pspec_pool,
903 G_OBJECT_CLASS_TYPE (class),
912 * g_object_interface_list_properties:
913 * @g_iface: any interface vtable for the interface, or the default
914 * vtable for the interface
915 * @n_properties_p: (out): location to store number of properties returned.
917 * Lists the properties of an interface.Generally, the interface
918 * vtable passed in as @g_iface will be the default vtable from
919 * g_type_default_interface_ref(), or, if you know the interface has
920 * already been loaded, g_type_default_interface_peek().
924 * Returns: (array length=n_properties_p) (transfer container): a
925 * pointer to an array of pointers to #GParamSpec
926 * structures. The paramspecs are owned by GLib, but the
927 * array should be freed with g_free() when you are done with
931 g_object_interface_list_properties (gpointer g_iface,
932 guint *n_properties_p)
934 GTypeInterface *iface_class = g_iface;
938 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
940 pspecs = g_param_spec_pool_list (pspec_pool,
949 static inline gboolean
950 object_in_construction (GObject *object)
952 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
956 g_object_init (GObject *object,
959 object->ref_count = 1;
960 object->qdata = NULL;
962 if (CLASS_HAS_PROPS (class))
964 /* freeze object's notification queue, g_object_newv() preserves pairedness */
965 g_object_notify_queue_freeze (object, FALSE);
968 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
970 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
971 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
974 #ifdef G_ENABLE_DEBUG
977 G_LOCK (debug_objects);
978 debug_objects_count++;
979 g_hash_table_insert (debug_objects_ht, object, object);
980 G_UNLOCK (debug_objects);
982 #endif /* G_ENABLE_DEBUG */
986 g_object_do_set_property (GObject *object,
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1000 g_object_do_get_property (GObject *object,
1005 switch (property_id)
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1014 g_object_real_dispose (GObject *object)
1016 g_signal_handlers_destroy (object);
1017 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1018 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1022 g_object_finalize (GObject *object)
1024 if (object_in_construction (object))
1026 g_critical ("object %s %p finalized while still in-construction",
1027 G_OBJECT_TYPE_NAME (object), object);
1030 g_datalist_clear (&object->qdata);
1032 #ifdef G_ENABLE_DEBUG
1035 G_LOCK (debug_objects);
1036 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1037 g_hash_table_remove (debug_objects_ht, object);
1038 debug_objects_count--;
1039 G_UNLOCK (debug_objects);
1041 #endif /* G_ENABLE_DEBUG */
1046 g_object_dispatch_properties_changed (GObject *object,
1048 GParamSpec **pspecs)
1052 for (i = 0; i < n_pspecs; i++)
1053 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1057 * g_object_run_dispose:
1058 * @object: a #GObject
1060 * Releases all references to other objects. This can be used to break
1063 * This functions should only be called from object system implementations.
1066 g_object_run_dispose (GObject *object)
1068 g_return_if_fail (G_IS_OBJECT (object));
1069 g_return_if_fail (object->ref_count > 0);
1071 g_object_ref (object);
1072 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1073 G_OBJECT_GET_CLASS (object)->dispose (object);
1074 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1075 g_object_unref (object);
1079 * g_object_freeze_notify:
1080 * @object: a #GObject
1082 * Increases the freeze count on @object. If the freeze count is
1083 * non-zero, the emission of "notify" signals on @object is
1084 * stopped. The signals are queued until the freeze count is decreased
1085 * to zero. Duplicate notifications are squashed so that at most one
1086 * #GObject::notify signal is emitted for each property modified while the
1089 * This is necessary for accessors that modify multiple properties to prevent
1090 * premature notification while the object is still being modified.
1093 g_object_freeze_notify (GObject *object)
1095 g_return_if_fail (G_IS_OBJECT (object));
1097 if (g_atomic_int_get (&object->ref_count) == 0)
1100 g_object_ref (object);
1101 g_object_notify_queue_freeze (object, FALSE);
1102 g_object_unref (object);
1106 get_notify_pspec (GParamSpec *pspec)
1108 GParamSpec *redirected;
1110 /* we don't notify on non-READABLE parameters */
1111 if (~pspec->flags & G_PARAM_READABLE)
1114 /* if the paramspec is redirected, notify on the target */
1115 redirected = g_param_spec_get_redirect_target (pspec);
1116 if (redirected != NULL)
1119 /* else, notify normally */
1124 g_object_notify_by_spec_internal (GObject *object,
1127 GParamSpec *notify_pspec;
1129 notify_pspec = get_notify_pspec (pspec);
1131 if (notify_pspec != NULL)
1133 GObjectNotifyQueue *nqueue;
1135 /* conditional freeze: only increase freeze count if already frozen */
1136 nqueue = g_object_notify_queue_freeze (object, TRUE);
1140 /* we're frozen, so add to the queue and release our freeze */
1141 g_object_notify_queue_add (object, nqueue, notify_pspec);
1142 g_object_notify_queue_thaw (object, nqueue);
1145 /* not frozen, so just dispatch the notification directly */
1146 G_OBJECT_GET_CLASS (object)
1147 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1153 * @object: a #GObject
1154 * @property_name: the name of a property installed on the class of @object.
1156 * Emits a "notify" signal for the property @property_name on @object.
1158 * When possible, eg. when signaling a property change from within the class
1159 * that registered the property, you should use g_object_notify_by_pspec()
1162 * Note that emission of the notify signal may be blocked with
1163 * g_object_freeze_notify(). In this case, the signal emissions are queued
1164 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1168 g_object_notify (GObject *object,
1169 const gchar *property_name)
1173 g_return_if_fail (G_IS_OBJECT (object));
1174 g_return_if_fail (property_name != NULL);
1175 if (g_atomic_int_get (&object->ref_count) == 0)
1178 g_object_ref (object);
1179 /* We don't need to get the redirect target
1180 * (by, e.g. calling g_object_class_find_property())
1181 * because g_object_notify_queue_add() does that
1183 pspec = g_param_spec_pool_lookup (pspec_pool,
1185 G_OBJECT_TYPE (object),
1189 g_warning ("%s: object class '%s' has no property named '%s'",
1191 G_OBJECT_TYPE_NAME (object),
1194 g_object_notify_by_spec_internal (object, pspec);
1195 g_object_unref (object);
1199 * g_object_notify_by_pspec:
1200 * @object: a #GObject
1201 * @pspec: the #GParamSpec of a property installed on the class of @object.
1203 * Emits a "notify" signal for the property specified by @pspec on @object.
1205 * This function omits the property name lookup, hence it is faster than
1206 * g_object_notify().
1208 * One way to avoid using g_object_notify() from within the
1209 * class that registered the properties, and using g_object_notify_by_pspec()
1210 * instead, is to store the GParamSpec used with
1211 * g_object_class_install_property() inside a static array, e.g.:
1213 *|[<!-- language="C" -->
1221 * static GParamSpec *properties[PROP_LAST];
1224 * my_object_class_init (MyObjectClass *klass)
1226 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1229 * G_PARAM_READWRITE);
1230 * g_object_class_install_property (gobject_class,
1232 * properties[PROP_FOO]);
1236 * and then notify a change on the "foo" property with:
1238 * |[<!-- language="C" -->
1239 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1245 g_object_notify_by_pspec (GObject *object,
1249 g_return_if_fail (G_IS_OBJECT (object));
1250 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1252 if (g_atomic_int_get (&object->ref_count) == 0)
1255 g_object_ref (object);
1256 g_object_notify_by_spec_internal (object, pspec);
1257 g_object_unref (object);
1261 * g_object_thaw_notify:
1262 * @object: a #GObject
1264 * Reverts the effect of a previous call to
1265 * g_object_freeze_notify(). The freeze count is decreased on @object
1266 * and when it reaches zero, queued "notify" signals are emitted.
1268 * Duplicate notifications for each property are squashed so that at most one
1269 * #GObject::notify signal is emitted for each property, in the reverse order
1270 * in which they have been queued.
1272 * It is an error to call this function when the freeze count is zero.
1275 g_object_thaw_notify (GObject *object)
1277 GObjectNotifyQueue *nqueue;
1279 g_return_if_fail (G_IS_OBJECT (object));
1280 if (g_atomic_int_get (&object->ref_count) == 0)
1283 g_object_ref (object);
1285 /* FIXME: Freezing is the only way to get at the notify queue.
1286 * So we freeze once and then thaw twice.
1288 nqueue = g_object_notify_queue_freeze (object, FALSE);
1289 g_object_notify_queue_thaw (object, nqueue);
1290 g_object_notify_queue_thaw (object, nqueue);
1292 g_object_unref (object);
1296 object_get_property (GObject *object,
1300 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1301 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1302 GParamSpec *redirect;
1306 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1307 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1311 redirect = g_param_spec_get_redirect_target (pspec);
1315 class->get_property (object, param_id, value, pspec);
1319 object_set_property (GObject *object,
1321 const GValue *value,
1322 GObjectNotifyQueue *nqueue)
1324 GValue tmp_value = G_VALUE_INIT;
1325 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1326 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1327 GParamSpec *redirect;
1328 static const gchar * enable_diagnostic = NULL;
1332 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1333 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1337 redirect = g_param_spec_get_redirect_target (pspec);
1341 if (G_UNLIKELY (!enable_diagnostic))
1343 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1344 if (!enable_diagnostic)
1345 enable_diagnostic = "0";
1348 if (enable_diagnostic[0] == '1')
1350 if (pspec->flags & G_PARAM_DEPRECATED)
1352 /* don't warn for automatically provided construct properties */
1353 if (!(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) ||
1354 !object_in_construction (object))
1356 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1357 "anymore. It will be removed in a future version.",
1358 G_OBJECT_TYPE_NAME (object), pspec->name);
1363 /* provide a copy to work from, convert (if necessary) and validate */
1364 g_value_init (&tmp_value, pspec->value_type);
1365 if (!g_value_transform (value, &tmp_value))
1366 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1368 g_type_name (pspec->value_type),
1369 G_VALUE_TYPE_NAME (value));
1370 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1372 gchar *contents = g_strdup_value_contents (value);
1374 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1376 G_VALUE_TYPE_NAME (value),
1378 g_type_name (pspec->value_type));
1383 GParamSpec *notify_pspec;
1385 class->set_property (object, param_id, &tmp_value, pspec);
1387 notify_pspec = get_notify_pspec (pspec);
1389 if (notify_pspec != NULL)
1390 g_object_notify_queue_add (object, nqueue, notify_pspec);
1392 g_value_unset (&tmp_value);
1396 object_interface_check_properties (gpointer check_data,
1399 GTypeInterface *iface_class = g_iface;
1400 GObjectClass *class;
1401 GType iface_type = iface_class->g_type;
1402 GParamSpec **pspecs;
1405 class = g_type_class_ref (iface_class->g_instance_type);
1410 if (!G_IS_OBJECT_CLASS (class))
1413 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1417 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1419 G_OBJECT_CLASS_TYPE (class),
1424 g_critical ("Object class %s doesn't implement property "
1425 "'%s' from interface '%s'",
1426 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1428 g_type_name (iface_type));
1433 /* We do a number of checks on the properties of an interface to
1434 * make sure that all classes implementing the interface are
1435 * overriding the properties in a sane way.
1437 * We do the checks in order of importance so that we can give
1438 * more useful error messages first.
1440 * First, we check that the implementation doesn't remove the
1441 * basic functionality (readability, writability) advertised by
1442 * the interface. Next, we check that it doesn't introduce
1443 * additional restrictions (such as construct-only). Finally, we
1444 * make sure the types are compatible.
1447 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1448 /* If the property on the interface is readable then the
1449 * implementation must be readable. If the interface is writable
1450 * then the implementation must be writable.
1452 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1454 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1455 "property on interface '%s'\n", pspecs[n]->name,
1456 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1460 /* If the property on the interface is writable then we need to
1461 * make sure the implementation doesn't introduce new restrictions
1462 * on that writability (ie: construct-only).
1464 * If the interface was not writable to begin with then we don't
1465 * really have any problems here because "writable at construct
1466 * type only" is still more permissive than "read only".
1468 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1470 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1472 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1473 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1474 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1480 /* If the property on the interface is readable then we are
1481 * effectively advertising that reading the property will return a
1482 * value of a specific type. All implementations of the interface
1483 * need to return items of this type -- but may be more
1484 * restrictive. For example, it is legal to have:
1486 * GtkWidget *get_item();
1488 * that is implemented by a function that always returns a
1489 * GtkEntry. In short: readability implies that the
1490 * implementation value type must be equal or more restrictive.
1492 * Similarly, if the property on the interface is writable then
1493 * must be able to accept the property being set to any value of
1494 * that type, including subclasses. In this case, we may also be
1495 * less restrictive. For example, it is legal to have:
1497 * set_item (GtkEntry *);
1499 * that is implemented by a function that will actually work with
1500 * any GtkWidget. In short: writability implies that the
1501 * implementation value type must be equal or less restrictive.
1503 * In the case that the property is both readable and writable
1504 * then the only way that both of the above can be satisfied is
1505 * with a type that is exactly equal.
1507 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1509 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1510 /* class pspec value type must have exact equality with interface */
1511 if (pspecs[n]->value_type != class_pspec->value_type)
1512 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1513 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1514 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1515 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1518 case G_PARAM_READABLE:
1519 /* class pspec value type equal or more restrictive than interface */
1520 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1521 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1522 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1523 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1524 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1527 case G_PARAM_WRITABLE:
1528 /* class pspec value type equal or less restrictive than interface */
1529 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1530 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1531 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1532 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1533 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1537 g_assert_not_reached ();
1544 g_type_class_unref (class);
1548 g_object_get_type (void)
1550 return G_TYPE_OBJECT;
1554 * g_object_new: (skip)
1555 * @object_type: the type id of the #GObject subtype to instantiate
1556 * @first_property_name: the name of the first property
1557 * @...: the value of the first property, followed optionally by more
1558 * name/value pairs, followed by %NULL
1560 * Creates a new instance of a #GObject subtype and sets its properties.
1562 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1563 * which are not explicitly specified are set to their default values.
1565 * Returns: (transfer full): a new instance of @object_type
1568 g_object_new (GType object_type,
1569 const gchar *first_property_name,
1575 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1577 /* short circuit for calls supplying no properties */
1578 if (!first_property_name)
1579 return g_object_newv (object_type, 0, NULL);
1581 va_start (var_args, first_property_name);
1582 object = g_object_new_valist (object_type, first_property_name, var_args);
1589 g_object_new_with_custom_constructor (GObjectClass *class,
1590 GObjectConstructParam *params,
1593 GObjectNotifyQueue *nqueue = NULL;
1594 gboolean newly_constructed;
1595 GObjectConstructParam *cparams;
1603 /* If we have ->constructed() then we have to do a lot more work.
1604 * It's possible that this is a singleton and it's also possible
1605 * that the user's constructor() will attempt to modify the values
1606 * that we pass in, so we'll need to allocate copies of them.
1607 * It's also possible that the user may attempt to call
1608 * g_object_set() from inside of their constructor, so we need to
1609 * add ourselves to a list of objects for which that is allowed
1610 * while their constructor() is running.
1613 /* Create the array of GObjectConstructParams for constructor() */
1614 n_cparams = g_slist_length (class->construct_properties);
1615 cparams = g_new (GObjectConstructParam, n_cparams);
1616 cvalues = g_new0 (GValue, n_cparams);
1620 /* As above, we may find the value in the passed-in params list.
1622 * If we have the value passed in then we can use the GValue from
1623 * it directly because it is safe to modify. If we use the
1624 * default value from the class, we had better not pass that in
1625 * and risk it being modified, so we create a new one.
1627 for (node = class->construct_properties; node; node = node->next)
1634 value = NULL; /* to silence gcc... */
1636 for (j = 0; j < n_params; j++)
1637 if (params[j].pspec == pspec)
1639 value = params[j].value;
1645 value = &cvalues[cvals_used++];
1646 g_value_init (value, pspec->value_type);
1647 g_param_value_set_default (pspec, value);
1650 cparams[i].pspec = pspec;
1651 cparams[i].value = value;
1655 /* construct object from construction parameters */
1656 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1657 /* free construction values */
1659 while (cvals_used--)
1660 g_value_unset (&cvalues[cvals_used]);
1663 /* There is code in the wild that relies on being able to return NULL
1664 * from its custom constructor. This was never a supported operation,
1665 * but since the code is already out there...
1669 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1670 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1674 /* g_object_init() will have marked the object as being in-construction.
1675 * Check if the returned object still is so marked, or if this is an
1676 * already-existing singleton (in which case we should not do 'constructed').
1678 newly_constructed = object_in_construction (object);
1679 if (newly_constructed)
1680 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1682 if (CLASS_HAS_PROPS (class))
1684 /* If this object was newly_constructed then g_object_init()
1685 * froze the queue. We need to freeze it here in order to get
1686 * the handle so that we can thaw it below (otherwise it will
1687 * be frozen forever).
1689 * We also want to do a freeze if we have any params to set,
1690 * even on a non-newly_constructed object.
1692 * It's possible that we have the case of non-newly created
1693 * singleton and all of the passed-in params were construct
1694 * properties so n_params > 0 but we will actually set no
1695 * properties. This is a pretty lame case to optimise, so
1696 * just ignore it and freeze anyway.
1698 if (newly_constructed || n_params)
1699 nqueue = g_object_notify_queue_freeze (object, FALSE);
1701 /* Remember: if it was newly_constructed then g_object_init()
1702 * already did a freeze, so we now have two. Release one.
1704 if (newly_constructed)
1705 g_object_notify_queue_thaw (object, nqueue);
1708 /* run 'constructed' handler if there is a custom one */
1709 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1710 class->constructed (object);
1712 /* set remaining properties */
1713 for (i = 0; i < n_params; i++)
1714 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1715 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1717 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1719 g_object_notify_queue_thaw (object, nqueue);
1725 g_object_new_internal (GObjectClass *class,
1726 GObjectConstructParam *params,
1729 GObjectNotifyQueue *nqueue = NULL;
1732 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1733 return g_object_new_with_custom_constructor (class, params, n_params);
1735 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1737 if (CLASS_HAS_PROPS (class))
1741 /* This will have been setup in g_object_init() */
1742 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1743 g_assert (nqueue != NULL);
1745 /* We will set exactly n_construct_properties construct
1746 * properties, but they may come from either the class default
1747 * values or the passed-in parameter list.
1749 for (node = class->construct_properties; node; node = node->next)
1751 const GValue *value;
1756 value = NULL; /* to silence gcc... */
1758 for (j = 0; j < n_params; j++)
1759 if (params[j].pspec == pspec)
1761 value = params[j].value;
1766 value = g_param_spec_get_default_value (pspec);
1768 object_set_property (object, pspec, value, nqueue);
1772 /* run 'constructed' handler if there is a custom one */
1773 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1774 class->constructed (object);
1780 /* Set remaining properties. The construct properties will
1781 * already have been taken, so set only the non-construct
1784 for (i = 0; i < n_params; i++)
1785 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1786 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1788 g_object_notify_queue_thaw (object, nqueue);
1796 * @object_type: the type id of the #GObject subtype to instantiate
1797 * @n_parameters: the length of the @parameters array
1798 * @parameters: (array length=n_parameters): an array of #GParameter
1800 * Creates a new instance of a #GObject subtype and sets its properties.
1802 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1803 * which are not explicitly specified are set to their default values.
1805 * Rename to: g_object_new
1806 * Returns: (type GObject.Object) (transfer full): a new instance of
1810 g_object_newv (GType object_type,
1812 GParameter *parameters)
1814 GObjectClass *class, *unref_class = NULL;
1817 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1818 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1820 /* Try to avoid thrashing the ref_count if we don't need to (since
1821 * it's a locked operation).
1823 class = g_type_class_peek_static (object_type);
1826 class = unref_class = g_type_class_ref (object_type);
1830 GObjectConstructParam *cparams;
1833 cparams = g_newa (GObjectConstructParam, n_parameters);
1836 for (i = 0; i < n_parameters; i++)
1841 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
1843 if G_UNLIKELY (!pspec)
1845 g_critical ("%s: object class '%s' has no property named '%s'",
1846 G_STRFUNC, g_type_name (object_type), parameters[i].name);
1850 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1852 g_critical ("%s: property '%s' of object class '%s' is not writable",
1853 G_STRFUNC, pspec->name, g_type_name (object_type));
1857 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1859 for (k = 0; k < j; k++)
1860 if (cparams[k].pspec == pspec)
1862 if G_UNLIKELY (k != j)
1864 g_critical ("%s: construct property '%s' for type '%s' cannot be set twice",
1865 G_STRFUNC, parameters[i].name, g_type_name (object_type));
1870 cparams[j].pspec = pspec;
1871 cparams[j].value = ¶meters[i].value;
1875 object = g_object_new_internal (class, cparams, j);
1878 /* Fast case: no properties passed in. */
1879 object = g_object_new_internal (class, NULL, 0);
1882 g_type_class_unref (unref_class);
1888 * g_object_new_valist: (skip)
1889 * @object_type: the type id of the #GObject subtype to instantiate
1890 * @first_property_name: the name of the first property
1891 * @var_args: the value of the first property, followed optionally by more
1892 * name/value pairs, followed by %NULL
1894 * Creates a new instance of a #GObject subtype and sets its properties.
1896 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1897 * which are not explicitly specified are set to their default values.
1899 * Returns: a new instance of @object_type
1902 g_object_new_valist (GType object_type,
1903 const gchar *first_property_name,
1906 GObjectClass *class, *unref_class = NULL;
1909 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1911 /* Try to avoid thrashing the ref_count if we don't need to (since
1912 * it's a locked operation).
1914 class = g_type_class_peek_static (object_type);
1917 class = unref_class = g_type_class_ref (object_type);
1919 if (first_property_name)
1921 GObjectConstructParam stack_params[16];
1922 GObjectConstructParam *params;
1926 name = first_property_name;
1927 params = stack_params;
1931 gchar *error = NULL;
1935 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
1937 if G_UNLIKELY (!pspec)
1939 g_critical ("%s: object class '%s' has no property named '%s'",
1940 G_STRFUNC, g_type_name (object_type), name);
1941 /* Can't continue because arg list will be out of sync. */
1945 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1947 g_critical ("%s: property '%s' of object class '%s' is not writable",
1948 G_STRFUNC, pspec->name, g_type_name (object_type));
1952 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1954 for (i = 0; i < n_params; i++)
1955 if (params[i].pspec == pspec)
1957 if G_UNLIKELY (i != n_params)
1959 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1960 G_STRFUNC, name, g_type_name (object_type));
1967 params = g_new (GObjectConstructParam, n_params + 1);
1968 memcpy (params, stack_params, sizeof stack_params);
1970 else if (n_params > 16)
1971 params = g_renew (GObjectConstructParam, params, n_params + 1);
1973 params[n_params].pspec = pspec;
1974 params[n_params].value = g_newa (GValue, 1);
1975 memset (params[n_params].value, 0, sizeof (GValue));
1977 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
1981 g_critical ("%s: %s", G_STRFUNC, error);
1982 g_value_unset (params[n_params].value);
1989 while ((name = va_arg (var_args, const gchar *)));
1991 object = g_object_new_internal (class, params, n_params);
1994 g_value_unset (params[n_params].value);
1996 if (params != stack_params)
2000 /* Fast case: no properties passed in. */
2001 object = g_object_new_internal (class, NULL, 0);
2004 g_type_class_unref (unref_class);
2010 g_object_constructor (GType type,
2011 guint n_construct_properties,
2012 GObjectConstructParam *construct_params)
2017 object = (GObject*) g_type_create_instance (type);
2019 /* set construction parameters */
2020 if (n_construct_properties)
2022 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2024 /* set construct properties */
2025 while (n_construct_properties--)
2027 GValue *value = construct_params->value;
2028 GParamSpec *pspec = construct_params->pspec;
2031 object_set_property (object, pspec, value, nqueue);
2033 g_object_notify_queue_thaw (object, nqueue);
2034 /* the notification queue is still frozen from g_object_init(), so
2035 * we don't need to handle it here, g_object_newv() takes
2044 g_object_constructed (GObject *object)
2046 /* empty default impl to allow unconditional upchaining */
2050 * g_object_set_valist: (skip)
2051 * @object: a #GObject
2052 * @first_property_name: name of the first property to set
2053 * @var_args: value for the first property, followed optionally by more
2054 * name/value pairs, followed by %NULL
2056 * Sets properties on an object.
2059 g_object_set_valist (GObject *object,
2060 const gchar *first_property_name,
2063 GObjectNotifyQueue *nqueue;
2066 g_return_if_fail (G_IS_OBJECT (object));
2068 g_object_ref (object);
2069 nqueue = g_object_notify_queue_freeze (object, FALSE);
2071 name = first_property_name;
2074 GValue value = G_VALUE_INIT;
2076 gchar *error = NULL;
2078 pspec = g_param_spec_pool_lookup (pspec_pool,
2080 G_OBJECT_TYPE (object),
2084 g_warning ("%s: object class '%s' has no property named '%s'",
2086 G_OBJECT_TYPE_NAME (object),
2090 if (!(pspec->flags & G_PARAM_WRITABLE))
2092 g_warning ("%s: property '%s' of object class '%s' is not writable",
2095 G_OBJECT_TYPE_NAME (object));
2098 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2100 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2101 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2105 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2109 g_warning ("%s: %s", G_STRFUNC, error);
2111 g_value_unset (&value);
2115 object_set_property (object, pspec, &value, nqueue);
2116 g_value_unset (&value);
2118 name = va_arg (var_args, gchar*);
2121 g_object_notify_queue_thaw (object, nqueue);
2122 g_object_unref (object);
2126 * g_object_get_valist: (skip)
2127 * @object: a #GObject
2128 * @first_property_name: name of the first property to get
2129 * @var_args: return location for the first property, followed optionally by more
2130 * name/return location pairs, followed by %NULL
2132 * Gets properties of an object.
2134 * In general, a copy is made of the property contents and the caller
2135 * is responsible for freeing the memory in the appropriate manner for
2136 * the type, for instance by calling g_free() or g_object_unref().
2138 * See g_object_get().
2141 g_object_get_valist (GObject *object,
2142 const gchar *first_property_name,
2147 g_return_if_fail (G_IS_OBJECT (object));
2149 g_object_ref (object);
2151 name = first_property_name;
2155 GValue value = G_VALUE_INIT;
2159 pspec = g_param_spec_pool_lookup (pspec_pool,
2161 G_OBJECT_TYPE (object),
2165 g_warning ("%s: object class '%s' has no property named '%s'",
2167 G_OBJECT_TYPE_NAME (object),
2171 if (!(pspec->flags & G_PARAM_READABLE))
2173 g_warning ("%s: property '%s' of object class '%s' is not readable",
2176 G_OBJECT_TYPE_NAME (object));
2180 g_value_init (&value, pspec->value_type);
2182 object_get_property (object, pspec, &value);
2184 G_VALUE_LCOPY (&value, var_args, 0, &error);
2187 g_warning ("%s: %s", G_STRFUNC, error);
2189 g_value_unset (&value);
2193 g_value_unset (&value);
2195 name = va_arg (var_args, gchar*);
2198 g_object_unref (object);
2202 * g_object_set: (skip)
2203 * @object: a #GObject
2204 * @first_property_name: name of the first property to set
2205 * @...: value for the first property, followed optionally by more
2206 * name/value pairs, followed by %NULL
2208 * Sets properties on an object.
2210 * Note that the "notify" signals are queued and only emitted (in
2211 * reverse order) after all properties have been set. See
2212 * g_object_freeze_notify().
2215 g_object_set (gpointer _object,
2216 const gchar *first_property_name,
2219 GObject *object = _object;
2222 g_return_if_fail (G_IS_OBJECT (object));
2224 va_start (var_args, first_property_name);
2225 g_object_set_valist (object, first_property_name, var_args);
2230 * g_object_get: (skip)
2231 * @object: a #GObject
2232 * @first_property_name: name of the first property to get
2233 * @...: return location for the first property, followed optionally by more
2234 * name/return location pairs, followed by %NULL
2236 * Gets properties of an object.
2238 * In general, a copy is made of the property contents and the caller
2239 * is responsible for freeing the memory in the appropriate manner for
2240 * the type, for instance by calling g_free() or g_object_unref().
2242 * Here is an example of using g_object_get() to get the contents
2243 * of three properties: an integer, a string and an object:
2244 * |[<!-- language="C" -->
2249 * g_object_get (my_object,
2250 * "int-property", &intval,
2251 * "str-property", &strval,
2252 * "obj-property", &objval,
2255 * // Do something with intval, strval, objval
2258 * g_object_unref (objval);
2262 g_object_get (gpointer _object,
2263 const gchar *first_property_name,
2266 GObject *object = _object;
2269 g_return_if_fail (G_IS_OBJECT (object));
2271 va_start (var_args, first_property_name);
2272 g_object_get_valist (object, first_property_name, var_args);
2277 * g_object_set_property:
2278 * @object: a #GObject
2279 * @property_name: the name of the property to set
2282 * Sets a property on an object.
2285 g_object_set_property (GObject *object,
2286 const gchar *property_name,
2287 const GValue *value)
2289 GObjectNotifyQueue *nqueue;
2292 g_return_if_fail (G_IS_OBJECT (object));
2293 g_return_if_fail (property_name != NULL);
2294 g_return_if_fail (G_IS_VALUE (value));
2296 g_object_ref (object);
2297 nqueue = g_object_notify_queue_freeze (object, FALSE);
2299 pspec = g_param_spec_pool_lookup (pspec_pool,
2301 G_OBJECT_TYPE (object),
2304 g_warning ("%s: object class '%s' has no property named '%s'",
2306 G_OBJECT_TYPE_NAME (object),
2308 else if (!(pspec->flags & G_PARAM_WRITABLE))
2309 g_warning ("%s: property '%s' of object class '%s' is not writable",
2312 G_OBJECT_TYPE_NAME (object));
2313 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2314 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2315 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2317 object_set_property (object, pspec, value, nqueue);
2319 g_object_notify_queue_thaw (object, nqueue);
2320 g_object_unref (object);
2324 * g_object_get_property:
2325 * @object: a #GObject
2326 * @property_name: the name of the property to get
2327 * @value: return location for the property value
2329 * Gets a property of an object. @value must have been initialized to the
2330 * expected type of the property (or a type to which the expected type can be
2331 * transformed) using g_value_init().
2333 * In general, a copy is made of the property contents and the caller is
2334 * responsible for freeing the memory by calling g_value_unset().
2336 * Note that g_object_get_property() is really intended for language
2337 * bindings, g_object_get() is much more convenient for C programming.
2340 g_object_get_property (GObject *object,
2341 const gchar *property_name,
2346 g_return_if_fail (G_IS_OBJECT (object));
2347 g_return_if_fail (property_name != NULL);
2348 g_return_if_fail (G_IS_VALUE (value));
2350 g_object_ref (object);
2352 pspec = g_param_spec_pool_lookup (pspec_pool,
2354 G_OBJECT_TYPE (object),
2357 g_warning ("%s: object class '%s' has no property named '%s'",
2359 G_OBJECT_TYPE_NAME (object),
2361 else if (!(pspec->flags & G_PARAM_READABLE))
2362 g_warning ("%s: property '%s' of object class '%s' is not readable",
2365 G_OBJECT_TYPE_NAME (object));
2368 GValue *prop_value, tmp_value = G_VALUE_INIT;
2370 /* auto-conversion of the callers value type
2372 if (G_VALUE_TYPE (value) == pspec->value_type)
2374 g_value_reset (value);
2377 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2379 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2380 G_STRFUNC, pspec->name,
2381 g_type_name (pspec->value_type),
2382 G_VALUE_TYPE_NAME (value));
2383 g_object_unref (object);
2388 g_value_init (&tmp_value, pspec->value_type);
2389 prop_value = &tmp_value;
2391 object_get_property (object, pspec, prop_value);
2392 if (prop_value != value)
2394 g_value_transform (prop_value, value);
2395 g_value_unset (&tmp_value);
2399 g_object_unref (object);
2403 * g_object_connect: (skip)
2404 * @object: a #GObject
2405 * @signal_spec: the spec for the first signal
2406 * @...: #GCallback for the first signal, followed by data for the
2407 * first signal, followed optionally by more signal
2408 * spec/callback/data triples, followed by %NULL
2410 * A convenience function to connect multiple signals at once.
2412 * The signal specs expected by this function have the form
2413 * "modifier::signal_name", where modifier can be one of the following:
2414 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2415 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2416 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2417 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2418 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2419 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2420 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2421 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2423 * |[<!-- language="C" -->
2424 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2425 * "type", GTK_WINDOW_POPUP,
2428 * "signal::event", gtk_menu_window_event, menu,
2429 * "signal::size_request", gtk_menu_window_size_request, menu,
2430 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2434 * Returns: (transfer none): @object
2437 g_object_connect (gpointer _object,
2438 const gchar *signal_spec,
2441 GObject *object = _object;
2444 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2445 g_return_val_if_fail (object->ref_count > 0, object);
2447 va_start (var_args, signal_spec);
2450 GCallback callback = va_arg (var_args, GCallback);
2451 gpointer data = va_arg (var_args, gpointer);
2453 if (strncmp (signal_spec, "signal::", 8) == 0)
2454 g_signal_connect_data (object, signal_spec + 8,
2455 callback, data, NULL,
2457 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2458 strncmp (signal_spec, "object-signal::", 15) == 0)
2459 g_signal_connect_object (object, signal_spec + 15,
2462 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2463 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2464 g_signal_connect_data (object, signal_spec + 16,
2465 callback, data, NULL,
2467 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2468 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2469 g_signal_connect_object (object, signal_spec + 23,
2472 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2473 strncmp (signal_spec, "signal-after::", 14) == 0)
2474 g_signal_connect_data (object, signal_spec + 14,
2475 callback, data, NULL,
2477 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2478 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2479 g_signal_connect_object (object, signal_spec + 21,
2482 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2483 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2484 g_signal_connect_data (object, signal_spec + 22,
2485 callback, data, NULL,
2486 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2487 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2488 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2489 g_signal_connect_object (object, signal_spec + 29,
2491 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2494 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2497 signal_spec = va_arg (var_args, gchar*);
2505 * g_object_disconnect: (skip)
2506 * @object: a #GObject
2507 * @signal_spec: the spec for the first signal
2508 * @...: #GCallback for the first signal, followed by data for the first signal,
2509 * followed optionally by more signal spec/callback/data triples,
2512 * A convenience function to disconnect multiple signals at once.
2514 * The signal specs expected by this function have the form
2515 * "any_signal", which means to disconnect any signal with matching
2516 * callback and data, or "any_signal::signal_name", which only
2517 * disconnects the signal named "signal_name".
2520 g_object_disconnect (gpointer _object,
2521 const gchar *signal_spec,
2524 GObject *object = _object;
2527 g_return_if_fail (G_IS_OBJECT (object));
2528 g_return_if_fail (object->ref_count > 0);
2530 va_start (var_args, signal_spec);
2533 GCallback callback = va_arg (var_args, GCallback);
2534 gpointer data = va_arg (var_args, gpointer);
2535 guint sid = 0, detail = 0, mask = 0;
2537 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2538 strncmp (signal_spec, "any-signal::", 12) == 0)
2541 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2543 else if (strcmp (signal_spec, "any_signal") == 0 ||
2544 strcmp (signal_spec, "any-signal") == 0)
2547 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2551 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2555 if ((mask & G_SIGNAL_MATCH_ID) &&
2556 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2557 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2558 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2560 NULL, (gpointer)callback, data))
2561 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2562 signal_spec = va_arg (var_args, gchar*);
2573 } weak_refs[1]; /* flexible array */
2577 weak_refs_notify (gpointer data)
2579 WeakRefStack *wstack = data;
2582 for (i = 0; i < wstack->n_weak_refs; i++)
2583 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2588 * g_object_weak_ref: (skip)
2589 * @object: #GObject to reference weakly
2590 * @notify: callback to invoke before the object is freed
2591 * @data: extra data to pass to notify
2593 * Adds a weak reference callback to an object. Weak references are
2594 * used for notification when an object is finalized. They are called
2595 * "weak references" because they allow you to safely hold a pointer
2596 * to an object without calling g_object_ref() (g_object_ref() adds a
2597 * strong reference, that is, forces the object to stay alive).
2599 * Note that the weak references created by this method are not
2600 * thread-safe: they cannot safely be used in one thread if the
2601 * object's last g_object_unref() might happen in another thread.
2602 * Use #GWeakRef if thread-safety is required.
2605 g_object_weak_ref (GObject *object,
2609 WeakRefStack *wstack;
2612 g_return_if_fail (G_IS_OBJECT (object));
2613 g_return_if_fail (notify != NULL);
2614 g_return_if_fail (object->ref_count >= 1);
2616 G_LOCK (weak_refs_mutex);
2617 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2620 i = wstack->n_weak_refs++;
2621 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2625 wstack = g_renew (WeakRefStack, NULL, 1);
2626 wstack->object = object;
2627 wstack->n_weak_refs = 1;
2630 wstack->weak_refs[i].notify = notify;
2631 wstack->weak_refs[i].data = data;
2632 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2633 G_UNLOCK (weak_refs_mutex);
2637 * g_object_weak_unref: (skip)
2638 * @object: #GObject to remove a weak reference from
2639 * @notify: callback to search for
2640 * @data: data to search for
2642 * Removes a weak reference callback to an object.
2645 g_object_weak_unref (GObject *object,
2649 WeakRefStack *wstack;
2650 gboolean found_one = FALSE;
2652 g_return_if_fail (G_IS_OBJECT (object));
2653 g_return_if_fail (notify != NULL);
2655 G_LOCK (weak_refs_mutex);
2656 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2661 for (i = 0; i < wstack->n_weak_refs; i++)
2662 if (wstack->weak_refs[i].notify == notify &&
2663 wstack->weak_refs[i].data == data)
2666 wstack->n_weak_refs -= 1;
2667 if (i != wstack->n_weak_refs)
2668 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2673 G_UNLOCK (weak_refs_mutex);
2675 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2679 * g_object_add_weak_pointer: (skip)
2680 * @object: The object that should be weak referenced.
2681 * @weak_pointer_location: (inout): The memory address of a pointer.
2683 * Adds a weak reference from weak_pointer to @object to indicate that
2684 * the pointer located at @weak_pointer_location is only valid during
2685 * the lifetime of @object. When the @object is finalized,
2686 * @weak_pointer will be set to %NULL.
2688 * Note that as with g_object_weak_ref(), the weak references created by
2689 * this method are not thread-safe: they cannot safely be used in one
2690 * thread if the object's last g_object_unref() might happen in another
2691 * thread. Use #GWeakRef if thread-safety is required.
2694 g_object_add_weak_pointer (GObject *object,
2695 gpointer *weak_pointer_location)
2697 g_return_if_fail (G_IS_OBJECT (object));
2698 g_return_if_fail (weak_pointer_location != NULL);
2700 g_object_weak_ref (object,
2701 (GWeakNotify) g_nullify_pointer,
2702 weak_pointer_location);
2706 * g_object_remove_weak_pointer: (skip)
2707 * @object: The object that is weak referenced.
2708 * @weak_pointer_location: (inout): The memory address of a pointer.
2710 * Removes a weak reference from @object that was previously added
2711 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2712 * to match the one used with g_object_add_weak_pointer().
2715 g_object_remove_weak_pointer (GObject *object,
2716 gpointer *weak_pointer_location)
2718 g_return_if_fail (G_IS_OBJECT (object));
2719 g_return_if_fail (weak_pointer_location != NULL);
2721 g_object_weak_unref (object,
2722 (GWeakNotify) g_nullify_pointer,
2723 weak_pointer_location);
2727 object_floating_flag_handler (GObject *object,
2733 case +1: /* force floating if possible */
2735 oldvalue = g_atomic_pointer_get (&object->qdata);
2736 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2737 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2738 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2739 case -1: /* sink if possible */
2741 oldvalue = g_atomic_pointer_get (&object->qdata);
2742 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2743 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2744 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2745 default: /* check floating */
2746 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2751 * g_object_is_floating:
2752 * @object: (type GObject.Object): a #GObject
2754 * Checks whether @object has a [floating][floating-ref] reference.
2758 * Returns: %TRUE if @object has a floating reference
2761 g_object_is_floating (gpointer _object)
2763 GObject *object = _object;
2764 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2765 return floating_flag_handler (object, 0);
2769 * g_object_ref_sink:
2770 * @object: (type GObject.Object): a #GObject
2772 * Increase the reference count of @object, and possibly remove the
2773 * [floating][floating-ref] reference, if @object has a floating reference.
2775 * In other words, if the object is floating, then this call "assumes
2776 * ownership" of the floating reference, converting it to a normal
2777 * reference by clearing the floating flag while leaving the reference
2778 * count unchanged. If the object is not floating, then this call
2779 * adds a new normal reference increasing the reference count by one.
2783 * Returns: (type GObject.Object) (transfer none): @object
2786 g_object_ref_sink (gpointer _object)
2788 GObject *object = _object;
2789 gboolean was_floating;
2790 g_return_val_if_fail (G_IS_OBJECT (object), object);
2791 g_return_val_if_fail (object->ref_count >= 1, object);
2792 g_object_ref (object);
2793 was_floating = floating_flag_handler (object, -1);
2795 g_object_unref (object);
2800 * g_object_force_floating:
2801 * @object: a #GObject
2803 * This function is intended for #GObject implementations to re-enforce
2804 * a [floating][floating-ref] object reference. Doing this is seldom
2805 * required: all #GInitiallyUnowneds are created with a floating reference
2806 * which usually just needs to be sunken by calling g_object_ref_sink().
2811 g_object_force_floating (GObject *object)
2813 g_return_if_fail (G_IS_OBJECT (object));
2814 g_return_if_fail (object->ref_count >= 1);
2816 floating_flag_handler (object, +1);
2821 guint n_toggle_refs;
2823 GToggleNotify notify;
2825 } toggle_refs[1]; /* flexible array */
2829 toggle_refs_notify (GObject *object,
2830 gboolean is_last_ref)
2832 ToggleRefStack tstack, *tstackptr;
2834 G_LOCK (toggle_refs_mutex);
2835 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2836 tstack = *tstackptr;
2837 G_UNLOCK (toggle_refs_mutex);
2839 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2840 * will only be notified when there is exactly one of them.
2842 g_assert (tstack.n_toggle_refs == 1);
2843 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2847 * g_object_add_toggle_ref: (skip)
2848 * @object: a #GObject
2849 * @notify: a function to call when this reference is the
2850 * last reference to the object, or is no longer
2851 * the last reference.
2852 * @data: data to pass to @notify
2854 * Increases the reference count of the object by one and sets a
2855 * callback to be called when all other references to the object are
2856 * dropped, or when this is already the last reference to the object
2857 * and another reference is established.
2859 * This functionality is intended for binding @object to a proxy
2860 * object managed by another memory manager. This is done with two
2861 * paired references: the strong reference added by
2862 * g_object_add_toggle_ref() and a reverse reference to the proxy
2863 * object which is either a strong reference or weak reference.
2865 * The setup is that when there are no other references to @object,
2866 * only a weak reference is held in the reverse direction from @object
2867 * to the proxy object, but when there are other references held to
2868 * @object, a strong reference is held. The @notify callback is called
2869 * when the reference from @object to the proxy object should be
2870 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
2871 * (@is_last_ref false).
2873 * Since a (normal) reference must be held to the object before
2874 * calling g_object_add_toggle_ref(), the initial state of the reverse
2875 * link is always strong.
2877 * Multiple toggle references may be added to the same gobject,
2878 * however if there are multiple toggle references to an object, none
2879 * of them will ever be notified until all but one are removed. For
2880 * this reason, you should only ever use a toggle reference if there
2881 * is important state in the proxy object.
2886 g_object_add_toggle_ref (GObject *object,
2887 GToggleNotify notify,
2890 ToggleRefStack *tstack;
2893 g_return_if_fail (G_IS_OBJECT (object));
2894 g_return_if_fail (notify != NULL);
2895 g_return_if_fail (object->ref_count >= 1);
2897 g_object_ref (object);
2899 G_LOCK (toggle_refs_mutex);
2900 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2903 i = tstack->n_toggle_refs++;
2904 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2905 * in tstate->toggle_refs */
2906 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2910 tstack = g_renew (ToggleRefStack, NULL, 1);
2911 tstack->object = object;
2912 tstack->n_toggle_refs = 1;
2916 /* Set a flag for fast lookup after adding the first toggle reference */
2917 if (tstack->n_toggle_refs == 1)
2918 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2920 tstack->toggle_refs[i].notify = notify;
2921 tstack->toggle_refs[i].data = data;
2922 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2923 (GDestroyNotify)g_free);
2924 G_UNLOCK (toggle_refs_mutex);
2928 * g_object_remove_toggle_ref: (skip)
2929 * @object: a #GObject
2930 * @notify: a function to call when this reference is the
2931 * last reference to the object, or is no longer
2932 * the last reference.
2933 * @data: data to pass to @notify
2935 * Removes a reference added with g_object_add_toggle_ref(). The
2936 * reference count of the object is decreased by one.
2941 g_object_remove_toggle_ref (GObject *object,
2942 GToggleNotify notify,
2945 ToggleRefStack *tstack;
2946 gboolean found_one = FALSE;
2948 g_return_if_fail (G_IS_OBJECT (object));
2949 g_return_if_fail (notify != NULL);
2951 G_LOCK (toggle_refs_mutex);
2952 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2957 for (i = 0; i < tstack->n_toggle_refs; i++)
2958 if (tstack->toggle_refs[i].notify == notify &&
2959 tstack->toggle_refs[i].data == data)
2962 tstack->n_toggle_refs -= 1;
2963 if (i != tstack->n_toggle_refs)
2964 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2966 if (tstack->n_toggle_refs == 0)
2967 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2972 G_UNLOCK (toggle_refs_mutex);
2975 g_object_unref (object);
2977 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2982 * @object: (type GObject.Object): a #GObject
2984 * Increases the reference count of @object.
2986 * Returns: (type GObject.Object) (transfer none): the same @object
2989 g_object_ref (gpointer _object)
2991 GObject *object = _object;
2994 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2995 g_return_val_if_fail (object->ref_count > 0, NULL);
2997 old_val = g_atomic_int_add (&object->ref_count, 1);
2999 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3000 toggle_refs_notify (object, FALSE);
3002 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3009 * @object: (type GObject.Object): a #GObject
3011 * Decreases the reference count of @object. When its reference count
3012 * drops to 0, the object is finalized (i.e. its memory is freed).
3015 g_object_unref (gpointer _object)
3017 GObject *object = _object;
3020 g_return_if_fail (G_IS_OBJECT (object));
3021 g_return_if_fail (object->ref_count > 0);
3023 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3024 retry_atomic_decrement1:
3025 old_ref = g_atomic_int_get (&object->ref_count);
3028 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3029 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3031 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3032 goto retry_atomic_decrement1;
3034 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3036 /* if we went from 2->1 we need to notify toggle refs if any */
3037 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3038 toggle_refs_notify (object, TRUE);
3042 GSList **weak_locations;
3044 /* The only way that this object can live at this point is if
3045 * there are outstanding weak references already established
3046 * before we got here.
3048 * If there were not already weak references then no more can be
3049 * established at this time, because the other thread would have
3050 * to hold a strong ref in order to call
3051 * g_object_add_weak_pointer() and then we wouldn't be here.
3053 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3055 if (weak_locations != NULL)
3057 g_rw_lock_writer_lock (&weak_locations_lock);
3059 /* It is possible that one of the weak references beat us to
3060 * the lock. Make sure the refcount is still what we expected
3063 old_ref = g_atomic_int_get (&object->ref_count);
3066 g_rw_lock_writer_unlock (&weak_locations_lock);
3067 goto retry_atomic_decrement1;
3070 /* We got the lock first, so the object will definitely die
3071 * now. Clear out all the weak references.
3073 while (*weak_locations)
3075 GWeakRef *weak_ref_location = (*weak_locations)->data;
3077 weak_ref_location->priv.p = NULL;
3078 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3081 g_rw_lock_writer_unlock (&weak_locations_lock);
3084 /* we are about to remove the last reference */
3085 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3086 G_OBJECT_GET_CLASS (object)->dispose (object);
3087 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3089 /* may have been re-referenced meanwhile */
3090 retry_atomic_decrement2:
3091 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3094 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3095 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3097 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3098 goto retry_atomic_decrement2;
3100 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3102 /* if we went from 2->1 we need to notify toggle refs if any */
3103 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3104 toggle_refs_notify (object, TRUE);
3109 /* we are still in the process of taking away the last ref */
3110 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3111 g_signal_handlers_destroy (object);
3112 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3114 /* decrement the last reference */
3115 old_ref = g_atomic_int_add (&object->ref_count, -1);
3117 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3119 /* may have been re-referenced meanwhile */
3120 if (G_LIKELY (old_ref == 1))
3122 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3123 G_OBJECT_GET_CLASS (object)->finalize (object);
3125 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3127 #ifdef G_ENABLE_DEBUG
3130 /* catch objects not chaining finalize handlers */
3131 G_LOCK (debug_objects);
3132 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3133 G_UNLOCK (debug_objects);
3135 #endif /* G_ENABLE_DEBUG */
3136 g_type_free_instance ((GTypeInstance*) object);
3142 * g_clear_object: (skip)
3143 * @object_ptr: a pointer to a #GObject reference
3145 * Clears a reference to a #GObject.
3147 * @object_ptr must not be %NULL.
3149 * If the reference is %NULL then this function does nothing.
3150 * Otherwise, the reference count of the object is decreased and the
3151 * pointer is set to %NULL.
3153 * This function is threadsafe and modifies the pointer atomically,
3154 * using memory barriers where needed.
3156 * A macro is also included that allows this function to be used without
3161 #undef g_clear_object
3163 g_clear_object (volatile GObject **object_ptr)
3165 g_clear_pointer (object_ptr, g_object_unref);
3169 * g_object_get_qdata:
3170 * @object: The GObject to get a stored user data pointer from
3171 * @quark: A #GQuark, naming the user data pointer
3173 * This function gets back user data pointers stored via
3174 * g_object_set_qdata().
3176 * Returns: (transfer none): The user data pointer set, or %NULL
3179 g_object_get_qdata (GObject *object,
3182 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3184 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3188 * g_object_set_qdata: (skip)
3189 * @object: The GObject to set store a user data pointer
3190 * @quark: A #GQuark, naming the user data pointer
3191 * @data: An opaque user data pointer
3193 * This sets an opaque, named pointer on an object.
3194 * The name is specified through a #GQuark (retrived e.g. via
3195 * g_quark_from_static_string()), and the pointer
3196 * can be gotten back from the @object with g_object_get_qdata()
3197 * until the @object is finalized.
3198 * Setting a previously set user data pointer, overrides (frees)
3199 * the old pointer set, using #NULL as pointer essentially
3200 * removes the data stored.
3203 g_object_set_qdata (GObject *object,
3207 g_return_if_fail (G_IS_OBJECT (object));
3208 g_return_if_fail (quark > 0);
3210 g_datalist_id_set_data (&object->qdata, quark, data);
3214 * g_object_dup_qdata:
3215 * @object: the #GObject to store user data on
3216 * @quark: a #GQuark, naming the user data pointer
3217 * @dup_func: (allow-none): function to dup the value
3218 * @user_data: (allow-none): passed as user_data to @dup_func
3220 * This is a variant of g_object_get_qdata() which returns
3221 * a 'duplicate' of the value. @dup_func defines the
3222 * meaning of 'duplicate' in this context, it could e.g.
3223 * take a reference on a ref-counted object.
3225 * If the @quark is not set on the object then @dup_func
3226 * will be called with a %NULL argument.
3228 * Note that @dup_func is called while user data of @object
3231 * This function can be useful to avoid races when multiple
3232 * threads are using object data on the same key on the same
3235 * Returns: the result of calling @dup_func on the value
3236 * associated with @quark on @object, or %NULL if not set.
3237 * If @dup_func is %NULL, the value is returned
3243 g_object_dup_qdata (GObject *object,
3245 GDuplicateFunc dup_func,
3248 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3249 g_return_val_if_fail (quark > 0, NULL);
3251 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3255 * g_object_replace_qdata:
3256 * @object: the #GObject to store user data on
3257 * @quark: a #GQuark, naming the user data pointer
3258 * @oldval: (allow-none): the old value to compare against
3259 * @newval: (allow-none): the new value
3260 * @destroy: (allow-none): a destroy notify for the new value
3261 * @old_destroy: (allow-none): destroy notify for the existing value
3263 * Compares the user data for the key @quark on @object with
3264 * @oldval, and if they are the same, replaces @oldval with
3267 * This is like a typical atomic compare-and-exchange
3268 * operation, for user data on an object.
3270 * If the previous value was replaced then ownership of the
3271 * old value (@oldval) is passed to the caller, including
3272 * the registered destroy notify for it (passed out in @old_destroy).
3273 * Its up to the caller to free this as he wishes, which may
3274 * or may not include using @old_destroy as sometimes replacement
3275 * should not destroy the object in the normal way.
3277 * Return: %TRUE if the existing value for @quark was replaced
3278 * by @newval, %FALSE otherwise.
3283 g_object_replace_qdata (GObject *object,
3287 GDestroyNotify destroy,
3288 GDestroyNotify *old_destroy)
3290 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3291 g_return_val_if_fail (quark > 0, FALSE);
3293 return g_datalist_id_replace_data (&object->qdata, quark,
3294 oldval, newval, destroy,
3299 * g_object_set_qdata_full: (skip)
3300 * @object: The GObject to set store a user data pointer
3301 * @quark: A #GQuark, naming the user data pointer
3302 * @data: An opaque user data pointer
3303 * @destroy: Function to invoke with @data as argument, when @data
3306 * This function works like g_object_set_qdata(), but in addition,
3307 * a void (*destroy) (gpointer) function may be specified which is
3308 * called with @data as argument when the @object is finalized, or
3309 * the data is being overwritten by a call to g_object_set_qdata()
3310 * with the same @quark.
3313 g_object_set_qdata_full (GObject *object,
3316 GDestroyNotify destroy)
3318 g_return_if_fail (G_IS_OBJECT (object));
3319 g_return_if_fail (quark > 0);
3321 g_datalist_id_set_data_full (&object->qdata, quark, data,
3322 data ? destroy : (GDestroyNotify) NULL);
3326 * g_object_steal_qdata:
3327 * @object: The GObject to get a stored user data pointer from
3328 * @quark: A #GQuark, naming the user data pointer
3330 * This function gets back user data pointers stored via
3331 * g_object_set_qdata() and removes the @data from object
3332 * without invoking its destroy() function (if any was
3334 * Usually, calling this function is only required to update
3335 * user data pointers with a destroy notifier, for example:
3336 * |[<!-- language="C" -->
3338 * object_add_to_user_list (GObject *object,
3339 * const gchar *new_string)
3341 * // the quark, naming the object data
3342 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3343 * // retrive the old string list
3344 * GList *list = g_object_steal_qdata (object, quark_string_list);
3346 * // prepend new string
3347 * list = g_list_prepend (list, g_strdup (new_string));
3348 * // this changed 'list', so we need to set it again
3349 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3352 * free_string_list (gpointer data)
3354 * GList *node, *list = data;
3356 * for (node = list; node; node = node->next)
3357 * g_free (node->data);
3358 * g_list_free (list);
3361 * Using g_object_get_qdata() in the above example, instead of
3362 * g_object_steal_qdata() would have left the destroy function set,
3363 * and thus the partial string list would have been freed upon
3364 * g_object_set_qdata_full().
3366 * Returns: (transfer full): The user data pointer set, or %NULL
3369 g_object_steal_qdata (GObject *object,
3372 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3373 g_return_val_if_fail (quark > 0, NULL);
3375 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3379 * g_object_get_data:
3380 * @object: #GObject containing the associations
3381 * @key: name of the key for that association
3383 * Gets a named field from the objects table of associations (see g_object_set_data()).
3385 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3388 g_object_get_data (GObject *object,
3391 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3392 g_return_val_if_fail (key != NULL, NULL);
3394 return g_datalist_get_data (&object->qdata, key);
3398 * g_object_set_data:
3399 * @object: #GObject containing the associations.
3400 * @key: name of the key
3401 * @data: data to associate with that key
3403 * Each object carries around a table of associations from
3404 * strings to pointers. This function lets you set an association.
3406 * If the object already had an association with that name,
3407 * the old association will be destroyed.
3410 g_object_set_data (GObject *object,
3414 g_return_if_fail (G_IS_OBJECT (object));
3415 g_return_if_fail (key != NULL);
3417 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3421 * g_object_dup_data:
3422 * @object: the #GObject to store user data on
3423 * @key: a string, naming the user data pointer
3424 * @dup_func: (allow-none): function to dup the value
3425 * @user_data: (allow-none): passed as user_data to @dup_func
3427 * This is a variant of g_object_get_data() which returns
3428 * a 'duplicate' of the value. @dup_func defines the
3429 * meaning of 'duplicate' in this context, it could e.g.
3430 * take a reference on a ref-counted object.
3432 * If the @key is not set on the object then @dup_func
3433 * will be called with a %NULL argument.
3435 * Note that @dup_func is called while user data of @object
3438 * This function can be useful to avoid races when multiple
3439 * threads are using object data on the same key on the same
3442 * Returns: the result of calling @dup_func on the value
3443 * associated with @key on @object, or %NULL if not set.
3444 * If @dup_func is %NULL, the value is returned
3450 g_object_dup_data (GObject *object,
3452 GDuplicateFunc dup_func,
3455 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3456 g_return_val_if_fail (key != NULL, NULL);
3458 return g_datalist_id_dup_data (&object->qdata,
3459 g_quark_from_string (key),
3460 dup_func, user_data);
3464 * g_object_replace_data:
3465 * @object: the #GObject to store user data on
3466 * @key: a string, naming the user data pointer
3467 * @oldval: (allow-none): the old value to compare against
3468 * @newval: (allow-none): the new value
3469 * @destroy: (allow-none): a destroy notify for the new value
3470 * @old_destroy: (allow-none): destroy notify for the existing value
3472 * Compares the user data for the key @key on @object with
3473 * @oldval, and if they are the same, replaces @oldval with
3476 * This is like a typical atomic compare-and-exchange
3477 * operation, for user data on an object.
3479 * If the previous value was replaced then ownership of the
3480 * old value (@oldval) is passed to the caller, including
3481 * the registered destroy notify for it (passed out in @old_destroy).
3482 * Its up to the caller to free this as he wishes, which may
3483 * or may not include using @old_destroy as sometimes replacement
3484 * should not destroy the object in the normal way.
3486 * Return: %TRUE if the existing value for @key was replaced
3487 * by @newval, %FALSE otherwise.
3492 g_object_replace_data (GObject *object,
3496 GDestroyNotify destroy,
3497 GDestroyNotify *old_destroy)
3499 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3500 g_return_val_if_fail (key != NULL, FALSE);
3502 return g_datalist_id_replace_data (&object->qdata,
3503 g_quark_from_string (key),
3504 oldval, newval, destroy,
3509 * g_object_set_data_full: (skip)
3510 * @object: #GObject containing the associations
3511 * @key: name of the key
3512 * @data: data to associate with that key
3513 * @destroy: function to call when the association is destroyed
3515 * Like g_object_set_data() except it adds notification
3516 * for when the association is destroyed, either by setting it
3517 * to a different value or when the object is destroyed.
3519 * Note that the @destroy callback is not called if @data is %NULL.
3522 g_object_set_data_full (GObject *object,
3525 GDestroyNotify destroy)
3527 g_return_if_fail (G_IS_OBJECT (object));
3528 g_return_if_fail (key != NULL);
3530 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3531 data ? destroy : (GDestroyNotify) NULL);
3535 * g_object_steal_data:
3536 * @object: #GObject containing the associations
3537 * @key: name of the key
3539 * Remove a specified datum from the object's data associations,
3540 * without invoking the association's destroy handler.
3542 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3545 g_object_steal_data (GObject *object,
3550 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3551 g_return_val_if_fail (key != NULL, NULL);
3553 quark = g_quark_try_string (key);
3555 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3559 g_value_object_init (GValue *value)
3561 value->data[0].v_pointer = NULL;
3565 g_value_object_free_value (GValue *value)
3567 if (value->data[0].v_pointer)
3568 g_object_unref (value->data[0].v_pointer);
3572 g_value_object_copy_value (const GValue *src_value,
3575 if (src_value->data[0].v_pointer)
3576 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3578 dest_value->data[0].v_pointer = NULL;
3582 g_value_object_transform_value (const GValue *src_value,
3585 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)))
3586 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3588 dest_value->data[0].v_pointer = NULL;
3592 g_value_object_peek_pointer (const GValue *value)
3594 return value->data[0].v_pointer;
3598 g_value_object_collect_value (GValue *value,
3599 guint n_collect_values,
3600 GTypeCValue *collect_values,
3601 guint collect_flags)
3603 if (collect_values[0].v_pointer)
3605 GObject *object = collect_values[0].v_pointer;
3607 if (object->g_type_instance.g_class == NULL)
3608 return g_strconcat ("invalid unclassed object pointer for value type '",
3609 G_VALUE_TYPE_NAME (value),
3612 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3613 return g_strconcat ("invalid object type '",
3614 G_OBJECT_TYPE_NAME (object),
3615 "' for value type '",
3616 G_VALUE_TYPE_NAME (value),
3619 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3620 value->data[0].v_pointer = g_object_ref (object);
3623 value->data[0].v_pointer = NULL;
3629 g_value_object_lcopy_value (const GValue *value,
3630 guint n_collect_values,
3631 GTypeCValue *collect_values,
3632 guint collect_flags)
3634 GObject **object_p = collect_values[0].v_pointer;
3637 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3639 if (!value->data[0].v_pointer)
3641 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3642 *object_p = value->data[0].v_pointer;
3644 *object_p = g_object_ref (value->data[0].v_pointer);
3650 * g_value_set_object:
3651 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3652 * @v_object: (type GObject.Object) (allow-none): object value to be set
3654 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3656 * g_value_set_object() increases the reference count of @v_object
3657 * (the #GValue holds a reference to @v_object). If you do not wish
3658 * to increase the reference count of the object (i.e. you wish to
3659 * pass your current reference to the #GValue because you no longer
3660 * need it), use g_value_take_object() instead.
3662 * It is important that your #GValue holds a reference to @v_object (either its
3663 * own, or one it has taken) to ensure that the object won't be destroyed while
3664 * the #GValue still exists).
3667 g_value_set_object (GValue *value,
3672 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3674 old = value->data[0].v_pointer;
3678 g_return_if_fail (G_IS_OBJECT (v_object));
3679 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3681 value->data[0].v_pointer = v_object;
3682 g_object_ref (value->data[0].v_pointer);
3685 value->data[0].v_pointer = NULL;
3688 g_object_unref (old);
3692 * g_value_set_object_take_ownership: (skip)
3693 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3694 * @v_object: (allow-none): object value to be set
3696 * This is an internal function introduced mainly for C marshallers.
3698 * Deprecated: 2.4: Use g_value_take_object() instead.
3701 g_value_set_object_take_ownership (GValue *value,
3704 g_value_take_object (value, v_object);
3708 * g_value_take_object: (skip)
3709 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3710 * @v_object: (allow-none): object value to be set
3712 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3713 * and takes over the ownership of the callers reference to @v_object;
3714 * the caller doesn't have to unref it any more (i.e. the reference
3715 * count of the object is not increased).
3717 * If you want the #GValue to hold its own reference to @v_object, use
3718 * g_value_set_object() instead.
3723 g_value_take_object (GValue *value,
3726 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3728 if (value->data[0].v_pointer)
3730 g_object_unref (value->data[0].v_pointer);
3731 value->data[0].v_pointer = NULL;
3736 g_return_if_fail (G_IS_OBJECT (v_object));
3737 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3739 value->data[0].v_pointer = v_object; /* we take over the reference count */
3744 * g_value_get_object:
3745 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3747 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3749 * Returns: (type GObject.Object) (transfer none): object contents of @value
3752 g_value_get_object (const GValue *value)
3754 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3756 return value->data[0].v_pointer;
3760 * g_value_dup_object:
3761 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3763 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3764 * its reference count. If the contents of the #GValue are %NULL, then
3765 * %NULL will be returned.
3767 * Returns: (type GObject.Object) (transfer full): object content of @value,
3768 * should be unreferenced when no longer needed.
3771 g_value_dup_object (const GValue *value)
3773 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3775 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3779 * g_signal_connect_object: (skip)
3780 * @instance: the instance to connect to.
3781 * @detailed_signal: a string of the form "signal-name::detail".
3782 * @c_handler: the #GCallback to connect.
3783 * @gobject: the object to pass as data to @c_handler.
3784 * @connect_flags: a combination of #GConnectFlags.
3786 * This is similar to g_signal_connect_data(), but uses a closure which
3787 * ensures that the @gobject stays alive during the call to @c_handler
3788 * by temporarily adding a reference count to @gobject.
3790 * When the @gobject is destroyed the signal handler will be automatically
3791 * disconnected. Note that this is not currently threadsafe (ie:
3792 * emitting a signal while @gobject is being destroyed in another thread
3795 * Returns: the handler id.
3798 g_signal_connect_object (gpointer instance,
3799 const gchar *detailed_signal,
3800 GCallback c_handler,
3802 GConnectFlags connect_flags)
3804 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3805 g_return_val_if_fail (detailed_signal != NULL, 0);
3806 g_return_val_if_fail (c_handler != NULL, 0);
3812 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3814 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3816 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3819 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3825 GClosure *closures[1]; /* flexible array */
3827 /* don't change this structure without supplying an accessor for
3828 * watched closures, e.g.:
3829 * GSList* g_object_list_watched_closures (GObject *object)
3832 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3833 * carray = g_object_get_data (object, "GObject-closure-array");
3836 * GSList *slist = NULL;
3838 * for (i = 0; i < carray->n_closures; i++)
3839 * slist = g_slist_prepend (slist, carray->closures[i]);
3847 object_remove_closure (gpointer data,
3850 GObject *object = data;
3854 G_LOCK (closure_array_mutex);
3855 carray = g_object_get_qdata (object, quark_closure_array);
3856 for (i = 0; i < carray->n_closures; i++)
3857 if (carray->closures[i] == closure)
3859 carray->n_closures--;
3860 if (i < carray->n_closures)
3861 carray->closures[i] = carray->closures[carray->n_closures];
3862 G_UNLOCK (closure_array_mutex);
3865 G_UNLOCK (closure_array_mutex);
3866 g_assert_not_reached ();
3870 destroy_closure_array (gpointer data)
3872 CArray *carray = data;
3873 GObject *object = carray->object;
3874 guint i, n = carray->n_closures;
3876 for (i = 0; i < n; i++)
3878 GClosure *closure = carray->closures[i];
3880 /* removing object_remove_closure() upfront is probably faster than
3881 * letting it fiddle with quark_closure_array which is empty anyways
3883 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3884 g_closure_invalidate (closure);
3890 * g_object_watch_closure:
3891 * @object: GObject restricting lifetime of @closure
3892 * @closure: GClosure to watch
3894 * This function essentially limits the life time of the @closure to
3895 * the life time of the object. That is, when the object is finalized,
3896 * the @closure is invalidated by calling g_closure_invalidate() on
3897 * it, in order to prevent invocations of the closure with a finalized
3898 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3899 * added as marshal guards to the @closure, to ensure that an extra
3900 * reference count is held on @object during invocation of the
3901 * @closure. Usually, this function will be called on closures that
3902 * use this @object as closure data.
3905 g_object_watch_closure (GObject *object,
3911 g_return_if_fail (G_IS_OBJECT (object));
3912 g_return_if_fail (closure != NULL);
3913 g_return_if_fail (closure->is_invalid == FALSE);
3914 g_return_if_fail (closure->in_marshal == FALSE);
3915 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3917 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3918 g_closure_add_marshal_guards (closure,
3919 object, (GClosureNotify) g_object_ref,
3920 object, (GClosureNotify) g_object_unref);
3921 G_LOCK (closure_array_mutex);
3922 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3925 carray = g_renew (CArray, NULL, 1);
3926 carray->object = object;
3927 carray->n_closures = 1;
3932 i = carray->n_closures++;
3933 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3935 carray->closures[i] = closure;
3936 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3937 G_UNLOCK (closure_array_mutex);
3941 * g_closure_new_object:
3942 * @sizeof_closure: the size of the structure to allocate, must be at least
3943 * `sizeof (GClosure)`
3944 * @object: a #GObject pointer to store in the @data field of the newly
3945 * allocated #GClosure
3947 * A variant of g_closure_new_simple() which stores @object in the
3948 * @data field of the closure and calls g_object_watch_closure() on
3949 * @object and the created closure. This function is mainly useful
3950 * when implementing new types of closures.
3952 * Returns: (transfer full): a newly allocated #GClosure
3955 g_closure_new_object (guint sizeof_closure,
3960 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3961 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3963 closure = g_closure_new_simple (sizeof_closure, object);
3964 g_object_watch_closure (object, closure);
3970 * g_cclosure_new_object: (skip)
3971 * @callback_func: the function to invoke
3972 * @object: a #GObject pointer to pass to @callback_func
3974 * A variant of g_cclosure_new() which uses @object as @user_data and
3975 * calls g_object_watch_closure() on @object and the created
3976 * closure. This function is useful when you have a callback closely
3977 * associated with a #GObject, and want the callback to no longer run
3978 * after the object is is freed.
3980 * Returns: a new #GCClosure
3983 g_cclosure_new_object (GCallback callback_func,
3988 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3989 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3990 g_return_val_if_fail (callback_func != NULL, NULL);
3992 closure = g_cclosure_new (callback_func, object, NULL);
3993 g_object_watch_closure (object, closure);
3999 * g_cclosure_new_object_swap: (skip)
4000 * @callback_func: the function to invoke
4001 * @object: a #GObject pointer to pass to @callback_func
4003 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4004 * and calls g_object_watch_closure() on @object and the created
4005 * closure. This function is useful when you have a callback closely
4006 * associated with a #GObject, and want the callback to no longer run
4007 * after the object is is freed.
4009 * Returns: a new #GCClosure
4012 g_cclosure_new_object_swap (GCallback callback_func,
4017 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4018 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4019 g_return_val_if_fail (callback_func != NULL, NULL);
4021 closure = g_cclosure_new_swap (callback_func, object, NULL);
4022 g_object_watch_closure (object, closure);
4028 g_object_compat_control (gsize what,
4034 case 1: /* floating base type */
4035 return G_TYPE_INITIALLY_UNOWNED;
4036 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4037 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4039 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4041 *pp = floating_flag_handler;
4048 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
4051 g_initially_unowned_init (GInitiallyUnowned *object)
4053 g_object_force_floating (object);
4057 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4064 * A structure containing a weak reference to a #GObject. It can either
4065 * be empty (i.e. point to %NULL), or point to an object for as long as
4066 * at least one "strong" reference to that object exists. Before the
4067 * object's #GObjectClass.dispose method is called, every #GWeakRef
4068 * associated with becomes empty (i.e. points to %NULL).
4070 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4071 * heap-allocated, or embedded in larger structures.
4073 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4074 * reference is thread-safe: converting a weak pointer to a reference is
4075 * atomic with respect to invalidation of weak pointers to destroyed
4078 * If the object's #GObjectClass.dispose method results in additional
4079 * references to the object being held, any #GWeakRefs taken
4080 * before it was disposed will continue to point to %NULL. If
4081 * #GWeakRefs are taken after the object is disposed and
4082 * re-referenced, they will continue to point to it until its refcount
4083 * goes back to zero, at which point they too will be invalidated.
4087 * g_weak_ref_init: (skip)
4088 * @weak_ref: (inout): uninitialized or empty location for a weak
4090 * @object: (allow-none): a #GObject or %NULL
4092 * Initialise a non-statically-allocated #GWeakRef.
4094 * This function also calls g_weak_ref_set() with @object on the
4095 * freshly-initialised weak reference.
4097 * This function should always be matched with a call to
4098 * g_weak_ref_clear(). It is not necessary to use this function for a
4099 * #GWeakRef in static storage because it will already be
4100 * properly initialised. Just use g_weak_ref_set() directly.
4105 g_weak_ref_init (GWeakRef *weak_ref,
4108 weak_ref->priv.p = NULL;
4110 g_weak_ref_set (weak_ref, object);
4114 * g_weak_ref_clear: (skip)
4115 * @weak_ref: (inout): location of a weak reference, which
4118 * Frees resources associated with a non-statically-allocated #GWeakRef.
4119 * After this call, the #GWeakRef is left in an undefined state.
4121 * You should only call this on a #GWeakRef that previously had
4122 * g_weak_ref_init() called on it.
4127 g_weak_ref_clear (GWeakRef *weak_ref)
4129 g_weak_ref_set (weak_ref, NULL);
4132 weak_ref->priv.p = (void *) 0xccccccccu;
4136 * g_weak_ref_get: (skip)
4137 * @weak_ref: (inout): location of a weak reference to a #GObject
4139 * If @weak_ref is not empty, atomically acquire a strong
4140 * reference to the object it points to, and return that reference.
4142 * This function is needed because of the potential race between taking
4143 * the pointer value and g_object_ref() on it, if the object was losing
4144 * its last reference at the same time in a different thread.
4146 * The caller should release the resulting reference in the usual way,
4147 * by using g_object_unref().
4149 * Returns: (transfer full) (type GObject.Object): the object pointed to
4150 * by @weak_ref, or %NULL if it was empty
4155 g_weak_ref_get (GWeakRef *weak_ref)
4157 gpointer object_or_null;
4159 g_return_val_if_fail (weak_ref!= NULL, NULL);
4161 g_rw_lock_reader_lock (&weak_locations_lock);
4163 object_or_null = weak_ref->priv.p;
4165 if (object_or_null != NULL)
4166 g_object_ref (object_or_null);
4168 g_rw_lock_reader_unlock (&weak_locations_lock);
4170 return object_or_null;
4174 * g_weak_ref_set: (skip)
4175 * @weak_ref: location for a weak reference
4176 * @object: (allow-none): a #GObject or %NULL
4178 * Change the object to which @weak_ref points, or set it to
4181 * You must own a strong reference on @object while calling this
4187 g_weak_ref_set (GWeakRef *weak_ref,
4190 GSList **weak_locations;
4191 GObject *new_object;
4192 GObject *old_object;
4194 g_return_if_fail (weak_ref != NULL);
4195 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4197 new_object = object;
4199 g_rw_lock_writer_lock (&weak_locations_lock);
4201 /* We use the extra level of indirection here so that if we have ever
4202 * had a weak pointer installed at any point in time on this object,
4203 * we can see that there is a non-NULL value associated with the
4204 * weak-pointer quark and know that this value will not change at any
4205 * point in the object's lifetime.
4207 * Both properties are important for reducing the amount of times we
4208 * need to acquire locks and for decreasing the duration of time the
4209 * lock is held while avoiding some rather tricky races.
4211 * Specifically: we can avoid having to do an extra unconditional lock
4212 * in g_object_unref() without worrying about some extremely tricky
4216 old_object = weak_ref->priv.p;
4217 if (new_object != old_object)
4219 weak_ref->priv.p = new_object;
4221 /* Remove the weak ref from the old object */
4222 if (old_object != NULL)
4224 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4225 /* for it to point to an object, the object must have had it added once */
4226 g_assert (weak_locations != NULL);
4228 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4231 /* Add the weak ref to the new object */
4232 if (new_object != NULL)
4234 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4236 if (weak_locations == NULL)
4238 weak_locations = g_new0 (GSList *, 1);
4239 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4242 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4246 g_rw_lock_writer_unlock (&weak_locations_lock);