1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * MT safe with regards to reference counting.
30 #include "gtype-private.h"
31 #include "gvaluecollector.h"
33 #include "gparamspecs.h"
34 #include "gvaluetypes.h"
35 #include "gobject_trace.h"
36 #include "gconstructor.h"
41 * @short_description: The base object type
42 * @see_also: #GParamSpecObject, g_param_spec_object()
44 * GObject is the fundamental type providing the common attributes and
45 * methods for all object types in GTK+, Pango and other libraries
46 * based on GObject. The GObject class provides methods for object
47 * construction and destruction, property access methods, and signal
48 * support. Signals are described in detail in <xref
49 * linkend="gobject-Signals"/>.
51 * <para id="floating-ref">
52 * GInitiallyUnowned is derived from GObject. The only difference between
53 * the two is that the initial reference of a GInitiallyUnowned is flagged
54 * as a <firstterm>floating</firstterm> reference.
55 * This means that it is not specifically claimed to be "owned" by
56 * any code portion. The main motivation for providing floating references is
57 * C convenience. In particular, it allows code to be written as:
59 * container = create_container ();
60 * container_add_child (container, create_child());
62 * If <function>container_add_child()</function> will g_object_ref_sink() the
63 * passed in child, no reference of the newly created child is leaked.
64 * Without floating references, <function>container_add_child()</function>
65 * can only g_object_ref() the new child, so to implement this code without
66 * reference leaks, it would have to be written as:
69 * container = create_container ();
70 * child = create_child ();
71 * container_add_child (container, child);
72 * g_object_unref (child);
74 * The floating reference can be converted into
75 * an ordinary reference by calling g_object_ref_sink().
76 * For already sunken objects (objects that don't have a floating reference
77 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
79 * Since floating references are useful almost exclusively for C convenience,
80 * language bindings that provide automated reference and memory ownership
81 * maintenance (such as smart pointers or garbage collection) should not
82 * expose floating references in their API.
85 * Some object implementations may need to save an objects floating state
86 * across certain code portions (an example is #GtkMenu), to achieve this,
87 * the following sequence can be used:
90 * /* save floating state */
91 * gboolean was_floating = g_object_is_floating (object);
92 * g_object_ref_sink (object);
93 * /* protected code portion */
95 * /* restore floating state */
97 * g_object_force_floating (object);
99 * g_object_unref (object); /* release previously acquired reference */
105 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
106 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
108 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
109 #define OBJECT_HAS_TOGGLE_REF(object) \
110 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
111 #define OBJECT_FLOATING_FLAG 0x2
113 #define CLASS_HAS_PROPS_FLAG 0x1
114 #define CLASS_HAS_PROPS(class) \
115 ((class)->flags & CLASS_HAS_PROPS_FLAG)
116 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
117 ((class)->constructor != g_object_constructor)
118 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
119 ((class)->constructed != g_object_constructed)
121 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
122 #define CLASS_HAS_DERIVED_CLASS(class) \
123 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
125 /* --- signals --- */
132 /* --- properties --- */
138 /* --- prototypes --- */
139 static void g_object_base_class_init (GObjectClass *class);
140 static void g_object_base_class_finalize (GObjectClass *class);
141 static void g_object_do_class_init (GObjectClass *class);
142 static void g_object_init (GObject *object,
143 GObjectClass *class);
144 static GObject* g_object_constructor (GType type,
145 guint n_construct_properties,
146 GObjectConstructParam *construct_params);
147 static void g_object_constructed (GObject *object);
148 static void g_object_real_dispose (GObject *object);
149 static void g_object_finalize (GObject *object);
150 static void g_object_do_set_property (GObject *object,
154 static void g_object_do_get_property (GObject *object,
158 static void g_value_object_init (GValue *value);
159 static void g_value_object_free_value (GValue *value);
160 static void g_value_object_copy_value (const GValue *src_value,
162 static void g_value_object_transform_value (const GValue *src_value,
164 static gpointer g_value_object_peek_pointer (const GValue *value);
165 static gchar* g_value_object_collect_value (GValue *value,
166 guint n_collect_values,
167 GTypeCValue *collect_values,
168 guint collect_flags);
169 static gchar* g_value_object_lcopy_value (const GValue *value,
170 guint n_collect_values,
171 GTypeCValue *collect_values,
172 guint collect_flags);
173 static void g_object_dispatch_properties_changed (GObject *object,
175 GParamSpec **pspecs);
176 static guint object_floating_flag_handler (GObject *object,
179 static void object_interface_check_properties (gpointer check_data,
182 /* --- typedefs --- */
183 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
185 struct _GObjectNotifyQueue
189 guint16 freeze_count;
192 /* --- variables --- */
193 G_LOCK_DEFINE_STATIC (closure_array_mutex);
194 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
195 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
196 static GQuark quark_closure_array = 0;
197 static GQuark quark_weak_refs = 0;
198 static GQuark quark_toggle_refs = 0;
199 static GQuark quark_notify_queue;
200 static GParamSpecPool *pspec_pool = NULL;
201 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
202 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
203 G_LOCK_DEFINE_STATIC (construction_mutex);
204 static GSList *construction_objects = NULL;
205 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
206 static GQuark quark_weak_locations = 0;
207 static GRWLock weak_locations_lock;
209 G_LOCK_DEFINE_STATIC(notify_lock);
211 /* --- functions --- */
213 g_object_notify_queue_free (gpointer data)
215 GObjectNotifyQueue *nqueue = data;
217 g_slist_free (nqueue->pspecs);
218 g_slice_free (GObjectNotifyQueue, nqueue);
221 static GObjectNotifyQueue*
222 g_object_notify_queue_freeze (GObject *object,
223 gboolean conditional)
225 GObjectNotifyQueue *nqueue;
228 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
233 G_UNLOCK(notify_lock);
237 nqueue = g_slice_new0 (GObjectNotifyQueue);
238 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
239 nqueue, g_object_notify_queue_free);
242 if (nqueue->freeze_count >= 65535)
243 g_critical("Free queue for %s (%p) is larger than 65535,"
244 " called g_object_freeze_notify() too often."
245 " Forgot to call g_object_thaw_notify() or infinite loop",
246 G_OBJECT_TYPE_NAME (object), object);
248 nqueue->freeze_count++;
249 G_UNLOCK(notify_lock);
255 g_object_notify_queue_thaw (GObject *object,
256 GObjectNotifyQueue *nqueue)
258 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
262 g_return_if_fail (nqueue->freeze_count > 0);
263 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
267 /* Just make sure we never get into some nasty race condition */
268 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
269 G_UNLOCK(notify_lock);
270 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
271 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
275 nqueue->freeze_count--;
276 if (nqueue->freeze_count) {
277 G_UNLOCK(notify_lock);
281 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
283 for (slist = nqueue->pspecs; slist; slist = slist->next)
285 pspecs[n_pspecs++] = slist->data;
287 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
289 G_UNLOCK(notify_lock);
292 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
297 g_object_notify_queue_add (GObject *object,
298 GObjectNotifyQueue *nqueue,
303 g_return_if_fail (nqueue->n_pspecs < 65535);
305 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
307 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
311 G_UNLOCK(notify_lock);
314 #ifdef G_ENABLE_DEBUG
315 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
316 G_LOCK_DEFINE_STATIC (debug_objects);
317 static volatile GObject *g_trap_object_ref = NULL;
318 static guint debug_objects_count = 0;
319 static GHashTable *debug_objects_ht = NULL;
322 debug_objects_foreach (gpointer key,
326 GObject *object = value;
328 g_message ("[%p] stale %s\tref_count=%u",
330 G_OBJECT_TYPE_NAME (object),
334 #ifdef G_HAS_CONSTRUCTORS
335 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
336 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
338 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
339 #endif /* G_HAS_CONSTRUCTORS */
342 debug_objects_atexit (void)
346 G_LOCK (debug_objects);
347 g_message ("stale GObjects: %u", debug_objects_count);
348 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
349 G_UNLOCK (debug_objects);
352 #endif /* G_ENABLE_DEBUG */
355 _g_object_type_init (void)
357 static gboolean initialized = FALSE;
358 static const GTypeFundamentalInfo finfo = {
359 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
362 sizeof (GObjectClass),
363 (GBaseInitFunc) g_object_base_class_init,
364 (GBaseFinalizeFunc) g_object_base_class_finalize,
365 (GClassInitFunc) g_object_do_class_init,
366 NULL /* class_destroy */,
367 NULL /* class_data */,
370 (GInstanceInitFunc) g_object_init,
371 NULL, /* value_table */
373 static const GTypeValueTable value_table = {
374 g_value_object_init, /* value_init */
375 g_value_object_free_value, /* value_free */
376 g_value_object_copy_value, /* value_copy */
377 g_value_object_peek_pointer, /* value_peek_pointer */
378 "p", /* collect_format */
379 g_value_object_collect_value, /* collect_value */
380 "p", /* lcopy_format */
381 g_value_object_lcopy_value, /* lcopy_value */
385 g_return_if_fail (initialized == FALSE);
390 info.value_table = &value_table;
391 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
392 g_assert (type == G_TYPE_OBJECT);
393 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
395 #ifdef G_ENABLE_DEBUG
398 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
399 #ifndef G_HAS_CONSTRUCTORS
400 g_atexit (debug_objects_atexit);
401 #endif /* G_HAS_CONSTRUCTORS */
403 #endif /* G_ENABLE_DEBUG */
407 g_object_base_class_init (GObjectClass *class)
409 GObjectClass *pclass = g_type_class_peek_parent (class);
411 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
412 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
415 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
417 /* reset instance specific fields and methods that don't get inherited */
418 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
419 class->get_property = NULL;
420 class->set_property = NULL;
424 g_object_base_class_finalize (GObjectClass *class)
428 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
430 g_slist_free (class->construct_properties);
431 class->construct_properties = NULL;
432 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
433 for (node = list; node; node = node->next)
435 GParamSpec *pspec = node->data;
437 g_param_spec_pool_remove (pspec_pool, pspec);
438 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
439 g_param_spec_unref (pspec);
445 g_object_do_class_init (GObjectClass *class)
447 /* read the comment about typedef struct CArray; on why not to change this quark */
448 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
450 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
451 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
452 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
453 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
454 pspec_pool = g_param_spec_pool_new (TRUE);
456 class->constructor = g_object_constructor;
457 class->constructed = g_object_constructed;
458 class->set_property = g_object_do_set_property;
459 class->get_property = g_object_do_get_property;
460 class->dispose = g_object_real_dispose;
461 class->finalize = g_object_finalize;
462 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
463 class->notify = NULL;
467 * @gobject: the object which received the signal.
468 * @pspec: the #GParamSpec of the property which changed.
470 * The notify signal is emitted on an object when one of its
471 * properties has been changed. Note that getting this signal
472 * doesn't guarantee that the value of the property has actually
473 * changed, it may also be emitted when the setter for the property
474 * is called to reinstate the previous value.
476 * This signal is typically used to obtain change notification for a
477 * single property, by specifying the property name as a detail in the
478 * g_signal_connect() call, like this:
480 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
481 * G_CALLBACK (gtk_text_view_target_list_notify),
484 * It is important to note that you must use
485 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
486 * detail strings for the notify signal.
488 gobject_signals[NOTIFY] =
489 g_signal_new (g_intern_static_string ("notify"),
490 G_TYPE_FROM_CLASS (class),
491 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
492 G_STRUCT_OFFSET (GObjectClass, notify),
494 g_cclosure_marshal_VOID__PARAM,
498 /* Install a check function that we'll use to verify that classes that
499 * implement an interface implement all properties for that interface
501 g_type_add_interface_check (NULL, object_interface_check_properties);
505 install_property_internal (GType g_type,
509 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
511 g_warning ("When installing property: type '%s' already has a property named '%s'",
512 g_type_name (g_type),
517 g_param_spec_ref_sink (pspec);
518 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
519 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
523 * g_object_class_install_property:
524 * @oclass: a #GObjectClass
525 * @property_id: the id for the new property
526 * @pspec: the #GParamSpec for the new property
528 * Installs a new property. This is usually done in the class initializer.
530 * Note that it is possible to redefine a property in a derived class,
531 * by installing a property with the same name. This can be useful at times,
532 * e.g. to change the range of allowed values or the default value.
535 g_object_class_install_property (GObjectClass *class,
539 g_return_if_fail (G_IS_OBJECT_CLASS (class));
540 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
542 if (CLASS_HAS_DERIVED_CLASS (class))
543 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
545 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (class)))
546 g_warning ("Attempt to add property %s::%s after class was initialised", G_OBJECT_CLASS_NAME (class), pspec->name);
548 class->flags |= CLASS_HAS_PROPS_FLAG;
550 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
551 if (pspec->flags & G_PARAM_WRITABLE)
552 g_return_if_fail (class->set_property != NULL);
553 if (pspec->flags & G_PARAM_READABLE)
554 g_return_if_fail (class->get_property != NULL);
555 g_return_if_fail (property_id > 0);
556 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
557 if (pspec->flags & G_PARAM_CONSTRUCT)
558 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
559 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
560 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
562 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
564 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
565 class->construct_properties = g_slist_append (class->construct_properties, pspec);
567 /* for property overrides of construct properties, we have to get rid
568 * of the overidden inherited construct property
570 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
571 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
576 * g_object_class_install_properties:
577 * @oclass: a #GObjectClass
578 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
579 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
580 * defining the new properties
582 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
583 * usually done in the class initializer.
585 * The property id of each property is the index of each #GParamSpec in
588 * The property id of 0 is treated specially by #GObject and it should not
589 * be used to store a #GParamSpec.
591 * This function should be used if you plan to use a static array of
592 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
593 * class initialization:
597 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
600 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
603 * my_object_class_init (MyObjectClass *klass)
605 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
607 * obj_properties[PROP_FOO] =
608 * g_param_spec_int ("foo", "Foo", "Foo",
611 * G_PARAM_READWRITE);
613 * obj_properties[PROP_BAR] =
614 * g_param_spec_string ("bar", "Bar", "Bar",
616 * G_PARAM_READWRITE);
618 * gobject_class->set_property = my_object_set_property;
619 * gobject_class->get_property = my_object_get_property;
620 * g_object_class_install_properties (gobject_class,
626 * allows calling g_object_notify_by_pspec() to notify of property changes:
630 * my_object_set_foo (MyObject *self, gint foo)
632 * if (self->foo != foo)
635 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
643 g_object_class_install_properties (GObjectClass *oclass,
647 GType oclass_type, parent_type;
650 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
651 g_return_if_fail (n_pspecs > 1);
652 g_return_if_fail (pspecs[0] == NULL);
654 if (CLASS_HAS_DERIVED_CLASS (oclass))
655 g_error ("Attempt to add properties to %s after it was derived",
656 G_OBJECT_CLASS_NAME (oclass));
658 if (!g_type_is_in_init (G_OBJECT_CLASS_TYPE (oclass)))
659 g_warning ("Attempt to add properties to %s after it was initialised", G_OBJECT_CLASS_NAME (oclass));
661 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
662 parent_type = g_type_parent (oclass_type);
664 /* we skip the first element of the array as it would have a 0 prop_id */
665 for (i = 1; i < n_pspecs; i++)
667 GParamSpec *pspec = pspecs[i];
669 g_return_if_fail (pspec != NULL);
671 if (pspec->flags & G_PARAM_WRITABLE)
672 g_return_if_fail (oclass->set_property != NULL);
673 if (pspec->flags & G_PARAM_READABLE)
674 g_return_if_fail (oclass->get_property != NULL);
675 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
676 if (pspec->flags & G_PARAM_CONSTRUCT)
677 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
678 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
679 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
681 oclass->flags |= CLASS_HAS_PROPS_FLAG;
682 install_property_internal (oclass_type, i, pspec);
684 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
685 oclass->construct_properties = g_slist_append (oclass->construct_properties, pspec);
687 /* for property overrides of construct properties, we have to get rid
688 * of the overidden inherited construct property
690 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
691 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
692 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
697 * g_object_interface_install_property:
698 * @g_iface: any interface vtable for the interface, or the default
699 * vtable for the interface.
700 * @pspec: the #GParamSpec for the new property
702 * Add a property to an interface; this is only useful for interfaces
703 * that are added to GObject-derived types. Adding a property to an
704 * interface forces all objects classes with that interface to have a
705 * compatible property. The compatible property could be a newly
706 * created #GParamSpec, but normally
707 * g_object_class_override_property() will be used so that the object
708 * class only needs to provide an implementation and inherits the
709 * property description, default value, bounds, and so forth from the
710 * interface property.
712 * This function is meant to be called from the interface's default
713 * vtable initialization function (the @class_init member of
714 * #GTypeInfo.) It must not be called after after @class_init has
715 * been called for any object types implementing this interface.
720 g_object_interface_install_property (gpointer g_iface,
723 GTypeInterface *iface_class = g_iface;
725 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
726 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
727 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
728 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
730 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
731 if (pspec->flags & G_PARAM_CONSTRUCT)
732 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
733 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
734 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
736 install_property_internal (iface_class->g_type, 0, pspec);
740 * g_object_class_find_property:
741 * @oclass: a #GObjectClass
742 * @property_name: the name of the property to look up
744 * Looks up the #GParamSpec for a property of a class.
746 * Returns: (transfer none): the #GParamSpec for the property, or
747 * %NULL if the class doesn't have a property of that name
750 g_object_class_find_property (GObjectClass *class,
751 const gchar *property_name)
754 GParamSpec *redirect;
756 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
757 g_return_val_if_fail (property_name != NULL, NULL);
759 pspec = g_param_spec_pool_lookup (pspec_pool,
761 G_OBJECT_CLASS_TYPE (class),
765 redirect = g_param_spec_get_redirect_target (pspec);
776 * g_object_interface_find_property:
777 * @g_iface: any interface vtable for the interface, or the default
778 * vtable for the interface
779 * @property_name: name of a property to lookup.
781 * Find the #GParamSpec with the given name for an
782 * interface. Generally, the interface vtable passed in as @g_iface
783 * will be the default vtable from g_type_default_interface_ref(), or,
784 * if you know the interface has already been loaded,
785 * g_type_default_interface_peek().
789 * Returns: (transfer none): the #GParamSpec for the property of the
790 * interface with the name @property_name, or %NULL if no
791 * such property exists.
794 g_object_interface_find_property (gpointer g_iface,
795 const gchar *property_name)
797 GTypeInterface *iface_class = g_iface;
799 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
800 g_return_val_if_fail (property_name != NULL, NULL);
802 return g_param_spec_pool_lookup (pspec_pool,
809 * g_object_class_override_property:
810 * @oclass: a #GObjectClass
811 * @property_id: the new property ID
812 * @name: the name of a property registered in a parent class or
813 * in an interface of this class.
815 * Registers @property_id as referring to a property with the
816 * name @name in a parent class or in an interface implemented
817 * by @oclass. This allows this class to <firstterm>override</firstterm>
818 * a property implementation in a parent class or to provide
819 * the implementation of a property from an interface.
822 * Internally, overriding is implemented by creating a property of type
823 * #GParamSpecOverride; generally operations that query the properties of
824 * the object class, such as g_object_class_find_property() or
825 * g_object_class_list_properties() will return the overridden
826 * property. However, in one case, the @construct_properties argument of
827 * the @constructor virtual function, the #GParamSpecOverride is passed
828 * instead, so that the @param_id field of the #GParamSpec will be
829 * correct. For virtually all uses, this makes no difference. If you
830 * need to get the overridden property, you can call
831 * g_param_spec_get_redirect_target().
837 g_object_class_override_property (GObjectClass *oclass,
841 GParamSpec *overridden = NULL;
845 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
846 g_return_if_fail (property_id > 0);
847 g_return_if_fail (name != NULL);
849 /* Find the overridden property; first check parent types
851 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
852 if (parent_type != G_TYPE_NONE)
853 overridden = g_param_spec_pool_lookup (pspec_pool,
862 /* Now check interfaces
864 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
865 while (n_ifaces-- && !overridden)
867 overridden = g_param_spec_pool_lookup (pspec_pool,
878 g_warning ("%s: Can't find property to override for '%s::%s'",
879 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
883 new = g_param_spec_override (name, overridden);
884 g_object_class_install_property (oclass, property_id, new);
888 * g_object_class_list_properties:
889 * @oclass: a #GObjectClass
890 * @n_properties: (out): return location for the length of the returned array
892 * Get an array of #GParamSpec* for all properties of a class.
894 * Returns: (array length=n_properties) (transfer container): an array of
895 * #GParamSpec* which should be freed after use
897 GParamSpec** /* free result */
898 g_object_class_list_properties (GObjectClass *class,
899 guint *n_properties_p)
904 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
906 pspecs = g_param_spec_pool_list (pspec_pool,
907 G_OBJECT_CLASS_TYPE (class),
916 * g_object_interface_list_properties:
917 * @g_iface: any interface vtable for the interface, or the default
918 * vtable for the interface
919 * @n_properties_p: (out): location to store number of properties returned.
921 * Lists the properties of an interface.Generally, the interface
922 * vtable passed in as @g_iface will be the default vtable from
923 * g_type_default_interface_ref(), or, if you know the interface has
924 * already been loaded, g_type_default_interface_peek().
928 * Returns: (array length=n_properties_p) (transfer container): a
929 * pointer to an array of pointers to #GParamSpec
930 * structures. The paramspecs are owned by GLib, but the
931 * array should be freed with g_free() when you are done with
935 g_object_interface_list_properties (gpointer g_iface,
936 guint *n_properties_p)
938 GTypeInterface *iface_class = g_iface;
942 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
944 pspecs = g_param_spec_pool_list (pspec_pool,
954 g_object_init (GObject *object,
957 object->ref_count = 1;
958 object->qdata = NULL;
960 if (CLASS_HAS_PROPS (class))
962 /* freeze object's notification queue, g_object_newv() preserves pairedness */
963 g_object_notify_queue_freeze (object, FALSE);
966 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
968 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
969 G_LOCK (construction_mutex);
970 construction_objects = g_slist_prepend (construction_objects, object);
971 G_UNLOCK (construction_mutex);
974 #ifdef G_ENABLE_DEBUG
977 G_LOCK (debug_objects);
978 debug_objects_count++;
979 g_hash_table_insert (debug_objects_ht, object, object);
980 G_UNLOCK (debug_objects);
982 #endif /* G_ENABLE_DEBUG */
986 g_object_do_set_property (GObject *object,
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1000 g_object_do_get_property (GObject *object,
1005 switch (property_id)
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1014 g_object_real_dispose (GObject *object)
1016 g_signal_handlers_destroy (object);
1017 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1018 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1022 g_object_finalize (GObject *object)
1024 g_datalist_clear (&object->qdata);
1026 #ifdef G_ENABLE_DEBUG
1029 G_LOCK (debug_objects);
1030 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1031 g_hash_table_remove (debug_objects_ht, object);
1032 debug_objects_count--;
1033 G_UNLOCK (debug_objects);
1035 #endif /* G_ENABLE_DEBUG */
1040 g_object_dispatch_properties_changed (GObject *object,
1042 GParamSpec **pspecs)
1046 for (i = 0; i < n_pspecs; i++)
1047 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1051 * g_object_run_dispose:
1052 * @object: a #GObject
1054 * Releases all references to other objects. This can be used to break
1057 * This functions should only be called from object system implementations.
1060 g_object_run_dispose (GObject *object)
1062 g_return_if_fail (G_IS_OBJECT (object));
1063 g_return_if_fail (object->ref_count > 0);
1065 g_object_ref (object);
1066 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1067 G_OBJECT_GET_CLASS (object)->dispose (object);
1068 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1069 g_object_unref (object);
1073 * g_object_freeze_notify:
1074 * @object: a #GObject
1076 * Increases the freeze count on @object. If the freeze count is
1077 * non-zero, the emission of "notify" signals on @object is
1078 * stopped. The signals are queued until the freeze count is decreased
1079 * to zero. Duplicate notifications are squashed so that at most one
1080 * #GObject::notify signal is emitted for each property modified while the
1083 * This is necessary for accessors that modify multiple properties to prevent
1084 * premature notification while the object is still being modified.
1087 g_object_freeze_notify (GObject *object)
1089 g_return_if_fail (G_IS_OBJECT (object));
1091 if (g_atomic_int_get (&object->ref_count) == 0)
1094 g_object_ref (object);
1095 g_object_notify_queue_freeze (object, FALSE);
1096 g_object_unref (object);
1100 get_notify_pspec (GParamSpec *pspec)
1102 GParamSpec *redirected;
1104 /* we don't notify on non-READABLE parameters */
1105 if (~pspec->flags & G_PARAM_READABLE)
1108 /* if the paramspec is redirected, notify on the target */
1109 redirected = g_param_spec_get_redirect_target (pspec);
1110 if (redirected != NULL)
1113 /* else, notify normally */
1118 g_object_notify_by_spec_internal (GObject *object,
1121 GParamSpec *notify_pspec;
1123 notify_pspec = get_notify_pspec (pspec);
1125 if (notify_pspec != NULL)
1127 GObjectNotifyQueue *nqueue;
1129 /* conditional freeze: only increase freeze count if already frozen */
1130 nqueue = g_object_notify_queue_freeze (object, TRUE);
1134 /* we're frozen, so add to the queue and release our freeze */
1135 g_object_notify_queue_add (object, nqueue, notify_pspec);
1136 g_object_notify_queue_thaw (object, nqueue);
1139 /* not frozen, so just dispatch the notification directly */
1140 G_OBJECT_GET_CLASS (object)
1141 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1147 * @object: a #GObject
1148 * @property_name: the name of a property installed on the class of @object.
1150 * Emits a "notify" signal for the property @property_name on @object.
1152 * When possible, eg. when signaling a property change from within the class
1153 * that registered the property, you should use g_object_notify_by_pspec()
1157 g_object_notify (GObject *object,
1158 const gchar *property_name)
1162 g_return_if_fail (G_IS_OBJECT (object));
1163 g_return_if_fail (property_name != NULL);
1164 if (g_atomic_int_get (&object->ref_count) == 0)
1167 g_object_ref (object);
1168 /* We don't need to get the redirect target
1169 * (by, e.g. calling g_object_class_find_property())
1170 * because g_object_notify_queue_add() does that
1172 pspec = g_param_spec_pool_lookup (pspec_pool,
1174 G_OBJECT_TYPE (object),
1178 g_warning ("%s: object class '%s' has no property named '%s'",
1180 G_OBJECT_TYPE_NAME (object),
1183 g_object_notify_by_spec_internal (object, pspec);
1184 g_object_unref (object);
1188 * g_object_notify_by_pspec:
1189 * @object: a #GObject
1190 * @pspec: the #GParamSpec of a property installed on the class of @object.
1192 * Emits a "notify" signal for the property specified by @pspec on @object.
1194 * This function omits the property name lookup, hence it is faster than
1195 * g_object_notify().
1197 * One way to avoid using g_object_notify() from within the
1198 * class that registered the properties, and using g_object_notify_by_pspec()
1199 * instead, is to store the GParamSpec used with
1200 * g_object_class_install_property() inside a static array, e.g.:
1210 * static GParamSpec *properties[PROP_LAST];
1213 * my_object_class_init (MyObjectClass *klass)
1215 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1218 * G_PARAM_READWRITE);
1219 * g_object_class_install_property (gobject_class,
1221 * properties[PROP_FOO]);
1225 * and then notify a change on the "foo" property with:
1228 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1234 g_object_notify_by_pspec (GObject *object,
1238 g_return_if_fail (G_IS_OBJECT (object));
1239 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1241 if (g_atomic_int_get (&object->ref_count) == 0)
1244 g_object_ref (object);
1245 g_object_notify_by_spec_internal (object, pspec);
1246 g_object_unref (object);
1250 * g_object_thaw_notify:
1251 * @object: a #GObject
1253 * Reverts the effect of a previous call to
1254 * g_object_freeze_notify(). The freeze count is decreased on @object
1255 * and when it reaches zero, queued "notify" signals are emitted.
1257 * Duplicate notifications for each property are squashed so that at most one
1258 * #GObject::notify signal is emitted for each property.
1260 * It is an error to call this function when the freeze count is zero.
1263 g_object_thaw_notify (GObject *object)
1265 GObjectNotifyQueue *nqueue;
1267 g_return_if_fail (G_IS_OBJECT (object));
1268 if (g_atomic_int_get (&object->ref_count) == 0)
1271 g_object_ref (object);
1273 /* FIXME: Freezing is the only way to get at the notify queue.
1274 * So we freeze once and then thaw twice.
1276 nqueue = g_object_notify_queue_freeze (object, FALSE);
1277 g_object_notify_queue_thaw (object, nqueue);
1278 g_object_notify_queue_thaw (object, nqueue);
1280 g_object_unref (object);
1284 object_get_property (GObject *object,
1288 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1289 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1290 GParamSpec *redirect;
1294 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1295 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1299 redirect = g_param_spec_get_redirect_target (pspec);
1303 class->get_property (object, param_id, value, pspec);
1307 object_set_property (GObject *object,
1309 const GValue *value,
1310 GObjectNotifyQueue *nqueue)
1312 GValue tmp_value = G_VALUE_INIT;
1313 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1314 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1315 GParamSpec *redirect;
1316 static const gchar * enable_diagnostic = NULL;
1320 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1321 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1325 redirect = g_param_spec_get_redirect_target (pspec);
1329 if (G_UNLIKELY (!enable_diagnostic))
1331 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1332 if (!enable_diagnostic)
1333 enable_diagnostic = "0";
1336 if (enable_diagnostic[0] == '1')
1338 if (pspec->flags & G_PARAM_DEPRECATED)
1339 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1340 "anymore. It will be removed in a future version.",
1341 G_OBJECT_TYPE_NAME (object), pspec->name);
1344 /* provide a copy to work from, convert (if necessary) and validate */
1345 g_value_init (&tmp_value, pspec->value_type);
1346 if (!g_value_transform (value, &tmp_value))
1347 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1349 g_type_name (pspec->value_type),
1350 G_VALUE_TYPE_NAME (value));
1351 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1353 gchar *contents = g_strdup_value_contents (value);
1355 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1357 G_VALUE_TYPE_NAME (value),
1359 g_type_name (pspec->value_type));
1364 GParamSpec *notify_pspec;
1366 class->set_property (object, param_id, &tmp_value, pspec);
1368 notify_pspec = get_notify_pspec (pspec);
1370 if (notify_pspec != NULL)
1371 g_object_notify_queue_add (object, nqueue, notify_pspec);
1373 g_value_unset (&tmp_value);
1377 object_interface_check_properties (gpointer check_data,
1380 GTypeInterface *iface_class = g_iface;
1381 GObjectClass *class;
1382 GType iface_type = iface_class->g_type;
1383 GParamSpec **pspecs;
1386 class = g_type_class_ref (iface_class->g_instance_type);
1388 if (!G_IS_OBJECT_CLASS (class))
1391 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1395 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1397 G_OBJECT_CLASS_TYPE (class),
1402 g_critical ("Object class %s doesn't implement property "
1403 "'%s' from interface '%s'",
1404 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1406 g_type_name (iface_type));
1411 /* We do a number of checks on the properties of an interface to
1412 * make sure that all classes implementing the interface are
1413 * overriding the properties in a sane way.
1415 * We do the checks in order of importance so that we can give
1416 * more useful error messages first.
1418 * First, we check that the implementation doesn't remove the
1419 * basic functionality (readability, writability) advertised by
1420 * the interface. Next, we check that it doesn't introduce
1421 * additional restrictions (such as construct-only). Finally, we
1422 * make sure the types are compatible.
1425 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1426 /* If the property on the interface is readable then the
1427 * implementation must be readable. If the interface is writable
1428 * then the implementation must be writable.
1430 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1432 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1433 "property on interface '%s'\n", pspecs[n]->name,
1434 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1438 /* If the property on the interface is writable then we need to
1439 * make sure the implementation doesn't introduce new restrictions
1440 * on that writability (ie: construct-only).
1442 * If the interface was not writable to begin with then we don't
1443 * really have any problems here because "writable at construct
1444 * type only" is still more permissive than "read only".
1446 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1448 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1450 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1451 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1452 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1458 /* If the property on the interface is readable then we are
1459 * effectively advertising that reading the property will return a
1460 * value of a specific type. All implementations of the interface
1461 * need to return items of this type -- but may be more
1462 * restrictive. For example, it is legal to have:
1464 * GtkWidget *get_item();
1466 * that is implemented by a function that always returns a
1467 * GtkEntry. In short: readability implies that the
1468 * implementation value type must be equal or more restrictive.
1470 * Similarly, if the property on the interface is writable then
1471 * must be able to accept the property being set to any value of
1472 * that type, including subclasses. In this case, we may also be
1473 * less restrictive. For example, it is legal to have:
1475 * set_item (GtkEntry *);
1477 * that is implemented by a function that will actually work with
1478 * any GtkWidget. In short: writability implies that the
1479 * implementation value type must be equal or less restrictive.
1481 * In the case that the property is both readable and writable
1482 * then the only way that both of the above can be satisfied is
1483 * with a type that is exactly equal.
1485 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1487 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1488 /* class pspec value type must have exact equality with interface */
1489 if (pspecs[n]->value_type != class_pspec->value_type)
1490 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1491 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1492 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1493 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1496 case G_PARAM_READABLE:
1497 /* class pspec value type equal or more restrictive than interface */
1498 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1499 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1500 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1501 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1502 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1505 case G_PARAM_WRITABLE:
1506 /* class pspec value type equal or less restrictive than interface */
1507 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1508 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1509 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1510 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1511 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1515 g_assert_not_reached ();
1521 g_type_class_unref (class);
1525 g_object_get_type (void)
1527 return G_TYPE_OBJECT;
1531 * g_object_new: (skip)
1532 * @object_type: the type id of the #GObject subtype to instantiate
1533 * @first_property_name: the name of the first property
1534 * @...: the value of the first property, followed optionally by more
1535 * name/value pairs, followed by %NULL
1537 * Creates a new instance of a #GObject subtype and sets its properties.
1539 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1540 * which are not explicitly specified are set to their default values.
1542 * Returns: (transfer full): a new instance of @object_type
1545 g_object_new (GType object_type,
1546 const gchar *first_property_name,
1552 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1554 /* short circuit for calls supplying no properties */
1555 if (!first_property_name)
1556 return g_object_newv (object_type, 0, NULL);
1558 va_start (var_args, first_property_name);
1559 object = g_object_new_valist (object_type, first_property_name, var_args);
1566 slist_maybe_remove (GSList **slist,
1569 GSList *last = NULL, *node = *slist;
1572 if (node->data == data)
1575 last->next = node->next;
1577 *slist = node->next;
1578 g_slist_free_1 (node);
1587 static inline gboolean
1588 object_in_construction_list (GObject *object)
1590 gboolean in_construction;
1591 G_LOCK (construction_mutex);
1592 in_construction = g_slist_find (construction_objects, object) != NULL;
1593 G_UNLOCK (construction_mutex);
1594 return in_construction;
1598 g_object_new_with_custom_constructor (GObjectClass *class,
1599 GObjectConstructParam *params,
1602 GObjectNotifyQueue *nqueue = NULL;
1603 gboolean newly_constructed;
1604 GObjectConstructParam *cparams;
1612 /* If we have ->constructed() then we have to do a lot more work.
1613 * It's possible that this is a singleton and it's also possible
1614 * that the user's constructor() will attempt to modify the values
1615 * that we pass in, so we'll need to allocate copies of them.
1616 * It's also possible that the user may attempt to call
1617 * g_object_set() from inside of their constructor, so we need to
1618 * add ourselves to a list of objects for which that is allowed
1619 * while their constructor() is running.
1622 /* Create the array of GObjectConstructParams for constructor() */
1623 n_cparams = g_slist_length (class->construct_properties);
1624 cparams = g_new (GObjectConstructParam, n_cparams);
1625 cvalues = g_new0 (GValue, n_cparams);
1629 /* As above, we may find the value in the passed-in params list.
1631 * If we have the value passed in then we can use the GValue from
1632 * it directly because it is safe to modify. If we use the
1633 * default value from the class, we had better not pass that in
1634 * and risk it being modified, so we create a new one.
1636 for (node = class->construct_properties; node; node = node->next)
1643 value = NULL; /* to silence gcc... */
1645 for (j = 0; j < n_params; j++)
1646 if (params[j].pspec == pspec)
1648 value = params[j].value;
1654 value = &cvalues[cvals_used++];
1655 g_value_init (value, pspec->value_type);
1656 g_param_value_set_default (pspec, value);
1659 cparams[i].pspec = pspec;
1660 cparams[i].value = value;
1664 /* construct object from construction parameters */
1665 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1666 /* free construction values */
1668 while (cvals_used--)
1669 g_value_unset (&cvalues[cvals_used]);
1672 /* There is code in the wild that relies on being able to return NULL
1673 * from its custom constructor. This was never a supported operation
1674 * and will leak memory, but since the code is already out there...
1678 g_critical ("Custom constructor for class %s returned NULL (which is invalid). Unable to remove object "
1679 "from construction_objects list, so memory was probably just leaked. Please use GInitable "
1680 "instead.", G_OBJECT_CLASS_NAME (class));
1684 /* g_object_init() will have added us to the construction_objects
1685 * list. Check if we're in it (and remove us) in order to find
1686 * out if we were newly-constructed or this is an already-existing
1687 * singleton (in which case we should not do 'constructed').
1689 G_LOCK (construction_mutex);
1690 newly_constructed = slist_maybe_remove (&construction_objects, object);
1691 G_UNLOCK (construction_mutex);
1693 if (CLASS_HAS_PROPS (class))
1695 /* If this object was newly_constructed then g_object_init()
1696 * froze the queue. We need to freeze it here in order to get
1697 * the handle so that we can thaw it below (otherwise it will
1698 * be frozen forever).
1700 * We also want to do a freeze if we have any params to set,
1701 * even on a non-newly_constructed object.
1703 * It's possible that we have the case of non-newly created
1704 * singleton and all of the passed-in params were construct
1705 * properties so n_params > 0 but we will actually set no
1706 * properties. This is a pretty lame case to optimise, so
1707 * just ignore it and freeze anyway.
1709 if (newly_constructed || n_params)
1710 nqueue = g_object_notify_queue_freeze (object, FALSE);
1712 /* Remember: if it was newly_constructed then g_object_init()
1713 * already did a freeze, so we now have two. Release one.
1715 if (newly_constructed)
1716 g_object_notify_queue_thaw (object, nqueue);
1719 /* run 'constructed' handler if there is a custom one */
1720 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1721 class->constructed (object);
1723 /* set remaining properties */
1724 for (i = 0; i < n_params; i++)
1725 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1726 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1728 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1730 g_object_notify_queue_thaw (object, nqueue);
1736 g_object_new_internal (GObjectClass *class,
1737 GObjectConstructParam *params,
1740 GObjectNotifyQueue *nqueue = NULL;
1743 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1744 return g_object_new_with_custom_constructor (class, params, n_params);
1746 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1748 if (CLASS_HAS_PROPS (class))
1752 /* This will have been setup in g_object_init() */
1753 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1754 g_assert (nqueue != NULL);
1756 /* We will set exactly n_construct_properties construct
1757 * properties, but they may come from either the class default
1758 * values or the passed-in parameter list.
1760 for (node = class->construct_properties; node; node = node->next)
1762 const GValue *value;
1767 value = NULL; /* to silence gcc... */
1769 for (j = 0; j < n_params; j++)
1770 if (params[j].pspec == pspec)
1772 value = params[j].value;
1777 value = g_param_spec_get_default_value (pspec);
1779 object_set_property (object, pspec, value, nqueue);
1783 /* run 'constructed' handler if there is a custom one */
1784 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1785 class->constructed (object);
1791 /* Set remaining properties. The construct properties will
1792 * already have been taken, so set only the non-construct
1795 for (i = 0; i < n_params; i++)
1796 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1797 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1799 g_object_notify_queue_thaw (object, nqueue);
1807 * @object_type: the type id of the #GObject subtype to instantiate
1808 * @n_parameters: the length of the @parameters array
1809 * @parameters: (array length=n_parameters): an array of #GParameter
1811 * Creates a new instance of a #GObject subtype and sets its properties.
1813 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1814 * which are not explicitly specified are set to their default values.
1816 * Rename to: g_object_new
1817 * Returns: (type GObject.Object) (transfer full): a new instance of
1821 g_object_newv (GType object_type,
1823 GParameter *parameters)
1825 GObjectClass *class, *unref_class = NULL;
1828 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1829 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1831 /* Try to avoid thrashing the ref_count if we don't need to (since
1832 * it's a locked operation).
1834 class = g_type_class_peek_static (object_type);
1837 class = unref_class = g_type_class_ref (object_type);
1841 GObjectConstructParam *cparams;
1844 cparams = g_newa (GObjectConstructParam, n_parameters);
1847 for (i = 0; i < n_parameters; i++)
1852 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
1854 if G_UNLIKELY (!pspec)
1856 g_critical ("%s: object class '%s' has no property named '%s'",
1857 G_STRFUNC, g_type_name (object_type), parameters[i].name);
1861 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1863 g_critical ("%s: property '%s' of object class '%s' is not writable",
1864 G_STRFUNC, pspec->name, g_type_name (object_type));
1868 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1870 for (k = 0; k < j; k++)
1871 if (cparams[k].pspec == pspec)
1873 if G_UNLIKELY (k != j)
1875 g_critical ("%s: construct property '%s' for type '%s' cannot be set twice",
1876 G_STRFUNC, parameters[i].name, g_type_name (object_type));
1881 cparams[j].pspec = pspec;
1882 cparams[j].value = ¶meters[i].value;
1886 object = g_object_new_internal (class, cparams, j);
1889 /* Fast case: no properties passed in. */
1890 object = g_object_new_internal (class, NULL, 0);
1893 g_type_class_unref (unref_class);
1899 * g_object_new_valist: (skip)
1900 * @object_type: the type id of the #GObject subtype to instantiate
1901 * @first_property_name: the name of the first property
1902 * @var_args: the value of the first property, followed optionally by more
1903 * name/value pairs, followed by %NULL
1905 * Creates a new instance of a #GObject subtype and sets its properties.
1907 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1908 * which are not explicitly specified are set to their default values.
1910 * Returns: a new instance of @object_type
1913 g_object_new_valist (GType object_type,
1914 const gchar *first_property_name,
1917 GObjectClass *class, *unref_class = NULL;
1920 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1922 /* Try to avoid thrashing the ref_count if we don't need to (since
1923 * it's a locked operation).
1925 class = g_type_class_peek_static (object_type);
1928 class = unref_class = g_type_class_ref (object_type);
1930 if (first_property_name)
1932 GObjectConstructParam stack_params[16];
1933 GObjectConstructParam *params;
1937 name = first_property_name;
1938 params = stack_params;
1942 gchar *error = NULL;
1946 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
1948 if G_UNLIKELY (!pspec)
1950 g_critical ("%s: object class '%s' has no property named '%s'",
1951 G_STRFUNC, g_type_name (object_type), name);
1952 /* Can't continue because arg list will be out of sync. */
1956 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1958 g_critical ("%s: property '%s' of object class '%s' is not writable",
1959 G_STRFUNC, pspec->name, g_type_name (object_type));
1963 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1965 for (i = 0; i < n_params; i++)
1966 if (params[i].pspec == pspec)
1968 if G_UNLIKELY (i != n_params)
1970 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1971 G_STRFUNC, name, g_type_name (object_type));
1978 params = g_new (GObjectConstructParam, n_params + 1);
1979 memcpy (params, stack_params, sizeof stack_params);
1981 else if (n_params > 16)
1982 params = g_renew (GObjectConstructParam, params, n_params + 1);
1984 params[n_params].pspec = pspec;
1985 params[n_params].value = g_newa (GValue, 1);
1986 memset (params[n_params].value, 0, sizeof (GValue));
1988 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
1992 g_critical ("%s: %s", G_STRFUNC, error);
1993 g_value_unset (params[n_params].value);
2000 while ((name = va_arg (var_args, const gchar *)));
2002 object = g_object_new_internal (class, params, n_params);
2005 g_value_unset (params[n_params].value);
2007 if (params != stack_params)
2011 /* Fast case: no properties passed in. */
2012 object = g_object_new_internal (class, NULL, 0);
2015 g_type_class_unref (unref_class);
2021 g_object_constructor (GType type,
2022 guint n_construct_properties,
2023 GObjectConstructParam *construct_params)
2028 object = (GObject*) g_type_create_instance (type);
2030 /* set construction parameters */
2031 if (n_construct_properties)
2033 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2035 /* set construct properties */
2036 while (n_construct_properties--)
2038 GValue *value = construct_params->value;
2039 GParamSpec *pspec = construct_params->pspec;
2042 object_set_property (object, pspec, value, nqueue);
2044 g_object_notify_queue_thaw (object, nqueue);
2045 /* the notification queue is still frozen from g_object_init(), so
2046 * we don't need to handle it here, g_object_newv() takes
2055 g_object_constructed (GObject *object)
2057 /* empty default impl to allow unconditional upchaining */
2061 * g_object_set_valist: (skip)
2062 * @object: a #GObject
2063 * @first_property_name: name of the first property to set
2064 * @var_args: value for the first property, followed optionally by more
2065 * name/value pairs, followed by %NULL
2067 * Sets properties on an object.
2070 g_object_set_valist (GObject *object,
2071 const gchar *first_property_name,
2074 GObjectNotifyQueue *nqueue;
2077 g_return_if_fail (G_IS_OBJECT (object));
2079 g_object_ref (object);
2080 nqueue = g_object_notify_queue_freeze (object, FALSE);
2082 name = first_property_name;
2085 GValue value = G_VALUE_INIT;
2087 gchar *error = NULL;
2089 pspec = g_param_spec_pool_lookup (pspec_pool,
2091 G_OBJECT_TYPE (object),
2095 g_warning ("%s: object class '%s' has no property named '%s'",
2097 G_OBJECT_TYPE_NAME (object),
2101 if (!(pspec->flags & G_PARAM_WRITABLE))
2103 g_warning ("%s: property '%s' of object class '%s' is not writable",
2106 G_OBJECT_TYPE_NAME (object));
2109 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2111 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2112 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2116 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2120 g_warning ("%s: %s", G_STRFUNC, error);
2122 g_value_unset (&value);
2126 object_set_property (object, pspec, &value, nqueue);
2127 g_value_unset (&value);
2129 name = va_arg (var_args, gchar*);
2132 g_object_notify_queue_thaw (object, nqueue);
2133 g_object_unref (object);
2137 * g_object_get_valist: (skip)
2138 * @object: a #GObject
2139 * @first_property_name: name of the first property to get
2140 * @var_args: return location for the first property, followed optionally by more
2141 * name/return location pairs, followed by %NULL
2143 * Gets properties of an object.
2145 * In general, a copy is made of the property contents and the caller
2146 * is responsible for freeing the memory in the appropriate manner for
2147 * the type, for instance by calling g_free() or g_object_unref().
2149 * See g_object_get().
2152 g_object_get_valist (GObject *object,
2153 const gchar *first_property_name,
2158 g_return_if_fail (G_IS_OBJECT (object));
2160 g_object_ref (object);
2162 name = first_property_name;
2166 GValue value = G_VALUE_INIT;
2170 pspec = g_param_spec_pool_lookup (pspec_pool,
2172 G_OBJECT_TYPE (object),
2176 g_warning ("%s: object class '%s' has no property named '%s'",
2178 G_OBJECT_TYPE_NAME (object),
2182 if (!(pspec->flags & G_PARAM_READABLE))
2184 g_warning ("%s: property '%s' of object class '%s' is not readable",
2187 G_OBJECT_TYPE_NAME (object));
2191 g_value_init (&value, pspec->value_type);
2193 object_get_property (object, pspec, &value);
2195 G_VALUE_LCOPY (&value, var_args, 0, &error);
2198 g_warning ("%s: %s", G_STRFUNC, error);
2200 g_value_unset (&value);
2204 g_value_unset (&value);
2206 name = va_arg (var_args, gchar*);
2209 g_object_unref (object);
2213 * g_object_set: (skip)
2214 * @object: a #GObject
2215 * @first_property_name: name of the first property to set
2216 * @...: value for the first property, followed optionally by more
2217 * name/value pairs, followed by %NULL
2219 * Sets properties on an object.
2222 g_object_set (gpointer _object,
2223 const gchar *first_property_name,
2226 GObject *object = _object;
2229 g_return_if_fail (G_IS_OBJECT (object));
2231 va_start (var_args, first_property_name);
2232 g_object_set_valist (object, first_property_name, var_args);
2237 * g_object_get: (skip)
2238 * @object: a #GObject
2239 * @first_property_name: name of the first property to get
2240 * @...: return location for the first property, followed optionally by more
2241 * name/return location pairs, followed by %NULL
2243 * Gets properties of an object.
2245 * In general, a copy is made of the property contents and the caller
2246 * is responsible for freeing the memory in the appropriate manner for
2247 * the type, for instance by calling g_free() or g_object_unref().
2250 * <title>Using g_object_get(<!-- -->)</title>
2251 * An example of using g_object_get() to get the contents
2252 * of three properties - one of type #G_TYPE_INT,
2253 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
2259 * g_object_get (my_object,
2260 * "int-property", &intval,
2261 * "str-property", &strval,
2262 * "obj-property", &objval,
2265 * // Do something with intval, strval, objval
2268 * g_object_unref (objval);
2273 g_object_get (gpointer _object,
2274 const gchar *first_property_name,
2277 GObject *object = _object;
2280 g_return_if_fail (G_IS_OBJECT (object));
2282 va_start (var_args, first_property_name);
2283 g_object_get_valist (object, first_property_name, var_args);
2288 * g_object_set_property:
2289 * @object: a #GObject
2290 * @property_name: the name of the property to set
2293 * Sets a property on an object.
2296 g_object_set_property (GObject *object,
2297 const gchar *property_name,
2298 const GValue *value)
2300 GObjectNotifyQueue *nqueue;
2303 g_return_if_fail (G_IS_OBJECT (object));
2304 g_return_if_fail (property_name != NULL);
2305 g_return_if_fail (G_IS_VALUE (value));
2307 g_object_ref (object);
2308 nqueue = g_object_notify_queue_freeze (object, FALSE);
2310 pspec = g_param_spec_pool_lookup (pspec_pool,
2312 G_OBJECT_TYPE (object),
2315 g_warning ("%s: object class '%s' has no property named '%s'",
2317 G_OBJECT_TYPE_NAME (object),
2319 else if (!(pspec->flags & G_PARAM_WRITABLE))
2320 g_warning ("%s: property '%s' of object class '%s' is not writable",
2323 G_OBJECT_TYPE_NAME (object));
2324 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2325 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2326 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2328 object_set_property (object, pspec, value, nqueue);
2330 g_object_notify_queue_thaw (object, nqueue);
2331 g_object_unref (object);
2335 * g_object_get_property:
2336 * @object: a #GObject
2337 * @property_name: the name of the property to get
2338 * @value: return location for the property value
2340 * Gets a property of an object. @value must have been initialized to the
2341 * expected type of the property (or a type to which the expected type can be
2342 * transformed) using g_value_init().
2344 * In general, a copy is made of the property contents and the caller is
2345 * responsible for freeing the memory by calling g_value_unset().
2347 * Note that g_object_get_property() is really intended for language
2348 * bindings, g_object_get() is much more convenient for C programming.
2351 g_object_get_property (GObject *object,
2352 const gchar *property_name,
2357 g_return_if_fail (G_IS_OBJECT (object));
2358 g_return_if_fail (property_name != NULL);
2359 g_return_if_fail (G_IS_VALUE (value));
2361 g_object_ref (object);
2363 pspec = g_param_spec_pool_lookup (pspec_pool,
2365 G_OBJECT_TYPE (object),
2368 g_warning ("%s: object class '%s' has no property named '%s'",
2370 G_OBJECT_TYPE_NAME (object),
2372 else if (!(pspec->flags & G_PARAM_READABLE))
2373 g_warning ("%s: property '%s' of object class '%s' is not readable",
2376 G_OBJECT_TYPE_NAME (object));
2379 GValue *prop_value, tmp_value = G_VALUE_INIT;
2381 /* auto-conversion of the callers value type
2383 if (G_VALUE_TYPE (value) == pspec->value_type)
2385 g_value_reset (value);
2388 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2390 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2391 G_STRFUNC, pspec->name,
2392 g_type_name (pspec->value_type),
2393 G_VALUE_TYPE_NAME (value));
2394 g_object_unref (object);
2399 g_value_init (&tmp_value, pspec->value_type);
2400 prop_value = &tmp_value;
2402 object_get_property (object, pspec, prop_value);
2403 if (prop_value != value)
2405 g_value_transform (prop_value, value);
2406 g_value_unset (&tmp_value);
2410 g_object_unref (object);
2414 * g_object_connect: (skip)
2415 * @object: a #GObject
2416 * @signal_spec: the spec for the first signal
2417 * @...: #GCallback for the first signal, followed by data for the
2418 * first signal, followed optionally by more signal
2419 * spec/callback/data triples, followed by %NULL
2421 * A convenience function to connect multiple signals at once.
2423 * The signal specs expected by this function have the form
2424 * "modifier::signal_name", where modifier can be one of the following:
2427 * <term>signal</term>
2429 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2430 * </para></listitem>
2433 * <term>object_signal</term>
2434 * <term>object-signal</term>
2436 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2437 * </para></listitem>
2440 * <term>swapped_signal</term>
2441 * <term>swapped-signal</term>
2443 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2444 * </para></listitem>
2447 * <term>swapped_object_signal</term>
2448 * <term>swapped-object-signal</term>
2450 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2451 * </para></listitem>
2454 * <term>signal_after</term>
2455 * <term>signal-after</term>
2457 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2458 * </para></listitem>
2461 * <term>object_signal_after</term>
2462 * <term>object-signal-after</term>
2464 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2465 * </para></listitem>
2468 * <term>swapped_signal_after</term>
2469 * <term>swapped-signal-after</term>
2471 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2472 * </para></listitem>
2475 * <term>swapped_object_signal_after</term>
2476 * <term>swapped-object-signal-after</term>
2478 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2479 * </para></listitem>
2484 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2485 * "type", GTK_WINDOW_POPUP,
2488 * "signal::event", gtk_menu_window_event, menu,
2489 * "signal::size_request", gtk_menu_window_size_request, menu,
2490 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2494 * Returns: (transfer none): @object
2497 g_object_connect (gpointer _object,
2498 const gchar *signal_spec,
2501 GObject *object = _object;
2504 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2505 g_return_val_if_fail (object->ref_count > 0, object);
2507 va_start (var_args, signal_spec);
2510 GCallback callback = va_arg (var_args, GCallback);
2511 gpointer data = va_arg (var_args, gpointer);
2513 if (strncmp (signal_spec, "signal::", 8) == 0)
2514 g_signal_connect_data (object, signal_spec + 8,
2515 callback, data, NULL,
2517 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2518 strncmp (signal_spec, "object-signal::", 15) == 0)
2519 g_signal_connect_object (object, signal_spec + 15,
2522 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2523 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2524 g_signal_connect_data (object, signal_spec + 16,
2525 callback, data, NULL,
2527 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2528 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2529 g_signal_connect_object (object, signal_spec + 23,
2532 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2533 strncmp (signal_spec, "signal-after::", 14) == 0)
2534 g_signal_connect_data (object, signal_spec + 14,
2535 callback, data, NULL,
2537 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2538 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2539 g_signal_connect_object (object, signal_spec + 21,
2542 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2543 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2544 g_signal_connect_data (object, signal_spec + 22,
2545 callback, data, NULL,
2546 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2547 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2548 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2549 g_signal_connect_object (object, signal_spec + 29,
2551 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2554 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2557 signal_spec = va_arg (var_args, gchar*);
2565 * g_object_disconnect: (skip)
2566 * @object: a #GObject
2567 * @signal_spec: the spec for the first signal
2568 * @...: #GCallback for the first signal, followed by data for the first signal,
2569 * followed optionally by more signal spec/callback/data triples,
2572 * A convenience function to disconnect multiple signals at once.
2574 * The signal specs expected by this function have the form
2575 * "any_signal", which means to disconnect any signal with matching
2576 * callback and data, or "any_signal::signal_name", which only
2577 * disconnects the signal named "signal_name".
2580 g_object_disconnect (gpointer _object,
2581 const gchar *signal_spec,
2584 GObject *object = _object;
2587 g_return_if_fail (G_IS_OBJECT (object));
2588 g_return_if_fail (object->ref_count > 0);
2590 va_start (var_args, signal_spec);
2593 GCallback callback = va_arg (var_args, GCallback);
2594 gpointer data = va_arg (var_args, gpointer);
2595 guint sid = 0, detail = 0, mask = 0;
2597 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2598 strncmp (signal_spec, "any-signal::", 12) == 0)
2601 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2603 else if (strcmp (signal_spec, "any_signal") == 0 ||
2604 strcmp (signal_spec, "any-signal") == 0)
2607 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2611 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2615 if ((mask & G_SIGNAL_MATCH_ID) &&
2616 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2617 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2618 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2620 NULL, (gpointer)callback, data))
2621 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2622 signal_spec = va_arg (var_args, gchar*);
2633 } weak_refs[1]; /* flexible array */
2637 weak_refs_notify (gpointer data)
2639 WeakRefStack *wstack = data;
2642 for (i = 0; i < wstack->n_weak_refs; i++)
2643 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2648 * g_object_weak_ref: (skip)
2649 * @object: #GObject to reference weakly
2650 * @notify: callback to invoke before the object is freed
2651 * @data: extra data to pass to notify
2653 * Adds a weak reference callback to an object. Weak references are
2654 * used for notification when an object is finalized. They are called
2655 * "weak references" because they allow you to safely hold a pointer
2656 * to an object without calling g_object_ref() (g_object_ref() adds a
2657 * strong reference, that is, forces the object to stay alive).
2659 * Note that the weak references created by this method are not
2660 * thread-safe: they cannot safely be used in one thread if the
2661 * object's last g_object_unref() might happen in another thread.
2662 * Use #GWeakRef if thread-safety is required.
2665 g_object_weak_ref (GObject *object,
2669 WeakRefStack *wstack;
2672 g_return_if_fail (G_IS_OBJECT (object));
2673 g_return_if_fail (notify != NULL);
2674 g_return_if_fail (object->ref_count >= 1);
2676 G_LOCK (weak_refs_mutex);
2677 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2680 i = wstack->n_weak_refs++;
2681 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2685 wstack = g_renew (WeakRefStack, NULL, 1);
2686 wstack->object = object;
2687 wstack->n_weak_refs = 1;
2690 wstack->weak_refs[i].notify = notify;
2691 wstack->weak_refs[i].data = data;
2692 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2693 G_UNLOCK (weak_refs_mutex);
2697 * g_object_weak_unref: (skip)
2698 * @object: #GObject to remove a weak reference from
2699 * @notify: callback to search for
2700 * @data: data to search for
2702 * Removes a weak reference callback to an object.
2705 g_object_weak_unref (GObject *object,
2709 WeakRefStack *wstack;
2710 gboolean found_one = FALSE;
2712 g_return_if_fail (G_IS_OBJECT (object));
2713 g_return_if_fail (notify != NULL);
2715 G_LOCK (weak_refs_mutex);
2716 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2721 for (i = 0; i < wstack->n_weak_refs; i++)
2722 if (wstack->weak_refs[i].notify == notify &&
2723 wstack->weak_refs[i].data == data)
2726 wstack->n_weak_refs -= 1;
2727 if (i != wstack->n_weak_refs)
2728 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2733 G_UNLOCK (weak_refs_mutex);
2735 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2739 * g_object_add_weak_pointer: (skip)
2740 * @object: The object that should be weak referenced.
2741 * @weak_pointer_location: (inout): The memory address of a pointer.
2743 * Adds a weak reference from weak_pointer to @object to indicate that
2744 * the pointer located at @weak_pointer_location is only valid during
2745 * the lifetime of @object. When the @object is finalized,
2746 * @weak_pointer will be set to %NULL.
2748 * Note that as with g_object_weak_ref(), the weak references created by
2749 * this method are not thread-safe: they cannot safely be used in one
2750 * thread if the object's last g_object_unref() might happen in another
2751 * thread. Use #GWeakRef if thread-safety is required.
2754 g_object_add_weak_pointer (GObject *object,
2755 gpointer *weak_pointer_location)
2757 g_return_if_fail (G_IS_OBJECT (object));
2758 g_return_if_fail (weak_pointer_location != NULL);
2760 g_object_weak_ref (object,
2761 (GWeakNotify) g_nullify_pointer,
2762 weak_pointer_location);
2766 * g_object_remove_weak_pointer: (skip)
2767 * @object: The object that is weak referenced.
2768 * @weak_pointer_location: (inout): The memory address of a pointer.
2770 * Removes a weak reference from @object that was previously added
2771 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2772 * to match the one used with g_object_add_weak_pointer().
2775 g_object_remove_weak_pointer (GObject *object,
2776 gpointer *weak_pointer_location)
2778 g_return_if_fail (G_IS_OBJECT (object));
2779 g_return_if_fail (weak_pointer_location != NULL);
2781 g_object_weak_unref (object,
2782 (GWeakNotify) g_nullify_pointer,
2783 weak_pointer_location);
2787 object_floating_flag_handler (GObject *object,
2793 case +1: /* force floating if possible */
2795 oldvalue = g_atomic_pointer_get (&object->qdata);
2796 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2797 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2798 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2799 case -1: /* sink if possible */
2801 oldvalue = g_atomic_pointer_get (&object->qdata);
2802 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2803 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2804 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2805 default: /* check floating */
2806 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2811 * g_object_is_floating:
2812 * @object: (type GObject.Object): a #GObject
2814 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2819 * Returns: %TRUE if @object has a floating reference
2822 g_object_is_floating (gpointer _object)
2824 GObject *object = _object;
2825 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2826 return floating_flag_handler (object, 0);
2830 * g_object_ref_sink:
2831 * @object: (type GObject.Object): a #GObject
2833 * Increase the reference count of @object, and possibly remove the
2834 * <link linkend="floating-ref">floating</link> reference, if @object
2835 * has a floating reference.
2837 * In other words, if the object is floating, then this call "assumes
2838 * ownership" of the floating reference, converting it to a normal
2839 * reference by clearing the floating flag while leaving the reference
2840 * count unchanged. If the object is not floating, then this call
2841 * adds a new normal reference increasing the reference count by one.
2845 * Returns: (type GObject.Object) (transfer none): @object
2848 g_object_ref_sink (gpointer _object)
2850 GObject *object = _object;
2851 gboolean was_floating;
2852 g_return_val_if_fail (G_IS_OBJECT (object), object);
2853 g_return_val_if_fail (object->ref_count >= 1, object);
2854 g_object_ref (object);
2855 was_floating = floating_flag_handler (object, -1);
2857 g_object_unref (object);
2862 * g_object_force_floating:
2863 * @object: a #GObject
2865 * This function is intended for #GObject implementations to re-enforce a
2866 * <link linkend="floating-ref">floating</link> object reference.
2867 * Doing this is seldom required: all
2868 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2869 * usually just needs to be sunken by calling g_object_ref_sink().
2874 g_object_force_floating (GObject *object)
2876 g_return_if_fail (G_IS_OBJECT (object));
2877 g_return_if_fail (object->ref_count >= 1);
2879 floating_flag_handler (object, +1);
2884 guint n_toggle_refs;
2886 GToggleNotify notify;
2888 } toggle_refs[1]; /* flexible array */
2892 toggle_refs_notify (GObject *object,
2893 gboolean is_last_ref)
2895 ToggleRefStack tstack, *tstackptr;
2897 G_LOCK (toggle_refs_mutex);
2898 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2899 tstack = *tstackptr;
2900 G_UNLOCK (toggle_refs_mutex);
2902 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2903 * will only be notified when there is exactly one of them.
2905 g_assert (tstack.n_toggle_refs == 1);
2906 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2910 * g_object_add_toggle_ref: (skip)
2911 * @object: a #GObject
2912 * @notify: a function to call when this reference is the
2913 * last reference to the object, or is no longer
2914 * the last reference.
2915 * @data: data to pass to @notify
2917 * Increases the reference count of the object by one and sets a
2918 * callback to be called when all other references to the object are
2919 * dropped, or when this is already the last reference to the object
2920 * and another reference is established.
2922 * This functionality is intended for binding @object to a proxy
2923 * object managed by another memory manager. This is done with two
2924 * paired references: the strong reference added by
2925 * g_object_add_toggle_ref() and a reverse reference to the proxy
2926 * object which is either a strong reference or weak reference.
2928 * The setup is that when there are no other references to @object,
2929 * only a weak reference is held in the reverse direction from @object
2930 * to the proxy object, but when there are other references held to
2931 * @object, a strong reference is held. The @notify callback is called
2932 * when the reference from @object to the proxy object should be
2933 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2934 * true) or weak to strong (@is_last_ref false).
2936 * Since a (normal) reference must be held to the object before
2937 * calling g_object_add_toggle_ref(), the initial state of the reverse
2938 * link is always strong.
2940 * Multiple toggle references may be added to the same gobject,
2941 * however if there are multiple toggle references to an object, none
2942 * of them will ever be notified until all but one are removed. For
2943 * this reason, you should only ever use a toggle reference if there
2944 * is important state in the proxy object.
2949 g_object_add_toggle_ref (GObject *object,
2950 GToggleNotify notify,
2953 ToggleRefStack *tstack;
2956 g_return_if_fail (G_IS_OBJECT (object));
2957 g_return_if_fail (notify != NULL);
2958 g_return_if_fail (object->ref_count >= 1);
2960 g_object_ref (object);
2962 G_LOCK (toggle_refs_mutex);
2963 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2966 i = tstack->n_toggle_refs++;
2967 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2968 * in tstate->toggle_refs */
2969 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2973 tstack = g_renew (ToggleRefStack, NULL, 1);
2974 tstack->object = object;
2975 tstack->n_toggle_refs = 1;
2979 /* Set a flag for fast lookup after adding the first toggle reference */
2980 if (tstack->n_toggle_refs == 1)
2981 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2983 tstack->toggle_refs[i].notify = notify;
2984 tstack->toggle_refs[i].data = data;
2985 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2986 (GDestroyNotify)g_free);
2987 G_UNLOCK (toggle_refs_mutex);
2991 * g_object_remove_toggle_ref: (skip)
2992 * @object: a #GObject
2993 * @notify: a function to call when this reference is the
2994 * last reference to the object, or is no longer
2995 * the last reference.
2996 * @data: data to pass to @notify
2998 * Removes a reference added with g_object_add_toggle_ref(). The
2999 * reference count of the object is decreased by one.
3004 g_object_remove_toggle_ref (GObject *object,
3005 GToggleNotify notify,
3008 ToggleRefStack *tstack;
3009 gboolean found_one = FALSE;
3011 g_return_if_fail (G_IS_OBJECT (object));
3012 g_return_if_fail (notify != NULL);
3014 G_LOCK (toggle_refs_mutex);
3015 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3020 for (i = 0; i < tstack->n_toggle_refs; i++)
3021 if (tstack->toggle_refs[i].notify == notify &&
3022 tstack->toggle_refs[i].data == data)
3025 tstack->n_toggle_refs -= 1;
3026 if (i != tstack->n_toggle_refs)
3027 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3029 if (tstack->n_toggle_refs == 0)
3030 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3035 G_UNLOCK (toggle_refs_mutex);
3038 g_object_unref (object);
3040 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3045 * @object: (type GObject.Object): a #GObject
3047 * Increases the reference count of @object.
3049 * Returns: (type GObject.Object) (transfer none): the same @object
3052 g_object_ref (gpointer _object)
3054 GObject *object = _object;
3057 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3058 g_return_val_if_fail (object->ref_count > 0, NULL);
3060 #ifdef G_ENABLE_DEBUG
3061 if (g_trap_object_ref == object)
3063 #endif /* G_ENABLE_DEBUG */
3066 old_val = g_atomic_int_add (&object->ref_count, 1);
3068 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3069 toggle_refs_notify (object, FALSE);
3071 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3078 * @object: (type GObject.Object): a #GObject
3080 * Decreases the reference count of @object. When its reference count
3081 * drops to 0, the object is finalized (i.e. its memory is freed).
3084 g_object_unref (gpointer _object)
3086 GObject *object = _object;
3089 g_return_if_fail (G_IS_OBJECT (object));
3090 g_return_if_fail (object->ref_count > 0);
3092 #ifdef G_ENABLE_DEBUG
3093 if (g_trap_object_ref == object)
3095 #endif /* G_ENABLE_DEBUG */
3097 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3098 retry_atomic_decrement1:
3099 old_ref = g_atomic_int_get (&object->ref_count);
3102 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3103 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3105 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3106 goto retry_atomic_decrement1;
3108 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3110 /* if we went from 2->1 we need to notify toggle refs if any */
3111 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3112 toggle_refs_notify (object, TRUE);
3116 GSList **weak_locations;
3118 /* The only way that this object can live at this point is if
3119 * there are outstanding weak references already established
3120 * before we got here.
3122 * If there were not already weak references then no more can be
3123 * established at this time, because the other thread would have
3124 * to hold a strong ref in order to call
3125 * g_object_add_weak_pointer() and then we wouldn't be here.
3127 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3129 if (weak_locations != NULL)
3131 g_rw_lock_writer_lock (&weak_locations_lock);
3133 /* It is possible that one of the weak references beat us to
3134 * the lock. Make sure the refcount is still what we expected
3137 old_ref = g_atomic_int_get (&object->ref_count);
3140 g_rw_lock_writer_unlock (&weak_locations_lock);
3141 goto retry_atomic_decrement1;
3144 /* We got the lock first, so the object will definitely die
3145 * now. Clear out all the weak references.
3147 while (*weak_locations)
3149 GWeakRef *weak_ref_location = (*weak_locations)->data;
3151 weak_ref_location->priv.p = NULL;
3152 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3155 g_rw_lock_writer_unlock (&weak_locations_lock);
3158 /* we are about to remove the last reference */
3159 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3160 G_OBJECT_GET_CLASS (object)->dispose (object);
3161 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3163 /* may have been re-referenced meanwhile */
3164 retry_atomic_decrement2:
3165 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3168 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3169 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3171 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3172 goto retry_atomic_decrement2;
3174 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3176 /* if we went from 2->1 we need to notify toggle refs if any */
3177 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3178 toggle_refs_notify (object, TRUE);
3183 /* we are still in the process of taking away the last ref */
3184 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3185 g_signal_handlers_destroy (object);
3186 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3188 /* decrement the last reference */
3189 old_ref = g_atomic_int_add (&object->ref_count, -1);
3191 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3193 /* may have been re-referenced meanwhile */
3194 if (G_LIKELY (old_ref == 1))
3196 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3197 G_OBJECT_GET_CLASS (object)->finalize (object);
3199 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3201 #ifdef G_ENABLE_DEBUG
3204 /* catch objects not chaining finalize handlers */
3205 G_LOCK (debug_objects);
3206 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3207 G_UNLOCK (debug_objects);
3209 #endif /* G_ENABLE_DEBUG */
3210 g_type_free_instance ((GTypeInstance*) object);
3216 * g_clear_object: (skip)
3217 * @object_ptr: a pointer to a #GObject reference
3219 * Clears a reference to a #GObject.
3221 * @object_ptr must not be %NULL.
3223 * If the reference is %NULL then this function does nothing.
3224 * Otherwise, the reference count of the object is decreased and the
3225 * pointer is set to %NULL.
3227 * This function is threadsafe and modifies the pointer atomically,
3228 * using memory barriers where needed.
3230 * A macro is also included that allows this function to be used without
3235 #undef g_clear_object
3237 g_clear_object (volatile GObject **object_ptr)
3239 g_clear_pointer (object_ptr, g_object_unref);
3243 * g_object_get_qdata:
3244 * @object: The GObject to get a stored user data pointer from
3245 * @quark: A #GQuark, naming the user data pointer
3247 * This function gets back user data pointers stored via
3248 * g_object_set_qdata().
3250 * Returns: (transfer none): The user data pointer set, or %NULL
3253 g_object_get_qdata (GObject *object,
3256 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3258 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3262 * g_object_set_qdata: (skip)
3263 * @object: The GObject to set store a user data pointer
3264 * @quark: A #GQuark, naming the user data pointer
3265 * @data: An opaque user data pointer
3267 * This sets an opaque, named pointer on an object.
3268 * The name is specified through a #GQuark (retrived e.g. via
3269 * g_quark_from_static_string()), and the pointer
3270 * can be gotten back from the @object with g_object_get_qdata()
3271 * until the @object is finalized.
3272 * Setting a previously set user data pointer, overrides (frees)
3273 * the old pointer set, using #NULL as pointer essentially
3274 * removes the data stored.
3277 g_object_set_qdata (GObject *object,
3281 g_return_if_fail (G_IS_OBJECT (object));
3282 g_return_if_fail (quark > 0);
3284 g_datalist_id_set_data (&object->qdata, quark, data);
3288 * g_object_dup_qdata:
3289 * @object: the #GObject to store user data on
3290 * @quark: a #GQuark, naming the user data pointer
3291 * @dup_func: (allow-none): function to dup the value
3292 * @user_data: (allow-none): passed as user_data to @dup_func
3294 * This is a variant of g_object_get_qdata() which returns
3295 * a 'duplicate' of the value. @dup_func defines the
3296 * meaning of 'duplicate' in this context, it could e.g.
3297 * take a reference on a ref-counted object.
3299 * If the @quark is not set on the object then @dup_func
3300 * will be called with a %NULL argument.
3302 * Note that @dup_func is called while user data of @object
3305 * This function can be useful to avoid races when multiple
3306 * threads are using object data on the same key on the same
3309 * Returns: the result of calling @dup_func on the value
3310 * associated with @quark on @object, or %NULL if not set.
3311 * If @dup_func is %NULL, the value is returned
3317 g_object_dup_qdata (GObject *object,
3319 GDuplicateFunc dup_func,
3322 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3323 g_return_val_if_fail (quark > 0, NULL);
3325 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3329 * g_object_replace_qdata:
3330 * @object: the #GObject to store user data on
3331 * @quark: a #GQuark, naming the user data pointer
3332 * @oldval: (allow-none): the old value to compare against
3333 * @newval: (allow-none): the new value
3334 * @destroy: (allow-none): a destroy notify for the new value
3335 * @old_destroy: (allow-none): destroy notify for the existing value
3337 * Compares the user data for the key @quark on @object with
3338 * @oldval, and if they are the same, replaces @oldval with
3341 * This is like a typical atomic compare-and-exchange
3342 * operation, for user data on an object.
3344 * If the previous value was replaced then ownership of the
3345 * old value (@oldval) is passed to the caller, including
3346 * the registered destroy notify for it (passed out in @old_destroy).
3347 * Its up to the caller to free this as he wishes, which may
3348 * or may not include using @old_destroy as sometimes replacement
3349 * should not destroy the object in the normal way.
3351 * Return: %TRUE if the existing value for @quark was replaced
3352 * by @newval, %FALSE otherwise.
3357 g_object_replace_qdata (GObject *object,
3361 GDestroyNotify destroy,
3362 GDestroyNotify *old_destroy)
3364 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3365 g_return_val_if_fail (quark > 0, FALSE);
3367 return g_datalist_id_replace_data (&object->qdata, quark,
3368 oldval, newval, destroy,
3373 * g_object_set_qdata_full: (skip)
3374 * @object: The GObject to set store a user data pointer
3375 * @quark: A #GQuark, naming the user data pointer
3376 * @data: An opaque user data pointer
3377 * @destroy: Function to invoke with @data as argument, when @data
3380 * This function works like g_object_set_qdata(), but in addition,
3381 * a void (*destroy) (gpointer) function may be specified which is
3382 * called with @data as argument when the @object is finalized, or
3383 * the data is being overwritten by a call to g_object_set_qdata()
3384 * with the same @quark.
3387 g_object_set_qdata_full (GObject *object,
3390 GDestroyNotify destroy)
3392 g_return_if_fail (G_IS_OBJECT (object));
3393 g_return_if_fail (quark > 0);
3395 g_datalist_id_set_data_full (&object->qdata, quark, data,
3396 data ? destroy : (GDestroyNotify) NULL);
3400 * g_object_steal_qdata:
3401 * @object: The GObject to get a stored user data pointer from
3402 * @quark: A #GQuark, naming the user data pointer
3404 * This function gets back user data pointers stored via
3405 * g_object_set_qdata() and removes the @data from object
3406 * without invoking its destroy() function (if any was
3408 * Usually, calling this function is only required to update
3409 * user data pointers with a destroy notifier, for example:
3412 * object_add_to_user_list (GObject *object,
3413 * const gchar *new_string)
3415 * // the quark, naming the object data
3416 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3417 * // retrive the old string list
3418 * GList *list = g_object_steal_qdata (object, quark_string_list);
3420 * // prepend new string
3421 * list = g_list_prepend (list, g_strdup (new_string));
3422 * // this changed 'list', so we need to set it again
3423 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3426 * free_string_list (gpointer data)
3428 * GList *node, *list = data;
3430 * for (node = list; node; node = node->next)
3431 * g_free (node->data);
3432 * g_list_free (list);
3435 * Using g_object_get_qdata() in the above example, instead of
3436 * g_object_steal_qdata() would have left the destroy function set,
3437 * and thus the partial string list would have been freed upon
3438 * g_object_set_qdata_full().
3440 * Returns: (transfer full): The user data pointer set, or %NULL
3443 g_object_steal_qdata (GObject *object,
3446 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3447 g_return_val_if_fail (quark > 0, NULL);
3449 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3453 * g_object_get_data:
3454 * @object: #GObject containing the associations
3455 * @key: name of the key for that association
3457 * Gets a named field from the objects table of associations (see g_object_set_data()).
3459 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3462 g_object_get_data (GObject *object,
3465 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3466 g_return_val_if_fail (key != NULL, NULL);
3468 return g_datalist_get_data (&object->qdata, key);
3472 * g_object_set_data:
3473 * @object: #GObject containing the associations.
3474 * @key: name of the key
3475 * @data: data to associate with that key
3477 * Each object carries around a table of associations from
3478 * strings to pointers. This function lets you set an association.
3480 * If the object already had an association with that name,
3481 * the old association will be destroyed.
3484 g_object_set_data (GObject *object,
3488 g_return_if_fail (G_IS_OBJECT (object));
3489 g_return_if_fail (key != NULL);
3491 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3495 * g_object_dup_data:
3496 * @object: the #GObject to store user data on
3497 * @key: a string, naming the user data pointer
3498 * @dup_func: (allow-none): function to dup the value
3499 * @user_data: (allow-none): passed as user_data to @dup_func
3501 * This is a variant of g_object_get_data() which returns
3502 * a 'duplicate' of the value. @dup_func defines the
3503 * meaning of 'duplicate' in this context, it could e.g.
3504 * take a reference on a ref-counted object.
3506 * If the @key is not set on the object then @dup_func
3507 * will be called with a %NULL argument.
3509 * Note that @dup_func is called while user data of @object
3512 * This function can be useful to avoid races when multiple
3513 * threads are using object data on the same key on the same
3516 * Returns: the result of calling @dup_func on the value
3517 * associated with @key on @object, or %NULL if not set.
3518 * If @dup_func is %NULL, the value is returned
3524 g_object_dup_data (GObject *object,
3526 GDuplicateFunc dup_func,
3529 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3530 g_return_val_if_fail (key != NULL, NULL);
3532 return g_datalist_id_dup_data (&object->qdata,
3533 g_quark_from_string (key),
3534 dup_func, user_data);
3538 * g_object_replace_data:
3539 * @object: the #GObject to store user data on
3540 * @key: a string, naming the user data pointer
3541 * @oldval: (allow-none): the old value to compare against
3542 * @newval: (allow-none): the new value
3543 * @destroy: (allow-none): a destroy notify for the new value
3544 * @old_destroy: (allow-none): destroy notify for the existing value
3546 * Compares the user data for the key @key on @object with
3547 * @oldval, and if they are the same, replaces @oldval with
3550 * This is like a typical atomic compare-and-exchange
3551 * operation, for user data on an object.
3553 * If the previous value was replaced then ownership of the
3554 * old value (@oldval) is passed to the caller, including
3555 * the registered destroy notify for it (passed out in @old_destroy).
3556 * Its up to the caller to free this as he wishes, which may
3557 * or may not include using @old_destroy as sometimes replacement
3558 * should not destroy the object in the normal way.
3560 * Return: %TRUE if the existing value for @key was replaced
3561 * by @newval, %FALSE otherwise.
3566 g_object_replace_data (GObject *object,
3570 GDestroyNotify destroy,
3571 GDestroyNotify *old_destroy)
3573 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3574 g_return_val_if_fail (key != NULL, FALSE);
3576 return g_datalist_id_replace_data (&object->qdata,
3577 g_quark_from_string (key),
3578 oldval, newval, destroy,
3583 * g_object_set_data_full: (skip)
3584 * @object: #GObject containing the associations
3585 * @key: name of the key
3586 * @data: data to associate with that key
3587 * @destroy: function to call when the association is destroyed
3589 * Like g_object_set_data() except it adds notification
3590 * for when the association is destroyed, either by setting it
3591 * to a different value or when the object is destroyed.
3593 * Note that the @destroy callback is not called if @data is %NULL.
3596 g_object_set_data_full (GObject *object,
3599 GDestroyNotify destroy)
3601 g_return_if_fail (G_IS_OBJECT (object));
3602 g_return_if_fail (key != NULL);
3604 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3605 data ? destroy : (GDestroyNotify) NULL);
3609 * g_object_steal_data:
3610 * @object: #GObject containing the associations
3611 * @key: name of the key
3613 * Remove a specified datum from the object's data associations,
3614 * without invoking the association's destroy handler.
3616 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3619 g_object_steal_data (GObject *object,
3624 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3625 g_return_val_if_fail (key != NULL, NULL);
3627 quark = g_quark_try_string (key);
3629 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3633 g_value_object_init (GValue *value)
3635 value->data[0].v_pointer = NULL;
3639 g_value_object_free_value (GValue *value)
3641 if (value->data[0].v_pointer)
3642 g_object_unref (value->data[0].v_pointer);
3646 g_value_object_copy_value (const GValue *src_value,
3649 if (src_value->data[0].v_pointer)
3650 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3652 dest_value->data[0].v_pointer = NULL;
3656 g_value_object_transform_value (const GValue *src_value,
3659 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)))
3660 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3662 dest_value->data[0].v_pointer = NULL;
3666 g_value_object_peek_pointer (const GValue *value)
3668 return value->data[0].v_pointer;
3672 g_value_object_collect_value (GValue *value,
3673 guint n_collect_values,
3674 GTypeCValue *collect_values,
3675 guint collect_flags)
3677 if (collect_values[0].v_pointer)
3679 GObject *object = collect_values[0].v_pointer;
3681 if (object->g_type_instance.g_class == NULL)
3682 return g_strconcat ("invalid unclassed object pointer for value type '",
3683 G_VALUE_TYPE_NAME (value),
3686 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3687 return g_strconcat ("invalid object type '",
3688 G_OBJECT_TYPE_NAME (object),
3689 "' for value type '",
3690 G_VALUE_TYPE_NAME (value),
3693 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3694 value->data[0].v_pointer = g_object_ref (object);
3697 value->data[0].v_pointer = NULL;
3703 g_value_object_lcopy_value (const GValue *value,
3704 guint n_collect_values,
3705 GTypeCValue *collect_values,
3706 guint collect_flags)
3708 GObject **object_p = collect_values[0].v_pointer;
3711 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3713 if (!value->data[0].v_pointer)
3715 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3716 *object_p = value->data[0].v_pointer;
3718 *object_p = g_object_ref (value->data[0].v_pointer);
3724 * g_value_set_object:
3725 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3726 * @v_object: (type GObject.Object) (allow-none): object value to be set
3728 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3730 * g_value_set_object() increases the reference count of @v_object
3731 * (the #GValue holds a reference to @v_object). If you do not wish
3732 * to increase the reference count of the object (i.e. you wish to
3733 * pass your current reference to the #GValue because you no longer
3734 * need it), use g_value_take_object() instead.
3736 * It is important that your #GValue holds a reference to @v_object (either its
3737 * own, or one it has taken) to ensure that the object won't be destroyed while
3738 * the #GValue still exists).
3741 g_value_set_object (GValue *value,
3746 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3748 old = value->data[0].v_pointer;
3752 g_return_if_fail (G_IS_OBJECT (v_object));
3753 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3755 value->data[0].v_pointer = v_object;
3756 g_object_ref (value->data[0].v_pointer);
3759 value->data[0].v_pointer = NULL;
3762 g_object_unref (old);
3766 * g_value_set_object_take_ownership: (skip)
3767 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3768 * @v_object: (allow-none): object value to be set
3770 * This is an internal function introduced mainly for C marshallers.
3772 * Deprecated: 2.4: Use g_value_take_object() instead.
3775 g_value_set_object_take_ownership (GValue *value,
3778 g_value_take_object (value, v_object);
3782 * g_value_take_object: (skip)
3783 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3784 * @v_object: (allow-none): object value to be set
3786 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3787 * and takes over the ownership of the callers reference to @v_object;
3788 * the caller doesn't have to unref it any more (i.e. the reference
3789 * count of the object is not increased).
3791 * If you want the #GValue to hold its own reference to @v_object, use
3792 * g_value_set_object() instead.
3797 g_value_take_object (GValue *value,
3800 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3802 if (value->data[0].v_pointer)
3804 g_object_unref (value->data[0].v_pointer);
3805 value->data[0].v_pointer = NULL;
3810 g_return_if_fail (G_IS_OBJECT (v_object));
3811 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3813 value->data[0].v_pointer = v_object; /* we take over the reference count */
3818 * g_value_get_object:
3819 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3821 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3823 * Returns: (type GObject.Object) (transfer none): object contents of @value
3826 g_value_get_object (const GValue *value)
3828 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3830 return value->data[0].v_pointer;
3834 * g_value_dup_object:
3835 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3837 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3838 * its reference count. If the contents of the #GValue are %NULL, then
3839 * %NULL will be returned.
3841 * Returns: (type GObject.Object) (transfer full): object content of @value,
3842 * should be unreferenced when no longer needed.
3845 g_value_dup_object (const GValue *value)
3847 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3849 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3853 * g_signal_connect_object: (skip)
3854 * @instance: the instance to connect to.
3855 * @detailed_signal: a string of the form "signal-name::detail".
3856 * @c_handler: the #GCallback to connect.
3857 * @gobject: the object to pass as data to @c_handler.
3858 * @connect_flags: a combination of #GConnectFlags.
3860 * This is similar to g_signal_connect_data(), but uses a closure which
3861 * ensures that the @gobject stays alive during the call to @c_handler
3862 * by temporarily adding a reference count to @gobject.
3864 * When the @gobject is destroyed the signal handler will be automatically
3865 * disconnected. Note that this is not currently threadsafe (ie:
3866 * emitting a signal while @gobject is being destroyed in another thread
3869 * Returns: the handler id.
3872 g_signal_connect_object (gpointer instance,
3873 const gchar *detailed_signal,
3874 GCallback c_handler,
3876 GConnectFlags connect_flags)
3878 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3879 g_return_val_if_fail (detailed_signal != NULL, 0);
3880 g_return_val_if_fail (c_handler != NULL, 0);
3886 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3888 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3890 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3893 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3899 GClosure *closures[1]; /* flexible array */
3901 /* don't change this structure without supplying an accessor for
3902 * watched closures, e.g.:
3903 * GSList* g_object_list_watched_closures (GObject *object)
3906 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3907 * carray = g_object_get_data (object, "GObject-closure-array");
3910 * GSList *slist = NULL;
3912 * for (i = 0; i < carray->n_closures; i++)
3913 * slist = g_slist_prepend (slist, carray->closures[i]);
3921 object_remove_closure (gpointer data,
3924 GObject *object = data;
3928 G_LOCK (closure_array_mutex);
3929 carray = g_object_get_qdata (object, quark_closure_array);
3930 for (i = 0; i < carray->n_closures; i++)
3931 if (carray->closures[i] == closure)
3933 carray->n_closures--;
3934 if (i < carray->n_closures)
3935 carray->closures[i] = carray->closures[carray->n_closures];
3936 G_UNLOCK (closure_array_mutex);
3939 G_UNLOCK (closure_array_mutex);
3940 g_assert_not_reached ();
3944 destroy_closure_array (gpointer data)
3946 CArray *carray = data;
3947 GObject *object = carray->object;
3948 guint i, n = carray->n_closures;
3950 for (i = 0; i < n; i++)
3952 GClosure *closure = carray->closures[i];
3954 /* removing object_remove_closure() upfront is probably faster than
3955 * letting it fiddle with quark_closure_array which is empty anyways
3957 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3958 g_closure_invalidate (closure);
3964 * g_object_watch_closure:
3965 * @object: GObject restricting lifetime of @closure
3966 * @closure: GClosure to watch
3968 * This function essentially limits the life time of the @closure to
3969 * the life time of the object. That is, when the object is finalized,
3970 * the @closure is invalidated by calling g_closure_invalidate() on
3971 * it, in order to prevent invocations of the closure with a finalized
3972 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3973 * added as marshal guards to the @closure, to ensure that an extra
3974 * reference count is held on @object during invocation of the
3975 * @closure. Usually, this function will be called on closures that
3976 * use this @object as closure data.
3979 g_object_watch_closure (GObject *object,
3985 g_return_if_fail (G_IS_OBJECT (object));
3986 g_return_if_fail (closure != NULL);
3987 g_return_if_fail (closure->is_invalid == FALSE);
3988 g_return_if_fail (closure->in_marshal == FALSE);
3989 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3991 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3992 g_closure_add_marshal_guards (closure,
3993 object, (GClosureNotify) g_object_ref,
3994 object, (GClosureNotify) g_object_unref);
3995 G_LOCK (closure_array_mutex);
3996 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3999 carray = g_renew (CArray, NULL, 1);
4000 carray->object = object;
4001 carray->n_closures = 1;
4006 i = carray->n_closures++;
4007 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4009 carray->closures[i] = closure;
4010 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4011 G_UNLOCK (closure_array_mutex);
4015 * g_closure_new_object:
4016 * @sizeof_closure: the size of the structure to allocate, must be at least
4017 * <literal>sizeof (GClosure)</literal>
4018 * @object: a #GObject pointer to store in the @data field of the newly
4019 * allocated #GClosure
4021 * A variant of g_closure_new_simple() which stores @object in the
4022 * @data field of the closure and calls g_object_watch_closure() on
4023 * @object and the created closure. This function is mainly useful
4024 * when implementing new types of closures.
4026 * Returns: (transfer full): a newly allocated #GClosure
4029 g_closure_new_object (guint sizeof_closure,
4034 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4035 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4037 closure = g_closure_new_simple (sizeof_closure, object);
4038 g_object_watch_closure (object, closure);
4044 * g_cclosure_new_object: (skip)
4045 * @callback_func: the function to invoke
4046 * @object: a #GObject pointer to pass to @callback_func
4048 * A variant of g_cclosure_new() which uses @object as @user_data and
4049 * calls g_object_watch_closure() on @object and the created
4050 * closure. This function is useful when you have a callback closely
4051 * associated with a #GObject, and want the callback to no longer run
4052 * after the object is is freed.
4054 * Returns: a new #GCClosure
4057 g_cclosure_new_object (GCallback callback_func,
4062 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4063 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4064 g_return_val_if_fail (callback_func != NULL, NULL);
4066 closure = g_cclosure_new (callback_func, object, NULL);
4067 g_object_watch_closure (object, closure);
4073 * g_cclosure_new_object_swap: (skip)
4074 * @callback_func: the function to invoke
4075 * @object: a #GObject pointer to pass to @callback_func
4077 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4078 * and calls g_object_watch_closure() on @object and the created
4079 * closure. This function is useful when you have a callback closely
4080 * associated with a #GObject, and want the callback to no longer run
4081 * after the object is is freed.
4083 * Returns: a new #GCClosure
4086 g_cclosure_new_object_swap (GCallback callback_func,
4091 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4092 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4093 g_return_val_if_fail (callback_func != NULL, NULL);
4095 closure = g_cclosure_new_swap (callback_func, object, NULL);
4096 g_object_watch_closure (object, closure);
4102 g_object_compat_control (gsize what,
4108 case 1: /* floating base type */
4109 return G_TYPE_INITIALLY_UNOWNED;
4110 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4111 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4113 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4115 *pp = floating_flag_handler;
4122 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
4125 g_initially_unowned_init (GInitiallyUnowned *object)
4127 g_object_force_floating (object);
4131 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4138 * A structure containing a weak reference to a #GObject. It can either
4139 * be empty (i.e. point to %NULL), or point to an object for as long as
4140 * at least one "strong" reference to that object exists. Before the
4141 * object's #GObjectClass.dispose method is called, every #GWeakRef
4142 * associated with becomes empty (i.e. points to %NULL).
4144 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4145 * heap-allocated, or embedded in larger structures.
4147 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4148 * reference is thread-safe: converting a weak pointer to a reference is
4149 * atomic with respect to invalidation of weak pointers to destroyed
4152 * If the object's #GObjectClass.dispose method results in additional
4153 * references to the object being held, any #GWeakRef<!-- -->s taken
4154 * before it was disposed will continue to point to %NULL. If
4155 * #GWeakRef<!-- -->s are taken after the object is disposed and
4156 * re-referenced, they will continue to point to it until its refcount
4157 * goes back to zero, at which point they too will be invalidated.
4161 * g_weak_ref_init: (skip)
4162 * @weak_ref: (inout): uninitialized or empty location for a weak
4164 * @object: (allow-none): a #GObject or %NULL
4166 * Initialise a non-statically-allocated #GWeakRef.
4168 * This function also calls g_weak_ref_set() with @object on the
4169 * freshly-initialised weak reference.
4171 * This function should always be matched with a call to
4172 * g_weak_ref_clear(). It is not necessary to use this function for a
4173 * #GWeakRef in static storage because it will already be
4174 * properly initialised. Just use g_weak_ref_set() directly.
4179 g_weak_ref_init (GWeakRef *weak_ref,
4182 weak_ref->priv.p = NULL;
4184 g_weak_ref_set (weak_ref, object);
4188 * g_weak_ref_clear: (skip)
4189 * @weak_ref: (inout): location of a weak reference, which
4192 * Frees resources associated with a non-statically-allocated #GWeakRef.
4193 * After this call, the #GWeakRef is left in an undefined state.
4195 * You should only call this on a #GWeakRef that previously had
4196 * g_weak_ref_init() called on it.
4201 g_weak_ref_clear (GWeakRef *weak_ref)
4203 g_weak_ref_set (weak_ref, NULL);
4206 weak_ref->priv.p = (void *) 0xccccccccu;
4210 * g_weak_ref_get: (skip)
4211 * @weak_ref: (inout): location of a weak reference to a #GObject
4213 * If @weak_ref is not empty, atomically acquire a strong
4214 * reference to the object it points to, and return that reference.
4216 * This function is needed because of the potential race between taking
4217 * the pointer value and g_object_ref() on it, if the object was losing
4218 * its last reference at the same time in a different thread.
4220 * The caller should release the resulting reference in the usual way,
4221 * by using g_object_unref().
4223 * Returns: (transfer full) (type GObject.Object): the object pointed to
4224 * by @weak_ref, or %NULL if it was empty
4229 g_weak_ref_get (GWeakRef *weak_ref)
4231 gpointer object_or_null;
4233 g_return_val_if_fail (weak_ref!= NULL, NULL);
4235 g_rw_lock_reader_lock (&weak_locations_lock);
4237 object_or_null = weak_ref->priv.p;
4239 if (object_or_null != NULL)
4240 g_object_ref (object_or_null);
4242 g_rw_lock_reader_unlock (&weak_locations_lock);
4244 return object_or_null;
4248 * g_weak_ref_set: (skip)
4249 * @weak_ref: location for a weak reference
4250 * @object: (allow-none): a #GObject or %NULL
4252 * Change the object to which @weak_ref points, or set it to
4255 * You must own a strong reference on @object while calling this
4261 g_weak_ref_set (GWeakRef *weak_ref,
4264 GSList **weak_locations;
4265 GObject *new_object;
4266 GObject *old_object;
4268 g_return_if_fail (weak_ref != NULL);
4269 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4271 new_object = object;
4273 g_rw_lock_writer_lock (&weak_locations_lock);
4275 /* We use the extra level of indirection here so that if we have ever
4276 * had a weak pointer installed at any point in time on this object,
4277 * we can see that there is a non-NULL value associated with the
4278 * weak-pointer quark and know that this value will not change at any
4279 * point in the object's lifetime.
4281 * Both properties are important for reducing the amount of times we
4282 * need to acquire locks and for decreasing the duration of time the
4283 * lock is held while avoiding some rather tricky races.
4285 * Specifically: we can avoid having to do an extra unconditional lock
4286 * in g_object_unref() without worrying about some extremely tricky
4290 old_object = weak_ref->priv.p;
4291 if (new_object != old_object)
4293 weak_ref->priv.p = new_object;
4295 /* Remove the weak ref from the old object */
4296 if (old_object != NULL)
4298 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4299 /* for it to point to an object, the object must have had it added once */
4300 g_assert (weak_locations != NULL);
4302 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4305 /* Add the weak ref to the new object */
4306 if (new_object != NULL)
4308 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4310 if (weak_locations == NULL)
4312 weak_locations = g_new0 (GSList *, 1);
4313 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4316 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4320 g_rw_lock_writer_unlock (&weak_locations_lock);