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
117 /* --- signals --- */
124 /* --- properties --- */
130 /* --- prototypes --- */
131 static void g_object_base_class_init (GObjectClass *class);
132 static void g_object_base_class_finalize (GObjectClass *class);
133 static void g_object_do_class_init (GObjectClass *class);
134 static void g_object_init (GObject *object);
135 static GObject* g_object_constructor (GType type,
136 guint n_construct_properties,
137 GObjectConstructParam *construct_params);
138 static void g_object_real_dispose (GObject *object);
139 static void g_object_finalize (GObject *object);
140 static void g_object_do_set_property (GObject *object,
144 static void g_object_do_get_property (GObject *object,
148 static void g_value_object_init (GValue *value);
149 static void g_value_object_free_value (GValue *value);
150 static void g_value_object_copy_value (const GValue *src_value,
152 static void g_value_object_transform_value (const GValue *src_value,
154 static gpointer g_value_object_peek_pointer (const GValue *value);
155 static gchar* g_value_object_collect_value (GValue *value,
156 guint n_collect_values,
157 GTypeCValue *collect_values,
158 guint collect_flags);
159 static gchar* g_value_object_lcopy_value (const GValue *value,
160 guint n_collect_values,
161 GTypeCValue *collect_values,
162 guint collect_flags);
163 static void g_object_dispatch_properties_changed (GObject *object,
165 GParamSpec **pspecs);
166 static inline void object_get_property (GObject *object,
169 static inline void object_set_property (GObject *object,
172 GObjectNotifyQueue *nqueue);
173 static guint object_floating_flag_handler (GObject *object,
176 static void object_interface_check_properties (gpointer func_data,
180 /* --- variables --- */
181 static GQuark quark_closure_array = 0;
182 static GQuark quark_weak_refs = 0;
183 static GQuark quark_toggle_refs = 0;
184 static GParamSpecPool *pspec_pool = NULL;
185 static GObjectNotifyContext property_notify_context = { 0, };
186 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
187 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
188 G_LOCK_DEFINE_STATIC (construction_mutex);
189 static GSList *construction_objects = NULL;
191 /* --- functions --- */
192 #ifdef G_ENABLE_DEBUG
193 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
194 G_LOCK_DEFINE_STATIC (debug_objects);
195 static volatile GObject *g_trap_object_ref = NULL;
196 static guint debug_objects_count = 0;
197 static GHashTable *debug_objects_ht = NULL;
200 debug_objects_foreach (gpointer key,
204 GObject *object = value;
206 g_message ("[%p] stale %s\tref_count=%u",
208 G_OBJECT_TYPE_NAME (object),
213 debug_objects_atexit (void)
217 G_LOCK (debug_objects);
218 g_message ("stale GObjects: %u", debug_objects_count);
219 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
220 G_UNLOCK (debug_objects);
223 #endif /* G_ENABLE_DEBUG */
226 g_object_type_init (void)
228 static gboolean initialized = FALSE;
229 static const GTypeFundamentalInfo finfo = {
230 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
232 static GTypeInfo info = {
233 sizeof (GObjectClass),
234 (GBaseInitFunc) g_object_base_class_init,
235 (GBaseFinalizeFunc) g_object_base_class_finalize,
236 (GClassInitFunc) g_object_do_class_init,
237 NULL /* class_destroy */,
238 NULL /* class_data */,
241 (GInstanceInitFunc) g_object_init,
242 NULL, /* value_table */
244 static const GTypeValueTable value_table = {
245 g_value_object_init, /* value_init */
246 g_value_object_free_value, /* value_free */
247 g_value_object_copy_value, /* value_copy */
248 g_value_object_peek_pointer, /* value_peek_pointer */
249 "p", /* collect_format */
250 g_value_object_collect_value, /* collect_value */
251 "p", /* lcopy_format */
252 g_value_object_lcopy_value, /* lcopy_value */
256 g_return_if_fail (initialized == FALSE);
261 info.value_table = &value_table;
262 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
263 g_assert (type == G_TYPE_OBJECT);
264 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
266 #ifdef G_ENABLE_DEBUG
269 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
270 g_atexit (debug_objects_atexit);
272 #endif /* G_ENABLE_DEBUG */
276 g_object_base_class_init (GObjectClass *class)
278 GObjectClass *pclass = g_type_class_peek_parent (class);
280 /* reset instance specific fields and methods that don't get inherited */
281 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
282 class->get_property = NULL;
283 class->set_property = NULL;
287 g_object_base_class_finalize (GObjectClass *class)
291 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
293 g_slist_free (class->construct_properties);
294 class->construct_properties = NULL;
295 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
296 for (node = list; node; node = node->next)
298 GParamSpec *pspec = node->data;
300 g_param_spec_pool_remove (pspec_pool, pspec);
301 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
302 g_param_spec_unref (pspec);
308 g_object_notify_dispatcher (GObject *object,
312 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
316 g_object_do_class_init (GObjectClass *class)
318 /* read the comment about typedef struct CArray; on why not to change this quark */
319 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
321 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
322 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
323 pspec_pool = g_param_spec_pool_new (TRUE);
324 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
325 property_notify_context.dispatcher = g_object_notify_dispatcher;
327 class->constructor = g_object_constructor;
328 class->set_property = g_object_do_set_property;
329 class->get_property = g_object_do_get_property;
330 class->dispose = g_object_real_dispose;
331 class->finalize = g_object_finalize;
332 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
333 class->notify = NULL;
337 * @pspec: the #GParamSpec of the property which changed
338 * @gobject: the object which received the signal.
340 * The notify signal is emitted on an object when one of its
341 * properties has been changed. Note that getting this signal
342 * doesn't guarantee that the value of the property has actually
343 * changed, it may also be emitted when the setter for the property
344 * is called to reinstate the previous value.
346 * This signal is typically used to obtain change notification for a
347 * single property, by specifying the property name as a detail in the
348 * g_signal_connect() call, like this:
350 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
351 * G_CALLBACK (gtk_text_view_target_list_notify),
354 * It is important to note that you must use
355 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
356 * detail strings for the notify signal.
358 gobject_signals[NOTIFY] =
359 g_signal_new (g_intern_static_string ("notify"),
360 G_TYPE_FROM_CLASS (class),
361 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
362 G_STRUCT_OFFSET (GObjectClass, notify),
364 g_cclosure_marshal_VOID__PARAM,
368 /* Install a check function that we'll use to verify that classes that
369 * implement an interface implement all properties for that interface
371 g_type_add_interface_check (NULL, object_interface_check_properties);
375 install_property_internal (GType g_type,
379 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
381 g_warning ("When installing property: type `%s' already has a property named `%s'",
382 g_type_name (g_type),
387 g_param_spec_ref (pspec);
388 g_param_spec_sink (pspec);
389 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
390 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
394 * g_object_class_install_property:
395 * @oclass: a #GObjectClass
396 * @property_id: the id for the new property
397 * @pspec: the #GParamSpec for the new property
399 * Installs a new property. This is usually done in the class initializer.
402 g_object_class_install_property (GObjectClass *class,
406 g_return_if_fail (G_IS_OBJECT_CLASS (class));
407 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
408 if (pspec->flags & G_PARAM_WRITABLE)
409 g_return_if_fail (class->set_property != NULL);
410 if (pspec->flags & G_PARAM_READABLE)
411 g_return_if_fail (class->get_property != NULL);
412 g_return_if_fail (property_id > 0);
413 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
414 if (pspec->flags & G_PARAM_CONSTRUCT)
415 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
416 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
417 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
419 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
421 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
422 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
424 /* for property overrides of construct poperties, we have to get rid
425 * of the overidden inherited construct property
427 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
428 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
429 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
433 * g_object_interface_install_property:
434 * @g_iface: any interface vtable for the interface, or the default
435 * vtable for the interface.
436 * @pspec: the #GParamSpec for the new property
438 * Add a property to an interface; this is only useful for interfaces
439 * that are added to GObject-derived types. Adding a property to an
440 * interface forces all objects classes with that interface to have a
441 * compatible property. The compatible property could be a newly
442 * created #GParamSpec, but normally
443 * g_object_class_override_property() will be used so that the object
444 * class only needs to provide an implementation and inherits the
445 * property description, default value, bounds, and so forth from the
446 * interface property.
448 * This function is meant to be called from the interface's default
449 * vtable initialization function (the @class_init member of
450 * #GTypeInfo.) It must not be called after after @class_init has
451 * been called for any object types implementing this interface.
456 g_object_interface_install_property (gpointer g_iface,
459 GTypeInterface *iface_class = g_iface;
461 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
462 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
463 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
464 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
466 install_property_internal (iface_class->g_type, 0, pspec);
470 * g_object_class_find_property:
471 * @oclass: a #GObjectClass
472 * @property_name: the name of the property to look up
474 * Looks up the #GParamSpec for a property of a class.
476 * Returns: the #GParamSpec for the property, or %NULL if the class
477 * doesn't have a property of that name
480 g_object_class_find_property (GObjectClass *class,
481 const gchar *property_name)
484 GParamSpec *redirect;
486 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
487 g_return_val_if_fail (property_name != NULL, NULL);
489 pspec = g_param_spec_pool_lookup (pspec_pool,
491 G_OBJECT_CLASS_TYPE (class),
495 redirect = g_param_spec_get_redirect_target (pspec);
506 * g_object_interface_find_property:
507 * @g_iface: any interface vtable for the interface, or the default
508 * vtable for the interface
509 * @property_name: name of a property to lookup.
511 * Find the #GParamSpec with the given name for an
512 * interface. Generally, the interface vtable passed in as @g_iface
513 * will be the default vtable from g_type_default_interface_ref(), or,
514 * if you know the interface has already been loaded,
515 * g_type_default_interface_peek().
519 * Returns: the #GParamSpec for the property of the interface with the
520 * name @property_name, or %NULL if no such property exists.
523 g_object_interface_find_property (gpointer g_iface,
524 const gchar *property_name)
526 GTypeInterface *iface_class = g_iface;
528 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
529 g_return_val_if_fail (property_name != NULL, NULL);
531 return g_param_spec_pool_lookup (pspec_pool,
538 * g_object_class_override_property:
539 * @oclass: a #GObjectClass
540 * @property_id: the new property ID
541 * @name: the name of a property registered in a parent class or
542 * in an interface of this class.
544 * Registers @property_id as referring to a property with the
545 * name @name in a parent class or in an interface implemented
546 * by @oclass. This allows this class to <firstterm>override</firstterm>
547 * a property implementation in a parent class or to provide
548 * the implementation of a property from an interface.
551 * Internally, overriding is implemented by creating a property of type
552 * #GParamSpecOverride; generally operations that query the properties of
553 * the object class, such as g_object_class_find_property() or
554 * g_object_class_list_properties() will return the overridden
555 * property. However, in one case, the @construct_properties argument of
556 * the @constructor virtual function, the #GParamSpecOverride is passed
557 * instead, so that the @param_id field of the #GParamSpec will be
558 * correct. For virtually all uses, this makes no difference. If you
559 * need to get the overridden property, you can call
560 * g_param_spec_get_redirect_target().
566 g_object_class_override_property (GObjectClass *oclass,
570 GParamSpec *overridden = NULL;
574 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
575 g_return_if_fail (property_id > 0);
576 g_return_if_fail (name != NULL);
578 /* Find the overridden property; first check parent types
580 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
581 if (parent_type != G_TYPE_NONE)
582 overridden = g_param_spec_pool_lookup (pspec_pool,
591 /* Now check interfaces
593 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
594 while (n_ifaces-- && !overridden)
596 overridden = g_param_spec_pool_lookup (pspec_pool,
607 g_warning ("%s: Can't find property to override for '%s::%s'",
608 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
612 new = g_param_spec_override (name, overridden);
613 g_object_class_install_property (oclass, property_id, new);
617 * g_object_class_list_properties:
618 * @oclass: a #GObjectClass
619 * @n_properties: return location for the length of the returned array
621 * Get an array of #GParamSpec* for all properties of a class.
623 * Returns: an array of #GParamSpec* which should be freed after use
625 GParamSpec** /* free result */
626 g_object_class_list_properties (GObjectClass *class,
627 guint *n_properties_p)
632 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
634 pspecs = g_param_spec_pool_list (pspec_pool,
635 G_OBJECT_CLASS_TYPE (class),
644 * g_object_interface_list_properties:
645 * @g_iface: any interface vtable for the interface, or the default
646 * vtable for the interface
647 * @n_properties_p: location to store number of properties returned.
649 * Lists the properties of an interface.Generally, the interface
650 * vtable passed in as @g_iface will be the default vtable from
651 * g_type_default_interface_ref(), or, if you know the interface has
652 * already been loaded, g_type_default_interface_peek().
656 * Returns: a pointer to an array of pointers to #GParamSpec
657 * structures. The paramspecs are owned by GLib, but the
658 * array should be freed with g_free() when you are done with
662 g_object_interface_list_properties (gpointer g_iface,
663 guint *n_properties_p)
665 GTypeInterface *iface_class = g_iface;
669 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
671 pspecs = g_param_spec_pool_list (pspec_pool,
681 g_object_init (GObject *object)
683 object->ref_count = 1;
684 g_datalist_init (&object->qdata);
686 /* freeze object's notification queue, g_object_newv() preserves pairedness */
687 g_object_notify_queue_freeze (object, &property_notify_context);
688 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
689 G_LOCK (construction_mutex);
690 construction_objects = g_slist_prepend (construction_objects, object);
691 G_UNLOCK (construction_mutex);
693 #ifdef G_ENABLE_DEBUG
696 G_LOCK (debug_objects);
697 debug_objects_count++;
698 g_hash_table_insert (debug_objects_ht, object, object);
699 G_UNLOCK (debug_objects);
701 #endif /* G_ENABLE_DEBUG */
705 g_object_do_set_property (GObject *object,
713 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
719 g_object_do_get_property (GObject *object,
727 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
733 g_object_real_dispose (GObject *object)
735 g_signal_handlers_destroy (object);
736 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
737 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
741 g_object_finalize (GObject *object)
743 g_datalist_clear (&object->qdata);
745 #ifdef G_ENABLE_DEBUG
748 G_LOCK (debug_objects);
749 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
750 g_hash_table_remove (debug_objects_ht, object);
751 debug_objects_count--;
752 G_UNLOCK (debug_objects);
754 #endif /* G_ENABLE_DEBUG */
759 g_object_dispatch_properties_changed (GObject *object,
765 for (i = 0; i < n_pspecs; i++)
766 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
770 * g_object_run_dispose:
771 * @object: a #GObject
773 * Releases all references to other objects. This can be used to break
776 * This functions should only be called from object system implementations.
779 g_object_run_dispose (GObject *object)
781 g_return_if_fail (G_IS_OBJECT (object));
782 g_return_if_fail (object->ref_count > 0);
784 g_object_ref (object);
785 G_OBJECT_GET_CLASS (object)->dispose (object);
786 g_object_unref (object);
790 * g_object_freeze_notify:
791 * @object: a #GObject
793 * Increases the freeze count on @object. If the freeze count is
794 * non-zero, the emission of "notify" signals on @object is
795 * stopped. The signals are queued until the freeze count is decreased
798 * This is necessary for accessors that modify multiple properties to prevent
799 * premature notification while the object is still being modified.
802 g_object_freeze_notify (GObject *object)
804 g_return_if_fail (G_IS_OBJECT (object));
806 if (g_atomic_int_get (&object->ref_count) == 0)
809 g_object_ref (object);
810 g_object_notify_queue_freeze (object, &property_notify_context);
811 g_object_unref (object);
816 * @object: a #GObject
817 * @property_name: the name of a property installed on the class of @object.
819 * Emits a "notify" signal for the property @property_name on @object.
822 g_object_notify (GObject *object,
823 const gchar *property_name)
827 g_return_if_fail (G_IS_OBJECT (object));
828 g_return_if_fail (property_name != NULL);
829 if (g_atomic_int_get (&object->ref_count) == 0)
832 g_object_ref (object);
833 /* We don't need to get the redirect target
834 * (by, e.g. calling g_object_class_find_property())
835 * because g_object_notify_queue_add() does that
837 pspec = g_param_spec_pool_lookup (pspec_pool,
839 G_OBJECT_TYPE (object),
843 g_warning ("%s: object class `%s' has no property named `%s'",
845 G_OBJECT_TYPE_NAME (object),
849 GObjectNotifyQueue *nqueue;
851 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
852 g_object_notify_queue_add (object, nqueue, pspec);
853 g_object_notify_queue_thaw (object, nqueue);
855 g_object_unref (object);
859 * g_object_thaw_notify:
860 * @object: a #GObject
862 * Reverts the effect of a previous call to
863 * g_object_freeze_notify(). The freeze count is decreased on @object
864 * and when it reaches zero, all queued "notify" signals are emitted.
866 * It is an error to call this function when the freeze count is zero.
869 g_object_thaw_notify (GObject *object)
871 GObjectNotifyQueue *nqueue;
873 g_return_if_fail (G_IS_OBJECT (object));
874 if (g_atomic_int_get (&object->ref_count) == 0)
877 g_object_ref (object);
878 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
879 if (!nqueue || !nqueue->freeze_count)
880 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
881 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
883 g_object_notify_queue_thaw (object, nqueue);
884 g_object_unref (object);
888 object_get_property (GObject *object,
892 GObjectClass *class = g_type_class_peek (pspec->owner_type);
893 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
894 GParamSpec *redirect;
896 redirect = g_param_spec_get_redirect_target (pspec);
900 class->get_property (object, param_id, value, pspec);
904 object_set_property (GObject *object,
907 GObjectNotifyQueue *nqueue)
909 GValue tmp_value = { 0, };
910 GObjectClass *class = g_type_class_peek (pspec->owner_type);
911 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
912 GParamSpec *redirect;
914 redirect = g_param_spec_get_redirect_target (pspec);
918 /* provide a copy to work from, convert (if necessary) and validate */
919 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
920 if (!g_value_transform (value, &tmp_value))
921 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
923 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
924 G_VALUE_TYPE_NAME (value));
925 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
927 gchar *contents = g_strdup_value_contents (value);
929 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
931 G_VALUE_TYPE_NAME (value),
933 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
938 class->set_property (object, param_id, &tmp_value, pspec);
939 g_object_notify_queue_add (object, nqueue, pspec);
941 g_value_unset (&tmp_value);
945 object_interface_check_properties (gpointer func_data,
948 GTypeInterface *iface_class = g_iface;
949 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
950 GType iface_type = iface_class->g_type;
954 if (!G_IS_OBJECT_CLASS (class))
957 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
961 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
963 G_OBJECT_CLASS_TYPE (class),
968 g_critical ("Object class %s doesn't implement property "
969 "'%s' from interface '%s'",
970 g_type_name (G_OBJECT_CLASS_TYPE (class)),
972 g_type_name (iface_type));
977 /* The implementation paramspec must have a less restrictive
978 * type than the interface parameter spec for set() and a
979 * more restrictive type for get(). We just require equality,
980 * rather than doing something more complicated checking
981 * the READABLE and WRITABLE flags. We also simplify here
982 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
985 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
986 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
988 g_critical ("Property '%s' on class '%s' has type '%s' "
989 "which is different from the type '%s', "
990 "of the property on interface '%s'\n",
992 g_type_name (G_OBJECT_CLASS_TYPE (class)),
993 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
994 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
995 g_type_name (iface_type));
998 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1000 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1001 * READABLE and WRITABLE remove restrictions. The implementation
1002 * paramspec must have less restrictive flags.
1005 (!SUBSET (class_pspec->flags,
1007 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1008 !SUBSET (pspecs[n]->flags,
1010 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1012 g_critical ("Flags for property '%s' on class '%s' "
1013 "are not compatible with the property on"
1016 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1017 g_type_name (iface_type));
1026 g_object_get_type (void)
1028 return G_TYPE_OBJECT;
1033 * @object_type: the type id of the #GObject subtype to instantiate
1034 * @first_property_name: the name of the first property
1035 * @...: the value of the first property, followed optionally by more
1036 * name/value pairs, followed by %NULL
1038 * Creates a new instance of a #GObject subtype and sets its properties.
1040 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1041 * which are not explicitly specified are set to their default values.
1043 * Returns: a new instance of @object_type
1046 g_object_new (GType object_type,
1047 const gchar *first_property_name,
1053 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1055 va_start (var_args, first_property_name);
1056 object = g_object_new_valist (object_type, first_property_name, var_args);
1063 slist_maybe_remove (GSList **slist,
1066 GSList *last = NULL, *node = *slist;
1069 if (node->data == data)
1072 last->next = node->next;
1074 *slist = node->next;
1075 g_slist_free_1 (node);
1084 static inline gboolean
1085 object_in_construction_list (GObject *object)
1087 gboolean in_construction;
1088 G_LOCK (construction_mutex);
1089 in_construction = g_slist_find (construction_objects, object) != NULL;
1090 G_UNLOCK (construction_mutex);
1091 return in_construction;
1096 * @object_type: the type id of the #GObject subtype to instantiate
1097 * @n_parameters: the length of the @parameters array
1098 * @parameters: an array of #GParameter
1100 * Creates a new instance of a #GObject subtype and sets its properties.
1102 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1103 * which are not explicitly specified are set to their default values.
1105 * Returns: a new instance of @object_type
1108 g_object_newv (GType object_type,
1110 GParameter *parameters)
1112 GObjectConstructParam *cparams, *oparams;
1113 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1115 GObjectClass *class, *unref_class = NULL;
1117 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1119 GList *clist = NULL;
1120 gboolean newly_constructed;
1123 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1125 class = g_type_class_peek_static (object_type);
1127 class = unref_class = g_type_class_ref (object_type);
1128 for (slist = class->construct_properties; slist; slist = slist->next)
1130 clist = g_list_prepend (clist, slist->data);
1131 n_total_cparams += 1;
1134 /* collect parameters, sort into construction and normal ones */
1135 oparams = g_new (GObjectConstructParam, n_parameters);
1136 cparams = g_new (GObjectConstructParam, n_total_cparams);
1137 for (i = 0; i < n_parameters; i++)
1139 GValue *value = ¶meters[i].value;
1140 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1146 g_warning ("%s: object class `%s' has no property named `%s'",
1148 g_type_name (object_type),
1149 parameters[i].name);
1152 if (!(pspec->flags & G_PARAM_WRITABLE))
1154 g_warning ("%s: property `%s' of object class `%s' is not writable",
1157 g_type_name (object_type));
1160 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1162 GList *list = g_list_find (clist, pspec);
1166 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1167 G_STRFUNC, pspec->name, g_type_name (object_type));
1170 cparams[n_cparams].pspec = pspec;
1171 cparams[n_cparams].value = value;
1176 list->prev->next = list->next;
1178 list->next->prev = list->prev;
1179 g_list_free_1 (list);
1183 oparams[n_oparams].pspec = pspec;
1184 oparams[n_oparams].value = value;
1189 /* set remaining construction properties to default values */
1190 n_cvalues = n_total_cparams - n_cparams;
1191 cvalues = g_new (GValue, n_cvalues);
1194 GList *tmp = clist->next;
1195 GParamSpec *pspec = clist->data;
1196 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1199 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1200 g_param_value_set_default (pspec, value);
1202 cparams[n_cparams].pspec = pspec;
1203 cparams[n_cparams].value = value;
1206 g_list_free_1 (clist);
1210 /* construct object from construction parameters */
1211 object = class->constructor (object_type, n_total_cparams, cparams);
1212 /* free construction values */
1215 g_value_unset (cvalues + n_cvalues);
1218 /* adjust freeze_count according to g_object_init() and remaining properties */
1219 G_LOCK (construction_mutex);
1220 newly_constructed = slist_maybe_remove (&construction_objects, object);
1221 G_UNLOCK (construction_mutex);
1222 if (newly_constructed || n_oparams)
1223 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1224 if (newly_constructed)
1225 g_object_notify_queue_thaw (object, nqueue);
1227 /* run 'constructed' handler if there is one */
1228 if (newly_constructed && class->constructed)
1229 class->constructed (object);
1231 /* set remaining properties */
1232 for (i = 0; i < n_oparams; i++)
1233 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1236 /* release our own freeze count and handle notifications */
1237 if (newly_constructed || n_oparams)
1238 g_object_notify_queue_thaw (object, nqueue);
1241 g_type_class_unref (unref_class);
1247 * g_object_new_valist:
1248 * @object_type: the type id of the #GObject subtype to instantiate
1249 * @first_property_name: the name of the first property
1250 * @var_args: the value of the first property, followed optionally by more
1251 * name/value pairs, followed by %NULL
1253 * Creates a new instance of a #GObject subtype and sets its properties.
1255 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1256 * which are not explicitly specified are set to their default values.
1258 * Returns: a new instance of @object_type
1261 g_object_new_valist (GType object_type,
1262 const gchar *first_property_name,
1265 GObjectClass *class;
1269 guint n_params = 0, n_alloced_params = 16;
1271 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1273 if (!first_property_name)
1274 return g_object_newv (object_type, 0, NULL);
1276 class = g_type_class_ref (object_type);
1278 params = g_new (GParameter, n_alloced_params);
1279 name = first_property_name;
1282 gchar *error = NULL;
1283 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1289 g_warning ("%s: object class `%s' has no property named `%s'",
1291 g_type_name (object_type),
1295 if (n_params >= n_alloced_params)
1297 n_alloced_params += 16;
1298 params = g_renew (GParameter, params, n_alloced_params);
1300 params[n_params].name = name;
1301 params[n_params].value.g_type = 0;
1302 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1303 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1306 g_warning ("%s: %s", G_STRFUNC, error);
1308 g_value_unset (¶ms[n_params].value);
1312 name = va_arg (var_args, gchar*);
1315 object = g_object_newv (object_type, n_params, params);
1318 g_value_unset (¶ms[n_params].value);
1321 g_type_class_unref (class);
1327 g_object_constructor (GType type,
1328 guint n_construct_properties,
1329 GObjectConstructParam *construct_params)
1334 object = (GObject*) g_type_create_instance (type);
1336 /* set construction parameters */
1337 if (n_construct_properties)
1339 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1341 /* set construct properties */
1342 while (n_construct_properties--)
1344 GValue *value = construct_params->value;
1345 GParamSpec *pspec = construct_params->pspec;
1348 object_set_property (object, pspec, value, nqueue);
1350 g_object_notify_queue_thaw (object, nqueue);
1351 /* the notification queue is still frozen from g_object_init(), so
1352 * we don't need to handle it here, g_object_newv() takes
1361 * g_object_set_valist:
1362 * @object: a #GObject
1363 * @first_property_name: name of the first property to set
1364 * @var_args: value for the first property, followed optionally by more
1365 * name/value pairs, followed by %NULL
1367 * Sets properties on an object.
1370 g_object_set_valist (GObject *object,
1371 const gchar *first_property_name,
1374 GObjectNotifyQueue *nqueue;
1377 g_return_if_fail (G_IS_OBJECT (object));
1379 g_object_ref (object);
1380 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1382 name = first_property_name;
1385 GValue value = { 0, };
1387 gchar *error = NULL;
1389 pspec = g_param_spec_pool_lookup (pspec_pool,
1391 G_OBJECT_TYPE (object),
1395 g_warning ("%s: object class `%s' has no property named `%s'",
1397 G_OBJECT_TYPE_NAME (object),
1401 if (!(pspec->flags & G_PARAM_WRITABLE))
1403 g_warning ("%s: property `%s' of object class `%s' is not writable",
1406 G_OBJECT_TYPE_NAME (object));
1409 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1411 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1412 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1416 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1418 G_VALUE_COLLECT (&value, var_args, 0, &error);
1421 g_warning ("%s: %s", G_STRFUNC, error);
1423 g_value_unset (&value);
1427 object_set_property (object, pspec, &value, nqueue);
1428 g_value_unset (&value);
1430 name = va_arg (var_args, gchar*);
1433 g_object_notify_queue_thaw (object, nqueue);
1434 g_object_unref (object);
1438 * g_object_get_valist:
1439 * @object: a #GObject
1440 * @first_property_name: name of the first property to get
1441 * @var_args: return location for the first property, followed optionally by more
1442 * name/return location pairs, followed by %NULL
1444 * Gets properties of an object.
1446 * In general, a copy is made of the property contents and the caller
1447 * is responsible for freeing the memory in the appropriate manner for
1448 * the type, for instance by calling g_free() or g_object_unref().
1450 * See g_object_get().
1453 g_object_get_valist (GObject *object,
1454 const gchar *first_property_name,
1459 g_return_if_fail (G_IS_OBJECT (object));
1461 g_object_ref (object);
1463 name = first_property_name;
1467 GValue value = { 0, };
1471 pspec = g_param_spec_pool_lookup (pspec_pool,
1473 G_OBJECT_TYPE (object),
1477 g_warning ("%s: object class `%s' has no property named `%s'",
1479 G_OBJECT_TYPE_NAME (object),
1483 if (!(pspec->flags & G_PARAM_READABLE))
1485 g_warning ("%s: property `%s' of object class `%s' is not readable",
1488 G_OBJECT_TYPE_NAME (object));
1492 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1494 object_get_property (object, pspec, &value);
1496 G_VALUE_LCOPY (&value, var_args, 0, &error);
1499 g_warning ("%s: %s", G_STRFUNC, error);
1501 g_value_unset (&value);
1505 g_value_unset (&value);
1507 name = va_arg (var_args, gchar*);
1510 g_object_unref (object);
1515 * @object: a #GObject
1516 * @first_property_name: name of the first property to set
1517 * @...: value for the first property, followed optionally by more
1518 * name/value pairs, followed by %NULL
1520 * Sets properties on an object.
1523 g_object_set (gpointer _object,
1524 const gchar *first_property_name,
1527 GObject *object = _object;
1530 g_return_if_fail (G_IS_OBJECT (object));
1532 va_start (var_args, first_property_name);
1533 g_object_set_valist (object, first_property_name, var_args);
1539 * @object: a #GObject
1540 * @first_property_name: name of the first property to get
1541 * @...: return location for the first property, followed optionally by more
1542 * name/return location pairs, followed by %NULL
1544 * Gets properties of an object.
1546 * In general, a copy is made of the property contents and the caller
1547 * is responsible for freeing the memory in the appropriate manner for
1548 * the type, for instance by calling g_free() or g_object_unref().
1551 * <title>Using g_object_get(<!-- -->)</title>
1552 * An example of using g_object_get() to get the contents
1553 * of three properties - one of type #G_TYPE_INT,
1554 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1560 * g_object_get (my_object,
1561 * "int-property", &intval,
1562 * "str-property", &strval,
1563 * "obj-property", &objval,
1566 * // Do something with intval, strval, objval
1569 * g_object_unref (objval);
1574 g_object_get (gpointer _object,
1575 const gchar *first_property_name,
1578 GObject *object = _object;
1581 g_return_if_fail (G_IS_OBJECT (object));
1583 va_start (var_args, first_property_name);
1584 g_object_get_valist (object, first_property_name, var_args);
1589 * g_object_set_property:
1590 * @object: a #GObject
1591 * @property_name: the name of the property to set
1594 * Sets a property on an object.
1597 g_object_set_property (GObject *object,
1598 const gchar *property_name,
1599 const GValue *value)
1601 GObjectNotifyQueue *nqueue;
1604 g_return_if_fail (G_IS_OBJECT (object));
1605 g_return_if_fail (property_name != NULL);
1606 g_return_if_fail (G_IS_VALUE (value));
1608 g_object_ref (object);
1609 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1611 pspec = g_param_spec_pool_lookup (pspec_pool,
1613 G_OBJECT_TYPE (object),
1616 g_warning ("%s: object class `%s' has no property named `%s'",
1618 G_OBJECT_TYPE_NAME (object),
1620 else if (!(pspec->flags & G_PARAM_WRITABLE))
1621 g_warning ("%s: property `%s' of object class `%s' is not writable",
1624 G_OBJECT_TYPE_NAME (object));
1625 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1626 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1627 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1629 object_set_property (object, pspec, value, nqueue);
1631 g_object_notify_queue_thaw (object, nqueue);
1632 g_object_unref (object);
1636 * g_object_get_property:
1637 * @object: a #GObject
1638 * @property_name: the name of the property to get
1639 * @value: return location for the property value
1641 * Gets a property of an object.
1643 * In general, a copy is made of the property contents and the caller is
1644 * responsible for freeing the memory by calling g_value_unset().
1646 * Note that g_object_get_property() is really intended for language
1647 * bindings, g_object_get() is much more convenient for C programming.
1650 g_object_get_property (GObject *object,
1651 const gchar *property_name,
1656 g_return_if_fail (G_IS_OBJECT (object));
1657 g_return_if_fail (property_name != NULL);
1658 g_return_if_fail (G_IS_VALUE (value));
1660 g_object_ref (object);
1662 pspec = g_param_spec_pool_lookup (pspec_pool,
1664 G_OBJECT_TYPE (object),
1667 g_warning ("%s: object class `%s' has no property named `%s'",
1669 G_OBJECT_TYPE_NAME (object),
1671 else if (!(pspec->flags & G_PARAM_READABLE))
1672 g_warning ("%s: property `%s' of object class `%s' is not readable",
1675 G_OBJECT_TYPE_NAME (object));
1678 GValue *prop_value, tmp_value = { 0, };
1680 /* auto-conversion of the callers value type
1682 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1684 g_value_reset (value);
1687 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1689 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1690 G_STRFUNC, pspec->name,
1691 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1692 G_VALUE_TYPE_NAME (value));
1693 g_object_unref (object);
1698 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1699 prop_value = &tmp_value;
1701 object_get_property (object, pspec, prop_value);
1702 if (prop_value != value)
1704 g_value_transform (prop_value, value);
1705 g_value_unset (&tmp_value);
1709 g_object_unref (object);
1714 * @object: a #GObject
1715 * @signal_spec: the spec for the first signal
1716 * @...: #GCallback for the first signal, followed by data for the
1717 * first signal, followed optionally by more signal
1718 * spec/callback/data triples, followed by %NULL
1720 * A convenience function to connect multiple signals at once.
1722 * The signal specs expected by this function have the form
1723 * "modifier::signal_name", where modifier can be one of the following:
1726 * <term>signal</term>
1728 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1729 * </para></listitem>
1732 * <term>object_signal</term>
1733 * <term>object-signal</term>
1735 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1736 * </para></listitem>
1739 * <term>swapped_signal</term>
1740 * <term>swapped-signal</term>
1742 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1743 * </para></listitem>
1746 * <term>swapped_object_signal</term>
1747 * <term>swapped-object-signal</term>
1749 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1750 * </para></listitem>
1753 * <term>signal_after</term>
1754 * <term>signal-after</term>
1756 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1757 * </para></listitem>
1760 * <term>object_signal_after</term>
1761 * <term>object-signal-after</term>
1763 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1764 * </para></listitem>
1767 * <term>swapped_signal_after</term>
1768 * <term>swapped-signal-after</term>
1770 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1771 * </para></listitem>
1774 * <term>swapped_object_signal_after</term>
1775 * <term>swapped-object-signal-after</term>
1777 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1778 * </para></listitem>
1783 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1784 * "type", GTK_WINDOW_POPUP,
1787 * "signal::event", gtk_menu_window_event, menu,
1788 * "signal::size_request", gtk_menu_window_size_request, menu,
1789 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1796 g_object_connect (gpointer _object,
1797 const gchar *signal_spec,
1800 GObject *object = _object;
1803 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1804 g_return_val_if_fail (object->ref_count > 0, object);
1806 va_start (var_args, signal_spec);
1809 GCallback callback = va_arg (var_args, GCallback);
1810 gpointer data = va_arg (var_args, gpointer);
1813 if (strncmp (signal_spec, "signal::", 8) == 0)
1814 sid = g_signal_connect_data (object, signal_spec + 8,
1815 callback, data, NULL,
1817 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1818 strncmp (signal_spec, "object-signal::", 15) == 0)
1819 sid = g_signal_connect_object (object, signal_spec + 15,
1822 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1823 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1824 sid = g_signal_connect_data (object, signal_spec + 16,
1825 callback, data, NULL,
1827 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1828 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1829 sid = g_signal_connect_object (object, signal_spec + 23,
1832 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1833 strncmp (signal_spec, "signal-after::", 14) == 0)
1834 sid = g_signal_connect_data (object, signal_spec + 14,
1835 callback, data, NULL,
1837 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1838 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1839 sid = g_signal_connect_object (object, signal_spec + 21,
1842 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1843 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1844 sid = g_signal_connect_data (object, signal_spec + 22,
1845 callback, data, NULL,
1846 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1847 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1848 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1849 sid = g_signal_connect_object (object, signal_spec + 29,
1851 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1854 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1857 signal_spec = va_arg (var_args, gchar*);
1865 * g_object_disconnect:
1866 * @object: a #GObject
1867 * @signal_spec: the spec for the first signal
1868 * @...: #GCallback for the first signal, followed by data for the first signal,
1869 * followed optionally by more signal spec/callback/data triples,
1872 * A convenience function to disconnect multiple signals at once.
1874 * The signal specs expected by this function have the form
1875 * "any_signal", which means to disconnect any signal with matching
1876 * callback and data, or "any_signal::signal_name", which only
1877 * disconnects the signal named "signal_name".
1880 g_object_disconnect (gpointer _object,
1881 const gchar *signal_spec,
1884 GObject *object = _object;
1887 g_return_if_fail (G_IS_OBJECT (object));
1888 g_return_if_fail (object->ref_count > 0);
1890 va_start (var_args, signal_spec);
1893 GCallback callback = va_arg (var_args, GCallback);
1894 gpointer data = va_arg (var_args, gpointer);
1895 guint sid = 0, detail = 0, mask = 0;
1897 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1898 strncmp (signal_spec, "any-signal::", 12) == 0)
1901 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1903 else if (strcmp (signal_spec, "any_signal") == 0 ||
1904 strcmp (signal_spec, "any-signal") == 0)
1907 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1911 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1915 if ((mask & G_SIGNAL_MATCH_ID) &&
1916 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1917 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1918 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1920 NULL, (gpointer)callback, data))
1921 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1922 signal_spec = va_arg (var_args, gchar*);
1933 } weak_refs[1]; /* flexible array */
1937 weak_refs_notify (gpointer data)
1939 WeakRefStack *wstack = data;
1942 for (i = 0; i < wstack->n_weak_refs; i++)
1943 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1948 * g_object_weak_ref:
1949 * @object: #GObject to reference weakly
1950 * @notify: callback to invoke before the object is freed
1951 * @data: extra data to pass to notify
1953 * Adds a weak reference callback to an object. Weak references are
1954 * used for notification when an object is finalized. They are called
1955 * "weak references" because they allow you to safely hold a pointer
1956 * to an object without calling g_object_ref() (g_object_ref() adds a
1957 * strong reference, that is, forces the object to stay alive).
1960 g_object_weak_ref (GObject *object,
1964 WeakRefStack *wstack;
1967 g_return_if_fail (G_IS_OBJECT (object));
1968 g_return_if_fail (notify != NULL);
1969 g_return_if_fail (object->ref_count >= 1);
1971 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1974 i = wstack->n_weak_refs++;
1975 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1979 wstack = g_renew (WeakRefStack, NULL, 1);
1980 wstack->object = object;
1981 wstack->n_weak_refs = 1;
1984 wstack->weak_refs[i].notify = notify;
1985 wstack->weak_refs[i].data = data;
1986 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1990 * g_object_weak_unref:
1991 * @object: #GObject to remove a weak reference from
1992 * @notify: callback to search for
1993 * @data: data to search for
1995 * Removes a weak reference callback to an object.
1998 g_object_weak_unref (GObject *object,
2002 WeakRefStack *wstack;
2003 gboolean found_one = FALSE;
2005 g_return_if_fail (G_IS_OBJECT (object));
2006 g_return_if_fail (notify != NULL);
2008 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2013 for (i = 0; i < wstack->n_weak_refs; i++)
2014 if (wstack->weak_refs[i].notify == notify &&
2015 wstack->weak_refs[i].data == data)
2018 wstack->n_weak_refs -= 1;
2019 if (i != wstack->n_weak_refs)
2020 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2026 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2030 * g_object_add_weak_pointer:
2031 * @object: The object that should be weak referenced.
2032 * @weak_pointer_location: The memory address of a pointer.
2034 * Adds a weak reference from weak_pointer to @object to indicate that
2035 * the pointer located at @weak_pointer_location is only valid during
2036 * the lifetime of @object. When the @object is finalized,
2037 * @weak_pointer will be set to %NULL.
2040 g_object_add_weak_pointer (GObject *object,
2041 gpointer *weak_pointer_location)
2043 g_return_if_fail (G_IS_OBJECT (object));
2044 g_return_if_fail (weak_pointer_location != NULL);
2046 g_object_weak_ref (object,
2047 (GWeakNotify) g_nullify_pointer,
2048 weak_pointer_location);
2052 * g_object_remove_weak_pointer:
2053 * @object: The object that is weak referenced.
2054 * @weak_pointer_location: The memory address of a pointer.
2056 * Removes a weak reference from @object that was previously added
2057 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2058 * to match the one used with g_object_add_weak_pointer().
2061 g_object_remove_weak_pointer (GObject *object,
2062 gpointer *weak_pointer_location)
2064 g_return_if_fail (G_IS_OBJECT (object));
2065 g_return_if_fail (weak_pointer_location != NULL);
2067 g_object_weak_unref (object,
2068 (GWeakNotify) g_nullify_pointer,
2069 weak_pointer_location);
2073 object_floating_flag_handler (GObject *object,
2079 case +1: /* force floating if possible */
2081 oldvalue = g_atomic_pointer_get (&object->qdata);
2082 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2083 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2084 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2085 case -1: /* sink if possible */
2087 oldvalue = g_atomic_pointer_get (&object->qdata);
2088 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2089 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2090 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2091 default: /* check floating */
2092 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2097 * g_object_is_floating:
2098 * @object: a #GObject
2100 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2105 * Returns: %TRUE if @object has a floating reference
2108 g_object_is_floating (gpointer _object)
2110 GObject *object = _object;
2111 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2112 return floating_flag_handler (object, 0);
2116 * g_object_ref_sink:
2117 * @object: a #GObject
2119 * Increase the reference count of @object, and possibly remove the
2120 * <link linkend="floating-ref">floating</link> reference, if @object
2121 * has a floating reference.
2123 * In other words, if the object is floating, then this call "assumes
2124 * ownership" of the floating reference, converting it to a normal
2125 * reference by clearing the floating flag while leaving the reference
2126 * count unchanged. If the object is not floating, then this call
2127 * adds a new normal reference increasing the reference count by one.
2134 g_object_ref_sink (gpointer _object)
2136 GObject *object = _object;
2137 gboolean was_floating;
2138 g_return_val_if_fail (G_IS_OBJECT (object), object);
2139 g_return_val_if_fail (object->ref_count >= 1, object);
2140 g_object_ref (object);
2141 was_floating = floating_flag_handler (object, -1);
2143 g_object_unref (object);
2148 * g_object_force_floating:
2149 * @object: a #GObject
2151 * This function is intended for #GObject implementations to re-enforce a
2152 * <link linkend="floating-ref">floating</link> object reference.
2153 * Doing this is seldomly required, all
2154 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2155 * usually just needs to be sunken by calling g_object_ref_sink().
2160 g_object_force_floating (GObject *object)
2162 gboolean was_floating;
2163 g_return_if_fail (G_IS_OBJECT (object));
2164 g_return_if_fail (object->ref_count >= 1);
2166 was_floating = floating_flag_handler (object, +1);
2171 guint n_toggle_refs;
2173 GToggleNotify notify;
2175 } toggle_refs[1]; /* flexible array */
2179 toggle_refs_notify (GObject *object,
2180 gboolean is_last_ref)
2182 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2184 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2185 * will only be notified when there is exactly one of them.
2187 g_assert (tstack->n_toggle_refs == 1);
2188 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2192 * g_object_add_toggle_ref:
2193 * @object: a #GObject
2194 * @notify: a function to call when this reference is the
2195 * last reference to the object, or is no longer
2196 * the last reference.
2197 * @data: data to pass to @notify
2199 * Increases the reference count of the object by one and sets a
2200 * callback to be called when all other references to the object are
2201 * dropped, or when this is already the last reference to the object
2202 * and another reference is established.
2204 * This functionality is intended for binding @object to a proxy
2205 * object managed by another memory manager. This is done with two
2206 * paired references: the strong reference added by
2207 * g_object_add_toggle_ref() and a reverse reference to the proxy
2208 * object which is either a strong reference or weak reference.
2210 * The setup is that when there are no other references to @object,
2211 * only a weak reference is held in the reverse direction from @object
2212 * to the proxy object, but when there are other references held to
2213 * @object, a strong reference is held. The @notify callback is called
2214 * when the reference from @object to the proxy object should be
2215 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2216 * true) or weak to strong (@is_last_ref false).
2218 * Since a (normal) reference must be held to the object before
2219 * calling g_object_toggle_ref(), the initial state of the reverse
2220 * link is always strong.
2222 * Multiple toggle references may be added to the same gobject,
2223 * however if there are multiple toggle references to an object, none
2224 * of them will ever be notified until all but one are removed. For
2225 * this reason, you should only ever use a toggle reference if there
2226 * is important state in the proxy object.
2231 g_object_add_toggle_ref (GObject *object,
2232 GToggleNotify notify,
2235 ToggleRefStack *tstack;
2238 g_return_if_fail (G_IS_OBJECT (object));
2239 g_return_if_fail (notify != NULL);
2240 g_return_if_fail (object->ref_count >= 1);
2242 g_object_ref (object);
2244 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2247 i = tstack->n_toggle_refs++;
2248 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2249 * in tstate->toggle_refs */
2250 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2254 tstack = g_renew (ToggleRefStack, NULL, 1);
2255 tstack->object = object;
2256 tstack->n_toggle_refs = 1;
2260 /* Set a flag for fast lookup after adding the first toggle reference */
2261 if (tstack->n_toggle_refs == 1)
2262 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2264 tstack->toggle_refs[i].notify = notify;
2265 tstack->toggle_refs[i].data = data;
2266 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2267 (GDestroyNotify)g_free);
2271 * g_object_remove_toggle_ref:
2272 * @object: a #GObject
2273 * @notify: a function to call when this reference is the
2274 * last reference to the object, or is no longer
2275 * the last reference.
2276 * @data: data to pass to @notify
2278 * Removes a reference added with g_object_add_toggle_ref(). The
2279 * reference count of the object is decreased by one.
2284 g_object_remove_toggle_ref (GObject *object,
2285 GToggleNotify notify,
2288 ToggleRefStack *tstack;
2289 gboolean found_one = FALSE;
2291 g_return_if_fail (G_IS_OBJECT (object));
2292 g_return_if_fail (notify != NULL);
2294 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2299 for (i = 0; i < tstack->n_toggle_refs; i++)
2300 if (tstack->toggle_refs[i].notify == notify &&
2301 tstack->toggle_refs[i].data == data)
2304 tstack->n_toggle_refs -= 1;
2305 if (i != tstack->n_toggle_refs)
2306 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2308 if (tstack->n_toggle_refs == 0)
2309 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2311 g_object_unref (object);
2318 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2323 * @object: a #GObject
2325 * Increases the reference count of @object.
2327 * Returns: the same @object
2330 g_object_ref (gpointer _object)
2332 GObject *object = _object;
2335 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2336 g_return_val_if_fail (object->ref_count > 0, NULL);
2338 #ifdef G_ENABLE_DEBUG
2339 if (g_trap_object_ref == object)
2341 #endif /* G_ENABLE_DEBUG */
2344 old_val = g_atomic_int_exchange_and_add (&object->ref_count, 1);
2346 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2347 toggle_refs_notify (object, FALSE);
2354 * @object: a #GObject
2356 * Decreases the reference count of @object. When its reference count
2357 * drops to 0, the object is finalized (i.e. its memory is freed).
2360 g_object_unref (gpointer _object)
2362 GObject *object = _object;
2366 g_return_if_fail (G_IS_OBJECT (object));
2367 g_return_if_fail (object->ref_count > 0);
2369 #ifdef G_ENABLE_DEBUG
2370 if (g_trap_object_ref == object)
2372 #endif /* G_ENABLE_DEBUG */
2374 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2375 retry_atomic_decrement1:
2376 old_ref = g_atomic_int_get (&object->ref_count);
2379 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2380 goto retry_atomic_decrement1;
2382 /* if we went from 2->1 we need to notify toggle refs if any */
2383 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2384 toggle_refs_notify (object, TRUE);
2388 /* we are about tp remove the last reference */
2389 G_OBJECT_GET_CLASS (object)->dispose (object);
2391 /* may have been re-referenced meanwhile */
2392 retry_atomic_decrement2:
2393 old_ref = g_atomic_int_get (&object->ref_count);
2396 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2397 goto retry_atomic_decrement2;
2399 /* if we went from 2->1 we need to notify toggle refs if any */
2400 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2401 toggle_refs_notify (object, TRUE);
2406 /* we are still in the process of taking away the last ref */
2407 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2408 g_signal_handlers_destroy (object);
2409 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2411 /* decrement the last reference */
2412 is_zero = g_atomic_int_dec_and_test (&object->ref_count);
2414 /* may have been re-referenced meanwhile */
2415 if (G_LIKELY (is_zero))
2417 G_OBJECT_GET_CLASS (object)->finalize (object);
2418 #ifdef G_ENABLE_DEBUG
2421 /* catch objects not chaining finalize handlers */
2422 G_LOCK (debug_objects);
2423 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2424 G_UNLOCK (debug_objects);
2426 #endif /* G_ENABLE_DEBUG */
2427 g_type_free_instance ((GTypeInstance*) object);
2433 * g_object_get_qdata:
2434 * @object: The GObject to get a stored user data pointer from
2435 * @quark: A #GQuark, naming the user data pointer
2437 * This function gets back user data pointers stored via
2438 * g_object_set_qdata().
2440 * Returns: The user data pointer set, or %NULL
2443 g_object_get_qdata (GObject *object,
2446 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2448 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2452 * g_object_set_qdata:
2453 * @object: The GObject to set store a user data pointer
2454 * @quark: A #GQuark, naming the user data pointer
2455 * @data: An opaque user data pointer
2457 * This sets an opaque, named pointer on an object.
2458 * The name is specified through a #GQuark (retrived e.g. via
2459 * g_quark_from_static_string()), and the pointer
2460 * can be gotten back from the @object with g_object_get_qdata()
2461 * until the @object is finalized.
2462 * Setting a previously set user data pointer, overrides (frees)
2463 * the old pointer set, using #NULL as pointer essentially
2464 * removes the data stored.
2467 g_object_set_qdata (GObject *object,
2471 g_return_if_fail (G_IS_OBJECT (object));
2472 g_return_if_fail (quark > 0);
2474 g_datalist_id_set_data (&object->qdata, quark, data);
2478 * g_object_set_qdata_full:
2479 * @object: The GObject to set store a user data pointer
2480 * @quark: A #GQuark, naming the user data pointer
2481 * @data: An opaque user data pointer
2482 * @destroy: Function to invoke with @data as argument, when @data
2485 * This function works like g_object_set_qdata(), but in addition,
2486 * a void (*destroy) (gpointer) function may be specified which is
2487 * called with @data as argument when the @object is finalized, or
2488 * the data is being overwritten by a call to g_object_set_qdata()
2489 * with the same @quark.
2492 g_object_set_qdata_full (GObject *object,
2495 GDestroyNotify destroy)
2497 g_return_if_fail (G_IS_OBJECT (object));
2498 g_return_if_fail (quark > 0);
2500 g_datalist_id_set_data_full (&object->qdata, quark, data,
2501 data ? destroy : (GDestroyNotify) NULL);
2505 * g_object_steal_qdata:
2506 * @object: The GObject to get a stored user data pointer from
2507 * @quark: A #GQuark, naming the user data pointer
2509 * This function gets back user data pointers stored via
2510 * g_object_set_qdata() and removes the @data from object
2511 * without invoking it's destroy() function (if any was
2513 * Usually, calling this function is only required to update
2514 * user data pointers with a destroy notifier, for example:
2517 * object_add_to_user_list (GObject *object,
2518 * const gchar *new_string)
2520 * // the quark, naming the object data
2521 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2522 * // retrive the old string list
2523 * GList *list = g_object_steal_qdata (object, quark_string_list);
2525 * // prepend new string
2526 * list = g_list_prepend (list, g_strdup (new_string));
2527 * // this changed 'list', so we need to set it again
2528 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2531 * free_string_list (gpointer data)
2533 * GList *node, *list = data;
2535 * for (node = list; node; node = node->next)
2536 * g_free (node->data);
2537 * g_list_free (list);
2540 * Using g_object_get_qdata() in the above example, instead of
2541 * g_object_steal_qdata() would have left the destroy function set,
2542 * and thus the partial string list would have been freed upon
2543 * g_object_set_qdata_full().
2545 * Returns: The user data pointer set, or %NULL
2548 g_object_steal_qdata (GObject *object,
2551 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2552 g_return_val_if_fail (quark > 0, NULL);
2554 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2558 * g_object_get_data:
2559 * @object: #GObject containing the associations
2560 * @key: name of the key for that association
2562 * Gets a named field from the objects table of associations (see g_object_set_data()).
2564 * Returns: the data if found, or %NULL if no such data exists.
2567 g_object_get_data (GObject *object,
2572 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2573 g_return_val_if_fail (key != NULL, NULL);
2575 quark = g_quark_try_string (key);
2577 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2581 * g_object_set_data:
2582 * @object: #GObject containing the associations.
2583 * @key: name of the key
2584 * @data: data to associate with that key
2586 * Each object carries around a table of associations from
2587 * strings to pointers. This function lets you set an association.
2589 * If the object already had an association with that name,
2590 * the old association will be destroyed.
2593 g_object_set_data (GObject *object,
2597 g_return_if_fail (G_IS_OBJECT (object));
2598 g_return_if_fail (key != NULL);
2600 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2604 * g_object_set_data_full:
2605 * @object: #GObject containing the associations
2606 * @key: name of the key
2607 * @data: data to associate with that key
2608 * @destroy: function to call when the association is destroyed
2610 * Like g_object_set_data() except it adds notification
2611 * for when the association is destroyed, either by setting it
2612 * to a different value or when the object is destroyed.
2614 * Note that the @destroy callback is not called if @data is %NULL.
2617 g_object_set_data_full (GObject *object,
2620 GDestroyNotify destroy)
2622 g_return_if_fail (G_IS_OBJECT (object));
2623 g_return_if_fail (key != NULL);
2625 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2626 data ? destroy : (GDestroyNotify) NULL);
2630 * g_object_steal_data:
2631 * @object: #GObject containing the associations
2632 * @key: name of the key
2634 * Remove a specified datum from the object's data associations,
2635 * without invoking the association's destroy handler.
2637 * Returns: the data if found, or %NULL if no such data exists.
2640 g_object_steal_data (GObject *object,
2645 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2646 g_return_val_if_fail (key != NULL, NULL);
2648 quark = g_quark_try_string (key);
2650 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2654 g_value_object_init (GValue *value)
2656 value->data[0].v_pointer = NULL;
2660 g_value_object_free_value (GValue *value)
2662 if (value->data[0].v_pointer)
2663 g_object_unref (value->data[0].v_pointer);
2667 g_value_object_copy_value (const GValue *src_value,
2670 if (src_value->data[0].v_pointer)
2671 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2673 dest_value->data[0].v_pointer = NULL;
2677 g_value_object_transform_value (const GValue *src_value,
2680 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)))
2681 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2683 dest_value->data[0].v_pointer = NULL;
2687 g_value_object_peek_pointer (const GValue *value)
2689 return value->data[0].v_pointer;
2693 g_value_object_collect_value (GValue *value,
2694 guint n_collect_values,
2695 GTypeCValue *collect_values,
2696 guint collect_flags)
2698 if (collect_values[0].v_pointer)
2700 GObject *object = collect_values[0].v_pointer;
2702 if (object->g_type_instance.g_class == NULL)
2703 return g_strconcat ("invalid unclassed object pointer for value type `",
2704 G_VALUE_TYPE_NAME (value),
2707 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2708 return g_strconcat ("invalid object type `",
2709 G_OBJECT_TYPE_NAME (object),
2710 "' for value type `",
2711 G_VALUE_TYPE_NAME (value),
2714 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2715 value->data[0].v_pointer = g_object_ref (object);
2718 value->data[0].v_pointer = NULL;
2724 g_value_object_lcopy_value (const GValue *value,
2725 guint n_collect_values,
2726 GTypeCValue *collect_values,
2727 guint collect_flags)
2729 GObject **object_p = collect_values[0].v_pointer;
2732 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2734 if (!value->data[0].v_pointer)
2736 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2737 *object_p = value->data[0].v_pointer;
2739 *object_p = g_object_ref (value->data[0].v_pointer);
2745 * g_value_set_object:
2746 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2747 * @v_object: object value to be set
2749 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2751 * g_value_set_object() increases the reference count of @v_object
2752 * (the #GValue holds a reference to @v_object). If you do not wish
2753 * to increase the reference count of the object (i.e. you wish to
2754 * pass your current reference to the #GValue because you no longer
2755 * need it), use g_value_take_object() instead.
2757 * It is important that your #GValue holds a reference to @v_object (either its
2758 * own, or one it has taken) to ensure that the object won't be destroyed while
2759 * the #GValue still exists).
2762 g_value_set_object (GValue *value,
2767 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2769 old = value->data[0].v_pointer;
2773 g_return_if_fail (G_IS_OBJECT (v_object));
2774 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2776 value->data[0].v_pointer = v_object;
2777 g_object_ref (value->data[0].v_pointer);
2780 value->data[0].v_pointer = NULL;
2783 g_object_unref (old);
2787 * g_value_set_object_take_ownership:
2788 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2789 * @v_object: object value to be set
2791 * This is an internal function introduced mainly for C marshallers.
2793 * Deprecated: 2.4: Use g_value_take_object() instead.
2796 g_value_set_object_take_ownership (GValue *value,
2799 g_value_take_object (value, v_object);
2803 * g_value_take_object:
2804 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2805 * @v_object: object value to be set
2807 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2808 * and takes over the ownership of the callers reference to @v_object;
2809 * the caller doesn't have to unref it any more (i.e. the reference
2810 * count of the object is not increased).
2812 * If you want the #GValue to hold its own reference to @v_object, use
2813 * g_value_set_object() instead.
2818 g_value_take_object (GValue *value,
2821 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2823 if (value->data[0].v_pointer)
2825 g_object_unref (value->data[0].v_pointer);
2826 value->data[0].v_pointer = NULL;
2831 g_return_if_fail (G_IS_OBJECT (v_object));
2832 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2834 value->data[0].v_pointer = v_object; /* we take over the reference count */
2839 * g_value_get_object:
2840 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2842 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2844 * Returns: object contents of @value
2847 g_value_get_object (const GValue *value)
2849 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2851 return value->data[0].v_pointer;
2855 * g_value_dup_object:
2856 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2858 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
2859 * its reference count.
2861 * Returns: object content of @value, should be unreferenced when no
2865 g_value_dup_object (const GValue *value)
2867 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2869 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2873 * g_signal_connect_object:
2874 * @instance: the instance to connect to.
2875 * @detailed_signal: a string of the form "signal-name::detail".
2876 * @c_handler: the #GCallback to connect.
2877 * @gobject: the object to pass as data to @c_handler.
2878 * @connect_flags: a combination of #GConnnectFlags.
2880 * This is similar to g_signal_connect_data(), but uses a closure which
2881 * ensures that the @gobject stays alive during the call to @c_handler
2882 * by temporarily adding a reference count to @gobject.
2884 * Note that there is a bug in GObject that makes this function
2885 * much less useful than it might seem otherwise. Once @gobject is
2886 * disposed, the callback will no longer be called, but, the signal
2887 * handler is <emphasis>not</emphasis> currently disconnected. If the
2888 * @instance is itself being freed at the same time than this doesn't
2889 * matter, since the signal will automatically be removed, but
2890 * if @instance persists, then the signal handler will leak. You
2891 * should not remove the signal yourself because in a future versions of
2892 * GObject, the handler <emphasis>will</emphasis> automatically
2895 * It's possible to work around this problem in a way that will
2896 * continue to work with future versions of GObject by checking
2897 * that the signal handler is still connected before disconnected it:
2898 * <informalexample><programlisting>
2899 * if (g_signal_handler_is_connected (instance, id))
2900 * g_signal_handler_disconnect (instance, id);
2901 * </programlisting></informalexample>
2903 * Returns: the handler id.
2906 g_signal_connect_object (gpointer instance,
2907 const gchar *detailed_signal,
2908 GCallback c_handler,
2910 GConnectFlags connect_flags)
2912 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2913 g_return_val_if_fail (detailed_signal != NULL, 0);
2914 g_return_val_if_fail (c_handler != NULL, 0);
2920 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2922 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2924 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2927 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
2933 GClosure *closures[1]; /* flexible array */
2935 /* don't change this structure without supplying an accessor for
2936 * watched closures, e.g.:
2937 * GSList* g_object_list_watched_closures (GObject *object)
2940 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2941 * carray = g_object_get_data (object, "GObject-closure-array");
2944 * GSList *slist = NULL;
2946 * for (i = 0; i < carray->n_closures; i++)
2947 * slist = g_slist_prepend (slist, carray->closures[i]);
2955 object_remove_closure (gpointer data,
2958 GObject *object = data;
2959 CArray *carray = g_object_get_qdata (object, quark_closure_array);
2962 for (i = 0; i < carray->n_closures; i++)
2963 if (carray->closures[i] == closure)
2965 carray->n_closures--;
2966 if (i < carray->n_closures)
2967 carray->closures[i] = carray->closures[carray->n_closures];
2970 g_assert_not_reached ();
2974 destroy_closure_array (gpointer data)
2976 CArray *carray = data;
2977 GObject *object = carray->object;
2978 guint i, n = carray->n_closures;
2980 for (i = 0; i < n; i++)
2982 GClosure *closure = carray->closures[i];
2984 /* removing object_remove_closure() upfront is probably faster than
2985 * letting it fiddle with quark_closure_array which is empty anyways
2987 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
2988 g_closure_invalidate (closure);
2994 * g_object_watch_closure:
2995 * @object: GObject restricting lifetime of @closure
2996 * @closure: GClosure to watch
2998 * This function essentially limits the life time of the @closure to
2999 * the life time of the object. That is, when the object is finalized,
3000 * the @closure is invalidated by calling g_closure_invalidate() on
3001 * it, in order to prevent invocations of the closure with a finalized
3002 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3003 * added as marshal guards to the @closure, to ensure that an extra
3004 * reference count is held on @object during invocation of the
3005 * @closure. Usually, this function will be called on closures that
3006 * use this @object as closure data.
3009 g_object_watch_closure (GObject *object,
3015 g_return_if_fail (G_IS_OBJECT (object));
3016 g_return_if_fail (closure != NULL);
3017 g_return_if_fail (closure->is_invalid == FALSE);
3018 g_return_if_fail (closure->in_marshal == FALSE);
3019 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3021 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3022 g_closure_add_marshal_guards (closure,
3023 object, (GClosureNotify) g_object_ref,
3024 object, (GClosureNotify) g_object_unref);
3025 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3028 carray = g_renew (CArray, NULL, 1);
3029 carray->object = object;
3030 carray->n_closures = 1;
3035 i = carray->n_closures++;
3036 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3038 carray->closures[i] = closure;
3039 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3043 * g_closure_new_object:
3044 * @sizeof_closure: the size of the structure to allocate, must be at least
3045 * <literal>sizeof (GClosure)</literal>
3046 * @object: a #GObject pointer to store in the @data field of the newly
3047 * allocated #GClosure
3049 * A variant of g_closure_new_simple() which stores @object in the
3050 * @data field of the closure and calls g_object_watch_closure() on
3051 * @object and the created closure. This function is mainly useful
3052 * when implementing new types of closures.
3054 * Returns: a newly allocated #GClosure
3057 g_closure_new_object (guint sizeof_closure,
3062 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3063 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3065 closure = g_closure_new_simple (sizeof_closure, object);
3066 g_object_watch_closure (object, closure);
3072 * g_cclosure_new_object:
3073 * @callback_func: the function to invoke
3074 * @object: a #GObject pointer to pass to @callback_func
3076 * A variant of g_cclosure_new() which uses @object as @user_data and
3077 * calls g_object_watch_closure() on @object and the created
3078 * closure. This function is useful when you have a callback closely
3079 * associated with a #GObject, and want the callback to no longer run
3080 * after the object is is freed.
3082 * Returns: a new #GCClosure
3085 g_cclosure_new_object (GCallback callback_func,
3090 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3091 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3092 g_return_val_if_fail (callback_func != NULL, NULL);
3094 closure = g_cclosure_new (callback_func, object, NULL);
3095 g_object_watch_closure (object, closure);
3101 * g_cclosure_new_object_swap:
3102 * @callback_func: the function to invoke
3103 * @object: a #GObject pointer to pass to @callback_func
3105 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3106 * and calls g_object_watch_closure() on @object and the created
3107 * closure. This function is useful when you have a callback closely
3108 * associated with a #GObject, and want the callback to no longer run
3109 * after the object is is freed.
3111 * Returns: a new #GCClosure
3114 g_cclosure_new_object_swap (GCallback callback_func,
3119 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3120 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3121 g_return_val_if_fail (callback_func != NULL, NULL);
3123 closure = g_cclosure_new_swap (callback_func, object, NULL);
3124 g_object_watch_closure (object, closure);
3130 g_object_compat_control (gsize what,
3136 case 1: /* floating base type */
3137 return G_TYPE_INITIALLY_UNOWNED;
3138 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3139 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3141 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3143 *pp = floating_flag_handler;
3150 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3153 g_initially_unowned_init (GInitiallyUnowned *object)
3155 g_object_force_floating (object);
3159 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3163 #define __G_OBJECT_C__
3164 #include "gobjectaliasdef.c"