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"
45 * @Short_description: The base object type
47 * @See_also:#GParamSpecObject, g_param_spec_object()
49 * @Title: The Base Object Type
51 * GObject is the fundamental type providing the common attributes and
52 * methods for all object types in GTK+, Pango and other libraries
53 * based on GObject. The GObject class provides methods for object
54 * construction and destruction, property access methods, and signal
55 * support. Signals are described in detail in <xref
56 * linkend="gobject-Signals"/>.
58 * <para id="floating-ref">
59 * #GInitiallyUnowned is derived from #GObject. The only difference between
60 * the two is that the initial reference of a #GInitiallyUnowned is flagged
61 * as a <firstterm>floating</firstterm> reference.
62 * This means that it is not specifically claimed to be "owned" by
63 * any code portion. The main motivation for providing floating references is
64 * C convenience. In particular, it allows code to be written as:
66 * container = create_container();
67 * container_add_child (container, create_child());
69 * If <function>container_add_child()</function> will g_object_ref_sink() the
70 * passed in child, no reference of the newly created child is leaked.
71 * Without floating references, <function>container_add_child()</function>
72 * can only g_object_ref() the new child, so to implement this code without
73 * reference leaks, it would have to be written as:
76 * container = create_container();
77 * child = create_child();
78 * container_add_child (container, child);
79 * g_object_unref (child);
81 * The floating reference can be converted into
82 * an ordinary reference by calling g_object_ref_sink().
83 * For already sunken objects (objects that don't have a floating reference
84 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
86 * Since floating references are useful almost exclusively for C convenience,
87 * language bindings that provide automated reference and memory ownership
88 * maintenance (such as smart pointers or garbage collection) therefore don't
89 * need to expose floating references in their API.
92 * Some object implementations may need to save an objects floating state
93 * across certain code portions (an example is #GtkMenu), to achive this, the
94 * following sequence can be used:
97 * // save floating state
98 * gboolean was_floating = g_object_is_floating (object);
99 * g_object_ref_sink (object);
100 * // protected code portion
102 * // restore floating state
104 * g_object_force_floating (object);
105 * g_obejct_unref (object); // release previously acquired reference
110 #define PREALLOC_CPARAMS (8)
114 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
115 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
117 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
118 #define OBJECT_HAS_TOGGLE_REF(object) \
119 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
120 #define OBJECT_FLOATING_FLAG 0x2
123 /* --- signals --- */
130 /* --- properties --- */
136 /* --- prototypes --- */
137 static void g_object_base_class_init (GObjectClass *class);
138 static void g_object_base_class_finalize (GObjectClass *class);
139 static void g_object_do_class_init (GObjectClass *class);
140 static void g_object_init (GObject *object);
141 static GObject* g_object_constructor (GType type,
142 guint n_construct_properties,
143 GObjectConstructParam *construct_params);
144 static void g_object_real_dispose (GObject *object);
145 static void g_object_finalize (GObject *object);
146 static void g_object_do_set_property (GObject *object,
150 static void g_object_do_get_property (GObject *object,
154 static void g_value_object_init (GValue *value);
155 static void g_value_object_free_value (GValue *value);
156 static void g_value_object_copy_value (const GValue *src_value,
158 static void g_value_object_transform_value (const GValue *src_value,
160 static gpointer g_value_object_peek_pointer (const GValue *value);
161 static gchar* g_value_object_collect_value (GValue *value,
162 guint n_collect_values,
163 GTypeCValue *collect_values,
164 guint collect_flags);
165 static gchar* g_value_object_lcopy_value (const GValue *value,
166 guint n_collect_values,
167 GTypeCValue *collect_values,
168 guint collect_flags);
169 static void g_object_dispatch_properties_changed (GObject *object,
171 GParamSpec **pspecs);
172 static inline void object_get_property (GObject *object,
175 static inline void object_set_property (GObject *object,
178 GObjectNotifyQueue *nqueue);
179 static guint object_floating_flag_handler (GObject *object,
182 static void object_interface_check_properties (gpointer func_data,
186 /* --- variables --- */
187 static GQuark quark_closure_array = 0;
188 static GQuark quark_weak_refs = 0;
189 static GQuark quark_toggle_refs = 0;
190 static GParamSpecPool *pspec_pool = NULL;
191 static GObjectNotifyContext property_notify_context = { 0, };
192 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
193 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
194 G_LOCK_DEFINE_STATIC (construction_mutex);
195 static GSList *construction_objects = NULL;
197 /* --- functions --- */
198 #ifdef G_ENABLE_DEBUG
199 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
200 G_LOCK_DEFINE_STATIC (debug_objects);
201 static volatile GObject *g_trap_object_ref = NULL;
202 static guint debug_objects_count = 0;
203 static GHashTable *debug_objects_ht = NULL;
206 debug_objects_foreach (gpointer key,
210 GObject *object = value;
212 g_message ("[%p] stale %s\tref_count=%u",
214 G_OBJECT_TYPE_NAME (object),
219 debug_objects_atexit (void)
223 G_LOCK (debug_objects);
224 g_message ("stale GObjects: %u", debug_objects_count);
225 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
226 G_UNLOCK (debug_objects);
229 #endif /* G_ENABLE_DEBUG */
232 g_object_type_init (void)
234 static gboolean initialized = FALSE;
235 static const GTypeFundamentalInfo finfo = {
236 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
238 static GTypeInfo info = {
239 sizeof (GObjectClass),
240 (GBaseInitFunc) g_object_base_class_init,
241 (GBaseFinalizeFunc) g_object_base_class_finalize,
242 (GClassInitFunc) g_object_do_class_init,
243 NULL /* class_destroy */,
244 NULL /* class_data */,
247 (GInstanceInitFunc) g_object_init,
248 NULL, /* value_table */
250 static const GTypeValueTable value_table = {
251 g_value_object_init, /* value_init */
252 g_value_object_free_value, /* value_free */
253 g_value_object_copy_value, /* value_copy */
254 g_value_object_peek_pointer, /* value_peek_pointer */
255 "p", /* collect_format */
256 g_value_object_collect_value, /* collect_value */
257 "p", /* lcopy_format */
258 g_value_object_lcopy_value, /* lcopy_value */
262 g_return_if_fail (initialized == FALSE);
267 info.value_table = &value_table;
268 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
269 g_assert (type == G_TYPE_OBJECT);
270 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
272 #ifdef G_ENABLE_DEBUG
275 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
276 g_atexit (debug_objects_atexit);
278 #endif /* G_ENABLE_DEBUG */
282 g_object_base_class_init (GObjectClass *class)
284 GObjectClass *pclass = g_type_class_peek_parent (class);
286 /* reset instance specific fields and methods that don't get inherited */
287 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
288 class->get_property = NULL;
289 class->set_property = NULL;
293 g_object_base_class_finalize (GObjectClass *class)
297 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
299 g_slist_free (class->construct_properties);
300 class->construct_properties = NULL;
301 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
302 for (node = list; node; node = node->next)
304 GParamSpec *pspec = node->data;
306 g_param_spec_pool_remove (pspec_pool, pspec);
307 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
308 g_param_spec_unref (pspec);
314 g_object_notify_dispatcher (GObject *object,
318 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
322 g_object_do_class_init (GObjectClass *class)
324 /* read the comment about typedef struct CArray; on why not to change this quark */
325 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
327 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
328 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
329 pspec_pool = g_param_spec_pool_new (TRUE);
330 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
331 property_notify_context.dispatcher = g_object_notify_dispatcher;
333 class->constructor = g_object_constructor;
334 class->set_property = g_object_do_set_property;
335 class->get_property = g_object_do_get_property;
336 class->dispose = g_object_real_dispose;
337 class->finalize = g_object_finalize;
338 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
339 class->notify = NULL;
343 * @pspec: the #GParamSpec of the property which changed
344 * @gobject: the object which received the signal.
346 * The notify signal is emitted on an object when one of its
347 * properties has been changed. Note that getting this signal
348 * doesn't guarantee that the value of the property has actually
349 * changed, it may also be emitted when the setter for the property
350 * is called to reinstate the previous value.
352 * This signal is typically used to obtain change notification for a
353 * single property, by specifying the property name as a detail in the
354 * g_signal_connect() call, like this:
356 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
357 * G_CALLBACK (gtk_text_view_target_list_notify),
360 * It is important to note that you must use
361 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
362 * detail strings for the notify signal.
364 gobject_signals[NOTIFY] =
365 g_signal_new (g_intern_static_string ("notify"),
366 G_TYPE_FROM_CLASS (class),
367 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
368 G_STRUCT_OFFSET (GObjectClass, notify),
370 g_cclosure_marshal_VOID__PARAM,
374 /* Install a check function that we'll use to verify that classes that
375 * implement an interface implement all properties for that interface
377 g_type_add_interface_check (NULL, object_interface_check_properties);
381 install_property_internal (GType g_type,
385 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
387 g_warning ("When installing property: type `%s' already has a property named `%s'",
388 g_type_name (g_type),
393 g_param_spec_ref (pspec);
394 g_param_spec_sink (pspec);
395 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
396 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
400 * g_object_class_install_property:
401 * @oclass: a #GObjectClass
402 * @property_id: the id for the new property
403 * @pspec: the #GParamSpec for the new property
405 * Installs a new property. This is usually done in the class initializer.
408 g_object_class_install_property (GObjectClass *class,
412 g_return_if_fail (G_IS_OBJECT_CLASS (class));
413 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
414 if (pspec->flags & G_PARAM_WRITABLE)
415 g_return_if_fail (class->set_property != NULL);
416 if (pspec->flags & G_PARAM_READABLE)
417 g_return_if_fail (class->get_property != NULL);
418 g_return_if_fail (property_id > 0);
419 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
420 if (pspec->flags & G_PARAM_CONSTRUCT)
421 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
422 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
423 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
425 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
427 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
428 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
430 /* for property overrides of construct poperties, we have to get rid
431 * of the overidden inherited construct property
433 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
434 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
435 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
439 * g_object_interface_install_property:
440 * @g_iface: any interface vtable for the interface, or the default
441 * vtable for the interface.
442 * @pspec: the #GParamSpec for the new property
444 * Add a property to an interface; this is only useful for interfaces
445 * that are added to GObject-derived types. Adding a property to an
446 * interface forces all objects classes with that interface to have a
447 * compatible property. The compatible property could be a newly
448 * created #GParamSpec, but normally
449 * g_object_class_override_property() will be used so that the object
450 * class only needs to provide an implementation and inherits the
451 * property description, default value, bounds, and so forth from the
452 * interface property.
454 * This function is meant to be called from the interface's default
455 * vtable initialization function (the @class_init member of
456 * #GTypeInfo.) It must not be called after after @class_init has
457 * been called for any object types implementing this interface.
462 g_object_interface_install_property (gpointer g_iface,
465 GTypeInterface *iface_class = g_iface;
467 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
468 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
469 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
470 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
472 install_property_internal (iface_class->g_type, 0, pspec);
476 * g_object_class_find_property:
477 * @oclass: a #GObjectClass
478 * @property_name: the name of the property to look up
480 * Looks up the #GParamSpec for a property of a class.
482 * Returns: the #GParamSpec for the property, or %NULL if the class
483 * doesn't have a property of that name
486 g_object_class_find_property (GObjectClass *class,
487 const gchar *property_name)
490 GParamSpec *redirect;
492 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
493 g_return_val_if_fail (property_name != NULL, NULL);
495 pspec = g_param_spec_pool_lookup (pspec_pool,
497 G_OBJECT_CLASS_TYPE (class),
501 redirect = g_param_spec_get_redirect_target (pspec);
512 * g_object_interface_find_property:
513 * @g_iface: any interface vtable for the interface, or the default
514 * vtable for the interface
515 * @property_name: name of a property to lookup.
517 * Find the #GParamSpec with the given name for an
518 * interface. Generally, the interface vtable passed in as @g_iface
519 * will be the default vtable from g_type_default_interface_ref(), or,
520 * if you know the interface has already been loaded,
521 * g_type_default_interface_peek().
525 * Returns: the #GParamSpec for the property of the interface with the
526 * name @property_name, or %NULL if no such property exists.
529 g_object_interface_find_property (gpointer g_iface,
530 const gchar *property_name)
532 GTypeInterface *iface_class = g_iface;
534 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
535 g_return_val_if_fail (property_name != NULL, NULL);
537 return g_param_spec_pool_lookup (pspec_pool,
544 * g_object_class_override_property:
545 * @oclass: a #GObjectClass
546 * @property_id: the new property ID
547 * @name: the name of a property registered in a parent class or
548 * in an interface of this class.
550 * Registers @property_id as referring to a property with the
551 * name @name in a parent class or in an interface implemented
552 * by @oclass. This allows this class to <firstterm>override</firstterm>
553 * a property implementation in a parent class or to provide
554 * the implementation of a property from an interface.
557 * Internally, overriding is implemented by creating a property of type
558 * #GParamSpecOverride; generally operations that query the properties of
559 * the object class, such as g_object_class_find_property() or
560 * g_object_class_list_properties() will return the overridden
561 * property. However, in one case, the @construct_properties argument of
562 * the @constructor virtual function, the #GParamSpecOverride is passed
563 * instead, so that the @param_id field of the #GParamSpec will be
564 * correct. For virtually all uses, this makes no difference. If you
565 * need to get the overridden property, you can call
566 * g_param_spec_get_redirect_target().
572 g_object_class_override_property (GObjectClass *oclass,
576 GParamSpec *overridden = NULL;
580 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
581 g_return_if_fail (property_id > 0);
582 g_return_if_fail (name != NULL);
584 /* Find the overridden property; first check parent types
586 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
587 if (parent_type != G_TYPE_NONE)
588 overridden = g_param_spec_pool_lookup (pspec_pool,
597 /* Now check interfaces
599 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
600 while (n_ifaces-- && !overridden)
602 overridden = g_param_spec_pool_lookup (pspec_pool,
613 g_warning ("%s: Can't find property to override for '%s::%s'",
614 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
618 new = g_param_spec_override (name, overridden);
619 g_object_class_install_property (oclass, property_id, new);
623 * g_object_class_list_properties:
624 * @oclass: a #GObjectClass
625 * @n_properties: return location for the length of the returned array
627 * Get an array of #GParamSpec* for all properties of a class.
629 * Returns: an array of #GParamSpec* which should be freed after use
631 GParamSpec** /* free result */
632 g_object_class_list_properties (GObjectClass *class,
633 guint *n_properties_p)
638 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
640 pspecs = g_param_spec_pool_list (pspec_pool,
641 G_OBJECT_CLASS_TYPE (class),
650 * g_object_interface_list_properties:
651 * @g_iface: any interface vtable for the interface, or the default
652 * vtable for the interface
653 * @n_properties_p: location to store number of properties returned.
655 * Lists the properties of an interface.Generally, the interface
656 * vtable passed in as @g_iface will be the default vtable from
657 * g_type_default_interface_ref(), or, if you know the interface has
658 * already been loaded, g_type_default_interface_peek().
662 * Returns: a pointer to an array of pointers to #GParamSpec
663 * structures. The paramspecs are owned by GLib, but the
664 * array should be freed with g_free() when you are done with
668 g_object_interface_list_properties (gpointer g_iface,
669 guint *n_properties_p)
671 GTypeInterface *iface_class = g_iface;
675 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
677 pspecs = g_param_spec_pool_list (pspec_pool,
687 g_object_init (GObject *object)
689 object->ref_count = 1;
690 g_datalist_init (&object->qdata);
692 /* freeze object's notification queue, g_object_newv() preserves pairedness */
693 g_object_notify_queue_freeze (object, &property_notify_context);
694 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
695 G_LOCK (construction_mutex);
696 construction_objects = g_slist_prepend (construction_objects, object);
697 G_UNLOCK (construction_mutex);
699 #ifdef G_ENABLE_DEBUG
702 G_LOCK (debug_objects);
703 debug_objects_count++;
704 g_hash_table_insert (debug_objects_ht, object, object);
705 G_UNLOCK (debug_objects);
707 #endif /* G_ENABLE_DEBUG */
711 g_object_do_set_property (GObject *object,
719 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
725 g_object_do_get_property (GObject *object,
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
739 g_object_real_dispose (GObject *object)
741 g_signal_handlers_destroy (object);
742 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
743 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
747 g_object_finalize (GObject *object)
749 g_datalist_clear (&object->qdata);
751 #ifdef G_ENABLE_DEBUG
754 G_LOCK (debug_objects);
755 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
756 g_hash_table_remove (debug_objects_ht, object);
757 debug_objects_count--;
758 G_UNLOCK (debug_objects);
760 #endif /* G_ENABLE_DEBUG */
765 g_object_dispatch_properties_changed (GObject *object,
771 for (i = 0; i < n_pspecs; i++)
772 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
776 * g_object_run_dispose:
777 * @object: a #GObject
779 * Releases all references to other objects. This can be used to break
782 * This functions should only be called from object system implementations.
785 g_object_run_dispose (GObject *object)
787 g_return_if_fail (G_IS_OBJECT (object));
788 g_return_if_fail (object->ref_count > 0);
790 g_object_ref (object);
791 G_OBJECT_GET_CLASS (object)->dispose (object);
792 g_object_unref (object);
796 * g_object_freeze_notify:
797 * @object: a #GObject
799 * Stops emission of "notify" signals on @object. The signals are
800 * queued until g_object_thaw_notify() is called on @object.
802 * This is necessary for accessors that modify multiple properties to prevent
803 * premature notification while the object is still being modified.
806 g_object_freeze_notify (GObject *object)
808 g_return_if_fail (G_IS_OBJECT (object));
810 if (g_atomic_int_get (&object->ref_count) == 0)
813 g_object_ref (object);
814 g_object_notify_queue_freeze (object, &property_notify_context);
815 g_object_unref (object);
820 * @object: a #GObject
821 * @property_name: the name of a property installed on the class of @object.
823 * Emits a "notify" signal for the property @property_name on @object.
826 g_object_notify (GObject *object,
827 const gchar *property_name)
831 g_return_if_fail (G_IS_OBJECT (object));
832 g_return_if_fail (property_name != NULL);
833 if (g_atomic_int_get (&object->ref_count) == 0)
836 g_object_ref (object);
837 /* We don't need to get the redirect target
838 * (by, e.g. calling g_object_class_find_property())
839 * because g_object_notify_queue_add() does that
841 pspec = g_param_spec_pool_lookup (pspec_pool,
843 G_OBJECT_TYPE (object),
847 g_warning ("%s: object class `%s' has no property named `%s'",
849 G_OBJECT_TYPE_NAME (object),
853 GObjectNotifyQueue *nqueue;
855 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
856 g_object_notify_queue_add (object, nqueue, pspec);
857 g_object_notify_queue_thaw (object, nqueue);
859 g_object_unref (object);
863 * g_object_thaw_notify:
864 * @object: a #GObject
866 * Reverts the effect of a previous call to g_object_freeze_notify().
867 * This causes all queued "notify" signals on @object to be emitted.
870 g_object_thaw_notify (GObject *object)
872 GObjectNotifyQueue *nqueue;
874 g_return_if_fail (G_IS_OBJECT (object));
875 if (g_atomic_int_get (&object->ref_count) == 0)
878 g_object_ref (object);
879 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
880 if (!nqueue || !nqueue->freeze_count)
881 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
882 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
884 g_object_notify_queue_thaw (object, nqueue);
885 g_object_unref (object);
889 object_get_property (GObject *object,
893 GObjectClass *class = g_type_class_peek (pspec->owner_type);
894 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
895 GParamSpec *redirect;
897 redirect = g_param_spec_get_redirect_target (pspec);
901 class->get_property (object, param_id, value, pspec);
905 object_set_property (GObject *object,
908 GObjectNotifyQueue *nqueue)
910 GValue tmp_value = { 0, };
911 GObjectClass *class = g_type_class_peek (pspec->owner_type);
912 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
913 GParamSpec *redirect;
915 redirect = g_param_spec_get_redirect_target (pspec);
919 /* provide a copy to work from, convert (if necessary) and validate */
920 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
921 if (!g_value_transform (value, &tmp_value))
922 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
924 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
925 G_VALUE_TYPE_NAME (value));
926 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
928 gchar *contents = g_strdup_value_contents (value);
930 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
932 G_VALUE_TYPE_NAME (value),
934 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
939 class->set_property (object, param_id, &tmp_value, pspec);
940 g_object_notify_queue_add (object, nqueue, pspec);
942 g_value_unset (&tmp_value);
946 object_interface_check_properties (gpointer func_data,
949 GTypeInterface *iface_class = g_iface;
950 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
951 GType iface_type = iface_class->g_type;
955 if (!G_IS_OBJECT_CLASS (class))
958 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
962 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
964 G_OBJECT_CLASS_TYPE (class),
969 g_critical ("Object class %s doesn't implement property "
970 "'%s' from interface '%s'",
971 g_type_name (G_OBJECT_CLASS_TYPE (class)),
973 g_type_name (iface_type));
978 /* The implementation paramspec must have a less restrictive
979 * type than the interface parameter spec for set() and a
980 * more restrictive type for get(). We just require equality,
981 * rather than doing something more complicated checking
982 * the READABLE and WRITABLE flags. We also simplify here
983 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
986 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
987 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
989 g_critical ("Property '%s' on class '%s' has type '%s' "
990 "which is different from the type '%s', "
991 "of the property on interface '%s'\n",
993 g_type_name (G_OBJECT_CLASS_TYPE (class)),
994 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
995 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
996 g_type_name (iface_type));
999 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1001 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1002 * READABLE and WRITABLE remove restrictions. The implementation
1003 * paramspec must have less restrictive flags.
1006 (!SUBSET (class_pspec->flags,
1008 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1009 !SUBSET (pspecs[n]->flags,
1011 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1013 g_critical ("Flags for property '%s' on class '%s' "
1014 "are not compatible with the property on"
1017 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1018 g_type_name (iface_type));
1028 * @object_type: the type id of the #GObject subtype to instantiate
1029 * @first_property_name: the name of the first property
1030 * @...: the value of the first property, followed optionally by more
1031 * name/value pairs, followed by %NULL
1033 * Creates a new instance of a #GObject subtype and sets its properties.
1035 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1036 * which are not explicitly specified are set to their default values.
1038 * Returns: a new instance of @object_type
1041 g_object_new (GType object_type,
1042 const gchar *first_property_name,
1048 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1050 va_start (var_args, first_property_name);
1051 object = g_object_new_valist (object_type, first_property_name, var_args);
1058 slist_maybe_remove (GSList **slist,
1061 GSList *last = NULL, *node = *slist;
1064 if (node->data == data)
1067 last->next = node->next;
1069 *slist = node->next;
1070 g_slist_free_1 (node);
1079 static inline gboolean
1080 object_in_construction_list (GObject *object)
1082 gboolean in_construction;
1083 G_LOCK (construction_mutex);
1084 in_construction = g_slist_find (construction_objects, object) != NULL;
1085 G_UNLOCK (construction_mutex);
1086 return in_construction;
1091 * @object_type: the type id of the #GObject subtype to instantiate
1092 * @n_parameters: the length of the @parameters array
1093 * @parameters: an array of #GParameter
1095 * Creates a new instance of a #GObject subtype and sets its properties.
1097 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1098 * which are not explicitly specified are set to their default values.
1100 * Returns: a new instance of @object_type
1103 g_object_newv (GType object_type,
1105 GParameter *parameters)
1107 GObjectConstructParam *cparams, *oparams;
1108 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1110 GObjectClass *class, *unref_class = NULL;
1112 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1114 GList *clist = NULL;
1115 gboolean newly_constructed;
1118 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1120 class = g_type_class_peek_static (object_type);
1122 class = unref_class = g_type_class_ref (object_type);
1123 for (slist = class->construct_properties; slist; slist = slist->next)
1125 clist = g_list_prepend (clist, slist->data);
1126 n_total_cparams += 1;
1129 /* collect parameters, sort into construction and normal ones */
1130 oparams = g_new (GObjectConstructParam, n_parameters);
1131 cparams = g_new (GObjectConstructParam, n_total_cparams);
1132 for (i = 0; i < n_parameters; i++)
1134 GValue *value = ¶meters[i].value;
1135 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1141 g_warning ("%s: object class `%s' has no property named `%s'",
1143 g_type_name (object_type),
1144 parameters[i].name);
1147 if (!(pspec->flags & G_PARAM_WRITABLE))
1149 g_warning ("%s: property `%s' of object class `%s' is not writable",
1152 g_type_name (object_type));
1155 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1157 GList *list = g_list_find (clist, pspec);
1161 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1162 G_STRFUNC, pspec->name, g_type_name (object_type));
1165 cparams[n_cparams].pspec = pspec;
1166 cparams[n_cparams].value = value;
1171 list->prev->next = list->next;
1173 list->next->prev = list->prev;
1174 g_list_free_1 (list);
1178 oparams[n_oparams].pspec = pspec;
1179 oparams[n_oparams].value = value;
1184 /* set remaining construction properties to default values */
1185 n_cvalues = n_total_cparams - n_cparams;
1186 cvalues = g_new (GValue, n_cvalues);
1189 GList *tmp = clist->next;
1190 GParamSpec *pspec = clist->data;
1191 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1194 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1195 g_param_value_set_default (pspec, value);
1197 cparams[n_cparams].pspec = pspec;
1198 cparams[n_cparams].value = value;
1201 g_list_free_1 (clist);
1205 /* construct object from construction parameters */
1206 object = class->constructor (object_type, n_total_cparams, cparams);
1207 /* free construction values */
1210 g_value_unset (cvalues + n_cvalues);
1213 /* adjust freeze_count according to g_object_init() and remaining properties */
1214 G_LOCK (construction_mutex);
1215 newly_constructed = slist_maybe_remove (&construction_objects, object);
1216 G_UNLOCK (construction_mutex);
1217 if (newly_constructed || n_oparams)
1218 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1219 if (newly_constructed)
1220 g_object_notify_queue_thaw (object, nqueue);
1222 /* run 'constructed' handler if there is one */
1223 if (newly_constructed && class->constructed)
1224 class->constructed (object);
1226 /* set remaining properties */
1227 for (i = 0; i < n_oparams; i++)
1228 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1231 /* release our own freeze count and handle notifications */
1232 if (newly_constructed || n_oparams)
1233 g_object_notify_queue_thaw (object, nqueue);
1236 g_type_class_unref (unref_class);
1242 * g_object_new_valist:
1243 * @object_type: the type id of the #GObject subtype to instantiate
1244 * @first_property_name: the name of the first property
1245 * @var_args: the value of the first property, followed optionally by more
1246 * name/value pairs, followed by %NULL
1248 * Creates a new instance of a #GObject subtype and sets its properties.
1250 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1251 * which are not explicitly specified are set to their default values.
1253 * Returns: a new instance of @object_type
1256 g_object_new_valist (GType object_type,
1257 const gchar *first_property_name,
1260 GObjectClass *class;
1264 guint n_params = 0, n_alloced_params = 16;
1266 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1268 if (!first_property_name)
1269 return g_object_newv (object_type, 0, NULL);
1271 class = g_type_class_ref (object_type);
1273 params = g_new (GParameter, n_alloced_params);
1274 name = first_property_name;
1277 gchar *error = NULL;
1278 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1284 g_warning ("%s: object class `%s' has no property named `%s'",
1286 g_type_name (object_type),
1290 if (n_params >= n_alloced_params)
1292 n_alloced_params += 16;
1293 params = g_renew (GParameter, params, n_alloced_params);
1295 params[n_params].name = name;
1296 params[n_params].value.g_type = 0;
1297 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1298 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1301 g_warning ("%s: %s", G_STRFUNC, error);
1303 g_value_unset (¶ms[n_params].value);
1307 name = va_arg (var_args, gchar*);
1310 object = g_object_newv (object_type, n_params, params);
1313 g_value_unset (¶ms[n_params].value);
1316 g_type_class_unref (class);
1322 g_object_constructor (GType type,
1323 guint n_construct_properties,
1324 GObjectConstructParam *construct_params)
1329 object = (GObject*) g_type_create_instance (type);
1331 /* set construction parameters */
1332 if (n_construct_properties)
1334 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1336 /* set construct properties */
1337 while (n_construct_properties--)
1339 GValue *value = construct_params->value;
1340 GParamSpec *pspec = construct_params->pspec;
1343 object_set_property (object, pspec, value, nqueue);
1345 g_object_notify_queue_thaw (object, nqueue);
1346 /* the notification queue is still frozen from g_object_init(), so
1347 * we don't need to handle it here, g_object_newv() takes
1356 * g_object_set_valist:
1357 * @object: a #GObject
1358 * @first_property_name: name of the first property to set
1359 * @var_args: value for the first property, followed optionally by more
1360 * name/value pairs, followed by %NULL
1362 * Sets properties on an object.
1365 g_object_set_valist (GObject *object,
1366 const gchar *first_property_name,
1369 GObjectNotifyQueue *nqueue;
1372 g_return_if_fail (G_IS_OBJECT (object));
1374 g_object_ref (object);
1375 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1377 name = first_property_name;
1380 GValue value = { 0, };
1382 gchar *error = NULL;
1384 pspec = g_param_spec_pool_lookup (pspec_pool,
1386 G_OBJECT_TYPE (object),
1390 g_warning ("%s: object class `%s' has no property named `%s'",
1392 G_OBJECT_TYPE_NAME (object),
1396 if (!(pspec->flags & G_PARAM_WRITABLE))
1398 g_warning ("%s: property `%s' of object class `%s' is not writable",
1401 G_OBJECT_TYPE_NAME (object));
1404 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1406 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1407 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1411 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1413 G_VALUE_COLLECT (&value, var_args, 0, &error);
1416 g_warning ("%s: %s", G_STRFUNC, error);
1418 g_value_unset (&value);
1422 object_set_property (object, pspec, &value, nqueue);
1423 g_value_unset (&value);
1425 name = va_arg (var_args, gchar*);
1428 g_object_notify_queue_thaw (object, nqueue);
1429 g_object_unref (object);
1433 * g_object_get_valist:
1434 * @object: a #GObject
1435 * @first_property_name: name of the first property to get
1436 * @var_args: return location for the first property, followed optionally by more
1437 * name/return location pairs, followed by %NULL
1439 * Gets properties of an object.
1441 * In general, a copy is made of the property contents and the caller
1442 * is responsible for freeing the memory in the appropriate manner for
1443 * the type, for instance by calling g_free() or g_object_unref().
1445 * See g_object_get().
1448 g_object_get_valist (GObject *object,
1449 const gchar *first_property_name,
1454 g_return_if_fail (G_IS_OBJECT (object));
1456 g_object_ref (object);
1458 name = first_property_name;
1462 GValue value = { 0, };
1466 pspec = g_param_spec_pool_lookup (pspec_pool,
1468 G_OBJECT_TYPE (object),
1472 g_warning ("%s: object class `%s' has no property named `%s'",
1474 G_OBJECT_TYPE_NAME (object),
1478 if (!(pspec->flags & G_PARAM_READABLE))
1480 g_warning ("%s: property `%s' of object class `%s' is not readable",
1483 G_OBJECT_TYPE_NAME (object));
1487 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1489 object_get_property (object, pspec, &value);
1491 G_VALUE_LCOPY (&value, var_args, 0, &error);
1494 g_warning ("%s: %s", G_STRFUNC, error);
1496 g_value_unset (&value);
1500 g_value_unset (&value);
1502 name = va_arg (var_args, gchar*);
1505 g_object_unref (object);
1510 * @object: a #GObject
1511 * @first_property_name: name of the first property to set
1512 * @...: value for the first property, followed optionally by more
1513 * name/value pairs, followed by %NULL
1515 * Sets properties on an object.
1518 g_object_set (gpointer _object,
1519 const gchar *first_property_name,
1522 GObject *object = _object;
1525 g_return_if_fail (G_IS_OBJECT (object));
1527 va_start (var_args, first_property_name);
1528 g_object_set_valist (object, first_property_name, var_args);
1534 * @object: a #GObject
1535 * @first_property_name: name of the first property to get
1536 * @...: return location for the first property, followed optionally by more
1537 * name/return location pairs, followed by %NULL
1539 * Gets properties of an object.
1541 * In general, a copy is made of the property contents and the caller
1542 * is responsible for freeing the memory in the appropriate manner for
1543 * the type, for instance by calling g_free() or g_object_unref().
1546 * <title>Using g_object_get(<!-- -->)</title>
1547 * An example of using g_object_get() to get the contents
1548 * of three properties - one of type #G_TYPE_INT,
1549 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1555 * g_object_get (my_object,
1556 * "int-property", &intval,
1557 * "str-property", &strval,
1558 * "obj-property", &objval,
1561 * // Do something with intval, strval, objval
1564 * g_object_unref (objval);
1569 g_object_get (gpointer _object,
1570 const gchar *first_property_name,
1573 GObject *object = _object;
1576 g_return_if_fail (G_IS_OBJECT (object));
1578 va_start (var_args, first_property_name);
1579 g_object_get_valist (object, first_property_name, var_args);
1584 * g_object_set_property:
1585 * @object: a #GObject
1586 * @property_name: the name of the property to set
1589 * Sets a property on an object.
1592 g_object_set_property (GObject *object,
1593 const gchar *property_name,
1594 const GValue *value)
1596 GObjectNotifyQueue *nqueue;
1599 g_return_if_fail (G_IS_OBJECT (object));
1600 g_return_if_fail (property_name != NULL);
1601 g_return_if_fail (G_IS_VALUE (value));
1603 g_object_ref (object);
1604 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1606 pspec = g_param_spec_pool_lookup (pspec_pool,
1608 G_OBJECT_TYPE (object),
1611 g_warning ("%s: object class `%s' has no property named `%s'",
1613 G_OBJECT_TYPE_NAME (object),
1615 else if (!(pspec->flags & G_PARAM_WRITABLE))
1616 g_warning ("%s: property `%s' of object class `%s' is not writable",
1619 G_OBJECT_TYPE_NAME (object));
1620 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1621 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1622 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1624 object_set_property (object, pspec, value, nqueue);
1626 g_object_notify_queue_thaw (object, nqueue);
1627 g_object_unref (object);
1631 * g_object_get_property:
1632 * @object: a #GObject
1633 * @property_name: the name of the property to get
1634 * @value: return location for the property value
1636 * Gets a property of an object.
1638 * In general, a copy is made of the property contents and the caller is
1639 * responsible for freeing the memory by calling g_value_unset().
1641 * Note that g_object_get_property() is really intended for language
1642 * bindings, g_object_get() is much more convenient for C programming.
1645 g_object_get_property (GObject *object,
1646 const gchar *property_name,
1651 g_return_if_fail (G_IS_OBJECT (object));
1652 g_return_if_fail (property_name != NULL);
1653 g_return_if_fail (G_IS_VALUE (value));
1655 g_object_ref (object);
1657 pspec = g_param_spec_pool_lookup (pspec_pool,
1659 G_OBJECT_TYPE (object),
1662 g_warning ("%s: object class `%s' has no property named `%s'",
1664 G_OBJECT_TYPE_NAME (object),
1666 else if (!(pspec->flags & G_PARAM_READABLE))
1667 g_warning ("%s: property `%s' of object class `%s' is not readable",
1670 G_OBJECT_TYPE_NAME (object));
1673 GValue *prop_value, tmp_value = { 0, };
1675 /* auto-conversion of the callers value type
1677 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1679 g_value_reset (value);
1682 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1684 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1685 G_STRFUNC, pspec->name,
1686 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1687 G_VALUE_TYPE_NAME (value));
1688 g_object_unref (object);
1693 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1694 prop_value = &tmp_value;
1696 object_get_property (object, pspec, prop_value);
1697 if (prop_value != value)
1699 g_value_transform (prop_value, value);
1700 g_value_unset (&tmp_value);
1704 g_object_unref (object);
1709 * @object: a #GObject
1710 * @signal_spec: the spec for the first signal
1711 * @...: #GCallback for the first signal, followed by data for the
1712 * first signal, followed optionally by more signal
1713 * spec/callback/data triples, followed by %NULL
1715 * A convenience function to connect multiple signals at once.
1717 * The signal specs expected by this function have the form
1718 * "modifier::signal_name", where modifier can be one of the following:
1721 * <term>signal</term>
1723 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1724 * </para></listitem>
1727 * <term>object_signal</term>
1728 * <term>object-signal</term>
1730 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1731 * </para></listitem>
1734 * <term>swapped_signal</term>
1735 * <term>swapped-signal</term>
1737 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1738 * </para></listitem>
1741 * <term>swapped_object_signal</term>
1742 * <term>swapped-object-signal</term>
1744 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1745 * </para></listitem>
1748 * <term>signal_after</term>
1749 * <term>signal-after</term>
1751 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1752 * </para></listitem>
1755 * <term>object_signal_after</term>
1756 * <term>object-signal-after</term>
1758 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1759 * </para></listitem>
1762 * <term>swapped_signal_after</term>
1763 * <term>swapped-signal-after</term>
1765 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1766 * </para></listitem>
1769 * <term>swapped_object_signal_after</term>
1770 * <term>swapped-object-signal-after</term>
1772 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1773 * </para></listitem>
1778 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1779 * "type", GTK_WINDOW_POPUP,
1782 * "signal::event", gtk_menu_window_event, menu,
1783 * "signal::size_request", gtk_menu_window_size_request, menu,
1784 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1791 g_object_connect (gpointer _object,
1792 const gchar *signal_spec,
1795 GObject *object = _object;
1798 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1799 g_return_val_if_fail (object->ref_count > 0, object);
1801 va_start (var_args, signal_spec);
1804 GCallback callback = va_arg (var_args, GCallback);
1805 gpointer data = va_arg (var_args, gpointer);
1808 if (strncmp (signal_spec, "signal::", 8) == 0)
1809 sid = g_signal_connect_data (object, signal_spec + 8,
1810 callback, data, NULL,
1812 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1813 strncmp (signal_spec, "object-signal::", 15) == 0)
1814 sid = g_signal_connect_object (object, signal_spec + 15,
1817 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1818 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1819 sid = g_signal_connect_data (object, signal_spec + 16,
1820 callback, data, NULL,
1822 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1823 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1824 sid = g_signal_connect_object (object, signal_spec + 23,
1827 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1828 strncmp (signal_spec, "signal-after::", 14) == 0)
1829 sid = g_signal_connect_data (object, signal_spec + 14,
1830 callback, data, NULL,
1832 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1833 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1834 sid = g_signal_connect_object (object, signal_spec + 21,
1837 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1838 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1839 sid = g_signal_connect_data (object, signal_spec + 22,
1840 callback, data, NULL,
1841 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1842 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1843 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1844 sid = g_signal_connect_object (object, signal_spec + 29,
1846 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1849 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1852 signal_spec = va_arg (var_args, gchar*);
1860 * g_object_disconnect:
1861 * @object: a #GObject
1862 * @signal_spec: the spec for the first signal
1863 * @...: #GCallback for the first signal, followed by data for the first signal,
1864 * followed optionally by more signal spec/callback/data triples,
1867 * A convenience function to disconnect multiple signals at once.
1869 * The signal specs expected by this function have the form
1870 * "any_signal", which means to disconnect any signal with matching
1871 * callback and data, or "any_signal::signal_name", which only
1872 * disconnects the signal named "signal_name".
1875 g_object_disconnect (gpointer _object,
1876 const gchar *signal_spec,
1879 GObject *object = _object;
1882 g_return_if_fail (G_IS_OBJECT (object));
1883 g_return_if_fail (object->ref_count > 0);
1885 va_start (var_args, signal_spec);
1888 GCallback callback = va_arg (var_args, GCallback);
1889 gpointer data = va_arg (var_args, gpointer);
1890 guint sid = 0, detail = 0, mask = 0;
1892 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1893 strncmp (signal_spec, "any-signal::", 12) == 0)
1896 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1898 else if (strcmp (signal_spec, "any_signal") == 0 ||
1899 strcmp (signal_spec, "any-signal") == 0)
1902 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1906 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1910 if ((mask & G_SIGNAL_MATCH_ID) &&
1911 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1912 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1913 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1915 NULL, (gpointer)callback, data))
1916 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1917 signal_spec = va_arg (var_args, gchar*);
1928 } weak_refs[1]; /* flexible array */
1932 weak_refs_notify (gpointer data)
1934 WeakRefStack *wstack = data;
1937 for (i = 0; i < wstack->n_weak_refs; i++)
1938 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1943 * g_object_weak_ref:
1944 * @object: #GObject to reference weakly
1945 * @notify: callback to invoke before the object is freed
1946 * @data: extra data to pass to notify
1948 * Adds a weak reference callback to an object. Weak references are
1949 * used for notification when an object is finalized. They are called
1950 * "weak references" because they allow you to safely hold a pointer
1951 * to an object without calling g_object_ref() (g_object_ref() adds a
1952 * strong reference, that is, forces the object to stay alive).
1955 g_object_weak_ref (GObject *object,
1959 WeakRefStack *wstack;
1962 g_return_if_fail (G_IS_OBJECT (object));
1963 g_return_if_fail (notify != NULL);
1964 g_return_if_fail (object->ref_count >= 1);
1966 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1969 i = wstack->n_weak_refs++;
1970 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1974 wstack = g_renew (WeakRefStack, NULL, 1);
1975 wstack->object = object;
1976 wstack->n_weak_refs = 1;
1979 wstack->weak_refs[i].notify = notify;
1980 wstack->weak_refs[i].data = data;
1981 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1985 * g_object_weak_unref:
1986 * @object: #GObject to remove a weak reference from
1987 * @notify: callback to search for
1988 * @data: data to search for
1990 * Removes a weak reference callback to an object.
1993 g_object_weak_unref (GObject *object,
1997 WeakRefStack *wstack;
1998 gboolean found_one = FALSE;
2000 g_return_if_fail (G_IS_OBJECT (object));
2001 g_return_if_fail (notify != NULL);
2003 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2008 for (i = 0; i < wstack->n_weak_refs; i++)
2009 if (wstack->weak_refs[i].notify == notify &&
2010 wstack->weak_refs[i].data == data)
2013 wstack->n_weak_refs -= 1;
2014 if (i != wstack->n_weak_refs)
2015 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2021 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2025 * g_object_add_weak_pointer:
2026 * @object: The object that should be weak referenced.
2027 * @weak_pointer_location: The memory address of a pointer.
2029 * Adds a weak reference from weak_pointer to @object to indicate that
2030 * the pointer located at @weak_pointer_location is only valid during
2031 * the lifetime of @object. When the @object is finalized,
2032 * @weak_pointer will be set to %NULL.
2035 g_object_add_weak_pointer (GObject *object,
2036 gpointer *weak_pointer_location)
2038 g_return_if_fail (G_IS_OBJECT (object));
2039 g_return_if_fail (weak_pointer_location != NULL);
2041 g_object_weak_ref (object,
2042 (GWeakNotify) g_nullify_pointer,
2043 weak_pointer_location);
2047 * g_object_remove_weak_pointer:
2048 * @object: The object that is weak referenced.
2049 * @weak_pointer_location: The memory address of a pointer.
2051 * Removes a weak reference from @object that was previously added
2052 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2053 * to match the one used with g_object_add_weak_pointer().
2056 g_object_remove_weak_pointer (GObject *object,
2057 gpointer *weak_pointer_location)
2059 g_return_if_fail (G_IS_OBJECT (object));
2060 g_return_if_fail (weak_pointer_location != NULL);
2062 g_object_weak_unref (object,
2063 (GWeakNotify) g_nullify_pointer,
2064 weak_pointer_location);
2068 object_floating_flag_handler (GObject *object,
2074 case +1: /* force floating if possible */
2076 oldvalue = g_atomic_pointer_get (&object->qdata);
2077 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2078 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2079 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2080 case -1: /* sink if possible */
2082 oldvalue = g_atomic_pointer_get (&object->qdata);
2083 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2084 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2085 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2086 default: /* check floating */
2087 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2092 * g_object_is_floating:
2093 * @object: a #GObject
2095 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2100 * Returns: %TRUE if @object has a floating reference
2103 g_object_is_floating (gpointer _object)
2105 GObject *object = _object;
2106 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2107 return floating_flag_handler (object, 0);
2111 * g_object_ref_sink:
2112 * @object: a #GObject
2114 * Increase the reference count of @object, and possibly remove the
2115 * <link linkend="floating-ref">floating</link> reference, if @object
2116 * has a floating reference.
2118 * In other words, if the object is floating, then this call "assumes
2119 * ownership" of the floating reference, converting it to a normal
2120 * reference by clearing the floating flag while leaving the reference
2121 * count unchanged. If the object is not floating, then this call
2122 * adds a new normal reference increasing the reference count by one.
2129 g_object_ref_sink (gpointer _object)
2131 GObject *object = _object;
2132 gboolean was_floating;
2133 g_return_val_if_fail (G_IS_OBJECT (object), object);
2134 g_return_val_if_fail (object->ref_count >= 1, object);
2135 g_object_ref (object);
2136 was_floating = floating_flag_handler (object, -1);
2138 g_object_unref (object);
2143 * g_object_force_floating:
2144 * @object: a #GObject
2146 * This function is intended for #GObject implementations to re-enforce a
2147 * <link linkend="floating-ref">floating</link> object reference.
2148 * Doing this is seldomly required, all
2149 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2150 * usually just needs to be sunken by calling g_object_ref_sink().
2155 g_object_force_floating (GObject *object)
2157 gboolean was_floating;
2158 g_return_if_fail (G_IS_OBJECT (object));
2159 g_return_if_fail (object->ref_count >= 1);
2161 was_floating = floating_flag_handler (object, +1);
2166 guint n_toggle_refs;
2168 GToggleNotify notify;
2170 } toggle_refs[1]; /* flexible array */
2174 toggle_refs_notify (GObject *object,
2175 gboolean is_last_ref)
2177 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2179 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2180 * will only be notified when there is exactly one of them.
2182 g_assert (tstack->n_toggle_refs == 1);
2183 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2187 * g_object_add_toggle_ref:
2188 * @object: a #GObject
2189 * @notify: a function to call when this reference is the
2190 * last reference to the object, or is no longer
2191 * the last reference.
2192 * @data: data to pass to @notify
2194 * Increases the reference count of the object by one and sets a
2195 * callback to be called when all other references to the object are
2196 * dropped, or when this is already the last reference to the object
2197 * and another reference is established.
2199 * This functionality is intended for binding @object to a proxy
2200 * object managed by another memory manager. This is done with two
2201 * paired references: the strong reference added by
2202 * g_object_add_toggle_ref() and a reverse reference to the proxy
2203 * object which is either a strong reference or weak reference.
2205 * The setup is that when there are no other references to @object,
2206 * only a weak reference is held in the reverse direction from @object
2207 * to the proxy object, but when there are other references held to
2208 * @object, a strong reference is held. The @notify callback is called
2209 * when the reference from @object to the proxy object should be
2210 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2211 * true) or weak to strong (@is_last_ref false).
2213 * Since a (normal) reference must be held to the object before
2214 * calling g_object_toggle_ref(), the initial state of the reverse
2215 * link is always strong.
2217 * Multiple toggle references may be added to the same gobject,
2218 * however if there are multiple toggle references to an object, none
2219 * of them will ever be notified until all but one are removed. For
2220 * this reason, you should only ever use a toggle reference if there
2221 * is important state in the proxy object.
2226 g_object_add_toggle_ref (GObject *object,
2227 GToggleNotify notify,
2230 ToggleRefStack *tstack;
2233 g_return_if_fail (G_IS_OBJECT (object));
2234 g_return_if_fail (notify != NULL);
2235 g_return_if_fail (object->ref_count >= 1);
2237 g_object_ref (object);
2239 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2242 i = tstack->n_toggle_refs++;
2243 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2244 * in tstate->toggle_refs */
2245 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2249 tstack = g_renew (ToggleRefStack, NULL, 1);
2250 tstack->object = object;
2251 tstack->n_toggle_refs = 1;
2255 /* Set a flag for fast lookup after adding the first toggle reference */
2256 if (tstack->n_toggle_refs == 1)
2257 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2259 tstack->toggle_refs[i].notify = notify;
2260 tstack->toggle_refs[i].data = data;
2261 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2262 (GDestroyNotify)g_free);
2266 * g_object_remove_toggle_ref:
2267 * @object: a #GObject
2268 * @notify: a function to call when this reference is the
2269 * last reference to the object, or is no longer
2270 * the last reference.
2271 * @data: data to pass to @notify
2273 * Removes a reference added with g_object_add_toggle_ref(). The
2274 * reference count of the object is decreased by one.
2279 g_object_remove_toggle_ref (GObject *object,
2280 GToggleNotify notify,
2283 ToggleRefStack *tstack;
2284 gboolean found_one = FALSE;
2286 g_return_if_fail (G_IS_OBJECT (object));
2287 g_return_if_fail (notify != NULL);
2289 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2294 for (i = 0; i < tstack->n_toggle_refs; i++)
2295 if (tstack->toggle_refs[i].notify == notify &&
2296 tstack->toggle_refs[i].data == data)
2299 tstack->n_toggle_refs -= 1;
2300 if (i != tstack->n_toggle_refs)
2301 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2303 if (tstack->n_toggle_refs == 0)
2304 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2306 g_object_unref (object);
2313 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2318 * @object: a #GObject
2320 * Increases the reference count of @object.
2322 * Returns: the same @object
2325 g_object_ref (gpointer _object)
2327 GObject *object = _object;
2330 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2331 g_return_val_if_fail (object->ref_count > 0, NULL);
2333 #ifdef G_ENABLE_DEBUG
2334 if (g_trap_object_ref == object)
2336 #endif /* G_ENABLE_DEBUG */
2339 old_val = g_atomic_int_exchange_and_add (&object->ref_count, 1);
2341 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2342 toggle_refs_notify (object, FALSE);
2349 * @object: a #GObject
2351 * Decreases the reference count of @object. When its reference count
2352 * drops to 0, the object is finalized (i.e. its memory is freed).
2355 g_object_unref (gpointer _object)
2357 GObject *object = _object;
2361 g_return_if_fail (G_IS_OBJECT (object));
2362 g_return_if_fail (object->ref_count > 0);
2364 #ifdef G_ENABLE_DEBUG
2365 if (g_trap_object_ref == object)
2367 #endif /* G_ENABLE_DEBUG */
2369 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2370 retry_atomic_decrement1:
2371 old_ref = g_atomic_int_get (&object->ref_count);
2374 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2375 goto retry_atomic_decrement1;
2377 /* if we went from 2->1 we need to notify toggle refs if any */
2378 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2379 toggle_refs_notify (object, TRUE);
2383 /* we are about tp remove the last reference */
2384 G_OBJECT_GET_CLASS (object)->dispose (object);
2386 /* may have been re-referenced meanwhile */
2387 retry_atomic_decrement2:
2388 old_ref = g_atomic_int_get (&object->ref_count);
2391 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2392 goto retry_atomic_decrement2;
2394 /* if we went from 2->1 we need to notify toggle refs if any */
2395 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2396 toggle_refs_notify (object, TRUE);
2401 /* we are still in the process of taking away the last ref */
2402 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2403 g_signal_handlers_destroy (object);
2404 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2406 /* decrement the last reference */
2407 is_zero = g_atomic_int_dec_and_test (&object->ref_count);
2409 /* may have been re-referenced meanwhile */
2410 if (G_LIKELY (is_zero))
2412 G_OBJECT_GET_CLASS (object)->finalize (object);
2413 #ifdef G_ENABLE_DEBUG
2416 /* catch objects not chaining finalize handlers */
2417 G_LOCK (debug_objects);
2418 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2419 G_UNLOCK (debug_objects);
2421 #endif /* G_ENABLE_DEBUG */
2422 g_type_free_instance ((GTypeInstance*) object);
2428 * g_object_get_qdata:
2429 * @object: The GObject to get a stored user data pointer from
2430 * @quark: A #GQuark, naming the user data pointer
2432 * This function gets back user data pointers stored via
2433 * g_object_set_qdata().
2435 * Returns: The user data pointer set, or %NULL
2438 g_object_get_qdata (GObject *object,
2441 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2443 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2447 * g_object_set_qdata:
2448 * @object: The GObject to set store a user data pointer
2449 * @quark: A #GQuark, naming the user data pointer
2450 * @data: An opaque user data pointer
2452 * This sets an opaque, named pointer on an object.
2453 * The name is specified through a #GQuark (retrived e.g. via
2454 * g_quark_from_static_string()), and the pointer
2455 * can be gotten back from the @object with g_object_get_qdata()
2456 * until the @object is finalized.
2457 * Setting a previously set user data pointer, overrides (frees)
2458 * the old pointer set, using #NULL as pointer essentially
2459 * removes the data stored.
2462 g_object_set_qdata (GObject *object,
2466 g_return_if_fail (G_IS_OBJECT (object));
2467 g_return_if_fail (quark > 0);
2469 g_datalist_id_set_data (&object->qdata, quark, data);
2473 * g_object_set_qdata_full:
2474 * @object: The GObject to set store a user data pointer
2475 * @quark: A #GQuark, naming the user data pointer
2476 * @data: An opaque user data pointer
2477 * @destroy: Function to invoke with @data as argument, when @data
2480 * This function works like g_object_set_qdata(), but in addition,
2481 * a void (*destroy) (gpointer) function may be specified which is
2482 * called with @data as argument when the @object is finalized, or
2483 * the data is being overwritten by a call to g_object_set_qdata()
2484 * with the same @quark.
2487 g_object_set_qdata_full (GObject *object,
2490 GDestroyNotify destroy)
2492 g_return_if_fail (G_IS_OBJECT (object));
2493 g_return_if_fail (quark > 0);
2495 g_datalist_id_set_data_full (&object->qdata, quark, data,
2496 data ? destroy : (GDestroyNotify) NULL);
2500 * g_object_steal_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() and removes the @data from object
2506 * without invoking it's destroy() function (if any was
2508 * Usually, calling this function is only required to update
2509 * user data pointers with a destroy notifier, for example:
2512 * object_add_to_user_list (GObject *object,
2513 * const gchar *new_string)
2515 * // the quark, naming the object data
2516 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2517 * // retrive the old string list
2518 * GList *list = g_object_steal_qdata (object, quark_string_list);
2520 * // prepend new string
2521 * list = g_list_prepend (list, g_strdup (new_string));
2522 * // this changed 'list', so we need to set it again
2523 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2526 * free_string_list (gpointer data)
2528 * GList *node, *list = data;
2530 * for (node = list; node; node = node->next)
2531 * g_free (node->data);
2532 * g_list_free (list);
2535 * Using g_object_get_qdata() in the above example, instead of
2536 * g_object_steal_qdata() would have left the destroy function set,
2537 * and thus the partial string list would have been freed upon
2538 * g_object_set_qdata_full().
2540 * Returns: The user data pointer set, or %NULL
2543 g_object_steal_qdata (GObject *object,
2546 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2547 g_return_val_if_fail (quark > 0, NULL);
2549 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2553 * g_object_get_data:
2554 * @object: #GObject containing the associations
2555 * @key: name of the key for that association
2557 * Gets a named field from the objects table of associations (see g_object_set_data()).
2559 * Returns: the data if found, or %NULL if no such data exists.
2562 g_object_get_data (GObject *object,
2567 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2568 g_return_val_if_fail (key != NULL, NULL);
2570 quark = g_quark_try_string (key);
2572 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2576 * g_object_set_data:
2577 * @object: #GObject containing the associations.
2578 * @key: name of the key
2579 * @data: data to associate with that key
2581 * Each object carries around a table of associations from
2582 * strings to pointers. This function lets you set an association.
2584 * If the object already had an association with that name,
2585 * the old association will be destroyed.
2588 g_object_set_data (GObject *object,
2592 g_return_if_fail (G_IS_OBJECT (object));
2593 g_return_if_fail (key != NULL);
2595 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2599 * g_object_set_data_full:
2600 * @object: #GObject containing the associations
2601 * @key: name of the key
2602 * @data: data to associate with that key
2603 * @destroy: function to call when the association is destroyed
2605 * Like g_object_set_data() except it adds notification
2606 * for when the association is destroyed, either by setting it
2607 * to a different value or when the object is destroyed.
2609 * Note that the @destroy callback is not called if @data is %NULL.
2612 g_object_set_data_full (GObject *object,
2615 GDestroyNotify destroy)
2617 g_return_if_fail (G_IS_OBJECT (object));
2618 g_return_if_fail (key != NULL);
2620 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2621 data ? destroy : (GDestroyNotify) NULL);
2625 * g_object_steal_data:
2626 * @object: #GObject containing the associations
2627 * @key: name of the key
2629 * Remove a specified datum from the object's data associations,
2630 * without invoking the association's destroy handler.
2632 * Returns: the data if found, or %NULL if no such data exists.
2635 g_object_steal_data (GObject *object,
2640 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2641 g_return_val_if_fail (key != NULL, NULL);
2643 quark = g_quark_try_string (key);
2645 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2649 g_value_object_init (GValue *value)
2651 value->data[0].v_pointer = NULL;
2655 g_value_object_free_value (GValue *value)
2657 if (value->data[0].v_pointer)
2658 g_object_unref (value->data[0].v_pointer);
2662 g_value_object_copy_value (const GValue *src_value,
2665 if (src_value->data[0].v_pointer)
2666 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2668 dest_value->data[0].v_pointer = NULL;
2672 g_value_object_transform_value (const GValue *src_value,
2675 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)))
2676 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2678 dest_value->data[0].v_pointer = NULL;
2682 g_value_object_peek_pointer (const GValue *value)
2684 return value->data[0].v_pointer;
2688 g_value_object_collect_value (GValue *value,
2689 guint n_collect_values,
2690 GTypeCValue *collect_values,
2691 guint collect_flags)
2693 if (collect_values[0].v_pointer)
2695 GObject *object = collect_values[0].v_pointer;
2697 if (object->g_type_instance.g_class == NULL)
2698 return g_strconcat ("invalid unclassed object pointer for value type `",
2699 G_VALUE_TYPE_NAME (value),
2702 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2703 return g_strconcat ("invalid object type `",
2704 G_OBJECT_TYPE_NAME (object),
2705 "' for value type `",
2706 G_VALUE_TYPE_NAME (value),
2709 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2710 value->data[0].v_pointer = g_object_ref (object);
2713 value->data[0].v_pointer = NULL;
2719 g_value_object_lcopy_value (const GValue *value,
2720 guint n_collect_values,
2721 GTypeCValue *collect_values,
2722 guint collect_flags)
2724 GObject **object_p = collect_values[0].v_pointer;
2727 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2729 if (!value->data[0].v_pointer)
2731 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2732 *object_p = value->data[0].v_pointer;
2734 *object_p = g_object_ref (value->data[0].v_pointer);
2740 * g_value_set_object:
2741 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2742 * @v_object: object value to be set
2744 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2746 * g_value_set_object() increases the reference count of @v_object
2747 * (the #GValue holds a reference to @v_object). If you do not wish
2748 * to increase the reference count of the object (i.e. you wish to
2749 * pass your current reference to the #GValue because you no longer
2750 * need it), use g_value_take_object() instead.
2752 * It is important that your #GValue holds a reference to @v_object (either its
2753 * own, or one it has taken) to ensure that the object won't be destroyed while
2754 * the #GValue still exists).
2757 g_value_set_object (GValue *value,
2762 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2764 old = value->data[0].v_pointer;
2768 g_return_if_fail (G_IS_OBJECT (v_object));
2769 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2771 value->data[0].v_pointer = v_object;
2772 g_object_ref (value->data[0].v_pointer);
2775 value->data[0].v_pointer = NULL;
2778 g_object_unref (old);
2782 * g_value_set_object_take_ownership:
2783 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2784 * @v_object: object value to be set
2786 * This is an internal function introduced mainly for C marshallers.
2788 * Deprecated: 2.4: Use g_value_take_object() instead.
2791 g_value_set_object_take_ownership (GValue *value,
2794 g_value_take_object (value, v_object);
2798 * g_value_take_object:
2799 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2800 * @v_object: object value to be set
2802 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2803 * and takes over the ownership of the callers reference to @v_object;
2804 * the caller doesn't have to unref it any more (i.e. the reference
2805 * count of the object is not increased).
2807 * If you want the #GValue to hold its own reference to @v_object, use
2808 * g_value_set_object() instead.
2813 g_value_take_object (GValue *value,
2816 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2818 if (value->data[0].v_pointer)
2820 g_object_unref (value->data[0].v_pointer);
2821 value->data[0].v_pointer = NULL;
2826 g_return_if_fail (G_IS_OBJECT (v_object));
2827 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2829 value->data[0].v_pointer = v_object; /* we take over the reference count */
2834 * g_value_get_object:
2835 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2837 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2839 * Returns: object contents of @value
2842 g_value_get_object (const GValue *value)
2844 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2846 return value->data[0].v_pointer;
2850 * g_value_dup_object:
2851 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2853 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
2854 * its reference count.
2856 * Returns: object content of @value, should be unreferenced when no
2860 g_value_dup_object (const GValue *value)
2862 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2864 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2868 * g_signal_connect_object:
2869 * @instance: the instance to connect to.
2870 * @detailed_signal: a string of the form "signal-name::detail".
2871 * @c_handler: the #GCallback to connect.
2872 * @gobject: the object to pass as data to @c_handler.
2873 * @connect_flags: a combination of #GConnnectFlags.
2875 * This is similar to g_signal_connect_data(), but uses a closure which
2876 * ensures that the @gobject stays alive during the call to @c_handler
2877 * by temporarily adding a reference count to @gobject.
2879 * Note that there is a bug in GObject that makes this function
2880 * much less useful than it might seem otherwise. Once @gobject is
2881 * disposed, the callback will no longer be called, but, the signal
2882 * handler is <emphasis>not</emphasis> currently disconnected. If the
2883 * @instance is itself being freed at the same time than this doesn't
2884 * matter, since the signal will automatically be removed, but
2885 * if @instance persists, then the signal handler will leak. You
2886 * should not remove the signal yourself because in a future versions of
2887 * GObject, the handler <emphasis>will</emphasis> automatically
2890 * It's possible to work around this problem in a way that will
2891 * continue to work with future versions of GObject by checking
2892 * that the signal handler is still connected before disconnected it:
2893 * <informalexample><programlisting>
2894 * if (g_signal_handler_is_connected (instance, id))
2895 * g_signal_handler_disconnect (instance, id);
2896 * </programlisting></informalexample>
2898 * Returns: the handler id.
2901 g_signal_connect_object (gpointer instance,
2902 const gchar *detailed_signal,
2903 GCallback c_handler,
2905 GConnectFlags connect_flags)
2907 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2908 g_return_val_if_fail (detailed_signal != NULL, 0);
2909 g_return_val_if_fail (c_handler != NULL, 0);
2915 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2917 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2919 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2922 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
2928 GClosure *closures[1]; /* flexible array */
2930 /* don't change this structure without supplying an accessor for
2931 * watched closures, e.g.:
2932 * GSList* g_object_list_watched_closures (GObject *object)
2935 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2936 * carray = g_object_get_data (object, "GObject-closure-array");
2939 * GSList *slist = NULL;
2941 * for (i = 0; i < carray->n_closures; i++)
2942 * slist = g_slist_prepend (slist, carray->closures[i]);
2950 object_remove_closure (gpointer data,
2953 GObject *object = data;
2954 CArray *carray = g_object_get_qdata (object, quark_closure_array);
2957 for (i = 0; i < carray->n_closures; i++)
2958 if (carray->closures[i] == closure)
2960 carray->n_closures--;
2961 if (i < carray->n_closures)
2962 carray->closures[i] = carray->closures[carray->n_closures];
2965 g_assert_not_reached ();
2969 destroy_closure_array (gpointer data)
2971 CArray *carray = data;
2972 GObject *object = carray->object;
2973 guint i, n = carray->n_closures;
2975 for (i = 0; i < n; i++)
2977 GClosure *closure = carray->closures[i];
2979 /* removing object_remove_closure() upfront is probably faster than
2980 * letting it fiddle with quark_closure_array which is empty anyways
2982 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
2983 g_closure_invalidate (closure);
2989 * g_object_watch_closure:
2990 * @object: GObject restricting lifetime of @closure
2991 * @closure: GClosure to watch
2993 * This function essentially limits the life time of the @closure to
2994 * the life time of the object. That is, when the object is finalized,
2995 * the @closure is invalidated by calling g_closure_invalidate() on
2996 * it, in order to prevent invocations of the closure with a finalized
2997 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
2998 * added as marshal guards to the @closure, to ensure that an extra
2999 * reference count is held on @object during invocation of the
3000 * @closure. Usually, this function will be called on closures that
3001 * use this @object as closure data.
3004 g_object_watch_closure (GObject *object,
3010 g_return_if_fail (G_IS_OBJECT (object));
3011 g_return_if_fail (closure != NULL);
3012 g_return_if_fail (closure->is_invalid == FALSE);
3013 g_return_if_fail (closure->in_marshal == FALSE);
3014 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3016 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3017 g_closure_add_marshal_guards (closure,
3018 object, (GClosureNotify) g_object_ref,
3019 object, (GClosureNotify) g_object_unref);
3020 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3023 carray = g_renew (CArray, NULL, 1);
3024 carray->object = object;
3025 carray->n_closures = 1;
3030 i = carray->n_closures++;
3031 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3033 carray->closures[i] = closure;
3034 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3038 * g_closure_new_object:
3039 * @sizeof_closure: the size of the structure to allocate, must be at least
3040 * <literal>sizeof (GClosure)</literal>
3041 * @object: a #GObject pointer to store in the @data field of the newly
3042 * allocated #GClosure
3044 * A variant of g_closure_new_simple() which stores @object in the
3045 * @data field of the closure and calls g_object_watch_closure() on
3046 * @object and the created closure. This function is mainly useful
3047 * when implementing new types of closures.
3049 * Returns: a newly allocated #GClosure
3052 g_closure_new_object (guint sizeof_closure,
3057 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3058 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3060 closure = g_closure_new_simple (sizeof_closure, object);
3061 g_object_watch_closure (object, closure);
3067 * g_cclosure_new_object:
3068 * @callback_func: the function to invoke
3069 * @object: a #GObject pointer to pass to @callback_func
3071 * A variant of g_cclosure_new() which uses @object as @user_data and
3072 * calls g_object_watch_closure() on @object and the created
3073 * closure. This function is useful when you have a callback closely
3074 * associated with a #GObject, and want the callback to no longer run
3075 * after the object is is freed.
3077 * Returns: a new #GCClosure
3080 g_cclosure_new_object (GCallback callback_func,
3085 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3086 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3087 g_return_val_if_fail (callback_func != NULL, NULL);
3089 closure = g_cclosure_new (callback_func, object, NULL);
3090 g_object_watch_closure (object, closure);
3096 * g_cclosure_new_object_swap:
3097 * @callback_func: the function to invoke
3098 * @object: a #GObject pointer to pass to @callback_func
3100 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3101 * and calls g_object_watch_closure() on @object and the created
3102 * closure. This function is useful when you have a callback closely
3103 * associated with a #GObject, and want the callback to no longer run
3104 * after the object is is freed.
3106 * Returns: a new #GCClosure
3109 g_cclosure_new_object_swap (GCallback callback_func,
3114 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3115 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3116 g_return_val_if_fail (callback_func != NULL, NULL);
3118 closure = g_cclosure_new_swap (callback_func, object, NULL);
3119 g_object_watch_closure (object, closure);
3125 g_object_compat_control (gsize what,
3131 case 1: /* floating base type */
3132 return G_TYPE_INITIALLY_UNOWNED;
3133 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3134 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3136 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3138 *pp = floating_flag_handler;
3145 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3148 g_initially_unowned_init (GInitiallyUnowned *object)
3150 g_object_force_floating (object);
3154 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3158 #define __G_OBJECT_C__
3159 #include "gobjectaliasdef.c"