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 "gobject_trace.h"
38 #include "gobjectnotifyqueue.c"
42 * @short_description: The base object type
43 * @see_also: #GParamSpecObject, g_param_spec_object()
44 * @title: The Base Object Type
46 * GObject is the fundamental type providing the common attributes and
47 * methods for all object types in GTK+, Pango and other libraries
48 * based on GObject. The GObject class provides methods for object
49 * construction and destruction, property access methods, and signal
50 * support. Signals are described in detail in <xref
51 * linkend="gobject-Signals"/>.
53 * <para id="floating-ref">
54 * #GInitiallyUnowned is derived from #GObject. The only difference between
55 * the two is that the initial reference of a #GInitiallyUnowned is flagged
56 * as a <firstterm>floating</firstterm> reference.
57 * This means that it is not specifically claimed to be "owned" by
58 * any code portion. The main motivation for providing floating references is
59 * C convenience. In particular, it allows code to be written as:
61 * container = create_container();
62 * container_add_child (container, create_child());
64 * If <function>container_add_child()</function> will g_object_ref_sink() the
65 * passed in child, no reference of the newly created child is leaked.
66 * Without floating references, <function>container_add_child()</function>
67 * can only g_object_ref() the new child, so to implement this code without
68 * reference leaks, it would have to be written as:
71 * container = create_container();
72 * child = create_child();
73 * container_add_child (container, child);
74 * g_object_unref (child);
76 * The floating reference can be converted into
77 * an ordinary reference by calling g_object_ref_sink().
78 * For already sunken objects (objects that don't have a floating reference
79 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
81 * Since floating references are useful almost exclusively for C convenience,
82 * language bindings that provide automated reference and memory ownership
83 * maintenance (such as smart pointers or garbage collection) therefore don't
84 * need to expose floating references in their API.
87 * Some object implementations may need to save an objects floating state
88 * across certain code portions (an example is #GtkMenu), to achive this, the
89 * following sequence can be used:
92 * // save floating state
93 * gboolean was_floating = g_object_is_floating (object);
94 * g_object_ref_sink (object);
95 * // protected code portion
97 * // restore floating state
99 * g_object_force_floating (object);
100 * g_obejct_unref (object); // release previously acquired reference
106 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
107 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
109 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
110 #define OBJECT_HAS_TOGGLE_REF(object) \
111 ((G_DATALIST_GET_FLAGS (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
112 #define OBJECT_FLOATING_FLAG 0x2
114 #define CLASS_HAS_PROPS_FLAG 0x1
115 #define CLASS_HAS_PROPS(class) \
116 ((class)->flags & CLASS_HAS_PROPS_FLAG)
117 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
118 ((class)->constructor != g_object_constructor)
120 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
121 #define CLASS_HAS_DERIVED_CLASS(class) \
122 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
124 /* --- signals --- */
131 /* --- properties --- */
137 /* --- prototypes --- */
138 static void g_object_base_class_init (GObjectClass *class);
139 static void g_object_base_class_finalize (GObjectClass *class);
140 static void g_object_do_class_init (GObjectClass *class);
141 static void g_object_init (GObject *object,
142 GObjectClass *class);
143 static GObject* g_object_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void g_object_real_dispose (GObject *object);
147 static void g_object_finalize (GObject *object);
148 static void g_object_do_set_property (GObject *object,
152 static void g_object_do_get_property (GObject *object,
156 static void g_value_object_init (GValue *value);
157 static void g_value_object_free_value (GValue *value);
158 static void g_value_object_copy_value (const GValue *src_value,
160 static void g_value_object_transform_value (const GValue *src_value,
162 static gpointer g_value_object_peek_pointer (const GValue *value);
163 static gchar* g_value_object_collect_value (GValue *value,
164 guint n_collect_values,
165 GTypeCValue *collect_values,
166 guint collect_flags);
167 static gchar* g_value_object_lcopy_value (const GValue *value,
168 guint n_collect_values,
169 GTypeCValue *collect_values,
170 guint collect_flags);
171 static void g_object_dispatch_properties_changed (GObject *object,
173 GParamSpec **pspecs);
174 static inline void object_get_property (GObject *object,
177 static inline void object_set_property (GObject *object,
180 GObjectNotifyQueue *nqueue);
181 static guint object_floating_flag_handler (GObject *object,
184 static void object_interface_check_properties (gpointer func_data,
188 /* --- variables --- */
189 static GQuark quark_closure_array = 0;
190 static GQuark quark_weak_refs = 0;
191 static GQuark quark_toggle_refs = 0;
192 static GParamSpecPool *pspec_pool = NULL;
193 static GObjectNotifyContext property_notify_context = { 0, };
194 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
195 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
196 G_LOCK_DEFINE_STATIC (construction_mutex);
197 static GSList *construction_objects = NULL;
199 /* --- functions --- */
200 #ifdef G_ENABLE_DEBUG
201 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
202 G_LOCK_DEFINE_STATIC (debug_objects);
203 static volatile GObject *g_trap_object_ref = NULL;
204 static guint debug_objects_count = 0;
205 static GHashTable *debug_objects_ht = NULL;
208 debug_objects_foreach (gpointer key,
212 GObject *object = value;
214 g_message ("[%p] stale %s\tref_count=%u",
216 G_OBJECT_TYPE_NAME (object),
221 debug_objects_atexit (void)
225 G_LOCK (debug_objects);
226 g_message ("stale GObjects: %u", debug_objects_count);
227 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
228 G_UNLOCK (debug_objects);
231 #endif /* G_ENABLE_DEBUG */
234 g_object_type_init (void)
236 static gboolean initialized = FALSE;
237 static const GTypeFundamentalInfo finfo = {
238 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
240 static GTypeInfo info = {
241 sizeof (GObjectClass),
242 (GBaseInitFunc) g_object_base_class_init,
243 (GBaseFinalizeFunc) g_object_base_class_finalize,
244 (GClassInitFunc) g_object_do_class_init,
245 NULL /* class_destroy */,
246 NULL /* class_data */,
249 (GInstanceInitFunc) g_object_init,
250 NULL, /* value_table */
252 static const GTypeValueTable value_table = {
253 g_value_object_init, /* value_init */
254 g_value_object_free_value, /* value_free */
255 g_value_object_copy_value, /* value_copy */
256 g_value_object_peek_pointer, /* value_peek_pointer */
257 "p", /* collect_format */
258 g_value_object_collect_value, /* collect_value */
259 "p", /* lcopy_format */
260 g_value_object_lcopy_value, /* lcopy_value */
264 g_return_if_fail (initialized == FALSE);
269 info.value_table = &value_table;
270 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
271 g_assert (type == G_TYPE_OBJECT);
272 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
274 #ifdef G_ENABLE_DEBUG
277 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
278 g_atexit (debug_objects_atexit);
280 #endif /* G_ENABLE_DEBUG */
284 g_object_base_class_init (GObjectClass *class)
286 GObjectClass *pclass = g_type_class_peek_parent (class);
288 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
289 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
292 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
294 /* reset instance specific fields and methods that don't get inherited */
295 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
296 class->get_property = NULL;
297 class->set_property = NULL;
301 g_object_base_class_finalize (GObjectClass *class)
305 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
307 g_slist_free (class->construct_properties);
308 class->construct_properties = NULL;
309 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
310 for (node = list; node; node = node->next)
312 GParamSpec *pspec = node->data;
314 g_param_spec_pool_remove (pspec_pool, pspec);
315 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
316 g_param_spec_unref (pspec);
322 g_object_notify_dispatcher (GObject *object,
326 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
330 g_object_do_class_init (GObjectClass *class)
332 /* read the comment about typedef struct CArray; on why not to change this quark */
333 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
335 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
336 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
337 pspec_pool = g_param_spec_pool_new (TRUE);
338 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
339 property_notify_context.dispatcher = g_object_notify_dispatcher;
341 class->constructor = g_object_constructor;
342 class->set_property = g_object_do_set_property;
343 class->get_property = g_object_do_get_property;
344 class->dispose = g_object_real_dispose;
345 class->finalize = g_object_finalize;
346 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
347 class->notify = NULL;
351 * @gobject: the object which received the signal.
352 * @pspec: the #GParamSpec of the property which changed.
354 * The notify signal is emitted on an object when one of its
355 * properties has been changed. Note that getting this signal
356 * doesn't guarantee that the value of the property has actually
357 * changed, it may also be emitted when the setter for the property
358 * is called to reinstate the previous value.
360 * This signal is typically used to obtain change notification for a
361 * single property, by specifying the property name as a detail in the
362 * g_signal_connect() call, like this:
364 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
365 * G_CALLBACK (gtk_text_view_target_list_notify),
368 * It is important to note that you must use
369 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
370 * detail strings for the notify signal.
372 gobject_signals[NOTIFY] =
373 g_signal_new (g_intern_static_string ("notify"),
374 G_TYPE_FROM_CLASS (class),
375 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
376 G_STRUCT_OFFSET (GObjectClass, notify),
378 g_cclosure_marshal_VOID__PARAM,
382 /* Install a check function that we'll use to verify that classes that
383 * implement an interface implement all properties for that interface
385 g_type_add_interface_check (NULL, object_interface_check_properties);
389 install_property_internal (GType g_type,
393 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
395 g_warning ("When installing property: type `%s' already has a property named `%s'",
396 g_type_name (g_type),
401 g_param_spec_ref (pspec);
402 g_param_spec_sink (pspec);
403 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
404 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
408 * g_object_class_install_property:
409 * @oclass: a #GObjectClass
410 * @property_id: the id for the new property
411 * @pspec: the #GParamSpec for the new property
413 * Installs a new property. This is usually done in the class initializer.
415 * Note that it is possible to redefine a property in a derived class,
416 * by installing a property with the same name. This can be useful at times,
417 * e.g. to change the range of allowed values or the default value.
420 g_object_class_install_property (GObjectClass *class,
424 g_return_if_fail (G_IS_OBJECT_CLASS (class));
425 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
427 if (CLASS_HAS_DERIVED_CLASS (class))
428 g_error ("Attempt to add property %s::%s to class after it was derived",
429 G_OBJECT_CLASS_NAME (class), pspec->name);
431 class->flags |= CLASS_HAS_PROPS_FLAG;
433 if (pspec->flags & G_PARAM_WRITABLE)
434 g_return_if_fail (class->set_property != NULL);
435 if (pspec->flags & G_PARAM_READABLE)
436 g_return_if_fail (class->get_property != NULL);
437 g_return_if_fail (property_id > 0);
438 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
439 if (pspec->flags & G_PARAM_CONSTRUCT)
440 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
441 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
442 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
444 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
446 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
447 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
449 /* for property overrides of construct poperties, we have to get rid
450 * of the overidden inherited construct property
452 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
453 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
454 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
458 * g_object_interface_install_property:
459 * @g_iface: any interface vtable for the interface, or the default
460 * vtable for the interface.
461 * @pspec: the #GParamSpec for the new property
463 * Add a property to an interface; this is only useful for interfaces
464 * that are added to GObject-derived types. Adding a property to an
465 * interface forces all objects classes with that interface to have a
466 * compatible property. The compatible property could be a newly
467 * created #GParamSpec, but normally
468 * g_object_class_override_property() will be used so that the object
469 * class only needs to provide an implementation and inherits the
470 * property description, default value, bounds, and so forth from the
471 * interface property.
473 * This function is meant to be called from the interface's default
474 * vtable initialization function (the @class_init member of
475 * #GTypeInfo.) It must not be called after after @class_init has
476 * been called for any object types implementing this interface.
481 g_object_interface_install_property (gpointer g_iface,
484 GTypeInterface *iface_class = g_iface;
486 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
487 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
488 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
489 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
491 install_property_internal (iface_class->g_type, 0, pspec);
495 * g_object_class_find_property:
496 * @oclass: a #GObjectClass
497 * @property_name: the name of the property to look up
499 * Looks up the #GParamSpec for a property of a class.
501 * Returns: the #GParamSpec for the property, or %NULL if the class
502 * doesn't have a property of that name
505 g_object_class_find_property (GObjectClass *class,
506 const gchar *property_name)
509 GParamSpec *redirect;
511 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
512 g_return_val_if_fail (property_name != NULL, NULL);
514 pspec = g_param_spec_pool_lookup (pspec_pool,
516 G_OBJECT_CLASS_TYPE (class),
520 redirect = g_param_spec_get_redirect_target (pspec);
531 * g_object_interface_find_property:
532 * @g_iface: any interface vtable for the interface, or the default
533 * vtable for the interface
534 * @property_name: name of a property to lookup.
536 * Find the #GParamSpec with the given name for an
537 * interface. Generally, the interface vtable passed in as @g_iface
538 * will be the default vtable from g_type_default_interface_ref(), or,
539 * if you know the interface has already been loaded,
540 * g_type_default_interface_peek().
544 * Returns: the #GParamSpec for the property of the interface with the
545 * name @property_name, or %NULL if no such property exists.
548 g_object_interface_find_property (gpointer g_iface,
549 const gchar *property_name)
551 GTypeInterface *iface_class = g_iface;
553 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
554 g_return_val_if_fail (property_name != NULL, NULL);
556 return g_param_spec_pool_lookup (pspec_pool,
563 * g_object_class_override_property:
564 * @oclass: a #GObjectClass
565 * @property_id: the new property ID
566 * @name: the name of a property registered in a parent class or
567 * in an interface of this class.
569 * Registers @property_id as referring to a property with the
570 * name @name in a parent class or in an interface implemented
571 * by @oclass. This allows this class to <firstterm>override</firstterm>
572 * a property implementation in a parent class or to provide
573 * the implementation of a property from an interface.
576 * Internally, overriding is implemented by creating a property of type
577 * #GParamSpecOverride; generally operations that query the properties of
578 * the object class, such as g_object_class_find_property() or
579 * g_object_class_list_properties() will return the overridden
580 * property. However, in one case, the @construct_properties argument of
581 * the @constructor virtual function, the #GParamSpecOverride is passed
582 * instead, so that the @param_id field of the #GParamSpec will be
583 * correct. For virtually all uses, this makes no difference. If you
584 * need to get the overridden property, you can call
585 * g_param_spec_get_redirect_target().
591 g_object_class_override_property (GObjectClass *oclass,
595 GParamSpec *overridden = NULL;
599 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
600 g_return_if_fail (property_id > 0);
601 g_return_if_fail (name != NULL);
603 /* Find the overridden property; first check parent types
605 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
606 if (parent_type != G_TYPE_NONE)
607 overridden = g_param_spec_pool_lookup (pspec_pool,
616 /* Now check interfaces
618 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
619 while (n_ifaces-- && !overridden)
621 overridden = g_param_spec_pool_lookup (pspec_pool,
632 g_warning ("%s: Can't find property to override for '%s::%s'",
633 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
637 new = g_param_spec_override (name, overridden);
638 g_object_class_install_property (oclass, property_id, new);
642 * g_object_class_list_properties:
643 * @oclass: a #GObjectClass
644 * @n_properties: return location for the length of the returned array
646 * Get an array of #GParamSpec* for all properties of a class.
648 * Returns: an array of #GParamSpec* which should be freed after use
650 GParamSpec** /* free result */
651 g_object_class_list_properties (GObjectClass *class,
652 guint *n_properties_p)
657 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
659 pspecs = g_param_spec_pool_list (pspec_pool,
660 G_OBJECT_CLASS_TYPE (class),
669 * g_object_interface_list_properties:
670 * @g_iface: any interface vtable for the interface, or the default
671 * vtable for the interface
672 * @n_properties_p: location to store number of properties returned.
674 * Lists the properties of an interface.Generally, the interface
675 * vtable passed in as @g_iface will be the default vtable from
676 * g_type_default_interface_ref(), or, if you know the interface has
677 * already been loaded, g_type_default_interface_peek().
681 * Returns: a pointer to an array of pointers to #GParamSpec
682 * structures. The paramspecs are owned by GLib, but the
683 * array should be freed with g_free() when you are done with
687 g_object_interface_list_properties (gpointer g_iface,
688 guint *n_properties_p)
690 GTypeInterface *iface_class = g_iface;
694 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
696 pspecs = g_param_spec_pool_list (pspec_pool,
706 g_object_init (GObject *object,
709 object->ref_count = 1;
710 g_datalist_init (&object->qdata);
712 if (CLASS_HAS_PROPS (class))
714 /* freeze object's notification queue, g_object_newv() preserves pairedness */
715 g_object_notify_queue_freeze (object, &property_notify_context);
718 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
720 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
721 G_LOCK (construction_mutex);
722 construction_objects = g_slist_prepend (construction_objects, object);
723 G_UNLOCK (construction_mutex);
726 #ifdef G_ENABLE_DEBUG
729 G_LOCK (debug_objects);
730 debug_objects_count++;
731 g_hash_table_insert (debug_objects_ht, object, object);
732 G_UNLOCK (debug_objects);
734 #endif /* G_ENABLE_DEBUG */
738 g_object_do_set_property (GObject *object,
746 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
752 g_object_do_get_property (GObject *object,
760 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
766 g_object_real_dispose (GObject *object)
768 g_signal_handlers_destroy (object);
769 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
770 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
774 g_object_finalize (GObject *object)
776 g_datalist_clear (&object->qdata);
778 #ifdef G_ENABLE_DEBUG
781 G_LOCK (debug_objects);
782 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
783 g_hash_table_remove (debug_objects_ht, object);
784 debug_objects_count--;
785 G_UNLOCK (debug_objects);
787 #endif /* G_ENABLE_DEBUG */
792 g_object_dispatch_properties_changed (GObject *object,
798 for (i = 0; i < n_pspecs; i++)
799 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
803 * g_object_run_dispose:
804 * @object: a #GObject
806 * Releases all references to other objects. This can be used to break
809 * This functions should only be called from object system implementations.
812 g_object_run_dispose (GObject *object)
814 g_return_if_fail (G_IS_OBJECT (object));
815 g_return_if_fail (object->ref_count > 0);
817 g_object_ref (object);
818 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
819 G_OBJECT_GET_CLASS (object)->dispose (object);
820 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
821 g_object_unref (object);
825 * g_object_freeze_notify:
826 * @object: a #GObject
828 * Increases the freeze count on @object. If the freeze count is
829 * non-zero, the emission of "notify" signals on @object is
830 * stopped. The signals are queued until the freeze count is decreased
833 * This is necessary for accessors that modify multiple properties to prevent
834 * premature notification while the object is still being modified.
837 g_object_freeze_notify (GObject *object)
839 g_return_if_fail (G_IS_OBJECT (object));
841 if (g_atomic_int_get (&object->ref_count) == 0)
844 g_object_ref (object);
845 g_object_notify_queue_freeze (object, &property_notify_context);
846 g_object_unref (object);
850 g_object_notify_by_spec_internal (GObject *object,
853 GObjectNotifyQueue *nqueue;
855 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
856 g_object_notify_queue_add (object, nqueue, pspec);
857 g_object_notify_queue_thaw (object, nqueue);
862 * @object: a #GObject
863 * @property_name: the name of a property installed on the class of @object.
865 * Emits a "notify" signal for the property @property_name on @object.
867 * When possible, eg. when signaling a property change from within the class
868 * that registered the property, you should use g_object_notify_by_pspec()
872 g_object_notify (GObject *object,
873 const gchar *property_name)
877 g_return_if_fail (G_IS_OBJECT (object));
878 g_return_if_fail (property_name != NULL);
879 if (g_atomic_int_get (&object->ref_count) == 0)
882 g_object_ref (object);
883 /* We don't need to get the redirect target
884 * (by, e.g. calling g_object_class_find_property())
885 * because g_object_notify_queue_add() does that
887 pspec = g_param_spec_pool_lookup (pspec_pool,
889 G_OBJECT_TYPE (object),
893 g_warning ("%s: object class `%s' has no property named `%s'",
895 G_OBJECT_TYPE_NAME (object),
898 g_object_notify_by_spec_internal (object, pspec);
899 g_object_unref (object);
903 * g_object_notify_by_pspec:
904 * @object: a #GObject
905 * @pspec: the #GParamSpec of a property installed on the class of @object.
907 * Emits a "notify" signal for the property specified by @pspec on @object.
909 * This function omits the property name lookup, hence it is faster than
912 * One way to avoid using g_object_notify() from within the
913 * class that registered the properties, and using g_object_notify_by_pspec()
914 * instead, is to store the GParamSpec used with
915 * g_object_class_install_property() inside a static array, e.g.:
925 * static GParamSpec *properties[PROP_LAST];
928 * my_object_class_init (MyObjectClass *klass)
930 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
933 * G_PARAM_READWRITE);
934 * g_object_class_install_property (gobject_class,
936 * properties[PROP_FOO]);
940 * and then notify a change on the "foo" property with:
943 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
949 g_object_notify_by_pspec (GObject *object,
953 g_return_if_fail (G_IS_OBJECT (object));
954 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
956 g_object_ref (object);
957 g_object_notify_by_spec_internal (object, pspec);
958 g_object_unref (object);
962 * g_object_thaw_notify:
963 * @object: a #GObject
965 * Reverts the effect of a previous call to
966 * g_object_freeze_notify(). The freeze count is decreased on @object
967 * and when it reaches zero, all queued "notify" signals are emitted.
969 * It is an error to call this function when the freeze count is zero.
972 g_object_thaw_notify (GObject *object)
974 GObjectNotifyQueue *nqueue;
976 g_return_if_fail (G_IS_OBJECT (object));
977 if (g_atomic_int_get (&object->ref_count) == 0)
980 g_object_ref (object);
981 nqueue = g_object_notify_queue_from_object (object, &property_notify_context);
982 if (!nqueue || !nqueue->freeze_count)
983 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
984 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
986 g_object_notify_queue_thaw (object, nqueue);
987 g_object_unref (object);
991 object_get_property (GObject *object,
995 GObjectClass *class = g_type_class_peek (pspec->owner_type);
996 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
997 GParamSpec *redirect;
999 redirect = g_param_spec_get_redirect_target (pspec);
1003 class->get_property (object, param_id, value, pspec);
1007 object_set_property (GObject *object,
1009 const GValue *value,
1010 GObjectNotifyQueue *nqueue)
1012 GValue tmp_value = { 0, };
1013 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1014 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1015 GParamSpec *redirect;
1016 static gchar* enable_diagnostic = NULL;
1018 redirect = g_param_spec_get_redirect_target (pspec);
1022 if (G_UNLIKELY (!enable_diagnostic))
1024 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1025 if (!enable_diagnostic)
1026 enable_diagnostic = "0";
1029 if (enable_diagnostic[0] == '1')
1031 if (pspec->flags & G_PARAM_DEPRECATED)
1032 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1033 "anymore. It will be removed in a future version.",
1034 G_OBJECT_TYPE_NAME (object), pspec->name);
1037 /* provide a copy to work from, convert (if necessary) and validate */
1038 g_value_init (&tmp_value, pspec->value_type);
1039 if (!g_value_transform (value, &tmp_value))
1040 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1042 g_type_name (pspec->value_type),
1043 G_VALUE_TYPE_NAME (value));
1044 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1046 gchar *contents = g_strdup_value_contents (value);
1048 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1050 G_VALUE_TYPE_NAME (value),
1052 g_type_name (pspec->value_type));
1057 class->set_property (object, param_id, &tmp_value, pspec);
1058 g_object_notify_queue_add (object, nqueue, pspec);
1060 g_value_unset (&tmp_value);
1064 object_interface_check_properties (gpointer func_data,
1067 GTypeInterface *iface_class = g_iface;
1068 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1069 GType iface_type = iface_class->g_type;
1070 GParamSpec **pspecs;
1073 if (!G_IS_OBJECT_CLASS (class))
1076 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1080 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1082 G_OBJECT_CLASS_TYPE (class),
1087 g_critical ("Object class %s doesn't implement property "
1088 "'%s' from interface '%s'",
1089 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1091 g_type_name (iface_type));
1096 /* The implementation paramspec must have a less restrictive
1097 * type than the interface parameter spec for set() and a
1098 * more restrictive type for get(). We just require equality,
1099 * rather than doing something more complicated checking
1100 * the READABLE and WRITABLE flags. We also simplify here
1101 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1104 !g_type_is_a (pspecs[n]->value_type,
1105 class_pspec->value_type))
1107 g_critical ("Property '%s' on class '%s' has type '%s' "
1108 "which is different from the type '%s', "
1109 "of the property on interface '%s'\n",
1111 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1112 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1113 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1114 g_type_name (iface_type));
1117 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1119 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1120 * READABLE and WRITABLE remove restrictions. The implementation
1121 * paramspec must have less restrictive flags.
1124 (!SUBSET (class_pspec->flags,
1126 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1127 !SUBSET (pspecs[n]->flags,
1129 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1131 g_critical ("Flags for property '%s' on class '%s' "
1132 "are not compatible with the property on"
1135 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1136 g_type_name (iface_type));
1145 g_object_get_type (void)
1147 return G_TYPE_OBJECT;
1152 * @object_type: the type id of the #GObject subtype to instantiate
1153 * @first_property_name: the name of the first property
1154 * @...: the value of the first property, followed optionally by more
1155 * name/value pairs, followed by %NULL
1157 * Creates a new instance of a #GObject subtype and sets its properties.
1159 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1160 * which are not explicitly specified are set to their default values.
1162 * Returns: a new instance of @object_type
1165 g_object_new (GType object_type,
1166 const gchar *first_property_name,
1172 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1174 /* short circuit for calls supplying no properties */
1175 if (!first_property_name)
1176 return g_object_newv (object_type, 0, NULL);
1178 va_start (var_args, first_property_name);
1179 object = g_object_new_valist (object_type, first_property_name, var_args);
1186 slist_maybe_remove (GSList **slist,
1189 GSList *last = NULL, *node = *slist;
1192 if (node->data == data)
1195 last->next = node->next;
1197 *slist = node->next;
1198 g_slist_free_1 (node);
1207 static inline gboolean
1208 object_in_construction_list (GObject *object)
1210 gboolean in_construction;
1211 G_LOCK (construction_mutex);
1212 in_construction = g_slist_find (construction_objects, object) != NULL;
1213 G_UNLOCK (construction_mutex);
1214 return in_construction;
1219 * @object_type: the type id of the #GObject subtype to instantiate
1220 * @n_parameters: the length of the @parameters array
1221 * @parameters: an array of #GParameter
1223 * Creates a new instance of a #GObject subtype and sets its properties.
1225 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1226 * which are not explicitly specified are set to their default values.
1228 * Returns: a new instance of @object_type
1231 g_object_newv (GType object_type,
1233 GParameter *parameters)
1235 GObjectConstructParam *cparams = NULL, *oparams;
1236 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1238 GObjectClass *class, *unref_class = NULL;
1240 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1242 GList *clist = NULL;
1243 gboolean newly_constructed;
1246 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1248 class = g_type_class_peek_static (object_type);
1250 class = unref_class = g_type_class_ref (object_type);
1251 for (slist = class->construct_properties; slist; slist = slist->next)
1253 clist = g_list_prepend (clist, slist->data);
1254 n_total_cparams += 1;
1257 if (n_parameters == 0 && n_total_cparams == 0)
1259 /* This is a simple object with no construct properties, and
1260 * no properties are being set, so short circuit the parameter
1261 * handling. This speeds up simple object construction.
1264 object = class->constructor (object_type, 0, NULL);
1265 goto did_construction;
1268 /* collect parameters, sort into construction and normal ones */
1269 oparams = g_new (GObjectConstructParam, n_parameters);
1270 cparams = g_new (GObjectConstructParam, n_total_cparams);
1271 for (i = 0; i < n_parameters; i++)
1273 GValue *value = ¶meters[i].value;
1274 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1280 g_warning ("%s: object class `%s' has no property named `%s'",
1282 g_type_name (object_type),
1283 parameters[i].name);
1286 if (!(pspec->flags & G_PARAM_WRITABLE))
1288 g_warning ("%s: property `%s' of object class `%s' is not writable",
1291 g_type_name (object_type));
1294 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1296 GList *list = g_list_find (clist, pspec);
1300 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1301 G_STRFUNC, pspec->name, g_type_name (object_type));
1304 cparams[n_cparams].pspec = pspec;
1305 cparams[n_cparams].value = value;
1310 list->prev->next = list->next;
1312 list->next->prev = list->prev;
1313 g_list_free_1 (list);
1317 oparams[n_oparams].pspec = pspec;
1318 oparams[n_oparams].value = value;
1323 /* set remaining construction properties to default values */
1324 n_cvalues = n_total_cparams - n_cparams;
1325 cvalues = g_new (GValue, n_cvalues);
1328 GList *tmp = clist->next;
1329 GParamSpec *pspec = clist->data;
1330 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1333 g_value_init (value, pspec->value_type);
1334 g_param_value_set_default (pspec, value);
1336 cparams[n_cparams].pspec = pspec;
1337 cparams[n_cparams].value = value;
1340 g_list_free_1 (clist);
1344 /* construct object from construction parameters */
1345 object = class->constructor (object_type, n_total_cparams, cparams);
1346 /* free construction values */
1349 g_value_unset (cvalues + n_cvalues);
1353 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1355 /* adjust freeze_count according to g_object_init() and remaining properties */
1356 G_LOCK (construction_mutex);
1357 newly_constructed = slist_maybe_remove (&construction_objects, object);
1358 G_UNLOCK (construction_mutex);
1361 newly_constructed = TRUE;
1363 if (CLASS_HAS_PROPS (class))
1365 if (newly_constructed || n_oparams)
1366 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1367 if (newly_constructed)
1368 g_object_notify_queue_thaw (object, nqueue);
1371 /* run 'constructed' handler if there is one */
1372 if (newly_constructed && class->constructed)
1373 class->constructed (object);
1375 /* set remaining properties */
1376 for (i = 0; i < n_oparams; i++)
1377 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1380 if (CLASS_HAS_PROPS (class))
1382 /* release our own freeze count and handle notifications */
1383 if (newly_constructed || n_oparams)
1384 g_object_notify_queue_thaw (object, nqueue);
1388 g_type_class_unref (unref_class);
1394 * g_object_new_valist:
1395 * @object_type: the type id of the #GObject subtype to instantiate
1396 * @first_property_name: the name of the first property
1397 * @var_args: the value of the first property, followed optionally by more
1398 * name/value pairs, followed by %NULL
1400 * Creates a new instance of a #GObject subtype and sets its properties.
1402 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1403 * which are not explicitly specified are set to their default values.
1405 * Returns: a new instance of @object_type
1408 g_object_new_valist (GType object_type,
1409 const gchar *first_property_name,
1412 GObjectClass *class;
1416 guint n_params = 0, n_alloced_params = 16;
1418 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1420 if (!first_property_name)
1421 return g_object_newv (object_type, 0, NULL);
1423 class = g_type_class_ref (object_type);
1425 params = g_new0 (GParameter, n_alloced_params);
1426 name = first_property_name;
1429 gchar *error = NULL;
1430 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1436 g_warning ("%s: object class `%s' has no property named `%s'",
1438 g_type_name (object_type),
1442 if (n_params >= n_alloced_params)
1444 n_alloced_params += 16;
1445 params = g_renew (GParameter, params, n_alloced_params);
1447 params[n_params].name = name;
1448 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1449 var_args, 0, &error);
1452 g_warning ("%s: %s", G_STRFUNC, error);
1454 g_value_unset (¶ms[n_params].value);
1458 name = va_arg (var_args, gchar*);
1461 object = g_object_newv (object_type, n_params, params);
1464 g_value_unset (¶ms[n_params].value);
1467 g_type_class_unref (class);
1473 g_object_constructor (GType type,
1474 guint n_construct_properties,
1475 GObjectConstructParam *construct_params)
1480 object = (GObject*) g_type_create_instance (type);
1482 /* set construction parameters */
1483 if (n_construct_properties)
1485 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1487 /* set construct properties */
1488 while (n_construct_properties--)
1490 GValue *value = construct_params->value;
1491 GParamSpec *pspec = construct_params->pspec;
1494 object_set_property (object, pspec, value, nqueue);
1496 g_object_notify_queue_thaw (object, nqueue);
1497 /* the notification queue is still frozen from g_object_init(), so
1498 * we don't need to handle it here, g_object_newv() takes
1507 * g_object_set_valist:
1508 * @object: a #GObject
1509 * @first_property_name: name of the first property to set
1510 * @var_args: value for the first property, followed optionally by more
1511 * name/value pairs, followed by %NULL
1513 * Sets properties on an object.
1516 g_object_set_valist (GObject *object,
1517 const gchar *first_property_name,
1520 GObjectNotifyQueue *nqueue;
1523 g_return_if_fail (G_IS_OBJECT (object));
1525 g_object_ref (object);
1526 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1528 name = first_property_name;
1531 GValue value = { 0, };
1533 gchar *error = NULL;
1535 pspec = g_param_spec_pool_lookup (pspec_pool,
1537 G_OBJECT_TYPE (object),
1541 g_warning ("%s: object class `%s' has no property named `%s'",
1543 G_OBJECT_TYPE_NAME (object),
1547 if (!(pspec->flags & G_PARAM_WRITABLE))
1549 g_warning ("%s: property `%s' of object class `%s' is not writable",
1552 G_OBJECT_TYPE_NAME (object));
1555 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1557 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1558 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1562 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1566 g_warning ("%s: %s", G_STRFUNC, error);
1568 g_value_unset (&value);
1572 object_set_property (object, pspec, &value, nqueue);
1573 g_value_unset (&value);
1575 name = va_arg (var_args, gchar*);
1578 g_object_notify_queue_thaw (object, nqueue);
1579 g_object_unref (object);
1583 * g_object_get_valist:
1584 * @object: a #GObject
1585 * @first_property_name: name of the first property to get
1586 * @var_args: return location for the first property, followed optionally by more
1587 * name/return location pairs, followed by %NULL
1589 * Gets properties of an object.
1591 * In general, a copy is made of the property contents and the caller
1592 * is responsible for freeing the memory in the appropriate manner for
1593 * the type, for instance by calling g_free() or g_object_unref().
1595 * See g_object_get().
1598 g_object_get_valist (GObject *object,
1599 const gchar *first_property_name,
1604 g_return_if_fail (G_IS_OBJECT (object));
1606 g_object_ref (object);
1608 name = first_property_name;
1612 GValue value = { 0, };
1616 pspec = g_param_spec_pool_lookup (pspec_pool,
1618 G_OBJECT_TYPE (object),
1622 g_warning ("%s: object class `%s' has no property named `%s'",
1624 G_OBJECT_TYPE_NAME (object),
1628 if (!(pspec->flags & G_PARAM_READABLE))
1630 g_warning ("%s: property `%s' of object class `%s' is not readable",
1633 G_OBJECT_TYPE_NAME (object));
1637 g_value_init (&value, pspec->value_type);
1639 object_get_property (object, pspec, &value);
1641 G_VALUE_LCOPY (&value, var_args, 0, &error);
1644 g_warning ("%s: %s", G_STRFUNC, error);
1646 g_value_unset (&value);
1650 g_value_unset (&value);
1652 name = va_arg (var_args, gchar*);
1655 g_object_unref (object);
1660 * @object: a #GObject
1661 * @first_property_name: name of the first property to set
1662 * @...: value for the first property, followed optionally by more
1663 * name/value pairs, followed by %NULL
1665 * Sets properties on an object.
1668 g_object_set (gpointer _object,
1669 const gchar *first_property_name,
1672 GObject *object = _object;
1675 g_return_if_fail (G_IS_OBJECT (object));
1677 va_start (var_args, first_property_name);
1678 g_object_set_valist (object, first_property_name, var_args);
1684 * @object: a #GObject
1685 * @first_property_name: name of the first property to get
1686 * @...: return location for the first property, followed optionally by more
1687 * name/return location pairs, followed by %NULL
1689 * Gets properties of an object.
1691 * In general, a copy is made of the property contents and the caller
1692 * is responsible for freeing the memory in the appropriate manner for
1693 * the type, for instance by calling g_free() or g_object_unref().
1696 * <title>Using g_object_get(<!-- -->)</title>
1697 * An example of using g_object_get() to get the contents
1698 * of three properties - one of type #G_TYPE_INT,
1699 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1705 * g_object_get (my_object,
1706 * "int-property", &intval,
1707 * "str-property", &strval,
1708 * "obj-property", &objval,
1711 * // Do something with intval, strval, objval
1714 * g_object_unref (objval);
1719 g_object_get (gpointer _object,
1720 const gchar *first_property_name,
1723 GObject *object = _object;
1726 g_return_if_fail (G_IS_OBJECT (object));
1728 va_start (var_args, first_property_name);
1729 g_object_get_valist (object, first_property_name, var_args);
1734 * g_object_set_property:
1735 * @object: a #GObject
1736 * @property_name: the name of the property to set
1739 * Sets a property on an object.
1742 g_object_set_property (GObject *object,
1743 const gchar *property_name,
1744 const GValue *value)
1746 GObjectNotifyQueue *nqueue;
1749 g_return_if_fail (G_IS_OBJECT (object));
1750 g_return_if_fail (property_name != NULL);
1751 g_return_if_fail (G_IS_VALUE (value));
1753 g_object_ref (object);
1754 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1756 pspec = g_param_spec_pool_lookup (pspec_pool,
1758 G_OBJECT_TYPE (object),
1761 g_warning ("%s: object class `%s' has no property named `%s'",
1763 G_OBJECT_TYPE_NAME (object),
1765 else if (!(pspec->flags & G_PARAM_WRITABLE))
1766 g_warning ("%s: property `%s' of object class `%s' is not writable",
1769 G_OBJECT_TYPE_NAME (object));
1770 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1771 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1772 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1774 object_set_property (object, pspec, value, nqueue);
1776 g_object_notify_queue_thaw (object, nqueue);
1777 g_object_unref (object);
1781 * g_object_get_property:
1782 * @object: a #GObject
1783 * @property_name: the name of the property to get
1784 * @value: return location for the property value
1786 * Gets a property of an object.
1788 * In general, a copy is made of the property contents and the caller is
1789 * responsible for freeing the memory by calling g_value_unset().
1791 * Note that g_object_get_property() is really intended for language
1792 * bindings, g_object_get() is much more convenient for C programming.
1795 g_object_get_property (GObject *object,
1796 const gchar *property_name,
1801 g_return_if_fail (G_IS_OBJECT (object));
1802 g_return_if_fail (property_name != NULL);
1803 g_return_if_fail (G_IS_VALUE (value));
1805 g_object_ref (object);
1807 pspec = g_param_spec_pool_lookup (pspec_pool,
1809 G_OBJECT_TYPE (object),
1812 g_warning ("%s: object class `%s' has no property named `%s'",
1814 G_OBJECT_TYPE_NAME (object),
1816 else if (!(pspec->flags & G_PARAM_READABLE))
1817 g_warning ("%s: property `%s' of object class `%s' is not readable",
1820 G_OBJECT_TYPE_NAME (object));
1823 GValue *prop_value, tmp_value = { 0, };
1825 /* auto-conversion of the callers value type
1827 if (G_VALUE_TYPE (value) == pspec->value_type)
1829 g_value_reset (value);
1832 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1834 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1835 G_STRFUNC, pspec->name,
1836 g_type_name (pspec->value_type),
1837 G_VALUE_TYPE_NAME (value));
1838 g_object_unref (object);
1843 g_value_init (&tmp_value, pspec->value_type);
1844 prop_value = &tmp_value;
1846 object_get_property (object, pspec, prop_value);
1847 if (prop_value != value)
1849 g_value_transform (prop_value, value);
1850 g_value_unset (&tmp_value);
1854 g_object_unref (object);
1859 * @object: a #GObject
1860 * @signal_spec: the spec for the first signal
1861 * @...: #GCallback for the first signal, followed by data for the
1862 * first signal, followed optionally by more signal
1863 * spec/callback/data triples, followed by %NULL
1865 * A convenience function to connect multiple signals at once.
1867 * The signal specs expected by this function have the form
1868 * "modifier::signal_name", where modifier can be one of the following:
1871 * <term>signal</term>
1873 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1874 * </para></listitem>
1877 * <term>object_signal</term>
1878 * <term>object-signal</term>
1880 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1881 * </para></listitem>
1884 * <term>swapped_signal</term>
1885 * <term>swapped-signal</term>
1887 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1888 * </para></listitem>
1891 * <term>swapped_object_signal</term>
1892 * <term>swapped-object-signal</term>
1894 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1895 * </para></listitem>
1898 * <term>signal_after</term>
1899 * <term>signal-after</term>
1901 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1902 * </para></listitem>
1905 * <term>object_signal_after</term>
1906 * <term>object-signal-after</term>
1908 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1909 * </para></listitem>
1912 * <term>swapped_signal_after</term>
1913 * <term>swapped-signal-after</term>
1915 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1916 * </para></listitem>
1919 * <term>swapped_object_signal_after</term>
1920 * <term>swapped-object-signal-after</term>
1922 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1923 * </para></listitem>
1928 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1929 * "type", GTK_WINDOW_POPUP,
1932 * "signal::event", gtk_menu_window_event, menu,
1933 * "signal::size_request", gtk_menu_window_size_request, menu,
1934 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1941 g_object_connect (gpointer _object,
1942 const gchar *signal_spec,
1945 GObject *object = _object;
1948 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1949 g_return_val_if_fail (object->ref_count > 0, object);
1951 va_start (var_args, signal_spec);
1954 GCallback callback = va_arg (var_args, GCallback);
1955 gpointer data = va_arg (var_args, gpointer);
1958 if (strncmp (signal_spec, "signal::", 8) == 0)
1959 sid = g_signal_connect_data (object, signal_spec + 8,
1960 callback, data, NULL,
1962 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1963 strncmp (signal_spec, "object-signal::", 15) == 0)
1964 sid = g_signal_connect_object (object, signal_spec + 15,
1967 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1968 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1969 sid = g_signal_connect_data (object, signal_spec + 16,
1970 callback, data, NULL,
1972 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1973 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1974 sid = g_signal_connect_object (object, signal_spec + 23,
1977 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1978 strncmp (signal_spec, "signal-after::", 14) == 0)
1979 sid = g_signal_connect_data (object, signal_spec + 14,
1980 callback, data, NULL,
1982 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1983 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1984 sid = g_signal_connect_object (object, signal_spec + 21,
1987 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1988 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1989 sid = g_signal_connect_data (object, signal_spec + 22,
1990 callback, data, NULL,
1991 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1992 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1993 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1994 sid = g_signal_connect_object (object, signal_spec + 29,
1996 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1999 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2002 signal_spec = va_arg (var_args, gchar*);
2010 * g_object_disconnect:
2011 * @object: a #GObject
2012 * @signal_spec: the spec for the first signal
2013 * @...: #GCallback for the first signal, followed by data for the first signal,
2014 * followed optionally by more signal spec/callback/data triples,
2017 * A convenience function to disconnect multiple signals at once.
2019 * The signal specs expected by this function have the form
2020 * "any_signal", which means to disconnect any signal with matching
2021 * callback and data, or "any_signal::signal_name", which only
2022 * disconnects the signal named "signal_name".
2025 g_object_disconnect (gpointer _object,
2026 const gchar *signal_spec,
2029 GObject *object = _object;
2032 g_return_if_fail (G_IS_OBJECT (object));
2033 g_return_if_fail (object->ref_count > 0);
2035 va_start (var_args, signal_spec);
2038 GCallback callback = va_arg (var_args, GCallback);
2039 gpointer data = va_arg (var_args, gpointer);
2040 guint sid = 0, detail = 0, mask = 0;
2042 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2043 strncmp (signal_spec, "any-signal::", 12) == 0)
2046 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2048 else if (strcmp (signal_spec, "any_signal") == 0 ||
2049 strcmp (signal_spec, "any-signal") == 0)
2052 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2056 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2060 if ((mask & G_SIGNAL_MATCH_ID) &&
2061 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2062 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2063 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2065 NULL, (gpointer)callback, data))
2066 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2067 signal_spec = va_arg (var_args, gchar*);
2078 } weak_refs[1]; /* flexible array */
2082 weak_refs_notify (gpointer data)
2084 WeakRefStack *wstack = data;
2087 for (i = 0; i < wstack->n_weak_refs; i++)
2088 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2093 * g_object_weak_ref:
2094 * @object: #GObject to reference weakly
2095 * @notify: callback to invoke before the object is freed
2096 * @data: extra data to pass to notify
2098 * Adds a weak reference callback to an object. Weak references are
2099 * used for notification when an object is finalized. They are called
2100 * "weak references" because they allow you to safely hold a pointer
2101 * to an object without calling g_object_ref() (g_object_ref() adds a
2102 * strong reference, that is, forces the object to stay alive).
2105 g_object_weak_ref (GObject *object,
2109 WeakRefStack *wstack;
2112 g_return_if_fail (G_IS_OBJECT (object));
2113 g_return_if_fail (notify != NULL);
2114 g_return_if_fail (object->ref_count >= 1);
2116 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2119 i = wstack->n_weak_refs++;
2120 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2124 wstack = g_renew (WeakRefStack, NULL, 1);
2125 wstack->object = object;
2126 wstack->n_weak_refs = 1;
2129 wstack->weak_refs[i].notify = notify;
2130 wstack->weak_refs[i].data = data;
2131 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2135 * g_object_weak_unref:
2136 * @object: #GObject to remove a weak reference from
2137 * @notify: callback to search for
2138 * @data: data to search for
2140 * Removes a weak reference callback to an object.
2143 g_object_weak_unref (GObject *object,
2147 WeakRefStack *wstack;
2148 gboolean found_one = FALSE;
2150 g_return_if_fail (G_IS_OBJECT (object));
2151 g_return_if_fail (notify != NULL);
2153 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2158 for (i = 0; i < wstack->n_weak_refs; i++)
2159 if (wstack->weak_refs[i].notify == notify &&
2160 wstack->weak_refs[i].data == data)
2163 wstack->n_weak_refs -= 1;
2164 if (i != wstack->n_weak_refs)
2165 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2171 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2175 * g_object_add_weak_pointer:
2176 * @object: The object that should be weak referenced.
2177 * @weak_pointer_location: The memory address of a pointer.
2179 * Adds a weak reference from weak_pointer to @object to indicate that
2180 * the pointer located at @weak_pointer_location is only valid during
2181 * the lifetime of @object. When the @object is finalized,
2182 * @weak_pointer will be set to %NULL.
2185 g_object_add_weak_pointer (GObject *object,
2186 gpointer *weak_pointer_location)
2188 g_return_if_fail (G_IS_OBJECT (object));
2189 g_return_if_fail (weak_pointer_location != NULL);
2191 g_object_weak_ref (object,
2192 (GWeakNotify) g_nullify_pointer,
2193 weak_pointer_location);
2197 * g_object_remove_weak_pointer:
2198 * @object: The object that is weak referenced.
2199 * @weak_pointer_location: The memory address of a pointer.
2201 * Removes a weak reference from @object that was previously added
2202 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2203 * to match the one used with g_object_add_weak_pointer().
2206 g_object_remove_weak_pointer (GObject *object,
2207 gpointer *weak_pointer_location)
2209 g_return_if_fail (G_IS_OBJECT (object));
2210 g_return_if_fail (weak_pointer_location != NULL);
2212 g_object_weak_unref (object,
2213 (GWeakNotify) g_nullify_pointer,
2214 weak_pointer_location);
2218 object_floating_flag_handler (GObject *object,
2224 case +1: /* force floating if possible */
2226 oldvalue = g_atomic_pointer_get (&object->qdata);
2227 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2228 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2229 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2230 case -1: /* sink if possible */
2232 oldvalue = g_atomic_pointer_get (&object->qdata);
2233 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2234 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2235 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2236 default: /* check floating */
2237 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2242 * g_object_is_floating:
2243 * @object: a #GObject
2245 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2250 * Returns: %TRUE if @object has a floating reference
2253 g_object_is_floating (gpointer _object)
2255 GObject *object = _object;
2256 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2257 return floating_flag_handler (object, 0);
2261 * g_object_ref_sink:
2262 * @object: a #GObject
2264 * Increase the reference count of @object, and possibly remove the
2265 * <link linkend="floating-ref">floating</link> reference, if @object
2266 * has a floating reference.
2268 * In other words, if the object is floating, then this call "assumes
2269 * ownership" of the floating reference, converting it to a normal
2270 * reference by clearing the floating flag while leaving the reference
2271 * count unchanged. If the object is not floating, then this call
2272 * adds a new normal reference increasing the reference count by one.
2279 g_object_ref_sink (gpointer _object)
2281 GObject *object = _object;
2282 gboolean was_floating;
2283 g_return_val_if_fail (G_IS_OBJECT (object), object);
2284 g_return_val_if_fail (object->ref_count >= 1, object);
2285 g_object_ref (object);
2286 was_floating = floating_flag_handler (object, -1);
2288 g_object_unref (object);
2293 * g_object_force_floating:
2294 * @object: a #GObject
2296 * This function is intended for #GObject implementations to re-enforce a
2297 * <link linkend="floating-ref">floating</link> object reference.
2298 * Doing this is seldomly required, all
2299 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2300 * usually just needs to be sunken by calling g_object_ref_sink().
2305 g_object_force_floating (GObject *object)
2307 gboolean was_floating;
2308 g_return_if_fail (G_IS_OBJECT (object));
2309 g_return_if_fail (object->ref_count >= 1);
2311 was_floating = floating_flag_handler (object, +1);
2316 guint n_toggle_refs;
2318 GToggleNotify notify;
2320 } toggle_refs[1]; /* flexible array */
2324 toggle_refs_notify (GObject *object,
2325 gboolean is_last_ref)
2327 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2329 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2330 * will only be notified when there is exactly one of them.
2332 g_assert (tstack->n_toggle_refs == 1);
2333 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2337 * g_object_add_toggle_ref:
2338 * @object: a #GObject
2339 * @notify: a function to call when this reference is the
2340 * last reference to the object, or is no longer
2341 * the last reference.
2342 * @data: data to pass to @notify
2344 * Increases the reference count of the object by one and sets a
2345 * callback to be called when all other references to the object are
2346 * dropped, or when this is already the last reference to the object
2347 * and another reference is established.
2349 * This functionality is intended for binding @object to a proxy
2350 * object managed by another memory manager. This is done with two
2351 * paired references: the strong reference added by
2352 * g_object_add_toggle_ref() and a reverse reference to the proxy
2353 * object which is either a strong reference or weak reference.
2355 * The setup is that when there are no other references to @object,
2356 * only a weak reference is held in the reverse direction from @object
2357 * to the proxy object, but when there are other references held to
2358 * @object, a strong reference is held. The @notify callback is called
2359 * when the reference from @object to the proxy object should be
2360 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2361 * true) or weak to strong (@is_last_ref false).
2363 * Since a (normal) reference must be held to the object before
2364 * calling g_object_toggle_ref(), the initial state of the reverse
2365 * link is always strong.
2367 * Multiple toggle references may be added to the same gobject,
2368 * however if there are multiple toggle references to an object, none
2369 * of them will ever be notified until all but one are removed. For
2370 * this reason, you should only ever use a toggle reference if there
2371 * is important state in the proxy object.
2376 g_object_add_toggle_ref (GObject *object,
2377 GToggleNotify notify,
2380 ToggleRefStack *tstack;
2383 g_return_if_fail (G_IS_OBJECT (object));
2384 g_return_if_fail (notify != NULL);
2385 g_return_if_fail (object->ref_count >= 1);
2387 g_object_ref (object);
2389 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2392 i = tstack->n_toggle_refs++;
2393 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2394 * in tstate->toggle_refs */
2395 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2399 tstack = g_renew (ToggleRefStack, NULL, 1);
2400 tstack->object = object;
2401 tstack->n_toggle_refs = 1;
2405 /* Set a flag for fast lookup after adding the first toggle reference */
2406 if (tstack->n_toggle_refs == 1)
2407 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2409 tstack->toggle_refs[i].notify = notify;
2410 tstack->toggle_refs[i].data = data;
2411 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2412 (GDestroyNotify)g_free);
2416 * g_object_remove_toggle_ref:
2417 * @object: a #GObject
2418 * @notify: a function to call when this reference is the
2419 * last reference to the object, or is no longer
2420 * the last reference.
2421 * @data: data to pass to @notify
2423 * Removes a reference added with g_object_add_toggle_ref(). The
2424 * reference count of the object is decreased by one.
2429 g_object_remove_toggle_ref (GObject *object,
2430 GToggleNotify notify,
2433 ToggleRefStack *tstack;
2434 gboolean found_one = FALSE;
2436 g_return_if_fail (G_IS_OBJECT (object));
2437 g_return_if_fail (notify != NULL);
2439 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2444 for (i = 0; i < tstack->n_toggle_refs; i++)
2445 if (tstack->toggle_refs[i].notify == notify &&
2446 tstack->toggle_refs[i].data == data)
2449 tstack->n_toggle_refs -= 1;
2450 if (i != tstack->n_toggle_refs)
2451 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2453 if (tstack->n_toggle_refs == 0)
2454 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2456 g_object_unref (object);
2463 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2468 * @object: a #GObject
2470 * Increases the reference count of @object.
2472 * Returns: the same @object
2475 g_object_ref (gpointer _object)
2477 GObject *object = _object;
2480 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2481 g_return_val_if_fail (object->ref_count > 0, NULL);
2483 #ifdef G_ENABLE_DEBUG
2484 if (g_trap_object_ref == object)
2486 #endif /* G_ENABLE_DEBUG */
2489 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2491 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2492 toggle_refs_notify (object, FALSE);
2494 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2501 * @object: a #GObject
2503 * Decreases the reference count of @object. When its reference count
2504 * drops to 0, the object is finalized (i.e. its memory is freed).
2507 g_object_unref (gpointer _object)
2509 GObject *object = _object;
2512 g_return_if_fail (G_IS_OBJECT (object));
2513 g_return_if_fail (object->ref_count > 0);
2515 #ifdef G_ENABLE_DEBUG
2516 if (g_trap_object_ref == object)
2518 #endif /* G_ENABLE_DEBUG */
2520 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2521 retry_atomic_decrement1:
2522 old_ref = g_atomic_int_get (&object->ref_count);
2525 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2526 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2528 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2529 goto retry_atomic_decrement1;
2531 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2533 /* if we went from 2->1 we need to notify toggle refs if any */
2534 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2535 toggle_refs_notify (object, TRUE);
2539 /* we are about tp remove the last reference */
2540 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2541 G_OBJECT_GET_CLASS (object)->dispose (object);
2542 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2544 /* may have been re-referenced meanwhile */
2545 retry_atomic_decrement2:
2546 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2549 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2550 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2552 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2553 goto retry_atomic_decrement2;
2555 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2557 /* if we went from 2->1 we need to notify toggle refs if any */
2558 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2559 toggle_refs_notify (object, TRUE);
2564 /* we are still in the process of taking away the last ref */
2565 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2566 g_signal_handlers_destroy (object);
2567 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2569 /* decrement the last reference */
2570 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2572 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2574 /* may have been re-referenced meanwhile */
2575 if (G_LIKELY (old_ref == 1))
2577 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2578 G_OBJECT_GET_CLASS (object)->finalize (object);
2580 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2582 #ifdef G_ENABLE_DEBUG
2585 /* catch objects not chaining finalize handlers */
2586 G_LOCK (debug_objects);
2587 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2588 G_UNLOCK (debug_objects);
2590 #endif /* G_ENABLE_DEBUG */
2591 g_type_free_instance ((GTypeInstance*) object);
2597 * g_object_get_qdata:
2598 * @object: The GObject to get a stored user data pointer from
2599 * @quark: A #GQuark, naming the user data pointer
2601 * This function gets back user data pointers stored via
2602 * g_object_set_qdata().
2604 * Returns: The user data pointer set, or %NULL
2607 g_object_get_qdata (GObject *object,
2610 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2612 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2616 * g_object_set_qdata:
2617 * @object: The GObject to set store a user data pointer
2618 * @quark: A #GQuark, naming the user data pointer
2619 * @data: An opaque user data pointer
2621 * This sets an opaque, named pointer on an object.
2622 * The name is specified through a #GQuark (retrived e.g. via
2623 * g_quark_from_static_string()), and the pointer
2624 * can be gotten back from the @object with g_object_get_qdata()
2625 * until the @object is finalized.
2626 * Setting a previously set user data pointer, overrides (frees)
2627 * the old pointer set, using #NULL as pointer essentially
2628 * removes the data stored.
2631 g_object_set_qdata (GObject *object,
2635 g_return_if_fail (G_IS_OBJECT (object));
2636 g_return_if_fail (quark > 0);
2638 g_datalist_id_set_data (&object->qdata, quark, data);
2642 * g_object_set_qdata_full:
2643 * @object: The GObject to set store a user data pointer
2644 * @quark: A #GQuark, naming the user data pointer
2645 * @data: An opaque user data pointer
2646 * @destroy: Function to invoke with @data as argument, when @data
2649 * This function works like g_object_set_qdata(), but in addition,
2650 * a void (*destroy) (gpointer) function may be specified which is
2651 * called with @data as argument when the @object is finalized, or
2652 * the data is being overwritten by a call to g_object_set_qdata()
2653 * with the same @quark.
2656 g_object_set_qdata_full (GObject *object,
2659 GDestroyNotify destroy)
2661 g_return_if_fail (G_IS_OBJECT (object));
2662 g_return_if_fail (quark > 0);
2664 g_datalist_id_set_data_full (&object->qdata, quark, data,
2665 data ? destroy : (GDestroyNotify) NULL);
2669 * g_object_steal_qdata:
2670 * @object: The GObject to get a stored user data pointer from
2671 * @quark: A #GQuark, naming the user data pointer
2673 * This function gets back user data pointers stored via
2674 * g_object_set_qdata() and removes the @data from object
2675 * without invoking its destroy() function (if any was
2677 * Usually, calling this function is only required to update
2678 * user data pointers with a destroy notifier, for example:
2681 * object_add_to_user_list (GObject *object,
2682 * const gchar *new_string)
2684 * // the quark, naming the object data
2685 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2686 * // retrive the old string list
2687 * GList *list = g_object_steal_qdata (object, quark_string_list);
2689 * // prepend new string
2690 * list = g_list_prepend (list, g_strdup (new_string));
2691 * // this changed 'list', so we need to set it again
2692 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2695 * free_string_list (gpointer data)
2697 * GList *node, *list = data;
2699 * for (node = list; node; node = node->next)
2700 * g_free (node->data);
2701 * g_list_free (list);
2704 * Using g_object_get_qdata() in the above example, instead of
2705 * g_object_steal_qdata() would have left the destroy function set,
2706 * and thus the partial string list would have been freed upon
2707 * g_object_set_qdata_full().
2709 * Returns: The user data pointer set, or %NULL
2712 g_object_steal_qdata (GObject *object,
2715 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2716 g_return_val_if_fail (quark > 0, NULL);
2718 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2722 * g_object_get_data:
2723 * @object: #GObject containing the associations
2724 * @key: name of the key for that association
2726 * Gets a named field from the objects table of associations (see g_object_set_data()).
2728 * Returns: the data if found, or %NULL if no such data exists.
2731 g_object_get_data (GObject *object,
2736 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2737 g_return_val_if_fail (key != NULL, NULL);
2739 quark = g_quark_try_string (key);
2741 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2745 * g_object_set_data:
2746 * @object: #GObject containing the associations.
2747 * @key: name of the key
2748 * @data: data to associate with that key
2750 * Each object carries around a table of associations from
2751 * strings to pointers. This function lets you set an association.
2753 * If the object already had an association with that name,
2754 * the old association will be destroyed.
2757 g_object_set_data (GObject *object,
2761 g_return_if_fail (G_IS_OBJECT (object));
2762 g_return_if_fail (key != NULL);
2764 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2768 * g_object_set_data_full:
2769 * @object: #GObject containing the associations
2770 * @key: name of the key
2771 * @data: data to associate with that key
2772 * @destroy: function to call when the association is destroyed
2774 * Like g_object_set_data() except it adds notification
2775 * for when the association is destroyed, either by setting it
2776 * to a different value or when the object is destroyed.
2778 * Note that the @destroy callback is not called if @data is %NULL.
2781 g_object_set_data_full (GObject *object,
2784 GDestroyNotify destroy)
2786 g_return_if_fail (G_IS_OBJECT (object));
2787 g_return_if_fail (key != NULL);
2789 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2790 data ? destroy : (GDestroyNotify) NULL);
2794 * g_object_steal_data:
2795 * @object: #GObject containing the associations
2796 * @key: name of the key
2798 * Remove a specified datum from the object's data associations,
2799 * without invoking the association's destroy handler.
2801 * Returns: the data if found, or %NULL if no such data exists.
2804 g_object_steal_data (GObject *object,
2809 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2810 g_return_val_if_fail (key != NULL, NULL);
2812 quark = g_quark_try_string (key);
2814 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2818 g_value_object_init (GValue *value)
2820 value->data[0].v_pointer = NULL;
2824 g_value_object_free_value (GValue *value)
2826 if (value->data[0].v_pointer)
2827 g_object_unref (value->data[0].v_pointer);
2831 g_value_object_copy_value (const GValue *src_value,
2834 if (src_value->data[0].v_pointer)
2835 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2837 dest_value->data[0].v_pointer = NULL;
2841 g_value_object_transform_value (const GValue *src_value,
2844 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)))
2845 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2847 dest_value->data[0].v_pointer = NULL;
2851 g_value_object_peek_pointer (const GValue *value)
2853 return value->data[0].v_pointer;
2857 g_value_object_collect_value (GValue *value,
2858 guint n_collect_values,
2859 GTypeCValue *collect_values,
2860 guint collect_flags)
2862 if (collect_values[0].v_pointer)
2864 GObject *object = collect_values[0].v_pointer;
2866 if (object->g_type_instance.g_class == NULL)
2867 return g_strconcat ("invalid unclassed object pointer for value type `",
2868 G_VALUE_TYPE_NAME (value),
2871 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2872 return g_strconcat ("invalid object type `",
2873 G_OBJECT_TYPE_NAME (object),
2874 "' for value type `",
2875 G_VALUE_TYPE_NAME (value),
2878 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2879 value->data[0].v_pointer = g_object_ref (object);
2882 value->data[0].v_pointer = NULL;
2888 g_value_object_lcopy_value (const GValue *value,
2889 guint n_collect_values,
2890 GTypeCValue *collect_values,
2891 guint collect_flags)
2893 GObject **object_p = collect_values[0].v_pointer;
2896 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2898 if (!value->data[0].v_pointer)
2900 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2901 *object_p = value->data[0].v_pointer;
2903 *object_p = g_object_ref (value->data[0].v_pointer);
2909 * g_value_set_object:
2910 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2911 * @v_object: object value to be set
2913 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2915 * g_value_set_object() increases the reference count of @v_object
2916 * (the #GValue holds a reference to @v_object). If you do not wish
2917 * to increase the reference count of the object (i.e. you wish to
2918 * pass your current reference to the #GValue because you no longer
2919 * need it), use g_value_take_object() instead.
2921 * It is important that your #GValue holds a reference to @v_object (either its
2922 * own, or one it has taken) to ensure that the object won't be destroyed while
2923 * the #GValue still exists).
2926 g_value_set_object (GValue *value,
2931 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2933 old = value->data[0].v_pointer;
2937 g_return_if_fail (G_IS_OBJECT (v_object));
2938 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2940 value->data[0].v_pointer = v_object;
2941 g_object_ref (value->data[0].v_pointer);
2944 value->data[0].v_pointer = NULL;
2947 g_object_unref (old);
2951 * g_value_set_object_take_ownership:
2952 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2953 * @v_object: object value to be set
2955 * This is an internal function introduced mainly for C marshallers.
2957 * Deprecated: 2.4: Use g_value_take_object() instead.
2960 g_value_set_object_take_ownership (GValue *value,
2963 g_value_take_object (value, v_object);
2967 * g_value_take_object:
2968 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2969 * @v_object: object value to be set
2971 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2972 * and takes over the ownership of the callers reference to @v_object;
2973 * the caller doesn't have to unref it any more (i.e. the reference
2974 * count of the object is not increased).
2976 * If you want the #GValue to hold its own reference to @v_object, use
2977 * g_value_set_object() instead.
2982 g_value_take_object (GValue *value,
2985 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2987 if (value->data[0].v_pointer)
2989 g_object_unref (value->data[0].v_pointer);
2990 value->data[0].v_pointer = NULL;
2995 g_return_if_fail (G_IS_OBJECT (v_object));
2996 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2998 value->data[0].v_pointer = v_object; /* we take over the reference count */
3003 * g_value_get_object:
3004 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3006 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3008 * Returns: object contents of @value
3011 g_value_get_object (const GValue *value)
3013 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3015 return value->data[0].v_pointer;
3019 * g_value_dup_object:
3020 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3022 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3023 * its reference count.
3025 * Returns: object content of @value, should be unreferenced when no
3029 g_value_dup_object (const GValue *value)
3031 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3033 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3037 * g_signal_connect_object:
3038 * @instance: the instance to connect to.
3039 * @detailed_signal: a string of the form "signal-name::detail".
3040 * @c_handler: the #GCallback to connect.
3041 * @gobject: the object to pass as data to @c_handler.
3042 * @connect_flags: a combination of #GConnnectFlags.
3044 * This is similar to g_signal_connect_data(), but uses a closure which
3045 * ensures that the @gobject stays alive during the call to @c_handler
3046 * by temporarily adding a reference count to @gobject.
3048 * Note that there is a bug in GObject that makes this function
3049 * much less useful than it might seem otherwise. Once @gobject is
3050 * disposed, the callback will no longer be called, but, the signal
3051 * handler is <emphasis>not</emphasis> currently disconnected. If the
3052 * @instance is itself being freed at the same time than this doesn't
3053 * matter, since the signal will automatically be removed, but
3054 * if @instance persists, then the signal handler will leak. You
3055 * should not remove the signal yourself because in a future versions of
3056 * GObject, the handler <emphasis>will</emphasis> automatically
3059 * It's possible to work around this problem in a way that will
3060 * continue to work with future versions of GObject by checking
3061 * that the signal handler is still connected before disconnected it:
3062 * <informalexample><programlisting>
3063 * if (g_signal_handler_is_connected (instance, id))
3064 * g_signal_handler_disconnect (instance, id);
3065 * </programlisting></informalexample>
3067 * Returns: the handler id.
3070 g_signal_connect_object (gpointer instance,
3071 const gchar *detailed_signal,
3072 GCallback c_handler,
3074 GConnectFlags connect_flags)
3076 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3077 g_return_val_if_fail (detailed_signal != NULL, 0);
3078 g_return_val_if_fail (c_handler != NULL, 0);
3084 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3086 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3088 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3091 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3097 GClosure *closures[1]; /* flexible array */
3099 /* don't change this structure without supplying an accessor for
3100 * watched closures, e.g.:
3101 * GSList* g_object_list_watched_closures (GObject *object)
3104 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3105 * carray = g_object_get_data (object, "GObject-closure-array");
3108 * GSList *slist = NULL;
3110 * for (i = 0; i < carray->n_closures; i++)
3111 * slist = g_slist_prepend (slist, carray->closures[i]);
3119 object_remove_closure (gpointer data,
3122 GObject *object = data;
3123 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3126 for (i = 0; i < carray->n_closures; i++)
3127 if (carray->closures[i] == closure)
3129 carray->n_closures--;
3130 if (i < carray->n_closures)
3131 carray->closures[i] = carray->closures[carray->n_closures];
3134 g_assert_not_reached ();
3138 destroy_closure_array (gpointer data)
3140 CArray *carray = data;
3141 GObject *object = carray->object;
3142 guint i, n = carray->n_closures;
3144 for (i = 0; i < n; i++)
3146 GClosure *closure = carray->closures[i];
3148 /* removing object_remove_closure() upfront is probably faster than
3149 * letting it fiddle with quark_closure_array which is empty anyways
3151 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3152 g_closure_invalidate (closure);
3158 * g_object_watch_closure:
3159 * @object: GObject restricting lifetime of @closure
3160 * @closure: GClosure to watch
3162 * This function essentially limits the life time of the @closure to
3163 * the life time of the object. That is, when the object is finalized,
3164 * the @closure is invalidated by calling g_closure_invalidate() on
3165 * it, in order to prevent invocations of the closure with a finalized
3166 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3167 * added as marshal guards to the @closure, to ensure that an extra
3168 * reference count is held on @object during invocation of the
3169 * @closure. Usually, this function will be called on closures that
3170 * use this @object as closure data.
3173 g_object_watch_closure (GObject *object,
3179 g_return_if_fail (G_IS_OBJECT (object));
3180 g_return_if_fail (closure != NULL);
3181 g_return_if_fail (closure->is_invalid == FALSE);
3182 g_return_if_fail (closure->in_marshal == FALSE);
3183 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3185 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3186 g_closure_add_marshal_guards (closure,
3187 object, (GClosureNotify) g_object_ref,
3188 object, (GClosureNotify) g_object_unref);
3189 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3192 carray = g_renew (CArray, NULL, 1);
3193 carray->object = object;
3194 carray->n_closures = 1;
3199 i = carray->n_closures++;
3200 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3202 carray->closures[i] = closure;
3203 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3207 * g_closure_new_object:
3208 * @sizeof_closure: the size of the structure to allocate, must be at least
3209 * <literal>sizeof (GClosure)</literal>
3210 * @object: a #GObject pointer to store in the @data field of the newly
3211 * allocated #GClosure
3213 * A variant of g_closure_new_simple() which stores @object in the
3214 * @data field of the closure and calls g_object_watch_closure() on
3215 * @object and the created closure. This function is mainly useful
3216 * when implementing new types of closures.
3218 * Returns: a newly allocated #GClosure
3221 g_closure_new_object (guint sizeof_closure,
3226 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3227 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3229 closure = g_closure_new_simple (sizeof_closure, object);
3230 g_object_watch_closure (object, closure);
3236 * g_cclosure_new_object:
3237 * @callback_func: the function to invoke
3238 * @object: a #GObject pointer to pass to @callback_func
3240 * A variant of g_cclosure_new() which uses @object as @user_data and
3241 * calls g_object_watch_closure() on @object and the created
3242 * closure. This function is useful when you have a callback closely
3243 * associated with a #GObject, and want the callback to no longer run
3244 * after the object is is freed.
3246 * Returns: a new #GCClosure
3249 g_cclosure_new_object (GCallback callback_func,
3254 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3255 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3256 g_return_val_if_fail (callback_func != NULL, NULL);
3258 closure = g_cclosure_new (callback_func, object, NULL);
3259 g_object_watch_closure (object, closure);
3265 * g_cclosure_new_object_swap:
3266 * @callback_func: the function to invoke
3267 * @object: a #GObject pointer to pass to @callback_func
3269 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3270 * and calls g_object_watch_closure() on @object and the created
3271 * closure. This function is useful when you have a callback closely
3272 * associated with a #GObject, and want the callback to no longer run
3273 * after the object is is freed.
3275 * Returns: a new #GCClosure
3278 g_cclosure_new_object_swap (GCallback callback_func,
3283 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3284 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3285 g_return_val_if_fail (callback_func != NULL, NULL);
3287 closure = g_cclosure_new_swap (callback_func, object, NULL);
3288 g_object_watch_closure (object, closure);
3294 g_object_compat_control (gsize what,
3300 case 1: /* floating base type */
3301 return G_TYPE_INITIALLY_UNOWNED;
3302 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3303 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3305 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3307 *pp = floating_flag_handler;
3314 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3317 g_initially_unowned_init (GInitiallyUnowned *object)
3319 g_object_force_floating (object);
3323 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)