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.
30 #include "gvaluecollector.h"
32 #include "gparamspecs.h"
33 #include "gvaluetypes.h"
34 #include "gobject_trace.h"
36 #include "gobjectnotifyqueue.c"
40 * @short_description: The base object type
41 * @see_also: #GParamSpecObject, g_param_spec_object()
42 * @title: The Base Object Type
44 * GObject is the fundamental type providing the common attributes and
45 * methods for all object types in GTK+, Pango and other libraries
46 * based on GObject. The GObject class provides methods for object
47 * construction and destruction, property access methods, and signal
48 * support. Signals are described in detail in <xref
49 * linkend="gobject-Signals"/>.
51 * <para id="floating-ref">
52 * #GInitiallyUnowned is derived from #GObject. The only difference between
53 * the two is that the initial reference of a #GInitiallyUnowned is flagged
54 * as a <firstterm>floating</firstterm> reference.
55 * This means that it is not specifically claimed to be "owned" by
56 * any code portion. The main motivation for providing floating references is
57 * C convenience. In particular, it allows code to be written as:
59 * container = create_container();
60 * container_add_child (container, create_child());
62 * If <function>container_add_child()</function> will g_object_ref_sink() the
63 * passed in child, no reference of the newly created child is leaked.
64 * Without floating references, <function>container_add_child()</function>
65 * can only g_object_ref() the new child, so to implement this code without
66 * reference leaks, it would have to be written as:
69 * container = create_container();
70 * child = create_child();
71 * container_add_child (container, child);
72 * g_object_unref (child);
74 * The floating reference can be converted into
75 * an ordinary reference by calling g_object_ref_sink().
76 * For already sunken objects (objects that don't have a floating reference
77 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
79 * Since floating references are useful almost exclusively for C convenience,
80 * language bindings that provide automated reference and memory ownership
81 * maintenance (such as smart pointers or garbage collection) therefore don't
82 * need to expose floating references in their API.
85 * Some object implementations may need to save an objects floating state
86 * across certain code portions (an example is #GtkMenu), to achive this, the
87 * following sequence can be used:
90 * // save floating state
91 * gboolean was_floating = g_object_is_floating (object);
92 * g_object_ref_sink (object);
93 * // protected code portion
95 * // restore floating state
97 * g_object_force_floating (object);
98 * g_obejct_unref (object); // release previously acquired reference
104 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
105 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
107 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
108 #define OBJECT_HAS_TOGGLE_REF(object) \
109 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
110 #define OBJECT_FLOATING_FLAG 0x2
112 #define CLASS_HAS_PROPS_FLAG 0x1
113 #define CLASS_HAS_PROPS(class) \
114 ((class)->flags & CLASS_HAS_PROPS_FLAG)
115 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
116 ((class)->constructor != g_object_constructor)
118 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
119 #define CLASS_HAS_DERIVED_CLASS(class) \
120 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
122 /* --- signals --- */
129 /* --- properties --- */
135 /* --- prototypes --- */
136 static void g_object_base_class_init (GObjectClass *class);
137 static void g_object_base_class_finalize (GObjectClass *class);
138 static void g_object_do_class_init (GObjectClass *class);
139 static void g_object_init (GObject *object,
140 GObjectClass *class);
141 static GObject* g_object_constructor (GType type,
142 guint n_construct_properties,
143 GObjectConstructParam *construct_params);
144 static void g_object_real_dispose (GObject *object);
145 static void g_object_finalize (GObject *object);
146 static void g_object_do_set_property (GObject *object,
150 static void g_object_do_get_property (GObject *object,
154 static void g_value_object_init (GValue *value);
155 static void g_value_object_free_value (GValue *value);
156 static void g_value_object_copy_value (const GValue *src_value,
158 static void g_value_object_transform_value (const GValue *src_value,
160 static gpointer g_value_object_peek_pointer (const GValue *value);
161 static gchar* g_value_object_collect_value (GValue *value,
162 guint n_collect_values,
163 GTypeCValue *collect_values,
164 guint collect_flags);
165 static gchar* g_value_object_lcopy_value (const GValue *value,
166 guint n_collect_values,
167 GTypeCValue *collect_values,
168 guint collect_flags);
169 static void g_object_dispatch_properties_changed (GObject *object,
171 GParamSpec **pspecs);
172 static inline void object_get_property (GObject *object,
175 static inline void object_set_property (GObject *object,
178 GObjectNotifyQueue *nqueue);
179 static guint object_floating_flag_handler (GObject *object,
182 static void object_interface_check_properties (gpointer func_data,
186 /* --- variables --- */
187 G_LOCK_DEFINE_STATIC (closure_array_mutex);
188 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
189 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
190 static GQuark quark_closure_array = 0;
191 static GQuark quark_weak_refs = 0;
192 static GQuark quark_toggle_refs = 0;
193 static GParamSpecPool *pspec_pool = NULL;
194 static GObjectNotifyContext property_notify_context = { 0, };
195 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
196 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
197 G_LOCK_DEFINE_STATIC (construction_mutex);
198 static GSList *construction_objects = NULL;
200 /* --- functions --- */
201 #ifdef G_ENABLE_DEBUG
202 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
203 G_LOCK_DEFINE_STATIC (debug_objects);
204 static volatile GObject *g_trap_object_ref = NULL;
205 static guint debug_objects_count = 0;
206 static GHashTable *debug_objects_ht = NULL;
209 debug_objects_foreach (gpointer key,
213 GObject *object = value;
215 g_message ("[%p] stale %s\tref_count=%u",
217 G_OBJECT_TYPE_NAME (object),
222 debug_objects_atexit (void)
226 G_LOCK (debug_objects);
227 g_message ("stale GObjects: %u", debug_objects_count);
228 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
229 G_UNLOCK (debug_objects);
232 #endif /* G_ENABLE_DEBUG */
235 g_object_type_init (void)
237 static gboolean initialized = FALSE;
238 static const GTypeFundamentalInfo finfo = {
239 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
241 static GTypeInfo info = {
242 sizeof (GObjectClass),
243 (GBaseInitFunc) g_object_base_class_init,
244 (GBaseFinalizeFunc) g_object_base_class_finalize,
245 (GClassInitFunc) g_object_do_class_init,
246 NULL /* class_destroy */,
247 NULL /* class_data */,
250 (GInstanceInitFunc) g_object_init,
251 NULL, /* value_table */
253 static const GTypeValueTable value_table = {
254 g_value_object_init, /* value_init */
255 g_value_object_free_value, /* value_free */
256 g_value_object_copy_value, /* value_copy */
257 g_value_object_peek_pointer, /* value_peek_pointer */
258 "p", /* collect_format */
259 g_value_object_collect_value, /* collect_value */
260 "p", /* lcopy_format */
261 g_value_object_lcopy_value, /* lcopy_value */
265 g_return_if_fail (initialized == FALSE);
270 info.value_table = &value_table;
271 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
272 g_assert (type == G_TYPE_OBJECT);
273 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
275 #ifdef G_ENABLE_DEBUG
278 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
279 g_atexit (debug_objects_atexit);
281 #endif /* G_ENABLE_DEBUG */
285 g_object_base_class_init (GObjectClass *class)
287 GObjectClass *pclass = g_type_class_peek_parent (class);
289 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
290 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
293 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
295 /* reset instance specific fields and methods that don't get inherited */
296 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
297 class->get_property = NULL;
298 class->set_property = NULL;
302 g_object_base_class_finalize (GObjectClass *class)
306 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
308 g_slist_free (class->construct_properties);
309 class->construct_properties = NULL;
310 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
311 for (node = list; node; node = node->next)
313 GParamSpec *pspec = node->data;
315 g_param_spec_pool_remove (pspec_pool, pspec);
316 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
317 g_param_spec_unref (pspec);
323 g_object_notify_dispatcher (GObject *object,
327 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
331 g_object_do_class_init (GObjectClass *class)
333 /* read the comment about typedef struct CArray; on why not to change this quark */
334 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
336 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
337 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
338 pspec_pool = g_param_spec_pool_new (TRUE);
339 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
340 property_notify_context.dispatcher = g_object_notify_dispatcher;
342 class->constructor = g_object_constructor;
343 class->set_property = g_object_do_set_property;
344 class->get_property = g_object_do_get_property;
345 class->dispose = g_object_real_dispose;
346 class->finalize = g_object_finalize;
347 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
348 class->notify = NULL;
352 * @gobject: the object which received the signal.
353 * @pspec: the #GParamSpec of the property which changed.
355 * The notify signal is emitted on an object when one of its
356 * properties has been changed. Note that getting this signal
357 * doesn't guarantee that the value of the property has actually
358 * changed, it may also be emitted when the setter for the property
359 * is called to reinstate the previous value.
361 * This signal is typically used to obtain change notification for a
362 * single property, by specifying the property name as a detail in the
363 * g_signal_connect() call, like this:
365 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
366 * G_CALLBACK (gtk_text_view_target_list_notify),
369 * It is important to note that you must use
370 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
371 * detail strings for the notify signal.
373 gobject_signals[NOTIFY] =
374 g_signal_new (g_intern_static_string ("notify"),
375 G_TYPE_FROM_CLASS (class),
376 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
377 G_STRUCT_OFFSET (GObjectClass, notify),
379 g_cclosure_marshal_VOID__PARAM,
383 /* Install a check function that we'll use to verify that classes that
384 * implement an interface implement all properties for that interface
386 g_type_add_interface_check (NULL, object_interface_check_properties);
390 install_property_internal (GType g_type,
394 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
396 g_warning ("When installing property: type `%s' already has a property named `%s'",
397 g_type_name (g_type),
402 g_param_spec_ref (pspec);
403 g_param_spec_sink (pspec);
404 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
405 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
409 * g_object_class_install_property:
410 * @oclass: a #GObjectClass
411 * @property_id: the id for the new property
412 * @pspec: the #GParamSpec for the new property
414 * Installs a new property. This is usually done in the class initializer.
416 * Note that it is possible to redefine a property in a derived class,
417 * by installing a property with the same name. This can be useful at times,
418 * e.g. to change the range of allowed values or the default value.
421 g_object_class_install_property (GObjectClass *class,
425 g_return_if_fail (G_IS_OBJECT_CLASS (class));
426 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
428 if (CLASS_HAS_DERIVED_CLASS (class))
429 g_error ("Attempt to add property %s::%s to class after it was derived",
430 G_OBJECT_CLASS_NAME (class), pspec->name);
432 class->flags |= CLASS_HAS_PROPS_FLAG;
434 if (pspec->flags & G_PARAM_WRITABLE)
435 g_return_if_fail (class->set_property != NULL);
436 if (pspec->flags & G_PARAM_READABLE)
437 g_return_if_fail (class->get_property != NULL);
438 g_return_if_fail (property_id > 0);
439 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
440 if (pspec->flags & G_PARAM_CONSTRUCT)
441 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
442 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
443 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
445 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
447 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
448 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
450 /* for property overrides of construct properties, we have to get rid
451 * of the overidden inherited construct property
453 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
454 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
455 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
459 * g_object_class_install_properties:
460 * @oclass: a #GObjectClass
461 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
462 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
463 * defining the new properties
465 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
466 * usually done in the class initializer.
468 * The property id of each property is the index of each #GParamSpec in
471 * The property id of 0 is treated specially by #GObject and it should not
472 * be used to store a #GParamSpec.
474 * This function should be used if you plan to use a static array of
475 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
476 * class initialization:
480 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
483 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
486 * my_object_class_init (MyObjectClass *klass)
488 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
490 * obj_properties[PROP_FOO] =
491 * g_param_spec_int ("foo", "Foo", "Foo",
494 * G_PARAM_READWRITE);
496 * obj_properties[PROP_BAR] =
497 * g_param_spec_string ("bar", "Bar", "Bar",
499 * G_PARAM_READWRITE);
501 * gobject_class->set_property = my_object_set_property;
502 * gobject_class->get_property = my_object_get_property;
503 * g_object_class_install_properties (gobject_class,
509 * allows calling g_object_notify_by_pspec() to notify of property changes:
513 * my_object_set_foo (MyObject *self, gint foo)
515 * if (self->foo != foo)
518 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
526 g_object_class_install_properties (GObjectClass *oclass,
530 GType oclass_type, parent_type;
533 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
534 g_return_if_fail (n_pspecs > 1);
535 g_return_if_fail (pspecs[0] == NULL);
537 if (CLASS_HAS_DERIVED_CLASS (oclass))
538 g_error ("Attempt to add properties to %s after it was derived",
539 G_OBJECT_CLASS_NAME (oclass));
541 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
542 parent_type = g_type_parent (oclass_type);
544 /* we skip the first element of the array as it would have a 0 prop_id */
545 for (i = 1; i < n_pspecs; i++)
547 GParamSpec *pspec = pspecs[i];
549 g_return_if_fail (pspec != NULL);
551 if (pspec->flags & G_PARAM_WRITABLE)
552 g_return_if_fail (oclass->set_property != NULL);
553 if (pspec->flags & G_PARAM_READABLE)
554 g_return_if_fail (oclass->get_property != NULL);
555 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
556 if (pspec->flags & G_PARAM_CONSTRUCT)
557 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
558 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
559 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
561 oclass->flags |= CLASS_HAS_PROPS_FLAG;
562 install_property_internal (oclass_type, i, pspec);
564 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
565 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
567 /* for property overrides of construct properties, we have to get rid
568 * of the overidden inherited construct property
570 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
571 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
577 * g_object_interface_install_property:
578 * @g_iface: any interface vtable for the interface, or the default
579 * vtable for the interface.
580 * @pspec: the #GParamSpec for the new property
582 * Add a property to an interface; this is only useful for interfaces
583 * that are added to GObject-derived types. Adding a property to an
584 * interface forces all objects classes with that interface to have a
585 * compatible property. The compatible property could be a newly
586 * created #GParamSpec, but normally
587 * g_object_class_override_property() will be used so that the object
588 * class only needs to provide an implementation and inherits the
589 * property description, default value, bounds, and so forth from the
590 * interface property.
592 * This function is meant to be called from the interface's default
593 * vtable initialization function (the @class_init member of
594 * #GTypeInfo.) It must not be called after after @class_init has
595 * been called for any object types implementing this interface.
600 g_object_interface_install_property (gpointer g_iface,
603 GTypeInterface *iface_class = g_iface;
605 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
606 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
607 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
608 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
610 install_property_internal (iface_class->g_type, 0, pspec);
614 * g_object_class_find_property:
615 * @oclass: a #GObjectClass
616 * @property_name: the name of the property to look up
618 * Looks up the #GParamSpec for a property of a class.
620 * Returns: the #GParamSpec for the property, or %NULL if the class
621 * doesn't have a property of that name
624 g_object_class_find_property (GObjectClass *class,
625 const gchar *property_name)
628 GParamSpec *redirect;
630 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
631 g_return_val_if_fail (property_name != NULL, NULL);
633 pspec = g_param_spec_pool_lookup (pspec_pool,
635 G_OBJECT_CLASS_TYPE (class),
639 redirect = g_param_spec_get_redirect_target (pspec);
650 * g_object_interface_find_property:
651 * @g_iface: any interface vtable for the interface, or the default
652 * vtable for the interface
653 * @property_name: name of a property to lookup.
655 * Find the #GParamSpec with the given name for an
656 * interface. Generally, the interface vtable passed in as @g_iface
657 * will be the default vtable from g_type_default_interface_ref(), or,
658 * if you know the interface has already been loaded,
659 * g_type_default_interface_peek().
663 * Returns: the #GParamSpec for the property of the interface with the
664 * name @property_name, or %NULL if no such property exists.
667 g_object_interface_find_property (gpointer g_iface,
668 const gchar *property_name)
670 GTypeInterface *iface_class = g_iface;
672 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
673 g_return_val_if_fail (property_name != NULL, NULL);
675 return g_param_spec_pool_lookup (pspec_pool,
682 * g_object_class_override_property:
683 * @oclass: a #GObjectClass
684 * @property_id: the new property ID
685 * @name: the name of a property registered in a parent class or
686 * in an interface of this class.
688 * Registers @property_id as referring to a property with the
689 * name @name in a parent class or in an interface implemented
690 * by @oclass. This allows this class to <firstterm>override</firstterm>
691 * a property implementation in a parent class or to provide
692 * the implementation of a property from an interface.
695 * Internally, overriding is implemented by creating a property of type
696 * #GParamSpecOverride; generally operations that query the properties of
697 * the object class, such as g_object_class_find_property() or
698 * g_object_class_list_properties() will return the overridden
699 * property. However, in one case, the @construct_properties argument of
700 * the @constructor virtual function, the #GParamSpecOverride is passed
701 * instead, so that the @param_id field of the #GParamSpec will be
702 * correct. For virtually all uses, this makes no difference. If you
703 * need to get the overridden property, you can call
704 * g_param_spec_get_redirect_target().
710 g_object_class_override_property (GObjectClass *oclass,
714 GParamSpec *overridden = NULL;
718 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
719 g_return_if_fail (property_id > 0);
720 g_return_if_fail (name != NULL);
722 /* Find the overridden property; first check parent types
724 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
725 if (parent_type != G_TYPE_NONE)
726 overridden = g_param_spec_pool_lookup (pspec_pool,
735 /* Now check interfaces
737 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
738 while (n_ifaces-- && !overridden)
740 overridden = g_param_spec_pool_lookup (pspec_pool,
751 g_warning ("%s: Can't find property to override for '%s::%s'",
752 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
756 new = g_param_spec_override (name, overridden);
757 g_object_class_install_property (oclass, property_id, new);
761 * g_object_class_list_properties:
762 * @oclass: a #GObjectClass
763 * @n_properties: return location for the length of the returned array
765 * Get an array of #GParamSpec* for all properties of a class.
767 * Returns: (array length=n_properties) (transfer full): an array of
768 * #GParamSpec* which should be freed after use
770 GParamSpec** /* free result */
771 g_object_class_list_properties (GObjectClass *class,
772 guint *n_properties_p)
777 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
779 pspecs = g_param_spec_pool_list (pspec_pool,
780 G_OBJECT_CLASS_TYPE (class),
789 * g_object_interface_list_properties:
790 * @g_iface: any interface vtable for the interface, or the default
791 * vtable for the interface
792 * @n_properties_p: location to store number of properties returned.
794 * Lists the properties of an interface.Generally, the interface
795 * vtable passed in as @g_iface will be the default vtable from
796 * g_type_default_interface_ref(), or, if you know the interface has
797 * already been loaded, g_type_default_interface_peek().
801 * Returns: a pointer to an array of pointers to #GParamSpec
802 * structures. The paramspecs are owned by GLib, but the
803 * array should be freed with g_free() when you are done with
807 g_object_interface_list_properties (gpointer g_iface,
808 guint *n_properties_p)
810 GTypeInterface *iface_class = g_iface;
814 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
816 pspecs = g_param_spec_pool_list (pspec_pool,
826 g_object_init (GObject *object,
829 object->ref_count = 1;
830 g_datalist_init (&object->qdata);
832 if (CLASS_HAS_PROPS (class))
834 /* freeze object's notification queue, g_object_newv() preserves pairedness */
835 g_object_notify_queue_freeze (object, &property_notify_context);
838 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
840 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
841 G_LOCK (construction_mutex);
842 construction_objects = g_slist_prepend (construction_objects, object);
843 G_UNLOCK (construction_mutex);
846 #ifdef G_ENABLE_DEBUG
849 G_LOCK (debug_objects);
850 debug_objects_count++;
851 g_hash_table_insert (debug_objects_ht, object, object);
852 G_UNLOCK (debug_objects);
854 #endif /* G_ENABLE_DEBUG */
858 g_object_do_set_property (GObject *object,
866 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
872 g_object_do_get_property (GObject *object,
880 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
886 g_object_real_dispose (GObject *object)
888 g_signal_handlers_destroy (object);
889 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
890 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
894 g_object_finalize (GObject *object)
896 g_datalist_clear (&object->qdata);
898 #ifdef G_ENABLE_DEBUG
901 G_LOCK (debug_objects);
902 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
903 g_hash_table_remove (debug_objects_ht, object);
904 debug_objects_count--;
905 G_UNLOCK (debug_objects);
907 #endif /* G_ENABLE_DEBUG */
912 g_object_dispatch_properties_changed (GObject *object,
918 for (i = 0; i < n_pspecs; i++)
919 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
923 * g_object_run_dispose:
924 * @object: a #GObject
926 * Releases all references to other objects. This can be used to break
929 * This functions should only be called from object system implementations.
932 g_object_run_dispose (GObject *object)
934 g_return_if_fail (G_IS_OBJECT (object));
935 g_return_if_fail (object->ref_count > 0);
937 g_object_ref (object);
938 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
939 G_OBJECT_GET_CLASS (object)->dispose (object);
940 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
941 g_object_unref (object);
945 * g_object_freeze_notify:
946 * @object: a #GObject
948 * Increases the freeze count on @object. If the freeze count is
949 * non-zero, the emission of "notify" signals on @object is
950 * stopped. The signals are queued until the freeze count is decreased
953 * This is necessary for accessors that modify multiple properties to prevent
954 * premature notification while the object is still being modified.
957 g_object_freeze_notify (GObject *object)
959 g_return_if_fail (G_IS_OBJECT (object));
961 if (g_atomic_int_get (&object->ref_count) == 0)
964 g_object_ref (object);
965 g_object_notify_queue_freeze (object, &property_notify_context);
966 g_object_unref (object);
970 g_object_notify_by_spec_internal (GObject *object,
973 GObjectNotifyQueue *nqueue;
975 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
976 g_object_notify_queue_add (object, nqueue, pspec);
977 g_object_notify_queue_thaw (object, nqueue);
982 * @object: a #GObject
983 * @property_name: the name of a property installed on the class of @object.
985 * Emits a "notify" signal for the property @property_name on @object.
987 * When possible, eg. when signaling a property change from within the class
988 * that registered the property, you should use g_object_notify_by_pspec()
992 g_object_notify (GObject *object,
993 const gchar *property_name)
997 g_return_if_fail (G_IS_OBJECT (object));
998 g_return_if_fail (property_name != NULL);
999 if (g_atomic_int_get (&object->ref_count) == 0)
1002 g_object_ref (object);
1003 /* We don't need to get the redirect target
1004 * (by, e.g. calling g_object_class_find_property())
1005 * because g_object_notify_queue_add() does that
1007 pspec = g_param_spec_pool_lookup (pspec_pool,
1009 G_OBJECT_TYPE (object),
1013 g_warning ("%s: object class `%s' has no property named `%s'",
1015 G_OBJECT_TYPE_NAME (object),
1018 g_object_notify_by_spec_internal (object, pspec);
1019 g_object_unref (object);
1023 * g_object_notify_by_pspec:
1024 * @object: a #GObject
1025 * @pspec: the #GParamSpec of a property installed on the class of @object.
1027 * Emits a "notify" signal for the property specified by @pspec on @object.
1029 * This function omits the property name lookup, hence it is faster than
1030 * g_object_notify().
1032 * One way to avoid using g_object_notify() from within the
1033 * class that registered the properties, and using g_object_notify_by_pspec()
1034 * instead, is to store the GParamSpec used with
1035 * g_object_class_install_property() inside a static array, e.g.:
1045 * static GParamSpec *properties[PROP_LAST];
1048 * my_object_class_init (MyObjectClass *klass)
1050 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1053 * G_PARAM_READWRITE);
1054 * g_object_class_install_property (gobject_class,
1056 * properties[PROP_FOO]);
1060 * and then notify a change on the "foo" property with:
1063 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1069 g_object_notify_by_pspec (GObject *object,
1073 g_return_if_fail (G_IS_OBJECT (object));
1074 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1076 g_object_ref (object);
1077 g_object_notify_by_spec_internal (object, pspec);
1078 g_object_unref (object);
1082 * g_object_thaw_notify:
1083 * @object: a #GObject
1085 * Reverts the effect of a previous call to
1086 * g_object_freeze_notify(). The freeze count is decreased on @object
1087 * and when it reaches zero, all queued "notify" signals are emitted.
1089 * It is an error to call this function when the freeze count is zero.
1092 g_object_thaw_notify (GObject *object)
1094 GObjectNotifyQueue *nqueue;
1096 g_return_if_fail (G_IS_OBJECT (object));
1097 if (g_atomic_int_get (&object->ref_count) == 0)
1100 g_object_ref (object);
1102 /* FIXME: Freezing is the only way to get at the notify queue.
1103 * So we freeze once and then thaw twice.
1105 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1106 g_object_notify_queue_thaw (object, nqueue);
1107 g_object_notify_queue_thaw (object, nqueue);
1109 g_object_unref (object);
1113 object_get_property (GObject *object,
1117 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1118 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1119 GParamSpec *redirect;
1121 redirect = g_param_spec_get_redirect_target (pspec);
1125 class->get_property (object, param_id, value, pspec);
1129 object_set_property (GObject *object,
1131 const GValue *value,
1132 GObjectNotifyQueue *nqueue)
1134 GValue tmp_value = { 0, };
1135 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1136 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1137 GParamSpec *redirect;
1138 static gchar* enable_diagnostic = NULL;
1140 redirect = g_param_spec_get_redirect_target (pspec);
1144 if (G_UNLIKELY (!enable_diagnostic))
1146 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1147 if (!enable_diagnostic)
1148 enable_diagnostic = "0";
1151 if (enable_diagnostic[0] == '1')
1153 if (pspec->flags & G_PARAM_DEPRECATED)
1154 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1155 "anymore. It will be removed in a future version.",
1156 G_OBJECT_TYPE_NAME (object), pspec->name);
1159 /* provide a copy to work from, convert (if necessary) and validate */
1160 g_value_init (&tmp_value, pspec->value_type);
1161 if (!g_value_transform (value, &tmp_value))
1162 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1164 g_type_name (pspec->value_type),
1165 G_VALUE_TYPE_NAME (value));
1166 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1168 gchar *contents = g_strdup_value_contents (value);
1170 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1172 G_VALUE_TYPE_NAME (value),
1174 g_type_name (pspec->value_type));
1179 class->set_property (object, param_id, &tmp_value, pspec);
1180 g_object_notify_queue_add (object, nqueue, pspec);
1182 g_value_unset (&tmp_value);
1186 object_interface_check_properties (gpointer func_data,
1189 GTypeInterface *iface_class = g_iface;
1190 GObjectClass *class = g_type_class_peek (iface_class->g_instance_type);
1191 GType iface_type = iface_class->g_type;
1192 GParamSpec **pspecs;
1195 if (!G_IS_OBJECT_CLASS (class))
1198 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1202 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1204 G_OBJECT_CLASS_TYPE (class),
1209 g_critical ("Object class %s doesn't implement property "
1210 "'%s' from interface '%s'",
1211 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1213 g_type_name (iface_type));
1218 /* The implementation paramspec must have a less restrictive
1219 * type than the interface parameter spec for set() and a
1220 * more restrictive type for get(). We just require equality,
1221 * rather than doing something more complicated checking
1222 * the READABLE and WRITABLE flags. We also simplify here
1223 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1226 !g_type_is_a (pspecs[n]->value_type,
1227 class_pspec->value_type))
1229 g_critical ("Property '%s' on class '%s' has type '%s' "
1230 "which is different from the type '%s', "
1231 "of the property on interface '%s'\n",
1233 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1234 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1235 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1236 g_type_name (iface_type));
1239 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1241 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1242 * READABLE and WRITABLE remove restrictions. The implementation
1243 * paramspec must have less restrictive flags.
1246 (!SUBSET (class_pspec->flags,
1248 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1249 !SUBSET (pspecs[n]->flags,
1251 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1253 g_critical ("Flags for property '%s' on class '%s' "
1254 "are not compatible with the property on"
1257 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1258 g_type_name (iface_type));
1267 g_object_get_type (void)
1269 return G_TYPE_OBJECT;
1274 * @object_type: the type id of the #GObject subtype to instantiate
1275 * @first_property_name: the name of the first property
1276 * @...: the value of the first property, followed optionally by more
1277 * name/value pairs, followed by %NULL
1279 * Creates a new instance of a #GObject subtype and sets its properties.
1281 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1282 * which are not explicitly specified are set to their default values.
1284 * Returns: a new instance of @object_type
1287 g_object_new (GType object_type,
1288 const gchar *first_property_name,
1294 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1296 /* short circuit for calls supplying no properties */
1297 if (!first_property_name)
1298 return g_object_newv (object_type, 0, NULL);
1300 va_start (var_args, first_property_name);
1301 object = g_object_new_valist (object_type, first_property_name, var_args);
1308 slist_maybe_remove (GSList **slist,
1311 GSList *last = NULL, *node = *slist;
1314 if (node->data == data)
1317 last->next = node->next;
1319 *slist = node->next;
1320 g_slist_free_1 (node);
1329 static inline gboolean
1330 object_in_construction_list (GObject *object)
1332 gboolean in_construction;
1333 G_LOCK (construction_mutex);
1334 in_construction = g_slist_find (construction_objects, object) != NULL;
1335 G_UNLOCK (construction_mutex);
1336 return in_construction;
1341 * @object_type: the type id of the #GObject subtype to instantiate
1342 * @n_parameters: the length of the @parameters array
1343 * @parameters: an array of #GParameter
1345 * Creates a new instance of a #GObject subtype and sets its properties.
1347 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1348 * which are not explicitly specified are set to their default values.
1350 * Returns: a new instance of @object_type
1353 g_object_newv (GType object_type,
1355 GParameter *parameters)
1357 GObjectConstructParam *cparams = NULL, *oparams;
1358 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1360 GObjectClass *class, *unref_class = NULL;
1362 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1364 GList *clist = NULL;
1365 gboolean newly_constructed;
1368 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1370 class = g_type_class_peek_static (object_type);
1372 class = unref_class = g_type_class_ref (object_type);
1373 for (slist = class->construct_properties; slist; slist = slist->next)
1375 clist = g_list_prepend (clist, slist->data);
1376 n_total_cparams += 1;
1379 if (n_parameters == 0 && n_total_cparams == 0)
1381 /* This is a simple object with no construct properties, and
1382 * no properties are being set, so short circuit the parameter
1383 * handling. This speeds up simple object construction.
1386 object = class->constructor (object_type, 0, NULL);
1387 goto did_construction;
1390 /* collect parameters, sort into construction and normal ones */
1391 oparams = g_new (GObjectConstructParam, n_parameters);
1392 cparams = g_new (GObjectConstructParam, n_total_cparams);
1393 for (i = 0; i < n_parameters; i++)
1395 GValue *value = ¶meters[i].value;
1396 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1402 g_warning ("%s: object class `%s' has no property named `%s'",
1404 g_type_name (object_type),
1405 parameters[i].name);
1408 if (!(pspec->flags & G_PARAM_WRITABLE))
1410 g_warning ("%s: property `%s' of object class `%s' is not writable",
1413 g_type_name (object_type));
1416 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1418 GList *list = g_list_find (clist, pspec);
1422 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1423 G_STRFUNC, pspec->name, g_type_name (object_type));
1426 cparams[n_cparams].pspec = pspec;
1427 cparams[n_cparams].value = value;
1432 list->prev->next = list->next;
1434 list->next->prev = list->prev;
1435 g_list_free_1 (list);
1439 oparams[n_oparams].pspec = pspec;
1440 oparams[n_oparams].value = value;
1445 /* set remaining construction properties to default values */
1446 n_cvalues = n_total_cparams - n_cparams;
1447 cvalues = g_new (GValue, n_cvalues);
1450 GList *tmp = clist->next;
1451 GParamSpec *pspec = clist->data;
1452 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1455 g_value_init (value, pspec->value_type);
1456 g_param_value_set_default (pspec, value);
1458 cparams[n_cparams].pspec = pspec;
1459 cparams[n_cparams].value = value;
1462 g_list_free_1 (clist);
1466 /* construct object from construction parameters */
1467 object = class->constructor (object_type, n_total_cparams, cparams);
1468 /* free construction values */
1471 g_value_unset (cvalues + n_cvalues);
1475 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1477 /* adjust freeze_count according to g_object_init() and remaining properties */
1478 G_LOCK (construction_mutex);
1479 newly_constructed = slist_maybe_remove (&construction_objects, object);
1480 G_UNLOCK (construction_mutex);
1483 newly_constructed = TRUE;
1485 if (CLASS_HAS_PROPS (class))
1487 if (newly_constructed || n_oparams)
1488 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1489 if (newly_constructed)
1490 g_object_notify_queue_thaw (object, nqueue);
1493 /* run 'constructed' handler if there is one */
1494 if (newly_constructed && class->constructed)
1495 class->constructed (object);
1497 /* set remaining properties */
1498 for (i = 0; i < n_oparams; i++)
1499 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1502 if (CLASS_HAS_PROPS (class))
1504 /* release our own freeze count and handle notifications */
1505 if (newly_constructed || n_oparams)
1506 g_object_notify_queue_thaw (object, nqueue);
1510 g_type_class_unref (unref_class);
1516 * g_object_new_valist:
1517 * @object_type: the type id of the #GObject subtype to instantiate
1518 * @first_property_name: the name of the first property
1519 * @var_args: the value of the first property, followed optionally by more
1520 * name/value pairs, followed by %NULL
1522 * Creates a new instance of a #GObject subtype and sets its properties.
1524 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1525 * which are not explicitly specified are set to their default values.
1527 * Returns: a new instance of @object_type
1530 g_object_new_valist (GType object_type,
1531 const gchar *first_property_name,
1534 GObjectClass *class;
1538 guint n_params = 0, n_alloced_params = 16;
1540 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1542 if (!first_property_name)
1543 return g_object_newv (object_type, 0, NULL);
1545 class = g_type_class_ref (object_type);
1547 params = g_new0 (GParameter, n_alloced_params);
1548 name = first_property_name;
1551 gchar *error = NULL;
1552 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1558 g_warning ("%s: object class `%s' has no property named `%s'",
1560 g_type_name (object_type),
1564 if (n_params >= n_alloced_params)
1566 n_alloced_params += 16;
1567 params = g_renew (GParameter, params, n_alloced_params);
1568 memset (params + n_params, 0, 16 * (sizeof *params));
1570 params[n_params].name = name;
1571 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1572 var_args, 0, &error);
1575 g_warning ("%s: %s", G_STRFUNC, error);
1577 g_value_unset (¶ms[n_params].value);
1581 name = va_arg (var_args, gchar*);
1584 object = g_object_newv (object_type, n_params, params);
1587 g_value_unset (¶ms[n_params].value);
1590 g_type_class_unref (class);
1596 g_object_constructor (GType type,
1597 guint n_construct_properties,
1598 GObjectConstructParam *construct_params)
1603 object = (GObject*) g_type_create_instance (type);
1605 /* set construction parameters */
1606 if (n_construct_properties)
1608 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1610 /* set construct properties */
1611 while (n_construct_properties--)
1613 GValue *value = construct_params->value;
1614 GParamSpec *pspec = construct_params->pspec;
1617 object_set_property (object, pspec, value, nqueue);
1619 g_object_notify_queue_thaw (object, nqueue);
1620 /* the notification queue is still frozen from g_object_init(), so
1621 * we don't need to handle it here, g_object_newv() takes
1630 * g_object_set_valist:
1631 * @object: a #GObject
1632 * @first_property_name: name of the first property to set
1633 * @var_args: value for the first property, followed optionally by more
1634 * name/value pairs, followed by %NULL
1636 * Sets properties on an object.
1639 g_object_set_valist (GObject *object,
1640 const gchar *first_property_name,
1643 GObjectNotifyQueue *nqueue;
1646 g_return_if_fail (G_IS_OBJECT (object));
1648 g_object_ref (object);
1649 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1651 name = first_property_name;
1654 GValue value = { 0, };
1656 gchar *error = NULL;
1658 pspec = g_param_spec_pool_lookup (pspec_pool,
1660 G_OBJECT_TYPE (object),
1664 g_warning ("%s: object class `%s' has no property named `%s'",
1666 G_OBJECT_TYPE_NAME (object),
1670 if (!(pspec->flags & G_PARAM_WRITABLE))
1672 g_warning ("%s: property `%s' of object class `%s' is not writable",
1675 G_OBJECT_TYPE_NAME (object));
1678 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1680 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1681 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1685 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1689 g_warning ("%s: %s", G_STRFUNC, error);
1691 g_value_unset (&value);
1695 object_set_property (object, pspec, &value, nqueue);
1696 g_value_unset (&value);
1698 name = va_arg (var_args, gchar*);
1701 g_object_notify_queue_thaw (object, nqueue);
1702 g_object_unref (object);
1706 * g_object_get_valist:
1707 * @object: a #GObject
1708 * @first_property_name: name of the first property to get
1709 * @var_args: return location for the first property, followed optionally by more
1710 * name/return location pairs, followed by %NULL
1712 * Gets properties of an object.
1714 * In general, a copy is made of the property contents and the caller
1715 * is responsible for freeing the memory in the appropriate manner for
1716 * the type, for instance by calling g_free() or g_object_unref().
1718 * See g_object_get().
1721 g_object_get_valist (GObject *object,
1722 const gchar *first_property_name,
1727 g_return_if_fail (G_IS_OBJECT (object));
1729 g_object_ref (object);
1731 name = first_property_name;
1735 GValue value = { 0, };
1739 pspec = g_param_spec_pool_lookup (pspec_pool,
1741 G_OBJECT_TYPE (object),
1745 g_warning ("%s: object class `%s' has no property named `%s'",
1747 G_OBJECT_TYPE_NAME (object),
1751 if (!(pspec->flags & G_PARAM_READABLE))
1753 g_warning ("%s: property `%s' of object class `%s' is not readable",
1756 G_OBJECT_TYPE_NAME (object));
1760 g_value_init (&value, pspec->value_type);
1762 object_get_property (object, pspec, &value);
1764 G_VALUE_LCOPY (&value, var_args, 0, &error);
1767 g_warning ("%s: %s", G_STRFUNC, error);
1769 g_value_unset (&value);
1773 g_value_unset (&value);
1775 name = va_arg (var_args, gchar*);
1778 g_object_unref (object);
1783 * @object: a #GObject
1784 * @first_property_name: name of the first property to set
1785 * @...: value for the first property, followed optionally by more
1786 * name/value pairs, followed by %NULL
1788 * Sets properties on an object.
1791 g_object_set (gpointer _object,
1792 const gchar *first_property_name,
1795 GObject *object = _object;
1798 g_return_if_fail (G_IS_OBJECT (object));
1800 va_start (var_args, first_property_name);
1801 g_object_set_valist (object, first_property_name, var_args);
1807 * @object: a #GObject
1808 * @first_property_name: name of the first property to get
1809 * @...: return location for the first property, followed optionally by more
1810 * name/return location pairs, followed by %NULL
1812 * Gets properties of an object.
1814 * In general, a copy is made of the property contents and the caller
1815 * is responsible for freeing the memory in the appropriate manner for
1816 * the type, for instance by calling g_free() or g_object_unref().
1819 * <title>Using g_object_get(<!-- -->)</title>
1820 * An example of using g_object_get() to get the contents
1821 * of three properties - one of type #G_TYPE_INT,
1822 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1828 * g_object_get (my_object,
1829 * "int-property", &intval,
1830 * "str-property", &strval,
1831 * "obj-property", &objval,
1834 * // Do something with intval, strval, objval
1837 * g_object_unref (objval);
1842 g_object_get (gpointer _object,
1843 const gchar *first_property_name,
1846 GObject *object = _object;
1849 g_return_if_fail (G_IS_OBJECT (object));
1851 va_start (var_args, first_property_name);
1852 g_object_get_valist (object, first_property_name, var_args);
1857 * g_object_set_property:
1858 * @object: a #GObject
1859 * @property_name: the name of the property to set
1862 * Sets a property on an object.
1865 g_object_set_property (GObject *object,
1866 const gchar *property_name,
1867 const GValue *value)
1869 GObjectNotifyQueue *nqueue;
1872 g_return_if_fail (G_IS_OBJECT (object));
1873 g_return_if_fail (property_name != NULL);
1874 g_return_if_fail (G_IS_VALUE (value));
1876 g_object_ref (object);
1877 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1879 pspec = g_param_spec_pool_lookup (pspec_pool,
1881 G_OBJECT_TYPE (object),
1884 g_warning ("%s: object class `%s' has no property named `%s'",
1886 G_OBJECT_TYPE_NAME (object),
1888 else if (!(pspec->flags & G_PARAM_WRITABLE))
1889 g_warning ("%s: property `%s' of object class `%s' is not writable",
1892 G_OBJECT_TYPE_NAME (object));
1893 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1894 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1895 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1897 object_set_property (object, pspec, value, nqueue);
1899 g_object_notify_queue_thaw (object, nqueue);
1900 g_object_unref (object);
1904 * g_object_get_property:
1905 * @object: a #GObject
1906 * @property_name: the name of the property to get
1907 * @value: return location for the property value
1909 * Gets a property of an object.
1911 * In general, a copy is made of the property contents and the caller is
1912 * responsible for freeing the memory by calling g_value_unset().
1914 * Note that g_object_get_property() is really intended for language
1915 * bindings, g_object_get() is much more convenient for C programming.
1918 g_object_get_property (GObject *object,
1919 const gchar *property_name,
1924 g_return_if_fail (G_IS_OBJECT (object));
1925 g_return_if_fail (property_name != NULL);
1926 g_return_if_fail (G_IS_VALUE (value));
1928 g_object_ref (object);
1930 pspec = g_param_spec_pool_lookup (pspec_pool,
1932 G_OBJECT_TYPE (object),
1935 g_warning ("%s: object class `%s' has no property named `%s'",
1937 G_OBJECT_TYPE_NAME (object),
1939 else if (!(pspec->flags & G_PARAM_READABLE))
1940 g_warning ("%s: property `%s' of object class `%s' is not readable",
1943 G_OBJECT_TYPE_NAME (object));
1946 GValue *prop_value, tmp_value = { 0, };
1948 /* auto-conversion of the callers value type
1950 if (G_VALUE_TYPE (value) == pspec->value_type)
1952 g_value_reset (value);
1955 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1957 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1958 G_STRFUNC, pspec->name,
1959 g_type_name (pspec->value_type),
1960 G_VALUE_TYPE_NAME (value));
1961 g_object_unref (object);
1966 g_value_init (&tmp_value, pspec->value_type);
1967 prop_value = &tmp_value;
1969 object_get_property (object, pspec, prop_value);
1970 if (prop_value != value)
1972 g_value_transform (prop_value, value);
1973 g_value_unset (&tmp_value);
1977 g_object_unref (object);
1982 * @object: a #GObject
1983 * @signal_spec: the spec for the first signal
1984 * @...: #GCallback for the first signal, followed by data for the
1985 * first signal, followed optionally by more signal
1986 * spec/callback/data triples, followed by %NULL
1988 * A convenience function to connect multiple signals at once.
1990 * The signal specs expected by this function have the form
1991 * "modifier::signal_name", where modifier can be one of the following:
1994 * <term>signal</term>
1996 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
1997 * </para></listitem>
2000 * <term>object_signal</term>
2001 * <term>object-signal</term>
2003 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2004 * </para></listitem>
2007 * <term>swapped_signal</term>
2008 * <term>swapped-signal</term>
2010 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2011 * </para></listitem>
2014 * <term>swapped_object_signal</term>
2015 * <term>swapped-object-signal</term>
2017 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2018 * </para></listitem>
2021 * <term>signal_after</term>
2022 * <term>signal-after</term>
2024 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2025 * </para></listitem>
2028 * <term>object_signal_after</term>
2029 * <term>object-signal-after</term>
2031 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2032 * </para></listitem>
2035 * <term>swapped_signal_after</term>
2036 * <term>swapped-signal-after</term>
2038 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2039 * </para></listitem>
2042 * <term>swapped_object_signal_after</term>
2043 * <term>swapped-object-signal-after</term>
2045 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2046 * </para></listitem>
2051 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2052 * "type", GTK_WINDOW_POPUP,
2055 * "signal::event", gtk_menu_window_event, menu,
2056 * "signal::size_request", gtk_menu_window_size_request, menu,
2057 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2064 g_object_connect (gpointer _object,
2065 const gchar *signal_spec,
2068 GObject *object = _object;
2071 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2072 g_return_val_if_fail (object->ref_count > 0, object);
2074 va_start (var_args, signal_spec);
2077 GCallback callback = va_arg (var_args, GCallback);
2078 gpointer data = va_arg (var_args, gpointer);
2081 if (strncmp (signal_spec, "signal::", 8) == 0)
2082 sid = g_signal_connect_data (object, signal_spec + 8,
2083 callback, data, NULL,
2085 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2086 strncmp (signal_spec, "object-signal::", 15) == 0)
2087 sid = g_signal_connect_object (object, signal_spec + 15,
2090 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2091 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2092 sid = g_signal_connect_data (object, signal_spec + 16,
2093 callback, data, NULL,
2095 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2096 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2097 sid = g_signal_connect_object (object, signal_spec + 23,
2100 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2101 strncmp (signal_spec, "signal-after::", 14) == 0)
2102 sid = g_signal_connect_data (object, signal_spec + 14,
2103 callback, data, NULL,
2105 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2106 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2107 sid = g_signal_connect_object (object, signal_spec + 21,
2110 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2111 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2112 sid = g_signal_connect_data (object, signal_spec + 22,
2113 callback, data, NULL,
2114 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2115 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2116 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2117 sid = g_signal_connect_object (object, signal_spec + 29,
2119 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2122 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2125 signal_spec = va_arg (var_args, gchar*);
2133 * g_object_disconnect:
2134 * @object: a #GObject
2135 * @signal_spec: the spec for the first signal
2136 * @...: #GCallback for the first signal, followed by data for the first signal,
2137 * followed optionally by more signal spec/callback/data triples,
2140 * A convenience function to disconnect multiple signals at once.
2142 * The signal specs expected by this function have the form
2143 * "any_signal", which means to disconnect any signal with matching
2144 * callback and data, or "any_signal::signal_name", which only
2145 * disconnects the signal named "signal_name".
2148 g_object_disconnect (gpointer _object,
2149 const gchar *signal_spec,
2152 GObject *object = _object;
2155 g_return_if_fail (G_IS_OBJECT (object));
2156 g_return_if_fail (object->ref_count > 0);
2158 va_start (var_args, signal_spec);
2161 GCallback callback = va_arg (var_args, GCallback);
2162 gpointer data = va_arg (var_args, gpointer);
2163 guint sid = 0, detail = 0, mask = 0;
2165 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2166 strncmp (signal_spec, "any-signal::", 12) == 0)
2169 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2171 else if (strcmp (signal_spec, "any_signal") == 0 ||
2172 strcmp (signal_spec, "any-signal") == 0)
2175 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2179 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2183 if ((mask & G_SIGNAL_MATCH_ID) &&
2184 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2185 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2186 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2188 NULL, (gpointer)callback, data))
2189 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2190 signal_spec = va_arg (var_args, gchar*);
2201 } weak_refs[1]; /* flexible array */
2205 weak_refs_notify (gpointer data)
2207 WeakRefStack *wstack = data;
2210 for (i = 0; i < wstack->n_weak_refs; i++)
2211 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2216 * g_object_weak_ref:
2217 * @object: #GObject to reference weakly
2218 * @notify: callback to invoke before the object is freed
2219 * @data: extra data to pass to notify
2221 * Adds a weak reference callback to an object. Weak references are
2222 * used for notification when an object is finalized. They are called
2223 * "weak references" because they allow you to safely hold a pointer
2224 * to an object without calling g_object_ref() (g_object_ref() adds a
2225 * strong reference, that is, forces the object to stay alive).
2228 g_object_weak_ref (GObject *object,
2232 WeakRefStack *wstack;
2235 g_return_if_fail (G_IS_OBJECT (object));
2236 g_return_if_fail (notify != NULL);
2237 g_return_if_fail (object->ref_count >= 1);
2239 G_LOCK (weak_refs_mutex);
2240 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2243 i = wstack->n_weak_refs++;
2244 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2248 wstack = g_renew (WeakRefStack, NULL, 1);
2249 wstack->object = object;
2250 wstack->n_weak_refs = 1;
2253 wstack->weak_refs[i].notify = notify;
2254 wstack->weak_refs[i].data = data;
2255 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2256 G_UNLOCK (weak_refs_mutex);
2260 * g_object_weak_unref:
2261 * @object: #GObject to remove a weak reference from
2262 * @notify: callback to search for
2263 * @data: data to search for
2265 * Removes a weak reference callback to an object.
2268 g_object_weak_unref (GObject *object,
2272 WeakRefStack *wstack;
2273 gboolean found_one = FALSE;
2275 g_return_if_fail (G_IS_OBJECT (object));
2276 g_return_if_fail (notify != NULL);
2278 G_LOCK (weak_refs_mutex);
2279 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2284 for (i = 0; i < wstack->n_weak_refs; i++)
2285 if (wstack->weak_refs[i].notify == notify &&
2286 wstack->weak_refs[i].data == data)
2289 wstack->n_weak_refs -= 1;
2290 if (i != wstack->n_weak_refs)
2291 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2296 G_UNLOCK (weak_refs_mutex);
2298 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2302 * g_object_add_weak_pointer:
2303 * @object: The object that should be weak referenced.
2304 * @weak_pointer_location: (inout): The memory address of a pointer.
2306 * Adds a weak reference from weak_pointer to @object to indicate that
2307 * the pointer located at @weak_pointer_location is only valid during
2308 * the lifetime of @object. When the @object is finalized,
2309 * @weak_pointer will be set to %NULL.
2312 g_object_add_weak_pointer (GObject *object,
2313 gpointer *weak_pointer_location)
2315 g_return_if_fail (G_IS_OBJECT (object));
2316 g_return_if_fail (weak_pointer_location != NULL);
2318 g_object_weak_ref (object,
2319 (GWeakNotify) g_nullify_pointer,
2320 weak_pointer_location);
2324 * g_object_remove_weak_pointer:
2325 * @object: The object that is weak referenced.
2326 * @weak_pointer_location: (inout): The memory address of a pointer.
2328 * Removes a weak reference from @object that was previously added
2329 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2330 * to match the one used with g_object_add_weak_pointer().
2333 g_object_remove_weak_pointer (GObject *object,
2334 gpointer *weak_pointer_location)
2336 g_return_if_fail (G_IS_OBJECT (object));
2337 g_return_if_fail (weak_pointer_location != NULL);
2339 g_object_weak_unref (object,
2340 (GWeakNotify) g_nullify_pointer,
2341 weak_pointer_location);
2345 object_floating_flag_handler (GObject *object,
2351 case +1: /* force floating if possible */
2353 oldvalue = g_atomic_pointer_get (&object->qdata);
2354 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2355 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2356 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2357 case -1: /* sink if possible */
2359 oldvalue = g_atomic_pointer_get (&object->qdata);
2360 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2361 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2362 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2363 default: /* check floating */
2364 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2369 * g_object_is_floating:
2370 * @object: a #GObject
2372 * Checks wether @object has a <link linkend="floating-ref">floating</link>
2377 * Returns: %TRUE if @object has a floating reference
2380 g_object_is_floating (gpointer _object)
2382 GObject *object = _object;
2383 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2384 return floating_flag_handler (object, 0);
2388 * g_object_ref_sink:
2389 * @object: a #GObject
2391 * Increase the reference count of @object, and possibly remove the
2392 * <link linkend="floating-ref">floating</link> reference, if @object
2393 * has a floating reference.
2395 * In other words, if the object is floating, then this call "assumes
2396 * ownership" of the floating reference, converting it to a normal
2397 * reference by clearing the floating flag while leaving the reference
2398 * count unchanged. If the object is not floating, then this call
2399 * adds a new normal reference increasing the reference count by one.
2406 g_object_ref_sink (gpointer _object)
2408 GObject *object = _object;
2409 gboolean was_floating;
2410 g_return_val_if_fail (G_IS_OBJECT (object), object);
2411 g_return_val_if_fail (object->ref_count >= 1, object);
2412 g_object_ref (object);
2413 was_floating = floating_flag_handler (object, -1);
2415 g_object_unref (object);
2420 * g_object_force_floating:
2421 * @object: a #GObject
2423 * This function is intended for #GObject implementations to re-enforce a
2424 * <link linkend="floating-ref">floating</link> object reference.
2425 * Doing this is seldomly required, all
2426 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2427 * usually just needs to be sunken by calling g_object_ref_sink().
2432 g_object_force_floating (GObject *object)
2434 gboolean was_floating;
2435 g_return_if_fail (G_IS_OBJECT (object));
2436 g_return_if_fail (object->ref_count >= 1);
2438 was_floating = floating_flag_handler (object, +1);
2443 guint n_toggle_refs;
2445 GToggleNotify notify;
2447 } toggle_refs[1]; /* flexible array */
2451 toggle_refs_notify (GObject *object,
2452 gboolean is_last_ref)
2454 ToggleRefStack tstack, *tstackptr;
2456 G_LOCK (toggle_refs_mutex);
2457 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2458 tstack = *tstackptr;
2459 G_UNLOCK (toggle_refs_mutex);
2461 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2462 * will only be notified when there is exactly one of them.
2464 g_assert (tstack.n_toggle_refs == 1);
2465 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2469 * g_object_add_toggle_ref:
2470 * @object: a #GObject
2471 * @notify: a function to call when this reference is the
2472 * last reference to the object, or is no longer
2473 * the last reference.
2474 * @data: data to pass to @notify
2476 * Increases the reference count of the object by one and sets a
2477 * callback to be called when all other references to the object are
2478 * dropped, or when this is already the last reference to the object
2479 * and another reference is established.
2481 * This functionality is intended for binding @object to a proxy
2482 * object managed by another memory manager. This is done with two
2483 * paired references: the strong reference added by
2484 * g_object_add_toggle_ref() and a reverse reference to the proxy
2485 * object which is either a strong reference or weak reference.
2487 * The setup is that when there are no other references to @object,
2488 * only a weak reference is held in the reverse direction from @object
2489 * to the proxy object, but when there are other references held to
2490 * @object, a strong reference is held. The @notify callback is called
2491 * when the reference from @object to the proxy object should be
2492 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2493 * true) or weak to strong (@is_last_ref false).
2495 * Since a (normal) reference must be held to the object before
2496 * calling g_object_toggle_ref(), the initial state of the reverse
2497 * link is always strong.
2499 * Multiple toggle references may be added to the same gobject,
2500 * however if there are multiple toggle references to an object, none
2501 * of them will ever be notified until all but one are removed. For
2502 * this reason, you should only ever use a toggle reference if there
2503 * is important state in the proxy object.
2508 g_object_add_toggle_ref (GObject *object,
2509 GToggleNotify notify,
2512 ToggleRefStack *tstack;
2515 g_return_if_fail (G_IS_OBJECT (object));
2516 g_return_if_fail (notify != NULL);
2517 g_return_if_fail (object->ref_count >= 1);
2519 g_object_ref (object);
2521 G_LOCK (toggle_refs_mutex);
2522 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2525 i = tstack->n_toggle_refs++;
2526 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2527 * in tstate->toggle_refs */
2528 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2532 tstack = g_renew (ToggleRefStack, NULL, 1);
2533 tstack->object = object;
2534 tstack->n_toggle_refs = 1;
2538 /* Set a flag for fast lookup after adding the first toggle reference */
2539 if (tstack->n_toggle_refs == 1)
2540 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2542 tstack->toggle_refs[i].notify = notify;
2543 tstack->toggle_refs[i].data = data;
2544 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2545 (GDestroyNotify)g_free);
2546 G_UNLOCK (toggle_refs_mutex);
2550 * g_object_remove_toggle_ref:
2551 * @object: a #GObject
2552 * @notify: a function to call when this reference is the
2553 * last reference to the object, or is no longer
2554 * the last reference.
2555 * @data: data to pass to @notify
2557 * Removes a reference added with g_object_add_toggle_ref(). The
2558 * reference count of the object is decreased by one.
2563 g_object_remove_toggle_ref (GObject *object,
2564 GToggleNotify notify,
2567 ToggleRefStack *tstack;
2568 gboolean found_one = FALSE;
2570 g_return_if_fail (G_IS_OBJECT (object));
2571 g_return_if_fail (notify != NULL);
2573 G_LOCK (toggle_refs_mutex);
2574 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2579 for (i = 0; i < tstack->n_toggle_refs; i++)
2580 if (tstack->toggle_refs[i].notify == notify &&
2581 tstack->toggle_refs[i].data == data)
2584 tstack->n_toggle_refs -= 1;
2585 if (i != tstack->n_toggle_refs)
2586 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2588 if (tstack->n_toggle_refs == 0)
2589 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2594 G_UNLOCK (toggle_refs_mutex);
2597 g_object_unref (object);
2599 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2604 * @object: a #GObject
2606 * Increases the reference count of @object.
2608 * Returns: the same @object
2611 g_object_ref (gpointer _object)
2613 GObject *object = _object;
2616 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2617 g_return_val_if_fail (object->ref_count > 0, NULL);
2619 #ifdef G_ENABLE_DEBUG
2620 if (g_trap_object_ref == object)
2622 #endif /* G_ENABLE_DEBUG */
2625 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2627 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2628 toggle_refs_notify (object, FALSE);
2630 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2637 * @object: a #GObject
2639 * Decreases the reference count of @object. When its reference count
2640 * drops to 0, the object is finalized (i.e. its memory is freed).
2643 g_object_unref (gpointer _object)
2645 GObject *object = _object;
2648 g_return_if_fail (G_IS_OBJECT (object));
2649 g_return_if_fail (object->ref_count > 0);
2651 #ifdef G_ENABLE_DEBUG
2652 if (g_trap_object_ref == object)
2654 #endif /* G_ENABLE_DEBUG */
2656 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2657 retry_atomic_decrement1:
2658 old_ref = g_atomic_int_get (&object->ref_count);
2661 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2662 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2664 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2665 goto retry_atomic_decrement1;
2667 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2669 /* if we went from 2->1 we need to notify toggle refs if any */
2670 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2671 toggle_refs_notify (object, TRUE);
2675 /* we are about tp remove the last reference */
2676 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2677 G_OBJECT_GET_CLASS (object)->dispose (object);
2678 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2680 /* may have been re-referenced meanwhile */
2681 retry_atomic_decrement2:
2682 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2685 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2686 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2688 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2689 goto retry_atomic_decrement2;
2691 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2693 /* if we went from 2->1 we need to notify toggle refs if any */
2694 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2695 toggle_refs_notify (object, TRUE);
2700 /* we are still in the process of taking away the last ref */
2701 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2702 g_signal_handlers_destroy (object);
2703 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2705 /* decrement the last reference */
2706 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2708 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2710 /* may have been re-referenced meanwhile */
2711 if (G_LIKELY (old_ref == 1))
2713 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2714 G_OBJECT_GET_CLASS (object)->finalize (object);
2716 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2718 #ifdef G_ENABLE_DEBUG
2721 /* catch objects not chaining finalize handlers */
2722 G_LOCK (debug_objects);
2723 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2724 G_UNLOCK (debug_objects);
2726 #endif /* G_ENABLE_DEBUG */
2727 g_type_free_instance ((GTypeInstance*) object);
2734 * @object_ptr: a pointer to a #GObject reference
2736 * Clears a reference to a #GObject.
2738 * @object_ptr must not be %NULL.
2740 * If the reference is %NULL then this function does nothing.
2741 * Otherwise, the reference count of the object is decreased and the
2742 * pointer is set to %NULL.
2744 * This function is threadsafe and modifies the pointer atomically,
2745 * using memory barriers where needed.
2747 * A macro is also included that allows this function to be used without
2752 #undef g_clear_object
2754 g_clear_object (volatile GObject **object_ptr)
2756 gpointer *ptr = (gpointer) object_ptr;
2759 /* This is a little frustrating.
2760 * Would be nice to have an atomic exchange (with no compare).
2763 old = g_atomic_pointer_get (ptr);
2764 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2767 g_object_unref (old);
2771 * g_object_get_qdata:
2772 * @object: The GObject to get a stored user data pointer from
2773 * @quark: A #GQuark, naming the user data pointer
2775 * This function gets back user data pointers stored via
2776 * g_object_set_qdata().
2778 * Returns: The user data pointer set, or %NULL
2781 g_object_get_qdata (GObject *object,
2784 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2786 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2790 * g_object_set_qdata:
2791 * @object: The GObject to set store a user data pointer
2792 * @quark: A #GQuark, naming the user data pointer
2793 * @data: An opaque user data pointer
2795 * This sets an opaque, named pointer on an object.
2796 * The name is specified through a #GQuark (retrived e.g. via
2797 * g_quark_from_static_string()), and the pointer
2798 * can be gotten back from the @object with g_object_get_qdata()
2799 * until the @object is finalized.
2800 * Setting a previously set user data pointer, overrides (frees)
2801 * the old pointer set, using #NULL as pointer essentially
2802 * removes the data stored.
2805 g_object_set_qdata (GObject *object,
2809 g_return_if_fail (G_IS_OBJECT (object));
2810 g_return_if_fail (quark > 0);
2812 g_datalist_id_set_data (&object->qdata, quark, data);
2816 * g_object_set_qdata_full:
2817 * @object: The GObject to set store a user data pointer
2818 * @quark: A #GQuark, naming the user data pointer
2819 * @data: An opaque user data pointer
2820 * @destroy: Function to invoke with @data as argument, when @data
2823 * This function works like g_object_set_qdata(), but in addition,
2824 * a void (*destroy) (gpointer) function may be specified which is
2825 * called with @data as argument when the @object is finalized, or
2826 * the data is being overwritten by a call to g_object_set_qdata()
2827 * with the same @quark.
2830 g_object_set_qdata_full (GObject *object,
2833 GDestroyNotify destroy)
2835 g_return_if_fail (G_IS_OBJECT (object));
2836 g_return_if_fail (quark > 0);
2838 g_datalist_id_set_data_full (&object->qdata, quark, data,
2839 data ? destroy : (GDestroyNotify) NULL);
2843 * g_object_steal_qdata:
2844 * @object: The GObject to get a stored user data pointer from
2845 * @quark: A #GQuark, naming the user data pointer
2847 * This function gets back user data pointers stored via
2848 * g_object_set_qdata() and removes the @data from object
2849 * without invoking its destroy() function (if any was
2851 * Usually, calling this function is only required to update
2852 * user data pointers with a destroy notifier, for example:
2855 * object_add_to_user_list (GObject *object,
2856 * const gchar *new_string)
2858 * // the quark, naming the object data
2859 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2860 * // retrive the old string list
2861 * GList *list = g_object_steal_qdata (object, quark_string_list);
2863 * // prepend new string
2864 * list = g_list_prepend (list, g_strdup (new_string));
2865 * // this changed 'list', so we need to set it again
2866 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2869 * free_string_list (gpointer data)
2871 * GList *node, *list = data;
2873 * for (node = list; node; node = node->next)
2874 * g_free (node->data);
2875 * g_list_free (list);
2878 * Using g_object_get_qdata() in the above example, instead of
2879 * g_object_steal_qdata() would have left the destroy function set,
2880 * and thus the partial string list would have been freed upon
2881 * g_object_set_qdata_full().
2883 * Returns: The user data pointer set, or %NULL
2886 g_object_steal_qdata (GObject *object,
2889 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2890 g_return_val_if_fail (quark > 0, NULL);
2892 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2896 * g_object_get_data:
2897 * @object: #GObject containing the associations
2898 * @key: name of the key for that association
2900 * Gets a named field from the objects table of associations (see g_object_set_data()).
2902 * Returns: the data if found, or %NULL if no such data exists.
2905 g_object_get_data (GObject *object,
2910 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2911 g_return_val_if_fail (key != NULL, NULL);
2913 quark = g_quark_try_string (key);
2915 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2919 * g_object_set_data:
2920 * @object: #GObject containing the associations.
2921 * @key: name of the key
2922 * @data: data to associate with that key
2924 * Each object carries around a table of associations from
2925 * strings to pointers. This function lets you set an association.
2927 * If the object already had an association with that name,
2928 * the old association will be destroyed.
2931 g_object_set_data (GObject *object,
2935 g_return_if_fail (G_IS_OBJECT (object));
2936 g_return_if_fail (key != NULL);
2938 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2942 * g_object_set_data_full:
2943 * @object: #GObject containing the associations
2944 * @key: name of the key
2945 * @data: data to associate with that key
2946 * @destroy: function to call when the association is destroyed
2948 * Like g_object_set_data() except it adds notification
2949 * for when the association is destroyed, either by setting it
2950 * to a different value or when the object is destroyed.
2952 * Note that the @destroy callback is not called if @data is %NULL.
2955 g_object_set_data_full (GObject *object,
2958 GDestroyNotify destroy)
2960 g_return_if_fail (G_IS_OBJECT (object));
2961 g_return_if_fail (key != NULL);
2963 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2964 data ? destroy : (GDestroyNotify) NULL);
2968 * g_object_steal_data:
2969 * @object: #GObject containing the associations
2970 * @key: name of the key
2972 * Remove a specified datum from the object's data associations,
2973 * without invoking the association's destroy handler.
2975 * Returns: the data if found, or %NULL if no such data exists.
2978 g_object_steal_data (GObject *object,
2983 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2984 g_return_val_if_fail (key != NULL, NULL);
2986 quark = g_quark_try_string (key);
2988 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
2992 g_value_object_init (GValue *value)
2994 value->data[0].v_pointer = NULL;
2998 g_value_object_free_value (GValue *value)
3000 if (value->data[0].v_pointer)
3001 g_object_unref (value->data[0].v_pointer);
3005 g_value_object_copy_value (const GValue *src_value,
3008 if (src_value->data[0].v_pointer)
3009 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3011 dest_value->data[0].v_pointer = NULL;
3015 g_value_object_transform_value (const GValue *src_value,
3018 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)))
3019 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3021 dest_value->data[0].v_pointer = NULL;
3025 g_value_object_peek_pointer (const GValue *value)
3027 return value->data[0].v_pointer;
3031 g_value_object_collect_value (GValue *value,
3032 guint n_collect_values,
3033 GTypeCValue *collect_values,
3034 guint collect_flags)
3036 if (collect_values[0].v_pointer)
3038 GObject *object = collect_values[0].v_pointer;
3040 if (object->g_type_instance.g_class == NULL)
3041 return g_strconcat ("invalid unclassed object pointer for value type `",
3042 G_VALUE_TYPE_NAME (value),
3045 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3046 return g_strconcat ("invalid object type `",
3047 G_OBJECT_TYPE_NAME (object),
3048 "' for value type `",
3049 G_VALUE_TYPE_NAME (value),
3052 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3053 value->data[0].v_pointer = g_object_ref (object);
3056 value->data[0].v_pointer = NULL;
3062 g_value_object_lcopy_value (const GValue *value,
3063 guint n_collect_values,
3064 GTypeCValue *collect_values,
3065 guint collect_flags)
3067 GObject **object_p = collect_values[0].v_pointer;
3070 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3072 if (!value->data[0].v_pointer)
3074 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3075 *object_p = value->data[0].v_pointer;
3077 *object_p = g_object_ref (value->data[0].v_pointer);
3083 * g_value_set_object:
3084 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3085 * @v_object: object value to be set
3087 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3089 * g_value_set_object() increases the reference count of @v_object
3090 * (the #GValue holds a reference to @v_object). If you do not wish
3091 * to increase the reference count of the object (i.e. you wish to
3092 * pass your current reference to the #GValue because you no longer
3093 * need it), use g_value_take_object() instead.
3095 * It is important that your #GValue holds a reference to @v_object (either its
3096 * own, or one it has taken) to ensure that the object won't be destroyed while
3097 * the #GValue still exists).
3100 g_value_set_object (GValue *value,
3105 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3107 old = value->data[0].v_pointer;
3111 g_return_if_fail (G_IS_OBJECT (v_object));
3112 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3114 value->data[0].v_pointer = v_object;
3115 g_object_ref (value->data[0].v_pointer);
3118 value->data[0].v_pointer = NULL;
3121 g_object_unref (old);
3125 * g_value_set_object_take_ownership:
3126 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3127 * @v_object: object value to be set
3129 * This is an internal function introduced mainly for C marshallers.
3131 * Deprecated: 2.4: Use g_value_take_object() instead.
3134 g_value_set_object_take_ownership (GValue *value,
3137 g_value_take_object (value, v_object);
3141 * g_value_take_object:
3142 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3143 * @v_object: object value to be set
3145 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3146 * and takes over the ownership of the callers reference to @v_object;
3147 * the caller doesn't have to unref it any more (i.e. the reference
3148 * count of the object is not increased).
3150 * If you want the #GValue to hold its own reference to @v_object, use
3151 * g_value_set_object() instead.
3156 g_value_take_object (GValue *value,
3159 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3161 if (value->data[0].v_pointer)
3163 g_object_unref (value->data[0].v_pointer);
3164 value->data[0].v_pointer = NULL;
3169 g_return_if_fail (G_IS_OBJECT (v_object));
3170 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3172 value->data[0].v_pointer = v_object; /* we take over the reference count */
3177 * g_value_get_object:
3178 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3180 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3182 * Returns: object contents of @value
3185 g_value_get_object (const GValue *value)
3187 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3189 return value->data[0].v_pointer;
3193 * g_value_dup_object:
3194 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3196 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3197 * its reference count.
3199 * Returns: object content of @value, should be unreferenced when no
3203 g_value_dup_object (const GValue *value)
3205 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3207 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3211 * g_signal_connect_object:
3212 * @instance: the instance to connect to.
3213 * @detailed_signal: a string of the form "signal-name::detail".
3214 * @c_handler: the #GCallback to connect.
3215 * @gobject: the object to pass as data to @c_handler.
3216 * @connect_flags: a combination of #GConnnectFlags.
3218 * This is similar to g_signal_connect_data(), but uses a closure which
3219 * ensures that the @gobject stays alive during the call to @c_handler
3220 * by temporarily adding a reference count to @gobject.
3222 * Note that there is a bug in GObject that makes this function
3223 * much less useful than it might seem otherwise. Once @gobject is
3224 * disposed, the callback will no longer be called, but, the signal
3225 * handler is <emphasis>not</emphasis> currently disconnected. If the
3226 * @instance is itself being freed at the same time than this doesn't
3227 * matter, since the signal will automatically be removed, but
3228 * if @instance persists, then the signal handler will leak. You
3229 * should not remove the signal yourself because in a future versions of
3230 * GObject, the handler <emphasis>will</emphasis> automatically
3233 * It's possible to work around this problem in a way that will
3234 * continue to work with future versions of GObject by checking
3235 * that the signal handler is still connected before disconnected it:
3236 * <informalexample><programlisting>
3237 * if (g_signal_handler_is_connected (instance, id))
3238 * g_signal_handler_disconnect (instance, id);
3239 * </programlisting></informalexample>
3241 * Returns: the handler id.
3244 g_signal_connect_object (gpointer instance,
3245 const gchar *detailed_signal,
3246 GCallback c_handler,
3248 GConnectFlags connect_flags)
3250 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3251 g_return_val_if_fail (detailed_signal != NULL, 0);
3252 g_return_val_if_fail (c_handler != NULL, 0);
3258 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3260 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3262 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3265 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3271 GClosure *closures[1]; /* flexible array */
3273 /* don't change this structure without supplying an accessor for
3274 * watched closures, e.g.:
3275 * GSList* g_object_list_watched_closures (GObject *object)
3278 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3279 * carray = g_object_get_data (object, "GObject-closure-array");
3282 * GSList *slist = NULL;
3284 * for (i = 0; i < carray->n_closures; i++)
3285 * slist = g_slist_prepend (slist, carray->closures[i]);
3293 object_remove_closure (gpointer data,
3296 GObject *object = data;
3300 G_LOCK (closure_array_mutex);
3301 carray = g_object_get_qdata (object, quark_closure_array);
3302 for (i = 0; i < carray->n_closures; i++)
3303 if (carray->closures[i] == closure)
3305 carray->n_closures--;
3306 if (i < carray->n_closures)
3307 carray->closures[i] = carray->closures[carray->n_closures];
3308 G_UNLOCK (closure_array_mutex);
3311 G_UNLOCK (closure_array_mutex);
3312 g_assert_not_reached ();
3316 destroy_closure_array (gpointer data)
3318 CArray *carray = data;
3319 GObject *object = carray->object;
3320 guint i, n = carray->n_closures;
3322 for (i = 0; i < n; i++)
3324 GClosure *closure = carray->closures[i];
3326 /* removing object_remove_closure() upfront is probably faster than
3327 * letting it fiddle with quark_closure_array which is empty anyways
3329 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3330 g_closure_invalidate (closure);
3336 * g_object_watch_closure:
3337 * @object: GObject restricting lifetime of @closure
3338 * @closure: GClosure to watch
3340 * This function essentially limits the life time of the @closure to
3341 * the life time of the object. That is, when the object is finalized,
3342 * the @closure is invalidated by calling g_closure_invalidate() on
3343 * it, in order to prevent invocations of the closure with a finalized
3344 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3345 * added as marshal guards to the @closure, to ensure that an extra
3346 * reference count is held on @object during invocation of the
3347 * @closure. Usually, this function will be called on closures that
3348 * use this @object as closure data.
3351 g_object_watch_closure (GObject *object,
3357 g_return_if_fail (G_IS_OBJECT (object));
3358 g_return_if_fail (closure != NULL);
3359 g_return_if_fail (closure->is_invalid == FALSE);
3360 g_return_if_fail (closure->in_marshal == FALSE);
3361 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3363 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3364 g_closure_add_marshal_guards (closure,
3365 object, (GClosureNotify) g_object_ref,
3366 object, (GClosureNotify) g_object_unref);
3367 G_LOCK (closure_array_mutex);
3368 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3371 carray = g_renew (CArray, NULL, 1);
3372 carray->object = object;
3373 carray->n_closures = 1;
3378 i = carray->n_closures++;
3379 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3381 carray->closures[i] = closure;
3382 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3383 G_UNLOCK (closure_array_mutex);
3387 * g_closure_new_object:
3388 * @sizeof_closure: the size of the structure to allocate, must be at least
3389 * <literal>sizeof (GClosure)</literal>
3390 * @object: a #GObject pointer to store in the @data field of the newly
3391 * allocated #GClosure
3393 * A variant of g_closure_new_simple() which stores @object in the
3394 * @data field of the closure and calls g_object_watch_closure() on
3395 * @object and the created closure. This function is mainly useful
3396 * when implementing new types of closures.
3398 * Returns: a newly allocated #GClosure
3401 g_closure_new_object (guint sizeof_closure,
3406 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3407 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3409 closure = g_closure_new_simple (sizeof_closure, object);
3410 g_object_watch_closure (object, closure);
3416 * g_cclosure_new_object:
3417 * @callback_func: the function to invoke
3418 * @object: a #GObject pointer to pass to @callback_func
3420 * A variant of g_cclosure_new() which uses @object as @user_data and
3421 * calls g_object_watch_closure() on @object and the created
3422 * closure. This function is useful when you have a callback closely
3423 * associated with a #GObject, and want the callback to no longer run
3424 * after the object is is freed.
3426 * Returns: a new #GCClosure
3429 g_cclosure_new_object (GCallback callback_func,
3434 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3435 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3436 g_return_val_if_fail (callback_func != NULL, NULL);
3438 closure = g_cclosure_new (callback_func, object, NULL);
3439 g_object_watch_closure (object, closure);
3445 * g_cclosure_new_object_swap:
3446 * @callback_func: the function to invoke
3447 * @object: a #GObject pointer to pass to @callback_func
3449 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3450 * and calls g_object_watch_closure() on @object and the created
3451 * closure. This function is useful when you have a callback closely
3452 * associated with a #GObject, and want the callback to no longer run
3453 * after the object is is freed.
3455 * Returns: a new #GCClosure
3458 g_cclosure_new_object_swap (GCallback callback_func,
3463 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3464 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3465 g_return_val_if_fail (callback_func != NULL, NULL);
3467 closure = g_cclosure_new_swap (callback_func, object, NULL);
3468 g_object_watch_closure (object, closure);
3474 g_object_compat_control (gsize what,
3480 case 1: /* floating base type */
3481 return G_TYPE_INITIALLY_UNOWNED;
3482 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3483 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3485 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3487 *pp = floating_flag_handler;
3494 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3497 g_initially_unowned_init (GInitiallyUnowned *object)
3499 g_object_force_floating (object);
3503 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)