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)
120 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
121 #define CLASS_HAS_DERIVED_CLASS(class) \
122 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
124 /* --- signals --- */
131 /* --- properties --- */
137 /* --- prototypes --- */
138 static void g_object_base_class_init (GObjectClass *class);
139 static void g_object_base_class_finalize (GObjectClass *class);
140 static void g_object_do_class_init (GObjectClass *class);
141 static void g_object_init (GObject *object,
142 GObjectClass *class);
143 static GObject* g_object_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void g_object_real_dispose (GObject *object);
147 static void g_object_finalize (GObject *object);
148 static void g_object_do_set_property (GObject *object,
152 static void g_object_do_get_property (GObject *object,
156 static void g_value_object_init (GValue *value);
157 static void g_value_object_free_value (GValue *value);
158 static void g_value_object_copy_value (const GValue *src_value,
160 static void g_value_object_transform_value (const GValue *src_value,
162 static gpointer g_value_object_peek_pointer (const GValue *value);
163 static gchar* g_value_object_collect_value (GValue *value,
164 guint n_collect_values,
165 GTypeCValue *collect_values,
166 guint collect_flags);
167 static gchar* g_value_object_lcopy_value (const GValue *value,
168 guint n_collect_values,
169 GTypeCValue *collect_values,
170 guint collect_flags);
171 static void g_object_dispatch_properties_changed (GObject *object,
173 GParamSpec **pspecs);
174 static inline void object_get_property (GObject *object,
177 static inline void object_set_property (GObject *object,
180 GObjectNotifyQueue *nqueue);
181 static guint object_floating_flag_handler (GObject *object,
184 static void object_interface_check_properties (gpointer func_data,
188 /* --- variables --- */
189 static GQuark quark_closure_array = 0;
190 static GQuark quark_weak_refs = 0;
191 static GQuark quark_toggle_refs = 0;
192 static GParamSpecPool *pspec_pool = NULL;
193 static GObjectNotifyContext property_notify_context = { 0, };
194 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
195 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
196 G_LOCK_DEFINE_STATIC (construction_mutex);
197 static GSList *construction_objects = NULL;
199 /* --- functions --- */
200 #ifdef G_ENABLE_DEBUG
201 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
202 G_LOCK_DEFINE_STATIC (debug_objects);
203 static volatile GObject *g_trap_object_ref = NULL;
204 static guint debug_objects_count = 0;
205 static GHashTable *debug_objects_ht = NULL;
208 debug_objects_foreach (gpointer key,
212 GObject *object = value;
214 g_message ("[%p] stale %s\tref_count=%u",
216 G_OBJECT_TYPE_NAME (object),
221 debug_objects_atexit (void)
225 G_LOCK (debug_objects);
226 g_message ("stale GObjects: %u", debug_objects_count);
227 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
228 G_UNLOCK (debug_objects);
231 #endif /* G_ENABLE_DEBUG */
234 g_object_type_init (void)
236 static gboolean initialized = FALSE;
237 static const GTypeFundamentalInfo finfo = {
238 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
240 static GTypeInfo info = {
241 sizeof (GObjectClass),
242 (GBaseInitFunc) g_object_base_class_init,
243 (GBaseFinalizeFunc) g_object_base_class_finalize,
244 (GClassInitFunc) g_object_do_class_init,
245 NULL /* class_destroy */,
246 NULL /* class_data */,
249 (GInstanceInitFunc) g_object_init,
250 NULL, /* value_table */
252 static const GTypeValueTable value_table = {
253 g_value_object_init, /* value_init */
254 g_value_object_free_value, /* value_free */
255 g_value_object_copy_value, /* value_copy */
256 g_value_object_peek_pointer, /* value_peek_pointer */
257 "p", /* collect_format */
258 g_value_object_collect_value, /* collect_value */
259 "p", /* lcopy_format */
260 g_value_object_lcopy_value, /* lcopy_value */
264 g_return_if_fail (initialized == FALSE);
269 info.value_table = &value_table;
270 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
271 g_assert (type == G_TYPE_OBJECT);
272 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
274 #ifdef G_ENABLE_DEBUG
277 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
278 g_atexit (debug_objects_atexit);
280 #endif /* G_ENABLE_DEBUG */
284 g_object_base_class_init (GObjectClass *class)
286 GObjectClass *pclass = g_type_class_peek_parent (class);
288 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
289 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
292 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
294 /* reset instance specific fields and methods that don't get inherited */
295 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
296 class->get_property = NULL;
297 class->set_property = NULL;
301 g_object_base_class_finalize (GObjectClass *class)
305 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
307 g_slist_free (class->construct_properties);
308 class->construct_properties = NULL;
309 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
310 for (node = list; node; node = node->next)
312 GParamSpec *pspec = node->data;
314 g_param_spec_pool_remove (pspec_pool, pspec);
315 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
316 g_param_spec_unref (pspec);
322 g_object_notify_dispatcher (GObject *object,
326 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
330 g_object_do_class_init (GObjectClass *class)
332 /* read the comment about typedef struct CArray; on why not to change this quark */
333 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
335 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
336 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
337 pspec_pool = g_param_spec_pool_new (TRUE);
338 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
339 property_notify_context.dispatcher = g_object_notify_dispatcher;
341 class->constructor = g_object_constructor;
342 class->set_property = g_object_do_set_property;
343 class->get_property = g_object_do_get_property;
344 class->dispose = g_object_real_dispose;
345 class->finalize = g_object_finalize;
346 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
347 class->notify = NULL;
351 * @gobject: the object which received the signal.
352 * @pspec: the #GParamSpec of the property which changed.
354 * The notify signal is emitted on an object when one of its
355 * properties has been changed. Note that getting this signal
356 * doesn't guarantee that the value of the property has actually
357 * changed, it may also be emitted when the setter for the property
358 * is called to reinstate the previous value.
360 * This signal is typically used to obtain change notification for a
361 * single property, by specifying the property name as a detail in the
362 * g_signal_connect() call, like this:
364 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
365 * G_CALLBACK (gtk_text_view_target_list_notify),
368 * It is important to note that you must use
369 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
370 * detail strings for the notify signal.
372 gobject_signals[NOTIFY] =
373 g_signal_new (g_intern_static_string ("notify"),
374 G_TYPE_FROM_CLASS (class),
375 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
376 G_STRUCT_OFFSET (GObjectClass, notify),
378 g_cclosure_marshal_VOID__PARAM,
382 /* Install a check function that we'll use to verify that classes that
383 * implement an interface implement all properties for that interface
385 g_type_add_interface_check (NULL, object_interface_check_properties);
389 install_property_internal (GType g_type,
393 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
395 g_warning ("When installing property: type `%s' already has a property named `%s'",
396 g_type_name (g_type),
401 g_param_spec_ref (pspec);
402 g_param_spec_sink (pspec);
403 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
404 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
408 * g_object_class_install_property:
409 * @oclass: a #GObjectClass
410 * @property_id: the id for the new property
411 * @pspec: the #GParamSpec for the new property
413 * Installs a new property. This is usually done in the class initializer.
415 * Note that it is possible to redefine a property in a derived class,
416 * by installing a property with the same name. This can be useful at times,
417 * e.g. to change the range of allowed values or the default value.
420 g_object_class_install_property (GObjectClass *class,
424 g_return_if_fail (G_IS_OBJECT_CLASS (class));
425 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
427 if (CLASS_HAS_DERIVED_CLASS (class))
428 g_error ("Attempt to add property %s::%s to class after it was derived",
429 G_OBJECT_CLASS_NAME (class), pspec->name);
431 class->flags |= CLASS_HAS_PROPS_FLAG;
433 if (pspec->flags & G_PARAM_WRITABLE)
434 g_return_if_fail (class->set_property != NULL);
435 if (pspec->flags & G_PARAM_READABLE)
436 g_return_if_fail (class->get_property != NULL);
437 g_return_if_fail (property_id > 0);
438 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
439 if (pspec->flags & G_PARAM_CONSTRUCT)
440 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
441 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
442 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
444 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
446 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
447 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
449 /* for property overrides of construct poperties, we have to get rid
450 * of the overidden inherited construct property
452 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
453 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
454 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
458 * g_object_interface_install_property:
459 * @g_iface: any interface vtable for the interface, or the default
460 * vtable for the interface.
461 * @pspec: the #GParamSpec for the new property
463 * Add a property to an interface; this is only useful for interfaces
464 * that are added to GObject-derived types. Adding a property to an
465 * interface forces all objects classes with that interface to have a
466 * compatible property. The compatible property could be a newly
467 * created #GParamSpec, but normally
468 * g_object_class_override_property() will be used so that the object
469 * class only needs to provide an implementation and inherits the
470 * property description, default value, bounds, and so forth from the
471 * interface property.
473 * This function is meant to be called from the interface's default
474 * vtable initialization function (the @class_init member of
475 * #GTypeInfo.) It must not be called after after @class_init has
476 * been called for any object types implementing this interface.
481 g_object_interface_install_property (gpointer g_iface,
484 GTypeInterface *iface_class = g_iface;
486 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
487 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
488 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
489 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
491 install_property_internal (iface_class->g_type, 0, pspec);
495 * g_object_class_find_property:
496 * @oclass: a #GObjectClass
497 * @property_name: the name of the property to look up
499 * Looks up the #GParamSpec for a property of a class.
501 * Returns: the #GParamSpec for the property, or %NULL if the class
502 * doesn't have a property of that name
505 g_object_class_find_property (GObjectClass *class,
506 const gchar *property_name)
509 GParamSpec *redirect;
511 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
512 g_return_val_if_fail (property_name != NULL, NULL);
514 pspec = g_param_spec_pool_lookup (pspec_pool,
516 G_OBJECT_CLASS_TYPE (class),
520 redirect = g_param_spec_get_redirect_target (pspec);
531 * g_object_interface_find_property:
532 * @g_iface: any interface vtable for the interface, or the default
533 * vtable for the interface
534 * @property_name: name of a property to lookup.
536 * Find the #GParamSpec with the given name for an
537 * interface. Generally, the interface vtable passed in as @g_iface
538 * will be the default vtable from g_type_default_interface_ref(), or,
539 * if you know the interface has already been loaded,
540 * g_type_default_interface_peek().
544 * Returns: the #GParamSpec for the property of the interface with the
545 * name @property_name, or %NULL if no such property exists.
548 g_object_interface_find_property (gpointer g_iface,
549 const gchar *property_name)
551 GTypeInterface *iface_class = g_iface;
553 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
554 g_return_val_if_fail (property_name != NULL, NULL);
556 return g_param_spec_pool_lookup (pspec_pool,
563 * g_object_class_override_property:
564 * @oclass: a #GObjectClass
565 * @property_id: the new property ID
566 * @name: the name of a property registered in a parent class or
567 * in an interface of this class.
569 * Registers @property_id as referring to a property with the
570 * name @name in a parent class or in an interface implemented
571 * by @oclass. This allows this class to <firstterm>override</firstterm>
572 * a property implementation in a parent class or to provide
573 * the implementation of a property from an interface.
576 * Internally, overriding is implemented by creating a property of type
577 * #GParamSpecOverride; generally operations that query the properties of
578 * the object class, such as g_object_class_find_property() or
579 * g_object_class_list_properties() will return the overridden
580 * property. However, in one case, the @construct_properties argument of
581 * the @constructor virtual function, the #GParamSpecOverride is passed
582 * instead, so that the @param_id field of the #GParamSpec will be
583 * correct. For virtually all uses, this makes no difference. If you
584 * need to get the overridden property, you can call
585 * g_param_spec_get_redirect_target().
591 g_object_class_override_property (GObjectClass *oclass,
595 GParamSpec *overridden = NULL;
599 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
600 g_return_if_fail (property_id > 0);
601 g_return_if_fail (name != NULL);
603 /* Find the overridden property; first check parent types
605 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
606 if (parent_type != G_TYPE_NONE)
607 overridden = g_param_spec_pool_lookup (pspec_pool,
616 /* Now check interfaces
618 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
619 while (n_ifaces-- && !overridden)
621 overridden = g_param_spec_pool_lookup (pspec_pool,
632 g_warning ("%s: Can't find property to override for '%s::%s'",
633 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
637 new = g_param_spec_override (name, overridden);
638 g_object_class_install_property (oclass, property_id, new);
642 * g_object_class_list_properties:
643 * @oclass: a #GObjectClass
644 * @n_properties: return location for the length of the returned array
646 * Get an array of #GParamSpec* for all properties of a class.
648 * Returns: an array of #GParamSpec* which should be freed after use
650 GParamSpec** /* free result */
651 g_object_class_list_properties (GObjectClass *class,
652 guint *n_properties_p)
657 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
659 pspecs = g_param_spec_pool_list (pspec_pool,
660 G_OBJECT_CLASS_TYPE (class),
669 * g_object_interface_list_properties:
670 * @g_iface: any interface vtable for the interface, or the default
671 * vtable for the interface
672 * @n_properties_p: location to store number of properties returned.
674 * Lists the properties of an interface.Generally, the interface
675 * vtable passed in as @g_iface will be the default vtable from
676 * g_type_default_interface_ref(), or, if you know the interface has
677 * already been loaded, g_type_default_interface_peek().
681 * Returns: a pointer to an array of pointers to #GParamSpec
682 * structures. The paramspecs are owned by GLib, but the
683 * array should be freed with g_free() when you are done with
687 g_object_interface_list_properties (gpointer g_iface,
688 guint *n_properties_p)
690 GTypeInterface *iface_class = g_iface;
694 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
696 pspecs = g_param_spec_pool_list (pspec_pool,
706 g_object_init (GObject *object,
709 object->ref_count = 1;
710 g_datalist_init (&object->qdata);
712 if (CLASS_HAS_PROPS (class))
714 /* freeze object's notification queue, g_object_newv() preserves pairedness */
715 g_object_notify_queue_freeze (object, &property_notify_context);
718 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
719 G_LOCK (construction_mutex);
720 construction_objects = g_slist_prepend (construction_objects, object);
721 G_UNLOCK (construction_mutex);
723 #ifdef G_ENABLE_DEBUG
726 G_LOCK (debug_objects);
727 debug_objects_count++;
728 g_hash_table_insert (debug_objects_ht, object, object);
729 G_UNLOCK (debug_objects);
731 #endif /* G_ENABLE_DEBUG */
735 g_object_do_set_property (GObject *object,
743 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
749 g_object_do_get_property (GObject *object,
757 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
763 g_object_real_dispose (GObject *object)
765 g_signal_handlers_destroy (object);
766 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
767 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
771 g_object_finalize (GObject *object)
773 g_datalist_clear (&object->qdata);
775 #ifdef G_ENABLE_DEBUG
778 G_LOCK (debug_objects);
779 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
780 g_hash_table_remove (debug_objects_ht, object);
781 debug_objects_count--;
782 G_UNLOCK (debug_objects);
784 #endif /* G_ENABLE_DEBUG */
789 g_object_dispatch_properties_changed (GObject *object,
795 for (i = 0; i < n_pspecs; i++)
796 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
800 * g_object_run_dispose:
801 * @object: a #GObject
803 * Releases all references to other objects. This can be used to break
806 * This functions should only be called from object system implementations.
809 g_object_run_dispose (GObject *object)
811 g_return_if_fail (G_IS_OBJECT (object));
812 g_return_if_fail (object->ref_count > 0);
814 g_object_ref (object);
815 G_OBJECT_GET_CLASS (object)->dispose (object);
816 g_object_unref (object);
820 * g_object_freeze_notify:
821 * @object: a #GObject
823 * Increases the freeze count on @object. If the freeze count is
824 * non-zero, the emission of "notify" signals on @object is
825 * stopped. The signals are queued until the freeze count is decreased
828 * This is necessary for accessors that modify multiple properties to prevent
829 * premature notification while the object is still being modified.
832 g_object_freeze_notify (GObject *object)
834 g_return_if_fail (G_IS_OBJECT (object));
836 if (g_atomic_int_get (&object->ref_count) == 0)
839 g_object_ref (object);
840 g_object_notify_queue_freeze (object, &property_notify_context);
841 g_object_unref (object);
846 * @object: a #GObject
847 * @property_name: the name of a property installed on the class of @object.
849 * Emits a "notify" signal for the property @property_name on @object.
852 g_object_notify (GObject *object,
853 const gchar *property_name)
857 g_return_if_fail (G_IS_OBJECT (object));
858 g_return_if_fail (property_name != NULL);
859 if (g_atomic_int_get (&object->ref_count) == 0)
862 g_object_ref (object);
863 /* We don't need to get the redirect target
864 * (by, e.g. calling g_object_class_find_property())
865 * because g_object_notify_queue_add() does that
867 pspec = g_param_spec_pool_lookup (pspec_pool,
869 G_OBJECT_TYPE (object),
873 g_warning ("%s: object class `%s' has no property named `%s'",
875 G_OBJECT_TYPE_NAME (object),
879 GObjectNotifyQueue *nqueue;
881 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
882 g_object_notify_queue_add (object, nqueue, pspec);
883 g_object_notify_queue_thaw (object, nqueue);
885 g_object_unref (object);
889 * g_object_thaw_notify:
890 * @object: a #GObject
892 * Reverts the effect of a previous call to
893 * g_object_freeze_notify(). The freeze count is decreased on @object
894 * and when it reaches zero, all queued "notify" signals are emitted.
896 * It is an error to call this function when the freeze count is zero.
899 g_object_thaw_notify (GObject *object)
901 GObjectNotifyQueue *nqueue;
903 g_return_if_fail (G_IS_OBJECT (object));
904 if (g_atomic_int_get (&object->ref_count) == 0)
907 g_object_ref (object);
908 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
909 if (!nqueue || !nqueue->freeze_count)
910 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
911 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
913 g_object_notify_queue_thaw (object, nqueue);
914 g_object_unref (object);
918 object_get_property (GObject *object,
922 GObjectClass *class = g_type_class_peek (pspec->owner_type);
923 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
924 GParamSpec *redirect;
926 redirect = g_param_spec_get_redirect_target (pspec);
930 class->get_property (object, param_id, value, pspec);
934 object_set_property (GObject *object,
937 GObjectNotifyQueue *nqueue)
939 GValue tmp_value = { 0, };
940 GObjectClass *class = g_type_class_peek (pspec->owner_type);
941 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
942 GParamSpec *redirect;
944 redirect = g_param_spec_get_redirect_target (pspec);
948 /* provide a copy to work from, convert (if necessary) and validate */
949 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
950 if (!g_value_transform (value, &tmp_value))
951 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
953 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
954 G_VALUE_TYPE_NAME (value));
955 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
957 gchar *contents = g_strdup_value_contents (value);
959 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
961 G_VALUE_TYPE_NAME (value),
963 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
968 class->set_property (object, param_id, &tmp_value, pspec);
969 g_object_notify_queue_add (object, nqueue, pspec);
971 g_value_unset (&tmp_value);
975 object_interface_check_properties (gpointer func_data,
978 GTypeInterface *iface_class = g_iface;
979 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
980 GType iface_type = iface_class->g_type;
984 if (!G_IS_OBJECT_CLASS (class))
987 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
991 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
993 G_OBJECT_CLASS_TYPE (class),
998 g_critical ("Object class %s doesn't implement property "
999 "'%s' from interface '%s'",
1000 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1002 g_type_name (iface_type));
1007 /* The implementation paramspec must have a less restrictive
1008 * type than the interface parameter spec for set() and a
1009 * more restrictive type for get(). We just require equality,
1010 * rather than doing something more complicated checking
1011 * the READABLE and WRITABLE flags. We also simplify here
1012 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1015 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
1016 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
1018 g_critical ("Property '%s' on class '%s' has type '%s' "
1019 "which is different from the type '%s', "
1020 "of the property on interface '%s'\n",
1022 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1023 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1024 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1025 g_type_name (iface_type));
1028 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1030 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1031 * READABLE and WRITABLE remove restrictions. The implementation
1032 * paramspec must have less restrictive flags.
1035 (!SUBSET (class_pspec->flags,
1037 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1038 !SUBSET (pspecs[n]->flags,
1040 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1042 g_critical ("Flags for property '%s' on class '%s' "
1043 "are not compatible with the property on"
1046 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1047 g_type_name (iface_type));
1056 g_object_get_type (void)
1058 return G_TYPE_OBJECT;
1063 * @object_type: the type id of the #GObject subtype to instantiate
1064 * @first_property_name: the name of the first property
1065 * @...: the value of the first property, followed optionally by more
1066 * name/value pairs, followed by %NULL
1068 * Creates a new instance of a #GObject subtype and sets its properties.
1070 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1071 * which are not explicitly specified are set to their default values.
1073 * Returns: a new instance of @object_type
1076 g_object_new (GType object_type,
1077 const gchar *first_property_name,
1083 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1085 va_start (var_args, first_property_name);
1086 object = g_object_new_valist (object_type, first_property_name, var_args);
1093 slist_maybe_remove (GSList **slist,
1096 GSList *last = NULL, *node = *slist;
1099 if (node->data == data)
1102 last->next = node->next;
1104 *slist = node->next;
1105 g_slist_free_1 (node);
1114 static inline gboolean
1115 object_in_construction_list (GObject *object)
1117 gboolean in_construction;
1118 G_LOCK (construction_mutex);
1119 in_construction = g_slist_find (construction_objects, object) != NULL;
1120 G_UNLOCK (construction_mutex);
1121 return in_construction;
1126 * @object_type: the type id of the #GObject subtype to instantiate
1127 * @n_parameters: the length of the @parameters array
1128 * @parameters: an array of #GParameter
1130 * Creates a new instance of a #GObject subtype and sets its properties.
1132 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1133 * which are not explicitly specified are set to their default values.
1135 * Returns: a new instance of @object_type
1138 g_object_newv (GType object_type,
1140 GParameter *parameters)
1142 GObjectConstructParam *cparams = NULL, *oparams;
1143 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1145 GObjectClass *class, *unref_class = NULL;
1147 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1149 GList *clist = NULL;
1150 gboolean newly_constructed;
1153 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1155 class = g_type_class_peek_static (object_type);
1157 class = unref_class = g_type_class_ref (object_type);
1158 for (slist = class->construct_properties; slist; slist = slist->next)
1160 clist = g_list_prepend (clist, slist->data);
1161 n_total_cparams += 1;
1164 if (n_parameters == 0 && n_total_cparams == 0)
1166 /* This is a simple object with no construct properties, and
1167 * no properties are being set, so short circuit the parameter
1168 * handling. This speeds up simple object construction.
1171 object = class->constructor (object_type, 0, NULL);
1172 goto did_construction;
1175 /* collect parameters, sort into construction and normal ones */
1176 oparams = g_new (GObjectConstructParam, n_parameters);
1177 cparams = g_new (GObjectConstructParam, n_total_cparams);
1178 for (i = 0; i < n_parameters; i++)
1180 GValue *value = ¶meters[i].value;
1181 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1187 g_warning ("%s: object class `%s' has no property named `%s'",
1189 g_type_name (object_type),
1190 parameters[i].name);
1193 if (!(pspec->flags & G_PARAM_WRITABLE))
1195 g_warning ("%s: property `%s' of object class `%s' is not writable",
1198 g_type_name (object_type));
1201 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1203 GList *list = g_list_find (clist, pspec);
1207 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1208 G_STRFUNC, pspec->name, g_type_name (object_type));
1211 cparams[n_cparams].pspec = pspec;
1212 cparams[n_cparams].value = value;
1217 list->prev->next = list->next;
1219 list->next->prev = list->prev;
1220 g_list_free_1 (list);
1224 oparams[n_oparams].pspec = pspec;
1225 oparams[n_oparams].value = value;
1230 /* set remaining construction properties to default values */
1231 n_cvalues = n_total_cparams - n_cparams;
1232 cvalues = g_new (GValue, n_cvalues);
1235 GList *tmp = clist->next;
1236 GParamSpec *pspec = clist->data;
1237 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1240 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1241 g_param_value_set_default (pspec, value);
1243 cparams[n_cparams].pspec = pspec;
1244 cparams[n_cparams].value = value;
1247 g_list_free_1 (clist);
1251 /* construct object from construction parameters */
1252 object = class->constructor (object_type, n_total_cparams, cparams);
1253 /* free construction values */
1256 g_value_unset (cvalues + n_cvalues);
1260 /* adjust freeze_count according to g_object_init() and remaining properties */
1261 G_LOCK (construction_mutex);
1262 newly_constructed = slist_maybe_remove (&construction_objects, object);
1263 G_UNLOCK (construction_mutex);
1265 if (CLASS_HAS_PROPS (class))
1267 if (newly_constructed || n_oparams)
1268 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1269 if (newly_constructed)
1270 g_object_notify_queue_thaw (object, nqueue);
1273 /* run 'constructed' handler if there is one */
1274 if (newly_constructed && class->constructed)
1275 class->constructed (object);
1277 /* set remaining properties */
1278 for (i = 0; i < n_oparams; i++)
1279 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1282 if (CLASS_HAS_PROPS (class))
1284 /* release our own freeze count and handle notifications */
1285 if (newly_constructed || n_oparams)
1286 g_object_notify_queue_thaw (object, nqueue);
1290 g_type_class_unref (unref_class);
1296 * g_object_new_valist:
1297 * @object_type: the type id of the #GObject subtype to instantiate
1298 * @first_property_name: the name of the first property
1299 * @var_args: the value of the first property, followed optionally by more
1300 * name/value pairs, followed by %NULL
1302 * Creates a new instance of a #GObject subtype and sets its properties.
1304 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1305 * which are not explicitly specified are set to their default values.
1307 * Returns: a new instance of @object_type
1310 g_object_new_valist (GType object_type,
1311 const gchar *first_property_name,
1314 GObjectClass *class;
1318 guint n_params = 0, n_alloced_params = 16;
1320 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1322 if (!first_property_name)
1323 return g_object_newv (object_type, 0, NULL);
1325 class = g_type_class_ref (object_type);
1327 params = g_new (GParameter, n_alloced_params);
1328 name = first_property_name;
1331 gchar *error = NULL;
1332 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1338 g_warning ("%s: object class `%s' has no property named `%s'",
1340 g_type_name (object_type),
1344 if (n_params >= n_alloced_params)
1346 n_alloced_params += 16;
1347 params = g_renew (GParameter, params, n_alloced_params);
1349 params[n_params].name = name;
1350 params[n_params].value.g_type = 0;
1351 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1352 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1355 g_warning ("%s: %s", G_STRFUNC, error);
1357 g_value_unset (¶ms[n_params].value);
1361 name = va_arg (var_args, gchar*);
1364 object = g_object_newv (object_type, n_params, params);
1367 g_value_unset (¶ms[n_params].value);
1370 g_type_class_unref (class);
1376 g_object_constructor (GType type,
1377 guint n_construct_properties,
1378 GObjectConstructParam *construct_params)
1383 object = (GObject*) g_type_create_instance (type);
1385 /* set construction parameters */
1386 if (n_construct_properties)
1388 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1390 /* set construct properties */
1391 while (n_construct_properties--)
1393 GValue *value = construct_params->value;
1394 GParamSpec *pspec = construct_params->pspec;
1397 object_set_property (object, pspec, value, nqueue);
1399 g_object_notify_queue_thaw (object, nqueue);
1400 /* the notification queue is still frozen from g_object_init(), so
1401 * we don't need to handle it here, g_object_newv() takes
1410 * g_object_set_valist:
1411 * @object: a #GObject
1412 * @first_property_name: name of the first property to set
1413 * @var_args: value for the first property, followed optionally by more
1414 * name/value pairs, followed by %NULL
1416 * Sets properties on an object.
1419 g_object_set_valist (GObject *object,
1420 const gchar *first_property_name,
1423 GObjectNotifyQueue *nqueue;
1426 g_return_if_fail (G_IS_OBJECT (object));
1428 g_object_ref (object);
1429 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1431 name = first_property_name;
1434 GValue value = { 0, };
1436 gchar *error = NULL;
1438 pspec = g_param_spec_pool_lookup (pspec_pool,
1440 G_OBJECT_TYPE (object),
1444 g_warning ("%s: object class `%s' has no property named `%s'",
1446 G_OBJECT_TYPE_NAME (object),
1450 if (!(pspec->flags & G_PARAM_WRITABLE))
1452 g_warning ("%s: property `%s' of object class `%s' is not writable",
1455 G_OBJECT_TYPE_NAME (object));
1458 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1460 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1461 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1465 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1467 G_VALUE_COLLECT (&value, var_args, 0, &error);
1470 g_warning ("%s: %s", G_STRFUNC, error);
1472 g_value_unset (&value);
1476 object_set_property (object, pspec, &value, nqueue);
1477 g_value_unset (&value);
1479 name = va_arg (var_args, gchar*);
1482 g_object_notify_queue_thaw (object, nqueue);
1483 g_object_unref (object);
1487 * g_object_get_valist:
1488 * @object: a #GObject
1489 * @first_property_name: name of the first property to get
1490 * @var_args: return location for the first property, followed optionally by more
1491 * name/return location pairs, followed by %NULL
1493 * Gets properties of an object.
1495 * In general, a copy is made of the property contents and the caller
1496 * is responsible for freeing the memory in the appropriate manner for
1497 * the type, for instance by calling g_free() or g_object_unref().
1499 * See g_object_get().
1502 g_object_get_valist (GObject *object,
1503 const gchar *first_property_name,
1508 g_return_if_fail (G_IS_OBJECT (object));
1510 g_object_ref (object);
1512 name = first_property_name;
1516 GValue value = { 0, };
1520 pspec = g_param_spec_pool_lookup (pspec_pool,
1522 G_OBJECT_TYPE (object),
1526 g_warning ("%s: object class `%s' has no property named `%s'",
1528 G_OBJECT_TYPE_NAME (object),
1532 if (!(pspec->flags & G_PARAM_READABLE))
1534 g_warning ("%s: property `%s' of object class `%s' is not readable",
1537 G_OBJECT_TYPE_NAME (object));
1541 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1543 object_get_property (object, pspec, &value);
1545 G_VALUE_LCOPY (&value, var_args, 0, &error);
1548 g_warning ("%s: %s", G_STRFUNC, error);
1550 g_value_unset (&value);
1554 g_value_unset (&value);
1556 name = va_arg (var_args, gchar*);
1559 g_object_unref (object);
1564 * @object: a #GObject
1565 * @first_property_name: name of the first property to set
1566 * @...: value for the first property, followed optionally by more
1567 * name/value pairs, followed by %NULL
1569 * Sets properties on an object.
1572 g_object_set (gpointer _object,
1573 const gchar *first_property_name,
1576 GObject *object = _object;
1579 g_return_if_fail (G_IS_OBJECT (object));
1581 va_start (var_args, first_property_name);
1582 g_object_set_valist (object, first_property_name, var_args);
1588 * @object: a #GObject
1589 * @first_property_name: name of the first property to get
1590 * @...: return location for the first property, followed optionally by more
1591 * name/return location pairs, followed by %NULL
1593 * Gets properties of an object.
1595 * In general, a copy is made of the property contents and the caller
1596 * is responsible for freeing the memory in the appropriate manner for
1597 * the type, for instance by calling g_free() or g_object_unref().
1600 * <title>Using g_object_get(<!-- -->)</title>
1601 * An example of using g_object_get() to get the contents
1602 * of three properties - one of type #G_TYPE_INT,
1603 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1609 * g_object_get (my_object,
1610 * "int-property", &intval,
1611 * "str-property", &strval,
1612 * "obj-property", &objval,
1615 * // Do something with intval, strval, objval
1618 * g_object_unref (objval);
1623 g_object_get (gpointer _object,
1624 const gchar *first_property_name,
1627 GObject *object = _object;
1630 g_return_if_fail (G_IS_OBJECT (object));
1632 va_start (var_args, first_property_name);
1633 g_object_get_valist (object, first_property_name, var_args);
1638 * g_object_set_property:
1639 * @object: a #GObject
1640 * @property_name: the name of the property to set
1643 * Sets a property on an object.
1646 g_object_set_property (GObject *object,
1647 const gchar *property_name,
1648 const GValue *value)
1650 GObjectNotifyQueue *nqueue;
1653 g_return_if_fail (G_IS_OBJECT (object));
1654 g_return_if_fail (property_name != NULL);
1655 g_return_if_fail (G_IS_VALUE (value));
1657 g_object_ref (object);
1658 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1660 pspec = g_param_spec_pool_lookup (pspec_pool,
1662 G_OBJECT_TYPE (object),
1665 g_warning ("%s: object class `%s' has no property named `%s'",
1667 G_OBJECT_TYPE_NAME (object),
1669 else if (!(pspec->flags & G_PARAM_WRITABLE))
1670 g_warning ("%s: property `%s' of object class `%s' is not writable",
1673 G_OBJECT_TYPE_NAME (object));
1674 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1675 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1676 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1678 object_set_property (object, pspec, value, nqueue);
1680 g_object_notify_queue_thaw (object, nqueue);
1681 g_object_unref (object);
1685 * g_object_get_property:
1686 * @object: a #GObject
1687 * @property_name: the name of the property to get
1688 * @value: return location for the property value
1690 * Gets a property of an object.
1692 * In general, a copy is made of the property contents and the caller is
1693 * responsible for freeing the memory by calling g_value_unset().
1695 * Note that g_object_get_property() is really intended for language
1696 * bindings, g_object_get() is much more convenient for C programming.
1699 g_object_get_property (GObject *object,
1700 const gchar *property_name,
1705 g_return_if_fail (G_IS_OBJECT (object));
1706 g_return_if_fail (property_name != NULL);
1707 g_return_if_fail (G_IS_VALUE (value));
1709 g_object_ref (object);
1711 pspec = g_param_spec_pool_lookup (pspec_pool,
1713 G_OBJECT_TYPE (object),
1716 g_warning ("%s: object class `%s' has no property named `%s'",
1718 G_OBJECT_TYPE_NAME (object),
1720 else if (!(pspec->flags & G_PARAM_READABLE))
1721 g_warning ("%s: property `%s' of object class `%s' is not readable",
1724 G_OBJECT_TYPE_NAME (object));
1727 GValue *prop_value, tmp_value = { 0, };
1729 /* auto-conversion of the callers value type
1731 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1733 g_value_reset (value);
1736 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1738 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1739 G_STRFUNC, pspec->name,
1740 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1741 G_VALUE_TYPE_NAME (value));
1742 g_object_unref (object);
1747 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1748 prop_value = &tmp_value;
1750 object_get_property (object, pspec, prop_value);
1751 if (prop_value != value)
1753 g_value_transform (prop_value, value);
1754 g_value_unset (&tmp_value);
1758 g_object_unref (object);
1763 * @object: a #GObject
1764 * @signal_spec: the spec for the first signal
1765 * @...: #GCallback for the first signal, followed by data for the
1766 * first signal, followed optionally by more signal
1767 * spec/callback/data triples, followed by %NULL
1769 * A convenience function to connect multiple signals at once.
1771 * The signal specs expected by this function have the form
1772 * "modifier::signal_name", where modifier can be one of the following:
1775 * <term>signal</term>
1777 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1778 * </para></listitem>
1781 * <term>object_signal</term>
1782 * <term>object-signal</term>
1784 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1785 * </para></listitem>
1788 * <term>swapped_signal</term>
1789 * <term>swapped-signal</term>
1791 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1792 * </para></listitem>
1795 * <term>swapped_object_signal</term>
1796 * <term>swapped-object-signal</term>
1798 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1799 * </para></listitem>
1802 * <term>signal_after</term>
1803 * <term>signal-after</term>
1805 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1806 * </para></listitem>
1809 * <term>object_signal_after</term>
1810 * <term>object-signal-after</term>
1812 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1813 * </para></listitem>
1816 * <term>swapped_signal_after</term>
1817 * <term>swapped-signal-after</term>
1819 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1820 * </para></listitem>
1823 * <term>swapped_object_signal_after</term>
1824 * <term>swapped-object-signal-after</term>
1826 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1827 * </para></listitem>
1832 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1833 * "type", GTK_WINDOW_POPUP,
1836 * "signal::event", gtk_menu_window_event, menu,
1837 * "signal::size_request", gtk_menu_window_size_request, menu,
1838 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1845 g_object_connect (gpointer _object,
1846 const gchar *signal_spec,
1849 GObject *object = _object;
1852 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1853 g_return_val_if_fail (object->ref_count > 0, object);
1855 va_start (var_args, signal_spec);
1858 GCallback callback = va_arg (var_args, GCallback);
1859 gpointer data = va_arg (var_args, gpointer);
1862 if (strncmp (signal_spec, "signal::", 8) == 0)
1863 sid = g_signal_connect_data (object, signal_spec + 8,
1864 callback, data, NULL,
1866 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1867 strncmp (signal_spec, "object-signal::", 15) == 0)
1868 sid = g_signal_connect_object (object, signal_spec + 15,
1871 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1872 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1873 sid = g_signal_connect_data (object, signal_spec + 16,
1874 callback, data, NULL,
1876 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1877 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1878 sid = g_signal_connect_object (object, signal_spec + 23,
1881 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1882 strncmp (signal_spec, "signal-after::", 14) == 0)
1883 sid = g_signal_connect_data (object, signal_spec + 14,
1884 callback, data, NULL,
1886 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1887 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1888 sid = g_signal_connect_object (object, signal_spec + 21,
1891 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1892 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1893 sid = g_signal_connect_data (object, signal_spec + 22,
1894 callback, data, NULL,
1895 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1896 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1897 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1898 sid = g_signal_connect_object (object, signal_spec + 29,
1900 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1903 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1906 signal_spec = va_arg (var_args, gchar*);
1914 * g_object_disconnect:
1915 * @object: a #GObject
1916 * @signal_spec: the spec for the first signal
1917 * @...: #GCallback for the first signal, followed by data for the first signal,
1918 * followed optionally by more signal spec/callback/data triples,
1921 * A convenience function to disconnect multiple signals at once.
1923 * The signal specs expected by this function have the form
1924 * "any_signal", which means to disconnect any signal with matching
1925 * callback and data, or "any_signal::signal_name", which only
1926 * disconnects the signal named "signal_name".
1929 g_object_disconnect (gpointer _object,
1930 const gchar *signal_spec,
1933 GObject *object = _object;
1936 g_return_if_fail (G_IS_OBJECT (object));
1937 g_return_if_fail (object->ref_count > 0);
1939 va_start (var_args, signal_spec);
1942 GCallback callback = va_arg (var_args, GCallback);
1943 gpointer data = va_arg (var_args, gpointer);
1944 guint sid = 0, detail = 0, mask = 0;
1946 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1947 strncmp (signal_spec, "any-signal::", 12) == 0)
1950 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1952 else if (strcmp (signal_spec, "any_signal") == 0 ||
1953 strcmp (signal_spec, "any-signal") == 0)
1956 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1960 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1964 if ((mask & G_SIGNAL_MATCH_ID) &&
1965 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1966 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1967 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1969 NULL, (gpointer)callback, data))
1970 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1971 signal_spec = va_arg (var_args, gchar*);
1982 } weak_refs[1]; /* flexible array */
1986 weak_refs_notify (gpointer data)
1988 WeakRefStack *wstack = data;
1991 for (i = 0; i < wstack->n_weak_refs; i++)
1992 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1997 * g_object_weak_ref:
1998 * @object: #GObject to reference weakly
1999 * @notify: callback to invoke before the object is freed
2000 * @data: extra data to pass to notify
2002 * Adds a weak reference callback to an object. Weak references are
2003 * used for notification when an object is finalized. They are called
2004 * "weak references" because they allow you to safely hold a pointer
2005 * to an object without calling g_object_ref() (g_object_ref() adds a
2006 * strong reference, that is, forces the object to stay alive).
2009 g_object_weak_ref (GObject *object,
2013 WeakRefStack *wstack;
2016 g_return_if_fail (G_IS_OBJECT (object));
2017 g_return_if_fail (notify != NULL);
2018 g_return_if_fail (object->ref_count >= 1);
2020 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2023 i = wstack->n_weak_refs++;
2024 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2028 wstack = g_renew (WeakRefStack, NULL, 1);
2029 wstack->object = object;
2030 wstack->n_weak_refs = 1;
2033 wstack->weak_refs[i].notify = notify;
2034 wstack->weak_refs[i].data = data;
2035 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2039 * g_object_weak_unref:
2040 * @object: #GObject to remove a weak reference from
2041 * @notify: callback to search for
2042 * @data: data to search for
2044 * Removes a weak reference callback to an object.
2047 g_object_weak_unref (GObject *object,
2051 WeakRefStack *wstack;
2052 gboolean found_one = FALSE;
2054 g_return_if_fail (G_IS_OBJECT (object));
2055 g_return_if_fail (notify != NULL);
2057 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2062 for (i = 0; i < wstack->n_weak_refs; i++)
2063 if (wstack->weak_refs[i].notify == notify &&
2064 wstack->weak_refs[i].data == data)
2067 wstack->n_weak_refs -= 1;
2068 if (i != wstack->n_weak_refs)
2069 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2075 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2079 * g_object_add_weak_pointer:
2080 * @object: The object that should be weak referenced.
2081 * @weak_pointer_location: The memory address of a pointer.
2083 * Adds a weak reference from weak_pointer to @object to indicate that
2084 * the pointer located at @weak_pointer_location is only valid during
2085 * the lifetime of @object. When the @object is finalized,
2086 * @weak_pointer will be set to %NULL.
2089 g_object_add_weak_pointer (GObject *object,
2090 gpointer *weak_pointer_location)
2092 g_return_if_fail (G_IS_OBJECT (object));
2093 g_return_if_fail (weak_pointer_location != NULL);
2095 g_object_weak_ref (object,
2096 (GWeakNotify) g_nullify_pointer,
2097 weak_pointer_location);
2101 * g_object_remove_weak_pointer:
2102 * @object: The object that is weak referenced.
2103 * @weak_pointer_location: The memory address of a pointer.
2105 * Removes a weak reference from @object that was previously added
2106 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2107 * to match the one used with g_object_add_weak_pointer().
2110 g_object_remove_weak_pointer (GObject *object,
2111 gpointer *weak_pointer_location)
2113 g_return_if_fail (G_IS_OBJECT (object));
2114 g_return_if_fail (weak_pointer_location != NULL);
2116 g_object_weak_unref (object,
2117 (GWeakNotify) g_nullify_pointer,
2118 weak_pointer_location);
2122 object_floating_flag_handler (GObject *object,
2128 case +1: /* force floating if possible */
2130 oldvalue = g_atomic_pointer_get (&object->qdata);
2131 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2132 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2133 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2134 case -1: /* sink if possible */
2136 oldvalue = g_atomic_pointer_get (&object->qdata);
2137 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2138 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2139 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2140 default: /* check floating */
2141 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2146 * g_object_is_floating:
2147 * @object: a #GObject
2149 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2154 * Returns: %TRUE if @object has a floating reference
2157 g_object_is_floating (gpointer _object)
2159 GObject *object = _object;
2160 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2161 return floating_flag_handler (object, 0);
2165 * g_object_ref_sink:
2166 * @object: a #GObject
2168 * Increase the reference count of @object, and possibly remove the
2169 * <link linkend="floating-ref">floating</link> reference, if @object
2170 * has a floating reference.
2172 * In other words, if the object is floating, then this call "assumes
2173 * ownership" of the floating reference, converting it to a normal
2174 * reference by clearing the floating flag while leaving the reference
2175 * count unchanged. If the object is not floating, then this call
2176 * adds a new normal reference increasing the reference count by one.
2183 g_object_ref_sink (gpointer _object)
2185 GObject *object = _object;
2186 gboolean was_floating;
2187 g_return_val_if_fail (G_IS_OBJECT (object), object);
2188 g_return_val_if_fail (object->ref_count >= 1, object);
2189 g_object_ref (object);
2190 was_floating = floating_flag_handler (object, -1);
2192 g_object_unref (object);
2197 * g_object_force_floating:
2198 * @object: a #GObject
2200 * This function is intended for #GObject implementations to re-enforce a
2201 * <link linkend="floating-ref">floating</link> object reference.
2202 * Doing this is seldomly required, all
2203 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2204 * usually just needs to be sunken by calling g_object_ref_sink().
2209 g_object_force_floating (GObject *object)
2211 gboolean was_floating;
2212 g_return_if_fail (G_IS_OBJECT (object));
2213 g_return_if_fail (object->ref_count >= 1);
2215 was_floating = floating_flag_handler (object, +1);
2220 guint n_toggle_refs;
2222 GToggleNotify notify;
2224 } toggle_refs[1]; /* flexible array */
2228 toggle_refs_notify (GObject *object,
2229 gboolean is_last_ref)
2231 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2233 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2234 * will only be notified when there is exactly one of them.
2236 g_assert (tstack->n_toggle_refs == 1);
2237 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2241 * g_object_add_toggle_ref:
2242 * @object: a #GObject
2243 * @notify: a function to call when this reference is the
2244 * last reference to the object, or is no longer
2245 * the last reference.
2246 * @data: data to pass to @notify
2248 * Increases the reference count of the object by one and sets a
2249 * callback to be called when all other references to the object are
2250 * dropped, or when this is already the last reference to the object
2251 * and another reference is established.
2253 * This functionality is intended for binding @object to a proxy
2254 * object managed by another memory manager. This is done with two
2255 * paired references: the strong reference added by
2256 * g_object_add_toggle_ref() and a reverse reference to the proxy
2257 * object which is either a strong reference or weak reference.
2259 * The setup is that when there are no other references to @object,
2260 * only a weak reference is held in the reverse direction from @object
2261 * to the proxy object, but when there are other references held to
2262 * @object, a strong reference is held. The @notify callback is called
2263 * when the reference from @object to the proxy object should be
2264 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2265 * true) or weak to strong (@is_last_ref false).
2267 * Since a (normal) reference must be held to the object before
2268 * calling g_object_toggle_ref(), the initial state of the reverse
2269 * link is always strong.
2271 * Multiple toggle references may be added to the same gobject,
2272 * however if there are multiple toggle references to an object, none
2273 * of them will ever be notified until all but one are removed. For
2274 * this reason, you should only ever use a toggle reference if there
2275 * is important state in the proxy object.
2280 g_object_add_toggle_ref (GObject *object,
2281 GToggleNotify notify,
2284 ToggleRefStack *tstack;
2287 g_return_if_fail (G_IS_OBJECT (object));
2288 g_return_if_fail (notify != NULL);
2289 g_return_if_fail (object->ref_count >= 1);
2291 g_object_ref (object);
2293 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2296 i = tstack->n_toggle_refs++;
2297 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2298 * in tstate->toggle_refs */
2299 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2303 tstack = g_renew (ToggleRefStack, NULL, 1);
2304 tstack->object = object;
2305 tstack->n_toggle_refs = 1;
2309 /* Set a flag for fast lookup after adding the first toggle reference */
2310 if (tstack->n_toggle_refs == 1)
2311 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2313 tstack->toggle_refs[i].notify = notify;
2314 tstack->toggle_refs[i].data = data;
2315 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2316 (GDestroyNotify)g_free);
2320 * g_object_remove_toggle_ref:
2321 * @object: a #GObject
2322 * @notify: a function to call when this reference is the
2323 * last reference to the object, or is no longer
2324 * the last reference.
2325 * @data: data to pass to @notify
2327 * Removes a reference added with g_object_add_toggle_ref(). The
2328 * reference count of the object is decreased by one.
2333 g_object_remove_toggle_ref (GObject *object,
2334 GToggleNotify notify,
2337 ToggleRefStack *tstack;
2338 gboolean found_one = FALSE;
2340 g_return_if_fail (G_IS_OBJECT (object));
2341 g_return_if_fail (notify != NULL);
2343 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2348 for (i = 0; i < tstack->n_toggle_refs; i++)
2349 if (tstack->toggle_refs[i].notify == notify &&
2350 tstack->toggle_refs[i].data == data)
2353 tstack->n_toggle_refs -= 1;
2354 if (i != tstack->n_toggle_refs)
2355 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2357 if (tstack->n_toggle_refs == 0)
2358 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2360 g_object_unref (object);
2367 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2372 * @object: a #GObject
2374 * Increases the reference count of @object.
2376 * Returns: the same @object
2379 g_object_ref (gpointer _object)
2381 GObject *object = _object;
2384 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2385 g_return_val_if_fail (object->ref_count > 0, NULL);
2387 #ifdef G_ENABLE_DEBUG
2388 if (g_trap_object_ref == object)
2390 #endif /* G_ENABLE_DEBUG */
2393 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2395 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2396 toggle_refs_notify (object, FALSE);
2403 * @object: a #GObject
2405 * Decreases the reference count of @object. When its reference count
2406 * drops to 0, the object is finalized (i.e. its memory is freed).
2409 g_object_unref (gpointer _object)
2411 GObject *object = _object;
2415 g_return_if_fail (G_IS_OBJECT (object));
2416 g_return_if_fail (object->ref_count > 0);
2418 #ifdef G_ENABLE_DEBUG
2419 if (g_trap_object_ref == object)
2421 #endif /* G_ENABLE_DEBUG */
2423 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2424 retry_atomic_decrement1:
2425 old_ref = g_atomic_int_get (&object->ref_count);
2428 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2429 goto retry_atomic_decrement1;
2431 /* if we went from 2->1 we need to notify toggle refs if any */
2432 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2433 toggle_refs_notify (object, TRUE);
2437 /* we are about tp remove the last reference */
2438 G_OBJECT_GET_CLASS (object)->dispose (object);
2440 /* may have been re-referenced meanwhile */
2441 retry_atomic_decrement2:
2442 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2445 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2446 goto retry_atomic_decrement2;
2448 /* if we went from 2->1 we need to notify toggle refs if any */
2449 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2450 toggle_refs_notify (object, TRUE);
2455 /* we are still in the process of taking away the last ref */
2456 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2457 g_signal_handlers_destroy (object);
2458 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2460 /* decrement the last reference */
2461 is_zero = g_atomic_int_dec_and_test ((int *)&object->ref_count);
2463 /* may have been re-referenced meanwhile */
2464 if (G_LIKELY (is_zero))
2466 G_OBJECT_GET_CLASS (object)->finalize (object);
2467 #ifdef G_ENABLE_DEBUG
2470 /* catch objects not chaining finalize handlers */
2471 G_LOCK (debug_objects);
2472 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2473 G_UNLOCK (debug_objects);
2475 #endif /* G_ENABLE_DEBUG */
2476 g_type_free_instance ((GTypeInstance*) object);
2482 * g_object_get_qdata:
2483 * @object: The GObject to get a stored user data pointer from
2484 * @quark: A #GQuark, naming the user data pointer
2486 * This function gets back user data pointers stored via
2487 * g_object_set_qdata().
2489 * Returns: The user data pointer set, or %NULL
2492 g_object_get_qdata (GObject *object,
2495 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2497 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2501 * g_object_set_qdata:
2502 * @object: The GObject to set store a user data pointer
2503 * @quark: A #GQuark, naming the user data pointer
2504 * @data: An opaque user data pointer
2506 * This sets an opaque, named pointer on an object.
2507 * The name is specified through a #GQuark (retrived e.g. via
2508 * g_quark_from_static_string()), and the pointer
2509 * can be gotten back from the @object with g_object_get_qdata()
2510 * until the @object is finalized.
2511 * Setting a previously set user data pointer, overrides (frees)
2512 * the old pointer set, using #NULL as pointer essentially
2513 * removes the data stored.
2516 g_object_set_qdata (GObject *object,
2520 g_return_if_fail (G_IS_OBJECT (object));
2521 g_return_if_fail (quark > 0);
2523 g_datalist_id_set_data (&object->qdata, quark, data);
2527 * g_object_set_qdata_full:
2528 * @object: The GObject to set store a user data pointer
2529 * @quark: A #GQuark, naming the user data pointer
2530 * @data: An opaque user data pointer
2531 * @destroy: Function to invoke with @data as argument, when @data
2534 * This function works like g_object_set_qdata(), but in addition,
2535 * a void (*destroy) (gpointer) function may be specified which is
2536 * called with @data as argument when the @object is finalized, or
2537 * the data is being overwritten by a call to g_object_set_qdata()
2538 * with the same @quark.
2541 g_object_set_qdata_full (GObject *object,
2544 GDestroyNotify destroy)
2546 g_return_if_fail (G_IS_OBJECT (object));
2547 g_return_if_fail (quark > 0);
2549 g_datalist_id_set_data_full (&object->qdata, quark, data,
2550 data ? destroy : (GDestroyNotify) NULL);
2554 * g_object_steal_qdata:
2555 * @object: The GObject to get a stored user data pointer from
2556 * @quark: A #GQuark, naming the user data pointer
2558 * This function gets back user data pointers stored via
2559 * g_object_set_qdata() and removes the @data from object
2560 * without invoking its destroy() function (if any was
2562 * Usually, calling this function is only required to update
2563 * user data pointers with a destroy notifier, for example:
2566 * object_add_to_user_list (GObject *object,
2567 * const gchar *new_string)
2569 * // the quark, naming the object data
2570 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2571 * // retrive the old string list
2572 * GList *list = g_object_steal_qdata (object, quark_string_list);
2574 * // prepend new string
2575 * list = g_list_prepend (list, g_strdup (new_string));
2576 * // this changed 'list', so we need to set it again
2577 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2580 * free_string_list (gpointer data)
2582 * GList *node, *list = data;
2584 * for (node = list; node; node = node->next)
2585 * g_free (node->data);
2586 * g_list_free (list);
2589 * Using g_object_get_qdata() in the above example, instead of
2590 * g_object_steal_qdata() would have left the destroy function set,
2591 * and thus the partial string list would have been freed upon
2592 * g_object_set_qdata_full().
2594 * Returns: The user data pointer set, or %NULL
2597 g_object_steal_qdata (GObject *object,
2600 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2601 g_return_val_if_fail (quark > 0, NULL);
2603 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2607 * g_object_get_data:
2608 * @object: #GObject containing the associations
2609 * @key: name of the key for that association
2611 * Gets a named field from the objects table of associations (see g_object_set_data()).
2613 * Returns: the data if found, or %NULL if no such data exists.
2616 g_object_get_data (GObject *object,
2621 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2622 g_return_val_if_fail (key != NULL, NULL);
2624 quark = g_quark_try_string (key);
2626 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2630 * g_object_set_data:
2631 * @object: #GObject containing the associations.
2632 * @key: name of the key
2633 * @data: data to associate with that key
2635 * Each object carries around a table of associations from
2636 * strings to pointers. This function lets you set an association.
2638 * If the object already had an association with that name,
2639 * the old association will be destroyed.
2642 g_object_set_data (GObject *object,
2646 g_return_if_fail (G_IS_OBJECT (object));
2647 g_return_if_fail (key != NULL);
2649 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2653 * g_object_set_data_full:
2654 * @object: #GObject containing the associations
2655 * @key: name of the key
2656 * @data: data to associate with that key
2657 * @destroy: function to call when the association is destroyed
2659 * Like g_object_set_data() except it adds notification
2660 * for when the association is destroyed, either by setting it
2661 * to a different value or when the object is destroyed.
2663 * Note that the @destroy callback is not called if @data is %NULL.
2666 g_object_set_data_full (GObject *object,
2669 GDestroyNotify destroy)
2671 g_return_if_fail (G_IS_OBJECT (object));
2672 g_return_if_fail (key != NULL);
2674 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2675 data ? destroy : (GDestroyNotify) NULL);
2679 * g_object_steal_data:
2680 * @object: #GObject containing the associations
2681 * @key: name of the key
2683 * Remove a specified datum from the object's data associations,
2684 * without invoking the association's destroy handler.
2686 * Returns: the data if found, or %NULL if no such data exists.
2689 g_object_steal_data (GObject *object,
2694 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2695 g_return_val_if_fail (key != NULL, NULL);
2697 quark = g_quark_try_string (key);
2699 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2703 g_value_object_init (GValue *value)
2705 value->data[0].v_pointer = NULL;
2709 g_value_object_free_value (GValue *value)
2711 if (value->data[0].v_pointer)
2712 g_object_unref (value->data[0].v_pointer);
2716 g_value_object_copy_value (const GValue *src_value,
2719 if (src_value->data[0].v_pointer)
2720 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2722 dest_value->data[0].v_pointer = NULL;
2726 g_value_object_transform_value (const GValue *src_value,
2729 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)))
2730 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2732 dest_value->data[0].v_pointer = NULL;
2736 g_value_object_peek_pointer (const GValue *value)
2738 return value->data[0].v_pointer;
2742 g_value_object_collect_value (GValue *value,
2743 guint n_collect_values,
2744 GTypeCValue *collect_values,
2745 guint collect_flags)
2747 if (collect_values[0].v_pointer)
2749 GObject *object = collect_values[0].v_pointer;
2751 if (object->g_type_instance.g_class == NULL)
2752 return g_strconcat ("invalid unclassed object pointer for value type `",
2753 G_VALUE_TYPE_NAME (value),
2756 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2757 return g_strconcat ("invalid object type `",
2758 G_OBJECT_TYPE_NAME (object),
2759 "' for value type `",
2760 G_VALUE_TYPE_NAME (value),
2763 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2764 value->data[0].v_pointer = g_object_ref (object);
2767 value->data[0].v_pointer = NULL;
2773 g_value_object_lcopy_value (const GValue *value,
2774 guint n_collect_values,
2775 GTypeCValue *collect_values,
2776 guint collect_flags)
2778 GObject **object_p = collect_values[0].v_pointer;
2781 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2783 if (!value->data[0].v_pointer)
2785 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2786 *object_p = value->data[0].v_pointer;
2788 *object_p = g_object_ref (value->data[0].v_pointer);
2794 * g_value_set_object:
2795 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2796 * @v_object: object value to be set
2798 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2800 * g_value_set_object() increases the reference count of @v_object
2801 * (the #GValue holds a reference to @v_object). If you do not wish
2802 * to increase the reference count of the object (i.e. you wish to
2803 * pass your current reference to the #GValue because you no longer
2804 * need it), use g_value_take_object() instead.
2806 * It is important that your #GValue holds a reference to @v_object (either its
2807 * own, or one it has taken) to ensure that the object won't be destroyed while
2808 * the #GValue still exists).
2811 g_value_set_object (GValue *value,
2816 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2818 old = value->data[0].v_pointer;
2822 g_return_if_fail (G_IS_OBJECT (v_object));
2823 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2825 value->data[0].v_pointer = v_object;
2826 g_object_ref (value->data[0].v_pointer);
2829 value->data[0].v_pointer = NULL;
2832 g_object_unref (old);
2836 * g_value_set_object_take_ownership:
2837 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2838 * @v_object: object value to be set
2840 * This is an internal function introduced mainly for C marshallers.
2842 * Deprecated: 2.4: Use g_value_take_object() instead.
2845 g_value_set_object_take_ownership (GValue *value,
2848 g_value_take_object (value, v_object);
2852 * g_value_take_object:
2853 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2854 * @v_object: object value to be set
2856 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2857 * and takes over the ownership of the callers reference to @v_object;
2858 * the caller doesn't have to unref it any more (i.e. the reference
2859 * count of the object is not increased).
2861 * If you want the #GValue to hold its own reference to @v_object, use
2862 * g_value_set_object() instead.
2867 g_value_take_object (GValue *value,
2870 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2872 if (value->data[0].v_pointer)
2874 g_object_unref (value->data[0].v_pointer);
2875 value->data[0].v_pointer = NULL;
2880 g_return_if_fail (G_IS_OBJECT (v_object));
2881 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2883 value->data[0].v_pointer = v_object; /* we take over the reference count */
2888 * g_value_get_object:
2889 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2891 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2893 * Returns: object contents of @value
2896 g_value_get_object (const GValue *value)
2898 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2900 return value->data[0].v_pointer;
2904 * g_value_dup_object:
2905 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2907 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
2908 * its reference count.
2910 * Returns: object content of @value, should be unreferenced when no
2914 g_value_dup_object (const GValue *value)
2916 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2918 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2922 * g_signal_connect_object:
2923 * @instance: the instance to connect to.
2924 * @detailed_signal: a string of the form "signal-name::detail".
2925 * @c_handler: the #GCallback to connect.
2926 * @gobject: the object to pass as data to @c_handler.
2927 * @connect_flags: a combination of #GConnnectFlags.
2929 * This is similar to g_signal_connect_data(), but uses a closure which
2930 * ensures that the @gobject stays alive during the call to @c_handler
2931 * by temporarily adding a reference count to @gobject.
2933 * Note that there is a bug in GObject that makes this function
2934 * much less useful than it might seem otherwise. Once @gobject is
2935 * disposed, the callback will no longer be called, but, the signal
2936 * handler is <emphasis>not</emphasis> currently disconnected. If the
2937 * @instance is itself being freed at the same time than this doesn't
2938 * matter, since the signal will automatically be removed, but
2939 * if @instance persists, then the signal handler will leak. You
2940 * should not remove the signal yourself because in a future versions of
2941 * GObject, the handler <emphasis>will</emphasis> automatically
2944 * It's possible to work around this problem in a way that will
2945 * continue to work with future versions of GObject by checking
2946 * that the signal handler is still connected before disconnected it:
2947 * <informalexample><programlisting>
2948 * if (g_signal_handler_is_connected (instance, id))
2949 * g_signal_handler_disconnect (instance, id);
2950 * </programlisting></informalexample>
2952 * Returns: the handler id.
2955 g_signal_connect_object (gpointer instance,
2956 const gchar *detailed_signal,
2957 GCallback c_handler,
2959 GConnectFlags connect_flags)
2961 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2962 g_return_val_if_fail (detailed_signal != NULL, 0);
2963 g_return_val_if_fail (c_handler != NULL, 0);
2969 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2971 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2973 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2976 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
2982 GClosure *closures[1]; /* flexible array */
2984 /* don't change this structure without supplying an accessor for
2985 * watched closures, e.g.:
2986 * GSList* g_object_list_watched_closures (GObject *object)
2989 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2990 * carray = g_object_get_data (object, "GObject-closure-array");
2993 * GSList *slist = NULL;
2995 * for (i = 0; i < carray->n_closures; i++)
2996 * slist = g_slist_prepend (slist, carray->closures[i]);
3004 object_remove_closure (gpointer data,
3007 GObject *object = data;
3008 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3011 for (i = 0; i < carray->n_closures; i++)
3012 if (carray->closures[i] == closure)
3014 carray->n_closures--;
3015 if (i < carray->n_closures)
3016 carray->closures[i] = carray->closures[carray->n_closures];
3019 g_assert_not_reached ();
3023 destroy_closure_array (gpointer data)
3025 CArray *carray = data;
3026 GObject *object = carray->object;
3027 guint i, n = carray->n_closures;
3029 for (i = 0; i < n; i++)
3031 GClosure *closure = carray->closures[i];
3033 /* removing object_remove_closure() upfront is probably faster than
3034 * letting it fiddle with quark_closure_array which is empty anyways
3036 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3037 g_closure_invalidate (closure);
3043 * g_object_watch_closure:
3044 * @object: GObject restricting lifetime of @closure
3045 * @closure: GClosure to watch
3047 * This function essentially limits the life time of the @closure to
3048 * the life time of the object. That is, when the object is finalized,
3049 * the @closure is invalidated by calling g_closure_invalidate() on
3050 * it, in order to prevent invocations of the closure with a finalized
3051 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3052 * added as marshal guards to the @closure, to ensure that an extra
3053 * reference count is held on @object during invocation of the
3054 * @closure. Usually, this function will be called on closures that
3055 * use this @object as closure data.
3058 g_object_watch_closure (GObject *object,
3064 g_return_if_fail (G_IS_OBJECT (object));
3065 g_return_if_fail (closure != NULL);
3066 g_return_if_fail (closure->is_invalid == FALSE);
3067 g_return_if_fail (closure->in_marshal == FALSE);
3068 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3070 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3071 g_closure_add_marshal_guards (closure,
3072 object, (GClosureNotify) g_object_ref,
3073 object, (GClosureNotify) g_object_unref);
3074 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3077 carray = g_renew (CArray, NULL, 1);
3078 carray->object = object;
3079 carray->n_closures = 1;
3084 i = carray->n_closures++;
3085 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3087 carray->closures[i] = closure;
3088 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3092 * g_closure_new_object:
3093 * @sizeof_closure: the size of the structure to allocate, must be at least
3094 * <literal>sizeof (GClosure)</literal>
3095 * @object: a #GObject pointer to store in the @data field of the newly
3096 * allocated #GClosure
3098 * A variant of g_closure_new_simple() which stores @object in the
3099 * @data field of the closure and calls g_object_watch_closure() on
3100 * @object and the created closure. This function is mainly useful
3101 * when implementing new types of closures.
3103 * Returns: a newly allocated #GClosure
3106 g_closure_new_object (guint sizeof_closure,
3111 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3112 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3114 closure = g_closure_new_simple (sizeof_closure, object);
3115 g_object_watch_closure (object, closure);
3121 * g_cclosure_new_object:
3122 * @callback_func: the function to invoke
3123 * @object: a #GObject pointer to pass to @callback_func
3125 * A variant of g_cclosure_new() which uses @object as @user_data and
3126 * calls g_object_watch_closure() on @object and the created
3127 * closure. This function is useful when you have a callback closely
3128 * associated with a #GObject, and want the callback to no longer run
3129 * after the object is is freed.
3131 * Returns: a new #GCClosure
3134 g_cclosure_new_object (GCallback callback_func,
3139 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3140 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3141 g_return_val_if_fail (callback_func != NULL, NULL);
3143 closure = g_cclosure_new (callback_func, object, NULL);
3144 g_object_watch_closure (object, closure);
3150 * g_cclosure_new_object_swap:
3151 * @callback_func: the function to invoke
3152 * @object: a #GObject pointer to pass to @callback_func
3154 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3155 * and calls g_object_watch_closure() on @object and the created
3156 * closure. This function is useful when you have a callback closely
3157 * associated with a #GObject, and want the callback to no longer run
3158 * after the object is is freed.
3160 * Returns: a new #GCClosure
3163 g_cclosure_new_object_swap (GCallback callback_func,
3168 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3169 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3170 g_return_val_if_fail (callback_func != NULL, NULL);
3172 closure = g_cclosure_new_swap (callback_func, object, NULL);
3173 g_object_watch_closure (object, closure);
3179 g_object_compat_control (gsize what,
3185 case 1: /* floating base type */
3186 return G_TYPE_INITIALLY_UNOWNED;
3187 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3188 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3190 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3192 *pp = floating_flag_handler;
3199 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3202 g_initially_unowned_init (GInitiallyUnowned *object)
3204 g_object_force_floating (object);
3208 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3212 #define __G_OBJECT_C__
3213 #include "gobjectaliasdef.c"