Better documentation for g_value_dup_object().
[glib.git] / gobject / gobject.c
blobc31ec5241ee4669559a432ac4d5c3e1502529102
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.
24 #include "config.h"
26 #include <string.h>
27 #include <signal.h>
29 #include "gobject.h"
30 #include "gvaluecollector.h"
31 #include "gsignal.h"
32 #include "gparamspecs.h"
33 #include "gvaluetypes.h"
34 #include "gobject_trace.h"
36 #include "gobjectnotifyqueue.c"
38 /**
39 * SECTION:objects
40 * @short_description: The base object type
41 * @see_also: #GParamSpecObject, g_param_spec_object()
42 * @title: The Base Object Type
44 * GObject is the fundamental type providing the common attributes and
45 * methods for all object types in GTK+, Pango and other libraries
46 * based on GObject. The GObject class provides methods for object
47 * construction and destruction, property access methods, and signal
48 * support. Signals are described in detail in <xref
49 * linkend="gobject-Signals"/>.
51 * <para id="floating-ref">
52 * #GInitiallyUnowned is derived from #GObject. The only difference between
53 * the two is that the initial reference of a #GInitiallyUnowned is flagged
54 * as a <firstterm>floating</firstterm> reference.
55 * This means that it is not specifically claimed to be "owned" by
56 * any code portion. The main motivation for providing floating references is
57 * C convenience. In particular, it allows code to be written as:
58 * |[
59 * container = create_container();
60 * container_add_child (container, create_child());
61 * ]|
62 * If <function>container_add_child()</function> will g_object_ref_sink() the
63 * passed in child, no reference of the newly created child is leaked.
64 * Without floating references, <function>container_add_child()</function>
65 * can only g_object_ref() the new child, so to implement this code without
66 * reference leaks, it would have to be written as:
67 * |[
68 * Child *child;
69 * container = create_container();
70 * child = create_child();
71 * container_add_child (container, child);
72 * g_object_unref (child);
73 * ]|
74 * The floating reference can be converted into
75 * an ordinary reference by calling g_object_ref_sink().
76 * For already sunken objects (objects that don't have a floating reference
77 * anymore), g_object_ref_sink() is equivalent to g_object_ref() and returns
78 * a new reference.
79 * Since floating references are useful almost exclusively for C convenience,
80 * language bindings that provide automated reference and memory ownership
81 * maintenance (such as smart pointers or garbage collection) therefore don't
82 * need to expose floating references in their API.
83 * </para>
85 * Some object implementations may need to save an objects floating state
86 * across certain code portions (an example is #GtkMenu), to achive this, the
87 * following sequence can be used:
89 * |[
90 * // save floating state
91 * gboolean was_floating = g_object_is_floating (object);
92 * g_object_ref_sink (object);
93 * // protected code portion
94 * ...;
95 * // restore floating state
96 * if (was_floating)
97 * g_object_force_floating (object);
98 * g_obejct_unref (object); // release previously acquired reference
99 * ]|
103 /* --- macros --- */
104 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
105 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
107 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
108 #define OBJECT_HAS_TOGGLE_REF(object) \
109 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
110 #define OBJECT_FLOATING_FLAG 0x2
112 #define CLASS_HAS_PROPS_FLAG 0x1
113 #define CLASS_HAS_PROPS(class) \
114 ((class)->flags & CLASS_HAS_PROPS_FLAG)
115 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
116 ((class)->constructor != g_object_constructor)
117 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
118 ((class)->constructed != g_object_constructed)
120 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
121 #define CLASS_HAS_DERIVED_CLASS(class) \
122 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
124 /* --- signals --- */
125 enum {
126 NOTIFY,
127 LAST_SIGNAL
131 /* --- properties --- */
132 enum {
133 PROP_NONE
137 /* --- prototypes --- */
138 static void g_object_base_class_init (GObjectClass *class);
139 static void g_object_base_class_finalize (GObjectClass *class);
140 static void g_object_do_class_init (GObjectClass *class);
141 static void g_object_init (GObject *object,
142 GObjectClass *class);
143 static GObject* g_object_constructor (GType type,
144 guint n_construct_properties,
145 GObjectConstructParam *construct_params);
146 static void g_object_constructed (GObject *object);
147 static void g_object_real_dispose (GObject *object);
148 static void g_object_finalize (GObject *object);
149 static void g_object_do_set_property (GObject *object,
150 guint property_id,
151 const GValue *value,
152 GParamSpec *pspec);
153 static void g_object_do_get_property (GObject *object,
154 guint property_id,
155 GValue *value,
156 GParamSpec *pspec);
157 static void g_value_object_init (GValue *value);
158 static void g_value_object_free_value (GValue *value);
159 static void g_value_object_copy_value (const GValue *src_value,
160 GValue *dest_value);
161 static void g_value_object_transform_value (const GValue *src_value,
162 GValue *dest_value);
163 static gpointer g_value_object_peek_pointer (const GValue *value);
164 static gchar* g_value_object_collect_value (GValue *value,
165 guint n_collect_values,
166 GTypeCValue *collect_values,
167 guint collect_flags);
168 static gchar* g_value_object_lcopy_value (const GValue *value,
169 guint n_collect_values,
170 GTypeCValue *collect_values,
171 guint collect_flags);
172 static void g_object_dispatch_properties_changed (GObject *object,
173 guint n_pspecs,
174 GParamSpec **pspecs);
175 static inline void object_get_property (GObject *object,
176 GParamSpec *pspec,
177 GValue *value);
178 static inline void object_set_property (GObject *object,
179 GParamSpec *pspec,
180 const GValue *value,
181 GObjectNotifyQueue *nqueue);
182 static guint object_floating_flag_handler (GObject *object,
183 gint job);
185 static void object_interface_check_properties (gpointer func_data,
186 gpointer g_iface);
189 /* --- variables --- */
190 G_LOCK_DEFINE_STATIC (closure_array_mutex);
191 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
192 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
193 static GQuark quark_closure_array = 0;
194 static GQuark quark_weak_refs = 0;
195 static GQuark quark_toggle_refs = 0;
196 static GParamSpecPool *pspec_pool = NULL;
197 static GObjectNotifyContext property_notify_context = { 0, };
198 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
199 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
200 G_LOCK_DEFINE_STATIC (construction_mutex);
201 static GSList *construction_objects = NULL;
203 /* --- functions --- */
204 #ifdef G_ENABLE_DEBUG
205 #define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
206 G_LOCK_DEFINE_STATIC (debug_objects);
207 static volatile GObject *g_trap_object_ref = NULL;
208 static guint debug_objects_count = 0;
209 static GHashTable *debug_objects_ht = NULL;
211 static void
212 debug_objects_foreach (gpointer key,
213 gpointer value,
214 gpointer user_data)
216 GObject *object = value;
218 g_message ("[%p] stale %s\tref_count=%u",
219 object,
220 G_OBJECT_TYPE_NAME (object),
221 object->ref_count);
224 static void
225 debug_objects_atexit (void)
227 IF_DEBUG (OBJECTS)
229 G_LOCK (debug_objects);
230 g_message ("stale GObjects: %u", debug_objects_count);
231 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
232 G_UNLOCK (debug_objects);
235 #endif /* G_ENABLE_DEBUG */
237 void
238 g_object_type_init (void)
240 static gboolean initialized = FALSE;
241 static const GTypeFundamentalInfo finfo = {
242 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
244 static GTypeInfo info = {
245 sizeof (GObjectClass),
246 (GBaseInitFunc) g_object_base_class_init,
247 (GBaseFinalizeFunc) g_object_base_class_finalize,
248 (GClassInitFunc) g_object_do_class_init,
249 NULL /* class_destroy */,
250 NULL /* class_data */,
251 sizeof (GObject),
252 0 /* n_preallocs */,
253 (GInstanceInitFunc) g_object_init,
254 NULL, /* value_table */
256 static const GTypeValueTable value_table = {
257 g_value_object_init, /* value_init */
258 g_value_object_free_value, /* value_free */
259 g_value_object_copy_value, /* value_copy */
260 g_value_object_peek_pointer, /* value_peek_pointer */
261 "p", /* collect_format */
262 g_value_object_collect_value, /* collect_value */
263 "p", /* lcopy_format */
264 g_value_object_lcopy_value, /* lcopy_value */
266 GType type;
268 g_return_if_fail (initialized == FALSE);
269 initialized = TRUE;
271 /* G_TYPE_OBJECT
273 info.value_table = &value_table;
274 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
275 g_assert (type == G_TYPE_OBJECT);
276 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
278 #ifdef G_ENABLE_DEBUG
279 IF_DEBUG (OBJECTS)
281 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
282 g_atexit (debug_objects_atexit);
284 #endif /* G_ENABLE_DEBUG */
287 static void
288 g_object_base_class_init (GObjectClass *class)
290 GObjectClass *pclass = g_type_class_peek_parent (class);
292 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
293 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
295 if (pclass)
296 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
298 /* reset instance specific fields and methods that don't get inherited */
299 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
300 class->get_property = NULL;
301 class->set_property = NULL;
304 static void
305 g_object_base_class_finalize (GObjectClass *class)
307 GList *list, *node;
309 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
311 g_slist_free (class->construct_properties);
312 class->construct_properties = NULL;
313 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
314 for (node = list; node; node = node->next)
316 GParamSpec *pspec = node->data;
318 g_param_spec_pool_remove (pspec_pool, pspec);
319 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
320 g_param_spec_unref (pspec);
322 g_list_free (list);
325 static void
326 g_object_notify_dispatcher (GObject *object,
327 guint n_pspecs,
328 GParamSpec **pspecs)
330 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
333 static void
334 g_object_do_class_init (GObjectClass *class)
336 /* read the comment about typedef struct CArray; on why not to change this quark */
337 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
339 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
340 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
341 pspec_pool = g_param_spec_pool_new (TRUE);
342 property_notify_context.quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
343 property_notify_context.dispatcher = g_object_notify_dispatcher;
345 class->constructor = g_object_constructor;
346 class->constructed = g_object_constructed;
347 class->set_property = g_object_do_set_property;
348 class->get_property = g_object_do_get_property;
349 class->dispose = g_object_real_dispose;
350 class->finalize = g_object_finalize;
351 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
352 class->notify = NULL;
355 * GObject::notify:
356 * @gobject: the object which received the signal.
357 * @pspec: the #GParamSpec of the property which changed.
359 * The notify signal is emitted on an object when one of its
360 * properties has been changed. Note that getting this signal
361 * doesn't guarantee that the value of the property has actually
362 * changed, it may also be emitted when the setter for the property
363 * is called to reinstate the previous value.
365 * This signal is typically used to obtain change notification for a
366 * single property, by specifying the property name as a detail in the
367 * g_signal_connect() call, like this:
368 * |[
369 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
370 * G_CALLBACK (gtk_text_view_target_list_notify),
371 * text_view)
372 * ]|
373 * It is important to note that you must use
374 * <link linkend="canonical-parameter-name">canonical</link> parameter names as
375 * detail strings for the notify signal.
377 gobject_signals[NOTIFY] =
378 g_signal_new (g_intern_static_string ("notify"),
379 G_TYPE_FROM_CLASS (class),
380 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
381 G_STRUCT_OFFSET (GObjectClass, notify),
382 NULL, NULL,
383 g_cclosure_marshal_VOID__PARAM,
384 G_TYPE_NONE,
385 1, G_TYPE_PARAM);
387 /* Install a check function that we'll use to verify that classes that
388 * implement an interface implement all properties for that interface
390 g_type_add_interface_check (NULL, object_interface_check_properties);
393 static inline void
394 install_property_internal (GType g_type,
395 guint property_id,
396 GParamSpec *pspec)
398 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
400 g_warning ("When installing property: type `%s' already has a property named `%s'",
401 g_type_name (g_type),
402 pspec->name);
403 return;
406 g_param_spec_ref (pspec);
407 g_param_spec_sink (pspec);
408 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
409 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
413 * g_object_class_install_property:
414 * @oclass: a #GObjectClass
415 * @property_id: the id for the new property
416 * @pspec: the #GParamSpec for the new property
418 * Installs a new property. This is usually done in the class initializer.
420 * Note that it is possible to redefine a property in a derived class,
421 * by installing a property with the same name. This can be useful at times,
422 * e.g. to change the range of allowed values or the default value.
424 void
425 g_object_class_install_property (GObjectClass *class,
426 guint property_id,
427 GParamSpec *pspec)
429 g_return_if_fail (G_IS_OBJECT_CLASS (class));
430 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
432 if (CLASS_HAS_DERIVED_CLASS (class))
433 g_error ("Attempt to add property %s::%s to class after it was derived",
434 G_OBJECT_CLASS_NAME (class), pspec->name);
436 class->flags |= CLASS_HAS_PROPS_FLAG;
438 if (pspec->flags & G_PARAM_WRITABLE)
439 g_return_if_fail (class->set_property != NULL);
440 if (pspec->flags & G_PARAM_READABLE)
441 g_return_if_fail (class->get_property != NULL);
442 g_return_if_fail (property_id > 0);
443 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
444 if (pspec->flags & G_PARAM_CONSTRUCT)
445 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
446 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
447 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
449 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
451 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
452 class->construct_properties = g_slist_prepend (class->construct_properties, pspec);
454 /* for property overrides of construct properties, we have to get rid
455 * of the overidden inherited construct property
457 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
458 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
459 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
463 * g_object_class_install_properties:
464 * @oclass: a #GObjectClass
465 * @n_pspecs: the length of the #GParamSpec<!-- -->s array
466 * @pspecs: (array length=n_pspecs): the #GParamSpec<!-- -->s array
467 * defining the new properties
469 * Installs new properties from an array of #GParamSpec<!-- -->s. This is
470 * usually done in the class initializer.
472 * The property id of each property is the index of each #GParamSpec in
473 * the @pspecs array.
475 * The property id of 0 is treated specially by #GObject and it should not
476 * be used to store a #GParamSpec.
478 * This function should be used if you plan to use a static array of
479 * #GParamSpec<!-- -->s and g_object_notify_by_pspec(). For instance, this
480 * class initialization:
482 * |[
483 * enum {
484 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
485 * };
487 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
489 * static void
490 * my_object_class_init (MyObjectClass *klass)
492 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
494 * obj_properties[PROP_FOO] =
495 * g_param_spec_int ("foo", "Foo", "Foo",
496 * -1, G_MAXINT,
497 * 0,
498 * G_PARAM_READWRITE);
500 * obj_properties[PROP_BAR] =
501 * g_param_spec_string ("bar", "Bar", "Bar",
502 * NULL,
503 * G_PARAM_READWRITE);
505 * gobject_class->set_property = my_object_set_property;
506 * gobject_class->get_property = my_object_get_property;
507 * g_object_class_install_properties (gobject_class,
508 * N_PROPERTIES,
509 * obj_properties);
511 * ]|
513 * allows calling g_object_notify_by_pspec() to notify of property changes:
515 * |[
516 * void
517 * my_object_set_foo (MyObject *self, gint foo)
519 * if (self->foo != foo)
521 * self->foo = foo;
522 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
525 * ]|
527 * Since: 2.26
529 void
530 g_object_class_install_properties (GObjectClass *oclass,
531 guint n_pspecs,
532 GParamSpec **pspecs)
534 GType oclass_type, parent_type;
535 gint i;
537 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
538 g_return_if_fail (n_pspecs > 1);
539 g_return_if_fail (pspecs[0] == NULL);
541 if (CLASS_HAS_DERIVED_CLASS (oclass))
542 g_error ("Attempt to add properties to %s after it was derived",
543 G_OBJECT_CLASS_NAME (oclass));
545 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
546 parent_type = g_type_parent (oclass_type);
548 /* we skip the first element of the array as it would have a 0 prop_id */
549 for (i = 1; i < n_pspecs; i++)
551 GParamSpec *pspec = pspecs[i];
553 g_return_if_fail (pspec != NULL);
555 if (pspec->flags & G_PARAM_WRITABLE)
556 g_return_if_fail (oclass->set_property != NULL);
557 if (pspec->flags & G_PARAM_READABLE)
558 g_return_if_fail (oclass->get_property != NULL);
559 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
560 if (pspec->flags & G_PARAM_CONSTRUCT)
561 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
562 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
563 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
565 oclass->flags |= CLASS_HAS_PROPS_FLAG;
566 install_property_internal (oclass_type, i, pspec);
568 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
569 oclass->construct_properties = g_slist_prepend (oclass->construct_properties, pspec);
571 /* for property overrides of construct properties, we have to get rid
572 * of the overidden inherited construct property
574 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
575 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
576 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
581 * g_object_interface_install_property:
582 * @g_iface: any interface vtable for the interface, or the default
583 * vtable for the interface.
584 * @pspec: the #GParamSpec for the new property
586 * Add a property to an interface; this is only useful for interfaces
587 * that are added to GObject-derived types. Adding a property to an
588 * interface forces all objects classes with that interface to have a
589 * compatible property. The compatible property could be a newly
590 * created #GParamSpec, but normally
591 * g_object_class_override_property() will be used so that the object
592 * class only needs to provide an implementation and inherits the
593 * property description, default value, bounds, and so forth from the
594 * interface property.
596 * This function is meant to be called from the interface's default
597 * vtable initialization function (the @class_init member of
598 * #GTypeInfo.) It must not be called after after @class_init has
599 * been called for any object types implementing this interface.
601 * Since: 2.4
603 void
604 g_object_interface_install_property (gpointer g_iface,
605 GParamSpec *pspec)
607 GTypeInterface *iface_class = g_iface;
609 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
610 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
611 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
612 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
614 install_property_internal (iface_class->g_type, 0, pspec);
618 * g_object_class_find_property:
619 * @oclass: a #GObjectClass
620 * @property_name: the name of the property to look up
622 * Looks up the #GParamSpec for a property of a class.
624 * Returns: (transfer none): the #GParamSpec for the property, or
625 * %NULL if the class doesn't have a property of that name
627 GParamSpec*
628 g_object_class_find_property (GObjectClass *class,
629 const gchar *property_name)
631 GParamSpec *pspec;
632 GParamSpec *redirect;
634 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
635 g_return_val_if_fail (property_name != NULL, NULL);
637 pspec = g_param_spec_pool_lookup (pspec_pool,
638 property_name,
639 G_OBJECT_CLASS_TYPE (class),
640 TRUE);
641 if (pspec)
643 redirect = g_param_spec_get_redirect_target (pspec);
644 if (redirect)
645 return redirect;
646 else
647 return pspec;
649 else
650 return NULL;
654 * g_object_interface_find_property:
655 * @g_iface: any interface vtable for the interface, or the default
656 * vtable for the interface
657 * @property_name: name of a property to lookup.
659 * Find the #GParamSpec with the given name for an
660 * interface. Generally, the interface vtable passed in as @g_iface
661 * will be the default vtable from g_type_default_interface_ref(), or,
662 * if you know the interface has already been loaded,
663 * g_type_default_interface_peek().
665 * Since: 2.4
667 * Returns: (transfer none): the #GParamSpec for the property of the
668 * interface with the name @property_name, or %NULL if no
669 * such property exists.
671 GParamSpec*
672 g_object_interface_find_property (gpointer g_iface,
673 const gchar *property_name)
675 GTypeInterface *iface_class = g_iface;
677 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
678 g_return_val_if_fail (property_name != NULL, NULL);
680 return g_param_spec_pool_lookup (pspec_pool,
681 property_name,
682 iface_class->g_type,
683 FALSE);
687 * g_object_class_override_property:
688 * @oclass: a #GObjectClass
689 * @property_id: the new property ID
690 * @name: the name of a property registered in a parent class or
691 * in an interface of this class.
693 * Registers @property_id as referring to a property with the
694 * name @name in a parent class or in an interface implemented
695 * by @oclass. This allows this class to <firstterm>override</firstterm>
696 * a property implementation in a parent class or to provide
697 * the implementation of a property from an interface.
699 * <note>
700 * Internally, overriding is implemented by creating a property of type
701 * #GParamSpecOverride; generally operations that query the properties of
702 * the object class, such as g_object_class_find_property() or
703 * g_object_class_list_properties() will return the overridden
704 * property. However, in one case, the @construct_properties argument of
705 * the @constructor virtual function, the #GParamSpecOverride is passed
706 * instead, so that the @param_id field of the #GParamSpec will be
707 * correct. For virtually all uses, this makes no difference. If you
708 * need to get the overridden property, you can call
709 * g_param_spec_get_redirect_target().
710 * </note>
712 * Since: 2.4
714 void
715 g_object_class_override_property (GObjectClass *oclass,
716 guint property_id,
717 const gchar *name)
719 GParamSpec *overridden = NULL;
720 GParamSpec *new;
721 GType parent_type;
723 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
724 g_return_if_fail (property_id > 0);
725 g_return_if_fail (name != NULL);
727 /* Find the overridden property; first check parent types
729 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
730 if (parent_type != G_TYPE_NONE)
731 overridden = g_param_spec_pool_lookup (pspec_pool,
732 name,
733 parent_type,
734 TRUE);
735 if (!overridden)
737 GType *ifaces;
738 guint n_ifaces;
740 /* Now check interfaces
742 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
743 while (n_ifaces-- && !overridden)
745 overridden = g_param_spec_pool_lookup (pspec_pool,
746 name,
747 ifaces[n_ifaces],
748 FALSE);
751 g_free (ifaces);
754 if (!overridden)
756 g_warning ("%s: Can't find property to override for '%s::%s'",
757 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
758 return;
761 new = g_param_spec_override (name, overridden);
762 g_object_class_install_property (oclass, property_id, new);
766 * g_object_class_list_properties:
767 * @oclass: a #GObjectClass
768 * @n_properties: (out): return location for the length of the returned array
770 * Get an array of #GParamSpec* for all properties of a class.
772 * Returns: (array length=n_properties) (transfer container): an array of
773 * #GParamSpec* which should be freed after use
775 GParamSpec** /* free result */
776 g_object_class_list_properties (GObjectClass *class,
777 guint *n_properties_p)
779 GParamSpec **pspecs;
780 guint n;
782 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
784 pspecs = g_param_spec_pool_list (pspec_pool,
785 G_OBJECT_CLASS_TYPE (class),
786 &n);
787 if (n_properties_p)
788 *n_properties_p = n;
790 return pspecs;
794 * g_object_interface_list_properties:
795 * @g_iface: any interface vtable for the interface, or the default
796 * vtable for the interface
797 * @n_properties_p: (out): location to store number of properties returned.
799 * Lists the properties of an interface.Generally, the interface
800 * vtable passed in as @g_iface will be the default vtable from
801 * g_type_default_interface_ref(), or, if you know the interface has
802 * already been loaded, g_type_default_interface_peek().
804 * Since: 2.4
806 * Returns: (array length=n_properties_p) (transfer container): a
807 * pointer to an array of pointers to #GParamSpec
808 * structures. The paramspecs are owned by GLib, but the
809 * array should be freed with g_free() when you are done with
810 * it.
812 GParamSpec**
813 g_object_interface_list_properties (gpointer g_iface,
814 guint *n_properties_p)
816 GTypeInterface *iface_class = g_iface;
817 GParamSpec **pspecs;
818 guint n;
820 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
822 pspecs = g_param_spec_pool_list (pspec_pool,
823 iface_class->g_type,
824 &n);
825 if (n_properties_p)
826 *n_properties_p = n;
828 return pspecs;
831 static void
832 g_object_init (GObject *object,
833 GObjectClass *class)
835 object->ref_count = 1;
836 g_datalist_init (&object->qdata);
838 if (CLASS_HAS_PROPS (class))
840 /* freeze object's notification queue, g_object_newv() preserves pairedness */
841 g_object_notify_queue_freeze (object, &property_notify_context);
844 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
846 /* enter construction list for notify_queue_thaw() and to allow construct-only properties */
847 G_LOCK (construction_mutex);
848 construction_objects = g_slist_prepend (construction_objects, object);
849 G_UNLOCK (construction_mutex);
852 #ifdef G_ENABLE_DEBUG
853 IF_DEBUG (OBJECTS)
855 G_LOCK (debug_objects);
856 debug_objects_count++;
857 g_hash_table_insert (debug_objects_ht, object, object);
858 G_UNLOCK (debug_objects);
860 #endif /* G_ENABLE_DEBUG */
863 static void
864 g_object_do_set_property (GObject *object,
865 guint property_id,
866 const GValue *value,
867 GParamSpec *pspec)
869 switch (property_id)
871 default:
872 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
873 break;
877 static void
878 g_object_do_get_property (GObject *object,
879 guint property_id,
880 GValue *value,
881 GParamSpec *pspec)
883 switch (property_id)
885 default:
886 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
887 break;
891 static void
892 g_object_real_dispose (GObject *object)
894 g_signal_handlers_destroy (object);
895 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
896 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
899 static void
900 g_object_finalize (GObject *object)
902 g_datalist_clear (&object->qdata);
904 #ifdef G_ENABLE_DEBUG
905 IF_DEBUG (OBJECTS)
907 G_LOCK (debug_objects);
908 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
909 g_hash_table_remove (debug_objects_ht, object);
910 debug_objects_count--;
911 G_UNLOCK (debug_objects);
913 #endif /* G_ENABLE_DEBUG */
917 static void
918 g_object_dispatch_properties_changed (GObject *object,
919 guint n_pspecs,
920 GParamSpec **pspecs)
922 guint i;
924 for (i = 0; i < n_pspecs; i++)
925 g_signal_emit (object, gobject_signals[NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
929 * g_object_run_dispose:
930 * @object: a #GObject
932 * Releases all references to other objects. This can be used to break
933 * reference cycles.
935 * This functions should only be called from object system implementations.
937 void
938 g_object_run_dispose (GObject *object)
940 g_return_if_fail (G_IS_OBJECT (object));
941 g_return_if_fail (object->ref_count > 0);
943 g_object_ref (object);
944 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
945 G_OBJECT_GET_CLASS (object)->dispose (object);
946 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
947 g_object_unref (object);
951 * g_object_freeze_notify:
952 * @object: a #GObject
954 * Increases the freeze count on @object. If the freeze count is
955 * non-zero, the emission of "notify" signals on @object is
956 * stopped. The signals are queued until the freeze count is decreased
957 * to zero.
959 * This is necessary for accessors that modify multiple properties to prevent
960 * premature notification while the object is still being modified.
962 void
963 g_object_freeze_notify (GObject *object)
965 g_return_if_fail (G_IS_OBJECT (object));
967 if (g_atomic_int_get (&object->ref_count) == 0)
968 return;
970 g_object_ref (object);
971 g_object_notify_queue_freeze (object, &property_notify_context);
972 g_object_unref (object);
975 static inline void
976 g_object_notify_by_spec_internal (GObject *object,
977 GParamSpec *pspec)
979 GObjectNotifyQueue *nqueue;
981 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
982 g_object_notify_queue_add (object, nqueue, pspec);
983 g_object_notify_queue_thaw (object, nqueue);
987 * g_object_notify:
988 * @object: a #GObject
989 * @property_name: the name of a property installed on the class of @object.
991 * Emits a "notify" signal for the property @property_name on @object.
993 * When possible, eg. when signaling a property change from within the class
994 * that registered the property, you should use g_object_notify_by_pspec()
995 * instead.
997 void
998 g_object_notify (GObject *object,
999 const gchar *property_name)
1001 GParamSpec *pspec;
1003 g_return_if_fail (G_IS_OBJECT (object));
1004 g_return_if_fail (property_name != NULL);
1005 if (g_atomic_int_get (&object->ref_count) == 0)
1006 return;
1008 g_object_ref (object);
1009 /* We don't need to get the redirect target
1010 * (by, e.g. calling g_object_class_find_property())
1011 * because g_object_notify_queue_add() does that
1013 pspec = g_param_spec_pool_lookup (pspec_pool,
1014 property_name,
1015 G_OBJECT_TYPE (object),
1016 TRUE);
1018 if (!pspec)
1019 g_warning ("%s: object class `%s' has no property named `%s'",
1020 G_STRFUNC,
1021 G_OBJECT_TYPE_NAME (object),
1022 property_name);
1023 else
1024 g_object_notify_by_spec_internal (object, pspec);
1025 g_object_unref (object);
1029 * g_object_notify_by_pspec:
1030 * @object: a #GObject
1031 * @pspec: the #GParamSpec of a property installed on the class of @object.
1033 * Emits a "notify" signal for the property specified by @pspec on @object.
1035 * This function omits the property name lookup, hence it is faster than
1036 * g_object_notify().
1038 * One way to avoid using g_object_notify() from within the
1039 * class that registered the properties, and using g_object_notify_by_pspec()
1040 * instead, is to store the GParamSpec used with
1041 * g_object_class_install_property() inside a static array, e.g.:
1044 * enum
1046 * PROP_0,
1047 * PROP_FOO,
1048 * PROP_LAST
1049 * };
1051 * static GParamSpec *properties[PROP_LAST];
1053 * static void
1054 * my_object_class_init (MyObjectClass *klass)
1056 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1057 * 0, 100,
1058 * 50,
1059 * G_PARAM_READWRITE);
1060 * g_object_class_install_property (gobject_class,
1061 * PROP_FOO,
1062 * properties[PROP_FOO]);
1064 * ]|
1066 * and then notify a change on the "foo" property with:
1068 * |[
1069 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1070 * ]|
1072 * Since: 2.26
1074 void
1075 g_object_notify_by_pspec (GObject *object,
1076 GParamSpec *pspec)
1079 g_return_if_fail (G_IS_OBJECT (object));
1080 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1082 g_object_ref (object);
1083 g_object_notify_by_spec_internal (object, pspec);
1084 g_object_unref (object);
1088 * g_object_thaw_notify:
1089 * @object: a #GObject
1091 * Reverts the effect of a previous call to
1092 * g_object_freeze_notify(). The freeze count is decreased on @object
1093 * and when it reaches zero, all queued "notify" signals are emitted.
1095 * It is an error to call this function when the freeze count is zero.
1097 void
1098 g_object_thaw_notify (GObject *object)
1100 GObjectNotifyQueue *nqueue;
1102 g_return_if_fail (G_IS_OBJECT (object));
1103 if (g_atomic_int_get (&object->ref_count) == 0)
1104 return;
1106 g_object_ref (object);
1108 /* FIXME: Freezing is the only way to get at the notify queue.
1109 * So we freeze once and then thaw twice.
1111 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1112 g_object_notify_queue_thaw (object, nqueue);
1113 g_object_notify_queue_thaw (object, nqueue);
1115 g_object_unref (object);
1118 static inline void
1119 object_get_property (GObject *object,
1120 GParamSpec *pspec,
1121 GValue *value)
1123 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1124 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1125 GParamSpec *redirect;
1127 redirect = g_param_spec_get_redirect_target (pspec);
1128 if (redirect)
1129 pspec = redirect;
1131 class->get_property (object, param_id, value, pspec);
1134 static inline void
1135 object_set_property (GObject *object,
1136 GParamSpec *pspec,
1137 const GValue *value,
1138 GObjectNotifyQueue *nqueue)
1140 GValue tmp_value = { 0, };
1141 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1142 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1143 GParamSpec *redirect;
1144 static gchar* enable_diagnostic = NULL;
1146 redirect = g_param_spec_get_redirect_target (pspec);
1147 if (redirect)
1148 pspec = redirect;
1150 if (G_UNLIKELY (!enable_diagnostic))
1152 enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1153 if (!enable_diagnostic)
1154 enable_diagnostic = "0";
1157 if (enable_diagnostic[0] == '1')
1159 if (pspec->flags & G_PARAM_DEPRECATED)
1160 g_warning ("The property %s::%s is deprecated and shouldn't be used "
1161 "anymore. It will be removed in a future version.",
1162 G_OBJECT_TYPE_NAME (object), pspec->name);
1165 /* provide a copy to work from, convert (if necessary) and validate */
1166 g_value_init (&tmp_value, pspec->value_type);
1167 if (!g_value_transform (value, &tmp_value))
1168 g_warning ("unable to set property `%s' of type `%s' from value of type `%s'",
1169 pspec->name,
1170 g_type_name (pspec->value_type),
1171 G_VALUE_TYPE_NAME (value));
1172 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1174 gchar *contents = g_strdup_value_contents (value);
1176 g_warning ("value \"%s\" of type `%s' is invalid or out of range for property `%s' of type `%s'",
1177 contents,
1178 G_VALUE_TYPE_NAME (value),
1179 pspec->name,
1180 g_type_name (pspec->value_type));
1181 g_free (contents);
1183 else
1185 class->set_property (object, param_id, &tmp_value, pspec);
1186 g_object_notify_queue_add (object, nqueue, pspec);
1188 g_value_unset (&tmp_value);
1191 static void
1192 object_interface_check_properties (gpointer func_data,
1193 gpointer g_iface)
1195 GTypeInterface *iface_class = g_iface;
1196 GObjectClass *class;
1197 GType iface_type = iface_class->g_type;
1198 GParamSpec **pspecs;
1199 guint n;
1201 class = g_type_class_ref (iface_class->g_instance_type);
1203 if (!G_IS_OBJECT_CLASS (class))
1204 return;
1206 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1208 while (n--)
1210 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1211 pspecs[n]->name,
1212 G_OBJECT_CLASS_TYPE (class),
1213 TRUE);
1215 if (!class_pspec)
1217 g_critical ("Object class %s doesn't implement property "
1218 "'%s' from interface '%s'",
1219 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1220 pspecs[n]->name,
1221 g_type_name (iface_type));
1223 continue;
1226 /* The implementation paramspec must have a less restrictive
1227 * type than the interface parameter spec for set() and a
1228 * more restrictive type for get(). We just require equality,
1229 * rather than doing something more complicated checking
1230 * the READABLE and WRITABLE flags. We also simplify here
1231 * by only checking the value type, not the G_PARAM_SPEC_TYPE.
1233 if (class_pspec &&
1234 !g_type_is_a (pspecs[n]->value_type,
1235 class_pspec->value_type))
1237 g_critical ("Property '%s' on class '%s' has type '%s' "
1238 "which is different from the type '%s', "
1239 "of the property on interface '%s'\n",
1240 pspecs[n]->name,
1241 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1242 g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1243 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])),
1244 g_type_name (iface_type));
1247 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1249 /* CONSTRUCT and CONSTRUCT_ONLY add restrictions.
1250 * READABLE and WRITABLE remove restrictions. The implementation
1251 * paramspec must have less restrictive flags.
1253 if (class_pspec &&
1254 (!SUBSET (class_pspec->flags,
1255 pspecs[n]->flags,
1256 G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY) ||
1257 !SUBSET (pspecs[n]->flags,
1258 class_pspec->flags,
1259 G_PARAM_READABLE | G_PARAM_WRITABLE)))
1261 g_critical ("Flags for property '%s' on class '%s' "
1262 "are not compatible with the property on"
1263 "interface '%s'\n",
1264 pspecs[n]->name,
1265 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1266 g_type_name (iface_type));
1268 #undef SUBSET
1271 g_free (pspecs);
1273 g_type_class_unref (class);
1276 GType
1277 g_object_get_type (void)
1279 return G_TYPE_OBJECT;
1283 * g_object_new: (skip)
1284 * @object_type: the type id of the #GObject subtype to instantiate
1285 * @first_property_name: the name of the first property
1286 * @...: the value of the first property, followed optionally by more
1287 * name/value pairs, followed by %NULL
1289 * Creates a new instance of a #GObject subtype and sets its properties.
1291 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1292 * which are not explicitly specified are set to their default values.
1294 * Returns: (transfer full): a new instance of @object_type
1296 gpointer
1297 g_object_new (GType object_type,
1298 const gchar *first_property_name,
1299 ...)
1301 GObject *object;
1302 va_list var_args;
1304 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1306 /* short circuit for calls supplying no properties */
1307 if (!first_property_name)
1308 return g_object_newv (object_type, 0, NULL);
1310 va_start (var_args, first_property_name);
1311 object = g_object_new_valist (object_type, first_property_name, var_args);
1312 va_end (var_args);
1314 return object;
1317 static gboolean
1318 slist_maybe_remove (GSList **slist,
1319 gconstpointer data)
1321 GSList *last = NULL, *node = *slist;
1322 while (node)
1324 if (node->data == data)
1326 if (last)
1327 last->next = node->next;
1328 else
1329 *slist = node->next;
1330 g_slist_free_1 (node);
1331 return TRUE;
1333 last = node;
1334 node = last->next;
1336 return FALSE;
1339 static inline gboolean
1340 object_in_construction_list (GObject *object)
1342 gboolean in_construction;
1343 G_LOCK (construction_mutex);
1344 in_construction = g_slist_find (construction_objects, object) != NULL;
1345 G_UNLOCK (construction_mutex);
1346 return in_construction;
1350 * g_object_newv:
1351 * @object_type: the type id of the #GObject subtype to instantiate
1352 * @n_parameters: the length of the @parameters array
1353 * @parameters: (array length=n_parameters): an array of #GParameter
1355 * Creates a new instance of a #GObject subtype and sets its properties.
1357 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1358 * which are not explicitly specified are set to their default values.
1360 * Rename to: g_object_new
1361 * Returns: (type GObject.Object) (transfer full): a new instance of
1362 * @object_type
1364 gpointer
1365 g_object_newv (GType object_type,
1366 guint n_parameters,
1367 GParameter *parameters)
1369 GObjectConstructParam *cparams = NULL, *oparams;
1370 GObjectNotifyQueue *nqueue = NULL; /* shouldn't be initialized, just to silence compiler */
1371 GObject *object;
1372 GObjectClass *class, *unref_class = NULL;
1373 GSList *slist;
1374 guint n_total_cparams = 0, n_cparams = 0, n_oparams = 0, n_cvalues;
1375 GValue *cvalues;
1376 GList *clist = NULL;
1377 gboolean newly_constructed;
1378 guint i;
1380 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1382 class = g_type_class_peek_static (object_type);
1383 if (!class)
1384 class = unref_class = g_type_class_ref (object_type);
1385 for (slist = class->construct_properties; slist; slist = slist->next)
1387 clist = g_list_prepend (clist, slist->data);
1388 n_total_cparams += 1;
1391 if (n_parameters == 0 && n_total_cparams == 0)
1393 /* This is a simple object with no construct properties, and
1394 * no properties are being set, so short circuit the parameter
1395 * handling. This speeds up simple object construction.
1397 oparams = NULL;
1398 object = class->constructor (object_type, 0, NULL);
1399 goto did_construction;
1402 /* collect parameters, sort into construction and normal ones */
1403 oparams = g_new (GObjectConstructParam, n_parameters);
1404 cparams = g_new (GObjectConstructParam, n_total_cparams);
1405 for (i = 0; i < n_parameters; i++)
1407 GValue *value = &parameters[i].value;
1408 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1409 parameters[i].name,
1410 object_type,
1411 TRUE);
1412 if (!pspec)
1414 g_warning ("%s: object class `%s' has no property named `%s'",
1415 G_STRFUNC,
1416 g_type_name (object_type),
1417 parameters[i].name);
1418 continue;
1420 if (!(pspec->flags & G_PARAM_WRITABLE))
1422 g_warning ("%s: property `%s' of object class `%s' is not writable",
1423 G_STRFUNC,
1424 pspec->name,
1425 g_type_name (object_type));
1426 continue;
1428 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1430 GList *list = g_list_find (clist, pspec);
1432 if (!list)
1434 g_warning ("%s: construct property \"%s\" for object `%s' can't be set twice",
1435 G_STRFUNC, pspec->name, g_type_name (object_type));
1436 continue;
1438 cparams[n_cparams].pspec = pspec;
1439 cparams[n_cparams].value = value;
1440 n_cparams++;
1441 if (!list->prev)
1442 clist = list->next;
1443 else
1444 list->prev->next = list->next;
1445 if (list->next)
1446 list->next->prev = list->prev;
1447 g_list_free_1 (list);
1449 else
1451 oparams[n_oparams].pspec = pspec;
1452 oparams[n_oparams].value = value;
1453 n_oparams++;
1457 /* set remaining construction properties to default values */
1458 n_cvalues = n_total_cparams - n_cparams;
1459 cvalues = g_new (GValue, n_cvalues);
1460 while (clist)
1462 GList *tmp = clist->next;
1463 GParamSpec *pspec = clist->data;
1464 GValue *value = cvalues + n_total_cparams - n_cparams - 1;
1466 value->g_type = 0;
1467 g_value_init (value, pspec->value_type);
1468 g_param_value_set_default (pspec, value);
1470 cparams[n_cparams].pspec = pspec;
1471 cparams[n_cparams].value = value;
1472 n_cparams++;
1474 g_list_free_1 (clist);
1475 clist = tmp;
1478 /* construct object from construction parameters */
1479 object = class->constructor (object_type, n_total_cparams, cparams);
1480 /* free construction values */
1481 g_free (cparams);
1482 while (n_cvalues--)
1483 g_value_unset (cvalues + n_cvalues);
1484 g_free (cvalues);
1486 did_construction:
1487 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1489 /* adjust freeze_count according to g_object_init() and remaining properties */
1490 G_LOCK (construction_mutex);
1491 newly_constructed = slist_maybe_remove (&construction_objects, object);
1492 G_UNLOCK (construction_mutex);
1494 else
1495 newly_constructed = TRUE;
1497 if (CLASS_HAS_PROPS (class))
1499 if (newly_constructed || n_oparams)
1500 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1501 if (newly_constructed)
1502 g_object_notify_queue_thaw (object, nqueue);
1505 /* run 'constructed' handler if there is a custom one */
1506 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1507 class->constructed (object);
1509 /* set remaining properties */
1510 for (i = 0; i < n_oparams; i++)
1511 object_set_property (object, oparams[i].pspec, oparams[i].value, nqueue);
1512 g_free (oparams);
1514 if (CLASS_HAS_PROPS (class))
1516 /* release our own freeze count and handle notifications */
1517 if (newly_constructed || n_oparams)
1518 g_object_notify_queue_thaw (object, nqueue);
1521 if (unref_class)
1522 g_type_class_unref (unref_class);
1524 return object;
1528 * g_object_new_valist: (skip)
1529 * @object_type: the type id of the #GObject subtype to instantiate
1530 * @first_property_name: the name of the first property
1531 * @var_args: the value of the first property, followed optionally by more
1532 * name/value pairs, followed by %NULL
1534 * Creates a new instance of a #GObject subtype and sets its properties.
1536 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1537 * which are not explicitly specified are set to their default values.
1539 * Returns: a new instance of @object_type
1541 GObject*
1542 g_object_new_valist (GType object_type,
1543 const gchar *first_property_name,
1544 va_list var_args)
1546 GObjectClass *class;
1547 GParameter *params;
1548 const gchar *name;
1549 GObject *object;
1550 guint n_params = 0, n_alloced_params = 16;
1552 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1554 if (!first_property_name)
1555 return g_object_newv (object_type, 0, NULL);
1557 class = g_type_class_ref (object_type);
1559 params = g_new0 (GParameter, n_alloced_params);
1560 name = first_property_name;
1561 while (name)
1563 gchar *error = NULL;
1564 GParamSpec *pspec = g_param_spec_pool_lookup (pspec_pool,
1565 name,
1566 object_type,
1567 TRUE);
1568 if (!pspec)
1570 g_warning ("%s: object class `%s' has no property named `%s'",
1571 G_STRFUNC,
1572 g_type_name (object_type),
1573 name);
1574 break;
1576 if (n_params >= n_alloced_params)
1578 n_alloced_params += 16;
1579 params = g_renew (GParameter, params, n_alloced_params);
1580 memset (params + n_params, 0, 16 * (sizeof *params));
1582 params[n_params].name = name;
1583 G_VALUE_COLLECT_INIT (&params[n_params].value, pspec->value_type,
1584 var_args, 0, &error);
1585 if (error)
1587 g_warning ("%s: %s", G_STRFUNC, error);
1588 g_free (error);
1589 g_value_unset (&params[n_params].value);
1590 break;
1592 n_params++;
1593 name = va_arg (var_args, gchar*);
1596 object = g_object_newv (object_type, n_params, params);
1598 while (n_params--)
1599 g_value_unset (&params[n_params].value);
1600 g_free (params);
1602 g_type_class_unref (class);
1604 return object;
1607 static GObject*
1608 g_object_constructor (GType type,
1609 guint n_construct_properties,
1610 GObjectConstructParam *construct_params)
1612 GObject *object;
1614 /* create object */
1615 object = (GObject*) g_type_create_instance (type);
1617 /* set construction parameters */
1618 if (n_construct_properties)
1620 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1622 /* set construct properties */
1623 while (n_construct_properties--)
1625 GValue *value = construct_params->value;
1626 GParamSpec *pspec = construct_params->pspec;
1628 construct_params++;
1629 object_set_property (object, pspec, value, nqueue);
1631 g_object_notify_queue_thaw (object, nqueue);
1632 /* the notification queue is still frozen from g_object_init(), so
1633 * we don't need to handle it here, g_object_newv() takes
1634 * care of that
1638 return object;
1641 static void
1642 g_object_constructed (GObject *object)
1644 /* empty default impl to allow unconditional upchaining */
1648 * g_object_set_valist: (skip)
1649 * @object: a #GObject
1650 * @first_property_name: name of the first property to set
1651 * @var_args: value for the first property, followed optionally by more
1652 * name/value pairs, followed by %NULL
1654 * Sets properties on an object.
1656 void
1657 g_object_set_valist (GObject *object,
1658 const gchar *first_property_name,
1659 va_list var_args)
1661 GObjectNotifyQueue *nqueue;
1662 const gchar *name;
1664 g_return_if_fail (G_IS_OBJECT (object));
1666 g_object_ref (object);
1667 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1669 name = first_property_name;
1670 while (name)
1672 GValue value = { 0, };
1673 GParamSpec *pspec;
1674 gchar *error = NULL;
1676 pspec = g_param_spec_pool_lookup (pspec_pool,
1677 name,
1678 G_OBJECT_TYPE (object),
1679 TRUE);
1680 if (!pspec)
1682 g_warning ("%s: object class `%s' has no property named `%s'",
1683 G_STRFUNC,
1684 G_OBJECT_TYPE_NAME (object),
1685 name);
1686 break;
1688 if (!(pspec->flags & G_PARAM_WRITABLE))
1690 g_warning ("%s: property `%s' of object class `%s' is not writable",
1691 G_STRFUNC,
1692 pspec->name,
1693 G_OBJECT_TYPE_NAME (object));
1694 break;
1696 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1698 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1699 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1700 break;
1703 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
1704 0, &error);
1705 if (error)
1707 g_warning ("%s: %s", G_STRFUNC, error);
1708 g_free (error);
1709 g_value_unset (&value);
1710 break;
1713 object_set_property (object, pspec, &value, nqueue);
1714 g_value_unset (&value);
1716 name = va_arg (var_args, gchar*);
1719 g_object_notify_queue_thaw (object, nqueue);
1720 g_object_unref (object);
1724 * g_object_get_valist: (skip)
1725 * @object: a #GObject
1726 * @first_property_name: name of the first property to get
1727 * @var_args: return location for the first property, followed optionally by more
1728 * name/return location pairs, followed by %NULL
1730 * Gets properties of an object.
1732 * In general, a copy is made of the property contents and the caller
1733 * is responsible for freeing the memory in the appropriate manner for
1734 * the type, for instance by calling g_free() or g_object_unref().
1736 * See g_object_get().
1738 void
1739 g_object_get_valist (GObject *object,
1740 const gchar *first_property_name,
1741 va_list var_args)
1743 const gchar *name;
1745 g_return_if_fail (G_IS_OBJECT (object));
1747 g_object_ref (object);
1749 name = first_property_name;
1751 while (name)
1753 GValue value = { 0, };
1754 GParamSpec *pspec;
1755 gchar *error;
1757 pspec = g_param_spec_pool_lookup (pspec_pool,
1758 name,
1759 G_OBJECT_TYPE (object),
1760 TRUE);
1761 if (!pspec)
1763 g_warning ("%s: object class `%s' has no property named `%s'",
1764 G_STRFUNC,
1765 G_OBJECT_TYPE_NAME (object),
1766 name);
1767 break;
1769 if (!(pspec->flags & G_PARAM_READABLE))
1771 g_warning ("%s: property `%s' of object class `%s' is not readable",
1772 G_STRFUNC,
1773 pspec->name,
1774 G_OBJECT_TYPE_NAME (object));
1775 break;
1778 g_value_init (&value, pspec->value_type);
1780 object_get_property (object, pspec, &value);
1782 G_VALUE_LCOPY (&value, var_args, 0, &error);
1783 if (error)
1785 g_warning ("%s: %s", G_STRFUNC, error);
1786 g_free (error);
1787 g_value_unset (&value);
1788 break;
1791 g_value_unset (&value);
1793 name = va_arg (var_args, gchar*);
1796 g_object_unref (object);
1800 * g_object_set: (skip)
1801 * @object: a #GObject
1802 * @first_property_name: name of the first property to set
1803 * @...: value for the first property, followed optionally by more
1804 * name/value pairs, followed by %NULL
1806 * Sets properties on an object.
1808 void
1809 g_object_set (gpointer _object,
1810 const gchar *first_property_name,
1811 ...)
1813 GObject *object = _object;
1814 va_list var_args;
1816 g_return_if_fail (G_IS_OBJECT (object));
1818 va_start (var_args, first_property_name);
1819 g_object_set_valist (object, first_property_name, var_args);
1820 va_end (var_args);
1824 * g_object_get: (skip)
1825 * @object: a #GObject
1826 * @first_property_name: name of the first property to get
1827 * @...: return location for the first property, followed optionally by more
1828 * name/return location pairs, followed by %NULL
1830 * Gets properties of an object.
1832 * In general, a copy is made of the property contents and the caller
1833 * is responsible for freeing the memory in the appropriate manner for
1834 * the type, for instance by calling g_free() or g_object_unref().
1836 * <example>
1837 * <title>Using g_object_get(<!-- -->)</title>
1838 * An example of using g_object_get() to get the contents
1839 * of three properties - one of type #G_TYPE_INT,
1840 * one of type #G_TYPE_STRING, and one of type #G_TYPE_OBJECT:
1841 * <programlisting>
1842 * gint intval;
1843 * gchar *strval;
1844 * GObject *objval;
1846 * g_object_get (my_object,
1847 * "int-property", &intval,
1848 * "str-property", &strval,
1849 * "obj-property", &objval,
1850 * NULL);
1852 * // Do something with intval, strval, objval
1854 * g_free (strval);
1855 * g_object_unref (objval);
1856 * </programlisting>
1857 * </example>
1859 void
1860 g_object_get (gpointer _object,
1861 const gchar *first_property_name,
1862 ...)
1864 GObject *object = _object;
1865 va_list var_args;
1867 g_return_if_fail (G_IS_OBJECT (object));
1869 va_start (var_args, first_property_name);
1870 g_object_get_valist (object, first_property_name, var_args);
1871 va_end (var_args);
1875 * g_object_set_property:
1876 * @object: a #GObject
1877 * @property_name: the name of the property to set
1878 * @value: the value
1880 * Sets a property on an object.
1882 void
1883 g_object_set_property (GObject *object,
1884 const gchar *property_name,
1885 const GValue *value)
1887 GObjectNotifyQueue *nqueue;
1888 GParamSpec *pspec;
1890 g_return_if_fail (G_IS_OBJECT (object));
1891 g_return_if_fail (property_name != NULL);
1892 g_return_if_fail (G_IS_VALUE (value));
1894 g_object_ref (object);
1895 nqueue = g_object_notify_queue_freeze (object, &property_notify_context);
1897 pspec = g_param_spec_pool_lookup (pspec_pool,
1898 property_name,
1899 G_OBJECT_TYPE (object),
1900 TRUE);
1901 if (!pspec)
1902 g_warning ("%s: object class `%s' has no property named `%s'",
1903 G_STRFUNC,
1904 G_OBJECT_TYPE_NAME (object),
1905 property_name);
1906 else if (!(pspec->flags & G_PARAM_WRITABLE))
1907 g_warning ("%s: property `%s' of object class `%s' is not writable",
1908 G_STRFUNC,
1909 pspec->name,
1910 G_OBJECT_TYPE_NAME (object));
1911 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction_list (object))
1912 g_warning ("%s: construct property \"%s\" for object `%s' can't be set after construction",
1913 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
1914 else
1915 object_set_property (object, pspec, value, nqueue);
1917 g_object_notify_queue_thaw (object, nqueue);
1918 g_object_unref (object);
1922 * g_object_get_property:
1923 * @object: a #GObject
1924 * @property_name: the name of the property to get
1925 * @value: return location for the property value
1927 * Gets a property of an object. @value must have been initialized to the
1928 * expected type of the property (or a type to which the expected type can be
1929 * transformed) using g_value_init().
1931 * In general, a copy is made of the property contents and the caller is
1932 * responsible for freeing the memory by calling g_value_unset().
1934 * Note that g_object_get_property() is really intended for language
1935 * bindings, g_object_get() is much more convenient for C programming.
1937 void
1938 g_object_get_property (GObject *object,
1939 const gchar *property_name,
1940 GValue *value)
1942 GParamSpec *pspec;
1944 g_return_if_fail (G_IS_OBJECT (object));
1945 g_return_if_fail (property_name != NULL);
1946 g_return_if_fail (G_IS_VALUE (value));
1948 g_object_ref (object);
1950 pspec = g_param_spec_pool_lookup (pspec_pool,
1951 property_name,
1952 G_OBJECT_TYPE (object),
1953 TRUE);
1954 if (!pspec)
1955 g_warning ("%s: object class `%s' has no property named `%s'",
1956 G_STRFUNC,
1957 G_OBJECT_TYPE_NAME (object),
1958 property_name);
1959 else if (!(pspec->flags & G_PARAM_READABLE))
1960 g_warning ("%s: property `%s' of object class `%s' is not readable",
1961 G_STRFUNC,
1962 pspec->name,
1963 G_OBJECT_TYPE_NAME (object));
1964 else
1966 GValue *prop_value, tmp_value = { 0, };
1968 /* auto-conversion of the callers value type
1970 if (G_VALUE_TYPE (value) == pspec->value_type)
1972 g_value_reset (value);
1973 prop_value = value;
1975 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
1977 g_warning ("%s: can't retrieve property `%s' of type `%s' as value of type `%s'",
1978 G_STRFUNC, pspec->name,
1979 g_type_name (pspec->value_type),
1980 G_VALUE_TYPE_NAME (value));
1981 g_object_unref (object);
1982 return;
1984 else
1986 g_value_init (&tmp_value, pspec->value_type);
1987 prop_value = &tmp_value;
1989 object_get_property (object, pspec, prop_value);
1990 if (prop_value != value)
1992 g_value_transform (prop_value, value);
1993 g_value_unset (&tmp_value);
1997 g_object_unref (object);
2001 * g_object_connect: (skip)
2002 * @object: a #GObject
2003 * @signal_spec: the spec for the first signal
2004 * @...: #GCallback for the first signal, followed by data for the
2005 * first signal, followed optionally by more signal
2006 * spec/callback/data triples, followed by %NULL
2008 * A convenience function to connect multiple signals at once.
2010 * The signal specs expected by this function have the form
2011 * "modifier::signal_name", where modifier can be one of the following:
2012 * <variablelist>
2013 * <varlistentry>
2014 * <term>signal</term>
2015 * <listitem><para>
2016 * equivalent to <literal>g_signal_connect_data (..., NULL, 0)</literal>
2017 * </para></listitem>
2018 * </varlistentry>
2019 * <varlistentry>
2020 * <term>object_signal</term>
2021 * <term>object-signal</term>
2022 * <listitem><para>
2023 * equivalent to <literal>g_signal_connect_object (..., 0)</literal>
2024 * </para></listitem>
2025 * </varlistentry>
2026 * <varlistentry>
2027 * <term>swapped_signal</term>
2028 * <term>swapped-signal</term>
2029 * <listitem><para>
2030 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)</literal>
2031 * </para></listitem>
2032 * </varlistentry>
2033 * <varlistentry>
2034 * <term>swapped_object_signal</term>
2035 * <term>swapped-object-signal</term>
2036 * <listitem><para>
2037 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED)</literal>
2038 * </para></listitem>
2039 * </varlistentry>
2040 * <varlistentry>
2041 * <term>signal_after</term>
2042 * <term>signal-after</term>
2043 * <listitem><para>
2044 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_AFTER)</literal>
2045 * </para></listitem>
2046 * </varlistentry>
2047 * <varlistentry>
2048 * <term>object_signal_after</term>
2049 * <term>object-signal-after</term>
2050 * <listitem><para>
2051 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_AFTER)</literal>
2052 * </para></listitem>
2053 * </varlistentry>
2054 * <varlistentry>
2055 * <term>swapped_signal_after</term>
2056 * <term>swapped-signal-after</term>
2057 * <listitem><para>
2058 * equivalent to <literal>g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2059 * </para></listitem>
2060 * </varlistentry>
2061 * <varlistentry>
2062 * <term>swapped_object_signal_after</term>
2063 * <term>swapped-object-signal-after</term>
2064 * <listitem><para>
2065 * equivalent to <literal>g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)</literal>
2066 * </para></listitem>
2067 * </varlistentry>
2068 * </variablelist>
2070 * |[
2071 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2072 * "type", GTK_WINDOW_POPUP,
2073 * "child", menu,
2074 * NULL),
2075 * "signal::event", gtk_menu_window_event, menu,
2076 * "signal::size_request", gtk_menu_window_size_request, menu,
2077 * "signal::destroy", gtk_widget_destroyed, &amp;menu-&gt;toplevel,
2078 * NULL);
2079 * ]|
2081 * Returns: (transfer none): @object
2083 gpointer
2084 g_object_connect (gpointer _object,
2085 const gchar *signal_spec,
2086 ...)
2088 GObject *object = _object;
2089 va_list var_args;
2091 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2092 g_return_val_if_fail (object->ref_count > 0, object);
2094 va_start (var_args, signal_spec);
2095 while (signal_spec)
2097 GCallback callback = va_arg (var_args, GCallback);
2098 gpointer data = va_arg (var_args, gpointer);
2099 gulong sid;
2101 if (strncmp (signal_spec, "signal::", 8) == 0)
2102 sid = g_signal_connect_data (object, signal_spec + 8,
2103 callback, data, NULL,
2105 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2106 strncmp (signal_spec, "object-signal::", 15) == 0)
2107 sid = g_signal_connect_object (object, signal_spec + 15,
2108 callback, data,
2110 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2111 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2112 sid = g_signal_connect_data (object, signal_spec + 16,
2113 callback, data, NULL,
2114 G_CONNECT_SWAPPED);
2115 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2116 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2117 sid = g_signal_connect_object (object, signal_spec + 23,
2118 callback, data,
2119 G_CONNECT_SWAPPED);
2120 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2121 strncmp (signal_spec, "signal-after::", 14) == 0)
2122 sid = g_signal_connect_data (object, signal_spec + 14,
2123 callback, data, NULL,
2124 G_CONNECT_AFTER);
2125 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2126 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2127 sid = g_signal_connect_object (object, signal_spec + 21,
2128 callback, data,
2129 G_CONNECT_AFTER);
2130 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2131 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2132 sid = g_signal_connect_data (object, signal_spec + 22,
2133 callback, data, NULL,
2134 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2135 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2136 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2137 sid = g_signal_connect_object (object, signal_spec + 29,
2138 callback, data,
2139 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2140 else
2142 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2143 break;
2145 signal_spec = va_arg (var_args, gchar*);
2147 va_end (var_args);
2149 return object;
2153 * g_object_disconnect: (skip)
2154 * @object: a #GObject
2155 * @signal_spec: the spec for the first signal
2156 * @...: #GCallback for the first signal, followed by data for the first signal,
2157 * followed optionally by more signal spec/callback/data triples,
2158 * followed by %NULL
2160 * A convenience function to disconnect multiple signals at once.
2162 * The signal specs expected by this function have the form
2163 * "any_signal", which means to disconnect any signal with matching
2164 * callback and data, or "any_signal::signal_name", which only
2165 * disconnects the signal named "signal_name".
2167 void
2168 g_object_disconnect (gpointer _object,
2169 const gchar *signal_spec,
2170 ...)
2172 GObject *object = _object;
2173 va_list var_args;
2175 g_return_if_fail (G_IS_OBJECT (object));
2176 g_return_if_fail (object->ref_count > 0);
2178 va_start (var_args, signal_spec);
2179 while (signal_spec)
2181 GCallback callback = va_arg (var_args, GCallback);
2182 gpointer data = va_arg (var_args, gpointer);
2183 guint sid = 0, detail = 0, mask = 0;
2185 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2186 strncmp (signal_spec, "any-signal::", 12) == 0)
2188 signal_spec += 12;
2189 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2191 else if (strcmp (signal_spec, "any_signal") == 0 ||
2192 strcmp (signal_spec, "any-signal") == 0)
2194 signal_spec += 10;
2195 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2197 else
2199 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2200 break;
2203 if ((mask & G_SIGNAL_MATCH_ID) &&
2204 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2205 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2206 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2207 sid, detail,
2208 NULL, (gpointer)callback, data))
2209 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2210 signal_spec = va_arg (var_args, gchar*);
2212 va_end (var_args);
2215 typedef struct {
2216 GObject *object;
2217 guint n_weak_refs;
2218 struct {
2219 GWeakNotify notify;
2220 gpointer data;
2221 } weak_refs[1]; /* flexible array */
2222 } WeakRefStack;
2224 static void
2225 weak_refs_notify (gpointer data)
2227 WeakRefStack *wstack = data;
2228 guint i;
2230 for (i = 0; i < wstack->n_weak_refs; i++)
2231 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2232 g_free (wstack);
2236 * g_object_weak_ref: (skip)
2237 * @object: #GObject to reference weakly
2238 * @notify: callback to invoke before the object is freed
2239 * @data: extra data to pass to notify
2241 * Adds a weak reference callback to an object. Weak references are
2242 * used for notification when an object is finalized. They are called
2243 * "weak references" because they allow you to safely hold a pointer
2244 * to an object without calling g_object_ref() (g_object_ref() adds a
2245 * strong reference, that is, forces the object to stay alive).
2247 void
2248 g_object_weak_ref (GObject *object,
2249 GWeakNotify notify,
2250 gpointer data)
2252 WeakRefStack *wstack;
2253 guint i;
2255 g_return_if_fail (G_IS_OBJECT (object));
2256 g_return_if_fail (notify != NULL);
2257 g_return_if_fail (object->ref_count >= 1);
2259 G_LOCK (weak_refs_mutex);
2260 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2261 if (wstack)
2263 i = wstack->n_weak_refs++;
2264 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2266 else
2268 wstack = g_renew (WeakRefStack, NULL, 1);
2269 wstack->object = object;
2270 wstack->n_weak_refs = 1;
2271 i = 0;
2273 wstack->weak_refs[i].notify = notify;
2274 wstack->weak_refs[i].data = data;
2275 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2276 G_UNLOCK (weak_refs_mutex);
2280 * g_object_weak_unref: (skip)
2281 * @object: #GObject to remove a weak reference from
2282 * @notify: callback to search for
2283 * @data: data to search for
2285 * Removes a weak reference callback to an object.
2287 void
2288 g_object_weak_unref (GObject *object,
2289 GWeakNotify notify,
2290 gpointer data)
2292 WeakRefStack *wstack;
2293 gboolean found_one = FALSE;
2295 g_return_if_fail (G_IS_OBJECT (object));
2296 g_return_if_fail (notify != NULL);
2298 G_LOCK (weak_refs_mutex);
2299 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2300 if (wstack)
2302 guint i;
2304 for (i = 0; i < wstack->n_weak_refs; i++)
2305 if (wstack->weak_refs[i].notify == notify &&
2306 wstack->weak_refs[i].data == data)
2308 found_one = TRUE;
2309 wstack->n_weak_refs -= 1;
2310 if (i != wstack->n_weak_refs)
2311 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2313 break;
2316 G_UNLOCK (weak_refs_mutex);
2317 if (!found_one)
2318 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2322 * g_object_add_weak_pointer: (skip)
2323 * @object: The object that should be weak referenced.
2324 * @weak_pointer_location: (inout): The memory address of a pointer.
2326 * Adds a weak reference from weak_pointer to @object to indicate that
2327 * the pointer located at @weak_pointer_location is only valid during
2328 * the lifetime of @object. When the @object is finalized,
2329 * @weak_pointer will be set to %NULL.
2331 void
2332 g_object_add_weak_pointer (GObject *object,
2333 gpointer *weak_pointer_location)
2335 g_return_if_fail (G_IS_OBJECT (object));
2336 g_return_if_fail (weak_pointer_location != NULL);
2338 g_object_weak_ref (object,
2339 (GWeakNotify) g_nullify_pointer,
2340 weak_pointer_location);
2344 * g_object_remove_weak_pointer: (skip)
2345 * @object: The object that is weak referenced.
2346 * @weak_pointer_location: (inout): The memory address of a pointer.
2348 * Removes a weak reference from @object that was previously added
2349 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2350 * to match the one used with g_object_add_weak_pointer().
2352 void
2353 g_object_remove_weak_pointer (GObject *object,
2354 gpointer *weak_pointer_location)
2356 g_return_if_fail (G_IS_OBJECT (object));
2357 g_return_if_fail (weak_pointer_location != NULL);
2359 g_object_weak_unref (object,
2360 (GWeakNotify) g_nullify_pointer,
2361 weak_pointer_location);
2364 static guint
2365 object_floating_flag_handler (GObject *object,
2366 gint job)
2368 switch (job)
2370 gpointer oldvalue;
2371 case +1: /* force floating if possible */
2373 oldvalue = g_atomic_pointer_get (&object->qdata);
2374 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2375 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2376 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2377 case -1: /* sink if possible */
2379 oldvalue = g_atomic_pointer_get (&object->qdata);
2380 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2381 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2382 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2383 default: /* check floating */
2384 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2389 * g_object_is_floating:
2390 * @object: (type GObject.Object): a #GObject
2392 * Checks whether @object has a <link linkend="floating-ref">floating</link>
2393 * reference.
2395 * Since: 2.10
2397 * Returns: %TRUE if @object has a floating reference
2399 gboolean
2400 g_object_is_floating (gpointer _object)
2402 GObject *object = _object;
2403 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2404 return floating_flag_handler (object, 0);
2408 * g_object_ref_sink:
2409 * @object: (type GObject.Object): a #GObject
2411 * Increase the reference count of @object, and possibly remove the
2412 * <link linkend="floating-ref">floating</link> reference, if @object
2413 * has a floating reference.
2415 * In other words, if the object is floating, then this call "assumes
2416 * ownership" of the floating reference, converting it to a normal
2417 * reference by clearing the floating flag while leaving the reference
2418 * count unchanged. If the object is not floating, then this call
2419 * adds a new normal reference increasing the reference count by one.
2421 * Since: 2.10
2423 * Returns: (type GObject.Object) (transfer none): @object
2425 gpointer
2426 g_object_ref_sink (gpointer _object)
2428 GObject *object = _object;
2429 gboolean was_floating;
2430 g_return_val_if_fail (G_IS_OBJECT (object), object);
2431 g_return_val_if_fail (object->ref_count >= 1, object);
2432 g_object_ref (object);
2433 was_floating = floating_flag_handler (object, -1);
2434 if (was_floating)
2435 g_object_unref (object);
2436 return object;
2440 * g_object_force_floating:
2441 * @object: a #GObject
2443 * This function is intended for #GObject implementations to re-enforce a
2444 * <link linkend="floating-ref">floating</link> object reference.
2445 * Doing this is seldomly required: all
2446 * #GInitiallyUnowned<!-- -->s are created with a floating reference which
2447 * usually just needs to be sunken by calling g_object_ref_sink().
2449 * Since: 2.10
2451 void
2452 g_object_force_floating (GObject *object)
2454 gboolean was_floating;
2455 g_return_if_fail (G_IS_OBJECT (object));
2456 g_return_if_fail (object->ref_count >= 1);
2458 was_floating = floating_flag_handler (object, +1);
2461 typedef struct {
2462 GObject *object;
2463 guint n_toggle_refs;
2464 struct {
2465 GToggleNotify notify;
2466 gpointer data;
2467 } toggle_refs[1]; /* flexible array */
2468 } ToggleRefStack;
2470 static void
2471 toggle_refs_notify (GObject *object,
2472 gboolean is_last_ref)
2474 ToggleRefStack tstack, *tstackptr;
2476 G_LOCK (toggle_refs_mutex);
2477 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2478 tstack = *tstackptr;
2479 G_UNLOCK (toggle_refs_mutex);
2481 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2482 * will only be notified when there is exactly one of them.
2484 g_assert (tstack.n_toggle_refs == 1);
2485 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2489 * g_object_add_toggle_ref: (skip)
2490 * @object: a #GObject
2491 * @notify: a function to call when this reference is the
2492 * last reference to the object, or is no longer
2493 * the last reference.
2494 * @data: data to pass to @notify
2496 * Increases the reference count of the object by one and sets a
2497 * callback to be called when all other references to the object are
2498 * dropped, or when this is already the last reference to the object
2499 * and another reference is established.
2501 * This functionality is intended for binding @object to a proxy
2502 * object managed by another memory manager. This is done with two
2503 * paired references: the strong reference added by
2504 * g_object_add_toggle_ref() and a reverse reference to the proxy
2505 * object which is either a strong reference or weak reference.
2507 * The setup is that when there are no other references to @object,
2508 * only a weak reference is held in the reverse direction from @object
2509 * to the proxy object, but when there are other references held to
2510 * @object, a strong reference is held. The @notify callback is called
2511 * when the reference from @object to the proxy object should be
2512 * <firstterm>toggled</firstterm> from strong to weak (@is_last_ref
2513 * true) or weak to strong (@is_last_ref false).
2515 * Since a (normal) reference must be held to the object before
2516 * calling g_object_toggle_ref(), the initial state of the reverse
2517 * link is always strong.
2519 * Multiple toggle references may be added to the same gobject,
2520 * however if there are multiple toggle references to an object, none
2521 * of them will ever be notified until all but one are removed. For
2522 * this reason, you should only ever use a toggle reference if there
2523 * is important state in the proxy object.
2525 * Since: 2.8
2527 void
2528 g_object_add_toggle_ref (GObject *object,
2529 GToggleNotify notify,
2530 gpointer data)
2532 ToggleRefStack *tstack;
2533 guint i;
2535 g_return_if_fail (G_IS_OBJECT (object));
2536 g_return_if_fail (notify != NULL);
2537 g_return_if_fail (object->ref_count >= 1);
2539 g_object_ref (object);
2541 G_LOCK (toggle_refs_mutex);
2542 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2543 if (tstack)
2545 i = tstack->n_toggle_refs++;
2546 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2547 * in tstate->toggle_refs */
2548 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2550 else
2552 tstack = g_renew (ToggleRefStack, NULL, 1);
2553 tstack->object = object;
2554 tstack->n_toggle_refs = 1;
2555 i = 0;
2558 /* Set a flag for fast lookup after adding the first toggle reference */
2559 if (tstack->n_toggle_refs == 1)
2560 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2562 tstack->toggle_refs[i].notify = notify;
2563 tstack->toggle_refs[i].data = data;
2564 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2565 (GDestroyNotify)g_free);
2566 G_UNLOCK (toggle_refs_mutex);
2570 * g_object_remove_toggle_ref: (skip)
2571 * @object: a #GObject
2572 * @notify: a function to call when this reference is the
2573 * last reference to the object, or is no longer
2574 * the last reference.
2575 * @data: data to pass to @notify
2577 * Removes a reference added with g_object_add_toggle_ref(). The
2578 * reference count of the object is decreased by one.
2580 * Since: 2.8
2582 void
2583 g_object_remove_toggle_ref (GObject *object,
2584 GToggleNotify notify,
2585 gpointer data)
2587 ToggleRefStack *tstack;
2588 gboolean found_one = FALSE;
2590 g_return_if_fail (G_IS_OBJECT (object));
2591 g_return_if_fail (notify != NULL);
2593 G_LOCK (toggle_refs_mutex);
2594 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2595 if (tstack)
2597 guint i;
2599 for (i = 0; i < tstack->n_toggle_refs; i++)
2600 if (tstack->toggle_refs[i].notify == notify &&
2601 tstack->toggle_refs[i].data == data)
2603 found_one = TRUE;
2604 tstack->n_toggle_refs -= 1;
2605 if (i != tstack->n_toggle_refs)
2606 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
2608 if (tstack->n_toggle_refs == 0)
2609 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2611 break;
2614 G_UNLOCK (toggle_refs_mutex);
2616 if (found_one)
2617 g_object_unref (object);
2618 else
2619 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
2623 * g_object_ref:
2624 * @object: (type GObject.Object): a #GObject
2626 * Increases the reference count of @object.
2628 * Returns: (type GObject.Object) (transfer none): the same @object
2630 gpointer
2631 g_object_ref (gpointer _object)
2633 GObject *object = _object;
2634 gint old_val;
2636 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2637 g_return_val_if_fail (object->ref_count > 0, NULL);
2639 #ifdef G_ENABLE_DEBUG
2640 if (g_trap_object_ref == object)
2641 G_BREAKPOINT ();
2642 #endif /* G_ENABLE_DEBUG */
2645 old_val = g_atomic_int_exchange_and_add ((int *)&object->ref_count, 1);
2647 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
2648 toggle_refs_notify (object, FALSE);
2650 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
2652 return object;
2656 * g_object_unref:
2657 * @object: (type GObject.Object): a #GObject
2659 * Decreases the reference count of @object. When its reference count
2660 * drops to 0, the object is finalized (i.e. its memory is freed).
2662 void
2663 g_object_unref (gpointer _object)
2665 GObject *object = _object;
2666 gint old_ref;
2668 g_return_if_fail (G_IS_OBJECT (object));
2669 g_return_if_fail (object->ref_count > 0);
2671 #ifdef G_ENABLE_DEBUG
2672 if (g_trap_object_ref == object)
2673 G_BREAKPOINT ();
2674 #endif /* G_ENABLE_DEBUG */
2676 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
2677 retry_atomic_decrement1:
2678 old_ref = g_atomic_int_get (&object->ref_count);
2679 if (old_ref > 1)
2681 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2682 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2684 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2685 goto retry_atomic_decrement1;
2687 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2689 /* if we went from 2->1 we need to notify toggle refs if any */
2690 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2691 toggle_refs_notify (object, TRUE);
2693 else
2695 /* we are about tp remove the last reference */
2696 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
2697 G_OBJECT_GET_CLASS (object)->dispose (object);
2698 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
2700 /* may have been re-referenced meanwhile */
2701 retry_atomic_decrement2:
2702 old_ref = g_atomic_int_get ((int *)&object->ref_count);
2703 if (old_ref > 1)
2705 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
2706 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
2708 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
2709 goto retry_atomic_decrement2;
2711 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2713 /* if we went from 2->1 we need to notify toggle refs if any */
2714 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
2715 toggle_refs_notify (object, TRUE);
2717 return;
2720 /* we are still in the process of taking away the last ref */
2721 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
2722 g_signal_handlers_destroy (object);
2723 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
2725 /* decrement the last reference */
2726 old_ref = g_atomic_int_exchange_and_add ((int *)&object->ref_count, -1);
2728 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
2730 /* may have been re-referenced meanwhile */
2731 if (G_LIKELY (old_ref == 1))
2733 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
2734 G_OBJECT_GET_CLASS (object)->finalize (object);
2736 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
2738 #ifdef G_ENABLE_DEBUG
2739 IF_DEBUG (OBJECTS)
2741 /* catch objects not chaining finalize handlers */
2742 G_LOCK (debug_objects);
2743 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
2744 G_UNLOCK (debug_objects);
2746 #endif /* G_ENABLE_DEBUG */
2747 g_type_free_instance ((GTypeInstance*) object);
2753 * g_clear_object: (skip)
2754 * @object_ptr: a pointer to a #GObject reference
2756 * Clears a reference to a #GObject.
2758 * @object_ptr must not be %NULL.
2760 * If the reference is %NULL then this function does nothing.
2761 * Otherwise, the reference count of the object is decreased and the
2762 * pointer is set to %NULL.
2764 * This function is threadsafe and modifies the pointer atomically,
2765 * using memory barriers where needed.
2767 * A macro is also included that allows this function to be used without
2768 * pointer casts.
2770 * Since: 2.28
2772 #undef g_clear_object
2773 void
2774 g_clear_object (volatile GObject **object_ptr)
2776 gpointer *ptr = (gpointer) object_ptr;
2777 gpointer old;
2779 /* This is a little frustrating.
2780 * Would be nice to have an atomic exchange (with no compare).
2783 old = g_atomic_pointer_get (ptr);
2784 while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (ptr, old, NULL));
2786 if (old)
2787 g_object_unref (old);
2791 * g_object_get_qdata:
2792 * @object: The GObject to get a stored user data pointer from
2793 * @quark: A #GQuark, naming the user data pointer
2795 * This function gets back user data pointers stored via
2796 * g_object_set_qdata().
2798 * Returns: (transfer none): The user data pointer set, or %NULL
2800 gpointer
2801 g_object_get_qdata (GObject *object,
2802 GQuark quark)
2804 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2806 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2810 * g_object_set_qdata: (skip)
2811 * @object: The GObject to set store a user data pointer
2812 * @quark: A #GQuark, naming the user data pointer
2813 * @data: An opaque user data pointer
2815 * This sets an opaque, named pointer on an object.
2816 * The name is specified through a #GQuark (retrived e.g. via
2817 * g_quark_from_static_string()), and the pointer
2818 * can be gotten back from the @object with g_object_get_qdata()
2819 * until the @object is finalized.
2820 * Setting a previously set user data pointer, overrides (frees)
2821 * the old pointer set, using #NULL as pointer essentially
2822 * removes the data stored.
2824 void
2825 g_object_set_qdata (GObject *object,
2826 GQuark quark,
2827 gpointer data)
2829 g_return_if_fail (G_IS_OBJECT (object));
2830 g_return_if_fail (quark > 0);
2832 g_datalist_id_set_data (&object->qdata, quark, data);
2836 * g_object_set_qdata_full: (skip)
2837 * @object: The GObject to set store a user data pointer
2838 * @quark: A #GQuark, naming the user data pointer
2839 * @data: An opaque user data pointer
2840 * @destroy: Function to invoke with @data as argument, when @data
2841 * needs to be freed
2843 * This function works like g_object_set_qdata(), but in addition,
2844 * a void (*destroy) (gpointer) function may be specified which is
2845 * called with @data as argument when the @object is finalized, or
2846 * the data is being overwritten by a call to g_object_set_qdata()
2847 * with the same @quark.
2849 void
2850 g_object_set_qdata_full (GObject *object,
2851 GQuark quark,
2852 gpointer data,
2853 GDestroyNotify destroy)
2855 g_return_if_fail (G_IS_OBJECT (object));
2856 g_return_if_fail (quark > 0);
2858 g_datalist_id_set_data_full (&object->qdata, quark, data,
2859 data ? destroy : (GDestroyNotify) NULL);
2863 * g_object_steal_qdata:
2864 * @object: The GObject to get a stored user data pointer from
2865 * @quark: A #GQuark, naming the user data pointer
2867 * This function gets back user data pointers stored via
2868 * g_object_set_qdata() and removes the @data from object
2869 * without invoking its destroy() function (if any was
2870 * set).
2871 * Usually, calling this function is only required to update
2872 * user data pointers with a destroy notifier, for example:
2873 * |[
2874 * void
2875 * object_add_to_user_list (GObject *object,
2876 * const gchar *new_string)
2878 * // the quark, naming the object data
2879 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
2880 * // retrive the old string list
2881 * GList *list = g_object_steal_qdata (object, quark_string_list);
2883 * // prepend new string
2884 * list = g_list_prepend (list, g_strdup (new_string));
2885 * // this changed 'list', so we need to set it again
2886 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
2888 * static void
2889 * free_string_list (gpointer data)
2891 * GList *node, *list = data;
2893 * for (node = list; node; node = node->next)
2894 * g_free (node->data);
2895 * g_list_free (list);
2897 * ]|
2898 * Using g_object_get_qdata() in the above example, instead of
2899 * g_object_steal_qdata() would have left the destroy function set,
2900 * and thus the partial string list would have been freed upon
2901 * g_object_set_qdata_full().
2903 * Returns: (transfer full): The user data pointer set, or %NULL
2905 gpointer
2906 g_object_steal_qdata (GObject *object,
2907 GQuark quark)
2909 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2910 g_return_val_if_fail (quark > 0, NULL);
2912 return g_datalist_id_remove_no_notify (&object->qdata, quark);
2916 * g_object_get_data:
2917 * @object: #GObject containing the associations
2918 * @key: name of the key for that association
2920 * Gets a named field from the objects table of associations (see g_object_set_data()).
2922 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
2924 gpointer
2925 g_object_get_data (GObject *object,
2926 const gchar *key)
2928 GQuark quark;
2930 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2931 g_return_val_if_fail (key != NULL, NULL);
2933 quark = g_quark_try_string (key);
2935 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
2939 * g_object_set_data:
2940 * @object: #GObject containing the associations.
2941 * @key: name of the key
2942 * @data: data to associate with that key
2944 * Each object carries around a table of associations from
2945 * strings to pointers. This function lets you set an association.
2947 * If the object already had an association with that name,
2948 * the old association will be destroyed.
2950 void
2951 g_object_set_data (GObject *object,
2952 const gchar *key,
2953 gpointer data)
2955 g_return_if_fail (G_IS_OBJECT (object));
2956 g_return_if_fail (key != NULL);
2958 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
2962 * g_object_set_data_full: (skip)
2963 * @object: #GObject containing the associations
2964 * @key: name of the key
2965 * @data: data to associate with that key
2966 * @destroy: function to call when the association is destroyed
2968 * Like g_object_set_data() except it adds notification
2969 * for when the association is destroyed, either by setting it
2970 * to a different value or when the object is destroyed.
2972 * Note that the @destroy callback is not called if @data is %NULL.
2974 void
2975 g_object_set_data_full (GObject *object,
2976 const gchar *key,
2977 gpointer data,
2978 GDestroyNotify destroy)
2980 g_return_if_fail (G_IS_OBJECT (object));
2981 g_return_if_fail (key != NULL);
2983 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
2984 data ? destroy : (GDestroyNotify) NULL);
2988 * g_object_steal_data:
2989 * @object: #GObject containing the associations
2990 * @key: name of the key
2992 * Remove a specified datum from the object's data associations,
2993 * without invoking the association's destroy handler.
2995 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
2997 gpointer
2998 g_object_steal_data (GObject *object,
2999 const gchar *key)
3001 GQuark quark;
3003 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3004 g_return_val_if_fail (key != NULL, NULL);
3006 quark = g_quark_try_string (key);
3008 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3011 static void
3012 g_value_object_init (GValue *value)
3014 value->data[0].v_pointer = NULL;
3017 static void
3018 g_value_object_free_value (GValue *value)
3020 if (value->data[0].v_pointer)
3021 g_object_unref (value->data[0].v_pointer);
3024 static void
3025 g_value_object_copy_value (const GValue *src_value,
3026 GValue *dest_value)
3028 if (src_value->data[0].v_pointer)
3029 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3030 else
3031 dest_value->data[0].v_pointer = NULL;
3034 static void
3035 g_value_object_transform_value (const GValue *src_value,
3036 GValue *dest_value)
3038 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)))
3039 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3040 else
3041 dest_value->data[0].v_pointer = NULL;
3044 static gpointer
3045 g_value_object_peek_pointer (const GValue *value)
3047 return value->data[0].v_pointer;
3050 static gchar*
3051 g_value_object_collect_value (GValue *value,
3052 guint n_collect_values,
3053 GTypeCValue *collect_values,
3054 guint collect_flags)
3056 if (collect_values[0].v_pointer)
3058 GObject *object = collect_values[0].v_pointer;
3060 if (object->g_type_instance.g_class == NULL)
3061 return g_strconcat ("invalid unclassed object pointer for value type `",
3062 G_VALUE_TYPE_NAME (value),
3063 "'",
3064 NULL);
3065 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3066 return g_strconcat ("invalid object type `",
3067 G_OBJECT_TYPE_NAME (object),
3068 "' for value type `",
3069 G_VALUE_TYPE_NAME (value),
3070 "'",
3071 NULL);
3072 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3073 value->data[0].v_pointer = g_object_ref (object);
3075 else
3076 value->data[0].v_pointer = NULL;
3078 return NULL;
3081 static gchar*
3082 g_value_object_lcopy_value (const GValue *value,
3083 guint n_collect_values,
3084 GTypeCValue *collect_values,
3085 guint collect_flags)
3087 GObject **object_p = collect_values[0].v_pointer;
3089 if (!object_p)
3090 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3092 if (!value->data[0].v_pointer)
3093 *object_p = NULL;
3094 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3095 *object_p = value->data[0].v_pointer;
3096 else
3097 *object_p = g_object_ref (value->data[0].v_pointer);
3099 return NULL;
3103 * g_value_set_object:
3104 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3105 * @v_object: (type GObject.Object): object value to be set
3107 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3109 * g_value_set_object() increases the reference count of @v_object
3110 * (the #GValue holds a reference to @v_object). If you do not wish
3111 * to increase the reference count of the object (i.e. you wish to
3112 * pass your current reference to the #GValue because you no longer
3113 * need it), use g_value_take_object() instead.
3115 * It is important that your #GValue holds a reference to @v_object (either its
3116 * own, or one it has taken) to ensure that the object won't be destroyed while
3117 * the #GValue still exists).
3119 void
3120 g_value_set_object (GValue *value,
3121 gpointer v_object)
3123 GObject *old;
3125 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3127 old = value->data[0].v_pointer;
3129 if (v_object)
3131 g_return_if_fail (G_IS_OBJECT (v_object));
3132 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3134 value->data[0].v_pointer = v_object;
3135 g_object_ref (value->data[0].v_pointer);
3137 else
3138 value->data[0].v_pointer = NULL;
3140 if (old)
3141 g_object_unref (old);
3145 * g_value_set_object_take_ownership: (skip)
3146 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3147 * @v_object: object value to be set
3149 * This is an internal function introduced mainly for C marshallers.
3151 * Deprecated: 2.4: Use g_value_take_object() instead.
3153 void
3154 g_value_set_object_take_ownership (GValue *value,
3155 gpointer v_object)
3157 g_value_take_object (value, v_object);
3161 * g_value_take_object: (skip)
3162 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3163 * @v_object: object value to be set
3165 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3166 * and takes over the ownership of the callers reference to @v_object;
3167 * the caller doesn't have to unref it any more (i.e. the reference
3168 * count of the object is not increased).
3170 * If you want the #GValue to hold its own reference to @v_object, use
3171 * g_value_set_object() instead.
3173 * Since: 2.4
3175 void
3176 g_value_take_object (GValue *value,
3177 gpointer v_object)
3179 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3181 if (value->data[0].v_pointer)
3183 g_object_unref (value->data[0].v_pointer);
3184 value->data[0].v_pointer = NULL;
3187 if (v_object)
3189 g_return_if_fail (G_IS_OBJECT (v_object));
3190 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3192 value->data[0].v_pointer = v_object; /* we take over the reference count */
3197 * g_value_get_object:
3198 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3200 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3202 * Returns: (type GObject.Object) (transfer none): object contents of @value
3204 gpointer
3205 g_value_get_object (const GValue *value)
3207 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3209 return value->data[0].v_pointer;
3213 * g_value_dup_object:
3214 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3216 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3217 * its reference count. If the contents of the #GValue are %NULL, then
3218 * %NULL will be returned.
3220 * Returns: (type GObject.Object) (transfer full): object content of @value,
3221 * should be unreferenced when no longer needed.
3223 gpointer
3224 g_value_dup_object (const GValue *value)
3226 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3228 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3232 * g_signal_connect_object: (skip)
3233 * @instance: the instance to connect to.
3234 * @detailed_signal: a string of the form "signal-name::detail".
3235 * @c_handler: the #GCallback to connect.
3236 * @gobject: the object to pass as data to @c_handler.
3237 * @connect_flags: a combination of #GConnnectFlags.
3239 * This is similar to g_signal_connect_data(), but uses a closure which
3240 * ensures that the @gobject stays alive during the call to @c_handler
3241 * by temporarily adding a reference count to @gobject.
3243 * Note that there is a bug in GObject that makes this function
3244 * much less useful than it might seem otherwise. Once @gobject is
3245 * disposed, the callback will no longer be called, but, the signal
3246 * handler is <emphasis>not</emphasis> currently disconnected. If the
3247 * @instance is itself being freed at the same time than this doesn't
3248 * matter, since the signal will automatically be removed, but
3249 * if @instance persists, then the signal handler will leak. You
3250 * should not remove the signal yourself because in a future versions of
3251 * GObject, the handler <emphasis>will</emphasis> automatically
3252 * be disconnected.
3254 * It's possible to work around this problem in a way that will
3255 * continue to work with future versions of GObject by checking
3256 * that the signal handler is still connected before disconnected it:
3257 * <informalexample><programlisting>
3258 * if (g_signal_handler_is_connected (instance, id))
3259 * g_signal_handler_disconnect (instance, id);
3260 * </programlisting></informalexample>
3262 * Returns: the handler id.
3264 gulong
3265 g_signal_connect_object (gpointer instance,
3266 const gchar *detailed_signal,
3267 GCallback c_handler,
3268 gpointer gobject,
3269 GConnectFlags connect_flags)
3271 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3272 g_return_val_if_fail (detailed_signal != NULL, 0);
3273 g_return_val_if_fail (c_handler != NULL, 0);
3275 if (gobject)
3277 GClosure *closure;
3279 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3281 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3283 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3285 else
3286 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3289 typedef struct {
3290 GObject *object;
3291 guint n_closures;
3292 GClosure *closures[1]; /* flexible array */
3293 } CArray;
3294 /* don't change this structure without supplying an accessor for
3295 * watched closures, e.g.:
3296 * GSList* g_object_list_watched_closures (GObject *object)
3298 * CArray *carray;
3299 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3300 * carray = g_object_get_data (object, "GObject-closure-array");
3301 * if (carray)
3303 * GSList *slist = NULL;
3304 * guint i;
3305 * for (i = 0; i < carray->n_closures; i++)
3306 * slist = g_slist_prepend (slist, carray->closures[i]);
3307 * return slist;
3309 * return NULL;
3313 static void
3314 object_remove_closure (gpointer data,
3315 GClosure *closure)
3317 GObject *object = data;
3318 CArray *carray;
3319 guint i;
3321 G_LOCK (closure_array_mutex);
3322 carray = g_object_get_qdata (object, quark_closure_array);
3323 for (i = 0; i < carray->n_closures; i++)
3324 if (carray->closures[i] == closure)
3326 carray->n_closures--;
3327 if (i < carray->n_closures)
3328 carray->closures[i] = carray->closures[carray->n_closures];
3329 G_UNLOCK (closure_array_mutex);
3330 return;
3332 G_UNLOCK (closure_array_mutex);
3333 g_assert_not_reached ();
3336 static void
3337 destroy_closure_array (gpointer data)
3339 CArray *carray = data;
3340 GObject *object = carray->object;
3341 guint i, n = carray->n_closures;
3343 for (i = 0; i < n; i++)
3345 GClosure *closure = carray->closures[i];
3347 /* removing object_remove_closure() upfront is probably faster than
3348 * letting it fiddle with quark_closure_array which is empty anyways
3350 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3351 g_closure_invalidate (closure);
3353 g_free (carray);
3357 * g_object_watch_closure:
3358 * @object: GObject restricting lifetime of @closure
3359 * @closure: GClosure to watch
3361 * This function essentially limits the life time of the @closure to
3362 * the life time of the object. That is, when the object is finalized,
3363 * the @closure is invalidated by calling g_closure_invalidate() on
3364 * it, in order to prevent invocations of the closure with a finalized
3365 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3366 * added as marshal guards to the @closure, to ensure that an extra
3367 * reference count is held on @object during invocation of the
3368 * @closure. Usually, this function will be called on closures that
3369 * use this @object as closure data.
3371 void
3372 g_object_watch_closure (GObject *object,
3373 GClosure *closure)
3375 CArray *carray;
3376 guint i;
3378 g_return_if_fail (G_IS_OBJECT (object));
3379 g_return_if_fail (closure != NULL);
3380 g_return_if_fail (closure->is_invalid == FALSE);
3381 g_return_if_fail (closure->in_marshal == FALSE);
3382 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3384 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3385 g_closure_add_marshal_guards (closure,
3386 object, (GClosureNotify) g_object_ref,
3387 object, (GClosureNotify) g_object_unref);
3388 G_LOCK (closure_array_mutex);
3389 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3390 if (!carray)
3392 carray = g_renew (CArray, NULL, 1);
3393 carray->object = object;
3394 carray->n_closures = 1;
3395 i = 0;
3397 else
3399 i = carray->n_closures++;
3400 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3402 carray->closures[i] = closure;
3403 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3404 G_UNLOCK (closure_array_mutex);
3408 * g_closure_new_object:
3409 * @sizeof_closure: the size of the structure to allocate, must be at least
3410 * <literal>sizeof (GClosure)</literal>
3411 * @object: a #GObject pointer to store in the @data field of the newly
3412 * allocated #GClosure
3414 * A variant of g_closure_new_simple() which stores @object in the
3415 * @data field of the closure and calls g_object_watch_closure() on
3416 * @object and the created closure. This function is mainly useful
3417 * when implementing new types of closures.
3419 * Returns: (transfer full): a newly allocated #GClosure
3421 GClosure*
3422 g_closure_new_object (guint sizeof_closure,
3423 GObject *object)
3425 GClosure *closure;
3427 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3428 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3430 closure = g_closure_new_simple (sizeof_closure, object);
3431 g_object_watch_closure (object, closure);
3433 return closure;
3437 * g_cclosure_new_object: (skip)
3438 * @callback_func: the function to invoke
3439 * @object: a #GObject pointer to pass to @callback_func
3441 * A variant of g_cclosure_new() which uses @object as @user_data and
3442 * calls g_object_watch_closure() on @object and the created
3443 * closure. This function is useful when you have a callback closely
3444 * associated with a #GObject, and want the callback to no longer run
3445 * after the object is is freed.
3447 * Returns: a new #GCClosure
3449 GClosure*
3450 g_cclosure_new_object (GCallback callback_func,
3451 GObject *object)
3453 GClosure *closure;
3455 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3456 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3457 g_return_val_if_fail (callback_func != NULL, NULL);
3459 closure = g_cclosure_new (callback_func, object, NULL);
3460 g_object_watch_closure (object, closure);
3462 return closure;
3466 * g_cclosure_new_object_swap: (skip)
3467 * @callback_func: the function to invoke
3468 * @object: a #GObject pointer to pass to @callback_func
3470 * A variant of g_cclosure_new_swap() which uses @object as @user_data
3471 * and calls g_object_watch_closure() on @object and the created
3472 * closure. This function is useful when you have a callback closely
3473 * associated with a #GObject, and want the callback to no longer run
3474 * after the object is is freed.
3476 * Returns: a new #GCClosure
3478 GClosure*
3479 g_cclosure_new_object_swap (GCallback callback_func,
3480 GObject *object)
3482 GClosure *closure;
3484 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3485 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
3486 g_return_val_if_fail (callback_func != NULL, NULL);
3488 closure = g_cclosure_new_swap (callback_func, object, NULL);
3489 g_object_watch_closure (object, closure);
3491 return closure;
3494 gsize
3495 g_object_compat_control (gsize what,
3496 gpointer data)
3498 switch (what)
3500 gpointer *pp;
3501 case 1: /* floating base type */
3502 return G_TYPE_INITIALLY_UNOWNED;
3503 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3504 floating_flag_handler = (guint(*)(GObject*,gint)) data;
3505 return 1;
3506 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
3507 pp = data;
3508 *pp = floating_flag_handler;
3509 return 1;
3510 default:
3511 return 0;
3515 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
3517 static void
3518 g_initially_unowned_init (GInitiallyUnowned *object)
3520 g_object_force_floating (object);
3523 static void
3524 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)