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
107 #define PREALLOC_CPARAMS (8)
111 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
112 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
114 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
115 #define OBJECT_HAS_TOGGLE_REF(object) \
116 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
117 #define OBJECT_FLOATING_FLAG 0x2
120 /* --- signals --- */
127 /* --- properties --- */
133 /* --- prototypes --- */
134 static void g_object_base_class_init (GObjectClass *class);
135 static void g_object_base_class_finalize (GObjectClass *class);
136 static void g_object_do_class_init (GObjectClass *class);
137 static void g_object_init (GObject *object);
138 static GObject* g_object_constructor (GType type,
139 guint n_construct_properties,
140 GObjectConstructParam *construct_params);
141 static void g_object_real_dispose (GObject *object);
142 static void g_object_finalize (GObject *object);
143 static void g_object_do_set_property (GObject *object,
147 static void g_object_do_get_property (GObject *object,
151 static void g_value_object_init (GValue *value);
152 static void g_value_object_free_value (GValue *value);
153 static void g_value_object_copy_value (const GValue *src_value,
155 static void g_value_object_transform_value (const GValue *src_value,
157 static gpointer g_value_object_peek_pointer (const GValue *value);
158 static gchar* g_value_object_collect_value (GValue *value,
159 guint n_collect_values,
160 GTypeCValue *collect_values,
161 guint collect_flags);
162 static gchar* g_value_object_lcopy_value (const GValue *value,
163 guint n_collect_values,
164 GTypeCValue *collect_values,
165 guint collect_flags);
166 static void g_object_dispatch_properties_changed (GObject *object,
168 GParamSpec **pspecs);
169 static inline void object_get_property (GObject *object,
172 static inline void object_set_property (GObject *object,
175 GObjectNotifyQueue *nqueue);
176 static guint object_floating_flag_handler (GObject *object,
179 static void object_interface_check_properties (gpointer func_data,
183 /* --- variables --- */
184 static GQuark quark_closure_array = 0;
185 static GQuark quark_weak_refs = 0;
186 static GQuark quark_toggle_refs = 0;
187 static GParamSpecPool *pspec_pool = NULL;
188 static GObjectNotifyContext property_notify_context = { 0, };
189 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
190 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
191 G_LOCK_DEFINE_STATIC (construction_mutex);
192 static GSList *construction_objects = NULL;
194 /* --- functions --- */
195 #ifdef G_ENABLE_DEBUG
196 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
197 G_LOCK_DEFINE_STATIC (debug_objects);
198 static volatile GObject *g_trap_object_ref = NULL;
199 static guint debug_objects_count = 0;
200 static GHashTable *debug_objects_ht = NULL;
202 debug_objects_foreach (gpointer key,
206 GObject *object = value;
208 g_message ("[%p] stale %s\tref_count=%u",
210 G_OBJECT_TYPE_NAME (object),
214 debug_objects_atexit (void)
218 G_LOCK (debug_objects);
219 g_message ("stale GObjects: %u", debug_objects_count);
220 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
221 G_UNLOCK (debug_objects);
224 #endif /* G_ENABLE_DEBUG */
227 g_object_type_init (void)
229 static gboolean initialized = FALSE;
230 static const GTypeFundamentalInfo finfo = {
231 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
233 static GTypeInfo info = {
234 sizeof (GObjectClass),
235 (GBaseInitFunc) g_object_base_class_init,
236 (GBaseFinalizeFunc) g_object_base_class_finalize,
237 (GClassInitFunc) g_object_do_class_init,
238 NULL /* class_destroy */,
239 NULL /* class_data */,
242 (GInstanceInitFunc) g_object_init,
243 NULL, /* value_table */
245 static const GTypeValueTable value_table = {
246 g_value_object_init, /* value_init */
247 g_value_object_free_value, /* value_free */
248 g_value_object_copy_value, /* value_copy */
249 g_value_object_peek_pointer, /* value_peek_pointer */
250 "p", /* collect_format */
251 g_value_object_collect_value, /* collect_value */
252 "p", /* lcopy_format */
253 g_value_object_lcopy_value, /* lcopy_value */
257 g_return_if_fail (initialized == FALSE);
262 info.value_table = &value_table;
263 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
264 g_assert (type == G_TYPE_OBJECT);
265 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
267 #ifdef G_ENABLE_DEBUG
270 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
271 g_atexit (debug_objects_atexit);
273 #endif /* G_ENABLE_DEBUG */
277 g_object_base_class_init (GObjectClass *class)
279 GObjectClass *pclass = g_type_class_peek_parent (class);
281 /* reset instance specific fields and methods that don't get inherited */
282 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
283 class->get_property = NULL;
284 class->set_property = NULL;
288 g_object_base_class_finalize (GObjectClass *class)
292 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
294 g_slist_free (class->construct_properties);
295 class->construct_properties = NULL;
296 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
297 for (node = list; node; node = node->next)
299 GParamSpec *pspec = node->data;
301 g_param_spec_pool_remove (pspec_pool, pspec);
302 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
303 g_param_spec_unref (pspec);
309 g_object_notify_dispatcher (GObject *object,
313 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
317 g_object_do_class_init (GObjectClass *class)
319 /* read the comment about typedef struct CArray; on why not to change this quark */
320 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
322 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
323 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
324 pspec_pool = g_param_spec_pool_new (TRUE);
325 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
326 property_notify_context.dispatcher = g_object_notify_dispatcher;
328 class->constructor = g_object_constructor;
329 class->set_property = g_object_do_set_property;
330 class->get_property = g_object_do_get_property;
331 class->dispose = g_object_real_dispose;
332 class->finalize = g_object_finalize;
333 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
334 class->notify = NULL;
338 * @pspec: the #GParamSpec of the property which changed
339 * @gobject: the object which received the signal.
341 * The notify signal is emitted on an object when one of its properties
342 * has been changed. Note that getting this signal doesn't guarantee that the
343 * value of the property has actually changed, it may also be emitted when
344 * the setter for the property 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 doesn't have
477 * 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().
518 * Returns: the #GParamSpec for the property of the
519 * interface with the name @property_name, or %NULL
520 * 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().
655 * Returns: a pointer to an array of pointers to #GParamSpec structures.
656 * The paramspecs are owned by GLib, but the array should
657 * be freed with g_free() when you are done with it.
660 g_object_interface_list_properties (gpointer g_iface,
661 guint *n_properties_p)
663 GTypeInterface *iface_class = g_iface;
667 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
669 pspecs = g_param_spec_pool_list (pspec_pool,
679 g_object_init (GObject *object)
681 object->ref_count = 1;
682 g_datalist_init (&object->qdata);
684 /* freeze object's notification queue, g_object_newv() preserves pairedness */
685 g_object_notify_queue_freeze (object, &property_notify_context);
686 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
687 G_LOCK (construction_mutex);
688 construction_objects = g_slist_prepend (construction_objects, object);
689 G_UNLOCK (construction_mutex);
691 #ifdef G_ENABLE_DEBUG
694 G_LOCK (debug_objects);
695 debug_objects_count++;
696 g_hash_table_insert (debug_objects_ht, object, object);
697 G_UNLOCK (debug_objects);
699 #endif /* G_ENABLE_DEBUG */
703 g_object_do_set_property (GObject *object,
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
717 g_object_do_get_property (GObject *object,
725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
731 g_object_real_dispose (GObject *object)
733 g_signal_handlers_destroy (object);
734 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
735 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
739 g_object_finalize (GObject *object)
741 g_datalist_clear (&object->qdata);
743 #ifdef G_ENABLE_DEBUG
746 G_LOCK (debug_objects);
747 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
748 g_hash_table_remove (debug_objects_ht, object);
749 debug_objects_count--;
750 G_UNLOCK (debug_objects);
752 #endif /* G_ENABLE_DEBUG */
757 g_object_dispatch_properties_changed (GObject *object,
763 for (i = 0; i < n_pspecs; i++)
764 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
768 * g_object_run_dispose:
769 * @object: a #GObject
771 * Releases all references to other objects. This can be used to break
774 * This functions should only be called from object system implementations.
777 g_object_run_dispose (GObject *object)
779 g_return_if_fail (G_IS_OBJECT (object));
780 g_return_if_fail (object->ref_count > 0);
782 g_object_ref (object);
783 G_OBJECT_GET_CLASS (object)->dispose (object);
784 g_object_unref (object);
788 * g_object_freeze_notify:
789 * @object: a #GObject
791 * Stops emission of "notify" signals on @object. The signals are
792 * queued until g_object_thaw_notify() is called on @object.
794 * This is necessary for accessors that modify multiple properties to prevent
795 * premature notification while the object is still being modified.
798 g_object_freeze_notify (GObject *object)
800 g_return_if_fail (G_IS_OBJECT (object));
802 if (g_atomic_int_get (&object->ref_count) == 0)
805 g_object_ref (object);
806 g_object_notify_queue_freeze (object, &property_notify_context);
807 g_object_unref (object);
812 * @object: a #GObject
813 * @property_name: the name of a property installed on the class of @object.
815 * Emits a "notify" signal for the property @property_name on @object.
818 g_object_notify (GObject *object,
819 const gchar *property_name)
823 g_return_if_fail (G_IS_OBJECT (object));
824 g_return_if_fail (property_name != NULL);
825 if (g_atomic_int_get (&object->ref_count) == 0)
828 g_object_ref (object);
829 /* We don't need to get the redirect target
830 * (by, e.g. calling g_object_class_find_property())
831 * because g_object_notify_queue_add() does that
833 pspec = g_param_spec_pool_lookup (pspec_pool,
835 G_OBJECT_TYPE (object),
839 g_warning ("%s: object class `%s' has no property named `%s'",
841 G_OBJECT_TYPE_NAME (object),
845 GObjectNotifyQueue *nqueue;
847 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
848 g_object_notify_queue_add (object, nqueue, pspec);
849 g_object_notify_queue_thaw (object, nqueue);
851 g_object_unref (object);
855 * g_object_thaw_notify:
856 * @object: a #GObject
858 * Reverts the effect of a previous call to g_object_freeze_notify().
859 * This causes all queued "notify" signals on @object to be emitted.
862 g_object_thaw_notify (GObject *object)
864 GObjectNotifyQueue *nqueue;
866 g_return_if_fail (G_IS_OBJECT (object));
867 if (g_atomic_int_get (&object->ref_count) == 0)
870 g_object_ref (object);
871 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
872 if (!nqueue || !nqueue->freeze_count)
873 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
874 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
876 g_object_notify_queue_thaw (object, nqueue);
877 g_object_unref (object);
881 object_get_property (GObject *object,
885 GObjectClass *class = g_type_class_peek (pspec->owner_type);
886 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
887 GParamSpec *redirect;
889 redirect = g_param_spec_get_redirect_target (pspec);
893 class->get_property (object, param_id, value, pspec);
897 object_set_property (GObject *object,
900 GObjectNotifyQueue *nqueue)
902 GValue tmp_value = { 0, };
903 GObjectClass *class = g_type_class_peek (pspec->owner_type);
904 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
905 GParamSpec *redirect;
907 redirect = g_param_spec_get_redirect_target (pspec);
911 /* provide a copy to work from, convert (if necessary) and validate */
912 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
913 if (!g_value_transform (value, &tmp_value))
914 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
916 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
917 G_VALUE_TYPE_NAME (value));
918 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
920 gchar *contents = g_strdup_value_contents (value);
922 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
924 G_VALUE_TYPE_NAME (value),
926 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
931 class->set_property (object, param_id, &tmp_value, pspec);
932 g_object_notify_queue_add (object, nqueue, pspec);
934 g_value_unset (&tmp_value);
938 object_interface_check_properties (gpointer func_data,
941 GTypeInterface *iface_class = g_iface;
942 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
943 GType iface_type = iface_class->g_type;
947 if (!G_IS_OBJECT_CLASS (class))
950 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
954 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
956 G_OBJECT_CLASS_TYPE (class),
961 g_critical ("Object class %s doesn't implement property "
962 "'%s' from interface '%s'",
963 g_type_name (G_OBJECT_CLASS_TYPE (class)),
965 g_type_name (iface_type));
970 /* The implementation paramspec must have a less restrictive
971 * type than the interface parameter spec for set() and a
972 * more restrictive type for get(). We just require equality,
973 * rather than doing something more complicated checking
974 * the READABLE and WRITABLE flags. We also simplify here
975 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
978 !g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (pspecs[n]),
979 G_PARAM_SPEC_VALUE_TYPE (class_pspec)))
981 g_critical ("Property '%s' on class '%s' has type '%s' "
982 "which is different from the type '%s', "
983 "of the property on interface '%s'\n",
985 g_type_name (G_OBJECT_CLASS_TYPE (class)),
986 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
987 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
988 g_type_name (iface_type));
991 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
993 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
994 * READABLE and WRITABLE remove restrictions. The implementation
995 * paramspec must have less restrictive flags.
998 (!SUBSET (class_pspec->flags,
1000 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1001 !SUBSET (pspecs[n]->flags,
1003 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1005 g_critical ("Flags for property '%s' on class '%s' "
1006 "are not compatible with the property on"
1009 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1010 g_type_name (iface_type));
1020 * @object_type: the type id of the #GObject subtype to instantiate
1021 * @first_property_name: the name of the first property
1022 * @...: the value of the first property, followed optionally by more
1023 * name/value pairs, followed by %NULL
1025 * Creates a new instance of a #GObject subtype and sets its properties.
1027 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1028 * which are not explicitly specified are set to their default values.
1030 * Returns: a new instance of @object_type
1033 g_object_new (GType object_type,
1034 const gchar *first_property_name,
1040 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1042 va_start (var_args, first_property_name);
1043 object = g_object_new_valist (object_type, first_property_name, var_args);
1050 slist_maybe_remove (GSList **slist,
1053 GSList *last = NULL, *node = *slist;
1056 if (node->data == data)
1059 last->next = node->next;
1061 *slist = node->next;
1062 g_slist_free_1 (node);
1071 static inline gboolean
1072 object_in_construction_list (GObject *object)
1074 gboolean in_construction;
1075 G_LOCK (construction_mutex);
1076 in_construction = g_slist_find (construction_objects, object) != NULL;
1077 G_UNLOCK (construction_mutex);
1078 return in_construction;
1083 * @object_type: the type id of the #GObject subtype to instantiate
1084 * @n_parameters: the length of the @parameters array
1085 * @parameters: an array of #GParameter
1087 * Creates a new instance of a #GObject subtype and sets its properties.
1089 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1090 * which are not explicitly specified are set to their default values.
1092 * Returns: a new instance of @object_type
1095 g_object_newv (GType object_type,
1097 GParameter *parameters)
1099 GObjectConstructParam *cparams, *oparams;
1100 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1102 GObjectClass *class, *unref_class = NULL;
1104 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1106 GList *clist = NULL;
1107 gboolean newly_constructed;
1110 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1112 class = g_type_class_peek_static (object_type);
1114 class = unref_class = g_type_class_ref (object_type);
1115 for (slist = class->construct_properties; slist; slist = slist->next)
1117 clist = g_list_prepend (clist, slist->data);
1118 n_total_cparams += 1;
1121 /* collect parameters, sort into construction and normal ones */
1122 oparams = g_new (GObjectConstructParam, n_parameters);
1123 cparams = g_new (GObjectConstructParam, n_total_cparams);
1124 for (i = 0; i < n_parameters; i++)
1126 GValue *value = ¶meters[i].value;
1127 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1133 g_warning ("%s: object class `%s' has no property named `%s'",
1135 g_type_name (object_type),
1136 parameters[i].name);
1139 if (!(pspec->flags & G_PARAM_WRITABLE))
1141 g_warning ("%s: property `%s' of object class `%s' is not writable",
1144 g_type_name (object_type));
1147 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1149 GList *list = g_list_find (clist, pspec);
1153 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1154 G_STRFUNC, pspec->name, g_type_name (object_type));
1157 cparams[n_cparams].pspec = pspec;
1158 cparams[n_cparams].value = value;
1163 list->prev->next = list->next;
1165 list->next->prev = list->prev;
1166 g_list_free_1 (list);
1170 oparams[n_oparams].pspec = pspec;
1171 oparams[n_oparams].value = value;
1176 /* set remaining construction properties to default values */
1177 n_cvalues = n_total_cparams - n_cparams;
1178 cvalues = g_new (GValue, n_cvalues);
1181 GList *tmp = clist->next;
1182 GParamSpec *pspec = clist->data;
1183 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1186 g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1187 g_param_value_set_default (pspec, value);
1189 cparams[n_cparams].pspec = pspec;
1190 cparams[n_cparams].value = value;
1193 g_list_free_1 (clist);
1197 /* construct object from construction parameters */
1198 object = class->constructor (object_type, n_total_cparams, cparams);
1199 /* free construction values */
1202 g_value_unset (cvalues + n_cvalues);
1205 /* adjust freeze_count according to g_object_init() and remaining properties */
1206 G_LOCK (construction_mutex);
1207 newly_constructed = slist_maybe_remove (&construction_objects, object);
1208 G_UNLOCK (construction_mutex);
1209 if (newly_constructed || n_oparams)
1210 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1211 if (newly_constructed)
1212 g_object_notify_queue_thaw (object, nqueue);
1214 /* run 'constructed' handler if there is one */
1215 if (newly_constructed && class->constructed)
1216 class->constructed (object);
1218 /* set remaining properties */
1219 for (i = 0; i < n_oparams; i++)
1220 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1223 /* release our own freeze count and handle notifications */
1224 if (newly_constructed || n_oparams)
1225 g_object_notify_queue_thaw (object, nqueue);
1228 g_type_class_unref (unref_class);
1234 * g_object_new_valist:
1235 * @object_type: the type id of the #GObject subtype to instantiate
1236 * @first_property_name: the name of the first property
1237 * @var_args: the value of the first property, followed optionally by more
1238 * name/value pairs, followed by %NULL
1240 * Creates a new instance of a #GObject subtype and sets its properties.
1242 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1243 * which are not explicitly specified are set to their default values.
1245 * Returns: a new instance of @object_type
1248 g_object_new_valist (GType object_type,
1249 const gchar *first_property_name,
1252 GObjectClass *class;
1256 guint n_params = 0, n_alloced_params = 16;
1258 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1260 if (!first_property_name)
1261 return g_object_newv (object_type, 0, NULL);
1263 class = g_type_class_ref (object_type);
1265 params = g_new (GParameter, n_alloced_params);
1266 name = first_property_name;
1269 gchar *error = NULL;
1270 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1276 g_warning ("%s: object class `%s' has no property named `%s'",
1278 g_type_name (object_type),
1282 if (n_params >= n_alloced_params)
1284 n_alloced_params += 16;
1285 params = g_renew (GParameter, params, n_alloced_params);
1287 params[n_params].name = name;
1288 params[n_params].value.g_type = 0;
1289 g_value_init (¶ms[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1290 G_VALUE_COLLECT (¶ms[n_params].value, var_args, 0, &error);
1293 g_warning ("%s: %s", G_STRFUNC, error);
1295 g_value_unset (¶ms[n_params].value);
1299 name = va_arg (var_args, gchar*);
1302 object = g_object_newv (object_type, n_params, params);
1305 g_value_unset (¶ms[n_params].value);
1308 g_type_class_unref (class);
1314 g_object_constructor (GType type,
1315 guint n_construct_properties,
1316 GObjectConstructParam *construct_params)
1321 object = (GObject*) g_type_create_instance (type);
1323 /* set construction parameters */
1324 if (n_construct_properties)
1326 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1328 /* set construct properties */
1329 while (n_construct_properties--)
1331 GValue *value = construct_params->value;
1332 GParamSpec *pspec = construct_params->pspec;
1335 object_set_property (object, pspec, value, nqueue);
1337 g_object_notify_queue_thaw (object, nqueue);
1338 /* the notification queue is still frozen from g_object_init(), so
1339 * we don't need to handle it here, g_object_newv() takes
1348 * g_object_set_valist:
1349 * @object: a #GObject
1350 * @first_property_name: name of the first property to set
1351 * @var_args: value for the first property, followed optionally by more
1352 * name/value pairs, followed by %NULL
1354 * Sets properties on an object.
1357 g_object_set_valist (GObject *object,
1358 const gchar *first_property_name,
1361 GObjectNotifyQueue *nqueue;
1364 g_return_if_fail (G_IS_OBJECT (object));
1366 g_object_ref (object);
1367 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1369 name = first_property_name;
1372 GValue value = { 0, };
1374 gchar *error = NULL;
1376 pspec = g_param_spec_pool_lookup (pspec_pool,
1378 G_OBJECT_TYPE (object),
1382 g_warning ("%s: object class `%s' has no property named `%s'",
1384 G_OBJECT_TYPE_NAME (object),
1388 if (!(pspec->flags & G_PARAM_WRITABLE))
1390 g_warning ("%s: property `%s' of object class `%s' is not writable",
1393 G_OBJECT_TYPE_NAME (object));
1396 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1398 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1399 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1403 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1405 G_VALUE_COLLECT (&value, var_args, 0, &error);
1408 g_warning ("%s: %s", G_STRFUNC, error);
1410 g_value_unset (&value);
1414 object_set_property (object, pspec, &value, nqueue);
1415 g_value_unset (&value);
1417 name = va_arg (var_args, gchar*);
1420 g_object_notify_queue_thaw (object, nqueue);
1421 g_object_unref (object);
1425 * g_object_get_valist:
1426 * @object: a #GObject
1427 * @first_property_name: name of the first property to get
1428 * @var_args: return location for the first property, followed optionally by more
1429 * name/return location pairs, followed by %NULL
1431 * Gets properties of an object.
1433 * In general, a copy is made of the property contents and the caller is
1434 * responsible for freeing the memory in the appropriate manner for the type,
1435 * for instance by calling g_free() or g_object_unref().
1437 * See g_object_get().
1440 g_object_get_valist (GObject *object,
1441 const gchar *first_property_name,
1446 g_return_if_fail (G_IS_OBJECT (object));
1448 g_object_ref (object);
1450 name = first_property_name;
1454 GValue value = { 0, };
1458 pspec = g_param_spec_pool_lookup (pspec_pool,
1460 G_OBJECT_TYPE (object),
1464 g_warning ("%s: object class `%s' has no property named `%s'",
1466 G_OBJECT_TYPE_NAME (object),
1470 if (!(pspec->flags & G_PARAM_READABLE))
1472 g_warning ("%s: property `%s' of object class `%s' is not readable",
1475 G_OBJECT_TYPE_NAME (object));
1479 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1481 object_get_property (object, pspec, &value);
1483 G_VALUE_LCOPY (&value, var_args, 0, &error);
1486 g_warning ("%s: %s", G_STRFUNC, error);
1488 g_value_unset (&value);
1492 g_value_unset (&value);
1494 name = va_arg (var_args, gchar*);
1497 g_object_unref (object);
1502 * @object: a #GObject
1503 * @first_property_name: name of the first property to set
1504 * @...: value for the first property, followed optionally by more
1505 * name/value pairs, followed by %NULL
1507 * Sets properties on an object.
1510 g_object_set (gpointer _object,
1511 const gchar *first_property_name,
1514 GObject *object = _object;
1517 g_return_if_fail (G_IS_OBJECT (object));
1519 va_start (var_args, first_property_name);
1520 g_object_set_valist (object, first_property_name, var_args);
1526 * @object: a #GObject
1527 * @first_property_name: name of the first property to get
1528 * @...: return location for the first property, followed optionally by more
1529 * name/return location pairs, followed by %NULL
1531 * Gets properties of an object.
1533 * In general, a copy is made of the property contents and the caller is
1534 * responsible for freeing the memory in the appropriate manner for the type,
1535 * for instance by calling g_free() or g_object_unref().
1538 * <title>Using g_object_get(<!-- -->)</title>
1539 * An example of using g_object_get() to get the contents
1540 * of three properties - one of type #G_TYPE_INT,
1541 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1547 * g_object_get (my_object,
1548 * "int-property", &intval,
1549 * "str-property", &strval,
1550 * "obj-property", &objval,
1553 * // Do something with intval, strval, objval
1556 * g_object_unref (objval);
1561 g_object_get (gpointer _object,
1562 const gchar *first_property_name,
1565 GObject *object = _object;
1568 g_return_if_fail (G_IS_OBJECT (object));
1570 va_start (var_args, first_property_name);
1571 g_object_get_valist (object, first_property_name, var_args);
1576 * g_object_set_property:
1577 * @object: a #GObject
1578 * @property_name: the name of the property to set
1581 * Sets a property on an object.
1584 g_object_set_property (GObject *object,
1585 const gchar *property_name,
1586 const GValue *value)
1588 GObjectNotifyQueue *nqueue;
1591 g_return_if_fail (G_IS_OBJECT (object));
1592 g_return_if_fail (property_name != NULL);
1593 g_return_if_fail (G_IS_VALUE (value));
1595 g_object_ref (object);
1596 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1598 pspec = g_param_spec_pool_lookup (pspec_pool,
1600 G_OBJECT_TYPE (object),
1603 g_warning ("%s: object class `%s' has no property named `%s'",
1605 G_OBJECT_TYPE_NAME (object),
1607 else if (!(pspec->flags & G_PARAM_WRITABLE))
1608 g_warning ("%s: property `%s' of object class `%s' is not writable",
1611 G_OBJECT_TYPE_NAME (object));
1612 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1613 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1614 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1616 object_set_property (object, pspec, value, nqueue);
1618 g_object_notify_queue_thaw (object, nqueue);
1619 g_object_unref (object);
1623 * g_object_get_property:
1624 * @object: a #GObject
1625 * @property_name: the name of the property to get
1626 * @value: return location for the property value
1628 * Gets a property of an object.
1630 * In general, a copy is made of the property contents and the caller is
1631 * responsible for freeing the memory by calling g_value_unset().
1633 * Note that g_object_get_property() is really intended for language
1634 * bindings, g_object_get() is much more convenient for C programming.
1637 g_object_get_property (GObject *object,
1638 const gchar *property_name,
1643 g_return_if_fail (G_IS_OBJECT (object));
1644 g_return_if_fail (property_name != NULL);
1645 g_return_if_fail (G_IS_VALUE (value));
1647 g_object_ref (object);
1649 pspec = g_param_spec_pool_lookup (pspec_pool,
1651 G_OBJECT_TYPE (object),
1654 g_warning ("%s: object class `%s' has no property named `%s'",
1656 G_OBJECT_TYPE_NAME (object),
1658 else if (!(pspec->flags & G_PARAM_READABLE))
1659 g_warning ("%s: property `%s' of object class `%s' is not readable",
1662 G_OBJECT_TYPE_NAME (object));
1665 GValue *prop_value, tmp_value = { 0, };
1667 /* auto-conversion of the callers value type
1669 if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1671 g_value_reset (value);
1674 else if (!g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1676 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1677 G_STRFUNC, pspec->name,
1678 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1679 G_VALUE_TYPE_NAME (value));
1680 g_object_unref (object);
1685 g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1686 prop_value = &tmp_value;
1688 object_get_property (object, pspec, prop_value);
1689 if (prop_value != value)
1691 g_value_transform (prop_value, value);
1692 g_value_unset (&tmp_value);
1696 g_object_unref (object);
1701 * @object: a #GObject
1702 * @signal_spec: the spec for the first signal
1703 * @...: #GCallback for the first signal, followed by data for the first signal,
1704 * followed optionally by more signal spec/callback/data triples,
1707 * A convenience function to connect multiple signals at once.
1709 * The signal specs expected by this function have the form
1710 * "modifier::signal_name", where modifier can be one of the following:
1713 * <term>signal</term>
1715 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1716 * </para></listitem>
1719 * <term>object_signal</term>
1720 * <term>object-signal</term>
1722 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1723 * </para></listitem>
1726 * <term>swapped_signal</term>
1727 * <term>swapped-signal</term>
1729 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1730 * </para></listitem>
1733 * <term>swapped_object_signal</term>
1734 * <term>swapped-object-signal</term>
1736 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1737 * </para></listitem>
1740 * <term>signal_after</term>
1741 * <term>signal-after</term>
1743 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1744 * </para></listitem>
1747 * <term>object_signal_after</term>
1748 * <term>object-signal-after</term>
1750 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1751 * </para></listitem>
1754 * <term>swapped_signal_after</term>
1755 * <term>swapped-signal-after</term>
1757 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1758 * </para></listitem>
1761 * <term>swapped_object_signal_after</term>
1762 * <term>swapped-object-signal-after</term>
1764 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1765 * </para></listitem>
1770 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1771 * "type", GTK_WINDOW_POPUP,
1774 * "signal::event", gtk_menu_window_event, menu,
1775 * "signal::size_request", gtk_menu_window_size_request, menu,
1776 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1783 g_object_connect (gpointer _object,
1784 const gchar *signal_spec,
1787 GObject *object = _object;
1790 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1791 g_return_val_if_fail (object->ref_count > 0, object);
1793 va_start (var_args, signal_spec);
1796 GCallback callback = va_arg (var_args, GCallback);
1797 gpointer data = va_arg (var_args, gpointer);
1800 if (strncmp (signal_spec, "signal::", 8) == 0)
1801 sid = g_signal_connect_data (object, signal_spec + 8,
1802 callback, data, NULL,
1804 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1805 strncmp (signal_spec, "object-signal::", 15) == 0)
1806 sid = g_signal_connect_object (object, signal_spec + 15,
1809 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1810 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1811 sid = g_signal_connect_data (object, signal_spec + 16,
1812 callback, data, NULL,
1814 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1815 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1816 sid = g_signal_connect_object (object, signal_spec + 23,
1819 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1820 strncmp (signal_spec, "signal-after::", 14) == 0)
1821 sid = g_signal_connect_data (object, signal_spec + 14,
1822 callback, data, NULL,
1824 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1825 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1826 sid = g_signal_connect_object (object, signal_spec + 21,
1829 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1830 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1831 sid = g_signal_connect_data (object, signal_spec + 22,
1832 callback, data, NULL,
1833 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1834 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1835 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1836 sid = g_signal_connect_object (object, signal_spec + 29,
1838 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1841 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1844 signal_spec = va_arg (var_args, gchar*);
1852 * g_object_disconnect:
1853 * @object: a #GObject
1854 * @signal_spec: the spec for the first signal
1855 * @...: #GCallback for the first signal, followed by data for the first signal,
1856 * followed optionally by more signal spec/callback/data triples,
1859 * A convenience function to disconnect multiple signals at once.
1861 * The signal specs expected by this function have the form "any_signal", which
1862 * means to disconnect any signal with matching callback and data, or
1863 * "any_signal::signal_name", which only disconnects the signal named "signal_name".
1866 g_object_disconnect (gpointer _object,
1867 const gchar *signal_spec,
1870 GObject *object = _object;
1873 g_return_if_fail (G_IS_OBJECT (object));
1874 g_return_if_fail (object->ref_count > 0);
1876 va_start (var_args, signal_spec);
1879 GCallback callback = va_arg (var_args, GCallback);
1880 gpointer data = va_arg (var_args, gpointer);
1881 guint sid = 0, detail = 0, mask = 0;
1883 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
1884 strncmp (signal_spec, "any-signal::", 12) == 0)
1887 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1889 else if (strcmp (signal_spec, "any_signal") == 0 ||
1890 strcmp (signal_spec, "any-signal") == 0)
1893 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
1897 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
1901 if ((mask & G_SIGNAL_MATCH_ID) &&
1902 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
1903 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
1904 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
1906 NULL, (gpointer)callback, data))
1907 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
1908 signal_spec = va_arg (var_args, gchar*);
1919 } weak_refs[1]; /* flexible array */
1923 weak_refs_notify (gpointer data)
1925 WeakRefStack *wstack = data;
1928 for (i = 0; i < wstack->n_weak_refs; i++)
1929 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
1934 * g_object_weak_ref:
1935 * @object: #GObject to reference weakly
1936 * @notify: callback to invoke before the object is freed
1937 * @data: extra data to pass to notify
1939 * Adds a weak reference callback to an object. Weak references are used for
1940 * notification when an object is finalized. They are called "weak references"
1941 * because they allow you to safely hold a pointer to an object without calling
1942 * g_object_ref() (g_object_ref() adds a strong reference, that is, forces the
1943 * object to stay alive).
1946 g_object_weak_ref (GObject *object,
1950 WeakRefStack *wstack;
1953 g_return_if_fail (G_IS_OBJECT (object));
1954 g_return_if_fail (notify != NULL);
1955 g_return_if_fail (object->ref_count >= 1);
1957 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
1960 i = wstack->n_weak_refs++;
1961 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
1965 wstack = g_renew (WeakRefStack, NULL, 1);
1966 wstack->object = object;
1967 wstack->n_weak_refs = 1;
1970 wstack->weak_refs[i].notify = notify;
1971 wstack->weak_refs[i].data = data;
1972 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
1976 * g_object_weak_unref:
1977 * @object: #GObject to remove a weak reference from
1978 * @notify: callback to search for
1979 * @data: data to search for
1981 * Removes a weak reference callback to an object.
1984 g_object_weak_unref (GObject *object,
1988 WeakRefStack *wstack;
1989 gboolean found_one = FALSE;
1991 g_return_if_fail (G_IS_OBJECT (object));
1992 g_return_if_fail (notify != NULL);
1994 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
1999 for (i = 0; i < wstack->n_weak_refs; i++)
2000 if (wstack->weak_refs[i].notify == notify &&
2001 wstack->weak_refs[i].data == data)
2004 wstack->n_weak_refs -= 1;
2005 if (i != wstack->n_weak_refs)
2006 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2012 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2016 * g_object_add_weak_pointer:
2017 * @object: The object that should be weak referenced.
2018 * @weak_pointer_location: The memory address of a pointer.
2020 * Adds a weak reference from weak_pointer to @object to indicate that
2021 * the pointer located at @weak_pointer_location is only valid during the
2022 * lifetime of @object. When the @object is finalized, @weak_pointer will
2026 g_object_add_weak_pointer (GObject *object,
2027 gpointer *weak_pointer_location)
2029 g_return_if_fail (G_IS_OBJECT (object));
2030 g_return_if_fail (weak_pointer_location != NULL);
2032 g_object_weak_ref (object,
2033 (GWeakNotify) g_nullify_pointer,
2034 weak_pointer_location);
2038 * g_object_remove_weak_pointer:
2039 * @object: The object that is weak referenced.
2040 * @weak_pointer_location: The memory address of a pointer.
2042 * Removes a weak reference from @object that was previously added
2043 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2044 * to match the one used with g_object_add_weak_pointer().
2047 g_object_remove_weak_pointer (GObject *object,
2048 gpointer *weak_pointer_location)
2050 g_return_if_fail (G_IS_OBJECT (object));
2051 g_return_if_fail (weak_pointer_location != NULL);
2053 g_object_weak_unref (object,
2054 (GWeakNotify) g_nullify_pointer,
2055 weak_pointer_location);
2059 object_floating_flag_handler (GObject *object,
2065 case +1: /* force floating if possible */
2067 oldvalue = g_atomic_pointer_get (&object->qdata);
2068 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2069 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2070 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2071 case -1: /* sink if possible */
2073 oldvalue = g_atomic_pointer_get (&object->qdata);
2074 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2075 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2076 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2077 default: /* check floating */
2078 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2083 * g_object_is_floating:
2084 * @object: a #GObject
2086 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2090 * Returns: %TRUE if @object has a floating reference
2093 g_object_is_floating (gpointer _object)
2095 GObject *object = _object;
2096 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2097 return floating_flag_handler (object, 0);
2101 * g_object_ref_sink:
2102 * @object: a #GObject
2104 * Increase the reference count of @object, and possibly remove the
2105 * <link linkend="floating-ref">floating</link> reference, if @object
2106 * has a floating reference.
2108 * In other words, if the object is floating, then this call "assumes
2109 * ownership" of the floating reference, converting it to a normal reference
2110 * by clearing the floating flag while leaving the reference count unchanged.
2111 * If the object is not floating, then this call adds a new normal reference
2112 * increasing the reference count by one.
2118 g_object_ref_sink (gpointer _object)
2120 GObject *object = _object;
2121 gboolean was_floating;
2122 g_return_val_if_fail (G_IS_OBJECT (object), object);
2123 g_return_val_if_fail (object->ref_count >= 1, object);
2124 g_object_ref (object);
2125 was_floating = floating_flag_handler (object, -1);
2127 g_object_unref (object);
2132 * g_object_force_floating:
2133 * @object: a #GObject
2135 * This function is intended for #GObject implementations to re-enforce a
2136 * <link linkend="floating-ref">floating</link> object reference.
2137 * Doing this is seldomly required, all
2138 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2139 * usually just needs to be sunken by calling g_object_ref_sink().
2144 g_object_force_floating (GObject *object)
2146 gboolean was_floating;
2147 g_return_if_fail (G_IS_OBJECT (object));
2148 g_return_if_fail (object->ref_count >= 1);
2150 was_floating = floating_flag_handler (object, +1);
2155 guint n_toggle_refs;
2157 GToggleNotify notify;
2159 } toggle_refs[1]; /* flexible array */
2163 toggle_refs_notify (GObject *object,
2164 gboolean is_last_ref)
2166 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2168 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2169 * will only be notified when there is exactly one of them.
2171 g_assert (tstack->n_toggle_refs == 1);
2172 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2176 * g_object_add_toggle_ref:
2177 * @object: a #GObject
2178 * @notify: a function to call when this reference is the
2179 * last reference to the object, or is no longer
2180 * the last reference.
2181 * @data: data to pass to @notify
2183 * Increases the reference count of the object by one and sets a
2184 * callback to be called when all other references to the object are
2185 * dropped, or when this is already the last reference to the object
2186 * and another reference is established.
2188 * This functionality is intended for binding @object to a proxy
2189 * object managed by another memory manager. This is done with two
2190 * paired references: the strong reference added by
2191 * g_object_add_toggle_ref() and a reverse reference to the proxy
2192 * object which is either a strong reference or weak reference.
2194 * The setup is that when there are no other references to @object,
2195 * only a weak reference is held in the reverse direction from @object
2196 * to the proxy object, but when there are other references held to
2197 * @object, a strong reference is held. The @notify callback is called
2198 * when the reference from @object to the proxy object should be
2199 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2200 * true) or weak to strong (@is_last_ref false).
2202 * Since a (normal) reference must be held to the object before
2203 * calling g_object_toggle_ref(), the initial state of the reverse
2204 * link is always strong.
2206 * Multiple toggle references may be added to the same gobject,
2207 * however if there are multiple toggle references to an object, none
2208 * of them will ever be notified until all but one are removed. For
2209 * this reason, you should only ever use a toggle reference if there
2210 * is important state in the proxy object.
2215 g_object_add_toggle_ref (GObject *object,
2216 GToggleNotify notify,
2219 ToggleRefStack *tstack;
2222 g_return_if_fail (G_IS_OBJECT (object));
2223 g_return_if_fail (notify != NULL);
2224 g_return_if_fail (object->ref_count >= 1);
2226 g_object_ref (object);
2228 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2231 i = tstack->n_toggle_refs++;
2232 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2233 * in tstate->toggle_refs */
2234 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2238 tstack = g_renew (ToggleRefStack, NULL, 1);
2239 tstack->object = object;
2240 tstack->n_toggle_refs = 1;
2244 /* Set a flag for fast lookup after adding the first toggle reference */
2245 if (tstack->n_toggle_refs == 1)
2246 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2248 tstack->toggle_refs[i].notify = notify;
2249 tstack->toggle_refs[i].data = data;
2250 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2251 (GDestroyNotify)g_free);
2255 * g_object_remove_toggle_ref:
2256 * @object: a #GObject
2257 * @notify: a function to call when this reference is the
2258 * last reference to the object, or is no longer
2259 * the last reference.
2260 * @data: data to pass to @notify
2262 * Removes a reference added with g_object_add_toggle_ref(). The
2263 * reference count of the object is decreased by one.
2268 g_object_remove_toggle_ref (GObject *object,
2269 GToggleNotify notify,
2272 ToggleRefStack *tstack;
2273 gboolean found_one = FALSE;
2275 g_return_if_fail (G_IS_OBJECT (object));
2276 g_return_if_fail (notify != NULL);
2278 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2283 for (i = 0; i < tstack->n_toggle_refs; i++)
2284 if (tstack->toggle_refs[i].notify == notify &&
2285 tstack->toggle_refs[i].data == data)
2288 tstack->n_toggle_refs -= 1;
2289 if (i != tstack->n_toggle_refs)
2290 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2292 if (tstack->n_toggle_refs == 0)
2293 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2295 g_object_unref (object);
2302 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2307 * @object: a #GObject
2309 * Increases the reference count of @object.
2311 * Returns: the same @object
2314 g_object_ref (gpointer _object)
2316 GObject *object = _object;
2319 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2320 g_return_val_if_fail (object->ref_count > 0, NULL);
2322 #ifdef G_ENABLE_DEBUG
2323 if (g_trap_object_ref == object)
2325 #endif /* G_ENABLE_DEBUG */
2328 old_val = g_atomic_int_exchange_and_add (&object->ref_count, 1);
2330 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2331 toggle_refs_notify (object, FALSE);
2338 * @object: a #GObject
2340 * Decreases the reference count of @object.
2341 * When its reference count drops to 0, the object is finalized
2342 * (i.e. its memory is freed).
2345 g_object_unref (gpointer _object)
2347 GObject *object = _object;
2351 g_return_if_fail (G_IS_OBJECT (object));
2352 g_return_if_fail (object->ref_count > 0);
2354 #ifdef G_ENABLE_DEBUG
2355 if (g_trap_object_ref == object)
2357 #endif /* G_ENABLE_DEBUG */
2359 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2360 retry_atomic_decrement1:
2361 old_ref = g_atomic_int_get (&object->ref_count);
2364 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2365 goto retry_atomic_decrement1;
2367 /* if we went from 2->1 we need to notify toggle refs if any */
2368 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2369 toggle_refs_notify (object, TRUE);
2373 /* we are about tp remove the last reference */
2374 G_OBJECT_GET_CLASS (object)->dispose (object);
2376 /* may have been re-referenced meanwhile */
2377 retry_atomic_decrement2:
2378 old_ref = g_atomic_int_get (&object->ref_count);
2381 if (!g_atomic_int_compare_and_exchange (&object->ref_count, old_ref, old_ref - 1))
2382 goto retry_atomic_decrement2;
2384 /* if we went from 2->1 we need to notify toggle refs if any */
2385 if (old_ref == 2 && OBJECT_HAS_TOGGLE_REF (object))
2386 toggle_refs_notify (object, TRUE);
2391 /* we are still in the process of taking away the last ref */
2392 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2393 g_signal_handlers_destroy (object);
2394 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2396 /* decrement the last reference */
2397 is_zero = g_atomic_int_dec_and_test (&object->ref_count);
2399 /* may have been re-referenced meanwhile */
2400 if (G_LIKELY (is_zero))
2402 G_OBJECT_GET_CLASS (object)->finalize (object);
2403 #ifdef G_ENABLE_DEBUG
2406 /* catch objects not chaining finalize handlers */
2407 G_LOCK (debug_objects);
2408 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2409 G_UNLOCK (debug_objects);
2411 #endif /* G_ENABLE_DEBUG */
2412 g_type_free_instance ((GTypeInstance*) object);
2418 * g_object_get_qdata:
2419 * @object: The GObject to get a stored user data pointer from
2420 * @quark: A #GQuark, naming the user data pointer
2422 * This function gets back user data pointers stored via
2423 * g_object_set_qdata().
2425 * Returns: The user data pointer set, or %NULL
2428 g_object_get_qdata (GObject *object,
2431 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2433 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2437 * g_object_set_qdata:
2438 * @object: The GObject to set store a user data pointer
2439 * @quark: A #GQuark, naming the user data pointer
2440 * @data: An opaque user data pointer
2442 * This sets an opaque, named pointer on an object.
2443 * The name is specified through a #GQuark (retrived e.g. via
2444 * g_quark_from_static_string()), and the pointer
2445 * can be gotten back from the @object with g_object_get_qdata()
2446 * until the @object is finalized.
2447 * Setting a previously set user data pointer, overrides (frees)
2448 * the old pointer set, using #NULL as pointer essentially
2449 * removes the data stored.
2452 g_object_set_qdata (GObject *object,
2456 g_return_if_fail (G_IS_OBJECT (object));
2457 g_return_if_fail (quark > 0);
2459 g_datalist_id_set_data (&object->qdata, quark, data);
2463 * g_object_set_qdata_full:
2464 * @object: The GObject to set store a user data pointer
2465 * @quark: A #GQuark, naming the user data pointer
2466 * @data: An opaque user data pointer
2467 * @destroy: Function to invoke with @data as argument, when @data needs to be freed
2469 * This function works like g_object_set_qdata(), but in addition,
2470 * a void (*destroy) (gpointer) function may be specified which is
2471 * called with @data as argument when the @object is finalized, or
2472 * the data is being overwritten by a call to g_object_set_qdata()
2473 * with the same @quark.
2476 g_object_set_qdata_full (GObject *object,
2479 GDestroyNotify destroy)
2481 g_return_if_fail (G_IS_OBJECT (object));
2482 g_return_if_fail (quark > 0);
2484 g_datalist_id_set_data_full (&object->qdata, quark, data,
2485 data ? destroy : (GDestroyNotify) NULL);
2489 * g_object_steal_qdata:
2490 * @object: The GObject to get a stored user data pointer from
2491 * @quark: A #GQuark, naming the user data pointer
2493 * This function gets back user data pointers stored via
2494 * g_object_set_qdata() and removes the @data from object
2495 * without invoking it's destroy() function (if any was
2497 * Usually, calling this function is only required to update
2498 * user data pointers with a destroy notifier, for example:
2501 * object_add_to_user_list (GObject *object,
2502 * const gchar *new_string)
2504 * // the quark, naming the object data
2505 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2506 * // retrive the old string list
2507 * GList *list = g_object_steal_qdata (object, quark_string_list);
2509 * // prepend new string
2510 * list = g_list_prepend (list, g_strdup (new_string));
2511 * // this changed 'list', so we need to set it again
2512 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2515 * free_string_list (gpointer data)
2517 * GList *node, *list = data;
2519 * for (node = list; node; node = node->next)
2520 * g_free (node->data);
2521 * g_list_free (list);
2524 * Using g_object_get_qdata() in the above example, instead of g_object_steal_qdata()
2525 * would have left the destroy function set, and thus the partial string list would
2526 * have been freed upon g_object_set_qdata_full().
2528 * Returns: The user data pointer set, or %NULL
2531 g_object_steal_qdata (GObject *object,
2534 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2535 g_return_val_if_fail (quark > 0, NULL);
2537 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2541 * g_object_get_data:
2542 * @object: #GObject containing the associations
2543 * @key: name of the key for that association
2545 * Gets a named field from the objects table of associations (see g_object_set_data()).
2547 * Returns: the data if found, or %NULL if no such data exists.
2550 g_object_get_data (GObject *object,
2555 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2556 g_return_val_if_fail (key != NULL, NULL);
2558 quark = g_quark_try_string (key);
2560 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2564 * g_object_set_data:
2565 * @object: #GObject containing the associations.
2566 * @key: name of the key
2567 * @data: data to associate with that key
2569 * Each object carries around a table of associations from
2570 * strings to pointers. This function lets you set an association.
2572 * If the object already had an association with that name,
2573 * the old association will be destroyed.
2576 g_object_set_data (GObject *object,
2580 g_return_if_fail (G_IS_OBJECT (object));
2581 g_return_if_fail (key != NULL);
2583 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2587 * g_object_set_data_full:
2588 * @object: #GObject containing the associations
2589 * @key: name of the key
2590 * @data: data to associate with that key
2591 * @destroy: function to call when the association is destroyed
2593 * Like g_object_set_data() except it adds notification
2594 * for when the association is destroyed, either by setting it
2595 * to a different value or when the object is destroyed.
2597 * Note that the @destroy callback is not called if @data is %NULL.
2600 g_object_set_data_full (GObject *object,
2603 GDestroyNotify destroy)
2605 g_return_if_fail (G_IS_OBJECT (object));
2606 g_return_if_fail (key != NULL);
2608 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2609 data ? destroy : (GDestroyNotify) NULL);
2613 * g_object_steal_data:
2614 * @object: #GObject containing the associations
2615 * @key: name of the key
2617 * Remove a specified datum from the object's data associations,
2618 * without invoking the association's destroy handler.
2620 * Returns: the data if found, or %NULL if no such data exists.
2623 g_object_steal_data (GObject *object,
2628 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2629 g_return_val_if_fail (key != NULL, NULL);
2631 quark = g_quark_try_string (key);
2633 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2637 g_value_object_init (GValue *value)
2639 value->data[0].v_pointer = NULL;
2643 g_value_object_free_value (GValue *value)
2645 if (value->data[0].v_pointer)
2646 g_object_unref (value->data[0].v_pointer);
2650 g_value_object_copy_value (const GValue *src_value,
2653 if (src_value->data[0].v_pointer)
2654 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2656 dest_value->data[0].v_pointer = NULL;
2660 g_value_object_transform_value (const GValue *src_value,
2663 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)))
2664 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2666 dest_value->data[0].v_pointer = NULL;
2670 g_value_object_peek_pointer (const GValue *value)
2672 return value->data[0].v_pointer;
2676 g_value_object_collect_value (GValue *value,
2677 guint n_collect_values,
2678 GTypeCValue *collect_values,
2679 guint collect_flags)
2681 if (collect_values[0].v_pointer)
2683 GObject *object = collect_values[0].v_pointer;
2685 if (object->g_type_instance.g_class == NULL)
2686 return g_strconcat ("invalid unclassed object pointer for value type `",
2687 G_VALUE_TYPE_NAME (value),
2690 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2691 return g_strconcat ("invalid object type `",
2692 G_OBJECT_TYPE_NAME (object),
2693 "' for value type `",
2694 G_VALUE_TYPE_NAME (value),
2697 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2698 value->data[0].v_pointer = g_object_ref (object);
2701 value->data[0].v_pointer = NULL;
2707 g_value_object_lcopy_value (const GValue *value,
2708 guint n_collect_values,
2709 GTypeCValue *collect_values,
2710 guint collect_flags)
2712 GObject **object_p = collect_values[0].v_pointer;
2715 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2717 if (!value->data[0].v_pointer)
2719 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2720 *object_p = value->data[0].v_pointer;
2722 *object_p = g_object_ref (value->data[0].v_pointer);
2728 * g_value_set_object:
2729 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2730 * @v_object: object value to be set
2732 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2734 * g_value_set_object() increases the reference count of @v_object
2735 * (the #GValue holds a reference to @v_object).
2736 * If you do not wish to increase the reference count of the object
2737 * (i.e. you wish to pass your current reference to the #GValue because you no
2739 * use g_value_take_object() instead.
2741 * It is important that your #GValue holds a reference to @v_object (either its
2742 * own, or one it has taken) to ensure that the object won't be destroyed while
2743 * the #GValue still exists).
2746 g_value_set_object (GValue *value,
2751 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2753 old = value->data[0].v_pointer;
2757 g_return_if_fail (G_IS_OBJECT (v_object));
2758 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2760 value->data[0].v_pointer = v_object;
2761 g_object_ref (value->data[0].v_pointer);
2764 value->data[0].v_pointer = NULL;
2767 g_object_unref (old);
2771 * g_value_set_object_take_ownership:
2772 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2773 * @v_object: object value to be set
2775 * This is an internal function introduced mainly for C marshallers.
2777 * Deprecated: 2.4: Use g_value_take_object() instead.
2780 g_value_set_object_take_ownership (GValue *value,
2783 g_value_take_object (value, v_object);
2787 * g_value_take_object:
2788 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2789 * @v_object: object value to be set
2791 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2792 * and takes over the ownership of the callers reference to @v_object;
2793 * the caller doesn't have to unref it any more (i.e. the reference
2794 * count of the object is not increased).
2796 * If you want the #GValue to hold its own reference to @v_object, use
2797 * g_value_set_object() instead.
2802 g_value_take_object (GValue *value,
2805 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2807 if (value->data[0].v_pointer)
2809 g_object_unref (value->data[0].v_pointer);
2810 value->data[0].v_pointer = NULL;
2815 g_return_if_fail (G_IS_OBJECT (v_object));
2816 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2818 value->data[0].v_pointer = v_object; /* we take over the reference count */
2823 * g_value_get_object:
2824 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2826 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
2828 * Returns: object contents of @value
2831 g_value_get_object (const GValue *value)
2833 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2835 return value->data[0].v_pointer;
2839 * g_value_dup_object:
2840 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
2842 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing its reference count.
2844 * Returns: object content of @value, should be unreferenced when no longer needed.
2847 g_value_dup_object (const GValue *value)
2849 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
2851 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
2855 * g_signal_connect_object:
2856 * @instance: the instance to connect to.
2857 * @detailed_signal: a string of the form "signal-name::detail".
2858 * @c_handler: the #GCallback to connect.
2859 * @gobject: the object to pass as data to @c_handler.
2860 * @connect_flags: a combination of #GConnnectFlags.
2862 * This is similar to g_signal_connect_data(), but uses a closure which
2863 * ensures that the @gobject stays alive during the call to @c_handler
2864 * by temporarily adding a reference count to @gobject.
2866 * Note that there is a bug in GObject that makes this function
2867 * much less useful than it might seem otherwise. Once @gobject is
2868 * disposed, the callback will no longer be called, but, the signal
2869 * handler is <emphasis>not</emphasis> currently disconnected. If the
2870 * @instance is itself being freed at the same time than this doesn't
2871 * matter, since the signal will automatically be removed, but
2872 * if @instance persists, then the signal handler will leak. You
2873 * should not remove the signal yourself because in a future versions of
2874 * GObject, the handler <emphasis>will</emphasis> automatically
2877 * It's possible to work around this problem in a way that will
2878 * continue to work with future versions of GObject by checking
2879 * that the signal handler is still connected before disconnected it:
2880 * <informalexample><programlisting>
2881 * if (g_signal_handler_is_connected (instance, id))
2882 * g_signal_handler_disconnect (instance, id);
2883 * </programlisting></informalexample>
2885 * Returns: the handler id.
2888 g_signal_connect_object (gpointer instance,
2889 const gchar *detailed_signal,
2890 GCallback c_handler,
2892 GConnectFlags connect_flags)
2894 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2895 g_return_val_if_fail (detailed_signal != NULL, 0);
2896 g_return_val_if_fail (c_handler != NULL, 0);
2902 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
2904 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
2906 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
2909 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
2915 GClosure *closures[1]; /* flexible array */
2917 /* don't change this structure without supplying an accessor for
2918 * watched closures, e.g.:
2919 * GSList* g_object_list_watched_closures (GObject *object)
2922 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2923 * carray = g_object_get_data (object, "GObject-closure-array");
2926 * GSList *slist = NULL;
2928 * for (i = 0; i < carray->n_closures; i++)
2929 * slist = g_slist_prepend (slist, carray->closures[i]);
2937 object_remove_closure (gpointer data,
2940 GObject *object = data;
2941 CArray *carray = g_object_get_qdata (object, quark_closure_array);
2944 for (i = 0; i < carray->n_closures; i++)
2945 if (carray->closures[i] == closure)
2947 carray->n_closures--;
2948 if (i < carray->n_closures)
2949 carray->closures[i] = carray->closures[carray->n_closures];
2952 g_assert_not_reached ();
2956 destroy_closure_array (gpointer data)
2958 CArray *carray = data;
2959 GObject *object = carray->object;
2960 guint i, n = carray->n_closures;
2962 for (i = 0; i < n; i++)
2964 GClosure *closure = carray->closures[i];
2966 /* removing object_remove_closure() upfront is probably faster than
2967 * letting it fiddle with quark_closure_array which is empty anyways
2969 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
2970 g_closure_invalidate (closure);
2976 * g_object_watch_closure:
2977 * @object: GObject restricting lifetime of @closure
2978 * @closure: GClosure to watch
2980 * This function essentially limits the life time of the @closure
2981 * to the life time of the object. That is, when the object is finalized,
2982 * the @closure is invalidated by calling g_closure_invalidate() on it,
2983 * in order to prevent invocations of the closure with a finalized
2984 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are added
2985 * as marshal guards to the @closure, to ensure that an extra reference
2986 * count is held on @object during invocation of the @closure.
2987 * Usually, this function will be called on closures that use this @object
2991 g_object_watch_closure (GObject *object,
2997 g_return_if_fail (G_IS_OBJECT (object));
2998 g_return_if_fail (closure != NULL);
2999 g_return_if_fail (closure->is_invalid == FALSE);
3000 g_return_if_fail (closure->in_marshal == FALSE);
3001 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3003 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3004 g_closure_add_marshal_guards (closure,
3005 object, (GClosureNotify) g_object_ref,
3006 object, (GClosureNotify) g_object_unref);
3007 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3010 carray = g_renew (CArray, NULL, 1);
3011 carray->object = object;
3012 carray->n_closures = 1;
3017 i = carray->n_closures++;
3018 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3020 carray->closures[i] = closure;
3021 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3025 * g_closure_new_object:
3026 * @sizeof_closure: the size of the structure to allocate, must be at least
3027 * <literal>sizeof (GClosure)</literal>
3028 * @object: a #GObject pointer to store in the @data field of the newly
3029 * allocated #GClosure
3031 * A variant of g_closure_new_simple() which stores @object in the @data
3032 * field of the closure and calls g_object_watch_closure() on @object and the
3033 * created closure. This function is mainly useful when implementing new types
3036 * Returns: a newly allocated #GClosure
3039 g_closure_new_object (guint sizeof_closure,
3044 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3045 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3047 closure = g_closure_new_simple (sizeof_closure, object);
3048 g_object_watch_closure (object, closure);
3054 * g_cclosure_new_object:
3055 * @callback_func: the function to invoke
3056 * @object: a #GObject pointer to pass to @callback_func
3058 * A variant of g_cclosure_new() which uses @object as @user_data and calls
3059 * g_object_watch_closure() on @object and the created closure. This function
3060 * is useful when you have a callback closely associated with a #GObject,
3061 * and want the callback to no longer run after the object is is freed.
3063 * Returns: a new #GCClosure
3066 g_cclosure_new_object (GCallback callback_func,
3071 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3072 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3073 g_return_val_if_fail (callback_func != NULL, NULL);
3075 closure = g_cclosure_new (callback_func, object, NULL);
3076 g_object_watch_closure (object, closure);
3082 * g_cclosure_new_object_swap:
3083 * @callback_func: the function to invoke
3084 * @object: a #GObject pointer to pass to @callback_func
3086 * A variant of g_cclosure_new_swap() which uses @object as @user_data and calls
3087 * g_object_watch_closure() on @object and the created closure. This function
3088 * is useful when you have a callback closely associated with a #GObject,
3089 * and want the callback to no longer run after the object is is freed.
3091 * Returns: a new #GCClosure
3094 g_cclosure_new_object_swap (GCallback callback_func,
3099 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3100 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3101 g_return_val_if_fail (callback_func != NULL, NULL);
3103 closure = g_cclosure_new_swap (callback_func, object, NULL);
3104 g_object_watch_closure (object, closure);
3110 g_object_compat_control (gsize what,
3116 case 1: /* floating base type */
3117 return G_TYPE_INITIALLY_UNOWNED;
3118 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3119 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3121 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3123 *pp = floating_flag_handler;
3130 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3133 g_initially_unowned_init (GInitiallyUnowned *object)
3135 g_object_force_floating (object);
3139 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
3143 #define __G_OBJECT_C__
3144 #include "gobjectaliasdef.c"