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);
982 /* FIXME: Freezing is the only way to get at the notify queue.
983 * So we freeze once and then thaw twice.
985 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
986 g_object_notify_queue_thaw (object, nqueue);
987 g_object_notify_queue_thaw (object, nqueue);
989 g_object_unref (object);
993 object_get_property (GObject *object,
997 GObjectClass *class = g_type_class_peek (pspec->owner_type);
998 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
999 GParamSpec *redirect;
1001 redirect = g_param_spec_get_redirect_target (pspec);
1005 class->get_property (object, param_id, value, pspec);
1009 object_set_property (GObject *object,
1011 const GValue *value,
1012 GObjectNotifyQueue *nqueue)
1014 GValue tmp_value = { 0, };
1015 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1016 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1017 GParamSpec *redirect;
1018 static gchar* enable_diagnostic = NULL;
1020 redirect = g_param_spec_get_redirect_target (pspec);
1024 if (G_UNLIKELY (!enable_diagnostic))
1026 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1027 if (!enable_diagnostic)
1028 enable_diagnostic = "0";
1031 if (enable_diagnostic[0] == '1')
1033 if (pspec->flags & G_PARAM_DEPRECATED)
1034 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1035 "anymore. It will be removed in a future version.",
1036 G_OBJECT_TYPE_NAME (object), pspec->name);
1039 /* provide a copy to work from, convert (if necessary) and validate */
1040 g_value_init (&tmp_value, pspec->value_type);
1041 if (!g_value_transform (value, &tmp_value))
1042 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1044 g_type_name (pspec->value_type),
1045 G_VALUE_TYPE_NAME (value));
1046 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1048 gchar *contents = g_strdup_value_contents (value);
1050 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1052 G_VALUE_TYPE_NAME (value),
1054 g_type_name (pspec->value_type));
1059 class->set_property (object, param_id, &tmp_value, pspec);
1060 g_object_notify_queue_add (object, nqueue, pspec);
1062 g_value_unset (&tmp_value);
1066 object_interface_check_properties (gpointer func_data,
1069 GTypeInterface *iface_class = g_iface;
1070 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1071 GType iface_type = iface_class->g_type;
1072 GParamSpec **pspecs;
1075 if (!G_IS_OBJECT_CLASS (class))
1078 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1082 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1084 G_OBJECT_CLASS_TYPE (class),
1089 g_critical ("Object class %s doesn't implement property "
1090 "'%s' from interface '%s'",
1091 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1093 g_type_name (iface_type));
1098 /* The implementation paramspec must have a less restrictive
1099 * type than the interface parameter spec for set() and a
1100 * more restrictive type for get(). We just require equality,
1101 * rather than doing something more complicated checking
1102 * the READABLE and WRITABLE flags. We also simplify here
1103 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1106 !g_type_is_a (pspecs[n]->value_type,
1107 class_pspec->value_type))
1109 g_critical ("Property '%s' on class '%s' has type '%s' "
1110 "which is different from the type '%s', "
1111 "of the property on interface '%s'\n",
1113 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1114 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1115 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1116 g_type_name (iface_type));
1119 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1121 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1122 * READABLE and WRITABLE remove restrictions. The implementation
1123 * paramspec must have less restrictive flags.
1126 (!SUBSET (class_pspec->flags,
1128 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1129 !SUBSET (pspecs[n]->flags,
1131 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1133 g_critical ("Flags for property '%s' on class '%s' "
1134 "are not compatible with the property on"
1137 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1138 g_type_name (iface_type));
1147 g_object_get_type (void)
1149 return G_TYPE_OBJECT;
1154 * @object_type: the type id of the #GObject subtype to instantiate
1155 * @first_property_name: the name of the first property
1156 * @...: the value of the first property, followed optionally by more
1157 * name/value pairs, followed by %NULL
1159 * Creates a new instance of a #GObject subtype and sets its properties.
1161 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1162 * which are not explicitly specified are set to their default values.
1164 * Returns: a new instance of @object_type
1167 g_object_new (GType object_type,
1168 const gchar *first_property_name,
1174 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1176 /* short circuit for calls supplying no properties */
1177 if (!first_property_name)
1178 return g_object_newv (object_type, 0, NULL);
1180 va_start (var_args, first_property_name);
1181 object = g_object_new_valist (object_type, first_property_name, var_args);
1188 slist_maybe_remove (GSList **slist,
1191 GSList *last = NULL, *node = *slist;
1194 if (node->data == data)
1197 last->next = node->next;
1199 *slist = node->next;
1200 g_slist_free_1 (node);
1209 static inline gboolean
1210 object_in_construction_list (GObject *object)
1212 gboolean in_construction;
1213 G_LOCK (construction_mutex);
1214 in_construction = g_slist_find (construction_objects, object) != NULL;
1215 G_UNLOCK (construction_mutex);
1216 return in_construction;
1221 * @object_type: the type id of the #GObject subtype to instantiate
1222 * @n_parameters: the length of the @parameters array
1223 * @parameters: an array of #GParameter
1225 * Creates a new instance of a #GObject subtype and sets its properties.
1227 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1228 * which are not explicitly specified are set to their default values.
1230 * Returns: a new instance of @object_type
1233 g_object_newv (GType object_type,
1235 GParameter *parameters)
1237 GObjectConstructParam *cparams = NULL, *oparams;
1238 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1240 GObjectClass *class, *unref_class = NULL;
1242 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1244 GList *clist = NULL;
1245 gboolean newly_constructed;
1248 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1250 class = g_type_class_peek_static (object_type);
1252 class = unref_class = g_type_class_ref (object_type);
1253 for (slist = class->construct_properties; slist; slist = slist->next)
1255 clist = g_list_prepend (clist, slist->data);
1256 n_total_cparams += 1;
1259 if (n_parameters == 0 && n_total_cparams == 0)
1261 /* This is a simple object with no construct properties, and
1262 * no properties are being set, so short circuit the parameter
1263 * handling. This speeds up simple object construction.
1266 object = class->constructor (object_type, 0, NULL);
1267 goto did_construction;
1270 /* collect parameters, sort into construction and normal ones */
1271 oparams = g_new (GObjectConstructParam, n_parameters);
1272 cparams = g_new (GObjectConstructParam, n_total_cparams);
1273 for (i = 0; i < n_parameters; i++)
1275 GValue *value = ¶meters[i].value;
1276 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1282 g_warning ("%s: object class `%s' has no property named `%s'",
1284 g_type_name (object_type),
1285 parameters[i].name);
1288 if (!(pspec->flags & G_PARAM_WRITABLE))
1290 g_warning ("%s: property `%s' of object class `%s' is not writable",
1293 g_type_name (object_type));
1296 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1298 GList *list = g_list_find (clist, pspec);
1302 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1303 G_STRFUNC, pspec->name, g_type_name (object_type));
1306 cparams[n_cparams].pspec = pspec;
1307 cparams[n_cparams].value = value;
1312 list->prev->next = list->next;
1314 list->next->prev = list->prev;
1315 g_list_free_1 (list);
1319 oparams[n_oparams].pspec = pspec;
1320 oparams[n_oparams].value = value;
1325 /* set remaining construction properties to default values */
1326 n_cvalues = n_total_cparams - n_cparams;
1327 cvalues = g_new (GValue, n_cvalues);
1330 GList *tmp = clist->next;
1331 GParamSpec *pspec = clist->data;
1332 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1335 g_value_init (value, pspec->value_type);
1336 g_param_value_set_default (pspec, value);
1338 cparams[n_cparams].pspec = pspec;
1339 cparams[n_cparams].value = value;
1342 g_list_free_1 (clist);
1346 /* construct object from construction parameters */
1347 object = class->constructor (object_type, n_total_cparams, cparams);
1348 /* free construction values */
1351 g_value_unset (cvalues + n_cvalues);
1355 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1357 /* adjust freeze_count according to g_object_init() and remaining properties */
1358 G_LOCK (construction_mutex);
1359 newly_constructed = slist_maybe_remove (&construction_objects, object);
1360 G_UNLOCK (construction_mutex);
1363 newly_constructed = TRUE;
1365 if (CLASS_HAS_PROPS (class))
1367 if (newly_constructed || n_oparams)
1368 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1369 if (newly_constructed)
1370 g_object_notify_queue_thaw (object, nqueue);
1373 /* run 'constructed' handler if there is one */
1374 if (newly_constructed && class->constructed)
1375 class->constructed (object);
1377 /* set remaining properties */
1378 for (i = 0; i < n_oparams; i++)
1379 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1382 if (CLASS_HAS_PROPS (class))
1384 /* release our own freeze count and handle notifications */
1385 if (newly_constructed || n_oparams)
1386 g_object_notify_queue_thaw (object, nqueue);
1390 g_type_class_unref (unref_class);
1396 * g_object_new_valist:
1397 * @object_type: the type id of the #GObject subtype to instantiate
1398 * @first_property_name: the name of the first property
1399 * @var_args: the value of the first property, followed optionally by more
1400 * name/value pairs, followed by %NULL
1402 * Creates a new instance of a #GObject subtype and sets its properties.
1404 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1405 * which are not explicitly specified are set to their default values.
1407 * Returns: a new instance of @object_type
1410 g_object_new_valist (GType object_type,
1411 const gchar *first_property_name,
1414 GObjectClass *class;
1418 guint n_params = 0, n_alloced_params = 16;
1420 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1422 if (!first_property_name)
1423 return g_object_newv (object_type, 0, NULL);
1425 class = g_type_class_ref (object_type);
1427 params = g_new0 (GParameter, n_alloced_params);
1428 name = first_property_name;
1431 gchar *error = NULL;
1432 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1438 g_warning ("%s: object class `%s' has no property named `%s'",
1440 g_type_name (object_type),
1444 if (n_params >= n_alloced_params)
1446 n_alloced_params += 16;
1447 params = g_renew (GParameter, params, n_alloced_params);
1449 params[n_params].name = name;
1450 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1451 var_args, 0, &error);
1454 g_warning ("%s: %s", G_STRFUNC, error);
1456 g_value_unset (¶ms[n_params].value);
1460 name = va_arg (var_args, gchar*);
1463 object = g_object_newv (object_type, n_params, params);
1466 g_value_unset (¶ms[n_params].value);
1469 g_type_class_unref (class);
1475 g_object_constructor (GType type,
1476 guint n_construct_properties,
1477 GObjectConstructParam *construct_params)
1482 object = (GObject*) g_type_create_instance (type);
1484 /* set construction parameters */
1485 if (n_construct_properties)
1487 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1489 /* set construct properties */
1490 while (n_construct_properties--)
1492 GValue *value = construct_params->value;
1493 GParamSpec *pspec = construct_params->pspec;
1496 object_set_property (object, pspec, value, nqueue);
1498 g_object_notify_queue_thaw (object, nqueue);
1499 /* the notification queue is still frozen from g_object_init(), so
1500 * we don't need to handle it here, g_object_newv() takes
1509 * g_object_set_valist:
1510 * @object: a #GObject
1511 * @first_property_name: name of the first property to set
1512 * @var_args: value for the first property, followed optionally by more
1513 * name/value pairs, followed by %NULL
1515 * Sets properties on an object.
1518 g_object_set_valist (GObject *object,
1519 const gchar *first_property_name,
1522 GObjectNotifyQueue *nqueue;
1525 g_return_if_fail (G_IS_OBJECT (object));
1527 g_object_ref (object);
1528 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1530 name = first_property_name;
1533 GValue value = { 0, };
1535 gchar *error = NULL;
1537 pspec = g_param_spec_pool_lookup (pspec_pool,
1539 G_OBJECT_TYPE (object),
1543 g_warning ("%s: object class `%s' has no property named `%s'",
1545 G_OBJECT_TYPE_NAME (object),
1549 if (!(pspec->flags & G_PARAM_WRITABLE))
1551 g_warning ("%s: property `%s' of object class `%s' is not writable",
1554 G_OBJECT_TYPE_NAME (object));
1557 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1559 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1560 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1564 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1568 g_warning ("%s: %s", G_STRFUNC, error);
1570 g_value_unset (&value);
1574 object_set_property (object, pspec, &value, nqueue);
1575 g_value_unset (&value);
1577 name = va_arg (var_args, gchar*);
1580 g_object_notify_queue_thaw (object, nqueue);
1581 g_object_unref (object);
1585 * g_object_get_valist:
1586 * @object: a #GObject
1587 * @first_property_name: name of the first property to get
1588 * @var_args: return location for the first property, followed optionally by more
1589 * name/return location pairs, followed by %NULL
1591 * Gets properties of an object.
1593 * In general, a copy is made of the property contents and the caller
1594 * is responsible for freeing the memory in the appropriate manner for
1595 * the type, for instance by calling g_free() or g_object_unref().
1597 * See g_object_get().
1600 g_object_get_valist (GObject *object,
1601 const gchar *first_property_name,
1606 g_return_if_fail (G_IS_OBJECT (object));
1608 g_object_ref (object);
1610 name = first_property_name;
1614 GValue value = { 0, };
1618 pspec = g_param_spec_pool_lookup (pspec_pool,
1620 G_OBJECT_TYPE (object),
1624 g_warning ("%s: object class `%s' has no property named `%s'",
1626 G_OBJECT_TYPE_NAME (object),
1630 if (!(pspec->flags & G_PARAM_READABLE))
1632 g_warning ("%s: property `%s' of object class `%s' is not readable",
1635 G_OBJECT_TYPE_NAME (object));
1639 g_value_init (&value, pspec->value_type);
1641 object_get_property (object, pspec, &value);
1643 G_VALUE_LCOPY (&value, var_args, 0, &error);
1646 g_warning ("%s: %s", G_STRFUNC, error);
1648 g_value_unset (&value);
1652 g_value_unset (&value);
1654 name = va_arg (var_args, gchar*);
1657 g_object_unref (object);
1662 * @object: a #GObject
1663 * @first_property_name: name of the first property to set
1664 * @...: value for the first property, followed optionally by more
1665 * name/value pairs, followed by %NULL
1667 * Sets properties on an object.
1670 g_object_set (gpointer _object,
1671 const gchar *first_property_name,
1674 GObject *object = _object;
1677 g_return_if_fail (G_IS_OBJECT (object));
1679 va_start (var_args, first_property_name);
1680 g_object_set_valist (object, first_property_name, var_args);
1686 * @object: a #GObject
1687 * @first_property_name: name of the first property to get
1688 * @...: return location for the first property, followed optionally by more
1689 * name/return location pairs, followed by %NULL
1691 * Gets properties of an object.
1693 * In general, a copy is made of the property contents and the caller
1694 * is responsible for freeing the memory in the appropriate manner for
1695 * the type, for instance by calling g_free() or g_object_unref().
1698 * <title>Using g_object_get(<!-- -->)</title>
1699 * An example of using g_object_get() to get the contents
1700 * of three properties - one of type #G_TYPE_INT,
1701 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1707 * g_object_get (my_object,
1708 * "int-property", &intval,
1709 * "str-property", &strval,
1710 * "obj-property", &objval,
1713 * // Do something with intval, strval, objval
1716 * g_object_unref (objval);
1721 g_object_get (gpointer _object,
1722 const gchar *first_property_name,
1725 GObject *object = _object;
1728 g_return_if_fail (G_IS_OBJECT (object));
1730 va_start (var_args, first_property_name);
1731 g_object_get_valist (object, first_property_name, var_args);
1736 * g_object_set_property:
1737 * @object: a #GObject
1738 * @property_name: the name of the property to set
1741 * Sets a property on an object.
1744 g_object_set_property (GObject *object,
1745 const gchar *property_name,
1746 const GValue *value)
1748 GObjectNotifyQueue *nqueue;
1751 g_return_if_fail (G_IS_OBJECT (object));
1752 g_return_if_fail (property_name != NULL);
1753 g_return_if_fail (G_IS_VALUE (value));
1755 g_object_ref (object);
1756 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1758 pspec = g_param_spec_pool_lookup (pspec_pool,
1760 G_OBJECT_TYPE (object),
1763 g_warning ("%s: object class `%s' has no property named `%s'",
1765 G_OBJECT_TYPE_NAME (object),
1767 else if (!(pspec->flags & G_PARAM_WRITABLE))
1768 g_warning ("%s: property `%s' of object class `%s' is not writable",
1771 G_OBJECT_TYPE_NAME (object));
1772 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1773 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1774 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1776 object_set_property (object, pspec, value, nqueue);
1778 g_object_notify_queue_thaw (object, nqueue);
1779 g_object_unref (object);
1783 * g_object_get_property:
1784 * @object: a #GObject
1785 * @property_name: the name of the property to get
1786 * @value: return location for the property value
1788 * Gets a property of an object.
1790 * In general, a copy is made of the property contents and the caller is
1791 * responsible for freeing the memory by calling g_value_unset().
1793 * Note that g_object_get_property() is really intended for language
1794 * bindings, g_object_get() is much more convenient for C programming.
1797 g_object_get_property (GObject *object,
1798 const gchar *property_name,
1803 g_return_if_fail (G_IS_OBJECT (object));
1804 g_return_if_fail (property_name != NULL);
1805 g_return_if_fail (G_IS_VALUE (value));
1807 g_object_ref (object);
1809 pspec = g_param_spec_pool_lookup (pspec_pool,
1811 G_OBJECT_TYPE (object),
1814 g_warning ("%s: object class `%s' has no property named `%s'",
1816 G_OBJECT_TYPE_NAME (object),
1818 else if (!(pspec->flags & G_PARAM_READABLE))
1819 g_warning ("%s: property `%s' of object class `%s' is not readable",
1822 G_OBJECT_TYPE_NAME (object));
1825 GValue *prop_value, tmp_value = { 0, };
1827 /* auto-conversion of the callers value type
1829 if (G_VALUE_TYPE (value) == pspec->value_type)
1831 g_value_reset (value);
1834 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1836 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1837 G_STRFUNC, pspec->name,
1838 g_type_name (pspec->value_type),
1839 G_VALUE_TYPE_NAME (value));
1840 g_object_unref (object);
1845 g_value_init (&tmp_value, pspec->value_type);
1846 prop_value = &tmp_value;
1848 object_get_property (object, pspec, prop_value);
1849 if (prop_value != value)
1851 g_value_transform (prop_value, value);
1852 g_value_unset (&tmp_value);
1856 g_object_unref (object);
1861 * @object: a #GObject
1862 * @signal_spec: the spec for the first signal
1863 * @...: #GCallback for the first signal, followed by data for the
1864 * first signal, followed optionally by more signal
1865 * spec/callback/data triples, followed by %NULL
1867 * A convenience function to connect multiple signals at once.
1869 * The signal specs expected by this function have the form
1870 * "modifier::signal_name", where modifier can be one of the following:
1873 * <term>signal</term>
1875 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1876 * </para></listitem>
1879 * <term>object_signal</term>
1880 * <term>object-signal</term>
1882 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
1883 * </para></listitem>
1886 * <term>swapped_signal</term>
1887 * <term>swapped-signal</term>
1889 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
1890 * </para></listitem>
1893 * <term>swapped_object_signal</term>
1894 * <term>swapped-object-signal</term>
1896 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
1897 * </para></listitem>
1900 * <term>signal_after</term>
1901 * <term>signal-after</term>
1903 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
1904 * </para></listitem>
1907 * <term>object_signal_after</term>
1908 * <term>object-signal-after</term>
1910 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
1911 * </para></listitem>
1914 * <term>swapped_signal_after</term>
1915 * <term>swapped-signal-after</term>
1917 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1918 * </para></listitem>
1921 * <term>swapped_object_signal_after</term>
1922 * <term>swapped-object-signal-after</term>
1924 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
1925 * </para></listitem>
1930 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1931 * "type", GTK_WINDOW_POPUP,
1934 * "signal::event", gtk_menu_window_event, menu,
1935 * "signal::size_request", gtk_menu_window_size_request, menu,
1936 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1943 g_object_connect (gpointer _object,
1944 const gchar *signal_spec,
1947 GObject *object = _object;
1950 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
1951 g_return_val_if_fail (object->ref_count > 0, object);
1953 va_start (var_args, signal_spec);
1956 GCallback callback = va_arg (var_args, GCallback);
1957 gpointer data = va_arg (var_args, gpointer);
1960 if (strncmp (signal_spec, "signal::", 8) == 0)
1961 sid = g_signal_connect_data (object, signal_spec + 8,
1962 callback, data, NULL,
1964 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
1965 strncmp (signal_spec, "object-signal::", 15) == 0)
1966 sid = g_signal_connect_object (object, signal_spec + 15,
1969 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
1970 strncmp (signal_spec, "swapped-signal::", 16) == 0)
1971 sid = g_signal_connect_data (object, signal_spec + 16,
1972 callback, data, NULL,
1974 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
1975 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
1976 sid = g_signal_connect_object (object, signal_spec + 23,
1979 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
1980 strncmp (signal_spec, "signal-after::", 14) == 0)
1981 sid = g_signal_connect_data (object, signal_spec + 14,
1982 callback, data, NULL,
1984 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
1985 strncmp (signal_spec, "object-signal-after::", 21) == 0)
1986 sid = g_signal_connect_object (object, signal_spec + 21,
1989 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
1990 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
1991 sid = g_signal_connect_data (object, signal_spec + 22,
1992 callback, data, NULL,
1993 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
1994 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
1995 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
1996 sid = g_signal_connect_object (object, signal_spec + 29,
1998 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2001 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2004 signal_spec = va_arg (var_args, gchar*);
2012 * g_object_disconnect:
2013 * @object: a #GObject
2014 * @signal_spec: the spec for the first signal
2015 * @...: #GCallback for the first signal, followed by data for the first signal,
2016 * followed optionally by more signal spec/callback/data triples,
2019 * A convenience function to disconnect multiple signals at once.
2021 * The signal specs expected by this function have the form
2022 * "any_signal", which means to disconnect any signal with matching
2023 * callback and data, or "any_signal::signal_name", which only
2024 * disconnects the signal named "signal_name".
2027 g_object_disconnect (gpointer _object,
2028 const gchar *signal_spec,
2031 GObject *object = _object;
2034 g_return_if_fail (G_IS_OBJECT (object));
2035 g_return_if_fail (object->ref_count > 0);
2037 va_start (var_args, signal_spec);
2040 GCallback callback = va_arg (var_args, GCallback);
2041 gpointer data = va_arg (var_args, gpointer);
2042 guint sid = 0, detail = 0, mask = 0;
2044 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2045 strncmp (signal_spec, "any-signal::", 12) == 0)
2048 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2050 else if (strcmp (signal_spec, "any_signal") == 0 ||
2051 strcmp (signal_spec, "any-signal") == 0)
2054 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2058 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2062 if ((mask & G_SIGNAL_MATCH_ID) &&
2063 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2064 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2065 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2067 NULL, (gpointer)callback, data))
2068 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2069 signal_spec = va_arg (var_args, gchar*);
2080 } weak_refs[1]; /* flexible array */
2084 weak_refs_notify (gpointer data)
2086 WeakRefStack *wstack = data;
2089 for (i = 0; i < wstack->n_weak_refs; i++)
2090 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2095 * g_object_weak_ref:
2096 * @object: #GObject to reference weakly
2097 * @notify: callback to invoke before the object is freed
2098 * @data: extra data to pass to notify
2100 * Adds a weak reference callback to an object. Weak references are
2101 * used for notification when an object is finalized. They are called
2102 * "weak references" because they allow you to safely hold a pointer
2103 * to an object without calling g_object_ref() (g_object_ref() adds a
2104 * strong reference, that is, forces the object to stay alive).
2107 g_object_weak_ref (GObject *object,
2111 WeakRefStack *wstack;
2114 g_return_if_fail (G_IS_OBJECT (object));
2115 g_return_if_fail (notify != NULL);
2116 g_return_if_fail (object->ref_count >= 1);
2118 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2121 i = wstack->n_weak_refs++;
2122 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2126 wstack = g_renew (WeakRefStack, NULL, 1);
2127 wstack->object = object;
2128 wstack->n_weak_refs = 1;
2131 wstack->weak_refs[i].notify = notify;
2132 wstack->weak_refs[i].data = data;
2133 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2137 * g_object_weak_unref:
2138 * @object: #GObject to remove a weak reference from
2139 * @notify: callback to search for
2140 * @data: data to search for
2142 * Removes a weak reference callback to an object.
2145 g_object_weak_unref (GObject *object,
2149 WeakRefStack *wstack;
2150 gboolean found_one = FALSE;
2152 g_return_if_fail (G_IS_OBJECT (object));
2153 g_return_if_fail (notify != NULL);
2155 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2160 for (i = 0; i < wstack->n_weak_refs; i++)
2161 if (wstack->weak_refs[i].notify == notify &&
2162 wstack->weak_refs[i].data == data)
2165 wstack->n_weak_refs -= 1;
2166 if (i != wstack->n_weak_refs)
2167 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2173 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2177 * g_object_add_weak_pointer:
2178 * @object: The object that should be weak referenced.
2179 * @weak_pointer_location: The memory address of a pointer.
2181 * Adds a weak reference from weak_pointer to @object to indicate that
2182 * the pointer located at @weak_pointer_location is only valid during
2183 * the lifetime of @object. When the @object is finalized,
2184 * @weak_pointer will be set to %NULL.
2187 g_object_add_weak_pointer (GObject *object,
2188 gpointer *weak_pointer_location)
2190 g_return_if_fail (G_IS_OBJECT (object));
2191 g_return_if_fail (weak_pointer_location != NULL);
2193 g_object_weak_ref (object,
2194 (GWeakNotify) g_nullify_pointer,
2195 weak_pointer_location);
2199 * g_object_remove_weak_pointer:
2200 * @object: The object that is weak referenced.
2201 * @weak_pointer_location: The memory address of a pointer.
2203 * Removes a weak reference from @object that was previously added
2204 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2205 * to match the one used with g_object_add_weak_pointer().
2208 g_object_remove_weak_pointer (GObject *object,
2209 gpointer *weak_pointer_location)
2211 g_return_if_fail (G_IS_OBJECT (object));
2212 g_return_if_fail (weak_pointer_location != NULL);
2214 g_object_weak_unref (object,
2215 (GWeakNotify) g_nullify_pointer,
2216 weak_pointer_location);
2220 object_floating_flag_handler (GObject *object,
2226 case +1: /* force floating if possible */
2228 oldvalue = g_atomic_pointer_get (&object->qdata);
2229 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2230 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2231 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2232 case -1: /* sink if possible */
2234 oldvalue = g_atomic_pointer_get (&object->qdata);
2235 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2236 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2237 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2238 default: /* check floating */
2239 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2244 * g_object_is_floating:
2245 * @object: a #GObject
2247 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2252 * Returns: %TRUE if @object has a floating reference
2255 g_object_is_floating (gpointer _object)
2257 GObject *object = _object;
2258 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2259 return floating_flag_handler (object, 0);
2263 * g_object_ref_sink:
2264 * @object: a #GObject
2266 * Increase the reference count of @object, and possibly remove the
2267 * <link linkend="floating-ref">floating</link> reference, if @object
2268 * has a floating reference.
2270 * In other words, if the object is floating, then this call "assumes
2271 * ownership" of the floating reference, converting it to a normal
2272 * reference by clearing the floating flag while leaving the reference
2273 * count unchanged. If the object is not floating, then this call
2274 * adds a new normal reference increasing the reference count by one.
2281 g_object_ref_sink (gpointer _object)
2283 GObject *object = _object;
2284 gboolean was_floating;
2285 g_return_val_if_fail (G_IS_OBJECT (object), object);
2286 g_return_val_if_fail (object->ref_count >= 1, object);
2287 g_object_ref (object);
2288 was_floating = floating_flag_handler (object, -1);
2290 g_object_unref (object);
2295 * g_object_force_floating:
2296 * @object: a #GObject
2298 * This function is intended for #GObject implementations to re-enforce a
2299 * <link linkend="floating-ref">floating</link> object reference.
2300 * Doing this is seldomly required, all
2301 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2302 * usually just needs to be sunken by calling g_object_ref_sink().
2307 g_object_force_floating (GObject *object)
2309 gboolean was_floating;
2310 g_return_if_fail (G_IS_OBJECT (object));
2311 g_return_if_fail (object->ref_count >= 1);
2313 was_floating = floating_flag_handler (object, +1);
2318 guint n_toggle_refs;
2320 GToggleNotify notify;
2322 } toggle_refs[1]; /* flexible array */
2326 toggle_refs_notify (GObject *object,
2327 gboolean is_last_ref)
2329 ToggleRefStack *tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2331 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2332 * will only be notified when there is exactly one of them.
2334 g_assert (tstack->n_toggle_refs == 1);
2335 tstack->toggle_refs[0].notify (tstack->toggle_refs[0].data, tstack->object, is_last_ref);
2339 * g_object_add_toggle_ref:
2340 * @object: a #GObject
2341 * @notify: a function to call when this reference is the
2342 * last reference to the object, or is no longer
2343 * the last reference.
2344 * @data: data to pass to @notify
2346 * Increases the reference count of the object by one and sets a
2347 * callback to be called when all other references to the object are
2348 * dropped, or when this is already the last reference to the object
2349 * and another reference is established.
2351 * This functionality is intended for binding @object to a proxy
2352 * object managed by another memory manager. This is done with two
2353 * paired references: the strong reference added by
2354 * g_object_add_toggle_ref() and a reverse reference to the proxy
2355 * object which is either a strong reference or weak reference.
2357 * The setup is that when there are no other references to @object,
2358 * only a weak reference is held in the reverse direction from @object
2359 * to the proxy object, but when there are other references held to
2360 * @object, a strong reference is held. The @notify callback is called
2361 * when the reference from @object to the proxy object should be
2362 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2363 * true) or weak to strong (@is_last_ref false).
2365 * Since a (normal) reference must be held to the object before
2366 * calling g_object_toggle_ref(), the initial state of the reverse
2367 * link is always strong.
2369 * Multiple toggle references may be added to the same gobject,
2370 * however if there are multiple toggle references to an object, none
2371 * of them will ever be notified until all but one are removed. For
2372 * this reason, you should only ever use a toggle reference if there
2373 * is important state in the proxy object.
2378 g_object_add_toggle_ref (GObject *object,
2379 GToggleNotify notify,
2382 ToggleRefStack *tstack;
2385 g_return_if_fail (G_IS_OBJECT (object));
2386 g_return_if_fail (notify != NULL);
2387 g_return_if_fail (object->ref_count >= 1);
2389 g_object_ref (object);
2391 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2394 i = tstack->n_toggle_refs++;
2395 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2396 * in tstate->toggle_refs */
2397 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2401 tstack = g_renew (ToggleRefStack, NULL, 1);
2402 tstack->object = object;
2403 tstack->n_toggle_refs = 1;
2407 /* Set a flag for fast lookup after adding the first toggle reference */
2408 if (tstack->n_toggle_refs == 1)
2409 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2411 tstack->toggle_refs[i].notify = notify;
2412 tstack->toggle_refs[i].data = data;
2413 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2414 (GDestroyNotify)g_free);
2418 * g_object_remove_toggle_ref:
2419 * @object: a #GObject
2420 * @notify: a function to call when this reference is the
2421 * last reference to the object, or is no longer
2422 * the last reference.
2423 * @data: data to pass to @notify
2425 * Removes a reference added with g_object_add_toggle_ref(). The
2426 * reference count of the object is decreased by one.
2431 g_object_remove_toggle_ref (GObject *object,
2432 GToggleNotify notify,
2435 ToggleRefStack *tstack;
2436 gboolean found_one = FALSE;
2438 g_return_if_fail (G_IS_OBJECT (object));
2439 g_return_if_fail (notify != NULL);
2441 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2446 for (i = 0; i < tstack->n_toggle_refs; i++)
2447 if (tstack->toggle_refs[i].notify == notify &&
2448 tstack->toggle_refs[i].data == data)
2451 tstack->n_toggle_refs -= 1;
2452 if (i != tstack->n_toggle_refs)
2453 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2455 if (tstack->n_toggle_refs == 0)
2456 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2458 g_object_unref (object);
2465 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2470 * @object: a #GObject
2472 * Increases the reference count of @object.
2474 * Returns: the same @object
2477 g_object_ref (gpointer _object)
2479 GObject *object = _object;
2482 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2483 g_return_val_if_fail (object->ref_count > 0, NULL);
2485 #ifdef G_ENABLE_DEBUG
2486 if (g_trap_object_ref == object)
2488 #endif /* G_ENABLE_DEBUG */
2491 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2493 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2494 toggle_refs_notify (object, FALSE);
2496 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2503 * @object: a #GObject
2505 * Decreases the reference count of @object. When its reference count
2506 * drops to 0, the object is finalized (i.e. its memory is freed).
2509 g_object_unref (gpointer _object)
2511 GObject *object = _object;
2514 g_return_if_fail (G_IS_OBJECT (object));
2515 g_return_if_fail (object->ref_count > 0);
2517 #ifdef G_ENABLE_DEBUG
2518 if (g_trap_object_ref == object)
2520 #endif /* G_ENABLE_DEBUG */
2522 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2523 retry_atomic_decrement1:
2524 old_ref = g_atomic_int_get (&object->ref_count);
2527 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2528 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2530 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2531 goto retry_atomic_decrement1;
2533 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2535 /* if we went from 2->1 we need to notify toggle refs if any */
2536 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2537 toggle_refs_notify (object, TRUE);
2541 /* we are about tp remove the last reference */
2542 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2543 G_OBJECT_GET_CLASS (object)->dispose (object);
2544 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2546 /* may have been re-referenced meanwhile */
2547 retry_atomic_decrement2:
2548 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2551 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2552 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2554 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2555 goto retry_atomic_decrement2;
2557 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2559 /* if we went from 2->1 we need to notify toggle refs if any */
2560 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2561 toggle_refs_notify (object, TRUE);
2566 /* we are still in the process of taking away the last ref */
2567 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2568 g_signal_handlers_destroy (object);
2569 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2571 /* decrement the last reference */
2572 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2574 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2576 /* may have been re-referenced meanwhile */
2577 if (G_LIKELY (old_ref == 1))
2579 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2580 G_OBJECT_GET_CLASS (object)->finalize (object);
2582 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2584 #ifdef G_ENABLE_DEBUG
2587 /* catch objects not chaining finalize handlers */
2588 G_LOCK (debug_objects);
2589 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2590 G_UNLOCK (debug_objects);
2592 #endif /* G_ENABLE_DEBUG */
2593 g_type_free_instance ((GTypeInstance*) object);
2599 * g_object_get_qdata:
2600 * @object: The GObject to get a stored user data pointer from
2601 * @quark: A #GQuark, naming the user data pointer
2603 * This function gets back user data pointers stored via
2604 * g_object_set_qdata().
2606 * Returns: The user data pointer set, or %NULL
2609 g_object_get_qdata (GObject *object,
2612 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2614 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2618 * g_object_set_qdata:
2619 * @object: The GObject to set store a user data pointer
2620 * @quark: A #GQuark, naming the user data pointer
2621 * @data: An opaque user data pointer
2623 * This sets an opaque, named pointer on an object.
2624 * The name is specified through a #GQuark (retrived e.g. via
2625 * g_quark_from_static_string()), and the pointer
2626 * can be gotten back from the @object with g_object_get_qdata()
2627 * until the @object is finalized.
2628 * Setting a previously set user data pointer, overrides (frees)
2629 * the old pointer set, using #NULL as pointer essentially
2630 * removes the data stored.
2633 g_object_set_qdata (GObject *object,
2637 g_return_if_fail (G_IS_OBJECT (object));
2638 g_return_if_fail (quark > 0);
2640 g_datalist_id_set_data (&object->qdata, quark, data);
2644 * g_object_set_qdata_full:
2645 * @object: The GObject to set store a user data pointer
2646 * @quark: A #GQuark, naming the user data pointer
2647 * @data: An opaque user data pointer
2648 * @destroy: Function to invoke with @data as argument, when @data
2651 * This function works like g_object_set_qdata(), but in addition,
2652 * a void (*destroy) (gpointer) function may be specified which is
2653 * called with @data as argument when the @object is finalized, or
2654 * the data is being overwritten by a call to g_object_set_qdata()
2655 * with the same @quark.
2658 g_object_set_qdata_full (GObject *object,
2661 GDestroyNotify destroy)
2663 g_return_if_fail (G_IS_OBJECT (object));
2664 g_return_if_fail (quark > 0);
2666 g_datalist_id_set_data_full (&object->qdata, quark, data,
2667 data ? destroy : (GDestroyNotify) NULL);
2671 * g_object_steal_qdata:
2672 * @object: The GObject to get a stored user data pointer from
2673 * @quark: A #GQuark, naming the user data pointer
2675 * This function gets back user data pointers stored via
2676 * g_object_set_qdata() and removes the @data from object
2677 * without invoking its destroy() function (if any was
2679 * Usually, calling this function is only required to update
2680 * user data pointers with a destroy notifier, for example:
2683 * object_add_to_user_list (GObject *object,
2684 * const gchar *new_string)
2686 * // the quark, naming the object data
2687 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2688 * // retrive the old string list
2689 * GList *list = g_object_steal_qdata (object, quark_string_list);
2691 * // prepend new string
2692 * list = g_list_prepend (list, g_strdup (new_string));
2693 * // this changed 'list', so we need to set it again
2694 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2697 * free_string_list (gpointer data)
2699 * GList *node, *list = data;
2701 * for (node = list; node; node = node->next)
2702 * g_free (node->data);
2703 * g_list_free (list);
2706 * Using g_object_get_qdata() in the above example, instead of
2707 * g_object_steal_qdata() would have left the destroy function set,
2708 * and thus the partial string list would have been freed upon
2709 * g_object_set_qdata_full().
2711 * Returns: The user data pointer set, or %NULL
2714 g_object_steal_qdata (GObject *object,
2717 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2718 g_return_val_if_fail (quark > 0, NULL);
2720 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2724 * g_object_get_data:
2725 * @object: #GObject containing the associations
2726 * @key: name of the key for that association
2728 * Gets a named field from the objects table of associations (see g_object_set_data()).
2730 * Returns: the data if found, or %NULL if no such data exists.
2733 g_object_get_data (GObject *object,
2738 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2739 g_return_val_if_fail (key != NULL, NULL);
2741 quark = g_quark_try_string (key);
2743 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2747 * g_object_set_data:
2748 * @object: #GObject containing the associations.
2749 * @key: name of the key
2750 * @data: data to associate with that key
2752 * Each object carries around a table of associations from
2753 * strings to pointers. This function lets you set an association.
2755 * If the object already had an association with that name,
2756 * the old association will be destroyed.
2759 g_object_set_data (GObject *object,
2763 g_return_if_fail (G_IS_OBJECT (object));
2764 g_return_if_fail (key != NULL);
2766 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2770 * g_object_set_data_full:
2771 * @object: #GObject containing the associations
2772 * @key: name of the key
2773 * @data: data to associate with that key
2774 * @destroy: function to call when the association is destroyed
2776 * Like g_object_set_data() except it adds notification
2777 * for when the association is destroyed, either by setting it
2778 * to a different value or when the object is destroyed.
2780 * Note that the @destroy callback is not called if @data is %NULL.
2783 g_object_set_data_full (GObject *object,
2786 GDestroyNotify destroy)
2788 g_return_if_fail (G_IS_OBJECT (object));
2789 g_return_if_fail (key != NULL);
2791 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2792 data ? destroy : (GDestroyNotify) NULL);
2796 * g_object_steal_data:
2797 * @object: #GObject containing the associations
2798 * @key: name of the key
2800 * Remove a specified datum from the object's data associations,
2801 * without invoking the association's destroy handler.
2803 * Returns: the data if found, or %NULL if no such data exists.
2806 g_object_steal_data (GObject *object,
2811 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2812 g_return_val_if_fail (key != NULL, NULL);
2814 quark = g_quark_try_string (key);
2816 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2820 g_value_object_init (GValue *value)
2822 value->data[0].v_pointer = NULL;
2826 g_value_object_free_value (GValue *value)
2828 if (value->data[0].v_pointer)
2829 g_object_unref (value->data[0].v_pointer);
2833 g_value_object_copy_value (const GValue *src_value,
2836 if (src_value->data[0].v_pointer)
2837 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2839 dest_value->data[0].v_pointer = NULL;
2843 g_value_object_transform_value (const GValue *src_value,
2846 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)))
2847 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
2849 dest_value->data[0].v_pointer = NULL;
2853 g_value_object_peek_pointer (const GValue *value)
2855 return value->data[0].v_pointer;
2859 g_value_object_collect_value (GValue *value,
2860 guint n_collect_values,
2861 GTypeCValue *collect_values,
2862 guint collect_flags)
2864 if (collect_values[0].v_pointer)
2866 GObject *object = collect_values[0].v_pointer;
2868 if (object->g_type_instance.g_class == NULL)
2869 return g_strconcat ("invalid unclassed object pointer for value type `",
2870 G_VALUE_TYPE_NAME (value),
2873 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
2874 return g_strconcat ("invalid object type `",
2875 G_OBJECT_TYPE_NAME (object),
2876 "' for value type `",
2877 G_VALUE_TYPE_NAME (value),
2880 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
2881 value->data[0].v_pointer = g_object_ref (object);
2884 value->data[0].v_pointer = NULL;
2890 g_value_object_lcopy_value (const GValue *value,
2891 guint n_collect_values,
2892 GTypeCValue *collect_values,
2893 guint collect_flags)
2895 GObject **object_p = collect_values[0].v_pointer;
2898 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
2900 if (!value->data[0].v_pointer)
2902 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
2903 *object_p = value->data[0].v_pointer;
2905 *object_p = g_object_ref (value->data[0].v_pointer);
2911 * g_value_set_object:
2912 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2913 * @v_object: object value to be set
2915 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
2917 * g_value_set_object() increases the reference count of @v_object
2918 * (the #GValue holds a reference to @v_object). If you do not wish
2919 * to increase the reference count of the object (i.e. you wish to
2920 * pass your current reference to the #GValue because you no longer
2921 * need it), use g_value_take_object() instead.
2923 * It is important that your #GValue holds a reference to @v_object (either its
2924 * own, or one it has taken) to ensure that the object won't be destroyed while
2925 * the #GValue still exists).
2928 g_value_set_object (GValue *value,
2933 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2935 old = value->data[0].v_pointer;
2939 g_return_if_fail (G_IS_OBJECT (v_object));
2940 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
2942 value->data[0].v_pointer = v_object;
2943 g_object_ref (value->data[0].v_pointer);
2946 value->data[0].v_pointer = NULL;
2949 g_object_unref (old);
2953 * g_value_set_object_take_ownership:
2954 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2955 * @v_object: object value to be set
2957 * This is an internal function introduced mainly for C marshallers.
2959 * Deprecated: 2.4: Use g_value_take_object() instead.
2962 g_value_set_object_take_ownership (GValue *value,
2965 g_value_take_object (value, v_object);
2969 * g_value_take_object:
2970 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
2971 * @v_object: object value to be set
2973 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
2974 * and takes over the ownership of the callers reference to @v_object;
2975 * the caller doesn't have to unref it any more (i.e. the reference
2976 * count of the object is not increased).
2978 * If you want the #GValue to hold its own reference to @v_object, use
2979 * g_value_set_object() instead.
2984 g_value_take_object (GValue *value,
2987 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
2989 if (value->data[0].v_pointer)
2991 g_object_unref (value->data[0].v_pointer);
2992 value->data[0].v_pointer = NULL;
2997 g_return_if_fail (G_IS_OBJECT (v_object));
2998 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3000 value->data[0].v_pointer = v_object; /* we take over the reference count */
3005 * g_value_get_object:
3006 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3008 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3010 * Returns: object contents of @value
3013 g_value_get_object (const GValue *value)
3015 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3017 return value->data[0].v_pointer;
3021 * g_value_dup_object:
3022 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3024 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3025 * its reference count.
3027 * Returns: object content of @value, should be unreferenced when no
3031 g_value_dup_object (const GValue *value)
3033 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3035 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3039 * g_signal_connect_object:
3040 * @instance: the instance to connect to.
3041 * @detailed_signal: a string of the form "signal-name::detail".
3042 * @c_handler: the #GCallback to connect.
3043 * @gobject: the object to pass as data to @c_handler.
3044 * @connect_flags: a combination of #GConnnectFlags.
3046 * This is similar to g_signal_connect_data(), but uses a closure which
3047 * ensures that the @gobject stays alive during the call to @c_handler
3048 * by temporarily adding a reference count to @gobject.
3050 * Note that there is a bug in GObject that makes this function
3051 * much less useful than it might seem otherwise. Once @gobject is
3052 * disposed, the callback will no longer be called, but, the signal
3053 * handler is <emphasis>not</emphasis> currently disconnected. If the
3054 * @instance is itself being freed at the same time than this doesn't
3055 * matter, since the signal will automatically be removed, but
3056 * if @instance persists, then the signal handler will leak. You
3057 * should not remove the signal yourself because in a future versions of
3058 * GObject, the handler <emphasis>will</emphasis> automatically
3061 * It's possible to work around this problem in a way that will
3062 * continue to work with future versions of GObject by checking
3063 * that the signal handler is still connected before disconnected it:
3064 * <informalexample><programlisting>
3065 * if (g_signal_handler_is_connected (instance, id))
3066 * g_signal_handler_disconnect (instance, id);
3067 * </programlisting></informalexample>
3069 * Returns: the handler id.
3072 g_signal_connect_object (gpointer instance,
3073 const gchar *detailed_signal,
3074 GCallback c_handler,
3076 GConnectFlags connect_flags)
3078 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3079 g_return_val_if_fail (detailed_signal != NULL, 0);
3080 g_return_val_if_fail (c_handler != NULL, 0);
3086 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3088 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3090 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3093 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3099 GClosure *closures[1]; /* flexible array */
3101 /* don't change this structure without supplying an accessor for
3102 * watched closures, e.g.:
3103 * GSList* g_object_list_watched_closures (GObject *object)
3106 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3107 * carray = g_object_get_data (object, "GObject-closure-array");
3110 * GSList *slist = NULL;
3112 * for (i = 0; i < carray->n_closures; i++)
3113 * slist = g_slist_prepend (slist, carray->closures[i]);
3121 object_remove_closure (gpointer data,
3124 GObject *object = data;
3125 CArray *carray = g_object_get_qdata (object, quark_closure_array);
3128 for (i = 0; i < carray->n_closures; i++)
3129 if (carray->closures[i] == closure)
3131 carray->n_closures--;
3132 if (i < carray->n_closures)
3133 carray->closures[i] = carray->closures[carray->n_closures];
3136 g_assert_not_reached ();
3140 destroy_closure_array (gpointer data)
3142 CArray *carray = data;
3143 GObject *object = carray->object;
3144 guint i, n = carray->n_closures;
3146 for (i = 0; i < n; i++)
3148 GClosure *closure = carray->closures[i];
3150 /* removing object_remove_closure() upfront is probably faster than
3151 * letting it fiddle with quark_closure_array which is empty anyways
3153 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3154 g_closure_invalidate (closure);
3160 * g_object_watch_closure:
3161 * @object: GObject restricting lifetime of @closure
3162 * @closure: GClosure to watch
3164 * This function essentially limits the life time of the @closure to
3165 * the life time of the object. That is, when the object is finalized,
3166 * the @closure is invalidated by calling g_closure_invalidate() on
3167 * it, in order to prevent invocations of the closure with a finalized
3168 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3169 * added as marshal guards to the @closure, to ensure that an extra
3170 * reference count is held on @object during invocation of the
3171 * @closure. Usually, this function will be called on closures that
3172 * use this @object as closure data.
3175 g_object_watch_closure (GObject *object,
3181 g_return_if_fail (G_IS_OBJECT (object));
3182 g_return_if_fail (closure != NULL);
3183 g_return_if_fail (closure->is_invalid == FALSE);
3184 g_return_if_fail (closure->in_marshal == FALSE);
3185 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3187 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3188 g_closure_add_marshal_guards (closure,
3189 object, (GClosureNotify) g_object_ref,
3190 object, (GClosureNotify) g_object_unref);
3191 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3194 carray = g_renew (CArray, NULL, 1);
3195 carray->object = object;
3196 carray->n_closures = 1;
3201 i = carray->n_closures++;
3202 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3204 carray->closures[i] = closure;
3205 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3209 * g_closure_new_object:
3210 * @sizeof_closure: the size of the structure to allocate, must be at least
3211 * <literal>sizeof (GClosure)</literal>
3212 * @object: a #GObject pointer to store in the @data field of the newly
3213 * allocated #GClosure
3215 * A variant of g_closure_new_simple() which stores @object in the
3216 * @data field of the closure and calls g_object_watch_closure() on
3217 * @object and the created closure. This function is mainly useful
3218 * when implementing new types of closures.
3220 * Returns: a newly allocated #GClosure
3223 g_closure_new_object (guint sizeof_closure,
3228 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3229 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3231 closure = g_closure_new_simple (sizeof_closure, object);
3232 g_object_watch_closure (object, closure);
3238 * g_cclosure_new_object:
3239 * @callback_func: the function to invoke
3240 * @object: a #GObject pointer to pass to @callback_func
3242 * A variant of g_cclosure_new() which uses @object as @user_data and
3243 * calls g_object_watch_closure() on @object and the created
3244 * closure. This function is useful when you have a callback closely
3245 * associated with a #GObject, and want the callback to no longer run
3246 * after the object is is freed.
3248 * Returns: a new #GCClosure
3251 g_cclosure_new_object (GCallback callback_func,
3256 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3257 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3258 g_return_val_if_fail (callback_func != NULL, NULL);
3260 closure = g_cclosure_new (callback_func, object, NULL);
3261 g_object_watch_closure (object, closure);
3267 * g_cclosure_new_object_swap:
3268 * @callback_func: the function to invoke
3269 * @object: a #GObject pointer to pass to @callback_func
3271 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3272 * and calls g_object_watch_closure() on @object and the created
3273 * closure. This function is useful when you have a callback closely
3274 * associated with a #GObject, and want the callback to no longer run
3275 * after the object is is freed.
3277 * Returns: a new #GCClosure
3280 g_cclosure_new_object_swap (GCallback callback_func,
3285 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3286 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3287 g_return_val_if_fail (callback_func != NULL, NULL);
3289 closure = g_cclosure_new_swap (callback_func, object, NULL);
3290 g_object_watch_closure (object, closure);
3296 g_object_compat_control (gsize what,
3302 case 1: /* floating base type */
3303 return G_TYPE_INITIALLY_UNOWNED;
3304 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3305 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3307 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3309 *pp = floating_flag_handler;
3316 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3319 g_initially_unowned_init (GInitiallyUnowned *object)
3321 g_object_force_floating (object);
3325 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)