Update Friulian translation
[glib.git] / gobject / gobject.c
blobb59cf8b1b1066950cc640759849ff49aefc4d318
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.1 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 /* short circuit for calls supplying no properties */
1620 if (!first_property_name)
1621 return g_object_new_with_properties (object_type, 0, NULL, NULL);
1623 va_start (var_args, first_property_name);
1624 object = g_object_new_valist (object_type, first_property_name, var_args);
1625 va_end (var_args);
1627 return object;
1630 static gpointer
1631 g_object_new_with_custom_constructor (GObjectClass *class,
1632 GObjectConstructParam *params,
1633 guint n_params)
1635 GObjectNotifyQueue *nqueue = NULL;
1636 gboolean newly_constructed;
1637 GObjectConstructParam *cparams;
1638 GObject *object;
1639 GValue *cvalues;
1640 gint n_cparams;
1641 gint cvals_used;
1642 GSList *node;
1643 gint i;
1645 /* If we have ->constructed() then we have to do a lot more work.
1646 * It's possible that this is a singleton and it's also possible
1647 * that the user's constructor() will attempt to modify the values
1648 * that we pass in, so we'll need to allocate copies of them.
1649 * It's also possible that the user may attempt to call
1650 * g_object_set() from inside of their constructor, so we need to
1651 * add ourselves to a list of objects for which that is allowed
1652 * while their constructor() is running.
1655 /* Create the array of GObjectConstructParams for constructor() */
1656 n_cparams = g_slist_length (class->construct_properties);
1657 cparams = g_new (GObjectConstructParam, n_cparams);
1658 cvalues = g_new0 (GValue, n_cparams);
1659 cvals_used = 0;
1660 i = 0;
1662 /* As above, we may find the value in the passed-in params list.
1664 * If we have the value passed in then we can use the GValue from
1665 * it directly because it is safe to modify. If we use the
1666 * default value from the class, we had better not pass that in
1667 * and risk it being modified, so we create a new one.
1668 * */
1669 for (node = class->construct_properties; node; node = node->next)
1671 GParamSpec *pspec;
1672 GValue *value;
1673 gint j;
1675 pspec = node->data;
1676 value = NULL; /* to silence gcc... */
1678 for (j = 0; j < n_params; j++)
1679 if (params[j].pspec == pspec)
1681 consider_issuing_property_deprecation_warning (pspec);
1682 value = params[j].value;
1683 break;
1686 if (j == n_params)
1688 value = &cvalues[cvals_used++];
1689 g_value_init (value, pspec->value_type);
1690 g_param_value_set_default (pspec, value);
1693 cparams[i].pspec = pspec;
1694 cparams[i].value = value;
1695 i++;
1698 /* construct object from construction parameters */
1699 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1700 /* free construction values */
1701 g_free (cparams);
1702 while (cvals_used--)
1703 g_value_unset (&cvalues[cvals_used]);
1704 g_free (cvalues);
1706 /* There is code in the wild that relies on being able to return NULL
1707 * from its custom constructor. This was never a supported operation,
1708 * but since the code is already out there...
1710 if (object == NULL)
1712 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1713 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1714 return NULL;
1717 /* g_object_init() will have marked the object as being in-construction.
1718 * Check if the returned object still is so marked, or if this is an
1719 * already-existing singleton (in which case we should not do 'constructed').
1721 newly_constructed = object_in_construction (object);
1722 if (newly_constructed)
1723 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1725 if (CLASS_HAS_PROPS (class))
1727 /* If this object was newly_constructed then g_object_init()
1728 * froze the queue. We need to freeze it here in order to get
1729 * the handle so that we can thaw it below (otherwise it will
1730 * be frozen forever).
1732 * We also want to do a freeze if we have any params to set,
1733 * even on a non-newly_constructed object.
1735 * It's possible that we have the case of non-newly created
1736 * singleton and all of the passed-in params were construct
1737 * properties so n_params > 0 but we will actually set no
1738 * properties. This is a pretty lame case to optimise, so
1739 * just ignore it and freeze anyway.
1741 if (newly_constructed || n_params)
1742 nqueue = g_object_notify_queue_freeze (object, FALSE);
1744 /* Remember: if it was newly_constructed then g_object_init()
1745 * already did a freeze, so we now have two. Release one.
1747 if (newly_constructed)
1748 g_object_notify_queue_thaw (object, nqueue);
1751 /* run 'constructed' handler if there is a custom one */
1752 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1753 class->constructed (object);
1755 /* set remaining properties */
1756 for (i = 0; i < n_params; i++)
1757 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1759 consider_issuing_property_deprecation_warning (params[i].pspec);
1760 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1763 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1764 if (nqueue)
1765 g_object_notify_queue_thaw (object, nqueue);
1767 return object;
1770 static gpointer
1771 g_object_new_internal (GObjectClass *class,
1772 GObjectConstructParam *params,
1773 guint n_params)
1775 GObjectNotifyQueue *nqueue = NULL;
1776 GObject *object;
1778 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1779 return g_object_new_with_custom_constructor (class, params, n_params);
1781 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1783 if (CLASS_HAS_PROPS (class))
1785 GSList *node;
1787 /* This will have been setup in g_object_init() */
1788 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1789 g_assert (nqueue != NULL);
1791 /* We will set exactly n_construct_properties construct
1792 * properties, but they may come from either the class default
1793 * values or the passed-in parameter list.
1795 for (node = class->construct_properties; node; node = node->next)
1797 const GValue *value;
1798 GParamSpec *pspec;
1799 gint j;
1801 pspec = node->data;
1802 value = NULL; /* to silence gcc... */
1804 for (j = 0; j < n_params; j++)
1805 if (params[j].pspec == pspec)
1807 consider_issuing_property_deprecation_warning (pspec);
1808 value = params[j].value;
1809 break;
1812 if (j == n_params)
1813 value = g_param_spec_get_default_value (pspec);
1815 object_set_property (object, pspec, value, nqueue);
1819 /* run 'constructed' handler if there is a custom one */
1820 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1821 class->constructed (object);
1823 if (nqueue)
1825 gint i;
1827 /* Set remaining properties. The construct properties will
1828 * already have been taken, so set only the non-construct
1829 * ones.
1831 for (i = 0; i < n_params; i++)
1832 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1834 consider_issuing_property_deprecation_warning (params[i].pspec);
1835 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1838 g_object_notify_queue_thaw (object, nqueue);
1841 return object;
1845 static inline gboolean
1846 g_object_new_is_valid_property (GType object_type,
1847 GParamSpec *pspec,
1848 const char *name,
1849 GObjectConstructParam *params,
1850 int n_params)
1852 gint i;
1853 if (G_UNLIKELY (pspec == NULL))
1855 g_critical ("%s: object class '%s' has no property named '%s'",
1856 G_STRFUNC, g_type_name (object_type), name);
1857 return FALSE;
1860 if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
1862 g_critical ("%s: property '%s' of object class '%s' is not writable",
1863 G_STRFUNC, pspec->name, g_type_name (object_type));
1864 return FALSE;
1867 if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1869 for (i = 0; i < n_params; i++)
1870 if (params[i].pspec == pspec)
1871 break;
1872 if (G_UNLIKELY (i != n_params))
1874 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1875 G_STRFUNC, name, g_type_name (object_type));
1876 return FALSE;
1879 return TRUE;
1884 * g_object_new_with_properties: (rename-to g_object_new)
1885 * @object_type: the object type to instantiate
1886 * @n_properties: the number of properties
1887 * @names: (array length=n_properties): the names of each property to be set
1888 * @values: (array length=n_properties): the values of each property to be set
1890 * Creates a new instance of a #GObject subtype and sets its properties using
1891 * the provided arrays. Both arrays must have exactly @n_properties elements,
1892 * and the names and values correspond by index.
1894 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
1895 * which are not explicitly specified are set to their default values.
1897 * Returns: (type GObject.Object) (transfer full): a new instance of
1898 * @object_type
1900 * Since: 2.54
1902 GObject *
1903 g_object_new_with_properties (GType object_type,
1904 guint n_properties,
1905 const char *names[],
1906 const GValue values[])
1908 GObjectClass *class, *unref_class = NULL;
1909 GObject *object;
1911 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1913 /* Try to avoid thrashing the ref_count if we don't need to (since
1914 * it's a locked operation).
1916 class = g_type_class_peek_static (object_type);
1918 if (class == NULL)
1919 class = unref_class = g_type_class_ref (object_type);
1921 if (n_properties > 0)
1923 guint i, count = 0;
1924 GObjectConstructParam *params;
1926 params = g_newa (GObjectConstructParam, n_properties);
1927 for (i = 0; i < n_properties; i++)
1929 GParamSpec *pspec;
1930 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], object_type, TRUE);
1931 if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
1932 continue;
1933 params[count].pspec = pspec;
1935 /* Init GValue */
1936 params[count].value = g_newa (GValue, 1);
1937 memset (params[count].value, 0, sizeof (GValue));
1938 g_value_init (params[count].value, G_VALUE_TYPE (&values[i]));
1940 g_value_copy (&values[i], params[count].value);
1941 count++;
1943 object = g_object_new_internal (class, params, count);
1945 while (count--)
1946 g_value_unset (params[count].value);
1948 else
1949 object = g_object_new_internal (class, NULL, 0);
1951 if (unref_class != NULL)
1952 g_type_class_unref (unref_class);
1954 return object;
1958 * g_object_newv:
1959 * @object_type: the type id of the #GObject subtype to instantiate
1960 * @n_parameters: the length of the @parameters array
1961 * @parameters: (array length=n_parameters): an array of #GParameter
1963 * Creates a new instance of a #GObject subtype and sets its properties.
1965 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1966 * which are not explicitly specified are set to their default values.
1968 * Returns: (type GObject.Object) (transfer full): a new instance of
1969 * @object_type
1971 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
1972 * deprecated. See #GParameter for more information.
1974 gpointer
1975 g_object_newv (GType object_type,
1976 guint n_parameters,
1977 GParameter *parameters)
1979 GObjectClass *class, *unref_class = NULL;
1980 GObject *object;
1982 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1983 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
1985 /* Try to avoid thrashing the ref_count if we don't need to (since
1986 * it's a locked operation).
1988 class = g_type_class_peek_static (object_type);
1990 if (!class)
1991 class = unref_class = g_type_class_ref (object_type);
1993 if (n_parameters)
1995 GObjectConstructParam *cparams;
1996 guint i, j;
1998 cparams = g_newa (GObjectConstructParam, n_parameters);
1999 j = 0;
2001 for (i = 0; i < n_parameters; i++)
2003 GParamSpec *pspec;
2005 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
2006 if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2007 continue;
2009 cparams[j].pspec = pspec;
2010 cparams[j].value = &parameters[i].value;
2011 j++;
2014 object = g_object_new_internal (class, cparams, j);
2016 else
2017 /* Fast case: no properties passed in. */
2018 object = g_object_new_internal (class, NULL, 0);
2020 if (unref_class)
2021 g_type_class_unref (unref_class);
2023 return object;
2027 * g_object_new_valist: (skip)
2028 * @object_type: the type id of the #GObject subtype to instantiate
2029 * @first_property_name: the name of the first property
2030 * @var_args: the value of the first property, followed optionally by more
2031 * name/value pairs, followed by %NULL
2033 * Creates a new instance of a #GObject subtype and sets its properties.
2035 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
2036 * which are not explicitly specified are set to their default values.
2038 * Returns: a new instance of @object_type
2040 GObject*
2041 g_object_new_valist (GType object_type,
2042 const gchar *first_property_name,
2043 va_list var_args)
2045 GObjectClass *class, *unref_class = NULL;
2046 GObject *object;
2048 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2050 /* Try to avoid thrashing the ref_count if we don't need to (since
2051 * it's a locked operation).
2053 class = g_type_class_peek_static (object_type);
2055 if (!class)
2056 class = unref_class = g_type_class_ref (object_type);
2058 if (first_property_name)
2060 GObjectConstructParam stack_params[16];
2061 GObjectConstructParam *params;
2062 const gchar *name;
2063 gint n_params = 0;
2065 name = first_property_name;
2066 params = stack_params;
2070 gchar *error = NULL;
2071 GParamSpec *pspec;
2073 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
2075 if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2076 break;
2078 if (n_params == 16)
2080 params = g_new (GObjectConstructParam, n_params + 1);
2081 memcpy (params, stack_params, sizeof stack_params);
2083 else if (n_params > 16)
2084 params = g_renew (GObjectConstructParam, params, n_params + 1);
2086 params[n_params].pspec = pspec;
2087 params[n_params].value = g_newa (GValue, 1);
2088 memset (params[n_params].value, 0, sizeof (GValue));
2090 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
2092 if (error)
2094 g_critical ("%s: %s", G_STRFUNC, error);
2095 g_value_unset (params[n_params].value);
2096 g_free (error);
2097 break;
2100 n_params++;
2102 while ((name = va_arg (var_args, const gchar *)));
2104 object = g_object_new_internal (class, params, n_params);
2106 while (n_params--)
2107 g_value_unset (params[n_params].value);
2109 if (params != stack_params)
2110 g_free (params);
2112 else
2113 /* Fast case: no properties passed in. */
2114 object = g_object_new_internal (class, NULL, 0);
2116 if (unref_class)
2117 g_type_class_unref (unref_class);
2119 return object;
2122 static GObject*
2123 g_object_constructor (GType type,
2124 guint n_construct_properties,
2125 GObjectConstructParam *construct_params)
2127 GObject *object;
2129 /* create object */
2130 object = (GObject*) g_type_create_instance (type);
2132 /* set construction parameters */
2133 if (n_construct_properties)
2135 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2137 /* set construct properties */
2138 while (n_construct_properties--)
2140 GValue *value = construct_params->value;
2141 GParamSpec *pspec = construct_params->pspec;
2143 construct_params++;
2144 object_set_property (object, pspec, value, nqueue);
2146 g_object_notify_queue_thaw (object, nqueue);
2147 /* the notification queue is still frozen from g_object_init(), so
2148 * we don't need to handle it here, g_object_newv() takes
2149 * care of that
2153 return object;
2156 static void
2157 g_object_constructed (GObject *object)
2159 /* empty default impl to allow unconditional upchaining */
2162 static inline gboolean
2163 g_object_set_is_valid_property (GObject *object,
2164 GParamSpec *pspec,
2165 const char *property_name)
2167 if (G_UNLIKELY (pspec == NULL))
2169 g_warning ("%s: object class '%s' has no property named '%s'",
2170 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2171 return FALSE;
2173 if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
2175 g_warning ("%s: property '%s' of object class '%s' is not writable",
2176 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2177 return FALSE;
2179 if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
2181 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2182 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2183 return FALSE;
2185 return TRUE;
2189 * g_object_setv: (skip)
2190 * @object: a #GObject
2191 * @n_properties: the number of properties
2192 * @names: (array length=n_properties): the names of each property to be set
2193 * @values: (array length=n_properties): the values of each property to be set
2195 * Sets @n_properties properties for an @object.
2196 * Properties to be set will be taken from @values. All properties must be
2197 * valid. Warnings will be emitted and undefined behaviour may result if invalid
2198 * properties are passed in.
2200 * Since: 2.54
2202 void
2203 g_object_setv (GObject *object,
2204 guint n_properties,
2205 const gchar *names[],
2206 const GValue values[])
2208 guint i;
2209 GObjectNotifyQueue *nqueue;
2210 GParamSpec *pspec;
2211 GType obj_type;
2213 g_return_if_fail (G_IS_OBJECT (object));
2215 if (n_properties == 0)
2216 return;
2218 g_object_ref (object);
2219 obj_type = G_OBJECT_TYPE (object);
2220 nqueue = g_object_notify_queue_freeze (object, FALSE);
2221 for (i = 0; i < n_properties; i++)
2223 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], obj_type, TRUE);
2225 if (!g_object_set_is_valid_property (object, pspec, names[i]))
2226 break;
2228 consider_issuing_property_deprecation_warning (pspec);
2229 object_set_property (object, pspec, &values[i], nqueue);
2232 g_object_notify_queue_thaw (object, nqueue);
2233 g_object_unref (object);
2237 * g_object_set_valist: (skip)
2238 * @object: a #GObject
2239 * @first_property_name: name of the first property to set
2240 * @var_args: value for the first property, followed optionally by more
2241 * name/value pairs, followed by %NULL
2243 * Sets properties on an object.
2245 void
2246 g_object_set_valist (GObject *object,
2247 const gchar *first_property_name,
2248 va_list var_args)
2250 GObjectNotifyQueue *nqueue;
2251 const gchar *name;
2253 g_return_if_fail (G_IS_OBJECT (object));
2255 g_object_ref (object);
2256 nqueue = g_object_notify_queue_freeze (object, FALSE);
2258 name = first_property_name;
2259 while (name)
2261 GValue value = G_VALUE_INIT;
2262 GParamSpec *pspec;
2263 gchar *error = NULL;
2265 pspec = g_param_spec_pool_lookup (pspec_pool,
2266 name,
2267 G_OBJECT_TYPE (object),
2268 TRUE);
2270 if (!g_object_set_is_valid_property (object, pspec, name))
2271 break;
2273 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2274 0, &error);
2275 if (error)
2277 g_warning ("%s: %s", G_STRFUNC, error);
2278 g_free (error);
2279 g_value_unset (&value);
2280 break;
2283 consider_issuing_property_deprecation_warning (pspec);
2284 object_set_property (object, pspec, &value, nqueue);
2285 g_value_unset (&value);
2287 name = va_arg (var_args, gchar*);
2290 g_object_notify_queue_thaw (object, nqueue);
2291 g_object_unref (object);
2294 static inline gboolean
2295 g_object_get_is_valid_property (GObject *object,
2296 GParamSpec *pspec,
2297 const char *property_name)
2299 if (G_UNLIKELY (pspec == NULL))
2301 g_warning ("%s: object class '%s' has no property named '%s'",
2302 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2303 return FALSE;
2305 if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
2307 g_warning ("%s: property '%s' of object class '%s' is not readable",
2308 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2309 return FALSE;
2311 return TRUE;
2315 * g_object_getv:
2316 * @object: a #GObject
2317 * @n_properties: the number of properties
2318 * @names: (array length=n_properties): the names of each property to get
2319 * @values: (array length=n_properties): the values of each property to get
2321 * Gets @n_properties properties for an @object.
2322 * Obtained properties will be set to @values. All properties must be valid.
2323 * Warnings will be emitted and undefined behaviour may result if invalid
2324 * properties are passed in.
2326 * Since: 2.54
2328 void
2329 g_object_getv (GObject *object,
2330 guint n_properties,
2331 const gchar *names[],
2332 GValue values[])
2334 guint i;
2335 GParamSpec *pspec;
2336 GType obj_type;
2338 g_return_if_fail (G_IS_OBJECT (object));
2340 if (n_properties == 0)
2341 return;
2343 g_object_ref (object);
2345 obj_type = G_OBJECT_TYPE (object);
2346 for (i = 0; i < n_properties; i++)
2348 pspec = g_param_spec_pool_lookup (pspec_pool,
2349 names[i],
2350 obj_type,
2351 TRUE);
2352 if (!g_object_get_is_valid_property (object, pspec, names[i]))
2353 break;
2355 memset (&values[i], 0, sizeof (GValue));
2356 g_value_init (&values[i], pspec->value_type);
2357 object_get_property (object, pspec, &values[i]);
2359 g_object_unref (object);
2363 * g_object_get_valist: (skip)
2364 * @object: a #GObject
2365 * @first_property_name: name of the first property to get
2366 * @var_args: return location for the first property, followed optionally by more
2367 * name/return location pairs, followed by %NULL
2369 * Gets properties of an object.
2371 * In general, a copy is made of the property contents and the caller
2372 * is responsible for freeing the memory in the appropriate manner for
2373 * the type, for instance by calling g_free() or g_object_unref().
2375 * See g_object_get().
2377 void
2378 g_object_get_valist (GObject *object,
2379 const gchar *first_property_name,
2380 va_list var_args)
2382 const gchar *name;
2384 g_return_if_fail (G_IS_OBJECT (object));
2386 g_object_ref (object);
2388 name = first_property_name;
2390 while (name)
2392 GValue value = G_VALUE_INIT;
2393 GParamSpec *pspec;
2394 gchar *error;
2396 pspec = g_param_spec_pool_lookup (pspec_pool,
2397 name,
2398 G_OBJECT_TYPE (object),
2399 TRUE);
2401 if (!g_object_get_is_valid_property (object, pspec, name))
2402 break;
2404 g_value_init (&value, pspec->value_type);
2406 object_get_property (object, pspec, &value);
2408 G_VALUE_LCOPY (&value, var_args, 0, &error);
2409 if (error)
2411 g_warning ("%s: %s", G_STRFUNC, error);
2412 g_free (error);
2413 g_value_unset (&value);
2414 break;
2417 g_value_unset (&value);
2419 name = va_arg (var_args, gchar*);
2422 g_object_unref (object);
2426 * g_object_set: (skip)
2427 * @object: (type GObject.Object): a #GObject
2428 * @first_property_name: name of the first property to set
2429 * @...: value for the first property, followed optionally by more
2430 * name/value pairs, followed by %NULL
2432 * Sets properties on an object.
2434 * Note that the "notify" signals are queued and only emitted (in
2435 * reverse order) after all properties have been set. See
2436 * g_object_freeze_notify().
2438 void
2439 g_object_set (gpointer _object,
2440 const gchar *first_property_name,
2441 ...)
2443 GObject *object = _object;
2444 va_list var_args;
2446 g_return_if_fail (G_IS_OBJECT (object));
2448 va_start (var_args, first_property_name);
2449 g_object_set_valist (object, first_property_name, var_args);
2450 va_end (var_args);
2454 * g_object_get: (skip)
2455 * @object: (type GObject.Object): a #GObject
2456 * @first_property_name: name of the first property to get
2457 * @...: return location for the first property, followed optionally by more
2458 * name/return location pairs, followed by %NULL
2460 * Gets properties of an object.
2462 * In general, a copy is made of the property contents and the caller
2463 * is responsible for freeing the memory in the appropriate manner for
2464 * the type, for instance by calling g_free() or g_object_unref().
2466 * Here is an example of using g_object_get() to get the contents
2467 * of three properties: an integer, a string and an object:
2468 * |[<!-- language="C" -->
2469 * gint intval;
2470 * gchar *strval;
2471 * GObject *objval;
2473 * g_object_get (my_object,
2474 * "int-property", &intval,
2475 * "str-property", &strval,
2476 * "obj-property", &objval,
2477 * NULL);
2479 * // Do something with intval, strval, objval
2481 * g_free (strval);
2482 * g_object_unref (objval);
2483 * ]|
2485 void
2486 g_object_get (gpointer _object,
2487 const gchar *first_property_name,
2488 ...)
2490 GObject *object = _object;
2491 va_list var_args;
2493 g_return_if_fail (G_IS_OBJECT (object));
2495 va_start (var_args, first_property_name);
2496 g_object_get_valist (object, first_property_name, var_args);
2497 va_end (var_args);
2501 * g_object_set_property:
2502 * @object: a #GObject
2503 * @property_name: the name of the property to set
2504 * @value: the value
2506 * Sets a property on an object.
2508 void
2509 g_object_set_property (GObject *object,
2510 const gchar *property_name,
2511 const GValue *value)
2513 g_object_setv (object, 1, &property_name, value);
2517 * g_object_get_property:
2518 * @object: a #GObject
2519 * @property_name: the name of the property to get
2520 * @value: return location for the property value
2522 * Gets a property of an object. @value must have been initialized to the
2523 * expected type of the property (or a type to which the expected type can be
2524 * transformed) using g_value_init().
2526 * In general, a copy is made of the property contents and the caller is
2527 * responsible for freeing the memory by calling g_value_unset().
2529 * Note that g_object_get_property() is really intended for language
2530 * bindings, g_object_get() is much more convenient for C programming.
2532 void
2533 g_object_get_property (GObject *object,
2534 const gchar *property_name,
2535 GValue *value)
2537 GParamSpec *pspec;
2539 g_return_if_fail (G_IS_OBJECT (object));
2540 g_return_if_fail (property_name != NULL);
2541 g_return_if_fail (G_IS_VALUE (value));
2543 g_object_ref (object);
2545 pspec = g_param_spec_pool_lookup (pspec_pool,
2546 property_name,
2547 G_OBJECT_TYPE (object),
2548 TRUE);
2550 if (g_object_get_is_valid_property (object, pspec, property_name))
2552 GValue *prop_value, tmp_value = G_VALUE_INIT;
2554 /* auto-conversion of the callers value type
2556 if (G_VALUE_TYPE (value) == pspec->value_type)
2558 g_value_reset (value);
2559 prop_value = value;
2561 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2563 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2564 G_STRFUNC, pspec->name,
2565 g_type_name (pspec->value_type),
2566 G_VALUE_TYPE_NAME (value));
2567 g_object_unref (object);
2568 return;
2570 else
2572 g_value_init (&tmp_value, pspec->value_type);
2573 prop_value = &tmp_value;
2575 object_get_property (object, pspec, prop_value);
2576 if (prop_value != value)
2578 g_value_transform (prop_value, value);
2579 g_value_unset (&tmp_value);
2583 g_object_unref (object);
2587 * g_object_connect: (skip)
2588 * @object: (type GObject.Object): a #GObject
2589 * @signal_spec: the spec for the first signal
2590 * @...: #GCallback for the first signal, followed by data for the
2591 * first signal, followed optionally by more signal
2592 * spec/callback/data triples, followed by %NULL
2594 * A convenience function to connect multiple signals at once.
2596 * The signal specs expected by this function have the form
2597 * "modifier::signal_name", where modifier can be one of the following:
2598 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2599 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2600 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2601 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2602 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2603 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2604 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2605 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2607 * |[<!-- language="C" -->
2608 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2609 * "type", GTK_WINDOW_POPUP,
2610 * "child", menu,
2611 * NULL),
2612 * "signal::event", gtk_menu_window_event, menu,
2613 * "signal::size_request", gtk_menu_window_size_request, menu,
2614 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2615 * NULL);
2616 * ]|
2618 * Returns: (transfer none) (type GObject.Object): @object
2620 gpointer
2621 g_object_connect (gpointer _object,
2622 const gchar *signal_spec,
2623 ...)
2625 GObject *object = _object;
2626 va_list var_args;
2628 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2629 g_return_val_if_fail (object->ref_count > 0, object);
2631 va_start (var_args, signal_spec);
2632 while (signal_spec)
2634 GCallback callback = va_arg (var_args, GCallback);
2635 gpointer data = va_arg (var_args, gpointer);
2637 if (strncmp (signal_spec, "signal::", 8) == 0)
2638 g_signal_connect_data (object, signal_spec + 8,
2639 callback, data, NULL,
2641 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2642 strncmp (signal_spec, "object-signal::", 15) == 0)
2643 g_signal_connect_object (object, signal_spec + 15,
2644 callback, data,
2646 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2647 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2648 g_signal_connect_data (object, signal_spec + 16,
2649 callback, data, NULL,
2650 G_CONNECT_SWAPPED);
2651 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2652 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2653 g_signal_connect_object (object, signal_spec + 23,
2654 callback, data,
2655 G_CONNECT_SWAPPED);
2656 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2657 strncmp (signal_spec, "signal-after::", 14) == 0)
2658 g_signal_connect_data (object, signal_spec + 14,
2659 callback, data, NULL,
2660 G_CONNECT_AFTER);
2661 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2662 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2663 g_signal_connect_object (object, signal_spec + 21,
2664 callback, data,
2665 G_CONNECT_AFTER);
2666 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2667 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2668 g_signal_connect_data (object, signal_spec + 22,
2669 callback, data, NULL,
2670 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2671 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2672 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2673 g_signal_connect_object (object, signal_spec + 29,
2674 callback, data,
2675 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2676 else
2678 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2679 break;
2681 signal_spec = va_arg (var_args, gchar*);
2683 va_end (var_args);
2685 return object;
2689 * g_object_disconnect: (skip)
2690 * @object: (type GObject.Object): a #GObject
2691 * @signal_spec: the spec for the first signal
2692 * @...: #GCallback for the first signal, followed by data for the first signal,
2693 * followed optionally by more signal spec/callback/data triples,
2694 * followed by %NULL
2696 * A convenience function to disconnect multiple signals at once.
2698 * The signal specs expected by this function have the form
2699 * "any_signal", which means to disconnect any signal with matching
2700 * callback and data, or "any_signal::signal_name", which only
2701 * disconnects the signal named "signal_name".
2703 void
2704 g_object_disconnect (gpointer _object,
2705 const gchar *signal_spec,
2706 ...)
2708 GObject *object = _object;
2709 va_list var_args;
2711 g_return_if_fail (G_IS_OBJECT (object));
2712 g_return_if_fail (object->ref_count > 0);
2714 va_start (var_args, signal_spec);
2715 while (signal_spec)
2717 GCallback callback = va_arg (var_args, GCallback);
2718 gpointer data = va_arg (var_args, gpointer);
2719 guint sid = 0, detail = 0, mask = 0;
2721 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2722 strncmp (signal_spec, "any-signal::", 12) == 0)
2724 signal_spec += 12;
2725 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2727 else if (strcmp (signal_spec, "any_signal") == 0 ||
2728 strcmp (signal_spec, "any-signal") == 0)
2730 signal_spec += 10;
2731 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2733 else
2735 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2736 break;
2739 if ((mask & G_SIGNAL_MATCH_ID) &&
2740 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2741 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2742 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2743 sid, detail,
2744 NULL, (gpointer)callback, data))
2745 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2746 signal_spec = va_arg (var_args, gchar*);
2748 va_end (var_args);
2751 typedef struct {
2752 GObject *object;
2753 guint n_weak_refs;
2754 struct {
2755 GWeakNotify notify;
2756 gpointer data;
2757 } weak_refs[1]; /* flexible array */
2758 } WeakRefStack;
2760 static void
2761 weak_refs_notify (gpointer data)
2763 WeakRefStack *wstack = data;
2764 guint i;
2766 for (i = 0; i < wstack->n_weak_refs; i++)
2767 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2768 g_free (wstack);
2772 * g_object_weak_ref: (skip)
2773 * @object: #GObject to reference weakly
2774 * @notify: callback to invoke before the object is freed
2775 * @data: extra data to pass to notify
2777 * Adds a weak reference callback to an object. Weak references are
2778 * used for notification when an object is finalized. They are called
2779 * "weak references" because they allow you to safely hold a pointer
2780 * to an object without calling g_object_ref() (g_object_ref() adds a
2781 * strong reference, that is, forces the object to stay alive).
2783 * Note that the weak references created by this method are not
2784 * thread-safe: they cannot safely be used in one thread if the
2785 * object's last g_object_unref() might happen in another thread.
2786 * Use #GWeakRef if thread-safety is required.
2788 void
2789 g_object_weak_ref (GObject *object,
2790 GWeakNotify notify,
2791 gpointer data)
2793 WeakRefStack *wstack;
2794 guint i;
2796 g_return_if_fail (G_IS_OBJECT (object));
2797 g_return_if_fail (notify != NULL);
2798 g_return_if_fail (object->ref_count >= 1);
2800 G_LOCK (weak_refs_mutex);
2801 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2802 if (wstack)
2804 i = wstack->n_weak_refs++;
2805 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2807 else
2809 wstack = g_renew (WeakRefStack, NULL, 1);
2810 wstack->object = object;
2811 wstack->n_weak_refs = 1;
2812 i = 0;
2814 wstack->weak_refs[i].notify = notify;
2815 wstack->weak_refs[i].data = data;
2816 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2817 G_UNLOCK (weak_refs_mutex);
2821 * g_object_weak_unref: (skip)
2822 * @object: #GObject to remove a weak reference from
2823 * @notify: callback to search for
2824 * @data: data to search for
2826 * Removes a weak reference callback to an object.
2828 void
2829 g_object_weak_unref (GObject *object,
2830 GWeakNotify notify,
2831 gpointer data)
2833 WeakRefStack *wstack;
2834 gboolean found_one = FALSE;
2836 g_return_if_fail (G_IS_OBJECT (object));
2837 g_return_if_fail (notify != NULL);
2839 G_LOCK (weak_refs_mutex);
2840 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2841 if (wstack)
2843 guint i;
2845 for (i = 0; i < wstack->n_weak_refs; i++)
2846 if (wstack->weak_refs[i].notify == notify &&
2847 wstack->weak_refs[i].data == data)
2849 found_one = TRUE;
2850 wstack->n_weak_refs -= 1;
2851 if (i != wstack->n_weak_refs)
2852 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2854 break;
2857 G_UNLOCK (weak_refs_mutex);
2858 if (!found_one)
2859 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2863 * g_object_add_weak_pointer: (skip)
2864 * @object: The object that should be weak referenced.
2865 * @weak_pointer_location: (inout) (not optional): The memory address
2866 * of a pointer.
2868 * Adds a weak reference from weak_pointer to @object to indicate that
2869 * the pointer located at @weak_pointer_location is only valid during
2870 * the lifetime of @object. When the @object is finalized,
2871 * @weak_pointer will be set to %NULL.
2873 * Note that as with g_object_weak_ref(), the weak references created by
2874 * this method are not thread-safe: they cannot safely be used in one
2875 * thread if the object's last g_object_unref() might happen in another
2876 * thread. Use #GWeakRef if thread-safety is required.
2878 void
2879 g_object_add_weak_pointer (GObject *object,
2880 gpointer *weak_pointer_location)
2882 g_return_if_fail (G_IS_OBJECT (object));
2883 g_return_if_fail (weak_pointer_location != NULL);
2885 g_object_weak_ref (object,
2886 (GWeakNotify) g_nullify_pointer,
2887 weak_pointer_location);
2891 * g_object_remove_weak_pointer: (skip)
2892 * @object: The object that is weak referenced.
2893 * @weak_pointer_location: (inout) (not optional): The memory address
2894 * of a pointer.
2896 * Removes a weak reference from @object that was previously added
2897 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2898 * to match the one used with g_object_add_weak_pointer().
2900 void
2901 g_object_remove_weak_pointer (GObject *object,
2902 gpointer *weak_pointer_location)
2904 g_return_if_fail (G_IS_OBJECT (object));
2905 g_return_if_fail (weak_pointer_location != NULL);
2907 g_object_weak_unref (object,
2908 (GWeakNotify) g_nullify_pointer,
2909 weak_pointer_location);
2912 static guint
2913 object_floating_flag_handler (GObject *object,
2914 gint job)
2916 switch (job)
2918 gpointer oldvalue;
2919 case +1: /* force floating if possible */
2921 oldvalue = g_atomic_pointer_get (&object->qdata);
2922 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2923 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2924 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2925 case -1: /* sink if possible */
2927 oldvalue = g_atomic_pointer_get (&object->qdata);
2928 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2929 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2930 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2931 default: /* check floating */
2932 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2937 * g_object_is_floating:
2938 * @object: (type GObject.Object): a #GObject
2940 * Checks whether @object has a [floating][floating-ref] reference.
2942 * Since: 2.10
2944 * Returns: %TRUE if @object has a floating reference
2946 gboolean
2947 g_object_is_floating (gpointer _object)
2949 GObject *object = _object;
2950 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2951 return floating_flag_handler (object, 0);
2955 * g_object_ref_sink:
2956 * @object: (type GObject.Object): a #GObject
2958 * Increase the reference count of @object, and possibly remove the
2959 * [floating][floating-ref] reference, if @object has a floating reference.
2961 * In other words, if the object is floating, then this call "assumes
2962 * ownership" of the floating reference, converting it to a normal
2963 * reference by clearing the floating flag while leaving the reference
2964 * count unchanged. If the object is not floating, then this call
2965 * adds a new normal reference increasing the reference count by one.
2967 * Since: 2.10
2969 * Returns: (type GObject.Object) (transfer none): @object
2971 gpointer
2972 g_object_ref_sink (gpointer _object)
2974 GObject *object = _object;
2975 gboolean was_floating;
2976 g_return_val_if_fail (G_IS_OBJECT (object), object);
2977 g_return_val_if_fail (object->ref_count >= 1, object);
2978 g_object_ref (object);
2979 was_floating = floating_flag_handler (object, -1);
2980 if (was_floating)
2981 g_object_unref (object);
2982 return object;
2986 * g_object_force_floating:
2987 * @object: a #GObject
2989 * This function is intended for #GObject implementations to re-enforce
2990 * a [floating][floating-ref] object reference. Doing this is seldom
2991 * required: all #GInitiallyUnowneds are created with a floating reference
2992 * which usually just needs to be sunken by calling g_object_ref_sink().
2994 * Since: 2.10
2996 void
2997 g_object_force_floating (GObject *object)
2999 g_return_if_fail (G_IS_OBJECT (object));
3000 g_return_if_fail (object->ref_count >= 1);
3002 floating_flag_handler (object, +1);
3005 typedef struct {
3006 GObject *object;
3007 guint n_toggle_refs;
3008 struct {
3009 GToggleNotify notify;
3010 gpointer data;
3011 } toggle_refs[1]; /* flexible array */
3012 } ToggleRefStack;
3014 static void
3015 toggle_refs_notify (GObject *object,
3016 gboolean is_last_ref)
3018 ToggleRefStack tstack, *tstackptr;
3020 G_LOCK (toggle_refs_mutex);
3021 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3022 tstack = *tstackptr;
3023 G_UNLOCK (toggle_refs_mutex);
3025 /* Reentrancy here is not as tricky as it seems, because a toggle reference
3026 * will only be notified when there is exactly one of them.
3028 g_assert (tstack.n_toggle_refs == 1);
3029 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
3033 * g_object_add_toggle_ref: (skip)
3034 * @object: a #GObject
3035 * @notify: a function to call when this reference is the
3036 * last reference to the object, or is no longer
3037 * the last reference.
3038 * @data: data to pass to @notify
3040 * Increases the reference count of the object by one and sets a
3041 * callback to be called when all other references to the object are
3042 * dropped, or when this is already the last reference to the object
3043 * and another reference is established.
3045 * This functionality is intended for binding @object to a proxy
3046 * object managed by another memory manager. This is done with two
3047 * paired references: the strong reference added by
3048 * g_object_add_toggle_ref() and a reverse reference to the proxy
3049 * object which is either a strong reference or weak reference.
3051 * The setup is that when there are no other references to @object,
3052 * only a weak reference is held in the reverse direction from @object
3053 * to the proxy object, but when there are other references held to
3054 * @object, a strong reference is held. The @notify callback is called
3055 * when the reference from @object to the proxy object should be
3056 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
3057 * (@is_last_ref false).
3059 * Since a (normal) reference must be held to the object before
3060 * calling g_object_add_toggle_ref(), the initial state of the reverse
3061 * link is always strong.
3063 * Multiple toggle references may be added to the same gobject,
3064 * however if there are multiple toggle references to an object, none
3065 * of them will ever be notified until all but one are removed. For
3066 * this reason, you should only ever use a toggle reference if there
3067 * is important state in the proxy object.
3069 * Since: 2.8
3071 void
3072 g_object_add_toggle_ref (GObject *object,
3073 GToggleNotify notify,
3074 gpointer data)
3076 ToggleRefStack *tstack;
3077 guint i;
3079 g_return_if_fail (G_IS_OBJECT (object));
3080 g_return_if_fail (notify != NULL);
3081 g_return_if_fail (object->ref_count >= 1);
3083 g_object_ref (object);
3085 G_LOCK (toggle_refs_mutex);
3086 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
3087 if (tstack)
3089 i = tstack->n_toggle_refs++;
3090 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
3091 * in tstate->toggle_refs */
3092 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
3094 else
3096 tstack = g_renew (ToggleRefStack, NULL, 1);
3097 tstack->object = object;
3098 tstack->n_toggle_refs = 1;
3099 i = 0;
3102 /* Set a flag for fast lookup after adding the first toggle reference */
3103 if (tstack->n_toggle_refs == 1)
3104 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3106 tstack->toggle_refs[i].notify = notify;
3107 tstack->toggle_refs[i].data = data;
3108 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
3109 (GDestroyNotify)g_free);
3110 G_UNLOCK (toggle_refs_mutex);
3114 * g_object_remove_toggle_ref: (skip)
3115 * @object: a #GObject
3116 * @notify: a function to call when this reference is the
3117 * last reference to the object, or is no longer
3118 * the last reference.
3119 * @data: data to pass to @notify
3121 * Removes a reference added with g_object_add_toggle_ref(). The
3122 * reference count of the object is decreased by one.
3124 * Since: 2.8
3126 void
3127 g_object_remove_toggle_ref (GObject *object,
3128 GToggleNotify notify,
3129 gpointer data)
3131 ToggleRefStack *tstack;
3132 gboolean found_one = FALSE;
3134 g_return_if_fail (G_IS_OBJECT (object));
3135 g_return_if_fail (notify != NULL);
3137 G_LOCK (toggle_refs_mutex);
3138 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3139 if (tstack)
3141 guint i;
3143 for (i = 0; i < tstack->n_toggle_refs; i++)
3144 if (tstack->toggle_refs[i].notify == notify &&
3145 tstack->toggle_refs[i].data == data)
3147 found_one = TRUE;
3148 tstack->n_toggle_refs -= 1;
3149 if (i != tstack->n_toggle_refs)
3150 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3152 if (tstack->n_toggle_refs == 0)
3153 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3155 break;
3158 G_UNLOCK (toggle_refs_mutex);
3160 if (found_one)
3161 g_object_unref (object);
3162 else
3163 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3167 * g_object_ref:
3168 * @object: (type GObject.Object): a #GObject
3170 * Increases the reference count of @object.
3172 * Returns: (type GObject.Object) (transfer none): the same @object
3174 gpointer
3175 g_object_ref (gpointer _object)
3177 GObject *object = _object;
3178 gint old_val;
3180 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3181 g_return_val_if_fail (object->ref_count > 0, NULL);
3183 old_val = g_atomic_int_add (&object->ref_count, 1);
3185 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3186 toggle_refs_notify (object, FALSE);
3188 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3190 return object;
3194 * g_object_unref:
3195 * @object: (type GObject.Object): a #GObject
3197 * Decreases the reference count of @object. When its reference count
3198 * drops to 0, the object is finalized (i.e. its memory is freed).
3200 * If the pointer to the #GObject may be reused in future (for example, if it is
3201 * an instance variable of another object), it is recommended to clear the
3202 * pointer to %NULL rather than retain a dangling pointer to a potentially
3203 * invalid #GObject instance. Use g_clear_object() for this.
3205 void
3206 g_object_unref (gpointer _object)
3208 GObject *object = _object;
3209 gint old_ref;
3211 g_return_if_fail (G_IS_OBJECT (object));
3212 g_return_if_fail (object->ref_count > 0);
3214 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3215 retry_atomic_decrement1:
3216 old_ref = g_atomic_int_get (&object->ref_count);
3217 if (old_ref > 1)
3219 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3220 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3222 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3223 goto retry_atomic_decrement1;
3225 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3227 /* if we went from 2->1 we need to notify toggle refs if any */
3228 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3229 toggle_refs_notify (object, TRUE);
3231 else
3233 GSList **weak_locations;
3235 /* The only way that this object can live at this point is if
3236 * there are outstanding weak references already established
3237 * before we got here.
3239 * If there were not already weak references then no more can be
3240 * established at this time, because the other thread would have
3241 * to hold a strong ref in order to call
3242 * g_object_add_weak_pointer() and then we wouldn't be here.
3244 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3246 if (weak_locations != NULL)
3248 g_rw_lock_writer_lock (&weak_locations_lock);
3250 /* It is possible that one of the weak references beat us to
3251 * the lock. Make sure the refcount is still what we expected
3252 * it to be.
3254 old_ref = g_atomic_int_get (&object->ref_count);
3255 if (old_ref != 1)
3257 g_rw_lock_writer_unlock (&weak_locations_lock);
3258 goto retry_atomic_decrement1;
3261 /* We got the lock first, so the object will definitely die
3262 * now. Clear out all the weak references.
3264 while (*weak_locations)
3266 GWeakRef *weak_ref_location = (*weak_locations)->data;
3268 weak_ref_location->priv.p = NULL;
3269 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3272 g_rw_lock_writer_unlock (&weak_locations_lock);
3275 /* we are about to remove the last reference */
3276 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3277 G_OBJECT_GET_CLASS (object)->dispose (object);
3278 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3280 /* may have been re-referenced meanwhile */
3281 retry_atomic_decrement2:
3282 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3283 if (old_ref > 1)
3285 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3286 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3288 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3289 goto retry_atomic_decrement2;
3291 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3293 /* if we went from 2->1 we need to notify toggle refs if any */
3294 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3295 toggle_refs_notify (object, TRUE);
3297 return;
3300 /* we are still in the process of taking away the last ref */
3301 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3302 g_signal_handlers_destroy (object);
3303 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3305 /* decrement the last reference */
3306 old_ref = g_atomic_int_add (&object->ref_count, -1);
3308 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3310 /* may have been re-referenced meanwhile */
3311 if (G_LIKELY (old_ref == 1))
3313 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3314 G_OBJECT_GET_CLASS (object)->finalize (object);
3316 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3318 GOBJECT_IF_DEBUG (OBJECTS,
3320 /* catch objects not chaining finalize handlers */
3321 G_LOCK (debug_objects);
3322 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3323 G_UNLOCK (debug_objects);
3325 g_type_free_instance ((GTypeInstance*) object);
3331 * g_clear_object: (skip)
3332 * @object_ptr: a pointer to a #GObject reference
3334 * Clears a reference to a #GObject.
3336 * @object_ptr must not be %NULL.
3338 * If the reference is %NULL then this function does nothing.
3339 * Otherwise, the reference count of the object is decreased and the
3340 * pointer is set to %NULL.
3342 * A macro is also included that allows this function to be used without
3343 * pointer casts.
3345 * Since: 2.28
3347 #undef g_clear_object
3348 void
3349 g_clear_object (volatile GObject **object_ptr)
3351 g_clear_pointer (object_ptr, g_object_unref);
3355 * g_object_get_qdata:
3356 * @object: The GObject to get a stored user data pointer from
3357 * @quark: A #GQuark, naming the user data pointer
3359 * This function gets back user data pointers stored via
3360 * g_object_set_qdata().
3362 * Returns: (transfer none): The user data pointer set, or %NULL
3364 gpointer
3365 g_object_get_qdata (GObject *object,
3366 GQuark quark)
3368 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3370 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3374 * g_object_set_qdata: (skip)
3375 * @object: The GObject to set store a user data pointer
3376 * @quark: A #GQuark, naming the user data pointer
3377 * @data: An opaque user data pointer
3379 * This sets an opaque, named pointer on an object.
3380 * The name is specified through a #GQuark (retrived e.g. via
3381 * g_quark_from_static_string()), and the pointer
3382 * can be gotten back from the @object with g_object_get_qdata()
3383 * until the @object is finalized.
3384 * Setting a previously set user data pointer, overrides (frees)
3385 * the old pointer set, using #NULL as pointer essentially
3386 * removes the data stored.
3388 void
3389 g_object_set_qdata (GObject *object,
3390 GQuark quark,
3391 gpointer data)
3393 g_return_if_fail (G_IS_OBJECT (object));
3394 g_return_if_fail (quark > 0);
3396 g_datalist_id_set_data (&object->qdata, quark, data);
3400 * g_object_dup_qdata:
3401 * @object: the #GObject to store user data on
3402 * @quark: a #GQuark, naming the user data pointer
3403 * @dup_func: (nullable): function to dup the value
3404 * @user_data: (nullable): passed as user_data to @dup_func
3406 * This is a variant of g_object_get_qdata() which returns
3407 * a 'duplicate' of the value. @dup_func defines the
3408 * meaning of 'duplicate' in this context, it could e.g.
3409 * take a reference on a ref-counted object.
3411 * If the @quark is not set on the object then @dup_func
3412 * will be called with a %NULL argument.
3414 * Note that @dup_func is called while user data of @object
3415 * is locked.
3417 * This function can be useful to avoid races when multiple
3418 * threads are using object data on the same key on the same
3419 * object.
3421 * Returns: the result of calling @dup_func on the value
3422 * associated with @quark on @object, or %NULL if not set.
3423 * If @dup_func is %NULL, the value is returned
3424 * unmodified.
3426 * Since: 2.34
3428 gpointer
3429 g_object_dup_qdata (GObject *object,
3430 GQuark quark,
3431 GDuplicateFunc dup_func,
3432 gpointer user_data)
3434 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3435 g_return_val_if_fail (quark > 0, NULL);
3437 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3441 * g_object_replace_qdata:
3442 * @object: the #GObject to store user data on
3443 * @quark: a #GQuark, naming the user data pointer
3444 * @oldval: (nullable): the old value to compare against
3445 * @newval: (nullable): the new value
3446 * @destroy: (nullable): a destroy notify for the new value
3447 * @old_destroy: (nullable): destroy notify for the existing value
3449 * Compares the user data for the key @quark on @object with
3450 * @oldval, and if they are the same, replaces @oldval with
3451 * @newval.
3453 * This is like a typical atomic compare-and-exchange
3454 * operation, for user data on an object.
3456 * If the previous value was replaced then ownership of the
3457 * old value (@oldval) is passed to the caller, including
3458 * the registered destroy notify for it (passed out in @old_destroy).
3459 * Its up to the caller to free this as he wishes, which may
3460 * or may not include using @old_destroy as sometimes replacement
3461 * should not destroy the object in the normal way.
3463 * Returns: %TRUE if the existing value for @quark was replaced
3464 * by @newval, %FALSE otherwise.
3466 * Since: 2.34
3468 gboolean
3469 g_object_replace_qdata (GObject *object,
3470 GQuark quark,
3471 gpointer oldval,
3472 gpointer newval,
3473 GDestroyNotify destroy,
3474 GDestroyNotify *old_destroy)
3476 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3477 g_return_val_if_fail (quark > 0, FALSE);
3479 return g_datalist_id_replace_data (&object->qdata, quark,
3480 oldval, newval, destroy,
3481 old_destroy);
3485 * g_object_set_qdata_full: (skip)
3486 * @object: The GObject to set store a user data pointer
3487 * @quark: A #GQuark, naming the user data pointer
3488 * @data: An opaque user data pointer
3489 * @destroy: Function to invoke with @data as argument, when @data
3490 * needs to be freed
3492 * This function works like g_object_set_qdata(), but in addition,
3493 * a void (*destroy) (gpointer) function may be specified which is
3494 * called with @data as argument when the @object is finalized, or
3495 * the data is being overwritten by a call to g_object_set_qdata()
3496 * with the same @quark.
3498 void
3499 g_object_set_qdata_full (GObject *object,
3500 GQuark quark,
3501 gpointer data,
3502 GDestroyNotify destroy)
3504 g_return_if_fail (G_IS_OBJECT (object));
3505 g_return_if_fail (quark > 0);
3507 g_datalist_id_set_data_full (&object->qdata, quark, data,
3508 data ? destroy : (GDestroyNotify) NULL);
3512 * g_object_steal_qdata:
3513 * @object: The GObject to get a stored user data pointer from
3514 * @quark: A #GQuark, naming the user data pointer
3516 * This function gets back user data pointers stored via
3517 * g_object_set_qdata() and removes the @data from object
3518 * without invoking its destroy() function (if any was
3519 * set).
3520 * Usually, calling this function is only required to update
3521 * user data pointers with a destroy notifier, for example:
3522 * |[<!-- language="C" -->
3523 * void
3524 * object_add_to_user_list (GObject *object,
3525 * const gchar *new_string)
3527 * // the quark, naming the object data
3528 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3529 * // retrive the old string list
3530 * GList *list = g_object_steal_qdata (object, quark_string_list);
3532 * // prepend new string
3533 * list = g_list_prepend (list, g_strdup (new_string));
3534 * // this changed 'list', so we need to set it again
3535 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3537 * static void
3538 * free_string_list (gpointer data)
3540 * GList *node, *list = data;
3542 * for (node = list; node; node = node->next)
3543 * g_free (node->data);
3544 * g_list_free (list);
3546 * ]|
3547 * Using g_object_get_qdata() in the above example, instead of
3548 * g_object_steal_qdata() would have left the destroy function set,
3549 * and thus the partial string list would have been freed upon
3550 * g_object_set_qdata_full().
3552 * Returns: (transfer full): The user data pointer set, or %NULL
3554 gpointer
3555 g_object_steal_qdata (GObject *object,
3556 GQuark quark)
3558 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3559 g_return_val_if_fail (quark > 0, NULL);
3561 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3565 * g_object_get_data:
3566 * @object: #GObject containing the associations
3567 * @key: name of the key for that association
3569 * Gets a named field from the objects table of associations (see g_object_set_data()).
3571 * Returns: (transfer none): the data if found, or %NULL if no such data exists.
3573 gpointer
3574 g_object_get_data (GObject *object,
3575 const gchar *key)
3577 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3578 g_return_val_if_fail (key != NULL, NULL);
3580 return g_datalist_get_data (&object->qdata, key);
3584 * g_object_set_data:
3585 * @object: #GObject containing the associations.
3586 * @key: name of the key
3587 * @data: data to associate with that key
3589 * Each object carries around a table of associations from
3590 * strings to pointers. This function lets you set an association.
3592 * If the object already had an association with that name,
3593 * the old association will be destroyed.
3595 void
3596 g_object_set_data (GObject *object,
3597 const gchar *key,
3598 gpointer data)
3600 g_return_if_fail (G_IS_OBJECT (object));
3601 g_return_if_fail (key != NULL);
3603 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3607 * g_object_dup_data:
3608 * @object: the #GObject to store user data on
3609 * @key: a string, naming the user data pointer
3610 * @dup_func: (nullable): function to dup the value
3611 * @user_data: (nullable): passed as user_data to @dup_func
3613 * This is a variant of g_object_get_data() which returns
3614 * a 'duplicate' of the value. @dup_func defines the
3615 * meaning of 'duplicate' in this context, it could e.g.
3616 * take a reference on a ref-counted object.
3618 * If the @key is not set on the object then @dup_func
3619 * will be called with a %NULL argument.
3621 * Note that @dup_func is called while user data of @object
3622 * is locked.
3624 * This function can be useful to avoid races when multiple
3625 * threads are using object data on the same key on the same
3626 * object.
3628 * Returns: the result of calling @dup_func on the value
3629 * associated with @key on @object, or %NULL if not set.
3630 * If @dup_func is %NULL, the value is returned
3631 * unmodified.
3633 * Since: 2.34
3635 gpointer
3636 g_object_dup_data (GObject *object,
3637 const gchar *key,
3638 GDuplicateFunc dup_func,
3639 gpointer user_data)
3641 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3642 g_return_val_if_fail (key != NULL, NULL);
3644 return g_datalist_id_dup_data (&object->qdata,
3645 g_quark_from_string (key),
3646 dup_func, user_data);
3650 * g_object_replace_data:
3651 * @object: the #GObject to store user data on
3652 * @key: a string, naming the user data pointer
3653 * @oldval: (nullable): the old value to compare against
3654 * @newval: (nullable): the new value
3655 * @destroy: (nullable): a destroy notify for the new value
3656 * @old_destroy: (nullable): destroy notify for the existing value
3658 * Compares the user data for the key @key on @object with
3659 * @oldval, and if they are the same, replaces @oldval with
3660 * @newval.
3662 * This is like a typical atomic compare-and-exchange
3663 * operation, for user data on an object.
3665 * If the previous value was replaced then ownership of the
3666 * old value (@oldval) is passed to the caller, including
3667 * the registered destroy notify for it (passed out in @old_destroy).
3668 * Its up to the caller to free this as he wishes, which may
3669 * or may not include using @old_destroy as sometimes replacement
3670 * should not destroy the object in the normal way.
3672 * Returns: %TRUE if the existing value for @key was replaced
3673 * by @newval, %FALSE otherwise.
3675 * Since: 2.34
3677 gboolean
3678 g_object_replace_data (GObject *object,
3679 const gchar *key,
3680 gpointer oldval,
3681 gpointer newval,
3682 GDestroyNotify destroy,
3683 GDestroyNotify *old_destroy)
3685 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3686 g_return_val_if_fail (key != NULL, FALSE);
3688 return g_datalist_id_replace_data (&object->qdata,
3689 g_quark_from_string (key),
3690 oldval, newval, destroy,
3691 old_destroy);
3695 * g_object_set_data_full: (skip)
3696 * @object: #GObject containing the associations
3697 * @key: name of the key
3698 * @data: data to associate with that key
3699 * @destroy: function to call when the association is destroyed
3701 * Like g_object_set_data() except it adds notification
3702 * for when the association is destroyed, either by setting it
3703 * to a different value or when the object is destroyed.
3705 * Note that the @destroy callback is not called if @data is %NULL.
3707 void
3708 g_object_set_data_full (GObject *object,
3709 const gchar *key,
3710 gpointer data,
3711 GDestroyNotify destroy)
3713 g_return_if_fail (G_IS_OBJECT (object));
3714 g_return_if_fail (key != NULL);
3716 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3717 data ? destroy : (GDestroyNotify) NULL);
3721 * g_object_steal_data:
3722 * @object: #GObject containing the associations
3723 * @key: name of the key
3725 * Remove a specified datum from the object's data associations,
3726 * without invoking the association's destroy handler.
3728 * Returns: (transfer full): the data if found, or %NULL if no such data exists.
3730 gpointer
3731 g_object_steal_data (GObject *object,
3732 const gchar *key)
3734 GQuark quark;
3736 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3737 g_return_val_if_fail (key != NULL, NULL);
3739 quark = g_quark_try_string (key);
3741 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3744 static void
3745 g_value_object_init (GValue *value)
3747 value->data[0].v_pointer = NULL;
3750 static void
3751 g_value_object_free_value (GValue *value)
3753 if (value->data[0].v_pointer)
3754 g_object_unref (value->data[0].v_pointer);
3757 static void
3758 g_value_object_copy_value (const GValue *src_value,
3759 GValue *dest_value)
3761 if (src_value->data[0].v_pointer)
3762 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3763 else
3764 dest_value->data[0].v_pointer = NULL;
3767 static void
3768 g_value_object_transform_value (const GValue *src_value,
3769 GValue *dest_value)
3771 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)))
3772 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3773 else
3774 dest_value->data[0].v_pointer = NULL;
3777 static gpointer
3778 g_value_object_peek_pointer (const GValue *value)
3780 return value->data[0].v_pointer;
3783 static gchar*
3784 g_value_object_collect_value (GValue *value,
3785 guint n_collect_values,
3786 GTypeCValue *collect_values,
3787 guint collect_flags)
3789 if (collect_values[0].v_pointer)
3791 GObject *object = collect_values[0].v_pointer;
3793 if (object->g_type_instance.g_class == NULL)
3794 return g_strconcat ("invalid unclassed object pointer for value type '",
3795 G_VALUE_TYPE_NAME (value),
3796 "'",
3797 NULL);
3798 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3799 return g_strconcat ("invalid object type '",
3800 G_OBJECT_TYPE_NAME (object),
3801 "' for value type '",
3802 G_VALUE_TYPE_NAME (value),
3803 "'",
3804 NULL);
3805 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3806 value->data[0].v_pointer = g_object_ref (object);
3808 else
3809 value->data[0].v_pointer = NULL;
3811 return NULL;
3814 static gchar*
3815 g_value_object_lcopy_value (const GValue *value,
3816 guint n_collect_values,
3817 GTypeCValue *collect_values,
3818 guint collect_flags)
3820 GObject **object_p = collect_values[0].v_pointer;
3822 if (!object_p)
3823 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3825 if (!value->data[0].v_pointer)
3826 *object_p = NULL;
3827 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3828 *object_p = value->data[0].v_pointer;
3829 else
3830 *object_p = g_object_ref (value->data[0].v_pointer);
3832 return NULL;
3836 * g_value_set_object:
3837 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3838 * @v_object: (type GObject.Object) (nullable): object value to be set
3840 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3842 * g_value_set_object() increases the reference count of @v_object
3843 * (the #GValue holds a reference to @v_object). If you do not wish
3844 * to increase the reference count of the object (i.e. you wish to
3845 * pass your current reference to the #GValue because you no longer
3846 * need it), use g_value_take_object() instead.
3848 * It is important that your #GValue holds a reference to @v_object (either its
3849 * own, or one it has taken) to ensure that the object won't be destroyed while
3850 * the #GValue still exists).
3852 void
3853 g_value_set_object (GValue *value,
3854 gpointer v_object)
3856 GObject *old;
3858 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3860 old = value->data[0].v_pointer;
3862 if (v_object)
3864 g_return_if_fail (G_IS_OBJECT (v_object));
3865 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3867 value->data[0].v_pointer = v_object;
3868 g_object_ref (value->data[0].v_pointer);
3870 else
3871 value->data[0].v_pointer = NULL;
3873 if (old)
3874 g_object_unref (old);
3878 * g_value_set_object_take_ownership: (skip)
3879 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3880 * @v_object: (nullable): object value to be set
3882 * This is an internal function introduced mainly for C marshallers.
3884 * Deprecated: 2.4: Use g_value_take_object() instead.
3886 void
3887 g_value_set_object_take_ownership (GValue *value,
3888 gpointer v_object)
3890 g_value_take_object (value, v_object);
3894 * g_value_take_object: (skip)
3895 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3896 * @v_object: (nullable): object value to be set
3898 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3899 * and takes over the ownership of the callers reference to @v_object;
3900 * the caller doesn't have to unref it any more (i.e. the reference
3901 * count of the object is not increased).
3903 * If you want the #GValue to hold its own reference to @v_object, use
3904 * g_value_set_object() instead.
3906 * Since: 2.4
3908 void
3909 g_value_take_object (GValue *value,
3910 gpointer v_object)
3912 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3914 if (value->data[0].v_pointer)
3916 g_object_unref (value->data[0].v_pointer);
3917 value->data[0].v_pointer = NULL;
3920 if (v_object)
3922 g_return_if_fail (G_IS_OBJECT (v_object));
3923 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3925 value->data[0].v_pointer = v_object; /* we take over the reference count */
3930 * g_value_get_object:
3931 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3933 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3935 * Returns: (type GObject.Object) (transfer none): object contents of @value
3937 gpointer
3938 g_value_get_object (const GValue *value)
3940 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3942 return value->data[0].v_pointer;
3946 * g_value_dup_object:
3947 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3949 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3950 * its reference count. If the contents of the #GValue are %NULL, then
3951 * %NULL will be returned.
3953 * Returns: (type GObject.Object) (transfer full): object content of @value,
3954 * should be unreferenced when no longer needed.
3956 gpointer
3957 g_value_dup_object (const GValue *value)
3959 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3961 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3965 * g_signal_connect_object: (skip)
3966 * @instance: (type GObject.TypeInstance): the instance to connect to.
3967 * @detailed_signal: a string of the form "signal-name::detail".
3968 * @c_handler: the #GCallback to connect.
3969 * @gobject: (type GObject.Object) (nullable): the object to pass as data
3970 * to @c_handler.
3971 * @connect_flags: a combination of #GConnectFlags.
3973 * This is similar to g_signal_connect_data(), but uses a closure which
3974 * ensures that the @gobject stays alive during the call to @c_handler
3975 * by temporarily adding a reference count to @gobject.
3977 * When the @gobject is destroyed the signal handler will be automatically
3978 * disconnected. Note that this is not currently threadsafe (ie:
3979 * emitting a signal while @gobject is being destroyed in another thread
3980 * is not safe).
3982 * Returns: the handler id.
3984 gulong
3985 g_signal_connect_object (gpointer instance,
3986 const gchar *detailed_signal,
3987 GCallback c_handler,
3988 gpointer gobject,
3989 GConnectFlags connect_flags)
3991 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3992 g_return_val_if_fail (detailed_signal != NULL, 0);
3993 g_return_val_if_fail (c_handler != NULL, 0);
3995 if (gobject)
3997 GClosure *closure;
3999 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
4001 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
4003 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
4005 else
4006 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
4009 typedef struct {
4010 GObject *object;
4011 guint n_closures;
4012 GClosure *closures[1]; /* flexible array */
4013 } CArray;
4014 /* don't change this structure without supplying an accessor for
4015 * watched closures, e.g.:
4016 * GSList* g_object_list_watched_closures (GObject *object)
4018 * CArray *carray;
4019 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4020 * carray = g_object_get_data (object, "GObject-closure-array");
4021 * if (carray)
4023 * GSList *slist = NULL;
4024 * guint i;
4025 * for (i = 0; i < carray->n_closures; i++)
4026 * slist = g_slist_prepend (slist, carray->closures[i]);
4027 * return slist;
4029 * return NULL;
4033 static void
4034 object_remove_closure (gpointer data,
4035 GClosure *closure)
4037 GObject *object = data;
4038 CArray *carray;
4039 guint i;
4041 G_LOCK (closure_array_mutex);
4042 carray = g_object_get_qdata (object, quark_closure_array);
4043 for (i = 0; i < carray->n_closures; i++)
4044 if (carray->closures[i] == closure)
4046 carray->n_closures--;
4047 if (i < carray->n_closures)
4048 carray->closures[i] = carray->closures[carray->n_closures];
4049 G_UNLOCK (closure_array_mutex);
4050 return;
4052 G_UNLOCK (closure_array_mutex);
4053 g_assert_not_reached ();
4056 static void
4057 destroy_closure_array (gpointer data)
4059 CArray *carray = data;
4060 GObject *object = carray->object;
4061 guint i, n = carray->n_closures;
4063 for (i = 0; i < n; i++)
4065 GClosure *closure = carray->closures[i];
4067 /* removing object_remove_closure() upfront is probably faster than
4068 * letting it fiddle with quark_closure_array which is empty anyways
4070 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
4071 g_closure_invalidate (closure);
4073 g_free (carray);
4077 * g_object_watch_closure:
4078 * @object: GObject restricting lifetime of @closure
4079 * @closure: GClosure to watch
4081 * This function essentially limits the life time of the @closure to
4082 * the life time of the object. That is, when the object is finalized,
4083 * the @closure is invalidated by calling g_closure_invalidate() on
4084 * it, in order to prevent invocations of the closure with a finalized
4085 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
4086 * added as marshal guards to the @closure, to ensure that an extra
4087 * reference count is held on @object during invocation of the
4088 * @closure. Usually, this function will be called on closures that
4089 * use this @object as closure data.
4091 void
4092 g_object_watch_closure (GObject *object,
4093 GClosure *closure)
4095 CArray *carray;
4096 guint i;
4098 g_return_if_fail (G_IS_OBJECT (object));
4099 g_return_if_fail (closure != NULL);
4100 g_return_if_fail (closure->is_invalid == FALSE);
4101 g_return_if_fail (closure->in_marshal == FALSE);
4102 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
4104 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
4105 g_closure_add_marshal_guards (closure,
4106 object, (GClosureNotify) g_object_ref,
4107 object, (GClosureNotify) g_object_unref);
4108 G_LOCK (closure_array_mutex);
4109 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
4110 if (!carray)
4112 carray = g_renew (CArray, NULL, 1);
4113 carray->object = object;
4114 carray->n_closures = 1;
4115 i = 0;
4117 else
4119 i = carray->n_closures++;
4120 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4122 carray->closures[i] = closure;
4123 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4124 G_UNLOCK (closure_array_mutex);
4128 * g_closure_new_object:
4129 * @sizeof_closure: the size of the structure to allocate, must be at least
4130 * `sizeof (GClosure)`
4131 * @object: a #GObject pointer to store in the @data field of the newly
4132 * allocated #GClosure
4134 * A variant of g_closure_new_simple() which stores @object in the
4135 * @data field of the closure and calls g_object_watch_closure() on
4136 * @object and the created closure. This function is mainly useful
4137 * when implementing new types of closures.
4139 * Returns: (transfer full): a newly allocated #GClosure
4141 GClosure*
4142 g_closure_new_object (guint sizeof_closure,
4143 GObject *object)
4145 GClosure *closure;
4147 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4148 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4150 closure = g_closure_new_simple (sizeof_closure, object);
4151 g_object_watch_closure (object, closure);
4153 return closure;
4157 * g_cclosure_new_object: (skip)
4158 * @callback_func: the function to invoke
4159 * @object: a #GObject pointer to pass to @callback_func
4161 * A variant of g_cclosure_new() which uses @object as @user_data and
4162 * calls g_object_watch_closure() on @object and the created
4163 * closure. This function is useful when you have a callback closely
4164 * associated with a #GObject, and want the callback to no longer run
4165 * after the object is is freed.
4167 * Returns: a new #GCClosure
4169 GClosure*
4170 g_cclosure_new_object (GCallback callback_func,
4171 GObject *object)
4173 GClosure *closure;
4175 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4176 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4177 g_return_val_if_fail (callback_func != NULL, NULL);
4179 closure = g_cclosure_new (callback_func, object, NULL);
4180 g_object_watch_closure (object, closure);
4182 return closure;
4186 * g_cclosure_new_object_swap: (skip)
4187 * @callback_func: the function to invoke
4188 * @object: a #GObject pointer to pass to @callback_func
4190 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4191 * and calls g_object_watch_closure() on @object and the created
4192 * closure. This function is useful when you have a callback closely
4193 * associated with a #GObject, and want the callback to no longer run
4194 * after the object is is freed.
4196 * Returns: a new #GCClosure
4198 GClosure*
4199 g_cclosure_new_object_swap (GCallback callback_func,
4200 GObject *object)
4202 GClosure *closure;
4204 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4205 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4206 g_return_val_if_fail (callback_func != NULL, NULL);
4208 closure = g_cclosure_new_swap (callback_func, object, NULL);
4209 g_object_watch_closure (object, closure);
4211 return closure;
4214 gsize
4215 g_object_compat_control (gsize what,
4216 gpointer data)
4218 switch (what)
4220 gpointer *pp;
4221 case 1: /* floating base type */
4222 return G_TYPE_INITIALLY_UNOWNED;
4223 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4224 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4225 return 1;
4226 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4227 pp = data;
4228 *pp = floating_flag_handler;
4229 return 1;
4230 default:
4231 return 0;
4235 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
4237 static void
4238 g_initially_unowned_init (GInitiallyUnowned *object)
4240 g_object_force_floating (object);
4243 static void
4244 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4249 * GWeakRef:
4251 * A structure containing a weak reference to a #GObject. It can either
4252 * be empty (i.e. point to %NULL), or point to an object for as long as
4253 * at least one "strong" reference to that object exists. Before the
4254 * object's #GObjectClass.dispose method is called, every #GWeakRef
4255 * associated with becomes empty (i.e. points to %NULL).
4257 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4258 * heap-allocated, or embedded in larger structures.
4260 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4261 * reference is thread-safe: converting a weak pointer to a reference is
4262 * atomic with respect to invalidation of weak pointers to destroyed
4263 * objects.
4265 * If the object's #GObjectClass.dispose method results in additional
4266 * references to the object being held, any #GWeakRefs taken
4267 * before it was disposed will continue to point to %NULL. If
4268 * #GWeakRefs are taken after the object is disposed and
4269 * re-referenced, they will continue to point to it until its refcount
4270 * goes back to zero, at which point they too will be invalidated.
4274 * g_weak_ref_init: (skip)
4275 * @weak_ref: (inout): uninitialized or empty location for a weak
4276 * reference
4277 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4279 * Initialise a non-statically-allocated #GWeakRef.
4281 * This function also calls g_weak_ref_set() with @object on the
4282 * freshly-initialised weak reference.
4284 * This function should always be matched with a call to
4285 * g_weak_ref_clear(). It is not necessary to use this function for a
4286 * #GWeakRef in static storage because it will already be
4287 * properly initialised. Just use g_weak_ref_set() directly.
4289 * Since: 2.32
4291 void
4292 g_weak_ref_init (GWeakRef *weak_ref,
4293 gpointer object)
4295 weak_ref->priv.p = NULL;
4297 g_weak_ref_set (weak_ref, object);
4301 * g_weak_ref_clear: (skip)
4302 * @weak_ref: (inout): location of a weak reference, which
4303 * may be empty
4305 * Frees resources associated with a non-statically-allocated #GWeakRef.
4306 * After this call, the #GWeakRef is left in an undefined state.
4308 * You should only call this on a #GWeakRef that previously had
4309 * g_weak_ref_init() called on it.
4311 * Since: 2.32
4313 void
4314 g_weak_ref_clear (GWeakRef *weak_ref)
4316 g_weak_ref_set (weak_ref, NULL);
4318 /* be unkind */
4319 weak_ref->priv.p = (void *) 0xccccccccu;
4323 * g_weak_ref_get: (skip)
4324 * @weak_ref: (inout): location of a weak reference to a #GObject
4326 * If @weak_ref is not empty, atomically acquire a strong
4327 * reference to the object it points to, and return that reference.
4329 * This function is needed because of the potential race between taking
4330 * the pointer value and g_object_ref() on it, if the object was losing
4331 * its last reference at the same time in a different thread.
4333 * The caller should release the resulting reference in the usual way,
4334 * by using g_object_unref().
4336 * Returns: (transfer full) (type GObject.Object): the object pointed to
4337 * by @weak_ref, or %NULL if it was empty
4339 * Since: 2.32
4341 gpointer
4342 g_weak_ref_get (GWeakRef *weak_ref)
4344 gpointer object_or_null;
4346 g_return_val_if_fail (weak_ref!= NULL, NULL);
4348 g_rw_lock_reader_lock (&weak_locations_lock);
4350 object_or_null = weak_ref->priv.p;
4352 if (object_or_null != NULL)
4353 g_object_ref (object_or_null);
4355 g_rw_lock_reader_unlock (&weak_locations_lock);
4357 return object_or_null;
4361 * g_weak_ref_set: (skip)
4362 * @weak_ref: location for a weak reference
4363 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4365 * Change the object to which @weak_ref points, or set it to
4366 * %NULL.
4368 * You must own a strong reference on @object while calling this
4369 * function.
4371 * Since: 2.32
4373 void
4374 g_weak_ref_set (GWeakRef *weak_ref,
4375 gpointer object)
4377 GSList **weak_locations;
4378 GObject *new_object;
4379 GObject *old_object;
4381 g_return_if_fail (weak_ref != NULL);
4382 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4384 new_object = object;
4386 g_rw_lock_writer_lock (&weak_locations_lock);
4388 /* We use the extra level of indirection here so that if we have ever
4389 * had a weak pointer installed at any point in time on this object,
4390 * we can see that there is a non-NULL value associated with the
4391 * weak-pointer quark and know that this value will not change at any
4392 * point in the object's lifetime.
4394 * Both properties are important for reducing the amount of times we
4395 * need to acquire locks and for decreasing the duration of time the
4396 * lock is held while avoiding some rather tricky races.
4398 * Specifically: we can avoid having to do an extra unconditional lock
4399 * in g_object_unref() without worrying about some extremely tricky
4400 * races.
4403 old_object = weak_ref->priv.p;
4404 if (new_object != old_object)
4406 weak_ref->priv.p = new_object;
4408 /* Remove the weak ref from the old object */
4409 if (old_object != NULL)
4411 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4412 /* for it to point to an object, the object must have had it added once */
4413 g_assert (weak_locations != NULL);
4415 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4418 /* Add the weak ref to the new object */
4419 if (new_object != NULL)
4421 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4423 if (weak_locations == NULL)
4425 weak_locations = g_new0 (GSList *, 1);
4426 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4429 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4433 g_rw_lock_writer_unlock (&weak_locations_lock);