Update Hungarian translation
[glib.git] / gobject / gobject.c
blobd04f89c6ca20dd38d1413df593b4794f58892628
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, see <http://www.gnu.org/licenses/>.
19 * MT safe with regards to reference counting.
22 #include "config.h"
24 #include <string.h>
25 #include <signal.h>
27 #include "gobject.h"
28 #include "gtype-private.h"
29 #include "gvaluecollector.h"
30 #include "gsignal.h"
31 #include "gparamspecs.h"
32 #include "gvaluetypes.h"
33 #include "gobject_trace.h"
34 #include "gconstructor.h"
36 /**
37 * SECTION:objects
38 * @title: GObject
39 * @short_description: The base object type
40 * @see_also: #GParamSpecObject, g_param_spec_object()
42 * GObject is the fundamental type providing the common attributes and
43 * methods for all object types in GTK+, Pango and other libraries
44 * based on GObject. The GObject class provides methods for object
45 * construction and destruction, property access methods, and signal
46 * support. Signals are described in detail [here][gobject-Signals].
48 * For a tutorial on implementing a new GObject class, see [How to define and
49 * implement a new GObject][howto-gobject]. For a list of naming conventions for
50 * GObjects and their methods, see the [GType conventions][gtype-conventions].
51 * For the high-level concepts behind GObject, read [Instantiable classed types:
52 * Objects][gtype-instantiable-classed].
54 * ## Floating references # {#floating-ref}
56 * GInitiallyUnowned is derived from GObject. The only difference between
57 * the two is that the initial reference of a GInitiallyUnowned is flagged
58 * as a "floating" reference. This means that it is not specifically
59 * claimed to be "owned" by any code portion. The main motivation for
60 * providing floating references is C convenience. In particular, it
61 * allows code to be written as:
62 * |[<!-- language="C" -->
63 * container = create_container ();
64 * container_add_child (container, create_child());
65 * ]|
66 * If container_add_child() calls g_object_ref_sink() on the passed-in child,
67 * no reference of the newly created child is leaked. Without floating
68 * references, container_add_child() can only g_object_ref() the new child,
69 * so to implement this code without reference leaks, it would have to be
70 * written as:
71 * |[<!-- language="C" -->
72 * Child *child;
73 * container = create_container ();
74 * child = create_child ();
75 * container_add_child (container, child);
76 * g_object_unref (child);
77 * ]|
78 * The floating reference can be converted into an ordinary reference by
79 * calling g_object_ref_sink(). For already sunken objects (objects that
80 * don't have a floating reference anymore), g_object_ref_sink() is equivalent
81 * to g_object_ref() and returns a new reference.
83 * Since floating references are useful almost exclusively for C convenience,
84 * language bindings that provide automated reference and memory ownership
85 * maintenance (such as smart pointers or garbage collection) should not
86 * expose floating references in their API.
88 * Some object implementations may need to save an objects floating state
89 * across certain code portions (an example is #GtkMenu), to achieve this,
90 * the following sequence can be used:
92 * |[<!-- language="C" -->
93 * // save floating state
94 * gboolean was_floating = g_object_is_floating (object);
95 * g_object_ref_sink (object);
96 * // protected code portion
98 * ...
100 * // restore floating state
101 * if (was_floating)
102 * g_object_force_floating (object);
103 * else
104 * g_object_unref (object); // release previously acquired reference
105 * ]|
109 /* --- macros --- */
110 #define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
111 #define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
113 #define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
114 #define OBJECT_HAS_TOGGLE_REF(object) \
115 ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
116 #define OBJECT_FLOATING_FLAG 0x2
118 #define CLASS_HAS_PROPS_FLAG 0x1
119 #define CLASS_HAS_PROPS(class) \
120 ((class)->flags & CLASS_HAS_PROPS_FLAG)
121 #define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
122 ((class)->constructor != g_object_constructor)
123 #define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
124 ((class)->constructed != g_object_constructed)
126 #define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
127 #define CLASS_HAS_DERIVED_CLASS(class) \
128 ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
130 /* --- signals --- */
131 enum {
132 NOTIFY,
133 LAST_SIGNAL
137 /* --- properties --- */
138 enum {
139 PROP_NONE
143 /* --- prototypes --- */
144 static void g_object_base_class_init (GObjectClass *class);
145 static void g_object_base_class_finalize (GObjectClass *class);
146 static void g_object_do_class_init (GObjectClass *class);
147 static void g_object_init (GObject *object,
148 GObjectClass *class);
149 static GObject* g_object_constructor (GType type,
150 guint n_construct_properties,
151 GObjectConstructParam *construct_params);
152 static void g_object_constructed (GObject *object);
153 static void g_object_real_dispose (GObject *object);
154 static void g_object_finalize (GObject *object);
155 static void g_object_do_set_property (GObject *object,
156 guint property_id,
157 const GValue *value,
158 GParamSpec *pspec);
159 static void g_object_do_get_property (GObject *object,
160 guint property_id,
161 GValue *value,
162 GParamSpec *pspec);
163 static void g_value_object_init (GValue *value);
164 static void g_value_object_free_value (GValue *value);
165 static void g_value_object_copy_value (const GValue *src_value,
166 GValue *dest_value);
167 static void g_value_object_transform_value (const GValue *src_value,
168 GValue *dest_value);
169 static gpointer g_value_object_peek_pointer (const GValue *value);
170 static gchar* g_value_object_collect_value (GValue *value,
171 guint n_collect_values,
172 GTypeCValue *collect_values,
173 guint collect_flags);
174 static gchar* g_value_object_lcopy_value (const GValue *value,
175 guint n_collect_values,
176 GTypeCValue *collect_values,
177 guint collect_flags);
178 static void g_object_dispatch_properties_changed (GObject *object,
179 guint n_pspecs,
180 GParamSpec **pspecs);
181 static guint object_floating_flag_handler (GObject *object,
182 gint job);
184 static void object_interface_check_properties (gpointer check_data,
185 gpointer g_iface);
187 /* --- typedefs --- */
188 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
190 struct _GObjectNotifyQueue
192 GSList *pspecs;
193 guint16 n_pspecs;
194 guint16 freeze_count;
197 /* --- variables --- */
198 G_LOCK_DEFINE_STATIC (closure_array_mutex);
199 G_LOCK_DEFINE_STATIC (weak_refs_mutex);
200 G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
201 static GQuark quark_closure_array = 0;
202 static GQuark quark_weak_refs = 0;
203 static GQuark quark_toggle_refs = 0;
204 static GQuark quark_notify_queue;
205 static GQuark quark_in_construction;
206 static GParamSpecPool *pspec_pool = NULL;
207 static gulong gobject_signals[LAST_SIGNAL] = { 0, };
208 static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
209 /* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
210 static GQuark quark_weak_locations = 0;
211 static GRWLock weak_locations_lock;
213 G_LOCK_DEFINE_STATIC(notify_lock);
215 /* --- functions --- */
216 static void
217 g_object_notify_queue_free (gpointer data)
219 GObjectNotifyQueue *nqueue = data;
221 g_slist_free (nqueue->pspecs);
222 g_slice_free (GObjectNotifyQueue, nqueue);
225 static GObjectNotifyQueue*
226 g_object_notify_queue_freeze (GObject *object,
227 gboolean conditional)
229 GObjectNotifyQueue *nqueue;
231 G_LOCK(notify_lock);
232 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
233 if (!nqueue)
235 if (conditional)
237 G_UNLOCK(notify_lock);
238 return NULL;
241 nqueue = g_slice_new0 (GObjectNotifyQueue);
242 g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
243 nqueue, g_object_notify_queue_free);
246 if (nqueue->freeze_count >= 65535)
247 g_critical("Free queue for %s (%p) is larger than 65535,"
248 " called g_object_freeze_notify() too often."
249 " Forgot to call g_object_thaw_notify() or infinite loop",
250 G_OBJECT_TYPE_NAME (object), object);
251 else
252 nqueue->freeze_count++;
253 G_UNLOCK(notify_lock);
255 return nqueue;
258 static void
259 g_object_notify_queue_thaw (GObject *object,
260 GObjectNotifyQueue *nqueue)
262 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
263 GSList *slist;
264 guint n_pspecs = 0;
266 g_return_if_fail (nqueue->freeze_count > 0);
267 g_return_if_fail (g_atomic_int_get(&object->ref_count) > 0);
269 G_LOCK(notify_lock);
271 /* Just make sure we never get into some nasty race condition */
272 if (G_UNLIKELY(nqueue->freeze_count == 0)) {
273 G_UNLOCK(notify_lock);
274 g_warning ("%s: property-changed notification for %s(%p) is not frozen",
275 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
276 return;
279 nqueue->freeze_count--;
280 if (nqueue->freeze_count) {
281 G_UNLOCK(notify_lock);
282 return;
285 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
287 for (slist = nqueue->pspecs; slist; slist = slist->next)
289 pspecs[n_pspecs++] = slist->data;
291 g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
293 G_UNLOCK(notify_lock);
295 if (n_pspecs)
296 G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
297 g_free (free_me);
300 static void
301 g_object_notify_queue_add (GObject *object,
302 GObjectNotifyQueue *nqueue,
303 GParamSpec *pspec)
305 G_LOCK(notify_lock);
307 g_assert (nqueue->n_pspecs < 65535);
309 if (g_slist_find (nqueue->pspecs, pspec) == NULL)
311 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
312 nqueue->n_pspecs++;
315 G_UNLOCK(notify_lock);
318 #ifdef G_ENABLE_DEBUG
319 G_LOCK_DEFINE_STATIC (debug_objects);
320 static guint debug_objects_count = 0;
321 static GHashTable *debug_objects_ht = NULL;
323 static void
324 debug_objects_foreach (gpointer key,
325 gpointer value,
326 gpointer user_data)
328 GObject *object = value;
330 g_message ("[%p] stale %s\tref_count=%u",
331 object,
332 G_OBJECT_TYPE_NAME (object),
333 object->ref_count);
336 #ifdef G_HAS_CONSTRUCTORS
337 #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
338 #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
339 #endif
340 G_DEFINE_DESTRUCTOR(debug_objects_atexit)
341 #endif /* G_HAS_CONSTRUCTORS */
343 static void
344 debug_objects_atexit (void)
346 GOBJECT_IF_DEBUG (OBJECTS,
348 G_LOCK (debug_objects);
349 g_message ("stale GObjects: %u", debug_objects_count);
350 g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
351 G_UNLOCK (debug_objects);
354 #endif /* G_ENABLE_DEBUG */
356 void
357 _g_object_type_init (void)
359 static gboolean initialized = FALSE;
360 static const GTypeFundamentalInfo finfo = {
361 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
363 GTypeInfo info = {
364 sizeof (GObjectClass),
365 (GBaseInitFunc) g_object_base_class_init,
366 (GBaseFinalizeFunc) g_object_base_class_finalize,
367 (GClassInitFunc) g_object_do_class_init,
368 NULL /* class_destroy */,
369 NULL /* class_data */,
370 sizeof (GObject),
371 0 /* n_preallocs */,
372 (GInstanceInitFunc) g_object_init,
373 NULL, /* value_table */
375 static const GTypeValueTable value_table = {
376 g_value_object_init, /* value_init */
377 g_value_object_free_value, /* value_free */
378 g_value_object_copy_value, /* value_copy */
379 g_value_object_peek_pointer, /* value_peek_pointer */
380 "p", /* collect_format */
381 g_value_object_collect_value, /* collect_value */
382 "p", /* lcopy_format */
383 g_value_object_lcopy_value, /* lcopy_value */
385 GType type;
387 g_return_if_fail (initialized == FALSE);
388 initialized = TRUE;
390 /* G_TYPE_OBJECT
392 info.value_table = &value_table;
393 type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
394 g_assert (type == G_TYPE_OBJECT);
395 g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
397 #if G_ENABLE_DEBUG
398 /* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS
399 * conditional in between, as the C spec leaves conditionals inside macro
400 * expansions as undefined behavior. Only GCC and Clang are known to work
401 * but compilation breaks on MSVC.
403 * See: https://bugzilla.gnome.org/show_bug.cgi?id=769504
405 if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \
407 debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
408 # ifndef G_HAS_CONSTRUCTORS
409 g_atexit (debug_objects_atexit);
410 # endif /* G_HAS_CONSTRUCTORS */
412 #endif /* G_ENABLE_DEBUG */
415 static void
416 g_object_base_class_init (GObjectClass *class)
418 GObjectClass *pclass = g_type_class_peek_parent (class);
420 /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
421 class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
423 if (pclass)
424 pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
426 /* reset instance specific fields and methods that don't get inherited */
427 class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
428 class->get_property = NULL;
429 class->set_property = NULL;
432 static void
433 g_object_base_class_finalize (GObjectClass *class)
435 GList *list, *node;
437 _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
439 g_slist_free (class->construct_properties);
440 class->construct_properties = NULL;
441 list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
442 for (node = list; node; node = node->next)
444 GParamSpec *pspec = node->data;
446 g_param_spec_pool_remove (pspec_pool, pspec);
447 PARAM_SPEC_SET_PARAM_ID (pspec, 0);
448 g_param_spec_unref (pspec);
450 g_list_free (list);
453 static void
454 g_object_do_class_init (GObjectClass *class)
456 /* read the comment about typedef struct CArray; on why not to change this quark */
457 quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
459 quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
460 quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
461 quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
462 quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
463 quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
464 pspec_pool = g_param_spec_pool_new (TRUE);
466 class->constructor = g_object_constructor;
467 class->constructed = g_object_constructed;
468 class->set_property = g_object_do_set_property;
469 class->get_property = g_object_do_get_property;
470 class->dispose = g_object_real_dispose;
471 class->finalize = g_object_finalize;
472 class->dispatch_properties_changed = g_object_dispatch_properties_changed;
473 class->notify = NULL;
476 * GObject::notify:
477 * @gobject: the object which received the signal.
478 * @pspec: the #GParamSpec of the property which changed.
480 * The notify signal is emitted on an object when one of its
481 * properties has been changed. Note that getting this signal
482 * doesn't guarantee that the value of the property has actually
483 * changed, it may also be emitted when the setter for the property
484 * is called to reinstate the previous value.
486 * This signal is typically used to obtain change notification for a
487 * single property, by specifying the property name as a detail in the
488 * g_signal_connect() call, like this:
489 * |[<!-- language="C" -->
490 * g_signal_connect (text_view->buffer, "notify::paste-target-list",
491 * G_CALLBACK (gtk_text_view_target_list_notify),
492 * text_view)
493 * ]|
494 * It is important to note that you must use
495 * [canonical][canonical-parameter-name] parameter names as
496 * detail strings for the notify signal.
498 gobject_signals[NOTIFY] =
499 g_signal_new (g_intern_static_string ("notify"),
500 G_TYPE_FROM_CLASS (class),
501 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
502 G_STRUCT_OFFSET (GObjectClass, notify),
503 NULL, NULL,
504 g_cclosure_marshal_VOID__PARAM,
505 G_TYPE_NONE,
506 1, G_TYPE_PARAM);
508 /* Install a check function that we'll use to verify that classes that
509 * implement an interface implement all properties for that interface
511 g_type_add_interface_check (NULL, object_interface_check_properties);
514 static inline void
515 install_property_internal (GType g_type,
516 guint property_id,
517 GParamSpec *pspec)
519 if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
521 g_warning ("When installing property: type '%s' already has a property named '%s'",
522 g_type_name (g_type),
523 pspec->name);
524 return;
527 g_param_spec_ref_sink (pspec);
528 PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
529 g_param_spec_pool_insert (pspec_pool, pspec, g_type);
533 * g_object_class_install_property:
534 * @oclass: a #GObjectClass
535 * @property_id: the id for the new property
536 * @pspec: the #GParamSpec for the new property
538 * Installs a new property.
540 * All properties should be installed during the class initializer. It
541 * is possible to install properties after that, but doing so is not
542 * recommend, and specifically, is not guaranteed to be thread-safe vs.
543 * use of properties on the same type on other threads.
545 * Note that it is possible to redefine a property in a derived class,
546 * by installing a property with the same name. This can be useful at times,
547 * e.g. to change the range of allowed values or the default value.
549 void
550 g_object_class_install_property (GObjectClass *class,
551 guint property_id,
552 GParamSpec *pspec)
554 g_return_if_fail (G_IS_OBJECT_CLASS (class));
555 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
557 if (CLASS_HAS_DERIVED_CLASS (class))
558 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
560 class->flags |= CLASS_HAS_PROPS_FLAG;
562 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
563 if (pspec->flags & G_PARAM_WRITABLE)
564 g_return_if_fail (class->set_property != NULL);
565 if (pspec->flags & G_PARAM_READABLE)
566 g_return_if_fail (class->get_property != NULL);
567 g_return_if_fail (property_id > 0);
568 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
569 if (pspec->flags & G_PARAM_CONSTRUCT)
570 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
571 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
572 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
574 install_property_internal (G_OBJECT_CLASS_TYPE (class), property_id, pspec);
576 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
577 class->construct_properties = g_slist_append (class->construct_properties, pspec);
579 /* for property overrides of construct properties, we have to get rid
580 * of the overidden inherited construct property
582 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type_parent (G_OBJECT_CLASS_TYPE (class)), TRUE);
583 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
584 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
588 * g_object_class_install_properties:
589 * @oclass: a #GObjectClass
590 * @n_pspecs: the length of the #GParamSpecs array
591 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
592 * defining the new properties
594 * Installs new properties from an array of #GParamSpecs.
596 * All properties should be installed during the class initializer. It
597 * is possible to install properties after that, but doing so is not
598 * recommend, and specifically, is not guaranteed to be thread-safe vs.
599 * use of properties on the same type on other threads.
601 * The property id of each property is the index of each #GParamSpec in
602 * the @pspecs array.
604 * The property id of 0 is treated specially by #GObject and it should not
605 * be used to store a #GParamSpec.
607 * This function should be used if you plan to use a static array of
608 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
609 * class initialization:
611 * |[<!-- language="C" -->
612 * enum {
613 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
614 * };
616 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
618 * static void
619 * my_object_class_init (MyObjectClass *klass)
621 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
623 * obj_properties[PROP_FOO] =
624 * g_param_spec_int ("foo", "Foo", "Foo",
625 * -1, G_MAXINT,
626 * 0,
627 * G_PARAM_READWRITE);
629 * obj_properties[PROP_BAR] =
630 * g_param_spec_string ("bar", "Bar", "Bar",
631 * NULL,
632 * G_PARAM_READWRITE);
634 * gobject_class->set_property = my_object_set_property;
635 * gobject_class->get_property = my_object_get_property;
636 * g_object_class_install_properties (gobject_class,
637 * N_PROPERTIES,
638 * obj_properties);
640 * ]|
642 * allows calling g_object_notify_by_pspec() to notify of property changes:
644 * |[<!-- language="C" -->
645 * void
646 * my_object_set_foo (MyObject *self, gint foo)
648 * if (self->foo != foo)
650 * self->foo = foo;
651 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
654 * ]|
656 * Since: 2.26
658 void
659 g_object_class_install_properties (GObjectClass *oclass,
660 guint n_pspecs,
661 GParamSpec **pspecs)
663 GType oclass_type, parent_type;
664 gint i;
666 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
667 g_return_if_fail (n_pspecs > 1);
668 g_return_if_fail (pspecs[0] == NULL);
670 if (CLASS_HAS_DERIVED_CLASS (oclass))
671 g_error ("Attempt to add properties to %s after it was derived",
672 G_OBJECT_CLASS_NAME (oclass));
674 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
675 parent_type = g_type_parent (oclass_type);
677 /* we skip the first element of the array as it would have a 0 prop_id */
678 for (i = 1; i < n_pspecs; i++)
680 GParamSpec *pspec = pspecs[i];
682 g_return_if_fail (pspec != NULL);
684 if (pspec->flags & G_PARAM_WRITABLE)
685 g_return_if_fail (oclass->set_property != NULL);
686 if (pspec->flags & G_PARAM_READABLE)
687 g_return_if_fail (oclass->get_property != NULL);
688 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
689 if (pspec->flags & G_PARAM_CONSTRUCT)
690 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
691 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
692 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
694 oclass->flags |= CLASS_HAS_PROPS_FLAG;
695 install_property_internal (oclass_type, i, pspec);
697 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
698 oclass->construct_properties = g_slist_append (oclass->construct_properties, pspec);
700 /* for property overrides of construct properties, we have to get rid
701 * of the overidden inherited construct property
703 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
704 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
705 oclass->construct_properties = g_slist_remove (oclass->construct_properties, pspec);
710 * g_object_interface_install_property:
711 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
712 * interface, or the default
713 * vtable for the interface.
714 * @pspec: the #GParamSpec for the new property
716 * Add a property to an interface; this is only useful for interfaces
717 * that are added to GObject-derived types. Adding a property to an
718 * interface forces all objects classes with that interface to have a
719 * compatible property. The compatible property could be a newly
720 * created #GParamSpec, but normally
721 * g_object_class_override_property() will be used so that the object
722 * class only needs to provide an implementation and inherits the
723 * property description, default value, bounds, and so forth from the
724 * interface property.
726 * This function is meant to be called from the interface's default
727 * vtable initialization function (the @class_init member of
728 * #GTypeInfo.) It must not be called after after @class_init has
729 * been called for any object types implementing this interface.
731 * Since: 2.4
733 void
734 g_object_interface_install_property (gpointer g_iface,
735 GParamSpec *pspec)
737 GTypeInterface *iface_class = g_iface;
739 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
740 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
741 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
742 g_return_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0); /* paranoid */
744 g_return_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE));
745 if (pspec->flags & G_PARAM_CONSTRUCT)
746 g_return_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
747 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
748 g_return_if_fail (pspec->flags & G_PARAM_WRITABLE);
750 install_property_internal (iface_class->g_type, 0, pspec);
754 * g_object_class_find_property:
755 * @oclass: a #GObjectClass
756 * @property_name: the name of the property to look up
758 * Looks up the #GParamSpec for a property of a class.
760 * Returns: (transfer none): the #GParamSpec for the property, or
761 * %NULL if the class doesn't have a property of that name
763 GParamSpec*
764 g_object_class_find_property (GObjectClass *class,
765 const gchar *property_name)
767 GParamSpec *pspec;
768 GParamSpec *redirect;
770 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
771 g_return_val_if_fail (property_name != NULL, NULL);
773 pspec = g_param_spec_pool_lookup (pspec_pool,
774 property_name,
775 G_OBJECT_CLASS_TYPE (class),
776 TRUE);
777 if (pspec)
779 redirect = g_param_spec_get_redirect_target (pspec);
780 if (redirect)
781 return redirect;
782 else
783 return pspec;
785 else
786 return NULL;
790 * g_object_interface_find_property:
791 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
792 * interface, or the default vtable for the interface
793 * @property_name: name of a property to lookup.
795 * Find the #GParamSpec with the given name for an
796 * interface. Generally, the interface vtable passed in as @g_iface
797 * will be the default vtable from g_type_default_interface_ref(), or,
798 * if you know the interface has already been loaded,
799 * g_type_default_interface_peek().
801 * Since: 2.4
803 * Returns: (transfer none): the #GParamSpec for the property of the
804 * interface with the name @property_name, or %NULL if no
805 * such property exists.
807 GParamSpec*
808 g_object_interface_find_property (gpointer g_iface,
809 const gchar *property_name)
811 GTypeInterface *iface_class = g_iface;
813 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
814 g_return_val_if_fail (property_name != NULL, NULL);
816 return g_param_spec_pool_lookup (pspec_pool,
817 property_name,
818 iface_class->g_type,
819 FALSE);
823 * g_object_class_override_property:
824 * @oclass: a #GObjectClass
825 * @property_id: the new property ID
826 * @name: the name of a property registered in a parent class or
827 * in an interface of this class.
829 * Registers @property_id as referring to a property with the name
830 * @name in a parent class or in an interface implemented by @oclass.
831 * This allows this class to "override" a property implementation in
832 * a parent class or to provide the implementation of a property from
833 * an interface.
835 * Internally, overriding is implemented by creating a property of type
836 * #GParamSpecOverride; generally operations that query the properties of
837 * the object class, such as g_object_class_find_property() or
838 * g_object_class_list_properties() will return the overridden
839 * property. However, in one case, the @construct_properties argument of
840 * the @constructor virtual function, the #GParamSpecOverride is passed
841 * instead, so that the @param_id field of the #GParamSpec will be
842 * correct. For virtually all uses, this makes no difference. If you
843 * need to get the overridden property, you can call
844 * g_param_spec_get_redirect_target().
846 * Since: 2.4
848 void
849 g_object_class_override_property (GObjectClass *oclass,
850 guint property_id,
851 const gchar *name)
853 GParamSpec *overridden = NULL;
854 GParamSpec *new;
855 GType parent_type;
857 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
858 g_return_if_fail (property_id > 0);
859 g_return_if_fail (name != NULL);
861 /* Find the overridden property; first check parent types
863 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
864 if (parent_type != G_TYPE_NONE)
865 overridden = g_param_spec_pool_lookup (pspec_pool,
866 name,
867 parent_type,
868 TRUE);
869 if (!overridden)
871 GType *ifaces;
872 guint n_ifaces;
874 /* Now check interfaces
876 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
877 while (n_ifaces-- && !overridden)
879 overridden = g_param_spec_pool_lookup (pspec_pool,
880 name,
881 ifaces[n_ifaces],
882 FALSE);
885 g_free (ifaces);
888 if (!overridden)
890 g_warning ("%s: Can't find property to override for '%s::%s'",
891 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
892 return;
895 new = g_param_spec_override (name, overridden);
896 g_object_class_install_property (oclass, property_id, new);
900 * g_object_class_list_properties:
901 * @oclass: a #GObjectClass
902 * @n_properties: (out): return location for the length of the returned array
904 * Get an array of #GParamSpec* for all properties of a class.
906 * Returns: (array length=n_properties) (transfer container): an array of
907 * #GParamSpec* which should be freed after use
909 GParamSpec** /* free result */
910 g_object_class_list_properties (GObjectClass *class,
911 guint *n_properties_p)
913 GParamSpec **pspecs;
914 guint n;
916 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
918 pspecs = g_param_spec_pool_list (pspec_pool,
919 G_OBJECT_CLASS_TYPE (class),
920 &n);
921 if (n_properties_p)
922 *n_properties_p = n;
924 return pspecs;
928 * g_object_interface_list_properties:
929 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
930 * interface, or the default vtable for the interface
931 * @n_properties_p: (out): location to store number of properties returned.
933 * Lists the properties of an interface.Generally, the interface
934 * vtable passed in as @g_iface will be the default vtable from
935 * g_type_default_interface_ref(), or, if you know the interface has
936 * already been loaded, g_type_default_interface_peek().
938 * Since: 2.4
940 * Returns: (array length=n_properties_p) (transfer container): a
941 * pointer to an array of pointers to #GParamSpec
942 * structures. The paramspecs are owned by GLib, but the
943 * array should be freed with g_free() when you are done with
944 * it.
946 GParamSpec**
947 g_object_interface_list_properties (gpointer g_iface,
948 guint *n_properties_p)
950 GTypeInterface *iface_class = g_iface;
951 GParamSpec **pspecs;
952 guint n;
954 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
956 pspecs = g_param_spec_pool_list (pspec_pool,
957 iface_class->g_type,
958 &n);
959 if (n_properties_p)
960 *n_properties_p = n;
962 return pspecs;
965 static inline gboolean
966 object_in_construction (GObject *object)
968 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
971 static void
972 g_object_init (GObject *object,
973 GObjectClass *class)
975 object->ref_count = 1;
976 object->qdata = NULL;
978 if (CLASS_HAS_PROPS (class))
980 /* freeze object's notification queue, g_object_newv() preserves pairedness */
981 g_object_notify_queue_freeze (object, FALSE);
984 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
986 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
987 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
990 GOBJECT_IF_DEBUG (OBJECTS,
992 G_LOCK (debug_objects);
993 debug_objects_count++;
994 g_hash_table_insert (debug_objects_ht, object, object);
995 G_UNLOCK (debug_objects);
999 static void
1000 g_object_do_set_property (GObject *object,
1001 guint property_id,
1002 const GValue *value,
1003 GParamSpec *pspec)
1005 switch (property_id)
1007 default:
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1009 break;
1013 static void
1014 g_object_do_get_property (GObject *object,
1015 guint property_id,
1016 GValue *value,
1017 GParamSpec *pspec)
1019 switch (property_id)
1021 default:
1022 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1023 break;
1027 static void
1028 g_object_real_dispose (GObject *object)
1030 g_signal_handlers_destroy (object);
1031 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1032 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1035 static void
1036 g_object_finalize (GObject *object)
1038 if (object_in_construction (object))
1040 g_critical ("object %s %p finalized while still in-construction",
1041 G_OBJECT_TYPE_NAME (object), object);
1044 g_datalist_clear (&object->qdata);
1046 GOBJECT_IF_DEBUG (OBJECTS,
1048 G_LOCK (debug_objects);
1049 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1050 g_hash_table_remove (debug_objects_ht, object);
1051 debug_objects_count--;
1052 G_UNLOCK (debug_objects);
1056 static void
1057 g_object_dispatch_properties_changed (GObject *object,
1058 guint n_pspecs,
1059 GParamSpec **pspecs)
1061 guint i;
1063 for (i = 0; i < n_pspecs; i++)
1064 g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1068 * g_object_run_dispose:
1069 * @object: a #GObject
1071 * Releases all references to other objects. This can be used to break
1072 * reference cycles.
1074 * This function should only be called from object system implementations.
1076 void
1077 g_object_run_dispose (GObject *object)
1079 g_return_if_fail (G_IS_OBJECT (object));
1080 g_return_if_fail (object->ref_count > 0);
1082 g_object_ref (object);
1083 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1084 G_OBJECT_GET_CLASS (object)->dispose (object);
1085 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1086 g_object_unref (object);
1090 * g_object_freeze_notify:
1091 * @object: a #GObject
1093 * Increases the freeze count on @object. If the freeze count is
1094 * non-zero, the emission of "notify" signals on @object is
1095 * stopped. The signals are queued until the freeze count is decreased
1096 * to zero. Duplicate notifications are squashed so that at most one
1097 * #GObject::notify signal is emitted for each property modified while the
1098 * object is frozen.
1100 * This is necessary for accessors that modify multiple properties to prevent
1101 * premature notification while the object is still being modified.
1103 void
1104 g_object_freeze_notify (GObject *object)
1106 g_return_if_fail (G_IS_OBJECT (object));
1108 if (g_atomic_int_get (&object->ref_count) == 0)
1109 return;
1111 g_object_ref (object);
1112 g_object_notify_queue_freeze (object, FALSE);
1113 g_object_unref (object);
1116 static GParamSpec *
1117 get_notify_pspec (GParamSpec *pspec)
1119 GParamSpec *redirected;
1121 /* we don't notify on non-READABLE parameters */
1122 if (~pspec->flags & G_PARAM_READABLE)
1123 return NULL;
1125 /* if the paramspec is redirected, notify on the target */
1126 redirected = g_param_spec_get_redirect_target (pspec);
1127 if (redirected != NULL)
1128 return redirected;
1130 /* else, notify normally */
1131 return pspec;
1134 static inline void
1135 g_object_notify_by_spec_internal (GObject *object,
1136 GParamSpec *pspec)
1138 GParamSpec *notify_pspec;
1140 notify_pspec = get_notify_pspec (pspec);
1142 if (notify_pspec != NULL)
1144 GObjectNotifyQueue *nqueue;
1146 /* conditional freeze: only increase freeze count if already frozen */
1147 nqueue = g_object_notify_queue_freeze (object, TRUE);
1149 if (nqueue != NULL)
1151 /* we're frozen, so add to the queue and release our freeze */
1152 g_object_notify_queue_add (object, nqueue, notify_pspec);
1153 g_object_notify_queue_thaw (object, nqueue);
1155 else
1156 /* not frozen, so just dispatch the notification directly */
1157 G_OBJECT_GET_CLASS (object)
1158 ->dispatch_properties_changed (object, 1, &notify_pspec);
1163 * g_object_notify:
1164 * @object: a #GObject
1165 * @property_name: the name of a property installed on the class of @object.
1167 * Emits a "notify" signal for the property @property_name on @object.
1169 * When possible, eg. when signaling a property change from within the class
1170 * that registered the property, you should use g_object_notify_by_pspec()
1171 * instead.
1173 * Note that emission of the notify signal may be blocked with
1174 * g_object_freeze_notify(). In this case, the signal emissions are queued
1175 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1176 * called.
1178 void
1179 g_object_notify (GObject *object,
1180 const gchar *property_name)
1182 GParamSpec *pspec;
1184 g_return_if_fail (G_IS_OBJECT (object));
1185 g_return_if_fail (property_name != NULL);
1186 if (g_atomic_int_get (&object->ref_count) == 0)
1187 return;
1189 g_object_ref (object);
1190 /* We don't need to get the redirect target
1191 * (by, e.g. calling g_object_class_find_property())
1192 * because g_object_notify_queue_add() does that
1194 pspec = g_param_spec_pool_lookup (pspec_pool,
1195 property_name,
1196 G_OBJECT_TYPE (object),
1197 TRUE);
1199 if (!pspec)
1200 g_warning ("%s: object class '%s' has no property named '%s'",
1201 G_STRFUNC,
1202 G_OBJECT_TYPE_NAME (object),
1203 property_name);
1204 else
1205 g_object_notify_by_spec_internal (object, pspec);
1206 g_object_unref (object);
1210 * g_object_notify_by_pspec:
1211 * @object: a #GObject
1212 * @pspec: the #GParamSpec of a property installed on the class of @object.
1214 * Emits a "notify" signal for the property specified by @pspec on @object.
1216 * This function omits the property name lookup, hence it is faster than
1217 * g_object_notify().
1219 * One way to avoid using g_object_notify() from within the
1220 * class that registered the properties, and using g_object_notify_by_pspec()
1221 * instead, is to store the GParamSpec used with
1222 * g_object_class_install_property() inside a static array, e.g.:
1224 *|[<!-- language="C" -->
1225 * enum
1227 * PROP_0,
1228 * PROP_FOO,
1229 * PROP_LAST
1230 * };
1232 * static GParamSpec *properties[PROP_LAST];
1234 * static void
1235 * my_object_class_init (MyObjectClass *klass)
1237 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1238 * 0, 100,
1239 * 50,
1240 * G_PARAM_READWRITE);
1241 * g_object_class_install_property (gobject_class,
1242 * PROP_FOO,
1243 * properties[PROP_FOO]);
1245 * ]|
1247 * and then notify a change on the "foo" property with:
1249 * |[<!-- language="C" -->
1250 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1251 * ]|
1253 * Since: 2.26
1255 void
1256 g_object_notify_by_pspec (GObject *object,
1257 GParamSpec *pspec)
1260 g_return_if_fail (G_IS_OBJECT (object));
1261 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1263 if (g_atomic_int_get (&object->ref_count) == 0)
1264 return;
1266 g_object_ref (object);
1267 g_object_notify_by_spec_internal (object, pspec);
1268 g_object_unref (object);
1272 * g_object_thaw_notify:
1273 * @object: a #GObject
1275 * Reverts the effect of a previous call to
1276 * g_object_freeze_notify(). The freeze count is decreased on @object
1277 * and when it reaches zero, queued "notify" signals are emitted.
1279 * Duplicate notifications for each property are squashed so that at most one
1280 * #GObject::notify signal is emitted for each property, in the reverse order
1281 * in which they have been queued.
1283 * It is an error to call this function when the freeze count is zero.
1285 void
1286 g_object_thaw_notify (GObject *object)
1288 GObjectNotifyQueue *nqueue;
1290 g_return_if_fail (G_IS_OBJECT (object));
1291 if (g_atomic_int_get (&object->ref_count) == 0)
1292 return;
1294 g_object_ref (object);
1296 /* FIXME: Freezing is the only way to get at the notify queue.
1297 * So we freeze once and then thaw twice.
1299 nqueue = g_object_notify_queue_freeze (object, FALSE);
1300 g_object_notify_queue_thaw (object, nqueue);
1301 g_object_notify_queue_thaw (object, nqueue);
1303 g_object_unref (object);
1306 static void
1307 consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
1309 static GHashTable *already_warned_table;
1310 static const gchar *enable_diagnostic;
1311 static GMutex already_warned_lock;
1312 gboolean already;
1314 if (!(pspec->flags & G_PARAM_DEPRECATED))
1315 return;
1317 if (g_once_init_enter (&enable_diagnostic))
1319 const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1321 if (!value)
1322 value = "0";
1324 g_once_init_leave (&enable_diagnostic, value);
1327 if (enable_diagnostic[0] == '0')
1328 return;
1330 /* We hash only on property names: this means that we could end up in
1331 * a situation where we fail to emit a warning about a pair of
1332 * same-named deprecated properties used on two separate types.
1333 * That's pretty unlikely to occur, and even if it does, you'll still
1334 * have seen the warning for the first one...
1336 * Doing it this way lets us hash directly on the (interned) property
1337 * name pointers.
1339 g_mutex_lock (&already_warned_lock);
1341 if (already_warned_table == NULL)
1342 already_warned_table = g_hash_table_new (NULL, NULL);
1344 already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
1345 if (!already)
1346 g_hash_table_add (already_warned_table, (gpointer) pspec->name);
1348 g_mutex_unlock (&already_warned_lock);
1350 if (!already)
1351 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1352 "anymore. It will be removed in a future version.",
1353 g_type_name (pspec->owner_type), pspec->name);
1356 static inline void
1357 object_get_property (GObject *object,
1358 GParamSpec *pspec,
1359 GValue *value)
1361 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1362 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1363 GParamSpec *redirect;
1365 if (class == NULL)
1367 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1368 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1369 return;
1372 redirect = g_param_spec_get_redirect_target (pspec);
1373 if (redirect)
1374 pspec = redirect;
1376 consider_issuing_property_deprecation_warning (pspec);
1378 class->get_property (object, param_id, value, pspec);
1381 static inline void
1382 object_set_property (GObject *object,
1383 GParamSpec *pspec,
1384 const GValue *value,
1385 GObjectNotifyQueue *nqueue)
1387 GValue tmp_value = G_VALUE_INIT;
1388 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1389 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1390 GParamSpec *redirect;
1392 if (class == NULL)
1394 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1395 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1396 return;
1399 redirect = g_param_spec_get_redirect_target (pspec);
1400 if (redirect)
1401 pspec = redirect;
1403 /* provide a copy to work from, convert (if necessary) and validate */
1404 g_value_init (&tmp_value, pspec->value_type);
1405 if (!g_value_transform (value, &tmp_value))
1406 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1407 pspec->name,
1408 g_type_name (pspec->value_type),
1409 G_VALUE_TYPE_NAME (value));
1410 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1412 gchar *contents = g_strdup_value_contents (value);
1414 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1415 contents,
1416 G_VALUE_TYPE_NAME (value),
1417 pspec->name,
1418 g_type_name (pspec->value_type));
1419 g_free (contents);
1421 else
1423 class->set_property (object, param_id, &tmp_value, pspec);
1425 if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY)
1427 GParamSpec *notify_pspec;
1429 notify_pspec = get_notify_pspec (pspec);
1431 if (notify_pspec != NULL)
1432 g_object_notify_queue_add (object, nqueue, notify_pspec);
1435 g_value_unset (&tmp_value);
1438 static void
1439 object_interface_check_properties (gpointer check_data,
1440 gpointer g_iface)
1442 GTypeInterface *iface_class = g_iface;
1443 GObjectClass *class;
1444 GType iface_type = iface_class->g_type;
1445 GParamSpec **pspecs;
1446 guint n;
1448 class = g_type_class_ref (iface_class->g_instance_type);
1450 if (class == NULL)
1451 return;
1453 if (!G_IS_OBJECT_CLASS (class))
1454 goto out;
1456 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1458 while (n--)
1460 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1461 pspecs[n]->name,
1462 G_OBJECT_CLASS_TYPE (class),
1463 TRUE);
1465 if (!class_pspec)
1467 g_critical ("Object class %s doesn't implement property "
1468 "'%s' from interface '%s'",
1469 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1470 pspecs[n]->name,
1471 g_type_name (iface_type));
1473 continue;
1476 /* We do a number of checks on the properties of an interface to
1477 * make sure that all classes implementing the interface are
1478 * overriding the properties in a sane way.
1480 * We do the checks in order of importance so that we can give
1481 * more useful error messages first.
1483 * First, we check that the implementation doesn't remove the
1484 * basic functionality (readability, writability) advertised by
1485 * the interface. Next, we check that it doesn't introduce
1486 * additional restrictions (such as construct-only). Finally, we
1487 * make sure the types are compatible.
1490 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1491 /* If the property on the interface is readable then the
1492 * implementation must be readable. If the interface is writable
1493 * then the implementation must be writable.
1495 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1497 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1498 "property on interface '%s'\n", pspecs[n]->name,
1499 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1500 continue;
1503 /* If the property on the interface is writable then we need to
1504 * make sure the implementation doesn't introduce new restrictions
1505 * on that writability (ie: construct-only).
1507 * If the interface was not writable to begin with then we don't
1508 * really have any problems here because "writable at construct
1509 * time only" is still more permissive than "read only".
1511 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1513 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1515 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1516 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1517 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1518 continue;
1521 #undef SUBSET
1523 /* If the property on the interface is readable then we are
1524 * effectively advertising that reading the property will return a
1525 * value of a specific type. All implementations of the interface
1526 * need to return items of this type -- but may be more
1527 * restrictive. For example, it is legal to have:
1529 * GtkWidget *get_item();
1531 * that is implemented by a function that always returns a
1532 * GtkEntry. In short: readability implies that the
1533 * implementation value type must be equal or more restrictive.
1535 * Similarly, if the property on the interface is writable then
1536 * must be able to accept the property being set to any value of
1537 * that type, including subclasses. In this case, we may also be
1538 * less restrictive. For example, it is legal to have:
1540 * set_item (GtkEntry *);
1542 * that is implemented by a function that will actually work with
1543 * any GtkWidget. In short: writability implies that the
1544 * implementation value type must be equal or less restrictive.
1546 * In the case that the property is both readable and writable
1547 * then the only way that both of the above can be satisfied is
1548 * with a type that is exactly equal.
1550 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1552 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1553 /* class pspec value type must have exact equality with interface */
1554 if (pspecs[n]->value_type != class_pspec->value_type)
1555 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1556 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1557 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1558 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1559 break;
1561 case G_PARAM_READABLE:
1562 /* class pspec value type equal or more restrictive than interface */
1563 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1564 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1565 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1566 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1567 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1568 break;
1570 case G_PARAM_WRITABLE:
1571 /* class pspec value type equal or less restrictive than interface */
1572 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1573 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1574 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1575 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1576 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1577 break;
1579 default:
1580 g_assert_not_reached ();
1584 g_free (pspecs);
1586 out:
1587 g_type_class_unref (class);
1590 GType
1591 g_object_get_type (void)
1593 return G_TYPE_OBJECT;
1597 * g_object_new: (skip)
1598 * @object_type: the type id of the #GObject subtype to instantiate
1599 * @first_property_name: the name of the first property
1600 * @...: the value of the first property, followed optionally by more
1601 * name/value pairs, followed by %NULL
1603 * Creates a new instance of a #GObject subtype and sets its properties.
1605 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1606 * which are not explicitly specified are set to their default values.
1608 * Returns: (transfer full) (type GObject.Object) : a new instance of
1609 * @object_type
1611 gpointer
1612 g_object_new (GType object_type,
1613 const gchar *first_property_name,
1614 ...)
1616 GObject *object;
1617 va_list var_args;
1619 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1621 /* short circuit for calls supplying no properties */
1622 if (!first_property_name)
1623 return g_object_newv (object_type, 0, NULL);
1625 va_start (var_args, first_property_name);
1626 object = g_object_new_valist (object_type, first_property_name, var_args);
1627 va_end (var_args);
1629 return object;
1632 static gpointer
1633 g_object_new_with_custom_constructor (GObjectClass *class,
1634 GObjectConstructParam *params,
1635 guint n_params)
1637 GObjectNotifyQueue *nqueue = NULL;
1638 gboolean newly_constructed;
1639 GObjectConstructParam *cparams;
1640 GObject *object;
1641 GValue *cvalues;
1642 gint n_cparams;
1643 gint cvals_used;
1644 GSList *node;
1645 gint i;
1647 /* If we have ->constructed() then we have to do a lot more work.
1648 * It's possible that this is a singleton and it's also possible
1649 * that the user's constructor() will attempt to modify the values
1650 * that we pass in, so we'll need to allocate copies of them.
1651 * It's also possible that the user may attempt to call
1652 * g_object_set() from inside of their constructor, so we need to
1653 * add ourselves to a list of objects for which that is allowed
1654 * while their constructor() is running.
1657 /* Create the array of GObjectConstructParams for constructor() */
1658 n_cparams = g_slist_length (class->construct_properties);
1659 cparams = g_new (GObjectConstructParam, n_cparams);
1660 cvalues = g_new0 (GValue, n_cparams);
1661 cvals_used = 0;
1662 i = 0;
1664 /* As above, we may find the value in the passed-in params list.
1666 * If we have the value passed in then we can use the GValue from
1667 * it directly because it is safe to modify. If we use the
1668 * default value from the class, we had better not pass that in
1669 * and risk it being modified, so we create a new one.
1670 * */
1671 for (node = class->construct_properties; node; node = node->next)
1673 GParamSpec *pspec;
1674 GValue *value;
1675 gint j;
1677 pspec = node->data;
1678 value = NULL; /* to silence gcc... */
1680 for (j = 0; j < n_params; j++)
1681 if (params[j].pspec == pspec)
1683 consider_issuing_property_deprecation_warning (pspec);
1684 value = params[j].value;
1685 break;
1688 if (j == n_params)
1690 value = &cvalues[cvals_used++];
1691 g_value_init (value, pspec->value_type);
1692 g_param_value_set_default (pspec, value);
1695 cparams[i].pspec = pspec;
1696 cparams[i].value = value;
1697 i++;
1700 /* construct object from construction parameters */
1701 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1702 /* free construction values */
1703 g_free (cparams);
1704 while (cvals_used--)
1705 g_value_unset (&cvalues[cvals_used]);
1706 g_free (cvalues);
1708 /* There is code in the wild that relies on being able to return NULL
1709 * from its custom constructor. This was never a supported operation,
1710 * but since the code is already out there...
1712 if (object == NULL)
1714 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1715 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1716 return NULL;
1719 /* g_object_init() will have marked the object as being in-construction.
1720 * Check if the returned object still is so marked, or if this is an
1721 * already-existing singleton (in which case we should not do 'constructed').
1723 newly_constructed = object_in_construction (object);
1724 if (newly_constructed)
1725 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1727 if (CLASS_HAS_PROPS (class))
1729 /* If this object was newly_constructed then g_object_init()
1730 * froze the queue. We need to freeze it here in order to get
1731 * the handle so that we can thaw it below (otherwise it will
1732 * be frozen forever).
1734 * We also want to do a freeze if we have any params to set,
1735 * even on a non-newly_constructed object.
1737 * It's possible that we have the case of non-newly created
1738 * singleton and all of the passed-in params were construct
1739 * properties so n_params > 0 but we will actually set no
1740 * properties. This is a pretty lame case to optimise, so
1741 * just ignore it and freeze anyway.
1743 if (newly_constructed || n_params)
1744 nqueue = g_object_notify_queue_freeze (object, FALSE);
1746 /* Remember: if it was newly_constructed then g_object_init()
1747 * already did a freeze, so we now have two. Release one.
1749 if (newly_constructed)
1750 g_object_notify_queue_thaw (object, nqueue);
1753 /* run 'constructed' handler if there is a custom one */
1754 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1755 class->constructed (object);
1757 /* set remaining properties */
1758 for (i = 0; i < n_params; i++)
1759 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1761 consider_issuing_property_deprecation_warning (params[i].pspec);
1762 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1765 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1766 if (nqueue)
1767 g_object_notify_queue_thaw (object, nqueue);
1769 return object;
1772 static gpointer
1773 g_object_new_internal (GObjectClass *class,
1774 GObjectConstructParam *params,
1775 guint n_params)
1777 GObjectNotifyQueue *nqueue = NULL;
1778 GObject *object;
1780 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1781 return g_object_new_with_custom_constructor (class, params, n_params);
1783 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1785 if (CLASS_HAS_PROPS (class))
1787 GSList *node;
1789 /* This will have been setup in g_object_init() */
1790 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1791 g_assert (nqueue != NULL);
1793 /* We will set exactly n_construct_properties construct
1794 * properties, but they may come from either the class default
1795 * values or the passed-in parameter list.
1797 for (node = class->construct_properties; node; node = node->next)
1799 const GValue *value;
1800 GParamSpec *pspec;
1801 gint j;
1803 pspec = node->data;
1804 value = NULL; /* to silence gcc... */
1806 for (j = 0; j < n_params; j++)
1807 if (params[j].pspec == pspec)
1809 consider_issuing_property_deprecation_warning (pspec);
1810 value = params[j].value;
1811 break;
1814 if (j == n_params)
1815 value = g_param_spec_get_default_value (pspec);
1817 object_set_property (object, pspec, value, nqueue);
1821 /* run 'constructed' handler if there is a custom one */
1822 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1823 class->constructed (object);
1825 if (nqueue)
1827 gint i;
1829 /* Set remaining properties. The construct properties will
1830 * already have been taken, so set only the non-construct
1831 * ones.
1833 for (i = 0; i < n_params; i++)
1834 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1836 consider_issuing_property_deprecation_warning (params[i].pspec);
1837 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1840 g_object_notify_queue_thaw (object, nqueue);
1843 return object;
1847 * g_object_newv: (rename-to g_object_new)
1848 * @object_type: the type id of the #GObject subtype to instantiate
1849 * @n_parameters: the length of the @parameters array
1850 * @parameters: (array length=n_parameters): an array of #GParameter
1852 * Creates a new instance of a #GObject subtype and sets its properties.
1854 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1855 * which are not explicitly specified are set to their default values.
1857 * Returns: (type GObject.Object) (transfer full): a new instance of
1858 * @object_type
1860 gpointer
1861 g_object_newv (GType object_type,
1862 guint n_parameters,
1863 GParameter *parameters)
1865 GObjectClass *class, *unref_class = NULL;
1866 GObject *object;
1868 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1869 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1871 /* Try to avoid thrashing the ref_count if we don't need to (since
1872 * it's a locked operation).
1874 class = g_type_class_peek_static (object_type);
1876 if (!class)
1877 class = unref_class = g_type_class_ref (object_type);
1879 if (n_parameters)
1881 GObjectConstructParam *cparams;
1882 guint i, j;
1884 cparams = g_newa (GObjectConstructParam, n_parameters);
1885 j = 0;
1887 for (i = 0; i < n_parameters; i++)
1889 GParamSpec *pspec;
1890 gint k;
1892 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
1894 if G_UNLIKELY (!pspec)
1896 g_critical ("%s: object class '%s' has no property named '%s'",
1897 G_STRFUNC, g_type_name (object_type), parameters[i].name);
1898 continue;
1901 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1903 g_critical ("%s: property '%s' of object class '%s' is not writable",
1904 G_STRFUNC, pspec->name, g_type_name (object_type));
1905 continue;
1908 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
1910 for (k = 0; k < j; k++)
1911 if (cparams[k].pspec == pspec)
1912 break;
1913 if G_UNLIKELY (k != j)
1915 g_critical ("%s: construct property '%s' for type '%s' cannot be set twice",
1916 G_STRFUNC, parameters[i].name, g_type_name (object_type));
1917 continue;
1921 cparams[j].pspec = pspec;
1922 cparams[j].value = &parameters[i].value;
1923 j++;
1926 object = g_object_new_internal (class, cparams, j);
1928 else
1929 /* Fast case: no properties passed in. */
1930 object = g_object_new_internal (class, NULL, 0);
1932 if (unref_class)
1933 g_type_class_unref (unref_class);
1935 return object;
1939 * g_object_new_valist: (skip)
1940 * @object_type: the type id of the #GObject subtype to instantiate
1941 * @first_property_name: the name of the first property
1942 * @var_args: the value of the first property, followed optionally by more
1943 * name/value pairs, followed by %NULL
1945 * Creates a new instance of a #GObject subtype and sets its properties.
1947 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1948 * which are not explicitly specified are set to their default values.
1950 * Returns: a new instance of @object_type
1952 GObject*
1953 g_object_new_valist (GType object_type,
1954 const gchar *first_property_name,
1955 va_list var_args)
1957 GObjectClass *class, *unref_class = NULL;
1958 GObject *object;
1960 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1962 /* Try to avoid thrashing the ref_count if we don't need to (since
1963 * it's a locked operation).
1965 class = g_type_class_peek_static (object_type);
1967 if (!class)
1968 class = unref_class = g_type_class_ref (object_type);
1970 if (first_property_name)
1972 GObjectConstructParam stack_params[16];
1973 GObjectConstructParam *params;
1974 const gchar *name;
1975 gint n_params = 0;
1977 name = first_property_name;
1978 params = stack_params;
1982 gchar *error = NULL;
1983 GParamSpec *pspec;
1984 gint i;
1986 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
1988 if G_UNLIKELY (!pspec)
1990 g_critical ("%s: object class '%s' has no property named '%s'",
1991 G_STRFUNC, g_type_name (object_type), name);
1992 /* Can't continue because arg list will be out of sync. */
1993 break;
1996 if G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE)
1998 g_critical ("%s: property '%s' of object class '%s' is not writable",
1999 G_STRFUNC, pspec->name, g_type_name (object_type));
2000 break;
2003 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
2005 for (i = 0; i < n_params; i++)
2006 if (params[i].pspec == pspec)
2007 break;
2008 if G_UNLIKELY (i != n_params)
2010 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
2011 G_STRFUNC, name, g_type_name (object_type));
2012 break;
2016 if (n_params == 16)
2018 params = g_new (GObjectConstructParam, n_params + 1);
2019 memcpy (params, stack_params, sizeof stack_params);
2021 else if (n_params > 16)
2022 params = g_renew (GObjectConstructParam, params, n_params + 1);
2024 params[n_params].pspec = pspec;
2025 params[n_params].value = g_newa (GValue, 1);
2026 memset (params[n_params].value, 0, sizeof (GValue));
2028 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
2030 if (error)
2032 g_critical ("%s: %s", G_STRFUNC, error);
2033 g_value_unset (params[n_params].value);
2034 g_free (error);
2035 break;
2038 n_params++;
2040 while ((name = va_arg (var_args, const gchar *)));
2042 object = g_object_new_internal (class, params, n_params);
2044 while (n_params--)
2045 g_value_unset (params[n_params].value);
2047 if (params != stack_params)
2048 g_free (params);
2050 else
2051 /* Fast case: no properties passed in. */
2052 object = g_object_new_internal (class, NULL, 0);
2054 if (unref_class)
2055 g_type_class_unref (unref_class);
2057 return object;
2060 static GObject*
2061 g_object_constructor (GType type,
2062 guint n_construct_properties,
2063 GObjectConstructParam *construct_params)
2065 GObject *object;
2067 /* create object */
2068 object = (GObject*) g_type_create_instance (type);
2070 /* set construction parameters */
2071 if (n_construct_properties)
2073 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2075 /* set construct properties */
2076 while (n_construct_properties--)
2078 GValue *value = construct_params->value;
2079 GParamSpec *pspec = construct_params->pspec;
2081 construct_params++;
2082 object_set_property (object, pspec, value, nqueue);
2084 g_object_notify_queue_thaw (object, nqueue);
2085 /* the notification queue is still frozen from g_object_init(), so
2086 * we don't need to handle it here, g_object_newv() takes
2087 * care of that
2091 return object;
2094 static void
2095 g_object_constructed (GObject *object)
2097 /* empty default impl to allow unconditional upchaining */
2101 * g_object_set_valist: (skip)
2102 * @object: a #GObject
2103 * @first_property_name: name of the first property to set
2104 * @var_args: value for the first property, followed optionally by more
2105 * name/value pairs, followed by %NULL
2107 * Sets properties on an object.
2109 void
2110 g_object_set_valist (GObject *object,
2111 const gchar *first_property_name,
2112 va_list var_args)
2114 GObjectNotifyQueue *nqueue;
2115 const gchar *name;
2117 g_return_if_fail (G_IS_OBJECT (object));
2119 g_object_ref (object);
2120 nqueue = g_object_notify_queue_freeze (object, FALSE);
2122 name = first_property_name;
2123 while (name)
2125 GValue value = G_VALUE_INIT;
2126 GParamSpec *pspec;
2127 gchar *error = NULL;
2129 pspec = g_param_spec_pool_lookup (pspec_pool,
2130 name,
2131 G_OBJECT_TYPE (object),
2132 TRUE);
2133 if (!pspec)
2135 g_warning ("%s: object class '%s' has no property named '%s'",
2136 G_STRFUNC,
2137 G_OBJECT_TYPE_NAME (object),
2138 name);
2139 break;
2141 if (!(pspec->flags & G_PARAM_WRITABLE))
2143 g_warning ("%s: property '%s' of object class '%s' is not writable",
2144 G_STRFUNC,
2145 pspec->name,
2146 G_OBJECT_TYPE_NAME (object));
2147 break;
2149 if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2151 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2152 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2153 break;
2156 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2157 0, &error);
2158 if (error)
2160 g_warning ("%s: %s", G_STRFUNC, error);
2161 g_free (error);
2162 g_value_unset (&value);
2163 break;
2166 consider_issuing_property_deprecation_warning (pspec);
2167 object_set_property (object, pspec, &value, nqueue);
2168 g_value_unset (&value);
2170 name = va_arg (var_args, gchar*);
2173 g_object_notify_queue_thaw (object, nqueue);
2174 g_object_unref (object);
2178 * g_object_get_valist: (skip)
2179 * @object: a #GObject
2180 * @first_property_name: name of the first property to get
2181 * @var_args: return location for the first property, followed optionally by more
2182 * name/return location pairs, followed by %NULL
2184 * Gets properties of an object.
2186 * In general, a copy is made of the property contents and the caller
2187 * is responsible for freeing the memory in the appropriate manner for
2188 * the type, for instance by calling g_free() or g_object_unref().
2190 * See g_object_get().
2192 void
2193 g_object_get_valist (GObject *object,
2194 const gchar *first_property_name,
2195 va_list var_args)
2197 const gchar *name;
2199 g_return_if_fail (G_IS_OBJECT (object));
2201 g_object_ref (object);
2203 name = first_property_name;
2205 while (name)
2207 GValue value = G_VALUE_INIT;
2208 GParamSpec *pspec;
2209 gchar *error;
2211 pspec = g_param_spec_pool_lookup (pspec_pool,
2212 name,
2213 G_OBJECT_TYPE (object),
2214 TRUE);
2215 if (!pspec)
2217 g_warning ("%s: object class '%s' has no property named '%s'",
2218 G_STRFUNC,
2219 G_OBJECT_TYPE_NAME (object),
2220 name);
2221 break;
2223 if (!(pspec->flags & G_PARAM_READABLE))
2225 g_warning ("%s: property '%s' of object class '%s' is not readable",
2226 G_STRFUNC,
2227 pspec->name,
2228 G_OBJECT_TYPE_NAME (object));
2229 break;
2232 g_value_init (&value, pspec->value_type);
2234 object_get_property (object, pspec, &value);
2236 G_VALUE_LCOPY (&value, var_args, 0, &error);
2237 if (error)
2239 g_warning ("%s: %s", G_STRFUNC, error);
2240 g_free (error);
2241 g_value_unset (&value);
2242 break;
2245 g_value_unset (&value);
2247 name = va_arg (var_args, gchar*);
2250 g_object_unref (object);
2254 * g_object_set: (skip)
2255 * @object: (type GObject.Object): a #GObject
2256 * @first_property_name: name of the first property to set
2257 * @...: value for the first property, followed optionally by more
2258 * name/value pairs, followed by %NULL
2260 * Sets properties on an object.
2262 * Note that the "notify" signals are queued and only emitted (in
2263 * reverse order) after all properties have been set. See
2264 * g_object_freeze_notify().
2266 void
2267 g_object_set (gpointer _object,
2268 const gchar *first_property_name,
2269 ...)
2271 GObject *object = _object;
2272 va_list var_args;
2274 g_return_if_fail (G_IS_OBJECT (object));
2276 va_start (var_args, first_property_name);
2277 g_object_set_valist (object, first_property_name, var_args);
2278 va_end (var_args);
2282 * g_object_get: (skip)
2283 * @object: (type GObject.Object): a #GObject
2284 * @first_property_name: name of the first property to get
2285 * @...: return location for the first property, followed optionally by more
2286 * name/return location pairs, followed by %NULL
2288 * Gets properties of an object.
2290 * In general, a copy is made of the property contents and the caller
2291 * is responsible for freeing the memory in the appropriate manner for
2292 * the type, for instance by calling g_free() or g_object_unref().
2294 * Here is an example of using g_object_get() to get the contents
2295 * of three properties: an integer, a string and an object:
2296 * |[<!-- language="C" -->
2297 * gint intval;
2298 * gchar *strval;
2299 * GObject *objval;
2301 * g_object_get (my_object,
2302 * "int-property", &intval,
2303 * "str-property", &strval,
2304 * "obj-property", &objval,
2305 * NULL);
2307 * // Do something with intval, strval, objval
2309 * g_free (strval);
2310 * g_object_unref (objval);
2311 * ]|
2313 void
2314 g_object_get (gpointer _object,
2315 const gchar *first_property_name,
2316 ...)
2318 GObject *object = _object;
2319 va_list var_args;
2321 g_return_if_fail (G_IS_OBJECT (object));
2323 va_start (var_args, first_property_name);
2324 g_object_get_valist (object, first_property_name, var_args);
2325 va_end (var_args);
2329 * g_object_set_property:
2330 * @object: a #GObject
2331 * @property_name: the name of the property to set
2332 * @value: the value
2334 * Sets a property on an object.
2336 void
2337 g_object_set_property (GObject *object,
2338 const gchar *property_name,
2339 const GValue *value)
2341 GObjectNotifyQueue *nqueue;
2342 GParamSpec *pspec;
2344 g_return_if_fail (G_IS_OBJECT (object));
2345 g_return_if_fail (property_name != NULL);
2346 g_return_if_fail (G_IS_VALUE (value));
2348 g_object_ref (object);
2349 nqueue = g_object_notify_queue_freeze (object, FALSE);
2351 pspec = g_param_spec_pool_lookup (pspec_pool,
2352 property_name,
2353 G_OBJECT_TYPE (object),
2354 TRUE);
2355 if (!pspec)
2356 g_warning ("%s: object class '%s' has no property named '%s'",
2357 G_STRFUNC,
2358 G_OBJECT_TYPE_NAME (object),
2359 property_name);
2360 else if (!(pspec->flags & G_PARAM_WRITABLE))
2361 g_warning ("%s: property '%s' of object class '%s' is not writable",
2362 G_STRFUNC,
2363 pspec->name,
2364 G_OBJECT_TYPE_NAME (object));
2365 else if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))
2366 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2367 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2368 else
2370 consider_issuing_property_deprecation_warning (pspec);
2371 object_set_property (object, pspec, value, nqueue);
2374 g_object_notify_queue_thaw (object, nqueue);
2375 g_object_unref (object);
2379 * g_object_get_property:
2380 * @object: a #GObject
2381 * @property_name: the name of the property to get
2382 * @value: return location for the property value
2384 * Gets a property of an object. @value must have been initialized to the
2385 * expected type of the property (or a type to which the expected type can be
2386 * transformed) using g_value_init().
2388 * In general, a copy is made of the property contents and the caller is
2389 * responsible for freeing the memory by calling g_value_unset().
2391 * Note that g_object_get_property() is really intended for language
2392 * bindings, g_object_get() is much more convenient for C programming.
2394 void
2395 g_object_get_property (GObject *object,
2396 const gchar *property_name,
2397 GValue *value)
2399 GParamSpec *pspec;
2401 g_return_if_fail (G_IS_OBJECT (object));
2402 g_return_if_fail (property_name != NULL);
2403 g_return_if_fail (G_IS_VALUE (value));
2405 g_object_ref (object);
2407 pspec = g_param_spec_pool_lookup (pspec_pool,
2408 property_name,
2409 G_OBJECT_TYPE (object),
2410 TRUE);
2411 if (!pspec)
2412 g_warning ("%s: object class '%s' has no property named '%s'",
2413 G_STRFUNC,
2414 G_OBJECT_TYPE_NAME (object),
2415 property_name);
2416 else if (!(pspec->flags & G_PARAM_READABLE))
2417 g_warning ("%s: property '%s' of object class '%s' is not readable",
2418 G_STRFUNC,
2419 pspec->name,
2420 G_OBJECT_TYPE_NAME (object));
2421 else
2423 GValue *prop_value, tmp_value = G_VALUE_INIT;
2425 /* auto-conversion of the callers value type
2427 if (G_VALUE_TYPE (value) == pspec->value_type)
2429 g_value_reset (value);
2430 prop_value = value;
2432 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2434 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2435 G_STRFUNC, pspec->name,
2436 g_type_name (pspec->value_type),
2437 G_VALUE_TYPE_NAME (value));
2438 g_object_unref (object);
2439 return;
2441 else
2443 g_value_init (&tmp_value, pspec->value_type);
2444 prop_value = &tmp_value;
2446 object_get_property (object, pspec, prop_value);
2447 if (prop_value != value)
2449 g_value_transform (prop_value, value);
2450 g_value_unset (&tmp_value);
2454 g_object_unref (object);
2458 * g_object_connect: (skip)
2459 * @object: (type GObject.Object): a #GObject
2460 * @signal_spec: the spec for the first signal
2461 * @...: #GCallback for the first signal, followed by data for the
2462 * first signal, followed optionally by more signal
2463 * spec/callback/data triples, followed by %NULL
2465 * A convenience function to connect multiple signals at once.
2467 * The signal specs expected by this function have the form
2468 * "modifier::signal_name", where modifier can be one of the following:
2469 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2470 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2471 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2472 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2473 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2474 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2475 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2476 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2478 * |[<!-- language="C" -->
2479 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2480 * "type", GTK_WINDOW_POPUP,
2481 * "child", menu,
2482 * NULL),
2483 * "signal::event", gtk_menu_window_event, menu,
2484 * "signal::size_request", gtk_menu_window_size_request, menu,
2485 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2486 * NULL);
2487 * ]|
2489 * Returns: (transfer none) (type GObject.Object): @object
2491 gpointer
2492 g_object_connect (gpointer _object,
2493 const gchar *signal_spec,
2494 ...)
2496 GObject *object = _object;
2497 va_list var_args;
2499 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2500 g_return_val_if_fail (object->ref_count > 0, object);
2502 va_start (var_args, signal_spec);
2503 while (signal_spec)
2505 GCallback callback = va_arg (var_args, GCallback);
2506 gpointer data = va_arg (var_args, gpointer);
2508 if (strncmp (signal_spec, "signal::", 8) == 0)
2509 g_signal_connect_data (object, signal_spec + 8,
2510 callback, data, NULL,
2512 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2513 strncmp (signal_spec, "object-signal::", 15) == 0)
2514 g_signal_connect_object (object, signal_spec + 15,
2515 callback, data,
2517 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2518 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2519 g_signal_connect_data (object, signal_spec + 16,
2520 callback, data, NULL,
2521 G_CONNECT_SWAPPED);
2522 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2523 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2524 g_signal_connect_object (object, signal_spec + 23,
2525 callback, data,
2526 G_CONNECT_SWAPPED);
2527 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2528 strncmp (signal_spec, "signal-after::", 14) == 0)
2529 g_signal_connect_data (object, signal_spec + 14,
2530 callback, data, NULL,
2531 G_CONNECT_AFTER);
2532 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2533 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2534 g_signal_connect_object (object, signal_spec + 21,
2535 callback, data,
2536 G_CONNECT_AFTER);
2537 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2538 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2539 g_signal_connect_data (object, signal_spec + 22,
2540 callback, data, NULL,
2541 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2542 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2543 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2544 g_signal_connect_object (object, signal_spec + 29,
2545 callback, data,
2546 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2547 else
2549 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2550 break;
2552 signal_spec = va_arg (var_args, gchar*);
2554 va_end (var_args);
2556 return object;
2560 * g_object_disconnect: (skip)
2561 * @object: (type GObject.Object): a #GObject
2562 * @signal_spec: the spec for the first signal
2563 * @...: #GCallback for the first signal, followed by data for the first signal,
2564 * followed optionally by more signal spec/callback/data triples,
2565 * followed by %NULL
2567 * A convenience function to disconnect multiple signals at once.
2569 * The signal specs expected by this function have the form
2570 * "any_signal", which means to disconnect any signal with matching
2571 * callback and data, or "any_signal::signal_name", which only
2572 * disconnects the signal named "signal_name".
2574 void
2575 g_object_disconnect (gpointer _object,
2576 const gchar *signal_spec,
2577 ...)
2579 GObject *object = _object;
2580 va_list var_args;
2582 g_return_if_fail (G_IS_OBJECT (object));
2583 g_return_if_fail (object->ref_count > 0);
2585 va_start (var_args, signal_spec);
2586 while (signal_spec)
2588 GCallback callback = va_arg (var_args, GCallback);
2589 gpointer data = va_arg (var_args, gpointer);
2590 guint sid = 0, detail = 0, mask = 0;
2592 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2593 strncmp (signal_spec, "any-signal::", 12) == 0)
2595 signal_spec += 12;
2596 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2598 else if (strcmp (signal_spec, "any_signal") == 0 ||
2599 strcmp (signal_spec, "any-signal") == 0)
2601 signal_spec += 10;
2602 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2604 else
2606 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2607 break;
2610 if ((mask & G_SIGNAL_MATCH_ID) &&
2611 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2612 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2613 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2614 sid, detail,
2615 NULL, (gpointer)callback, data))
2616 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2617 signal_spec = va_arg (var_args, gchar*);
2619 va_end (var_args);
2622 typedef struct {
2623 GObject *object;
2624 guint n_weak_refs;
2625 struct {
2626 GWeakNotify notify;
2627 gpointer data;
2628 } weak_refs[1]; /* flexible array */
2629 } WeakRefStack;
2631 static void
2632 weak_refs_notify (gpointer data)
2634 WeakRefStack *wstack = data;
2635 guint i;
2637 for (i = 0; i < wstack->n_weak_refs; i++)
2638 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2639 g_free (wstack);
2643 * g_object_weak_ref: (skip)
2644 * @object: #GObject to reference weakly
2645 * @notify: callback to invoke before the object is freed
2646 * @data: extra data to pass to notify
2648 * Adds a weak reference callback to an object. Weak references are
2649 * used for notification when an object is finalized. They are called
2650 * "weak references" because they allow you to safely hold a pointer
2651 * to an object without calling g_object_ref() (g_object_ref() adds a
2652 * strong reference, that is, forces the object to stay alive).
2654 * Note that the weak references created by this method are not
2655 * thread-safe: they cannot safely be used in one thread if the
2656 * object's last g_object_unref() might happen in another thread.
2657 * Use #GWeakRef if thread-safety is required.
2659 void
2660 g_object_weak_ref (GObject *object,
2661 GWeakNotify notify,
2662 gpointer data)
2664 WeakRefStack *wstack;
2665 guint i;
2667 g_return_if_fail (G_IS_OBJECT (object));
2668 g_return_if_fail (notify != NULL);
2669 g_return_if_fail (object->ref_count >= 1);
2671 G_LOCK (weak_refs_mutex);
2672 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2673 if (wstack)
2675 i = wstack->n_weak_refs++;
2676 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2678 else
2680 wstack = g_renew (WeakRefStack, NULL, 1);
2681 wstack->object = object;
2682 wstack->n_weak_refs = 1;
2683 i = 0;
2685 wstack->weak_refs[i].notify = notify;
2686 wstack->weak_refs[i].data = data;
2687 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2688 G_UNLOCK (weak_refs_mutex);
2692 * g_object_weak_unref: (skip)
2693 * @object: #GObject to remove a weak reference from
2694 * @notify: callback to search for
2695 * @data: data to search for
2697 * Removes a weak reference callback to an object.
2699 void
2700 g_object_weak_unref (GObject *object,
2701 GWeakNotify notify,
2702 gpointer data)
2704 WeakRefStack *wstack;
2705 gboolean found_one = FALSE;
2707 g_return_if_fail (G_IS_OBJECT (object));
2708 g_return_if_fail (notify != NULL);
2710 G_LOCK (weak_refs_mutex);
2711 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2712 if (wstack)
2714 guint i;
2716 for (i = 0; i < wstack->n_weak_refs; i++)
2717 if (wstack->weak_refs[i].notify == notify &&
2718 wstack->weak_refs[i].data == data)
2720 found_one = TRUE;
2721 wstack->n_weak_refs -= 1;
2722 if (i != wstack->n_weak_refs)
2723 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2725 break;
2728 G_UNLOCK (weak_refs_mutex);
2729 if (!found_one)
2730 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2734 * g_object_add_weak_pointer: (skip)
2735 * @object: The object that should be weak referenced.
2736 * @weak_pointer_location: (inout) (not optional): The memory address
2737 * of a pointer.
2739 * Adds a weak reference from weak_pointer to @object to indicate that
2740 * the pointer located at @weak_pointer_location is only valid during
2741 * the lifetime of @object. When the @object is finalized,
2742 * @weak_pointer will be set to %NULL.
2744 * Note that as with g_object_weak_ref(), the weak references created by
2745 * this method are not thread-safe: they cannot safely be used in one
2746 * thread if the object's last g_object_unref() might happen in another
2747 * thread. Use #GWeakRef if thread-safety is required.
2749 void
2750 g_object_add_weak_pointer (GObject *object,
2751 gpointer *weak_pointer_location)
2753 g_return_if_fail (G_IS_OBJECT (object));
2754 g_return_if_fail (weak_pointer_location != NULL);
2756 g_object_weak_ref (object,
2757 (GWeakNotify) g_nullify_pointer,
2758 weak_pointer_location);
2762 * g_object_remove_weak_pointer: (skip)
2763 * @object: The object that is weak referenced.
2764 * @weak_pointer_location: (inout) (not optional): The memory address
2765 * of a pointer.
2767 * Removes a weak reference from @object that was previously added
2768 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2769 * to match the one used with g_object_add_weak_pointer().
2771 void
2772 g_object_remove_weak_pointer (GObject *object,
2773 gpointer *weak_pointer_location)
2775 g_return_if_fail (G_IS_OBJECT (object));
2776 g_return_if_fail (weak_pointer_location != NULL);
2778 g_object_weak_unref (object,
2779 (GWeakNotify) g_nullify_pointer,
2780 weak_pointer_location);
2783 static guint
2784 object_floating_flag_handler (GObject *object,
2785 gint job)
2787 switch (job)
2789 gpointer oldvalue;
2790 case +1: /* force floating if possible */
2792 oldvalue = g_atomic_pointer_get (&object->qdata);
2793 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2794 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2795 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2796 case -1: /* sink if possible */
2798 oldvalue = g_atomic_pointer_get (&object->qdata);
2799 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2800 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2801 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2802 default: /* check floating */
2803 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2808 * g_object_is_floating:
2809 * @object: (type GObject.Object): a #GObject
2811 * Checks whether @object has a [floating][floating-ref] reference.
2813 * Since: 2.10
2815 * Returns: %TRUE if @object has a floating reference
2817 gboolean
2818 g_object_is_floating (gpointer _object)
2820 GObject *object = _object;
2821 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2822 return floating_flag_handler (object, 0);
2826 * g_object_ref_sink:
2827 * @object: (type GObject.Object): a #GObject
2829 * Increase the reference count of @object, and possibly remove the
2830 * [floating][floating-ref] reference, if @object has a floating reference.
2832 * In other words, if the object is floating, then this call "assumes
2833 * ownership" of the floating reference, converting it to a normal
2834 * reference by clearing the floating flag while leaving the reference
2835 * count unchanged. If the object is not floating, then this call
2836 * adds a new normal reference increasing the reference count by one.
2838 * Since: 2.10
2840 * Returns: (type GObject.Object) (transfer none): @object
2842 gpointer
2843 g_object_ref_sink (gpointer _object)
2845 GObject *object = _object;
2846 gboolean was_floating;
2847 g_return_val_if_fail (G_IS_OBJECT (object), object);
2848 g_return_val_if_fail (object->ref_count >= 1, object);
2849 g_object_ref (object);
2850 was_floating = floating_flag_handler (object, -1);
2851 if (was_floating)
2852 g_object_unref (object);
2853 return object;
2857 * g_object_force_floating:
2858 * @object: a #GObject
2860 * This function is intended for #GObject implementations to re-enforce
2861 * a [floating][floating-ref] object reference. Doing this is seldom
2862 * required: all #GInitiallyUnowneds are created with a floating reference
2863 * which usually just needs to be sunken by calling g_object_ref_sink().
2865 * Since: 2.10
2867 void
2868 g_object_force_floating (GObject *object)
2870 g_return_if_fail (G_IS_OBJECT (object));
2871 g_return_if_fail (object->ref_count >= 1);
2873 floating_flag_handler (object, +1);
2876 typedef struct {
2877 GObject *object;
2878 guint n_toggle_refs;
2879 struct {
2880 GToggleNotify notify;
2881 gpointer data;
2882 } toggle_refs[1]; /* flexible array */
2883 } ToggleRefStack;
2885 static void
2886 toggle_refs_notify (GObject *object,
2887 gboolean is_last_ref)
2889 ToggleRefStack tstack, *tstackptr;
2891 G_LOCK (toggle_refs_mutex);
2892 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
2893 tstack = *tstackptr;
2894 G_UNLOCK (toggle_refs_mutex);
2896 /* Reentrancy here is not as tricky as it seems, because a toggle reference
2897 * will only be notified when there is exactly one of them.
2899 g_assert (tstack.n_toggle_refs == 1);
2900 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
2904 * g_object_add_toggle_ref: (skip)
2905 * @object: a #GObject
2906 * @notify: a function to call when this reference is the
2907 * last reference to the object, or is no longer
2908 * the last reference.
2909 * @data: data to pass to @notify
2911 * Increases the reference count of the object by one and sets a
2912 * callback to be called when all other references to the object are
2913 * dropped, or when this is already the last reference to the object
2914 * and another reference is established.
2916 * This functionality is intended for binding @object to a proxy
2917 * object managed by another memory manager. This is done with two
2918 * paired references: the strong reference added by
2919 * g_object_add_toggle_ref() and a reverse reference to the proxy
2920 * object which is either a strong reference or weak reference.
2922 * The setup is that when there are no other references to @object,
2923 * only a weak reference is held in the reverse direction from @object
2924 * to the proxy object, but when there are other references held to
2925 * @object, a strong reference is held. The @notify callback is called
2926 * when the reference from @object to the proxy object should be
2927 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
2928 * (@is_last_ref false).
2930 * Since a (normal) reference must be held to the object before
2931 * calling g_object_add_toggle_ref(), the initial state of the reverse
2932 * link is always strong.
2934 * Multiple toggle references may be added to the same gobject,
2935 * however if there are multiple toggle references to an object, none
2936 * of them will ever be notified until all but one are removed. For
2937 * this reason, you should only ever use a toggle reference if there
2938 * is important state in the proxy object.
2940 * Since: 2.8
2942 void
2943 g_object_add_toggle_ref (GObject *object,
2944 GToggleNotify notify,
2945 gpointer data)
2947 ToggleRefStack *tstack;
2948 guint i;
2950 g_return_if_fail (G_IS_OBJECT (object));
2951 g_return_if_fail (notify != NULL);
2952 g_return_if_fail (object->ref_count >= 1);
2954 g_object_ref (object);
2956 G_LOCK (toggle_refs_mutex);
2957 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
2958 if (tstack)
2960 i = tstack->n_toggle_refs++;
2961 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
2962 * in tstate->toggle_refs */
2963 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
2965 else
2967 tstack = g_renew (ToggleRefStack, NULL, 1);
2968 tstack->object = object;
2969 tstack->n_toggle_refs = 1;
2970 i = 0;
2973 /* Set a flag for fast lookup after adding the first toggle reference */
2974 if (tstack->n_toggle_refs == 1)
2975 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
2977 tstack->toggle_refs[i].notify = notify;
2978 tstack->toggle_refs[i].data = data;
2979 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
2980 (GDestroyNotify)g_free);
2981 G_UNLOCK (toggle_refs_mutex);
2985 * g_object_remove_toggle_ref: (skip)
2986 * @object: a #GObject
2987 * @notify: a function to call when this reference is the
2988 * last reference to the object, or is no longer
2989 * the last reference.
2990 * @data: data to pass to @notify
2992 * Removes a reference added with g_object_add_toggle_ref(). The
2993 * reference count of the object is decreased by one.
2995 * Since: 2.8
2997 void
2998 g_object_remove_toggle_ref (GObject *object,
2999 GToggleNotify notify,
3000 gpointer data)
3002 ToggleRefStack *tstack;
3003 gboolean found_one = FALSE;
3005 g_return_if_fail (G_IS_OBJECT (object));
3006 g_return_if_fail (notify != NULL);
3008 G_LOCK (toggle_refs_mutex);
3009 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3010 if (tstack)
3012 guint i;
3014 for (i = 0; i < tstack->n_toggle_refs; i++)
3015 if (tstack->toggle_refs[i].notify == notify &&
3016 tstack->toggle_refs[i].data == data)
3018 found_one = TRUE;
3019 tstack->n_toggle_refs -= 1;
3020 if (i != tstack->n_toggle_refs)
3021 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3023 if (tstack->n_toggle_refs == 0)
3024 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3026 break;
3029 G_UNLOCK (toggle_refs_mutex);
3031 if (found_one)
3032 g_object_unref (object);
3033 else
3034 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3038 * g_object_ref:
3039 * @object: (type GObject.Object): a #GObject
3041 * Increases the reference count of @object.
3043 * Returns: (type GObject.Object) (transfer none): the same @object
3045 gpointer
3046 g_object_ref (gpointer _object)
3048 GObject *object = _object;
3049 gint old_val;
3051 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3052 g_return_val_if_fail (object->ref_count > 0, NULL);
3054 old_val = g_atomic_int_add (&object->ref_count, 1);
3056 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3057 toggle_refs_notify (object, FALSE);
3059 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3061 return object;
3065 * g_object_unref:
3066 * @object: (type GObject.Object): a #GObject
3068 * Decreases the reference count of @object. When its reference count
3069 * drops to 0, the object is finalized (i.e. its memory is freed).
3071 * If the pointer to the #GObject may be reused in future (for example, if it is
3072 * an instance variable of another object), it is recommended to clear the
3073 * pointer to %NULL rather than retain a dangling pointer to a potentially
3074 * invalid #GObject instance. Use g_clear_object() for this.
3076 void
3077 g_object_unref (gpointer _object)
3079 GObject *object = _object;
3080 gint old_ref;
3082 g_return_if_fail (G_IS_OBJECT (object));
3083 g_return_if_fail (object->ref_count > 0);
3085 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3086 retry_atomic_decrement1:
3087 old_ref = g_atomic_int_get (&object->ref_count);
3088 if (old_ref > 1)
3090 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3091 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3093 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3094 goto retry_atomic_decrement1;
3096 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3098 /* if we went from 2->1 we need to notify toggle refs if any */
3099 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3100 toggle_refs_notify (object, TRUE);
3102 else
3104 GSList **weak_locations;
3106 /* The only way that this object can live at this point is if
3107 * there are outstanding weak references already established
3108 * before we got here.
3110 * If there were not already weak references then no more can be
3111 * established at this time, because the other thread would have
3112 * to hold a strong ref in order to call
3113 * g_object_add_weak_pointer() and then we wouldn't be here.
3115 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3117 if (weak_locations != NULL)
3119 g_rw_lock_writer_lock (&weak_locations_lock);
3121 /* It is possible that one of the weak references beat us to
3122 * the lock. Make sure the refcount is still what we expected
3123 * it to be.
3125 old_ref = g_atomic_int_get (&object->ref_count);
3126 if (old_ref != 1)
3128 g_rw_lock_writer_unlock (&weak_locations_lock);
3129 goto retry_atomic_decrement1;
3132 /* We got the lock first, so the object will definitely die
3133 * now. Clear out all the weak references.
3135 while (*weak_locations)
3137 GWeakRef *weak_ref_location = (*weak_locations)->data;
3139 weak_ref_location->priv.p = NULL;
3140 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3143 g_rw_lock_writer_unlock (&weak_locations_lock);
3146 /* we are about to remove the last reference */
3147 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3148 G_OBJECT_GET_CLASS (object)->dispose (object);
3149 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3151 /* may have been re-referenced meanwhile */
3152 retry_atomic_decrement2:
3153 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3154 if (old_ref > 1)
3156 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3157 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3159 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3160 goto retry_atomic_decrement2;
3162 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3164 /* if we went from 2->1 we need to notify toggle refs if any */
3165 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3166 toggle_refs_notify (object, TRUE);
3168 return;
3171 /* we are still in the process of taking away the last ref */
3172 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3173 g_signal_handlers_destroy (object);
3174 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3176 /* decrement the last reference */
3177 old_ref = g_atomic_int_add (&object->ref_count, -1);
3179 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3181 /* may have been re-referenced meanwhile */
3182 if (G_LIKELY (old_ref == 1))
3184 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3185 G_OBJECT_GET_CLASS (object)->finalize (object);
3187 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3189 GOBJECT_IF_DEBUG (OBJECTS,
3191 /* catch objects not chaining finalize handlers */
3192 G_LOCK (debug_objects);
3193 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3194 G_UNLOCK (debug_objects);
3196 g_type_free_instance ((GTypeInstance*) object);
3202 * g_clear_object: (skip)
3203 * @object_ptr: a pointer to a #GObject reference
3205 * Clears a reference to a #GObject.
3207 * @object_ptr must not be %NULL.
3209 * If the reference is %NULL then this function does nothing.
3210 * Otherwise, the reference count of the object is decreased and the
3211 * pointer is set to %NULL.
3213 * A macro is also included that allows this function to be used without
3214 * pointer casts.
3216 * Since: 2.28
3218 #undef g_clear_object
3219 void
3220 g_clear_object (volatile GObject **object_ptr)
3222 g_clear_pointer (object_ptr, g_object_unref);
3226 * g_object_get_qdata:
3227 * @object: The GObject to get a stored user data pointer from
3228 * @quark: A #GQuark, naming the user data pointer
3230 * This function gets back user data pointers stored via
3231 * g_object_set_qdata().
3233 * Returns: (transfer none): The user data pointer set, or %NULL
3235 gpointer
3236 g_object_get_qdata (GObject *object,
3237 GQuark quark)
3239 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3241 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3245 * g_object_set_qdata: (skip)
3246 * @object: The GObject to set store a user data pointer
3247 * @quark: A #GQuark, naming the user data pointer
3248 * @data: An opaque user data pointer
3250 * This sets an opaque, named pointer on an object.
3251 * The name is specified through a #GQuark (retrived e.g. via
3252 * g_quark_from_static_string()), and the pointer
3253 * can be gotten back from the @object with g_object_get_qdata()
3254 * until the @object is finalized.
3255 * Setting a previously set user data pointer, overrides (frees)
3256 * the old pointer set, using #NULL as pointer essentially
3257 * removes the data stored.
3259 void
3260 g_object_set_qdata (GObject *object,
3261 GQuark quark,
3262 gpointer data)
3264 g_return_if_fail (G_IS_OBJECT (object));
3265 g_return_if_fail (quark > 0);
3267 g_datalist_id_set_data (&object->qdata, quark, data);
3271 * g_object_dup_qdata:
3272 * @object: the #GObject to store user data on
3273 * @quark: a #GQuark, naming the user data pointer
3274 * @dup_func: (nullable): function to dup the value
3275 * @user_data: (nullable): passed as user_data to @dup_func
3277 * This is a variant of g_object_get_qdata() which returns
3278 * a 'duplicate' of the value. @dup_func defines the
3279 * meaning of 'duplicate' in this context, it could e.g.
3280 * take a reference on a ref-counted object.
3282 * If the @quark is not set on the object then @dup_func
3283 * will be called with a %NULL argument.
3285 * Note that @dup_func is called while user data of @object
3286 * is locked.
3288 * This function can be useful to avoid races when multiple
3289 * threads are using object data on the same key on the same
3290 * object.
3292 * Returns: the result of calling @dup_func on the value
3293 * associated with @quark on @object, or %NULL if not set.
3294 * If @dup_func is %NULL, the value is returned
3295 * unmodified.
3297 * Since: 2.34
3299 gpointer
3300 g_object_dup_qdata (GObject *object,
3301 GQuark quark,
3302 GDuplicateFunc dup_func,
3303 gpointer user_data)
3305 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3306 g_return_val_if_fail (quark > 0, NULL);
3308 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3312 * g_object_replace_qdata:
3313 * @object: the #GObject to store user data on
3314 * @quark: a #GQuark, naming the user data pointer
3315 * @oldval: (nullable): the old value to compare against
3316 * @newval: (nullable): the new value
3317 * @destroy: (nullable): a destroy notify for the new value
3318 * @old_destroy: (nullable): destroy notify for the existing value
3320 * Compares the user data for the key @quark on @object with
3321 * @oldval, and if they are the same, replaces @oldval with
3322 * @newval.
3324 * This is like a typical atomic compare-and-exchange
3325 * operation, for user data on an object.
3327 * If the previous value was replaced then ownership of the
3328 * old value (@oldval) is passed to the caller, including
3329 * the registered destroy notify for it (passed out in @old_destroy).
3330 * Its up to the caller to free this as he wishes, which may
3331 * or may not include using @old_destroy as sometimes replacement
3332 * should not destroy the object in the normal way.
3334 * Returns: %TRUE if the existing value for @quark was replaced
3335 * by @newval, %FALSE otherwise.
3337 * Since: 2.34
3339 gboolean
3340 g_object_replace_qdata (GObject *object,
3341 GQuark quark,
3342 gpointer oldval,
3343 gpointer newval,
3344 GDestroyNotify destroy,
3345 GDestroyNotify *old_destroy)
3347 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3348 g_return_val_if_fail (quark > 0, FALSE);
3350 return g_datalist_id_replace_data (&object->qdata, quark,
3351 oldval, newval, destroy,
3352 old_destroy);
3356 * g_object_set_qdata_full: (skip)
3357 * @object: The GObject to set store a user data pointer
3358 * @quark: A #GQuark, naming the user data pointer
3359 * @data: An opaque user data pointer
3360 * @destroy: Function to invoke with @data as argument, when @data
3361 * needs to be freed
3363 * This function works like g_object_set_qdata(), but in addition,
3364 * a void (*destroy) (gpointer) function may be specified which is
3365 * called with @data as argument when the @object is finalized, or
3366 * the data is being overwritten by a call to g_object_set_qdata()
3367 * with the same @quark.
3369 void
3370 g_object_set_qdata_full (GObject *object,
3371 GQuark quark,
3372 gpointer data,
3373 GDestroyNotify destroy)
3375 g_return_if_fail (G_IS_OBJECT (object));
3376 g_return_if_fail (quark > 0);
3378 g_datalist_id_set_data_full (&object->qdata, quark, data,
3379 data ? destroy : (GDestroyNotify) NULL);
3383 * g_object_steal_qdata:
3384 * @object: The GObject to get a stored user data pointer from
3385 * @quark: A #GQuark, naming the user data pointer
3387 * This function gets back user data pointers stored via
3388 * g_object_set_qdata() and removes the @data from object
3389 * without invoking its destroy() function (if any was
3390 * set).
3391 * Usually, calling this function is only required to update
3392 * user data pointers with a destroy notifier, for example:
3393 * |[<!-- language="C" -->
3394 * void
3395 * object_add_to_user_list (GObject *object,
3396 * const gchar *new_string)
3398 * // the quark, naming the object data
3399 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3400 * // retrive the old string list
3401 * GList *list = g_object_steal_qdata (object, quark_string_list);
3403 * // prepend new string
3404 * list = g_list_prepend (list, g_strdup (new_string));
3405 * // this changed 'list', so we need to set it again
3406 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3408 * static void
3409 * free_string_list (gpointer data)
3411 * GList *node, *list = data;
3413 * for (node = list; node; node = node->next)
3414 * g_free (node->data);
3415 * g_list_free (list);
3417 * ]|
3418 * Using g_object_get_qdata() in the above example, instead of
3419 * g_object_steal_qdata() would have left the destroy function set,
3420 * and thus the partial string list would have been freed upon
3421 * g_object_set_qdata_full().
3423 * Returns: (transfer full): The user data pointer set, or %NULL
3425 gpointer
3426 g_object_steal_qdata (GObject *object,
3427 GQuark quark)
3429 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3430 g_return_val_if_fail (quark > 0, NULL);
3432 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3436 * g_object_get_data:
3437 * @object: #GObject containing the associations
3438 * @key: name of the key for that association
3440 * Gets a named field from the objects table of associations (see g_object_set_data()).
3442 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3444 gpointer
3445 g_object_get_data (GObject *object,
3446 const gchar *key)
3448 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3449 g_return_val_if_fail (key != NULL, NULL);
3451 return g_datalist_get_data (&object->qdata, key);
3455 * g_object_set_data:
3456 * @object: #GObject containing the associations.
3457 * @key: name of the key
3458 * @data: data to associate with that key
3460 * Each object carries around a table of associations from
3461 * strings to pointers. This function lets you set an association.
3463 * If the object already had an association with that name,
3464 * the old association will be destroyed.
3466 void
3467 g_object_set_data (GObject *object,
3468 const gchar *key,
3469 gpointer data)
3471 g_return_if_fail (G_IS_OBJECT (object));
3472 g_return_if_fail (key != NULL);
3474 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3478 * g_object_dup_data:
3479 * @object: the #GObject to store user data on
3480 * @key: a string, naming the user data pointer
3481 * @dup_func: (nullable): function to dup the value
3482 * @user_data: (nullable): passed as user_data to @dup_func
3484 * This is a variant of g_object_get_data() which returns
3485 * a 'duplicate' of the value. @dup_func defines the
3486 * meaning of 'duplicate' in this context, it could e.g.
3487 * take a reference on a ref-counted object.
3489 * If the @key is not set on the object then @dup_func
3490 * will be called with a %NULL argument.
3492 * Note that @dup_func is called while user data of @object
3493 * is locked.
3495 * This function can be useful to avoid races when multiple
3496 * threads are using object data on the same key on the same
3497 * object.
3499 * Returns: the result of calling @dup_func on the value
3500 * associated with @key on @object, or %NULL if not set.
3501 * If @dup_func is %NULL, the value is returned
3502 * unmodified.
3504 * Since: 2.34
3506 gpointer
3507 g_object_dup_data (GObject *object,
3508 const gchar *key,
3509 GDuplicateFunc dup_func,
3510 gpointer user_data)
3512 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3513 g_return_val_if_fail (key != NULL, NULL);
3515 return g_datalist_id_dup_data (&object->qdata,
3516 g_quark_from_string (key),
3517 dup_func, user_data);
3521 * g_object_replace_data:
3522 * @object: the #GObject to store user data on
3523 * @key: a string, naming the user data pointer
3524 * @oldval: (nullable): the old value to compare against
3525 * @newval: (nullable): the new value
3526 * @destroy: (nullable): a destroy notify for the new value
3527 * @old_destroy: (nullable): destroy notify for the existing value
3529 * Compares the user data for the key @key on @object with
3530 * @oldval, and if they are the same, replaces @oldval with
3531 * @newval.
3533 * This is like a typical atomic compare-and-exchange
3534 * operation, for user data on an object.
3536 * If the previous value was replaced then ownership of the
3537 * old value (@oldval) is passed to the caller, including
3538 * the registered destroy notify for it (passed out in @old_destroy).
3539 * Its up to the caller to free this as he wishes, which may
3540 * or may not include using @old_destroy as sometimes replacement
3541 * should not destroy the object in the normal way.
3543 * Returns: %TRUE if the existing value for @key was replaced
3544 * by @newval, %FALSE otherwise.
3546 * Since: 2.34
3548 gboolean
3549 g_object_replace_data (GObject *object,
3550 const gchar *key,
3551 gpointer oldval,
3552 gpointer newval,
3553 GDestroyNotify destroy,
3554 GDestroyNotify *old_destroy)
3556 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3557 g_return_val_if_fail (key != NULL, FALSE);
3559 return g_datalist_id_replace_data (&object->qdata,
3560 g_quark_from_string (key),
3561 oldval, newval, destroy,
3562 old_destroy);
3566 * g_object_set_data_full: (skip)
3567 * @object: #GObject containing the associations
3568 * @key: name of the key
3569 * @data: data to associate with that key
3570 * @destroy: function to call when the association is destroyed
3572 * Like g_object_set_data() except it adds notification
3573 * for when the association is destroyed, either by setting it
3574 * to a different value or when the object is destroyed.
3576 * Note that the @destroy callback is not called if @data is %NULL.
3578 void
3579 g_object_set_data_full (GObject *object,
3580 const gchar *key,
3581 gpointer data,
3582 GDestroyNotify destroy)
3584 g_return_if_fail (G_IS_OBJECT (object));
3585 g_return_if_fail (key != NULL);
3587 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3588 data ? destroy : (GDestroyNotify) NULL);
3592 * g_object_steal_data:
3593 * @object: #GObject containing the associations
3594 * @key: name of the key
3596 * Remove a specified datum from the object's data associations,
3597 * without invoking the association's destroy handler.
3599 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3601 gpointer
3602 g_object_steal_data (GObject *object,
3603 const gchar *key)
3605 GQuark quark;
3607 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3608 g_return_val_if_fail (key != NULL, NULL);
3610 quark = g_quark_try_string (key);
3612 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3615 static void
3616 g_value_object_init (GValue *value)
3618 value->data[0].v_pointer = NULL;
3621 static void
3622 g_value_object_free_value (GValue *value)
3624 if (value->data[0].v_pointer)
3625 g_object_unref (value->data[0].v_pointer);
3628 static void
3629 g_value_object_copy_value (const GValue *src_value,
3630 GValue *dest_value)
3632 if (src_value->data[0].v_pointer)
3633 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3634 else
3635 dest_value->data[0].v_pointer = NULL;
3638 static void
3639 g_value_object_transform_value (const GValue *src_value,
3640 GValue *dest_value)
3642 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)))
3643 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3644 else
3645 dest_value->data[0].v_pointer = NULL;
3648 static gpointer
3649 g_value_object_peek_pointer (const GValue *value)
3651 return value->data[0].v_pointer;
3654 static gchar*
3655 g_value_object_collect_value (GValue *value,
3656 guint n_collect_values,
3657 GTypeCValue *collect_values,
3658 guint collect_flags)
3660 if (collect_values[0].v_pointer)
3662 GObject *object = collect_values[0].v_pointer;
3664 if (object->g_type_instance.g_class == NULL)
3665 return g_strconcat ("invalid unclassed object pointer for value type '",
3666 G_VALUE_TYPE_NAME (value),
3667 "'",
3668 NULL);
3669 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3670 return g_strconcat ("invalid object type '",
3671 G_OBJECT_TYPE_NAME (object),
3672 "' for value type '",
3673 G_VALUE_TYPE_NAME (value),
3674 "'",
3675 NULL);
3676 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3677 value->data[0].v_pointer = g_object_ref (object);
3679 else
3680 value->data[0].v_pointer = NULL;
3682 return NULL;
3685 static gchar*
3686 g_value_object_lcopy_value (const GValue *value,
3687 guint n_collect_values,
3688 GTypeCValue *collect_values,
3689 guint collect_flags)
3691 GObject **object_p = collect_values[0].v_pointer;
3693 if (!object_p)
3694 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3696 if (!value->data[0].v_pointer)
3697 *object_p = NULL;
3698 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3699 *object_p = value->data[0].v_pointer;
3700 else
3701 *object_p = g_object_ref (value->data[0].v_pointer);
3703 return NULL;
3707 * g_value_set_object:
3708 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3709 * @v_object: (type GObject.Object) (nullable): object value to be set
3711 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3713 * g_value_set_object() increases the reference count of @v_object
3714 * (the #GValue holds a reference to @v_object). If you do not wish
3715 * to increase the reference count of the object (i.e. you wish to
3716 * pass your current reference to the #GValue because you no longer
3717 * need it), use g_value_take_object() instead.
3719 * It is important that your #GValue holds a reference to @v_object (either its
3720 * own, or one it has taken) to ensure that the object won't be destroyed while
3721 * the #GValue still exists).
3723 void
3724 g_value_set_object (GValue *value,
3725 gpointer v_object)
3727 GObject *old;
3729 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3731 old = value->data[0].v_pointer;
3733 if (v_object)
3735 g_return_if_fail (G_IS_OBJECT (v_object));
3736 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3738 value->data[0].v_pointer = v_object;
3739 g_object_ref (value->data[0].v_pointer);
3741 else
3742 value->data[0].v_pointer = NULL;
3744 if (old)
3745 g_object_unref (old);
3749 * g_value_set_object_take_ownership: (skip)
3750 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3751 * @v_object: (nullable): object value to be set
3753 * This is an internal function introduced mainly for C marshallers.
3755 * Deprecated: 2.4: Use g_value_take_object() instead.
3757 void
3758 g_value_set_object_take_ownership (GValue *value,
3759 gpointer v_object)
3761 g_value_take_object (value, v_object);
3765 * g_value_take_object: (skip)
3766 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3767 * @v_object: (nullable): object value to be set
3769 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3770 * and takes over the ownership of the callers reference to @v_object;
3771 * the caller doesn't have to unref it any more (i.e. the reference
3772 * count of the object is not increased).
3774 * If you want the #GValue to hold its own reference to @v_object, use
3775 * g_value_set_object() instead.
3777 * Since: 2.4
3779 void
3780 g_value_take_object (GValue *value,
3781 gpointer v_object)
3783 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3785 if (value->data[0].v_pointer)
3787 g_object_unref (value->data[0].v_pointer);
3788 value->data[0].v_pointer = NULL;
3791 if (v_object)
3793 g_return_if_fail (G_IS_OBJECT (v_object));
3794 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3796 value->data[0].v_pointer = v_object; /* we take over the reference count */
3801 * g_value_get_object:
3802 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3804 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3806 * Returns: (type GObject.Object) (transfer none): object contents of @value
3808 gpointer
3809 g_value_get_object (const GValue *value)
3811 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3813 return value->data[0].v_pointer;
3817 * g_value_dup_object:
3818 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3820 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3821 * its reference count. If the contents of the #GValue are %NULL, then
3822 * %NULL will be returned.
3824 * Returns: (type GObject.Object) (transfer full): object content of @value,
3825 * should be unreferenced when no longer needed.
3827 gpointer
3828 g_value_dup_object (const GValue *value)
3830 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3832 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3836 * g_signal_connect_object: (skip)
3837 * @instance: (type GObject.TypeInstance): the instance to connect to.
3838 * @detailed_signal: a string of the form "signal-name::detail".
3839 * @c_handler: the #GCallback to connect.
3840 * @gobject: (type GObject.Object) (nullable): the object to pass as data
3841 * to @c_handler.
3842 * @connect_flags: a combination of #GConnectFlags.
3844 * This is similar to g_signal_connect_data(), but uses a closure which
3845 * ensures that the @gobject stays alive during the call to @c_handler
3846 * by temporarily adding a reference count to @gobject.
3848 * When the @gobject is destroyed the signal handler will be automatically
3849 * disconnected. Note that this is not currently threadsafe (ie:
3850 * emitting a signal while @gobject is being destroyed in another thread
3851 * is not safe).
3853 * Returns: the handler id.
3855 gulong
3856 g_signal_connect_object (gpointer instance,
3857 const gchar *detailed_signal,
3858 GCallback c_handler,
3859 gpointer gobject,
3860 GConnectFlags connect_flags)
3862 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3863 g_return_val_if_fail (detailed_signal != NULL, 0);
3864 g_return_val_if_fail (c_handler != NULL, 0);
3866 if (gobject)
3868 GClosure *closure;
3870 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
3872 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
3874 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
3876 else
3877 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
3880 typedef struct {
3881 GObject *object;
3882 guint n_closures;
3883 GClosure *closures[1]; /* flexible array */
3884 } CArray;
3885 /* don't change this structure without supplying an accessor for
3886 * watched closures, e.g.:
3887 * GSList* g_object_list_watched_closures (GObject *object)
3889 * CArray *carray;
3890 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3891 * carray = g_object_get_data (object, "GObject-closure-array");
3892 * if (carray)
3894 * GSList *slist = NULL;
3895 * guint i;
3896 * for (i = 0; i < carray->n_closures; i++)
3897 * slist = g_slist_prepend (slist, carray->closures[i]);
3898 * return slist;
3900 * return NULL;
3904 static void
3905 object_remove_closure (gpointer data,
3906 GClosure *closure)
3908 GObject *object = data;
3909 CArray *carray;
3910 guint i;
3912 G_LOCK (closure_array_mutex);
3913 carray = g_object_get_qdata (object, quark_closure_array);
3914 for (i = 0; i < carray->n_closures; i++)
3915 if (carray->closures[i] == closure)
3917 carray->n_closures--;
3918 if (i < carray->n_closures)
3919 carray->closures[i] = carray->closures[carray->n_closures];
3920 G_UNLOCK (closure_array_mutex);
3921 return;
3923 G_UNLOCK (closure_array_mutex);
3924 g_assert_not_reached ();
3927 static void
3928 destroy_closure_array (gpointer data)
3930 CArray *carray = data;
3931 GObject *object = carray->object;
3932 guint i, n = carray->n_closures;
3934 for (i = 0; i < n; i++)
3936 GClosure *closure = carray->closures[i];
3938 /* removing object_remove_closure() upfront is probably faster than
3939 * letting it fiddle with quark_closure_array which is empty anyways
3941 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
3942 g_closure_invalidate (closure);
3944 g_free (carray);
3948 * g_object_watch_closure:
3949 * @object: GObject restricting lifetime of @closure
3950 * @closure: GClosure to watch
3952 * This function essentially limits the life time of the @closure to
3953 * the life time of the object. That is, when the object is finalized,
3954 * the @closure is invalidated by calling g_closure_invalidate() on
3955 * it, in order to prevent invocations of the closure with a finalized
3956 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
3957 * added as marshal guards to the @closure, to ensure that an extra
3958 * reference count is held on @object during invocation of the
3959 * @closure. Usually, this function will be called on closures that
3960 * use this @object as closure data.
3962 void
3963 g_object_watch_closure (GObject *object,
3964 GClosure *closure)
3966 CArray *carray;
3967 guint i;
3969 g_return_if_fail (G_IS_OBJECT (object));
3970 g_return_if_fail (closure != NULL);
3971 g_return_if_fail (closure->is_invalid == FALSE);
3972 g_return_if_fail (closure->in_marshal == FALSE);
3973 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
3975 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
3976 g_closure_add_marshal_guards (closure,
3977 object, (GClosureNotify) g_object_ref,
3978 object, (GClosureNotify) g_object_unref);
3979 G_LOCK (closure_array_mutex);
3980 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
3981 if (!carray)
3983 carray = g_renew (CArray, NULL, 1);
3984 carray->object = object;
3985 carray->n_closures = 1;
3986 i = 0;
3988 else
3990 i = carray->n_closures++;
3991 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
3993 carray->closures[i] = closure;
3994 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
3995 G_UNLOCK (closure_array_mutex);
3999 * g_closure_new_object:
4000 * @sizeof_closure: the size of the structure to allocate, must be at least
4001 * `sizeof (GClosure)`
4002 * @object: a #GObject pointer to store in the @data field of the newly
4003 * allocated #GClosure
4005 * A variant of g_closure_new_simple() which stores @object in the
4006 * @data field of the closure and calls g_object_watch_closure() on
4007 * @object and the created closure. This function is mainly useful
4008 * when implementing new types of closures.
4010 * Returns: (transfer full): a newly allocated #GClosure
4012 GClosure*
4013 g_closure_new_object (guint sizeof_closure,
4014 GObject *object)
4016 GClosure *closure;
4018 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4019 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4021 closure = g_closure_new_simple (sizeof_closure, object);
4022 g_object_watch_closure (object, closure);
4024 return closure;
4028 * g_cclosure_new_object: (skip)
4029 * @callback_func: the function to invoke
4030 * @object: a #GObject pointer to pass to @callback_func
4032 * A variant of g_cclosure_new() which uses @object as @user_data and
4033 * calls g_object_watch_closure() on @object and the created
4034 * closure. This function is useful when you have a callback closely
4035 * associated with a #GObject, and want the callback to no longer run
4036 * after the object is is freed.
4038 * Returns: a new #GCClosure
4040 GClosure*
4041 g_cclosure_new_object (GCallback callback_func,
4042 GObject *object)
4044 GClosure *closure;
4046 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4047 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4048 g_return_val_if_fail (callback_func != NULL, NULL);
4050 closure = g_cclosure_new (callback_func, object, NULL);
4051 g_object_watch_closure (object, closure);
4053 return closure;
4057 * g_cclosure_new_object_swap: (skip)
4058 * @callback_func: the function to invoke
4059 * @object: a #GObject pointer to pass to @callback_func
4061 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4062 * and calls g_object_watch_closure() on @object and the created
4063 * closure. This function is useful when you have a callback closely
4064 * associated with a #GObject, and want the callback to no longer run
4065 * after the object is is freed.
4067 * Returns: a new #GCClosure
4069 GClosure*
4070 g_cclosure_new_object_swap (GCallback callback_func,
4071 GObject *object)
4073 GClosure *closure;
4075 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4076 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4077 g_return_val_if_fail (callback_func != NULL, NULL);
4079 closure = g_cclosure_new_swap (callback_func, object, NULL);
4080 g_object_watch_closure (object, closure);
4082 return closure;
4085 gsize
4086 g_object_compat_control (gsize what,
4087 gpointer data)
4089 switch (what)
4091 gpointer *pp;
4092 case 1: /* floating base type */
4093 return G_TYPE_INITIALLY_UNOWNED;
4094 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4095 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4096 return 1;
4097 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4098 pp = data;
4099 *pp = floating_flag_handler;
4100 return 1;
4101 default:
4102 return 0;
4106 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT);
4108 static void
4109 g_initially_unowned_init (GInitiallyUnowned *object)
4111 g_object_force_floating (object);
4114 static void
4115 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4120 * GWeakRef:
4122 * A structure containing a weak reference to a #GObject. It can either
4123 * be empty (i.e. point to %NULL), or point to an object for as long as
4124 * at least one "strong" reference to that object exists. Before the
4125 * object's #GObjectClass.dispose method is called, every #GWeakRef
4126 * associated with becomes empty (i.e. points to %NULL).
4128 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4129 * heap-allocated, or embedded in larger structures.
4131 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4132 * reference is thread-safe: converting a weak pointer to a reference is
4133 * atomic with respect to invalidation of weak pointers to destroyed
4134 * objects.
4136 * If the object's #GObjectClass.dispose method results in additional
4137 * references to the object being held, any #GWeakRefs taken
4138 * before it was disposed will continue to point to %NULL. If
4139 * #GWeakRefs are taken after the object is disposed and
4140 * re-referenced, they will continue to point to it until its refcount
4141 * goes back to zero, at which point they too will be invalidated.
4145 * g_weak_ref_init: (skip)
4146 * @weak_ref: (inout): uninitialized or empty location for a weak
4147 * reference
4148 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4150 * Initialise a non-statically-allocated #GWeakRef.
4152 * This function also calls g_weak_ref_set() with @object on the
4153 * freshly-initialised weak reference.
4155 * This function should always be matched with a call to
4156 * g_weak_ref_clear(). It is not necessary to use this function for a
4157 * #GWeakRef in static storage because it will already be
4158 * properly initialised. Just use g_weak_ref_set() directly.
4160 * Since: 2.32
4162 void
4163 g_weak_ref_init (GWeakRef *weak_ref,
4164 gpointer object)
4166 weak_ref->priv.p = NULL;
4168 g_weak_ref_set (weak_ref, object);
4172 * g_weak_ref_clear: (skip)
4173 * @weak_ref: (inout): location of a weak reference, which
4174 * may be empty
4176 * Frees resources associated with a non-statically-allocated #GWeakRef.
4177 * After this call, the #GWeakRef is left in an undefined state.
4179 * You should only call this on a #GWeakRef that previously had
4180 * g_weak_ref_init() called on it.
4182 * Since: 2.32
4184 void
4185 g_weak_ref_clear (GWeakRef *weak_ref)
4187 g_weak_ref_set (weak_ref, NULL);
4189 /* be unkind */
4190 weak_ref->priv.p = (void *) 0xccccccccu;
4194 * g_weak_ref_get: (skip)
4195 * @weak_ref: (inout): location of a weak reference to a #GObject
4197 * If @weak_ref is not empty, atomically acquire a strong
4198 * reference to the object it points to, and return that reference.
4200 * This function is needed because of the potential race between taking
4201 * the pointer value and g_object_ref() on it, if the object was losing
4202 * its last reference at the same time in a different thread.
4204 * The caller should release the resulting reference in the usual way,
4205 * by using g_object_unref().
4207 * Returns: (transfer full) (type GObject.Object): the object pointed to
4208 * by @weak_ref, or %NULL if it was empty
4210 * Since: 2.32
4212 gpointer
4213 g_weak_ref_get (GWeakRef *weak_ref)
4215 gpointer object_or_null;
4217 g_return_val_if_fail (weak_ref!= NULL, NULL);
4219 g_rw_lock_reader_lock (&weak_locations_lock);
4221 object_or_null = weak_ref->priv.p;
4223 if (object_or_null != NULL)
4224 g_object_ref (object_or_null);
4226 g_rw_lock_reader_unlock (&weak_locations_lock);
4228 return object_or_null;
4232 * g_weak_ref_set: (skip)
4233 * @weak_ref: location for a weak reference
4234 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4236 * Change the object to which @weak_ref points, or set it to
4237 * %NULL.
4239 * You must own a strong reference on @object while calling this
4240 * function.
4242 * Since: 2.32
4244 void
4245 g_weak_ref_set (GWeakRef *weak_ref,
4246 gpointer object)
4248 GSList **weak_locations;
4249 GObject *new_object;
4250 GObject *old_object;
4252 g_return_if_fail (weak_ref != NULL);
4253 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4255 new_object = object;
4257 g_rw_lock_writer_lock (&weak_locations_lock);
4259 /* We use the extra level of indirection here so that if we have ever
4260 * had a weak pointer installed at any point in time on this object,
4261 * we can see that there is a non-NULL value associated with the
4262 * weak-pointer quark and know that this value will not change at any
4263 * point in the object's lifetime.
4265 * Both properties are important for reducing the amount of times we
4266 * need to acquire locks and for decreasing the duration of time the
4267 * lock is held while avoiding some rather tricky races.
4269 * Specifically: we can avoid having to do an extra unconditional lock
4270 * in g_object_unref() without worrying about some extremely tricky
4271 * races.
4274 old_object = weak_ref->priv.p;
4275 if (new_object != old_object)
4277 weak_ref->priv.p = new_object;
4279 /* Remove the weak ref from the old object */
4280 if (old_object != NULL)
4282 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4283 /* for it to point to an object, the object must have had it added once */
4284 g_assert (weak_locations != NULL);
4286 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4289 /* Add the weak ref to the new object */
4290 if (new_object != NULL)
4292 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4294 if (weak_locations == NULL)
4296 weak_locations = g_new0 (GSList *, 1);
4297 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4300 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4304 g_rw_lock_writer_unlock (&weak_locations_lock);