1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * MT safe with regards to reference counting.
30 #include "gtype-private.h"
31 #include "gvaluecollector.h"
33 #include "gparamspecs.h"
34 #include "gvaluetypes.h"
35 #include "gobject_trace.h"
36 #include "gconstructor.h"
41 * @short_description: The base object type
42 * @see_also: #GParamSpecObject, g_param_spec_object()
44 * GObject is the fundamental type providing the common attributes and
45 * methods for all object types in GTK+, Pango and other libraries
46 * based on GObject. The GObject class provides methods for object
47 * construction and destruction, property access methods, and signal
48 * support. Signals are described in detail in <xref
49 * linkend="gobject-Signals"/>.
51 * <para id="floating-ref">
52 * GInitiallyUnowned is derived from GObject. The only difference between
53 * the two is that the initial reference of a GInitiallyUnowned is flagged
54 * as a <firstterm>floating</firstterm> reference.
55 * This means that it is not specifically claimed to be "owned" by
56 * any code portion. The main motivation for providing floating references is
57 * C convenience. In particular, it allows code to be written as:
59 * container = create_container ();
60 * container_add_child (container, create_child());
62 * If <function>container_add_child()</function> will g_object_ref_sink() the
63 * passed in child, no reference of the newly created child is leaked.
64 * Without floating references, <function>container_add_child()</function>
65 * can only g_object_ref() the new child, so to implement this code without
66 * reference leaks, it would have to be written as:
69 * container = create_container ();
70 * child = create_child ();
71 * container_add_child (container, child);
72 * g_object_unref (child);
74 * The floating reference can be converted into
75 * an ordinary reference by calling g_object_ref_sink().
76 * For already sunken objects (objects that don't have a floating reference
77 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
79 * Since floating references are useful almost exclusively for C convenience,
80 * language bindings that provide automated reference and memory ownership
81 * maintenance (such as smart pointers or garbage collection) should not
82 * expose floating references in their API.
85 * Some object implementations may need to save an objects floating state
86 * across certain code portions (an example is #GtkMenu), to achieve this,
87 * the following sequence can be used:
90 * /* save floating state */
91 * gboolean was_floating = g_object_is_floating (object);
92 * g_object_ref_sink (object);
93 * /* protected code portion */
95 * /* restore floating state */
97 * g_object_force_floating (object);
98 * g_object_unref (object); /* release previously acquired reference */
104 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
105 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
107 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
108 #define OBJECT_HAS_TOGGLE_REF(object) \
109 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
110 #define OBJECT_FLOATING_FLAG 0x2
112 #define CLASS_HAS_PROPS_FLAG 0x1
113 #define CLASS_HAS_PROPS(class) \
114 ((class)->flags & CLASS_HAS_PROPS_FLAG)
115 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
116 ((class)->constructor != g_object_constructor)
117 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
118 ((class)->constructed != g_object_constructed)
120 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
121 #define CLASS_HAS_DERIVED_CLASS(class) \
122 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
124 /* --- signals --- */
131 /* --- properties --- */
137 /* --- prototypes --- */
138 static void g_object_base_class_init (GObjectClass *class);
139 static void g_object_base_class_finalize (GObjectClass *class);
140 static void g_object_do_class_init (GObjectClass *class);
141 static void g_object_init (GObject *object,
142 GObjectClass *class);
143 static GObject* g_object_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void g_object_constructed (GObject *object);
147 static void g_object_real_dispose (GObject *object);
148 static void g_object_finalize (GObject *object);
149 static void g_object_do_set_property (GObject *object,
153 static void g_object_do_get_property (GObject *object,
157 static void g_value_object_init (GValue *value);
158 static void g_value_object_free_value (GValue *value);
159 static void g_value_object_copy_value (const GValue *src_value,
161 static void g_value_object_transform_value (const GValue *src_value,
163 static gpointer g_value_object_peek_pointer (const GValue *value);
164 static gchar* g_value_object_collect_value (GValue *value,
165 guint n_collect_values,
166 GTypeCValue *collect_values,
167 guint collect_flags);
168 static gchar* g_value_object_lcopy_value (const GValue *value,
169 guint n_collect_values,
170 GTypeCValue *collect_values,
171 guint collect_flags);
172 static void g_object_dispatch_properties_changed (GObject *object,
174 GParamSpec **pspecs);
175 static guint object_floating_flag_handler (GObject *object,
178 static void object_interface_check_properties (gpointer func_data,
181 /* --- typedefs --- */
182 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
184 struct _GObjectNotifyQueue
188 guint16 freeze_count;
191 /* --- variables --- */
192 G_LOCK_DEFINE_STATIC (closure_array_mutex);
193 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
194 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
195 static GQuark quark_closure_array = 0;
196 static GQuark quark_weak_refs = 0;
197 static GQuark quark_toggle_refs = 0;
198 static GQuark quark_notify_queue;
199 static GParamSpecPool *pspec_pool = NULL;
200 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
201 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
202 G_LOCK_DEFINE_STATIC (construction_mutex);
203 static GSList *construction_objects = NULL;
204 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
205 static GQuark quark_weak_locations = 0;
206 static GRWLock weak_locations_lock;
208 G_LOCK_DEFINE_STATIC(notify_lock);
210 /* --- functions --- */
212 g_object_notify_queue_free (gpointer data)
214 GObjectNotifyQueue *nqueue = data;
216 g_slist_free (nqueue->pspecs);
217 g_slice_free (GObjectNotifyQueue, nqueue);
220 static GObjectNotifyQueue*
221 g_object_notify_queue_freeze (GObject *object,
222 gboolean conditional)
224 GObjectNotifyQueue *nqueue;
227 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
232 G_UNLOCK(notify_lock);
236 nqueue = g_slice_new0 (GObjectNotifyQueue);
237 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
238 nqueue, g_object_notify_queue_free);
241 if (nqueue->freeze_count >= 65535)
242 g_critical("Free queue for %s (%p) is larger than 65535,"
243 " called g_object_freeze_notify() too often."
244 " Forgot to call g_object_thaw_notify() or infinite loop",
245 G_OBJECT_TYPE_NAME (object), object);
247 nqueue->freeze_count++;
248 G_UNLOCK(notify_lock);
254 g_object_notify_queue_thaw (GObject *object,
255 GObjectNotifyQueue *nqueue)
257 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
261 g_return_if_fail (nqueue->freeze_count > 0);
262 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
266 /* Just make sure we never get into some nasty race condition */
267 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
268 G_UNLOCK(notify_lock);
269 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
270 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
274 nqueue->freeze_count--;
275 if (nqueue->freeze_count) {
276 G_UNLOCK(notify_lock);
280 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
282 for (slist = nqueue->pspecs; slist; slist = slist->next)
284 pspecs[n_pspecs++] = slist->data;
286 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
288 G_UNLOCK(notify_lock);
291 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
296 g_object_notify_queue_add (GObject *object,
297 GObjectNotifyQueue *nqueue,
302 g_return_if_fail (nqueue->n_pspecs < 65535);
304 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
306 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
310 G_UNLOCK(notify_lock);
313 #ifdef G_ENABLE_DEBUG
314 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
315 G_LOCK_DEFINE_STATIC (debug_objects);
316 static volatile GObject *g_trap_object_ref = NULL;
317 static guint debug_objects_count = 0;
318 static GHashTable *debug_objects_ht = NULL;
321 debug_objects_foreach (gpointer key,
325 GObject *object = value;
327 g_message ("[%p] stale %s\tref_count=%u",
329 G_OBJECT_TYPE_NAME (object),
333 #ifdef G_HAS_CONSTRUCTORS
334 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
335 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
337 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
338 #endif /* G_HAS_CONSTRUCTORS */
341 debug_objects_atexit (void)
345 G_LOCK (debug_objects);
346 g_message ("stale GObjects: %u", debug_objects_count);
347 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
348 G_UNLOCK (debug_objects);
351 #endif /* G_ENABLE_DEBUG */
354 _g_object_type_init (void)
356 static gboolean initialized = FALSE;
357 static const GTypeFundamentalInfo finfo = {
358 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
361 sizeof (GObjectClass),
362 (GBaseInitFunc) g_object_base_class_init,
363 (GBaseFinalizeFunc) g_object_base_class_finalize,
364 (GClassInitFunc) g_object_do_class_init,
365 NULL /* class_destroy */,
366 NULL /* class_data */,
369 (GInstanceInitFunc) g_object_init,
370 NULL, /* value_table */
372 static const GTypeValueTable value_table = {
373 g_value_object_init, /* value_init */
374 g_value_object_free_value, /* value_free */
375 g_value_object_copy_value, /* value_copy */
376 g_value_object_peek_pointer, /* value_peek_pointer */
377 "p", /* collect_format */
378 g_value_object_collect_value, /* collect_value */
379 "p", /* lcopy_format */
380 g_value_object_lcopy_value, /* lcopy_value */
384 g_return_if_fail (initialized == FALSE);
389 info.value_table = &value_table;
390 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
391 g_assert (type == G_TYPE_OBJECT);
392 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
394 #ifdef G_ENABLE_DEBUG
397 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
398 #ifndef G_HAS_CONSTRUCTORS
399 g_atexit (debug_objects_atexit);
400 #endif /* G_HAS_CONSTRUCTORS */
402 #endif /* G_ENABLE_DEBUG */
406 g_object_base_class_init (GObjectClass *class)
408 GObjectClass *pclass = g_type_class_peek_parent (class);
410 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
411 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
414 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
416 /* reset instance specific fields and methods that don't get inherited */
417 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
418 class->get_property = NULL;
419 class->set_property = NULL;
423 g_object_base_class_finalize (GObjectClass *class)
427 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
429 g_slist_free (class->construct_properties);
430 class->construct_properties = NULL;
431 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
432 for (node = list; node; node = node->next)
434 GParamSpec *pspec = node->data;
436 g_param_spec_pool_remove (pspec_pool, pspec);
437 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
438 g_param_spec_unref (pspec);
444 g_object_do_class_init (GObjectClass *class)
446 /* read the comment about typedef struct CArray; on why not to change this quark */
447 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
449 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
450 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
451 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
452 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
453 pspec_pool = g_param_spec_pool_new (TRUE);
455 class->constructor = g_object_constructor;
456 class->constructed = g_object_constructed;
457 class->set_property = g_object_do_set_property;
458 class->get_property = g_object_do_get_property;
459 class->dispose = g_object_real_dispose;
460 class->finalize = g_object_finalize;
461 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
462 class->notify = NULL;
466 * @gobject: the object which received the signal.
467 * @pspec: the #GParamSpec of the property which changed.
469 * The notify signal is emitted on an object when one of its
470 * properties has been changed. Note that getting this signal
471 * doesn't guarantee that the value of the property has actually
472 * changed, it may also be emitted when the setter for the property
473 * is called to reinstate the previous value.
475 * This signal is typically used to obtain change notification for a
476 * single property, by specifying the property name as a detail in the
477 * g_signal_connect() call, like this:
479 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
480 * G_CALLBACK (gtk_text_view_target_list_notify),
483 * It is important to note that you must use
484 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
485 * detail strings for the notify signal.
487 gobject_signals[NOTIFY] =
488 g_signal_new (g_intern_static_string ("notify"),
489 G_TYPE_FROM_CLASS (class),
490 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
491 G_STRUCT_OFFSET (GObjectClass, notify),
493 g_cclosure_marshal_VOID__PARAM,
497 /* Install a check function that we'll use to verify that classes that
498 * implement an interface implement all properties for that interface
500 g_type_add_interface_check (NULL, object_interface_check_properties);
504 install_property_internal (GType g_type,
508 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
510 g_warning ("When installing property: type `%s' already has a property named `%s'",
511 g_type_name (g_type),
516 g_param_spec_ref_sink (pspec);
517 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
518 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
522 * g_object_class_install_property:
523 * @oclass: a #GObjectClass
524 * @property_id: the id for the new property
525 * @pspec: the #GParamSpec for the new property
527 * Installs a new property. This is usually done in the class initializer.
529 * Note that it is possible to redefine a property in a derived class,
530 * by installing a property with the same name. This can be useful at times,
531 * e.g. to change the range of allowed values or the default value.
534 g_object_class_install_property (GObjectClass *class,
538 g_return_if_fail (G_IS_OBJECT_CLASS (class));
539 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
541 if (CLASS_HAS_DERIVED_CLASS (class))
542 g_error ("Attempt to add property %s::%s to class after it was derived",
543 G_OBJECT_CLASS_NAME (class), pspec->name);
545 class->flags |= CLASS_HAS_PROPS_FLAG;
547 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
548 if (pspec->flags & G_PARAM_WRITABLE)
549 g_return_if_fail (class->set_property != NULL);
550 if (pspec->flags & G_PARAM_READABLE)
551 g_return_if_fail (class->get_property != NULL);
552 g_return_if_fail (property_id > 0);
553 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
554 if (pspec->flags & G_PARAM_CONSTRUCT)
555 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
556 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
557 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
559 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
561 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
562 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
564 /* for property overrides of construct properties, we have to get rid
565 * of the overidden inherited construct property
567 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
568 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
569 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
573 * g_object_class_install_properties:
574 * @oclass: a #GObjectClass
575 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
576 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
577 * defining the new properties
579 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
580 * usually done in the class initializer.
582 * The property id of each property is the index of each #GParamSpec in
585 * The property id of 0 is treated specially by #GObject and it should not
586 * be used to store a #GParamSpec.
588 * This function should be used if you plan to use a static array of
589 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
590 * class initialization:
594 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
597 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
600 * my_object_class_init (MyObjectClass *klass)
602 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
604 * obj_properties[PROP_FOO] =
605 * g_param_spec_int ("foo", "Foo", "Foo",
608 * G_PARAM_READWRITE);
610 * obj_properties[PROP_BAR] =
611 * g_param_spec_string ("bar", "Bar", "Bar",
613 * G_PARAM_READWRITE);
615 * gobject_class->set_property = my_object_set_property;
616 * gobject_class->get_property = my_object_get_property;
617 * g_object_class_install_properties (gobject_class,
623 * allows calling g_object_notify_by_pspec() to notify of property changes:
627 * my_object_set_foo (MyObject *self, gint foo)
629 * if (self->foo != foo)
632 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
640 g_object_class_install_properties (GObjectClass *oclass,
644 GType oclass_type, parent_type;
647 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
648 g_return_if_fail (n_pspecs > 1);
649 g_return_if_fail (pspecs[0] == NULL);
651 if (CLASS_HAS_DERIVED_CLASS (oclass))
652 g_error ("Attempt to add properties to %s after it was derived",
653 G_OBJECT_CLASS_NAME (oclass));
655 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
656 parent_type = g_type_parent (oclass_type);
658 /* we skip the first element of the array as it would have a 0 prop_id */
659 for (i = 1; i < n_pspecs; i++)
661 GParamSpec *pspec = pspecs[i];
663 g_return_if_fail (pspec != NULL);
665 if (pspec->flags & G_PARAM_WRITABLE)
666 g_return_if_fail (oclass->set_property != NULL);
667 if (pspec->flags & G_PARAM_READABLE)
668 g_return_if_fail (oclass->get_property != NULL);
669 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
670 if (pspec->flags & G_PARAM_CONSTRUCT)
671 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
672 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
673 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
675 oclass->flags |= CLASS_HAS_PROPS_FLAG;
676 install_property_internal (oclass_type, i, pspec);
678 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
679 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
681 /* for property overrides of construct properties, we have to get rid
682 * of the overidden inherited construct property
684 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
685 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
686 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
691 * g_object_interface_install_property:
692 * @g_iface: any interface vtable for the interface, or the default
693 * vtable for the interface.
694 * @pspec: the #GParamSpec for the new property
696 * Add a property to an interface; this is only useful for interfaces
697 * that are added to GObject-derived types. Adding a property to an
698 * interface forces all objects classes with that interface to have a
699 * compatible property. The compatible property could be a newly
700 * created #GParamSpec, but normally
701 * g_object_class_override_property() will be used so that the object
702 * class only needs to provide an implementation and inherits the
703 * property description, default value, bounds, and so forth from the
704 * interface property.
706 * This function is meant to be called from the interface's default
707 * vtable initialization function (the @class_init member of
708 * #GTypeInfo.) It must not be called after after @class_init has
709 * been called for any object types implementing this interface.
714 g_object_interface_install_property (gpointer g_iface,
717 GTypeInterface *iface_class = g_iface;
719 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
720 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
721 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
722 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
724 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
725 if (pspec->flags & G_PARAM_CONSTRUCT)
726 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
727 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
728 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
730 install_property_internal (iface_class->g_type, 0, pspec);
734 * g_object_class_find_property:
735 * @oclass: a #GObjectClass
736 * @property_name: the name of the property to look up
738 * Looks up the #GParamSpec for a property of a class.
740 * Returns: (transfer none): the #GParamSpec for the property, or
741 * %NULL if the class doesn't have a property of that name
744 g_object_class_find_property (GObjectClass *class,
745 const gchar *property_name)
748 GParamSpec *redirect;
750 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
751 g_return_val_if_fail (property_name != NULL, NULL);
753 pspec = g_param_spec_pool_lookup (pspec_pool,
755 G_OBJECT_CLASS_TYPE (class),
759 redirect = g_param_spec_get_redirect_target (pspec);
770 * g_object_interface_find_property:
771 * @g_iface: any interface vtable for the interface, or the default
772 * vtable for the interface
773 * @property_name: name of a property to lookup.
775 * Find the #GParamSpec with the given name for an
776 * interface. Generally, the interface vtable passed in as @g_iface
777 * will be the default vtable from g_type_default_interface_ref(), or,
778 * if you know the interface has already been loaded,
779 * g_type_default_interface_peek().
783 * Returns: (transfer none): the #GParamSpec for the property of the
784 * interface with the name @property_name, or %NULL if no
785 * such property exists.
788 g_object_interface_find_property (gpointer g_iface,
789 const gchar *property_name)
791 GTypeInterface *iface_class = g_iface;
793 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
794 g_return_val_if_fail (property_name != NULL, NULL);
796 return g_param_spec_pool_lookup (pspec_pool,
803 * g_object_class_override_property:
804 * @oclass: a #GObjectClass
805 * @property_id: the new property ID
806 * @name: the name of a property registered in a parent class or
807 * in an interface of this class.
809 * Registers @property_id as referring to a property with the
810 * name @name in a parent class or in an interface implemented
811 * by @oclass. This allows this class to <firstterm>override</firstterm>
812 * a property implementation in a parent class or to provide
813 * the implementation of a property from an interface.
816 * Internally, overriding is implemented by creating a property of type
817 * #GParamSpecOverride; generally operations that query the properties of
818 * the object class, such as g_object_class_find_property() or
819 * g_object_class_list_properties() will return the overridden
820 * property. However, in one case, the @construct_properties argument of
821 * the @constructor virtual function, the #GParamSpecOverride is passed
822 * instead, so that the @param_id field of the #GParamSpec will be
823 * correct. For virtually all uses, this makes no difference. If you
824 * need to get the overridden property, you can call
825 * g_param_spec_get_redirect_target().
831 g_object_class_override_property (GObjectClass *oclass,
835 GParamSpec *overridden = NULL;
839 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
840 g_return_if_fail (property_id > 0);
841 g_return_if_fail (name != NULL);
843 /* Find the overridden property; first check parent types
845 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
846 if (parent_type != G_TYPE_NONE)
847 overridden = g_param_spec_pool_lookup (pspec_pool,
856 /* Now check interfaces
858 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
859 while (n_ifaces-- && !overridden)
861 overridden = g_param_spec_pool_lookup (pspec_pool,
872 g_warning ("%s: Can't find property to override for '%s::%s'",
873 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
877 new = g_param_spec_override (name, overridden);
878 g_object_class_install_property (oclass, property_id, new);
882 * g_object_class_list_properties:
883 * @oclass: a #GObjectClass
884 * @n_properties: (out): return location for the length of the returned array
886 * Get an array of #GParamSpec* for all properties of a class.
888 * Returns: (array length=n_properties) (transfer container): an array of
889 * #GParamSpec* which should be freed after use
891 GParamSpec** /* free result */
892 g_object_class_list_properties (GObjectClass *class,
893 guint *n_properties_p)
898 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
900 pspecs = g_param_spec_pool_list (pspec_pool,
901 G_OBJECT_CLASS_TYPE (class),
910 * g_object_interface_list_properties:
911 * @g_iface: any interface vtable for the interface, or the default
912 * vtable for the interface
913 * @n_properties_p: (out): location to store number of properties returned.
915 * Lists the properties of an interface.Generally, the interface
916 * vtable passed in as @g_iface will be the default vtable from
917 * g_type_default_interface_ref(), or, if you know the interface has
918 * already been loaded, g_type_default_interface_peek().
922 * Returns: (array length=n_properties_p) (transfer container): a
923 * pointer to an array of pointers to #GParamSpec
924 * structures. The paramspecs are owned by GLib, but the
925 * array should be freed with g_free() when you are done with
929 g_object_interface_list_properties (gpointer g_iface,
930 guint *n_properties_p)
932 GTypeInterface *iface_class = g_iface;
936 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
938 pspecs = g_param_spec_pool_list (pspec_pool,
948 g_object_init (GObject *object,
951 object->ref_count = 1;
952 object->qdata = NULL;
954 if (CLASS_HAS_PROPS (class))
956 /* freeze object's notification queue, g_object_newv() preserves pairedness */
957 g_object_notify_queue_freeze (object, FALSE);
960 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
962 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
963 G_LOCK (construction_mutex);
964 construction_objects = g_slist_prepend (construction_objects, object);
965 G_UNLOCK (construction_mutex);
968 #ifdef G_ENABLE_DEBUG
971 G_LOCK (debug_objects);
972 debug_objects_count++;
973 g_hash_table_insert (debug_objects_ht, object, object);
974 G_UNLOCK (debug_objects);
976 #endif /* G_ENABLE_DEBUG */
980 g_object_do_set_property (GObject *object,
988 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
994 g_object_do_get_property (GObject *object,
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1008 g_object_real_dispose (GObject *object)
1010 g_signal_handlers_destroy (object);
1011 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1012 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1016 g_object_finalize (GObject *object)
1018 g_datalist_clear (&object->qdata);
1020 #ifdef G_ENABLE_DEBUG
1023 G_LOCK (debug_objects);
1024 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1025 g_hash_table_remove (debug_objects_ht, object);
1026 debug_objects_count--;
1027 G_UNLOCK (debug_objects);
1029 #endif /* G_ENABLE_DEBUG */
1034 g_object_dispatch_properties_changed (GObject *object,
1036 GParamSpec **pspecs)
1040 for (i = 0; i < n_pspecs; i++)
1041 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1045 * g_object_run_dispose:
1046 * @object: a #GObject
1048 * Releases all references to other objects. This can be used to break
1051 * This functions should only be called from object system implementations.
1054 g_object_run_dispose (GObject *object)
1056 g_return_if_fail (G_IS_OBJECT (object));
1057 g_return_if_fail (object->ref_count > 0);
1059 g_object_ref (object);
1060 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1061 G_OBJECT_GET_CLASS (object)->dispose (object);
1062 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1063 g_object_unref (object);
1067 * g_object_freeze_notify:
1068 * @object: a #GObject
1070 * Increases the freeze count on @object. If the freeze count is
1071 * non-zero, the emission of "notify" signals on @object is
1072 * stopped. The signals are queued until the freeze count is decreased
1075 * This is necessary for accessors that modify multiple properties to prevent
1076 * premature notification while the object is still being modified.
1079 g_object_freeze_notify (GObject *object)
1081 g_return_if_fail (G_IS_OBJECT (object));
1083 if (g_atomic_int_get (&object->ref_count) == 0)
1086 g_object_ref (object);
1087 g_object_notify_queue_freeze (object, FALSE);
1088 g_object_unref (object);
1092 get_notify_pspec (GParamSpec *pspec)
1094 GParamSpec *redirected;
1096 /* we don't notify on non-READABLE parameters */
1097 if (~pspec->flags & G_PARAM_READABLE)
1100 /* if the paramspec is redirected, notify on the target */
1101 redirected = g_param_spec_get_redirect_target (pspec);
1102 if (redirected != NULL)
1105 /* else, notify normally */
1110 g_object_notify_by_spec_internal (GObject *object,
1113 GParamSpec *notify_pspec;
1115 notify_pspec = get_notify_pspec (pspec);
1117 if (notify_pspec != NULL)
1119 GObjectNotifyQueue *nqueue;
1121 /* conditional freeze: only increase freeze count if already frozen */
1122 nqueue = g_object_notify_queue_freeze (object, TRUE);
1126 /* we're frozen, so add to the queue and release our freeze */
1127 g_object_notify_queue_add (object, nqueue, notify_pspec);
1128 g_object_notify_queue_thaw (object, nqueue);
1131 /* not frozen, so just dispatch the notification directly */
1132 G_OBJECT_GET_CLASS (object)
1133 ->dispatch_properties_changed (object, 1, ¬ify_pspec);
1139 * @object: a #GObject
1140 * @property_name: the name of a property installed on the class of @object.
1142 * Emits a "notify" signal for the property @property_name on @object.
1144 * When possible, eg. when signaling a property change from within the class
1145 * that registered the property, you should use g_object_notify_by_pspec()
1149 g_object_notify (GObject *object,
1150 const gchar *property_name)
1154 g_return_if_fail (G_IS_OBJECT (object));
1155 g_return_if_fail (property_name != NULL);
1156 if (g_atomic_int_get (&object->ref_count) == 0)
1159 g_object_ref (object);
1160 /* We don't need to get the redirect target
1161 * (by, e.g. calling g_object_class_find_property())
1162 * because g_object_notify_queue_add() does that
1164 pspec = g_param_spec_pool_lookup (pspec_pool,
1166 G_OBJECT_TYPE (object),
1170 g_warning ("%s: object class `%s' has no property named `%s'",
1172 G_OBJECT_TYPE_NAME (object),
1175 g_object_notify_by_spec_internal (object, pspec);
1176 g_object_unref (object);
1180 * g_object_notify_by_pspec:
1181 * @object: a #GObject
1182 * @pspec: the #GParamSpec of a property installed on the class of @object.
1184 * Emits a "notify" signal for the property specified by @pspec on @object.
1186 * This function omits the property name lookup, hence it is faster than
1187 * g_object_notify().
1189 * One way to avoid using g_object_notify() from within the
1190 * class that registered the properties, and using g_object_notify_by_pspec()
1191 * instead, is to store the GParamSpec used with
1192 * g_object_class_install_property() inside a static array, e.g.:
1202 * static GParamSpec *properties[PROP_LAST];
1205 * my_object_class_init (MyObjectClass *klass)
1207 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1210 * G_PARAM_READWRITE);
1211 * g_object_class_install_property (gobject_class,
1213 * properties[PROP_FOO]);
1217 * and then notify a change on the "foo" property with:
1220 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1226 g_object_notify_by_pspec (GObject *object,
1230 g_return_if_fail (G_IS_OBJECT (object));
1231 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1233 g_object_ref (object);
1234 g_object_notify_by_spec_internal (object, pspec);
1235 g_object_unref (object);
1239 * g_object_thaw_notify:
1240 * @object: a #GObject
1242 * Reverts the effect of a previous call to
1243 * g_object_freeze_notify(). The freeze count is decreased on @object
1244 * and when it reaches zero, all queued "notify" signals are emitted.
1246 * It is an error to call this function when the freeze count is zero.
1249 g_object_thaw_notify (GObject *object)
1251 GObjectNotifyQueue *nqueue;
1253 g_return_if_fail (G_IS_OBJECT (object));
1254 if (g_atomic_int_get (&object->ref_count) == 0)
1257 g_object_ref (object);
1259 /* FIXME: Freezing is the only way to get at the notify queue.
1260 * So we freeze once and then thaw twice.
1262 nqueue = g_object_notify_queue_freeze (object, FALSE);
1263 g_object_notify_queue_thaw (object, nqueue);
1264 g_object_notify_queue_thaw (object, nqueue);
1266 g_object_unref (object);
1270 object_get_property (GObject *object,
1274 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1275 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1276 GParamSpec *redirect;
1280 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1281 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1285 redirect = g_param_spec_get_redirect_target (pspec);
1289 class->get_property (object, param_id, value, pspec);
1293 object_set_property (GObject *object,
1295 const GValue *value,
1296 GObjectNotifyQueue *nqueue)
1298 GValue tmp_value = G_VALUE_INIT;
1299 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1300 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1301 GParamSpec *redirect;
1302 static const gchar * enable_diagnostic = NULL;
1306 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1307 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1311 redirect = g_param_spec_get_redirect_target (pspec);
1315 if (G_UNLIKELY (!enable_diagnostic))
1317 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1318 if (!enable_diagnostic)
1319 enable_diagnostic = "0";
1322 if (enable_diagnostic[0] == '1')
1324 if (pspec->flags & G_PARAM_DEPRECATED)
1325 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1326 "anymore. It will be removed in a future version.",
1327 G_OBJECT_TYPE_NAME (object), pspec->name);
1330 /* provide a copy to work from, convert (if necessary) and validate */
1331 g_value_init (&tmp_value, pspec->value_type);
1332 if (!g_value_transform (value, &tmp_value))
1333 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1335 g_type_name (pspec->value_type),
1336 G_VALUE_TYPE_NAME (value));
1337 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1339 gchar *contents = g_strdup_value_contents (value);
1341 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1343 G_VALUE_TYPE_NAME (value),
1345 g_type_name (pspec->value_type));
1350 GParamSpec *notify_pspec;
1352 class->set_property (object, param_id, &tmp_value, pspec);
1354 notify_pspec = get_notify_pspec (pspec);
1356 if (notify_pspec != NULL)
1357 g_object_notify_queue_add (object, nqueue, notify_pspec);
1359 g_value_unset (&tmp_value);
1363 object_interface_check_properties (gpointer func_data,
1366 GTypeInterface *iface_class = g_iface;
1367 GObjectClass *class;
1368 GType iface_type = iface_class->g_type;
1369 GParamSpec **pspecs;
1372 class = g_type_class_ref (iface_class->g_instance_type);
1374 if (!G_IS_OBJECT_CLASS (class))
1377 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1381 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1383 G_OBJECT_CLASS_TYPE (class),
1388 g_critical ("Object class %s doesn't implement property "
1389 "'%s' from interface '%s'",
1390 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1392 g_type_name (iface_type));
1397 /* We do a number of checks on the properties of an interface to
1398 * make sure that all classes implementing the interface are
1399 * overriding the properties in a sane way.
1401 * We do the checks in order of importance so that we can give
1402 * more useful error messages first.
1404 * First, we check that the implementation doesn't remove the
1405 * basic functionality (readability, writability) advertised by
1406 * the interface. Next, we check that it doesn't introduce
1407 * additional restrictions (such as construct-only). Finally, we
1408 * make sure the types are compatible.
1411 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1412 /* If the property on the interface is readable then the
1413 * implementation must be readable. If the interface is writable
1414 * then the implementation must be writable.
1416 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1418 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1419 "property on interface '%s'\n", pspecs[n]->name,
1420 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1424 /* If the property on the interface is writable then we need to
1425 * make sure the implementation doesn't introduce new restrictions
1426 * on that writability (ie: construct-only).
1428 * If the interface was not writable to begin with then we don't
1429 * really have any problems here because "writable at construct
1430 * type only" is still more permissive than "read only".
1432 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1434 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1436 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1437 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1438 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1444 /* If the property on the interface is readable then we are
1445 * effectively advertising that reading the property will return a
1446 * value of a specific type. All implementations of the interface
1447 * need to return items of this type -- but may be more
1448 * restrictive. For example, it is legal to have:
1450 * GtkWidget *get_item();
1452 * that is implemented by a function that always returns a
1453 * GtkEntry. In short: readability implies that the
1454 * implementation value type must be equal or more restrictive.
1456 * Similarly, if the property on the interface is writable then
1457 * must be able to accept the property being set to any value of
1458 * that type, including subclasses. In this case, we may also be
1459 * less restrictive. For example, it is legal to have:
1461 * set_item (GtkEntry *);
1463 * that is implemented by a function that will actually work with
1464 * any GtkWidget. In short: writability implies that the
1465 * implementation value type must be equal or less restrictive.
1467 * In the case that the property is both readable and writable
1468 * then the only way that both of the above can be satisfied is
1469 * with a type that is exactly equal.
1471 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1473 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1474 /* class pspec value type must have exact equality with interface */
1475 if (pspecs[n]->value_type != class_pspec->value_type)
1476 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1477 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1478 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1479 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1482 case G_PARAM_READABLE:
1483 /* class pspec value type equal or more restrictive than interface */
1484 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1485 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1486 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1487 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1488 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1491 case G_PARAM_WRITABLE:
1492 /* class pspec value type equal or less restrictive than interface */
1493 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1494 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1495 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1496 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1497 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1501 g_assert_not_reached ();
1507 g_type_class_unref (class);
1511 g_object_get_type (void)
1513 return G_TYPE_OBJECT;
1517 * g_object_new: (skip)
1518 * @object_type: the type id of the #GObject subtype to instantiate
1519 * @first_property_name: the name of the first property
1520 * @...: the value of the first property, followed optionally by more
1521 * name/value pairs, followed by %NULL
1523 * Creates a new instance of a #GObject subtype and sets its properties.
1525 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1526 * which are not explicitly specified are set to their default values.
1528 * Returns: (transfer full): a new instance of @object_type
1531 g_object_new (GType object_type,
1532 const gchar *first_property_name,
1538 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1540 /* short circuit for calls supplying no properties */
1541 if (!first_property_name)
1542 return g_object_newv (object_type, 0, NULL);
1544 va_start (var_args, first_property_name);
1545 object = g_object_new_valist (object_type, first_property_name, var_args);
1552 slist_maybe_remove (GSList **slist,
1555 GSList *last = NULL, *node = *slist;
1558 if (node->data == data)
1561 last->next = node->next;
1563 *slist = node->next;
1564 g_slist_free_1 (node);
1573 static inline gboolean
1574 object_in_construction_list (GObject *object)
1576 gboolean in_construction;
1577 G_LOCK (construction_mutex);
1578 in_construction = g_slist_find (construction_objects, object) != NULL;
1579 G_UNLOCK (construction_mutex);
1580 return in_construction;
1585 * @object_type: the type id of the #GObject subtype to instantiate
1586 * @n_parameters: the length of the @parameters array
1587 * @parameters: (array length=n_parameters): an array of #GParameter
1589 * Creates a new instance of a #GObject subtype and sets its properties.
1591 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1592 * which are not explicitly specified are set to their default values.
1594 * Rename to: g_object_new
1595 * Returns: (type GObject.Object) (transfer full): a new instance of
1599 g_object_newv (GType object_type,
1601 GParameter *parameters)
1603 GObjectConstructParam *cparams = NULL, *oparams;
1604 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1606 GObjectClass *class, *unref_class = NULL;
1608 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1610 GList *clist = NULL;
1611 gboolean newly_constructed;
1614 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1616 class = g_type_class_peek_static (object_type);
1618 class = unref_class = g_type_class_ref (object_type);
1619 for (slist = class->construct_properties; slist; slist = slist->next)
1621 clist = g_list_prepend (clist, slist->data);
1622 n_total_cparams += 1;
1625 if (n_parameters == 0 && n_total_cparams == 0)
1627 /* This is a simple object with no construct properties, and
1628 * no properties are being set, so short circuit the parameter
1629 * handling. This speeds up simple object construction.
1632 object = class->constructor (object_type, 0, NULL);
1633 goto did_construction;
1636 /* collect parameters, sort into construction and normal ones */
1637 oparams = g_new (GObjectConstructParam, n_parameters);
1638 cparams = g_new (GObjectConstructParam, n_total_cparams);
1639 for (i = 0; i < n_parameters; i++)
1641 GValue *value = ¶meters[i].value;
1642 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1648 g_warning ("%s: object class `%s' has no property named `%s'",
1650 g_type_name (object_type),
1651 parameters[i].name);
1654 if (!(pspec->flags & G_PARAM_WRITABLE))
1656 g_warning ("%s: property `%s' of object class `%s' is not writable",
1659 g_type_name (object_type));
1662 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1664 GList *list = g_list_find (clist, pspec);
1668 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1669 G_STRFUNC, pspec->name, g_type_name (object_type));
1672 cparams[n_cparams].pspec = pspec;
1673 cparams[n_cparams].value = value;
1678 list->prev->next = list->next;
1680 list->next->prev = list->prev;
1681 g_list_free_1 (list);
1685 oparams[n_oparams].pspec = pspec;
1686 oparams[n_oparams].value = value;
1691 /* set remaining construction properties to default values */
1692 n_cvalues = n_total_cparams - n_cparams;
1693 cvalues = g_new (GValue, n_cvalues);
1696 GList *tmp = clist->next;
1697 GParamSpec *pspec = clist->data;
1698 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1701 g_value_init (value, pspec->value_type);
1702 g_param_value_set_default (pspec, value);
1704 cparams[n_cparams].pspec = pspec;
1705 cparams[n_cparams].value = value;
1708 g_list_free_1 (clist);
1712 /* construct object from construction parameters */
1713 object = class->constructor (object_type, n_total_cparams, cparams);
1714 /* free construction values */
1717 g_value_unset (cvalues + n_cvalues);
1721 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1723 /* adjust freeze_count according to g_object_init() and remaining properties */
1724 G_LOCK (construction_mutex);
1725 newly_constructed = slist_maybe_remove (&construction_objects, object);
1726 G_UNLOCK (construction_mutex);
1729 newly_constructed = TRUE;
1731 if (CLASS_HAS_PROPS (class))
1733 if (newly_constructed || n_oparams)
1734 nqueue = g_object_notify_queue_freeze (object, FALSE);
1735 if (newly_constructed)
1736 g_object_notify_queue_thaw (object, nqueue);
1739 /* run 'constructed' handler if there is a custom one */
1740 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1741 class->constructed (object);
1743 /* set remaining properties */
1744 for (i = 0; i < n_oparams; i++)
1745 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1748 if (CLASS_HAS_PROPS (class))
1750 /* release our own freeze count and handle notifications */
1751 if (newly_constructed || n_oparams)
1752 g_object_notify_queue_thaw (object, nqueue);
1756 g_type_class_unref (unref_class);
1762 * g_object_new_valist: (skip)
1763 * @object_type: the type id of the #GObject subtype to instantiate
1764 * @first_property_name: the name of the first property
1765 * @var_args: the value of the first property, followed optionally by more
1766 * name/value pairs, followed by %NULL
1768 * Creates a new instance of a #GObject subtype and sets its properties.
1770 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1771 * which are not explicitly specified are set to their default values.
1773 * Returns: a new instance of @object_type
1776 g_object_new_valist (GType object_type,
1777 const gchar *first_property_name,
1780 GObjectClass *class;
1784 guint n_params = 0, n_alloced_params = 16;
1786 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1788 if (!first_property_name)
1789 return g_object_newv (object_type, 0, NULL);
1791 class = g_type_class_ref (object_type);
1793 params = g_new0 (GParameter, n_alloced_params);
1794 name = first_property_name;
1797 gchar *error = NULL;
1798 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1804 g_warning ("%s: object class `%s' has no property named `%s'",
1806 g_type_name (object_type),
1810 if (n_params >= n_alloced_params)
1812 n_alloced_params += 16;
1813 params = g_renew (GParameter, params, n_alloced_params);
1814 memset (params + n_params, 0, 16 * (sizeof *params));
1816 params[n_params].name = name;
1817 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1818 var_args, 0, &error);
1821 g_warning ("%s: %s", G_STRFUNC, error);
1823 g_value_unset (¶ms[n_params].value);
1827 name = va_arg (var_args, gchar*);
1830 object = g_object_newv (object_type, n_params, params);
1833 g_value_unset (¶ms[n_params].value);
1836 g_type_class_unref (class);
1842 g_object_constructor (GType type,
1843 guint n_construct_properties,
1844 GObjectConstructParam *construct_params)
1849 object = (GObject*) g_type_create_instance (type);
1851 /* set construction parameters */
1852 if (n_construct_properties)
1854 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
1856 /* set construct properties */
1857 while (n_construct_properties--)
1859 GValue *value = construct_params->value;
1860 GParamSpec *pspec = construct_params->pspec;
1863 object_set_property (object, pspec, value, nqueue);
1865 g_object_notify_queue_thaw (object, nqueue);
1866 /* the notification queue is still frozen from g_object_init(), so
1867 * we don't need to handle it here, g_object_newv() takes
1876 g_object_constructed (GObject *object)
1878 /* empty default impl to allow unconditional upchaining */
1882 * g_object_set_valist: (skip)
1883 * @object: a #GObject
1884 * @first_property_name: name of the first property to set
1885 * @var_args: value for the first property, followed optionally by more
1886 * name/value pairs, followed by %NULL
1888 * Sets properties on an object.
1891 g_object_set_valist (GObject *object,
1892 const gchar *first_property_name,
1895 GObjectNotifyQueue *nqueue;
1898 g_return_if_fail (G_IS_OBJECT (object));
1900 g_object_ref (object);
1901 nqueue = g_object_notify_queue_freeze (object, FALSE);
1903 name = first_property_name;
1906 GValue value = G_VALUE_INIT;
1908 gchar *error = NULL;
1910 pspec = g_param_spec_pool_lookup (pspec_pool,
1912 G_OBJECT_TYPE (object),
1916 g_warning ("%s: object class `%s' has no property named `%s'",
1918 G_OBJECT_TYPE_NAME (object),
1922 if (!(pspec->flags & G_PARAM_WRITABLE))
1924 g_warning ("%s: property `%s' of object class `%s' is not writable",
1927 G_OBJECT_TYPE_NAME (object));
1930 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1932 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1933 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1937 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1941 g_warning ("%s: %s", G_STRFUNC, error);
1943 g_value_unset (&value);
1947 object_set_property (object, pspec, &value, nqueue);
1948 g_value_unset (&value);
1950 name = va_arg (var_args, gchar*);
1953 g_object_notify_queue_thaw (object, nqueue);
1954 g_object_unref (object);
1958 * g_object_get_valist: (skip)
1959 * @object: a #GObject
1960 * @first_property_name: name of the first property to get
1961 * @var_args: return location for the first property, followed optionally by more
1962 * name/return location pairs, followed by %NULL
1964 * Gets properties of an object.
1966 * In general, a copy is made of the property contents and the caller
1967 * is responsible for freeing the memory in the appropriate manner for
1968 * the type, for instance by calling g_free() or g_object_unref().
1970 * See g_object_get().
1973 g_object_get_valist (GObject *object,
1974 const gchar *first_property_name,
1979 g_return_if_fail (G_IS_OBJECT (object));
1981 g_object_ref (object);
1983 name = first_property_name;
1987 GValue value = G_VALUE_INIT;
1991 pspec = g_param_spec_pool_lookup (pspec_pool,
1993 G_OBJECT_TYPE (object),
1997 g_warning ("%s: object class `%s' has no property named `%s'",
1999 G_OBJECT_TYPE_NAME (object),
2003 if (!(pspec->flags & G_PARAM_READABLE))
2005 g_warning ("%s: property `%s' of object class `%s' is not readable",
2008 G_OBJECT_TYPE_NAME (object));
2012 g_value_init (&value, pspec->value_type);
2014 object_get_property (object, pspec, &value);
2016 G_VALUE_LCOPY (&value, var_args, 0, &error);
2019 g_warning ("%s: %s", G_STRFUNC, error);
2021 g_value_unset (&value);
2025 g_value_unset (&value);
2027 name = va_arg (var_args, gchar*);
2030 g_object_unref (object);
2034 * g_object_set: (skip)
2035 * @object: a #GObject
2036 * @first_property_name: name of the first property to set
2037 * @...: value for the first property, followed optionally by more
2038 * name/value pairs, followed by %NULL
2040 * Sets properties on an object.
2043 g_object_set (gpointer _object,
2044 const gchar *first_property_name,
2047 GObject *object = _object;
2050 g_return_if_fail (G_IS_OBJECT (object));
2052 va_start (var_args, first_property_name);
2053 g_object_set_valist (object, first_property_name, var_args);
2058 * g_object_get: (skip)
2059 * @object: a #GObject
2060 * @first_property_name: name of the first property to get
2061 * @...: return location for the first property, followed optionally by more
2062 * name/return location pairs, followed by %NULL
2064 * Gets properties of an object.
2066 * In general, a copy is made of the property contents and the caller
2067 * is responsible for freeing the memory in the appropriate manner for
2068 * the type, for instance by calling g_free() or g_object_unref().
2071 * <title>Using g_object_get(<!-- -->)</title>
2072 * An example of using g_object_get() to get the contents
2073 * of three properties - one of type #G_TYPE_INT,
2074 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
2080 * g_object_get (my_object,
2081 * "int-property", &intval,
2082 * "str-property", &strval,
2083 * "obj-property", &objval,
2086 * // Do something with intval, strval, objval
2089 * g_object_unref (objval);
2094 g_object_get (gpointer _object,
2095 const gchar *first_property_name,
2098 GObject *object = _object;
2101 g_return_if_fail (G_IS_OBJECT (object));
2103 va_start (var_args, first_property_name);
2104 g_object_get_valist (object, first_property_name, var_args);
2109 * g_object_set_property:
2110 * @object: a #GObject
2111 * @property_name: the name of the property to set
2114 * Sets a property on an object.
2117 g_object_set_property (GObject *object,
2118 const gchar *property_name,
2119 const GValue *value)
2121 GObjectNotifyQueue *nqueue;
2124 g_return_if_fail (G_IS_OBJECT (object));
2125 g_return_if_fail (property_name != NULL);
2126 g_return_if_fail (G_IS_VALUE (value));
2128 g_object_ref (object);
2129 nqueue = g_object_notify_queue_freeze (object, FALSE);
2131 pspec = g_param_spec_pool_lookup (pspec_pool,
2133 G_OBJECT_TYPE (object),
2136 g_warning ("%s: object class `%s' has no property named `%s'",
2138 G_OBJECT_TYPE_NAME (object),
2140 else if (!(pspec->flags & G_PARAM_WRITABLE))
2141 g_warning ("%s: property `%s' of object class `%s' is not writable",
2144 G_OBJECT_TYPE_NAME (object));
2145 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2146 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2147 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2149 object_set_property (object, pspec, value, nqueue);
2151 g_object_notify_queue_thaw (object, nqueue);
2152 g_object_unref (object);
2156 * g_object_get_property:
2157 * @object: a #GObject
2158 * @property_name: the name of the property to get
2159 * @value: return location for the property value
2161 * Gets a property of an object. @value must have been initialized to the
2162 * expected type of the property (or a type to which the expected type can be
2163 * transformed) using g_value_init().
2165 * In general, a copy is made of the property contents and the caller is
2166 * responsible for freeing the memory by calling g_value_unset().
2168 * Note that g_object_get_property() is really intended for language
2169 * bindings, g_object_get() is much more convenient for C programming.
2172 g_object_get_property (GObject *object,
2173 const gchar *property_name,
2178 g_return_if_fail (G_IS_OBJECT (object));
2179 g_return_if_fail (property_name != NULL);
2180 g_return_if_fail (G_IS_VALUE (value));
2182 g_object_ref (object);
2184 pspec = g_param_spec_pool_lookup (pspec_pool,
2186 G_OBJECT_TYPE (object),
2189 g_warning ("%s: object class `%s' has no property named `%s'",
2191 G_OBJECT_TYPE_NAME (object),
2193 else if (!(pspec->flags & G_PARAM_READABLE))
2194 g_warning ("%s: property `%s' of object class `%s' is not readable",
2197 G_OBJECT_TYPE_NAME (object));
2200 GValue *prop_value, tmp_value = G_VALUE_INIT;
2202 /* auto-conversion of the callers value type
2204 if (G_VALUE_TYPE (value) == pspec->value_type)
2206 g_value_reset (value);
2209 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2211 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2212 G_STRFUNC, pspec->name,
2213 g_type_name (pspec->value_type),
2214 G_VALUE_TYPE_NAME (value));
2215 g_object_unref (object);
2220 g_value_init (&tmp_value, pspec->value_type);
2221 prop_value = &tmp_value;
2223 object_get_property (object, pspec, prop_value);
2224 if (prop_value != value)
2226 g_value_transform (prop_value, value);
2227 g_value_unset (&tmp_value);
2231 g_object_unref (object);
2235 * g_object_connect: (skip)
2236 * @object: a #GObject
2237 * @signal_spec: the spec for the first signal
2238 * @...: #GCallback for the first signal, followed by data for the
2239 * first signal, followed optionally by more signal
2240 * spec/callback/data triples, followed by %NULL
2242 * A convenience function to connect multiple signals at once.
2244 * The signal specs expected by this function have the form
2245 * "modifier::signal_name", where modifier can be one of the following:
2248 * <term>signal</term>
2250 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2251 * </para></listitem>
2254 * <term>object_signal</term>
2255 * <term>object-signal</term>
2257 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2258 * </para></listitem>
2261 * <term>swapped_signal</term>
2262 * <term>swapped-signal</term>
2264 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2265 * </para></listitem>
2268 * <term>swapped_object_signal</term>
2269 * <term>swapped-object-signal</term>
2271 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2272 * </para></listitem>
2275 * <term>signal_after</term>
2276 * <term>signal-after</term>
2278 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2279 * </para></listitem>
2282 * <term>object_signal_after</term>
2283 * <term>object-signal-after</term>
2285 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2286 * </para></listitem>
2289 * <term>swapped_signal_after</term>
2290 * <term>swapped-signal-after</term>
2292 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2293 * </para></listitem>
2296 * <term>swapped_object_signal_after</term>
2297 * <term>swapped-object-signal-after</term>
2299 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2300 * </para></listitem>
2305 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2306 * "type", GTK_WINDOW_POPUP,
2309 * "signal::event", gtk_menu_window_event, menu,
2310 * "signal::size_request", gtk_menu_window_size_request, menu,
2311 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2315 * Returns: (transfer none): @object
2318 g_object_connect (gpointer _object,
2319 const gchar *signal_spec,
2322 GObject *object = _object;
2325 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2326 g_return_val_if_fail (object->ref_count > 0, object);
2328 va_start (var_args, signal_spec);
2331 GCallback callback = va_arg (var_args, GCallback);
2332 gpointer data = va_arg (var_args, gpointer);
2334 if (strncmp (signal_spec, "signal::", 8) == 0)
2335 g_signal_connect_data (object, signal_spec + 8,
2336 callback, data, NULL,
2338 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2339 strncmp (signal_spec, "object-signal::", 15) == 0)
2340 g_signal_connect_object (object, signal_spec + 15,
2343 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2344 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2345 g_signal_connect_data (object, signal_spec + 16,
2346 callback, data, NULL,
2348 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2349 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2350 g_signal_connect_object (object, signal_spec + 23,
2353 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2354 strncmp (signal_spec, "signal-after::", 14) == 0)
2355 g_signal_connect_data (object, signal_spec + 14,
2356 callback, data, NULL,
2358 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2359 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2360 g_signal_connect_object (object, signal_spec + 21,
2363 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2364 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2365 g_signal_connect_data (object, signal_spec + 22,
2366 callback, data, NULL,
2367 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2368 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2369 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2370 g_signal_connect_object (object, signal_spec + 29,
2372 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2375 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2378 signal_spec = va_arg (var_args, gchar*);
2386 * g_object_disconnect: (skip)
2387 * @object: a #GObject
2388 * @signal_spec: the spec for the first signal
2389 * @...: #GCallback for the first signal, followed by data for the first signal,
2390 * followed optionally by more signal spec/callback/data triples,
2393 * A convenience function to disconnect multiple signals at once.
2395 * The signal specs expected by this function have the form
2396 * "any_signal", which means to disconnect any signal with matching
2397 * callback and data, or "any_signal::signal_name", which only
2398 * disconnects the signal named "signal_name".
2401 g_object_disconnect (gpointer _object,
2402 const gchar *signal_spec,
2405 GObject *object = _object;
2408 g_return_if_fail (G_IS_OBJECT (object));
2409 g_return_if_fail (object->ref_count > 0);
2411 va_start (var_args, signal_spec);
2414 GCallback callback = va_arg (var_args, GCallback);
2415 gpointer data = va_arg (var_args, gpointer);
2416 guint sid = 0, detail = 0, mask = 0;
2418 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2419 strncmp (signal_spec, "any-signal::", 12) == 0)
2422 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2424 else if (strcmp (signal_spec, "any_signal") == 0 ||
2425 strcmp (signal_spec, "any-signal") == 0)
2428 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2432 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2436 if ((mask & G_SIGNAL_MATCH_ID) &&
2437 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2438 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2439 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2441 NULL, (gpointer)callback, data))
2442 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2443 signal_spec = va_arg (var_args, gchar*);
2454 } weak_refs[1]; /* flexible array */
2458 weak_refs_notify (gpointer data)
2460 WeakRefStack *wstack = data;
2463 for (i = 0; i < wstack->n_weak_refs; i++)
2464 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2469 * g_object_weak_ref: (skip)
2470 * @object: #GObject to reference weakly
2471 * @notify: callback to invoke before the object is freed
2472 * @data: extra data to pass to notify
2474 * Adds a weak reference callback to an object. Weak references are
2475 * used for notification when an object is finalized. They are called
2476 * "weak references" because they allow you to safely hold a pointer
2477 * to an object without calling g_object_ref() (g_object_ref() adds a
2478 * strong reference, that is, forces the object to stay alive).
2480 * Note that the weak references created by this method are not
2481 * thread-safe: they cannot safely be used in one thread if the
2482 * object's last g_object_unref() might happen in another thread.
2483 * Use #GWeakRef if thread-safety is required.
2486 g_object_weak_ref (GObject *object,
2490 WeakRefStack *wstack;
2493 g_return_if_fail (G_IS_OBJECT (object));
2494 g_return_if_fail (notify != NULL);
2495 g_return_if_fail (object->ref_count >= 1);
2497 G_LOCK (weak_refs_mutex);
2498 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2501 i = wstack->n_weak_refs++;
2502 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2506 wstack = g_renew (WeakRefStack, NULL, 1);
2507 wstack->object = object;
2508 wstack->n_weak_refs = 1;
2511 wstack->weak_refs[i].notify = notify;
2512 wstack->weak_refs[i].data = data;
2513 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2514 G_UNLOCK (weak_refs_mutex);
2518 * g_object_weak_unref: (skip)
2519 * @object: #GObject to remove a weak reference from
2520 * @notify: callback to search for
2521 * @data: data to search for
2523 * Removes a weak reference callback to an object.
2526 g_object_weak_unref (GObject *object,
2530 WeakRefStack *wstack;
2531 gboolean found_one = FALSE;
2533 g_return_if_fail (G_IS_OBJECT (object));
2534 g_return_if_fail (notify != NULL);
2536 G_LOCK (weak_refs_mutex);
2537 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2542 for (i = 0; i < wstack->n_weak_refs; i++)
2543 if (wstack->weak_refs[i].notify == notify &&
2544 wstack->weak_refs[i].data == data)
2547 wstack->n_weak_refs -= 1;
2548 if (i != wstack->n_weak_refs)
2549 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2554 G_UNLOCK (weak_refs_mutex);
2556 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2560 * g_object_add_weak_pointer: (skip)
2561 * @object: The object that should be weak referenced.
2562 * @weak_pointer_location: (inout): The memory address of a pointer.
2564 * Adds a weak reference from weak_pointer to @object to indicate that
2565 * the pointer located at @weak_pointer_location is only valid during
2566 * the lifetime of @object. When the @object is finalized,
2567 * @weak_pointer will be set to %NULL.
2569 * Note that as with g_object_weak_ref(), the weak references created by
2570 * this method are not thread-safe: they cannot safely be used in one
2571 * thread if the object's last g_object_unref() might happen in another
2572 * thread. Use #GWeakRef if thread-safety is required.
2575 g_object_add_weak_pointer (GObject *object,
2576 gpointer *weak_pointer_location)
2578 g_return_if_fail (G_IS_OBJECT (object));
2579 g_return_if_fail (weak_pointer_location != NULL);
2581 g_object_weak_ref (object,
2582 (GWeakNotify) g_nullify_pointer,
2583 weak_pointer_location);
2587 * g_object_remove_weak_pointer: (skip)
2588 * @object: The object that is weak referenced.
2589 * @weak_pointer_location: (inout): The memory address of a pointer.
2591 * Removes a weak reference from @object that was previously added
2592 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2593 * to match the one used with g_object_add_weak_pointer().
2596 g_object_remove_weak_pointer (GObject *object,
2597 gpointer *weak_pointer_location)
2599 g_return_if_fail (G_IS_OBJECT (object));
2600 g_return_if_fail (weak_pointer_location != NULL);
2602 g_object_weak_unref (object,
2603 (GWeakNotify) g_nullify_pointer,
2604 weak_pointer_location);
2608 object_floating_flag_handler (GObject *object,
2614 case +1: /* force floating if possible */
2616 oldvalue = g_atomic_pointer_get (&object->qdata);
2617 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2618 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2619 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2620 case -1: /* sink if possible */
2622 oldvalue = g_atomic_pointer_get (&object->qdata);
2623 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2624 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2625 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2626 default: /* check floating */
2627 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2632 * g_object_is_floating:
2633 * @object: (type GObject.Object): a #GObject
2635 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2640 * Returns: %TRUE if @object has a floating reference
2643 g_object_is_floating (gpointer _object)
2645 GObject *object = _object;
2646 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2647 return floating_flag_handler (object, 0);
2651 * g_object_ref_sink:
2652 * @object: (type GObject.Object): a #GObject
2654 * Increase the reference count of @object, and possibly remove the
2655 * <link linkend="floating-ref">floating</link> reference, if @object
2656 * has a floating reference.
2658 * In other words, if the object is floating, then this call "assumes
2659 * ownership" of the floating reference, converting it to a normal
2660 * reference by clearing the floating flag while leaving the reference
2661 * count unchanged. If the object is not floating, then this call
2662 * adds a new normal reference increasing the reference count by one.
2666 * Returns: (type GObject.Object) (transfer none): @object
2669 g_object_ref_sink (gpointer _object)
2671 GObject *object = _object;
2672 gboolean was_floating;
2673 g_return_val_if_fail (G_IS_OBJECT (object), object);
2674 g_return_val_if_fail (object->ref_count >= 1, object);
2675 g_object_ref (object);
2676 was_floating = floating_flag_handler (object, -1);
2678 g_object_unref (object);
2683 * g_object_force_floating:
2684 * @object: a #GObject
2686 * This function is intended for #GObject implementations to re-enforce a
2687 * <link linkend="floating-ref">floating</link> object reference.
2688 * Doing this is seldom required: all
2689 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2690 * usually just needs to be sunken by calling g_object_ref_sink().
2695 g_object_force_floating (GObject *object)
2697 g_return_if_fail (G_IS_OBJECT (object));
2698 g_return_if_fail (object->ref_count >= 1);
2700 floating_flag_handler (object, +1);
2705 guint n_toggle_refs;
2707 GToggleNotify notify;
2709 } toggle_refs[1]; /* flexible array */
2713 toggle_refs_notify (GObject *object,
2714 gboolean is_last_ref)
2716 ToggleRefStack tstack, *tstackptr;
2718 G_LOCK (toggle_refs_mutex);
2719 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2720 tstack = *tstackptr;
2721 G_UNLOCK (toggle_refs_mutex);
2723 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2724 * will only be notified when there is exactly one of them.
2726 g_assert (tstack.n_toggle_refs == 1);
2727 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2731 * g_object_add_toggle_ref: (skip)
2732 * @object: a #GObject
2733 * @notify: a function to call when this reference is the
2734 * last reference to the object, or is no longer
2735 * the last reference.
2736 * @data: data to pass to @notify
2738 * Increases the reference count of the object by one and sets a
2739 * callback to be called when all other references to the object are
2740 * dropped, or when this is already the last reference to the object
2741 * and another reference is established.
2743 * This functionality is intended for binding @object to a proxy
2744 * object managed by another memory manager. This is done with two
2745 * paired references: the strong reference added by
2746 * g_object_add_toggle_ref() and a reverse reference to the proxy
2747 * object which is either a strong reference or weak reference.
2749 * The setup is that when there are no other references to @object,
2750 * only a weak reference is held in the reverse direction from @object
2751 * to the proxy object, but when there are other references held to
2752 * @object, a strong reference is held. The @notify callback is called
2753 * when the reference from @object to the proxy object should be
2754 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2755 * true) or weak to strong (@is_last_ref false).
2757 * Since a (normal) reference must be held to the object before
2758 * calling g_object_add_toggle_ref(), the initial state of the reverse
2759 * link is always strong.
2761 * Multiple toggle references may be added to the same gobject,
2762 * however if there are multiple toggle references to an object, none
2763 * of them will ever be notified until all but one are removed. For
2764 * this reason, you should only ever use a toggle reference if there
2765 * is important state in the proxy object.
2770 g_object_add_toggle_ref (GObject *object,
2771 GToggleNotify notify,
2774 ToggleRefStack *tstack;
2777 g_return_if_fail (G_IS_OBJECT (object));
2778 g_return_if_fail (notify != NULL);
2779 g_return_if_fail (object->ref_count >= 1);
2781 g_object_ref (object);
2783 G_LOCK (toggle_refs_mutex);
2784 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2787 i = tstack->n_toggle_refs++;
2788 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2789 * in tstate->toggle_refs */
2790 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2794 tstack = g_renew (ToggleRefStack, NULL, 1);
2795 tstack->object = object;
2796 tstack->n_toggle_refs = 1;
2800 /* Set a flag for fast lookup after adding the first toggle reference */
2801 if (tstack->n_toggle_refs == 1)
2802 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2804 tstack->toggle_refs[i].notify = notify;
2805 tstack->toggle_refs[i].data = data;
2806 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2807 (GDestroyNotify)g_free);
2808 G_UNLOCK (toggle_refs_mutex);
2812 * g_object_remove_toggle_ref: (skip)
2813 * @object: a #GObject
2814 * @notify: a function to call when this reference is the
2815 * last reference to the object, or is no longer
2816 * the last reference.
2817 * @data: data to pass to @notify
2819 * Removes a reference added with g_object_add_toggle_ref(). The
2820 * reference count of the object is decreased by one.
2825 g_object_remove_toggle_ref (GObject *object,
2826 GToggleNotify notify,
2829 ToggleRefStack *tstack;
2830 gboolean found_one = FALSE;
2832 g_return_if_fail (G_IS_OBJECT (object));
2833 g_return_if_fail (notify != NULL);
2835 G_LOCK (toggle_refs_mutex);
2836 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2841 for (i = 0; i < tstack->n_toggle_refs; i++)
2842 if (tstack->toggle_refs[i].notify == notify &&
2843 tstack->toggle_refs[i].data == data)
2846 tstack->n_toggle_refs -= 1;
2847 if (i != tstack->n_toggle_refs)
2848 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2850 if (tstack->n_toggle_refs == 0)
2851 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2856 G_UNLOCK (toggle_refs_mutex);
2859 g_object_unref (object);
2861 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2866 * @object: (type GObject.Object): a #GObject
2868 * Increases the reference count of @object.
2870 * Returns: (type GObject.Object) (transfer none): the same @object
2873 g_object_ref (gpointer _object)
2875 GObject *object = _object;
2878 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2879 g_return_val_if_fail (object->ref_count > 0, NULL);
2881 #ifdef G_ENABLE_DEBUG
2882 if (g_trap_object_ref == object)
2884 #endif /* G_ENABLE_DEBUG */
2887 old_val = g_atomic_int_add (&object->ref_count, 1);
2889 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2890 toggle_refs_notify (object, FALSE);
2892 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2899 * @object: (type GObject.Object): a #GObject
2901 * Decreases the reference count of @object. When its reference count
2902 * drops to 0, the object is finalized (i.e. its memory is freed).
2905 g_object_unref (gpointer _object)
2907 GObject *object = _object;
2910 g_return_if_fail (G_IS_OBJECT (object));
2911 g_return_if_fail (object->ref_count > 0);
2913 #ifdef G_ENABLE_DEBUG
2914 if (g_trap_object_ref == object)
2916 #endif /* G_ENABLE_DEBUG */
2918 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2919 retry_atomic_decrement1:
2920 old_ref = g_atomic_int_get (&object->ref_count);
2923 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2924 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2926 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2927 goto retry_atomic_decrement1;
2929 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2931 /* if we went from 2->1 we need to notify toggle refs if any */
2932 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2933 toggle_refs_notify (object, TRUE);
2937 GSList **weak_locations;
2939 /* The only way that this object can live at this point is if
2940 * there are outstanding weak references already established
2941 * before we got here.
2943 * If there were not already weak references then no more can be
2944 * established at this time, because the other thread would have
2945 * to hold a strong ref in order to call
2946 * g_object_add_weak_pointer() and then we wouldn't be here.
2948 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
2950 if (weak_locations != NULL)
2952 g_rw_lock_writer_lock (&weak_locations_lock);
2954 /* It is possible that one of the weak references beat us to
2955 * the lock. Make sure the refcount is still what we expected
2958 old_ref = g_atomic_int_get (&object->ref_count);
2961 g_rw_lock_writer_unlock (&weak_locations_lock);
2962 goto retry_atomic_decrement1;
2965 /* We got the lock first, so the object will definitely die
2966 * now. Clear out all the weak references.
2968 while (*weak_locations)
2970 GWeakRef *weak_ref_location = (*weak_locations)->data;
2972 weak_ref_location->priv.p = NULL;
2973 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
2976 g_rw_lock_writer_unlock (&weak_locations_lock);
2979 /* we are about to remove the last reference */
2980 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2981 G_OBJECT_GET_CLASS (object)->dispose (object);
2982 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2984 /* may have been re-referenced meanwhile */
2985 retry_atomic_decrement2:
2986 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2989 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2990 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2992 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2993 goto retry_atomic_decrement2;
2995 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2997 /* if we went from 2->1 we need to notify toggle refs if any */
2998 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2999 toggle_refs_notify (object, TRUE);
3004 /* we are still in the process of taking away the last ref */
3005 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3006 g_signal_handlers_destroy (object);
3007 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3009 /* decrement the last reference */
3010 old_ref = g_atomic_int_add (&object->ref_count, -1);
3012 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3014 /* may have been re-referenced meanwhile */
3015 if (G_LIKELY (old_ref == 1))
3017 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3018 G_OBJECT_GET_CLASS (object)->finalize (object);
3020 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3022 #ifdef G_ENABLE_DEBUG
3025 /* catch objects not chaining finalize handlers */
3026 G_LOCK (debug_objects);
3027 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3028 G_UNLOCK (debug_objects);
3030 #endif /* G_ENABLE_DEBUG */
3031 g_type_free_instance ((GTypeInstance*) object);
3037 * g_clear_object: (skip)
3038 * @object_ptr: a pointer to a #GObject reference
3040 * Clears a reference to a #GObject.
3042 * @object_ptr must not be %NULL.
3044 * If the reference is %NULL then this function does nothing.
3045 * Otherwise, the reference count of the object is decreased and the
3046 * pointer is set to %NULL.
3048 * This function is threadsafe and modifies the pointer atomically,
3049 * using memory barriers where needed.
3051 * A macro is also included that allows this function to be used without
3056 #undef g_clear_object
3058 g_clear_object (volatile GObject **object_ptr)
3060 g_clear_pointer (object_ptr, g_object_unref);
3064 * g_object_get_qdata:
3065 * @object: The GObject to get a stored user data pointer from
3066 * @quark: A #GQuark, naming the user data pointer
3068 * This function gets back user data pointers stored via
3069 * g_object_set_qdata().
3071 * Returns: (transfer none): The user data pointer set, or %NULL
3074 g_object_get_qdata (GObject *object,
3077 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3079 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3083 * g_object_set_qdata: (skip)
3084 * @object: The GObject to set store a user data pointer
3085 * @quark: A #GQuark, naming the user data pointer
3086 * @data: An opaque user data pointer
3088 * This sets an opaque, named pointer on an object.
3089 * The name is specified through a #GQuark (retrived e.g. via
3090 * g_quark_from_static_string()), and the pointer
3091 * can be gotten back from the @object with g_object_get_qdata()
3092 * until the @object is finalized.
3093 * Setting a previously set user data pointer, overrides (frees)
3094 * the old pointer set, using #NULL as pointer essentially
3095 * removes the data stored.
3098 g_object_set_qdata (GObject *object,
3102 g_return_if_fail (G_IS_OBJECT (object));
3103 g_return_if_fail (quark > 0);
3105 g_datalist_id_set_data (&object->qdata, quark, data);
3109 * g_object_set_qdata_full: (skip)
3110 * @object: The GObject to set store a user data pointer
3111 * @quark: A #GQuark, naming the user data pointer
3112 * @data: An opaque user data pointer
3113 * @destroy: Function to invoke with @data as argument, when @data
3116 * This function works like g_object_set_qdata(), but in addition,
3117 * a void (*destroy) (gpointer) function may be specified which is
3118 * called with @data as argument when the @object is finalized, or
3119 * the data is being overwritten by a call to g_object_set_qdata()
3120 * with the same @quark.
3123 g_object_set_qdata_full (GObject *object,
3126 GDestroyNotify destroy)
3128 g_return_if_fail (G_IS_OBJECT (object));
3129 g_return_if_fail (quark > 0);
3131 g_datalist_id_set_data_full (&object->qdata, quark, data,
3132 data ? destroy : (GDestroyNotify) NULL);
3136 * g_object_steal_qdata:
3137 * @object: The GObject to get a stored user data pointer from
3138 * @quark: A #GQuark, naming the user data pointer
3140 * This function gets back user data pointers stored via
3141 * g_object_set_qdata() and removes the @data from object
3142 * without invoking its destroy() function (if any was
3144 * Usually, calling this function is only required to update
3145 * user data pointers with a destroy notifier, for example:
3148 * object_add_to_user_list (GObject *object,
3149 * const gchar *new_string)
3151 * // the quark, naming the object data
3152 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3153 * // retrive the old string list
3154 * GList *list = g_object_steal_qdata (object, quark_string_list);
3156 * // prepend new string
3157 * list = g_list_prepend (list, g_strdup (new_string));
3158 * // this changed 'list', so we need to set it again
3159 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3162 * free_string_list (gpointer data)
3164 * GList *node, *list = data;
3166 * for (node = list; node; node = node->next)
3167 * g_free (node->data);
3168 * g_list_free (list);
3171 * Using g_object_get_qdata() in the above example, instead of
3172 * g_object_steal_qdata() would have left the destroy function set,
3173 * and thus the partial string list would have been freed upon
3174 * g_object_set_qdata_full().
3176 * Returns: (transfer full): The user data pointer set, or %NULL
3179 g_object_steal_qdata (GObject *object,
3182 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3183 g_return_val_if_fail (quark > 0, NULL);
3185 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3189 * g_object_get_data:
3190 * @object: #GObject containing the associations
3191 * @key: name of the key for that association
3193 * Gets a named field from the objects table of associations (see g_object_set_data()).
3195 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3198 g_object_get_data (GObject *object,
3201 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3202 g_return_val_if_fail (key != NULL, NULL);
3204 return g_datalist_get_data (&object->qdata, key);
3208 * g_object_set_data:
3209 * @object: #GObject containing the associations.
3210 * @key: name of the key
3211 * @data: data to associate with that key
3213 * Each object carries around a table of associations from
3214 * strings to pointers. This function lets you set an association.
3216 * If the object already had an association with that name,
3217 * the old association will be destroyed.
3220 g_object_set_data (GObject *object,
3224 g_return_if_fail (G_IS_OBJECT (object));
3225 g_return_if_fail (key != NULL);
3227 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3231 * g_object_set_data_full: (skip)
3232 * @object: #GObject containing the associations
3233 * @key: name of the key
3234 * @data: data to associate with that key
3235 * @destroy: function to call when the association is destroyed
3237 * Like g_object_set_data() except it adds notification
3238 * for when the association is destroyed, either by setting it
3239 * to a different value or when the object is destroyed.
3241 * Note that the @destroy callback is not called if @data is %NULL.
3244 g_object_set_data_full (GObject *object,
3247 GDestroyNotify destroy)
3249 g_return_if_fail (G_IS_OBJECT (object));
3250 g_return_if_fail (key != NULL);
3252 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3253 data ? destroy : (GDestroyNotify) NULL);
3257 * g_object_steal_data:
3258 * @object: #GObject containing the associations
3259 * @key: name of the key
3261 * Remove a specified datum from the object's data associations,
3262 * without invoking the association's destroy handler.
3264 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3267 g_object_steal_data (GObject *object,
3272 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3273 g_return_val_if_fail (key != NULL, NULL);
3275 quark = g_quark_try_string (key);
3277 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3281 g_value_object_init (GValue *value)
3283 value->data[0].v_pointer = NULL;
3287 g_value_object_free_value (GValue *value)
3289 if (value->data[0].v_pointer)
3290 g_object_unref (value->data[0].v_pointer);
3294 g_value_object_copy_value (const GValue *src_value,
3297 if (src_value->data[0].v_pointer)
3298 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3300 dest_value->data[0].v_pointer = NULL;
3304 g_value_object_transform_value (const GValue *src_value,
3307 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)))
3308 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3310 dest_value->data[0].v_pointer = NULL;
3314 g_value_object_peek_pointer (const GValue *value)
3316 return value->data[0].v_pointer;
3320 g_value_object_collect_value (GValue *value,
3321 guint n_collect_values,
3322 GTypeCValue *collect_values,
3323 guint collect_flags)
3325 if (collect_values[0].v_pointer)
3327 GObject *object = collect_values[0].v_pointer;
3329 if (object->g_type_instance.g_class == NULL)
3330 return g_strconcat ("invalid unclassed object pointer for value type `",
3331 G_VALUE_TYPE_NAME (value),
3334 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3335 return g_strconcat ("invalid object type `",
3336 G_OBJECT_TYPE_NAME (object),
3337 "' for value type `",
3338 G_VALUE_TYPE_NAME (value),
3341 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3342 value->data[0].v_pointer = g_object_ref (object);
3345 value->data[0].v_pointer = NULL;
3351 g_value_object_lcopy_value (const GValue *value,
3352 guint n_collect_values,
3353 GTypeCValue *collect_values,
3354 guint collect_flags)
3356 GObject **object_p = collect_values[0].v_pointer;
3359 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3361 if (!value->data[0].v_pointer)
3363 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3364 *object_p = value->data[0].v_pointer;
3366 *object_p = g_object_ref (value->data[0].v_pointer);
3372 * g_value_set_object:
3373 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3374 * @v_object: (type GObject.Object) (allow-none): object value to be set
3376 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3378 * g_value_set_object() increases the reference count of @v_object
3379 * (the #GValue holds a reference to @v_object). If you do not wish
3380 * to increase the reference count of the object (i.e. you wish to
3381 * pass your current reference to the #GValue because you no longer
3382 * need it), use g_value_take_object() instead.
3384 * It is important that your #GValue holds a reference to @v_object (either its
3385 * own, or one it has taken) to ensure that the object won't be destroyed while
3386 * the #GValue still exists).
3389 g_value_set_object (GValue *value,
3394 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3396 old = value->data[0].v_pointer;
3400 g_return_if_fail (G_IS_OBJECT (v_object));
3401 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3403 value->data[0].v_pointer = v_object;
3404 g_object_ref (value->data[0].v_pointer);
3407 value->data[0].v_pointer = NULL;
3410 g_object_unref (old);
3414 * g_value_set_object_take_ownership: (skip)
3415 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3416 * @v_object: (allow-none): object value to be set
3418 * This is an internal function introduced mainly for C marshallers.
3420 * Deprecated: 2.4: Use g_value_take_object() instead.
3423 g_value_set_object_take_ownership (GValue *value,
3426 g_value_take_object (value, v_object);
3430 * g_value_take_object: (skip)
3431 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3432 * @v_object: (allow-none): object value to be set
3434 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3435 * and takes over the ownership of the callers reference to @v_object;
3436 * the caller doesn't have to unref it any more (i.e. the reference
3437 * count of the object is not increased).
3439 * If you want the #GValue to hold its own reference to @v_object, use
3440 * g_value_set_object() instead.
3445 g_value_take_object (GValue *value,
3448 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3450 if (value->data[0].v_pointer)
3452 g_object_unref (value->data[0].v_pointer);
3453 value->data[0].v_pointer = NULL;
3458 g_return_if_fail (G_IS_OBJECT (v_object));
3459 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3461 value->data[0].v_pointer = v_object; /* we take over the reference count */
3466 * g_value_get_object:
3467 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3469 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3471 * Returns: (type GObject.Object) (transfer none): object contents of @value
3474 g_value_get_object (const GValue *value)
3476 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3478 return value->data[0].v_pointer;
3482 * g_value_dup_object:
3483 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3485 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3486 * its reference count. If the contents of the #GValue are %NULL, then
3487 * %NULL will be returned.
3489 * Returns: (type GObject.Object) (transfer full): object content of @value,
3490 * should be unreferenced when no longer needed.
3493 g_value_dup_object (const GValue *value)
3495 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3497 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3501 * g_signal_connect_object: (skip)
3502 * @instance: the instance to connect to.
3503 * @detailed_signal: a string of the form "signal-name::detail".
3504 * @c_handler: the #GCallback to connect.
3505 * @gobject: the object to pass as data to @c_handler.
3506 * @connect_flags: a combination of #GConnectFlags.
3508 * This is similar to g_signal_connect_data(), but uses a closure which
3509 * ensures that the @gobject stays alive during the call to @c_handler
3510 * by temporarily adding a reference count to @gobject.
3512 * Note that there is a bug in GObject that makes this function
3513 * much less useful than it might seem otherwise. Once @gobject is
3514 * disposed, the callback will no longer be called, but, the signal
3515 * handler is <emphasis>not</emphasis> currently disconnected. If the
3516 * @instance is itself being freed at the same time than this doesn't
3517 * matter, since the signal will automatically be removed, but
3518 * if @instance persists, then the signal handler will leak. You
3519 * should not remove the signal yourself because in a future versions of
3520 * GObject, the handler <emphasis>will</emphasis> automatically
3523 * It's possible to work around this problem in a way that will
3524 * continue to work with future versions of GObject by checking
3525 * that the signal handler is still connected before disconnected it:
3526 * <informalexample><programlisting>
3527 * if (g_signal_handler_is_connected (instance, id))
3528 * g_signal_handler_disconnect (instance, id);
3529 * </programlisting></informalexample>
3531 * Returns: the handler id.
3534 g_signal_connect_object (gpointer instance,
3535 const gchar *detailed_signal,
3536 GCallback c_handler,
3538 GConnectFlags connect_flags)
3540 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3541 g_return_val_if_fail (detailed_signal != NULL, 0);
3542 g_return_val_if_fail (c_handler != NULL, 0);
3548 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3550 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3552 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3555 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3561 GClosure *closures[1]; /* flexible array */
3563 /* don't change this structure without supplying an accessor for
3564 * watched closures, e.g.:
3565 * GSList* g_object_list_watched_closures (GObject *object)
3568 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3569 * carray = g_object_get_data (object, "GObject-closure-array");
3572 * GSList *slist = NULL;
3574 * for (i = 0; i < carray->n_closures; i++)
3575 * slist = g_slist_prepend (slist, carray->closures[i]);
3583 object_remove_closure (gpointer data,
3586 GObject *object = data;
3590 G_LOCK (closure_array_mutex);
3591 carray = g_object_get_qdata (object, quark_closure_array);
3592 for (i = 0; i < carray->n_closures; i++)
3593 if (carray->closures[i] == closure)
3595 carray->n_closures--;
3596 if (i < carray->n_closures)
3597 carray->closures[i] = carray->closures[carray->n_closures];
3598 G_UNLOCK (closure_array_mutex);
3601 G_UNLOCK (closure_array_mutex);
3602 g_assert_not_reached ();
3606 destroy_closure_array (gpointer data)
3608 CArray *carray = data;
3609 GObject *object = carray->object;
3610 guint i, n = carray->n_closures;
3612 for (i = 0; i < n; i++)
3614 GClosure *closure = carray->closures[i];
3616 /* removing object_remove_closure() upfront is probably faster than
3617 * letting it fiddle with quark_closure_array which is empty anyways
3619 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3620 g_closure_invalidate (closure);
3626 * g_object_watch_closure:
3627 * @object: GObject restricting lifetime of @closure
3628 * @closure: GClosure to watch
3630 * This function essentially limits the life time of the @closure to
3631 * the life time of the object. That is, when the object is finalized,
3632 * the @closure is invalidated by calling g_closure_invalidate() on
3633 * it, in order to prevent invocations of the closure with a finalized
3634 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3635 * added as marshal guards to the @closure, to ensure that an extra
3636 * reference count is held on @object during invocation of the
3637 * @closure. Usually, this function will be called on closures that
3638 * use this @object as closure data.
3641 g_object_watch_closure (GObject *object,
3647 g_return_if_fail (G_IS_OBJECT (object));
3648 g_return_if_fail (closure != NULL);
3649 g_return_if_fail (closure->is_invalid == FALSE);
3650 g_return_if_fail (closure->in_marshal == FALSE);
3651 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3653 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3654 g_closure_add_marshal_guards (closure,
3655 object, (GClosureNotify) g_object_ref,
3656 object, (GClosureNotify) g_object_unref);
3657 G_LOCK (closure_array_mutex);
3658 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3661 carray = g_renew (CArray, NULL, 1);
3662 carray->object = object;
3663 carray->n_closures = 1;
3668 i = carray->n_closures++;
3669 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3671 carray->closures[i] = closure;
3672 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3673 G_UNLOCK (closure_array_mutex);
3677 * g_closure_new_object:
3678 * @sizeof_closure: the size of the structure to allocate, must be at least
3679 * <literal>sizeof (GClosure)</literal>
3680 * @object: a #GObject pointer to store in the @data field of the newly
3681 * allocated #GClosure
3683 * A variant of g_closure_new_simple() which stores @object in the
3684 * @data field of the closure and calls g_object_watch_closure() on
3685 * @object and the created closure. This function is mainly useful
3686 * when implementing new types of closures.
3688 * Returns: (transfer full): a newly allocated #GClosure
3691 g_closure_new_object (guint sizeof_closure,
3696 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3697 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3699 closure = g_closure_new_simple (sizeof_closure, object);
3700 g_object_watch_closure (object, closure);
3706 * g_cclosure_new_object: (skip)
3707 * @callback_func: the function to invoke
3708 * @object: a #GObject pointer to pass to @callback_func
3710 * A variant of g_cclosure_new() which uses @object as @user_data and
3711 * calls g_object_watch_closure() on @object and the created
3712 * closure. This function is useful when you have a callback closely
3713 * associated with a #GObject, and want the callback to no longer run
3714 * after the object is is freed.
3716 * Returns: a new #GCClosure
3719 g_cclosure_new_object (GCallback callback_func,
3724 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3725 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3726 g_return_val_if_fail (callback_func != NULL, NULL);
3728 closure = g_cclosure_new (callback_func, object, NULL);
3729 g_object_watch_closure (object, closure);
3735 * g_cclosure_new_object_swap: (skip)
3736 * @callback_func: the function to invoke
3737 * @object: a #GObject pointer to pass to @callback_func
3739 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3740 * and calls g_object_watch_closure() on @object and the created
3741 * closure. This function is useful when you have a callback closely
3742 * associated with a #GObject, and want the callback to no longer run
3743 * after the object is is freed.
3745 * Returns: a new #GCClosure
3748 g_cclosure_new_object_swap (GCallback callback_func,
3753 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3754 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3755 g_return_val_if_fail (callback_func != NULL, NULL);
3757 closure = g_cclosure_new_swap (callback_func, object, NULL);
3758 g_object_watch_closure (object, closure);
3764 g_object_compat_control (gsize what,
3770 case 1: /* floating base type */
3771 return G_TYPE_INITIALLY_UNOWNED;
3772 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3773 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3775 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3777 *pp = floating_flag_handler;
3784 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3787 g_initially_unowned_init (GInitiallyUnowned *object)
3789 g_object_force_floating (object);
3793 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3800 * A structure containing a weak reference to a #GObject. It can either
3801 * be empty (i.e. point to %NULL), or point to an object for as long as
3802 * at least one "strong" reference to that object exists. Before the
3803 * object's #GObjectClass.dispose method is called, every #GWeakRef
3804 * associated with becomes empty (i.e. points to %NULL).
3806 * Like #GValue, #GWeakRef can be statically allocated, stack- or
3807 * heap-allocated, or embedded in larger structures.
3809 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
3810 * reference is thread-safe: converting a weak pointer to a reference is
3811 * atomic with respect to invalidation of weak pointers to destroyed
3814 * If the object's #GObjectClass.dispose method results in additional
3815 * references to the object being held, any #GWeakRef<!-- -->s taken
3816 * before it was disposed will continue to point to %NULL. If
3817 * #GWeakRef<!-- -->s are taken after the object is disposed and
3818 * re-referenced, they will continue to point to it until its refcount
3819 * goes back to zero, at which point they too will be invalidated.
3823 * g_weak_ref_init: (skip)
3824 * @weak_ref: (inout): uninitialized or empty location for a weak
3826 * @object: (allow-none): a #GObject or %NULL
3828 * Initialise a non-statically-allocated #GWeakRef.
3830 * This function also calls g_weak_ref_set() with @object on the
3831 * freshly-initialised weak reference.
3833 * This function should always be matched with a call to
3834 * g_weak_ref_clear(). It is not necessary to use this function for a
3835 * #GWeakRef in static storage because it will already be
3836 * properly initialised. Just use g_weak_ref_set() directly.
3841 g_weak_ref_init (GWeakRef *weak_ref,
3844 weak_ref->priv.p = NULL;
3846 g_weak_ref_set (weak_ref, object);
3850 * g_weak_ref_clear: (skip)
3851 * @weak_ref: (inout): location of a weak reference, which
3854 * Frees resources associated with a non-statically-allocated #GWeakRef.
3855 * After this call, the #GWeakRef is left in an undefined state.
3857 * You should only call this on a #GWeakRef that previously had
3858 * g_weak_ref_init() called on it.
3863 g_weak_ref_clear (GWeakRef *weak_ref)
3865 g_weak_ref_set (weak_ref, NULL);
3868 weak_ref->priv.p = (void *) 0xccccccccu;
3872 * g_weak_ref_get: (skip)
3873 * @weak_ref: (inout): location of a weak reference to a #GObject
3875 * If @weak_ref is not empty, atomically acquire a strong
3876 * reference to the object it points to, and return that reference.
3878 * This function is needed because of the potential race between taking
3879 * the pointer value and g_object_ref() on it, if the object was losing
3880 * its last reference at the same time in a different thread.
3882 * The caller should release the resulting reference in the usual way,
3883 * by using g_object_unref().
3885 * Returns: (transfer full) (type GObject.Object): the object pointed to
3886 * by @weak_ref, or %NULL if it was empty
3891 g_weak_ref_get (GWeakRef *weak_ref)
3893 gpointer object_or_null;
3895 g_return_val_if_fail (weak_ref!= NULL, NULL);
3897 g_rw_lock_reader_lock (&weak_locations_lock);
3899 object_or_null = weak_ref->priv.p;
3901 if (object_or_null != NULL)
3902 g_object_ref (object_or_null);
3904 g_rw_lock_reader_unlock (&weak_locations_lock);
3906 return object_or_null;
3910 * g_weak_ref_set: (skip)
3911 * @weak_ref: location for a weak reference
3912 * @object: (allow-none): a #GObject or %NULL
3914 * Change the object to which @weak_ref points, or set it to
3917 * You must own a strong reference on @object while calling this
3923 g_weak_ref_set (GWeakRef *weak_ref,
3926 GSList **weak_locations;
3927 GObject *new_object;
3928 GObject *old_object;
3930 g_return_if_fail (weak_ref != NULL);
3931 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
3933 new_object = object;
3935 g_rw_lock_writer_lock (&weak_locations_lock);
3937 /* We use the extra level of indirection here so that if we have ever
3938 * had a weak pointer installed at any point in time on this object,
3939 * we can see that there is a non-NULL value associated with the
3940 * weak-pointer quark and know that this value will not change at any
3941 * point in the object's lifetime.
3943 * Both properties are important for reducing the amount of times we
3944 * need to acquire locks and for decreasing the duration of time the
3945 * lock is held while avoiding some rather tricky races.
3947 * Specifically: we can avoid having to do an extra unconditional lock
3948 * in g_object_unref() without worrying about some extremely tricky
3952 old_object = weak_ref->priv.p;
3953 if (new_object != old_object)
3955 weak_ref->priv.p = new_object;
3957 /* Remove the weak ref from the old object */
3958 if (old_object != NULL)
3960 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
3961 /* for it to point to an object, the object must have had it added once */
3962 g_assert (weak_locations != NULL);
3964 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
3967 /* Add the weak ref to the new object */
3968 if (new_object != NULL)
3970 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
3972 if (weak_locations == NULL)
3974 weak_locations = g_new0 (GSList *, 1);
3975 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
3978 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
3982 g_rw_lock_writer_unlock (&weak_locations_lock);