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;
193 GTrashStack *_nqueue_trash; /* unused */
195 struct _GObjectNotifyQueue
197 GObjectNotifyContext *context;
200 guint16 freeze_count;
203 /* --- variables --- */
204 G_LOCK_DEFINE_STATIC (closure_array_mutex);
205 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
206 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
207 static GQuark quark_closure_array = 0;
208 static GQuark quark_weak_refs = 0;
209 static GQuark quark_toggle_refs = 0;
210 static GParamSpecPool *pspec_pool = NULL;
211 static GObjectNotifyContext property_notify_context = { 0, };
212 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
213 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
214 G_LOCK_DEFINE_STATIC (construction_mutex);
215 static GSList *construction_objects = NULL;
217 /* --- functions --- */
218 #ifdef G_ENABLE_DEBUG
219 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
220 G_LOCK_DEFINE_STATIC (debug_objects);
221 static volatile GObject *g_trap_object_ref = NULL;
222 static guint debug_objects_count = 0;
223 static GHashTable *debug_objects_ht = NULL;
225 G_LOCK_DEFINE_STATIC(notify_lock);
227 /* --- functions --- */
229 g_object_notify_queue_free (gpointer data)
231 GObjectNotifyQueue *nqueue = data;
233 g_slist_free (nqueue->pspecs);
234 g_slice_free (GObjectNotifyQueue, nqueue);
237 static inline GObjectNotifyQueue*
238 g_object_notify_queue_freeze (GObject *object,
239 GObjectNotifyContext *context)
241 GObjectNotifyQueue *nqueue;
244 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
247 nqueue = g_slice_new0 (GObjectNotifyQueue);
248 nqueue->context = context;
249 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
250 nqueue, g_object_notify_queue_free);
253 if (nqueue->freeze_count >= 65535)
254 g_critical("Free queue for %s (%p) is larger than 65535,"
255 " called g_object_freeze_notify() too often."
256 " Forgot to call g_object_thaw_notify() or infinite loop",
257 G_OBJECT_TYPE_NAME (object), object);
259 nqueue->freeze_count++;
260 G_UNLOCK(notify_lock);
266 g_object_notify_queue_thaw (GObject *object,
267 GObjectNotifyQueue *nqueue)
269 GObjectNotifyContext *context = nqueue->context;
270 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
274 g_return_if_fail (nqueue->freeze_count > 0);
275 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
279 /* Just make sure we never get into some nasty race condition */
280 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
281 G_UNLOCK(notify_lock);
282 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
283 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
287 nqueue->freeze_count--;
288 if (nqueue->freeze_count) {
289 G_UNLOCK(notify_lock);
293 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
295 for (slist = nqueue->pspecs; slist; slist = slist->next)
297 pspecs[n_pspecs++] = slist->data;
299 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
301 G_UNLOCK(notify_lock);
304 context->dispatcher (object, n_pspecs, pspecs);
309 g_object_notify_queue_clear (GObject *object,
310 GObjectNotifyQueue *nqueue)
312 g_return_if_fail (nqueue->freeze_count > 0);
316 g_slist_free (nqueue->pspecs);
317 nqueue->pspecs = NULL;
318 nqueue->n_pspecs = 0;
320 G_UNLOCK(notify_lock);
324 g_object_notify_queue_add (GObject *object,
325 GObjectNotifyQueue *nqueue,
328 if (pspec->flags & G_PARAM_READABLE)
330 GParamSpec *redirect;
334 g_return_if_fail (nqueue->n_pspecs < 65535);
336 redirect = g_param_spec_get_redirect_target (pspec);
340 /* we do the deduping in _thaw */
341 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
343 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
347 G_UNLOCK(notify_lock);
351 /* NB: This function is not threadsafe, do not ever use it if
352 * you need a threadsafe notify queue.
353 * Use g_object_notify_queue_freeze() to acquire the queue and
354 * g_object_notify_queue_thaw() after you are done instead.
356 static inline GObjectNotifyQueue*
357 g_object_notify_queue_from_object (GObject *object,
358 GObjectNotifyContext *context)
360 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
364 debug_objects_foreach (gpointer key,
368 GObject *object = value;
370 g_message ("[%p] stale %s\tref_count=%u",
372 G_OBJECT_TYPE_NAME (object),
377 debug_objects_atexit (void)
381 G_LOCK (debug_objects);
382 g_message ("stale GObjects: %u", debug_objects_count);
383 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
384 G_UNLOCK (debug_objects);
387 #endif /* G_ENABLE_DEBUG */
390 _g_object_type_init (void)
392 static gboolean initialized = FALSE;
393 static const GTypeFundamentalInfo finfo = {
394 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
396 static GTypeInfo info = {
397 sizeof (GObjectClass),
398 (GBaseInitFunc) g_object_base_class_init,
399 (GBaseFinalizeFunc) g_object_base_class_finalize,
400 (GClassInitFunc) g_object_do_class_init,
401 NULL /* class_destroy */,
402 NULL /* class_data */,
405 (GInstanceInitFunc) g_object_init,
406 NULL, /* value_table */
408 static const GTypeValueTable value_table = {
409 g_value_object_init, /* value_init */
410 g_value_object_free_value, /* value_free */
411 g_value_object_copy_value, /* value_copy */
412 g_value_object_peek_pointer, /* value_peek_pointer */
413 "p", /* collect_format */
414 g_value_object_collect_value, /* collect_value */
415 "p", /* lcopy_format */
416 g_value_object_lcopy_value, /* lcopy_value */
420 g_return_if_fail (initialized == FALSE);
425 info.value_table = &value_table;
426 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
427 g_assert (type == G_TYPE_OBJECT);
428 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
430 #ifdef G_ENABLE_DEBUG
433 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
434 g_atexit (debug_objects_atexit);
436 #endif /* G_ENABLE_DEBUG */
440 g_object_base_class_init (GObjectClass *class)
442 GObjectClass *pclass = g_type_class_peek_parent (class);
444 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
445 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
448 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
450 /* reset instance specific fields and methods that don't get inherited */
451 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
452 class->get_property = NULL;
453 class->set_property = NULL;
457 g_object_base_class_finalize (GObjectClass *class)
461 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
463 g_slist_free (class->construct_properties);
464 class->construct_properties = NULL;
465 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
466 for (node = list; node; node = node->next)
468 GParamSpec *pspec = node->data;
470 g_param_spec_pool_remove (pspec_pool, pspec);
471 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
472 g_param_spec_unref (pspec);
478 g_object_notify_dispatcher (GObject *object,
482 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
486 g_object_do_class_init (GObjectClass *class)
488 /* read the comment about typedef struct CArray; on why not to change this quark */
489 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
491 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
492 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
493 pspec_pool = g_param_spec_pool_new (TRUE);
494 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
495 property_notify_context.dispatcher = g_object_notify_dispatcher;
497 class->constructor = g_object_constructor;
498 class->constructed = g_object_constructed;
499 class->set_property = g_object_do_set_property;
500 class->get_property = g_object_do_get_property;
501 class->dispose = g_object_real_dispose;
502 class->finalize = g_object_finalize;
503 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
504 class->notify = NULL;
508 * @gobject: the object which received the signal.
509 * @pspec: the #GParamSpec of the property which changed.
511 * The notify signal is emitted on an object when one of its
512 * properties has been changed. Note that getting this signal
513 * doesn't guarantee that the value of the property has actually
514 * changed, it may also be emitted when the setter for the property
515 * is called to reinstate the previous value.
517 * This signal is typically used to obtain change notification for a
518 * single property, by specifying the property name as a detail in the
519 * g_signal_connect() call, like this:
521 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
522 * G_CALLBACK (gtk_text_view_target_list_notify),
525 * It is important to note that you must use
526 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
527 * detail strings for the notify signal.
529 gobject_signals[NOTIFY] =
530 g_signal_new (g_intern_static_string ("notify"),
531 G_TYPE_FROM_CLASS (class),
532 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
533 G_STRUCT_OFFSET (GObjectClass, notify),
535 g_cclosure_marshal_VOID__PARAM,
539 /* Install a check function that we'll use to verify that classes that
540 * implement an interface implement all properties for that interface
542 g_type_add_interface_check (NULL, object_interface_check_properties);
546 install_property_internal (GType g_type,
550 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
552 g_warning ("When installing property: type `%s' already has a property named `%s'",
553 g_type_name (g_type),
558 g_param_spec_ref_sink (pspec);
559 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
560 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
564 * g_object_class_install_property:
565 * @oclass: a #GObjectClass
566 * @property_id: the id for the new property
567 * @pspec: the #GParamSpec for the new property
569 * Installs a new property. This is usually done in the class initializer.
571 * Note that it is possible to redefine a property in a derived class,
572 * by installing a property with the same name. This can be useful at times,
573 * e.g. to change the range of allowed values or the default value.
576 g_object_class_install_property (GObjectClass *class,
580 g_return_if_fail (G_IS_OBJECT_CLASS (class));
581 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
583 if (CLASS_HAS_DERIVED_CLASS (class))
584 g_error ("Attempt to add property %s::%s to class after it was derived",
585 G_OBJECT_CLASS_NAME (class), pspec->name);
587 class->flags |= CLASS_HAS_PROPS_FLAG;
589 if (pspec->flags & G_PARAM_WRITABLE)
590 g_return_if_fail (class->set_property != NULL);
591 if (pspec->flags & G_PARAM_READABLE)
592 g_return_if_fail (class->get_property != NULL);
593 g_return_if_fail (property_id > 0);
594 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
595 if (pspec->flags & G_PARAM_CONSTRUCT)
596 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
597 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
598 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
600 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
602 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
603 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
605 /* for property overrides of construct properties, we have to get rid
606 * of the overidden inherited construct property
608 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
609 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
610 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
614 * g_object_class_install_properties:
615 * @oclass: a #GObjectClass
616 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
617 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
618 * defining the new properties
620 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
621 * usually done in the class initializer.
623 * The property id of each property is the index of each #GParamSpec in
626 * The property id of 0 is treated specially by #GObject and it should not
627 * be used to store a #GParamSpec.
629 * This function should be used if you plan to use a static array of
630 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
631 * class initialization:
635 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
638 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
641 * my_object_class_init (MyObjectClass *klass)
643 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
645 * obj_properties[PROP_FOO] =
646 * g_param_spec_int ("foo", "Foo", "Foo",
649 * G_PARAM_READWRITE);
651 * obj_properties[PROP_BAR] =
652 * g_param_spec_string ("bar", "Bar", "Bar",
654 * G_PARAM_READWRITE);
656 * gobject_class->set_property = my_object_set_property;
657 * gobject_class->get_property = my_object_get_property;
658 * g_object_class_install_properties (gobject_class,
664 * allows calling g_object_notify_by_pspec() to notify of property changes:
668 * my_object_set_foo (MyObject *self, gint foo)
670 * if (self->foo != foo)
673 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
681 g_object_class_install_properties (GObjectClass *oclass,
685 GType oclass_type, parent_type;
688 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
689 g_return_if_fail (n_pspecs > 1);
690 g_return_if_fail (pspecs[0] == NULL);
692 if (CLASS_HAS_DERIVED_CLASS (oclass))
693 g_error ("Attempt to add properties to %s after it was derived",
694 G_OBJECT_CLASS_NAME (oclass));
696 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
697 parent_type = g_type_parent (oclass_type);
699 /* we skip the first element of the array as it would have a 0 prop_id */
700 for (i = 1; i < n_pspecs; i++)
702 GParamSpec *pspec = pspecs[i];
704 g_return_if_fail (pspec != NULL);
706 if (pspec->flags & G_PARAM_WRITABLE)
707 g_return_if_fail (oclass->set_property != NULL);
708 if (pspec->flags & G_PARAM_READABLE)
709 g_return_if_fail (oclass->get_property != NULL);
710 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
711 if (pspec->flags & G_PARAM_CONSTRUCT)
712 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
713 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
714 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
716 oclass->flags |= CLASS_HAS_PROPS_FLAG;
717 install_property_internal (oclass_type, i, pspec);
719 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
720 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
722 /* for property overrides of construct properties, we have to get rid
723 * of the overidden inherited construct property
725 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
726 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
727 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
732 * g_object_interface_install_property:
733 * @g_iface: any interface vtable for the interface, or the default
734 * vtable for the interface.
735 * @pspec: the #GParamSpec for the new property
737 * Add a property to an interface; this is only useful for interfaces
738 * that are added to GObject-derived types. Adding a property to an
739 * interface forces all objects classes with that interface to have a
740 * compatible property. The compatible property could be a newly
741 * created #GParamSpec, but normally
742 * g_object_class_override_property() will be used so that the object
743 * class only needs to provide an implementation and inherits the
744 * property description, default value, bounds, and so forth from the
745 * interface property.
747 * This function is meant to be called from the interface's default
748 * vtable initialization function (the @class_init member of
749 * #GTypeInfo.) It must not be called after after @class_init has
750 * been called for any object types implementing this interface.
755 g_object_interface_install_property (gpointer g_iface,
758 GTypeInterface *iface_class = g_iface;
760 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
761 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
762 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
763 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
765 install_property_internal (iface_class->g_type, 0, pspec);
769 * g_object_class_find_property:
770 * @oclass: a #GObjectClass
771 * @property_name: the name of the property to look up
773 * Looks up the #GParamSpec for a property of a class.
775 * Returns: (transfer none): the #GParamSpec for the property, or
776 * %NULL if the class doesn't have a property of that name
779 g_object_class_find_property (GObjectClass *class,
780 const gchar *property_name)
783 GParamSpec *redirect;
785 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
786 g_return_val_if_fail (property_name != NULL, NULL);
788 pspec = g_param_spec_pool_lookup (pspec_pool,
790 G_OBJECT_CLASS_TYPE (class),
794 redirect = g_param_spec_get_redirect_target (pspec);
805 * g_object_interface_find_property:
806 * @g_iface: any interface vtable for the interface, or the default
807 * vtable for the interface
808 * @property_name: name of a property to lookup.
810 * Find the #GParamSpec with the given name for an
811 * interface. Generally, the interface vtable passed in as @g_iface
812 * will be the default vtable from g_type_default_interface_ref(), or,
813 * if you know the interface has already been loaded,
814 * g_type_default_interface_peek().
818 * Returns: (transfer none): the #GParamSpec for the property of the
819 * interface with the name @property_name, or %NULL if no
820 * such property exists.
823 g_object_interface_find_property (gpointer g_iface,
824 const gchar *property_name)
826 GTypeInterface *iface_class = g_iface;
828 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
829 g_return_val_if_fail (property_name != NULL, NULL);
831 return g_param_spec_pool_lookup (pspec_pool,
838 * g_object_class_override_property:
839 * @oclass: a #GObjectClass
840 * @property_id: the new property ID
841 * @name: the name of a property registered in a parent class or
842 * in an interface of this class.
844 * Registers @property_id as referring to a property with the
845 * name @name in a parent class or in an interface implemented
846 * by @oclass. This allows this class to <firstterm>override</firstterm>
847 * a property implementation in a parent class or to provide
848 * the implementation of a property from an interface.
851 * Internally, overriding is implemented by creating a property of type
852 * #GParamSpecOverride; generally operations that query the properties of
853 * the object class, such as g_object_class_find_property() or
854 * g_object_class_list_properties() will return the overridden
855 * property. However, in one case, the @construct_properties argument of
856 * the @constructor virtual function, the #GParamSpecOverride is passed
857 * instead, so that the @param_id field of the #GParamSpec will be
858 * correct. For virtually all uses, this makes no difference. If you
859 * need to get the overridden property, you can call
860 * g_param_spec_get_redirect_target().
866 g_object_class_override_property (GObjectClass *oclass,
870 GParamSpec *overridden = NULL;
874 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
875 g_return_if_fail (property_id > 0);
876 g_return_if_fail (name != NULL);
878 /* Find the overridden property; first check parent types
880 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
881 if (parent_type != G_TYPE_NONE)
882 overridden = g_param_spec_pool_lookup (pspec_pool,
891 /* Now check interfaces
893 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
894 while (n_ifaces-- && !overridden)
896 overridden = g_param_spec_pool_lookup (pspec_pool,
907 g_warning ("%s: Can't find property to override for '%s::%s'",
908 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
912 new = g_param_spec_override (name, overridden);
913 g_object_class_install_property (oclass, property_id, new);
917 * g_object_class_list_properties:
918 * @oclass: a #GObjectClass
919 * @n_properties: (out): return location for the length of the returned array
921 * Get an array of #GParamSpec* for all properties of a class.
923 * Returns: (array length=n_properties) (transfer container): an array of
924 * #GParamSpec* which should be freed after use
926 GParamSpec** /* free result */
927 g_object_class_list_properties (GObjectClass *class,
928 guint *n_properties_p)
933 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
935 pspecs = g_param_spec_pool_list (pspec_pool,
936 G_OBJECT_CLASS_TYPE (class),
945 * g_object_interface_list_properties:
946 * @g_iface: any interface vtable for the interface, or the default
947 * vtable for the interface
948 * @n_properties_p: (out): location to store number of properties returned.
950 * Lists the properties of an interface.Generally, the interface
951 * vtable passed in as @g_iface will be the default vtable from
952 * g_type_default_interface_ref(), or, if you know the interface has
953 * already been loaded, g_type_default_interface_peek().
957 * Returns: (array length=n_properties_p) (transfer container): a
958 * pointer to an array of pointers to #GParamSpec
959 * structures. The paramspecs are owned by GLib, but the
960 * array should be freed with g_free() when you are done with
964 g_object_interface_list_properties (gpointer g_iface,
965 guint *n_properties_p)
967 GTypeInterface *iface_class = g_iface;
971 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
973 pspecs = g_param_spec_pool_list (pspec_pool,
983 g_object_init (GObject *object,
986 object->ref_count = 1;
987 object->qdata = NULL;
989 if (CLASS_HAS_PROPS (class))
991 /* freeze object's notification queue, g_object_newv() preserves pairedness */
992 g_object_notify_queue_freeze (object, &property_notify_context);
995 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
997 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
998 G_LOCK (construction_mutex);
999 construction_objects = g_slist_prepend (construction_objects, object);
1000 G_UNLOCK (construction_mutex);
1003 #ifdef G_ENABLE_DEBUG
1006 G_LOCK (debug_objects);
1007 debug_objects_count++;
1008 g_hash_table_insert (debug_objects_ht, object, object);
1009 G_UNLOCK (debug_objects);
1011 #endif /* G_ENABLE_DEBUG */
1015 g_object_do_set_property (GObject *object,
1017 const GValue *value,
1020 switch (property_id)
1023 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1029 g_object_do_get_property (GObject *object,
1034 switch (property_id)
1037 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1043 g_object_real_dispose (GObject *object)
1045 g_signal_handlers_destroy (object);
1046 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1047 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1051 g_object_finalize (GObject *object)
1053 g_datalist_clear (&object->qdata);
1055 #ifdef G_ENABLE_DEBUG
1058 G_LOCK (debug_objects);
1059 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1060 g_hash_table_remove (debug_objects_ht, object);
1061 debug_objects_count--;
1062 G_UNLOCK (debug_objects);
1064 #endif /* G_ENABLE_DEBUG */
1069 g_object_dispatch_properties_changed (GObject *object,
1071 GParamSpec **pspecs)
1075 for (i = 0; i < n_pspecs; i++)
1076 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1080 * g_object_run_dispose:
1081 * @object: a #GObject
1083 * Releases all references to other objects. This can be used to break
1086 * This functions should only be called from object system implementations.
1089 g_object_run_dispose (GObject *object)
1091 g_return_if_fail (G_IS_OBJECT (object));
1092 g_return_if_fail (object->ref_count > 0);
1094 g_object_ref (object);
1095 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1096 G_OBJECT_GET_CLASS (object)->dispose (object);
1097 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1098 g_object_unref (object);
1102 * g_object_freeze_notify:
1103 * @object: a #GObject
1105 * Increases the freeze count on @object. If the freeze count is
1106 * non-zero, the emission of "notify" signals on @object is
1107 * stopped. The signals are queued until the freeze count is decreased
1110 * This is necessary for accessors that modify multiple properties to prevent
1111 * premature notification while the object is still being modified.
1114 g_object_freeze_notify (GObject *object)
1116 g_return_if_fail (G_IS_OBJECT (object));
1118 if (g_atomic_int_get (&object->ref_count) == 0)
1121 g_object_ref (object);
1122 g_object_notify_queue_freeze (object, &property_notify_context);
1123 g_object_unref (object);
1127 g_object_notify_by_spec_internal (GObject *object,
1130 GObjectNotifyQueue *nqueue;
1132 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1133 g_object_notify_queue_add (object, nqueue, pspec);
1134 g_object_notify_queue_thaw (object, nqueue);
1139 * @object: a #GObject
1140 * @property_name: the name of a property installed on the class of @object.
1142 * Emits a "notify" signal for the property @property_name on @object.
1144 * When possible, eg. when signaling a property change from within the class
1145 * that registered the property, you should use g_object_notify_by_pspec()
1149 g_object_notify (GObject *object,
1150 const gchar *property_name)
1154 g_return_if_fail (G_IS_OBJECT (object));
1155 g_return_if_fail (property_name != NULL);
1156 if (g_atomic_int_get (&object->ref_count) == 0)
1159 g_object_ref (object);
1160 /* We don't need to get the redirect target
1161 * (by, e.g. calling g_object_class_find_property())
1162 * because g_object_notify_queue_add() does that
1164 pspec = g_param_spec_pool_lookup (pspec_pool,
1166 G_OBJECT_TYPE (object),
1170 g_warning ("%s: object class `%s' has no property named `%s'",
1172 G_OBJECT_TYPE_NAME (object),
1175 g_object_notify_by_spec_internal (object, pspec);
1176 g_object_unref (object);
1180 * g_object_notify_by_pspec:
1181 * @object: a #GObject
1182 * @pspec: the #GParamSpec of a property installed on the class of @object.
1184 * Emits a "notify" signal for the property specified by @pspec on @object.
1186 * This function omits the property name lookup, hence it is faster than
1187 * g_object_notify().
1189 * One way to avoid using g_object_notify() from within the
1190 * class that registered the properties, and using g_object_notify_by_pspec()
1191 * instead, is to store the GParamSpec used with
1192 * g_object_class_install_property() inside a static array, e.g.:
1202 * static GParamSpec *properties[PROP_LAST];
1205 * my_object_class_init (MyObjectClass *klass)
1207 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1210 * G_PARAM_READWRITE);
1211 * g_object_class_install_property (gobject_class,
1213 * properties[PROP_FOO]);
1217 * and then notify a change on the "foo" property with:
1220 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1226 g_object_notify_by_pspec (GObject *object,
1230 g_return_if_fail (G_IS_OBJECT (object));
1231 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1233 g_object_ref (object);
1234 g_object_notify_by_spec_internal (object, pspec);
1235 g_object_unref (object);
1239 * g_object_thaw_notify:
1240 * @object: a #GObject
1242 * Reverts the effect of a previous call to
1243 * g_object_freeze_notify(). The freeze count is decreased on @object
1244 * and when it reaches zero, all queued "notify" signals are emitted.
1246 * It is an error to call this function when the freeze count is zero.
1249 g_object_thaw_notify (GObject *object)
1251 GObjectNotifyQueue *nqueue;
1253 g_return_if_fail (G_IS_OBJECT (object));
1254 if (g_atomic_int_get (&object->ref_count) == 0)
1257 g_object_ref (object);
1259 /* FIXME: Freezing is the only way to get at the notify queue.
1260 * So we freeze once and then thaw twice.
1262 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1263 g_object_notify_queue_thaw (object, nqueue);
1264 g_object_notify_queue_thaw (object, nqueue);
1266 g_object_unref (object);
1270 object_get_property (GObject *object,
1274 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1275 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1276 GParamSpec *redirect;
1280 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1281 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1285 redirect = g_param_spec_get_redirect_target (pspec);
1289 class->get_property (object, param_id, value, pspec);
1293 object_set_property (GObject *object,
1295 const GValue *value,
1296 GObjectNotifyQueue *nqueue)
1298 GValue tmp_value = G_VALUE_INIT;
1299 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1300 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1301 GParamSpec *redirect;
1302 static const gchar * enable_diagnostic = NULL;
1306 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1307 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1311 redirect = g_param_spec_get_redirect_target (pspec);
1315 if (G_UNLIKELY (!enable_diagnostic))
1317 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1318 if (!enable_diagnostic)
1319 enable_diagnostic = "0";
1322 if (enable_diagnostic[0] == '1')
1324 if (pspec->flags & G_PARAM_DEPRECATED)
1325 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1326 "anymore. It will be removed in a future version.",
1327 G_OBJECT_TYPE_NAME (object), pspec->name);
1330 /* provide a copy to work from, convert (if necessary) and validate */
1331 g_value_init (&tmp_value, pspec->value_type);
1332 if (!g_value_transform (value, &tmp_value))
1333 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1335 g_type_name (pspec->value_type),
1336 G_VALUE_TYPE_NAME (value));
1337 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1339 gchar *contents = g_strdup_value_contents (value);
1341 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1343 G_VALUE_TYPE_NAME (value),
1345 g_type_name (pspec->value_type));
1350 class->set_property (object, param_id, &tmp_value, pspec);
1351 g_object_notify_queue_add (object, nqueue, pspec);
1353 g_value_unset (&tmp_value);
1357 object_interface_check_properties (gpointer func_data,
1360 GTypeInterface *iface_class = g_iface;
1361 GObjectClass *class;
1362 GType iface_type = iface_class->g_type;
1363 GParamSpec **pspecs;
1366 class = g_type_class_ref (iface_class->g_instance_type);
1368 if (!G_IS_OBJECT_CLASS (class))
1371 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1375 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1377 G_OBJECT_CLASS_TYPE (class),
1382 g_critical ("Object class %s doesn't implement property "
1383 "'%s' from interface '%s'",
1384 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1386 g_type_name (iface_type));
1391 /* The implementation paramspec must have a less restrictive
1392 * type than the interface parameter spec for set() and a
1393 * more restrictive type for get(). We just require equality,
1394 * rather than doing something more complicated checking
1395 * the READABLE and WRITABLE flags. We also simplify here
1396 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1399 !g_type_is_a (pspecs[n]->value_type,
1400 class_pspec->value_type))
1402 g_critical ("Property '%s' on class '%s' has type '%s' "
1403 "which is different from the type '%s', "
1404 "of the property on interface '%s'\n",
1406 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1407 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1408 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1409 g_type_name (iface_type));
1412 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1414 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1415 * READABLE and WRITABLE remove restrictions. The implementation
1416 * paramspec must have less restrictive flags.
1419 (!SUBSET (class_pspec->flags,
1421 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1422 !SUBSET (pspecs[n]->flags,
1424 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1426 g_critical ("Flags for property '%s' on class '%s' "
1427 "are not compatible with the property on"
1430 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1431 g_type_name (iface_type));
1438 g_type_class_unref (class);
1442 g_object_get_type (void)
1444 return G_TYPE_OBJECT;
1448 * g_object_new: (skip)
1449 * @object_type: the type id of the #GObject subtype to instantiate
1450 * @first_property_name: the name of the first property
1451 * @...: the value of the first property, followed optionally by more
1452 * name/value pairs, followed by %NULL
1454 * Creates a new instance of a #GObject subtype and sets its properties.
1456 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1457 * which are not explicitly specified are set to their default values.
1459 * Returns: (transfer full): a new instance of @object_type
1462 g_object_new (GType object_type,
1463 const gchar *first_property_name,
1469 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1471 /* short circuit for calls supplying no properties */
1472 if (!first_property_name)
1473 return g_object_newv (object_type, 0, NULL);
1475 va_start (var_args, first_property_name);
1476 object = g_object_new_valist (object_type, first_property_name, var_args);
1483 slist_maybe_remove (GSList **slist,
1486 GSList *last = NULL, *node = *slist;
1489 if (node->data == data)
1492 last->next = node->next;
1494 *slist = node->next;
1495 g_slist_free_1 (node);
1504 static inline gboolean
1505 object_in_construction_list (GObject *object)
1507 gboolean in_construction;
1508 G_LOCK (construction_mutex);
1509 in_construction = g_slist_find (construction_objects, object) != NULL;
1510 G_UNLOCK (construction_mutex);
1511 return in_construction;
1516 * @object_type: the type id of the #GObject subtype to instantiate
1517 * @n_parameters: the length of the @parameters array
1518 * @parameters: (array length=n_parameters): an array of #GParameter
1520 * Creates a new instance of a #GObject subtype and sets its properties.
1522 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1523 * which are not explicitly specified are set to their default values.
1525 * Rename to: g_object_new
1526 * Returns: (type GObject.Object) (transfer full): a new instance of
1530 g_object_newv (GType object_type,
1532 GParameter *parameters)
1534 GObjectConstructParam *cparams = NULL, *oparams;
1535 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1537 GObjectClass *class, *unref_class = NULL;
1539 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1541 GList *clist = NULL;
1542 gboolean newly_constructed;
1545 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1547 class = g_type_class_peek_static (object_type);
1549 class = unref_class = g_type_class_ref (object_type);
1550 for (slist = class->construct_properties; slist; slist = slist->next)
1552 clist = g_list_prepend (clist, slist->data);
1553 n_total_cparams += 1;
1556 if (n_parameters == 0 && n_total_cparams == 0)
1558 /* This is a simple object with no construct properties, and
1559 * no properties are being set, so short circuit the parameter
1560 * handling. This speeds up simple object construction.
1563 object = class->constructor (object_type, 0, NULL);
1564 goto did_construction;
1567 /* collect parameters, sort into construction and normal ones */
1568 oparams = g_new (GObjectConstructParam, n_parameters);
1569 cparams = g_new (GObjectConstructParam, n_total_cparams);
1570 for (i = 0; i < n_parameters; i++)
1572 GValue *value = ¶meters[i].value;
1573 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1579 g_warning ("%s: object class `%s' has no property named `%s'",
1581 g_type_name (object_type),
1582 parameters[i].name);
1585 if (!(pspec->flags & G_PARAM_WRITABLE))
1587 g_warning ("%s: property `%s' of object class `%s' is not writable",
1590 g_type_name (object_type));
1593 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1595 GList *list = g_list_find (clist, pspec);
1599 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1600 G_STRFUNC, pspec->name, g_type_name (object_type));
1603 cparams[n_cparams].pspec = pspec;
1604 cparams[n_cparams].value = value;
1609 list->prev->next = list->next;
1611 list->next->prev = list->prev;
1612 g_list_free_1 (list);
1616 oparams[n_oparams].pspec = pspec;
1617 oparams[n_oparams].value = value;
1622 /* set remaining construction properties to default values */
1623 n_cvalues = n_total_cparams - n_cparams;
1624 cvalues = g_new (GValue, n_cvalues);
1627 GList *tmp = clist->next;
1628 GParamSpec *pspec = clist->data;
1629 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1632 g_value_init (value, pspec->value_type);
1633 g_param_value_set_default (pspec, value);
1635 cparams[n_cparams].pspec = pspec;
1636 cparams[n_cparams].value = value;
1639 g_list_free_1 (clist);
1643 /* construct object from construction parameters */
1644 object = class->constructor (object_type, n_total_cparams, cparams);
1645 /* free construction values */
1648 g_value_unset (cvalues + n_cvalues);
1652 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1654 /* adjust freeze_count according to g_object_init() and remaining properties */
1655 G_LOCK (construction_mutex);
1656 newly_constructed = slist_maybe_remove (&construction_objects, object);
1657 G_UNLOCK (construction_mutex);
1660 newly_constructed = TRUE;
1662 if (CLASS_HAS_PROPS (class))
1664 if (newly_constructed || n_oparams)
1665 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1666 if (newly_constructed)
1667 g_object_notify_queue_thaw (object, nqueue);
1670 /* run 'constructed' handler if there is a custom one */
1671 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1672 class->constructed (object);
1674 /* set remaining properties */
1675 for (i = 0; i < n_oparams; i++)
1676 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1679 if (CLASS_HAS_PROPS (class))
1681 /* release our own freeze count and handle notifications */
1682 if (newly_constructed || n_oparams)
1683 g_object_notify_queue_thaw (object, nqueue);
1687 g_type_class_unref (unref_class);
1693 * g_object_new_valist: (skip)
1694 * @object_type: the type id of the #GObject subtype to instantiate
1695 * @first_property_name: the name of the first property
1696 * @var_args: the value of the first property, followed optionally by more
1697 * name/value pairs, followed by %NULL
1699 * Creates a new instance of a #GObject subtype and sets its properties.
1701 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1702 * which are not explicitly specified are set to their default values.
1704 * Returns: a new instance of @object_type
1707 g_object_new_valist (GType object_type,
1708 const gchar *first_property_name,
1711 GObjectClass *class;
1715 guint n_params = 0, n_alloced_params = 16;
1717 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1719 if (!first_property_name)
1720 return g_object_newv (object_type, 0, NULL);
1722 class = g_type_class_ref (object_type);
1724 params = g_new0 (GParameter, n_alloced_params);
1725 name = first_property_name;
1728 gchar *error = NULL;
1729 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1735 g_warning ("%s: object class `%s' has no property named `%s'",
1737 g_type_name (object_type),
1741 if (n_params >= n_alloced_params)
1743 n_alloced_params += 16;
1744 params = g_renew (GParameter, params, n_alloced_params);
1745 memset (params + n_params, 0, 16 * (sizeof *params));
1747 params[n_params].name = name;
1748 G_VALUE_COLLECT_INIT (¶ms[n_params].value, pspec->value_type,
1749 var_args, 0, &error);
1752 g_warning ("%s: %s", G_STRFUNC, error);
1754 g_value_unset (¶ms[n_params].value);
1758 name = va_arg (var_args, gchar*);
1761 object = g_object_newv (object_type, n_params, params);
1764 g_value_unset (¶ms[n_params].value);
1767 g_type_class_unref (class);
1773 g_object_constructor (GType type,
1774 guint n_construct_properties,
1775 GObjectConstructParam *construct_params)
1780 object = (GObject*) g_type_create_instance (type);
1782 /* set construction parameters */
1783 if (n_construct_properties)
1785 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1787 /* set construct properties */
1788 while (n_construct_properties--)
1790 GValue *value = construct_params->value;
1791 GParamSpec *pspec = construct_params->pspec;
1794 object_set_property (object, pspec, value, nqueue);
1796 g_object_notify_queue_thaw (object, nqueue);
1797 /* the notification queue is still frozen from g_object_init(), so
1798 * we don't need to handle it here, g_object_newv() takes
1807 g_object_constructed (GObject *object)
1809 /* empty default impl to allow unconditional upchaining */
1813 * g_object_set_valist: (skip)
1814 * @object: a #GObject
1815 * @first_property_name: name of the first property to set
1816 * @var_args: value for the first property, followed optionally by more
1817 * name/value pairs, followed by %NULL
1819 * Sets properties on an object.
1822 g_object_set_valist (GObject *object,
1823 const gchar *first_property_name,
1826 GObjectNotifyQueue *nqueue;
1829 g_return_if_fail (G_IS_OBJECT (object));
1831 g_object_ref (object);
1832 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1834 name = first_property_name;
1837 GValue value = G_VALUE_INIT;
1839 gchar *error = NULL;
1841 pspec = g_param_spec_pool_lookup (pspec_pool,
1843 G_OBJECT_TYPE (object),
1847 g_warning ("%s: object class `%s' has no property named `%s'",
1849 G_OBJECT_TYPE_NAME (object),
1853 if (!(pspec->flags & G_PARAM_WRITABLE))
1855 g_warning ("%s: property `%s' of object class `%s' is not writable",
1858 G_OBJECT_TYPE_NAME (object));
1861 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1863 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1864 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1868 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1872 g_warning ("%s: %s", G_STRFUNC, error);
1874 g_value_unset (&value);
1878 object_set_property (object, pspec, &value, nqueue);
1879 g_value_unset (&value);
1881 name = va_arg (var_args, gchar*);
1884 g_object_notify_queue_thaw (object, nqueue);
1885 g_object_unref (object);
1889 * g_object_get_valist: (skip)
1890 * @object: a #GObject
1891 * @first_property_name: name of the first property to get
1892 * @var_args: return location for the first property, followed optionally by more
1893 * name/return location pairs, followed by %NULL
1895 * Gets properties of an object.
1897 * In general, a copy is made of the property contents and the caller
1898 * is responsible for freeing the memory in the appropriate manner for
1899 * the type, for instance by calling g_free() or g_object_unref().
1901 * See g_object_get().
1904 g_object_get_valist (GObject *object,
1905 const gchar *first_property_name,
1910 g_return_if_fail (G_IS_OBJECT (object));
1912 g_object_ref (object);
1914 name = first_property_name;
1918 GValue value = G_VALUE_INIT;
1922 pspec = g_param_spec_pool_lookup (pspec_pool,
1924 G_OBJECT_TYPE (object),
1928 g_warning ("%s: object class `%s' has no property named `%s'",
1930 G_OBJECT_TYPE_NAME (object),
1934 if (!(pspec->flags & G_PARAM_READABLE))
1936 g_warning ("%s: property `%s' of object class `%s' is not readable",
1939 G_OBJECT_TYPE_NAME (object));
1943 g_value_init (&value, pspec->value_type);
1945 object_get_property (object, pspec, &value);
1947 G_VALUE_LCOPY (&value, var_args, 0, &error);
1950 g_warning ("%s: %s", G_STRFUNC, error);
1952 g_value_unset (&value);
1956 g_value_unset (&value);
1958 name = va_arg (var_args, gchar*);
1961 g_object_unref (object);
1965 * g_object_set: (skip)
1966 * @object: a #GObject
1967 * @first_property_name: name of the first property to set
1968 * @...: value for the first property, followed optionally by more
1969 * name/value pairs, followed by %NULL
1971 * Sets properties on an object.
1974 g_object_set (gpointer _object,
1975 const gchar *first_property_name,
1978 GObject *object = _object;
1981 g_return_if_fail (G_IS_OBJECT (object));
1983 va_start (var_args, first_property_name);
1984 g_object_set_valist (object, first_property_name, var_args);
1989 * g_object_get: (skip)
1990 * @object: a #GObject
1991 * @first_property_name: name of the first property to get
1992 * @...: return location for the first property, followed optionally by more
1993 * name/return location pairs, followed by %NULL
1995 * Gets properties of an object.
1997 * In general, a copy is made of the property contents and the caller
1998 * is responsible for freeing the memory in the appropriate manner for
1999 * the type, for instance by calling g_free() or g_object_unref().
2002 * <title>Using g_object_get(<!-- -->)</title>
2003 * An example of using g_object_get() to get the contents
2004 * of three properties - one of type #G_TYPE_INT,
2005 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
2011 * g_object_get (my_object,
2012 * "int-property", &intval,
2013 * "str-property", &strval,
2014 * "obj-property", &objval,
2017 * // Do something with intval, strval, objval
2020 * g_object_unref (objval);
2025 g_object_get (gpointer _object,
2026 const gchar *first_property_name,
2029 GObject *object = _object;
2032 g_return_if_fail (G_IS_OBJECT (object));
2034 va_start (var_args, first_property_name);
2035 g_object_get_valist (object, first_property_name, var_args);
2040 * g_object_set_property:
2041 * @object: a #GObject
2042 * @property_name: the name of the property to set
2045 * Sets a property on an object.
2048 g_object_set_property (GObject *object,
2049 const gchar *property_name,
2050 const GValue *value)
2052 GObjectNotifyQueue *nqueue;
2055 g_return_if_fail (G_IS_OBJECT (object));
2056 g_return_if_fail (property_name != NULL);
2057 g_return_if_fail (G_IS_VALUE (value));
2059 g_object_ref (object);
2060 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
2062 pspec = g_param_spec_pool_lookup (pspec_pool,
2064 G_OBJECT_TYPE (object),
2067 g_warning ("%s: object class `%s' has no property named `%s'",
2069 G_OBJECT_TYPE_NAME (object),
2071 else if (!(pspec->flags & G_PARAM_WRITABLE))
2072 g_warning ("%s: property `%s' of object class `%s' is not writable",
2075 G_OBJECT_TYPE_NAME (object));
2076 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
2077 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
2078 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2080 object_set_property (object, pspec, value, nqueue);
2082 g_object_notify_queue_thaw (object, nqueue);
2083 g_object_unref (object);
2087 * g_object_get_property:
2088 * @object: a #GObject
2089 * @property_name: the name of the property to get
2090 * @value: return location for the property value
2092 * Gets a property of an object. @value must have been initialized to the
2093 * expected type of the property (or a type to which the expected type can be
2094 * transformed) using g_value_init().
2096 * In general, a copy is made of the property contents and the caller is
2097 * responsible for freeing the memory by calling g_value_unset().
2099 * Note that g_object_get_property() is really intended for language
2100 * bindings, g_object_get() is much more convenient for C programming.
2103 g_object_get_property (GObject *object,
2104 const gchar *property_name,
2109 g_return_if_fail (G_IS_OBJECT (object));
2110 g_return_if_fail (property_name != NULL);
2111 g_return_if_fail (G_IS_VALUE (value));
2113 g_object_ref (object);
2115 pspec = g_param_spec_pool_lookup (pspec_pool,
2117 G_OBJECT_TYPE (object),
2120 g_warning ("%s: object class `%s' has no property named `%s'",
2122 G_OBJECT_TYPE_NAME (object),
2124 else if (!(pspec->flags & G_PARAM_READABLE))
2125 g_warning ("%s: property `%s' of object class `%s' is not readable",
2128 G_OBJECT_TYPE_NAME (object));
2131 GValue *prop_value, tmp_value = G_VALUE_INIT;
2133 /* auto-conversion of the callers value type
2135 if (G_VALUE_TYPE (value) == pspec->value_type)
2137 g_value_reset (value);
2140 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2142 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
2143 G_STRFUNC, pspec->name,
2144 g_type_name (pspec->value_type),
2145 G_VALUE_TYPE_NAME (value));
2146 g_object_unref (object);
2151 g_value_init (&tmp_value, pspec->value_type);
2152 prop_value = &tmp_value;
2154 object_get_property (object, pspec, prop_value);
2155 if (prop_value != value)
2157 g_value_transform (prop_value, value);
2158 g_value_unset (&tmp_value);
2162 g_object_unref (object);
2166 * g_object_connect: (skip)
2167 * @object: a #GObject
2168 * @signal_spec: the spec for the first signal
2169 * @...: #GCallback for the first signal, followed by data for the
2170 * first signal, followed optionally by more signal
2171 * spec/callback/data triples, followed by %NULL
2173 * A convenience function to connect multiple signals at once.
2175 * The signal specs expected by this function have the form
2176 * "modifier::signal_name", where modifier can be one of the following:
2179 * <term>signal</term>
2181 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2182 * </para></listitem>
2185 * <term>object_signal</term>
2186 * <term>object-signal</term>
2188 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2189 * </para></listitem>
2192 * <term>swapped_signal</term>
2193 * <term>swapped-signal</term>
2195 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2196 * </para></listitem>
2199 * <term>swapped_object_signal</term>
2200 * <term>swapped-object-signal</term>
2202 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2203 * </para></listitem>
2206 * <term>signal_after</term>
2207 * <term>signal-after</term>
2209 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2210 * </para></listitem>
2213 * <term>object_signal_after</term>
2214 * <term>object-signal-after</term>
2216 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2217 * </para></listitem>
2220 * <term>swapped_signal_after</term>
2221 * <term>swapped-signal-after</term>
2223 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2224 * </para></listitem>
2227 * <term>swapped_object_signal_after</term>
2228 * <term>swapped-object-signal-after</term>
2230 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2231 * </para></listitem>
2236 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2237 * "type", GTK_WINDOW_POPUP,
2240 * "signal::event", gtk_menu_window_event, menu,
2241 * "signal::size_request", gtk_menu_window_size_request, menu,
2242 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2246 * Returns: (transfer none): @object
2249 g_object_connect (gpointer _object,
2250 const gchar *signal_spec,
2253 GObject *object = _object;
2256 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2257 g_return_val_if_fail (object->ref_count > 0, object);
2259 va_start (var_args, signal_spec);
2262 GCallback callback = va_arg (var_args, GCallback);
2263 gpointer data = va_arg (var_args, gpointer);
2265 if (strncmp (signal_spec, "signal::", 8) == 0)
2266 g_signal_connect_data (object, signal_spec + 8,
2267 callback, data, NULL,
2269 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2270 strncmp (signal_spec, "object-signal::", 15) == 0)
2271 g_signal_connect_object (object, signal_spec + 15,
2274 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2275 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2276 g_signal_connect_data (object, signal_spec + 16,
2277 callback, data, NULL,
2279 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2280 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2281 g_signal_connect_object (object, signal_spec + 23,
2284 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2285 strncmp (signal_spec, "signal-after::", 14) == 0)
2286 g_signal_connect_data (object, signal_spec + 14,
2287 callback, data, NULL,
2289 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2290 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2291 g_signal_connect_object (object, signal_spec + 21,
2294 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2295 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2296 g_signal_connect_data (object, signal_spec + 22,
2297 callback, data, NULL,
2298 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2299 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2300 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2301 g_signal_connect_object (object, signal_spec + 29,
2303 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2306 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2309 signal_spec = va_arg (var_args, gchar*);
2317 * g_object_disconnect: (skip)
2318 * @object: a #GObject
2319 * @signal_spec: the spec for the first signal
2320 * @...: #GCallback for the first signal, followed by data for the first signal,
2321 * followed optionally by more signal spec/callback/data triples,
2324 * A convenience function to disconnect multiple signals at once.
2326 * The signal specs expected by this function have the form
2327 * "any_signal", which means to disconnect any signal with matching
2328 * callback and data, or "any_signal::signal_name", which only
2329 * disconnects the signal named "signal_name".
2332 g_object_disconnect (gpointer _object,
2333 const gchar *signal_spec,
2336 GObject *object = _object;
2339 g_return_if_fail (G_IS_OBJECT (object));
2340 g_return_if_fail (object->ref_count > 0);
2342 va_start (var_args, signal_spec);
2345 GCallback callback = va_arg (var_args, GCallback);
2346 gpointer data = va_arg (var_args, gpointer);
2347 guint sid = 0, detail = 0, mask = 0;
2349 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2350 strncmp (signal_spec, "any-signal::", 12) == 0)
2353 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2355 else if (strcmp (signal_spec, "any_signal") == 0 ||
2356 strcmp (signal_spec, "any-signal") == 0)
2359 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2363 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2367 if ((mask & G_SIGNAL_MATCH_ID) &&
2368 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2369 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2370 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2372 NULL, (gpointer)callback, data))
2373 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2374 signal_spec = va_arg (var_args, gchar*);
2385 } weak_refs[1]; /* flexible array */
2389 weak_refs_notify (gpointer data)
2391 WeakRefStack *wstack = data;
2394 for (i = 0; i < wstack->n_weak_refs; i++)
2395 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2400 * g_object_weak_ref: (skip)
2401 * @object: #GObject to reference weakly
2402 * @notify: callback to invoke before the object is freed
2403 * @data: extra data to pass to notify
2405 * Adds a weak reference callback to an object. Weak references are
2406 * used for notification when an object is finalized. They are called
2407 * "weak references" because they allow you to safely hold a pointer
2408 * to an object without calling g_object_ref() (g_object_ref() adds a
2409 * strong reference, that is, forces the object to stay alive).
2412 g_object_weak_ref (GObject *object,
2416 WeakRefStack *wstack;
2419 g_return_if_fail (G_IS_OBJECT (object));
2420 g_return_if_fail (notify != NULL);
2421 g_return_if_fail (object->ref_count >= 1);
2423 G_LOCK (weak_refs_mutex);
2424 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2427 i = wstack->n_weak_refs++;
2428 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2432 wstack = g_renew (WeakRefStack, NULL, 1);
2433 wstack->object = object;
2434 wstack->n_weak_refs = 1;
2437 wstack->weak_refs[i].notify = notify;
2438 wstack->weak_refs[i].data = data;
2439 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2440 G_UNLOCK (weak_refs_mutex);
2444 * g_object_weak_unref: (skip)
2445 * @object: #GObject to remove a weak reference from
2446 * @notify: callback to search for
2447 * @data: data to search for
2449 * Removes a weak reference callback to an object.
2452 g_object_weak_unref (GObject *object,
2456 WeakRefStack *wstack;
2457 gboolean found_one = FALSE;
2459 g_return_if_fail (G_IS_OBJECT (object));
2460 g_return_if_fail (notify != NULL);
2462 G_LOCK (weak_refs_mutex);
2463 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2468 for (i = 0; i < wstack->n_weak_refs; i++)
2469 if (wstack->weak_refs[i].notify == notify &&
2470 wstack->weak_refs[i].data == data)
2473 wstack->n_weak_refs -= 1;
2474 if (i != wstack->n_weak_refs)
2475 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2480 G_UNLOCK (weak_refs_mutex);
2482 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2486 * g_object_add_weak_pointer: (skip)
2487 * @object: The object that should be weak referenced.
2488 * @weak_pointer_location: (inout): The memory address of a pointer.
2490 * Adds a weak reference from weak_pointer to @object to indicate that
2491 * the pointer located at @weak_pointer_location is only valid during
2492 * the lifetime of @object. When the @object is finalized,
2493 * @weak_pointer will be set to %NULL.
2496 g_object_add_weak_pointer (GObject *object,
2497 gpointer *weak_pointer_location)
2499 g_return_if_fail (G_IS_OBJECT (object));
2500 g_return_if_fail (weak_pointer_location != NULL);
2502 g_object_weak_ref (object,
2503 (GWeakNotify) g_nullify_pointer,
2504 weak_pointer_location);
2508 * g_object_remove_weak_pointer: (skip)
2509 * @object: The object that is weak referenced.
2510 * @weak_pointer_location: (inout): The memory address of a pointer.
2512 * Removes a weak reference from @object that was previously added
2513 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2514 * to match the one used with g_object_add_weak_pointer().
2517 g_object_remove_weak_pointer (GObject *object,
2518 gpointer *weak_pointer_location)
2520 g_return_if_fail (G_IS_OBJECT (object));
2521 g_return_if_fail (weak_pointer_location != NULL);
2523 g_object_weak_unref (object,
2524 (GWeakNotify) g_nullify_pointer,
2525 weak_pointer_location);
2529 object_floating_flag_handler (GObject *object,
2535 case +1: /* force floating if possible */
2537 oldvalue = g_atomic_pointer_get (&object->qdata);
2538 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2539 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2540 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2541 case -1: /* sink if possible */
2543 oldvalue = g_atomic_pointer_get (&object->qdata);
2544 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2545 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2546 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2547 default: /* check floating */
2548 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2553 * g_object_is_floating:
2554 * @object: (type GObject.Object): a #GObject
2556 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2561 * Returns: %TRUE if @object has a floating reference
2564 g_object_is_floating (gpointer _object)
2566 GObject *object = _object;
2567 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2568 return floating_flag_handler (object, 0);
2572 * g_object_ref_sink:
2573 * @object: (type GObject.Object): a #GObject
2575 * Increase the reference count of @object, and possibly remove the
2576 * <link linkend="floating-ref">floating</link> reference, if @object
2577 * has a floating reference.
2579 * In other words, if the object is floating, then this call "assumes
2580 * ownership" of the floating reference, converting it to a normal
2581 * reference by clearing the floating flag while leaving the reference
2582 * count unchanged. If the object is not floating, then this call
2583 * adds a new normal reference increasing the reference count by one.
2587 * Returns: (type GObject.Object) (transfer none): @object
2590 g_object_ref_sink (gpointer _object)
2592 GObject *object = _object;
2593 gboolean was_floating;
2594 g_return_val_if_fail (G_IS_OBJECT (object), object);
2595 g_return_val_if_fail (object->ref_count >= 1, object);
2596 g_object_ref (object);
2597 was_floating = floating_flag_handler (object, -1);
2599 g_object_unref (object);
2604 * g_object_force_floating:
2605 * @object: a #GObject
2607 * This function is intended for #GObject implementations to re-enforce a
2608 * <link linkend="floating-ref">floating</link> object reference.
2609 * Doing this is seldom required: all
2610 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2611 * usually just needs to be sunken by calling g_object_ref_sink().
2616 g_object_force_floating (GObject *object)
2618 g_return_if_fail (G_IS_OBJECT (object));
2619 g_return_if_fail (object->ref_count >= 1);
2621 floating_flag_handler (object, +1);
2626 guint n_toggle_refs;
2628 GToggleNotify notify;
2630 } toggle_refs[1]; /* flexible array */
2634 toggle_refs_notify (GObject *object,
2635 gboolean is_last_ref)
2637 ToggleRefStack tstack, *tstackptr;
2639 G_LOCK (toggle_refs_mutex);
2640 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2641 tstack = *tstackptr;
2642 G_UNLOCK (toggle_refs_mutex);
2644 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2645 * will only be notified when there is exactly one of them.
2647 g_assert (tstack.n_toggle_refs == 1);
2648 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2652 * g_object_add_toggle_ref: (skip)
2653 * @object: a #GObject
2654 * @notify: a function to call when this reference is the
2655 * last reference to the object, or is no longer
2656 * the last reference.
2657 * @data: data to pass to @notify
2659 * Increases the reference count of the object by one and sets a
2660 * callback to be called when all other references to the object are
2661 * dropped, or when this is already the last reference to the object
2662 * and another reference is established.
2664 * This functionality is intended for binding @object to a proxy
2665 * object managed by another memory manager. This is done with two
2666 * paired references: the strong reference added by
2667 * g_object_add_toggle_ref() and a reverse reference to the proxy
2668 * object which is either a strong reference or weak reference.
2670 * The setup is that when there are no other references to @object,
2671 * only a weak reference is held in the reverse direction from @object
2672 * to the proxy object, but when there are other references held to
2673 * @object, a strong reference is held. The @notify callback is called
2674 * when the reference from @object to the proxy object should be
2675 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2676 * true) or weak to strong (@is_last_ref false).
2678 * Since a (normal) reference must be held to the object before
2679 * calling g_object_add_toggle_ref(), the initial state of the reverse
2680 * link is always strong.
2682 * Multiple toggle references may be added to the same gobject,
2683 * however if there are multiple toggle references to an object, none
2684 * of them will ever be notified until all but one are removed. For
2685 * this reason, you should only ever use a toggle reference if there
2686 * is important state in the proxy object.
2691 g_object_add_toggle_ref (GObject *object,
2692 GToggleNotify notify,
2695 ToggleRefStack *tstack;
2698 g_return_if_fail (G_IS_OBJECT (object));
2699 g_return_if_fail (notify != NULL);
2700 g_return_if_fail (object->ref_count >= 1);
2702 g_object_ref (object);
2704 G_LOCK (toggle_refs_mutex);
2705 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2708 i = tstack->n_toggle_refs++;
2709 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2710 * in tstate->toggle_refs */
2711 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2715 tstack = g_renew (ToggleRefStack, NULL, 1);
2716 tstack->object = object;
2717 tstack->n_toggle_refs = 1;
2721 /* Set a flag for fast lookup after adding the first toggle reference */
2722 if (tstack->n_toggle_refs == 1)
2723 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2725 tstack->toggle_refs[i].notify = notify;
2726 tstack->toggle_refs[i].data = data;
2727 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2728 (GDestroyNotify)g_free);
2729 G_UNLOCK (toggle_refs_mutex);
2733 * g_object_remove_toggle_ref: (skip)
2734 * @object: a #GObject
2735 * @notify: a function to call when this reference is the
2736 * last reference to the object, or is no longer
2737 * the last reference.
2738 * @data: data to pass to @notify
2740 * Removes a reference added with g_object_add_toggle_ref(). The
2741 * reference count of the object is decreased by one.
2746 g_object_remove_toggle_ref (GObject *object,
2747 GToggleNotify notify,
2750 ToggleRefStack *tstack;
2751 gboolean found_one = FALSE;
2753 g_return_if_fail (G_IS_OBJECT (object));
2754 g_return_if_fail (notify != NULL);
2756 G_LOCK (toggle_refs_mutex);
2757 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2762 for (i = 0; i < tstack->n_toggle_refs; i++)
2763 if (tstack->toggle_refs[i].notify == notify &&
2764 tstack->toggle_refs[i].data == data)
2767 tstack->n_toggle_refs -= 1;
2768 if (i != tstack->n_toggle_refs)
2769 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2771 if (tstack->n_toggle_refs == 0)
2772 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2777 G_UNLOCK (toggle_refs_mutex);
2780 g_object_unref (object);
2782 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2787 * @object: (type GObject.Object): a #GObject
2789 * Increases the reference count of @object.
2791 * Returns: (type GObject.Object) (transfer none): the same @object
2794 g_object_ref (gpointer _object)
2796 GObject *object = _object;
2799 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2800 g_return_val_if_fail (object->ref_count > 0, NULL);
2802 #ifdef G_ENABLE_DEBUG
2803 if (g_trap_object_ref == object)
2805 #endif /* G_ENABLE_DEBUG */
2808 old_val = g_atomic_int_add (&object->ref_count, 1);
2810 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2811 toggle_refs_notify (object, FALSE);
2813 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2820 * @object: (type GObject.Object): a #GObject
2822 * Decreases the reference count of @object. When its reference count
2823 * drops to 0, the object is finalized (i.e. its memory is freed).
2826 g_object_unref (gpointer _object)
2828 GObject *object = _object;
2831 g_return_if_fail (G_IS_OBJECT (object));
2832 g_return_if_fail (object->ref_count > 0);
2834 #ifdef G_ENABLE_DEBUG
2835 if (g_trap_object_ref == object)
2837 #endif /* G_ENABLE_DEBUG */
2839 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2840 retry_atomic_decrement1:
2841 old_ref = g_atomic_int_get (&object->ref_count);
2844 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2845 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2847 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2848 goto retry_atomic_decrement1;
2850 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2852 /* if we went from 2->1 we need to notify toggle refs if any */
2853 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2854 toggle_refs_notify (object, TRUE);
2858 /* we are about tp remove the last reference */
2859 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2860 G_OBJECT_GET_CLASS (object)->dispose (object);
2861 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2863 /* may have been re-referenced meanwhile */
2864 retry_atomic_decrement2:
2865 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2868 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2869 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2871 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2872 goto retry_atomic_decrement2;
2874 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2876 /* if we went from 2->1 we need to notify toggle refs if any */
2877 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2878 toggle_refs_notify (object, TRUE);
2883 /* we are still in the process of taking away the last ref */
2884 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2885 g_signal_handlers_destroy (object);
2886 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2888 /* decrement the last reference */
2889 old_ref = g_atomic_int_add (&object->ref_count, -1);
2891 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2893 /* may have been re-referenced meanwhile */
2894 if (G_LIKELY (old_ref == 1))
2896 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2897 G_OBJECT_GET_CLASS (object)->finalize (object);
2899 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2901 #ifdef G_ENABLE_DEBUG
2904 /* catch objects not chaining finalize handlers */
2905 G_LOCK (debug_objects);
2906 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2907 G_UNLOCK (debug_objects);
2909 #endif /* G_ENABLE_DEBUG */
2910 g_type_free_instance ((GTypeInstance*) object);
2916 * g_clear_object: (skip)
2917 * @object_ptr: a pointer to a #GObject reference
2919 * Clears a reference to a #GObject.
2921 * @object_ptr must not be %NULL.
2923 * If the reference is %NULL then this function does nothing.
2924 * Otherwise, the reference count of the object is decreased and the
2925 * pointer is set to %NULL.
2927 * This function is threadsafe and modifies the pointer atomically,
2928 * using memory barriers where needed.
2930 * A macro is also included that allows this function to be used without
2935 #undef g_clear_object
2937 g_clear_object (volatile GObject **object_ptr)
2939 gpointer *ptr = (gpointer) object_ptr;
2942 /* This is a little frustrating.
2943 * Would be nice to have an atomic exchange (with no compare).
2946 old = g_atomic_pointer_get (ptr);
2947 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2950 g_object_unref (old);
2954 * g_object_get_qdata:
2955 * @object: The GObject to get a stored user data pointer from
2956 * @quark: A #GQuark, naming the user data pointer
2958 * This function gets back user data pointers stored via
2959 * g_object_set_qdata().
2961 * Returns: (transfer none): The user data pointer set, or %NULL
2964 g_object_get_qdata (GObject *object,
2967 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2969 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2973 * g_object_set_qdata: (skip)
2974 * @object: The GObject to set store a user data pointer
2975 * @quark: A #GQuark, naming the user data pointer
2976 * @data: An opaque user data pointer
2978 * This sets an opaque, named pointer on an object.
2979 * The name is specified through a #GQuark (retrived e.g. via
2980 * g_quark_from_static_string()), and the pointer
2981 * can be gotten back from the @object with g_object_get_qdata()
2982 * until the @object is finalized.
2983 * Setting a previously set user data pointer, overrides (frees)
2984 * the old pointer set, using #NULL as pointer essentially
2985 * removes the data stored.
2988 g_object_set_qdata (GObject *object,
2992 g_return_if_fail (G_IS_OBJECT (object));
2993 g_return_if_fail (quark > 0);
2995 g_datalist_id_set_data (&object->qdata, quark, data);
2999 * g_object_set_qdata_full: (skip)
3000 * @object: The GObject to set store a user data pointer
3001 * @quark: A #GQuark, naming the user data pointer
3002 * @data: An opaque user data pointer
3003 * @destroy: Function to invoke with @data as argument, when @data
3006 * This function works like g_object_set_qdata(), but in addition,
3007 * a void (*destroy) (gpointer) function may be specified which is
3008 * called with @data as argument when the @object is finalized, or
3009 * the data is being overwritten by a call to g_object_set_qdata()
3010 * with the same @quark.
3013 g_object_set_qdata_full (GObject *object,
3016 GDestroyNotify destroy)
3018 g_return_if_fail (G_IS_OBJECT (object));
3019 g_return_if_fail (quark > 0);
3021 g_datalist_id_set_data_full (&object->qdata, quark, data,
3022 data ? destroy : (GDestroyNotify) NULL);
3026 * g_object_steal_qdata:
3027 * @object: The GObject to get a stored user data pointer from
3028 * @quark: A #GQuark, naming the user data pointer
3030 * This function gets back user data pointers stored via
3031 * g_object_set_qdata() and removes the @data from object
3032 * without invoking its destroy() function (if any was
3034 * Usually, calling this function is only required to update
3035 * user data pointers with a destroy notifier, for example:
3038 * object_add_to_user_list (GObject *object,
3039 * const gchar *new_string)
3041 * // the quark, naming the object data
3042 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3043 * // retrive the old string list
3044 * GList *list = g_object_steal_qdata (object, quark_string_list);
3046 * // prepend new string
3047 * list = g_list_prepend (list, g_strdup (new_string));
3048 * // this changed 'list', so we need to set it again
3049 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3052 * free_string_list (gpointer data)
3054 * GList *node, *list = data;
3056 * for (node = list; node; node = node->next)
3057 * g_free (node->data);
3058 * g_list_free (list);
3061 * Using g_object_get_qdata() in the above example, instead of
3062 * g_object_steal_qdata() would have left the destroy function set,
3063 * and thus the partial string list would have been freed upon
3064 * g_object_set_qdata_full().
3066 * Returns: (transfer full): The user data pointer set, or %NULL
3069 g_object_steal_qdata (GObject *object,
3072 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3073 g_return_val_if_fail (quark > 0, NULL);
3075 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3079 * g_object_get_data:
3080 * @object: #GObject containing the associations
3081 * @key: name of the key for that association
3083 * Gets a named field from the objects table of associations (see g_object_set_data()).
3085 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3088 g_object_get_data (GObject *object,
3091 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3092 g_return_val_if_fail (key != NULL, NULL);
3094 return g_datalist_get_data (&object->qdata, key);
3098 * g_object_set_data:
3099 * @object: #GObject containing the associations.
3100 * @key: name of the key
3101 * @data: data to associate with that key
3103 * Each object carries around a table of associations from
3104 * strings to pointers. This function lets you set an association.
3106 * If the object already had an association with that name,
3107 * the old association will be destroyed.
3110 g_object_set_data (GObject *object,
3114 g_return_if_fail (G_IS_OBJECT (object));
3115 g_return_if_fail (key != NULL);
3117 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3121 * g_object_set_data_full: (skip)
3122 * @object: #GObject containing the associations
3123 * @key: name of the key
3124 * @data: data to associate with that key
3125 * @destroy: function to call when the association is destroyed
3127 * Like g_object_set_data() except it adds notification
3128 * for when the association is destroyed, either by setting it
3129 * to a different value or when the object is destroyed.
3131 * Note that the @destroy callback is not called if @data is %NULL.
3134 g_object_set_data_full (GObject *object,
3137 GDestroyNotify destroy)
3139 g_return_if_fail (G_IS_OBJECT (object));
3140 g_return_if_fail (key != NULL);
3142 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3143 data ? destroy : (GDestroyNotify) NULL);
3147 * g_object_steal_data:
3148 * @object: #GObject containing the associations
3149 * @key: name of the key
3151 * Remove a specified datum from the object's data associations,
3152 * without invoking the association's destroy handler.
3154 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3157 g_object_steal_data (GObject *object,
3162 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3163 g_return_val_if_fail (key != NULL, NULL);
3165 quark = g_quark_try_string (key);
3167 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3171 g_value_object_init (GValue *value)
3173 value->data[0].v_pointer = NULL;
3177 g_value_object_free_value (GValue *value)
3179 if (value->data[0].v_pointer)
3180 g_object_unref (value->data[0].v_pointer);
3184 g_value_object_copy_value (const GValue *src_value,
3187 if (src_value->data[0].v_pointer)
3188 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3190 dest_value->data[0].v_pointer = NULL;
3194 g_value_object_transform_value (const GValue *src_value,
3197 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)))
3198 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3200 dest_value->data[0].v_pointer = NULL;
3204 g_value_object_peek_pointer (const GValue *value)
3206 return value->data[0].v_pointer;
3210 g_value_object_collect_value (GValue *value,
3211 guint n_collect_values,
3212 GTypeCValue *collect_values,
3213 guint collect_flags)
3215 if (collect_values[0].v_pointer)
3217 GObject *object = collect_values[0].v_pointer;
3219 if (object->g_type_instance.g_class == NULL)
3220 return g_strconcat ("invalid unclassed object pointer for value type `",
3221 G_VALUE_TYPE_NAME (value),
3224 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3225 return g_strconcat ("invalid object type `",
3226 G_OBJECT_TYPE_NAME (object),
3227 "' for value type `",
3228 G_VALUE_TYPE_NAME (value),
3231 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3232 value->data[0].v_pointer = g_object_ref (object);
3235 value->data[0].v_pointer = NULL;
3241 g_value_object_lcopy_value (const GValue *value,
3242 guint n_collect_values,
3243 GTypeCValue *collect_values,
3244 guint collect_flags)
3246 GObject **object_p = collect_values[0].v_pointer;
3249 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3251 if (!value->data[0].v_pointer)
3253 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3254 *object_p = value->data[0].v_pointer;
3256 *object_p = g_object_ref (value->data[0].v_pointer);
3262 * g_value_set_object:
3263 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3264 * @v_object: (type GObject.Object) (allow-none): object value to be set
3266 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3268 * g_value_set_object() increases the reference count of @v_object
3269 * (the #GValue holds a reference to @v_object). If you do not wish
3270 * to increase the reference count of the object (i.e. you wish to
3271 * pass your current reference to the #GValue because you no longer
3272 * need it), use g_value_take_object() instead.
3274 * It is important that your #GValue holds a reference to @v_object (either its
3275 * own, or one it has taken) to ensure that the object won't be destroyed while
3276 * the #GValue still exists).
3279 g_value_set_object (GValue *value,
3284 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3286 old = value->data[0].v_pointer;
3290 g_return_if_fail (G_IS_OBJECT (v_object));
3291 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3293 value->data[0].v_pointer = v_object;
3294 g_object_ref (value->data[0].v_pointer);
3297 value->data[0].v_pointer = NULL;
3300 g_object_unref (old);
3304 * g_value_set_object_take_ownership: (skip)
3305 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3306 * @v_object: (allow-none): object value to be set
3308 * This is an internal function introduced mainly for C marshallers.
3310 * Deprecated: 2.4: Use g_value_take_object() instead.
3313 g_value_set_object_take_ownership (GValue *value,
3316 g_value_take_object (value, v_object);
3320 * g_value_take_object: (skip)
3321 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3322 * @v_object: (allow-none): object value to be set
3324 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3325 * and takes over the ownership of the callers reference to @v_object;
3326 * the caller doesn't have to unref it any more (i.e. the reference
3327 * count of the object is not increased).
3329 * If you want the #GValue to hold its own reference to @v_object, use
3330 * g_value_set_object() instead.
3335 g_value_take_object (GValue *value,
3338 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3340 if (value->data[0].v_pointer)
3342 g_object_unref (value->data[0].v_pointer);
3343 value->data[0].v_pointer = NULL;
3348 g_return_if_fail (G_IS_OBJECT (v_object));
3349 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3351 value->data[0].v_pointer = v_object; /* we take over the reference count */
3356 * g_value_get_object:
3357 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3359 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3361 * Returns: (type GObject.Object) (transfer none): object contents of @value
3364 g_value_get_object (const GValue *value)
3366 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3368 return value->data[0].v_pointer;
3372 * g_value_dup_object:
3373 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3375 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3376 * its reference count. If the contents of the #GValue are %NULL, then
3377 * %NULL will be returned.
3379 * Returns: (type GObject.Object) (transfer full): object content of @value,
3380 * should be unreferenced when no longer needed.
3383 g_value_dup_object (const GValue *value)
3385 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3387 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3391 * g_signal_connect_object: (skip)
3392 * @instance: the instance to connect to.
3393 * @detailed_signal: a string of the form "signal-name::detail".
3394 * @c_handler: the #GCallback to connect.
3395 * @gobject: the object to pass as data to @c_handler.
3396 * @connect_flags: a combination of #GConnectFlags.
3398 * This is similar to g_signal_connect_data(), but uses a closure which
3399 * ensures that the @gobject stays alive during the call to @c_handler
3400 * by temporarily adding a reference count to @gobject.
3402 * Note that there is a bug in GObject that makes this function
3403 * much less useful than it might seem otherwise. Once @gobject is
3404 * disposed, the callback will no longer be called, but, the signal
3405 * handler is <emphasis>not</emphasis> currently disconnected. If the
3406 * @instance is itself being freed at the same time than this doesn't
3407 * matter, since the signal will automatically be removed, but
3408 * if @instance persists, then the signal handler will leak. You
3409 * should not remove the signal yourself because in a future versions of
3410 * GObject, the handler <emphasis>will</emphasis> automatically
3413 * It's possible to work around this problem in a way that will
3414 * continue to work with future versions of GObject by checking
3415 * that the signal handler is still connected before disconnected it:
3416 * <informalexample><programlisting>
3417 * if (g_signal_handler_is_connected (instance, id))
3418 * g_signal_handler_disconnect (instance, id);
3419 * </programlisting></informalexample>
3421 * Returns: the handler id.
3424 g_signal_connect_object (gpointer instance,
3425 const gchar *detailed_signal,
3426 GCallback c_handler,
3428 GConnectFlags connect_flags)
3430 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3431 g_return_val_if_fail (detailed_signal != NULL, 0);
3432 g_return_val_if_fail (c_handler != NULL, 0);
3438 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3440 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3442 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3445 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3451 GClosure *closures[1]; /* flexible array */
3453 /* don't change this structure without supplying an accessor for
3454 * watched closures, e.g.:
3455 * GSList* g_object_list_watched_closures (GObject *object)
3458 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3459 * carray = g_object_get_data (object, "GObject-closure-array");
3462 * GSList *slist = NULL;
3464 * for (i = 0; i < carray->n_closures; i++)
3465 * slist = g_slist_prepend (slist, carray->closures[i]);
3473 object_remove_closure (gpointer data,
3476 GObject *object = data;
3480 G_LOCK (closure_array_mutex);
3481 carray = g_object_get_qdata (object, quark_closure_array);
3482 for (i = 0; i < carray->n_closures; i++)
3483 if (carray->closures[i] == closure)
3485 carray->n_closures--;
3486 if (i < carray->n_closures)
3487 carray->closures[i] = carray->closures[carray->n_closures];
3488 G_UNLOCK (closure_array_mutex);
3491 G_UNLOCK (closure_array_mutex);
3492 g_assert_not_reached ();
3496 destroy_closure_array (gpointer data)
3498 CArray *carray = data;
3499 GObject *object = carray->object;
3500 guint i, n = carray->n_closures;
3502 for (i = 0; i < n; i++)
3504 GClosure *closure = carray->closures[i];
3506 /* removing object_remove_closure() upfront is probably faster than
3507 * letting it fiddle with quark_closure_array which is empty anyways
3509 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3510 g_closure_invalidate (closure);
3516 * g_object_watch_closure:
3517 * @object: GObject restricting lifetime of @closure
3518 * @closure: GClosure to watch
3520 * This function essentially limits the life time of the @closure to
3521 * the life time of the object. That is, when the object is finalized,
3522 * the @closure is invalidated by calling g_closure_invalidate() on
3523 * it, in order to prevent invocations of the closure with a finalized
3524 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3525 * added as marshal guards to the @closure, to ensure that an extra
3526 * reference count is held on @object during invocation of the
3527 * @closure. Usually, this function will be called on closures that
3528 * use this @object as closure data.
3531 g_object_watch_closure (GObject *object,
3537 g_return_if_fail (G_IS_OBJECT (object));
3538 g_return_if_fail (closure != NULL);
3539 g_return_if_fail (closure->is_invalid == FALSE);
3540 g_return_if_fail (closure->in_marshal == FALSE);
3541 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3543 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3544 g_closure_add_marshal_guards (closure,
3545 object, (GClosureNotify) g_object_ref,
3546 object, (GClosureNotify) g_object_unref);
3547 G_LOCK (closure_array_mutex);
3548 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3551 carray = g_renew (CArray, NULL, 1);
3552 carray->object = object;
3553 carray->n_closures = 1;
3558 i = carray->n_closures++;
3559 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3561 carray->closures[i] = closure;
3562 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3563 G_UNLOCK (closure_array_mutex);
3567 * g_closure_new_object:
3568 * @sizeof_closure: the size of the structure to allocate, must be at least
3569 * <literal>sizeof (GClosure)</literal>
3570 * @object: a #GObject pointer to store in the @data field of the newly
3571 * allocated #GClosure
3573 * A variant of g_closure_new_simple() which stores @object in the
3574 * @data field of the closure and calls g_object_watch_closure() on
3575 * @object and the created closure. This function is mainly useful
3576 * when implementing new types of closures.
3578 * Returns: (transfer full): a newly allocated #GClosure
3581 g_closure_new_object (guint sizeof_closure,
3586 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3587 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3589 closure = g_closure_new_simple (sizeof_closure, object);
3590 g_object_watch_closure (object, closure);
3596 * g_cclosure_new_object: (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() which uses @object as @user_data and
3601 * 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 (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 (callback_func, object, NULL);
3619 g_object_watch_closure (object, closure);
3625 * g_cclosure_new_object_swap: (skip)
3626 * @callback_func: the function to invoke
3627 * @object: a #GObject pointer to pass to @callback_func
3629 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3630 * and calls g_object_watch_closure() on @object and the created
3631 * closure. This function is useful when you have a callback closely
3632 * associated with a #GObject, and want the callback to no longer run
3633 * after the object is is freed.
3635 * Returns: a new #GCClosure
3638 g_cclosure_new_object_swap (GCallback callback_func,
3643 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3644 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3645 g_return_val_if_fail (callback_func != NULL, NULL);
3647 closure = g_cclosure_new_swap (callback_func, object, NULL);
3648 g_object_watch_closure (object, closure);
3654 g_object_compat_control (gsize what,
3660 case 1: /* floating base type */
3661 return G_TYPE_INITIALLY_UNOWNED;
3662 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3663 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3665 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3667 *pp = floating_flag_handler;
3674 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3677 g_initially_unowned_init (GInitiallyUnowned *object)
3679 g_object_force_floating (object);
3683 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)