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 /* fool the single inclusion guard in gatomic.h, which is directly
30 * included by gdatasetprivate.h
32 #define GLIB_COMPILATION
33 #include "glib/gdatasetprivate.h"
34 #undef GLIB_COMPILATION
37 #include "gvaluecollector.h"
39 #include "gparamspecs.h"
40 #include "gvaluetypes.h"
41 #include "gobject_trace.h"
43 #include "gobjectnotifyqueue.c"
47 * @short_description: The base object type
48 * @see_also: #GParamSpecObject, g_param_spec_object()
49 * @title: The Base Object Type
51 * GObject is the fundamental type providing the common attributes and
52 * methods for all object types in GTK+, Pango and other libraries
53 * based on GObject. The GObject class provides methods for object
54 * construction and destruction, property access methods, and signal
55 * support. Signals are described in detail in <xref
56 * linkend="gobject-Signals"/>.
58 * <para id="floating-ref">
59 * #GInitiallyUnowned is derived from #GObject. The only difference between
60 * the two is that the initial reference of a #GInitiallyUnowned is flagged
61 * as a <firstterm>floating</firstterm> reference.
62 * This means that it is not specifically claimed to be "owned" by
63 * any code portion. The main motivation for providing floating references is
64 * C convenience. In particular, it allows code to be written as:
66 * container = create_container();
67 * container_add_child (container, create_child());
69 * If <function>container_add_child()</function> will g_object_ref_sink() the
70 * passed in child, no reference of the newly created child is leaked.
71 * Without floating references, <function>container_add_child()</function>
72 * can only g_object_ref() the new child, so to implement this code without
73 * reference leaks, it would have to be written as:
76 * container = create_container();
77 * child = create_child();
78 * container_add_child (container, child);
79 * g_object_unref (child);
81 * The floating reference can be converted into
82 * an ordinary reference by calling g_object_ref_sink().
83 * For already sunken objects (objects that don't have a floating reference
84 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
86 * Since floating references are useful almost exclusively for C convenience,
87 * language bindings that provide automated reference and memory ownership
88 * maintenance (such as smart pointers or garbage collection) therefore don't
89 * need to expose floating references in their API.
92 * Some object implementations may need to save an objects floating state
93 * across certain code portions (an example is #GtkMenu), to achive this, the
94 * following sequence can be used:
97 * // save floating state
98 * gboolean was_floating = g_object_is_floating (object);
99 * g_object_ref_sink (object);
100 * // protected code portion
102 * // restore floating state
104 * g_object_force_floating (object);
105 * g_obejct_unref (object); // release previously acquired reference
111 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
112 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
114 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
115 #define OBJECT_HAS_TOGGLE_REF(object) \
116 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
117 #define OBJECT_FLOATING_FLAG 0x2
119 #define CLASS_HAS_PROPS_FLAG 0x1
120 #define CLASS_HAS_PROPS(class) \
121 ((class)->flags & CLASS_HAS_PROPS_FLAG)
122 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
123 ((class)->constructor != g_object_constructor)
125 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
126 #define CLASS_HAS_DERIVED_CLASS(class) \
127 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
129 /* --- signals --- */
136 /* --- properties --- */
142 /* --- prototypes --- */
143 static void g_object_base_class_init (GObjectClass *class);
144 static void g_object_base_class_finalize (GObjectClass *class);
145 static void g_object_do_class_init (GObjectClass *class);
146 static void g_object_init (GObject *object,
147 GObjectClass *class);
148 static GObject* g_object_constructor (GType type,
149 guint n_construct_properties,
150 GObjectConstructParam *construct_params);
151 static void g_object_real_dispose (GObject *object);
152 static void g_object_finalize (GObject *object);
153 static void g_object_do_set_property (GObject *object,
157 static void g_object_do_get_property (GObject *object,
161 static void g_value_object_init (GValue *value);
162 static void g_value_object_free_value (GValue *value);
163 static void g_value_object_copy_value (const GValue *src_value,
165 static void g_value_object_transform_value (const GValue *src_value,
167 static gpointer g_value_object_peek_pointer (const GValue *value);
168 static gchar* g_value_object_collect_value (GValue *value,
169 guint n_collect_values,
170 GTypeCValue *collect_values,
171 guint collect_flags);
172 static gchar* g_value_object_lcopy_value (const GValue *value,
173 guint n_collect_values,
174 GTypeCValue *collect_values,
175 guint collect_flags);
176 static void g_object_dispatch_properties_changed (GObject *object,
178 GParamSpec **pspecs);
179 static inline void object_get_property (GObject *object,
182 static inline void object_set_property (GObject *object,
185 GObjectNotifyQueue *nqueue);
186 static guint object_floating_flag_handler (GObject *object,
189 static void object_interface_check_properties (gpointer func_data,
193 /* --- variables --- */
194 static GQuark quark_closure_array = 0;
195 static GQuark quark_weak_refs = 0;
196 static GQuark quark_toggle_refs = 0;
197 static GParamSpecPool *pspec_pool = NULL;
198 static GObjectNotifyContext property_notify_context = { 0, };
199 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
200 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
201 G_LOCK_DEFINE_STATIC (construction_mutex);
202 static GSList *construction_objects = NULL;
204 /* --- functions --- */
205 #ifdef G_ENABLE_DEBUG
206 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
207 G_LOCK_DEFINE_STATIC (debug_objects);
208 static volatile GObject *g_trap_object_ref = NULL;
209 static guint debug_objects_count = 0;
210 static GHashTable *debug_objects_ht = NULL;
213 debug_objects_foreach (gpointer key,
217 GObject *object = value;
219 g_message ("[%p] stale %s\tref_count=%u",
221 G_OBJECT_TYPE_NAME (object),
226 debug_objects_atexit (void)
230 G_LOCK (debug_objects);
231 g_message ("stale GObjects: %u", debug_objects_count);
232 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
233 G_UNLOCK (debug_objects);
236 #endif /* G_ENABLE_DEBUG */
239 g_object_type_init (void)
241 static gboolean initialized = FALSE;
242 static const GTypeFundamentalInfo finfo = {
243 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
245 static GTypeInfo info = {
246 sizeof (GObjectClass),
247 (GBaseInitFunc) g_object_base_class_init,
248 (GBaseFinalizeFunc) g_object_base_class_finalize,
249 (GClassInitFunc) g_object_do_class_init,
250 NULL /* class_destroy */,
251 NULL /* class_data */,
254 (GInstanceInitFunc) g_object_init,
255 NULL, /* value_table */
257 static const GTypeValueTable value_table = {
258 g_value_object_init, /* value_init */
259 g_value_object_free_value, /* value_free */
260 g_value_object_copy_value, /* value_copy */
261 g_value_object_peek_pointer, /* value_peek_pointer */
262 "p", /* collect_format */
263 g_value_object_collect_value, /* collect_value */
264 "p", /* lcopy_format */
265 g_value_object_lcopy_value, /* lcopy_value */
269 g_return_if_fail (initialized == FALSE);
274 info.value_table = &value_table;
275 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
276 g_assert (type == G_TYPE_OBJECT);
277 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
279 #ifdef G_ENABLE_DEBUG
282 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
283 g_atexit (debug_objects_atexit);
285 #endif /* G_ENABLE_DEBUG */
289 g_object_base_class_init (GObjectClass *class)
291 GObjectClass *pclass = g_type_class_peek_parent (class);
293 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
294 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
297 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
299 /* reset instance specific fields and methods that don't get inherited */
300 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
301 class->get_property = NULL;
302 class->set_property = NULL;
306 g_object_base_class_finalize (GObjectClass *class)
310 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
312 g_slist_free (class->construct_properties);
313 class->construct_properties = NULL;
314 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
315 for (node = list; node; node = node->next)
317 GParamSpec *pspec = node->data;
319 g_param_spec_pool_remove (pspec_pool, pspec);
320 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
321 g_param_spec_unref (pspec);
327 g_object_notify_dispatcher (GObject *object,
331 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
335 g_object_do_class_init (GObjectClass *class)
337 /* read the comment about typedef struct CArray; on why not to change this quark */
338 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
340 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
341 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
342 pspec_pool = g_param_spec_pool_new (TRUE);
343 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
344 property_notify_context.dispatcher = g_object_notify_dispatcher;
346 class->constructor = g_object_constructor;
347 class->set_property = g_object_do_set_property;
348 class->get_property = g_object_do_get_property;
349 class->dispose = g_object_real_dispose;
350 class->finalize = g_object_finalize;
351 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
352 class->notify = NULL;
356 * @gobject: the object which received the signal.
357 * @pspec: the #GParamSpec of the property which changed.
359 * The notify signal is emitted on an object when one of its
360 * properties has been changed. Note that getting this signal
361 * doesn't guarantee that the value of the property has actually
362 * changed, it may also be emitted when the setter for the property
363 * is called to reinstate the previous value.
365 * This signal is typically used to obtain change notification for a
366 * single property, by specifying the property name as a detail in the
367 * g_signal_connect() call, like this:
369 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
370 * G_CALLBACK (gtk_text_view_target_list_notify),
373 * It is important to note that you must use
374 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
375 * detail strings for the notify signal.
377 gobject_signals[NOTIFY] =
378 g_signal_new (g_intern_static_string ("notify"),
379 G_TYPE_FROM_CLASS (class),
380 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
381 G_STRUCT_OFFSET (GObjectClass, notify),
383 g_cclosure_marshal_VOID__PARAM,
387 /* Install a check function that we'll use to verify that classes that
388 * implement an interface implement all properties for that interface
390 g_type_add_interface_check (NULL, object_interface_check_properties);
394 install_property_internal (GType g_type,
398 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
400 g_warning ("When installing property: type `%s' already has a property named `%s'",
401 g_type_name (g_type),
406 g_param_spec_ref (pspec);
407 g_param_spec_sink (pspec);
408 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
409 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
413 * g_object_class_install_property:
414 * @oclass: a #GObjectClass
415 * @property_id: the id for the new property
416 * @pspec: the #GParamSpec for the new property
418 * Installs a new property. This is usually done in the class initializer.
420 * Note that it is possible to redefine a property in a derived class,
421 * by installing a property with the same name. This can be useful at times,
422 * e.g. to change the range of allowed values or the default value.
425 g_object_class_install_property (GObjectClass *class,
429 g_return_if_fail (G_IS_OBJECT_CLASS (class));
430 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
432 if (CLASS_HAS_DERIVED_CLASS (class))
433 g_error ("Attempt to add property %s::%s to class after it was derived",
434 G_OBJECT_CLASS_NAME (class), pspec->name);
436 class->flags |= CLASS_HAS_PROPS_FLAG;
438 if (pspec->flags & G_PARAM_WRITABLE)
439 g_return_if_fail (class->set_property != NULL);
440 if (pspec->flags & G_PARAM_READABLE)
441 g_return_if_fail (class->get_property != NULL);
442 g_return_if_fail (property_id > 0);
443 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
444 if (pspec->flags & G_PARAM_CONSTRUCT)
445 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
446 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
447 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
449 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
451 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
452 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
454 /* for property overrides of construct poperties, we have to get rid
455 * of the overidden inherited construct property
457 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
458 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
459 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
463 * g_object_interface_install_property:
464 * @g_iface: any interface vtable for the interface, or the default
465 * vtable for the interface.
466 * @pspec: the #GParamSpec for the new property
468 * Add a property to an interface; this is only useful for interfaces
469 * that are added to GObject-derived types. Adding a property to an
470 * interface forces all objects classes with that interface to have a
471 * compatible property. The compatible property could be a newly
472 * created #GParamSpec, but normally
473 * g_object_class_override_property() will be used so that the object
474 * class only needs to provide an implementation and inherits the
475 * property description, default value, bounds, and so forth from the
476 * interface property.
478 * This function is meant to be called from the interface's default
479 * vtable initialization function (the @class_init member of
480 * #GTypeInfo.) It must not be called after after @class_init has
481 * been called for any object types implementing this interface.
486 g_object_interface_install_property (gpointer g_iface,
489 GTypeInterface *iface_class = g_iface;
491 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
492 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
493 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
494 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
496 install_property_internal (iface_class->g_type, 0, pspec);
500 * g_object_class_find_property:
501 * @oclass: a #GObjectClass
502 * @property_name: the name of the property to look up
504 * Looks up the #GParamSpec for a property of a class.
506 * Returns: the #GParamSpec for the property, or %NULL if the class
507 * doesn't have a property of that name
510 g_object_class_find_property (GObjectClass *class,
511 const gchar *property_name)
514 GParamSpec *redirect;
516 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
517 g_return_val_if_fail (property_name != NULL, NULL);
519 pspec = g_param_spec_pool_lookup (pspec_pool,
521 G_OBJECT_CLASS_TYPE (class),
525 redirect = g_param_spec_get_redirect_target (pspec);
536 * g_object_interface_find_property:
537 * @g_iface: any interface vtable for the interface, or the default
538 * vtable for the interface
539 * @property_name: name of a property to lookup.
541 * Find the #GParamSpec with the given name for an
542 * interface. Generally, the interface vtable passed in as @g_iface
543 * will be the default vtable from g_type_default_interface_ref(), or,
544 * if you know the interface has already been loaded,
545 * g_type_default_interface_peek().
549 * Returns: the #GParamSpec for the property of the interface with the
550 * name @property_name, or %NULL if no such property exists.
553 g_object_interface_find_property (gpointer g_iface,
554 const gchar *property_name)
556 GTypeInterface *iface_class = g_iface;
558 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
559 g_return_val_if_fail (property_name != NULL, NULL);
561 return g_param_spec_pool_lookup (pspec_pool,
568 * g_object_class_override_property:
569 * @oclass: a #GObjectClass
570 * @property_id: the new property ID
571 * @name: the name of a property registered in a parent class or
572 * in an interface of this class.
574 * Registers @property_id as referring to a property with the
575 * name @name in a parent class or in an interface implemented
576 * by @oclass. This allows this class to <firstterm>override</firstterm>
577 * a property implementation in a parent class or to provide
578 * the implementation of a property from an interface.
581 * Internally, overriding is implemented by creating a property of type
582 * #GParamSpecOverride; generally operations that query the properties of
583 * the object class, such as g_object_class_find_property() or
584 * g_object_class_list_properties() will return the overridden
585 * property. However, in one case, the @construct_properties argument of
586 * the @constructor virtual function, the #GParamSpecOverride is passed
587 * instead, so that the @param_id field of the #GParamSpec will be
588 * correct. For virtually all uses, this makes no difference. If you
589 * need to get the overridden property, you can call
590 * g_param_spec_get_redirect_target().
596 g_object_class_override_property (GObjectClass *oclass,
600 GParamSpec *overridden = NULL;
604 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
605 g_return_if_fail (property_id > 0);
606 g_return_if_fail (name != NULL);
608 /* Find the overridden property; first check parent types
610 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
611 if (parent_type != G_TYPE_NONE)
612 overridden = g_param_spec_pool_lookup (pspec_pool,
621 /* Now check interfaces
623 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
624 while (n_ifaces-- && !overridden)
626 overridden = g_param_spec_pool_lookup (pspec_pool,
637 g_warning ("%s: Can't find property to override for '%s::%s'",
638 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
642 new = g_param_spec_override (name, overridden);
643 g_object_class_install_property (oclass, property_id, new);
647 * g_object_class_list_properties:
648 * @oclass: a #GObjectClass
649 * @n_properties: return location for the length of the returned array
651 * Get an array of #GParamSpec* for all properties of a class.
653 * Returns: an array of #GParamSpec* which should be freed after use
655 GParamSpec** /* free result */
656 g_object_class_list_properties (GObjectClass *class,
657 guint *n_properties_p)
662 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
664 pspecs = g_param_spec_pool_list (pspec_pool,
665 G_OBJECT_CLASS_TYPE (class),
674 * g_object_interface_list_properties:
675 * @g_iface: any interface vtable for the interface, or the default
676 * vtable for the interface
677 * @n_properties_p: location to store number of properties returned.
679 * Lists the properties of an interface.Generally, the interface
680 * vtable passed in as @g_iface will be the default vtable from
681 * g_type_default_interface_ref(), or, if you know the interface has
682 * already been loaded, g_type_default_interface_peek().
686 * Returns: a pointer to an array of pointers to #GParamSpec
687 * structures. The paramspecs are owned by GLib, but the
688 * array should be freed with g_free() when you are done with
692 g_object_interface_list_properties (gpointer g_iface,
693 guint *n_properties_p)
695 GTypeInterface *iface_class = g_iface;
699 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
701 pspecs = g_param_spec_pool_list (pspec_pool,
711 g_object_init (GObject *object,
714 object->ref_count = 1;
715 g_datalist_init (&object->qdata);
717 if (CLASS_HAS_PROPS (class))
719 /* freeze object's notification queue, g_object_newv() preserves pairedness */
720 g_object_notify_queue_freeze (object, &property_notify_context);
723 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
725 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
726 G_LOCK (construction_mutex);
727 construction_objects = g_slist_prepend (construction_objects, object);
728 G_UNLOCK (construction_mutex);
731 #ifdef G_ENABLE_DEBUG
734 G_LOCK (debug_objects);
735 debug_objects_count++;
736 g_hash_table_insert (debug_objects_ht, object, object);
737 G_UNLOCK (debug_objects);
739 #endif /* G_ENABLE_DEBUG */
743 g_object_do_set_property (GObject *object,
751 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
757 g_object_do_get_property (GObject *object,
765 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
771 g_object_real_dispose (GObject *object)
773 g_signal_handlers_destroy (object);
774 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
775 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
779 g_object_finalize (GObject *object)
781 g_datalist_clear (&object->qdata);
783 #ifdef G_ENABLE_DEBUG
786 G_LOCK (debug_objects);
787 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
788 g_hash_table_remove (debug_objects_ht, object);
789 debug_objects_count--;
790 G_UNLOCK (debug_objects);
792 #endif /* G_ENABLE_DEBUG */
797 g_object_dispatch_properties_changed (GObject *object,
803 for (i = 0; i < n_pspecs; i++)
804 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
808 * g_object_run_dispose:
809 * @object: a #GObject
811 * Releases all references to other objects. This can be used to break
814 * This functions should only be called from object system implementations.
817 g_object_run_dispose (GObject *object)
819 g_return_if_fail (G_IS_OBJECT (object));
820 g_return_if_fail (object->ref_count > 0);
822 g_object_ref (object);
823 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
824 G_OBJECT_GET_CLASS (object)->dispose (object);
825 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
826 g_object_unref (object);
830 * g_object_freeze_notify:
831 * @object: a #GObject
833 * Increases the freeze count on @object. If the freeze count is
834 * non-zero, the emission of "notify" signals on @object is
835 * stopped. The signals are queued until the freeze count is decreased
838 * This is necessary for accessors that modify multiple properties to prevent
839 * premature notification while the object is still being modified.
842 g_object_freeze_notify (GObject *object)
844 g_return_if_fail (G_IS_OBJECT (object));
846 if (g_atomic_int_get (&object->ref_count) == 0)
849 g_object_ref (object);
850 g_object_notify_queue_freeze (object, &property_notify_context);
851 g_object_unref (object);
855 g_object_notify_by_spec_internal (GObject *object,
858 GObjectNotifyQueue *nqueue;
860 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
861 g_object_notify_queue_add (object, nqueue, pspec);
862 g_object_notify_queue_thaw (object, nqueue);
867 * @object: a #GObject
868 * @property_name: the name of a property installed on the class of @object.
870 * Emits a "notify" signal for the property @property_name on @object.
872 * When possible, eg. when signaling a property change from within the class
873 * that registered the property, you should use g_object_notify_by_pspec()
877 g_object_notify (GObject *object,
878 const gchar *property_name)
882 g_return_if_fail (G_IS_OBJECT (object));
883 g_return_if_fail (property_name != NULL);
884 if (g_atomic_int_get (&object->ref_count) == 0)
887 g_object_ref (object);
888 /* We don't need to get the redirect target
889 * (by, e.g. calling g_object_class_find_property())
890 * because g_object_notify_queue_add() does that
892 pspec = g_param_spec_pool_lookup (pspec_pool,
894 G_OBJECT_TYPE (object),
898 g_warning ("%s: object class `%s' has no property named `%s'",
900 G_OBJECT_TYPE_NAME (object),
903 g_object_notify_by_spec_internal (object, pspec);
904 g_object_unref (object);
908 * g_object_notify_by_pspec:
909 * @object: a #GObject
910 * @pspec: the #GParamSpec of a property installed on the class of @object.
912 * Emits a "notify" signal for the property specified by @pspec on @object.
914 * This function omits the property name lookup, hence it is faster than
917 * One way to avoid using g_object_notify() from within the
918 * class that registered the properties, and using g_object_notify_by_pspec()
919 * instead, is to store the GParamSpec used with
920 * g_object_class_install_property() inside a static array, e.g.:
930 * static GParamSpec *properties[PROP_LAST];
933 * my_object_class_init (MyObjectClass *klass)
935 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
938 * G_PARAM_READWRITE);
939 * g_object_class_install_property (gobject_class,
941 * properties[PROP_FOO]);
945 * and then notify a change on the "foo" property with:
948 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
954 g_object_notify_by_pspec (GObject *object,
958 g_return_if_fail (G_IS_OBJECT (object));
959 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
961 g_object_ref (object);
962 g_object_notify_by_spec_internal (object, pspec);
963 g_object_unref (object);
967 * g_object_thaw_notify:
968 * @object: a #GObject
970 * Reverts the effect of a previous call to
971 * g_object_freeze_notify(). The freeze count is decreased on @object
972 * and when it reaches zero, all queued "notify" signals are emitted.
974 * It is an error to call this function when the freeze count is zero.
977 g_object_thaw_notify (GObject *object)
979 GObjectNotifyQueue *nqueue;
981 g_return_if_fail (G_IS_OBJECT (object));
982 if (g_atomic_int_get (&object->ref_count) == 0)
985 g_object_ref (object);
987 /* FIXME: Freezing is the only way to get at the notify queue.
988 * So we freeze once and then thaw twice.
990 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
991 g_object_notify_queue_thaw (object, nqueue);
992 g_object_notify_queue_thaw (object, nqueue);
994 g_object_unref (object);
998 object_get_property (GObject *object,
1002 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1003 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1004 GParamSpec *redirect;
1006 redirect = g_param_spec_get_redirect_target (pspec);
1010 class->get_property (object, param_id, value, pspec);
1014 object_set_property (GObject *object,
1016 const GValue *value,
1017 GObjectNotifyQueue *nqueue)
1019 GValue tmp_value = { 0, };
1020 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1021 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1022 GParamSpec *redirect;
1023 static gchar* enable_diagnostic = NULL;
1025 redirect = g_param_spec_get_redirect_target (pspec);
1029 if (G_UNLIKELY (!enable_diagnostic))
1031 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1032 if (!enable_diagnostic)
1033 enable_diagnostic = "0";
1036 if (enable_diagnostic[0] == '1')
1038 if (pspec->flags & G_PARAM_DEPRECATED)
1039 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1040 "anymore. It will be removed in a future version.",
1041 G_OBJECT_TYPE_NAME (object), pspec->name);
1044 /* provide a copy to work from, convert (if necessary) and validate */
1045 g_value_init (&tmp_value, pspec->value_type);
1046 if (!g_value_transform (value, &tmp_value))
1047 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1049 g_type_name (pspec->value_type),
1050 G_VALUE_TYPE_NAME (value));
1051 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1053 gchar *contents = g_strdup_value_contents (value);
1055 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1057 G_VALUE_TYPE_NAME (value),
1059 g_type_name (pspec->value_type));
1064 class->set_property (object, param_id, &tmp_value, pspec);
1065 g_object_notify_queue_add (object, nqueue, pspec);
1067 g_value_unset (&tmp_value);
1071 object_interface_check_properties (gpointer func_data,
1074 GTypeInterface *iface_class = g_iface;
1075 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1076 GType iface_type = iface_class->g_type;
1077 GParamSpec **pspecs;
1080 if (!G_IS_OBJECT_CLASS (class))
1083 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1087 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1089 G_OBJECT_CLASS_TYPE (class),
1094 g_critical ("Object class %s doesn't implement property "
1095 "'%s' from interface '%s'",
1096 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1098 g_type_name (iface_type));
1103 /* The implementation paramspec must have a less restrictive
1104 * type than the interface parameter spec for set() and a
1105 * more restrictive type for get(). We just require equality,
1106 * rather than doing something more complicated checking
1107 * the READABLE and WRITABLE flags. We also simplify here
1108 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1111 !g_type_is_a (pspecs[n]->value_type,
1112 class_pspec->value_type))
1114 g_critical ("Property '%s' on class '%s' has type '%s' "
1115 "which is different from the type '%s', "
1116 "of the property on interface '%s'\n",
1118 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1119 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1120 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1121 g_type_name (iface_type));
1124 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1126 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1127 * READABLE and WRITABLE remove restrictions. The implementation
1128 * paramspec must have less restrictive flags.
1131 (!SUBSET (class_pspec->flags,
1133 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1134 !SUBSET (pspecs[n]->flags,
1136 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1138 g_critical ("Flags for property '%s' on class '%s' "
1139 "are not compatible with the property on"
1142 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1143 g_type_name (iface_type));
1152 g_object_get_type (void)
1154 return G_TYPE_OBJECT;
1159 * @object_type: the type id of the #GObject subtype to instantiate
1160 * @first_property_name: the name of the first property
1161 * @...: the value of the first property, followed optionally by more
1162 * name/value pairs, followed by %NULL
1164 * Creates a new instance of a #GObject subtype and sets its properties.
1166 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1167 * which are not explicitly specified are set to their default values.
1169 * Returns: a new instance of @object_type
1172 g_object_new (GType object_type,
1173 const gchar *first_property_name,
1179 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1181 /* short circuit for calls supplying no properties */
1182 if (!first_property_name)
1183 return g_object_newv (object_type, 0, NULL);
1185 va_start (var_args, first_property_name);
1186 object = g_object_new_valist (object_type, first_property_name, var_args);
1193 slist_maybe_remove (GSList **slist,
1196 GSList *last = NULL, *node = *slist;
1199 if (node->data == data)
1202 last->next = node->next;
1204 *slist = node->next;
1205 g_slist_free_1 (node);
1214 static inline gboolean
1215 object_in_construction_list (GObject *object)
1217 gboolean in_construction;
1218 G_LOCK (construction_mutex);
1219 in_construction = g_slist_find (construction_objects, object) != NULL;
1220 G_UNLOCK (construction_mutex);
1221 return in_construction;
1226 * @object_type: the type id of the #GObject subtype to instantiate
1227 * @n_parameters: the length of the @parameters array
1228 * @parameters: an array of #GParameter
1230 * Creates a new instance of a #GObject subtype and sets its properties.
1232 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1233 * which are not explicitly specified are set to their default values.
1235 * Returns: a new instance of @object_type
1238 g_object_newv (GType object_type,
1240 GParameter *parameters)
1242 GObjectConstructParam *cparams = NULL, *oparams;
1243 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1245 GObjectClass *class, *unref_class = NULL;
1247 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1249 GList *clist = NULL;
1250 gboolean newly_constructed;
1253 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1255 class = g_type_class_peek_static (object_type);
1257 class = unref_class = g_type_class_ref (object_type);
1258 for (slist = class->construct_properties; slist; slist = slist->next)
1260 clist = g_list_prepend (clist, slist->data);
1261 n_total_cparams += 1;
1264 if (n_parameters == 0 && n_total_cparams == 0)
1266 /* This is a simple object with no construct properties, and
1267 * no properties are being set, so short circuit the parameter
1268 * handling. This speeds up simple object construction.
1271 object = class->constructor (object_type, 0, NULL);
1272 goto did_construction;
1275 /* collect parameters, sort into construction and normal ones */
1276 oparams = g_new (GObjectConstructParam, n_parameters);
1277 cparams = g_new (GObjectConstructParam, n_total_cparams);
1278 for (i = 0; i < n_parameters; i++)
1280 GValue *value = ¶meters[i].value;
1281 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1287 g_warning ("%s: object class `%s' has no property named `%s'",
1289 g_type_name (object_type),
1290 parameters[i].name);
1293 if (!(pspec->flags & G_PARAM_WRITABLE))
1295 g_warning ("%s: property `%s' of object class `%s' is not writable",
1298 g_type_name (object_type));
1301 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1303 GList *list = g_list_find (clist, pspec);
1307 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1308 G_STRFUNC, pspec->name, g_type_name (object_type));
1311 cparams[n_cparams].pspec = pspec;
1312 cparams[n_cparams].value = value;
1317 list->prev->next = list->next;
1319 list->next->prev = list->prev;
1320 g_list_free_1 (list);
1324 oparams[n_oparams].pspec = pspec;
1325 oparams[n_oparams].value = value;
1330 /* set remaining construction properties to default values */
1331 n_cvalues = n_total_cparams - n_cparams;
1332 cvalues = g_new (GValue, n_cvalues);
1335 GList *tmp = clist->next;
1336 GParamSpec *pspec = clist->data;
1337 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1340 g_value_init (value, pspec->value_type);
1341 g_param_value_set_default (pspec, value);
1343 cparams[n_cparams].pspec = pspec;
1344 cparams[n_cparams].value = value;
1347 g_list_free_1 (clist);
1351 /* construct object from construction parameters */
1352 object = class->constructor (object_type, n_total_cparams, cparams);
1353 /* free construction values */
1356 g_value_unset (cvalues + n_cvalues);
1360 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1362 /* adjust freeze_count according to g_object_init() and remaining properties */
1363 G_LOCK (construction_mutex);
1364 newly_constructed = slist_maybe_remove (&construction_objects, object);
1365 G_UNLOCK (construction_mutex);
1368 newly_constructed = TRUE;
1370 if (CLASS_HAS_PROPS (class))
1372 if (newly_constructed || n_oparams)
1373 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1374 if (newly_constructed)
1375 g_object_notify_queue_thaw (object, nqueue);
1378 /* run 'constructed' handler if there is one */
1379 if (newly_constructed && class->constructed)
1380 class->constructed (object);
1382 /* set remaining properties */
1383 for (i = 0; i < n_oparams; i++)
1384 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1387 if (CLASS_HAS_PROPS (class))
1389 /* release our own freeze count and handle notifications */
1390 if (newly_constructed || n_oparams)
1391 g_object_notify_queue_thaw (object, nqueue);
1395 g_type_class_unref (unref_class);
1401 * g_object_new_valist:
1402 * @object_type: the type id of the #GObject subtype to instantiate
1403 * @first_property_name: the name of the first property
1404 * @var_args: the value of the first property, followed optionally by more
1405 * name/value pairs, followed by %NULL
1407 * Creates a new instance of a #GObject subtype and sets its properties.
1409 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1410 * which are not explicitly specified are set to their default values.
1412 * Returns: a new instance of @object_type
1415 g_object_new_valist (GType object_type,
1416 const gchar *first_property_name,
1419 GObjectClass *class;
1423 guint n_params = 0, n_alloced_params = 16;
1425 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1427 if (!first_property_name)
1428 return g_object_newv (object_type, 0, NULL);
1430 class = g_type_class_ref (object_type);
1432 params = g_new0 (GParameter, n_alloced_params);
1433 name = first_property_name;
1436 gchar *error = NULL;
1437 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1443 g_warning ("%s: object class `%s' has no property named `%s'",
1445 g_type_name (object_type),
1449 if (n_params >= n_alloced_params)
1451 n_alloced_params += 16;
1452 params = g_renew (GParameter, params, n_alloced_params);
1454 params[n_params].name = name;
1455 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1456 var_args, 0, &error);
1459 g_warning ("%s: %s", G_STRFUNC, error);
1461 g_value_unset (¶ms[n_params].value);
1465 name = va_arg (var_args, gchar*);
1468 object = g_object_newv (object_type, n_params, params);
1471 g_value_unset (¶ms[n_params].value);
1474 g_type_class_unref (class);
1480 g_object_constructor (GType type,
1481 guint n_construct_properties,
1482 GObjectConstructParam *construct_params)
1487 object = (GObject*) g_type_create_instance (type);
1489 /* set construction parameters */
1490 if (n_construct_properties)
1492 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1494 /* set construct properties */
1495 while (n_construct_properties--)
1497 GValue *value = construct_params->value;
1498 GParamSpec *pspec = construct_params->pspec;
1501 object_set_property (object, pspec, value, nqueue);
1503 g_object_notify_queue_thaw (object, nqueue);
1504 /* the notification queue is still frozen from g_object_init(), so
1505 * we don't need to handle it here, g_object_newv() takes
1514 * g_object_set_valist:
1515 * @object: a #GObject
1516 * @first_property_name: name of the first property to set
1517 * @var_args: value for the first property, followed optionally by more
1518 * name/value pairs, followed by %NULL
1520 * Sets properties on an object.
1523 g_object_set_valist (GObject *object,
1524 const gchar *first_property_name,
1527 GObjectNotifyQueue *nqueue;
1530 g_return_if_fail (G_IS_OBJECT (object));
1532 g_object_ref (object);
1533 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1535 name = first_property_name;
1538 GValue value = { 0, };
1540 gchar *error = NULL;
1542 pspec = g_param_spec_pool_lookup (pspec_pool,
1544 G_OBJECT_TYPE (object),
1548 g_warning ("%s: object class `%s' has no property named `%s'",
1550 G_OBJECT_TYPE_NAME (object),
1554 if (!(pspec->flags & G_PARAM_WRITABLE))
1556 g_warning ("%s: property `%s' of object class `%s' is not writable",
1559 G_OBJECT_TYPE_NAME (object));
1562 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1564 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1565 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1569 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1573 g_warning ("%s: %s", G_STRFUNC, error);
1575 g_value_unset (&value);
1579 object_set_property (object, pspec, &value, nqueue);
1580 g_value_unset (&value);
1582 name = va_arg (var_args, gchar*);
1585 g_object_notify_queue_thaw (object, nqueue);
1586 g_object_unref (object);
1590 * g_object_get_valist:
1591 * @object: a #GObject
1592 * @first_property_name: name of the first property to get
1593 * @var_args: return location for the first property, followed optionally by more
1594 * name/return location pairs, followed by %NULL
1596 * Gets properties of an object.
1598 * In general, a copy is made of the property contents and the caller
1599 * is responsible for freeing the memory in the appropriate manner for
1600 * the type, for instance by calling g_free() or g_object_unref().
1602 * See g_object_get().
1605 g_object_get_valist (GObject *object,
1606 const gchar *first_property_name,
1611 g_return_if_fail (G_IS_OBJECT (object));
1613 g_object_ref (object);
1615 name = first_property_name;
1619 GValue value = { 0, };
1623 pspec = g_param_spec_pool_lookup (pspec_pool,
1625 G_OBJECT_TYPE (object),
1629 g_warning ("%s: object class `%s' has no property named `%s'",
1631 G_OBJECT_TYPE_NAME (object),
1635 if (!(pspec->flags & G_PARAM_READABLE))
1637 g_warning ("%s: property `%s' of object class `%s' is not readable",
1640 G_OBJECT_TYPE_NAME (object));
1644 g_value_init (&value, pspec->value_type);
1646 object_get_property (object, pspec, &value);
1648 G_VALUE_LCOPY (&value, var_args, 0, &error);
1651 g_warning ("%s: %s", G_STRFUNC, error);
1653 g_value_unset (&value);
1657 g_value_unset (&value);
1659 name = va_arg (var_args, gchar*);
1662 g_object_unref (object);
1667 * @object: a #GObject
1668 * @first_property_name: name of the first property to set
1669 * @...: value for the first property, followed optionally by more
1670 * name/value pairs, followed by %NULL
1672 * Sets properties on an object.
1675 g_object_set (gpointer _object,
1676 const gchar *first_property_name,
1679 GObject *object = _object;
1682 g_return_if_fail (G_IS_OBJECT (object));
1684 va_start (var_args, first_property_name);
1685 g_object_set_valist (object, first_property_name, var_args);
1691 * @object: a #GObject
1692 * @first_property_name: name of the first property to get
1693 * @...: return location for the first property, followed optionally by more
1694 * name/return location pairs, followed by %NULL
1696 * Gets properties of an object.
1698 * In general, a copy is made of the property contents and the caller
1699 * is responsible for freeing the memory in the appropriate manner for
1700 * the type, for instance by calling g_free() or g_object_unref().
1703 * <title>Using g_object_get(<!-- -->)</title>
1704 * An example of using g_object_get() to get the contents
1705 * of three properties - one of type #G_TYPE_INT,
1706 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1712 * g_object_get (my_object,
1713 * "int-property", &intval,
1714 * "str-property", &strval,
1715 * "obj-property", &objval,
1718 * // Do something with intval, strval, objval
1721 * g_object_unref (objval);
1726 g_object_get (gpointer _object,
1727 const gchar *first_property_name,
1730 GObject *object = _object;
1733 g_return_if_fail (G_IS_OBJECT (object));
1735 va_start (var_args, first_property_name);
1736 g_object_get_valist (object, first_property_name, var_args);
1741 * g_object_set_property:
1742 * @object: a #GObject
1743 * @property_name: the name of the property to set
1746 * Sets a property on an object.
1749 g_object_set_property (GObject *object,
1750 const gchar *property_name,
1751 const GValue *value)
1753 GObjectNotifyQueue *nqueue;
1756 g_return_if_fail (G_IS_OBJECT (object));
1757 g_return_if_fail (property_name != NULL);
1758 g_return_if_fail (G_IS_VALUE (value));
1760 g_object_ref (object);
1761 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1763 pspec = g_param_spec_pool_lookup (pspec_pool,
1765 G_OBJECT_TYPE (object),
1768 g_warning ("%s: object class `%s' has no property named `%s'",
1770 G_OBJECT_TYPE_NAME (object),
1772 else if (!(pspec->flags & G_PARAM_WRITABLE))
1773 g_warning ("%s: property `%s' of object class `%s' is not writable",
1776 G_OBJECT_TYPE_NAME (object));
1777 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1778 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1779 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1781 object_set_property (object, pspec, value, nqueue);
1783 g_object_notify_queue_thaw (object, nqueue);
1784 g_object_unref (object);
1788 * g_object_get_property:
1789 * @object: a #GObject
1790 * @property_name: the name of the property to get
1791 * @value: return location for the property value
1793 * Gets a property of an object.
1795 * In general, a copy is made of the property contents and the caller is
1796 * responsible for freeing the memory by calling g_value_unset().
1798 * Note that g_object_get_property() is really intended for language
1799 * bindings, g_object_get() is much more convenient for C programming.
1802 g_object_get_property (GObject *object,
1803 const gchar *property_name,
1808 g_return_if_fail (G_IS_OBJECT (object));
1809 g_return_if_fail (property_name != NULL);
1810 g_return_if_fail (G_IS_VALUE (value));
1812 g_object_ref (object);
1814 pspec = g_param_spec_pool_lookup (pspec_pool,
1816 G_OBJECT_TYPE (object),
1819 g_warning ("%s: object class `%s' has no property named `%s'",
1821 G_OBJECT_TYPE_NAME (object),
1823 else if (!(pspec->flags & G_PARAM_READABLE))
1824 g_warning ("%s: property `%s' of object class `%s' is not readable",
1827 G_OBJECT_TYPE_NAME (object));
1830 GValue *prop_value, tmp_value = { 0, };
1832 /* auto-conversion of the callers value type
1834 if (G_VALUE_TYPE (value) == pspec->value_type)
1836 g_value_reset (value);
1839 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1841 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1842 G_STRFUNC, pspec->name,
1843 g_type_name (pspec->value_type),
1844 G_VALUE_TYPE_NAME (value));
1845 g_object_unref (object);
1850 g_value_init (&tmp_value, pspec->value_type);
1851 prop_value = &tmp_value;
1853 object_get_property (object, pspec, prop_value);
1854 if (prop_value != value)
1856 g_value_transform (prop_value, value);
1857 g_value_unset (&tmp_value);
1861 g_object_unref (object);
1866 * @object: a #GObject
1867 * @signal_spec: the spec for the first signal
1868 * @...: #GCallback for the first signal, followed by data for the
1869 * first signal, followed optionally by more signal
1870 * spec/callback/data triples, followed by %NULL
1872 * A convenience function to connect multiple signals at once.
1874 * The signal specs expected by this function have the form
1875 * "modifier::signal_name", where modifier can be one of the following:
1878 * <term>signal</term>
1880 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1881 * </para></listitem>
1884 * <term>object_signal</term>
1885 * <term>object-signal</term>
1887 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1888 * </para></listitem>
1891 * <term>swapped_signal</term>
1892 * <term>swapped-signal</term>
1894 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1895 * </para></listitem>
1898 * <term>swapped_object_signal</term>
1899 * <term>swapped-object-signal</term>
1901 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1902 * </para></listitem>
1905 * <term>signal_after</term>
1906 * <term>signal-after</term>
1908 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1909 * </para></listitem>
1912 * <term>object_signal_after</term>
1913 * <term>object-signal-after</term>
1915 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1916 * </para></listitem>
1919 * <term>swapped_signal_after</term>
1920 * <term>swapped-signal-after</term>
1922 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1923 * </para></listitem>
1926 * <term>swapped_object_signal_after</term>
1927 * <term>swapped-object-signal-after</term>
1929 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1930 * </para></listitem>
1935 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1936 * "type", GTK_WINDOW_POPUP,
1939 * "signal::event", gtk_menu_window_event, menu,
1940 * "signal::size_request", gtk_menu_window_size_request, menu,
1941 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1948 g_object_connect (gpointer _object,
1949 const gchar *signal_spec,
1952 GObject *object = _object;
1955 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1956 g_return_val_if_fail (object->ref_count > 0, object);
1958 va_start (var_args, signal_spec);
1961 GCallback callback = va_arg (var_args, GCallback);
1962 gpointer data = va_arg (var_args, gpointer);
1965 if (strncmp (signal_spec, "signal::", 8) == 0)
1966 sid = g_signal_connect_data (object, signal_spec + 8,
1967 callback, data, NULL,
1969 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1970 strncmp (signal_spec, "object-signal::", 15) == 0)
1971 sid = g_signal_connect_object (object, signal_spec + 15,
1974 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1975 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1976 sid = g_signal_connect_data (object, signal_spec + 16,
1977 callback, data, NULL,
1979 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1980 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1981 sid = g_signal_connect_object (object, signal_spec + 23,
1984 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1985 strncmp (signal_spec, "signal-after::", 14) == 0)
1986 sid = g_signal_connect_data (object, signal_spec + 14,
1987 callback, data, NULL,
1989 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1990 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1991 sid = g_signal_connect_object (object, signal_spec + 21,
1994 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1995 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1996 sid = g_signal_connect_data (object, signal_spec + 22,
1997 callback, data, NULL,
1998 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1999 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2000 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2001 sid = g_signal_connect_object (object, signal_spec + 29,
2003 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2006 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2009 signal_spec = va_arg (var_args, gchar*);
2017 * g_object_disconnect:
2018 * @object: a #GObject
2019 * @signal_spec: the spec for the first signal
2020 * @...: #GCallback for the first signal, followed by data for the first signal,
2021 * followed optionally by more signal spec/callback/data triples,
2024 * A convenience function to disconnect multiple signals at once.
2026 * The signal specs expected by this function have the form
2027 * "any_signal", which means to disconnect any signal with matching
2028 * callback and data, or "any_signal::signal_name", which only
2029 * disconnects the signal named "signal_name".
2032 g_object_disconnect (gpointer _object,
2033 const gchar *signal_spec,
2036 GObject *object = _object;
2039 g_return_if_fail (G_IS_OBJECT (object));
2040 g_return_if_fail (object->ref_count > 0);
2042 va_start (var_args, signal_spec);
2045 GCallback callback = va_arg (var_args, GCallback);
2046 gpointer data = va_arg (var_args, gpointer);
2047 guint sid = 0, detail = 0, mask = 0;
2049 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2050 strncmp (signal_spec, "any-signal::", 12) == 0)
2053 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2055 else if (strcmp (signal_spec, "any_signal") == 0 ||
2056 strcmp (signal_spec, "any-signal") == 0)
2059 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2063 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2067 if ((mask & G_SIGNAL_MATCH_ID) &&
2068 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2069 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2070 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2072 NULL, (gpointer)callback, data))
2073 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2074 signal_spec = va_arg (var_args, gchar*);
2085 } weak_refs[1]; /* flexible array */
2089 weak_refs_notify (gpointer data)
2091 WeakRefStack *wstack = data;
2094 for (i = 0; i < wstack->n_weak_refs; i++)
2095 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2100 * g_object_weak_ref:
2101 * @object: #GObject to reference weakly
2102 * @notify: callback to invoke before the object is freed
2103 * @data: extra data to pass to notify
2105 * Adds a weak reference callback to an object. Weak references are
2106 * used for notification when an object is finalized. They are called
2107 * "weak references" because they allow you to safely hold a pointer
2108 * to an object without calling g_object_ref() (g_object_ref() adds a
2109 * strong reference, that is, forces the object to stay alive).
2112 g_object_weak_ref (GObject *object,
2116 WeakRefStack *wstack;
2119 g_return_if_fail (G_IS_OBJECT (object));
2120 g_return_if_fail (notify != NULL);
2121 g_return_if_fail (object->ref_count >= 1);
2123 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2126 i = wstack->n_weak_refs++;
2127 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2131 wstack = g_renew (WeakRefStack, NULL, 1);
2132 wstack->object = object;
2133 wstack->n_weak_refs = 1;
2136 wstack->weak_refs[i].notify = notify;
2137 wstack->weak_refs[i].data = data;
2138 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2142 * g_object_weak_unref:
2143 * @object: #GObject to remove a weak reference from
2144 * @notify: callback to search for
2145 * @data: data to search for
2147 * Removes a weak reference callback to an object.
2150 g_object_weak_unref (GObject *object,
2154 WeakRefStack *wstack;
2155 gboolean found_one = FALSE;
2157 g_return_if_fail (G_IS_OBJECT (object));
2158 g_return_if_fail (notify != NULL);
2160 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2165 for (i = 0; i < wstack->n_weak_refs; i++)
2166 if (wstack->weak_refs[i].notify == notify &&
2167 wstack->weak_refs[i].data == data)
2170 wstack->n_weak_refs -= 1;
2171 if (i != wstack->n_weak_refs)
2172 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2178 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2182 * g_object_add_weak_pointer:
2183 * @object: The object that should be weak referenced.
2184 * @weak_pointer_location: The memory address of a pointer.
2186 * Adds a weak reference from weak_pointer to @object to indicate that
2187 * the pointer located at @weak_pointer_location is only valid during
2188 * the lifetime of @object. When the @object is finalized,
2189 * @weak_pointer will be set to %NULL.
2192 g_object_add_weak_pointer (GObject *object,
2193 gpointer *weak_pointer_location)
2195 g_return_if_fail (G_IS_OBJECT (object));
2196 g_return_if_fail (weak_pointer_location != NULL);
2198 g_object_weak_ref (object,
2199 (GWeakNotify) g_nullify_pointer,
2200 weak_pointer_location);
2204 * g_object_remove_weak_pointer:
2205 * @object: The object that is weak referenced.
2206 * @weak_pointer_location: The memory address of a pointer.
2208 * Removes a weak reference from @object that was previously added
2209 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2210 * to match the one used with g_object_add_weak_pointer().
2213 g_object_remove_weak_pointer (GObject *object,
2214 gpointer *weak_pointer_location)
2216 g_return_if_fail (G_IS_OBJECT (object));
2217 g_return_if_fail (weak_pointer_location != NULL);
2219 g_object_weak_unref (object,
2220 (GWeakNotify) g_nullify_pointer,
2221 weak_pointer_location);
2225 object_floating_flag_handler (GObject *object,
2231 case +1: /* force floating if possible */
2233 oldvalue = g_atomic_pointer_get (&object->qdata);
2234 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2235 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2236 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2237 case -1: /* sink if possible */
2239 oldvalue = g_atomic_pointer_get (&object->qdata);
2240 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2241 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2242 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2243 default: /* check floating */
2244 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2249 * g_object_is_floating:
2250 * @object: a #GObject
2252 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2257 * Returns: %TRUE if @object has a floating reference
2260 g_object_is_floating (gpointer _object)
2262 GObject *object = _object;
2263 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2264 return floating_flag_handler (object, 0);
2268 * g_object_ref_sink:
2269 * @object: a #GObject
2271 * Increase the reference count of @object, and possibly remove the
2272 * <link linkend="floating-ref">floating</link> reference, if @object
2273 * has a floating reference.
2275 * In other words, if the object is floating, then this call "assumes
2276 * ownership" of the floating reference, converting it to a normal
2277 * reference by clearing the floating flag while leaving the reference
2278 * count unchanged. If the object is not floating, then this call
2279 * adds a new normal reference increasing the reference count by one.
2286 g_object_ref_sink (gpointer _object)
2288 GObject *object = _object;
2289 gboolean was_floating;
2290 g_return_val_if_fail (G_IS_OBJECT (object), object);
2291 g_return_val_if_fail (object->ref_count >= 1, object);
2292 g_object_ref (object);
2293 was_floating = floating_flag_handler (object, -1);
2295 g_object_unref (object);
2300 * g_object_force_floating:
2301 * @object: a #GObject
2303 * This function is intended for #GObject implementations to re-enforce a
2304 * <link linkend="floating-ref">floating</link> object reference.
2305 * Doing this is seldomly required, all
2306 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2307 * usually just needs to be sunken by calling g_object_ref_sink().
2312 g_object_force_floating (GObject *object)
2314 gboolean was_floating;
2315 g_return_if_fail (G_IS_OBJECT (object));
2316 g_return_if_fail (object->ref_count >= 1);
2318 was_floating = floating_flag_handler (object, +1);
2323 guint n_toggle_refs;
2325 GToggleNotify notify;
2327 } toggle_refs[1]; /* flexible array */
2331 toggle_refs_notify (GObject *object,
2332 gboolean is_last_ref)
2334 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2336 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2337 * will only be notified when there is exactly one of them.
2339 g_assert (tstack->n_toggle_refs == 1);
2340 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2344 * g_object_add_toggle_ref:
2345 * @object: a #GObject
2346 * @notify: a function to call when this reference is the
2347 * last reference to the object, or is no longer
2348 * the last reference.
2349 * @data: data to pass to @notify
2351 * Increases the reference count of the object by one and sets a
2352 * callback to be called when all other references to the object are
2353 * dropped, or when this is already the last reference to the object
2354 * and another reference is established.
2356 * This functionality is intended for binding @object to a proxy
2357 * object managed by another memory manager. This is done with two
2358 * paired references: the strong reference added by
2359 * g_object_add_toggle_ref() and a reverse reference to the proxy
2360 * object which is either a strong reference or weak reference.
2362 * The setup is that when there are no other references to @object,
2363 * only a weak reference is held in the reverse direction from @object
2364 * to the proxy object, but when there are other references held to
2365 * @object, a strong reference is held. The @notify callback is called
2366 * when the reference from @object to the proxy object should be
2367 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2368 * true) or weak to strong (@is_last_ref false).
2370 * Since a (normal) reference must be held to the object before
2371 * calling g_object_toggle_ref(), the initial state of the reverse
2372 * link is always strong.
2374 * Multiple toggle references may be added to the same gobject,
2375 * however if there are multiple toggle references to an object, none
2376 * of them will ever be notified until all but one are removed. For
2377 * this reason, you should only ever use a toggle reference if there
2378 * is important state in the proxy object.
2383 g_object_add_toggle_ref (GObject *object,
2384 GToggleNotify notify,
2387 ToggleRefStack *tstack;
2390 g_return_if_fail (G_IS_OBJECT (object));
2391 g_return_if_fail (notify != NULL);
2392 g_return_if_fail (object->ref_count >= 1);
2394 g_object_ref (object);
2396 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2399 i = tstack->n_toggle_refs++;
2400 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2401 * in tstate->toggle_refs */
2402 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2406 tstack = g_renew (ToggleRefStack, NULL, 1);
2407 tstack->object = object;
2408 tstack->n_toggle_refs = 1;
2412 /* Set a flag for fast lookup after adding the first toggle reference */
2413 if (tstack->n_toggle_refs == 1)
2414 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2416 tstack->toggle_refs[i].notify = notify;
2417 tstack->toggle_refs[i].data = data;
2418 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2419 (GDestroyNotify)g_free);
2423 * g_object_remove_toggle_ref:
2424 * @object: a #GObject
2425 * @notify: a function to call when this reference is the
2426 * last reference to the object, or is no longer
2427 * the last reference.
2428 * @data: data to pass to @notify
2430 * Removes a reference added with g_object_add_toggle_ref(). The
2431 * reference count of the object is decreased by one.
2436 g_object_remove_toggle_ref (GObject *object,
2437 GToggleNotify notify,
2440 ToggleRefStack *tstack;
2441 gboolean found_one = FALSE;
2443 g_return_if_fail (G_IS_OBJECT (object));
2444 g_return_if_fail (notify != NULL);
2446 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2451 for (i = 0; i < tstack->n_toggle_refs; i++)
2452 if (tstack->toggle_refs[i].notify == notify &&
2453 tstack->toggle_refs[i].data == data)
2456 tstack->n_toggle_refs -= 1;
2457 if (i != tstack->n_toggle_refs)
2458 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2460 if (tstack->n_toggle_refs == 0)
2461 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2463 g_object_unref (object);
2470 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2475 * @object: a #GObject
2477 * Increases the reference count of @object.
2479 * Returns: the same @object
2482 g_object_ref (gpointer _object)
2484 GObject *object = _object;
2487 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2488 g_return_val_if_fail (object->ref_count > 0, NULL);
2490 #ifdef G_ENABLE_DEBUG
2491 if (g_trap_object_ref == object)
2493 #endif /* G_ENABLE_DEBUG */
2496 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2498 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2499 toggle_refs_notify (object, FALSE);
2501 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2508 * @object: a #GObject
2510 * Decreases the reference count of @object. When its reference count
2511 * drops to 0, the object is finalized (i.e. its memory is freed).
2514 g_object_unref (gpointer _object)
2516 GObject *object = _object;
2519 g_return_if_fail (G_IS_OBJECT (object));
2520 g_return_if_fail (object->ref_count > 0);
2522 #ifdef G_ENABLE_DEBUG
2523 if (g_trap_object_ref == object)
2525 #endif /* G_ENABLE_DEBUG */
2527 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2528 retry_atomic_decrement1:
2529 old_ref = g_atomic_int_get (&object->ref_count);
2532 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2533 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2535 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2536 goto retry_atomic_decrement1;
2538 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2540 /* if we went from 2->1 we need to notify toggle refs if any */
2541 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2542 toggle_refs_notify (object, TRUE);
2546 /* we are about tp remove the last reference */
2547 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2548 G_OBJECT_GET_CLASS (object)->dispose (object);
2549 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2551 /* may have been re-referenced meanwhile */
2552 retry_atomic_decrement2:
2553 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2556 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2557 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2559 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2560 goto retry_atomic_decrement2;
2562 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2564 /* if we went from 2->1 we need to notify toggle refs if any */
2565 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2566 toggle_refs_notify (object, TRUE);
2571 /* we are still in the process of taking away the last ref */
2572 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2573 g_signal_handlers_destroy (object);
2574 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2576 /* decrement the last reference */
2577 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2579 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2581 /* may have been re-referenced meanwhile */
2582 if (G_LIKELY (old_ref == 1))
2584 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2585 G_OBJECT_GET_CLASS (object)->finalize (object);
2587 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2589 #ifdef G_ENABLE_DEBUG
2592 /* catch objects not chaining finalize handlers */
2593 G_LOCK (debug_objects);
2594 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2595 G_UNLOCK (debug_objects);
2597 #endif /* G_ENABLE_DEBUG */
2598 g_type_free_instance ((GTypeInstance*) object);
2604 * g_object_get_qdata:
2605 * @object: The GObject to get a stored user data pointer from
2606 * @quark: A #GQuark, naming the user data pointer
2608 * This function gets back user data pointers stored via
2609 * g_object_set_qdata().
2611 * Returns: The user data pointer set, or %NULL
2614 g_object_get_qdata (GObject *object,
2617 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2619 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2623 * g_object_set_qdata:
2624 * @object: The GObject to set store a user data pointer
2625 * @quark: A #GQuark, naming the user data pointer
2626 * @data: An opaque user data pointer
2628 * This sets an opaque, named pointer on an object.
2629 * The name is specified through a #GQuark (retrived e.g. via
2630 * g_quark_from_static_string()), and the pointer
2631 * can be gotten back from the @object with g_object_get_qdata()
2632 * until the @object is finalized.
2633 * Setting a previously set user data pointer, overrides (frees)
2634 * the old pointer set, using #NULL as pointer essentially
2635 * removes the data stored.
2638 g_object_set_qdata (GObject *object,
2642 g_return_if_fail (G_IS_OBJECT (object));
2643 g_return_if_fail (quark > 0);
2645 g_datalist_id_set_data (&object->qdata, quark, data);
2649 * g_object_set_qdata_full:
2650 * @object: The GObject to set store a user data pointer
2651 * @quark: A #GQuark, naming the user data pointer
2652 * @data: An opaque user data pointer
2653 * @destroy: Function to invoke with @data as argument, when @data
2656 * This function works like g_object_set_qdata(), but in addition,
2657 * a void (*destroy) (gpointer) function may be specified which is
2658 * called with @data as argument when the @object is finalized, or
2659 * the data is being overwritten by a call to g_object_set_qdata()
2660 * with the same @quark.
2663 g_object_set_qdata_full (GObject *object,
2666 GDestroyNotify destroy)
2668 g_return_if_fail (G_IS_OBJECT (object));
2669 g_return_if_fail (quark > 0);
2671 g_datalist_id_set_data_full (&object->qdata, quark, data,
2672 data ? destroy : (GDestroyNotify) NULL);
2676 * g_object_steal_qdata:
2677 * @object: The GObject to get a stored user data pointer from
2678 * @quark: A #GQuark, naming the user data pointer
2680 * This function gets back user data pointers stored via
2681 * g_object_set_qdata() and removes the @data from object
2682 * without invoking its destroy() function (if any was
2684 * Usually, calling this function is only required to update
2685 * user data pointers with a destroy notifier, for example:
2688 * object_add_to_user_list (GObject *object,
2689 * const gchar *new_string)
2691 * // the quark, naming the object data
2692 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2693 * // retrive the old string list
2694 * GList *list = g_object_steal_qdata (object, quark_string_list);
2696 * // prepend new string
2697 * list = g_list_prepend (list, g_strdup (new_string));
2698 * // this changed 'list', so we need to set it again
2699 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2702 * free_string_list (gpointer data)
2704 * GList *node, *list = data;
2706 * for (node = list; node; node = node->next)
2707 * g_free (node->data);
2708 * g_list_free (list);
2711 * Using g_object_get_qdata() in the above example, instead of
2712 * g_object_steal_qdata() would have left the destroy function set,
2713 * and thus the partial string list would have been freed upon
2714 * g_object_set_qdata_full().
2716 * Returns: The user data pointer set, or %NULL
2719 g_object_steal_qdata (GObject *object,
2722 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2723 g_return_val_if_fail (quark > 0, NULL);
2725 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2729 * g_object_get_data:
2730 * @object: #GObject containing the associations
2731 * @key: name of the key for that association
2733 * Gets a named field from the objects table of associations (see g_object_set_data()).
2735 * Returns: the data if found, or %NULL if no such data exists.
2738 g_object_get_data (GObject *object,
2743 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2744 g_return_val_if_fail (key != NULL, NULL);
2746 quark = g_quark_try_string (key);
2748 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2752 * g_object_set_data:
2753 * @object: #GObject containing the associations.
2754 * @key: name of the key
2755 * @data: data to associate with that key
2757 * Each object carries around a table of associations from
2758 * strings to pointers. This function lets you set an association.
2760 * If the object already had an association with that name,
2761 * the old association will be destroyed.
2764 g_object_set_data (GObject *object,
2768 g_return_if_fail (G_IS_OBJECT (object));
2769 g_return_if_fail (key != NULL);
2771 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2775 * g_object_set_data_full:
2776 * @object: #GObject containing the associations
2777 * @key: name of the key
2778 * @data: data to associate with that key
2779 * @destroy: function to call when the association is destroyed
2781 * Like g_object_set_data() except it adds notification
2782 * for when the association is destroyed, either by setting it
2783 * to a different value or when the object is destroyed.
2785 * Note that the @destroy callback is not called if @data is %NULL.
2788 g_object_set_data_full (GObject *object,
2791 GDestroyNotify destroy)
2793 g_return_if_fail (G_IS_OBJECT (object));
2794 g_return_if_fail (key != NULL);
2796 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2797 data ? destroy : (GDestroyNotify) NULL);
2801 * g_object_steal_data:
2802 * @object: #GObject containing the associations
2803 * @key: name of the key
2805 * Remove a specified datum from the object's data associations,
2806 * without invoking the association's destroy handler.
2808 * Returns: the data if found, or %NULL if no such data exists.
2811 g_object_steal_data (GObject *object,
2816 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2817 g_return_val_if_fail (key != NULL, NULL);
2819 quark = g_quark_try_string (key);
2821 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2825 g_value_object_init (GValue *value)
2827 value->data[0].v_pointer = NULL;
2831 g_value_object_free_value (GValue *value)
2833 if (value->data[0].v_pointer)
2834 g_object_unref (value->data[0].v_pointer);
2838 g_value_object_copy_value (const GValue *src_value,
2841 if (src_value->data[0].v_pointer)
2842 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2844 dest_value->data[0].v_pointer = NULL;
2848 g_value_object_transform_value (const GValue *src_value,
2851 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)))
2852 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2854 dest_value->data[0].v_pointer = NULL;
2858 g_value_object_peek_pointer (const GValue *value)
2860 return value->data[0].v_pointer;
2864 g_value_object_collect_value (GValue *value,
2865 guint n_collect_values,
2866 GTypeCValue *collect_values,
2867 guint collect_flags)
2869 if (collect_values[0].v_pointer)
2871 GObject *object = collect_values[0].v_pointer;
2873 if (object->g_type_instance.g_class == NULL)
2874 return g_strconcat ("invalid unclassed object pointer for value type `",
2875 G_VALUE_TYPE_NAME (value),
2878 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2879 return g_strconcat ("invalid object type `",
2880 G_OBJECT_TYPE_NAME (object),
2881 "' for value type `",
2882 G_VALUE_TYPE_NAME (value),
2885 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2886 value->data[0].v_pointer = g_object_ref (object);
2889 value->data[0].v_pointer = NULL;
2895 g_value_object_lcopy_value (const GValue *value,
2896 guint n_collect_values,
2897 GTypeCValue *collect_values,
2898 guint collect_flags)
2900 GObject **object_p = collect_values[0].v_pointer;
2903 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2905 if (!value->data[0].v_pointer)
2907 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2908 *object_p = value->data[0].v_pointer;
2910 *object_p = g_object_ref (value->data[0].v_pointer);
2916 * g_value_set_object:
2917 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2918 * @v_object: object value to be set
2920 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2922 * g_value_set_object() increases the reference count of @v_object
2923 * (the #GValue holds a reference to @v_object). If you do not wish
2924 * to increase the reference count of the object (i.e. you wish to
2925 * pass your current reference to the #GValue because you no longer
2926 * need it), use g_value_take_object() instead.
2928 * It is important that your #GValue holds a reference to @v_object (either its
2929 * own, or one it has taken) to ensure that the object won't be destroyed while
2930 * the #GValue still exists).
2933 g_value_set_object (GValue *value,
2938 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2940 old = value->data[0].v_pointer;
2944 g_return_if_fail (G_IS_OBJECT (v_object));
2945 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2947 value->data[0].v_pointer = v_object;
2948 g_object_ref (value->data[0].v_pointer);
2951 value->data[0].v_pointer = NULL;
2954 g_object_unref (old);
2958 * g_value_set_object_take_ownership:
2959 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2960 * @v_object: object value to be set
2962 * This is an internal function introduced mainly for C marshallers.
2964 * Deprecated: 2.4: Use g_value_take_object() instead.
2967 g_value_set_object_take_ownership (GValue *value,
2970 g_value_take_object (value, v_object);
2974 * g_value_take_object:
2975 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2976 * @v_object: object value to be set
2978 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2979 * and takes over the ownership of the callers reference to @v_object;
2980 * the caller doesn't have to unref it any more (i.e. the reference
2981 * count of the object is not increased).
2983 * If you want the #GValue to hold its own reference to @v_object, use
2984 * g_value_set_object() instead.
2989 g_value_take_object (GValue *value,
2992 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2994 if (value->data[0].v_pointer)
2996 g_object_unref (value->data[0].v_pointer);
2997 value->data[0].v_pointer = NULL;
3002 g_return_if_fail (G_IS_OBJECT (v_object));
3003 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3005 value->data[0].v_pointer = v_object; /* we take over the reference count */
3010 * g_value_get_object:
3011 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3013 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3015 * Returns: object contents of @value
3018 g_value_get_object (const GValue *value)
3020 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3022 return value->data[0].v_pointer;
3026 * g_value_dup_object:
3027 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3029 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3030 * its reference count.
3032 * Returns: object content of @value, should be unreferenced when no
3036 g_value_dup_object (const GValue *value)
3038 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3040 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3044 * g_signal_connect_object:
3045 * @instance: the instance to connect to.
3046 * @detailed_signal: a string of the form "signal-name::detail".
3047 * @c_handler: the #GCallback to connect.
3048 * @gobject: the object to pass as data to @c_handler.
3049 * @connect_flags: a combination of #GConnnectFlags.
3051 * This is similar to g_signal_connect_data(), but uses a closure which
3052 * ensures that the @gobject stays alive during the call to @c_handler
3053 * by temporarily adding a reference count to @gobject.
3055 * Note that there is a bug in GObject that makes this function
3056 * much less useful than it might seem otherwise. Once @gobject is
3057 * disposed, the callback will no longer be called, but, the signal
3058 * handler is <emphasis>not</emphasis> currently disconnected. If the
3059 * @instance is itself being freed at the same time than this doesn't
3060 * matter, since the signal will automatically be removed, but
3061 * if @instance persists, then the signal handler will leak. You
3062 * should not remove the signal yourself because in a future versions of
3063 * GObject, the handler <emphasis>will</emphasis> automatically
3066 * It's possible to work around this problem in a way that will
3067 * continue to work with future versions of GObject by checking
3068 * that the signal handler is still connected before disconnected it:
3069 * <informalexample><programlisting>
3070 * if (g_signal_handler_is_connected (instance, id))
3071 * g_signal_handler_disconnect (instance, id);
3072 * </programlisting></informalexample>
3074 * Returns: the handler id.
3077 g_signal_connect_object (gpointer instance,
3078 const gchar *detailed_signal,
3079 GCallback c_handler,
3081 GConnectFlags connect_flags)
3083 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3084 g_return_val_if_fail (detailed_signal != NULL, 0);
3085 g_return_val_if_fail (c_handler != NULL, 0);
3091 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3093 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3095 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3098 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3104 GClosure *closures[1]; /* flexible array */
3106 /* don't change this structure without supplying an accessor for
3107 * watched closures, e.g.:
3108 * GSList* g_object_list_watched_closures (GObject *object)
3111 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3112 * carray = g_object_get_data (object, "GObject-closure-array");
3115 * GSList *slist = NULL;
3117 * for (i = 0; i < carray->n_closures; i++)
3118 * slist = g_slist_prepend (slist, carray->closures[i]);
3126 object_remove_closure (gpointer data,
3129 GObject *object = data;
3130 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3133 for (i = 0; i < carray->n_closures; i++)
3134 if (carray->closures[i] == closure)
3136 carray->n_closures--;
3137 if (i < carray->n_closures)
3138 carray->closures[i] = carray->closures[carray->n_closures];
3141 g_assert_not_reached ();
3145 destroy_closure_array (gpointer data)
3147 CArray *carray = data;
3148 GObject *object = carray->object;
3149 guint i, n = carray->n_closures;
3151 for (i = 0; i < n; i++)
3153 GClosure *closure = carray->closures[i];
3155 /* removing object_remove_closure() upfront is probably faster than
3156 * letting it fiddle with quark_closure_array which is empty anyways
3158 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3159 g_closure_invalidate (closure);
3165 * g_object_watch_closure:
3166 * @object: GObject restricting lifetime of @closure
3167 * @closure: GClosure to watch
3169 * This function essentially limits the life time of the @closure to
3170 * the life time of the object. That is, when the object is finalized,
3171 * the @closure is invalidated by calling g_closure_invalidate() on
3172 * it, in order to prevent invocations of the closure with a finalized
3173 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3174 * added as marshal guards to the @closure, to ensure that an extra
3175 * reference count is held on @object during invocation of the
3176 * @closure. Usually, this function will be called on closures that
3177 * use this @object as closure data.
3180 g_object_watch_closure (GObject *object,
3186 g_return_if_fail (G_IS_OBJECT (object));
3187 g_return_if_fail (closure != NULL);
3188 g_return_if_fail (closure->is_invalid == FALSE);
3189 g_return_if_fail (closure->in_marshal == FALSE);
3190 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3192 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3193 g_closure_add_marshal_guards (closure,
3194 object, (GClosureNotify) g_object_ref,
3195 object, (GClosureNotify) g_object_unref);
3196 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3199 carray = g_renew (CArray, NULL, 1);
3200 carray->object = object;
3201 carray->n_closures = 1;
3206 i = carray->n_closures++;
3207 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3209 carray->closures[i] = closure;
3210 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3214 * g_closure_new_object:
3215 * @sizeof_closure: the size of the structure to allocate, must be at least
3216 * <literal>sizeof (GClosure)</literal>
3217 * @object: a #GObject pointer to store in the @data field of the newly
3218 * allocated #GClosure
3220 * A variant of g_closure_new_simple() which stores @object in the
3221 * @data field of the closure and calls g_object_watch_closure() on
3222 * @object and the created closure. This function is mainly useful
3223 * when implementing new types of closures.
3225 * Returns: a newly allocated #GClosure
3228 g_closure_new_object (guint sizeof_closure,
3233 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3234 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3236 closure = g_closure_new_simple (sizeof_closure, object);
3237 g_object_watch_closure (object, closure);
3243 * g_cclosure_new_object:
3244 * @callback_func: the function to invoke
3245 * @object: a #GObject pointer to pass to @callback_func
3247 * A variant of g_cclosure_new() which uses @object as @user_data and
3248 * calls g_object_watch_closure() on @object and the created
3249 * closure. This function is useful when you have a callback closely
3250 * associated with a #GObject, and want the callback to no longer run
3251 * after the object is is freed.
3253 * Returns: a new #GCClosure
3256 g_cclosure_new_object (GCallback callback_func,
3261 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3262 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3263 g_return_val_if_fail (callback_func != NULL, NULL);
3265 closure = g_cclosure_new (callback_func, object, NULL);
3266 g_object_watch_closure (object, closure);
3272 * g_cclosure_new_object_swap:
3273 * @callback_func: the function to invoke
3274 * @object: a #GObject pointer to pass to @callback_func
3276 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3277 * and calls g_object_watch_closure() on @object and the created
3278 * closure. This function is useful when you have a callback closely
3279 * associated with a #GObject, and want the callback to no longer run
3280 * after the object is is freed.
3282 * Returns: a new #GCClosure
3285 g_cclosure_new_object_swap (GCallback callback_func,
3290 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3291 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3292 g_return_val_if_fail (callback_func != NULL, NULL);
3294 closure = g_cclosure_new_swap (callback_func, object, NULL);
3295 g_object_watch_closure (object, closure);
3301 g_object_compat_control (gsize what,
3307 case 1: /* floating base type */
3308 return G_TYPE_INITIALLY_UNOWNED;
3309 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3310 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3312 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3314 *pp = floating_flag_handler;
3321 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3324 g_initially_unowned_init (GInitiallyUnowned *object)
3326 g_object_force_floating (object);
3330 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)