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 /* --- functions --- */
205 #ifdef G_ENABLE_DEBUG
206 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
207 G_LOCK_DEFINE_STATIC (debug_objects);
208 static volatile GObject *g_trap_object_ref = NULL;
209 static guint debug_objects_count = 0;
210 static GHashTable *debug_objects_ht = NULL;
212 G_LOCK_DEFINE_STATIC(notify_lock);
214 /* --- functions --- */
216 g_object_notify_queue_free (gpointer data)
218 GObjectNotifyQueue *nqueue = data;
220 g_slist_free (nqueue->pspecs);
221 g_slice_free (GObjectNotifyQueue, nqueue);
224 static inline GObjectNotifyQueue*
225 g_object_notify_queue_freeze (GObject *object)
227 GObjectNotifyQueue *nqueue;
230 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
233 nqueue = g_slice_new0 (GObjectNotifyQueue);
234 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
235 nqueue, g_object_notify_queue_free);
238 if (nqueue->freeze_count >= 65535)
239 g_critical("Free queue for %s (%p) is larger than 65535,"
240 " called g_object_freeze_notify() too often."
241 " Forgot to call g_object_thaw_notify() or infinite loop",
242 G_OBJECT_TYPE_NAME (object), object);
244 nqueue->freeze_count++;
245 G_UNLOCK(notify_lock);
251 g_object_notify_queue_thaw (GObject *object,
252 GObjectNotifyQueue *nqueue)
254 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
258 g_return_if_fail (nqueue->freeze_count > 0);
259 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
263 /* Just make sure we never get into some nasty race condition */
264 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
265 G_UNLOCK(notify_lock);
266 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
267 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
271 nqueue->freeze_count--;
272 if (nqueue->freeze_count) {
273 G_UNLOCK(notify_lock);
277 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
279 for (slist = nqueue->pspecs; slist; slist = slist->next)
281 pspecs[n_pspecs++] = slist->data;
283 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
285 G_UNLOCK(notify_lock);
288 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
293 g_object_notify_queue_add (GObject *object,
294 GObjectNotifyQueue *nqueue,
299 g_return_if_fail (nqueue->n_pspecs < 65535);
301 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
303 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
307 G_UNLOCK(notify_lock);
311 debug_objects_foreach (gpointer key,
315 GObject *object = value;
317 g_message ("[%p] stale %s\tref_count=%u",
319 G_OBJECT_TYPE_NAME (object),
324 debug_objects_atexit (void)
328 G_LOCK (debug_objects);
329 g_message ("stale GObjects: %u", debug_objects_count);
330 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
331 G_UNLOCK (debug_objects);
334 #endif /* G_ENABLE_DEBUG */
337 _g_object_type_init (void)
339 static gboolean initialized = FALSE;
340 static const GTypeFundamentalInfo finfo = {
341 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
343 static GTypeInfo info = {
344 sizeof (GObjectClass),
345 (GBaseInitFunc) g_object_base_class_init,
346 (GBaseFinalizeFunc) g_object_base_class_finalize,
347 (GClassInitFunc) g_object_do_class_init,
348 NULL /* class_destroy */,
349 NULL /* class_data */,
352 (GInstanceInitFunc) g_object_init,
353 NULL, /* value_table */
355 static const GTypeValueTable value_table = {
356 g_value_object_init, /* value_init */
357 g_value_object_free_value, /* value_free */
358 g_value_object_copy_value, /* value_copy */
359 g_value_object_peek_pointer, /* value_peek_pointer */
360 "p", /* collect_format */
361 g_value_object_collect_value, /* collect_value */
362 "p", /* lcopy_format */
363 g_value_object_lcopy_value, /* lcopy_value */
367 g_return_if_fail (initialized == FALSE);
372 info.value_table = &value_table;
373 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
374 g_assert (type == G_TYPE_OBJECT);
375 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
377 #ifdef G_ENABLE_DEBUG
380 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
381 g_atexit (debug_objects_atexit);
383 #endif /* G_ENABLE_DEBUG */
387 g_object_base_class_init (GObjectClass *class)
389 GObjectClass *pclass = g_type_class_peek_parent (class);
391 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
392 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
395 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
397 /* reset instance specific fields and methods that don't get inherited */
398 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
399 class->get_property = NULL;
400 class->set_property = NULL;
404 g_object_base_class_finalize (GObjectClass *class)
408 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
410 g_slist_free (class->construct_properties);
411 class->construct_properties = NULL;
412 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
413 for (node = list; node; node = node->next)
415 GParamSpec *pspec = node->data;
417 g_param_spec_pool_remove (pspec_pool, pspec);
418 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
419 g_param_spec_unref (pspec);
425 g_object_do_class_init (GObjectClass *class)
427 /* read the comment about typedef struct CArray; on why not to change this quark */
428 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
430 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
431 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
432 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
433 pspec_pool = g_param_spec_pool_new (TRUE);
435 class->constructor = g_object_constructor;
436 class->constructed = g_object_constructed;
437 class->set_property = g_object_do_set_property;
438 class->get_property = g_object_do_get_property;
439 class->dispose = g_object_real_dispose;
440 class->finalize = g_object_finalize;
441 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
442 class->notify = NULL;
446 * @gobject: the object which received the signal.
447 * @pspec: the #GParamSpec of the property which changed.
449 * The notify signal is emitted on an object when one of its
450 * properties has been changed. Note that getting this signal
451 * doesn't guarantee that the value of the property has actually
452 * changed, it may also be emitted when the setter for the property
453 * is called to reinstate the previous value.
455 * This signal is typically used to obtain change notification for a
456 * single property, by specifying the property name as a detail in the
457 * g_signal_connect() call, like this:
459 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
460 * G_CALLBACK (gtk_text_view_target_list_notify),
463 * It is important to note that you must use
464 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
465 * detail strings for the notify signal.
467 gobject_signals[NOTIFY] =
468 g_signal_new (g_intern_static_string ("notify"),
469 G_TYPE_FROM_CLASS (class),
470 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
471 G_STRUCT_OFFSET (GObjectClass, notify),
473 g_cclosure_marshal_VOID__PARAM,
477 /* Install a check function that we'll use to verify that classes that
478 * implement an interface implement all properties for that interface
480 g_type_add_interface_check (NULL, object_interface_check_properties);
484 install_property_internal (GType g_type,
488 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
490 g_warning ("When installing property: type `%s' already has a property named `%s'",
491 g_type_name (g_type),
496 g_param_spec_ref_sink (pspec);
497 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
498 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
502 * g_object_class_install_property:
503 * @oclass: a #GObjectClass
504 * @property_id: the id for the new property
505 * @pspec: the #GParamSpec for the new property
507 * Installs a new property. This is usually done in the class initializer.
509 * Note that it is possible to redefine a property in a derived class,
510 * by installing a property with the same name. This can be useful at times,
511 * e.g. to change the range of allowed values or the default value.
514 g_object_class_install_property (GObjectClass *class,
518 g_return_if_fail (G_IS_OBJECT_CLASS (class));
519 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
521 if (CLASS_HAS_DERIVED_CLASS (class))
522 g_error ("Attempt to add property %s::%s to class after it was derived",
523 G_OBJECT_CLASS_NAME (class), pspec->name);
525 class->flags |= CLASS_HAS_PROPS_FLAG;
527 if (pspec->flags & G_PARAM_WRITABLE)
528 g_return_if_fail (class->set_property != NULL);
529 if (pspec->flags & G_PARAM_READABLE)
530 g_return_if_fail (class->get_property != NULL);
531 g_return_if_fail (property_id > 0);
532 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
533 if (pspec->flags & G_PARAM_CONSTRUCT)
534 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
535 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
536 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
538 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
540 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
541 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
543 /* for property overrides of construct properties, we have to get rid
544 * of the overidden inherited construct property
546 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
547 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
548 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
552 * g_object_class_install_properties:
553 * @oclass: a #GObjectClass
554 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
555 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
556 * defining the new properties
558 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
559 * usually done in the class initializer.
561 * The property id of each property is the index of each #GParamSpec in
564 * The property id of 0 is treated specially by #GObject and it should not
565 * be used to store a #GParamSpec.
567 * This function should be used if you plan to use a static array of
568 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
569 * class initialization:
573 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
576 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
579 * my_object_class_init (MyObjectClass *klass)
581 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
583 * obj_properties[PROP_FOO] =
584 * g_param_spec_int ("foo", "Foo", "Foo",
587 * G_PARAM_READWRITE);
589 * obj_properties[PROP_BAR] =
590 * g_param_spec_string ("bar", "Bar", "Bar",
592 * G_PARAM_READWRITE);
594 * gobject_class->set_property = my_object_set_property;
595 * gobject_class->get_property = my_object_get_property;
596 * g_object_class_install_properties (gobject_class,
602 * allows calling g_object_notify_by_pspec() to notify of property changes:
606 * my_object_set_foo (MyObject *self, gint foo)
608 * if (self->foo != foo)
611 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
619 g_object_class_install_properties (GObjectClass *oclass,
623 GType oclass_type, parent_type;
626 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
627 g_return_if_fail (n_pspecs > 1);
628 g_return_if_fail (pspecs[0] == NULL);
630 if (CLASS_HAS_DERIVED_CLASS (oclass))
631 g_error ("Attempt to add properties to %s after it was derived",
632 G_OBJECT_CLASS_NAME (oclass));
634 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
635 parent_type = g_type_parent (oclass_type);
637 /* we skip the first element of the array as it would have a 0 prop_id */
638 for (i = 1; i < n_pspecs; i++)
640 GParamSpec *pspec = pspecs[i];
642 g_return_if_fail (pspec != NULL);
644 if (pspec->flags & G_PARAM_WRITABLE)
645 g_return_if_fail (oclass->set_property != NULL);
646 if (pspec->flags & G_PARAM_READABLE)
647 g_return_if_fail (oclass->get_property != NULL);
648 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
649 if (pspec->flags & G_PARAM_CONSTRUCT)
650 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
651 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
652 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
654 oclass->flags |= CLASS_HAS_PROPS_FLAG;
655 install_property_internal (oclass_type, i, pspec);
657 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
658 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
660 /* for property overrides of construct properties, we have to get rid
661 * of the overidden inherited construct property
663 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
664 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
665 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
670 * g_object_interface_install_property:
671 * @g_iface: any interface vtable for the interface, or the default
672 * vtable for the interface.
673 * @pspec: the #GParamSpec for the new property
675 * Add a property to an interface; this is only useful for interfaces
676 * that are added to GObject-derived types. Adding a property to an
677 * interface forces all objects classes with that interface to have a
678 * compatible property. The compatible property could be a newly
679 * created #GParamSpec, but normally
680 * g_object_class_override_property() will be used so that the object
681 * class only needs to provide an implementation and inherits the
682 * property description, default value, bounds, and so forth from the
683 * interface property.
685 * This function is meant to be called from the interface's default
686 * vtable initialization function (the @class_init member of
687 * #GTypeInfo.) It must not be called after after @class_init has
688 * been called for any object types implementing this interface.
693 g_object_interface_install_property (gpointer g_iface,
696 GTypeInterface *iface_class = g_iface;
698 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
699 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
700 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
701 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
703 install_property_internal (iface_class->g_type, 0, pspec);
707 * g_object_class_find_property:
708 * @oclass: a #GObjectClass
709 * @property_name: the name of the property to look up
711 * Looks up the #GParamSpec for a property of a class.
713 * Returns: (transfer none): the #GParamSpec for the property, or
714 * %NULL if the class doesn't have a property of that name
717 g_object_class_find_property (GObjectClass *class,
718 const gchar *property_name)
721 GParamSpec *redirect;
723 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
724 g_return_val_if_fail (property_name != NULL, NULL);
726 pspec = g_param_spec_pool_lookup (pspec_pool,
728 G_OBJECT_CLASS_TYPE (class),
732 redirect = g_param_spec_get_redirect_target (pspec);
743 * g_object_interface_find_property:
744 * @g_iface: any interface vtable for the interface, or the default
745 * vtable for the interface
746 * @property_name: name of a property to lookup.
748 * Find the #GParamSpec with the given name for an
749 * interface. Generally, the interface vtable passed in as @g_iface
750 * will be the default vtable from g_type_default_interface_ref(), or,
751 * if you know the interface has already been loaded,
752 * g_type_default_interface_peek().
756 * Returns: (transfer none): the #GParamSpec for the property of the
757 * interface with the name @property_name, or %NULL if no
758 * such property exists.
761 g_object_interface_find_property (gpointer g_iface,
762 const gchar *property_name)
764 GTypeInterface *iface_class = g_iface;
766 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
767 g_return_val_if_fail (property_name != NULL, NULL);
769 return g_param_spec_pool_lookup (pspec_pool,
776 * g_object_class_override_property:
777 * @oclass: a #GObjectClass
778 * @property_id: the new property ID
779 * @name: the name of a property registered in a parent class or
780 * in an interface of this class.
782 * Registers @property_id as referring to a property with the
783 * name @name in a parent class or in an interface implemented
784 * by @oclass. This allows this class to <firstterm>override</firstterm>
785 * a property implementation in a parent class or to provide
786 * the implementation of a property from an interface.
789 * Internally, overriding is implemented by creating a property of type
790 * #GParamSpecOverride; generally operations that query the properties of
791 * the object class, such as g_object_class_find_property() or
792 * g_object_class_list_properties() will return the overridden
793 * property. However, in one case, the @construct_properties argument of
794 * the @constructor virtual function, the #GParamSpecOverride is passed
795 * instead, so that the @param_id field of the #GParamSpec will be
796 * correct. For virtually all uses, this makes no difference. If you
797 * need to get the overridden property, you can call
798 * g_param_spec_get_redirect_target().
804 g_object_class_override_property (GObjectClass *oclass,
808 GParamSpec *overridden = NULL;
812 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
813 g_return_if_fail (property_id > 0);
814 g_return_if_fail (name != NULL);
816 /* Find the overridden property; first check parent types
818 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
819 if (parent_type != G_TYPE_NONE)
820 overridden = g_param_spec_pool_lookup (pspec_pool,
829 /* Now check interfaces
831 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
832 while (n_ifaces-- && !overridden)
834 overridden = g_param_spec_pool_lookup (pspec_pool,
845 g_warning ("%s: Can't find property to override for '%s::%s'",
846 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
850 new = g_param_spec_override (name, overridden);
851 g_object_class_install_property (oclass, property_id, new);
855 * g_object_class_list_properties:
856 * @oclass: a #GObjectClass
857 * @n_properties: (out): return location for the length of the returned array
859 * Get an array of #GParamSpec* for all properties of a class.
861 * Returns: (array length=n_properties) (transfer container): an array of
862 * #GParamSpec* which should be freed after use
864 GParamSpec** /* free result */
865 g_object_class_list_properties (GObjectClass *class,
866 guint *n_properties_p)
871 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
873 pspecs = g_param_spec_pool_list (pspec_pool,
874 G_OBJECT_CLASS_TYPE (class),
883 * g_object_interface_list_properties:
884 * @g_iface: any interface vtable for the interface, or the default
885 * vtable for the interface
886 * @n_properties_p: (out): location to store number of properties returned.
888 * Lists the properties of an interface.Generally, the interface
889 * vtable passed in as @g_iface will be the default vtable from
890 * g_type_default_interface_ref(), or, if you know the interface has
891 * already been loaded, g_type_default_interface_peek().
895 * Returns: (array length=n_properties_p) (transfer container): a
896 * pointer to an array of pointers to #GParamSpec
897 * structures. The paramspecs are owned by GLib, but the
898 * array should be freed with g_free() when you are done with
902 g_object_interface_list_properties (gpointer g_iface,
903 guint *n_properties_p)
905 GTypeInterface *iface_class = g_iface;
909 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
911 pspecs = g_param_spec_pool_list (pspec_pool,
921 g_object_init (GObject *object,
924 object->ref_count = 1;
925 object->qdata = NULL;
927 if (CLASS_HAS_PROPS (class))
929 /* freeze object's notification queue, g_object_newv() preserves pairedness */
930 g_object_notify_queue_freeze (object);
933 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
935 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
936 G_LOCK (construction_mutex);
937 construction_objects = g_slist_prepend (construction_objects, object);
938 G_UNLOCK (construction_mutex);
941 #ifdef G_ENABLE_DEBUG
944 G_LOCK (debug_objects);
945 debug_objects_count++;
946 g_hash_table_insert (debug_objects_ht, object, object);
947 G_UNLOCK (debug_objects);
949 #endif /* G_ENABLE_DEBUG */
953 g_object_do_set_property (GObject *object,
961 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
967 g_object_do_get_property (GObject *object,
975 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
981 g_object_real_dispose (GObject *object)
983 g_signal_handlers_destroy (object);
984 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
985 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
989 g_object_finalize (GObject *object)
991 g_datalist_clear (&object->qdata);
993 #ifdef G_ENABLE_DEBUG
996 G_LOCK (debug_objects);
997 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
998 g_hash_table_remove (debug_objects_ht, object);
999 debug_objects_count--;
1000 G_UNLOCK (debug_objects);
1002 #endif /* G_ENABLE_DEBUG */
1007 g_object_dispatch_properties_changed (GObject *object,
1009 GParamSpec **pspecs)
1013 for (i = 0; i < n_pspecs; i++)
1014 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1018 * g_object_run_dispose:
1019 * @object: a #GObject
1021 * Releases all references to other objects. This can be used to break
1024 * This functions should only be called from object system implementations.
1027 g_object_run_dispose (GObject *object)
1029 g_return_if_fail (G_IS_OBJECT (object));
1030 g_return_if_fail (object->ref_count > 0);
1032 g_object_ref (object);
1033 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1034 G_OBJECT_GET_CLASS (object)->dispose (object);
1035 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1036 g_object_unref (object);
1040 * g_object_freeze_notify:
1041 * @object: a #GObject
1043 * Increases the freeze count on @object. If the freeze count is
1044 * non-zero, the emission of "notify" signals on @object is
1045 * stopped. The signals are queued until the freeze count is decreased
1048 * This is necessary for accessors that modify multiple properties to prevent
1049 * premature notification while the object is still being modified.
1052 g_object_freeze_notify (GObject *object)
1054 g_return_if_fail (G_IS_OBJECT (object));
1056 if (g_atomic_int_get (&object->ref_count) == 0)
1059 g_object_ref (object);
1060 g_object_notify_queue_freeze (object);
1061 g_object_unref (object);
1065 get_notify_pspec (GParamSpec *pspec)
1067 GParamSpec *redirected;
1069 /* we don't notify on non-READABLE parameters */
1070 if (~pspec->flags & G_PARAM_READABLE)
1073 /* if the paramspec is redirected, notify on the target */
1074 redirected = g_param_spec_get_redirect_target (pspec);
1075 if (redirected != NULL)
1078 /* else, notify normally */
1083 g_object_notify_by_spec_internal (GObject *object,
1086 GObjectNotifyQueue *nqueue;
1087 GParamSpec *notify_pspec;
1089 notify_pspec = get_notify_pspec (pspec);
1091 if (notify_pspec != NULL)
1093 nqueue = g_object_notify_queue_freeze (object);
1094 g_object_notify_queue_add (object, nqueue, notify_pspec);
1095 g_object_notify_queue_thaw (object, nqueue);
1101 * @object: a #GObject
1102 * @property_name: the name of a property installed on the class of @object.
1104 * Emits a "notify" signal for the property @property_name on @object.
1106 * When possible, eg. when signaling a property change from within the class
1107 * that registered the property, you should use g_object_notify_by_pspec()
1111 g_object_notify (GObject *object,
1112 const gchar *property_name)
1116 g_return_if_fail (G_IS_OBJECT (object));
1117 g_return_if_fail (property_name != NULL);
1118 if (g_atomic_int_get (&object->ref_count) == 0)
1121 g_object_ref (object);
1122 /* We don't need to get the redirect target
1123 * (by, e.g. calling g_object_class_find_property())
1124 * because g_object_notify_queue_add() does that
1126 pspec = g_param_spec_pool_lookup (pspec_pool,
1128 G_OBJECT_TYPE (object),
1132 g_warning ("%s: object class `%s' has no property named `%s'",
1134 G_OBJECT_TYPE_NAME (object),
1137 g_object_notify_by_spec_internal (object, pspec);
1138 g_object_unref (object);
1142 * g_object_notify_by_pspec:
1143 * @object: a #GObject
1144 * @pspec: the #GParamSpec of a property installed on the class of @object.
1146 * Emits a "notify" signal for the property specified by @pspec on @object.
1148 * This function omits the property name lookup, hence it is faster than
1149 * g_object_notify().
1151 * One way to avoid using g_object_notify() from within the
1152 * class that registered the properties, and using g_object_notify_by_pspec()
1153 * instead, is to store the GParamSpec used with
1154 * g_object_class_install_property() inside a static array, e.g.:
1164 * static GParamSpec *properties[PROP_LAST];
1167 * my_object_class_init (MyObjectClass *klass)
1169 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1172 * G_PARAM_READWRITE);
1173 * g_object_class_install_property (gobject_class,
1175 * properties[PROP_FOO]);
1179 * and then notify a change on the "foo" property with:
1182 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1188 g_object_notify_by_pspec (GObject *object,
1192 g_return_if_fail (G_IS_OBJECT (object));
1193 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1195 g_object_ref (object);
1196 g_object_notify_by_spec_internal (object, pspec);
1197 g_object_unref (object);
1201 * g_object_thaw_notify:
1202 * @object: a #GObject
1204 * Reverts the effect of a previous call to
1205 * g_object_freeze_notify(). The freeze count is decreased on @object
1206 * and when it reaches zero, all queued "notify" signals are emitted.
1208 * It is an error to call this function when the freeze count is zero.
1211 g_object_thaw_notify (GObject *object)
1213 GObjectNotifyQueue *nqueue;
1215 g_return_if_fail (G_IS_OBJECT (object));
1216 if (g_atomic_int_get (&object->ref_count) == 0)
1219 g_object_ref (object);
1221 /* FIXME: Freezing is the only way to get at the notify queue.
1222 * So we freeze once and then thaw twice.
1224 nqueue = g_object_notify_queue_freeze (object);
1225 g_object_notify_queue_thaw (object, nqueue);
1226 g_object_notify_queue_thaw (object, nqueue);
1228 g_object_unref (object);
1232 object_get_property (GObject *object,
1236 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1237 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1238 GParamSpec *redirect;
1242 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1243 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1247 redirect = g_param_spec_get_redirect_target (pspec);
1251 class->get_property (object, param_id, value, pspec);
1255 object_set_property (GObject *object,
1257 const GValue *value,
1258 GObjectNotifyQueue *nqueue)
1260 GValue tmp_value = G_VALUE_INIT;
1261 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1262 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1263 GParamSpec *redirect;
1264 static const gchar * enable_diagnostic = NULL;
1268 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1269 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1273 redirect = g_param_spec_get_redirect_target (pspec);
1277 if (G_UNLIKELY (!enable_diagnostic))
1279 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1280 if (!enable_diagnostic)
1281 enable_diagnostic = "0";
1284 if (enable_diagnostic[0] == '1')
1286 if (pspec->flags & G_PARAM_DEPRECATED)
1287 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1288 "anymore. It will be removed in a future version.",
1289 G_OBJECT_TYPE_NAME (object), pspec->name);
1292 /* provide a copy to work from, convert (if necessary) and validate */
1293 g_value_init (&tmp_value, pspec->value_type);
1294 if (!g_value_transform (value, &tmp_value))
1295 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1297 g_type_name (pspec->value_type),
1298 G_VALUE_TYPE_NAME (value));
1299 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1301 gchar *contents = g_strdup_value_contents (value);
1303 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1305 G_VALUE_TYPE_NAME (value),
1307 g_type_name (pspec->value_type));
1312 GParamSpec *notify_pspec;
1314 class->set_property (object, param_id, &tmp_value, pspec);
1316 notify_pspec = get_notify_pspec (pspec);
1318 if (notify_pspec != NULL)
1319 g_object_notify_queue_add (object, nqueue, notify_pspec);
1321 g_value_unset (&tmp_value);
1325 object_interface_check_properties (gpointer func_data,
1328 GTypeInterface *iface_class = g_iface;
1329 GObjectClass *class;
1330 GType iface_type = iface_class->g_type;
1331 GParamSpec **pspecs;
1334 class = g_type_class_ref (iface_class->g_instance_type);
1336 if (!G_IS_OBJECT_CLASS (class))
1339 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1343 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1345 G_OBJECT_CLASS_TYPE (class),
1350 g_critical ("Object class %s doesn't implement property "
1351 "'%s' from interface '%s'",
1352 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1354 g_type_name (iface_type));
1359 /* The implementation paramspec must have a less restrictive
1360 * type than the interface parameter spec for set() and a
1361 * more restrictive type for get(). We just require equality,
1362 * rather than doing something more complicated checking
1363 * the READABLE and WRITABLE flags. We also simplify here
1364 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1367 !g_type_is_a (pspecs[n]->value_type,
1368 class_pspec->value_type))
1370 g_critical ("Property '%s' on class '%s' has type '%s' "
1371 "which is different from the type '%s', "
1372 "of the property on interface '%s'\n",
1374 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1375 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1376 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1377 g_type_name (iface_type));
1380 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1382 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1383 * READABLE and WRITABLE remove restrictions. The implementation
1384 * paramspec must have less restrictive flags.
1387 (!SUBSET (class_pspec->flags,
1389 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1390 !SUBSET (pspecs[n]->flags,
1392 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1394 g_critical ("Flags for property '%s' on class '%s' "
1395 "are not compatible with the property on"
1398 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1399 g_type_name (iface_type));
1406 g_type_class_unref (class);
1410 g_object_get_type (void)
1412 return G_TYPE_OBJECT;
1416 * g_object_new: (skip)
1417 * @object_type: the type id of the #GObject subtype to instantiate
1418 * @first_property_name: the name of the first property
1419 * @...: the value of the first property, followed optionally by more
1420 * name/value pairs, followed by %NULL
1422 * Creates a new instance of a #GObject subtype and sets its properties.
1424 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1425 * which are not explicitly specified are set to their default values.
1427 * Returns: (transfer full): a new instance of @object_type
1430 g_object_new (GType object_type,
1431 const gchar *first_property_name,
1437 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1439 /* short circuit for calls supplying no properties */
1440 if (!first_property_name)
1441 return g_object_newv (object_type, 0, NULL);
1443 va_start (var_args, first_property_name);
1444 object = g_object_new_valist (object_type, first_property_name, var_args);
1451 slist_maybe_remove (GSList **slist,
1454 GSList *last = NULL, *node = *slist;
1457 if (node->data == data)
1460 last->next = node->next;
1462 *slist = node->next;
1463 g_slist_free_1 (node);
1472 static inline gboolean
1473 object_in_construction_list (GObject *object)
1475 gboolean in_construction;
1476 G_LOCK (construction_mutex);
1477 in_construction = g_slist_find (construction_objects, object) != NULL;
1478 G_UNLOCK (construction_mutex);
1479 return in_construction;
1484 * @object_type: the type id of the #GObject subtype to instantiate
1485 * @n_parameters: the length of the @parameters array
1486 * @parameters: (array length=n_parameters): an array of #GParameter
1488 * Creates a new instance of a #GObject subtype and sets its properties.
1490 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1491 * which are not explicitly specified are set to their default values.
1493 * Rename to: g_object_new
1494 * Returns: (type GObject.Object) (transfer full): a new instance of
1498 g_object_newv (GType object_type,
1500 GParameter *parameters)
1502 GObjectConstructParam *cparams = NULL, *oparams;
1503 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1505 GObjectClass *class, *unref_class = NULL;
1507 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1509 GList *clist = NULL;
1510 gboolean newly_constructed;
1513 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1515 class = g_type_class_peek_static (object_type);
1517 class = unref_class = g_type_class_ref (object_type);
1518 for (slist = class->construct_properties; slist; slist = slist->next)
1520 clist = g_list_prepend (clist, slist->data);
1521 n_total_cparams += 1;
1524 if (n_parameters == 0 && n_total_cparams == 0)
1526 /* This is a simple object with no construct properties, and
1527 * no properties are being set, so short circuit the parameter
1528 * handling. This speeds up simple object construction.
1531 object = class->constructor (object_type, 0, NULL);
1532 goto did_construction;
1535 /* collect parameters, sort into construction and normal ones */
1536 oparams = g_new (GObjectConstructParam, n_parameters);
1537 cparams = g_new (GObjectConstructParam, n_total_cparams);
1538 for (i = 0; i < n_parameters; i++)
1540 GValue *value = ¶meters[i].value;
1541 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1547 g_warning ("%s: object class `%s' has no property named `%s'",
1549 g_type_name (object_type),
1550 parameters[i].name);
1553 if (!(pspec->flags & G_PARAM_WRITABLE))
1555 g_warning ("%s: property `%s' of object class `%s' is not writable",
1558 g_type_name (object_type));
1561 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1563 GList *list = g_list_find (clist, pspec);
1567 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1568 G_STRFUNC, pspec->name, g_type_name (object_type));
1571 cparams[n_cparams].pspec = pspec;
1572 cparams[n_cparams].value = value;
1577 list->prev->next = list->next;
1579 list->next->prev = list->prev;
1580 g_list_free_1 (list);
1584 oparams[n_oparams].pspec = pspec;
1585 oparams[n_oparams].value = value;
1590 /* set remaining construction properties to default values */
1591 n_cvalues = n_total_cparams - n_cparams;
1592 cvalues = g_new (GValue, n_cvalues);
1595 GList *tmp = clist->next;
1596 GParamSpec *pspec = clist->data;
1597 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1600 g_value_init (value, pspec->value_type);
1601 g_param_value_set_default (pspec, value);
1603 cparams[n_cparams].pspec = pspec;
1604 cparams[n_cparams].value = value;
1607 g_list_free_1 (clist);
1611 /* construct object from construction parameters */
1612 object = class->constructor (object_type, n_total_cparams, cparams);
1613 /* free construction values */
1616 g_value_unset (cvalues + n_cvalues);
1620 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1622 /* adjust freeze_count according to g_object_init() and remaining properties */
1623 G_LOCK (construction_mutex);
1624 newly_constructed = slist_maybe_remove (&construction_objects, object);
1625 G_UNLOCK (construction_mutex);
1628 newly_constructed = TRUE;
1630 if (CLASS_HAS_PROPS (class))
1632 if (newly_constructed || n_oparams)
1633 nqueue = g_object_notify_queue_freeze (object);
1634 if (newly_constructed)
1635 g_object_notify_queue_thaw (object, nqueue);
1638 /* run 'constructed' handler if there is a custom one */
1639 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1640 class->constructed (object);
1642 /* set remaining properties */
1643 for (i = 0; i < n_oparams; i++)
1644 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1647 if (CLASS_HAS_PROPS (class))
1649 /* release our own freeze count and handle notifications */
1650 if (newly_constructed || n_oparams)
1651 g_object_notify_queue_thaw (object, nqueue);
1655 g_type_class_unref (unref_class);
1661 * g_object_new_valist: (skip)
1662 * @object_type: the type id of the #GObject subtype to instantiate
1663 * @first_property_name: the name of the first property
1664 * @var_args: the value of the first property, followed optionally by more
1665 * name/value pairs, followed by %NULL
1667 * Creates a new instance of a #GObject subtype and sets its properties.
1669 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1670 * which are not explicitly specified are set to their default values.
1672 * Returns: a new instance of @object_type
1675 g_object_new_valist (GType object_type,
1676 const gchar *first_property_name,
1679 GObjectClass *class;
1683 guint n_params = 0, n_alloced_params = 16;
1685 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1687 if (!first_property_name)
1688 return g_object_newv (object_type, 0, NULL);
1690 class = g_type_class_ref (object_type);
1692 params = g_new0 (GParameter, n_alloced_params);
1693 name = first_property_name;
1696 gchar *error = NULL;
1697 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1703 g_warning ("%s: object class `%s' has no property named `%s'",
1705 g_type_name (object_type),
1709 if (n_params >= n_alloced_params)
1711 n_alloced_params += 16;
1712 params = g_renew (GParameter, params, n_alloced_params);
1713 memset (params + n_params, 0, 16 * (sizeof *params));
1715 params[n_params].name = name;
1716 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1717 var_args, 0, &error);
1720 g_warning ("%s: %s", G_STRFUNC, error);
1722 g_value_unset (¶ms[n_params].value);
1726 name = va_arg (var_args, gchar*);
1729 object = g_object_newv (object_type, n_params, params);
1732 g_value_unset (¶ms[n_params].value);
1735 g_type_class_unref (class);
1741 g_object_constructor (GType type,
1742 guint n_construct_properties,
1743 GObjectConstructParam *construct_params)
1748 object = (GObject*) g_type_create_instance (type);
1750 /* set construction parameters */
1751 if (n_construct_properties)
1753 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object);
1755 /* set construct properties */
1756 while (n_construct_properties--)
1758 GValue *value = construct_params->value;
1759 GParamSpec *pspec = construct_params->pspec;
1762 object_set_property (object, pspec, value, nqueue);
1764 g_object_notify_queue_thaw (object, nqueue);
1765 /* the notification queue is still frozen from g_object_init(), so
1766 * we don't need to handle it here, g_object_newv() takes
1775 g_object_constructed (GObject *object)
1777 /* empty default impl to allow unconditional upchaining */
1781 * g_object_set_valist: (skip)
1782 * @object: a #GObject
1783 * @first_property_name: name of the first property to set
1784 * @var_args: value for the first property, followed optionally by more
1785 * name/value pairs, followed by %NULL
1787 * Sets properties on an object.
1790 g_object_set_valist (GObject *object,
1791 const gchar *first_property_name,
1794 GObjectNotifyQueue *nqueue;
1797 g_return_if_fail (G_IS_OBJECT (object));
1799 g_object_ref (object);
1800 nqueue = g_object_notify_queue_freeze (object);
1802 name = first_property_name;
1805 GValue value = G_VALUE_INIT;
1807 gchar *error = NULL;
1809 pspec = g_param_spec_pool_lookup (pspec_pool,
1811 G_OBJECT_TYPE (object),
1815 g_warning ("%s: object class `%s' has no property named `%s'",
1817 G_OBJECT_TYPE_NAME (object),
1821 if (!(pspec->flags & G_PARAM_WRITABLE))
1823 g_warning ("%s: property `%s' of object class `%s' is not writable",
1826 G_OBJECT_TYPE_NAME (object));
1829 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1831 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1832 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1836 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1840 g_warning ("%s: %s", G_STRFUNC, error);
1842 g_value_unset (&value);
1846 object_set_property (object, pspec, &value, nqueue);
1847 g_value_unset (&value);
1849 name = va_arg (var_args, gchar*);
1852 g_object_notify_queue_thaw (object, nqueue);
1853 g_object_unref (object);
1857 * g_object_get_valist: (skip)
1858 * @object: a #GObject
1859 * @first_property_name: name of the first property to get
1860 * @var_args: return location for the first property, followed optionally by more
1861 * name/return location pairs, followed by %NULL
1863 * Gets properties of an object.
1865 * In general, a copy is made of the property contents and the caller
1866 * is responsible for freeing the memory in the appropriate manner for
1867 * the type, for instance by calling g_free() or g_object_unref().
1869 * See g_object_get().
1872 g_object_get_valist (GObject *object,
1873 const gchar *first_property_name,
1878 g_return_if_fail (G_IS_OBJECT (object));
1880 g_object_ref (object);
1882 name = first_property_name;
1886 GValue value = G_VALUE_INIT;
1890 pspec = g_param_spec_pool_lookup (pspec_pool,
1892 G_OBJECT_TYPE (object),
1896 g_warning ("%s: object class `%s' has no property named `%s'",
1898 G_OBJECT_TYPE_NAME (object),
1902 if (!(pspec->flags & G_PARAM_READABLE))
1904 g_warning ("%s: property `%s' of object class `%s' is not readable",
1907 G_OBJECT_TYPE_NAME (object));
1911 g_value_init (&value, pspec->value_type);
1913 object_get_property (object, pspec, &value);
1915 G_VALUE_LCOPY (&value, var_args, 0, &error);
1918 g_warning ("%s: %s", G_STRFUNC, error);
1920 g_value_unset (&value);
1924 g_value_unset (&value);
1926 name = va_arg (var_args, gchar*);
1929 g_object_unref (object);
1933 * g_object_set: (skip)
1934 * @object: a #GObject
1935 * @first_property_name: name of the first property to set
1936 * @...: value for the first property, followed optionally by more
1937 * name/value pairs, followed by %NULL
1939 * Sets properties on an object.
1942 g_object_set (gpointer _object,
1943 const gchar *first_property_name,
1946 GObject *object = _object;
1949 g_return_if_fail (G_IS_OBJECT (object));
1951 va_start (var_args, first_property_name);
1952 g_object_set_valist (object, first_property_name, var_args);
1957 * g_object_get: (skip)
1958 * @object: a #GObject
1959 * @first_property_name: name of the first property to get
1960 * @...: return location for the first property, followed optionally by more
1961 * name/return location pairs, followed by %NULL
1963 * Gets properties of an object.
1965 * In general, a copy is made of the property contents and the caller
1966 * is responsible for freeing the memory in the appropriate manner for
1967 * the type, for instance by calling g_free() or g_object_unref().
1970 * <title>Using g_object_get(<!-- -->)</title>
1971 * An example of using g_object_get() to get the contents
1972 * of three properties - one of type #G_TYPE_INT,
1973 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1979 * g_object_get (my_object,
1980 * "int-property", &intval,
1981 * "str-property", &strval,
1982 * "obj-property", &objval,
1985 * // Do something with intval, strval, objval
1988 * g_object_unref (objval);
1993 g_object_get (gpointer _object,
1994 const gchar *first_property_name,
1997 GObject *object = _object;
2000 g_return_if_fail (G_IS_OBJECT (object));
2002 va_start (var_args, first_property_name);
2003 g_object_get_valist (object, first_property_name, var_args);
2008 * g_object_set_property:
2009 * @object: a #GObject
2010 * @property_name: the name of the property to set
2013 * Sets a property on an object.
2016 g_object_set_property (GObject *object,
2017 const gchar *property_name,
2018 const GValue *value)
2020 GObjectNotifyQueue *nqueue;
2023 g_return_if_fail (G_IS_OBJECT (object));
2024 g_return_if_fail (property_name != NULL);
2025 g_return_if_fail (G_IS_VALUE (value));
2027 g_object_ref (object);
2028 nqueue = g_object_notify_queue_freeze (object);
2030 pspec = g_param_spec_pool_lookup (pspec_pool,
2032 G_OBJECT_TYPE (object),
2035 g_warning ("%s: object class `%s' has no property named `%s'",
2037 G_OBJECT_TYPE_NAME (object),
2039 else if (!(pspec->flags & G_PARAM_WRITABLE))
2040 g_warning ("%s: property `%s' of object class `%s' is not writable",
2043 G_OBJECT_TYPE_NAME (object));
2044 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2045 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2046 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2048 object_set_property (object, pspec, value, nqueue);
2050 g_object_notify_queue_thaw (object, nqueue);
2051 g_object_unref (object);
2055 * g_object_get_property:
2056 * @object: a #GObject
2057 * @property_name: the name of the property to get
2058 * @value: return location for the property value
2060 * Gets a property of an object. @value must have been initialized to the
2061 * expected type of the property (or a type to which the expected type can be
2062 * transformed) using g_value_init().
2064 * In general, a copy is made of the property contents and the caller is
2065 * responsible for freeing the memory by calling g_value_unset().
2067 * Note that g_object_get_property() is really intended for language
2068 * bindings, g_object_get() is much more convenient for C programming.
2071 g_object_get_property (GObject *object,
2072 const gchar *property_name,
2077 g_return_if_fail (G_IS_OBJECT (object));
2078 g_return_if_fail (property_name != NULL);
2079 g_return_if_fail (G_IS_VALUE (value));
2081 g_object_ref (object);
2083 pspec = g_param_spec_pool_lookup (pspec_pool,
2085 G_OBJECT_TYPE (object),
2088 g_warning ("%s: object class `%s' has no property named `%s'",
2090 G_OBJECT_TYPE_NAME (object),
2092 else if (!(pspec->flags & G_PARAM_READABLE))
2093 g_warning ("%s: property `%s' of object class `%s' is not readable",
2096 G_OBJECT_TYPE_NAME (object));
2099 GValue *prop_value, tmp_value = G_VALUE_INIT;
2101 /* auto-conversion of the callers value type
2103 if (G_VALUE_TYPE (value) == pspec->value_type)
2105 g_value_reset (value);
2108 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2110 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2111 G_STRFUNC, pspec->name,
2112 g_type_name (pspec->value_type),
2113 G_VALUE_TYPE_NAME (value));
2114 g_object_unref (object);
2119 g_value_init (&tmp_value, pspec->value_type);
2120 prop_value = &tmp_value;
2122 object_get_property (object, pspec, prop_value);
2123 if (prop_value != value)
2125 g_value_transform (prop_value, value);
2126 g_value_unset (&tmp_value);
2130 g_object_unref (object);
2134 * g_object_connect: (skip)
2135 * @object: a #GObject
2136 * @signal_spec: the spec for the first signal
2137 * @...: #GCallback for the first signal, followed by data for the
2138 * first signal, followed optionally by more signal
2139 * spec/callback/data triples, followed by %NULL
2141 * A convenience function to connect multiple signals at once.
2143 * The signal specs expected by this function have the form
2144 * "modifier::signal_name", where modifier can be one of the following:
2147 * <term>signal</term>
2149 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2150 * </para></listitem>
2153 * <term>object_signal</term>
2154 * <term>object-signal</term>
2156 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2157 * </para></listitem>
2160 * <term>swapped_signal</term>
2161 * <term>swapped-signal</term>
2163 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2164 * </para></listitem>
2167 * <term>swapped_object_signal</term>
2168 * <term>swapped-object-signal</term>
2170 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2171 * </para></listitem>
2174 * <term>signal_after</term>
2175 * <term>signal-after</term>
2177 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2178 * </para></listitem>
2181 * <term>object_signal_after</term>
2182 * <term>object-signal-after</term>
2184 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2185 * </para></listitem>
2188 * <term>swapped_signal_after</term>
2189 * <term>swapped-signal-after</term>
2191 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2192 * </para></listitem>
2195 * <term>swapped_object_signal_after</term>
2196 * <term>swapped-object-signal-after</term>
2198 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2199 * </para></listitem>
2204 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2205 * "type", GTK_WINDOW_POPUP,
2208 * "signal::event", gtk_menu_window_event, menu,
2209 * "signal::size_request", gtk_menu_window_size_request, menu,
2210 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2214 * Returns: (transfer none): @object
2217 g_object_connect (gpointer _object,
2218 const gchar *signal_spec,
2221 GObject *object = _object;
2224 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2225 g_return_val_if_fail (object->ref_count > 0, object);
2227 va_start (var_args, signal_spec);
2230 GCallback callback = va_arg (var_args, GCallback);
2231 gpointer data = va_arg (var_args, gpointer);
2233 if (strncmp (signal_spec, "signal::", 8) == 0)
2234 g_signal_connect_data (object, signal_spec + 8,
2235 callback, data, NULL,
2237 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2238 strncmp (signal_spec, "object-signal::", 15) == 0)
2239 g_signal_connect_object (object, signal_spec + 15,
2242 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2243 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2244 g_signal_connect_data (object, signal_spec + 16,
2245 callback, data, NULL,
2247 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2248 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2249 g_signal_connect_object (object, signal_spec + 23,
2252 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2253 strncmp (signal_spec, "signal-after::", 14) == 0)
2254 g_signal_connect_data (object, signal_spec + 14,
2255 callback, data, NULL,
2257 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2258 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2259 g_signal_connect_object (object, signal_spec + 21,
2262 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2263 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2264 g_signal_connect_data (object, signal_spec + 22,
2265 callback, data, NULL,
2266 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2267 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2268 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2269 g_signal_connect_object (object, signal_spec + 29,
2271 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2274 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2277 signal_spec = va_arg (var_args, gchar*);
2285 * g_object_disconnect: (skip)
2286 * @object: a #GObject
2287 * @signal_spec: the spec for the first signal
2288 * @...: #GCallback for the first signal, followed by data for the first signal,
2289 * followed optionally by more signal spec/callback/data triples,
2292 * A convenience function to disconnect multiple signals at once.
2294 * The signal specs expected by this function have the form
2295 * "any_signal", which means to disconnect any signal with matching
2296 * callback and data, or "any_signal::signal_name", which only
2297 * disconnects the signal named "signal_name".
2300 g_object_disconnect (gpointer _object,
2301 const gchar *signal_spec,
2304 GObject *object = _object;
2307 g_return_if_fail (G_IS_OBJECT (object));
2308 g_return_if_fail (object->ref_count > 0);
2310 va_start (var_args, signal_spec);
2313 GCallback callback = va_arg (var_args, GCallback);
2314 gpointer data = va_arg (var_args, gpointer);
2315 guint sid = 0, detail = 0, mask = 0;
2317 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2318 strncmp (signal_spec, "any-signal::", 12) == 0)
2321 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2323 else if (strcmp (signal_spec, "any_signal") == 0 ||
2324 strcmp (signal_spec, "any-signal") == 0)
2327 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2331 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2335 if ((mask & G_SIGNAL_MATCH_ID) &&
2336 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2337 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2338 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2340 NULL, (gpointer)callback, data))
2341 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2342 signal_spec = va_arg (var_args, gchar*);
2353 } weak_refs[1]; /* flexible array */
2357 weak_refs_notify (gpointer data)
2359 WeakRefStack *wstack = data;
2362 for (i = 0; i < wstack->n_weak_refs; i++)
2363 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2368 * g_object_weak_ref: (skip)
2369 * @object: #GObject to reference weakly
2370 * @notify: callback to invoke before the object is freed
2371 * @data: extra data to pass to notify
2373 * Adds a weak reference callback to an object. Weak references are
2374 * used for notification when an object is finalized. They are called
2375 * "weak references" because they allow you to safely hold a pointer
2376 * to an object without calling g_object_ref() (g_object_ref() adds a
2377 * strong reference, that is, forces the object to stay alive).
2380 g_object_weak_ref (GObject *object,
2384 WeakRefStack *wstack;
2387 g_return_if_fail (G_IS_OBJECT (object));
2388 g_return_if_fail (notify != NULL);
2389 g_return_if_fail (object->ref_count >= 1);
2391 G_LOCK (weak_refs_mutex);
2392 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2395 i = wstack->n_weak_refs++;
2396 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2400 wstack = g_renew (WeakRefStack, NULL, 1);
2401 wstack->object = object;
2402 wstack->n_weak_refs = 1;
2405 wstack->weak_refs[i].notify = notify;
2406 wstack->weak_refs[i].data = data;
2407 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2408 G_UNLOCK (weak_refs_mutex);
2412 * g_object_weak_unref: (skip)
2413 * @object: #GObject to remove a weak reference from
2414 * @notify: callback to search for
2415 * @data: data to search for
2417 * Removes a weak reference callback to an object.
2420 g_object_weak_unref (GObject *object,
2424 WeakRefStack *wstack;
2425 gboolean found_one = FALSE;
2427 g_return_if_fail (G_IS_OBJECT (object));
2428 g_return_if_fail (notify != NULL);
2430 G_LOCK (weak_refs_mutex);
2431 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2436 for (i = 0; i < wstack->n_weak_refs; i++)
2437 if (wstack->weak_refs[i].notify == notify &&
2438 wstack->weak_refs[i].data == data)
2441 wstack->n_weak_refs -= 1;
2442 if (i != wstack->n_weak_refs)
2443 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2448 G_UNLOCK (weak_refs_mutex);
2450 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2454 * g_object_add_weak_pointer: (skip)
2455 * @object: The object that should be weak referenced.
2456 * @weak_pointer_location: (inout): The memory address of a pointer.
2458 * Adds a weak reference from weak_pointer to @object to indicate that
2459 * the pointer located at @weak_pointer_location is only valid during
2460 * the lifetime of @object. When the @object is finalized,
2461 * @weak_pointer will be set to %NULL.
2464 g_object_add_weak_pointer (GObject *object,
2465 gpointer *weak_pointer_location)
2467 g_return_if_fail (G_IS_OBJECT (object));
2468 g_return_if_fail (weak_pointer_location != NULL);
2470 g_object_weak_ref (object,
2471 (GWeakNotify) g_nullify_pointer,
2472 weak_pointer_location);
2476 * g_object_remove_weak_pointer: (skip)
2477 * @object: The object that is weak referenced.
2478 * @weak_pointer_location: (inout): The memory address of a pointer.
2480 * Removes a weak reference from @object that was previously added
2481 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2482 * to match the one used with g_object_add_weak_pointer().
2485 g_object_remove_weak_pointer (GObject *object,
2486 gpointer *weak_pointer_location)
2488 g_return_if_fail (G_IS_OBJECT (object));
2489 g_return_if_fail (weak_pointer_location != NULL);
2491 g_object_weak_unref (object,
2492 (GWeakNotify) g_nullify_pointer,
2493 weak_pointer_location);
2497 object_floating_flag_handler (GObject *object,
2503 case +1: /* force floating if possible */
2505 oldvalue = g_atomic_pointer_get (&object->qdata);
2506 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2507 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2508 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2509 case -1: /* sink if possible */
2511 oldvalue = g_atomic_pointer_get (&object->qdata);
2512 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2513 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2514 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2515 default: /* check floating */
2516 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2521 * g_object_is_floating:
2522 * @object: (type GObject.Object): a #GObject
2524 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2529 * Returns: %TRUE if @object has a floating reference
2532 g_object_is_floating (gpointer _object)
2534 GObject *object = _object;
2535 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2536 return floating_flag_handler (object, 0);
2540 * g_object_ref_sink:
2541 * @object: (type GObject.Object): a #GObject
2543 * Increase the reference count of @object, and possibly remove the
2544 * <link linkend="floating-ref">floating</link> reference, if @object
2545 * has a floating reference.
2547 * In other words, if the object is floating, then this call "assumes
2548 * ownership" of the floating reference, converting it to a normal
2549 * reference by clearing the floating flag while leaving the reference
2550 * count unchanged. If the object is not floating, then this call
2551 * adds a new normal reference increasing the reference count by one.
2555 * Returns: (type GObject.Object) (transfer none): @object
2558 g_object_ref_sink (gpointer _object)
2560 GObject *object = _object;
2561 gboolean was_floating;
2562 g_return_val_if_fail (G_IS_OBJECT (object), object);
2563 g_return_val_if_fail (object->ref_count >= 1, object);
2564 g_object_ref (object);
2565 was_floating = floating_flag_handler (object, -1);
2567 g_object_unref (object);
2572 * g_object_force_floating:
2573 * @object: a #GObject
2575 * This function is intended for #GObject implementations to re-enforce a
2576 * <link linkend="floating-ref">floating</link> object reference.
2577 * Doing this is seldom required: all
2578 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2579 * usually just needs to be sunken by calling g_object_ref_sink().
2584 g_object_force_floating (GObject *object)
2586 g_return_if_fail (G_IS_OBJECT (object));
2587 g_return_if_fail (object->ref_count >= 1);
2589 floating_flag_handler (object, +1);
2594 guint n_toggle_refs;
2596 GToggleNotify notify;
2598 } toggle_refs[1]; /* flexible array */
2602 toggle_refs_notify (GObject *object,
2603 gboolean is_last_ref)
2605 ToggleRefStack tstack, *tstackptr;
2607 G_LOCK (toggle_refs_mutex);
2608 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2609 tstack = *tstackptr;
2610 G_UNLOCK (toggle_refs_mutex);
2612 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2613 * will only be notified when there is exactly one of them.
2615 g_assert (tstack.n_toggle_refs == 1);
2616 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2620 * g_object_add_toggle_ref: (skip)
2621 * @object: a #GObject
2622 * @notify: a function to call when this reference is the
2623 * last reference to the object, or is no longer
2624 * the last reference.
2625 * @data: data to pass to @notify
2627 * Increases the reference count of the object by one and sets a
2628 * callback to be called when all other references to the object are
2629 * dropped, or when this is already the last reference to the object
2630 * and another reference is established.
2632 * This functionality is intended for binding @object to a proxy
2633 * object managed by another memory manager. This is done with two
2634 * paired references: the strong reference added by
2635 * g_object_add_toggle_ref() and a reverse reference to the proxy
2636 * object which is either a strong reference or weak reference.
2638 * The setup is that when there are no other references to @object,
2639 * only a weak reference is held in the reverse direction from @object
2640 * to the proxy object, but when there are other references held to
2641 * @object, a strong reference is held. The @notify callback is called
2642 * when the reference from @object to the proxy object should be
2643 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2644 * true) or weak to strong (@is_last_ref false).
2646 * Since a (normal) reference must be held to the object before
2647 * calling g_object_add_toggle_ref(), the initial state of the reverse
2648 * link is always strong.
2650 * Multiple toggle references may be added to the same gobject,
2651 * however if there are multiple toggle references to an object, none
2652 * of them will ever be notified until all but one are removed. For
2653 * this reason, you should only ever use a toggle reference if there
2654 * is important state in the proxy object.
2659 g_object_add_toggle_ref (GObject *object,
2660 GToggleNotify notify,
2663 ToggleRefStack *tstack;
2666 g_return_if_fail (G_IS_OBJECT (object));
2667 g_return_if_fail (notify != NULL);
2668 g_return_if_fail (object->ref_count >= 1);
2670 g_object_ref (object);
2672 G_LOCK (toggle_refs_mutex);
2673 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2676 i = tstack->n_toggle_refs++;
2677 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2678 * in tstate->toggle_refs */
2679 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2683 tstack = g_renew (ToggleRefStack, NULL, 1);
2684 tstack->object = object;
2685 tstack->n_toggle_refs = 1;
2689 /* Set a flag for fast lookup after adding the first toggle reference */
2690 if (tstack->n_toggle_refs == 1)
2691 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2693 tstack->toggle_refs[i].notify = notify;
2694 tstack->toggle_refs[i].data = data;
2695 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2696 (GDestroyNotify)g_free);
2697 G_UNLOCK (toggle_refs_mutex);
2701 * g_object_remove_toggle_ref: (skip)
2702 * @object: a #GObject
2703 * @notify: a function to call when this reference is the
2704 * last reference to the object, or is no longer
2705 * the last reference.
2706 * @data: data to pass to @notify
2708 * Removes a reference added with g_object_add_toggle_ref(). The
2709 * reference count of the object is decreased by one.
2714 g_object_remove_toggle_ref (GObject *object,
2715 GToggleNotify notify,
2718 ToggleRefStack *tstack;
2719 gboolean found_one = FALSE;
2721 g_return_if_fail (G_IS_OBJECT (object));
2722 g_return_if_fail (notify != NULL);
2724 G_LOCK (toggle_refs_mutex);
2725 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2730 for (i = 0; i < tstack->n_toggle_refs; i++)
2731 if (tstack->toggle_refs[i].notify == notify &&
2732 tstack->toggle_refs[i].data == data)
2735 tstack->n_toggle_refs -= 1;
2736 if (i != tstack->n_toggle_refs)
2737 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2739 if (tstack->n_toggle_refs == 0)
2740 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2745 G_UNLOCK (toggle_refs_mutex);
2748 g_object_unref (object);
2750 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2755 * @object: (type GObject.Object): a #GObject
2757 * Increases the reference count of @object.
2759 * Returns: (type GObject.Object) (transfer none): the same @object
2762 g_object_ref (gpointer _object)
2764 GObject *object = _object;
2767 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2768 g_return_val_if_fail (object->ref_count > 0, NULL);
2770 #ifdef G_ENABLE_DEBUG
2771 if (g_trap_object_ref == object)
2773 #endif /* G_ENABLE_DEBUG */
2776 old_val = g_atomic_int_add (&object->ref_count, 1);
2778 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2779 toggle_refs_notify (object, FALSE);
2781 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2788 * @object: (type GObject.Object): a #GObject
2790 * Decreases the reference count of @object. When its reference count
2791 * drops to 0, the object is finalized (i.e. its memory is freed).
2794 g_object_unref (gpointer _object)
2796 GObject *object = _object;
2799 g_return_if_fail (G_IS_OBJECT (object));
2800 g_return_if_fail (object->ref_count > 0);
2802 #ifdef G_ENABLE_DEBUG
2803 if (g_trap_object_ref == object)
2805 #endif /* G_ENABLE_DEBUG */
2807 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2808 retry_atomic_decrement1:
2809 old_ref = g_atomic_int_get (&object->ref_count);
2812 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2813 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2815 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2816 goto retry_atomic_decrement1;
2818 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2820 /* if we went from 2->1 we need to notify toggle refs if any */
2821 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2822 toggle_refs_notify (object, TRUE);
2826 /* we are about tp remove the last reference */
2827 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2828 G_OBJECT_GET_CLASS (object)->dispose (object);
2829 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2831 /* may have been re-referenced meanwhile */
2832 retry_atomic_decrement2:
2833 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2836 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2837 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2839 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2840 goto retry_atomic_decrement2;
2842 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2844 /* if we went from 2->1 we need to notify toggle refs if any */
2845 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2846 toggle_refs_notify (object, TRUE);
2851 /* we are still in the process of taking away the last ref */
2852 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2853 g_signal_handlers_destroy (object);
2854 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2856 /* decrement the last reference */
2857 old_ref = g_atomic_int_add (&object->ref_count, -1);
2859 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2861 /* may have been re-referenced meanwhile */
2862 if (G_LIKELY (old_ref == 1))
2864 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2865 G_OBJECT_GET_CLASS (object)->finalize (object);
2867 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2869 #ifdef G_ENABLE_DEBUG
2872 /* catch objects not chaining finalize handlers */
2873 G_LOCK (debug_objects);
2874 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2875 G_UNLOCK (debug_objects);
2877 #endif /* G_ENABLE_DEBUG */
2878 g_type_free_instance ((GTypeInstance*) object);
2884 * g_clear_object: (skip)
2885 * @object_ptr: a pointer to a #GObject reference
2887 * Clears a reference to a #GObject.
2889 * @object_ptr must not be %NULL.
2891 * If the reference is %NULL then this function does nothing.
2892 * Otherwise, the reference count of the object is decreased and the
2893 * pointer is set to %NULL.
2895 * This function is threadsafe and modifies the pointer atomically,
2896 * using memory barriers where needed.
2898 * A macro is also included that allows this function to be used without
2903 #undef g_clear_object
2905 g_clear_object (volatile GObject **object_ptr)
2907 gpointer *ptr = (gpointer) object_ptr;
2910 /* This is a little frustrating.
2911 * Would be nice to have an atomic exchange (with no compare).
2914 old = g_atomic_pointer_get (ptr);
2915 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2918 g_object_unref (old);
2922 * g_object_get_qdata:
2923 * @object: The GObject to get a stored user data pointer from
2924 * @quark: A #GQuark, naming the user data pointer
2926 * This function gets back user data pointers stored via
2927 * g_object_set_qdata().
2929 * Returns: (transfer none): The user data pointer set, or %NULL
2932 g_object_get_qdata (GObject *object,
2935 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2937 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2941 * g_object_set_qdata: (skip)
2942 * @object: The GObject to set store a user data pointer
2943 * @quark: A #GQuark, naming the user data pointer
2944 * @data: An opaque user data pointer
2946 * This sets an opaque, named pointer on an object.
2947 * The name is specified through a #GQuark (retrived e.g. via
2948 * g_quark_from_static_string()), and the pointer
2949 * can be gotten back from the @object with g_object_get_qdata()
2950 * until the @object is finalized.
2951 * Setting a previously set user data pointer, overrides (frees)
2952 * the old pointer set, using #NULL as pointer essentially
2953 * removes the data stored.
2956 g_object_set_qdata (GObject *object,
2960 g_return_if_fail (G_IS_OBJECT (object));
2961 g_return_if_fail (quark > 0);
2963 g_datalist_id_set_data (&object->qdata, quark, data);
2967 * g_object_set_qdata_full: (skip)
2968 * @object: The GObject to set store a user data pointer
2969 * @quark: A #GQuark, naming the user data pointer
2970 * @data: An opaque user data pointer
2971 * @destroy: Function to invoke with @data as argument, when @data
2974 * This function works like g_object_set_qdata(), but in addition,
2975 * a void (*destroy) (gpointer) function may be specified which is
2976 * called with @data as argument when the @object is finalized, or
2977 * the data is being overwritten by a call to g_object_set_qdata()
2978 * with the same @quark.
2981 g_object_set_qdata_full (GObject *object,
2984 GDestroyNotify destroy)
2986 g_return_if_fail (G_IS_OBJECT (object));
2987 g_return_if_fail (quark > 0);
2989 g_datalist_id_set_data_full (&object->qdata, quark, data,
2990 data ? destroy : (GDestroyNotify) NULL);
2994 * g_object_steal_qdata:
2995 * @object: The GObject to get a stored user data pointer from
2996 * @quark: A #GQuark, naming the user data pointer
2998 * This function gets back user data pointers stored via
2999 * g_object_set_qdata() and removes the @data from object
3000 * without invoking its destroy() function (if any was
3002 * Usually, calling this function is only required to update
3003 * user data pointers with a destroy notifier, for example:
3006 * object_add_to_user_list (GObject *object,
3007 * const gchar *new_string)
3009 * // the quark, naming the object data
3010 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3011 * // retrive the old string list
3012 * GList *list = g_object_steal_qdata (object, quark_string_list);
3014 * // prepend new string
3015 * list = g_list_prepend (list, g_strdup (new_string));
3016 * // this changed 'list', so we need to set it again
3017 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3020 * free_string_list (gpointer data)
3022 * GList *node, *list = data;
3024 * for (node = list; node; node = node->next)
3025 * g_free (node->data);
3026 * g_list_free (list);
3029 * Using g_object_get_qdata() in the above example, instead of
3030 * g_object_steal_qdata() would have left the destroy function set,
3031 * and thus the partial string list would have been freed upon
3032 * g_object_set_qdata_full().
3034 * Returns: (transfer full): The user data pointer set, or %NULL
3037 g_object_steal_qdata (GObject *object,
3040 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3041 g_return_val_if_fail (quark > 0, NULL);
3043 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3047 * g_object_get_data:
3048 * @object: #GObject containing the associations
3049 * @key: name of the key for that association
3051 * Gets a named field from the objects table of associations (see g_object_set_data()).
3053 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3056 g_object_get_data (GObject *object,
3059 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3060 g_return_val_if_fail (key != NULL, NULL);
3062 return g_datalist_get_data (&object->qdata, key);
3066 * g_object_set_data:
3067 * @object: #GObject containing the associations.
3068 * @key: name of the key
3069 * @data: data to associate with that key
3071 * Each object carries around a table of associations from
3072 * strings to pointers. This function lets you set an association.
3074 * If the object already had an association with that name,
3075 * the old association will be destroyed.
3078 g_object_set_data (GObject *object,
3082 g_return_if_fail (G_IS_OBJECT (object));
3083 g_return_if_fail (key != NULL);
3085 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3089 * g_object_set_data_full: (skip)
3090 * @object: #GObject containing the associations
3091 * @key: name of the key
3092 * @data: data to associate with that key
3093 * @destroy: function to call when the association is destroyed
3095 * Like g_object_set_data() except it adds notification
3096 * for when the association is destroyed, either by setting it
3097 * to a different value or when the object is destroyed.
3099 * Note that the @destroy callback is not called if @data is %NULL.
3102 g_object_set_data_full (GObject *object,
3105 GDestroyNotify destroy)
3107 g_return_if_fail (G_IS_OBJECT (object));
3108 g_return_if_fail (key != NULL);
3110 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3111 data ? destroy : (GDestroyNotify) NULL);
3115 * g_object_steal_data:
3116 * @object: #GObject containing the associations
3117 * @key: name of the key
3119 * Remove a specified datum from the object's data associations,
3120 * without invoking the association's destroy handler.
3122 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3125 g_object_steal_data (GObject *object,
3130 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3131 g_return_val_if_fail (key != NULL, NULL);
3133 quark = g_quark_try_string (key);
3135 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3139 g_value_object_init (GValue *value)
3141 value->data[0].v_pointer = NULL;
3145 g_value_object_free_value (GValue *value)
3147 if (value->data[0].v_pointer)
3148 g_object_unref (value->data[0].v_pointer);
3152 g_value_object_copy_value (const GValue *src_value,
3155 if (src_value->data[0].v_pointer)
3156 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3158 dest_value->data[0].v_pointer = NULL;
3162 g_value_object_transform_value (const GValue *src_value,
3165 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)))
3166 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3168 dest_value->data[0].v_pointer = NULL;
3172 g_value_object_peek_pointer (const GValue *value)
3174 return value->data[0].v_pointer;
3178 g_value_object_collect_value (GValue *value,
3179 guint n_collect_values,
3180 GTypeCValue *collect_values,
3181 guint collect_flags)
3183 if (collect_values[0].v_pointer)
3185 GObject *object = collect_values[0].v_pointer;
3187 if (object->g_type_instance.g_class == NULL)
3188 return g_strconcat ("invalid unclassed object pointer for value type `",
3189 G_VALUE_TYPE_NAME (value),
3192 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3193 return g_strconcat ("invalid object type `",
3194 G_OBJECT_TYPE_NAME (object),
3195 "' for value type `",
3196 G_VALUE_TYPE_NAME (value),
3199 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3200 value->data[0].v_pointer = g_object_ref (object);
3203 value->data[0].v_pointer = NULL;
3209 g_value_object_lcopy_value (const GValue *value,
3210 guint n_collect_values,
3211 GTypeCValue *collect_values,
3212 guint collect_flags)
3214 GObject **object_p = collect_values[0].v_pointer;
3217 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3219 if (!value->data[0].v_pointer)
3221 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3222 *object_p = value->data[0].v_pointer;
3224 *object_p = g_object_ref (value->data[0].v_pointer);
3230 * g_value_set_object:
3231 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3232 * @v_object: (type GObject.Object) (allow-none): object value to be set
3234 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3236 * g_value_set_object() increases the reference count of @v_object
3237 * (the #GValue holds a reference to @v_object). If you do not wish
3238 * to increase the reference count of the object (i.e. you wish to
3239 * pass your current reference to the #GValue because you no longer
3240 * need it), use g_value_take_object() instead.
3242 * It is important that your #GValue holds a reference to @v_object (either its
3243 * own, or one it has taken) to ensure that the object won't be destroyed while
3244 * the #GValue still exists).
3247 g_value_set_object (GValue *value,
3252 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3254 old = value->data[0].v_pointer;
3258 g_return_if_fail (G_IS_OBJECT (v_object));
3259 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3261 value->data[0].v_pointer = v_object;
3262 g_object_ref (value->data[0].v_pointer);
3265 value->data[0].v_pointer = NULL;
3268 g_object_unref (old);
3272 * g_value_set_object_take_ownership: (skip)
3273 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3274 * @v_object: (allow-none): object value to be set
3276 * This is an internal function introduced mainly for C marshallers.
3278 * Deprecated: 2.4: Use g_value_take_object() instead.
3281 g_value_set_object_take_ownership (GValue *value,
3284 g_value_take_object (value, v_object);
3288 * g_value_take_object: (skip)
3289 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3290 * @v_object: (allow-none): object value to be set
3292 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3293 * and takes over the ownership of the callers reference to @v_object;
3294 * the caller doesn't have to unref it any more (i.e. the reference
3295 * count of the object is not increased).
3297 * If you want the #GValue to hold its own reference to @v_object, use
3298 * g_value_set_object() instead.
3303 g_value_take_object (GValue *value,
3306 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3308 if (value->data[0].v_pointer)
3310 g_object_unref (value->data[0].v_pointer);
3311 value->data[0].v_pointer = NULL;
3316 g_return_if_fail (G_IS_OBJECT (v_object));
3317 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3319 value->data[0].v_pointer = v_object; /* we take over the reference count */
3324 * g_value_get_object:
3325 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3327 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3329 * Returns: (type GObject.Object) (transfer none): object contents of @value
3332 g_value_get_object (const GValue *value)
3334 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3336 return value->data[0].v_pointer;
3340 * g_value_dup_object:
3341 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3343 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3344 * its reference count. If the contents of the #GValue are %NULL, then
3345 * %NULL will be returned.
3347 * Returns: (type GObject.Object) (transfer full): object content of @value,
3348 * should be unreferenced when no longer needed.
3351 g_value_dup_object (const GValue *value)
3353 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3355 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3359 * g_signal_connect_object: (skip)
3360 * @instance: the instance to connect to.
3361 * @detailed_signal: a string of the form "signal-name::detail".
3362 * @c_handler: the #GCallback to connect.
3363 * @gobject: the object to pass as data to @c_handler.
3364 * @connect_flags: a combination of #GConnectFlags.
3366 * This is similar to g_signal_connect_data(), but uses a closure which
3367 * ensures that the @gobject stays alive during the call to @c_handler
3368 * by temporarily adding a reference count to @gobject.
3370 * Note that there is a bug in GObject that makes this function
3371 * much less useful than it might seem otherwise. Once @gobject is
3372 * disposed, the callback will no longer be called, but, the signal
3373 * handler is <emphasis>not</emphasis> currently disconnected. If the
3374 * @instance is itself being freed at the same time than this doesn't
3375 * matter, since the signal will automatically be removed, but
3376 * if @instance persists, then the signal handler will leak. You
3377 * should not remove the signal yourself because in a future versions of
3378 * GObject, the handler <emphasis>will</emphasis> automatically
3381 * It's possible to work around this problem in a way that will
3382 * continue to work with future versions of GObject by checking
3383 * that the signal handler is still connected before disconnected it:
3384 * <informalexample><programlisting>
3385 * if (g_signal_handler_is_connected (instance, id))
3386 * g_signal_handler_disconnect (instance, id);
3387 * </programlisting></informalexample>
3389 * Returns: the handler id.
3392 g_signal_connect_object (gpointer instance,
3393 const gchar *detailed_signal,
3394 GCallback c_handler,
3396 GConnectFlags connect_flags)
3398 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3399 g_return_val_if_fail (detailed_signal != NULL, 0);
3400 g_return_val_if_fail (c_handler != NULL, 0);
3406 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3408 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3410 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3413 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3419 GClosure *closures[1]; /* flexible array */
3421 /* don't change this structure without supplying an accessor for
3422 * watched closures, e.g.:
3423 * GSList* g_object_list_watched_closures (GObject *object)
3426 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3427 * carray = g_object_get_data (object, "GObject-closure-array");
3430 * GSList *slist = NULL;
3432 * for (i = 0; i < carray->n_closures; i++)
3433 * slist = g_slist_prepend (slist, carray->closures[i]);
3441 object_remove_closure (gpointer data,
3444 GObject *object = data;
3448 G_LOCK (closure_array_mutex);
3449 carray = g_object_get_qdata (object, quark_closure_array);
3450 for (i = 0; i < carray->n_closures; i++)
3451 if (carray->closures[i] == closure)
3453 carray->n_closures--;
3454 if (i < carray->n_closures)
3455 carray->closures[i] = carray->closures[carray->n_closures];
3456 G_UNLOCK (closure_array_mutex);
3459 G_UNLOCK (closure_array_mutex);
3460 g_assert_not_reached ();
3464 destroy_closure_array (gpointer data)
3466 CArray *carray = data;
3467 GObject *object = carray->object;
3468 guint i, n = carray->n_closures;
3470 for (i = 0; i < n; i++)
3472 GClosure *closure = carray->closures[i];
3474 /* removing object_remove_closure() upfront is probably faster than
3475 * letting it fiddle with quark_closure_array which is empty anyways
3477 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3478 g_closure_invalidate (closure);
3484 * g_object_watch_closure:
3485 * @object: GObject restricting lifetime of @closure
3486 * @closure: GClosure to watch
3488 * This function essentially limits the life time of the @closure to
3489 * the life time of the object. That is, when the object is finalized,
3490 * the @closure is invalidated by calling g_closure_invalidate() on
3491 * it, in order to prevent invocations of the closure with a finalized
3492 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3493 * added as marshal guards to the @closure, to ensure that an extra
3494 * reference count is held on @object during invocation of the
3495 * @closure. Usually, this function will be called on closures that
3496 * use this @object as closure data.
3499 g_object_watch_closure (GObject *object,
3505 g_return_if_fail (G_IS_OBJECT (object));
3506 g_return_if_fail (closure != NULL);
3507 g_return_if_fail (closure->is_invalid == FALSE);
3508 g_return_if_fail (closure->in_marshal == FALSE);
3509 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3511 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3512 g_closure_add_marshal_guards (closure,
3513 object, (GClosureNotify) g_object_ref,
3514 object, (GClosureNotify) g_object_unref);
3515 G_LOCK (closure_array_mutex);
3516 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3519 carray = g_renew (CArray, NULL, 1);
3520 carray->object = object;
3521 carray->n_closures = 1;
3526 i = carray->n_closures++;
3527 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3529 carray->closures[i] = closure;
3530 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3531 G_UNLOCK (closure_array_mutex);
3535 * g_closure_new_object:
3536 * @sizeof_closure: the size of the structure to allocate, must be at least
3537 * <literal>sizeof (GClosure)</literal>
3538 * @object: a #GObject pointer to store in the @data field of the newly
3539 * allocated #GClosure
3541 * A variant of g_closure_new_simple() which stores @object in the
3542 * @data field of the closure and calls g_object_watch_closure() on
3543 * @object and the created closure. This function is mainly useful
3544 * when implementing new types of closures.
3546 * Returns: (transfer full): a newly allocated #GClosure
3549 g_closure_new_object (guint sizeof_closure,
3554 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3555 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3557 closure = g_closure_new_simple (sizeof_closure, object);
3558 g_object_watch_closure (object, closure);
3564 * g_cclosure_new_object: (skip)
3565 * @callback_func: the function to invoke
3566 * @object: a #GObject pointer to pass to @callback_func
3568 * A variant of g_cclosure_new() which uses @object as @user_data and
3569 * calls g_object_watch_closure() on @object and the created
3570 * closure. This function is useful when you have a callback closely
3571 * associated with a #GObject, and want the callback to no longer run
3572 * after the object is is freed.
3574 * Returns: a new #GCClosure
3577 g_cclosure_new_object (GCallback callback_func,
3582 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3583 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3584 g_return_val_if_fail (callback_func != NULL, NULL);
3586 closure = g_cclosure_new (callback_func, object, NULL);
3587 g_object_watch_closure (object, closure);
3593 * g_cclosure_new_object_swap: (skip)
3594 * @callback_func: the function to invoke
3595 * @object: a #GObject pointer to pass to @callback_func
3597 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3598 * and calls g_object_watch_closure() on @object and the created
3599 * closure. This function is useful when you have a callback closely
3600 * associated with a #GObject, and want the callback to no longer run
3601 * after the object is is freed.
3603 * Returns: a new #GCClosure
3606 g_cclosure_new_object_swap (GCallback callback_func,
3611 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3612 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3613 g_return_val_if_fail (callback_func != NULL, NULL);
3615 closure = g_cclosure_new_swap (callback_func, object, NULL);
3616 g_object_watch_closure (object, closure);
3622 g_object_compat_control (gsize what,
3628 case 1: /* floating base type */
3629 return G_TYPE_INITIALLY_UNOWNED;
3630 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3631 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3633 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3635 *pp = floating_flag_handler;
3642 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3645 g_initially_unowned_init (GInitiallyUnowned *object)
3647 g_object_force_floating (object);
3651 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)