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.
29 #include "glib/gdatasetprivate.h"
32 #include "gvaluecollector.h"
34 #include "gparamspecs.h"
35 #include "gvaluetypes.h"
36 #include "gobject_trace.h"
37 #include "gobjectalias.h"
39 /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */
40 #include "gobjectnotifyqueue.c"
45 * @short_description: The base object type
46 * @see_also: #GParamSpecObject, g_param_spec_object()
47 * @title: The Base Object Type
49 * GObject is the fundamental type providing the common attributes and
50 * methods for all object types in GTK+, Pango and other libraries
51 * based on GObject. The GObject class provides methods for object
52 * construction and destruction, property access methods, and signal
53 * support. Signals are described in detail in <xref
54 * linkend="gobject-Signals"/>.
56 * <para id="floating-ref">
57 * #GInitiallyUnowned is derived from #GObject. The only difference between
58 * the two is that the initial reference of a #GInitiallyUnowned is flagged
59 * as a <firstterm>floating</firstterm> reference.
60 * This means that it is not specifically claimed to be "owned" by
61 * any code portion. The main motivation for providing floating references is
62 * C convenience. In particular, it allows code to be written as:
64 * container = create_container();
65 * container_add_child (container, create_child());
67 * If <function>container_add_child()</function> will g_object_ref_sink() the
68 * passed in child, no reference of the newly created child is leaked.
69 * Without floating references, <function>container_add_child()</function>
70 * can only g_object_ref() the new child, so to implement this code without
71 * reference leaks, it would have to be written as:
74 * container = create_container();
75 * child = create_child();
76 * container_add_child (container, child);
77 * g_object_unref (child);
79 * The floating reference can be converted into
80 * an ordinary reference by calling g_object_ref_sink().
81 * For already sunken objects (objects that don't have a floating reference
82 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
84 * Since floating references are useful almost exclusively for C convenience,
85 * language bindings that provide automated reference and memory ownership
86 * maintenance (such as smart pointers or garbage collection) therefore don't
87 * need to expose floating references in their API.
90 * Some object implementations may need to save an objects floating state
91 * across certain code portions (an example is #GtkMenu), to achive this, the
92 * following sequence can be used:
95 * // save floating state
96 * gboolean was_floating = g_object_is_floating (object);
97 * g_object_ref_sink (object);
98 * // protected code portion
100 * // restore floating state
102 * g_object_force_floating (object);
103 * g_obejct_unref (object); // release previously acquired reference
109 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
110 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
112 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
113 #define OBJECT_HAS_TOGGLE_REF(object) \
114 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
115 #define OBJECT_FLOATING_FLAG 0x2
117 #define CLASS_HAS_PROPS_FLAG 0x1
118 #define CLASS_HAS_PROPS(class) \
119 ((class)->flags & CLASS_HAS_PROPS_FLAG)
120 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
121 ((class)->constructor != g_object_constructor)
123 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
124 #define CLASS_HAS_DERIVED_CLASS(class) \
125 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
127 /* --- signals --- */
134 /* --- properties --- */
140 /* --- prototypes --- */
141 static void g_object_base_class_init (GObjectClass *class);
142 static void g_object_base_class_finalize (GObjectClass *class);
143 static void g_object_do_class_init (GObjectClass *class);
144 static void g_object_init (GObject *object,
145 GObjectClass *class);
146 static GObject* g_object_constructor (GType type,
147 guint n_construct_properties,
148 GObjectConstructParam *construct_params);
149 static void g_object_real_dispose (GObject *object);
150 static void g_object_finalize (GObject *object);
151 static void g_object_do_set_property (GObject *object,
155 static void g_object_do_get_property (GObject *object,
159 static void g_value_object_init (GValue *value);
160 static void g_value_object_free_value (GValue *value);
161 static void g_value_object_copy_value (const GValue *src_value,
163 static void g_value_object_transform_value (const GValue *src_value,
165 static gpointer g_value_object_peek_pointer (const GValue *value);
166 static gchar* g_value_object_collect_value (GValue *value,
167 guint n_collect_values,
168 GTypeCValue *collect_values,
169 guint collect_flags);
170 static gchar* g_value_object_lcopy_value (const GValue *value,
171 guint n_collect_values,
172 GTypeCValue *collect_values,
173 guint collect_flags);
174 static void g_object_dispatch_properties_changed (GObject *object,
176 GParamSpec **pspecs);
177 static inline void object_get_property (GObject *object,
180 static inline void object_set_property (GObject *object,
183 GObjectNotifyQueue *nqueue);
184 static guint object_floating_flag_handler (GObject *object,
187 static void object_interface_check_properties (gpointer func_data,
191 /* --- variables --- */
192 static GQuark quark_closure_array = 0;
193 static GQuark quark_weak_refs = 0;
194 static GQuark quark_toggle_refs = 0;
195 static GParamSpecPool *pspec_pool = NULL;
196 static GObjectNotifyContext property_notify_context = { 0, };
197 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
198 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
199 G_LOCK_DEFINE_STATIC (construction_mutex);
200 static GSList *construction_objects = NULL;
202 /* --- functions --- */
203 #ifdef G_ENABLE_DEBUG
204 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
205 G_LOCK_DEFINE_STATIC (debug_objects);
206 static volatile GObject *g_trap_object_ref = NULL;
207 static guint debug_objects_count = 0;
208 static GHashTable *debug_objects_ht = NULL;
211 debug_objects_foreach (gpointer key,
215 GObject *object = value;
217 g_message ("[%p] stale %s\tref_count=%u",
219 G_OBJECT_TYPE_NAME (object),
224 debug_objects_atexit (void)
228 G_LOCK (debug_objects);
229 g_message ("stale GObjects: %u", debug_objects_count);
230 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
231 G_UNLOCK (debug_objects);
234 #endif /* G_ENABLE_DEBUG */
237 g_object_type_init (void)
239 static gboolean initialized = FALSE;
240 static const GTypeFundamentalInfo finfo = {
241 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
243 static GTypeInfo info = {
244 sizeof (GObjectClass),
245 (GBaseInitFunc) g_object_base_class_init,
246 (GBaseFinalizeFunc) g_object_base_class_finalize,
247 (GClassInitFunc) g_object_do_class_init,
248 NULL /* class_destroy */,
249 NULL /* class_data */,
252 (GInstanceInitFunc) g_object_init,
253 NULL, /* value_table */
255 static const GTypeValueTable value_table = {
256 g_value_object_init, /* value_init */
257 g_value_object_free_value, /* value_free */
258 g_value_object_copy_value, /* value_copy */
259 g_value_object_peek_pointer, /* value_peek_pointer */
260 "p", /* collect_format */
261 g_value_object_collect_value, /* collect_value */
262 "p", /* lcopy_format */
263 g_value_object_lcopy_value, /* lcopy_value */
267 g_return_if_fail (initialized == FALSE);
272 info.value_table = &value_table;
273 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
274 g_assert (type == G_TYPE_OBJECT);
275 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
277 #ifdef G_ENABLE_DEBUG
280 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
281 g_atexit (debug_objects_atexit);
283 #endif /* G_ENABLE_DEBUG */
287 g_object_base_class_init (GObjectClass *class)
289 GObjectClass *pclass = g_type_class_peek_parent (class);
291 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
292 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
295 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
297 /* reset instance specific fields and methods that don't get inherited */
298 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
299 class->get_property = NULL;
300 class->set_property = NULL;
304 g_object_base_class_finalize (GObjectClass *class)
308 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
310 g_slist_free (class->construct_properties);
311 class->construct_properties = NULL;
312 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
313 for (node = list; node; node = node->next)
315 GParamSpec *pspec = node->data;
317 g_param_spec_pool_remove (pspec_pool, pspec);
318 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
319 g_param_spec_unref (pspec);
325 g_object_notify_dispatcher (GObject *object,
329 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
333 g_object_do_class_init (GObjectClass *class)
335 /* read the comment about typedef struct CArray; on why not to change this quark */
336 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
338 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
339 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
340 pspec_pool = g_param_spec_pool_new (TRUE);
341 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
342 property_notify_context.dispatcher = g_object_notify_dispatcher;
344 class->constructor = g_object_constructor;
345 class->set_property = g_object_do_set_property;
346 class->get_property = g_object_do_get_property;
347 class->dispose = g_object_real_dispose;
348 class->finalize = g_object_finalize;
349 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
350 class->notify = NULL;
354 * @gobject: the object which received the signal.
355 * @pspec: the #GParamSpec of the property which changed.
357 * The notify signal is emitted on an object when one of its
358 * properties has been changed. Note that getting this signal
359 * doesn't guarantee that the value of the property has actually
360 * changed, it may also be emitted when the setter for the property
361 * is called to reinstate the previous value.
363 * This signal is typically used to obtain change notification for a
364 * single property, by specifying the property name as a detail in the
365 * g_signal_connect() call, like this:
367 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
368 * G_CALLBACK (gtk_text_view_target_list_notify),
371 * It is important to note that you must use
372 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
373 * detail strings for the notify signal.
375 gobject_signals[NOTIFY] =
376 g_signal_new (g_intern_static_string ("notify"),
377 G_TYPE_FROM_CLASS (class),
378 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
379 G_STRUCT_OFFSET (GObjectClass, notify),
381 g_cclosure_marshal_VOID__PARAM,
385 /* Install a check function that we'll use to verify that classes that
386 * implement an interface implement all properties for that interface
388 g_type_add_interface_check (NULL, object_interface_check_properties);
392 install_property_internal (GType g_type,
396 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
398 g_warning ("When installing property: type `%s' already has a property named `%s'",
399 g_type_name (g_type),
404 g_param_spec_ref (pspec);
405 g_param_spec_sink (pspec);
406 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
407 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
411 * g_object_class_install_property:
412 * @oclass: a #GObjectClass
413 * @property_id: the id for the new property
414 * @pspec: the #GParamSpec for the new property
416 * Installs a new property. This is usually done in the class initializer.
418 * Note that it is possible to redefine a property in a derived class,
419 * by installing a property with the same name. This can be useful at times,
420 * e.g. to change the range of allowed values or the default value.
423 g_object_class_install_property (GObjectClass *class,
427 g_return_if_fail (G_IS_OBJECT_CLASS (class));
428 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
430 if (CLASS_HAS_DERIVED_CLASS (class))
431 g_error ("Attempt to add property %s::%s to class after it was derived",
432 G_OBJECT_CLASS_NAME (class), pspec->name);
434 class->flags |= CLASS_HAS_PROPS_FLAG;
436 if (pspec->flags & G_PARAM_WRITABLE)
437 g_return_if_fail (class->set_property != NULL);
438 if (pspec->flags & G_PARAM_READABLE)
439 g_return_if_fail (class->get_property != NULL);
440 g_return_if_fail (property_id > 0);
441 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
442 if (pspec->flags & G_PARAM_CONSTRUCT)
443 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
444 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
445 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
447 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
449 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
450 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
452 /* for property overrides of construct poperties, we have to get rid
453 * of the overidden inherited construct property
455 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
456 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
457 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
461 * g_object_interface_install_property:
462 * @g_iface: any interface vtable for the interface, or the default
463 * vtable for the interface.
464 * @pspec: the #GParamSpec for the new property
466 * Add a property to an interface; this is only useful for interfaces
467 * that are added to GObject-derived types. Adding a property to an
468 * interface forces all objects classes with that interface to have a
469 * compatible property. The compatible property could be a newly
470 * created #GParamSpec, but normally
471 * g_object_class_override_property() will be used so that the object
472 * class only needs to provide an implementation and inherits the
473 * property description, default value, bounds, and so forth from the
474 * interface property.
476 * This function is meant to be called from the interface's default
477 * vtable initialization function (the @class_init member of
478 * #GTypeInfo.) It must not be called after after @class_init has
479 * been called for any object types implementing this interface.
484 g_object_interface_install_property (gpointer g_iface,
487 GTypeInterface *iface_class = g_iface;
489 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
490 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
491 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
492 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
494 install_property_internal (iface_class->g_type, 0, pspec);
498 * g_object_class_find_property:
499 * @oclass: a #GObjectClass
500 * @property_name: the name of the property to look up
502 * Looks up the #GParamSpec for a property of a class.
504 * Returns: the #GParamSpec for the property, or %NULL if the class
505 * doesn't have a property of that name
508 g_object_class_find_property (GObjectClass *class,
509 const gchar *property_name)
512 GParamSpec *redirect;
514 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
515 g_return_val_if_fail (property_name != NULL, NULL);
517 pspec = g_param_spec_pool_lookup (pspec_pool,
519 G_OBJECT_CLASS_TYPE (class),
523 redirect = g_param_spec_get_redirect_target (pspec);
534 * g_object_interface_find_property:
535 * @g_iface: any interface vtable for the interface, or the default
536 * vtable for the interface
537 * @property_name: name of a property to lookup.
539 * Find the #GParamSpec with the given name for an
540 * interface. Generally, the interface vtable passed in as @g_iface
541 * will be the default vtable from g_type_default_interface_ref(), or,
542 * if you know the interface has already been loaded,
543 * g_type_default_interface_peek().
547 * Returns: the #GParamSpec for the property of the interface with the
548 * name @property_name, or %NULL if no such property exists.
551 g_object_interface_find_property (gpointer g_iface,
552 const gchar *property_name)
554 GTypeInterface *iface_class = g_iface;
556 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
557 g_return_val_if_fail (property_name != NULL, NULL);
559 return g_param_spec_pool_lookup (pspec_pool,
566 * g_object_class_override_property:
567 * @oclass: a #GObjectClass
568 * @property_id: the new property ID
569 * @name: the name of a property registered in a parent class or
570 * in an interface of this class.
572 * Registers @property_id as referring to a property with the
573 * name @name in a parent class or in an interface implemented
574 * by @oclass. This allows this class to <firstterm>override</firstterm>
575 * a property implementation in a parent class or to provide
576 * the implementation of a property from an interface.
579 * Internally, overriding is implemented by creating a property of type
580 * #GParamSpecOverride; generally operations that query the properties of
581 * the object class, such as g_object_class_find_property() or
582 * g_object_class_list_properties() will return the overridden
583 * property. However, in one case, the @construct_properties argument of
584 * the @constructor virtual function, the #GParamSpecOverride is passed
585 * instead, so that the @param_id field of the #GParamSpec will be
586 * correct. For virtually all uses, this makes no difference. If you
587 * need to get the overridden property, you can call
588 * g_param_spec_get_redirect_target().
594 g_object_class_override_property (GObjectClass *oclass,
598 GParamSpec *overridden = NULL;
602 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
603 g_return_if_fail (property_id > 0);
604 g_return_if_fail (name != NULL);
606 /* Find the overridden property; first check parent types
608 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
609 if (parent_type != G_TYPE_NONE)
610 overridden = g_param_spec_pool_lookup (pspec_pool,
619 /* Now check interfaces
621 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
622 while (n_ifaces-- && !overridden)
624 overridden = g_param_spec_pool_lookup (pspec_pool,
635 g_warning ("%s: Can't find property to override for '%s::%s'",
636 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
640 new = g_param_spec_override (name, overridden);
641 g_object_class_install_property (oclass, property_id, new);
645 * g_object_class_list_properties:
646 * @oclass: a #GObjectClass
647 * @n_properties: return location for the length of the returned array
649 * Get an array of #GParamSpec* for all properties of a class.
651 * Returns: an array of #GParamSpec* which should be freed after use
653 GParamSpec** /* free result */
654 g_object_class_list_properties (GObjectClass *class,
655 guint *n_properties_p)
660 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
662 pspecs = g_param_spec_pool_list (pspec_pool,
663 G_OBJECT_CLASS_TYPE (class),
672 * g_object_interface_list_properties:
673 * @g_iface: any interface vtable for the interface, or the default
674 * vtable for the interface
675 * @n_properties_p: location to store number of properties returned.
677 * Lists the properties of an interface.Generally, the interface
678 * vtable passed in as @g_iface will be the default vtable from
679 * g_type_default_interface_ref(), or, if you know the interface has
680 * already been loaded, g_type_default_interface_peek().
684 * Returns: a pointer to an array of pointers to #GParamSpec
685 * structures. The paramspecs are owned by GLib, but the
686 * array should be freed with g_free() when you are done with
690 g_object_interface_list_properties (gpointer g_iface,
691 guint *n_properties_p)
693 GTypeInterface *iface_class = g_iface;
697 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
699 pspecs = g_param_spec_pool_list (pspec_pool,
709 g_object_init (GObject *object,
712 object->ref_count = 1;
713 g_datalist_init (&object->qdata);
715 if (CLASS_HAS_PROPS (class))
717 /* freeze object's notification queue, g_object_newv() preserves pairedness */
718 g_object_notify_queue_freeze (object, &property_notify_context);
721 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
723 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
724 G_LOCK (construction_mutex);
725 construction_objects = g_slist_prepend (construction_objects, object);
726 G_UNLOCK (construction_mutex);
729 #ifdef G_ENABLE_DEBUG
732 G_LOCK (debug_objects);
733 debug_objects_count++;
734 g_hash_table_insert (debug_objects_ht, object, object);
735 G_UNLOCK (debug_objects);
737 #endif /* G_ENABLE_DEBUG */
741 g_object_do_set_property (GObject *object,
749 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
755 g_object_do_get_property (GObject *object,
763 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
769 g_object_real_dispose (GObject *object)
771 g_signal_handlers_destroy (object);
772 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
773 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
777 g_object_finalize (GObject *object)
779 g_datalist_clear (&object->qdata);
781 #ifdef G_ENABLE_DEBUG
784 G_LOCK (debug_objects);
785 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
786 g_hash_table_remove (debug_objects_ht, object);
787 debug_objects_count--;
788 G_UNLOCK (debug_objects);
790 #endif /* G_ENABLE_DEBUG */
795 g_object_dispatch_properties_changed (GObject *object,
801 for (i = 0; i < n_pspecs; i++)
802 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
806 * g_object_run_dispose:
807 * @object: a #GObject
809 * Releases all references to other objects. This can be used to break
812 * This functions should only be called from object system implementations.
815 g_object_run_dispose (GObject *object)
817 g_return_if_fail (G_IS_OBJECT (object));
818 g_return_if_fail (object->ref_count > 0);
820 g_object_ref (object);
821 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
822 G_OBJECT_GET_CLASS (object)->dispose (object);
823 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
824 g_object_unref (object);
828 * g_object_freeze_notify:
829 * @object: a #GObject
831 * Increases the freeze count on @object. If the freeze count is
832 * non-zero, the emission of "notify" signals on @object is
833 * stopped. The signals are queued until the freeze count is decreased
836 * This is necessary for accessors that modify multiple properties to prevent
837 * premature notification while the object is still being modified.
840 g_object_freeze_notify (GObject *object)
842 g_return_if_fail (G_IS_OBJECT (object));
844 if (g_atomic_int_get (&object->ref_count) == 0)
847 g_object_ref (object);
848 g_object_notify_queue_freeze (object, &property_notify_context);
849 g_object_unref (object);
853 g_object_notify_by_spec_internal (GObject *object,
856 GObjectNotifyQueue *nqueue;
858 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
859 g_object_notify_queue_add (object, nqueue, pspec);
860 g_object_notify_queue_thaw (object, nqueue);
865 * @object: a #GObject
866 * @property_name: the name of a property installed on the class of @object.
868 * Emits a "notify" signal for the property @property_name on @object.
870 * When possible, eg. when signaling a property change from within the class
871 * that registered the property, you should use g_object_notify_by_pspec()
875 g_object_notify (GObject *object,
876 const gchar *property_name)
880 g_return_if_fail (G_IS_OBJECT (object));
881 g_return_if_fail (property_name != NULL);
882 if (g_atomic_int_get (&object->ref_count) == 0)
885 g_object_ref (object);
886 /* We don't need to get the redirect target
887 * (by, e.g. calling g_object_class_find_property())
888 * because g_object_notify_queue_add() does that
890 pspec = g_param_spec_pool_lookup (pspec_pool,
892 G_OBJECT_TYPE (object),
896 g_warning ("%s: object class `%s' has no property named `%s'",
898 G_OBJECT_TYPE_NAME (object),
901 g_object_notify_by_spec_internal (object, pspec);
902 g_object_unref (object);
906 * g_object_notify_by_pspec:
907 * @object: a #GObject
908 * @pspec: the #GParamSpec of a property installed on the class of @object.
910 * Emits a "notify" signal for the property specified by @pspec on @object.
912 * This function omits the property name lookup, hence it is faster than
915 * One way to avoid using g_object_notify() from within the
916 * class that registered the properties, and using g_object_notify_by_pspec()
917 * instead, is to store the GParamSpec used with
918 * g_object_class_install_property() inside a static array, e.g.:
928 * static GParamSpec *properties[PROP_LAST];
931 * my_object_class_init (MyObjectClass *klass)
933 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
936 * G_PARAM_READWRITE);
937 * g_object_class_install_property (gobject_class,
939 * properties[PROP_FOO]);
943 * and then notify a change on the "foo" property with:
946 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
952 g_object_notify_by_pspec (GObject *object,
956 g_return_if_fail (G_IS_OBJECT (object));
957 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
959 g_object_ref (object);
960 g_object_notify_by_spec_internal (object, pspec);
961 g_object_unref (object);
965 * g_object_thaw_notify:
966 * @object: a #GObject
968 * Reverts the effect of a previous call to
969 * g_object_freeze_notify(). The freeze count is decreased on @object
970 * and when it reaches zero, all queued "notify" signals are emitted.
972 * It is an error to call this function when the freeze count is zero.
975 g_object_thaw_notify (GObject *object)
977 GObjectNotifyQueue *nqueue;
979 g_return_if_fail (G_IS_OBJECT (object));
980 if (g_atomic_int_get (&object->ref_count) == 0)
983 g_object_ref (object);
984 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
985 if (!nqueue || !nqueue->freeze_count)
986 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
987 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
989 g_object_notify_queue_thaw (object, nqueue);
990 g_object_unref (object);
994 object_get_property (GObject *object,
998 GObjectClass *class = g_type_class_peek (pspec->owner_type);
999 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1000 GParamSpec *redirect;
1002 redirect = g_param_spec_get_redirect_target (pspec);
1006 class->get_property (object, param_id, value, pspec);
1010 object_set_property (GObject *object,
1012 const GValue *value,
1013 GObjectNotifyQueue *nqueue)
1015 GValue tmp_value = { 0, };
1016 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1017 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1018 GParamSpec *redirect;
1019 static gchar* enable_diagnostic = NULL;
1021 redirect = g_param_spec_get_redirect_target (pspec);
1025 if (G_UNLIKELY (!enable_diagnostic))
1027 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1028 if (!enable_diagnostic)
1029 enable_diagnostic = "0";
1032 if (enable_diagnostic[0] == '1')
1034 if (pspec->flags & G_PARAM_DEPRECATED)
1035 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1036 "anymore. It will be removed in a future version.",
1037 G_OBJECT_TYPE_NAME (object), pspec->name);
1040 /* provide a copy to work from, convert (if necessary) and validate */
1041 g_value_init (&tmp_value, pspec->value_type);
1042 if (!g_value_transform (value, &tmp_value))
1043 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1045 g_type_name (pspec->value_type),
1046 G_VALUE_TYPE_NAME (value));
1047 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1049 gchar *contents = g_strdup_value_contents (value);
1051 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1053 G_VALUE_TYPE_NAME (value),
1055 g_type_name (pspec->value_type));
1060 class->set_property (object, param_id, &tmp_value, pspec);
1061 g_object_notify_queue_add (object, nqueue, pspec);
1063 g_value_unset (&tmp_value);
1067 object_interface_check_properties (gpointer func_data,
1070 GTypeInterface *iface_class = g_iface;
1071 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1072 GType iface_type = iface_class->g_type;
1073 GParamSpec **pspecs;
1076 if (!G_IS_OBJECT_CLASS (class))
1079 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1083 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1085 G_OBJECT_CLASS_TYPE (class),
1090 g_critical ("Object class %s doesn't implement property "
1091 "'%s' from interface '%s'",
1092 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1094 g_type_name (iface_type));
1099 /* The implementation paramspec must have a less restrictive
1100 * type than the interface parameter spec for set() and a
1101 * more restrictive type for get(). We just require equality,
1102 * rather than doing something more complicated checking
1103 * the READABLE and WRITABLE flags. We also simplify here
1104 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1107 !g_type_is_a (pspecs[n]->value_type,
1108 class_pspec->value_type))
1110 g_critical ("Property '%s' on class '%s' has type '%s' "
1111 "which is different from the type '%s', "
1112 "of the property on interface '%s'\n",
1114 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1115 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1116 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1117 g_type_name (iface_type));
1120 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1122 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1123 * READABLE and WRITABLE remove restrictions. The implementation
1124 * paramspec must have less restrictive flags.
1127 (!SUBSET (class_pspec->flags,
1129 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1130 !SUBSET (pspecs[n]->flags,
1132 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1134 g_critical ("Flags for property '%s' on class '%s' "
1135 "are not compatible with the property on"
1138 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1139 g_type_name (iface_type));
1148 g_object_get_type (void)
1150 return G_TYPE_OBJECT;
1155 * @object_type: the type id of the #GObject subtype to instantiate
1156 * @first_property_name: the name of the first property
1157 * @...: the value of the first property, followed optionally by more
1158 * name/value pairs, followed by %NULL
1160 * Creates a new instance of a #GObject subtype and sets its properties.
1162 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1163 * which are not explicitly specified are set to their default values.
1165 * Returns: a new instance of @object_type
1168 g_object_new (GType object_type,
1169 const gchar *first_property_name,
1175 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1177 /* short circuit for calls supplying no properties */
1178 if (!first_property_name)
1179 return g_object_newv (object_type, 0, NULL);
1181 va_start (var_args, first_property_name);
1182 object = g_object_new_valist (object_type, first_property_name, var_args);
1189 slist_maybe_remove (GSList **slist,
1192 GSList *last = NULL, *node = *slist;
1195 if (node->data == data)
1198 last->next = node->next;
1200 *slist = node->next;
1201 g_slist_free_1 (node);
1210 static inline gboolean
1211 object_in_construction_list (GObject *object)
1213 gboolean in_construction;
1214 G_LOCK (construction_mutex);
1215 in_construction = g_slist_find (construction_objects, object) != NULL;
1216 G_UNLOCK (construction_mutex);
1217 return in_construction;
1222 * @object_type: the type id of the #GObject subtype to instantiate
1223 * @n_parameters: the length of the @parameters array
1224 * @parameters: an array of #GParameter
1226 * Creates a new instance of a #GObject subtype and sets its properties.
1228 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1229 * which are not explicitly specified are set to their default values.
1231 * Returns: a new instance of @object_type
1234 g_object_newv (GType object_type,
1236 GParameter *parameters)
1238 GObjectConstructParam *cparams = NULL, *oparams;
1239 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1241 GObjectClass *class, *unref_class = NULL;
1243 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1245 GList *clist = NULL;
1246 gboolean newly_constructed;
1249 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1251 class = g_type_class_peek_static (object_type);
1253 class = unref_class = g_type_class_ref (object_type);
1254 for (slist = class->construct_properties; slist; slist = slist->next)
1256 clist = g_list_prepend (clist, slist->data);
1257 n_total_cparams += 1;
1260 if (n_parameters == 0 && n_total_cparams == 0)
1262 /* This is a simple object with no construct properties, and
1263 * no properties are being set, so short circuit the parameter
1264 * handling. This speeds up simple object construction.
1267 object = class->constructor (object_type, 0, NULL);
1268 goto did_construction;
1271 /* collect parameters, sort into construction and normal ones */
1272 oparams = g_new (GObjectConstructParam, n_parameters);
1273 cparams = g_new (GObjectConstructParam, n_total_cparams);
1274 for (i = 0; i < n_parameters; i++)
1276 GValue *value = ¶meters[i].value;
1277 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1283 g_warning ("%s: object class `%s' has no property named `%s'",
1285 g_type_name (object_type),
1286 parameters[i].name);
1289 if (!(pspec->flags & G_PARAM_WRITABLE))
1291 g_warning ("%s: property `%s' of object class `%s' is not writable",
1294 g_type_name (object_type));
1297 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1299 GList *list = g_list_find (clist, pspec);
1303 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1304 G_STRFUNC, pspec->name, g_type_name (object_type));
1307 cparams[n_cparams].pspec = pspec;
1308 cparams[n_cparams].value = value;
1313 list->prev->next = list->next;
1315 list->next->prev = list->prev;
1316 g_list_free_1 (list);
1320 oparams[n_oparams].pspec = pspec;
1321 oparams[n_oparams].value = value;
1326 /* set remaining construction properties to default values */
1327 n_cvalues = n_total_cparams - n_cparams;
1328 cvalues = g_new (GValue, n_cvalues);
1331 GList *tmp = clist->next;
1332 GParamSpec *pspec = clist->data;
1333 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1336 g_value_init (value, pspec->value_type);
1337 g_param_value_set_default (pspec, value);
1339 cparams[n_cparams].pspec = pspec;
1340 cparams[n_cparams].value = value;
1343 g_list_free_1 (clist);
1347 /* construct object from construction parameters */
1348 object = class->constructor (object_type, n_total_cparams, cparams);
1349 /* free construction values */
1352 g_value_unset (cvalues + n_cvalues);
1356 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1358 /* adjust freeze_count according to g_object_init() and remaining properties */
1359 G_LOCK (construction_mutex);
1360 newly_constructed = slist_maybe_remove (&construction_objects, object);
1361 G_UNLOCK (construction_mutex);
1364 newly_constructed = TRUE;
1366 if (CLASS_HAS_PROPS (class))
1368 if (newly_constructed || n_oparams)
1369 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1370 if (newly_constructed)
1371 g_object_notify_queue_thaw (object, nqueue);
1374 /* run 'constructed' handler if there is one */
1375 if (newly_constructed && class->constructed)
1376 class->constructed (object);
1378 /* set remaining properties */
1379 for (i = 0; i < n_oparams; i++)
1380 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1383 if (CLASS_HAS_PROPS (class))
1385 /* release our own freeze count and handle notifications */
1386 if (newly_constructed || n_oparams)
1387 g_object_notify_queue_thaw (object, nqueue);
1391 g_type_class_unref (unref_class);
1397 * g_object_new_valist:
1398 * @object_type: the type id of the #GObject subtype to instantiate
1399 * @first_property_name: the name of the first property
1400 * @var_args: the value of the first property, followed optionally by more
1401 * name/value pairs, followed by %NULL
1403 * Creates a new instance of a #GObject subtype and sets its properties.
1405 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1406 * which are not explicitly specified are set to their default values.
1408 * Returns: a new instance of @object_type
1411 g_object_new_valist (GType object_type,
1412 const gchar *first_property_name,
1415 GObjectClass *class;
1419 guint n_params = 0, n_alloced_params = 16;
1421 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1423 if (!first_property_name)
1424 return g_object_newv (object_type, 0, NULL);
1426 class = g_type_class_ref (object_type);
1428 params = g_new0 (GParameter, n_alloced_params);
1429 name = first_property_name;
1432 gchar *error = NULL;
1433 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1439 g_warning ("%s: object class `%s' has no property named `%s'",
1441 g_type_name (object_type),
1445 if (n_params >= n_alloced_params)
1447 n_alloced_params += 16;
1448 params = g_renew (GParameter, params, n_alloced_params);
1450 params[n_params].name = name;
1451 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1452 var_args, 0, &error);
1455 g_warning ("%s: %s", G_STRFUNC, error);
1457 g_value_unset (¶ms[n_params].value);
1461 name = va_arg (var_args, gchar*);
1464 object = g_object_newv (object_type, n_params, params);
1467 g_value_unset (¶ms[n_params].value);
1470 g_type_class_unref (class);
1476 g_object_constructor (GType type,
1477 guint n_construct_properties,
1478 GObjectConstructParam *construct_params)
1483 object = (GObject*) g_type_create_instance (type);
1485 /* set construction parameters */
1486 if (n_construct_properties)
1488 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1490 /* set construct properties */
1491 while (n_construct_properties--)
1493 GValue *value = construct_params->value;
1494 GParamSpec *pspec = construct_params->pspec;
1497 object_set_property (object, pspec, value, nqueue);
1499 g_object_notify_queue_thaw (object, nqueue);
1500 /* the notification queue is still frozen from g_object_init(), so
1501 * we don't need to handle it here, g_object_newv() takes
1510 * g_object_set_valist:
1511 * @object: a #GObject
1512 * @first_property_name: name of the first property to set
1513 * @var_args: value for the first property, followed optionally by more
1514 * name/value pairs, followed by %NULL
1516 * Sets properties on an object.
1519 g_object_set_valist (GObject *object,
1520 const gchar *first_property_name,
1523 GObjectNotifyQueue *nqueue;
1526 g_return_if_fail (G_IS_OBJECT (object));
1528 g_object_ref (object);
1529 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1531 name = first_property_name;
1534 GValue value = { 0, };
1536 gchar *error = NULL;
1538 pspec = g_param_spec_pool_lookup (pspec_pool,
1540 G_OBJECT_TYPE (object),
1544 g_warning ("%s: object class `%s' has no property named `%s'",
1546 G_OBJECT_TYPE_NAME (object),
1550 if (!(pspec->flags & G_PARAM_WRITABLE))
1552 g_warning ("%s: property `%s' of object class `%s' is not writable",
1555 G_OBJECT_TYPE_NAME (object));
1558 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1560 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1561 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1565 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1569 g_warning ("%s: %s", G_STRFUNC, error);
1571 g_value_unset (&value);
1575 object_set_property (object, pspec, &value, nqueue);
1576 g_value_unset (&value);
1578 name = va_arg (var_args, gchar*);
1581 g_object_notify_queue_thaw (object, nqueue);
1582 g_object_unref (object);
1586 * g_object_get_valist:
1587 * @object: a #GObject
1588 * @first_property_name: name of the first property to get
1589 * @var_args: return location for the first property, followed optionally by more
1590 * name/return location pairs, followed by %NULL
1592 * Gets properties of an object.
1594 * In general, a copy is made of the property contents and the caller
1595 * is responsible for freeing the memory in the appropriate manner for
1596 * the type, for instance by calling g_free() or g_object_unref().
1598 * See g_object_get().
1601 g_object_get_valist (GObject *object,
1602 const gchar *first_property_name,
1607 g_return_if_fail (G_IS_OBJECT (object));
1609 g_object_ref (object);
1611 name = first_property_name;
1615 GValue value = { 0, };
1619 pspec = g_param_spec_pool_lookup (pspec_pool,
1621 G_OBJECT_TYPE (object),
1625 g_warning ("%s: object class `%s' has no property named `%s'",
1627 G_OBJECT_TYPE_NAME (object),
1631 if (!(pspec->flags & G_PARAM_READABLE))
1633 g_warning ("%s: property `%s' of object class `%s' is not readable",
1636 G_OBJECT_TYPE_NAME (object));
1640 g_value_init (&value, pspec->value_type);
1642 object_get_property (object, pspec, &value);
1644 G_VALUE_LCOPY (&value, var_args, 0, &error);
1647 g_warning ("%s: %s", G_STRFUNC, error);
1649 g_value_unset (&value);
1653 g_value_unset (&value);
1655 name = va_arg (var_args, gchar*);
1658 g_object_unref (object);
1663 * @object: a #GObject
1664 * @first_property_name: name of the first property to set
1665 * @...: value for the first property, followed optionally by more
1666 * name/value pairs, followed by %NULL
1668 * Sets properties on an object.
1671 g_object_set (gpointer _object,
1672 const gchar *first_property_name,
1675 GObject *object = _object;
1678 g_return_if_fail (G_IS_OBJECT (object));
1680 va_start (var_args, first_property_name);
1681 g_object_set_valist (object, first_property_name, var_args);
1687 * @object: a #GObject
1688 * @first_property_name: name of the first property to get
1689 * @...: return location for the first property, followed optionally by more
1690 * name/return location pairs, followed by %NULL
1692 * Gets properties of an object.
1694 * In general, a copy is made of the property contents and the caller
1695 * is responsible for freeing the memory in the appropriate manner for
1696 * the type, for instance by calling g_free() or g_object_unref().
1699 * <title>Using g_object_get(<!-- -->)</title>
1700 * An example of using g_object_get() to get the contents
1701 * of three properties - one of type #G_TYPE_INT,
1702 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1708 * g_object_get (my_object,
1709 * "int-property", &intval,
1710 * "str-property", &strval,
1711 * "obj-property", &objval,
1714 * // Do something with intval, strval, objval
1717 * g_object_unref (objval);
1722 g_object_get (gpointer _object,
1723 const gchar *first_property_name,
1726 GObject *object = _object;
1729 g_return_if_fail (G_IS_OBJECT (object));
1731 va_start (var_args, first_property_name);
1732 g_object_get_valist (object, first_property_name, var_args);
1737 * g_object_set_property:
1738 * @object: a #GObject
1739 * @property_name: the name of the property to set
1742 * Sets a property on an object.
1745 g_object_set_property (GObject *object,
1746 const gchar *property_name,
1747 const GValue *value)
1749 GObjectNotifyQueue *nqueue;
1752 g_return_if_fail (G_IS_OBJECT (object));
1753 g_return_if_fail (property_name != NULL);
1754 g_return_if_fail (G_IS_VALUE (value));
1756 g_object_ref (object);
1757 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1759 pspec = g_param_spec_pool_lookup (pspec_pool,
1761 G_OBJECT_TYPE (object),
1764 g_warning ("%s: object class `%s' has no property named `%s'",
1766 G_OBJECT_TYPE_NAME (object),
1768 else if (!(pspec->flags & G_PARAM_WRITABLE))
1769 g_warning ("%s: property `%s' of object class `%s' is not writable",
1772 G_OBJECT_TYPE_NAME (object));
1773 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1774 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1775 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1777 object_set_property (object, pspec, value, nqueue);
1779 g_object_notify_queue_thaw (object, nqueue);
1780 g_object_unref (object);
1784 * g_object_get_property:
1785 * @object: a #GObject
1786 * @property_name: the name of the property to get
1787 * @value: return location for the property value
1789 * Gets a property of an object.
1791 * In general, a copy is made of the property contents and the caller is
1792 * responsible for freeing the memory by calling g_value_unset().
1794 * Note that g_object_get_property() is really intended for language
1795 * bindings, g_object_get() is much more convenient for C programming.
1798 g_object_get_property (GObject *object,
1799 const gchar *property_name,
1804 g_return_if_fail (G_IS_OBJECT (object));
1805 g_return_if_fail (property_name != NULL);
1806 g_return_if_fail (G_IS_VALUE (value));
1808 g_object_ref (object);
1810 pspec = g_param_spec_pool_lookup (pspec_pool,
1812 G_OBJECT_TYPE (object),
1815 g_warning ("%s: object class `%s' has no property named `%s'",
1817 G_OBJECT_TYPE_NAME (object),
1819 else if (!(pspec->flags & G_PARAM_READABLE))
1820 g_warning ("%s: property `%s' of object class `%s' is not readable",
1823 G_OBJECT_TYPE_NAME (object));
1826 GValue *prop_value, tmp_value = { 0, };
1828 /* auto-conversion of the callers value type
1830 if (G_VALUE_TYPE (value) == pspec->value_type)
1832 g_value_reset (value);
1835 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1837 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1838 G_STRFUNC, pspec->name,
1839 g_type_name (pspec->value_type),
1840 G_VALUE_TYPE_NAME (value));
1841 g_object_unref (object);
1846 g_value_init (&tmp_value, pspec->value_type);
1847 prop_value = &tmp_value;
1849 object_get_property (object, pspec, prop_value);
1850 if (prop_value != value)
1852 g_value_transform (prop_value, value);
1853 g_value_unset (&tmp_value);
1857 g_object_unref (object);
1862 * @object: a #GObject
1863 * @signal_spec: the spec for the first signal
1864 * @...: #GCallback for the first signal, followed by data for the
1865 * first signal, followed optionally by more signal
1866 * spec/callback/data triples, followed by %NULL
1868 * A convenience function to connect multiple signals at once.
1870 * The signal specs expected by this function have the form
1871 * "modifier::signal_name", where modifier can be one of the following:
1874 * <term>signal</term>
1876 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1877 * </para></listitem>
1880 * <term>object_signal</term>
1881 * <term>object-signal</term>
1883 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1884 * </para></listitem>
1887 * <term>swapped_signal</term>
1888 * <term>swapped-signal</term>
1890 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1891 * </para></listitem>
1894 * <term>swapped_object_signal</term>
1895 * <term>swapped-object-signal</term>
1897 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1898 * </para></listitem>
1901 * <term>signal_after</term>
1902 * <term>signal-after</term>
1904 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1905 * </para></listitem>
1908 * <term>object_signal_after</term>
1909 * <term>object-signal-after</term>
1911 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1912 * </para></listitem>
1915 * <term>swapped_signal_after</term>
1916 * <term>swapped-signal-after</term>
1918 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1919 * </para></listitem>
1922 * <term>swapped_object_signal_after</term>
1923 * <term>swapped-object-signal-after</term>
1925 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1926 * </para></listitem>
1931 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1932 * "type", GTK_WINDOW_POPUP,
1935 * "signal::event", gtk_menu_window_event, menu,
1936 * "signal::size_request", gtk_menu_window_size_request, menu,
1937 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1944 g_object_connect (gpointer _object,
1945 const gchar *signal_spec,
1948 GObject *object = _object;
1951 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1952 g_return_val_if_fail (object->ref_count > 0, object);
1954 va_start (var_args, signal_spec);
1957 GCallback callback = va_arg (var_args, GCallback);
1958 gpointer data = va_arg (var_args, gpointer);
1961 if (strncmp (signal_spec, "signal::", 8) == 0)
1962 sid = g_signal_connect_data (object, signal_spec + 8,
1963 callback, data, NULL,
1965 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1966 strncmp (signal_spec, "object-signal::", 15) == 0)
1967 sid = g_signal_connect_object (object, signal_spec + 15,
1970 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1971 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1972 sid = g_signal_connect_data (object, signal_spec + 16,
1973 callback, data, NULL,
1975 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1976 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1977 sid = g_signal_connect_object (object, signal_spec + 23,
1980 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1981 strncmp (signal_spec, "signal-after::", 14) == 0)
1982 sid = g_signal_connect_data (object, signal_spec + 14,
1983 callback, data, NULL,
1985 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1986 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1987 sid = g_signal_connect_object (object, signal_spec + 21,
1990 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1991 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1992 sid = g_signal_connect_data (object, signal_spec + 22,
1993 callback, data, NULL,
1994 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1995 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1996 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1997 sid = g_signal_connect_object (object, signal_spec + 29,
1999 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2002 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2005 signal_spec = va_arg (var_args, gchar*);
2013 * g_object_disconnect:
2014 * @object: a #GObject
2015 * @signal_spec: the spec for the first signal
2016 * @...: #GCallback for the first signal, followed by data for the first signal,
2017 * followed optionally by more signal spec/callback/data triples,
2020 * A convenience function to disconnect multiple signals at once.
2022 * The signal specs expected by this function have the form
2023 * "any_signal", which means to disconnect any signal with matching
2024 * callback and data, or "any_signal::signal_name", which only
2025 * disconnects the signal named "signal_name".
2028 g_object_disconnect (gpointer _object,
2029 const gchar *signal_spec,
2032 GObject *object = _object;
2035 g_return_if_fail (G_IS_OBJECT (object));
2036 g_return_if_fail (object->ref_count > 0);
2038 va_start (var_args, signal_spec);
2041 GCallback callback = va_arg (var_args, GCallback);
2042 gpointer data = va_arg (var_args, gpointer);
2043 guint sid = 0, detail = 0, mask = 0;
2045 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2046 strncmp (signal_spec, "any-signal::", 12) == 0)
2049 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2051 else if (strcmp (signal_spec, "any_signal") == 0 ||
2052 strcmp (signal_spec, "any-signal") == 0)
2055 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2059 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2063 if ((mask & G_SIGNAL_MATCH_ID) &&
2064 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2065 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2066 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2068 NULL, (gpointer)callback, data))
2069 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2070 signal_spec = va_arg (var_args, gchar*);
2081 } weak_refs[1]; /* flexible array */
2085 weak_refs_notify (gpointer data)
2087 WeakRefStack *wstack = data;
2090 for (i = 0; i < wstack->n_weak_refs; i++)
2091 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2096 * g_object_weak_ref:
2097 * @object: #GObject to reference weakly
2098 * @notify: callback to invoke before the object is freed
2099 * @data: extra data to pass to notify
2101 * Adds a weak reference callback to an object. Weak references are
2102 * used for notification when an object is finalized. They are called
2103 * "weak references" because they allow you to safely hold a pointer
2104 * to an object without calling g_object_ref() (g_object_ref() adds a
2105 * strong reference, that is, forces the object to stay alive).
2108 g_object_weak_ref (GObject *object,
2112 WeakRefStack *wstack;
2115 g_return_if_fail (G_IS_OBJECT (object));
2116 g_return_if_fail (notify != NULL);
2117 g_return_if_fail (object->ref_count >= 1);
2119 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2122 i = wstack->n_weak_refs++;
2123 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2127 wstack = g_renew (WeakRefStack, NULL, 1);
2128 wstack->object = object;
2129 wstack->n_weak_refs = 1;
2132 wstack->weak_refs[i].notify = notify;
2133 wstack->weak_refs[i].data = data;
2134 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2138 * g_object_weak_unref:
2139 * @object: #GObject to remove a weak reference from
2140 * @notify: callback to search for
2141 * @data: data to search for
2143 * Removes a weak reference callback to an object.
2146 g_object_weak_unref (GObject *object,
2150 WeakRefStack *wstack;
2151 gboolean found_one = FALSE;
2153 g_return_if_fail (G_IS_OBJECT (object));
2154 g_return_if_fail (notify != NULL);
2156 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2161 for (i = 0; i < wstack->n_weak_refs; i++)
2162 if (wstack->weak_refs[i].notify == notify &&
2163 wstack->weak_refs[i].data == data)
2166 wstack->n_weak_refs -= 1;
2167 if (i != wstack->n_weak_refs)
2168 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2174 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2178 * g_object_add_weak_pointer:
2179 * @object: The object that should be weak referenced.
2180 * @weak_pointer_location: The memory address of a pointer.
2182 * Adds a weak reference from weak_pointer to @object to indicate that
2183 * the pointer located at @weak_pointer_location is only valid during
2184 * the lifetime of @object. When the @object is finalized,
2185 * @weak_pointer will be set to %NULL.
2188 g_object_add_weak_pointer (GObject *object,
2189 gpointer *weak_pointer_location)
2191 g_return_if_fail (G_IS_OBJECT (object));
2192 g_return_if_fail (weak_pointer_location != NULL);
2194 g_object_weak_ref (object,
2195 (GWeakNotify) g_nullify_pointer,
2196 weak_pointer_location);
2200 * g_object_remove_weak_pointer:
2201 * @object: The object that is weak referenced.
2202 * @weak_pointer_location: The memory address of a pointer.
2204 * Removes a weak reference from @object that was previously added
2205 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2206 * to match the one used with g_object_add_weak_pointer().
2209 g_object_remove_weak_pointer (GObject *object,
2210 gpointer *weak_pointer_location)
2212 g_return_if_fail (G_IS_OBJECT (object));
2213 g_return_if_fail (weak_pointer_location != NULL);
2215 g_object_weak_unref (object,
2216 (GWeakNotify) g_nullify_pointer,
2217 weak_pointer_location);
2221 object_floating_flag_handler (GObject *object,
2227 case +1: /* force floating if possible */
2229 oldvalue = g_atomic_pointer_get (&object->qdata);
2230 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2231 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2232 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2233 case -1: /* sink if possible */
2235 oldvalue = g_atomic_pointer_get (&object->qdata);
2236 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2237 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2238 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2239 default: /* check floating */
2240 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2245 * g_object_is_floating:
2246 * @object: a #GObject
2248 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2253 * Returns: %TRUE if @object has a floating reference
2256 g_object_is_floating (gpointer _object)
2258 GObject *object = _object;
2259 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2260 return floating_flag_handler (object, 0);
2264 * g_object_ref_sink:
2265 * @object: a #GObject
2267 * Increase the reference count of @object, and possibly remove the
2268 * <link linkend="floating-ref">floating</link> reference, if @object
2269 * has a floating reference.
2271 * In other words, if the object is floating, then this call "assumes
2272 * ownership" of the floating reference, converting it to a normal
2273 * reference by clearing the floating flag while leaving the reference
2274 * count unchanged. If the object is not floating, then this call
2275 * adds a new normal reference increasing the reference count by one.
2282 g_object_ref_sink (gpointer _object)
2284 GObject *object = _object;
2285 gboolean was_floating;
2286 g_return_val_if_fail (G_IS_OBJECT (object), object);
2287 g_return_val_if_fail (object->ref_count >= 1, object);
2288 g_object_ref (object);
2289 was_floating = floating_flag_handler (object, -1);
2291 g_object_unref (object);
2296 * g_object_force_floating:
2297 * @object: a #GObject
2299 * This function is intended for #GObject implementations to re-enforce a
2300 * <link linkend="floating-ref">floating</link> object reference.
2301 * Doing this is seldomly required, all
2302 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2303 * usually just needs to be sunken by calling g_object_ref_sink().
2308 g_object_force_floating (GObject *object)
2310 gboolean was_floating;
2311 g_return_if_fail (G_IS_OBJECT (object));
2312 g_return_if_fail (object->ref_count >= 1);
2314 was_floating = floating_flag_handler (object, +1);
2319 guint n_toggle_refs;
2321 GToggleNotify notify;
2323 } toggle_refs[1]; /* flexible array */
2327 toggle_refs_notify (GObject *object,
2328 gboolean is_last_ref)
2330 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2332 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2333 * will only be notified when there is exactly one of them.
2335 g_assert (tstack->n_toggle_refs == 1);
2336 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2340 * g_object_add_toggle_ref:
2341 * @object: a #GObject
2342 * @notify: a function to call when this reference is the
2343 * last reference to the object, or is no longer
2344 * the last reference.
2345 * @data: data to pass to @notify
2347 * Increases the reference count of the object by one and sets a
2348 * callback to be called when all other references to the object are
2349 * dropped, or when this is already the last reference to the object
2350 * and another reference is established.
2352 * This functionality is intended for binding @object to a proxy
2353 * object managed by another memory manager. This is done with two
2354 * paired references: the strong reference added by
2355 * g_object_add_toggle_ref() and a reverse reference to the proxy
2356 * object which is either a strong reference or weak reference.
2358 * The setup is that when there are no other references to @object,
2359 * only a weak reference is held in the reverse direction from @object
2360 * to the proxy object, but when there are other references held to
2361 * @object, a strong reference is held. The @notify callback is called
2362 * when the reference from @object to the proxy object should be
2363 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2364 * true) or weak to strong (@is_last_ref false).
2366 * Since a (normal) reference must be held to the object before
2367 * calling g_object_toggle_ref(), the initial state of the reverse
2368 * link is always strong.
2370 * Multiple toggle references may be added to the same gobject,
2371 * however if there are multiple toggle references to an object, none
2372 * of them will ever be notified until all but one are removed. For
2373 * this reason, you should only ever use a toggle reference if there
2374 * is important state in the proxy object.
2379 g_object_add_toggle_ref (GObject *object,
2380 GToggleNotify notify,
2383 ToggleRefStack *tstack;
2386 g_return_if_fail (G_IS_OBJECT (object));
2387 g_return_if_fail (notify != NULL);
2388 g_return_if_fail (object->ref_count >= 1);
2390 g_object_ref (object);
2392 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2395 i = tstack->n_toggle_refs++;
2396 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2397 * in tstate->toggle_refs */
2398 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2402 tstack = g_renew (ToggleRefStack, NULL, 1);
2403 tstack->object = object;
2404 tstack->n_toggle_refs = 1;
2408 /* Set a flag for fast lookup after adding the first toggle reference */
2409 if (tstack->n_toggle_refs == 1)
2410 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2412 tstack->toggle_refs[i].notify = notify;
2413 tstack->toggle_refs[i].data = data;
2414 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2415 (GDestroyNotify)g_free);
2419 * g_object_remove_toggle_ref:
2420 * @object: a #GObject
2421 * @notify: a function to call when this reference is the
2422 * last reference to the object, or is no longer
2423 * the last reference.
2424 * @data: data to pass to @notify
2426 * Removes a reference added with g_object_add_toggle_ref(). The
2427 * reference count of the object is decreased by one.
2432 g_object_remove_toggle_ref (GObject *object,
2433 GToggleNotify notify,
2436 ToggleRefStack *tstack;
2437 gboolean found_one = FALSE;
2439 g_return_if_fail (G_IS_OBJECT (object));
2440 g_return_if_fail (notify != NULL);
2442 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2447 for (i = 0; i < tstack->n_toggle_refs; i++)
2448 if (tstack->toggle_refs[i].notify == notify &&
2449 tstack->toggle_refs[i].data == data)
2452 tstack->n_toggle_refs -= 1;
2453 if (i != tstack->n_toggle_refs)
2454 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2456 if (tstack->n_toggle_refs == 0)
2457 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2459 g_object_unref (object);
2466 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2471 * @object: a #GObject
2473 * Increases the reference count of @object.
2475 * Returns: the same @object
2478 g_object_ref (gpointer _object)
2480 GObject *object = _object;
2483 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2484 g_return_val_if_fail (object->ref_count > 0, NULL);
2486 #ifdef G_ENABLE_DEBUG
2487 if (g_trap_object_ref == object)
2489 #endif /* G_ENABLE_DEBUG */
2492 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2494 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2495 toggle_refs_notify (object, FALSE);
2497 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2504 * @object: a #GObject
2506 * Decreases the reference count of @object. When its reference count
2507 * drops to 0, the object is finalized (i.e. its memory is freed).
2510 g_object_unref (gpointer _object)
2512 GObject *object = _object;
2515 g_return_if_fail (G_IS_OBJECT (object));
2516 g_return_if_fail (object->ref_count > 0);
2518 #ifdef G_ENABLE_DEBUG
2519 if (g_trap_object_ref == object)
2521 #endif /* G_ENABLE_DEBUG */
2523 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2524 retry_atomic_decrement1:
2525 old_ref = g_atomic_int_get (&object->ref_count);
2528 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2529 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2531 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2532 goto retry_atomic_decrement1;
2534 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2536 /* if we went from 2->1 we need to notify toggle refs if any */
2537 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2538 toggle_refs_notify (object, TRUE);
2542 /* we are about tp remove the last reference */
2543 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2544 G_OBJECT_GET_CLASS (object)->dispose (object);
2545 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2547 /* may have been re-referenced meanwhile */
2548 retry_atomic_decrement2:
2549 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2552 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2553 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2555 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2556 goto retry_atomic_decrement2;
2558 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2560 /* if we went from 2->1 we need to notify toggle refs if any */
2561 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2562 toggle_refs_notify (object, TRUE);
2567 /* we are still in the process of taking away the last ref */
2568 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2569 g_signal_handlers_destroy (object);
2570 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2572 /* decrement the last reference */
2573 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2575 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2577 /* may have been re-referenced meanwhile */
2578 if (G_LIKELY (old_ref == 1))
2580 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2581 G_OBJECT_GET_CLASS (object)->finalize (object);
2583 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2585 #ifdef G_ENABLE_DEBUG
2588 /* catch objects not chaining finalize handlers */
2589 G_LOCK (debug_objects);
2590 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2591 G_UNLOCK (debug_objects);
2593 #endif /* G_ENABLE_DEBUG */
2594 g_type_free_instance ((GTypeInstance*) object);
2600 * g_object_get_qdata:
2601 * @object: The GObject to get a stored user data pointer from
2602 * @quark: A #GQuark, naming the user data pointer
2604 * This function gets back user data pointers stored via
2605 * g_object_set_qdata().
2607 * Returns: The user data pointer set, or %NULL
2610 g_object_get_qdata (GObject *object,
2613 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2615 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2619 * g_object_set_qdata:
2620 * @object: The GObject to set store a user data pointer
2621 * @quark: A #GQuark, naming the user data pointer
2622 * @data: An opaque user data pointer
2624 * This sets an opaque, named pointer on an object.
2625 * The name is specified through a #GQuark (retrived e.g. via
2626 * g_quark_from_static_string()), and the pointer
2627 * can be gotten back from the @object with g_object_get_qdata()
2628 * until the @object is finalized.
2629 * Setting a previously set user data pointer, overrides (frees)
2630 * the old pointer set, using #NULL as pointer essentially
2631 * removes the data stored.
2634 g_object_set_qdata (GObject *object,
2638 g_return_if_fail (G_IS_OBJECT (object));
2639 g_return_if_fail (quark > 0);
2641 g_datalist_id_set_data (&object->qdata, quark, data);
2645 * g_object_set_qdata_full:
2646 * @object: The GObject to set store a user data pointer
2647 * @quark: A #GQuark, naming the user data pointer
2648 * @data: An opaque user data pointer
2649 * @destroy: Function to invoke with @data as argument, when @data
2652 * This function works like g_object_set_qdata(), but in addition,
2653 * a void (*destroy) (gpointer) function may be specified which is
2654 * called with @data as argument when the @object is finalized, or
2655 * the data is being overwritten by a call to g_object_set_qdata()
2656 * with the same @quark.
2659 g_object_set_qdata_full (GObject *object,
2662 GDestroyNotify destroy)
2664 g_return_if_fail (G_IS_OBJECT (object));
2665 g_return_if_fail (quark > 0);
2667 g_datalist_id_set_data_full (&object->qdata, quark, data,
2668 data ? destroy : (GDestroyNotify) NULL);
2672 * g_object_steal_qdata:
2673 * @object: The GObject to get a stored user data pointer from
2674 * @quark: A #GQuark, naming the user data pointer
2676 * This function gets back user data pointers stored via
2677 * g_object_set_qdata() and removes the @data from object
2678 * without invoking its destroy() function (if any was
2680 * Usually, calling this function is only required to update
2681 * user data pointers with a destroy notifier, for example:
2684 * object_add_to_user_list (GObject *object,
2685 * const gchar *new_string)
2687 * // the quark, naming the object data
2688 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2689 * // retrive the old string list
2690 * GList *list = g_object_steal_qdata (object, quark_string_list);
2692 * // prepend new string
2693 * list = g_list_prepend (list, g_strdup (new_string));
2694 * // this changed 'list', so we need to set it again
2695 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2698 * free_string_list (gpointer data)
2700 * GList *node, *list = data;
2702 * for (node = list; node; node = node->next)
2703 * g_free (node->data);
2704 * g_list_free (list);
2707 * Using g_object_get_qdata() in the above example, instead of
2708 * g_object_steal_qdata() would have left the destroy function set,
2709 * and thus the partial string list would have been freed upon
2710 * g_object_set_qdata_full().
2712 * Returns: The user data pointer set, or %NULL
2715 g_object_steal_qdata (GObject *object,
2718 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2719 g_return_val_if_fail (quark > 0, NULL);
2721 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2725 * g_object_get_data:
2726 * @object: #GObject containing the associations
2727 * @key: name of the key for that association
2729 * Gets a named field from the objects table of associations (see g_object_set_data()).
2731 * Returns: the data if found, or %NULL if no such data exists.
2734 g_object_get_data (GObject *object,
2739 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2740 g_return_val_if_fail (key != NULL, NULL);
2742 quark = g_quark_try_string (key);
2744 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2748 * g_object_set_data:
2749 * @object: #GObject containing the associations.
2750 * @key: name of the key
2751 * @data: data to associate with that key
2753 * Each object carries around a table of associations from
2754 * strings to pointers. This function lets you set an association.
2756 * If the object already had an association with that name,
2757 * the old association will be destroyed.
2760 g_object_set_data (GObject *object,
2764 g_return_if_fail (G_IS_OBJECT (object));
2765 g_return_if_fail (key != NULL);
2767 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2771 * g_object_set_data_full:
2772 * @object: #GObject containing the associations
2773 * @key: name of the key
2774 * @data: data to associate with that key
2775 * @destroy: function to call when the association is destroyed
2777 * Like g_object_set_data() except it adds notification
2778 * for when the association is destroyed, either by setting it
2779 * to a different value or when the object is destroyed.
2781 * Note that the @destroy callback is not called if @data is %NULL.
2784 g_object_set_data_full (GObject *object,
2787 GDestroyNotify destroy)
2789 g_return_if_fail (G_IS_OBJECT (object));
2790 g_return_if_fail (key != NULL);
2792 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2793 data ? destroy : (GDestroyNotify) NULL);
2797 * g_object_steal_data:
2798 * @object: #GObject containing the associations
2799 * @key: name of the key
2801 * Remove a specified datum from the object's data associations,
2802 * without invoking the association's destroy handler.
2804 * Returns: the data if found, or %NULL if no such data exists.
2807 g_object_steal_data (GObject *object,
2812 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2813 g_return_val_if_fail (key != NULL, NULL);
2815 quark = g_quark_try_string (key);
2817 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2821 g_value_object_init (GValue *value)
2823 value->data[0].v_pointer = NULL;
2827 g_value_object_free_value (GValue *value)
2829 if (value->data[0].v_pointer)
2830 g_object_unref (value->data[0].v_pointer);
2834 g_value_object_copy_value (const GValue *src_value,
2837 if (src_value->data[0].v_pointer)
2838 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2840 dest_value->data[0].v_pointer = NULL;
2844 g_value_object_transform_value (const GValue *src_value,
2847 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)))
2848 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2850 dest_value->data[0].v_pointer = NULL;
2854 g_value_object_peek_pointer (const GValue *value)
2856 return value->data[0].v_pointer;
2860 g_value_object_collect_value (GValue *value,
2861 guint n_collect_values,
2862 GTypeCValue *collect_values,
2863 guint collect_flags)
2865 if (collect_values[0].v_pointer)
2867 GObject *object = collect_values[0].v_pointer;
2869 if (object->g_type_instance.g_class == NULL)
2870 return g_strconcat ("invalid unclassed object pointer for value type `",
2871 G_VALUE_TYPE_NAME (value),
2874 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2875 return g_strconcat ("invalid object type `",
2876 G_OBJECT_TYPE_NAME (object),
2877 "' for value type `",
2878 G_VALUE_TYPE_NAME (value),
2881 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2882 value->data[0].v_pointer = g_object_ref (object);
2885 value->data[0].v_pointer = NULL;
2891 g_value_object_lcopy_value (const GValue *value,
2892 guint n_collect_values,
2893 GTypeCValue *collect_values,
2894 guint collect_flags)
2896 GObject **object_p = collect_values[0].v_pointer;
2899 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2901 if (!value->data[0].v_pointer)
2903 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2904 *object_p = value->data[0].v_pointer;
2906 *object_p = g_object_ref (value->data[0].v_pointer);
2912 * g_value_set_object:
2913 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2914 * @v_object: object value to be set
2916 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2918 * g_value_set_object() increases the reference count of @v_object
2919 * (the #GValue holds a reference to @v_object). If you do not wish
2920 * to increase the reference count of the object (i.e. you wish to
2921 * pass your current reference to the #GValue because you no longer
2922 * need it), use g_value_take_object() instead.
2924 * It is important that your #GValue holds a reference to @v_object (either its
2925 * own, or one it has taken) to ensure that the object won't be destroyed while
2926 * the #GValue still exists).
2929 g_value_set_object (GValue *value,
2934 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2936 old = value->data[0].v_pointer;
2940 g_return_if_fail (G_IS_OBJECT (v_object));
2941 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2943 value->data[0].v_pointer = v_object;
2944 g_object_ref (value->data[0].v_pointer);
2947 value->data[0].v_pointer = NULL;
2950 g_object_unref (old);
2954 * g_value_set_object_take_ownership:
2955 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2956 * @v_object: object value to be set
2958 * This is an internal function introduced mainly for C marshallers.
2960 * Deprecated: 2.4: Use g_value_take_object() instead.
2963 g_value_set_object_take_ownership (GValue *value,
2966 g_value_take_object (value, v_object);
2970 * g_value_take_object:
2971 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2972 * @v_object: object value to be set
2974 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2975 * and takes over the ownership of the callers reference to @v_object;
2976 * the caller doesn't have to unref it any more (i.e. the reference
2977 * count of the object is not increased).
2979 * If you want the #GValue to hold its own reference to @v_object, use
2980 * g_value_set_object() instead.
2985 g_value_take_object (GValue *value,
2988 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2990 if (value->data[0].v_pointer)
2992 g_object_unref (value->data[0].v_pointer);
2993 value->data[0].v_pointer = NULL;
2998 g_return_if_fail (G_IS_OBJECT (v_object));
2999 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3001 value->data[0].v_pointer = v_object; /* we take over the reference count */
3006 * g_value_get_object:
3007 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3009 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3011 * Returns: object contents of @value
3014 g_value_get_object (const GValue *value)
3016 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3018 return value->data[0].v_pointer;
3022 * g_value_dup_object:
3023 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3025 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3026 * its reference count.
3028 * Returns: object content of @value, should be unreferenced when no
3032 g_value_dup_object (const GValue *value)
3034 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3036 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3040 * g_signal_connect_object:
3041 * @instance: the instance to connect to.
3042 * @detailed_signal: a string of the form "signal-name::detail".
3043 * @c_handler: the #GCallback to connect.
3044 * @gobject: the object to pass as data to @c_handler.
3045 * @connect_flags: a combination of #GConnnectFlags.
3047 * This is similar to g_signal_connect_data(), but uses a closure which
3048 * ensures that the @gobject stays alive during the call to @c_handler
3049 * by temporarily adding a reference count to @gobject.
3051 * Note that there is a bug in GObject that makes this function
3052 * much less useful than it might seem otherwise. Once @gobject is
3053 * disposed, the callback will no longer be called, but, the signal
3054 * handler is <emphasis>not</emphasis> currently disconnected. If the
3055 * @instance is itself being freed at the same time than this doesn't
3056 * matter, since the signal will automatically be removed, but
3057 * if @instance persists, then the signal handler will leak. You
3058 * should not remove the signal yourself because in a future versions of
3059 * GObject, the handler <emphasis>will</emphasis> automatically
3062 * It's possible to work around this problem in a way that will
3063 * continue to work with future versions of GObject by checking
3064 * that the signal handler is still connected before disconnected it:
3065 * <informalexample><programlisting>
3066 * if (g_signal_handler_is_connected (instance, id))
3067 * g_signal_handler_disconnect (instance, id);
3068 * </programlisting></informalexample>
3070 * Returns: the handler id.
3073 g_signal_connect_object (gpointer instance,
3074 const gchar *detailed_signal,
3075 GCallback c_handler,
3077 GConnectFlags connect_flags)
3079 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3080 g_return_val_if_fail (detailed_signal != NULL, 0);
3081 g_return_val_if_fail (c_handler != NULL, 0);
3087 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3089 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3091 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3094 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3100 GClosure *closures[1]; /* flexible array */
3102 /* don't change this structure without supplying an accessor for
3103 * watched closures, e.g.:
3104 * GSList* g_object_list_watched_closures (GObject *object)
3107 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3108 * carray = g_object_get_data (object, "GObject-closure-array");
3111 * GSList *slist = NULL;
3113 * for (i = 0; i < carray->n_closures; i++)
3114 * slist = g_slist_prepend (slist, carray->closures[i]);
3122 object_remove_closure (gpointer data,
3125 GObject *object = data;
3126 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3129 for (i = 0; i < carray->n_closures; i++)
3130 if (carray->closures[i] == closure)
3132 carray->n_closures--;
3133 if (i < carray->n_closures)
3134 carray->closures[i] = carray->closures[carray->n_closures];
3137 g_assert_not_reached ();
3141 destroy_closure_array (gpointer data)
3143 CArray *carray = data;
3144 GObject *object = carray->object;
3145 guint i, n = carray->n_closures;
3147 for (i = 0; i < n; i++)
3149 GClosure *closure = carray->closures[i];
3151 /* removing object_remove_closure() upfront is probably faster than
3152 * letting it fiddle with quark_closure_array which is empty anyways
3154 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3155 g_closure_invalidate (closure);
3161 * g_object_watch_closure:
3162 * @object: GObject restricting lifetime of @closure
3163 * @closure: GClosure to watch
3165 * This function essentially limits the life time of the @closure to
3166 * the life time of the object. That is, when the object is finalized,
3167 * the @closure is invalidated by calling g_closure_invalidate() on
3168 * it, in order to prevent invocations of the closure with a finalized
3169 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3170 * added as marshal guards to the @closure, to ensure that an extra
3171 * reference count is held on @object during invocation of the
3172 * @closure. Usually, this function will be called on closures that
3173 * use this @object as closure data.
3176 g_object_watch_closure (GObject *object,
3182 g_return_if_fail (G_IS_OBJECT (object));
3183 g_return_if_fail (closure != NULL);
3184 g_return_if_fail (closure->is_invalid == FALSE);
3185 g_return_if_fail (closure->in_marshal == FALSE);
3186 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3188 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3189 g_closure_add_marshal_guards (closure,
3190 object, (GClosureNotify) g_object_ref,
3191 object, (GClosureNotify) g_object_unref);
3192 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3195 carray = g_renew (CArray, NULL, 1);
3196 carray->object = object;
3197 carray->n_closures = 1;
3202 i = carray->n_closures++;
3203 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3205 carray->closures[i] = closure;
3206 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3210 * g_closure_new_object:
3211 * @sizeof_closure: the size of the structure to allocate, must be at least
3212 * <literal>sizeof (GClosure)</literal>
3213 * @object: a #GObject pointer to store in the @data field of the newly
3214 * allocated #GClosure
3216 * A variant of g_closure_new_simple() which stores @object in the
3217 * @data field of the closure and calls g_object_watch_closure() on
3218 * @object and the created closure. This function is mainly useful
3219 * when implementing new types of closures.
3221 * Returns: a newly allocated #GClosure
3224 g_closure_new_object (guint sizeof_closure,
3229 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3230 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3232 closure = g_closure_new_simple (sizeof_closure, object);
3233 g_object_watch_closure (object, closure);
3239 * g_cclosure_new_object:
3240 * @callback_func: the function to invoke
3241 * @object: a #GObject pointer to pass to @callback_func
3243 * A variant of g_cclosure_new() which uses @object as @user_data and
3244 * calls g_object_watch_closure() on @object and the created
3245 * closure. This function is useful when you have a callback closely
3246 * associated with a #GObject, and want the callback to no longer run
3247 * after the object is is freed.
3249 * Returns: a new #GCClosure
3252 g_cclosure_new_object (GCallback callback_func,
3257 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3258 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3259 g_return_val_if_fail (callback_func != NULL, NULL);
3261 closure = g_cclosure_new (callback_func, object, NULL);
3262 g_object_watch_closure (object, closure);
3268 * g_cclosure_new_object_swap:
3269 * @callback_func: the function to invoke
3270 * @object: a #GObject pointer to pass to @callback_func
3272 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3273 * and calls g_object_watch_closure() on @object and the created
3274 * closure. This function is useful when you have a callback closely
3275 * associated with a #GObject, and want the callback to no longer run
3276 * after the object is is freed.
3278 * Returns: a new #GCClosure
3281 g_cclosure_new_object_swap (GCallback callback_func,
3286 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3287 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3288 g_return_val_if_fail (callback_func != NULL, NULL);
3290 closure = g_cclosure_new_swap (callback_func, object, NULL);
3291 g_object_watch_closure (object, closure);
3297 g_object_compat_control (gsize what,
3303 case 1: /* floating base type */
3304 return G_TYPE_INITIALLY_UNOWNED;
3305 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3306 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3308 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3310 *pp = floating_flag_handler;
3317 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3320 g_initially_unowned_init (GInitiallyUnowned *object)
3322 g_object_force_floating (object);
3326 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3330 #define __G_OBJECT_C__
3331 #include "gobjectaliasdef.c"