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 "gobjectalias.h"
38 /* This should be included after gobjectalias.h (or pltcheck.sh will fail) */
39 #include "gobjectnotifyqueue.c"
44 * @short_description: The base object type
45 * @see_also: #GParamSpecObject, g_param_spec_object()
46 * @title: The Base Object Type
48 * GObject is the fundamental type providing the common attributes and
49 * methods for all object types in GTK+, Pango and other libraries
50 * based on GObject. The GObject class provides methods for object
51 * construction and destruction, property access methods, and signal
52 * support. Signals are described in detail in <xref
53 * linkend="gobject-Signals"/>.
55 * <para id="floating-ref">
56 * #GInitiallyUnowned is derived from #GObject. The only difference between
57 * the two is that the initial reference of a #GInitiallyUnowned is flagged
58 * as a <firstterm>floating</firstterm> reference.
59 * This means that it is not specifically claimed to be "owned" by
60 * any code portion. The main motivation for providing floating references is
61 * C convenience. In particular, it allows code to be written as:
63 * container = create_container();
64 * container_add_child (container, create_child());
66 * If <function>container_add_child()</function> will g_object_ref_sink() the
67 * passed in child, no reference of the newly created child is leaked.
68 * Without floating references, <function>container_add_child()</function>
69 * can only g_object_ref() the new child, so to implement this code without
70 * reference leaks, it would have to be written as:
73 * container = create_container();
74 * child = create_child();
75 * container_add_child (container, child);
76 * g_object_unref (child);
78 * The floating reference can be converted into
79 * an ordinary reference by calling g_object_ref_sink().
80 * For already sunken objects (objects that don't have a floating reference
81 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
83 * Since floating references are useful almost exclusively for C convenience,
84 * language bindings that provide automated reference and memory ownership
85 * maintenance (such as smart pointers or garbage collection) therefore don't
86 * need to expose floating references in their API.
89 * Some object implementations may need to save an objects floating state
90 * across certain code portions (an example is #GtkMenu), to achive this, the
91 * following sequence can be used:
94 * // save floating state
95 * gboolean was_floating = g_object_is_floating (object);
96 * g_object_ref_sink (object);
97 * // protected code portion
99 * // restore floating state
101 * g_object_force_floating (object);
102 * g_obejct_unref (object); // release previously acquired reference
108 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
109 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
111 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
112 #define OBJECT_HAS_TOGGLE_REF(object) \
113 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
114 #define OBJECT_FLOATING_FLAG 0x2
116 #define CLASS_HAS_PROPS_FLAG 0x1
117 #define CLASS_HAS_PROPS(class) \
118 ((class)->flags & CLASS_HAS_PROPS_FLAG)
119 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
120 ((class)->constructor != g_object_constructor)
122 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
123 #define CLASS_HAS_DERIVED_CLASS(class) \
124 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
126 /* --- signals --- */
133 /* --- properties --- */
139 /* --- prototypes --- */
140 static void g_object_base_class_init (GObjectClass *class);
141 static void g_object_base_class_finalize (GObjectClass *class);
142 static void g_object_do_class_init (GObjectClass *class);
143 static void g_object_init (GObject *object,
144 GObjectClass *class);
145 static GObject* g_object_constructor (GType type,
146 guint n_construct_properties,
147 GObjectConstructParam *construct_params);
148 static void g_object_real_dispose (GObject *object);
149 static void g_object_finalize (GObject *object);
150 static void g_object_do_set_property (GObject *object,
154 static void g_object_do_get_property (GObject *object,
158 static void g_value_object_init (GValue *value);
159 static void g_value_object_free_value (GValue *value);
160 static void g_value_object_copy_value (const GValue *src_value,
162 static void g_value_object_transform_value (const GValue *src_value,
164 static gpointer g_value_object_peek_pointer (const GValue *value);
165 static gchar* g_value_object_collect_value (GValue *value,
166 guint n_collect_values,
167 GTypeCValue *collect_values,
168 guint collect_flags);
169 static gchar* g_value_object_lcopy_value (const GValue *value,
170 guint n_collect_values,
171 GTypeCValue *collect_values,
172 guint collect_flags);
173 static void g_object_dispatch_properties_changed (GObject *object,
175 GParamSpec **pspecs);
176 static inline void object_get_property (GObject *object,
179 static inline void object_set_property (GObject *object,
182 GObjectNotifyQueue *nqueue);
183 static guint object_floating_flag_handler (GObject *object,
186 static void object_interface_check_properties (gpointer func_data,
190 /* --- variables --- */
191 static GQuark quark_closure_array = 0;
192 static GQuark quark_weak_refs = 0;
193 static GQuark quark_toggle_refs = 0;
194 static GParamSpecPool *pspec_pool = NULL;
195 static GObjectNotifyContext property_notify_context = { 0, };
196 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
197 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
198 G_LOCK_DEFINE_STATIC (construction_mutex);
199 static GSList *construction_objects = NULL;
201 /* --- functions --- */
202 #ifdef G_ENABLE_DEBUG
203 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
204 G_LOCK_DEFINE_STATIC (debug_objects);
205 static volatile GObject *g_trap_object_ref = NULL;
206 static guint debug_objects_count = 0;
207 static GHashTable *debug_objects_ht = NULL;
210 debug_objects_foreach (gpointer key,
214 GObject *object = value;
216 g_message ("[%p] stale %s\tref_count=%u",
218 G_OBJECT_TYPE_NAME (object),
223 debug_objects_atexit (void)
227 G_LOCK (debug_objects);
228 g_message ("stale GObjects: %u", debug_objects_count);
229 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
230 G_UNLOCK (debug_objects);
233 #endif /* G_ENABLE_DEBUG */
236 g_object_type_init (void)
238 static gboolean initialized = FALSE;
239 static const GTypeFundamentalInfo finfo = {
240 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
242 static GTypeInfo info = {
243 sizeof (GObjectClass),
244 (GBaseInitFunc) g_object_base_class_init,
245 (GBaseFinalizeFunc) g_object_base_class_finalize,
246 (GClassInitFunc) g_object_do_class_init,
247 NULL /* class_destroy */,
248 NULL /* class_data */,
251 (GInstanceInitFunc) g_object_init,
252 NULL, /* value_table */
254 static const GTypeValueTable value_table = {
255 g_value_object_init, /* value_init */
256 g_value_object_free_value, /* value_free */
257 g_value_object_copy_value, /* value_copy */
258 g_value_object_peek_pointer, /* value_peek_pointer */
259 "p", /* collect_format */
260 g_value_object_collect_value, /* collect_value */
261 "p", /* lcopy_format */
262 g_value_object_lcopy_value, /* lcopy_value */
266 g_return_if_fail (initialized == FALSE);
271 info.value_table = &value_table;
272 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
273 g_assert (type == G_TYPE_OBJECT);
274 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
276 #ifdef G_ENABLE_DEBUG
279 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
280 g_atexit (debug_objects_atexit);
282 #endif /* G_ENABLE_DEBUG */
286 g_object_base_class_init (GObjectClass *class)
288 GObjectClass *pclass = g_type_class_peek_parent (class);
290 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
291 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
294 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
296 /* reset instance specific fields and methods that don't get inherited */
297 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
298 class->get_property = NULL;
299 class->set_property = NULL;
303 g_object_base_class_finalize (GObjectClass *class)
307 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
309 g_slist_free (class->construct_properties);
310 class->construct_properties = NULL;
311 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
312 for (node = list; node; node = node->next)
314 GParamSpec *pspec = node->data;
316 g_param_spec_pool_remove (pspec_pool, pspec);
317 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
318 g_param_spec_unref (pspec);
324 g_object_notify_dispatcher (GObject *object,
328 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
332 g_object_do_class_init (GObjectClass *class)
334 /* read the comment about typedef struct CArray; on why not to change this quark */
335 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
337 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
338 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
339 pspec_pool = g_param_spec_pool_new (TRUE);
340 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
341 property_notify_context.dispatcher = g_object_notify_dispatcher;
343 class->constructor = g_object_constructor;
344 class->set_property = g_object_do_set_property;
345 class->get_property = g_object_do_get_property;
346 class->dispose = g_object_real_dispose;
347 class->finalize = g_object_finalize;
348 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
349 class->notify = NULL;
353 * @gobject: the object which received the signal.
354 * @pspec: the #GParamSpec of the property which changed.
356 * The notify signal is emitted on an object when one of its
357 * properties has been changed. Note that getting this signal
358 * doesn't guarantee that the value of the property has actually
359 * changed, it may also be emitted when the setter for the property
360 * is called to reinstate the previous value.
362 * This signal is typically used to obtain change notification for a
363 * single property, by specifying the property name as a detail in the
364 * g_signal_connect() call, like this:
366 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
367 * G_CALLBACK (gtk_text_view_target_list_notify),
370 * It is important to note that you must use
371 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
372 * detail strings for the notify signal.
374 gobject_signals[NOTIFY] =
375 g_signal_new (g_intern_static_string ("notify"),
376 G_TYPE_FROM_CLASS (class),
377 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
378 G_STRUCT_OFFSET (GObjectClass, notify),
380 g_cclosure_marshal_VOID__PARAM,
384 /* Install a check function that we'll use to verify that classes that
385 * implement an interface implement all properties for that interface
387 g_type_add_interface_check (NULL, object_interface_check_properties);
391 install_property_internal (GType g_type,
395 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
397 g_warning ("When installing property: type `%s' already has a property named `%s'",
398 g_type_name (g_type),
403 g_param_spec_ref (pspec);
404 g_param_spec_sink (pspec);
405 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
406 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
410 * g_object_class_install_property:
411 * @oclass: a #GObjectClass
412 * @property_id: the id for the new property
413 * @pspec: the #GParamSpec for the new property
415 * Installs a new property. This is usually done in the class initializer.
417 * Note that it is possible to redefine a property in a derived class,
418 * by installing a property with the same name. This can be useful at times,
419 * e.g. to change the range of allowed values or the default value.
422 g_object_class_install_property (GObjectClass *class,
426 g_return_if_fail (G_IS_OBJECT_CLASS (class));
427 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
429 if (CLASS_HAS_DERIVED_CLASS (class))
430 g_error ("Attempt to add property %s::%s to class after it was derived",
431 G_OBJECT_CLASS_NAME (class), pspec->name);
433 class->flags |= CLASS_HAS_PROPS_FLAG;
435 if (pspec->flags & G_PARAM_WRITABLE)
436 g_return_if_fail (class->set_property != NULL);
437 if (pspec->flags & G_PARAM_READABLE)
438 g_return_if_fail (class->get_property != NULL);
439 g_return_if_fail (property_id > 0);
440 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
441 if (pspec->flags & G_PARAM_CONSTRUCT)
442 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
443 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
444 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
446 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
448 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
449 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
451 /* for property overrides of construct poperties, we have to get rid
452 * of the overidden inherited construct property
454 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
455 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
456 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
460 * g_object_interface_install_property:
461 * @g_iface: any interface vtable for the interface, or the default
462 * vtable for the interface.
463 * @pspec: the #GParamSpec for the new property
465 * Add a property to an interface; this is only useful for interfaces
466 * that are added to GObject-derived types. Adding a property to an
467 * interface forces all objects classes with that interface to have a
468 * compatible property. The compatible property could be a newly
469 * created #GParamSpec, but normally
470 * g_object_class_override_property() will be used so that the object
471 * class only needs to provide an implementation and inherits the
472 * property description, default value, bounds, and so forth from the
473 * interface property.
475 * This function is meant to be called from the interface's default
476 * vtable initialization function (the @class_init member of
477 * #GTypeInfo.) It must not be called after after @class_init has
478 * been called for any object types implementing this interface.
483 g_object_interface_install_property (gpointer g_iface,
486 GTypeInterface *iface_class = g_iface;
488 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
489 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
490 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
491 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
493 install_property_internal (iface_class->g_type, 0, pspec);
497 * g_object_class_find_property:
498 * @oclass: a #GObjectClass
499 * @property_name: the name of the property to look up
501 * Looks up the #GParamSpec for a property of a class.
503 * Returns: the #GParamSpec for the property, or %NULL if the class
504 * doesn't have a property of that name
507 g_object_class_find_property (GObjectClass *class,
508 const gchar *property_name)
511 GParamSpec *redirect;
513 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
514 g_return_val_if_fail (property_name != NULL, NULL);
516 pspec = g_param_spec_pool_lookup (pspec_pool,
518 G_OBJECT_CLASS_TYPE (class),
522 redirect = g_param_spec_get_redirect_target (pspec);
533 * g_object_interface_find_property:
534 * @g_iface: any interface vtable for the interface, or the default
535 * vtable for the interface
536 * @property_name: name of a property to lookup.
538 * Find the #GParamSpec with the given name for an
539 * interface. Generally, the interface vtable passed in as @g_iface
540 * will be the default vtable from g_type_default_interface_ref(), or,
541 * if you know the interface has already been loaded,
542 * g_type_default_interface_peek().
546 * Returns: the #GParamSpec for the property of the interface with the
547 * name @property_name, or %NULL if no such property exists.
550 g_object_interface_find_property (gpointer g_iface,
551 const gchar *property_name)
553 GTypeInterface *iface_class = g_iface;
555 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
556 g_return_val_if_fail (property_name != NULL, NULL);
558 return g_param_spec_pool_lookup (pspec_pool,
565 * g_object_class_override_property:
566 * @oclass: a #GObjectClass
567 * @property_id: the new property ID
568 * @name: the name of a property registered in a parent class or
569 * in an interface of this class.
571 * Registers @property_id as referring to a property with the
572 * name @name in a parent class or in an interface implemented
573 * by @oclass. This allows this class to <firstterm>override</firstterm>
574 * a property implementation in a parent class or to provide
575 * the implementation of a property from an interface.
578 * Internally, overriding is implemented by creating a property of type
579 * #GParamSpecOverride; generally operations that query the properties of
580 * the object class, such as g_object_class_find_property() or
581 * g_object_class_list_properties() will return the overridden
582 * property. However, in one case, the @construct_properties argument of
583 * the @constructor virtual function, the #GParamSpecOverride is passed
584 * instead, so that the @param_id field of the #GParamSpec will be
585 * correct. For virtually all uses, this makes no difference. If you
586 * need to get the overridden property, you can call
587 * g_param_spec_get_redirect_target().
593 g_object_class_override_property (GObjectClass *oclass,
597 GParamSpec *overridden = NULL;
601 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
602 g_return_if_fail (property_id > 0);
603 g_return_if_fail (name != NULL);
605 /* Find the overridden property; first check parent types
607 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
608 if (parent_type != G_TYPE_NONE)
609 overridden = g_param_spec_pool_lookup (pspec_pool,
618 /* Now check interfaces
620 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
621 while (n_ifaces-- && !overridden)
623 overridden = g_param_spec_pool_lookup (pspec_pool,
634 g_warning ("%s: Can't find property to override for '%s::%s'",
635 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
639 new = g_param_spec_override (name, overridden);
640 g_object_class_install_property (oclass, property_id, new);
644 * g_object_class_list_properties:
645 * @oclass: a #GObjectClass
646 * @n_properties: return location for the length of the returned array
648 * Get an array of #GParamSpec* for all properties of a class.
650 * Returns: an array of #GParamSpec* which should be freed after use
652 GParamSpec** /* free result */
653 g_object_class_list_properties (GObjectClass *class,
654 guint *n_properties_p)
659 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
661 pspecs = g_param_spec_pool_list (pspec_pool,
662 G_OBJECT_CLASS_TYPE (class),
671 * g_object_interface_list_properties:
672 * @g_iface: any interface vtable for the interface, or the default
673 * vtable for the interface
674 * @n_properties_p: location to store number of properties returned.
676 * Lists the properties of an interface.Generally, the interface
677 * vtable passed in as @g_iface will be the default vtable from
678 * g_type_default_interface_ref(), or, if you know the interface has
679 * already been loaded, g_type_default_interface_peek().
683 * Returns: a pointer to an array of pointers to #GParamSpec
684 * structures. The paramspecs are owned by GLib, but the
685 * array should be freed with g_free() when you are done with
689 g_object_interface_list_properties (gpointer g_iface,
690 guint *n_properties_p)
692 GTypeInterface *iface_class = g_iface;
696 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
698 pspecs = g_param_spec_pool_list (pspec_pool,
708 g_object_init (GObject *object,
711 object->ref_count = 1;
712 g_datalist_init (&object->qdata);
714 if (CLASS_HAS_PROPS (class))
716 /* freeze object's notification queue, g_object_newv() preserves pairedness */
717 g_object_notify_queue_freeze (object, &property_notify_context);
720 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
722 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
723 G_LOCK (construction_mutex);
724 construction_objects = g_slist_prepend (construction_objects, object);
725 G_UNLOCK (construction_mutex);
728 #ifdef G_ENABLE_DEBUG
731 G_LOCK (debug_objects);
732 debug_objects_count++;
733 g_hash_table_insert (debug_objects_ht, object, object);
734 G_UNLOCK (debug_objects);
736 #endif /* G_ENABLE_DEBUG */
740 g_object_do_set_property (GObject *object,
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
754 g_object_do_get_property (GObject *object,
762 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
768 g_object_real_dispose (GObject *object)
770 g_signal_handlers_destroy (object);
771 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
772 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
776 g_object_finalize (GObject *object)
778 g_datalist_clear (&object->qdata);
780 #ifdef G_ENABLE_DEBUG
783 G_LOCK (debug_objects);
784 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
785 g_hash_table_remove (debug_objects_ht, object);
786 debug_objects_count--;
787 G_UNLOCK (debug_objects);
789 #endif /* G_ENABLE_DEBUG */
794 g_object_dispatch_properties_changed (GObject *object,
800 for (i = 0; i < n_pspecs; i++)
801 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
805 * g_object_run_dispose:
806 * @object: a #GObject
808 * Releases all references to other objects. This can be used to break
811 * This functions should only be called from object system implementations.
814 g_object_run_dispose (GObject *object)
816 g_return_if_fail (G_IS_OBJECT (object));
817 g_return_if_fail (object->ref_count > 0);
819 g_object_ref (object);
820 G_OBJECT_GET_CLASS (object)->dispose (object);
821 g_object_unref (object);
825 * g_object_freeze_notify:
826 * @object: a #GObject
828 * Increases the freeze count on @object. If the freeze count is
829 * non-zero, the emission of "notify" signals on @object is
830 * stopped. The signals are queued until the freeze count is decreased
833 * This is necessary for accessors that modify multiple properties to prevent
834 * premature notification while the object is still being modified.
837 g_object_freeze_notify (GObject *object)
839 g_return_if_fail (G_IS_OBJECT (object));
841 if (g_atomic_int_get (&object->ref_count) == 0)
844 g_object_ref (object);
845 g_object_notify_queue_freeze (object, &property_notify_context);
846 g_object_unref (object);
851 * @object: a #GObject
852 * @property_name: the name of a property installed on the class of @object.
854 * Emits a "notify" signal for the property @property_name on @object.
857 g_object_notify (GObject *object,
858 const gchar *property_name)
862 g_return_if_fail (G_IS_OBJECT (object));
863 g_return_if_fail (property_name != NULL);
864 if (g_atomic_int_get (&object->ref_count) == 0)
867 g_object_ref (object);
868 /* We don't need to get the redirect target
869 * (by, e.g. calling g_object_class_find_property())
870 * because g_object_notify_queue_add() does that
872 pspec = g_param_spec_pool_lookup (pspec_pool,
874 G_OBJECT_TYPE (object),
878 g_warning ("%s: object class `%s' has no property named `%s'",
880 G_OBJECT_TYPE_NAME (object),
884 GObjectNotifyQueue *nqueue;
886 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
887 g_object_notify_queue_add (object, nqueue, pspec);
888 g_object_notify_queue_thaw (object, nqueue);
890 g_object_unref (object);
894 * g_object_thaw_notify:
895 * @object: a #GObject
897 * Reverts the effect of a previous call to
898 * g_object_freeze_notify(). The freeze count is decreased on @object
899 * and when it reaches zero, all queued "notify" signals are emitted.
901 * It is an error to call this function when the freeze count is zero.
904 g_object_thaw_notify (GObject *object)
906 GObjectNotifyQueue *nqueue;
908 g_return_if_fail (G_IS_OBJECT (object));
909 if (g_atomic_int_get (&object->ref_count) == 0)
912 g_object_ref (object);
913 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
914 if (!nqueue || !nqueue->freeze_count)
915 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
916 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
918 g_object_notify_queue_thaw (object, nqueue);
919 g_object_unref (object);
923 object_get_property (GObject *object,
927 GObjectClass *class = g_type_class_peek (pspec->owner_type);
928 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
929 GParamSpec *redirect;
931 redirect = g_param_spec_get_redirect_target (pspec);
935 class->get_property (object, param_id, value, pspec);
939 object_set_property (GObject *object,
942 GObjectNotifyQueue *nqueue)
944 GValue tmp_value = { 0, };
945 GObjectClass *class = g_type_class_peek (pspec->owner_type);
946 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
947 GParamSpec *redirect;
949 redirect = g_param_spec_get_redirect_target (pspec);
953 /* provide a copy to work from, convert (if necessary) and validate */
954 g_value_init (&tmp_value, pspec->value_type);
955 if (!g_value_transform (value, &tmp_value))
956 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
958 g_type_name (pspec->value_type),
959 G_VALUE_TYPE_NAME (value));
960 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
962 gchar *contents = g_strdup_value_contents (value);
964 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
966 G_VALUE_TYPE_NAME (value),
968 g_type_name (pspec->value_type));
973 class->set_property (object, param_id, &tmp_value, pspec);
974 g_object_notify_queue_add (object, nqueue, pspec);
976 g_value_unset (&tmp_value);
980 object_interface_check_properties (gpointer func_data,
983 GTypeInterface *iface_class = g_iface;
984 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
985 GType iface_type = iface_class->g_type;
989 if (!G_IS_OBJECT_CLASS (class))
992 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
996 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
998 G_OBJECT_CLASS_TYPE (class),
1003 g_critical ("Object class %s doesn't implement property "
1004 "'%s' from interface '%s'",
1005 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1007 g_type_name (iface_type));
1012 /* The implementation paramspec must have a less restrictive
1013 * type than the interface parameter spec for set() and a
1014 * more restrictive type for get(). We just require equality,
1015 * rather than doing something more complicated checking
1016 * the READABLE and WRITABLE flags. We also simplify here
1017 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1020 !g_type_is_a (pspecs[n]->value_type,
1021 class_pspec->value_type))
1023 g_critical ("Property '%s' on class '%s' has type '%s' "
1024 "which is different from the type '%s', "
1025 "of the property on interface '%s'\n",
1027 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1028 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1029 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1030 g_type_name (iface_type));
1033 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1035 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1036 * READABLE and WRITABLE remove restrictions. The implementation
1037 * paramspec must have less restrictive flags.
1040 (!SUBSET (class_pspec->flags,
1042 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1043 !SUBSET (pspecs[n]->flags,
1045 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1047 g_critical ("Flags for property '%s' on class '%s' "
1048 "are not compatible with the property on"
1051 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1052 g_type_name (iface_type));
1061 g_object_get_type (void)
1063 return G_TYPE_OBJECT;
1068 * @object_type: the type id of the #GObject subtype to instantiate
1069 * @first_property_name: the name of the first property
1070 * @...: the value of the first property, followed optionally by more
1071 * name/value pairs, followed by %NULL
1073 * Creates a new instance of a #GObject subtype and sets its properties.
1075 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1076 * which are not explicitly specified are set to their default values.
1078 * Returns: a new instance of @object_type
1081 g_object_new (GType object_type,
1082 const gchar *first_property_name,
1088 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1090 /* short circuit for calls supplying no properties */
1091 if (!first_property_name)
1092 return g_object_newv (object_type, 0, NULL);
1094 va_start (var_args, first_property_name);
1095 object = g_object_new_valist (object_type, first_property_name, var_args);
1102 slist_maybe_remove (GSList **slist,
1105 GSList *last = NULL, *node = *slist;
1108 if (node->data == data)
1111 last->next = node->next;
1113 *slist = node->next;
1114 g_slist_free_1 (node);
1123 static inline gboolean
1124 object_in_construction_list (GObject *object)
1126 gboolean in_construction;
1127 G_LOCK (construction_mutex);
1128 in_construction = g_slist_find (construction_objects, object) != NULL;
1129 G_UNLOCK (construction_mutex);
1130 return in_construction;
1135 * @object_type: the type id of the #GObject subtype to instantiate
1136 * @n_parameters: the length of the @parameters array
1137 * @parameters: an array of #GParameter
1139 * Creates a new instance of a #GObject subtype and sets its properties.
1141 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1142 * which are not explicitly specified are set to their default values.
1144 * Returns: a new instance of @object_type
1147 g_object_newv (GType object_type,
1149 GParameter *parameters)
1151 GObjectConstructParam *cparams = NULL, *oparams;
1152 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1154 GObjectClass *class, *unref_class = NULL;
1156 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1158 GList *clist = NULL;
1159 gboolean newly_constructed;
1162 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1164 class = g_type_class_peek_static (object_type);
1166 class = unref_class = g_type_class_ref (object_type);
1167 for (slist = class->construct_properties; slist; slist = slist->next)
1169 clist = g_list_prepend (clist, slist->data);
1170 n_total_cparams += 1;
1173 if (n_parameters == 0 && n_total_cparams == 0)
1175 /* This is a simple object with no construct properties, and
1176 * no properties are being set, so short circuit the parameter
1177 * handling. This speeds up simple object construction.
1180 object = class->constructor (object_type, 0, NULL);
1181 goto did_construction;
1184 /* collect parameters, sort into construction and normal ones */
1185 oparams = g_new (GObjectConstructParam, n_parameters);
1186 cparams = g_new (GObjectConstructParam, n_total_cparams);
1187 for (i = 0; i < n_parameters; i++)
1189 GValue *value = ¶meters[i].value;
1190 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1196 g_warning ("%s: object class `%s' has no property named `%s'",
1198 g_type_name (object_type),
1199 parameters[i].name);
1202 if (!(pspec->flags & G_PARAM_WRITABLE))
1204 g_warning ("%s: property `%s' of object class `%s' is not writable",
1207 g_type_name (object_type));
1210 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1212 GList *list = g_list_find (clist, pspec);
1216 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1217 G_STRFUNC, pspec->name, g_type_name (object_type));
1220 cparams[n_cparams].pspec = pspec;
1221 cparams[n_cparams].value = value;
1226 list->prev->next = list->next;
1228 list->next->prev = list->prev;
1229 g_list_free_1 (list);
1233 oparams[n_oparams].pspec = pspec;
1234 oparams[n_oparams].value = value;
1239 /* set remaining construction properties to default values */
1240 n_cvalues = n_total_cparams - n_cparams;
1241 cvalues = g_new (GValue, n_cvalues);
1244 GList *tmp = clist->next;
1245 GParamSpec *pspec = clist->data;
1246 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1249 g_value_init (value, pspec->value_type);
1250 g_param_value_set_default (pspec, value);
1252 cparams[n_cparams].pspec = pspec;
1253 cparams[n_cparams].value = value;
1256 g_list_free_1 (clist);
1260 /* construct object from construction parameters */
1261 object = class->constructor (object_type, n_total_cparams, cparams);
1262 /* free construction values */
1265 g_value_unset (cvalues + n_cvalues);
1269 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1271 /* adjust freeze_count according to g_object_init() and remaining properties */
1272 G_LOCK (construction_mutex);
1273 newly_constructed = slist_maybe_remove (&construction_objects, object);
1274 G_UNLOCK (construction_mutex);
1277 newly_constructed = TRUE;
1279 if (CLASS_HAS_PROPS (class))
1281 if (newly_constructed || n_oparams)
1282 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1283 if (newly_constructed)
1284 g_object_notify_queue_thaw (object, nqueue);
1287 /* run 'constructed' handler if there is one */
1288 if (newly_constructed && class->constructed)
1289 class->constructed (object);
1291 /* set remaining properties */
1292 for (i = 0; i < n_oparams; i++)
1293 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1296 if (CLASS_HAS_PROPS (class))
1298 /* release our own freeze count and handle notifications */
1299 if (newly_constructed || n_oparams)
1300 g_object_notify_queue_thaw (object, nqueue);
1304 g_type_class_unref (unref_class);
1310 * g_object_new_valist:
1311 * @object_type: the type id of the #GObject subtype to instantiate
1312 * @first_property_name: the name of the first property
1313 * @var_args: the value of the first property, followed optionally by more
1314 * name/value pairs, followed by %NULL
1316 * Creates a new instance of a #GObject subtype and sets its properties.
1318 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1319 * which are not explicitly specified are set to their default values.
1321 * Returns: a new instance of @object_type
1324 g_object_new_valist (GType object_type,
1325 const gchar *first_property_name,
1328 GObjectClass *class;
1332 guint n_params = 0, n_alloced_params = 16;
1334 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1336 if (!first_property_name)
1337 return g_object_newv (object_type, 0, NULL);
1339 class = g_type_class_ref (object_type);
1341 params = g_new0 (GParameter, n_alloced_params);
1342 name = first_property_name;
1345 gchar *error = NULL;
1346 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1352 g_warning ("%s: object class `%s' has no property named `%s'",
1354 g_type_name (object_type),
1358 if (n_params >= n_alloced_params)
1360 n_alloced_params += 16;
1361 params = g_renew (GParameter, params, n_alloced_params);
1363 params[n_params].name = name;
1364 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1365 var_args, 0, &error);
1368 g_warning ("%s: %s", G_STRFUNC, error);
1370 g_value_unset (¶ms[n_params].value);
1374 name = va_arg (var_args, gchar*);
1377 object = g_object_newv (object_type, n_params, params);
1380 g_value_unset (¶ms[n_params].value);
1383 g_type_class_unref (class);
1389 g_object_constructor (GType type,
1390 guint n_construct_properties,
1391 GObjectConstructParam *construct_params)
1396 object = (GObject*) g_type_create_instance (type);
1398 /* set construction parameters */
1399 if (n_construct_properties)
1401 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1403 /* set construct properties */
1404 while (n_construct_properties--)
1406 GValue *value = construct_params->value;
1407 GParamSpec *pspec = construct_params->pspec;
1410 object_set_property (object, pspec, value, nqueue);
1412 g_object_notify_queue_thaw (object, nqueue);
1413 /* the notification queue is still frozen from g_object_init(), so
1414 * we don't need to handle it here, g_object_newv() takes
1423 * g_object_set_valist:
1424 * @object: a #GObject
1425 * @first_property_name: name of the first property to set
1426 * @var_args: value for the first property, followed optionally by more
1427 * name/value pairs, followed by %NULL
1429 * Sets properties on an object.
1432 g_object_set_valist (GObject *object,
1433 const gchar *first_property_name,
1436 GObjectNotifyQueue *nqueue;
1439 g_return_if_fail (G_IS_OBJECT (object));
1441 g_object_ref (object);
1442 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1444 name = first_property_name;
1447 GValue value = { 0, };
1449 gchar *error = NULL;
1451 pspec = g_param_spec_pool_lookup (pspec_pool,
1453 G_OBJECT_TYPE (object),
1457 g_warning ("%s: object class `%s' has no property named `%s'",
1459 G_OBJECT_TYPE_NAME (object),
1463 if (!(pspec->flags & G_PARAM_WRITABLE))
1465 g_warning ("%s: property `%s' of object class `%s' is not writable",
1468 G_OBJECT_TYPE_NAME (object));
1471 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1473 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1474 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1478 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1482 g_warning ("%s: %s", G_STRFUNC, error);
1484 g_value_unset (&value);
1488 object_set_property (object, pspec, &value, nqueue);
1489 g_value_unset (&value);
1491 name = va_arg (var_args, gchar*);
1494 g_object_notify_queue_thaw (object, nqueue);
1495 g_object_unref (object);
1499 * g_object_get_valist:
1500 * @object: a #GObject
1501 * @first_property_name: name of the first property to get
1502 * @var_args: return location for the first property, followed optionally by more
1503 * name/return location pairs, followed by %NULL
1505 * Gets properties of an object.
1507 * In general, a copy is made of the property contents and the caller
1508 * is responsible for freeing the memory in the appropriate manner for
1509 * the type, for instance by calling g_free() or g_object_unref().
1511 * See g_object_get().
1514 g_object_get_valist (GObject *object,
1515 const gchar *first_property_name,
1520 g_return_if_fail (G_IS_OBJECT (object));
1522 g_object_ref (object);
1524 name = first_property_name;
1528 GValue value = { 0, };
1532 pspec = g_param_spec_pool_lookup (pspec_pool,
1534 G_OBJECT_TYPE (object),
1538 g_warning ("%s: object class `%s' has no property named `%s'",
1540 G_OBJECT_TYPE_NAME (object),
1544 if (!(pspec->flags & G_PARAM_READABLE))
1546 g_warning ("%s: property `%s' of object class `%s' is not readable",
1549 G_OBJECT_TYPE_NAME (object));
1553 g_value_init (&value, pspec->value_type);
1555 object_get_property (object, pspec, &value);
1557 G_VALUE_LCOPY (&value, var_args, 0, &error);
1560 g_warning ("%s: %s", G_STRFUNC, error);
1562 g_value_unset (&value);
1566 g_value_unset (&value);
1568 name = va_arg (var_args, gchar*);
1571 g_object_unref (object);
1576 * @object: a #GObject
1577 * @first_property_name: name of the first property to set
1578 * @...: value for the first property, followed optionally by more
1579 * name/value pairs, followed by %NULL
1581 * Sets properties on an object.
1584 g_object_set (gpointer _object,
1585 const gchar *first_property_name,
1588 GObject *object = _object;
1591 g_return_if_fail (G_IS_OBJECT (object));
1593 va_start (var_args, first_property_name);
1594 g_object_set_valist (object, first_property_name, var_args);
1600 * @object: a #GObject
1601 * @first_property_name: name of the first property to get
1602 * @...: return location for the first property, followed optionally by more
1603 * name/return location pairs, followed by %NULL
1605 * Gets properties of an object.
1607 * In general, a copy is made of the property contents and the caller
1608 * is responsible for freeing the memory in the appropriate manner for
1609 * the type, for instance by calling g_free() or g_object_unref().
1612 * <title>Using g_object_get(<!-- -->)</title>
1613 * An example of using g_object_get() to get the contents
1614 * of three properties - one of type #G_TYPE_INT,
1615 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1621 * g_object_get (my_object,
1622 * "int-property", &intval,
1623 * "str-property", &strval,
1624 * "obj-property", &objval,
1627 * // Do something with intval, strval, objval
1630 * g_object_unref (objval);
1635 g_object_get (gpointer _object,
1636 const gchar *first_property_name,
1639 GObject *object = _object;
1642 g_return_if_fail (G_IS_OBJECT (object));
1644 va_start (var_args, first_property_name);
1645 g_object_get_valist (object, first_property_name, var_args);
1650 * g_object_set_property:
1651 * @object: a #GObject
1652 * @property_name: the name of the property to set
1655 * Sets a property on an object.
1658 g_object_set_property (GObject *object,
1659 const gchar *property_name,
1660 const GValue *value)
1662 GObjectNotifyQueue *nqueue;
1665 g_return_if_fail (G_IS_OBJECT (object));
1666 g_return_if_fail (property_name != NULL);
1667 g_return_if_fail (G_IS_VALUE (value));
1669 g_object_ref (object);
1670 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1672 pspec = g_param_spec_pool_lookup (pspec_pool,
1674 G_OBJECT_TYPE (object),
1677 g_warning ("%s: object class `%s' has no property named `%s'",
1679 G_OBJECT_TYPE_NAME (object),
1681 else if (!(pspec->flags & G_PARAM_WRITABLE))
1682 g_warning ("%s: property `%s' of object class `%s' is not writable",
1685 G_OBJECT_TYPE_NAME (object));
1686 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1687 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1688 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1690 object_set_property (object, pspec, value, nqueue);
1692 g_object_notify_queue_thaw (object, nqueue);
1693 g_object_unref (object);
1697 * g_object_get_property:
1698 * @object: a #GObject
1699 * @property_name: the name of the property to get
1700 * @value: return location for the property value
1702 * Gets a property of an object.
1704 * In general, a copy is made of the property contents and the caller is
1705 * responsible for freeing the memory by calling g_value_unset().
1707 * Note that g_object_get_property() is really intended for language
1708 * bindings, g_object_get() is much more convenient for C programming.
1711 g_object_get_property (GObject *object,
1712 const gchar *property_name,
1717 g_return_if_fail (G_IS_OBJECT (object));
1718 g_return_if_fail (property_name != NULL);
1719 g_return_if_fail (G_IS_VALUE (value));
1721 g_object_ref (object);
1723 pspec = g_param_spec_pool_lookup (pspec_pool,
1725 G_OBJECT_TYPE (object),
1728 g_warning ("%s: object class `%s' has no property named `%s'",
1730 G_OBJECT_TYPE_NAME (object),
1732 else if (!(pspec->flags & G_PARAM_READABLE))
1733 g_warning ("%s: property `%s' of object class `%s' is not readable",
1736 G_OBJECT_TYPE_NAME (object));
1739 GValue *prop_value, tmp_value = { 0, };
1741 /* auto-conversion of the callers value type
1743 if (G_VALUE_TYPE (value) == pspec->value_type)
1745 g_value_reset (value);
1748 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1750 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1751 G_STRFUNC, pspec->name,
1752 g_type_name (pspec->value_type),
1753 G_VALUE_TYPE_NAME (value));
1754 g_object_unref (object);
1759 g_value_init (&tmp_value, pspec->value_type);
1760 prop_value = &tmp_value;
1762 object_get_property (object, pspec, prop_value);
1763 if (prop_value != value)
1765 g_value_transform (prop_value, value);
1766 g_value_unset (&tmp_value);
1770 g_object_unref (object);
1775 * @object: a #GObject
1776 * @signal_spec: the spec for the first signal
1777 * @...: #GCallback for the first signal, followed by data for the
1778 * first signal, followed optionally by more signal
1779 * spec/callback/data triples, followed by %NULL
1781 * A convenience function to connect multiple signals at once.
1783 * The signal specs expected by this function have the form
1784 * "modifier::signal_name", where modifier can be one of the following:
1787 * <term>signal</term>
1789 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1790 * </para></listitem>
1793 * <term>object_signal</term>
1794 * <term>object-signal</term>
1796 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1797 * </para></listitem>
1800 * <term>swapped_signal</term>
1801 * <term>swapped-signal</term>
1803 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1804 * </para></listitem>
1807 * <term>swapped_object_signal</term>
1808 * <term>swapped-object-signal</term>
1810 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1811 * </para></listitem>
1814 * <term>signal_after</term>
1815 * <term>signal-after</term>
1817 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1818 * </para></listitem>
1821 * <term>object_signal_after</term>
1822 * <term>object-signal-after</term>
1824 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1825 * </para></listitem>
1828 * <term>swapped_signal_after</term>
1829 * <term>swapped-signal-after</term>
1831 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1832 * </para></listitem>
1835 * <term>swapped_object_signal_after</term>
1836 * <term>swapped-object-signal-after</term>
1838 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1839 * </para></listitem>
1844 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1845 * "type", GTK_WINDOW_POPUP,
1848 * "signal::event", gtk_menu_window_event, menu,
1849 * "signal::size_request", gtk_menu_window_size_request, menu,
1850 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1857 g_object_connect (gpointer _object,
1858 const gchar *signal_spec,
1861 GObject *object = _object;
1864 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1865 g_return_val_if_fail (object->ref_count > 0, object);
1867 va_start (var_args, signal_spec);
1870 GCallback callback = va_arg (var_args, GCallback);
1871 gpointer data = va_arg (var_args, gpointer);
1874 if (strncmp (signal_spec, "signal::", 8) == 0)
1875 sid = g_signal_connect_data (object, signal_spec + 8,
1876 callback, data, NULL,
1878 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1879 strncmp (signal_spec, "object-signal::", 15) == 0)
1880 sid = g_signal_connect_object (object, signal_spec + 15,
1883 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1884 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1885 sid = g_signal_connect_data (object, signal_spec + 16,
1886 callback, data, NULL,
1888 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1889 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1890 sid = g_signal_connect_object (object, signal_spec + 23,
1893 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1894 strncmp (signal_spec, "signal-after::", 14) == 0)
1895 sid = g_signal_connect_data (object, signal_spec + 14,
1896 callback, data, NULL,
1898 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1899 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1900 sid = g_signal_connect_object (object, signal_spec + 21,
1903 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1904 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1905 sid = g_signal_connect_data (object, signal_spec + 22,
1906 callback, data, NULL,
1907 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1908 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1909 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1910 sid = g_signal_connect_object (object, signal_spec + 29,
1912 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1915 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1918 signal_spec = va_arg (var_args, gchar*);
1926 * g_object_disconnect:
1927 * @object: a #GObject
1928 * @signal_spec: the spec for the first signal
1929 * @...: #GCallback for the first signal, followed by data for the first signal,
1930 * followed optionally by more signal spec/callback/data triples,
1933 * A convenience function to disconnect multiple signals at once.
1935 * The signal specs expected by this function have the form
1936 * "any_signal", which means to disconnect any signal with matching
1937 * callback and data, or "any_signal::signal_name", which only
1938 * disconnects the signal named "signal_name".
1941 g_object_disconnect (gpointer _object,
1942 const gchar *signal_spec,
1945 GObject *object = _object;
1948 g_return_if_fail (G_IS_OBJECT (object));
1949 g_return_if_fail (object->ref_count > 0);
1951 va_start (var_args, signal_spec);
1954 GCallback callback = va_arg (var_args, GCallback);
1955 gpointer data = va_arg (var_args, gpointer);
1956 guint sid = 0, detail = 0, mask = 0;
1958 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1959 strncmp (signal_spec, "any-signal::", 12) == 0)
1962 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1964 else if (strcmp (signal_spec, "any_signal") == 0 ||
1965 strcmp (signal_spec, "any-signal") == 0)
1968 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1972 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1976 if ((mask & G_SIGNAL_MATCH_ID) &&
1977 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1978 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1979 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1981 NULL, (gpointer)callback, data))
1982 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1983 signal_spec = va_arg (var_args, gchar*);
1994 } weak_refs[1]; /* flexible array */
1998 weak_refs_notify (gpointer data)
2000 WeakRefStack *wstack = data;
2003 for (i = 0; i < wstack->n_weak_refs; i++)
2004 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2009 * g_object_weak_ref:
2010 * @object: #GObject to reference weakly
2011 * @notify: callback to invoke before the object is freed
2012 * @data: extra data to pass to notify
2014 * Adds a weak reference callback to an object. Weak references are
2015 * used for notification when an object is finalized. They are called
2016 * "weak references" because they allow you to safely hold a pointer
2017 * to an object without calling g_object_ref() (g_object_ref() adds a
2018 * strong reference, that is, forces the object to stay alive).
2021 g_object_weak_ref (GObject *object,
2025 WeakRefStack *wstack;
2028 g_return_if_fail (G_IS_OBJECT (object));
2029 g_return_if_fail (notify != NULL);
2030 g_return_if_fail (object->ref_count >= 1);
2032 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2035 i = wstack->n_weak_refs++;
2036 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2040 wstack = g_renew (WeakRefStack, NULL, 1);
2041 wstack->object = object;
2042 wstack->n_weak_refs = 1;
2045 wstack->weak_refs[i].notify = notify;
2046 wstack->weak_refs[i].data = data;
2047 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2051 * g_object_weak_unref:
2052 * @object: #GObject to remove a weak reference from
2053 * @notify: callback to search for
2054 * @data: data to search for
2056 * Removes a weak reference callback to an object.
2059 g_object_weak_unref (GObject *object,
2063 WeakRefStack *wstack;
2064 gboolean found_one = FALSE;
2066 g_return_if_fail (G_IS_OBJECT (object));
2067 g_return_if_fail (notify != NULL);
2069 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2074 for (i = 0; i < wstack->n_weak_refs; i++)
2075 if (wstack->weak_refs[i].notify == notify &&
2076 wstack->weak_refs[i].data == data)
2079 wstack->n_weak_refs -= 1;
2080 if (i != wstack->n_weak_refs)
2081 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2087 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2091 * g_object_add_weak_pointer:
2092 * @object: The object that should be weak referenced.
2093 * @weak_pointer_location: The memory address of a pointer.
2095 * Adds a weak reference from weak_pointer to @object to indicate that
2096 * the pointer located at @weak_pointer_location is only valid during
2097 * the lifetime of @object. When the @object is finalized,
2098 * @weak_pointer will be set to %NULL.
2101 g_object_add_weak_pointer (GObject *object,
2102 gpointer *weak_pointer_location)
2104 g_return_if_fail (G_IS_OBJECT (object));
2105 g_return_if_fail (weak_pointer_location != NULL);
2107 g_object_weak_ref (object,
2108 (GWeakNotify) g_nullify_pointer,
2109 weak_pointer_location);
2113 * g_object_remove_weak_pointer:
2114 * @object: The object that is weak referenced.
2115 * @weak_pointer_location: The memory address of a pointer.
2117 * Removes a weak reference from @object that was previously added
2118 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2119 * to match the one used with g_object_add_weak_pointer().
2122 g_object_remove_weak_pointer (GObject *object,
2123 gpointer *weak_pointer_location)
2125 g_return_if_fail (G_IS_OBJECT (object));
2126 g_return_if_fail (weak_pointer_location != NULL);
2128 g_object_weak_unref (object,
2129 (GWeakNotify) g_nullify_pointer,
2130 weak_pointer_location);
2134 object_floating_flag_handler (GObject *object,
2140 case +1: /* force floating if possible */
2142 oldvalue = g_atomic_pointer_get (&object->qdata);
2143 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2144 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2145 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2146 case -1: /* sink if possible */
2148 oldvalue = g_atomic_pointer_get (&object->qdata);
2149 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2150 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2151 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2152 default: /* check floating */
2153 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2158 * g_object_is_floating:
2159 * @object: a #GObject
2161 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2166 * Returns: %TRUE if @object has a floating reference
2169 g_object_is_floating (gpointer _object)
2171 GObject *object = _object;
2172 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2173 return floating_flag_handler (object, 0);
2177 * g_object_ref_sink:
2178 * @object: a #GObject
2180 * Increase the reference count of @object, and possibly remove the
2181 * <link linkend="floating-ref">floating</link> reference, if @object
2182 * has a floating reference.
2184 * In other words, if the object is floating, then this call "assumes
2185 * ownership" of the floating reference, converting it to a normal
2186 * reference by clearing the floating flag while leaving the reference
2187 * count unchanged. If the object is not floating, then this call
2188 * adds a new normal reference increasing the reference count by one.
2195 g_object_ref_sink (gpointer _object)
2197 GObject *object = _object;
2198 gboolean was_floating;
2199 g_return_val_if_fail (G_IS_OBJECT (object), object);
2200 g_return_val_if_fail (object->ref_count >= 1, object);
2201 g_object_ref (object);
2202 was_floating = floating_flag_handler (object, -1);
2204 g_object_unref (object);
2209 * g_object_force_floating:
2210 * @object: a #GObject
2212 * This function is intended for #GObject implementations to re-enforce a
2213 * <link linkend="floating-ref">floating</link> object reference.
2214 * Doing this is seldomly required, all
2215 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2216 * usually just needs to be sunken by calling g_object_ref_sink().
2221 g_object_force_floating (GObject *object)
2223 gboolean was_floating;
2224 g_return_if_fail (G_IS_OBJECT (object));
2225 g_return_if_fail (object->ref_count >= 1);
2227 was_floating = floating_flag_handler (object, +1);
2232 guint n_toggle_refs;
2234 GToggleNotify notify;
2236 } toggle_refs[1]; /* flexible array */
2240 toggle_refs_notify (GObject *object,
2241 gboolean is_last_ref)
2243 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2245 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2246 * will only be notified when there is exactly one of them.
2248 g_assert (tstack->n_toggle_refs == 1);
2249 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2253 * g_object_add_toggle_ref:
2254 * @object: a #GObject
2255 * @notify: a function to call when this reference is the
2256 * last reference to the object, or is no longer
2257 * the last reference.
2258 * @data: data to pass to @notify
2260 * Increases the reference count of the object by one and sets a
2261 * callback to be called when all other references to the object are
2262 * dropped, or when this is already the last reference to the object
2263 * and another reference is established.
2265 * This functionality is intended for binding @object to a proxy
2266 * object managed by another memory manager. This is done with two
2267 * paired references: the strong reference added by
2268 * g_object_add_toggle_ref() and a reverse reference to the proxy
2269 * object which is either a strong reference or weak reference.
2271 * The setup is that when there are no other references to @object,
2272 * only a weak reference is held in the reverse direction from @object
2273 * to the proxy object, but when there are other references held to
2274 * @object, a strong reference is held. The @notify callback is called
2275 * when the reference from @object to the proxy object should be
2276 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2277 * true) or weak to strong (@is_last_ref false).
2279 * Since a (normal) reference must be held to the object before
2280 * calling g_object_toggle_ref(), the initial state of the reverse
2281 * link is always strong.
2283 * Multiple toggle references may be added to the same gobject,
2284 * however if there are multiple toggle references to an object, none
2285 * of them will ever be notified until all but one are removed. For
2286 * this reason, you should only ever use a toggle reference if there
2287 * is important state in the proxy object.
2292 g_object_add_toggle_ref (GObject *object,
2293 GToggleNotify notify,
2296 ToggleRefStack *tstack;
2299 g_return_if_fail (G_IS_OBJECT (object));
2300 g_return_if_fail (notify != NULL);
2301 g_return_if_fail (object->ref_count >= 1);
2303 g_object_ref (object);
2305 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2308 i = tstack->n_toggle_refs++;
2309 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2310 * in tstate->toggle_refs */
2311 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2315 tstack = g_renew (ToggleRefStack, NULL, 1);
2316 tstack->object = object;
2317 tstack->n_toggle_refs = 1;
2321 /* Set a flag for fast lookup after adding the first toggle reference */
2322 if (tstack->n_toggle_refs == 1)
2323 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2325 tstack->toggle_refs[i].notify = notify;
2326 tstack->toggle_refs[i].data = data;
2327 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2328 (GDestroyNotify)g_free);
2332 * g_object_remove_toggle_ref:
2333 * @object: a #GObject
2334 * @notify: a function to call when this reference is the
2335 * last reference to the object, or is no longer
2336 * the last reference.
2337 * @data: data to pass to @notify
2339 * Removes a reference added with g_object_add_toggle_ref(). The
2340 * reference count of the object is decreased by one.
2345 g_object_remove_toggle_ref (GObject *object,
2346 GToggleNotify notify,
2349 ToggleRefStack *tstack;
2350 gboolean found_one = FALSE;
2352 g_return_if_fail (G_IS_OBJECT (object));
2353 g_return_if_fail (notify != NULL);
2355 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2360 for (i = 0; i < tstack->n_toggle_refs; i++)
2361 if (tstack->toggle_refs[i].notify == notify &&
2362 tstack->toggle_refs[i].data == data)
2365 tstack->n_toggle_refs -= 1;
2366 if (i != tstack->n_toggle_refs)
2367 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2369 if (tstack->n_toggle_refs == 0)
2370 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2372 g_object_unref (object);
2379 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2384 * @object: a #GObject
2386 * Increases the reference count of @object.
2388 * Returns: the same @object
2391 g_object_ref (gpointer _object)
2393 GObject *object = _object;
2396 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2397 g_return_val_if_fail (object->ref_count > 0, NULL);
2399 #ifdef G_ENABLE_DEBUG
2400 if (g_trap_object_ref == object)
2402 #endif /* G_ENABLE_DEBUG */
2405 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2407 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2408 toggle_refs_notify (object, FALSE);
2415 * @object: a #GObject
2417 * Decreases the reference count of @object. When its reference count
2418 * drops to 0, the object is finalized (i.e. its memory is freed).
2421 g_object_unref (gpointer _object)
2423 GObject *object = _object;
2427 g_return_if_fail (G_IS_OBJECT (object));
2428 g_return_if_fail (object->ref_count > 0);
2430 #ifdef G_ENABLE_DEBUG
2431 if (g_trap_object_ref == object)
2433 #endif /* G_ENABLE_DEBUG */
2435 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2436 retry_atomic_decrement1:
2437 old_ref = g_atomic_int_get (&object->ref_count);
2440 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2441 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2443 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2444 goto retry_atomic_decrement1;
2446 /* if we went from 2->1 we need to notify toggle refs if any */
2447 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2448 toggle_refs_notify (object, TRUE);
2452 /* we are about tp remove the last reference */
2453 G_OBJECT_GET_CLASS (object)->dispose (object);
2455 /* may have been re-referenced meanwhile */
2456 retry_atomic_decrement2:
2457 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2460 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2461 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2463 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2464 goto retry_atomic_decrement2;
2466 /* if we went from 2->1 we need to notify toggle refs if any */
2467 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2468 toggle_refs_notify (object, TRUE);
2473 /* we are still in the process of taking away the last ref */
2474 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2475 g_signal_handlers_destroy (object);
2476 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2478 /* decrement the last reference */
2479 is_zero = g_atomic_int_dec_and_test ((int *)&object->ref_count);
2481 /* may have been re-referenced meanwhile */
2482 if (G_LIKELY (is_zero))
2484 G_OBJECT_GET_CLASS (object)->finalize (object);
2485 #ifdef G_ENABLE_DEBUG
2488 /* catch objects not chaining finalize handlers */
2489 G_LOCK (debug_objects);
2490 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2491 G_UNLOCK (debug_objects);
2493 #endif /* G_ENABLE_DEBUG */
2494 g_type_free_instance ((GTypeInstance*) object);
2500 * g_object_get_qdata:
2501 * @object: The GObject to get a stored user data pointer from
2502 * @quark: A #GQuark, naming the user data pointer
2504 * This function gets back user data pointers stored via
2505 * g_object_set_qdata().
2507 * Returns: The user data pointer set, or %NULL
2510 g_object_get_qdata (GObject *object,
2513 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2515 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2519 * g_object_set_qdata:
2520 * @object: The GObject to set store a user data pointer
2521 * @quark: A #GQuark, naming the user data pointer
2522 * @data: An opaque user data pointer
2524 * This sets an opaque, named pointer on an object.
2525 * The name is specified through a #GQuark (retrived e.g. via
2526 * g_quark_from_static_string()), and the pointer
2527 * can be gotten back from the @object with g_object_get_qdata()
2528 * until the @object is finalized.
2529 * Setting a previously set user data pointer, overrides (frees)
2530 * the old pointer set, using #NULL as pointer essentially
2531 * removes the data stored.
2534 g_object_set_qdata (GObject *object,
2538 g_return_if_fail (G_IS_OBJECT (object));
2539 g_return_if_fail (quark > 0);
2541 g_datalist_id_set_data (&object->qdata, quark, data);
2545 * g_object_set_qdata_full:
2546 * @object: The GObject to set store a user data pointer
2547 * @quark: A #GQuark, naming the user data pointer
2548 * @data: An opaque user data pointer
2549 * @destroy: Function to invoke with @data as argument, when @data
2552 * This function works like g_object_set_qdata(), but in addition,
2553 * a void (*destroy) (gpointer) function may be specified which is
2554 * called with @data as argument when the @object is finalized, or
2555 * the data is being overwritten by a call to g_object_set_qdata()
2556 * with the same @quark.
2559 g_object_set_qdata_full (GObject *object,
2562 GDestroyNotify destroy)
2564 g_return_if_fail (G_IS_OBJECT (object));
2565 g_return_if_fail (quark > 0);
2567 g_datalist_id_set_data_full (&object->qdata, quark, data,
2568 data ? destroy : (GDestroyNotify) NULL);
2572 * g_object_steal_qdata:
2573 * @object: The GObject to get a stored user data pointer from
2574 * @quark: A #GQuark, naming the user data pointer
2576 * This function gets back user data pointers stored via
2577 * g_object_set_qdata() and removes the @data from object
2578 * without invoking its destroy() function (if any was
2580 * Usually, calling this function is only required to update
2581 * user data pointers with a destroy notifier, for example:
2584 * object_add_to_user_list (GObject *object,
2585 * const gchar *new_string)
2587 * // the quark, naming the object data
2588 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2589 * // retrive the old string list
2590 * GList *list = g_object_steal_qdata (object, quark_string_list);
2592 * // prepend new string
2593 * list = g_list_prepend (list, g_strdup (new_string));
2594 * // this changed 'list', so we need to set it again
2595 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2598 * free_string_list (gpointer data)
2600 * GList *node, *list = data;
2602 * for (node = list; node; node = node->next)
2603 * g_free (node->data);
2604 * g_list_free (list);
2607 * Using g_object_get_qdata() in the above example, instead of
2608 * g_object_steal_qdata() would have left the destroy function set,
2609 * and thus the partial string list would have been freed upon
2610 * g_object_set_qdata_full().
2612 * Returns: The user data pointer set, or %NULL
2615 g_object_steal_qdata (GObject *object,
2618 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2619 g_return_val_if_fail (quark > 0, NULL);
2621 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2625 * g_object_get_data:
2626 * @object: #GObject containing the associations
2627 * @key: name of the key for that association
2629 * Gets a named field from the objects table of associations (see g_object_set_data()).
2631 * Returns: the data if found, or %NULL if no such data exists.
2634 g_object_get_data (GObject *object,
2639 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2640 g_return_val_if_fail (key != NULL, NULL);
2642 quark = g_quark_try_string (key);
2644 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2648 * g_object_set_data:
2649 * @object: #GObject containing the associations.
2650 * @key: name of the key
2651 * @data: data to associate with that key
2653 * Each object carries around a table of associations from
2654 * strings to pointers. This function lets you set an association.
2656 * If the object already had an association with that name,
2657 * the old association will be destroyed.
2660 g_object_set_data (GObject *object,
2664 g_return_if_fail (G_IS_OBJECT (object));
2665 g_return_if_fail (key != NULL);
2667 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2671 * g_object_set_data_full:
2672 * @object: #GObject containing the associations
2673 * @key: name of the key
2674 * @data: data to associate with that key
2675 * @destroy: function to call when the association is destroyed
2677 * Like g_object_set_data() except it adds notification
2678 * for when the association is destroyed, either by setting it
2679 * to a different value or when the object is destroyed.
2681 * Note that the @destroy callback is not called if @data is %NULL.
2684 g_object_set_data_full (GObject *object,
2687 GDestroyNotify destroy)
2689 g_return_if_fail (G_IS_OBJECT (object));
2690 g_return_if_fail (key != NULL);
2692 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2693 data ? destroy : (GDestroyNotify) NULL);
2697 * g_object_steal_data:
2698 * @object: #GObject containing the associations
2699 * @key: name of the key
2701 * Remove a specified datum from the object's data associations,
2702 * without invoking the association's destroy handler.
2704 * Returns: the data if found, or %NULL if no such data exists.
2707 g_object_steal_data (GObject *object,
2712 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2713 g_return_val_if_fail (key != NULL, NULL);
2715 quark = g_quark_try_string (key);
2717 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2721 g_value_object_init (GValue *value)
2723 value->data[0].v_pointer = NULL;
2727 g_value_object_free_value (GValue *value)
2729 if (value->data[0].v_pointer)
2730 g_object_unref (value->data[0].v_pointer);
2734 g_value_object_copy_value (const GValue *src_value,
2737 if (src_value->data[0].v_pointer)
2738 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2740 dest_value->data[0].v_pointer = NULL;
2744 g_value_object_transform_value (const GValue *src_value,
2747 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)))
2748 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2750 dest_value->data[0].v_pointer = NULL;
2754 g_value_object_peek_pointer (const GValue *value)
2756 return value->data[0].v_pointer;
2760 g_value_object_collect_value (GValue *value,
2761 guint n_collect_values,
2762 GTypeCValue *collect_values,
2763 guint collect_flags)
2765 if (collect_values[0].v_pointer)
2767 GObject *object = collect_values[0].v_pointer;
2769 if (object->g_type_instance.g_class == NULL)
2770 return g_strconcat ("invalid unclassed object pointer for value type `",
2771 G_VALUE_TYPE_NAME (value),
2774 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2775 return g_strconcat ("invalid object type `",
2776 G_OBJECT_TYPE_NAME (object),
2777 "' for value type `",
2778 G_VALUE_TYPE_NAME (value),
2781 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2782 value->data[0].v_pointer = g_object_ref (object);
2785 value->data[0].v_pointer = NULL;
2791 g_value_object_lcopy_value (const GValue *value,
2792 guint n_collect_values,
2793 GTypeCValue *collect_values,
2794 guint collect_flags)
2796 GObject **object_p = collect_values[0].v_pointer;
2799 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2801 if (!value->data[0].v_pointer)
2803 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2804 *object_p = value->data[0].v_pointer;
2806 *object_p = g_object_ref (value->data[0].v_pointer);
2812 * g_value_set_object:
2813 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2814 * @v_object: object value to be set
2816 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2818 * g_value_set_object() increases the reference count of @v_object
2819 * (the #GValue holds a reference to @v_object). If you do not wish
2820 * to increase the reference count of the object (i.e. you wish to
2821 * pass your current reference to the #GValue because you no longer
2822 * need it), use g_value_take_object() instead.
2824 * It is important that your #GValue holds a reference to @v_object (either its
2825 * own, or one it has taken) to ensure that the object won't be destroyed while
2826 * the #GValue still exists).
2829 g_value_set_object (GValue *value,
2834 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2836 old = value->data[0].v_pointer;
2840 g_return_if_fail (G_IS_OBJECT (v_object));
2841 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2843 value->data[0].v_pointer = v_object;
2844 g_object_ref (value->data[0].v_pointer);
2847 value->data[0].v_pointer = NULL;
2850 g_object_unref (old);
2854 * g_value_set_object_take_ownership:
2855 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2856 * @v_object: object value to be set
2858 * This is an internal function introduced mainly for C marshallers.
2860 * Deprecated: 2.4: Use g_value_take_object() instead.
2863 g_value_set_object_take_ownership (GValue *value,
2866 g_value_take_object (value, v_object);
2870 * g_value_take_object:
2871 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2872 * @v_object: object value to be set
2874 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2875 * and takes over the ownership of the callers reference to @v_object;
2876 * the caller doesn't have to unref it any more (i.e. the reference
2877 * count of the object is not increased).
2879 * If you want the #GValue to hold its own reference to @v_object, use
2880 * g_value_set_object() instead.
2885 g_value_take_object (GValue *value,
2888 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2890 if (value->data[0].v_pointer)
2892 g_object_unref (value->data[0].v_pointer);
2893 value->data[0].v_pointer = NULL;
2898 g_return_if_fail (G_IS_OBJECT (v_object));
2899 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2901 value->data[0].v_pointer = v_object; /* we take over the reference count */
2906 * g_value_get_object:
2907 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2909 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2911 * Returns: object contents of @value
2914 g_value_get_object (const GValue *value)
2916 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2918 return value->data[0].v_pointer;
2922 * g_value_dup_object:
2923 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2925 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
2926 * its reference count.
2928 * Returns: object content of @value, should be unreferenced when no
2932 g_value_dup_object (const GValue *value)
2934 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2936 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2940 * g_signal_connect_object:
2941 * @instance: the instance to connect to.
2942 * @detailed_signal: a string of the form "signal-name::detail".
2943 * @c_handler: the #GCallback to connect.
2944 * @gobject: the object to pass as data to @c_handler.
2945 * @connect_flags: a combination of #GConnnectFlags.
2947 * This is similar to g_signal_connect_data(), but uses a closure which
2948 * ensures that the @gobject stays alive during the call to @c_handler
2949 * by temporarily adding a reference count to @gobject.
2951 * Note that there is a bug in GObject that makes this function
2952 * much less useful than it might seem otherwise. Once @gobject is
2953 * disposed, the callback will no longer be called, but, the signal
2954 * handler is <emphasis>not</emphasis> currently disconnected. If the
2955 * @instance is itself being freed at the same time than this doesn't
2956 * matter, since the signal will automatically be removed, but
2957 * if @instance persists, then the signal handler will leak. You
2958 * should not remove the signal yourself because in a future versions of
2959 * GObject, the handler <emphasis>will</emphasis> automatically
2962 * It's possible to work around this problem in a way that will
2963 * continue to work with future versions of GObject by checking
2964 * that the signal handler is still connected before disconnected it:
2965 * <informalexample><programlisting>
2966 * if (g_signal_handler_is_connected (instance, id))
2967 * g_signal_handler_disconnect (instance, id);
2968 * </programlisting></informalexample>
2970 * Returns: the handler id.
2973 g_signal_connect_object (gpointer instance,
2974 const gchar *detailed_signal,
2975 GCallback c_handler,
2977 GConnectFlags connect_flags)
2979 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2980 g_return_val_if_fail (detailed_signal != NULL, 0);
2981 g_return_val_if_fail (c_handler != NULL, 0);
2987 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2989 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2991 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2994 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3000 GClosure *closures[1]; /* flexible array */
3002 /* don't change this structure without supplying an accessor for
3003 * watched closures, e.g.:
3004 * GSList* g_object_list_watched_closures (GObject *object)
3007 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3008 * carray = g_object_get_data (object, "GObject-closure-array");
3011 * GSList *slist = NULL;
3013 * for (i = 0; i < carray->n_closures; i++)
3014 * slist = g_slist_prepend (slist, carray->closures[i]);
3022 object_remove_closure (gpointer data,
3025 GObject *object = data;
3026 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3029 for (i = 0; i < carray->n_closures; i++)
3030 if (carray->closures[i] == closure)
3032 carray->n_closures--;
3033 if (i < carray->n_closures)
3034 carray->closures[i] = carray->closures[carray->n_closures];
3037 g_assert_not_reached ();
3041 destroy_closure_array (gpointer data)
3043 CArray *carray = data;
3044 GObject *object = carray->object;
3045 guint i, n = carray->n_closures;
3047 for (i = 0; i < n; i++)
3049 GClosure *closure = carray->closures[i];
3051 /* removing object_remove_closure() upfront is probably faster than
3052 * letting it fiddle with quark_closure_array which is empty anyways
3054 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3055 g_closure_invalidate (closure);
3061 * g_object_watch_closure:
3062 * @object: GObject restricting lifetime of @closure
3063 * @closure: GClosure to watch
3065 * This function essentially limits the life time of the @closure to
3066 * the life time of the object. That is, when the object is finalized,
3067 * the @closure is invalidated by calling g_closure_invalidate() on
3068 * it, in order to prevent invocations of the closure with a finalized
3069 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3070 * added as marshal guards to the @closure, to ensure that an extra
3071 * reference count is held on @object during invocation of the
3072 * @closure. Usually, this function will be called on closures that
3073 * use this @object as closure data.
3076 g_object_watch_closure (GObject *object,
3082 g_return_if_fail (G_IS_OBJECT (object));
3083 g_return_if_fail (closure != NULL);
3084 g_return_if_fail (closure->is_invalid == FALSE);
3085 g_return_if_fail (closure->in_marshal == FALSE);
3086 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3088 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3089 g_closure_add_marshal_guards (closure,
3090 object, (GClosureNotify) g_object_ref,
3091 object, (GClosureNotify) g_object_unref);
3092 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3095 carray = g_renew (CArray, NULL, 1);
3096 carray->object = object;
3097 carray->n_closures = 1;
3102 i = carray->n_closures++;
3103 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3105 carray->closures[i] = closure;
3106 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3110 * g_closure_new_object:
3111 * @sizeof_closure: the size of the structure to allocate, must be at least
3112 * <literal>sizeof (GClosure)</literal>
3113 * @object: a #GObject pointer to store in the @data field of the newly
3114 * allocated #GClosure
3116 * A variant of g_closure_new_simple() which stores @object in the
3117 * @data field of the closure and calls g_object_watch_closure() on
3118 * @object and the created closure. This function is mainly useful
3119 * when implementing new types of closures.
3121 * Returns: a newly allocated #GClosure
3124 g_closure_new_object (guint sizeof_closure,
3129 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3130 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3132 closure = g_closure_new_simple (sizeof_closure, object);
3133 g_object_watch_closure (object, closure);
3139 * g_cclosure_new_object:
3140 * @callback_func: the function to invoke
3141 * @object: a #GObject pointer to pass to @callback_func
3143 * A variant of g_cclosure_new() which uses @object as @user_data and
3144 * calls g_object_watch_closure() on @object and the created
3145 * closure. This function is useful when you have a callback closely
3146 * associated with a #GObject, and want the callback to no longer run
3147 * after the object is is freed.
3149 * Returns: a new #GCClosure
3152 g_cclosure_new_object (GCallback callback_func,
3157 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3158 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3159 g_return_val_if_fail (callback_func != NULL, NULL);
3161 closure = g_cclosure_new (callback_func, object, NULL);
3162 g_object_watch_closure (object, closure);
3168 * g_cclosure_new_object_swap:
3169 * @callback_func: the function to invoke
3170 * @object: a #GObject pointer to pass to @callback_func
3172 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3173 * and calls g_object_watch_closure() on @object and the created
3174 * closure. This function is useful when you have a callback closely
3175 * associated with a #GObject, and want the callback to no longer run
3176 * after the object is is freed.
3178 * Returns: a new #GCClosure
3181 g_cclosure_new_object_swap (GCallback callback_func,
3186 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3187 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3188 g_return_val_if_fail (callback_func != NULL, NULL);
3190 closure = g_cclosure_new_swap (callback_func, object, NULL);
3191 g_object_watch_closure (object, closure);
3197 g_object_compat_control (gsize what,
3203 case 1: /* floating base type */
3204 return G_TYPE_INITIALLY_UNOWNED;
3205 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3206 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3208 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3210 *pp = floating_flag_handler;
3217 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3220 g_initially_unowned_init (GInitiallyUnowned *object)
3222 g_object_force_floating (object);
3226 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3230 #define __G_OBJECT_C__
3231 #include "gobjectaliasdef.c"