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.
528 * All properties should be installed during the class initializer. It
529 * is possible to install properties after that, but doing so is not
530 * recommend, and specifically, is not guaranteed to be thread-safe vs.
531 * use of properties on the same type on other threads.
533 * Note that it is possible to redefine a property in a derived class,
534 * by installing a property with the same name. This can be useful at times,
535 * e.g. to change the range of allowed values or the default value.
538 g_object_class_install_property (GObjectClass *class,
542 g_return_if_fail (G_IS_OBJECT_CLASS (class));
543 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
545 if (CLASS_HAS_DERIVED_CLASS (class))
546 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
548 class->flags |= CLASS_HAS_PROPS_FLAG;
550 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
551 if (pspec->flags & G_PARAM_WRITABLE)
552 g_return_if_fail (class->set_property != NULL);
553 if (pspec->flags & G_PARAM_READABLE)
554 g_return_if_fail (class->get_property != NULL);
555 g_return_if_fail (property_id > 0);
556 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
557 if (pspec->flags & G_PARAM_CONSTRUCT)
558 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
559 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
560 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
562 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
564 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
565 class->construct_properties = g_slist_append (class->construct_properties, pspec);
567 /* for property overrides of construct properties, we have to get rid
568 * of the overidden inherited construct property
570 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
571 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
576 * g_object_class_install_properties:
577 * @oclass: a #GObjectClass
578 * @n_pspecs: the length of the #GParamSpecs array
579 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
580 * defining the new properties
582 * Installs new properties from an array of #GParamSpecs.
584 * All properties should be installed during the class initializer. It
585 * is possible to install properties after that, but doing so is not
586 * recommend, and specifically, is not guaranteed to be thread-safe vs.
587 * use of properties on the same type on other threads.
589 * The property id of each property is the index of each #GParamSpec in
592 * The property id of 0 is treated specially by #GObject and it should not
593 * be used to store a #GParamSpec.
595 * This function should be used if you plan to use a static array of
596 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
597 * class initialization:
599 * |[<!-- language="C" -->
601 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
604 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
607 * my_object_class_init (MyObjectClass *klass)
609 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
611 * obj_properties[PROP_FOO] =
612 * g_param_spec_int ("foo", "Foo", "Foo",
615 * G_PARAM_READWRITE);
617 * obj_properties[PROP_BAR] =
618 * g_param_spec_string ("bar", "Bar", "Bar",
620 * G_PARAM_READWRITE);
622 * gobject_class->set_property = my_object_set_property;
623 * gobject_class->get_property = my_object_get_property;
624 * g_object_class_install_properties (gobject_class,
630 * allows calling g_object_notify_by_pspec() to notify of property changes:
632 * |[<!-- language="C" -->
634 * my_object_set_foo (MyObject *self, gint foo)
636 * if (self->foo != foo)
639 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
647 g_object_class_install_properties (GObjectClass *oclass,
651 GType oclass_type, parent_type;
654 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
655 g_return_if_fail (n_pspecs > 1);
656 g_return_if_fail (pspecs[0] == NULL);
658 if (CLASS_HAS_DERIVED_CLASS (oclass))
659 g_error ("Attempt to add properties to %s after it was derived",
660 G_OBJECT_CLASS_NAME (oclass));
662 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
663 parent_type = g_type_parent (oclass_type);
665 /* we skip the first element of the array as it would have a 0 prop_id */
666 for (i = 1; i < n_pspecs; i++)
668 GParamSpec *pspec = pspecs[i];
670 g_return_if_fail (pspec != NULL);
672 if (pspec->flags & G_PARAM_WRITABLE)
673 g_return_if_fail (oclass->set_property != NULL);
674 if (pspec->flags & G_PARAM_READABLE)
675 g_return_if_fail (oclass->get_property != NULL);
676 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
677 if (pspec->flags & G_PARAM_CONSTRUCT)
678 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
679 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
680 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
682 oclass->flags |= CLASS_HAS_PROPS_FLAG;
683 install_property_internal (oclass_type, i, pspec);
685 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
686 oclass->construct_properties = g_slist_append (oclass->construct_properties, pspec);
688 /* for property overrides of construct properties, we have to get rid
689 * of the overidden inherited construct property
691 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
692 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
693 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
698 * g_object_interface_install_property:
699 * @g_iface: any interface vtable for the interface, or the default
700 * vtable for the interface.
701 * @pspec: the #GParamSpec for the new property
703 * Add a property to an interface; this is only useful for interfaces
704 * that are added to GObject-derived types. Adding a property to an
705 * interface forces all objects classes with that interface to have a
706 * compatible property. The compatible property could be a newly
707 * created #GParamSpec, but normally
708 * g_object_class_override_property() will be used so that the object
709 * class only needs to provide an implementation and inherits the
710 * property description, default value, bounds, and so forth from the
711 * interface property.
713 * This function is meant to be called from the interface's default
714 * vtable initialization function (the @class_init member of
715 * #GTypeInfo.) It must not be called after after @class_init has
716 * been called for any object types implementing this interface.
721 g_object_interface_install_property (gpointer g_iface,
724 GTypeInterface *iface_class = g_iface;
726 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
727 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
728 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
729 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
731 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
732 if (pspec->flags & G_PARAM_CONSTRUCT)
733 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
734 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
735 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
737 install_property_internal (iface_class->g_type, 0, pspec);
741 * g_object_class_find_property:
742 * @oclass: a #GObjectClass
743 * @property_name: the name of the property to look up
745 * Looks up the #GParamSpec for a property of a class.
747 * Returns: (transfer none): the #GParamSpec for the property, or
748 * %NULL if the class doesn't have a property of that name
751 g_object_class_find_property (GObjectClass *class,
752 const gchar *property_name)
755 GParamSpec *redirect;
757 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
758 g_return_val_if_fail (property_name != NULL, NULL);
760 pspec = g_param_spec_pool_lookup (pspec_pool,
762 G_OBJECT_CLASS_TYPE (class),
766 redirect = g_param_spec_get_redirect_target (pspec);
777 * g_object_interface_find_property:
778 * @g_iface: any interface vtable for the interface, or the default
779 * vtable for the interface
780 * @property_name: name of a property to lookup.
782 * Find the #GParamSpec with the given name for an
783 * interface. Generally, the interface vtable passed in as @g_iface
784 * will be the default vtable from g_type_default_interface_ref(), or,
785 * if you know the interface has already been loaded,
786 * g_type_default_interface_peek().
790 * Returns: (transfer none): the #GParamSpec for the property of the
791 * interface with the name @property_name, or %NULL if no
792 * such property exists.
795 g_object_interface_find_property (gpointer g_iface,
796 const gchar *property_name)
798 GTypeInterface *iface_class = g_iface;
800 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
801 g_return_val_if_fail (property_name != NULL, NULL);
803 return g_param_spec_pool_lookup (pspec_pool,
810 * g_object_class_override_property:
811 * @oclass: a #GObjectClass
812 * @property_id: the new property ID
813 * @name: the name of a property registered in a parent class or
814 * in an interface of this class.
816 * Registers @property_id as referring to a property with the name
817 * @name in a parent class or in an interface implemented by @oclass.
818 * This allows this class to "override" a property implementation in
819 * a parent class or to provide the implementation of a property from
822 * Internally, overriding is implemented by creating a property of type
823 * #GParamSpecOverride; generally operations that query the properties of
824 * the object class, such as g_object_class_find_property() or
825 * g_object_class_list_properties() will return the overridden
826 * property. However, in one case, the @construct_properties argument of
827 * the @constructor virtual function, the #GParamSpecOverride is passed
828 * instead, so that the @param_id field of the #GParamSpec will be
829 * correct. For virtually all uses, this makes no difference. If you
830 * need to get the overridden property, you can call
831 * g_param_spec_get_redirect_target().
836 g_object_class_override_property (GObjectClass *oclass,
840 GParamSpec *overridden = NULL;
844 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
845 g_return_if_fail (property_id > 0);
846 g_return_if_fail (name != NULL);
848 /* Find the overridden property; first check parent types
850 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
851 if (parent_type != G_TYPE_NONE)
852 overridden = g_param_spec_pool_lookup (pspec_pool,
861 /* Now check interfaces
863 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
864 while (n_ifaces-- && !overridden)
866 overridden = g_param_spec_pool_lookup (pspec_pool,
877 g_warning ("%s: Can't find property to override for '%s::%s'",
878 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
882 new = g_param_spec_override (name, overridden);
883 g_object_class_install_property (oclass, property_id, new);
887 * g_object_class_list_properties:
888 * @oclass: a #GObjectClass
889 * @n_properties: (out): return location for the length of the returned array
891 * Get an array of #GParamSpec* for all properties of a class.
893 * Returns: (array length=n_properties) (transfer container): an array of
894 * #GParamSpec* which should be freed after use
896 GParamSpec** /* free result */
897 g_object_class_list_properties (GObjectClass *class,
898 guint *n_properties_p)
903 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
905 pspecs = g_param_spec_pool_list (pspec_pool,
906 G_OBJECT_CLASS_TYPE (class),
915 * g_object_interface_list_properties:
916 * @g_iface: any interface vtable for the interface, or the default
917 * vtable for the interface
918 * @n_properties_p: (out): location to store number of properties returned.
920 * Lists the properties of an interface.Generally, the interface
921 * vtable passed in as @g_iface will be the default vtable from
922 * g_type_default_interface_ref(), or, if you know the interface has
923 * already been loaded, g_type_default_interface_peek().
927 * Returns: (array length=n_properties_p) (transfer container): a
928 * pointer to an array of pointers to #GParamSpec
929 * structures. The paramspecs are owned by GLib, but the
930 * array should be freed with g_free() when you are done with
934 g_object_interface_list_properties (gpointer g_iface,
935 guint *n_properties_p)
937 GTypeInterface *iface_class = g_iface;
941 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
943 pspecs = g_param_spec_pool_list (pspec_pool,
952 static inline gboolean
953 object_in_construction (GObject *object)
955 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
959 g_object_init (GObject *object,
962 object->ref_count = 1;
963 object->qdata = NULL;
965 if (CLASS_HAS_PROPS (class))
967 /* freeze object's notification queue, g_object_newv() preserves pairedness */
968 g_object_notify_queue_freeze (object, FALSE);
971 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
973 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
974 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
977 #ifdef G_ENABLE_DEBUG
980 G_LOCK (debug_objects);
981 debug_objects_count++;
982 g_hash_table_insert (debug_objects_ht, object, object);
983 G_UNLOCK (debug_objects);
985 #endif /* G_ENABLE_DEBUG */
989 g_object_do_set_property (GObject *object,
997 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1003 g_object_do_get_property (GObject *object,
1008 switch (property_id)
1011 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1017 g_object_real_dispose (GObject *object)
1019 g_signal_handlers_destroy (object);
1020 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1021 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1025 g_object_finalize (GObject *object)
1027 if (object_in_construction (object))
1029 g_critical ("object %s %p finalized while still in-construction",
1030 G_OBJECT_TYPE_NAME (object), object);
1033 g_datalist_clear (&object->qdata);
1035 #ifdef G_ENABLE_DEBUG
1038 G_LOCK (debug_objects);
1039 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1040 g_hash_table_remove (debug_objects_ht, object);
1041 debug_objects_count--;
1042 G_UNLOCK (debug_objects);
1044 #endif /* G_ENABLE_DEBUG */
1049 g_object_dispatch_properties_changed (GObject *object,
1051 GParamSpec **pspecs)
1055 for (i = 0; i < n_pspecs; i++)
1056 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1060 * g_object_run_dispose:
1061 * @object: a #GObject
1063 * Releases all references to other objects. This can be used to break
1066 * This functions should only be called from object system implementations.
1069 g_object_run_dispose (GObject *object)
1071 g_return_if_fail (G_IS_OBJECT (object));
1072 g_return_if_fail (object->ref_count > 0);
1074 g_object_ref (object);
1075 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1076 G_OBJECT_GET_CLASS (object)->dispose (object);
1077 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1078 g_object_unref (object);
1082 * g_object_freeze_notify:
1083 * @object: a #GObject
1085 * Increases the freeze count on @object. If the freeze count is
1086 * non-zero, the emission of "notify" signals on @object is
1087 * stopped. The signals are queued until the freeze count is decreased
1088 * to zero. Duplicate notifications are squashed so that at most one
1089 * #GObject::notify signal is emitted for each property modified while the
1092 * This is necessary for accessors that modify multiple properties to prevent
1093 * premature notification while the object is still being modified.
1096 g_object_freeze_notify (GObject *object)
1098 g_return_if_fail (G_IS_OBJECT (object));
1100 if (g_atomic_int_get (&object->ref_count) == 0)
1103 g_object_ref (object);
1104 g_object_notify_queue_freeze (object, FALSE);
1105 g_object_unref (object);
1109 get_notify_pspec (GParamSpec *pspec)
1111 GParamSpec *redirected;
1113 /* we don't notify on non-READABLE parameters */
1114 if (~pspec->flags & G_PARAM_READABLE)
1117 /* if the paramspec is redirected, notify on the target */
1118 redirected = g_param_spec_get_redirect_target (pspec);
1119 if (redirected != NULL)
1122 /* else, notify normally */
1127 g_object_notify_by_spec_internal (GObject *object,
1130 GParamSpec *notify_pspec;
1132 notify_pspec = get_notify_pspec (pspec);
1134 if (notify_pspec != NULL)
1136 GObjectNotifyQueue *nqueue;
1138 /* conditional freeze: only increase freeze count if already frozen */
1139 nqueue = g_object_notify_queue_freeze (object, TRUE);
1143 /* we're frozen, so add to the queue and release our freeze */
1144 g_object_notify_queue_add (object, nqueue, notify_pspec);
1145 g_object_notify_queue_thaw (object, nqueue);
1148 /* not frozen, so just dispatch the notification directly */
1149 G_OBJECT_GET_CLASS (object)
1150 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1156 * @object: a #GObject
1157 * @property_name: the name of a property installed on the class of @object.
1159 * Emits a "notify" signal for the property @property_name on @object.
1161 * When possible, eg. when signaling a property change from within the class
1162 * that registered the property, you should use g_object_notify_by_pspec()
1165 * Note that emission of the notify signal may be blocked with
1166 * g_object_freeze_notify(). In this case, the signal emissions are queued
1167 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1171 g_object_notify (GObject *object,
1172 const gchar *property_name)
1176 g_return_if_fail (G_IS_OBJECT (object));
1177 g_return_if_fail (property_name != NULL);
1178 if (g_atomic_int_get (&object->ref_count) == 0)
1181 g_object_ref (object);
1182 /* We don't need to get the redirect target
1183 * (by, e.g. calling g_object_class_find_property())
1184 * because g_object_notify_queue_add() does that
1186 pspec = g_param_spec_pool_lookup (pspec_pool,
1188 G_OBJECT_TYPE (object),
1192 g_warning ("%s: object class '%s' has no property named '%s'",
1194 G_OBJECT_TYPE_NAME (object),
1197 g_object_notify_by_spec_internal (object, pspec);
1198 g_object_unref (object);
1202 * g_object_notify_by_pspec:
1203 * @object: a #GObject
1204 * @pspec: the #GParamSpec of a property installed on the class of @object.
1206 * Emits a "notify" signal for the property specified by @pspec on @object.
1208 * This function omits the property name lookup, hence it is faster than
1209 * g_object_notify().
1211 * One way to avoid using g_object_notify() from within the
1212 * class that registered the properties, and using g_object_notify_by_pspec()
1213 * instead, is to store the GParamSpec used with
1214 * g_object_class_install_property() inside a static array, e.g.:
1216 *|[<!-- language="C" -->
1224 * static GParamSpec *properties[PROP_LAST];
1227 * my_object_class_init (MyObjectClass *klass)
1229 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1232 * G_PARAM_READWRITE);
1233 * g_object_class_install_property (gobject_class,
1235 * properties[PROP_FOO]);
1239 * and then notify a change on the "foo" property with:
1241 * |[<!-- language="C" -->
1242 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1248 g_object_notify_by_pspec (GObject *object,
1252 g_return_if_fail (G_IS_OBJECT (object));
1253 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1255 if (g_atomic_int_get (&object->ref_count) == 0)
1258 g_object_ref (object);
1259 g_object_notify_by_spec_internal (object, pspec);
1260 g_object_unref (object);
1264 * g_object_thaw_notify:
1265 * @object: a #GObject
1267 * Reverts the effect of a previous call to
1268 * g_object_freeze_notify(). The freeze count is decreased on @object
1269 * and when it reaches zero, queued "notify" signals are emitted.
1271 * Duplicate notifications for each property are squashed so that at most one
1272 * #GObject::notify signal is emitted for each property, in the reverse order
1273 * in which they have been queued.
1275 * It is an error to call this function when the freeze count is zero.
1278 g_object_thaw_notify (GObject *object)
1280 GObjectNotifyQueue *nqueue;
1282 g_return_if_fail (G_IS_OBJECT (object));
1283 if (g_atomic_int_get (&object->ref_count) == 0)
1286 g_object_ref (object);
1288 /* FIXME: Freezing is the only way to get at the notify queue.
1289 * So we freeze once and then thaw twice.
1291 nqueue = g_object_notify_queue_freeze (object, FALSE);
1292 g_object_notify_queue_thaw (object, nqueue);
1293 g_object_notify_queue_thaw (object, nqueue);
1295 g_object_unref (object);
1299 object_get_property (GObject *object,
1303 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1304 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1305 GParamSpec *redirect;
1309 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1310 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1314 redirect = g_param_spec_get_redirect_target (pspec);
1318 class->get_property (object, param_id, value, pspec);
1322 object_set_property (GObject *object,
1324 const GValue *value,
1325 GObjectNotifyQueue *nqueue)
1327 GValue tmp_value = G_VALUE_INIT;
1328 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1329 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1330 GParamSpec *redirect;
1331 static const gchar * enable_diagnostic = NULL;
1335 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1336 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1340 redirect = g_param_spec_get_redirect_target (pspec);
1344 if (G_UNLIKELY (!enable_diagnostic))
1346 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1347 if (!enable_diagnostic)
1348 enable_diagnostic = "0";
1351 if (enable_diagnostic[0] == '1')
1353 if (pspec->flags & G_PARAM_DEPRECATED)
1355 /* don't warn for automatically provided construct properties */
1356 if (!(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)) ||
1357 !object_in_construction (object))
1359 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1360 "anymore. It will be removed in a future version.",
1361 G_OBJECT_TYPE_NAME (object), pspec->name);
1366 /* provide a copy to work from, convert (if necessary) and validate */
1367 g_value_init (&tmp_value, pspec->value_type);
1368 if (!g_value_transform (value, &tmp_value))
1369 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1371 g_type_name (pspec->value_type),
1372 G_VALUE_TYPE_NAME (value));
1373 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1375 gchar *contents = g_strdup_value_contents (value);
1377 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1379 G_VALUE_TYPE_NAME (value),
1381 g_type_name (pspec->value_type));
1386 class->set_property (object, param_id, &tmp_value, pspec);
1388 if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY)
1390 GParamSpec *notify_pspec;
1392 notify_pspec = get_notify_pspec (pspec);
1394 if (notify_pspec != NULL)
1395 g_object_notify_queue_add (object, nqueue, notify_pspec);
1398 g_value_unset (&tmp_value);
1402 object_interface_check_properties (gpointer check_data,
1405 GTypeInterface *iface_class = g_iface;
1406 GObjectClass *class;
1407 GType iface_type = iface_class->g_type;
1408 GParamSpec **pspecs;
1411 class = g_type_class_ref (iface_class->g_instance_type);
1416 if (!G_IS_OBJECT_CLASS (class))
1419 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1423 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1425 G_OBJECT_CLASS_TYPE (class),
1430 g_critical ("Object class %s doesn't implement property "
1431 "'%s' from interface '%s'",
1432 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1434 g_type_name (iface_type));
1439 /* We do a number of checks on the properties of an interface to
1440 * make sure that all classes implementing the interface are
1441 * overriding the properties in a sane way.
1443 * We do the checks in order of importance so that we can give
1444 * more useful error messages first.
1446 * First, we check that the implementation doesn't remove the
1447 * basic functionality (readability, writability) advertised by
1448 * the interface. Next, we check that it doesn't introduce
1449 * additional restrictions (such as construct-only). Finally, we
1450 * make sure the types are compatible.
1453 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1454 /* If the property on the interface is readable then the
1455 * implementation must be readable. If the interface is writable
1456 * then the implementation must be writable.
1458 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1460 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1461 "property on interface '%s'\n", pspecs[n]->name,
1462 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1466 /* If the property on the interface is writable then we need to
1467 * make sure the implementation doesn't introduce new restrictions
1468 * on that writability (ie: construct-only).
1470 * If the interface was not writable to begin with then we don't
1471 * really have any problems here because "writable at construct
1472 * type only" is still more permissive than "read only".
1474 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1476 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1478 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1479 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1480 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1486 /* If the property on the interface is readable then we are
1487 * effectively advertising that reading the property will return a
1488 * value of a specific type. All implementations of the interface
1489 * need to return items of this type -- but may be more
1490 * restrictive. For example, it is legal to have:
1492 * GtkWidget *get_item();
1494 * that is implemented by a function that always returns a
1495 * GtkEntry. In short: readability implies that the
1496 * implementation value type must be equal or more restrictive.
1498 * Similarly, if the property on the interface is writable then
1499 * must be able to accept the property being set to any value of
1500 * that type, including subclasses. In this case, we may also be
1501 * less restrictive. For example, it is legal to have:
1503 * set_item (GtkEntry *);
1505 * that is implemented by a function that will actually work with
1506 * any GtkWidget. In short: writability implies that the
1507 * implementation value type must be equal or less restrictive.
1509 * In the case that the property is both readable and writable
1510 * then the only way that both of the above can be satisfied is
1511 * with a type that is exactly equal.
1513 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1515 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1516 /* class pspec value type must have exact equality with interface */
1517 if (pspecs[n]->value_type != class_pspec->value_type)
1518 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1519 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1520 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1521 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1524 case G_PARAM_READABLE:
1525 /* class pspec value type equal or more restrictive than interface */
1526 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1527 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1528 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1529 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1530 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1533 case G_PARAM_WRITABLE:
1534 /* class pspec value type equal or less restrictive than interface */
1535 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1536 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1537 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1538 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1539 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1543 g_assert_not_reached ();
1550 g_type_class_unref (class);
1554 g_object_get_type (void)
1556 return G_TYPE_OBJECT;
1560 * g_object_new: (skip)
1561 * @object_type: the type id of the #GObject subtype to instantiate
1562 * @first_property_name: the name of the first property
1563 * @...: the value of the first property, followed optionally by more
1564 * name/value pairs, followed by %NULL
1566 * Creates a new instance of a #GObject subtype and sets its properties.
1568 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1569 * which are not explicitly specified are set to their default values.
1571 * Returns: (transfer full): a new instance of @object_type
1574 g_object_new (GType object_type,
1575 const gchar *first_property_name,
1581 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1583 /* short circuit for calls supplying no properties */
1584 if (!first_property_name)
1585 return g_object_newv (object_type, 0, NULL);
1587 va_start (var_args, first_property_name);
1588 object = g_object_new_valist (object_type, first_property_name, var_args);
1595 g_object_new_with_custom_constructor (GObjectClass *class,
1596 GObjectConstructParam *params,
1599 GObjectNotifyQueue *nqueue = NULL;
1600 gboolean newly_constructed;
1601 GObjectConstructParam *cparams;
1609 /* If we have ->constructed() then we have to do a lot more work.
1610 * It's possible that this is a singleton and it's also possible
1611 * that the user's constructor() will attempt to modify the values
1612 * that we pass in, so we'll need to allocate copies of them.
1613 * It's also possible that the user may attempt to call
1614 * g_object_set() from inside of their constructor, so we need to
1615 * add ourselves to a list of objects for which that is allowed
1616 * while their constructor() is running.
1619 /* Create the array of GObjectConstructParams for constructor() */
1620 n_cparams = g_slist_length (class->construct_properties);
1621 cparams = g_new (GObjectConstructParam, n_cparams);
1622 cvalues = g_new0 (GValue, n_cparams);
1626 /* As above, we may find the value in the passed-in params list.
1628 * If we have the value passed in then we can use the GValue from
1629 * it directly because it is safe to modify. If we use the
1630 * default value from the class, we had better not pass that in
1631 * and risk it being modified, so we create a new one.
1633 for (node = class->construct_properties; node; node = node->next)
1640 value = NULL; /* to silence gcc... */
1642 for (j = 0; j < n_params; j++)
1643 if (params[j].pspec == pspec)
1645 value = params[j].value;
1651 value = &cvalues[cvals_used++];
1652 g_value_init (value, pspec->value_type);
1653 g_param_value_set_default (pspec, value);
1656 cparams[i].pspec = pspec;
1657 cparams[i].value = value;
1661 /* construct object from construction parameters */
1662 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1663 /* free construction values */
1665 while (cvals_used--)
1666 g_value_unset (&cvalues[cvals_used]);
1669 /* There is code in the wild that relies on being able to return NULL
1670 * from its custom constructor. This was never a supported operation,
1671 * but since the code is already out there...
1675 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1676 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1680 /* g_object_init() will have marked the object as being in-construction.
1681 * Check if the returned object still is so marked, or if this is an
1682 * already-existing singleton (in which case we should not do 'constructed').
1684 newly_constructed = object_in_construction (object);
1685 if (newly_constructed)
1686 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1688 if (CLASS_HAS_PROPS (class))
1690 /* If this object was newly_constructed then g_object_init()
1691 * froze the queue. We need to freeze it here in order to get
1692 * the handle so that we can thaw it below (otherwise it will
1693 * be frozen forever).
1695 * We also want to do a freeze if we have any params to set,
1696 * even on a non-newly_constructed object.
1698 * It's possible that we have the case of non-newly created
1699 * singleton and all of the passed-in params were construct
1700 * properties so n_params > 0 but we will actually set no
1701 * properties. This is a pretty lame case to optimise, so
1702 * just ignore it and freeze anyway.
1704 if (newly_constructed || n_params)
1705 nqueue = g_object_notify_queue_freeze (object, FALSE);
1707 /* Remember: if it was newly_constructed then g_object_init()
1708 * already did a freeze, so we now have two. Release one.
1710 if (newly_constructed)
1711 g_object_notify_queue_thaw (object, nqueue);
1714 /* run 'constructed' handler if there is a custom one */
1715 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1716 class->constructed (object);
1718 /* set remaining properties */
1719 for (i = 0; i < n_params; i++)
1720 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1721 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1723 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1725 g_object_notify_queue_thaw (object, nqueue);
1731 g_object_new_internal (GObjectClass *class,
1732 GObjectConstructParam *params,
1735 GObjectNotifyQueue *nqueue = NULL;
1738 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1739 return g_object_new_with_custom_constructor (class, params, n_params);
1741 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1743 if (CLASS_HAS_PROPS (class))
1747 /* This will have been setup in g_object_init() */
1748 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1749 g_assert (nqueue != NULL);
1751 /* We will set exactly n_construct_properties construct
1752 * properties, but they may come from either the class default
1753 * values or the passed-in parameter list.
1755 for (node = class->construct_properties; node; node = node->next)
1757 const GValue *value;
1762 value = NULL; /* to silence gcc... */
1764 for (j = 0; j < n_params; j++)
1765 if (params[j].pspec == pspec)
1767 value = params[j].value;
1772 value = g_param_spec_get_default_value (pspec);
1774 object_set_property (object, pspec, value, nqueue);
1778 /* run 'constructed' handler if there is a custom one */
1779 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1780 class->constructed (object);
1786 /* Set remaining properties. The construct properties will
1787 * already have been taken, so set only the non-construct
1790 for (i = 0; i < n_params; i++)
1791 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1792 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1794 g_object_notify_queue_thaw (object, nqueue);
1802 * @object_type: the type id of the #GObject subtype to instantiate
1803 * @n_parameters: the length of the @parameters array
1804 * @parameters: (array length=n_parameters): an array of #GParameter
1806 * Creates a new instance of a #GObject subtype and sets its properties.
1808 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1809 * which are not explicitly specified are set to their default values.
1811 * Rename to: g_object_new
1812 * Returns: (type GObject.Object) (transfer full): a new instance of
1816 g_object_newv (GType object_type,
1818 GParameter *parameters)
1820 GObjectClass *class, *unref_class = NULL;
1823 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1824 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1826 /* Try to avoid thrashing the ref_count if we don't need to (since
1827 * it's a locked operation).
1829 class = g_type_class_peek_static (object_type);
1832 class = unref_class = g_type_class_ref (object_type);
1836 GObjectConstructParam *cparams;
1839 cparams = g_newa (GObjectConstructParam, n_parameters);
1842 for (i = 0; i < n_parameters; i++)
1847 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
1849 if G_UNLIKELY (!pspec)
1851 g_critical ("%s: object class '%s' has no property named '%s'",
1852 G_STRFUNC, g_type_name (object_type), parameters[i].name);
1856 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1858 g_critical ("%s: property '%s' of object class '%s' is not writable",
1859 G_STRFUNC, pspec->name, g_type_name (object_type));
1863 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1865 for (k = 0; k < j; k++)
1866 if (cparams[k].pspec == pspec)
1868 if G_UNLIKELY (k != j)
1870 g_critical ("%s: construct property '%s' for type '%s' cannot be set twice",
1871 G_STRFUNC, parameters[i].name, g_type_name (object_type));
1876 cparams[j].pspec = pspec;
1877 cparams[j].value = ¶meters[i].value;
1881 object = g_object_new_internal (class, cparams, j);
1884 /* Fast case: no properties passed in. */
1885 object = g_object_new_internal (class, NULL, 0);
1888 g_type_class_unref (unref_class);
1894 * g_object_new_valist: (skip)
1895 * @object_type: the type id of the #GObject subtype to instantiate
1896 * @first_property_name: the name of the first property
1897 * @var_args: the value of the first property, followed optionally by more
1898 * name/value pairs, followed by %NULL
1900 * Creates a new instance of a #GObject subtype and sets its properties.
1902 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1903 * which are not explicitly specified are set to their default values.
1905 * Returns: a new instance of @object_type
1908 g_object_new_valist (GType object_type,
1909 const gchar *first_property_name,
1912 GObjectClass *class, *unref_class = NULL;
1915 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1917 /* Try to avoid thrashing the ref_count if we don't need to (since
1918 * it's a locked operation).
1920 class = g_type_class_peek_static (object_type);
1923 class = unref_class = g_type_class_ref (object_type);
1925 if (first_property_name)
1927 GObjectConstructParam stack_params[16];
1928 GObjectConstructParam *params;
1932 name = first_property_name;
1933 params = stack_params;
1937 gchar *error = NULL;
1941 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
1943 if G_UNLIKELY (!pspec)
1945 g_critical ("%s: object class '%s' has no property named '%s'",
1946 G_STRFUNC, g_type_name (object_type), name);
1947 /* Can't continue because arg list will be out of sync. */
1951 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1953 g_critical ("%s: property '%s' of object class '%s' is not writable",
1954 G_STRFUNC, pspec->name, g_type_name (object_type));
1958 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1960 for (i = 0; i < n_params; i++)
1961 if (params[i].pspec == pspec)
1963 if G_UNLIKELY (i != n_params)
1965 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1966 G_STRFUNC, name, g_type_name (object_type));
1973 params = g_new (GObjectConstructParam, n_params + 1);
1974 memcpy (params, stack_params, sizeof stack_params);
1976 else if (n_params > 16)
1977 params = g_renew (GObjectConstructParam, params, n_params + 1);
1979 params[n_params].pspec = pspec;
1980 params[n_params].value = g_newa (GValue, 1);
1981 memset (params[n_params].value, 0, sizeof (GValue));
1983 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
1987 g_critical ("%s: %s", G_STRFUNC, error);
1988 g_value_unset (params[n_params].value);
1995 while ((name = va_arg (var_args, const gchar *)));
1997 object = g_object_new_internal (class, params, n_params);
2000 g_value_unset (params[n_params].value);
2002 if (params != stack_params)
2006 /* Fast case: no properties passed in. */
2007 object = g_object_new_internal (class, NULL, 0);
2010 g_type_class_unref (unref_class);
2016 g_object_constructor (GType type,
2017 guint n_construct_properties,
2018 GObjectConstructParam *construct_params)
2023 object = (GObject*) g_type_create_instance (type);
2025 /* set construction parameters */
2026 if (n_construct_properties)
2028 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2030 /* set construct properties */
2031 while (n_construct_properties--)
2033 GValue *value = construct_params->value;
2034 GParamSpec *pspec = construct_params->pspec;
2037 object_set_property (object, pspec, value, nqueue);
2039 g_object_notify_queue_thaw (object, nqueue);
2040 /* the notification queue is still frozen from g_object_init(), so
2041 * we don't need to handle it here, g_object_newv() takes
2050 g_object_constructed (GObject *object)
2052 /* empty default impl to allow unconditional upchaining */
2056 * g_object_set_valist: (skip)
2057 * @object: a #GObject
2058 * @first_property_name: name of the first property to set
2059 * @var_args: value for the first property, followed optionally by more
2060 * name/value pairs, followed by %NULL
2062 * Sets properties on an object.
2065 g_object_set_valist (GObject *object,
2066 const gchar *first_property_name,
2069 GObjectNotifyQueue *nqueue;
2072 g_return_if_fail (G_IS_OBJECT (object));
2074 g_object_ref (object);
2075 nqueue = g_object_notify_queue_freeze (object, FALSE);
2077 name = first_property_name;
2080 GValue value = G_VALUE_INIT;
2082 gchar *error = NULL;
2084 pspec = g_param_spec_pool_lookup (pspec_pool,
2086 G_OBJECT_TYPE (object),
2090 g_warning ("%s: object class '%s' has no property named '%s'",
2092 G_OBJECT_TYPE_NAME (object),
2096 if (!(pspec->flags & G_PARAM_WRITABLE))
2098 g_warning ("%s: property '%s' of object class '%s' is not writable",
2101 G_OBJECT_TYPE_NAME (object));
2104 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2106 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2107 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2111 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2115 g_warning ("%s: %s", G_STRFUNC, error);
2117 g_value_unset (&value);
2121 object_set_property (object, pspec, &value, nqueue);
2122 g_value_unset (&value);
2124 name = va_arg (var_args, gchar*);
2127 g_object_notify_queue_thaw (object, nqueue);
2128 g_object_unref (object);
2132 * g_object_get_valist: (skip)
2133 * @object: a #GObject
2134 * @first_property_name: name of the first property to get
2135 * @var_args: return location for the first property, followed optionally by more
2136 * name/return location pairs, followed by %NULL
2138 * Gets properties of an object.
2140 * In general, a copy is made of the property contents and the caller
2141 * is responsible for freeing the memory in the appropriate manner for
2142 * the type, for instance by calling g_free() or g_object_unref().
2144 * See g_object_get().
2147 g_object_get_valist (GObject *object,
2148 const gchar *first_property_name,
2153 g_return_if_fail (G_IS_OBJECT (object));
2155 g_object_ref (object);
2157 name = first_property_name;
2161 GValue value = G_VALUE_INIT;
2165 pspec = g_param_spec_pool_lookup (pspec_pool,
2167 G_OBJECT_TYPE (object),
2171 g_warning ("%s: object class '%s' has no property named '%s'",
2173 G_OBJECT_TYPE_NAME (object),
2177 if (!(pspec->flags & G_PARAM_READABLE))
2179 g_warning ("%s: property '%s' of object class '%s' is not readable",
2182 G_OBJECT_TYPE_NAME (object));
2186 g_value_init (&value, pspec->value_type);
2188 object_get_property (object, pspec, &value);
2190 G_VALUE_LCOPY (&value, var_args, 0, &error);
2193 g_warning ("%s: %s", G_STRFUNC, error);
2195 g_value_unset (&value);
2199 g_value_unset (&value);
2201 name = va_arg (var_args, gchar*);
2204 g_object_unref (object);
2208 * g_object_set: (skip)
2209 * @object: a #GObject
2210 * @first_property_name: name of the first property to set
2211 * @...: value for the first property, followed optionally by more
2212 * name/value pairs, followed by %NULL
2214 * Sets properties on an object.
2216 * Note that the "notify" signals are queued and only emitted (in
2217 * reverse order) after all properties have been set. See
2218 * g_object_freeze_notify().
2221 g_object_set (gpointer _object,
2222 const gchar *first_property_name,
2225 GObject *object = _object;
2228 g_return_if_fail (G_IS_OBJECT (object));
2230 va_start (var_args, first_property_name);
2231 g_object_set_valist (object, first_property_name, var_args);
2236 * g_object_get: (skip)
2237 * @object: a #GObject
2238 * @first_property_name: name of the first property to get
2239 * @...: return location for the first property, followed optionally by more
2240 * name/return location pairs, followed by %NULL
2242 * Gets properties of an object.
2244 * In general, a copy is made of the property contents and the caller
2245 * is responsible for freeing the memory in the appropriate manner for
2246 * the type, for instance by calling g_free() or g_object_unref().
2248 * Here is an example of using g_object_get() to get the contents
2249 * of three properties: an integer, a string and an object:
2250 * |[<!-- language="C" -->
2255 * g_object_get (my_object,
2256 * "int-property", &intval,
2257 * "str-property", &strval,
2258 * "obj-property", &objval,
2261 * // Do something with intval, strval, objval
2264 * g_object_unref (objval);
2268 g_object_get (gpointer _object,
2269 const gchar *first_property_name,
2272 GObject *object = _object;
2275 g_return_if_fail (G_IS_OBJECT (object));
2277 va_start (var_args, first_property_name);
2278 g_object_get_valist (object, first_property_name, var_args);
2283 * g_object_set_property:
2284 * @object: a #GObject
2285 * @property_name: the name of the property to set
2288 * Sets a property on an object.
2291 g_object_set_property (GObject *object,
2292 const gchar *property_name,
2293 const GValue *value)
2295 GObjectNotifyQueue *nqueue;
2298 g_return_if_fail (G_IS_OBJECT (object));
2299 g_return_if_fail (property_name != NULL);
2300 g_return_if_fail (G_IS_VALUE (value));
2302 g_object_ref (object);
2303 nqueue = g_object_notify_queue_freeze (object, FALSE);
2305 pspec = g_param_spec_pool_lookup (pspec_pool,
2307 G_OBJECT_TYPE (object),
2310 g_warning ("%s: object class '%s' has no property named '%s'",
2312 G_OBJECT_TYPE_NAME (object),
2314 else if (!(pspec->flags & G_PARAM_WRITABLE))
2315 g_warning ("%s: property '%s' of object class '%s' is not writable",
2318 G_OBJECT_TYPE_NAME (object));
2319 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2320 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2321 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2323 object_set_property (object, pspec, value, nqueue);
2325 g_object_notify_queue_thaw (object, nqueue);
2326 g_object_unref (object);
2330 * g_object_get_property:
2331 * @object: a #GObject
2332 * @property_name: the name of the property to get
2333 * @value: return location for the property value
2335 * Gets a property of an object. @value must have been initialized to the
2336 * expected type of the property (or a type to which the expected type can be
2337 * transformed) using g_value_init().
2339 * In general, a copy is made of the property contents and the caller is
2340 * responsible for freeing the memory by calling g_value_unset().
2342 * Note that g_object_get_property() is really intended for language
2343 * bindings, g_object_get() is much more convenient for C programming.
2346 g_object_get_property (GObject *object,
2347 const gchar *property_name,
2352 g_return_if_fail (G_IS_OBJECT (object));
2353 g_return_if_fail (property_name != NULL);
2354 g_return_if_fail (G_IS_VALUE (value));
2356 g_object_ref (object);
2358 pspec = g_param_spec_pool_lookup (pspec_pool,
2360 G_OBJECT_TYPE (object),
2363 g_warning ("%s: object class '%s' has no property named '%s'",
2365 G_OBJECT_TYPE_NAME (object),
2367 else if (!(pspec->flags & G_PARAM_READABLE))
2368 g_warning ("%s: property '%s' of object class '%s' is not readable",
2371 G_OBJECT_TYPE_NAME (object));
2374 GValue *prop_value, tmp_value = G_VALUE_INIT;
2376 /* auto-conversion of the callers value type
2378 if (G_VALUE_TYPE (value) == pspec->value_type)
2380 g_value_reset (value);
2383 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2385 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2386 G_STRFUNC, pspec->name,
2387 g_type_name (pspec->value_type),
2388 G_VALUE_TYPE_NAME (value));
2389 g_object_unref (object);
2394 g_value_init (&tmp_value, pspec->value_type);
2395 prop_value = &tmp_value;
2397 object_get_property (object, pspec, prop_value);
2398 if (prop_value != value)
2400 g_value_transform (prop_value, value);
2401 g_value_unset (&tmp_value);
2405 g_object_unref (object);
2409 * g_object_connect: (skip)
2410 * @object: a #GObject
2411 * @signal_spec: the spec for the first signal
2412 * @...: #GCallback for the first signal, followed by data for the
2413 * first signal, followed optionally by more signal
2414 * spec/callback/data triples, followed by %NULL
2416 * A convenience function to connect multiple signals at once.
2418 * The signal specs expected by this function have the form
2419 * "modifier::signal_name", where modifier can be one of the following:
2420 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2421 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2422 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2423 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2424 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2425 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2426 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2427 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2429 * |[<!-- language="C" -->
2430 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2431 * "type", GTK_WINDOW_POPUP,
2434 * "signal::event", gtk_menu_window_event, menu,
2435 * "signal::size_request", gtk_menu_window_size_request, menu,
2436 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2440 * Returns: (transfer none): @object
2443 g_object_connect (gpointer _object,
2444 const gchar *signal_spec,
2447 GObject *object = _object;
2450 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2451 g_return_val_if_fail (object->ref_count > 0, object);
2453 va_start (var_args, signal_spec);
2456 GCallback callback = va_arg (var_args, GCallback);
2457 gpointer data = va_arg (var_args, gpointer);
2459 if (strncmp (signal_spec, "signal::", 8) == 0)
2460 g_signal_connect_data (object, signal_spec + 8,
2461 callback, data, NULL,
2463 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2464 strncmp (signal_spec, "object-signal::", 15) == 0)
2465 g_signal_connect_object (object, signal_spec + 15,
2468 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2469 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2470 g_signal_connect_data (object, signal_spec + 16,
2471 callback, data, NULL,
2473 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2474 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2475 g_signal_connect_object (object, signal_spec + 23,
2478 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2479 strncmp (signal_spec, "signal-after::", 14) == 0)
2480 g_signal_connect_data (object, signal_spec + 14,
2481 callback, data, NULL,
2483 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2484 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2485 g_signal_connect_object (object, signal_spec + 21,
2488 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2489 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2490 g_signal_connect_data (object, signal_spec + 22,
2491 callback, data, NULL,
2492 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2493 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2494 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2495 g_signal_connect_object (object, signal_spec + 29,
2497 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2500 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2503 signal_spec = va_arg (var_args, gchar*);
2511 * g_object_disconnect: (skip)
2512 * @object: a #GObject
2513 * @signal_spec: the spec for the first signal
2514 * @...: #GCallback for the first signal, followed by data for the first signal,
2515 * followed optionally by more signal spec/callback/data triples,
2518 * A convenience function to disconnect multiple signals at once.
2520 * The signal specs expected by this function have the form
2521 * "any_signal", which means to disconnect any signal with matching
2522 * callback and data, or "any_signal::signal_name", which only
2523 * disconnects the signal named "signal_name".
2526 g_object_disconnect (gpointer _object,
2527 const gchar *signal_spec,
2530 GObject *object = _object;
2533 g_return_if_fail (G_IS_OBJECT (object));
2534 g_return_if_fail (object->ref_count > 0);
2536 va_start (var_args, signal_spec);
2539 GCallback callback = va_arg (var_args, GCallback);
2540 gpointer data = va_arg (var_args, gpointer);
2541 guint sid = 0, detail = 0, mask = 0;
2543 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2544 strncmp (signal_spec, "any-signal::", 12) == 0)
2547 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2549 else if (strcmp (signal_spec, "any_signal") == 0 ||
2550 strcmp (signal_spec, "any-signal") == 0)
2553 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2557 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2561 if ((mask & G_SIGNAL_MATCH_ID) &&
2562 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2563 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2564 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2566 NULL, (gpointer)callback, data))
2567 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2568 signal_spec = va_arg (var_args, gchar*);
2579 } weak_refs[1]; /* flexible array */
2583 weak_refs_notify (gpointer data)
2585 WeakRefStack *wstack = data;
2588 for (i = 0; i < wstack->n_weak_refs; i++)
2589 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2594 * g_object_weak_ref: (skip)
2595 * @object: #GObject to reference weakly
2596 * @notify: callback to invoke before the object is freed
2597 * @data: extra data to pass to notify
2599 * Adds a weak reference callback to an object. Weak references are
2600 * used for notification when an object is finalized. They are called
2601 * "weak references" because they allow you to safely hold a pointer
2602 * to an object without calling g_object_ref() (g_object_ref() adds a
2603 * strong reference, that is, forces the object to stay alive).
2605 * Note that the weak references created by this method are not
2606 * thread-safe: they cannot safely be used in one thread if the
2607 * object's last g_object_unref() might happen in another thread.
2608 * Use #GWeakRef if thread-safety is required.
2611 g_object_weak_ref (GObject *object,
2615 WeakRefStack *wstack;
2618 g_return_if_fail (G_IS_OBJECT (object));
2619 g_return_if_fail (notify != NULL);
2620 g_return_if_fail (object->ref_count >= 1);
2622 G_LOCK (weak_refs_mutex);
2623 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2626 i = wstack->n_weak_refs++;
2627 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2631 wstack = g_renew (WeakRefStack, NULL, 1);
2632 wstack->object = object;
2633 wstack->n_weak_refs = 1;
2636 wstack->weak_refs[i].notify = notify;
2637 wstack->weak_refs[i].data = data;
2638 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2639 G_UNLOCK (weak_refs_mutex);
2643 * g_object_weak_unref: (skip)
2644 * @object: #GObject to remove a weak reference from
2645 * @notify: callback to search for
2646 * @data: data to search for
2648 * Removes a weak reference callback to an object.
2651 g_object_weak_unref (GObject *object,
2655 WeakRefStack *wstack;
2656 gboolean found_one = FALSE;
2658 g_return_if_fail (G_IS_OBJECT (object));
2659 g_return_if_fail (notify != NULL);
2661 G_LOCK (weak_refs_mutex);
2662 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2667 for (i = 0; i < wstack->n_weak_refs; i++)
2668 if (wstack->weak_refs[i].notify == notify &&
2669 wstack->weak_refs[i].data == data)
2672 wstack->n_weak_refs -= 1;
2673 if (i != wstack->n_weak_refs)
2674 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2679 G_UNLOCK (weak_refs_mutex);
2681 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2685 * g_object_add_weak_pointer: (skip)
2686 * @object: The object that should be weak referenced.
2687 * @weak_pointer_location: (inout): The memory address of a pointer.
2689 * Adds a weak reference from weak_pointer to @object to indicate that
2690 * the pointer located at @weak_pointer_location is only valid during
2691 * the lifetime of @object. When the @object is finalized,
2692 * @weak_pointer will be set to %NULL.
2694 * Note that as with g_object_weak_ref(), the weak references created by
2695 * this method are not thread-safe: they cannot safely be used in one
2696 * thread if the object's last g_object_unref() might happen in another
2697 * thread. Use #GWeakRef if thread-safety is required.
2700 g_object_add_weak_pointer (GObject *object,
2701 gpointer *weak_pointer_location)
2703 g_return_if_fail (G_IS_OBJECT (object));
2704 g_return_if_fail (weak_pointer_location != NULL);
2706 g_object_weak_ref (object,
2707 (GWeakNotify) g_nullify_pointer,
2708 weak_pointer_location);
2712 * g_object_remove_weak_pointer: (skip)
2713 * @object: The object that is weak referenced.
2714 * @weak_pointer_location: (inout): The memory address of a pointer.
2716 * Removes a weak reference from @object that was previously added
2717 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2718 * to match the one used with g_object_add_weak_pointer().
2721 g_object_remove_weak_pointer (GObject *object,
2722 gpointer *weak_pointer_location)
2724 g_return_if_fail (G_IS_OBJECT (object));
2725 g_return_if_fail (weak_pointer_location != NULL);
2727 g_object_weak_unref (object,
2728 (GWeakNotify) g_nullify_pointer,
2729 weak_pointer_location);
2733 object_floating_flag_handler (GObject *object,
2739 case +1: /* force floating 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 | OBJECT_FLOATING_FLAG)));
2744 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2745 case -1: /* sink if possible */
2747 oldvalue = g_atomic_pointer_get (&object->qdata);
2748 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2749 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2750 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2751 default: /* check floating */
2752 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2757 * g_object_is_floating:
2758 * @object: (type GObject.Object): a #GObject
2760 * Checks whether @object has a [floating][floating-ref] reference.
2764 * Returns: %TRUE if @object has a floating reference
2767 g_object_is_floating (gpointer _object)
2769 GObject *object = _object;
2770 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2771 return floating_flag_handler (object, 0);
2775 * g_object_ref_sink:
2776 * @object: (type GObject.Object): a #GObject
2778 * Increase the reference count of @object, and possibly remove the
2779 * [floating][floating-ref] reference, if @object has a floating reference.
2781 * In other words, if the object is floating, then this call "assumes
2782 * ownership" of the floating reference, converting it to a normal
2783 * reference by clearing the floating flag while leaving the reference
2784 * count unchanged. If the object is not floating, then this call
2785 * adds a new normal reference increasing the reference count by one.
2789 * Returns: (type GObject.Object) (transfer none): @object
2792 g_object_ref_sink (gpointer _object)
2794 GObject *object = _object;
2795 gboolean was_floating;
2796 g_return_val_if_fail (G_IS_OBJECT (object), object);
2797 g_return_val_if_fail (object->ref_count >= 1, object);
2798 g_object_ref (object);
2799 was_floating = floating_flag_handler (object, -1);
2801 g_object_unref (object);
2806 * g_object_force_floating:
2807 * @object: a #GObject
2809 * This function is intended for #GObject implementations to re-enforce
2810 * a [floating][floating-ref] object reference. Doing this is seldom
2811 * required: all #GInitiallyUnowneds are created with a floating reference
2812 * which usually just needs to be sunken by calling g_object_ref_sink().
2817 g_object_force_floating (GObject *object)
2819 g_return_if_fail (G_IS_OBJECT (object));
2820 g_return_if_fail (object->ref_count >= 1);
2822 floating_flag_handler (object, +1);
2827 guint n_toggle_refs;
2829 GToggleNotify notify;
2831 } toggle_refs[1]; /* flexible array */
2835 toggle_refs_notify (GObject *object,
2836 gboolean is_last_ref)
2838 ToggleRefStack tstack, *tstackptr;
2840 G_LOCK (toggle_refs_mutex);
2841 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2842 tstack = *tstackptr;
2843 G_UNLOCK (toggle_refs_mutex);
2845 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2846 * will only be notified when there is exactly one of them.
2848 g_assert (tstack.n_toggle_refs == 1);
2849 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2853 * g_object_add_toggle_ref: (skip)
2854 * @object: a #GObject
2855 * @notify: a function to call when this reference is the
2856 * last reference to the object, or is no longer
2857 * the last reference.
2858 * @data: data to pass to @notify
2860 * Increases the reference count of the object by one and sets a
2861 * callback to be called when all other references to the object are
2862 * dropped, or when this is already the last reference to the object
2863 * and another reference is established.
2865 * This functionality is intended for binding @object to a proxy
2866 * object managed by another memory manager. This is done with two
2867 * paired references: the strong reference added by
2868 * g_object_add_toggle_ref() and a reverse reference to the proxy
2869 * object which is either a strong reference or weak reference.
2871 * The setup is that when there are no other references to @object,
2872 * only a weak reference is held in the reverse direction from @object
2873 * to the proxy object, but when there are other references held to
2874 * @object, a strong reference is held. The @notify callback is called
2875 * when the reference from @object to the proxy object should be
2876 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
2877 * (@is_last_ref false).
2879 * Since a (normal) reference must be held to the object before
2880 * calling g_object_add_toggle_ref(), the initial state of the reverse
2881 * link is always strong.
2883 * Multiple toggle references may be added to the same gobject,
2884 * however if there are multiple toggle references to an object, none
2885 * of them will ever be notified until all but one are removed. For
2886 * this reason, you should only ever use a toggle reference if there
2887 * is important state in the proxy object.
2892 g_object_add_toggle_ref (GObject *object,
2893 GToggleNotify notify,
2896 ToggleRefStack *tstack;
2899 g_return_if_fail (G_IS_OBJECT (object));
2900 g_return_if_fail (notify != NULL);
2901 g_return_if_fail (object->ref_count >= 1);
2903 g_object_ref (object);
2905 G_LOCK (toggle_refs_mutex);
2906 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2909 i = tstack->n_toggle_refs++;
2910 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2911 * in tstate->toggle_refs */
2912 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2916 tstack = g_renew (ToggleRefStack, NULL, 1);
2917 tstack->object = object;
2918 tstack->n_toggle_refs = 1;
2922 /* Set a flag for fast lookup after adding the first toggle reference */
2923 if (tstack->n_toggle_refs == 1)
2924 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2926 tstack->toggle_refs[i].notify = notify;
2927 tstack->toggle_refs[i].data = data;
2928 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2929 (GDestroyNotify)g_free);
2930 G_UNLOCK (toggle_refs_mutex);
2934 * g_object_remove_toggle_ref: (skip)
2935 * @object: a #GObject
2936 * @notify: a function to call when this reference is the
2937 * last reference to the object, or is no longer
2938 * the last reference.
2939 * @data: data to pass to @notify
2941 * Removes a reference added with g_object_add_toggle_ref(). The
2942 * reference count of the object is decreased by one.
2947 g_object_remove_toggle_ref (GObject *object,
2948 GToggleNotify notify,
2951 ToggleRefStack *tstack;
2952 gboolean found_one = FALSE;
2954 g_return_if_fail (G_IS_OBJECT (object));
2955 g_return_if_fail (notify != NULL);
2957 G_LOCK (toggle_refs_mutex);
2958 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2963 for (i = 0; i < tstack->n_toggle_refs; i++)
2964 if (tstack->toggle_refs[i].notify == notify &&
2965 tstack->toggle_refs[i].data == data)
2968 tstack->n_toggle_refs -= 1;
2969 if (i != tstack->n_toggle_refs)
2970 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2972 if (tstack->n_toggle_refs == 0)
2973 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2978 G_UNLOCK (toggle_refs_mutex);
2981 g_object_unref (object);
2983 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2988 * @object: (type GObject.Object): a #GObject
2990 * Increases the reference count of @object.
2992 * Returns: (type GObject.Object) (transfer none): the same @object
2995 g_object_ref (gpointer _object)
2997 GObject *object = _object;
3000 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3001 g_return_val_if_fail (object->ref_count > 0, NULL);
3003 old_val = g_atomic_int_add (&object->ref_count, 1);
3005 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3006 toggle_refs_notify (object, FALSE);
3008 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3015 * @object: (type GObject.Object): a #GObject
3017 * Decreases the reference count of @object. When its reference count
3018 * drops to 0, the object is finalized (i.e. its memory is freed).
3021 g_object_unref (gpointer _object)
3023 GObject *object = _object;
3026 g_return_if_fail (G_IS_OBJECT (object));
3027 g_return_if_fail (object->ref_count > 0);
3029 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3030 retry_atomic_decrement1:
3031 old_ref = g_atomic_int_get (&object->ref_count);
3034 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3035 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3037 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3038 goto retry_atomic_decrement1;
3040 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3042 /* if we went from 2->1 we need to notify toggle refs if any */
3043 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3044 toggle_refs_notify (object, TRUE);
3048 GSList **weak_locations;
3050 /* The only way that this object can live at this point is if
3051 * there are outstanding weak references already established
3052 * before we got here.
3054 * If there were not already weak references then no more can be
3055 * established at this time, because the other thread would have
3056 * to hold a strong ref in order to call
3057 * g_object_add_weak_pointer() and then we wouldn't be here.
3059 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3061 if (weak_locations != NULL)
3063 g_rw_lock_writer_lock (&weak_locations_lock);
3065 /* It is possible that one of the weak references beat us to
3066 * the lock. Make sure the refcount is still what we expected
3069 old_ref = g_atomic_int_get (&object->ref_count);
3072 g_rw_lock_writer_unlock (&weak_locations_lock);
3073 goto retry_atomic_decrement1;
3076 /* We got the lock first, so the object will definitely die
3077 * now. Clear out all the weak references.
3079 while (*weak_locations)
3081 GWeakRef *weak_ref_location = (*weak_locations)->data;
3083 weak_ref_location->priv.p = NULL;
3084 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3087 g_rw_lock_writer_unlock (&weak_locations_lock);
3090 /* we are about to remove the last reference */
3091 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3092 G_OBJECT_GET_CLASS (object)->dispose (object);
3093 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3095 /* may have been re-referenced meanwhile */
3096 retry_atomic_decrement2:
3097 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3100 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3101 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3103 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3104 goto retry_atomic_decrement2;
3106 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3108 /* if we went from 2->1 we need to notify toggle refs if any */
3109 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3110 toggle_refs_notify (object, TRUE);
3115 /* we are still in the process of taking away the last ref */
3116 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3117 g_signal_handlers_destroy (object);
3118 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3120 /* decrement the last reference */
3121 old_ref = g_atomic_int_add (&object->ref_count, -1);
3123 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3125 /* may have been re-referenced meanwhile */
3126 if (G_LIKELY (old_ref == 1))
3128 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3129 G_OBJECT_GET_CLASS (object)->finalize (object);
3131 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3133 #ifdef G_ENABLE_DEBUG
3136 /* catch objects not chaining finalize handlers */
3137 G_LOCK (debug_objects);
3138 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3139 G_UNLOCK (debug_objects);
3141 #endif /* G_ENABLE_DEBUG */
3142 g_type_free_instance ((GTypeInstance*) object);
3148 * g_clear_object: (skip)
3149 * @object_ptr: a pointer to a #GObject reference
3151 * Clears a reference to a #GObject.
3153 * @object_ptr must not be %NULL.
3155 * If the reference is %NULL then this function does nothing.
3156 * Otherwise, the reference count of the object is decreased and the
3157 * pointer is set to %NULL.
3159 * This function is threadsafe and modifies the pointer atomically,
3160 * using memory barriers where needed.
3162 * A macro is also included that allows this function to be used without
3167 #undef g_clear_object
3169 g_clear_object (volatile GObject **object_ptr)
3171 g_clear_pointer (object_ptr, g_object_unref);
3175 * g_object_get_qdata:
3176 * @object: The GObject to get a stored user data pointer from
3177 * @quark: A #GQuark, naming the user data pointer
3179 * This function gets back user data pointers stored via
3180 * g_object_set_qdata().
3182 * Returns: (transfer none): The user data pointer set, or %NULL
3185 g_object_get_qdata (GObject *object,
3188 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3190 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3194 * g_object_set_qdata: (skip)
3195 * @object: The GObject to set store a user data pointer
3196 * @quark: A #GQuark, naming the user data pointer
3197 * @data: An opaque user data pointer
3199 * This sets an opaque, named pointer on an object.
3200 * The name is specified through a #GQuark (retrived e.g. via
3201 * g_quark_from_static_string()), and the pointer
3202 * can be gotten back from the @object with g_object_get_qdata()
3203 * until the @object is finalized.
3204 * Setting a previously set user data pointer, overrides (frees)
3205 * the old pointer set, using #NULL as pointer essentially
3206 * removes the data stored.
3209 g_object_set_qdata (GObject *object,
3213 g_return_if_fail (G_IS_OBJECT (object));
3214 g_return_if_fail (quark > 0);
3216 g_datalist_id_set_data (&object->qdata, quark, data);
3220 * g_object_dup_qdata:
3221 * @object: the #GObject to store user data on
3222 * @quark: a #GQuark, naming the user data pointer
3223 * @dup_func: (allow-none): function to dup the value
3224 * @user_data: (allow-none): passed as user_data to @dup_func
3226 * This is a variant of g_object_get_qdata() which returns
3227 * a 'duplicate' of the value. @dup_func defines the
3228 * meaning of 'duplicate' in this context, it could e.g.
3229 * take a reference on a ref-counted object.
3231 * If the @quark is not set on the object then @dup_func
3232 * will be called with a %NULL argument.
3234 * Note that @dup_func is called while user data of @object
3237 * This function can be useful to avoid races when multiple
3238 * threads are using object data on the same key on the same
3241 * Returns: the result of calling @dup_func on the value
3242 * associated with @quark on @object, or %NULL if not set.
3243 * If @dup_func is %NULL, the value is returned
3249 g_object_dup_qdata (GObject *object,
3251 GDuplicateFunc dup_func,
3254 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3255 g_return_val_if_fail (quark > 0, NULL);
3257 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3261 * g_object_replace_qdata:
3262 * @object: the #GObject to store user data on
3263 * @quark: a #GQuark, naming the user data pointer
3264 * @oldval: (allow-none): the old value to compare against
3265 * @newval: (allow-none): the new value
3266 * @destroy: (allow-none): a destroy notify for the new value
3267 * @old_destroy: (allow-none): destroy notify for the existing value
3269 * Compares the user data for the key @quark on @object with
3270 * @oldval, and if they are the same, replaces @oldval with
3273 * This is like a typical atomic compare-and-exchange
3274 * operation, for user data on an object.
3276 * If the previous value was replaced then ownership of the
3277 * old value (@oldval) is passed to the caller, including
3278 * the registered destroy notify for it (passed out in @old_destroy).
3279 * Its up to the caller to free this as he wishes, which may
3280 * or may not include using @old_destroy as sometimes replacement
3281 * should not destroy the object in the normal way.
3283 * Return: %TRUE if the existing value for @quark was replaced
3284 * by @newval, %FALSE otherwise.
3289 g_object_replace_qdata (GObject *object,
3293 GDestroyNotify destroy,
3294 GDestroyNotify *old_destroy)
3296 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3297 g_return_val_if_fail (quark > 0, FALSE);
3299 return g_datalist_id_replace_data (&object->qdata, quark,
3300 oldval, newval, destroy,
3305 * g_object_set_qdata_full: (skip)
3306 * @object: The GObject to set store a user data pointer
3307 * @quark: A #GQuark, naming the user data pointer
3308 * @data: An opaque user data pointer
3309 * @destroy: Function to invoke with @data as argument, when @data
3312 * This function works like g_object_set_qdata(), but in addition,
3313 * a void (*destroy) (gpointer) function may be specified which is
3314 * called with @data as argument when the @object is finalized, or
3315 * the data is being overwritten by a call to g_object_set_qdata()
3316 * with the same @quark.
3319 g_object_set_qdata_full (GObject *object,
3322 GDestroyNotify destroy)
3324 g_return_if_fail (G_IS_OBJECT (object));
3325 g_return_if_fail (quark > 0);
3327 g_datalist_id_set_data_full (&object->qdata, quark, data,
3328 data ? destroy : (GDestroyNotify) NULL);
3332 * g_object_steal_qdata:
3333 * @object: The GObject to get a stored user data pointer from
3334 * @quark: A #GQuark, naming the user data pointer
3336 * This function gets back user data pointers stored via
3337 * g_object_set_qdata() and removes the @data from object
3338 * without invoking its destroy() function (if any was
3340 * Usually, calling this function is only required to update
3341 * user data pointers with a destroy notifier, for example:
3342 * |[<!-- language="C" -->
3344 * object_add_to_user_list (GObject *object,
3345 * const gchar *new_string)
3347 * // the quark, naming the object data
3348 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3349 * // retrive the old string list
3350 * GList *list = g_object_steal_qdata (object, quark_string_list);
3352 * // prepend new string
3353 * list = g_list_prepend (list, g_strdup (new_string));
3354 * // this changed 'list', so we need to set it again
3355 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3358 * free_string_list (gpointer data)
3360 * GList *node, *list = data;
3362 * for (node = list; node; node = node->next)
3363 * g_free (node->data);
3364 * g_list_free (list);
3367 * Using g_object_get_qdata() in the above example, instead of
3368 * g_object_steal_qdata() would have left the destroy function set,
3369 * and thus the partial string list would have been freed upon
3370 * g_object_set_qdata_full().
3372 * Returns: (transfer full): The user data pointer set, or %NULL
3375 g_object_steal_qdata (GObject *object,
3378 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3379 g_return_val_if_fail (quark > 0, NULL);
3381 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3385 * g_object_get_data:
3386 * @object: #GObject containing the associations
3387 * @key: name of the key for that association
3389 * Gets a named field from the objects table of associations (see g_object_set_data()).
3391 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3394 g_object_get_data (GObject *object,
3397 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3398 g_return_val_if_fail (key != NULL, NULL);
3400 return g_datalist_get_data (&object->qdata, key);
3404 * g_object_set_data:
3405 * @object: #GObject containing the associations.
3406 * @key: name of the key
3407 * @data: data to associate with that key
3409 * Each object carries around a table of associations from
3410 * strings to pointers. This function lets you set an association.
3412 * If the object already had an association with that name,
3413 * the old association will be destroyed.
3416 g_object_set_data (GObject *object,
3420 g_return_if_fail (G_IS_OBJECT (object));
3421 g_return_if_fail (key != NULL);
3423 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3427 * g_object_dup_data:
3428 * @object: the #GObject to store user data on
3429 * @key: a string, naming the user data pointer
3430 * @dup_func: (allow-none): function to dup the value
3431 * @user_data: (allow-none): passed as user_data to @dup_func
3433 * This is a variant of g_object_get_data() which returns
3434 * a 'duplicate' of the value. @dup_func defines the
3435 * meaning of 'duplicate' in this context, it could e.g.
3436 * take a reference on a ref-counted object.
3438 * If the @key is not set on the object then @dup_func
3439 * will be called with a %NULL argument.
3441 * Note that @dup_func is called while user data of @object
3444 * This function can be useful to avoid races when multiple
3445 * threads are using object data on the same key on the same
3448 * Returns: the result of calling @dup_func on the value
3449 * associated with @key on @object, or %NULL if not set.
3450 * If @dup_func is %NULL, the value is returned
3456 g_object_dup_data (GObject *object,
3458 GDuplicateFunc dup_func,
3461 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3462 g_return_val_if_fail (key != NULL, NULL);
3464 return g_datalist_id_dup_data (&object->qdata,
3465 g_quark_from_string (key),
3466 dup_func, user_data);
3470 * g_object_replace_data:
3471 * @object: the #GObject to store user data on
3472 * @key: a string, naming the user data pointer
3473 * @oldval: (allow-none): the old value to compare against
3474 * @newval: (allow-none): the new value
3475 * @destroy: (allow-none): a destroy notify for the new value
3476 * @old_destroy: (allow-none): destroy notify for the existing value
3478 * Compares the user data for the key @key on @object with
3479 * @oldval, and if they are the same, replaces @oldval with
3482 * This is like a typical atomic compare-and-exchange
3483 * operation, for user data on an object.
3485 * If the previous value was replaced then ownership of the
3486 * old value (@oldval) is passed to the caller, including
3487 * the registered destroy notify for it (passed out in @old_destroy).
3488 * Its up to the caller to free this as he wishes, which may
3489 * or may not include using @old_destroy as sometimes replacement
3490 * should not destroy the object in the normal way.
3492 * Return: %TRUE if the existing value for @key was replaced
3493 * by @newval, %FALSE otherwise.
3498 g_object_replace_data (GObject *object,
3502 GDestroyNotify destroy,
3503 GDestroyNotify *old_destroy)
3505 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3506 g_return_val_if_fail (key != NULL, FALSE);
3508 return g_datalist_id_replace_data (&object->qdata,
3509 g_quark_from_string (key),
3510 oldval, newval, destroy,
3515 * g_object_set_data_full: (skip)
3516 * @object: #GObject containing the associations
3517 * @key: name of the key
3518 * @data: data to associate with that key
3519 * @destroy: function to call when the association is destroyed
3521 * Like g_object_set_data() except it adds notification
3522 * for when the association is destroyed, either by setting it
3523 * to a different value or when the object is destroyed.
3525 * Note that the @destroy callback is not called if @data is %NULL.
3528 g_object_set_data_full (GObject *object,
3531 GDestroyNotify destroy)
3533 g_return_if_fail (G_IS_OBJECT (object));
3534 g_return_if_fail (key != NULL);
3536 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3537 data ? destroy : (GDestroyNotify) NULL);
3541 * g_object_steal_data:
3542 * @object: #GObject containing the associations
3543 * @key: name of the key
3545 * Remove a specified datum from the object's data associations,
3546 * without invoking the association's destroy handler.
3548 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3551 g_object_steal_data (GObject *object,
3556 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3557 g_return_val_if_fail (key != NULL, NULL);
3559 quark = g_quark_try_string (key);
3561 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3565 g_value_object_init (GValue *value)
3567 value->data[0].v_pointer = NULL;
3571 g_value_object_free_value (GValue *value)
3573 if (value->data[0].v_pointer)
3574 g_object_unref (value->data[0].v_pointer);
3578 g_value_object_copy_value (const GValue *src_value,
3581 if (src_value->data[0].v_pointer)
3582 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3584 dest_value->data[0].v_pointer = NULL;
3588 g_value_object_transform_value (const GValue *src_value,
3591 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)))
3592 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3594 dest_value->data[0].v_pointer = NULL;
3598 g_value_object_peek_pointer (const GValue *value)
3600 return value->data[0].v_pointer;
3604 g_value_object_collect_value (GValue *value,
3605 guint n_collect_values,
3606 GTypeCValue *collect_values,
3607 guint collect_flags)
3609 if (collect_values[0].v_pointer)
3611 GObject *object = collect_values[0].v_pointer;
3613 if (object->g_type_instance.g_class == NULL)
3614 return g_strconcat ("invalid unclassed object pointer for value type '",
3615 G_VALUE_TYPE_NAME (value),
3618 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3619 return g_strconcat ("invalid object type '",
3620 G_OBJECT_TYPE_NAME (object),
3621 "' for value type '",
3622 G_VALUE_TYPE_NAME (value),
3625 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3626 value->data[0].v_pointer = g_object_ref (object);
3629 value->data[0].v_pointer = NULL;
3635 g_value_object_lcopy_value (const GValue *value,
3636 guint n_collect_values,
3637 GTypeCValue *collect_values,
3638 guint collect_flags)
3640 GObject **object_p = collect_values[0].v_pointer;
3643 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3645 if (!value->data[0].v_pointer)
3647 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3648 *object_p = value->data[0].v_pointer;
3650 *object_p = g_object_ref (value->data[0].v_pointer);
3656 * g_value_set_object:
3657 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3658 * @v_object: (type GObject.Object) (allow-none): object value to be set
3660 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3662 * g_value_set_object() increases the reference count of @v_object
3663 * (the #GValue holds a reference to @v_object). If you do not wish
3664 * to increase the reference count of the object (i.e. you wish to
3665 * pass your current reference to the #GValue because you no longer
3666 * need it), use g_value_take_object() instead.
3668 * It is important that your #GValue holds a reference to @v_object (either its
3669 * own, or one it has taken) to ensure that the object won't be destroyed while
3670 * the #GValue still exists).
3673 g_value_set_object (GValue *value,
3678 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3680 old = value->data[0].v_pointer;
3684 g_return_if_fail (G_IS_OBJECT (v_object));
3685 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3687 value->data[0].v_pointer = v_object;
3688 g_object_ref (value->data[0].v_pointer);
3691 value->data[0].v_pointer = NULL;
3694 g_object_unref (old);
3698 * g_value_set_object_take_ownership: (skip)
3699 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3700 * @v_object: (allow-none): object value to be set
3702 * This is an internal function introduced mainly for C marshallers.
3704 * Deprecated: 2.4: Use g_value_take_object() instead.
3707 g_value_set_object_take_ownership (GValue *value,
3710 g_value_take_object (value, v_object);
3714 * g_value_take_object: (skip)
3715 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3716 * @v_object: (allow-none): object value to be set
3718 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3719 * and takes over the ownership of the callers reference to @v_object;
3720 * the caller doesn't have to unref it any more (i.e. the reference
3721 * count of the object is not increased).
3723 * If you want the #GValue to hold its own reference to @v_object, use
3724 * g_value_set_object() instead.
3729 g_value_take_object (GValue *value,
3732 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3734 if (value->data[0].v_pointer)
3736 g_object_unref (value->data[0].v_pointer);
3737 value->data[0].v_pointer = NULL;
3742 g_return_if_fail (G_IS_OBJECT (v_object));
3743 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3745 value->data[0].v_pointer = v_object; /* we take over the reference count */
3750 * g_value_get_object:
3751 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3753 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3755 * Returns: (type GObject.Object) (transfer none): object contents of @value
3758 g_value_get_object (const GValue *value)
3760 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3762 return value->data[0].v_pointer;
3766 * g_value_dup_object:
3767 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3769 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3770 * its reference count. If the contents of the #GValue are %NULL, then
3771 * %NULL will be returned.
3773 * Returns: (type GObject.Object) (transfer full): object content of @value,
3774 * should be unreferenced when no longer needed.
3777 g_value_dup_object (const GValue *value)
3779 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3781 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3785 * g_signal_connect_object: (skip)
3786 * @instance: the instance to connect to.
3787 * @detailed_signal: a string of the form "signal-name::detail".
3788 * @c_handler: the #GCallback to connect.
3789 * @gobject: the object to pass as data to @c_handler.
3790 * @connect_flags: a combination of #GConnectFlags.
3792 * This is similar to g_signal_connect_data(), but uses a closure which
3793 * ensures that the @gobject stays alive during the call to @c_handler
3794 * by temporarily adding a reference count to @gobject.
3796 * When the @gobject is destroyed the signal handler will be automatically
3797 * disconnected. Note that this is not currently threadsafe (ie:
3798 * emitting a signal while @gobject is being destroyed in another thread
3801 * Returns: the handler id.
3804 g_signal_connect_object (gpointer instance,
3805 const gchar *detailed_signal,
3806 GCallback c_handler,
3808 GConnectFlags connect_flags)
3810 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3811 g_return_val_if_fail (detailed_signal != NULL, 0);
3812 g_return_val_if_fail (c_handler != NULL, 0);
3818 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3820 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3822 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3825 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3831 GClosure *closures[1]; /* flexible array */
3833 /* don't change this structure without supplying an accessor for
3834 * watched closures, e.g.:
3835 * GSList* g_object_list_watched_closures (GObject *object)
3838 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3839 * carray = g_object_get_data (object, "GObject-closure-array");
3842 * GSList *slist = NULL;
3844 * for (i = 0; i < carray->n_closures; i++)
3845 * slist = g_slist_prepend (slist, carray->closures[i]);
3853 object_remove_closure (gpointer data,
3856 GObject *object = data;
3860 G_LOCK (closure_array_mutex);
3861 carray = g_object_get_qdata (object, quark_closure_array);
3862 for (i = 0; i < carray->n_closures; i++)
3863 if (carray->closures[i] == closure)
3865 carray->n_closures--;
3866 if (i < carray->n_closures)
3867 carray->closures[i] = carray->closures[carray->n_closures];
3868 G_UNLOCK (closure_array_mutex);
3871 G_UNLOCK (closure_array_mutex);
3872 g_assert_not_reached ();
3876 destroy_closure_array (gpointer data)
3878 CArray *carray = data;
3879 GObject *object = carray->object;
3880 guint i, n = carray->n_closures;
3882 for (i = 0; i < n; i++)
3884 GClosure *closure = carray->closures[i];
3886 /* removing object_remove_closure() upfront is probably faster than
3887 * letting it fiddle with quark_closure_array which is empty anyways
3889 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3890 g_closure_invalidate (closure);
3896 * g_object_watch_closure:
3897 * @object: GObject restricting lifetime of @closure
3898 * @closure: GClosure to watch
3900 * This function essentially limits the life time of the @closure to
3901 * the life time of the object. That is, when the object is finalized,
3902 * the @closure is invalidated by calling g_closure_invalidate() on
3903 * it, in order to prevent invocations of the closure with a finalized
3904 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3905 * added as marshal guards to the @closure, to ensure that an extra
3906 * reference count is held on @object during invocation of the
3907 * @closure. Usually, this function will be called on closures that
3908 * use this @object as closure data.
3911 g_object_watch_closure (GObject *object,
3917 g_return_if_fail (G_IS_OBJECT (object));
3918 g_return_if_fail (closure != NULL);
3919 g_return_if_fail (closure->is_invalid == FALSE);
3920 g_return_if_fail (closure->in_marshal == FALSE);
3921 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3923 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3924 g_closure_add_marshal_guards (closure,
3925 object, (GClosureNotify) g_object_ref,
3926 object, (GClosureNotify) g_object_unref);
3927 G_LOCK (closure_array_mutex);
3928 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3931 carray = g_renew (CArray, NULL, 1);
3932 carray->object = object;
3933 carray->n_closures = 1;
3938 i = carray->n_closures++;
3939 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3941 carray->closures[i] = closure;
3942 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3943 G_UNLOCK (closure_array_mutex);
3947 * g_closure_new_object:
3948 * @sizeof_closure: the size of the structure to allocate, must be at least
3949 * `sizeof (GClosure)`
3950 * @object: a #GObject pointer to store in the @data field of the newly
3951 * allocated #GClosure
3953 * A variant of g_closure_new_simple() which stores @object in the
3954 * @data field of the closure and calls g_object_watch_closure() on
3955 * @object and the created closure. This function is mainly useful
3956 * when implementing new types of closures.
3958 * Returns: (transfer full): a newly allocated #GClosure
3961 g_closure_new_object (guint sizeof_closure,
3966 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3967 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3969 closure = g_closure_new_simple (sizeof_closure, object);
3970 g_object_watch_closure (object, closure);
3976 * g_cclosure_new_object: (skip)
3977 * @callback_func: the function to invoke
3978 * @object: a #GObject pointer to pass to @callback_func
3980 * A variant of g_cclosure_new() which uses @object as @user_data and
3981 * calls g_object_watch_closure() on @object and the created
3982 * closure. This function is useful when you have a callback closely
3983 * associated with a #GObject, and want the callback to no longer run
3984 * after the object is is freed.
3986 * Returns: a new #GCClosure
3989 g_cclosure_new_object (GCallback callback_func,
3994 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3995 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3996 g_return_val_if_fail (callback_func != NULL, NULL);
3998 closure = g_cclosure_new (callback_func, object, NULL);
3999 g_object_watch_closure (object, closure);
4005 * g_cclosure_new_object_swap: (skip)
4006 * @callback_func: the function to invoke
4007 * @object: a #GObject pointer to pass to @callback_func
4009 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4010 * and calls g_object_watch_closure() on @object and the created
4011 * closure. This function is useful when you have a callback closely
4012 * associated with a #GObject, and want the callback to no longer run
4013 * after the object is is freed.
4015 * Returns: a new #GCClosure
4018 g_cclosure_new_object_swap (GCallback callback_func,
4023 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4024 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4025 g_return_val_if_fail (callback_func != NULL, NULL);
4027 closure = g_cclosure_new_swap (callback_func, object, NULL);
4028 g_object_watch_closure (object, closure);
4034 g_object_compat_control (gsize what,
4040 case 1: /* floating base type */
4041 return G_TYPE_INITIALLY_UNOWNED;
4042 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4043 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4045 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4047 *pp = floating_flag_handler;
4054 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
4057 g_initially_unowned_init (GInitiallyUnowned *object)
4059 g_object_force_floating (object);
4063 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4070 * A structure containing a weak reference to a #GObject. It can either
4071 * be empty (i.e. point to %NULL), or point to an object for as long as
4072 * at least one "strong" reference to that object exists. Before the
4073 * object's #GObjectClass.dispose method is called, every #GWeakRef
4074 * associated with becomes empty (i.e. points to %NULL).
4076 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4077 * heap-allocated, or embedded in larger structures.
4079 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4080 * reference is thread-safe: converting a weak pointer to a reference is
4081 * atomic with respect to invalidation of weak pointers to destroyed
4084 * If the object's #GObjectClass.dispose method results in additional
4085 * references to the object being held, any #GWeakRefs taken
4086 * before it was disposed will continue to point to %NULL. If
4087 * #GWeakRefs are taken after the object is disposed and
4088 * re-referenced, they will continue to point to it until its refcount
4089 * goes back to zero, at which point they too will be invalidated.
4093 * g_weak_ref_init: (skip)
4094 * @weak_ref: (inout): uninitialized or empty location for a weak
4096 * @object: (allow-none): a #GObject or %NULL
4098 * Initialise a non-statically-allocated #GWeakRef.
4100 * This function also calls g_weak_ref_set() with @object on the
4101 * freshly-initialised weak reference.
4103 * This function should always be matched with a call to
4104 * g_weak_ref_clear(). It is not necessary to use this function for a
4105 * #GWeakRef in static storage because it will already be
4106 * properly initialised. Just use g_weak_ref_set() directly.
4111 g_weak_ref_init (GWeakRef *weak_ref,
4114 weak_ref->priv.p = NULL;
4116 g_weak_ref_set (weak_ref, object);
4120 * g_weak_ref_clear: (skip)
4121 * @weak_ref: (inout): location of a weak reference, which
4124 * Frees resources associated with a non-statically-allocated #GWeakRef.
4125 * After this call, the #GWeakRef is left in an undefined state.
4127 * You should only call this on a #GWeakRef that previously had
4128 * g_weak_ref_init() called on it.
4133 g_weak_ref_clear (GWeakRef *weak_ref)
4135 g_weak_ref_set (weak_ref, NULL);
4138 weak_ref->priv.p = (void *) 0xccccccccu;
4142 * g_weak_ref_get: (skip)
4143 * @weak_ref: (inout): location of a weak reference to a #GObject
4145 * If @weak_ref is not empty, atomically acquire a strong
4146 * reference to the object it points to, and return that reference.
4148 * This function is needed because of the potential race between taking
4149 * the pointer value and g_object_ref() on it, if the object was losing
4150 * its last reference at the same time in a different thread.
4152 * The caller should release the resulting reference in the usual way,
4153 * by using g_object_unref().
4155 * Returns: (transfer full) (type GObject.Object): the object pointed to
4156 * by @weak_ref, or %NULL if it was empty
4161 g_weak_ref_get (GWeakRef *weak_ref)
4163 gpointer object_or_null;
4165 g_return_val_if_fail (weak_ref!= NULL, NULL);
4167 g_rw_lock_reader_lock (&weak_locations_lock);
4169 object_or_null = weak_ref->priv.p;
4171 if (object_or_null != NULL)
4172 g_object_ref (object_or_null);
4174 g_rw_lock_reader_unlock (&weak_locations_lock);
4176 return object_or_null;
4180 * g_weak_ref_set: (skip)
4181 * @weak_ref: location for a weak reference
4182 * @object: (allow-none): a #GObject or %NULL
4184 * Change the object to which @weak_ref points, or set it to
4187 * You must own a strong reference on @object while calling this
4193 g_weak_ref_set (GWeakRef *weak_ref,
4196 GSList **weak_locations;
4197 GObject *new_object;
4198 GObject *old_object;
4200 g_return_if_fail (weak_ref != NULL);
4201 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4203 new_object = object;
4205 g_rw_lock_writer_lock (&weak_locations_lock);
4207 /* We use the extra level of indirection here so that if we have ever
4208 * had a weak pointer installed at any point in time on this object,
4209 * we can see that there is a non-NULL value associated with the
4210 * weak-pointer quark and know that this value will not change at any
4211 * point in the object's lifetime.
4213 * Both properties are important for reducing the amount of times we
4214 * need to acquire locks and for decreasing the duration of time the
4215 * lock is held while avoiding some rather tricky races.
4217 * Specifically: we can avoid having to do an extra unconditional lock
4218 * in g_object_unref() without worrying about some extremely tricky
4222 old_object = weak_ref->priv.p;
4223 if (new_object != old_object)
4225 weak_ref->priv.p = new_object;
4227 /* Remove the weak ref from the old object */
4228 if (old_object != NULL)
4230 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4231 /* for it to point to an object, the object must have had it added once */
4232 g_assert (weak_locations != NULL);
4234 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4237 /* Add the weak ref to the new object */
4238 if (new_object != NULL)
4240 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4242 if (weak_locations == NULL)
4244 weak_locations = g_new0 (GSList *, 1);
4245 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4248 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4252 g_rw_lock_writer_unlock (&weak_locations_lock);