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 "gtype-private.h"
31 #include "gvaluecollector.h"
33 #include "gparamspecs.h"
34 #include "gvaluetypes.h"
35 #include "gobject_trace.h"
39 * @short_description: The base object type
40 * @see_also: #GParamSpecObject, g_param_spec_object()
41 * @title: The Base Object Type
43 * GObject is the fundamental type providing the common attributes and
44 * methods for all object types in GTK+, Pango and other libraries
45 * based on GObject. The GObject class provides methods for object
46 * construction and destruction, property access methods, and signal
47 * support. Signals are described in detail in <xref
48 * linkend="gobject-Signals"/>.
50 * <para id="floating-ref">
51 * GInitiallyUnowned is derived from GObject. The only difference between
52 * the two is that the initial reference of a GInitiallyUnowned is flagged
53 * as a <firstterm>floating</firstterm> reference.
54 * This means that it is not specifically claimed to be "owned" by
55 * any code portion. The main motivation for providing floating references is
56 * C convenience. In particular, it allows code to be written as:
58 * container = create_container ();
59 * container_add_child (container, create_child());
61 * If <function>container_add_child()</function> will g_object_ref_sink() the
62 * passed in child, no reference of the newly created child is leaked.
63 * Without floating references, <function>container_add_child()</function>
64 * can only g_object_ref() the new child, so to implement this code without
65 * reference leaks, it would have to be written as:
68 * container = create_container ();
69 * child = create_child ();
70 * container_add_child (container, child);
71 * g_object_unref (child);
73 * The floating reference can be converted into
74 * an ordinary reference by calling g_object_ref_sink().
75 * For already sunken objects (objects that don't have a floating reference
76 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
78 * Since floating references are useful almost exclusively for C convenience,
79 * language bindings that provide automated reference and memory ownership
80 * maintenance (such as smart pointers or garbage collection) should not
81 * expose floating references in their API.
84 * Some object implementations may need to save an objects floating state
85 * across certain code portions (an example is #GtkMenu), to achieve this,
86 * the following sequence can be used:
89 * /* save floating state */
90 * gboolean was_floating = g_object_is_floating (object);
91 * g_object_ref_sink (object);
92 * /* protected code portion */
94 * /* restore floating state */
96 * g_object_force_floating (object);
97 * g_object_unref (object); /* release previously acquired reference */
103 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
104 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
106 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
107 #define OBJECT_HAS_TOGGLE_REF(object) \
108 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
109 #define OBJECT_FLOATING_FLAG 0x2
111 #define CLASS_HAS_PROPS_FLAG 0x1
112 #define CLASS_HAS_PROPS(class) \
113 ((class)->flags & CLASS_HAS_PROPS_FLAG)
114 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
115 ((class)->constructor != g_object_constructor)
116 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
117 ((class)->constructed != g_object_constructed)
119 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
120 #define CLASS_HAS_DERIVED_CLASS(class) \
121 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
123 /* --- signals --- */
130 /* --- properties --- */
136 /* --- prototypes --- */
137 static void g_object_base_class_init (GObjectClass *class);
138 static void g_object_base_class_finalize (GObjectClass *class);
139 static void g_object_do_class_init (GObjectClass *class);
140 static void g_object_init (GObject *object,
141 GObjectClass *class);
142 static GObject* g_object_constructor (GType type,
143 guint n_construct_properties,
144 GObjectConstructParam *construct_params);
145 static void g_object_constructed (GObject *object);
146 static void g_object_real_dispose (GObject *object);
147 static void g_object_finalize (GObject *object);
148 static void g_object_do_set_property (GObject *object,
152 static void g_object_do_get_property (GObject *object,
156 static void g_value_object_init (GValue *value);
157 static void g_value_object_free_value (GValue *value);
158 static void g_value_object_copy_value (const GValue *src_value,
160 static void g_value_object_transform_value (const GValue *src_value,
162 static gpointer g_value_object_peek_pointer (const GValue *value);
163 static gchar* g_value_object_collect_value (GValue *value,
164 guint n_collect_values,
165 GTypeCValue *collect_values,
166 guint collect_flags);
167 static gchar* g_value_object_lcopy_value (const GValue *value,
168 guint n_collect_values,
169 GTypeCValue *collect_values,
170 guint collect_flags);
171 static void g_object_dispatch_properties_changed (GObject *object,
173 GParamSpec **pspecs);
174 static guint object_floating_flag_handler (GObject *object,
177 static void object_interface_check_properties (gpointer func_data,
180 /* --- typedefs --- */
181 typedef struct _GObjectNotifyContext GObjectNotifyContext;
182 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
183 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
185 GParamSpec **pspecs);
188 /* --- structures --- */
189 struct _GObjectNotifyContext
191 GQuark quark_notify_queue;
192 GObjectNotifyQueueDispatcher dispatcher;
194 struct _GObjectNotifyQueue
196 GObjectNotifyContext *context;
199 guint16 freeze_count;
202 /* --- variables --- */
203 G_LOCK_DEFINE_STATIC (closure_array_mutex);
204 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
205 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
206 static GQuark quark_closure_array = 0;
207 static GQuark quark_weak_refs = 0;
208 static GQuark quark_toggle_refs = 0;
209 static GParamSpecPool *pspec_pool = NULL;
210 static GObjectNotifyContext property_notify_context = { 0, };
211 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
212 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
213 G_LOCK_DEFINE_STATIC (construction_mutex);
214 static GSList *construction_objects = NULL;
216 /* --- functions --- */
217 #ifdef G_ENABLE_DEBUG
218 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
219 G_LOCK_DEFINE_STATIC (debug_objects);
220 static volatile GObject *g_trap_object_ref = NULL;
221 static guint debug_objects_count = 0;
222 static GHashTable *debug_objects_ht = NULL;
224 G_LOCK_DEFINE_STATIC(notify_lock);
226 /* --- functions --- */
228 g_object_notify_queue_free (gpointer data)
230 GObjectNotifyQueue *nqueue = data;
232 g_slist_free (nqueue->pspecs);
233 g_slice_free (GObjectNotifyQueue, nqueue);
236 static inline GObjectNotifyQueue*
237 g_object_notify_queue_freeze (GObject *object,
238 GObjectNotifyContext *context)
240 GObjectNotifyQueue *nqueue;
243 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
246 nqueue = g_slice_new0 (GObjectNotifyQueue);
247 nqueue->context = context;
248 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
249 nqueue, g_object_notify_queue_free);
252 if (nqueue->freeze_count >= 65535)
253 g_critical("Free queue for %s (%p) is larger than 65535,"
254 " called g_object_freeze_notify() too often."
255 " Forgot to call g_object_thaw_notify() or infinite loop",
256 G_OBJECT_TYPE_NAME (object), object);
258 nqueue->freeze_count++;
259 G_UNLOCK(notify_lock);
265 g_object_notify_queue_thaw (GObject *object,
266 GObjectNotifyQueue *nqueue)
268 GObjectNotifyContext *context = nqueue->context;
269 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
273 g_return_if_fail (nqueue->freeze_count > 0);
274 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
278 /* Just make sure we never get into some nasty race condition */
279 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
280 G_UNLOCK(notify_lock);
281 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
282 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
286 nqueue->freeze_count--;
287 if (nqueue->freeze_count) {
288 G_UNLOCK(notify_lock);
292 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
294 for (slist = nqueue->pspecs; slist; slist = slist->next)
296 pspecs[n_pspecs++] = slist->data;
298 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
300 G_UNLOCK(notify_lock);
303 context->dispatcher (object, n_pspecs, pspecs);
308 g_object_notify_queue_add (GObject *object,
309 GObjectNotifyQueue *nqueue,
312 if (pspec->flags & G_PARAM_READABLE)
314 GParamSpec *redirect;
316 redirect = g_param_spec_get_redirect_target (pspec);
322 g_return_if_fail (nqueue->n_pspecs < 65535);
324 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
326 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
330 G_UNLOCK(notify_lock);
335 debug_objects_foreach (gpointer key,
339 GObject *object = value;
341 g_message ("[%p] stale %s\tref_count=%u",
343 G_OBJECT_TYPE_NAME (object),
348 debug_objects_atexit (void)
352 G_LOCK (debug_objects);
353 g_message ("stale GObjects: %u", debug_objects_count);
354 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
355 G_UNLOCK (debug_objects);
358 #endif /* G_ENABLE_DEBUG */
361 _g_object_type_init (void)
363 static gboolean initialized = FALSE;
364 static const GTypeFundamentalInfo finfo = {
365 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
367 static GTypeInfo info = {
368 sizeof (GObjectClass),
369 (GBaseInitFunc) g_object_base_class_init,
370 (GBaseFinalizeFunc) g_object_base_class_finalize,
371 (GClassInitFunc) g_object_do_class_init,
372 NULL /* class_destroy */,
373 NULL /* class_data */,
376 (GInstanceInitFunc) g_object_init,
377 NULL, /* value_table */
379 static const GTypeValueTable value_table = {
380 g_value_object_init, /* value_init */
381 g_value_object_free_value, /* value_free */
382 g_value_object_copy_value, /* value_copy */
383 g_value_object_peek_pointer, /* value_peek_pointer */
384 "p", /* collect_format */
385 g_value_object_collect_value, /* collect_value */
386 "p", /* lcopy_format */
387 g_value_object_lcopy_value, /* lcopy_value */
391 g_return_if_fail (initialized == FALSE);
396 info.value_table = &value_table;
397 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
398 g_assert (type == G_TYPE_OBJECT);
399 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
401 #ifdef G_ENABLE_DEBUG
404 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
405 g_atexit (debug_objects_atexit);
407 #endif /* G_ENABLE_DEBUG */
411 g_object_base_class_init (GObjectClass *class)
413 GObjectClass *pclass = g_type_class_peek_parent (class);
415 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
416 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
419 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
421 /* reset instance specific fields and methods that don't get inherited */
422 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
423 class->get_property = NULL;
424 class->set_property = NULL;
428 g_object_base_class_finalize (GObjectClass *class)
432 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
434 g_slist_free (class->construct_properties);
435 class->construct_properties = NULL;
436 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
437 for (node = list; node; node = node->next)
439 GParamSpec *pspec = node->data;
441 g_param_spec_pool_remove (pspec_pool, pspec);
442 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
443 g_param_spec_unref (pspec);
449 g_object_notify_dispatcher (GObject *object,
453 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
457 g_object_do_class_init (GObjectClass *class)
459 /* read the comment about typedef struct CArray; on why not to change this quark */
460 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
462 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
463 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
464 pspec_pool = g_param_spec_pool_new (TRUE);
465 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
466 property_notify_context.dispatcher = g_object_notify_dispatcher;
468 class->constructor = g_object_constructor;
469 class->constructed = g_object_constructed;
470 class->set_property = g_object_do_set_property;
471 class->get_property = g_object_do_get_property;
472 class->dispose = g_object_real_dispose;
473 class->finalize = g_object_finalize;
474 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
475 class->notify = NULL;
479 * @gobject: the object which received the signal.
480 * @pspec: the #GParamSpec of the property which changed.
482 * The notify signal is emitted on an object when one of its
483 * properties has been changed. Note that getting this signal
484 * doesn't guarantee that the value of the property has actually
485 * changed, it may also be emitted when the setter for the property
486 * is called to reinstate the previous value.
488 * This signal is typically used to obtain change notification for a
489 * single property, by specifying the property name as a detail in the
490 * g_signal_connect() call, like this:
492 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
493 * G_CALLBACK (gtk_text_view_target_list_notify),
496 * It is important to note that you must use
497 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
498 * detail strings for the notify signal.
500 gobject_signals[NOTIFY] =
501 g_signal_new (g_intern_static_string ("notify"),
502 G_TYPE_FROM_CLASS (class),
503 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
504 G_STRUCT_OFFSET (GObjectClass, notify),
506 g_cclosure_marshal_VOID__PARAM,
510 /* Install a check function that we'll use to verify that classes that
511 * implement an interface implement all properties for that interface
513 g_type_add_interface_check (NULL, object_interface_check_properties);
517 install_property_internal (GType g_type,
521 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
523 g_warning ("When installing property: type `%s' already has a property named `%s'",
524 g_type_name (g_type),
529 g_param_spec_ref_sink (pspec);
530 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
531 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
535 * g_object_class_install_property:
536 * @oclass: a #GObjectClass
537 * @property_id: the id for the new property
538 * @pspec: the #GParamSpec for the new property
540 * Installs a new property. This is usually done in the class initializer.
542 * Note that it is possible to redefine a property in a derived class,
543 * by installing a property with the same name. This can be useful at times,
544 * e.g. to change the range of allowed values or the default value.
547 g_object_class_install_property (GObjectClass *class,
551 g_return_if_fail (G_IS_OBJECT_CLASS (class));
552 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
554 if (CLASS_HAS_DERIVED_CLASS (class))
555 g_error ("Attempt to add property %s::%s to class after it was derived",
556 G_OBJECT_CLASS_NAME (class), pspec->name);
558 class->flags |= CLASS_HAS_PROPS_FLAG;
560 if (pspec->flags & G_PARAM_WRITABLE)
561 g_return_if_fail (class->set_property != NULL);
562 if (pspec->flags & G_PARAM_READABLE)
563 g_return_if_fail (class->get_property != NULL);
564 g_return_if_fail (property_id > 0);
565 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
566 if (pspec->flags & G_PARAM_CONSTRUCT)
567 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
568 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
569 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
571 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
573 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
574 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
576 /* for property overrides of construct properties, we have to get rid
577 * of the overidden inherited construct property
579 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
580 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
581 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
585 * g_object_class_install_properties:
586 * @oclass: a #GObjectClass
587 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
588 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
589 * defining the new properties
591 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
592 * usually done in the class initializer.
594 * The property id of each property is the index of each #GParamSpec in
597 * The property id of 0 is treated specially by #GObject and it should not
598 * be used to store a #GParamSpec.
600 * This function should be used if you plan to use a static array of
601 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
602 * class initialization:
606 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
609 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
612 * my_object_class_init (MyObjectClass *klass)
614 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
616 * obj_properties[PROP_FOO] =
617 * g_param_spec_int ("foo", "Foo", "Foo",
620 * G_PARAM_READWRITE);
622 * obj_properties[PROP_BAR] =
623 * g_param_spec_string ("bar", "Bar", "Bar",
625 * G_PARAM_READWRITE);
627 * gobject_class->set_property = my_object_set_property;
628 * gobject_class->get_property = my_object_get_property;
629 * g_object_class_install_properties (gobject_class,
635 * allows calling g_object_notify_by_pspec() to notify of property changes:
639 * my_object_set_foo (MyObject *self, gint foo)
641 * if (self->foo != foo)
644 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
652 g_object_class_install_properties (GObjectClass *oclass,
656 GType oclass_type, parent_type;
659 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
660 g_return_if_fail (n_pspecs > 1);
661 g_return_if_fail (pspecs[0] == NULL);
663 if (CLASS_HAS_DERIVED_CLASS (oclass))
664 g_error ("Attempt to add properties to %s after it was derived",
665 G_OBJECT_CLASS_NAME (oclass));
667 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
668 parent_type = g_type_parent (oclass_type);
670 /* we skip the first element of the array as it would have a 0 prop_id */
671 for (i = 1; i < n_pspecs; i++)
673 GParamSpec *pspec = pspecs[i];
675 g_return_if_fail (pspec != NULL);
677 if (pspec->flags & G_PARAM_WRITABLE)
678 g_return_if_fail (oclass->set_property != NULL);
679 if (pspec->flags & G_PARAM_READABLE)
680 g_return_if_fail (oclass->get_property != NULL);
681 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
682 if (pspec->flags & G_PARAM_CONSTRUCT)
683 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
684 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
685 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
687 oclass->flags |= CLASS_HAS_PROPS_FLAG;
688 install_property_internal (oclass_type, i, pspec);
690 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
691 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
693 /* for property overrides of construct properties, we have to get rid
694 * of the overidden inherited construct property
696 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
697 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
698 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
703 * g_object_interface_install_property:
704 * @g_iface: any interface vtable for the interface, or the default
705 * vtable for the interface.
706 * @pspec: the #GParamSpec for the new property
708 * Add a property to an interface; this is only useful for interfaces
709 * that are added to GObject-derived types. Adding a property to an
710 * interface forces all objects classes with that interface to have a
711 * compatible property. The compatible property could be a newly
712 * created #GParamSpec, but normally
713 * g_object_class_override_property() will be used so that the object
714 * class only needs to provide an implementation and inherits the
715 * property description, default value, bounds, and so forth from the
716 * interface property.
718 * This function is meant to be called from the interface's default
719 * vtable initialization function (the @class_init member of
720 * #GTypeInfo.) It must not be called after after @class_init has
721 * been called for any object types implementing this interface.
726 g_object_interface_install_property (gpointer g_iface,
729 GTypeInterface *iface_class = g_iface;
731 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
732 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
733 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
734 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
736 install_property_internal (iface_class->g_type, 0, pspec);
740 * g_object_class_find_property:
741 * @oclass: a #GObjectClass
742 * @property_name: the name of the property to look up
744 * Looks up the #GParamSpec for a property of a class.
746 * Returns: (transfer none): the #GParamSpec for the property, or
747 * %NULL if the class doesn't have a property of that name
750 g_object_class_find_property (GObjectClass *class,
751 const gchar *property_name)
754 GParamSpec *redirect;
756 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
757 g_return_val_if_fail (property_name != NULL, NULL);
759 pspec = g_param_spec_pool_lookup (pspec_pool,
761 G_OBJECT_CLASS_TYPE (class),
765 redirect = g_param_spec_get_redirect_target (pspec);
776 * g_object_interface_find_property:
777 * @g_iface: any interface vtable for the interface, or the default
778 * vtable for the interface
779 * @property_name: name of a property to lookup.
781 * Find the #GParamSpec with the given name for an
782 * interface. Generally, the interface vtable passed in as @g_iface
783 * will be the default vtable from g_type_default_interface_ref(), or,
784 * if you know the interface has already been loaded,
785 * g_type_default_interface_peek().
789 * Returns: (transfer none): the #GParamSpec for the property of the
790 * interface with the name @property_name, or %NULL if no
791 * such property exists.
794 g_object_interface_find_property (gpointer g_iface,
795 const gchar *property_name)
797 GTypeInterface *iface_class = g_iface;
799 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
800 g_return_val_if_fail (property_name != NULL, NULL);
802 return g_param_spec_pool_lookup (pspec_pool,
809 * g_object_class_override_property:
810 * @oclass: a #GObjectClass
811 * @property_id: the new property ID
812 * @name: the name of a property registered in a parent class or
813 * in an interface of this class.
815 * Registers @property_id as referring to a property with the
816 * name @name in a parent class or in an interface implemented
817 * by @oclass. This allows this class to <firstterm>override</firstterm>
818 * a property implementation in a parent class or to provide
819 * the implementation of a property from an interface.
822 * Internally, overriding is implemented by creating a property of type
823 * #GParamSpecOverride; generally operations that query the properties of
824 * the object class, such as g_object_class_find_property() or
825 * g_object_class_list_properties() will return the overridden
826 * property. However, in one case, the @construct_properties argument of
827 * the @constructor virtual function, the #GParamSpecOverride is passed
828 * instead, so that the @param_id field of the #GParamSpec will be
829 * correct. For virtually all uses, this makes no difference. If you
830 * need to get the overridden property, you can call
831 * g_param_spec_get_redirect_target().
837 g_object_class_override_property (GObjectClass *oclass,
841 GParamSpec *overridden = NULL;
845 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
846 g_return_if_fail (property_id > 0);
847 g_return_if_fail (name != NULL);
849 /* Find the overridden property; first check parent types
851 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
852 if (parent_type != G_TYPE_NONE)
853 overridden = g_param_spec_pool_lookup (pspec_pool,
862 /* Now check interfaces
864 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
865 while (n_ifaces-- && !overridden)
867 overridden = g_param_spec_pool_lookup (pspec_pool,
878 g_warning ("%s: Can't find property to override for '%s::%s'",
879 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
883 new = g_param_spec_override (name, overridden);
884 g_object_class_install_property (oclass, property_id, new);
888 * g_object_class_list_properties:
889 * @oclass: a #GObjectClass
890 * @n_properties: (out): return location for the length of the returned array
892 * Get an array of #GParamSpec* for all properties of a class.
894 * Returns: (array length=n_properties) (transfer container): an array of
895 * #GParamSpec* which should be freed after use
897 GParamSpec** /* free result */
898 g_object_class_list_properties (GObjectClass *class,
899 guint *n_properties_p)
904 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
906 pspecs = g_param_spec_pool_list (pspec_pool,
907 G_OBJECT_CLASS_TYPE (class),
916 * g_object_interface_list_properties:
917 * @g_iface: any interface vtable for the interface, or the default
918 * vtable for the interface
919 * @n_properties_p: (out): location to store number of properties returned.
921 * Lists the properties of an interface.Generally, the interface
922 * vtable passed in as @g_iface will be the default vtable from
923 * g_type_default_interface_ref(), or, if you know the interface has
924 * already been loaded, g_type_default_interface_peek().
928 * Returns: (array length=n_properties_p) (transfer container): a
929 * pointer to an array of pointers to #GParamSpec
930 * structures. The paramspecs are owned by GLib, but the
931 * array should be freed with g_free() when you are done with
935 g_object_interface_list_properties (gpointer g_iface,
936 guint *n_properties_p)
938 GTypeInterface *iface_class = g_iface;
942 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
944 pspecs = g_param_spec_pool_list (pspec_pool,
954 g_object_init (GObject *object,
957 object->ref_count = 1;
958 object->qdata = NULL;
960 if (CLASS_HAS_PROPS (class))
962 /* freeze object's notification queue, g_object_newv() preserves pairedness */
963 g_object_notify_queue_freeze (object, &property_notify_context);
966 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
968 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
969 G_LOCK (construction_mutex);
970 construction_objects = g_slist_prepend (construction_objects, object);
971 G_UNLOCK (construction_mutex);
974 #ifdef G_ENABLE_DEBUG
977 G_LOCK (debug_objects);
978 debug_objects_count++;
979 g_hash_table_insert (debug_objects_ht, object, object);
980 G_UNLOCK (debug_objects);
982 #endif /* G_ENABLE_DEBUG */
986 g_object_do_set_property (GObject *object,
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1000 g_object_do_get_property (GObject *object,
1005 switch (property_id)
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1014 g_object_real_dispose (GObject *object)
1016 g_signal_handlers_destroy (object);
1017 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1018 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1022 g_object_finalize (GObject *object)
1024 g_datalist_clear (&object->qdata);
1026 #ifdef G_ENABLE_DEBUG
1029 G_LOCK (debug_objects);
1030 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1031 g_hash_table_remove (debug_objects_ht, object);
1032 debug_objects_count--;
1033 G_UNLOCK (debug_objects);
1035 #endif /* G_ENABLE_DEBUG */
1040 g_object_dispatch_properties_changed (GObject *object,
1042 GParamSpec **pspecs)
1046 for (i = 0; i < n_pspecs; i++)
1047 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1051 * g_object_run_dispose:
1052 * @object: a #GObject
1054 * Releases all references to other objects. This can be used to break
1057 * This functions should only be called from object system implementations.
1060 g_object_run_dispose (GObject *object)
1062 g_return_if_fail (G_IS_OBJECT (object));
1063 g_return_if_fail (object->ref_count > 0);
1065 g_object_ref (object);
1066 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1067 G_OBJECT_GET_CLASS (object)->dispose (object);
1068 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1069 g_object_unref (object);
1073 * g_object_freeze_notify:
1074 * @object: a #GObject
1076 * Increases the freeze count on @object. If the freeze count is
1077 * non-zero, the emission of "notify" signals on @object is
1078 * stopped. The signals are queued until the freeze count is decreased
1081 * This is necessary for accessors that modify multiple properties to prevent
1082 * premature notification while the object is still being modified.
1085 g_object_freeze_notify (GObject *object)
1087 g_return_if_fail (G_IS_OBJECT (object));
1089 if (g_atomic_int_get (&object->ref_count) == 0)
1092 g_object_ref (object);
1093 g_object_notify_queue_freeze (object, &property_notify_context);
1094 g_object_unref (object);
1098 g_object_notify_by_spec_internal (GObject *object,
1101 GObjectNotifyQueue *nqueue;
1103 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1104 g_object_notify_queue_add (object, nqueue, pspec);
1105 g_object_notify_queue_thaw (object, nqueue);
1110 * @object: a #GObject
1111 * @property_name: the name of a property installed on the class of @object.
1113 * Emits a "notify" signal for the property @property_name on @object.
1115 * When possible, eg. when signaling a property change from within the class
1116 * that registered the property, you should use g_object_notify_by_pspec()
1120 g_object_notify (GObject *object,
1121 const gchar *property_name)
1125 g_return_if_fail (G_IS_OBJECT (object));
1126 g_return_if_fail (property_name != NULL);
1127 if (g_atomic_int_get (&object->ref_count) == 0)
1130 g_object_ref (object);
1131 /* We don't need to get the redirect target
1132 * (by, e.g. calling g_object_class_find_property())
1133 * because g_object_notify_queue_add() does that
1135 pspec = g_param_spec_pool_lookup (pspec_pool,
1137 G_OBJECT_TYPE (object),
1141 g_warning ("%s: object class `%s' has no property named `%s'",
1143 G_OBJECT_TYPE_NAME (object),
1146 g_object_notify_by_spec_internal (object, pspec);
1147 g_object_unref (object);
1151 * g_object_notify_by_pspec:
1152 * @object: a #GObject
1153 * @pspec: the #GParamSpec of a property installed on the class of @object.
1155 * Emits a "notify" signal for the property specified by @pspec on @object.
1157 * This function omits the property name lookup, hence it is faster than
1158 * g_object_notify().
1160 * One way to avoid using g_object_notify() from within the
1161 * class that registered the properties, and using g_object_notify_by_pspec()
1162 * instead, is to store the GParamSpec used with
1163 * g_object_class_install_property() inside a static array, e.g.:
1173 * static GParamSpec *properties[PROP_LAST];
1176 * my_object_class_init (MyObjectClass *klass)
1178 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1181 * G_PARAM_READWRITE);
1182 * g_object_class_install_property (gobject_class,
1184 * properties[PROP_FOO]);
1188 * and then notify a change on the "foo" property with:
1191 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1197 g_object_notify_by_pspec (GObject *object,
1201 g_return_if_fail (G_IS_OBJECT (object));
1202 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1204 g_object_ref (object);
1205 g_object_notify_by_spec_internal (object, pspec);
1206 g_object_unref (object);
1210 * g_object_thaw_notify:
1211 * @object: a #GObject
1213 * Reverts the effect of a previous call to
1214 * g_object_freeze_notify(). The freeze count is decreased on @object
1215 * and when it reaches zero, all queued "notify" signals are emitted.
1217 * It is an error to call this function when the freeze count is zero.
1220 g_object_thaw_notify (GObject *object)
1222 GObjectNotifyQueue *nqueue;
1224 g_return_if_fail (G_IS_OBJECT (object));
1225 if (g_atomic_int_get (&object->ref_count) == 0)
1228 g_object_ref (object);
1230 /* FIXME: Freezing is the only way to get at the notify queue.
1231 * So we freeze once and then thaw twice.
1233 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1234 g_object_notify_queue_thaw (object, nqueue);
1235 g_object_notify_queue_thaw (object, nqueue);
1237 g_object_unref (object);
1241 object_get_property (GObject *object,
1245 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1246 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1247 GParamSpec *redirect;
1251 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1252 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1256 redirect = g_param_spec_get_redirect_target (pspec);
1260 class->get_property (object, param_id, value, pspec);
1264 object_set_property (GObject *object,
1266 const GValue *value,
1267 GObjectNotifyQueue *nqueue)
1269 GValue tmp_value = G_VALUE_INIT;
1270 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1271 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1272 GParamSpec *redirect;
1273 static const gchar * enable_diagnostic = NULL;
1277 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1278 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1282 redirect = g_param_spec_get_redirect_target (pspec);
1286 if (G_UNLIKELY (!enable_diagnostic))
1288 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1289 if (!enable_diagnostic)
1290 enable_diagnostic = "0";
1293 if (enable_diagnostic[0] == '1')
1295 if (pspec->flags & G_PARAM_DEPRECATED)
1296 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1297 "anymore. It will be removed in a future version.",
1298 G_OBJECT_TYPE_NAME (object), pspec->name);
1301 /* provide a copy to work from, convert (if necessary) and validate */
1302 g_value_init (&tmp_value, pspec->value_type);
1303 if (!g_value_transform (value, &tmp_value))
1304 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1306 g_type_name (pspec->value_type),
1307 G_VALUE_TYPE_NAME (value));
1308 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1310 gchar *contents = g_strdup_value_contents (value);
1312 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1314 G_VALUE_TYPE_NAME (value),
1316 g_type_name (pspec->value_type));
1321 class->set_property (object, param_id, &tmp_value, pspec);
1322 g_object_notify_queue_add (object, nqueue, pspec);
1324 g_value_unset (&tmp_value);
1328 object_interface_check_properties (gpointer func_data,
1331 GTypeInterface *iface_class = g_iface;
1332 GObjectClass *class;
1333 GType iface_type = iface_class->g_type;
1334 GParamSpec **pspecs;
1337 class = g_type_class_ref (iface_class->g_instance_type);
1339 if (!G_IS_OBJECT_CLASS (class))
1342 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1346 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1348 G_OBJECT_CLASS_TYPE (class),
1353 g_critical ("Object class %s doesn't implement property "
1354 "'%s' from interface '%s'",
1355 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1357 g_type_name (iface_type));
1362 /* The implementation paramspec must have a less restrictive
1363 * type than the interface parameter spec for set() and a
1364 * more restrictive type for get(). We just require equality,
1365 * rather than doing something more complicated checking
1366 * the READABLE and WRITABLE flags. We also simplify here
1367 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1370 !g_type_is_a (pspecs[n]->value_type,
1371 class_pspec->value_type))
1373 g_critical ("Property '%s' on class '%s' has type '%s' "
1374 "which is different from the type '%s', "
1375 "of the property on interface '%s'\n",
1377 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1378 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1379 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1380 g_type_name (iface_type));
1383 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1385 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1386 * READABLE and WRITABLE remove restrictions. The implementation
1387 * paramspec must have less restrictive flags.
1390 (!SUBSET (class_pspec->flags,
1392 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1393 !SUBSET (pspecs[n]->flags,
1395 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1397 g_critical ("Flags for property '%s' on class '%s' "
1398 "are not compatible with the property on"
1401 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1402 g_type_name (iface_type));
1409 g_type_class_unref (class);
1413 g_object_get_type (void)
1415 return G_TYPE_OBJECT;
1419 * g_object_new: (skip)
1420 * @object_type: the type id of the #GObject subtype to instantiate
1421 * @first_property_name: the name of the first property
1422 * @...: the value of the first property, followed optionally by more
1423 * name/value pairs, followed by %NULL
1425 * Creates a new instance of a #GObject subtype and sets its properties.
1427 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1428 * which are not explicitly specified are set to their default values.
1430 * Returns: (transfer full): a new instance of @object_type
1433 g_object_new (GType object_type,
1434 const gchar *first_property_name,
1440 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1442 /* short circuit for calls supplying no properties */
1443 if (!first_property_name)
1444 return g_object_newv (object_type, 0, NULL);
1446 va_start (var_args, first_property_name);
1447 object = g_object_new_valist (object_type, first_property_name, var_args);
1454 slist_maybe_remove (GSList **slist,
1457 GSList *last = NULL, *node = *slist;
1460 if (node->data == data)
1463 last->next = node->next;
1465 *slist = node->next;
1466 g_slist_free_1 (node);
1475 static inline gboolean
1476 object_in_construction_list (GObject *object)
1478 gboolean in_construction;
1479 G_LOCK (construction_mutex);
1480 in_construction = g_slist_find (construction_objects, object) != NULL;
1481 G_UNLOCK (construction_mutex);
1482 return in_construction;
1487 * @object_type: the type id of the #GObject subtype to instantiate
1488 * @n_parameters: the length of the @parameters array
1489 * @parameters: (array length=n_parameters): an array of #GParameter
1491 * Creates a new instance of a #GObject subtype and sets its properties.
1493 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1494 * which are not explicitly specified are set to their default values.
1496 * Rename to: g_object_new
1497 * Returns: (type GObject.Object) (transfer full): a new instance of
1501 g_object_newv (GType object_type,
1503 GParameter *parameters)
1505 GObjectConstructParam *cparams = NULL, *oparams;
1506 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1508 GObjectClass *class, *unref_class = NULL;
1510 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1512 GList *clist = NULL;
1513 gboolean newly_constructed;
1516 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1518 class = g_type_class_peek_static (object_type);
1520 class = unref_class = g_type_class_ref (object_type);
1521 for (slist = class->construct_properties; slist; slist = slist->next)
1523 clist = g_list_prepend (clist, slist->data);
1524 n_total_cparams += 1;
1527 if (n_parameters == 0 && n_total_cparams == 0)
1529 /* This is a simple object with no construct properties, and
1530 * no properties are being set, so short circuit the parameter
1531 * handling. This speeds up simple object construction.
1534 object = class->constructor (object_type, 0, NULL);
1535 goto did_construction;
1538 /* collect parameters, sort into construction and normal ones */
1539 oparams = g_new (GObjectConstructParam, n_parameters);
1540 cparams = g_new (GObjectConstructParam, n_total_cparams);
1541 for (i = 0; i < n_parameters; i++)
1543 GValue *value = ¶meters[i].value;
1544 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1550 g_warning ("%s: object class `%s' has no property named `%s'",
1552 g_type_name (object_type),
1553 parameters[i].name);
1556 if (!(pspec->flags & G_PARAM_WRITABLE))
1558 g_warning ("%s: property `%s' of object class `%s' is not writable",
1561 g_type_name (object_type));
1564 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1566 GList *list = g_list_find (clist, pspec);
1570 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1571 G_STRFUNC, pspec->name, g_type_name (object_type));
1574 cparams[n_cparams].pspec = pspec;
1575 cparams[n_cparams].value = value;
1580 list->prev->next = list->next;
1582 list->next->prev = list->prev;
1583 g_list_free_1 (list);
1587 oparams[n_oparams].pspec = pspec;
1588 oparams[n_oparams].value = value;
1593 /* set remaining construction properties to default values */
1594 n_cvalues = n_total_cparams - n_cparams;
1595 cvalues = g_new (GValue, n_cvalues);
1598 GList *tmp = clist->next;
1599 GParamSpec *pspec = clist->data;
1600 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1603 g_value_init (value, pspec->value_type);
1604 g_param_value_set_default (pspec, value);
1606 cparams[n_cparams].pspec = pspec;
1607 cparams[n_cparams].value = value;
1610 g_list_free_1 (clist);
1614 /* construct object from construction parameters */
1615 object = class->constructor (object_type, n_total_cparams, cparams);
1616 /* free construction values */
1619 g_value_unset (cvalues + n_cvalues);
1623 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1625 /* adjust freeze_count according to g_object_init() and remaining properties */
1626 G_LOCK (construction_mutex);
1627 newly_constructed = slist_maybe_remove (&construction_objects, object);
1628 G_UNLOCK (construction_mutex);
1631 newly_constructed = TRUE;
1633 if (CLASS_HAS_PROPS (class))
1635 if (newly_constructed || n_oparams)
1636 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1637 if (newly_constructed)
1638 g_object_notify_queue_thaw (object, nqueue);
1641 /* run 'constructed' handler if there is a custom one */
1642 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1643 class->constructed (object);
1645 /* set remaining properties */
1646 for (i = 0; i < n_oparams; i++)
1647 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1650 if (CLASS_HAS_PROPS (class))
1652 /* release our own freeze count and handle notifications */
1653 if (newly_constructed || n_oparams)
1654 g_object_notify_queue_thaw (object, nqueue);
1658 g_type_class_unref (unref_class);
1664 * g_object_new_valist: (skip)
1665 * @object_type: the type id of the #GObject subtype to instantiate
1666 * @first_property_name: the name of the first property
1667 * @var_args: the value of the first property, followed optionally by more
1668 * name/value pairs, followed by %NULL
1670 * Creates a new instance of a #GObject subtype and sets its properties.
1672 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1673 * which are not explicitly specified are set to their default values.
1675 * Returns: a new instance of @object_type
1678 g_object_new_valist (GType object_type,
1679 const gchar *first_property_name,
1682 GObjectClass *class;
1686 guint n_params = 0, n_alloced_params = 16;
1688 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1690 if (!first_property_name)
1691 return g_object_newv (object_type, 0, NULL);
1693 class = g_type_class_ref (object_type);
1695 params = g_new0 (GParameter, n_alloced_params);
1696 name = first_property_name;
1699 gchar *error = NULL;
1700 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1706 g_warning ("%s: object class `%s' has no property named `%s'",
1708 g_type_name (object_type),
1712 if (n_params >= n_alloced_params)
1714 n_alloced_params += 16;
1715 params = g_renew (GParameter, params, n_alloced_params);
1716 memset (params + n_params, 0, 16 * (sizeof *params));
1718 params[n_params].name = name;
1719 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1720 var_args, 0, &error);
1723 g_warning ("%s: %s", G_STRFUNC, error);
1725 g_value_unset (¶ms[n_params].value);
1729 name = va_arg (var_args, gchar*);
1732 object = g_object_newv (object_type, n_params, params);
1735 g_value_unset (¶ms[n_params].value);
1738 g_type_class_unref (class);
1744 g_object_constructor (GType type,
1745 guint n_construct_properties,
1746 GObjectConstructParam *construct_params)
1751 object = (GObject*) g_type_create_instance (type);
1753 /* set construction parameters */
1754 if (n_construct_properties)
1756 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1758 /* set construct properties */
1759 while (n_construct_properties--)
1761 GValue *value = construct_params->value;
1762 GParamSpec *pspec = construct_params->pspec;
1765 object_set_property (object, pspec, value, nqueue);
1767 g_object_notify_queue_thaw (object, nqueue);
1768 /* the notification queue is still frozen from g_object_init(), so
1769 * we don't need to handle it here, g_object_newv() takes
1778 g_object_constructed (GObject *object)
1780 /* empty default impl to allow unconditional upchaining */
1784 * g_object_set_valist: (skip)
1785 * @object: a #GObject
1786 * @first_property_name: name of the first property to set
1787 * @var_args: value for the first property, followed optionally by more
1788 * name/value pairs, followed by %NULL
1790 * Sets properties on an object.
1793 g_object_set_valist (GObject *object,
1794 const gchar *first_property_name,
1797 GObjectNotifyQueue *nqueue;
1800 g_return_if_fail (G_IS_OBJECT (object));
1802 g_object_ref (object);
1803 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1805 name = first_property_name;
1808 GValue value = G_VALUE_INIT;
1810 gchar *error = NULL;
1812 pspec = g_param_spec_pool_lookup (pspec_pool,
1814 G_OBJECT_TYPE (object),
1818 g_warning ("%s: object class `%s' has no property named `%s'",
1820 G_OBJECT_TYPE_NAME (object),
1824 if (!(pspec->flags & G_PARAM_WRITABLE))
1826 g_warning ("%s: property `%s' of object class `%s' is not writable",
1829 G_OBJECT_TYPE_NAME (object));
1832 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1834 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1835 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1839 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1843 g_warning ("%s: %s", G_STRFUNC, error);
1845 g_value_unset (&value);
1849 object_set_property (object, pspec, &value, nqueue);
1850 g_value_unset (&value);
1852 name = va_arg (var_args, gchar*);
1855 g_object_notify_queue_thaw (object, nqueue);
1856 g_object_unref (object);
1860 * g_object_get_valist: (skip)
1861 * @object: a #GObject
1862 * @first_property_name: name of the first property to get
1863 * @var_args: return location for the first property, followed optionally by more
1864 * name/return location pairs, followed by %NULL
1866 * Gets properties of an object.
1868 * In general, a copy is made of the property contents and the caller
1869 * is responsible for freeing the memory in the appropriate manner for
1870 * the type, for instance by calling g_free() or g_object_unref().
1872 * See g_object_get().
1875 g_object_get_valist (GObject *object,
1876 const gchar *first_property_name,
1881 g_return_if_fail (G_IS_OBJECT (object));
1883 g_object_ref (object);
1885 name = first_property_name;
1889 GValue value = G_VALUE_INIT;
1893 pspec = g_param_spec_pool_lookup (pspec_pool,
1895 G_OBJECT_TYPE (object),
1899 g_warning ("%s: object class `%s' has no property named `%s'",
1901 G_OBJECT_TYPE_NAME (object),
1905 if (!(pspec->flags & G_PARAM_READABLE))
1907 g_warning ("%s: property `%s' of object class `%s' is not readable",
1910 G_OBJECT_TYPE_NAME (object));
1914 g_value_init (&value, pspec->value_type);
1916 object_get_property (object, pspec, &value);
1918 G_VALUE_LCOPY (&value, var_args, 0, &error);
1921 g_warning ("%s: %s", G_STRFUNC, error);
1923 g_value_unset (&value);
1927 g_value_unset (&value);
1929 name = va_arg (var_args, gchar*);
1932 g_object_unref (object);
1936 * g_object_set: (skip)
1937 * @object: a #GObject
1938 * @first_property_name: name of the first property to set
1939 * @...: value for the first property, followed optionally by more
1940 * name/value pairs, followed by %NULL
1942 * Sets properties on an object.
1945 g_object_set (gpointer _object,
1946 const gchar *first_property_name,
1949 GObject *object = _object;
1952 g_return_if_fail (G_IS_OBJECT (object));
1954 va_start (var_args, first_property_name);
1955 g_object_set_valist (object, first_property_name, var_args);
1960 * g_object_get: (skip)
1961 * @object: a #GObject
1962 * @first_property_name: name of the first property to get
1963 * @...: return location for the first property, followed optionally by more
1964 * name/return location pairs, followed by %NULL
1966 * Gets properties of an object.
1968 * In general, a copy is made of the property contents and the caller
1969 * is responsible for freeing the memory in the appropriate manner for
1970 * the type, for instance by calling g_free() or g_object_unref().
1973 * <title>Using g_object_get(<!-- -->)</title>
1974 * An example of using g_object_get() to get the contents
1975 * of three properties - one of type #G_TYPE_INT,
1976 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1982 * g_object_get (my_object,
1983 * "int-property", &intval,
1984 * "str-property", &strval,
1985 * "obj-property", &objval,
1988 * // Do something with intval, strval, objval
1991 * g_object_unref (objval);
1996 g_object_get (gpointer _object,
1997 const gchar *first_property_name,
2000 GObject *object = _object;
2003 g_return_if_fail (G_IS_OBJECT (object));
2005 va_start (var_args, first_property_name);
2006 g_object_get_valist (object, first_property_name, var_args);
2011 * g_object_set_property:
2012 * @object: a #GObject
2013 * @property_name: the name of the property to set
2016 * Sets a property on an object.
2019 g_object_set_property (GObject *object,
2020 const gchar *property_name,
2021 const GValue *value)
2023 GObjectNotifyQueue *nqueue;
2026 g_return_if_fail (G_IS_OBJECT (object));
2027 g_return_if_fail (property_name != NULL);
2028 g_return_if_fail (G_IS_VALUE (value));
2030 g_object_ref (object);
2031 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
2033 pspec = g_param_spec_pool_lookup (pspec_pool,
2035 G_OBJECT_TYPE (object),
2038 g_warning ("%s: object class `%s' has no property named `%s'",
2040 G_OBJECT_TYPE_NAME (object),
2042 else if (!(pspec->flags & G_PARAM_WRITABLE))
2043 g_warning ("%s: property `%s' of object class `%s' is not writable",
2046 G_OBJECT_TYPE_NAME (object));
2047 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2048 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2049 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2051 object_set_property (object, pspec, value, nqueue);
2053 g_object_notify_queue_thaw (object, nqueue);
2054 g_object_unref (object);
2058 * g_object_get_property:
2059 * @object: a #GObject
2060 * @property_name: the name of the property to get
2061 * @value: return location for the property value
2063 * Gets a property of an object. @value must have been initialized to the
2064 * expected type of the property (or a type to which the expected type can be
2065 * transformed) using g_value_init().
2067 * In general, a copy is made of the property contents and the caller is
2068 * responsible for freeing the memory by calling g_value_unset().
2070 * Note that g_object_get_property() is really intended for language
2071 * bindings, g_object_get() is much more convenient for C programming.
2074 g_object_get_property (GObject *object,
2075 const gchar *property_name,
2080 g_return_if_fail (G_IS_OBJECT (object));
2081 g_return_if_fail (property_name != NULL);
2082 g_return_if_fail (G_IS_VALUE (value));
2084 g_object_ref (object);
2086 pspec = g_param_spec_pool_lookup (pspec_pool,
2088 G_OBJECT_TYPE (object),
2091 g_warning ("%s: object class `%s' has no property named `%s'",
2093 G_OBJECT_TYPE_NAME (object),
2095 else if (!(pspec->flags & G_PARAM_READABLE))
2096 g_warning ("%s: property `%s' of object class `%s' is not readable",
2099 G_OBJECT_TYPE_NAME (object));
2102 GValue *prop_value, tmp_value = G_VALUE_INIT;
2104 /* auto-conversion of the callers value type
2106 if (G_VALUE_TYPE (value) == pspec->value_type)
2108 g_value_reset (value);
2111 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2113 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2114 G_STRFUNC, pspec->name,
2115 g_type_name (pspec->value_type),
2116 G_VALUE_TYPE_NAME (value));
2117 g_object_unref (object);
2122 g_value_init (&tmp_value, pspec->value_type);
2123 prop_value = &tmp_value;
2125 object_get_property (object, pspec, prop_value);
2126 if (prop_value != value)
2128 g_value_transform (prop_value, value);
2129 g_value_unset (&tmp_value);
2133 g_object_unref (object);
2137 * g_object_connect: (skip)
2138 * @object: a #GObject
2139 * @signal_spec: the spec for the first signal
2140 * @...: #GCallback for the first signal, followed by data for the
2141 * first signal, followed optionally by more signal
2142 * spec/callback/data triples, followed by %NULL
2144 * A convenience function to connect multiple signals at once.
2146 * The signal specs expected by this function have the form
2147 * "modifier::signal_name", where modifier can be one of the following:
2150 * <term>signal</term>
2152 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2153 * </para></listitem>
2156 * <term>object_signal</term>
2157 * <term>object-signal</term>
2159 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2160 * </para></listitem>
2163 * <term>swapped_signal</term>
2164 * <term>swapped-signal</term>
2166 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2167 * </para></listitem>
2170 * <term>swapped_object_signal</term>
2171 * <term>swapped-object-signal</term>
2173 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2174 * </para></listitem>
2177 * <term>signal_after</term>
2178 * <term>signal-after</term>
2180 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2181 * </para></listitem>
2184 * <term>object_signal_after</term>
2185 * <term>object-signal-after</term>
2187 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2188 * </para></listitem>
2191 * <term>swapped_signal_after</term>
2192 * <term>swapped-signal-after</term>
2194 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2195 * </para></listitem>
2198 * <term>swapped_object_signal_after</term>
2199 * <term>swapped-object-signal-after</term>
2201 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2202 * </para></listitem>
2207 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2208 * "type", GTK_WINDOW_POPUP,
2211 * "signal::event", gtk_menu_window_event, menu,
2212 * "signal::size_request", gtk_menu_window_size_request, menu,
2213 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2217 * Returns: (transfer none): @object
2220 g_object_connect (gpointer _object,
2221 const gchar *signal_spec,
2224 GObject *object = _object;
2227 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2228 g_return_val_if_fail (object->ref_count > 0, object);
2230 va_start (var_args, signal_spec);
2233 GCallback callback = va_arg (var_args, GCallback);
2234 gpointer data = va_arg (var_args, gpointer);
2236 if (strncmp (signal_spec, "signal::", 8) == 0)
2237 g_signal_connect_data (object, signal_spec + 8,
2238 callback, data, NULL,
2240 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2241 strncmp (signal_spec, "object-signal::", 15) == 0)
2242 g_signal_connect_object (object, signal_spec + 15,
2245 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2246 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2247 g_signal_connect_data (object, signal_spec + 16,
2248 callback, data, NULL,
2250 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2251 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2252 g_signal_connect_object (object, signal_spec + 23,
2255 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2256 strncmp (signal_spec, "signal-after::", 14) == 0)
2257 g_signal_connect_data (object, signal_spec + 14,
2258 callback, data, NULL,
2260 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2261 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2262 g_signal_connect_object (object, signal_spec + 21,
2265 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2266 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2267 g_signal_connect_data (object, signal_spec + 22,
2268 callback, data, NULL,
2269 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2270 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2271 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2272 g_signal_connect_object (object, signal_spec + 29,
2274 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2277 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2280 signal_spec = va_arg (var_args, gchar*);
2288 * g_object_disconnect: (skip)
2289 * @object: a #GObject
2290 * @signal_spec: the spec for the first signal
2291 * @...: #GCallback for the first signal, followed by data for the first signal,
2292 * followed optionally by more signal spec/callback/data triples,
2295 * A convenience function to disconnect multiple signals at once.
2297 * The signal specs expected by this function have the form
2298 * "any_signal", which means to disconnect any signal with matching
2299 * callback and data, or "any_signal::signal_name", which only
2300 * disconnects the signal named "signal_name".
2303 g_object_disconnect (gpointer _object,
2304 const gchar *signal_spec,
2307 GObject *object = _object;
2310 g_return_if_fail (G_IS_OBJECT (object));
2311 g_return_if_fail (object->ref_count > 0);
2313 va_start (var_args, signal_spec);
2316 GCallback callback = va_arg (var_args, GCallback);
2317 gpointer data = va_arg (var_args, gpointer);
2318 guint sid = 0, detail = 0, mask = 0;
2320 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2321 strncmp (signal_spec, "any-signal::", 12) == 0)
2324 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2326 else if (strcmp (signal_spec, "any_signal") == 0 ||
2327 strcmp (signal_spec, "any-signal") == 0)
2330 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2334 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2338 if ((mask & G_SIGNAL_MATCH_ID) &&
2339 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2340 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2341 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2343 NULL, (gpointer)callback, data))
2344 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2345 signal_spec = va_arg (var_args, gchar*);
2356 } weak_refs[1]; /* flexible array */
2360 weak_refs_notify (gpointer data)
2362 WeakRefStack *wstack = data;
2365 for (i = 0; i < wstack->n_weak_refs; i++)
2366 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2371 * g_object_weak_ref: (skip)
2372 * @object: #GObject to reference weakly
2373 * @notify: callback to invoke before the object is freed
2374 * @data: extra data to pass to notify
2376 * Adds a weak reference callback to an object. Weak references are
2377 * used for notification when an object is finalized. They are called
2378 * "weak references" because they allow you to safely hold a pointer
2379 * to an object without calling g_object_ref() (g_object_ref() adds a
2380 * strong reference, that is, forces the object to stay alive).
2383 g_object_weak_ref (GObject *object,
2387 WeakRefStack *wstack;
2390 g_return_if_fail (G_IS_OBJECT (object));
2391 g_return_if_fail (notify != NULL);
2392 g_return_if_fail (object->ref_count >= 1);
2394 G_LOCK (weak_refs_mutex);
2395 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2398 i = wstack->n_weak_refs++;
2399 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2403 wstack = g_renew (WeakRefStack, NULL, 1);
2404 wstack->object = object;
2405 wstack->n_weak_refs = 1;
2408 wstack->weak_refs[i].notify = notify;
2409 wstack->weak_refs[i].data = data;
2410 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2411 G_UNLOCK (weak_refs_mutex);
2415 * g_object_weak_unref: (skip)
2416 * @object: #GObject to remove a weak reference from
2417 * @notify: callback to search for
2418 * @data: data to search for
2420 * Removes a weak reference callback to an object.
2423 g_object_weak_unref (GObject *object,
2427 WeakRefStack *wstack;
2428 gboolean found_one = FALSE;
2430 g_return_if_fail (G_IS_OBJECT (object));
2431 g_return_if_fail (notify != NULL);
2433 G_LOCK (weak_refs_mutex);
2434 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2439 for (i = 0; i < wstack->n_weak_refs; i++)
2440 if (wstack->weak_refs[i].notify == notify &&
2441 wstack->weak_refs[i].data == data)
2444 wstack->n_weak_refs -= 1;
2445 if (i != wstack->n_weak_refs)
2446 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2451 G_UNLOCK (weak_refs_mutex);
2453 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2457 * g_object_add_weak_pointer: (skip)
2458 * @object: The object that should be weak referenced.
2459 * @weak_pointer_location: (inout): The memory address of a pointer.
2461 * Adds a weak reference from weak_pointer to @object to indicate that
2462 * the pointer located at @weak_pointer_location is only valid during
2463 * the lifetime of @object. When the @object is finalized,
2464 * @weak_pointer will be set to %NULL.
2467 g_object_add_weak_pointer (GObject *object,
2468 gpointer *weak_pointer_location)
2470 g_return_if_fail (G_IS_OBJECT (object));
2471 g_return_if_fail (weak_pointer_location != NULL);
2473 g_object_weak_ref (object,
2474 (GWeakNotify) g_nullify_pointer,
2475 weak_pointer_location);
2479 * g_object_remove_weak_pointer: (skip)
2480 * @object: The object that is weak referenced.
2481 * @weak_pointer_location: (inout): The memory address of a pointer.
2483 * Removes a weak reference from @object that was previously added
2484 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2485 * to match the one used with g_object_add_weak_pointer().
2488 g_object_remove_weak_pointer (GObject *object,
2489 gpointer *weak_pointer_location)
2491 g_return_if_fail (G_IS_OBJECT (object));
2492 g_return_if_fail (weak_pointer_location != NULL);
2494 g_object_weak_unref (object,
2495 (GWeakNotify) g_nullify_pointer,
2496 weak_pointer_location);
2500 object_floating_flag_handler (GObject *object,
2506 case +1: /* force floating if possible */
2508 oldvalue = g_atomic_pointer_get (&object->qdata);
2509 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2510 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2511 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2512 case -1: /* sink if possible */
2514 oldvalue = g_atomic_pointer_get (&object->qdata);
2515 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2516 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2517 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2518 default: /* check floating */
2519 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2524 * g_object_is_floating:
2525 * @object: (type GObject.Object): a #GObject
2527 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2532 * Returns: %TRUE if @object has a floating reference
2535 g_object_is_floating (gpointer _object)
2537 GObject *object = _object;
2538 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2539 return floating_flag_handler (object, 0);
2543 * g_object_ref_sink:
2544 * @object: (type GObject.Object): a #GObject
2546 * Increase the reference count of @object, and possibly remove the
2547 * <link linkend="floating-ref">floating</link> reference, if @object
2548 * has a floating reference.
2550 * In other words, if the object is floating, then this call "assumes
2551 * ownership" of the floating reference, converting it to a normal
2552 * reference by clearing the floating flag while leaving the reference
2553 * count unchanged. If the object is not floating, then this call
2554 * adds a new normal reference increasing the reference count by one.
2558 * Returns: (type GObject.Object) (transfer none): @object
2561 g_object_ref_sink (gpointer _object)
2563 GObject *object = _object;
2564 gboolean was_floating;
2565 g_return_val_if_fail (G_IS_OBJECT (object), object);
2566 g_return_val_if_fail (object->ref_count >= 1, object);
2567 g_object_ref (object);
2568 was_floating = floating_flag_handler (object, -1);
2570 g_object_unref (object);
2575 * g_object_force_floating:
2576 * @object: a #GObject
2578 * This function is intended for #GObject implementations to re-enforce a
2579 * <link linkend="floating-ref">floating</link> object reference.
2580 * Doing this is seldom required: all
2581 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2582 * usually just needs to be sunken by calling g_object_ref_sink().
2587 g_object_force_floating (GObject *object)
2589 g_return_if_fail (G_IS_OBJECT (object));
2590 g_return_if_fail (object->ref_count >= 1);
2592 floating_flag_handler (object, +1);
2597 guint n_toggle_refs;
2599 GToggleNotify notify;
2601 } toggle_refs[1]; /* flexible array */
2605 toggle_refs_notify (GObject *object,
2606 gboolean is_last_ref)
2608 ToggleRefStack tstack, *tstackptr;
2610 G_LOCK (toggle_refs_mutex);
2611 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2612 tstack = *tstackptr;
2613 G_UNLOCK (toggle_refs_mutex);
2615 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2616 * will only be notified when there is exactly one of them.
2618 g_assert (tstack.n_toggle_refs == 1);
2619 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2623 * g_object_add_toggle_ref: (skip)
2624 * @object: a #GObject
2625 * @notify: a function to call when this reference is the
2626 * last reference to the object, or is no longer
2627 * the last reference.
2628 * @data: data to pass to @notify
2630 * Increases the reference count of the object by one and sets a
2631 * callback to be called when all other references to the object are
2632 * dropped, or when this is already the last reference to the object
2633 * and another reference is established.
2635 * This functionality is intended for binding @object to a proxy
2636 * object managed by another memory manager. This is done with two
2637 * paired references: the strong reference added by
2638 * g_object_add_toggle_ref() and a reverse reference to the proxy
2639 * object which is either a strong reference or weak reference.
2641 * The setup is that when there are no other references to @object,
2642 * only a weak reference is held in the reverse direction from @object
2643 * to the proxy object, but when there are other references held to
2644 * @object, a strong reference is held. The @notify callback is called
2645 * when the reference from @object to the proxy object should be
2646 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2647 * true) or weak to strong (@is_last_ref false).
2649 * Since a (normal) reference must be held to the object before
2650 * calling g_object_add_toggle_ref(), the initial state of the reverse
2651 * link is always strong.
2653 * Multiple toggle references may be added to the same gobject,
2654 * however if there are multiple toggle references to an object, none
2655 * of them will ever be notified until all but one are removed. For
2656 * this reason, you should only ever use a toggle reference if there
2657 * is important state in the proxy object.
2662 g_object_add_toggle_ref (GObject *object,
2663 GToggleNotify notify,
2666 ToggleRefStack *tstack;
2669 g_return_if_fail (G_IS_OBJECT (object));
2670 g_return_if_fail (notify != NULL);
2671 g_return_if_fail (object->ref_count >= 1);
2673 g_object_ref (object);
2675 G_LOCK (toggle_refs_mutex);
2676 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2679 i = tstack->n_toggle_refs++;
2680 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2681 * in tstate->toggle_refs */
2682 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2686 tstack = g_renew (ToggleRefStack, NULL, 1);
2687 tstack->object = object;
2688 tstack->n_toggle_refs = 1;
2692 /* Set a flag for fast lookup after adding the first toggle reference */
2693 if (tstack->n_toggle_refs == 1)
2694 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2696 tstack->toggle_refs[i].notify = notify;
2697 tstack->toggle_refs[i].data = data;
2698 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2699 (GDestroyNotify)g_free);
2700 G_UNLOCK (toggle_refs_mutex);
2704 * g_object_remove_toggle_ref: (skip)
2705 * @object: a #GObject
2706 * @notify: a function to call when this reference is the
2707 * last reference to the object, or is no longer
2708 * the last reference.
2709 * @data: data to pass to @notify
2711 * Removes a reference added with g_object_add_toggle_ref(). The
2712 * reference count of the object is decreased by one.
2717 g_object_remove_toggle_ref (GObject *object,
2718 GToggleNotify notify,
2721 ToggleRefStack *tstack;
2722 gboolean found_one = FALSE;
2724 g_return_if_fail (G_IS_OBJECT (object));
2725 g_return_if_fail (notify != NULL);
2727 G_LOCK (toggle_refs_mutex);
2728 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2733 for (i = 0; i < tstack->n_toggle_refs; i++)
2734 if (tstack->toggle_refs[i].notify == notify &&
2735 tstack->toggle_refs[i].data == data)
2738 tstack->n_toggle_refs -= 1;
2739 if (i != tstack->n_toggle_refs)
2740 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2742 if (tstack->n_toggle_refs == 0)
2743 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2748 G_UNLOCK (toggle_refs_mutex);
2751 g_object_unref (object);
2753 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2758 * @object: (type GObject.Object): a #GObject
2760 * Increases the reference count of @object.
2762 * Returns: (type GObject.Object) (transfer none): the same @object
2765 g_object_ref (gpointer _object)
2767 GObject *object = _object;
2770 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2771 g_return_val_if_fail (object->ref_count > 0, NULL);
2773 #ifdef G_ENABLE_DEBUG
2774 if (g_trap_object_ref == object)
2776 #endif /* G_ENABLE_DEBUG */
2779 old_val = g_atomic_int_add (&object->ref_count, 1);
2781 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2782 toggle_refs_notify (object, FALSE);
2784 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2791 * @object: (type GObject.Object): a #GObject
2793 * Decreases the reference count of @object. When its reference count
2794 * drops to 0, the object is finalized (i.e. its memory is freed).
2797 g_object_unref (gpointer _object)
2799 GObject *object = _object;
2802 g_return_if_fail (G_IS_OBJECT (object));
2803 g_return_if_fail (object->ref_count > 0);
2805 #ifdef G_ENABLE_DEBUG
2806 if (g_trap_object_ref == object)
2808 #endif /* G_ENABLE_DEBUG */
2810 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2811 retry_atomic_decrement1:
2812 old_ref = g_atomic_int_get (&object->ref_count);
2815 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2816 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2818 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2819 goto retry_atomic_decrement1;
2821 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2823 /* if we went from 2->1 we need to notify toggle refs if any */
2824 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2825 toggle_refs_notify (object, TRUE);
2829 /* we are about tp remove the last reference */
2830 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2831 G_OBJECT_GET_CLASS (object)->dispose (object);
2832 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2834 /* may have been re-referenced meanwhile */
2835 retry_atomic_decrement2:
2836 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2839 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2840 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2842 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2843 goto retry_atomic_decrement2;
2845 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2847 /* if we went from 2->1 we need to notify toggle refs if any */
2848 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2849 toggle_refs_notify (object, TRUE);
2854 /* we are still in the process of taking away the last ref */
2855 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2856 g_signal_handlers_destroy (object);
2857 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2859 /* decrement the last reference */
2860 old_ref = g_atomic_int_add (&object->ref_count, -1);
2862 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2864 /* may have been re-referenced meanwhile */
2865 if (G_LIKELY (old_ref == 1))
2867 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2868 G_OBJECT_GET_CLASS (object)->finalize (object);
2870 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2872 #ifdef G_ENABLE_DEBUG
2875 /* catch objects not chaining finalize handlers */
2876 G_LOCK (debug_objects);
2877 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2878 G_UNLOCK (debug_objects);
2880 #endif /* G_ENABLE_DEBUG */
2881 g_type_free_instance ((GTypeInstance*) object);
2887 * g_clear_object: (skip)
2888 * @object_ptr: a pointer to a #GObject reference
2890 * Clears a reference to a #GObject.
2892 * @object_ptr must not be %NULL.
2894 * If the reference is %NULL then this function does nothing.
2895 * Otherwise, the reference count of the object is decreased and the
2896 * pointer is set to %NULL.
2898 * This function is threadsafe and modifies the pointer atomically,
2899 * using memory barriers where needed.
2901 * A macro is also included that allows this function to be used without
2906 #undef g_clear_object
2908 g_clear_object (volatile GObject **object_ptr)
2910 gpointer *ptr = (gpointer) object_ptr;
2913 /* This is a little frustrating.
2914 * Would be nice to have an atomic exchange (with no compare).
2917 old = g_atomic_pointer_get (ptr);
2918 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2921 g_object_unref (old);
2925 * g_object_get_qdata:
2926 * @object: The GObject to get a stored user data pointer from
2927 * @quark: A #GQuark, naming the user data pointer
2929 * This function gets back user data pointers stored via
2930 * g_object_set_qdata().
2932 * Returns: (transfer none): The user data pointer set, or %NULL
2935 g_object_get_qdata (GObject *object,
2938 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2940 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2944 * g_object_set_qdata: (skip)
2945 * @object: The GObject to set store a user data pointer
2946 * @quark: A #GQuark, naming the user data pointer
2947 * @data: An opaque user data pointer
2949 * This sets an opaque, named pointer on an object.
2950 * The name is specified through a #GQuark (retrived e.g. via
2951 * g_quark_from_static_string()), and the pointer
2952 * can be gotten back from the @object with g_object_get_qdata()
2953 * until the @object is finalized.
2954 * Setting a previously set user data pointer, overrides (frees)
2955 * the old pointer set, using #NULL as pointer essentially
2956 * removes the data stored.
2959 g_object_set_qdata (GObject *object,
2963 g_return_if_fail (G_IS_OBJECT (object));
2964 g_return_if_fail (quark > 0);
2966 g_datalist_id_set_data (&object->qdata, quark, data);
2970 * g_object_set_qdata_full: (skip)
2971 * @object: The GObject to set store a user data pointer
2972 * @quark: A #GQuark, naming the user data pointer
2973 * @data: An opaque user data pointer
2974 * @destroy: Function to invoke with @data as argument, when @data
2977 * This function works like g_object_set_qdata(), but in addition,
2978 * a void (*destroy) (gpointer) function may be specified which is
2979 * called with @data as argument when the @object is finalized, or
2980 * the data is being overwritten by a call to g_object_set_qdata()
2981 * with the same @quark.
2984 g_object_set_qdata_full (GObject *object,
2987 GDestroyNotify destroy)
2989 g_return_if_fail (G_IS_OBJECT (object));
2990 g_return_if_fail (quark > 0);
2992 g_datalist_id_set_data_full (&object->qdata, quark, data,
2993 data ? destroy : (GDestroyNotify) NULL);
2997 * g_object_steal_qdata:
2998 * @object: The GObject to get a stored user data pointer from
2999 * @quark: A #GQuark, naming the user data pointer
3001 * This function gets back user data pointers stored via
3002 * g_object_set_qdata() and removes the @data from object
3003 * without invoking its destroy() function (if any was
3005 * Usually, calling this function is only required to update
3006 * user data pointers with a destroy notifier, for example:
3009 * object_add_to_user_list (GObject *object,
3010 * const gchar *new_string)
3012 * // the quark, naming the object data
3013 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3014 * // retrive the old string list
3015 * GList *list = g_object_steal_qdata (object, quark_string_list);
3017 * // prepend new string
3018 * list = g_list_prepend (list, g_strdup (new_string));
3019 * // this changed 'list', so we need to set it again
3020 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3023 * free_string_list (gpointer data)
3025 * GList *node, *list = data;
3027 * for (node = list; node; node = node->next)
3028 * g_free (node->data);
3029 * g_list_free (list);
3032 * Using g_object_get_qdata() in the above example, instead of
3033 * g_object_steal_qdata() would have left the destroy function set,
3034 * and thus the partial string list would have been freed upon
3035 * g_object_set_qdata_full().
3037 * Returns: (transfer full): The user data pointer set, or %NULL
3040 g_object_steal_qdata (GObject *object,
3043 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3044 g_return_val_if_fail (quark > 0, NULL);
3046 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3050 * g_object_get_data:
3051 * @object: #GObject containing the associations
3052 * @key: name of the key for that association
3054 * Gets a named field from the objects table of associations (see g_object_set_data()).
3056 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3059 g_object_get_data (GObject *object,
3062 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3063 g_return_val_if_fail (key != NULL, NULL);
3065 return g_datalist_get_data (&object->qdata, key);
3069 * g_object_set_data:
3070 * @object: #GObject containing the associations.
3071 * @key: name of the key
3072 * @data: data to associate with that key
3074 * Each object carries around a table of associations from
3075 * strings to pointers. This function lets you set an association.
3077 * If the object already had an association with that name,
3078 * the old association will be destroyed.
3081 g_object_set_data (GObject *object,
3085 g_return_if_fail (G_IS_OBJECT (object));
3086 g_return_if_fail (key != NULL);
3088 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3092 * g_object_set_data_full: (skip)
3093 * @object: #GObject containing the associations
3094 * @key: name of the key
3095 * @data: data to associate with that key
3096 * @destroy: function to call when the association is destroyed
3098 * Like g_object_set_data() except it adds notification
3099 * for when the association is destroyed, either by setting it
3100 * to a different value or when the object is destroyed.
3102 * Note that the @destroy callback is not called if @data is %NULL.
3105 g_object_set_data_full (GObject *object,
3108 GDestroyNotify destroy)
3110 g_return_if_fail (G_IS_OBJECT (object));
3111 g_return_if_fail (key != NULL);
3113 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3114 data ? destroy : (GDestroyNotify) NULL);
3118 * g_object_steal_data:
3119 * @object: #GObject containing the associations
3120 * @key: name of the key
3122 * Remove a specified datum from the object's data associations,
3123 * without invoking the association's destroy handler.
3125 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3128 g_object_steal_data (GObject *object,
3133 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3134 g_return_val_if_fail (key != NULL, NULL);
3136 quark = g_quark_try_string (key);
3138 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3142 g_value_object_init (GValue *value)
3144 value->data[0].v_pointer = NULL;
3148 g_value_object_free_value (GValue *value)
3150 if (value->data[0].v_pointer)
3151 g_object_unref (value->data[0].v_pointer);
3155 g_value_object_copy_value (const GValue *src_value,
3158 if (src_value->data[0].v_pointer)
3159 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3161 dest_value->data[0].v_pointer = NULL;
3165 g_value_object_transform_value (const GValue *src_value,
3168 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)))
3169 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3171 dest_value->data[0].v_pointer = NULL;
3175 g_value_object_peek_pointer (const GValue *value)
3177 return value->data[0].v_pointer;
3181 g_value_object_collect_value (GValue *value,
3182 guint n_collect_values,
3183 GTypeCValue *collect_values,
3184 guint collect_flags)
3186 if (collect_values[0].v_pointer)
3188 GObject *object = collect_values[0].v_pointer;
3190 if (object->g_type_instance.g_class == NULL)
3191 return g_strconcat ("invalid unclassed object pointer for value type `",
3192 G_VALUE_TYPE_NAME (value),
3195 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3196 return g_strconcat ("invalid object type `",
3197 G_OBJECT_TYPE_NAME (object),
3198 "' for value type `",
3199 G_VALUE_TYPE_NAME (value),
3202 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3203 value->data[0].v_pointer = g_object_ref (object);
3206 value->data[0].v_pointer = NULL;
3212 g_value_object_lcopy_value (const GValue *value,
3213 guint n_collect_values,
3214 GTypeCValue *collect_values,
3215 guint collect_flags)
3217 GObject **object_p = collect_values[0].v_pointer;
3220 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3222 if (!value->data[0].v_pointer)
3224 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3225 *object_p = value->data[0].v_pointer;
3227 *object_p = g_object_ref (value->data[0].v_pointer);
3233 * g_value_set_object:
3234 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3235 * @v_object: (type GObject.Object) (allow-none): object value to be set
3237 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3239 * g_value_set_object() increases the reference count of @v_object
3240 * (the #GValue holds a reference to @v_object). If you do not wish
3241 * to increase the reference count of the object (i.e. you wish to
3242 * pass your current reference to the #GValue because you no longer
3243 * need it), use g_value_take_object() instead.
3245 * It is important that your #GValue holds a reference to @v_object (either its
3246 * own, or one it has taken) to ensure that the object won't be destroyed while
3247 * the #GValue still exists).
3250 g_value_set_object (GValue *value,
3255 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3257 old = value->data[0].v_pointer;
3261 g_return_if_fail (G_IS_OBJECT (v_object));
3262 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3264 value->data[0].v_pointer = v_object;
3265 g_object_ref (value->data[0].v_pointer);
3268 value->data[0].v_pointer = NULL;
3271 g_object_unref (old);
3275 * g_value_set_object_take_ownership: (skip)
3276 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3277 * @v_object: (allow-none): object value to be set
3279 * This is an internal function introduced mainly for C marshallers.
3281 * Deprecated: 2.4: Use g_value_take_object() instead.
3284 g_value_set_object_take_ownership (GValue *value,
3287 g_value_take_object (value, v_object);
3291 * g_value_take_object: (skip)
3292 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3293 * @v_object: (allow-none): object value to be set
3295 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3296 * and takes over the ownership of the callers reference to @v_object;
3297 * the caller doesn't have to unref it any more (i.e. the reference
3298 * count of the object is not increased).
3300 * If you want the #GValue to hold its own reference to @v_object, use
3301 * g_value_set_object() instead.
3306 g_value_take_object (GValue *value,
3309 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3311 if (value->data[0].v_pointer)
3313 g_object_unref (value->data[0].v_pointer);
3314 value->data[0].v_pointer = NULL;
3319 g_return_if_fail (G_IS_OBJECT (v_object));
3320 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3322 value->data[0].v_pointer = v_object; /* we take over the reference count */
3327 * g_value_get_object:
3328 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3330 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3332 * Returns: (type GObject.Object) (transfer none): object contents of @value
3335 g_value_get_object (const GValue *value)
3337 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3339 return value->data[0].v_pointer;
3343 * g_value_dup_object:
3344 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3346 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3347 * its reference count. If the contents of the #GValue are %NULL, then
3348 * %NULL will be returned.
3350 * Returns: (type GObject.Object) (transfer full): object content of @value,
3351 * should be unreferenced when no longer needed.
3354 g_value_dup_object (const GValue *value)
3356 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3358 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3362 * g_signal_connect_object: (skip)
3363 * @instance: the instance to connect to.
3364 * @detailed_signal: a string of the form "signal-name::detail".
3365 * @c_handler: the #GCallback to connect.
3366 * @gobject: the object to pass as data to @c_handler.
3367 * @connect_flags: a combination of #GConnectFlags.
3369 * This is similar to g_signal_connect_data(), but uses a closure which
3370 * ensures that the @gobject stays alive during the call to @c_handler
3371 * by temporarily adding a reference count to @gobject.
3373 * Note that there is a bug in GObject that makes this function
3374 * much less useful than it might seem otherwise. Once @gobject is
3375 * disposed, the callback will no longer be called, but, the signal
3376 * handler is <emphasis>not</emphasis> currently disconnected. If the
3377 * @instance is itself being freed at the same time than this doesn't
3378 * matter, since the signal will automatically be removed, but
3379 * if @instance persists, then the signal handler will leak. You
3380 * should not remove the signal yourself because in a future versions of
3381 * GObject, the handler <emphasis>will</emphasis> automatically
3384 * It's possible to work around this problem in a way that will
3385 * continue to work with future versions of GObject by checking
3386 * that the signal handler is still connected before disconnected it:
3387 * <informalexample><programlisting>
3388 * if (g_signal_handler_is_connected (instance, id))
3389 * g_signal_handler_disconnect (instance, id);
3390 * </programlisting></informalexample>
3392 * Returns: the handler id.
3395 g_signal_connect_object (gpointer instance,
3396 const gchar *detailed_signal,
3397 GCallback c_handler,
3399 GConnectFlags connect_flags)
3401 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3402 g_return_val_if_fail (detailed_signal != NULL, 0);
3403 g_return_val_if_fail (c_handler != NULL, 0);
3409 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3411 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3413 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3416 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3422 GClosure *closures[1]; /* flexible array */
3424 /* don't change this structure without supplying an accessor for
3425 * watched closures, e.g.:
3426 * GSList* g_object_list_watched_closures (GObject *object)
3429 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3430 * carray = g_object_get_data (object, "GObject-closure-array");
3433 * GSList *slist = NULL;
3435 * for (i = 0; i < carray->n_closures; i++)
3436 * slist = g_slist_prepend (slist, carray->closures[i]);
3444 object_remove_closure (gpointer data,
3447 GObject *object = data;
3451 G_LOCK (closure_array_mutex);
3452 carray = g_object_get_qdata (object, quark_closure_array);
3453 for (i = 0; i < carray->n_closures; i++)
3454 if (carray->closures[i] == closure)
3456 carray->n_closures--;
3457 if (i < carray->n_closures)
3458 carray->closures[i] = carray->closures[carray->n_closures];
3459 G_UNLOCK (closure_array_mutex);
3462 G_UNLOCK (closure_array_mutex);
3463 g_assert_not_reached ();
3467 destroy_closure_array (gpointer data)
3469 CArray *carray = data;
3470 GObject *object = carray->object;
3471 guint i, n = carray->n_closures;
3473 for (i = 0; i < n; i++)
3475 GClosure *closure = carray->closures[i];
3477 /* removing object_remove_closure() upfront is probably faster than
3478 * letting it fiddle with quark_closure_array which is empty anyways
3480 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3481 g_closure_invalidate (closure);
3487 * g_object_watch_closure:
3488 * @object: GObject restricting lifetime of @closure
3489 * @closure: GClosure to watch
3491 * This function essentially limits the life time of the @closure to
3492 * the life time of the object. That is, when the object is finalized,
3493 * the @closure is invalidated by calling g_closure_invalidate() on
3494 * it, in order to prevent invocations of the closure with a finalized
3495 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3496 * added as marshal guards to the @closure, to ensure that an extra
3497 * reference count is held on @object during invocation of the
3498 * @closure. Usually, this function will be called on closures that
3499 * use this @object as closure data.
3502 g_object_watch_closure (GObject *object,
3508 g_return_if_fail (G_IS_OBJECT (object));
3509 g_return_if_fail (closure != NULL);
3510 g_return_if_fail (closure->is_invalid == FALSE);
3511 g_return_if_fail (closure->in_marshal == FALSE);
3512 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3514 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3515 g_closure_add_marshal_guards (closure,
3516 object, (GClosureNotify) g_object_ref,
3517 object, (GClosureNotify) g_object_unref);
3518 G_LOCK (closure_array_mutex);
3519 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3522 carray = g_renew (CArray, NULL, 1);
3523 carray->object = object;
3524 carray->n_closures = 1;
3529 i = carray->n_closures++;
3530 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3532 carray->closures[i] = closure;
3533 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3534 G_UNLOCK (closure_array_mutex);
3538 * g_closure_new_object:
3539 * @sizeof_closure: the size of the structure to allocate, must be at least
3540 * <literal>sizeof (GClosure)</literal>
3541 * @object: a #GObject pointer to store in the @data field of the newly
3542 * allocated #GClosure
3544 * A variant of g_closure_new_simple() which stores @object in the
3545 * @data field of the closure and calls g_object_watch_closure() on
3546 * @object and the created closure. This function is mainly useful
3547 * when implementing new types of closures.
3549 * Returns: (transfer full): a newly allocated #GClosure
3552 g_closure_new_object (guint sizeof_closure,
3557 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3558 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3560 closure = g_closure_new_simple (sizeof_closure, object);
3561 g_object_watch_closure (object, closure);
3567 * g_cclosure_new_object: (skip)
3568 * @callback_func: the function to invoke
3569 * @object: a #GObject pointer to pass to @callback_func
3571 * A variant of g_cclosure_new() which uses @object as @user_data and
3572 * calls g_object_watch_closure() on @object and the created
3573 * closure. This function is useful when you have a callback closely
3574 * associated with a #GObject, and want the callback to no longer run
3575 * after the object is is freed.
3577 * Returns: a new #GCClosure
3580 g_cclosure_new_object (GCallback callback_func,
3585 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3586 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3587 g_return_val_if_fail (callback_func != NULL, NULL);
3589 closure = g_cclosure_new (callback_func, object, NULL);
3590 g_object_watch_closure (object, closure);
3596 * g_cclosure_new_object_swap: (skip)
3597 * @callback_func: the function to invoke
3598 * @object: a #GObject pointer to pass to @callback_func
3600 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3601 * and calls g_object_watch_closure() on @object and the created
3602 * closure. This function is useful when you have a callback closely
3603 * associated with a #GObject, and want the callback to no longer run
3604 * after the object is is freed.
3606 * Returns: a new #GCClosure
3609 g_cclosure_new_object_swap (GCallback callback_func,
3614 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3615 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3616 g_return_val_if_fail (callback_func != NULL, NULL);
3618 closure = g_cclosure_new_swap (callback_func, object, NULL);
3619 g_object_watch_closure (object, closure);
3625 g_object_compat_control (gsize what,
3631 case 1: /* floating base type */
3632 return G_TYPE_INITIALLY_UNOWNED;
3633 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3634 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3636 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3638 *pp = floating_flag_handler;
3645 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3648 g_initially_unowned_init (GInitiallyUnowned *object)
3650 g_object_force_floating (object);
3654 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)