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 _GObjectNotifyContext GObjectNotifyContext;
182 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
183 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
185 GParamSpec **pspecs);
188 /* --- structures --- */
189 struct _GObjectNotifyContext
191 GQuark quark_notify_queue;
192 GObjectNotifyQueueDispatcher dispatcher;
194 struct _GObjectNotifyQueue
196 GObjectNotifyContext *context;
199 guint16 freeze_count;
202 /* --- variables --- */
203 G_LOCK_DEFINE_STATIC (closure_array_mutex);
204 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
205 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
206 static GQuark quark_closure_array = 0;
207 static GQuark quark_weak_refs = 0;
208 static GQuark quark_toggle_refs = 0;
209 static GParamSpecPool *pspec_pool = NULL;
210 static GObjectNotifyContext property_notify_context = { 0, };
211 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
212 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
213 G_LOCK_DEFINE_STATIC (construction_mutex);
214 static GSList *construction_objects = NULL;
216 /* --- functions --- */
217 #ifdef G_ENABLE_DEBUG
218 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
219 G_LOCK_DEFINE_STATIC (debug_objects);
220 static volatile GObject *g_trap_object_ref = NULL;
221 static guint debug_objects_count = 0;
222 static GHashTable *debug_objects_ht = NULL;
224 G_LOCK_DEFINE_STATIC(notify_lock);
226 /* --- functions --- */
228 g_object_notify_queue_free (gpointer data)
230 GObjectNotifyQueue *nqueue = data;
232 g_slist_free (nqueue->pspecs);
233 g_slice_free (GObjectNotifyQueue, nqueue);
236 static inline GObjectNotifyQueue*
237 g_object_notify_queue_freeze (GObject *object,
238 GObjectNotifyContext *context)
240 GObjectNotifyQueue *nqueue;
243 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
246 nqueue = g_slice_new0 (GObjectNotifyQueue);
247 nqueue->context = context;
248 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
249 nqueue, g_object_notify_queue_free);
252 if (nqueue->freeze_count >= 65535)
253 g_critical("Free queue for %s (%p) is larger than 65535,"
254 " called g_object_freeze_notify() too often."
255 " Forgot to call g_object_thaw_notify() or infinite loop",
256 G_OBJECT_TYPE_NAME (object), object);
258 nqueue->freeze_count++;
259 G_UNLOCK(notify_lock);
265 g_object_notify_queue_thaw (GObject *object,
266 GObjectNotifyQueue *nqueue)
268 GObjectNotifyContext *context = nqueue->context;
269 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
273 g_return_if_fail (nqueue->freeze_count > 0);
274 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
278 /* Just make sure we never get into some nasty race condition */
279 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
280 G_UNLOCK(notify_lock);
281 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
282 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
286 nqueue->freeze_count--;
287 if (nqueue->freeze_count) {
288 G_UNLOCK(notify_lock);
292 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
294 for (slist = nqueue->pspecs; slist; slist = slist->next)
296 pspecs[n_pspecs++] = slist->data;
298 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
300 G_UNLOCK(notify_lock);
303 context->dispatcher (object, n_pspecs, pspecs);
308 g_object_notify_queue_add (GObject *object,
309 GObjectNotifyQueue *nqueue,
312 if (pspec->flags & G_PARAM_READABLE)
314 GParamSpec *redirect;
316 redirect = g_param_spec_get_redirect_target (pspec);
322 g_return_if_fail (nqueue->n_pspecs < 65535);
324 /* we do the deduping in _thaw */
325 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
327 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
331 G_UNLOCK(notify_lock);
336 debug_objects_foreach (gpointer key,
340 GObject *object = value;
342 g_message ("[%p] stale %s\tref_count=%u",
344 G_OBJECT_TYPE_NAME (object),
349 debug_objects_atexit (void)
353 G_LOCK (debug_objects);
354 g_message ("stale GObjects: %u", debug_objects_count);
355 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
356 G_UNLOCK (debug_objects);
359 #endif /* G_ENABLE_DEBUG */
362 _g_object_type_init (void)
364 static gboolean initialized = FALSE;
365 static const GTypeFundamentalInfo finfo = {
366 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
368 static GTypeInfo info = {
369 sizeof (GObjectClass),
370 (GBaseInitFunc) g_object_base_class_init,
371 (GBaseFinalizeFunc) g_object_base_class_finalize,
372 (GClassInitFunc) g_object_do_class_init,
373 NULL /* class_destroy */,
374 NULL /* class_data */,
377 (GInstanceInitFunc) g_object_init,
378 NULL, /* value_table */
380 static const GTypeValueTable value_table = {
381 g_value_object_init, /* value_init */
382 g_value_object_free_value, /* value_free */
383 g_value_object_copy_value, /* value_copy */
384 g_value_object_peek_pointer, /* value_peek_pointer */
385 "p", /* collect_format */
386 g_value_object_collect_value, /* collect_value */
387 "p", /* lcopy_format */
388 g_value_object_lcopy_value, /* lcopy_value */
392 g_return_if_fail (initialized == FALSE);
397 info.value_table = &value_table;
398 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
399 g_assert (type == G_TYPE_OBJECT);
400 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
402 #ifdef G_ENABLE_DEBUG
405 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
406 g_atexit (debug_objects_atexit);
408 #endif /* G_ENABLE_DEBUG */
412 g_object_base_class_init (GObjectClass *class)
414 GObjectClass *pclass = g_type_class_peek_parent (class);
416 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
417 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
420 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
422 /* reset instance specific fields and methods that don't get inherited */
423 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
424 class->get_property = NULL;
425 class->set_property = NULL;
429 g_object_base_class_finalize (GObjectClass *class)
433 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
435 g_slist_free (class->construct_properties);
436 class->construct_properties = NULL;
437 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
438 for (node = list; node; node = node->next)
440 GParamSpec *pspec = node->data;
442 g_param_spec_pool_remove (pspec_pool, pspec);
443 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
444 g_param_spec_unref (pspec);
450 g_object_notify_dispatcher (GObject *object,
454 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
458 g_object_do_class_init (GObjectClass *class)
460 /* read the comment about typedef struct CArray; on why not to change this quark */
461 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
463 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
464 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
465 pspec_pool = g_param_spec_pool_new (TRUE);
466 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
467 property_notify_context.dispatcher = g_object_notify_dispatcher;
469 class->constructor = g_object_constructor;
470 class->constructed = g_object_constructed;
471 class->set_property = g_object_do_set_property;
472 class->get_property = g_object_do_get_property;
473 class->dispose = g_object_real_dispose;
474 class->finalize = g_object_finalize;
475 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
476 class->notify = NULL;
480 * @gobject: the object which received the signal.
481 * @pspec: the #GParamSpec of the property which changed.
483 * The notify signal is emitted on an object when one of its
484 * properties has been changed. Note that getting this signal
485 * doesn't guarantee that the value of the property has actually
486 * changed, it may also be emitted when the setter for the property
487 * is called to reinstate the previous value.
489 * This signal is typically used to obtain change notification for a
490 * single property, by specifying the property name as a detail in the
491 * g_signal_connect() call, like this:
493 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
494 * G_CALLBACK (gtk_text_view_target_list_notify),
497 * It is important to note that you must use
498 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
499 * detail strings for the notify signal.
501 gobject_signals[NOTIFY] =
502 g_signal_new (g_intern_static_string ("notify"),
503 G_TYPE_FROM_CLASS (class),
504 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
505 G_STRUCT_OFFSET (GObjectClass, notify),
507 g_cclosure_marshal_VOID__PARAM,
511 /* Install a check function that we'll use to verify that classes that
512 * implement an interface implement all properties for that interface
514 g_type_add_interface_check (NULL, object_interface_check_properties);
518 install_property_internal (GType g_type,
522 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
524 g_warning ("When installing property: type `%s' already has a property named `%s'",
525 g_type_name (g_type),
530 g_param_spec_ref_sink (pspec);
531 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
532 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
536 * g_object_class_install_property:
537 * @oclass: a #GObjectClass
538 * @property_id: the id for the new property
539 * @pspec: the #GParamSpec for the new property
541 * Installs a new property. This is usually done in the class initializer.
543 * Note that it is possible to redefine a property in a derived class,
544 * by installing a property with the same name. This can be useful at times,
545 * e.g. to change the range of allowed values or the default value.
548 g_object_class_install_property (GObjectClass *class,
552 g_return_if_fail (G_IS_OBJECT_CLASS (class));
553 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
555 if (CLASS_HAS_DERIVED_CLASS (class))
556 g_error ("Attempt to add property %s::%s to class after it was derived",
557 G_OBJECT_CLASS_NAME (class), pspec->name);
559 class->flags |= CLASS_HAS_PROPS_FLAG;
561 if (pspec->flags & G_PARAM_WRITABLE)
562 g_return_if_fail (class->set_property != NULL);
563 if (pspec->flags & G_PARAM_READABLE)
564 g_return_if_fail (class->get_property != NULL);
565 g_return_if_fail (property_id > 0);
566 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
567 if (pspec->flags & G_PARAM_CONSTRUCT)
568 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
569 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
570 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
572 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
574 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
575 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
577 /* for property overrides of construct properties, we have to get rid
578 * of the overidden inherited construct property
580 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
581 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
582 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
586 * g_object_class_install_properties:
587 * @oclass: a #GObjectClass
588 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
589 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
590 * defining the new properties
592 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
593 * usually done in the class initializer.
595 * The property id of each property is the index of each #GParamSpec in
598 * The property id of 0 is treated specially by #GObject and it should not
599 * be used to store a #GParamSpec.
601 * This function should be used if you plan to use a static array of
602 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
603 * class initialization:
607 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
610 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
613 * my_object_class_init (MyObjectClass *klass)
615 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
617 * obj_properties[PROP_FOO] =
618 * g_param_spec_int ("foo", "Foo", "Foo",
621 * G_PARAM_READWRITE);
623 * obj_properties[PROP_BAR] =
624 * g_param_spec_string ("bar", "Bar", "Bar",
626 * G_PARAM_READWRITE);
628 * gobject_class->set_property = my_object_set_property;
629 * gobject_class->get_property = my_object_get_property;
630 * g_object_class_install_properties (gobject_class,
636 * allows calling g_object_notify_by_pspec() to notify of property changes:
640 * my_object_set_foo (MyObject *self, gint foo)
642 * if (self->foo != foo)
645 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
653 g_object_class_install_properties (GObjectClass *oclass,
657 GType oclass_type, parent_type;
660 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
661 g_return_if_fail (n_pspecs > 1);
662 g_return_if_fail (pspecs[0] == NULL);
664 if (CLASS_HAS_DERIVED_CLASS (oclass))
665 g_error ("Attempt to add properties to %s after it was derived",
666 G_OBJECT_CLASS_NAME (oclass));
668 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
669 parent_type = g_type_parent (oclass_type);
671 /* we skip the first element of the array as it would have a 0 prop_id */
672 for (i = 1; i < n_pspecs; i++)
674 GParamSpec *pspec = pspecs[i];
676 g_return_if_fail (pspec != NULL);
678 if (pspec->flags & G_PARAM_WRITABLE)
679 g_return_if_fail (oclass->set_property != NULL);
680 if (pspec->flags & G_PARAM_READABLE)
681 g_return_if_fail (oclass->get_property != NULL);
682 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
683 if (pspec->flags & G_PARAM_CONSTRUCT)
684 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
685 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
686 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
688 oclass->flags |= CLASS_HAS_PROPS_FLAG;
689 install_property_internal (oclass_type, i, pspec);
691 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
692 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
694 /* for property overrides of construct properties, we have to get rid
695 * of the overidden inherited construct property
697 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
698 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
699 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
704 * g_object_interface_install_property:
705 * @g_iface: any interface vtable for the interface, or the default
706 * vtable for the interface.
707 * @pspec: the #GParamSpec for the new property
709 * Add a property to an interface; this is only useful for interfaces
710 * that are added to GObject-derived types. Adding a property to an
711 * interface forces all objects classes with that interface to have a
712 * compatible property. The compatible property could be a newly
713 * created #GParamSpec, but normally
714 * g_object_class_override_property() will be used so that the object
715 * class only needs to provide an implementation and inherits the
716 * property description, default value, bounds, and so forth from the
717 * interface property.
719 * This function is meant to be called from the interface's default
720 * vtable initialization function (the @class_init member of
721 * #GTypeInfo.) It must not be called after after @class_init has
722 * been called for any object types implementing this interface.
727 g_object_interface_install_property (gpointer g_iface,
730 GTypeInterface *iface_class = g_iface;
732 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
733 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
734 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
735 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
737 install_property_internal (iface_class->g_type, 0, pspec);
741 * g_object_class_find_property:
742 * @oclass: a #GObjectClass
743 * @property_name: the name of the property to look up
745 * Looks up the #GParamSpec for a property of a class.
747 * Returns: (transfer none): the #GParamSpec for the property, or
748 * %NULL if the class doesn't have a property of that name
751 g_object_class_find_property (GObjectClass *class,
752 const gchar *property_name)
755 GParamSpec *redirect;
757 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
758 g_return_val_if_fail (property_name != NULL, NULL);
760 pspec = g_param_spec_pool_lookup (pspec_pool,
762 G_OBJECT_CLASS_TYPE (class),
766 redirect = g_param_spec_get_redirect_target (pspec);
777 * g_object_interface_find_property:
778 * @g_iface: any interface vtable for the interface, or the default
779 * vtable for the interface
780 * @property_name: name of a property to lookup.
782 * Find the #GParamSpec with the given name for an
783 * interface. Generally, the interface vtable passed in as @g_iface
784 * will be the default vtable from g_type_default_interface_ref(), or,
785 * if you know the interface has already been loaded,
786 * g_type_default_interface_peek().
790 * Returns: (transfer none): the #GParamSpec for the property of the
791 * interface with the name @property_name, or %NULL if no
792 * such property exists.
795 g_object_interface_find_property (gpointer g_iface,
796 const gchar *property_name)
798 GTypeInterface *iface_class = g_iface;
800 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
801 g_return_val_if_fail (property_name != NULL, NULL);
803 return g_param_spec_pool_lookup (pspec_pool,
810 * g_object_class_override_property:
811 * @oclass: a #GObjectClass
812 * @property_id: the new property ID
813 * @name: the name of a property registered in a parent class or
814 * in an interface of this class.
816 * Registers @property_id as referring to a property with the
817 * name @name in a parent class or in an interface implemented
818 * by @oclass. This allows this class to <firstterm>override</firstterm>
819 * a property implementation in a parent class or to provide
820 * the implementation of a property from an interface.
823 * Internally, overriding is implemented by creating a property of type
824 * #GParamSpecOverride; generally operations that query the properties of
825 * the object class, such as g_object_class_find_property() or
826 * g_object_class_list_properties() will return the overridden
827 * property. However, in one case, the @construct_properties argument of
828 * the @constructor virtual function, the #GParamSpecOverride is passed
829 * instead, so that the @param_id field of the #GParamSpec will be
830 * correct. For virtually all uses, this makes no difference. If you
831 * need to get the overridden property, you can call
832 * g_param_spec_get_redirect_target().
838 g_object_class_override_property (GObjectClass *oclass,
842 GParamSpec *overridden = NULL;
846 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
847 g_return_if_fail (property_id > 0);
848 g_return_if_fail (name != NULL);
850 /* Find the overridden property; first check parent types
852 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
853 if (parent_type != G_TYPE_NONE)
854 overridden = g_param_spec_pool_lookup (pspec_pool,
863 /* Now check interfaces
865 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
866 while (n_ifaces-- && !overridden)
868 overridden = g_param_spec_pool_lookup (pspec_pool,
879 g_warning ("%s: Can't find property to override for '%s::%s'",
880 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
884 new = g_param_spec_override (name, overridden);
885 g_object_class_install_property (oclass, property_id, new);
889 * g_object_class_list_properties:
890 * @oclass: a #GObjectClass
891 * @n_properties: (out): return location for the length of the returned array
893 * Get an array of #GParamSpec* for all properties of a class.
895 * Returns: (array length=n_properties) (transfer container): an array of
896 * #GParamSpec* which should be freed after use
898 GParamSpec** /* free result */
899 g_object_class_list_properties (GObjectClass *class,
900 guint *n_properties_p)
905 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
907 pspecs = g_param_spec_pool_list (pspec_pool,
908 G_OBJECT_CLASS_TYPE (class),
917 * g_object_interface_list_properties:
918 * @g_iface: any interface vtable for the interface, or the default
919 * vtable for the interface
920 * @n_properties_p: (out): location to store number of properties returned.
922 * Lists the properties of an interface.Generally, the interface
923 * vtable passed in as @g_iface will be the default vtable from
924 * g_type_default_interface_ref(), or, if you know the interface has
925 * already been loaded, g_type_default_interface_peek().
929 * Returns: (array length=n_properties_p) (transfer container): a
930 * pointer to an array of pointers to #GParamSpec
931 * structures. The paramspecs are owned by GLib, but the
932 * array should be freed with g_free() when you are done with
936 g_object_interface_list_properties (gpointer g_iface,
937 guint *n_properties_p)
939 GTypeInterface *iface_class = g_iface;
943 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
945 pspecs = g_param_spec_pool_list (pspec_pool,
955 g_object_init (GObject *object,
958 object->ref_count = 1;
959 object->qdata = NULL;
961 if (CLASS_HAS_PROPS (class))
963 /* freeze object's notification queue, g_object_newv() preserves pairedness */
964 g_object_notify_queue_freeze (object, &property_notify_context);
967 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
969 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
970 G_LOCK (construction_mutex);
971 construction_objects = g_slist_prepend (construction_objects, object);
972 G_UNLOCK (construction_mutex);
975 #ifdef G_ENABLE_DEBUG
978 G_LOCK (debug_objects);
979 debug_objects_count++;
980 g_hash_table_insert (debug_objects_ht, object, object);
981 G_UNLOCK (debug_objects);
983 #endif /* G_ENABLE_DEBUG */
987 g_object_do_set_property (GObject *object,
995 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1001 g_object_do_get_property (GObject *object,
1006 switch (property_id)
1009 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1015 g_object_real_dispose (GObject *object)
1017 g_signal_handlers_destroy (object);
1018 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1019 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1023 g_object_finalize (GObject *object)
1025 g_datalist_clear (&object->qdata);
1027 #ifdef G_ENABLE_DEBUG
1030 G_LOCK (debug_objects);
1031 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1032 g_hash_table_remove (debug_objects_ht, object);
1033 debug_objects_count--;
1034 G_UNLOCK (debug_objects);
1036 #endif /* G_ENABLE_DEBUG */
1041 g_object_dispatch_properties_changed (GObject *object,
1043 GParamSpec **pspecs)
1047 for (i = 0; i < n_pspecs; i++)
1048 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1052 * g_object_run_dispose:
1053 * @object: a #GObject
1055 * Releases all references to other objects. This can be used to break
1058 * This functions should only be called from object system implementations.
1061 g_object_run_dispose (GObject *object)
1063 g_return_if_fail (G_IS_OBJECT (object));
1064 g_return_if_fail (object->ref_count > 0);
1066 g_object_ref (object);
1067 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1068 G_OBJECT_GET_CLASS (object)->dispose (object);
1069 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1070 g_object_unref (object);
1074 * g_object_freeze_notify:
1075 * @object: a #GObject
1077 * Increases the freeze count on @object. If the freeze count is
1078 * non-zero, the emission of "notify" signals on @object is
1079 * stopped. The signals are queued until the freeze count is decreased
1082 * This is necessary for accessors that modify multiple properties to prevent
1083 * premature notification while the object is still being modified.
1086 g_object_freeze_notify (GObject *object)
1088 g_return_if_fail (G_IS_OBJECT (object));
1090 if (g_atomic_int_get (&object->ref_count) == 0)
1093 g_object_ref (object);
1094 g_object_notify_queue_freeze (object, &property_notify_context);
1095 g_object_unref (object);
1099 g_object_notify_by_spec_internal (GObject *object,
1102 GObjectNotifyQueue *nqueue;
1104 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1105 g_object_notify_queue_add (object, nqueue, pspec);
1106 g_object_notify_queue_thaw (object, nqueue);
1111 * @object: a #GObject
1112 * @property_name: the name of a property installed on the class of @object.
1114 * Emits a "notify" signal for the property @property_name on @object.
1116 * When possible, eg. when signaling a property change from within the class
1117 * that registered the property, you should use g_object_notify_by_pspec()
1121 g_object_notify (GObject *object,
1122 const gchar *property_name)
1126 g_return_if_fail (G_IS_OBJECT (object));
1127 g_return_if_fail (property_name != NULL);
1128 if (g_atomic_int_get (&object->ref_count) == 0)
1131 g_object_ref (object);
1132 /* We don't need to get the redirect target
1133 * (by, e.g. calling g_object_class_find_property())
1134 * because g_object_notify_queue_add() does that
1136 pspec = g_param_spec_pool_lookup (pspec_pool,
1138 G_OBJECT_TYPE (object),
1142 g_warning ("%s: object class `%s' has no property named `%s'",
1144 G_OBJECT_TYPE_NAME (object),
1147 g_object_notify_by_spec_internal (object, pspec);
1148 g_object_unref (object);
1152 * g_object_notify_by_pspec:
1153 * @object: a #GObject
1154 * @pspec: the #GParamSpec of a property installed on the class of @object.
1156 * Emits a "notify" signal for the property specified by @pspec on @object.
1158 * This function omits the property name lookup, hence it is faster than
1159 * g_object_notify().
1161 * One way to avoid using g_object_notify() from within the
1162 * class that registered the properties, and using g_object_notify_by_pspec()
1163 * instead, is to store the GParamSpec used with
1164 * g_object_class_install_property() inside a static array, e.g.:
1174 * static GParamSpec *properties[PROP_LAST];
1177 * my_object_class_init (MyObjectClass *klass)
1179 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1182 * G_PARAM_READWRITE);
1183 * g_object_class_install_property (gobject_class,
1185 * properties[PROP_FOO]);
1189 * and then notify a change on the "foo" property with:
1192 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1198 g_object_notify_by_pspec (GObject *object,
1202 g_return_if_fail (G_IS_OBJECT (object));
1203 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1205 g_object_ref (object);
1206 g_object_notify_by_spec_internal (object, pspec);
1207 g_object_unref (object);
1211 * g_object_thaw_notify:
1212 * @object: a #GObject
1214 * Reverts the effect of a previous call to
1215 * g_object_freeze_notify(). The freeze count is decreased on @object
1216 * and when it reaches zero, all queued "notify" signals are emitted.
1218 * It is an error to call this function when the freeze count is zero.
1221 g_object_thaw_notify (GObject *object)
1223 GObjectNotifyQueue *nqueue;
1225 g_return_if_fail (G_IS_OBJECT (object));
1226 if (g_atomic_int_get (&object->ref_count) == 0)
1229 g_object_ref (object);
1231 /* FIXME: Freezing is the only way to get at the notify queue.
1232 * So we freeze once and then thaw twice.
1234 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1235 g_object_notify_queue_thaw (object, nqueue);
1236 g_object_notify_queue_thaw (object, nqueue);
1238 g_object_unref (object);
1242 object_get_property (GObject *object,
1246 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1247 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1248 GParamSpec *redirect;
1252 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1253 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1257 redirect = g_param_spec_get_redirect_target (pspec);
1261 class->get_property (object, param_id, value, pspec);
1265 object_set_property (GObject *object,
1267 const GValue *value,
1268 GObjectNotifyQueue *nqueue)
1270 GValue tmp_value = G_VALUE_INIT;
1271 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1272 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1273 GParamSpec *redirect;
1274 static const gchar * enable_diagnostic = NULL;
1278 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1279 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1283 redirect = g_param_spec_get_redirect_target (pspec);
1287 if (G_UNLIKELY (!enable_diagnostic))
1289 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1290 if (!enable_diagnostic)
1291 enable_diagnostic = "0";
1294 if (enable_diagnostic[0] == '1')
1296 if (pspec->flags & G_PARAM_DEPRECATED)
1297 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1298 "anymore. It will be removed in a future version.",
1299 G_OBJECT_TYPE_NAME (object), pspec->name);
1302 /* provide a copy to work from, convert (if necessary) and validate */
1303 g_value_init (&tmp_value, pspec->value_type);
1304 if (!g_value_transform (value, &tmp_value))
1305 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1307 g_type_name (pspec->value_type),
1308 G_VALUE_TYPE_NAME (value));
1309 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1311 gchar *contents = g_strdup_value_contents (value);
1313 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1315 G_VALUE_TYPE_NAME (value),
1317 g_type_name (pspec->value_type));
1322 class->set_property (object, param_id, &tmp_value, pspec);
1323 g_object_notify_queue_add (object, nqueue, pspec);
1325 g_value_unset (&tmp_value);
1329 object_interface_check_properties (gpointer func_data,
1332 GTypeInterface *iface_class = g_iface;
1333 GObjectClass *class;
1334 GType iface_type = iface_class->g_type;
1335 GParamSpec **pspecs;
1338 class = g_type_class_ref (iface_class->g_instance_type);
1340 if (!G_IS_OBJECT_CLASS (class))
1343 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1347 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1349 G_OBJECT_CLASS_TYPE (class),
1354 g_critical ("Object class %s doesn't implement property "
1355 "'%s' from interface '%s'",
1356 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1358 g_type_name (iface_type));
1363 /* The implementation paramspec must have a less restrictive
1364 * type than the interface parameter spec for set() and a
1365 * more restrictive type for get(). We just require equality,
1366 * rather than doing something more complicated checking
1367 * the READABLE and WRITABLE flags. We also simplify here
1368 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1371 !g_type_is_a (pspecs[n]->value_type,
1372 class_pspec->value_type))
1374 g_critical ("Property '%s' on class '%s' has type '%s' "
1375 "which is different from the type '%s', "
1376 "of the property on interface '%s'\n",
1378 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1379 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1380 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1381 g_type_name (iface_type));
1384 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1386 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1387 * READABLE and WRITABLE remove restrictions. The implementation
1388 * paramspec must have less restrictive flags.
1391 (!SUBSET (class_pspec->flags,
1393 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1394 !SUBSET (pspecs[n]->flags,
1396 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1398 g_critical ("Flags for property '%s' on class '%s' "
1399 "are not compatible with the property on"
1402 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1403 g_type_name (iface_type));
1410 g_type_class_unref (class);
1414 g_object_get_type (void)
1416 return G_TYPE_OBJECT;
1420 * g_object_new: (skip)
1421 * @object_type: the type id of the #GObject subtype to instantiate
1422 * @first_property_name: the name of the first property
1423 * @...: the value of the first property, followed optionally by more
1424 * name/value pairs, followed by %NULL
1426 * Creates a new instance of a #GObject subtype and sets its properties.
1428 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1429 * which are not explicitly specified are set to their default values.
1431 * Returns: (transfer full): a new instance of @object_type
1434 g_object_new (GType object_type,
1435 const gchar *first_property_name,
1441 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1443 /* short circuit for calls supplying no properties */
1444 if (!first_property_name)
1445 return g_object_newv (object_type, 0, NULL);
1447 va_start (var_args, first_property_name);
1448 object = g_object_new_valist (object_type, first_property_name, var_args);
1455 slist_maybe_remove (GSList **slist,
1458 GSList *last = NULL, *node = *slist;
1461 if (node->data == data)
1464 last->next = node->next;
1466 *slist = node->next;
1467 g_slist_free_1 (node);
1476 static inline gboolean
1477 object_in_construction_list (GObject *object)
1479 gboolean in_construction;
1480 G_LOCK (construction_mutex);
1481 in_construction = g_slist_find (construction_objects, object) != NULL;
1482 G_UNLOCK (construction_mutex);
1483 return in_construction;
1488 * @object_type: the type id of the #GObject subtype to instantiate
1489 * @n_parameters: the length of the @parameters array
1490 * @parameters: (array length=n_parameters): an array of #GParameter
1492 * Creates a new instance of a #GObject subtype and sets its properties.
1494 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1495 * which are not explicitly specified are set to their default values.
1497 * Rename to: g_object_new
1498 * Returns: (type GObject.Object) (transfer full): a new instance of
1502 g_object_newv (GType object_type,
1504 GParameter *parameters)
1506 GObjectConstructParam *cparams = NULL, *oparams;
1507 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1509 GObjectClass *class, *unref_class = NULL;
1511 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1513 GList *clist = NULL;
1514 gboolean newly_constructed;
1517 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1519 class = g_type_class_peek_static (object_type);
1521 class = unref_class = g_type_class_ref (object_type);
1522 for (slist = class->construct_properties; slist; slist = slist->next)
1524 clist = g_list_prepend (clist, slist->data);
1525 n_total_cparams += 1;
1528 if (n_parameters == 0 && n_total_cparams == 0)
1530 /* This is a simple object with no construct properties, and
1531 * no properties are being set, so short circuit the parameter
1532 * handling. This speeds up simple object construction.
1535 object = class->constructor (object_type, 0, NULL);
1536 goto did_construction;
1539 /* collect parameters, sort into construction and normal ones */
1540 oparams = g_new (GObjectConstructParam, n_parameters);
1541 cparams = g_new (GObjectConstructParam, n_total_cparams);
1542 for (i = 0; i < n_parameters; i++)
1544 GValue *value = ¶meters[i].value;
1545 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1551 g_warning ("%s: object class `%s' has no property named `%s'",
1553 g_type_name (object_type),
1554 parameters[i].name);
1557 if (!(pspec->flags & G_PARAM_WRITABLE))
1559 g_warning ("%s: property `%s' of object class `%s' is not writable",
1562 g_type_name (object_type));
1565 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1567 GList *list = g_list_find (clist, pspec);
1571 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1572 G_STRFUNC, pspec->name, g_type_name (object_type));
1575 cparams[n_cparams].pspec = pspec;
1576 cparams[n_cparams].value = value;
1581 list->prev->next = list->next;
1583 list->next->prev = list->prev;
1584 g_list_free_1 (list);
1588 oparams[n_oparams].pspec = pspec;
1589 oparams[n_oparams].value = value;
1594 /* set remaining construction properties to default values */
1595 n_cvalues = n_total_cparams - n_cparams;
1596 cvalues = g_new (GValue, n_cvalues);
1599 GList *tmp = clist->next;
1600 GParamSpec *pspec = clist->data;
1601 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1604 g_value_init (value, pspec->value_type);
1605 g_param_value_set_default (pspec, value);
1607 cparams[n_cparams].pspec = pspec;
1608 cparams[n_cparams].value = value;
1611 g_list_free_1 (clist);
1615 /* construct object from construction parameters */
1616 object = class->constructor (object_type, n_total_cparams, cparams);
1617 /* free construction values */
1620 g_value_unset (cvalues + n_cvalues);
1624 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1626 /* adjust freeze_count according to g_object_init() and remaining properties */
1627 G_LOCK (construction_mutex);
1628 newly_constructed = slist_maybe_remove (&construction_objects, object);
1629 G_UNLOCK (construction_mutex);
1632 newly_constructed = TRUE;
1634 if (CLASS_HAS_PROPS (class))
1636 if (newly_constructed || n_oparams)
1637 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1638 if (newly_constructed)
1639 g_object_notify_queue_thaw (object, nqueue);
1642 /* run 'constructed' handler if there is a custom one */
1643 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1644 class->constructed (object);
1646 /* set remaining properties */
1647 for (i = 0; i < n_oparams; i++)
1648 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1651 if (CLASS_HAS_PROPS (class))
1653 /* release our own freeze count and handle notifications */
1654 if (newly_constructed || n_oparams)
1655 g_object_notify_queue_thaw (object, nqueue);
1659 g_type_class_unref (unref_class);
1665 * g_object_new_valist: (skip)
1666 * @object_type: the type id of the #GObject subtype to instantiate
1667 * @first_property_name: the name of the first property
1668 * @var_args: the value of the first property, followed optionally by more
1669 * name/value pairs, followed by %NULL
1671 * Creates a new instance of a #GObject subtype and sets its properties.
1673 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1674 * which are not explicitly specified are set to their default values.
1676 * Returns: a new instance of @object_type
1679 g_object_new_valist (GType object_type,
1680 const gchar *first_property_name,
1683 GObjectClass *class;
1687 guint n_params = 0, n_alloced_params = 16;
1689 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1691 if (!first_property_name)
1692 return g_object_newv (object_type, 0, NULL);
1694 class = g_type_class_ref (object_type);
1696 params = g_new0 (GParameter, n_alloced_params);
1697 name = first_property_name;
1700 gchar *error = NULL;
1701 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1707 g_warning ("%s: object class `%s' has no property named `%s'",
1709 g_type_name (object_type),
1713 if (n_params >= n_alloced_params)
1715 n_alloced_params += 16;
1716 params = g_renew (GParameter, params, n_alloced_params);
1717 memset (params + n_params, 0, 16 * (sizeof *params));
1719 params[n_params].name = name;
1720 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1721 var_args, 0, &error);
1724 g_warning ("%s: %s", G_STRFUNC, error);
1726 g_value_unset (¶ms[n_params].value);
1730 name = va_arg (var_args, gchar*);
1733 object = g_object_newv (object_type, n_params, params);
1736 g_value_unset (¶ms[n_params].value);
1739 g_type_class_unref (class);
1745 g_object_constructor (GType type,
1746 guint n_construct_properties,
1747 GObjectConstructParam *construct_params)
1752 object = (GObject*) g_type_create_instance (type);
1754 /* set construction parameters */
1755 if (n_construct_properties)
1757 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1759 /* set construct properties */
1760 while (n_construct_properties--)
1762 GValue *value = construct_params->value;
1763 GParamSpec *pspec = construct_params->pspec;
1766 object_set_property (object, pspec, value, nqueue);
1768 g_object_notify_queue_thaw (object, nqueue);
1769 /* the notification queue is still frozen from g_object_init(), so
1770 * we don't need to handle it here, g_object_newv() takes
1779 g_object_constructed (GObject *object)
1781 /* empty default impl to allow unconditional upchaining */
1785 * g_object_set_valist: (skip)
1786 * @object: a #GObject
1787 * @first_property_name: name of the first property to set
1788 * @var_args: value for the first property, followed optionally by more
1789 * name/value pairs, followed by %NULL
1791 * Sets properties on an object.
1794 g_object_set_valist (GObject *object,
1795 const gchar *first_property_name,
1798 GObjectNotifyQueue *nqueue;
1801 g_return_if_fail (G_IS_OBJECT (object));
1803 g_object_ref (object);
1804 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1806 name = first_property_name;
1809 GValue value = G_VALUE_INIT;
1811 gchar *error = NULL;
1813 pspec = g_param_spec_pool_lookup (pspec_pool,
1815 G_OBJECT_TYPE (object),
1819 g_warning ("%s: object class `%s' has no property named `%s'",
1821 G_OBJECT_TYPE_NAME (object),
1825 if (!(pspec->flags & G_PARAM_WRITABLE))
1827 g_warning ("%s: property `%s' of object class `%s' is not writable",
1830 G_OBJECT_TYPE_NAME (object));
1833 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1835 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1836 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1840 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1844 g_warning ("%s: %s", G_STRFUNC, error);
1846 g_value_unset (&value);
1850 object_set_property (object, pspec, &value, nqueue);
1851 g_value_unset (&value);
1853 name = va_arg (var_args, gchar*);
1856 g_object_notify_queue_thaw (object, nqueue);
1857 g_object_unref (object);
1861 * g_object_get_valist: (skip)
1862 * @object: a #GObject
1863 * @first_property_name: name of the first property to get
1864 * @var_args: return location for the first property, followed optionally by more
1865 * name/return location pairs, followed by %NULL
1867 * Gets properties of an object.
1869 * In general, a copy is made of the property contents and the caller
1870 * is responsible for freeing the memory in the appropriate manner for
1871 * the type, for instance by calling g_free() or g_object_unref().
1873 * See g_object_get().
1876 g_object_get_valist (GObject *object,
1877 const gchar *first_property_name,
1882 g_return_if_fail (G_IS_OBJECT (object));
1884 g_object_ref (object);
1886 name = first_property_name;
1890 GValue value = G_VALUE_INIT;
1894 pspec = g_param_spec_pool_lookup (pspec_pool,
1896 G_OBJECT_TYPE (object),
1900 g_warning ("%s: object class `%s' has no property named `%s'",
1902 G_OBJECT_TYPE_NAME (object),
1906 if (!(pspec->flags & G_PARAM_READABLE))
1908 g_warning ("%s: property `%s' of object class `%s' is not readable",
1911 G_OBJECT_TYPE_NAME (object));
1915 g_value_init (&value, pspec->value_type);
1917 object_get_property (object, pspec, &value);
1919 G_VALUE_LCOPY (&value, var_args, 0, &error);
1922 g_warning ("%s: %s", G_STRFUNC, error);
1924 g_value_unset (&value);
1928 g_value_unset (&value);
1930 name = va_arg (var_args, gchar*);
1933 g_object_unref (object);
1937 * g_object_set: (skip)
1938 * @object: a #GObject
1939 * @first_property_name: name of the first property to set
1940 * @...: value for the first property, followed optionally by more
1941 * name/value pairs, followed by %NULL
1943 * Sets properties on an object.
1946 g_object_set (gpointer _object,
1947 const gchar *first_property_name,
1950 GObject *object = _object;
1953 g_return_if_fail (G_IS_OBJECT (object));
1955 va_start (var_args, first_property_name);
1956 g_object_set_valist (object, first_property_name, var_args);
1961 * g_object_get: (skip)
1962 * @object: a #GObject
1963 * @first_property_name: name of the first property to get
1964 * @...: return location for the first property, followed optionally by more
1965 * name/return location pairs, followed by %NULL
1967 * Gets properties of an object.
1969 * In general, a copy is made of the property contents and the caller
1970 * is responsible for freeing the memory in the appropriate manner for
1971 * the type, for instance by calling g_free() or g_object_unref().
1974 * <title>Using g_object_get(<!-- -->)</title>
1975 * An example of using g_object_get() to get the contents
1976 * of three properties - one of type #G_TYPE_INT,
1977 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1983 * g_object_get (my_object,
1984 * "int-property", &intval,
1985 * "str-property", &strval,
1986 * "obj-property", &objval,
1989 * // Do something with intval, strval, objval
1992 * g_object_unref (objval);
1997 g_object_get (gpointer _object,
1998 const gchar *first_property_name,
2001 GObject *object = _object;
2004 g_return_if_fail (G_IS_OBJECT (object));
2006 va_start (var_args, first_property_name);
2007 g_object_get_valist (object, first_property_name, var_args);
2012 * g_object_set_property:
2013 * @object: a #GObject
2014 * @property_name: the name of the property to set
2017 * Sets a property on an object.
2020 g_object_set_property (GObject *object,
2021 const gchar *property_name,
2022 const GValue *value)
2024 GObjectNotifyQueue *nqueue;
2027 g_return_if_fail (G_IS_OBJECT (object));
2028 g_return_if_fail (property_name != NULL);
2029 g_return_if_fail (G_IS_VALUE (value));
2031 g_object_ref (object);
2032 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
2034 pspec = g_param_spec_pool_lookup (pspec_pool,
2036 G_OBJECT_TYPE (object),
2039 g_warning ("%s: object class `%s' has no property named `%s'",
2041 G_OBJECT_TYPE_NAME (object),
2043 else if (!(pspec->flags & G_PARAM_WRITABLE))
2044 g_warning ("%s: property `%s' of object class `%s' is not writable",
2047 G_OBJECT_TYPE_NAME (object));
2048 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2049 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2050 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2052 object_set_property (object, pspec, value, nqueue);
2054 g_object_notify_queue_thaw (object, nqueue);
2055 g_object_unref (object);
2059 * g_object_get_property:
2060 * @object: a #GObject
2061 * @property_name: the name of the property to get
2062 * @value: return location for the property value
2064 * Gets a property of an object. @value must have been initialized to the
2065 * expected type of the property (or a type to which the expected type can be
2066 * transformed) using g_value_init().
2068 * In general, a copy is made of the property contents and the caller is
2069 * responsible for freeing the memory by calling g_value_unset().
2071 * Note that g_object_get_property() is really intended for language
2072 * bindings, g_object_get() is much more convenient for C programming.
2075 g_object_get_property (GObject *object,
2076 const gchar *property_name,
2081 g_return_if_fail (G_IS_OBJECT (object));
2082 g_return_if_fail (property_name != NULL);
2083 g_return_if_fail (G_IS_VALUE (value));
2085 g_object_ref (object);
2087 pspec = g_param_spec_pool_lookup (pspec_pool,
2089 G_OBJECT_TYPE (object),
2092 g_warning ("%s: object class `%s' has no property named `%s'",
2094 G_OBJECT_TYPE_NAME (object),
2096 else if (!(pspec->flags & G_PARAM_READABLE))
2097 g_warning ("%s: property `%s' of object class `%s' is not readable",
2100 G_OBJECT_TYPE_NAME (object));
2103 GValue *prop_value, tmp_value = G_VALUE_INIT;
2105 /* auto-conversion of the callers value type
2107 if (G_VALUE_TYPE (value) == pspec->value_type)
2109 g_value_reset (value);
2112 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2114 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2115 G_STRFUNC, pspec->name,
2116 g_type_name (pspec->value_type),
2117 G_VALUE_TYPE_NAME (value));
2118 g_object_unref (object);
2123 g_value_init (&tmp_value, pspec->value_type);
2124 prop_value = &tmp_value;
2126 object_get_property (object, pspec, prop_value);
2127 if (prop_value != value)
2129 g_value_transform (prop_value, value);
2130 g_value_unset (&tmp_value);
2134 g_object_unref (object);
2138 * g_object_connect: (skip)
2139 * @object: a #GObject
2140 * @signal_spec: the spec for the first signal
2141 * @...: #GCallback for the first signal, followed by data for the
2142 * first signal, followed optionally by more signal
2143 * spec/callback/data triples, followed by %NULL
2145 * A convenience function to connect multiple signals at once.
2147 * The signal specs expected by this function have the form
2148 * "modifier::signal_name", where modifier can be one of the following:
2151 * <term>signal</term>
2153 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2154 * </para></listitem>
2157 * <term>object_signal</term>
2158 * <term>object-signal</term>
2160 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2161 * </para></listitem>
2164 * <term>swapped_signal</term>
2165 * <term>swapped-signal</term>
2167 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2168 * </para></listitem>
2171 * <term>swapped_object_signal</term>
2172 * <term>swapped-object-signal</term>
2174 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2175 * </para></listitem>
2178 * <term>signal_after</term>
2179 * <term>signal-after</term>
2181 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2182 * </para></listitem>
2185 * <term>object_signal_after</term>
2186 * <term>object-signal-after</term>
2188 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2189 * </para></listitem>
2192 * <term>swapped_signal_after</term>
2193 * <term>swapped-signal-after</term>
2195 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2196 * </para></listitem>
2199 * <term>swapped_object_signal_after</term>
2200 * <term>swapped-object-signal-after</term>
2202 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2203 * </para></listitem>
2208 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2209 * "type", GTK_WINDOW_POPUP,
2212 * "signal::event", gtk_menu_window_event, menu,
2213 * "signal::size_request", gtk_menu_window_size_request, menu,
2214 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2218 * Returns: (transfer none): @object
2221 g_object_connect (gpointer _object,
2222 const gchar *signal_spec,
2225 GObject *object = _object;
2228 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2229 g_return_val_if_fail (object->ref_count > 0, object);
2231 va_start (var_args, signal_spec);
2234 GCallback callback = va_arg (var_args, GCallback);
2235 gpointer data = va_arg (var_args, gpointer);
2237 if (strncmp (signal_spec, "signal::", 8) == 0)
2238 g_signal_connect_data (object, signal_spec + 8,
2239 callback, data, NULL,
2241 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2242 strncmp (signal_spec, "object-signal::", 15) == 0)
2243 g_signal_connect_object (object, signal_spec + 15,
2246 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2247 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2248 g_signal_connect_data (object, signal_spec + 16,
2249 callback, data, NULL,
2251 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2252 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2253 g_signal_connect_object (object, signal_spec + 23,
2256 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2257 strncmp (signal_spec, "signal-after::", 14) == 0)
2258 g_signal_connect_data (object, signal_spec + 14,
2259 callback, data, NULL,
2261 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2262 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2263 g_signal_connect_object (object, signal_spec + 21,
2266 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2267 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2268 g_signal_connect_data (object, signal_spec + 22,
2269 callback, data, NULL,
2270 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2271 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2272 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2273 g_signal_connect_object (object, signal_spec + 29,
2275 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2278 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2281 signal_spec = va_arg (var_args, gchar*);
2289 * g_object_disconnect: (skip)
2290 * @object: a #GObject
2291 * @signal_spec: the spec for the first signal
2292 * @...: #GCallback for the first signal, followed by data for the first signal,
2293 * followed optionally by more signal spec/callback/data triples,
2296 * A convenience function to disconnect multiple signals at once.
2298 * The signal specs expected by this function have the form
2299 * "any_signal", which means to disconnect any signal with matching
2300 * callback and data, or "any_signal::signal_name", which only
2301 * disconnects the signal named "signal_name".
2304 g_object_disconnect (gpointer _object,
2305 const gchar *signal_spec,
2308 GObject *object = _object;
2311 g_return_if_fail (G_IS_OBJECT (object));
2312 g_return_if_fail (object->ref_count > 0);
2314 va_start (var_args, signal_spec);
2317 GCallback callback = va_arg (var_args, GCallback);
2318 gpointer data = va_arg (var_args, gpointer);
2319 guint sid = 0, detail = 0, mask = 0;
2321 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2322 strncmp (signal_spec, "any-signal::", 12) == 0)
2325 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2327 else if (strcmp (signal_spec, "any_signal") == 0 ||
2328 strcmp (signal_spec, "any-signal") == 0)
2331 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2335 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2339 if ((mask & G_SIGNAL_MATCH_ID) &&
2340 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2341 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2342 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2344 NULL, (gpointer)callback, data))
2345 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2346 signal_spec = va_arg (var_args, gchar*);
2357 } weak_refs[1]; /* flexible array */
2361 weak_refs_notify (gpointer data)
2363 WeakRefStack *wstack = data;
2366 for (i = 0; i < wstack->n_weak_refs; i++)
2367 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2372 * g_object_weak_ref: (skip)
2373 * @object: #GObject to reference weakly
2374 * @notify: callback to invoke before the object is freed
2375 * @data: extra data to pass to notify
2377 * Adds a weak reference callback to an object. Weak references are
2378 * used for notification when an object is finalized. They are called
2379 * "weak references" because they allow you to safely hold a pointer
2380 * to an object without calling g_object_ref() (g_object_ref() adds a
2381 * strong reference, that is, forces the object to stay alive).
2384 g_object_weak_ref (GObject *object,
2388 WeakRefStack *wstack;
2391 g_return_if_fail (G_IS_OBJECT (object));
2392 g_return_if_fail (notify != NULL);
2393 g_return_if_fail (object->ref_count >= 1);
2395 G_LOCK (weak_refs_mutex);
2396 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2399 i = wstack->n_weak_refs++;
2400 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2404 wstack = g_renew (WeakRefStack, NULL, 1);
2405 wstack->object = object;
2406 wstack->n_weak_refs = 1;
2409 wstack->weak_refs[i].notify = notify;
2410 wstack->weak_refs[i].data = data;
2411 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2412 G_UNLOCK (weak_refs_mutex);
2416 * g_object_weak_unref: (skip)
2417 * @object: #GObject to remove a weak reference from
2418 * @notify: callback to search for
2419 * @data: data to search for
2421 * Removes a weak reference callback to an object.
2424 g_object_weak_unref (GObject *object,
2428 WeakRefStack *wstack;
2429 gboolean found_one = FALSE;
2431 g_return_if_fail (G_IS_OBJECT (object));
2432 g_return_if_fail (notify != NULL);
2434 G_LOCK (weak_refs_mutex);
2435 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2440 for (i = 0; i < wstack->n_weak_refs; i++)
2441 if (wstack->weak_refs[i].notify == notify &&
2442 wstack->weak_refs[i].data == data)
2445 wstack->n_weak_refs -= 1;
2446 if (i != wstack->n_weak_refs)
2447 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2452 G_UNLOCK (weak_refs_mutex);
2454 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2458 * g_object_add_weak_pointer: (skip)
2459 * @object: The object that should be weak referenced.
2460 * @weak_pointer_location: (inout): The memory address of a pointer.
2462 * Adds a weak reference from weak_pointer to @object to indicate that
2463 * the pointer located at @weak_pointer_location is only valid during
2464 * the lifetime of @object. When the @object is finalized,
2465 * @weak_pointer will be set to %NULL.
2468 g_object_add_weak_pointer (GObject *object,
2469 gpointer *weak_pointer_location)
2471 g_return_if_fail (G_IS_OBJECT (object));
2472 g_return_if_fail (weak_pointer_location != NULL);
2474 g_object_weak_ref (object,
2475 (GWeakNotify) g_nullify_pointer,
2476 weak_pointer_location);
2480 * g_object_remove_weak_pointer: (skip)
2481 * @object: The object that is weak referenced.
2482 * @weak_pointer_location: (inout): The memory address of a pointer.
2484 * Removes a weak reference from @object that was previously added
2485 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2486 * to match the one used with g_object_add_weak_pointer().
2489 g_object_remove_weak_pointer (GObject *object,
2490 gpointer *weak_pointer_location)
2492 g_return_if_fail (G_IS_OBJECT (object));
2493 g_return_if_fail (weak_pointer_location != NULL);
2495 g_object_weak_unref (object,
2496 (GWeakNotify) g_nullify_pointer,
2497 weak_pointer_location);
2501 object_floating_flag_handler (GObject *object,
2507 case +1: /* force floating if possible */
2509 oldvalue = g_atomic_pointer_get (&object->qdata);
2510 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2511 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2512 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2513 case -1: /* sink if possible */
2515 oldvalue = g_atomic_pointer_get (&object->qdata);
2516 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2517 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2518 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2519 default: /* check floating */
2520 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2525 * g_object_is_floating:
2526 * @object: (type GObject.Object): a #GObject
2528 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2533 * Returns: %TRUE if @object has a floating reference
2536 g_object_is_floating (gpointer _object)
2538 GObject *object = _object;
2539 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2540 return floating_flag_handler (object, 0);
2544 * g_object_ref_sink:
2545 * @object: (type GObject.Object): a #GObject
2547 * Increase the reference count of @object, and possibly remove the
2548 * <link linkend="floating-ref">floating</link> reference, if @object
2549 * has a floating reference.
2551 * In other words, if the object is floating, then this call "assumes
2552 * ownership" of the floating reference, converting it to a normal
2553 * reference by clearing the floating flag while leaving the reference
2554 * count unchanged. If the object is not floating, then this call
2555 * adds a new normal reference increasing the reference count by one.
2559 * Returns: (type GObject.Object) (transfer none): @object
2562 g_object_ref_sink (gpointer _object)
2564 GObject *object = _object;
2565 gboolean was_floating;
2566 g_return_val_if_fail (G_IS_OBJECT (object), object);
2567 g_return_val_if_fail (object->ref_count >= 1, object);
2568 g_object_ref (object);
2569 was_floating = floating_flag_handler (object, -1);
2571 g_object_unref (object);
2576 * g_object_force_floating:
2577 * @object: a #GObject
2579 * This function is intended for #GObject implementations to re-enforce a
2580 * <link linkend="floating-ref">floating</link> object reference.
2581 * Doing this is seldom required: all
2582 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2583 * usually just needs to be sunken by calling g_object_ref_sink().
2588 g_object_force_floating (GObject *object)
2590 g_return_if_fail (G_IS_OBJECT (object));
2591 g_return_if_fail (object->ref_count >= 1);
2593 floating_flag_handler (object, +1);
2598 guint n_toggle_refs;
2600 GToggleNotify notify;
2602 } toggle_refs[1]; /* flexible array */
2606 toggle_refs_notify (GObject *object,
2607 gboolean is_last_ref)
2609 ToggleRefStack tstack, *tstackptr;
2611 G_LOCK (toggle_refs_mutex);
2612 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2613 tstack = *tstackptr;
2614 G_UNLOCK (toggle_refs_mutex);
2616 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2617 * will only be notified when there is exactly one of them.
2619 g_assert (tstack.n_toggle_refs == 1);
2620 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2624 * g_object_add_toggle_ref: (skip)
2625 * @object: a #GObject
2626 * @notify: a function to call when this reference is the
2627 * last reference to the object, or is no longer
2628 * the last reference.
2629 * @data: data to pass to @notify
2631 * Increases the reference count of the object by one and sets a
2632 * callback to be called when all other references to the object are
2633 * dropped, or when this is already the last reference to the object
2634 * and another reference is established.
2636 * This functionality is intended for binding @object to a proxy
2637 * object managed by another memory manager. This is done with two
2638 * paired references: the strong reference added by
2639 * g_object_add_toggle_ref() and a reverse reference to the proxy
2640 * object which is either a strong reference or weak reference.
2642 * The setup is that when there are no other references to @object,
2643 * only a weak reference is held in the reverse direction from @object
2644 * to the proxy object, but when there are other references held to
2645 * @object, a strong reference is held. The @notify callback is called
2646 * when the reference from @object to the proxy object should be
2647 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2648 * true) or weak to strong (@is_last_ref false).
2650 * Since a (normal) reference must be held to the object before
2651 * calling g_object_add_toggle_ref(), the initial state of the reverse
2652 * link is always strong.
2654 * Multiple toggle references may be added to the same gobject,
2655 * however if there are multiple toggle references to an object, none
2656 * of them will ever be notified until all but one are removed. For
2657 * this reason, you should only ever use a toggle reference if there
2658 * is important state in the proxy object.
2663 g_object_add_toggle_ref (GObject *object,
2664 GToggleNotify notify,
2667 ToggleRefStack *tstack;
2670 g_return_if_fail (G_IS_OBJECT (object));
2671 g_return_if_fail (notify != NULL);
2672 g_return_if_fail (object->ref_count >= 1);
2674 g_object_ref (object);
2676 G_LOCK (toggle_refs_mutex);
2677 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2680 i = tstack->n_toggle_refs++;
2681 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2682 * in tstate->toggle_refs */
2683 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2687 tstack = g_renew (ToggleRefStack, NULL, 1);
2688 tstack->object = object;
2689 tstack->n_toggle_refs = 1;
2693 /* Set a flag for fast lookup after adding the first toggle reference */
2694 if (tstack->n_toggle_refs == 1)
2695 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2697 tstack->toggle_refs[i].notify = notify;
2698 tstack->toggle_refs[i].data = data;
2699 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2700 (GDestroyNotify)g_free);
2701 G_UNLOCK (toggle_refs_mutex);
2705 * g_object_remove_toggle_ref: (skip)
2706 * @object: a #GObject
2707 * @notify: a function to call when this reference is the
2708 * last reference to the object, or is no longer
2709 * the last reference.
2710 * @data: data to pass to @notify
2712 * Removes a reference added with g_object_add_toggle_ref(). The
2713 * reference count of the object is decreased by one.
2718 g_object_remove_toggle_ref (GObject *object,
2719 GToggleNotify notify,
2722 ToggleRefStack *tstack;
2723 gboolean found_one = FALSE;
2725 g_return_if_fail (G_IS_OBJECT (object));
2726 g_return_if_fail (notify != NULL);
2728 G_LOCK (toggle_refs_mutex);
2729 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2734 for (i = 0; i < tstack->n_toggle_refs; i++)
2735 if (tstack->toggle_refs[i].notify == notify &&
2736 tstack->toggle_refs[i].data == data)
2739 tstack->n_toggle_refs -= 1;
2740 if (i != tstack->n_toggle_refs)
2741 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2743 if (tstack->n_toggle_refs == 0)
2744 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2749 G_UNLOCK (toggle_refs_mutex);
2752 g_object_unref (object);
2754 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2759 * @object: (type GObject.Object): a #GObject
2761 * Increases the reference count of @object.
2763 * Returns: (type GObject.Object) (transfer none): the same @object
2766 g_object_ref (gpointer _object)
2768 GObject *object = _object;
2771 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2772 g_return_val_if_fail (object->ref_count > 0, NULL);
2774 #ifdef G_ENABLE_DEBUG
2775 if (g_trap_object_ref == object)
2777 #endif /* G_ENABLE_DEBUG */
2780 old_val = g_atomic_int_add (&object->ref_count, 1);
2782 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2783 toggle_refs_notify (object, FALSE);
2785 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2792 * @object: (type GObject.Object): a #GObject
2794 * Decreases the reference count of @object. When its reference count
2795 * drops to 0, the object is finalized (i.e. its memory is freed).
2798 g_object_unref (gpointer _object)
2800 GObject *object = _object;
2803 g_return_if_fail (G_IS_OBJECT (object));
2804 g_return_if_fail (object->ref_count > 0);
2806 #ifdef G_ENABLE_DEBUG
2807 if (g_trap_object_ref == object)
2809 #endif /* G_ENABLE_DEBUG */
2811 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2812 retry_atomic_decrement1:
2813 old_ref = g_atomic_int_get (&object->ref_count);
2816 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2817 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2819 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2820 goto retry_atomic_decrement1;
2822 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2824 /* if we went from 2->1 we need to notify toggle refs if any */
2825 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2826 toggle_refs_notify (object, TRUE);
2830 /* we are about tp remove the last reference */
2831 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2832 G_OBJECT_GET_CLASS (object)->dispose (object);
2833 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2835 /* may have been re-referenced meanwhile */
2836 retry_atomic_decrement2:
2837 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2840 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2841 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2843 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2844 goto retry_atomic_decrement2;
2846 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2848 /* if we went from 2->1 we need to notify toggle refs if any */
2849 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2850 toggle_refs_notify (object, TRUE);
2855 /* we are still in the process of taking away the last ref */
2856 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2857 g_signal_handlers_destroy (object);
2858 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2860 /* decrement the last reference */
2861 old_ref = g_atomic_int_add (&object->ref_count, -1);
2863 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2865 /* may have been re-referenced meanwhile */
2866 if (G_LIKELY (old_ref == 1))
2868 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2869 G_OBJECT_GET_CLASS (object)->finalize (object);
2871 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2873 #ifdef G_ENABLE_DEBUG
2876 /* catch objects not chaining finalize handlers */
2877 G_LOCK (debug_objects);
2878 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2879 G_UNLOCK (debug_objects);
2881 #endif /* G_ENABLE_DEBUG */
2882 g_type_free_instance ((GTypeInstance*) object);
2888 * g_clear_object: (skip)
2889 * @object_ptr: a pointer to a #GObject reference
2891 * Clears a reference to a #GObject.
2893 * @object_ptr must not be %NULL.
2895 * If the reference is %NULL then this function does nothing.
2896 * Otherwise, the reference count of the object is decreased and the
2897 * pointer is set to %NULL.
2899 * This function is threadsafe and modifies the pointer atomically,
2900 * using memory barriers where needed.
2902 * A macro is also included that allows this function to be used without
2907 #undef g_clear_object
2909 g_clear_object (volatile GObject **object_ptr)
2911 gpointer *ptr = (gpointer) object_ptr;
2914 /* This is a little frustrating.
2915 * Would be nice to have an atomic exchange (with no compare).
2918 old = g_atomic_pointer_get (ptr);
2919 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2922 g_object_unref (old);
2926 * g_object_get_qdata:
2927 * @object: The GObject to get a stored user data pointer from
2928 * @quark: A #GQuark, naming the user data pointer
2930 * This function gets back user data pointers stored via
2931 * g_object_set_qdata().
2933 * Returns: (transfer none): The user data pointer set, or %NULL
2936 g_object_get_qdata (GObject *object,
2939 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2941 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2945 * g_object_set_qdata: (skip)
2946 * @object: The GObject to set store a user data pointer
2947 * @quark: A #GQuark, naming the user data pointer
2948 * @data: An opaque user data pointer
2950 * This sets an opaque, named pointer on an object.
2951 * The name is specified through a #GQuark (retrived e.g. via
2952 * g_quark_from_static_string()), and the pointer
2953 * can be gotten back from the @object with g_object_get_qdata()
2954 * until the @object is finalized.
2955 * Setting a previously set user data pointer, overrides (frees)
2956 * the old pointer set, using #NULL as pointer essentially
2957 * removes the data stored.
2960 g_object_set_qdata (GObject *object,
2964 g_return_if_fail (G_IS_OBJECT (object));
2965 g_return_if_fail (quark > 0);
2967 g_datalist_id_set_data (&object->qdata, quark, data);
2971 * g_object_set_qdata_full: (skip)
2972 * @object: The GObject to set store a user data pointer
2973 * @quark: A #GQuark, naming the user data pointer
2974 * @data: An opaque user data pointer
2975 * @destroy: Function to invoke with @data as argument, when @data
2978 * This function works like g_object_set_qdata(), but in addition,
2979 * a void (*destroy) (gpointer) function may be specified which is
2980 * called with @data as argument when the @object is finalized, or
2981 * the data is being overwritten by a call to g_object_set_qdata()
2982 * with the same @quark.
2985 g_object_set_qdata_full (GObject *object,
2988 GDestroyNotify destroy)
2990 g_return_if_fail (G_IS_OBJECT (object));
2991 g_return_if_fail (quark > 0);
2993 g_datalist_id_set_data_full (&object->qdata, quark, data,
2994 data ? destroy : (GDestroyNotify) NULL);
2998 * g_object_steal_qdata:
2999 * @object: The GObject to get a stored user data pointer from
3000 * @quark: A #GQuark, naming the user data pointer
3002 * This function gets back user data pointers stored via
3003 * g_object_set_qdata() and removes the @data from object
3004 * without invoking its destroy() function (if any was
3006 * Usually, calling this function is only required to update
3007 * user data pointers with a destroy notifier, for example:
3010 * object_add_to_user_list (GObject *object,
3011 * const gchar *new_string)
3013 * // the quark, naming the object data
3014 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3015 * // retrive the old string list
3016 * GList *list = g_object_steal_qdata (object, quark_string_list);
3018 * // prepend new string
3019 * list = g_list_prepend (list, g_strdup (new_string));
3020 * // this changed 'list', so we need to set it again
3021 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3024 * free_string_list (gpointer data)
3026 * GList *node, *list = data;
3028 * for (node = list; node; node = node->next)
3029 * g_free (node->data);
3030 * g_list_free (list);
3033 * Using g_object_get_qdata() in the above example, instead of
3034 * g_object_steal_qdata() would have left the destroy function set,
3035 * and thus the partial string list would have been freed upon
3036 * g_object_set_qdata_full().
3038 * Returns: (transfer full): The user data pointer set, or %NULL
3041 g_object_steal_qdata (GObject *object,
3044 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3045 g_return_val_if_fail (quark > 0, NULL);
3047 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3051 * g_object_get_data:
3052 * @object: #GObject containing the associations
3053 * @key: name of the key for that association
3055 * Gets a named field from the objects table of associations (see g_object_set_data()).
3057 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3060 g_object_get_data (GObject *object,
3063 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3064 g_return_val_if_fail (key != NULL, NULL);
3066 return g_datalist_get_data (&object->qdata, key);
3070 * g_object_set_data:
3071 * @object: #GObject containing the associations.
3072 * @key: name of the key
3073 * @data: data to associate with that key
3075 * Each object carries around a table of associations from
3076 * strings to pointers. This function lets you set an association.
3078 * If the object already had an association with that name,
3079 * the old association will be destroyed.
3082 g_object_set_data (GObject *object,
3086 g_return_if_fail (G_IS_OBJECT (object));
3087 g_return_if_fail (key != NULL);
3089 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3093 * g_object_set_data_full: (skip)
3094 * @object: #GObject containing the associations
3095 * @key: name of the key
3096 * @data: data to associate with that key
3097 * @destroy: function to call when the association is destroyed
3099 * Like g_object_set_data() except it adds notification
3100 * for when the association is destroyed, either by setting it
3101 * to a different value or when the object is destroyed.
3103 * Note that the @destroy callback is not called if @data is %NULL.
3106 g_object_set_data_full (GObject *object,
3109 GDestroyNotify destroy)
3111 g_return_if_fail (G_IS_OBJECT (object));
3112 g_return_if_fail (key != NULL);
3114 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3115 data ? destroy : (GDestroyNotify) NULL);
3119 * g_object_steal_data:
3120 * @object: #GObject containing the associations
3121 * @key: name of the key
3123 * Remove a specified datum from the object's data associations,
3124 * without invoking the association's destroy handler.
3126 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3129 g_object_steal_data (GObject *object,
3134 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3135 g_return_val_if_fail (key != NULL, NULL);
3137 quark = g_quark_try_string (key);
3139 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3143 g_value_object_init (GValue *value)
3145 value->data[0].v_pointer = NULL;
3149 g_value_object_free_value (GValue *value)
3151 if (value->data[0].v_pointer)
3152 g_object_unref (value->data[0].v_pointer);
3156 g_value_object_copy_value (const GValue *src_value,
3159 if (src_value->data[0].v_pointer)
3160 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3162 dest_value->data[0].v_pointer = NULL;
3166 g_value_object_transform_value (const GValue *src_value,
3169 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)))
3170 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3172 dest_value->data[0].v_pointer = NULL;
3176 g_value_object_peek_pointer (const GValue *value)
3178 return value->data[0].v_pointer;
3182 g_value_object_collect_value (GValue *value,
3183 guint n_collect_values,
3184 GTypeCValue *collect_values,
3185 guint collect_flags)
3187 if (collect_values[0].v_pointer)
3189 GObject *object = collect_values[0].v_pointer;
3191 if (object->g_type_instance.g_class == NULL)
3192 return g_strconcat ("invalid unclassed object pointer for value type `",
3193 G_VALUE_TYPE_NAME (value),
3196 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3197 return g_strconcat ("invalid object type `",
3198 G_OBJECT_TYPE_NAME (object),
3199 "' for value type `",
3200 G_VALUE_TYPE_NAME (value),
3203 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3204 value->data[0].v_pointer = g_object_ref (object);
3207 value->data[0].v_pointer = NULL;
3213 g_value_object_lcopy_value (const GValue *value,
3214 guint n_collect_values,
3215 GTypeCValue *collect_values,
3216 guint collect_flags)
3218 GObject **object_p = collect_values[0].v_pointer;
3221 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3223 if (!value->data[0].v_pointer)
3225 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3226 *object_p = value->data[0].v_pointer;
3228 *object_p = g_object_ref (value->data[0].v_pointer);
3234 * g_value_set_object:
3235 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3236 * @v_object: (type GObject.Object) (allow-none): object value to be set
3238 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3240 * g_value_set_object() increases the reference count of @v_object
3241 * (the #GValue holds a reference to @v_object). If you do not wish
3242 * to increase the reference count of the object (i.e. you wish to
3243 * pass your current reference to the #GValue because you no longer
3244 * need it), use g_value_take_object() instead.
3246 * It is important that your #GValue holds a reference to @v_object (either its
3247 * own, or one it has taken) to ensure that the object won't be destroyed while
3248 * the #GValue still exists).
3251 g_value_set_object (GValue *value,
3256 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3258 old = value->data[0].v_pointer;
3262 g_return_if_fail (G_IS_OBJECT (v_object));
3263 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3265 value->data[0].v_pointer = v_object;
3266 g_object_ref (value->data[0].v_pointer);
3269 value->data[0].v_pointer = NULL;
3272 g_object_unref (old);
3276 * g_value_set_object_take_ownership: (skip)
3277 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3278 * @v_object: (allow-none): object value to be set
3280 * This is an internal function introduced mainly for C marshallers.
3282 * Deprecated: 2.4: Use g_value_take_object() instead.
3285 g_value_set_object_take_ownership (GValue *value,
3288 g_value_take_object (value, v_object);
3292 * g_value_take_object: (skip)
3293 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3294 * @v_object: (allow-none): object value to be set
3296 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3297 * and takes over the ownership of the callers reference to @v_object;
3298 * the caller doesn't have to unref it any more (i.e. the reference
3299 * count of the object is not increased).
3301 * If you want the #GValue to hold its own reference to @v_object, use
3302 * g_value_set_object() instead.
3307 g_value_take_object (GValue *value,
3310 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3312 if (value->data[0].v_pointer)
3314 g_object_unref (value->data[0].v_pointer);
3315 value->data[0].v_pointer = NULL;
3320 g_return_if_fail (G_IS_OBJECT (v_object));
3321 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3323 value->data[0].v_pointer = v_object; /* we take over the reference count */
3328 * g_value_get_object:
3329 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3331 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3333 * Returns: (type GObject.Object) (transfer none): object contents of @value
3336 g_value_get_object (const GValue *value)
3338 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3340 return value->data[0].v_pointer;
3344 * g_value_dup_object:
3345 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3347 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3348 * its reference count. If the contents of the #GValue are %NULL, then
3349 * %NULL will be returned.
3351 * Returns: (type GObject.Object) (transfer full): object content of @value,
3352 * should be unreferenced when no longer needed.
3355 g_value_dup_object (const GValue *value)
3357 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3359 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3363 * g_signal_connect_object: (skip)
3364 * @instance: the instance to connect to.
3365 * @detailed_signal: a string of the form "signal-name::detail".
3366 * @c_handler: the #GCallback to connect.
3367 * @gobject: the object to pass as data to @c_handler.
3368 * @connect_flags: a combination of #GConnectFlags.
3370 * This is similar to g_signal_connect_data(), but uses a closure which
3371 * ensures that the @gobject stays alive during the call to @c_handler
3372 * by temporarily adding a reference count to @gobject.
3374 * Note that there is a bug in GObject that makes this function
3375 * much less useful than it might seem otherwise. Once @gobject is
3376 * disposed, the callback will no longer be called, but, the signal
3377 * handler is <emphasis>not</emphasis> currently disconnected. If the
3378 * @instance is itself being freed at the same time than this doesn't
3379 * matter, since the signal will automatically be removed, but
3380 * if @instance persists, then the signal handler will leak. You
3381 * should not remove the signal yourself because in a future versions of
3382 * GObject, the handler <emphasis>will</emphasis> automatically
3385 * It's possible to work around this problem in a way that will
3386 * continue to work with future versions of GObject by checking
3387 * that the signal handler is still connected before disconnected it:
3388 * <informalexample><programlisting>
3389 * if (g_signal_handler_is_connected (instance, id))
3390 * g_signal_handler_disconnect (instance, id);
3391 * </programlisting></informalexample>
3393 * Returns: the handler id.
3396 g_signal_connect_object (gpointer instance,
3397 const gchar *detailed_signal,
3398 GCallback c_handler,
3400 GConnectFlags connect_flags)
3402 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3403 g_return_val_if_fail (detailed_signal != NULL, 0);
3404 g_return_val_if_fail (c_handler != NULL, 0);
3410 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3412 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3414 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3417 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3423 GClosure *closures[1]; /* flexible array */
3425 /* don't change this structure without supplying an accessor for
3426 * watched closures, e.g.:
3427 * GSList* g_object_list_watched_closures (GObject *object)
3430 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3431 * carray = g_object_get_data (object, "GObject-closure-array");
3434 * GSList *slist = NULL;
3436 * for (i = 0; i < carray->n_closures; i++)
3437 * slist = g_slist_prepend (slist, carray->closures[i]);
3445 object_remove_closure (gpointer data,
3448 GObject *object = data;
3452 G_LOCK (closure_array_mutex);
3453 carray = g_object_get_qdata (object, quark_closure_array);
3454 for (i = 0; i < carray->n_closures; i++)
3455 if (carray->closures[i] == closure)
3457 carray->n_closures--;
3458 if (i < carray->n_closures)
3459 carray->closures[i] = carray->closures[carray->n_closures];
3460 G_UNLOCK (closure_array_mutex);
3463 G_UNLOCK (closure_array_mutex);
3464 g_assert_not_reached ();
3468 destroy_closure_array (gpointer data)
3470 CArray *carray = data;
3471 GObject *object = carray->object;
3472 guint i, n = carray->n_closures;
3474 for (i = 0; i < n; i++)
3476 GClosure *closure = carray->closures[i];
3478 /* removing object_remove_closure() upfront is probably faster than
3479 * letting it fiddle with quark_closure_array which is empty anyways
3481 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3482 g_closure_invalidate (closure);
3488 * g_object_watch_closure:
3489 * @object: GObject restricting lifetime of @closure
3490 * @closure: GClosure to watch
3492 * This function essentially limits the life time of the @closure to
3493 * the life time of the object. That is, when the object is finalized,
3494 * the @closure is invalidated by calling g_closure_invalidate() on
3495 * it, in order to prevent invocations of the closure with a finalized
3496 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3497 * added as marshal guards to the @closure, to ensure that an extra
3498 * reference count is held on @object during invocation of the
3499 * @closure. Usually, this function will be called on closures that
3500 * use this @object as closure data.
3503 g_object_watch_closure (GObject *object,
3509 g_return_if_fail (G_IS_OBJECT (object));
3510 g_return_if_fail (closure != NULL);
3511 g_return_if_fail (closure->is_invalid == FALSE);
3512 g_return_if_fail (closure->in_marshal == FALSE);
3513 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3515 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3516 g_closure_add_marshal_guards (closure,
3517 object, (GClosureNotify) g_object_ref,
3518 object, (GClosureNotify) g_object_unref);
3519 G_LOCK (closure_array_mutex);
3520 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3523 carray = g_renew (CArray, NULL, 1);
3524 carray->object = object;
3525 carray->n_closures = 1;
3530 i = carray->n_closures++;
3531 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3533 carray->closures[i] = closure;
3534 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3535 G_UNLOCK (closure_array_mutex);
3539 * g_closure_new_object:
3540 * @sizeof_closure: the size of the structure to allocate, must be at least
3541 * <literal>sizeof (GClosure)</literal>
3542 * @object: a #GObject pointer to store in the @data field of the newly
3543 * allocated #GClosure
3545 * A variant of g_closure_new_simple() which stores @object in the
3546 * @data field of the closure and calls g_object_watch_closure() on
3547 * @object and the created closure. This function is mainly useful
3548 * when implementing new types of closures.
3550 * Returns: (transfer full): a newly allocated #GClosure
3553 g_closure_new_object (guint sizeof_closure,
3558 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3559 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3561 closure = g_closure_new_simple (sizeof_closure, object);
3562 g_object_watch_closure (object, closure);
3568 * g_cclosure_new_object: (skip)
3569 * @callback_func: the function to invoke
3570 * @object: a #GObject pointer to pass to @callback_func
3572 * A variant of g_cclosure_new() which uses @object as @user_data and
3573 * calls g_object_watch_closure() on @object and the created
3574 * closure. This function is useful when you have a callback closely
3575 * associated with a #GObject, and want the callback to no longer run
3576 * after the object is is freed.
3578 * Returns: a new #GCClosure
3581 g_cclosure_new_object (GCallback callback_func,
3586 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3587 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3588 g_return_val_if_fail (callback_func != NULL, NULL);
3590 closure = g_cclosure_new (callback_func, object, NULL);
3591 g_object_watch_closure (object, closure);
3597 * g_cclosure_new_object_swap: (skip)
3598 * @callback_func: the function to invoke
3599 * @object: a #GObject pointer to pass to @callback_func
3601 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3602 * and calls g_object_watch_closure() on @object and the created
3603 * closure. This function is useful when you have a callback closely
3604 * associated with a #GObject, and want the callback to no longer run
3605 * after the object is is freed.
3607 * Returns: a new #GCClosure
3610 g_cclosure_new_object_swap (GCallback callback_func,
3615 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3616 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3617 g_return_val_if_fail (callback_func != NULL, NULL);
3619 closure = g_cclosure_new_swap (callback_func, object, NULL);
3620 g_object_watch_closure (object, closure);
3626 g_object_compat_control (gsize what,
3632 case 1: /* floating base type */
3633 return G_TYPE_INITIALLY_UNOWNED;
3634 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3635 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3637 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3639 *pp = floating_flag_handler;
3646 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3649 g_initially_unowned_init (GInitiallyUnowned *object)
3651 g_object_force_floating (object);
3655 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)