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"
39 * @short_description: The base object type
40 * @see_also: #GParamSpecObject, g_param_spec_object()
41 * @title: The Base Object Type
43 * GObject is the fundamental type providing the common attributes and
44 * methods for all object types in GTK+, Pango and other libraries
45 * based on GObject. The GObject class provides methods for object
46 * construction and destruction, property access methods, and signal
47 * support. Signals are described in detail in <xref
48 * linkend="gobject-Signals"/>.
50 * <para id="floating-ref">
51 * GInitiallyUnowned is derived from GObject. The only difference between
52 * the two is that the initial reference of a GInitiallyUnowned is flagged
53 * as a <firstterm>floating</firstterm> reference.
54 * This means that it is not specifically claimed to be "owned" by
55 * any code portion. The main motivation for providing floating references is
56 * C convenience. In particular, it allows code to be written as:
58 * container = create_container ();
59 * container_add_child (container, create_child());
61 * If <function>container_add_child()</function> will g_object_ref_sink() the
62 * passed in child, no reference of the newly created child is leaked.
63 * Without floating references, <function>container_add_child()</function>
64 * can only g_object_ref() the new child, so to implement this code without
65 * reference leaks, it would have to be written as:
68 * container = create_container ();
69 * child = create_child ();
70 * container_add_child (container, child);
71 * g_object_unref (child);
73 * The floating reference can be converted into
74 * an ordinary reference by calling g_object_ref_sink().
75 * For already sunken objects (objects that don't have a floating reference
76 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
78 * Since floating references are useful almost exclusively for C convenience,
79 * language bindings that provide automated reference and memory ownership
80 * maintenance (such as smart pointers or garbage collection) should not
81 * expose floating references in their API.
84 * Some object implementations may need to save an objects floating state
85 * across certain code portions (an example is #GtkMenu), to achieve this,
86 * the following sequence can be used:
89 * /* save floating state */
90 * gboolean was_floating = g_object_is_floating (object);
91 * g_object_ref_sink (object);
92 * /* protected code portion */
94 * /* restore floating state */
96 * g_object_force_floating (object);
97 * g_object_unref (object); /* release previously acquired reference */
103 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
104 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
106 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
107 #define OBJECT_HAS_TOGGLE_REF(object) \
108 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
109 #define OBJECT_FLOATING_FLAG 0x2
111 #define CLASS_HAS_PROPS_FLAG 0x1
112 #define CLASS_HAS_PROPS(class) \
113 ((class)->flags & CLASS_HAS_PROPS_FLAG)
114 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
115 ((class)->constructor != g_object_constructor)
116 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
117 ((class)->constructed != g_object_constructed)
119 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
120 #define CLASS_HAS_DERIVED_CLASS(class) \
121 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
123 /* --- signals --- */
130 /* --- properties --- */
136 /* --- prototypes --- */
137 static void g_object_base_class_init (GObjectClass *class);
138 static void g_object_base_class_finalize (GObjectClass *class);
139 static void g_object_do_class_init (GObjectClass *class);
140 static void g_object_init (GObject *object,
141 GObjectClass *class);
142 static GObject* g_object_constructor (GType type,
143 guint n_construct_properties,
144 GObjectConstructParam *construct_params);
145 static void g_object_constructed (GObject *object);
146 static void g_object_real_dispose (GObject *object);
147 static void g_object_finalize (GObject *object);
148 static void g_object_do_set_property (GObject *object,
152 static void g_object_do_get_property (GObject *object,
156 static void g_value_object_init (GValue *value);
157 static void g_value_object_free_value (GValue *value);
158 static void g_value_object_copy_value (const GValue *src_value,
160 static void g_value_object_transform_value (const GValue *src_value,
162 static gpointer g_value_object_peek_pointer (const GValue *value);
163 static gchar* g_value_object_collect_value (GValue *value,
164 guint n_collect_values,
165 GTypeCValue *collect_values,
166 guint collect_flags);
167 static gchar* g_value_object_lcopy_value (const GValue *value,
168 guint n_collect_values,
169 GTypeCValue *collect_values,
170 guint collect_flags);
171 static void g_object_dispatch_properties_changed (GObject *object,
173 GParamSpec **pspecs);
174 static guint object_floating_flag_handler (GObject *object,
177 static void object_interface_check_properties (gpointer func_data,
180 /* --- typedefs --- */
181 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
183 struct _GObjectNotifyQueue
187 guint16 freeze_count;
190 /* --- variables --- */
191 G_LOCK_DEFINE_STATIC (closure_array_mutex);
192 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
193 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
194 static GQuark quark_closure_array = 0;
195 static GQuark quark_weak_refs = 0;
196 static GQuark quark_toggle_refs = 0;
197 static GQuark quark_notify_queue;
198 static GParamSpecPool *pspec_pool = NULL;
199 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
200 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
201 G_LOCK_DEFINE_STATIC (construction_mutex);
202 static GSList *construction_objects = NULL;
204 G_LOCK_DEFINE_STATIC(notify_lock);
206 /* --- functions --- */
208 g_object_notify_queue_free (gpointer data)
210 GObjectNotifyQueue *nqueue = data;
212 g_slist_free (nqueue->pspecs);
213 g_slice_free (GObjectNotifyQueue, nqueue);
216 static GObjectNotifyQueue*
217 g_object_notify_queue_freeze (GObject *object,
218 gboolean conditional)
220 GObjectNotifyQueue *nqueue;
223 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
228 G_UNLOCK(notify_lock);
232 nqueue = g_slice_new0 (GObjectNotifyQueue);
233 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
234 nqueue, g_object_notify_queue_free);
237 if (nqueue->freeze_count >= 65535)
238 g_critical("Free queue for %s (%p) is larger than 65535,"
239 " called g_object_freeze_notify() too often."
240 " Forgot to call g_object_thaw_notify() or infinite loop",
241 G_OBJECT_TYPE_NAME (object), object);
243 nqueue->freeze_count++;
244 G_UNLOCK(notify_lock);
250 g_object_notify_queue_thaw (GObject *object,
251 GObjectNotifyQueue *nqueue)
253 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
257 g_return_if_fail (nqueue->freeze_count > 0);
258 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
262 /* Just make sure we never get into some nasty race condition */
263 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
264 G_UNLOCK(notify_lock);
265 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
266 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
270 nqueue->freeze_count--;
271 if (nqueue->freeze_count) {
272 G_UNLOCK(notify_lock);
276 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
278 for (slist = nqueue->pspecs; slist; slist = slist->next)
280 pspecs[n_pspecs++] = slist->data;
282 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
284 G_UNLOCK(notify_lock);
287 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
292 g_object_notify_queue_add (GObject *object,
293 GObjectNotifyQueue *nqueue,
298 g_return_if_fail (nqueue->n_pspecs < 65535);
300 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
302 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
306 G_UNLOCK(notify_lock);
309 #ifdef G_ENABLE_DEBUG
310 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
311 G_LOCK_DEFINE_STATIC (debug_objects);
312 static volatile GObject *g_trap_object_ref = NULL;
313 static guint debug_objects_count = 0;
314 static GHashTable *debug_objects_ht = NULL;
317 debug_objects_foreach (gpointer key,
321 GObject *object = value;
323 g_message ("[%p] stale %s\tref_count=%u",
325 G_OBJECT_TYPE_NAME (object),
330 debug_objects_atexit (void)
334 G_LOCK (debug_objects);
335 g_message ("stale GObjects: %u", debug_objects_count);
336 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
337 G_UNLOCK (debug_objects);
340 #endif /* G_ENABLE_DEBUG */
343 _g_object_type_init (void)
345 static gboolean initialized = FALSE;
346 static const GTypeFundamentalInfo finfo = {
347 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
350 sizeof (GObjectClass),
351 (GBaseInitFunc) g_object_base_class_init,
352 (GBaseFinalizeFunc) g_object_base_class_finalize,
353 (GClassInitFunc) g_object_do_class_init,
354 NULL /* class_destroy */,
355 NULL /* class_data */,
358 (GInstanceInitFunc) g_object_init,
359 NULL, /* value_table */
361 static const GTypeValueTable value_table = {
362 g_value_object_init, /* value_init */
363 g_value_object_free_value, /* value_free */
364 g_value_object_copy_value, /* value_copy */
365 g_value_object_peek_pointer, /* value_peek_pointer */
366 "p", /* collect_format */
367 g_value_object_collect_value, /* collect_value */
368 "p", /* lcopy_format */
369 g_value_object_lcopy_value, /* lcopy_value */
373 g_return_if_fail (initialized == FALSE);
378 info.value_table = &value_table;
379 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
380 g_assert (type == G_TYPE_OBJECT);
381 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
383 #ifdef G_ENABLE_DEBUG
386 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
387 g_atexit (debug_objects_atexit);
389 #endif /* G_ENABLE_DEBUG */
393 g_object_base_class_init (GObjectClass *class)
395 GObjectClass *pclass = g_type_class_peek_parent (class);
397 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
398 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
401 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
403 /* reset instance specific fields and methods that don't get inherited */
404 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
405 class->get_property = NULL;
406 class->set_property = NULL;
410 g_object_base_class_finalize (GObjectClass *class)
414 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
416 g_slist_free (class->construct_properties);
417 class->construct_properties = NULL;
418 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
419 for (node = list; node; node = node->next)
421 GParamSpec *pspec = node->data;
423 g_param_spec_pool_remove (pspec_pool, pspec);
424 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
425 g_param_spec_unref (pspec);
431 g_object_do_class_init (GObjectClass *class)
433 /* read the comment about typedef struct CArray; on why not to change this quark */
434 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
436 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
437 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
438 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
439 pspec_pool = g_param_spec_pool_new (TRUE);
441 class->constructor = g_object_constructor;
442 class->constructed = g_object_constructed;
443 class->set_property = g_object_do_set_property;
444 class->get_property = g_object_do_get_property;
445 class->dispose = g_object_real_dispose;
446 class->finalize = g_object_finalize;
447 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
448 class->notify = NULL;
452 * @gobject: the object which received the signal.
453 * @pspec: the #GParamSpec of the property which changed.
455 * The notify signal is emitted on an object when one of its
456 * properties has been changed. Note that getting this signal
457 * doesn't guarantee that the value of the property has actually
458 * changed, it may also be emitted when the setter for the property
459 * is called to reinstate the previous value.
461 * This signal is typically used to obtain change notification for a
462 * single property, by specifying the property name as a detail in the
463 * g_signal_connect() call, like this:
465 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
466 * G_CALLBACK (gtk_text_view_target_list_notify),
469 * It is important to note that you must use
470 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
471 * detail strings for the notify signal.
473 gobject_signals[NOTIFY] =
474 g_signal_new (g_intern_static_string ("notify"),
475 G_TYPE_FROM_CLASS (class),
476 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
477 G_STRUCT_OFFSET (GObjectClass, notify),
479 g_cclosure_marshal_VOID__PARAM,
483 /* Install a check function that we'll use to verify that classes that
484 * implement an interface implement all properties for that interface
486 g_type_add_interface_check (NULL, object_interface_check_properties);
490 install_property_internal (GType g_type,
494 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
496 g_warning ("When installing property: type `%s' already has a property named `%s'",
497 g_type_name (g_type),
502 g_param_spec_ref_sink (pspec);
503 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
504 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
508 * g_object_class_install_property:
509 * @oclass: a #GObjectClass
510 * @property_id: the id for the new property
511 * @pspec: the #GParamSpec for the new property
513 * Installs a new property. This is usually done in the class initializer.
515 * Note that it is possible to redefine a property in a derived class,
516 * by installing a property with the same name. This can be useful at times,
517 * e.g. to change the range of allowed values or the default value.
520 g_object_class_install_property (GObjectClass *class,
524 g_return_if_fail (G_IS_OBJECT_CLASS (class));
525 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
527 if (CLASS_HAS_DERIVED_CLASS (class))
528 g_error ("Attempt to add property %s::%s to class after it was derived",
529 G_OBJECT_CLASS_NAME (class), pspec->name);
531 class->flags |= CLASS_HAS_PROPS_FLAG;
533 if (pspec->flags & G_PARAM_WRITABLE)
534 g_return_if_fail (class->set_property != NULL);
535 if (pspec->flags & G_PARAM_READABLE)
536 g_return_if_fail (class->get_property != NULL);
537 g_return_if_fail (property_id > 0);
538 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
539 if (pspec->flags & G_PARAM_CONSTRUCT)
540 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
541 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
542 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
544 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
546 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
547 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
549 /* for property overrides of construct properties, we have to get rid
550 * of the overidden inherited construct property
552 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
553 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
554 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
558 * g_object_class_install_properties:
559 * @oclass: a #GObjectClass
560 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
561 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
562 * defining the new properties
564 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
565 * usually done in the class initializer.
567 * The property id of each property is the index of each #GParamSpec in
570 * The property id of 0 is treated specially by #GObject and it should not
571 * be used to store a #GParamSpec.
573 * This function should be used if you plan to use a static array of
574 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
575 * class initialization:
579 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
582 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
585 * my_object_class_init (MyObjectClass *klass)
587 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
589 * obj_properties[PROP_FOO] =
590 * g_param_spec_int ("foo", "Foo", "Foo",
593 * G_PARAM_READWRITE);
595 * obj_properties[PROP_BAR] =
596 * g_param_spec_string ("bar", "Bar", "Bar",
598 * G_PARAM_READWRITE);
600 * gobject_class->set_property = my_object_set_property;
601 * gobject_class->get_property = my_object_get_property;
602 * g_object_class_install_properties (gobject_class,
608 * allows calling g_object_notify_by_pspec() to notify of property changes:
612 * my_object_set_foo (MyObject *self, gint foo)
614 * if (self->foo != foo)
617 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
625 g_object_class_install_properties (GObjectClass *oclass,
629 GType oclass_type, parent_type;
632 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
633 g_return_if_fail (n_pspecs > 1);
634 g_return_if_fail (pspecs[0] == NULL);
636 if (CLASS_HAS_DERIVED_CLASS (oclass))
637 g_error ("Attempt to add properties to %s after it was derived",
638 G_OBJECT_CLASS_NAME (oclass));
640 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
641 parent_type = g_type_parent (oclass_type);
643 /* we skip the first element of the array as it would have a 0 prop_id */
644 for (i = 1; i < n_pspecs; i++)
646 GParamSpec *pspec = pspecs[i];
648 g_return_if_fail (pspec != NULL);
650 if (pspec->flags & G_PARAM_WRITABLE)
651 g_return_if_fail (oclass->set_property != NULL);
652 if (pspec->flags & G_PARAM_READABLE)
653 g_return_if_fail (oclass->get_property != NULL);
654 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
655 if (pspec->flags & G_PARAM_CONSTRUCT)
656 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
657 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
658 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
660 oclass->flags |= CLASS_HAS_PROPS_FLAG;
661 install_property_internal (oclass_type, i, pspec);
663 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
664 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
666 /* for property overrides of construct properties, we have to get rid
667 * of the overidden inherited construct property
669 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
670 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
671 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
676 * g_object_interface_install_property:
677 * @g_iface: any interface vtable for the interface, or the default
678 * vtable for the interface.
679 * @pspec: the #GParamSpec for the new property
681 * Add a property to an interface; this is only useful for interfaces
682 * that are added to GObject-derived types. Adding a property to an
683 * interface forces all objects classes with that interface to have a
684 * compatible property. The compatible property could be a newly
685 * created #GParamSpec, but normally
686 * g_object_class_override_property() will be used so that the object
687 * class only needs to provide an implementation and inherits the
688 * property description, default value, bounds, and so forth from the
689 * interface property.
691 * This function is meant to be called from the interface's default
692 * vtable initialization function (the @class_init member of
693 * #GTypeInfo.) It must not be called after after @class_init has
694 * been called for any object types implementing this interface.
699 g_object_interface_install_property (gpointer g_iface,
702 GTypeInterface *iface_class = g_iface;
704 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
705 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
706 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
707 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
709 install_property_internal (iface_class->g_type, 0, pspec);
713 * g_object_class_find_property:
714 * @oclass: a #GObjectClass
715 * @property_name: the name of the property to look up
717 * Looks up the #GParamSpec for a property of a class.
719 * Returns: (transfer none): the #GParamSpec for the property, or
720 * %NULL if the class doesn't have a property of that name
723 g_object_class_find_property (GObjectClass *class,
724 const gchar *property_name)
727 GParamSpec *redirect;
729 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
730 g_return_val_if_fail (property_name != NULL, NULL);
732 pspec = g_param_spec_pool_lookup (pspec_pool,
734 G_OBJECT_CLASS_TYPE (class),
738 redirect = g_param_spec_get_redirect_target (pspec);
749 * g_object_interface_find_property:
750 * @g_iface: any interface vtable for the interface, or the default
751 * vtable for the interface
752 * @property_name: name of a property to lookup.
754 * Find the #GParamSpec with the given name for an
755 * interface. Generally, the interface vtable passed in as @g_iface
756 * will be the default vtable from g_type_default_interface_ref(), or,
757 * if you know the interface has already been loaded,
758 * g_type_default_interface_peek().
762 * Returns: (transfer none): the #GParamSpec for the property of the
763 * interface with the name @property_name, or %NULL if no
764 * such property exists.
767 g_object_interface_find_property (gpointer g_iface,
768 const gchar *property_name)
770 GTypeInterface *iface_class = g_iface;
772 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
773 g_return_val_if_fail (property_name != NULL, NULL);
775 return g_param_spec_pool_lookup (pspec_pool,
782 * g_object_class_override_property:
783 * @oclass: a #GObjectClass
784 * @property_id: the new property ID
785 * @name: the name of a property registered in a parent class or
786 * in an interface of this class.
788 * Registers @property_id as referring to a property with the
789 * name @name in a parent class or in an interface implemented
790 * by @oclass. This allows this class to <firstterm>override</firstterm>
791 * a property implementation in a parent class or to provide
792 * the implementation of a property from an interface.
795 * Internally, overriding is implemented by creating a property of type
796 * #GParamSpecOverride; generally operations that query the properties of
797 * the object class, such as g_object_class_find_property() or
798 * g_object_class_list_properties() will return the overridden
799 * property. However, in one case, the @construct_properties argument of
800 * the @constructor virtual function, the #GParamSpecOverride is passed
801 * instead, so that the @param_id field of the #GParamSpec will be
802 * correct. For virtually all uses, this makes no difference. If you
803 * need to get the overridden property, you can call
804 * g_param_spec_get_redirect_target().
810 g_object_class_override_property (GObjectClass *oclass,
814 GParamSpec *overridden = NULL;
818 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
819 g_return_if_fail (property_id > 0);
820 g_return_if_fail (name != NULL);
822 /* Find the overridden property; first check parent types
824 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
825 if (parent_type != G_TYPE_NONE)
826 overridden = g_param_spec_pool_lookup (pspec_pool,
835 /* Now check interfaces
837 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
838 while (n_ifaces-- && !overridden)
840 overridden = g_param_spec_pool_lookup (pspec_pool,
851 g_warning ("%s: Can't find property to override for '%s::%s'",
852 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
856 new = g_param_spec_override (name, overridden);
857 g_object_class_install_property (oclass, property_id, new);
861 * g_object_class_list_properties:
862 * @oclass: a #GObjectClass
863 * @n_properties: (out): return location for the length of the returned array
865 * Get an array of #GParamSpec* for all properties of a class.
867 * Returns: (array length=n_properties) (transfer container): an array of
868 * #GParamSpec* which should be freed after use
870 GParamSpec** /* free result */
871 g_object_class_list_properties (GObjectClass *class,
872 guint *n_properties_p)
877 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
879 pspecs = g_param_spec_pool_list (pspec_pool,
880 G_OBJECT_CLASS_TYPE (class),
889 * g_object_interface_list_properties:
890 * @g_iface: any interface vtable for the interface, or the default
891 * vtable for the interface
892 * @n_properties_p: (out): location to store number of properties returned.
894 * Lists the properties of an interface.Generally, the interface
895 * vtable passed in as @g_iface will be the default vtable from
896 * g_type_default_interface_ref(), or, if you know the interface has
897 * already been loaded, g_type_default_interface_peek().
901 * Returns: (array length=n_properties_p) (transfer container): a
902 * pointer to an array of pointers to #GParamSpec
903 * structures. The paramspecs are owned by GLib, but the
904 * array should be freed with g_free() when you are done with
908 g_object_interface_list_properties (gpointer g_iface,
909 guint *n_properties_p)
911 GTypeInterface *iface_class = g_iface;
915 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
917 pspecs = g_param_spec_pool_list (pspec_pool,
927 g_object_init (GObject *object,
930 object->ref_count = 1;
931 object->qdata = NULL;
933 if (CLASS_HAS_PROPS (class))
935 /* freeze object's notification queue, g_object_newv() preserves pairedness */
936 g_object_notify_queue_freeze (object, FALSE);
939 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
941 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
942 G_LOCK (construction_mutex);
943 construction_objects = g_slist_prepend (construction_objects, object);
944 G_UNLOCK (construction_mutex);
947 #ifdef G_ENABLE_DEBUG
950 G_LOCK (debug_objects);
951 debug_objects_count++;
952 g_hash_table_insert (debug_objects_ht, object, object);
953 G_UNLOCK (debug_objects);
955 #endif /* G_ENABLE_DEBUG */
959 g_object_do_set_property (GObject *object,
967 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
973 g_object_do_get_property (GObject *object,
981 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
987 g_object_real_dispose (GObject *object)
989 g_signal_handlers_destroy (object);
990 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
991 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
995 g_object_finalize (GObject *object)
997 g_datalist_clear (&object->qdata);
999 #ifdef G_ENABLE_DEBUG
1002 G_LOCK (debug_objects);
1003 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1004 g_hash_table_remove (debug_objects_ht, object);
1005 debug_objects_count--;
1006 G_UNLOCK (debug_objects);
1008 #endif /* G_ENABLE_DEBUG */
1013 g_object_dispatch_properties_changed (GObject *object,
1015 GParamSpec **pspecs)
1019 for (i = 0; i < n_pspecs; i++)
1020 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1024 * g_object_run_dispose:
1025 * @object: a #GObject
1027 * Releases all references to other objects. This can be used to break
1030 * This functions should only be called from object system implementations.
1033 g_object_run_dispose (GObject *object)
1035 g_return_if_fail (G_IS_OBJECT (object));
1036 g_return_if_fail (object->ref_count > 0);
1038 g_object_ref (object);
1039 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1040 G_OBJECT_GET_CLASS (object)->dispose (object);
1041 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1042 g_object_unref (object);
1046 * g_object_freeze_notify:
1047 * @object: a #GObject
1049 * Increases the freeze count on @object. If the freeze count is
1050 * non-zero, the emission of "notify" signals on @object is
1051 * stopped. The signals are queued until the freeze count is decreased
1054 * This is necessary for accessors that modify multiple properties to prevent
1055 * premature notification while the object is still being modified.
1058 g_object_freeze_notify (GObject *object)
1060 g_return_if_fail (G_IS_OBJECT (object));
1062 if (g_atomic_int_get (&object->ref_count) == 0)
1065 g_object_ref (object);
1066 g_object_notify_queue_freeze (object, FALSE);
1067 g_object_unref (object);
1071 get_notify_pspec (GParamSpec *pspec)
1073 GParamSpec *redirected;
1075 /* we don't notify on non-READABLE parameters */
1076 if (~pspec->flags & G_PARAM_READABLE)
1079 /* if the paramspec is redirected, notify on the target */
1080 redirected = g_param_spec_get_redirect_target (pspec);
1081 if (redirected != NULL)
1084 /* else, notify normally */
1089 g_object_notify_by_spec_internal (GObject *object,
1092 GParamSpec *notify_pspec;
1094 notify_pspec = get_notify_pspec (pspec);
1096 if (notify_pspec != NULL)
1098 GObjectNotifyQueue *nqueue;
1100 /* conditional freeze: only increase freeze count if already frozen */
1101 nqueue = g_object_notify_queue_freeze (object, TRUE);
1105 /* we're frozen, so add to the queue and release our freeze */
1106 g_object_notify_queue_add (object, nqueue, notify_pspec);
1107 g_object_notify_queue_thaw (object, nqueue);
1110 /* not frozen, so just dispatch the notification directly */
1111 G_OBJECT_GET_CLASS (object)
1112 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1118 * @object: a #GObject
1119 * @property_name: the name of a property installed on the class of @object.
1121 * Emits a "notify" signal for the property @property_name on @object.
1123 * When possible, eg. when signaling a property change from within the class
1124 * that registered the property, you should use g_object_notify_by_pspec()
1128 g_object_notify (GObject *object,
1129 const gchar *property_name)
1133 g_return_if_fail (G_IS_OBJECT (object));
1134 g_return_if_fail (property_name != NULL);
1135 if (g_atomic_int_get (&object->ref_count) == 0)
1138 g_object_ref (object);
1139 /* We don't need to get the redirect target
1140 * (by, e.g. calling g_object_class_find_property())
1141 * because g_object_notify_queue_add() does that
1143 pspec = g_param_spec_pool_lookup (pspec_pool,
1145 G_OBJECT_TYPE (object),
1149 g_warning ("%s: object class `%s' has no property named `%s'",
1151 G_OBJECT_TYPE_NAME (object),
1154 g_object_notify_by_spec_internal (object, pspec);
1155 g_object_unref (object);
1159 * g_object_notify_by_pspec:
1160 * @object: a #GObject
1161 * @pspec: the #GParamSpec of a property installed on the class of @object.
1163 * Emits a "notify" signal for the property specified by @pspec on @object.
1165 * This function omits the property name lookup, hence it is faster than
1166 * g_object_notify().
1168 * One way to avoid using g_object_notify() from within the
1169 * class that registered the properties, and using g_object_notify_by_pspec()
1170 * instead, is to store the GParamSpec used with
1171 * g_object_class_install_property() inside a static array, e.g.:
1181 * static GParamSpec *properties[PROP_LAST];
1184 * my_object_class_init (MyObjectClass *klass)
1186 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1189 * G_PARAM_READWRITE);
1190 * g_object_class_install_property (gobject_class,
1192 * properties[PROP_FOO]);
1196 * and then notify a change on the "foo" property with:
1199 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1205 g_object_notify_by_pspec (GObject *object,
1209 g_return_if_fail (G_IS_OBJECT (object));
1210 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1212 g_object_ref (object);
1213 g_object_notify_by_spec_internal (object, pspec);
1214 g_object_unref (object);
1218 * g_object_thaw_notify:
1219 * @object: a #GObject
1221 * Reverts the effect of a previous call to
1222 * g_object_freeze_notify(). The freeze count is decreased on @object
1223 * and when it reaches zero, all queued "notify" signals are emitted.
1225 * It is an error to call this function when the freeze count is zero.
1228 g_object_thaw_notify (GObject *object)
1230 GObjectNotifyQueue *nqueue;
1232 g_return_if_fail (G_IS_OBJECT (object));
1233 if (g_atomic_int_get (&object->ref_count) == 0)
1236 g_object_ref (object);
1238 /* FIXME: Freezing is the only way to get at the notify queue.
1239 * So we freeze once and then thaw twice.
1241 nqueue = g_object_notify_queue_freeze (object, FALSE);
1242 g_object_notify_queue_thaw (object, nqueue);
1243 g_object_notify_queue_thaw (object, nqueue);
1245 g_object_unref (object);
1249 object_get_property (GObject *object,
1253 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1254 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1255 GParamSpec *redirect;
1259 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1260 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1264 redirect = g_param_spec_get_redirect_target (pspec);
1268 class->get_property (object, param_id, value, pspec);
1272 object_set_property (GObject *object,
1274 const GValue *value,
1275 GObjectNotifyQueue *nqueue)
1277 GValue tmp_value = G_VALUE_INIT;
1278 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1279 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1280 GParamSpec *redirect;
1281 static const gchar * enable_diagnostic = NULL;
1285 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1286 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1290 redirect = g_param_spec_get_redirect_target (pspec);
1294 if (G_UNLIKELY (!enable_diagnostic))
1296 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1297 if (!enable_diagnostic)
1298 enable_diagnostic = "0";
1301 if (enable_diagnostic[0] == '1')
1303 if (pspec->flags & G_PARAM_DEPRECATED)
1304 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1305 "anymore. It will be removed in a future version.",
1306 G_OBJECT_TYPE_NAME (object), pspec->name);
1309 /* provide a copy to work from, convert (if necessary) and validate */
1310 g_value_init (&tmp_value, pspec->value_type);
1311 if (!g_value_transform (value, &tmp_value))
1312 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1314 g_type_name (pspec->value_type),
1315 G_VALUE_TYPE_NAME (value));
1316 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1318 gchar *contents = g_strdup_value_contents (value);
1320 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1322 G_VALUE_TYPE_NAME (value),
1324 g_type_name (pspec->value_type));
1329 GParamSpec *notify_pspec;
1331 class->set_property (object, param_id, &tmp_value, pspec);
1333 notify_pspec = get_notify_pspec (pspec);
1335 if (notify_pspec != NULL)
1336 g_object_notify_queue_add (object, nqueue, notify_pspec);
1338 g_value_unset (&tmp_value);
1342 object_interface_check_properties (gpointer func_data,
1345 GTypeInterface *iface_class = g_iface;
1346 GObjectClass *class;
1347 GType iface_type = iface_class->g_type;
1348 GParamSpec **pspecs;
1351 class = g_type_class_ref (iface_class->g_instance_type);
1353 if (!G_IS_OBJECT_CLASS (class))
1356 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1360 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1362 G_OBJECT_CLASS_TYPE (class),
1367 g_critical ("Object class %s doesn't implement property "
1368 "'%s' from interface '%s'",
1369 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1371 g_type_name (iface_type));
1376 /* The implementation paramspec must have a less restrictive
1377 * type than the interface parameter spec for set() and a
1378 * more restrictive type for get(). We just require equality,
1379 * rather than doing something more complicated checking
1380 * the READABLE and WRITABLE flags. We also simplify here
1381 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1384 !g_type_is_a (pspecs[n]->value_type,
1385 class_pspec->value_type))
1387 g_critical ("Property '%s' on class '%s' has type '%s' "
1388 "which is different from the type '%s', "
1389 "of the property on interface '%s'\n",
1391 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1392 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1393 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1394 g_type_name (iface_type));
1397 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1399 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1400 * READABLE and WRITABLE remove restrictions. The implementation
1401 * paramspec must have less restrictive flags.
1404 (!SUBSET (class_pspec->flags,
1406 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1407 !SUBSET (pspecs[n]->flags,
1409 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1411 g_critical ("Flags for property '%s' on class '%s' "
1412 "are not compatible with the property on"
1415 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1416 g_type_name (iface_type));
1423 g_type_class_unref (class);
1427 g_object_get_type (void)
1429 return G_TYPE_OBJECT;
1433 * g_object_new: (skip)
1434 * @object_type: the type id of the #GObject subtype to instantiate
1435 * @first_property_name: the name of the first property
1436 * @...: the value of the first property, followed optionally by more
1437 * name/value pairs, followed by %NULL
1439 * Creates a new instance of a #GObject subtype and sets its properties.
1441 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1442 * which are not explicitly specified are set to their default values.
1444 * Returns: (transfer full): a new instance of @object_type
1447 g_object_new (GType object_type,
1448 const gchar *first_property_name,
1454 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1456 /* short circuit for calls supplying no properties */
1457 if (!first_property_name)
1458 return g_object_newv (object_type, 0, NULL);
1460 va_start (var_args, first_property_name);
1461 object = g_object_new_valist (object_type, first_property_name, var_args);
1468 slist_maybe_remove (GSList **slist,
1471 GSList *last = NULL, *node = *slist;
1474 if (node->data == data)
1477 last->next = node->next;
1479 *slist = node->next;
1480 g_slist_free_1 (node);
1489 static inline gboolean
1490 object_in_construction_list (GObject *object)
1492 gboolean in_construction;
1493 G_LOCK (construction_mutex);
1494 in_construction = g_slist_find (construction_objects, object) != NULL;
1495 G_UNLOCK (construction_mutex);
1496 return in_construction;
1501 * @object_type: the type id of the #GObject subtype to instantiate
1502 * @n_parameters: the length of the @parameters array
1503 * @parameters: (array length=n_parameters): an array of #GParameter
1505 * Creates a new instance of a #GObject subtype and sets its properties.
1507 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1508 * which are not explicitly specified are set to their default values.
1510 * Rename to: g_object_new
1511 * Returns: (type GObject.Object) (transfer full): a new instance of
1515 g_object_newv (GType object_type,
1517 GParameter *parameters)
1519 GObjectConstructParam *cparams = NULL, *oparams;
1520 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1522 GObjectClass *class, *unref_class = NULL;
1524 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1526 GList *clist = NULL;
1527 gboolean newly_constructed;
1530 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1532 class = g_type_class_peek_static (object_type);
1534 class = unref_class = g_type_class_ref (object_type);
1535 for (slist = class->construct_properties; slist; slist = slist->next)
1537 clist = g_list_prepend (clist, slist->data);
1538 n_total_cparams += 1;
1541 if (n_parameters == 0 && n_total_cparams == 0)
1543 /* This is a simple object with no construct properties, and
1544 * no properties are being set, so short circuit the parameter
1545 * handling. This speeds up simple object construction.
1548 object = class->constructor (object_type, 0, NULL);
1549 goto did_construction;
1552 /* collect parameters, sort into construction and normal ones */
1553 oparams = g_new (GObjectConstructParam, n_parameters);
1554 cparams = g_new (GObjectConstructParam, n_total_cparams);
1555 for (i = 0; i < n_parameters; i++)
1557 GValue *value = ¶meters[i].value;
1558 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1564 g_warning ("%s: object class `%s' has no property named `%s'",
1566 g_type_name (object_type),
1567 parameters[i].name);
1570 if (!(pspec->flags & G_PARAM_WRITABLE))
1572 g_warning ("%s: property `%s' of object class `%s' is not writable",
1575 g_type_name (object_type));
1578 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1580 GList *list = g_list_find (clist, pspec);
1584 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1585 G_STRFUNC, pspec->name, g_type_name (object_type));
1588 cparams[n_cparams].pspec = pspec;
1589 cparams[n_cparams].value = value;
1594 list->prev->next = list->next;
1596 list->next->prev = list->prev;
1597 g_list_free_1 (list);
1601 oparams[n_oparams].pspec = pspec;
1602 oparams[n_oparams].value = value;
1607 /* set remaining construction properties to default values */
1608 n_cvalues = n_total_cparams - n_cparams;
1609 cvalues = g_new (GValue, n_cvalues);
1612 GList *tmp = clist->next;
1613 GParamSpec *pspec = clist->data;
1614 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1617 g_value_init (value, pspec->value_type);
1618 g_param_value_set_default (pspec, value);
1620 cparams[n_cparams].pspec = pspec;
1621 cparams[n_cparams].value = value;
1624 g_list_free_1 (clist);
1628 /* construct object from construction parameters */
1629 object = class->constructor (object_type, n_total_cparams, cparams);
1630 /* free construction values */
1633 g_value_unset (cvalues + n_cvalues);
1637 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1639 /* adjust freeze_count according to g_object_init() and remaining properties */
1640 G_LOCK (construction_mutex);
1641 newly_constructed = slist_maybe_remove (&construction_objects, object);
1642 G_UNLOCK (construction_mutex);
1645 newly_constructed = TRUE;
1647 if (CLASS_HAS_PROPS (class))
1649 if (newly_constructed || n_oparams)
1650 nqueue = g_object_notify_queue_freeze (object, FALSE);
1651 if (newly_constructed)
1652 g_object_notify_queue_thaw (object, nqueue);
1655 /* run 'constructed' handler if there is a custom one */
1656 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1657 class->constructed (object);
1659 /* set remaining properties */
1660 for (i = 0; i < n_oparams; i++)
1661 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1664 if (CLASS_HAS_PROPS (class))
1666 /* release our own freeze count and handle notifications */
1667 if (newly_constructed || n_oparams)
1668 g_object_notify_queue_thaw (object, nqueue);
1672 g_type_class_unref (unref_class);
1678 * g_object_new_valist: (skip)
1679 * @object_type: the type id of the #GObject subtype to instantiate
1680 * @first_property_name: the name of the first property
1681 * @var_args: the value of the first property, followed optionally by more
1682 * name/value pairs, followed by %NULL
1684 * Creates a new instance of a #GObject subtype and sets its properties.
1686 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1687 * which are not explicitly specified are set to their default values.
1689 * Returns: a new instance of @object_type
1692 g_object_new_valist (GType object_type,
1693 const gchar *first_property_name,
1696 GObjectClass *class;
1700 guint n_params = 0, n_alloced_params = 16;
1702 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1704 if (!first_property_name)
1705 return g_object_newv (object_type, 0, NULL);
1707 class = g_type_class_ref (object_type);
1709 params = g_new0 (GParameter, n_alloced_params);
1710 name = first_property_name;
1713 gchar *error = NULL;
1714 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1720 g_warning ("%s: object class `%s' has no property named `%s'",
1722 g_type_name (object_type),
1726 if (n_params >= n_alloced_params)
1728 n_alloced_params += 16;
1729 params = g_renew (GParameter, params, n_alloced_params);
1730 memset (params + n_params, 0, 16 * (sizeof *params));
1732 params[n_params].name = name;
1733 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1734 var_args, 0, &error);
1737 g_warning ("%s: %s", G_STRFUNC, error);
1739 g_value_unset (¶ms[n_params].value);
1743 name = va_arg (var_args, gchar*);
1746 object = g_object_newv (object_type, n_params, params);
1749 g_value_unset (¶ms[n_params].value);
1752 g_type_class_unref (class);
1758 g_object_constructor (GType type,
1759 guint n_construct_properties,
1760 GObjectConstructParam *construct_params)
1765 object = (GObject*) g_type_create_instance (type);
1767 /* set construction parameters */
1768 if (n_construct_properties)
1770 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
1772 /* set construct properties */
1773 while (n_construct_properties--)
1775 GValue *value = construct_params->value;
1776 GParamSpec *pspec = construct_params->pspec;
1779 object_set_property (object, pspec, value, nqueue);
1781 g_object_notify_queue_thaw (object, nqueue);
1782 /* the notification queue is still frozen from g_object_init(), so
1783 * we don't need to handle it here, g_object_newv() takes
1792 g_object_constructed (GObject *object)
1794 /* empty default impl to allow unconditional upchaining */
1798 * g_object_set_valist: (skip)
1799 * @object: a #GObject
1800 * @first_property_name: name of the first property to set
1801 * @var_args: value for the first property, followed optionally by more
1802 * name/value pairs, followed by %NULL
1804 * Sets properties on an object.
1807 g_object_set_valist (GObject *object,
1808 const gchar *first_property_name,
1811 GObjectNotifyQueue *nqueue;
1814 g_return_if_fail (G_IS_OBJECT (object));
1816 g_object_ref (object);
1817 nqueue = g_object_notify_queue_freeze (object, FALSE);
1819 name = first_property_name;
1822 GValue value = G_VALUE_INIT;
1824 gchar *error = NULL;
1826 pspec = g_param_spec_pool_lookup (pspec_pool,
1828 G_OBJECT_TYPE (object),
1832 g_warning ("%s: object class `%s' has no property named `%s'",
1834 G_OBJECT_TYPE_NAME (object),
1838 if (!(pspec->flags & G_PARAM_WRITABLE))
1840 g_warning ("%s: property `%s' of object class `%s' is not writable",
1843 G_OBJECT_TYPE_NAME (object));
1846 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1848 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1849 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1853 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1857 g_warning ("%s: %s", G_STRFUNC, error);
1859 g_value_unset (&value);
1863 object_set_property (object, pspec, &value, nqueue);
1864 g_value_unset (&value);
1866 name = va_arg (var_args, gchar*);
1869 g_object_notify_queue_thaw (object, nqueue);
1870 g_object_unref (object);
1874 * g_object_get_valist: (skip)
1875 * @object: a #GObject
1876 * @first_property_name: name of the first property to get
1877 * @var_args: return location for the first property, followed optionally by more
1878 * name/return location pairs, followed by %NULL
1880 * Gets properties of an object.
1882 * In general, a copy is made of the property contents and the caller
1883 * is responsible for freeing the memory in the appropriate manner for
1884 * the type, for instance by calling g_free() or g_object_unref().
1886 * See g_object_get().
1889 g_object_get_valist (GObject *object,
1890 const gchar *first_property_name,
1895 g_return_if_fail (G_IS_OBJECT (object));
1897 g_object_ref (object);
1899 name = first_property_name;
1903 GValue value = G_VALUE_INIT;
1907 pspec = g_param_spec_pool_lookup (pspec_pool,
1909 G_OBJECT_TYPE (object),
1913 g_warning ("%s: object class `%s' has no property named `%s'",
1915 G_OBJECT_TYPE_NAME (object),
1919 if (!(pspec->flags & G_PARAM_READABLE))
1921 g_warning ("%s: property `%s' of object class `%s' is not readable",
1924 G_OBJECT_TYPE_NAME (object));
1928 g_value_init (&value, pspec->value_type);
1930 object_get_property (object, pspec, &value);
1932 G_VALUE_LCOPY (&value, var_args, 0, &error);
1935 g_warning ("%s: %s", G_STRFUNC, error);
1937 g_value_unset (&value);
1941 g_value_unset (&value);
1943 name = va_arg (var_args, gchar*);
1946 g_object_unref (object);
1950 * g_object_set: (skip)
1951 * @object: a #GObject
1952 * @first_property_name: name of the first property to set
1953 * @...: value for the first property, followed optionally by more
1954 * name/value pairs, followed by %NULL
1956 * Sets properties on an object.
1959 g_object_set (gpointer _object,
1960 const gchar *first_property_name,
1963 GObject *object = _object;
1966 g_return_if_fail (G_IS_OBJECT (object));
1968 va_start (var_args, first_property_name);
1969 g_object_set_valist (object, first_property_name, var_args);
1974 * g_object_get: (skip)
1975 * @object: a #GObject
1976 * @first_property_name: name of the first property to get
1977 * @...: return location for the first property, followed optionally by more
1978 * name/return location pairs, followed by %NULL
1980 * Gets properties of an object.
1982 * In general, a copy is made of the property contents and the caller
1983 * is responsible for freeing the memory in the appropriate manner for
1984 * the type, for instance by calling g_free() or g_object_unref().
1987 * <title>Using g_object_get(<!-- -->)</title>
1988 * An example of using g_object_get() to get the contents
1989 * of three properties - one of type #G_TYPE_INT,
1990 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1996 * g_object_get (my_object,
1997 * "int-property", &intval,
1998 * "str-property", &strval,
1999 * "obj-property", &objval,
2002 * // Do something with intval, strval, objval
2005 * g_object_unref (objval);
2010 g_object_get (gpointer _object,
2011 const gchar *first_property_name,
2014 GObject *object = _object;
2017 g_return_if_fail (G_IS_OBJECT (object));
2019 va_start (var_args, first_property_name);
2020 g_object_get_valist (object, first_property_name, var_args);
2025 * g_object_set_property:
2026 * @object: a #GObject
2027 * @property_name: the name of the property to set
2030 * Sets a property on an object.
2033 g_object_set_property (GObject *object,
2034 const gchar *property_name,
2035 const GValue *value)
2037 GObjectNotifyQueue *nqueue;
2040 g_return_if_fail (G_IS_OBJECT (object));
2041 g_return_if_fail (property_name != NULL);
2042 g_return_if_fail (G_IS_VALUE (value));
2044 g_object_ref (object);
2045 nqueue = g_object_notify_queue_freeze (object, FALSE);
2047 pspec = g_param_spec_pool_lookup (pspec_pool,
2049 G_OBJECT_TYPE (object),
2052 g_warning ("%s: object class `%s' has no property named `%s'",
2054 G_OBJECT_TYPE_NAME (object),
2056 else if (!(pspec->flags & G_PARAM_WRITABLE))
2057 g_warning ("%s: property `%s' of object class `%s' is not writable",
2060 G_OBJECT_TYPE_NAME (object));
2061 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2062 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2063 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2065 object_set_property (object, pspec, value, nqueue);
2067 g_object_notify_queue_thaw (object, nqueue);
2068 g_object_unref (object);
2072 * g_object_get_property:
2073 * @object: a #GObject
2074 * @property_name: the name of the property to get
2075 * @value: return location for the property value
2077 * Gets a property of an object. @value must have been initialized to the
2078 * expected type of the property (or a type to which the expected type can be
2079 * transformed) using g_value_init().
2081 * In general, a copy is made of the property contents and the caller is
2082 * responsible for freeing the memory by calling g_value_unset().
2084 * Note that g_object_get_property() is really intended for language
2085 * bindings, g_object_get() is much more convenient for C programming.
2088 g_object_get_property (GObject *object,
2089 const gchar *property_name,
2094 g_return_if_fail (G_IS_OBJECT (object));
2095 g_return_if_fail (property_name != NULL);
2096 g_return_if_fail (G_IS_VALUE (value));
2098 g_object_ref (object);
2100 pspec = g_param_spec_pool_lookup (pspec_pool,
2102 G_OBJECT_TYPE (object),
2105 g_warning ("%s: object class `%s' has no property named `%s'",
2107 G_OBJECT_TYPE_NAME (object),
2109 else if (!(pspec->flags & G_PARAM_READABLE))
2110 g_warning ("%s: property `%s' of object class `%s' is not readable",
2113 G_OBJECT_TYPE_NAME (object));
2116 GValue *prop_value, tmp_value = G_VALUE_INIT;
2118 /* auto-conversion of the callers value type
2120 if (G_VALUE_TYPE (value) == pspec->value_type)
2122 g_value_reset (value);
2125 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2127 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2128 G_STRFUNC, pspec->name,
2129 g_type_name (pspec->value_type),
2130 G_VALUE_TYPE_NAME (value));
2131 g_object_unref (object);
2136 g_value_init (&tmp_value, pspec->value_type);
2137 prop_value = &tmp_value;
2139 object_get_property (object, pspec, prop_value);
2140 if (prop_value != value)
2142 g_value_transform (prop_value, value);
2143 g_value_unset (&tmp_value);
2147 g_object_unref (object);
2151 * g_object_connect: (skip)
2152 * @object: a #GObject
2153 * @signal_spec: the spec for the first signal
2154 * @...: #GCallback for the first signal, followed by data for the
2155 * first signal, followed optionally by more signal
2156 * spec/callback/data triples, followed by %NULL
2158 * A convenience function to connect multiple signals at once.
2160 * The signal specs expected by this function have the form
2161 * "modifier::signal_name", where modifier can be one of the following:
2164 * <term>signal</term>
2166 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2167 * </para></listitem>
2170 * <term>object_signal</term>
2171 * <term>object-signal</term>
2173 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2174 * </para></listitem>
2177 * <term>swapped_signal</term>
2178 * <term>swapped-signal</term>
2180 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2181 * </para></listitem>
2184 * <term>swapped_object_signal</term>
2185 * <term>swapped-object-signal</term>
2187 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2188 * </para></listitem>
2191 * <term>signal_after</term>
2192 * <term>signal-after</term>
2194 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2195 * </para></listitem>
2198 * <term>object_signal_after</term>
2199 * <term>object-signal-after</term>
2201 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2202 * </para></listitem>
2205 * <term>swapped_signal_after</term>
2206 * <term>swapped-signal-after</term>
2208 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2209 * </para></listitem>
2212 * <term>swapped_object_signal_after</term>
2213 * <term>swapped-object-signal-after</term>
2215 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2216 * </para></listitem>
2221 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2222 * "type", GTK_WINDOW_POPUP,
2225 * "signal::event", gtk_menu_window_event, menu,
2226 * "signal::size_request", gtk_menu_window_size_request, menu,
2227 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2231 * Returns: (transfer none): @object
2234 g_object_connect (gpointer _object,
2235 const gchar *signal_spec,
2238 GObject *object = _object;
2241 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2242 g_return_val_if_fail (object->ref_count > 0, object);
2244 va_start (var_args, signal_spec);
2247 GCallback callback = va_arg (var_args, GCallback);
2248 gpointer data = va_arg (var_args, gpointer);
2250 if (strncmp (signal_spec, "signal::", 8) == 0)
2251 g_signal_connect_data (object, signal_spec + 8,
2252 callback, data, NULL,
2254 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2255 strncmp (signal_spec, "object-signal::", 15) == 0)
2256 g_signal_connect_object (object, signal_spec + 15,
2259 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2260 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2261 g_signal_connect_data (object, signal_spec + 16,
2262 callback, data, NULL,
2264 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2265 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2266 g_signal_connect_object (object, signal_spec + 23,
2269 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2270 strncmp (signal_spec, "signal-after::", 14) == 0)
2271 g_signal_connect_data (object, signal_spec + 14,
2272 callback, data, NULL,
2274 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2275 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2276 g_signal_connect_object (object, signal_spec + 21,
2279 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2280 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2281 g_signal_connect_data (object, signal_spec + 22,
2282 callback, data, NULL,
2283 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2284 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2285 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2286 g_signal_connect_object (object, signal_spec + 29,
2288 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2291 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2294 signal_spec = va_arg (var_args, gchar*);
2302 * g_object_disconnect: (skip)
2303 * @object: a #GObject
2304 * @signal_spec: the spec for the first signal
2305 * @...: #GCallback for the first signal, followed by data for the first signal,
2306 * followed optionally by more signal spec/callback/data triples,
2309 * A convenience function to disconnect multiple signals at once.
2311 * The signal specs expected by this function have the form
2312 * "any_signal", which means to disconnect any signal with matching
2313 * callback and data, or "any_signal::signal_name", which only
2314 * disconnects the signal named "signal_name".
2317 g_object_disconnect (gpointer _object,
2318 const gchar *signal_spec,
2321 GObject *object = _object;
2324 g_return_if_fail (G_IS_OBJECT (object));
2325 g_return_if_fail (object->ref_count > 0);
2327 va_start (var_args, signal_spec);
2330 GCallback callback = va_arg (var_args, GCallback);
2331 gpointer data = va_arg (var_args, gpointer);
2332 guint sid = 0, detail = 0, mask = 0;
2334 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2335 strncmp (signal_spec, "any-signal::", 12) == 0)
2338 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2340 else if (strcmp (signal_spec, "any_signal") == 0 ||
2341 strcmp (signal_spec, "any-signal") == 0)
2344 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2348 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2352 if ((mask & G_SIGNAL_MATCH_ID) &&
2353 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2354 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2355 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2357 NULL, (gpointer)callback, data))
2358 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2359 signal_spec = va_arg (var_args, gchar*);
2370 } weak_refs[1]; /* flexible array */
2374 weak_refs_notify (gpointer data)
2376 WeakRefStack *wstack = data;
2379 for (i = 0; i < wstack->n_weak_refs; i++)
2380 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2385 * g_object_weak_ref: (skip)
2386 * @object: #GObject to reference weakly
2387 * @notify: callback to invoke before the object is freed
2388 * @data: extra data to pass to notify
2390 * Adds a weak reference callback to an object. Weak references are
2391 * used for notification when an object is finalized. They are called
2392 * "weak references" because they allow you to safely hold a pointer
2393 * to an object without calling g_object_ref() (g_object_ref() adds a
2394 * strong reference, that is, forces the object to stay alive).
2397 g_object_weak_ref (GObject *object,
2401 WeakRefStack *wstack;
2404 g_return_if_fail (G_IS_OBJECT (object));
2405 g_return_if_fail (notify != NULL);
2406 g_return_if_fail (object->ref_count >= 1);
2408 G_LOCK (weak_refs_mutex);
2409 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2412 i = wstack->n_weak_refs++;
2413 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2417 wstack = g_renew (WeakRefStack, NULL, 1);
2418 wstack->object = object;
2419 wstack->n_weak_refs = 1;
2422 wstack->weak_refs[i].notify = notify;
2423 wstack->weak_refs[i].data = data;
2424 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2425 G_UNLOCK (weak_refs_mutex);
2429 * g_object_weak_unref: (skip)
2430 * @object: #GObject to remove a weak reference from
2431 * @notify: callback to search for
2432 * @data: data to search for
2434 * Removes a weak reference callback to an object.
2437 g_object_weak_unref (GObject *object,
2441 WeakRefStack *wstack;
2442 gboolean found_one = FALSE;
2444 g_return_if_fail (G_IS_OBJECT (object));
2445 g_return_if_fail (notify != NULL);
2447 G_LOCK (weak_refs_mutex);
2448 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2453 for (i = 0; i < wstack->n_weak_refs; i++)
2454 if (wstack->weak_refs[i].notify == notify &&
2455 wstack->weak_refs[i].data == data)
2458 wstack->n_weak_refs -= 1;
2459 if (i != wstack->n_weak_refs)
2460 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2465 G_UNLOCK (weak_refs_mutex);
2467 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2471 * g_object_add_weak_pointer: (skip)
2472 * @object: The object that should be weak referenced.
2473 * @weak_pointer_location: (inout): The memory address of a pointer.
2475 * Adds a weak reference from weak_pointer to @object to indicate that
2476 * the pointer located at @weak_pointer_location is only valid during
2477 * the lifetime of @object. When the @object is finalized,
2478 * @weak_pointer will be set to %NULL.
2481 g_object_add_weak_pointer (GObject *object,
2482 gpointer *weak_pointer_location)
2484 g_return_if_fail (G_IS_OBJECT (object));
2485 g_return_if_fail (weak_pointer_location != NULL);
2487 g_object_weak_ref (object,
2488 (GWeakNotify) g_nullify_pointer,
2489 weak_pointer_location);
2493 * g_object_remove_weak_pointer: (skip)
2494 * @object: The object that is weak referenced.
2495 * @weak_pointer_location: (inout): The memory address of a pointer.
2497 * Removes a weak reference from @object that was previously added
2498 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2499 * to match the one used with g_object_add_weak_pointer().
2502 g_object_remove_weak_pointer (GObject *object,
2503 gpointer *weak_pointer_location)
2505 g_return_if_fail (G_IS_OBJECT (object));
2506 g_return_if_fail (weak_pointer_location != NULL);
2508 g_object_weak_unref (object,
2509 (GWeakNotify) g_nullify_pointer,
2510 weak_pointer_location);
2514 object_floating_flag_handler (GObject *object,
2520 case +1: /* force floating if possible */
2522 oldvalue = g_atomic_pointer_get (&object->qdata);
2523 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2524 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2525 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2526 case -1: /* sink if possible */
2528 oldvalue = g_atomic_pointer_get (&object->qdata);
2529 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2530 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2531 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2532 default: /* check floating */
2533 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2538 * g_object_is_floating:
2539 * @object: (type GObject.Object): a #GObject
2541 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2546 * Returns: %TRUE if @object has a floating reference
2549 g_object_is_floating (gpointer _object)
2551 GObject *object = _object;
2552 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2553 return floating_flag_handler (object, 0);
2557 * g_object_ref_sink:
2558 * @object: (type GObject.Object): a #GObject
2560 * Increase the reference count of @object, and possibly remove the
2561 * <link linkend="floating-ref">floating</link> reference, if @object
2562 * has a floating reference.
2564 * In other words, if the object is floating, then this call "assumes
2565 * ownership" of the floating reference, converting it to a normal
2566 * reference by clearing the floating flag while leaving the reference
2567 * count unchanged. If the object is not floating, then this call
2568 * adds a new normal reference increasing the reference count by one.
2572 * Returns: (type GObject.Object) (transfer none): @object
2575 g_object_ref_sink (gpointer _object)
2577 GObject *object = _object;
2578 gboolean was_floating;
2579 g_return_val_if_fail (G_IS_OBJECT (object), object);
2580 g_return_val_if_fail (object->ref_count >= 1, object);
2581 g_object_ref (object);
2582 was_floating = floating_flag_handler (object, -1);
2584 g_object_unref (object);
2589 * g_object_force_floating:
2590 * @object: a #GObject
2592 * This function is intended for #GObject implementations to re-enforce a
2593 * <link linkend="floating-ref">floating</link> object reference.
2594 * Doing this is seldom required: all
2595 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2596 * usually just needs to be sunken by calling g_object_ref_sink().
2601 g_object_force_floating (GObject *object)
2603 g_return_if_fail (G_IS_OBJECT (object));
2604 g_return_if_fail (object->ref_count >= 1);
2606 floating_flag_handler (object, +1);
2611 guint n_toggle_refs;
2613 GToggleNotify notify;
2615 } toggle_refs[1]; /* flexible array */
2619 toggle_refs_notify (GObject *object,
2620 gboolean is_last_ref)
2622 ToggleRefStack tstack, *tstackptr;
2624 G_LOCK (toggle_refs_mutex);
2625 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2626 tstack = *tstackptr;
2627 G_UNLOCK (toggle_refs_mutex);
2629 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2630 * will only be notified when there is exactly one of them.
2632 g_assert (tstack.n_toggle_refs == 1);
2633 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2637 * g_object_add_toggle_ref: (skip)
2638 * @object: a #GObject
2639 * @notify: a function to call when this reference is the
2640 * last reference to the object, or is no longer
2641 * the last reference.
2642 * @data: data to pass to @notify
2644 * Increases the reference count of the object by one and sets a
2645 * callback to be called when all other references to the object are
2646 * dropped, or when this is already the last reference to the object
2647 * and another reference is established.
2649 * This functionality is intended for binding @object to a proxy
2650 * object managed by another memory manager. This is done with two
2651 * paired references: the strong reference added by
2652 * g_object_add_toggle_ref() and a reverse reference to the proxy
2653 * object which is either a strong reference or weak reference.
2655 * The setup is that when there are no other references to @object,
2656 * only a weak reference is held in the reverse direction from @object
2657 * to the proxy object, but when there are other references held to
2658 * @object, a strong reference is held. The @notify callback is called
2659 * when the reference from @object to the proxy object should be
2660 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2661 * true) or weak to strong (@is_last_ref false).
2663 * Since a (normal) reference must be held to the object before
2664 * calling g_object_add_toggle_ref(), the initial state of the reverse
2665 * link is always strong.
2667 * Multiple toggle references may be added to the same gobject,
2668 * however if there are multiple toggle references to an object, none
2669 * of them will ever be notified until all but one are removed. For
2670 * this reason, you should only ever use a toggle reference if there
2671 * is important state in the proxy object.
2676 g_object_add_toggle_ref (GObject *object,
2677 GToggleNotify notify,
2680 ToggleRefStack *tstack;
2683 g_return_if_fail (G_IS_OBJECT (object));
2684 g_return_if_fail (notify != NULL);
2685 g_return_if_fail (object->ref_count >= 1);
2687 g_object_ref (object);
2689 G_LOCK (toggle_refs_mutex);
2690 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2693 i = tstack->n_toggle_refs++;
2694 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2695 * in tstate->toggle_refs */
2696 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2700 tstack = g_renew (ToggleRefStack, NULL, 1);
2701 tstack->object = object;
2702 tstack->n_toggle_refs = 1;
2706 /* Set a flag for fast lookup after adding the first toggle reference */
2707 if (tstack->n_toggle_refs == 1)
2708 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2710 tstack->toggle_refs[i].notify = notify;
2711 tstack->toggle_refs[i].data = data;
2712 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2713 (GDestroyNotify)g_free);
2714 G_UNLOCK (toggle_refs_mutex);
2718 * g_object_remove_toggle_ref: (skip)
2719 * @object: a #GObject
2720 * @notify: a function to call when this reference is the
2721 * last reference to the object, or is no longer
2722 * the last reference.
2723 * @data: data to pass to @notify
2725 * Removes a reference added with g_object_add_toggle_ref(). The
2726 * reference count of the object is decreased by one.
2731 g_object_remove_toggle_ref (GObject *object,
2732 GToggleNotify notify,
2735 ToggleRefStack *tstack;
2736 gboolean found_one = FALSE;
2738 g_return_if_fail (G_IS_OBJECT (object));
2739 g_return_if_fail (notify != NULL);
2741 G_LOCK (toggle_refs_mutex);
2742 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2747 for (i = 0; i < tstack->n_toggle_refs; i++)
2748 if (tstack->toggle_refs[i].notify == notify &&
2749 tstack->toggle_refs[i].data == data)
2752 tstack->n_toggle_refs -= 1;
2753 if (i != tstack->n_toggle_refs)
2754 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2756 if (tstack->n_toggle_refs == 0)
2757 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2762 G_UNLOCK (toggle_refs_mutex);
2765 g_object_unref (object);
2767 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2772 * @object: (type GObject.Object): a #GObject
2774 * Increases the reference count of @object.
2776 * Returns: (type GObject.Object) (transfer none): the same @object
2779 g_object_ref (gpointer _object)
2781 GObject *object = _object;
2784 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2785 g_return_val_if_fail (object->ref_count > 0, NULL);
2787 #ifdef G_ENABLE_DEBUG
2788 if (g_trap_object_ref == object)
2790 #endif /* G_ENABLE_DEBUG */
2793 old_val = g_atomic_int_add (&object->ref_count, 1);
2795 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2796 toggle_refs_notify (object, FALSE);
2798 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2805 * @object: (type GObject.Object): a #GObject
2807 * Decreases the reference count of @object. When its reference count
2808 * drops to 0, the object is finalized (i.e. its memory is freed).
2811 g_object_unref (gpointer _object)
2813 GObject *object = _object;
2816 g_return_if_fail (G_IS_OBJECT (object));
2817 g_return_if_fail (object->ref_count > 0);
2819 #ifdef G_ENABLE_DEBUG
2820 if (g_trap_object_ref == object)
2822 #endif /* G_ENABLE_DEBUG */
2824 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2825 retry_atomic_decrement1:
2826 old_ref = g_atomic_int_get (&object->ref_count);
2829 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2830 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2832 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2833 goto retry_atomic_decrement1;
2835 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2837 /* if we went from 2->1 we need to notify toggle refs if any */
2838 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2839 toggle_refs_notify (object, TRUE);
2843 /* we are about tp remove the last reference */
2844 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2845 G_OBJECT_GET_CLASS (object)->dispose (object);
2846 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2848 /* may have been re-referenced meanwhile */
2849 retry_atomic_decrement2:
2850 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2853 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2854 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2856 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2857 goto retry_atomic_decrement2;
2859 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2861 /* if we went from 2->1 we need to notify toggle refs if any */
2862 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2863 toggle_refs_notify (object, TRUE);
2868 /* we are still in the process of taking away the last ref */
2869 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2870 g_signal_handlers_destroy (object);
2871 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2873 /* decrement the last reference */
2874 old_ref = g_atomic_int_add (&object->ref_count, -1);
2876 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2878 /* may have been re-referenced meanwhile */
2879 if (G_LIKELY (old_ref == 1))
2881 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2882 G_OBJECT_GET_CLASS (object)->finalize (object);
2884 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2886 #ifdef G_ENABLE_DEBUG
2889 /* catch objects not chaining finalize handlers */
2890 G_LOCK (debug_objects);
2891 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2892 G_UNLOCK (debug_objects);
2894 #endif /* G_ENABLE_DEBUG */
2895 g_type_free_instance ((GTypeInstance*) object);
2901 * g_clear_object: (skip)
2902 * @object_ptr: a pointer to a #GObject reference
2904 * Clears a reference to a #GObject.
2906 * @object_ptr must not be %NULL.
2908 * If the reference is %NULL then this function does nothing.
2909 * Otherwise, the reference count of the object is decreased and the
2910 * pointer is set to %NULL.
2912 * This function is threadsafe and modifies the pointer atomically,
2913 * using memory barriers where needed.
2915 * A macro is also included that allows this function to be used without
2920 #undef g_clear_object
2922 g_clear_object (volatile GObject **object_ptr)
2924 gpointer *ptr = (gpointer) object_ptr;
2927 /* This is a little frustrating.
2928 * Would be nice to have an atomic exchange (with no compare).
2931 old = g_atomic_pointer_get (ptr);
2932 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2935 g_object_unref (old);
2939 * g_object_get_qdata:
2940 * @object: The GObject to get a stored user data pointer from
2941 * @quark: A #GQuark, naming the user data pointer
2943 * This function gets back user data pointers stored via
2944 * g_object_set_qdata().
2946 * Returns: (transfer none): The user data pointer set, or %NULL
2949 g_object_get_qdata (GObject *object,
2952 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2954 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2958 * g_object_set_qdata: (skip)
2959 * @object: The GObject to set store a user data pointer
2960 * @quark: A #GQuark, naming the user data pointer
2961 * @data: An opaque user data pointer
2963 * This sets an opaque, named pointer on an object.
2964 * The name is specified through a #GQuark (retrived e.g. via
2965 * g_quark_from_static_string()), and the pointer
2966 * can be gotten back from the @object with g_object_get_qdata()
2967 * until the @object is finalized.
2968 * Setting a previously set user data pointer, overrides (frees)
2969 * the old pointer set, using #NULL as pointer essentially
2970 * removes the data stored.
2973 g_object_set_qdata (GObject *object,
2977 g_return_if_fail (G_IS_OBJECT (object));
2978 g_return_if_fail (quark > 0);
2980 g_datalist_id_set_data (&object->qdata, quark, data);
2984 * g_object_set_qdata_full: (skip)
2985 * @object: The GObject to set store a user data pointer
2986 * @quark: A #GQuark, naming the user data pointer
2987 * @data: An opaque user data pointer
2988 * @destroy: Function to invoke with @data as argument, when @data
2991 * This function works like g_object_set_qdata(), but in addition,
2992 * a void (*destroy) (gpointer) function may be specified which is
2993 * called with @data as argument when the @object is finalized, or
2994 * the data is being overwritten by a call to g_object_set_qdata()
2995 * with the same @quark.
2998 g_object_set_qdata_full (GObject *object,
3001 GDestroyNotify destroy)
3003 g_return_if_fail (G_IS_OBJECT (object));
3004 g_return_if_fail (quark > 0);
3006 g_datalist_id_set_data_full (&object->qdata, quark, data,
3007 data ? destroy : (GDestroyNotify) NULL);
3011 * g_object_steal_qdata:
3012 * @object: The GObject to get a stored user data pointer from
3013 * @quark: A #GQuark, naming the user data pointer
3015 * This function gets back user data pointers stored via
3016 * g_object_set_qdata() and removes the @data from object
3017 * without invoking its destroy() function (if any was
3019 * Usually, calling this function is only required to update
3020 * user data pointers with a destroy notifier, for example:
3023 * object_add_to_user_list (GObject *object,
3024 * const gchar *new_string)
3026 * // the quark, naming the object data
3027 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3028 * // retrive the old string list
3029 * GList *list = g_object_steal_qdata (object, quark_string_list);
3031 * // prepend new string
3032 * list = g_list_prepend (list, g_strdup (new_string));
3033 * // this changed 'list', so we need to set it again
3034 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3037 * free_string_list (gpointer data)
3039 * GList *node, *list = data;
3041 * for (node = list; node; node = node->next)
3042 * g_free (node->data);
3043 * g_list_free (list);
3046 * Using g_object_get_qdata() in the above example, instead of
3047 * g_object_steal_qdata() would have left the destroy function set,
3048 * and thus the partial string list would have been freed upon
3049 * g_object_set_qdata_full().
3051 * Returns: (transfer full): The user data pointer set, or %NULL
3054 g_object_steal_qdata (GObject *object,
3057 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3058 g_return_val_if_fail (quark > 0, NULL);
3060 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3064 * g_object_get_data:
3065 * @object: #GObject containing the associations
3066 * @key: name of the key for that association
3068 * Gets a named field from the objects table of associations (see g_object_set_data()).
3070 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3073 g_object_get_data (GObject *object,
3076 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3077 g_return_val_if_fail (key != NULL, NULL);
3079 return g_datalist_get_data (&object->qdata, key);
3083 * g_object_set_data:
3084 * @object: #GObject containing the associations.
3085 * @key: name of the key
3086 * @data: data to associate with that key
3088 * Each object carries around a table of associations from
3089 * strings to pointers. This function lets you set an association.
3091 * If the object already had an association with that name,
3092 * the old association will be destroyed.
3095 g_object_set_data (GObject *object,
3099 g_return_if_fail (G_IS_OBJECT (object));
3100 g_return_if_fail (key != NULL);
3102 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3106 * g_object_set_data_full: (skip)
3107 * @object: #GObject containing the associations
3108 * @key: name of the key
3109 * @data: data to associate with that key
3110 * @destroy: function to call when the association is destroyed
3112 * Like g_object_set_data() except it adds notification
3113 * for when the association is destroyed, either by setting it
3114 * to a different value or when the object is destroyed.
3116 * Note that the @destroy callback is not called if @data is %NULL.
3119 g_object_set_data_full (GObject *object,
3122 GDestroyNotify destroy)
3124 g_return_if_fail (G_IS_OBJECT (object));
3125 g_return_if_fail (key != NULL);
3127 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3128 data ? destroy : (GDestroyNotify) NULL);
3132 * g_object_steal_data:
3133 * @object: #GObject containing the associations
3134 * @key: name of the key
3136 * Remove a specified datum from the object's data associations,
3137 * without invoking the association's destroy handler.
3139 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3142 g_object_steal_data (GObject *object,
3147 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3148 g_return_val_if_fail (key != NULL, NULL);
3150 quark = g_quark_try_string (key);
3152 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3156 g_value_object_init (GValue *value)
3158 value->data[0].v_pointer = NULL;
3162 g_value_object_free_value (GValue *value)
3164 if (value->data[0].v_pointer)
3165 g_object_unref (value->data[0].v_pointer);
3169 g_value_object_copy_value (const GValue *src_value,
3172 if (src_value->data[0].v_pointer)
3173 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3175 dest_value->data[0].v_pointer = NULL;
3179 g_value_object_transform_value (const GValue *src_value,
3182 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)))
3183 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3185 dest_value->data[0].v_pointer = NULL;
3189 g_value_object_peek_pointer (const GValue *value)
3191 return value->data[0].v_pointer;
3195 g_value_object_collect_value (GValue *value,
3196 guint n_collect_values,
3197 GTypeCValue *collect_values,
3198 guint collect_flags)
3200 if (collect_values[0].v_pointer)
3202 GObject *object = collect_values[0].v_pointer;
3204 if (object->g_type_instance.g_class == NULL)
3205 return g_strconcat ("invalid unclassed object pointer for value type `",
3206 G_VALUE_TYPE_NAME (value),
3209 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3210 return g_strconcat ("invalid object type `",
3211 G_OBJECT_TYPE_NAME (object),
3212 "' for value type `",
3213 G_VALUE_TYPE_NAME (value),
3216 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3217 value->data[0].v_pointer = g_object_ref (object);
3220 value->data[0].v_pointer = NULL;
3226 g_value_object_lcopy_value (const GValue *value,
3227 guint n_collect_values,
3228 GTypeCValue *collect_values,
3229 guint collect_flags)
3231 GObject **object_p = collect_values[0].v_pointer;
3234 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3236 if (!value->data[0].v_pointer)
3238 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3239 *object_p = value->data[0].v_pointer;
3241 *object_p = g_object_ref (value->data[0].v_pointer);
3247 * g_value_set_object:
3248 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3249 * @v_object: (type GObject.Object) (allow-none): object value to be set
3251 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3253 * g_value_set_object() increases the reference count of @v_object
3254 * (the #GValue holds a reference to @v_object). If you do not wish
3255 * to increase the reference count of the object (i.e. you wish to
3256 * pass your current reference to the #GValue because you no longer
3257 * need it), use g_value_take_object() instead.
3259 * It is important that your #GValue holds a reference to @v_object (either its
3260 * own, or one it has taken) to ensure that the object won't be destroyed while
3261 * the #GValue still exists).
3264 g_value_set_object (GValue *value,
3269 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3271 old = value->data[0].v_pointer;
3275 g_return_if_fail (G_IS_OBJECT (v_object));
3276 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3278 value->data[0].v_pointer = v_object;
3279 g_object_ref (value->data[0].v_pointer);
3282 value->data[0].v_pointer = NULL;
3285 g_object_unref (old);
3289 * g_value_set_object_take_ownership: (skip)
3290 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3291 * @v_object: (allow-none): object value to be set
3293 * This is an internal function introduced mainly for C marshallers.
3295 * Deprecated: 2.4: Use g_value_take_object() instead.
3298 g_value_set_object_take_ownership (GValue *value,
3301 g_value_take_object (value, v_object);
3305 * g_value_take_object: (skip)
3306 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3307 * @v_object: (allow-none): object value to be set
3309 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3310 * and takes over the ownership of the callers reference to @v_object;
3311 * the caller doesn't have to unref it any more (i.e. the reference
3312 * count of the object is not increased).
3314 * If you want the #GValue to hold its own reference to @v_object, use
3315 * g_value_set_object() instead.
3320 g_value_take_object (GValue *value,
3323 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3325 if (value->data[0].v_pointer)
3327 g_object_unref (value->data[0].v_pointer);
3328 value->data[0].v_pointer = NULL;
3333 g_return_if_fail (G_IS_OBJECT (v_object));
3334 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3336 value->data[0].v_pointer = v_object; /* we take over the reference count */
3341 * g_value_get_object:
3342 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3344 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3346 * Returns: (type GObject.Object) (transfer none): object contents of @value
3349 g_value_get_object (const GValue *value)
3351 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3353 return value->data[0].v_pointer;
3357 * g_value_dup_object:
3358 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3360 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3361 * its reference count. If the contents of the #GValue are %NULL, then
3362 * %NULL will be returned.
3364 * Returns: (type GObject.Object) (transfer full): object content of @value,
3365 * should be unreferenced when no longer needed.
3368 g_value_dup_object (const GValue *value)
3370 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3372 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3376 * g_signal_connect_object: (skip)
3377 * @instance: the instance to connect to.
3378 * @detailed_signal: a string of the form "signal-name::detail".
3379 * @c_handler: the #GCallback to connect.
3380 * @gobject: the object to pass as data to @c_handler.
3381 * @connect_flags: a combination of #GConnectFlags.
3383 * This is similar to g_signal_connect_data(), but uses a closure which
3384 * ensures that the @gobject stays alive during the call to @c_handler
3385 * by temporarily adding a reference count to @gobject.
3387 * Note that there is a bug in GObject that makes this function
3388 * much less useful than it might seem otherwise. Once @gobject is
3389 * disposed, the callback will no longer be called, but, the signal
3390 * handler is <emphasis>not</emphasis> currently disconnected. If the
3391 * @instance is itself being freed at the same time than this doesn't
3392 * matter, since the signal will automatically be removed, but
3393 * if @instance persists, then the signal handler will leak. You
3394 * should not remove the signal yourself because in a future versions of
3395 * GObject, the handler <emphasis>will</emphasis> automatically
3398 * It's possible to work around this problem in a way that will
3399 * continue to work with future versions of GObject by checking
3400 * that the signal handler is still connected before disconnected it:
3401 * <informalexample><programlisting>
3402 * if (g_signal_handler_is_connected (instance, id))
3403 * g_signal_handler_disconnect (instance, id);
3404 * </programlisting></informalexample>
3406 * Returns: the handler id.
3409 g_signal_connect_object (gpointer instance,
3410 const gchar *detailed_signal,
3411 GCallback c_handler,
3413 GConnectFlags connect_flags)
3415 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3416 g_return_val_if_fail (detailed_signal != NULL, 0);
3417 g_return_val_if_fail (c_handler != NULL, 0);
3423 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3425 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3427 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3430 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3436 GClosure *closures[1]; /* flexible array */
3438 /* don't change this structure without supplying an accessor for
3439 * watched closures, e.g.:
3440 * GSList* g_object_list_watched_closures (GObject *object)
3443 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3444 * carray = g_object_get_data (object, "GObject-closure-array");
3447 * GSList *slist = NULL;
3449 * for (i = 0; i < carray->n_closures; i++)
3450 * slist = g_slist_prepend (slist, carray->closures[i]);
3458 object_remove_closure (gpointer data,
3461 GObject *object = data;
3465 G_LOCK (closure_array_mutex);
3466 carray = g_object_get_qdata (object, quark_closure_array);
3467 for (i = 0; i < carray->n_closures; i++)
3468 if (carray->closures[i] == closure)
3470 carray->n_closures--;
3471 if (i < carray->n_closures)
3472 carray->closures[i] = carray->closures[carray->n_closures];
3473 G_UNLOCK (closure_array_mutex);
3476 G_UNLOCK (closure_array_mutex);
3477 g_assert_not_reached ();
3481 destroy_closure_array (gpointer data)
3483 CArray *carray = data;
3484 GObject *object = carray->object;
3485 guint i, n = carray->n_closures;
3487 for (i = 0; i < n; i++)
3489 GClosure *closure = carray->closures[i];
3491 /* removing object_remove_closure() upfront is probably faster than
3492 * letting it fiddle with quark_closure_array which is empty anyways
3494 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3495 g_closure_invalidate (closure);
3501 * g_object_watch_closure:
3502 * @object: GObject restricting lifetime of @closure
3503 * @closure: GClosure to watch
3505 * This function essentially limits the life time of the @closure to
3506 * the life time of the object. That is, when the object is finalized,
3507 * the @closure is invalidated by calling g_closure_invalidate() on
3508 * it, in order to prevent invocations of the closure with a finalized
3509 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3510 * added as marshal guards to the @closure, to ensure that an extra
3511 * reference count is held on @object during invocation of the
3512 * @closure. Usually, this function will be called on closures that
3513 * use this @object as closure data.
3516 g_object_watch_closure (GObject *object,
3522 g_return_if_fail (G_IS_OBJECT (object));
3523 g_return_if_fail (closure != NULL);
3524 g_return_if_fail (closure->is_invalid == FALSE);
3525 g_return_if_fail (closure->in_marshal == FALSE);
3526 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3528 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3529 g_closure_add_marshal_guards (closure,
3530 object, (GClosureNotify) g_object_ref,
3531 object, (GClosureNotify) g_object_unref);
3532 G_LOCK (closure_array_mutex);
3533 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3536 carray = g_renew (CArray, NULL, 1);
3537 carray->object = object;
3538 carray->n_closures = 1;
3543 i = carray->n_closures++;
3544 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3546 carray->closures[i] = closure;
3547 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3548 G_UNLOCK (closure_array_mutex);
3552 * g_closure_new_object:
3553 * @sizeof_closure: the size of the structure to allocate, must be at least
3554 * <literal>sizeof (GClosure)</literal>
3555 * @object: a #GObject pointer to store in the @data field of the newly
3556 * allocated #GClosure
3558 * A variant of g_closure_new_simple() which stores @object in the
3559 * @data field of the closure and calls g_object_watch_closure() on
3560 * @object and the created closure. This function is mainly useful
3561 * when implementing new types of closures.
3563 * Returns: (transfer full): a newly allocated #GClosure
3566 g_closure_new_object (guint sizeof_closure,
3571 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3572 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3574 closure = g_closure_new_simple (sizeof_closure, object);
3575 g_object_watch_closure (object, closure);
3581 * g_cclosure_new_object: (skip)
3582 * @callback_func: the function to invoke
3583 * @object: a #GObject pointer to pass to @callback_func
3585 * A variant of g_cclosure_new() which uses @object as @user_data and
3586 * calls g_object_watch_closure() on @object and the created
3587 * closure. This function is useful when you have a callback closely
3588 * associated with a #GObject, and want the callback to no longer run
3589 * after the object is is freed.
3591 * Returns: a new #GCClosure
3594 g_cclosure_new_object (GCallback callback_func,
3599 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3600 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3601 g_return_val_if_fail (callback_func != NULL, NULL);
3603 closure = g_cclosure_new (callback_func, object, NULL);
3604 g_object_watch_closure (object, closure);
3610 * g_cclosure_new_object_swap: (skip)
3611 * @callback_func: the function to invoke
3612 * @object: a #GObject pointer to pass to @callback_func
3614 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3615 * and calls g_object_watch_closure() on @object and the created
3616 * closure. This function is useful when you have a callback closely
3617 * associated with a #GObject, and want the callback to no longer run
3618 * after the object is is freed.
3620 * Returns: a new #GCClosure
3623 g_cclosure_new_object_swap (GCallback callback_func,
3628 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3629 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3630 g_return_val_if_fail (callback_func != NULL, NULL);
3632 closure = g_cclosure_new_swap (callback_func, object, NULL);
3633 g_object_watch_closure (object, closure);
3639 g_object_compat_control (gsize what,
3645 case 1: /* floating base type */
3646 return G_TYPE_INITIALLY_UNOWNED;
3647 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3648 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3650 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3652 *pp = floating_flag_handler;
3659 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3662 g_initially_unowned_init (GInitiallyUnowned *object)
3664 g_object_force_floating (object);
3668 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)