Increase the timeout for some GLib tests
[glib.git] / gobject / gobject.c
blob81a1162e9818d0b6f4854e15538fd38057818227
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 gboolean
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 FALSE;
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);
530 return TRUE;
533 static gboolean
534 validate_pspec_to_install (GParamSpec *pspec)
536 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
537 g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */
539 g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE);
541 if (pspec->flags & G_PARAM_CONSTRUCT)
542 g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE);
544 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
545 g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE);
547 return TRUE;
550 static gboolean
551 validate_and_install_class_property (GObjectClass *class,
552 GType oclass_type,
553 GType parent_type,
554 guint property_id,
555 GParamSpec *pspec)
557 if (!validate_pspec_to_install (pspec))
558 return FALSE;
560 if (pspec->flags & G_PARAM_WRITABLE)
561 g_return_val_if_fail (class->set_property != NULL, FALSE);
562 if (pspec->flags & G_PARAM_READABLE)
563 g_return_val_if_fail (class->get_property != NULL, FALSE);
565 class->flags |= CLASS_HAS_PROPS_FLAG;
566 if (install_property_internal (oclass_type, property_id, pspec))
568 if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
569 class->construct_properties = g_slist_append (class->construct_properties, pspec);
571 /* for property overrides of construct properties, we have to get rid
572 * of the overidden inherited construct property
574 pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
575 if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
576 class->construct_properties = g_slist_remove (class->construct_properties, pspec);
578 return TRUE;
580 else
581 return FALSE;
585 * g_object_class_install_property:
586 * @oclass: a #GObjectClass
587 * @property_id: the id for the new property
588 * @pspec: the #GParamSpec for the new property
590 * Installs a new property.
592 * All properties should be installed during the class initializer. It
593 * is possible to install properties after that, but doing so is not
594 * recommend, and specifically, is not guaranteed to be thread-safe vs.
595 * use of properties on the same type on other threads.
597 * Note that it is possible to redefine a property in a derived class,
598 * by installing a property with the same name. This can be useful at times,
599 * e.g. to change the range of allowed values or the default value.
601 void
602 g_object_class_install_property (GObjectClass *class,
603 guint property_id,
604 GParamSpec *pspec)
606 GType oclass_type, parent_type;
608 g_return_if_fail (G_IS_OBJECT_CLASS (class));
609 g_return_if_fail (property_id > 0);
611 oclass_type = G_OBJECT_CLASS_TYPE (class);
612 parent_type = g_type_parent (oclass_type);
614 if (CLASS_HAS_DERIVED_CLASS (class))
615 g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
617 (void) validate_and_install_class_property (class,
618 oclass_type,
619 parent_type,
620 property_id,
621 pspec);
625 * g_object_class_install_properties:
626 * @oclass: a #GObjectClass
627 * @n_pspecs: the length of the #GParamSpecs array
628 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
629 * defining the new properties
631 * Installs new properties from an array of #GParamSpecs.
633 * All properties should be installed during the class initializer. It
634 * is possible to install properties after that, but doing so is not
635 * recommend, and specifically, is not guaranteed to be thread-safe vs.
636 * use of properties on the same type on other threads.
638 * The property id of each property is the index of each #GParamSpec in
639 * the @pspecs array.
641 * The property id of 0 is treated specially by #GObject and it should not
642 * be used to store a #GParamSpec.
644 * This function should be used if you plan to use a static array of
645 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
646 * class initialization:
648 * |[<!-- language="C" -->
649 * enum {
650 * PROP_0, PROP_FOO, PROP_BAR, N_PROPERTIES
651 * };
653 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
655 * static void
656 * my_object_class_init (MyObjectClass *klass)
658 * GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
660 * obj_properties[PROP_FOO] =
661 * g_param_spec_int ("foo", "Foo", "Foo",
662 * -1, G_MAXINT,
663 * 0,
664 * G_PARAM_READWRITE);
666 * obj_properties[PROP_BAR] =
667 * g_param_spec_string ("bar", "Bar", "Bar",
668 * NULL,
669 * G_PARAM_READWRITE);
671 * gobject_class->set_property = my_object_set_property;
672 * gobject_class->get_property = my_object_get_property;
673 * g_object_class_install_properties (gobject_class,
674 * N_PROPERTIES,
675 * obj_properties);
677 * ]|
679 * allows calling g_object_notify_by_pspec() to notify of property changes:
681 * |[<!-- language="C" -->
682 * void
683 * my_object_set_foo (MyObject *self, gint foo)
685 * if (self->foo != foo)
687 * self->foo = foo;
688 * g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
691 * ]|
693 * Since: 2.26
695 void
696 g_object_class_install_properties (GObjectClass *oclass,
697 guint n_pspecs,
698 GParamSpec **pspecs)
700 GType oclass_type, parent_type;
701 gint i;
703 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
704 g_return_if_fail (n_pspecs > 1);
705 g_return_if_fail (pspecs[0] == NULL);
707 if (CLASS_HAS_DERIVED_CLASS (oclass))
708 g_error ("Attempt to add properties to %s after it was derived",
709 G_OBJECT_CLASS_NAME (oclass));
711 oclass_type = G_OBJECT_CLASS_TYPE (oclass);
712 parent_type = g_type_parent (oclass_type);
714 /* we skip the first element of the array as it would have a 0 prop_id */
715 for (i = 1; i < n_pspecs; i++)
717 GParamSpec *pspec = pspecs[i];
719 if (!validate_and_install_class_property (oclass,
720 oclass_type,
721 parent_type,
723 pspec))
725 break;
731 * g_object_interface_install_property:
732 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
733 * interface, or the default
734 * vtable for the interface.
735 * @pspec: the #GParamSpec for the new property
737 * Add a property to an interface; this is only useful for interfaces
738 * that are added to GObject-derived types. Adding a property to an
739 * interface forces all objects classes with that interface to have a
740 * compatible property. The compatible property could be a newly
741 * created #GParamSpec, but normally
742 * g_object_class_override_property() will be used so that the object
743 * class only needs to provide an implementation and inherits the
744 * property description, default value, bounds, and so forth from the
745 * interface property.
747 * This function is meant to be called from the interface's default
748 * vtable initialization function (the @class_init member of
749 * #GTypeInfo.) It must not be called after after @class_init has
750 * been called for any object types implementing this interface.
752 * If @pspec is a floating reference, it will be consumed.
754 * Since: 2.4
756 void
757 g_object_interface_install_property (gpointer g_iface,
758 GParamSpec *pspec)
760 GTypeInterface *iface_class = g_iface;
762 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
763 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
765 if (!validate_pspec_to_install (pspec))
766 return;
768 (void) install_property_internal (iface_class->g_type, 0, pspec);
772 * g_object_class_find_property:
773 * @oclass: a #GObjectClass
774 * @property_name: the name of the property to look up
776 * Looks up the #GParamSpec for a property of a class.
778 * Returns: (transfer none): the #GParamSpec for the property, or
779 * %NULL if the class doesn't have a property of that name
781 GParamSpec*
782 g_object_class_find_property (GObjectClass *class,
783 const gchar *property_name)
785 GParamSpec *pspec;
786 GParamSpec *redirect;
788 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
789 g_return_val_if_fail (property_name != NULL, NULL);
791 pspec = g_param_spec_pool_lookup (pspec_pool,
792 property_name,
793 G_OBJECT_CLASS_TYPE (class),
794 TRUE);
795 if (pspec)
797 redirect = g_param_spec_get_redirect_target (pspec);
798 if (redirect)
799 return redirect;
800 else
801 return pspec;
803 else
804 return NULL;
808 * g_object_interface_find_property:
809 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
810 * interface, or the default vtable for the interface
811 * @property_name: name of a property to lookup.
813 * Find the #GParamSpec with the given name for an
814 * interface. Generally, the interface vtable passed in as @g_iface
815 * will be the default vtable from g_type_default_interface_ref(), or,
816 * if you know the interface has already been loaded,
817 * g_type_default_interface_peek().
819 * Since: 2.4
821 * Returns: (transfer none): the #GParamSpec for the property of the
822 * interface with the name @property_name, or %NULL if no
823 * such property exists.
825 GParamSpec*
826 g_object_interface_find_property (gpointer g_iface,
827 const gchar *property_name)
829 GTypeInterface *iface_class = g_iface;
831 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
832 g_return_val_if_fail (property_name != NULL, NULL);
834 return g_param_spec_pool_lookup (pspec_pool,
835 property_name,
836 iface_class->g_type,
837 FALSE);
841 * g_object_class_override_property:
842 * @oclass: a #GObjectClass
843 * @property_id: the new property ID
844 * @name: the name of a property registered in a parent class or
845 * in an interface of this class.
847 * Registers @property_id as referring to a property with the name
848 * @name in a parent class or in an interface implemented by @oclass.
849 * This allows this class to "override" a property implementation in
850 * a parent class or to provide the implementation of a property from
851 * an interface.
853 * Internally, overriding is implemented by creating a property of type
854 * #GParamSpecOverride; generally operations that query the properties of
855 * the object class, such as g_object_class_find_property() or
856 * g_object_class_list_properties() will return the overridden
857 * property. However, in one case, the @construct_properties argument of
858 * the @constructor virtual function, the #GParamSpecOverride is passed
859 * instead, so that the @param_id field of the #GParamSpec will be
860 * correct. For virtually all uses, this makes no difference. If you
861 * need to get the overridden property, you can call
862 * g_param_spec_get_redirect_target().
864 * Since: 2.4
866 void
867 g_object_class_override_property (GObjectClass *oclass,
868 guint property_id,
869 const gchar *name)
871 GParamSpec *overridden = NULL;
872 GParamSpec *new;
873 GType parent_type;
875 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
876 g_return_if_fail (property_id > 0);
877 g_return_if_fail (name != NULL);
879 /* Find the overridden property; first check parent types
881 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
882 if (parent_type != G_TYPE_NONE)
883 overridden = g_param_spec_pool_lookup (pspec_pool,
884 name,
885 parent_type,
886 TRUE);
887 if (!overridden)
889 GType *ifaces;
890 guint n_ifaces;
892 /* Now check interfaces
894 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
895 while (n_ifaces-- && !overridden)
897 overridden = g_param_spec_pool_lookup (pspec_pool,
898 name,
899 ifaces[n_ifaces],
900 FALSE);
903 g_free (ifaces);
906 if (!overridden)
908 g_warning ("%s: Can't find property to override for '%s::%s'",
909 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
910 return;
913 new = g_param_spec_override (name, overridden);
914 g_object_class_install_property (oclass, property_id, new);
918 * g_object_class_list_properties:
919 * @oclass: a #GObjectClass
920 * @n_properties: (out): return location for the length of the returned array
922 * Get an array of #GParamSpec* for all properties of a class.
924 * Returns: (array length=n_properties) (transfer container): an array of
925 * #GParamSpec* which should be freed after use
927 GParamSpec** /* free result */
928 g_object_class_list_properties (GObjectClass *class,
929 guint *n_properties_p)
931 GParamSpec **pspecs;
932 guint n;
934 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
936 pspecs = g_param_spec_pool_list (pspec_pool,
937 G_OBJECT_CLASS_TYPE (class),
938 &n);
939 if (n_properties_p)
940 *n_properties_p = n;
942 return pspecs;
946 * g_object_interface_list_properties:
947 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
948 * interface, or the default vtable for the interface
949 * @n_properties_p: (out): location to store number of properties returned.
951 * Lists the properties of an interface.Generally, the interface
952 * vtable passed in as @g_iface will be the default vtable from
953 * g_type_default_interface_ref(), or, if you know the interface has
954 * already been loaded, g_type_default_interface_peek().
956 * Since: 2.4
958 * Returns: (array length=n_properties_p) (transfer container): a
959 * pointer to an array of pointers to #GParamSpec
960 * structures. The paramspecs are owned by GLib, but the
961 * array should be freed with g_free() when you are done with
962 * it.
964 GParamSpec**
965 g_object_interface_list_properties (gpointer g_iface,
966 guint *n_properties_p)
968 GTypeInterface *iface_class = g_iface;
969 GParamSpec **pspecs;
970 guint n;
972 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
974 pspecs = g_param_spec_pool_list (pspec_pool,
975 iface_class->g_type,
976 &n);
977 if (n_properties_p)
978 *n_properties_p = n;
980 return pspecs;
983 static inline gboolean
984 object_in_construction (GObject *object)
986 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
989 static void
990 g_object_init (GObject *object,
991 GObjectClass *class)
993 object->ref_count = 1;
994 object->qdata = NULL;
996 if (CLASS_HAS_PROPS (class))
998 /* freeze object's notification queue, g_object_newv() preserves pairedness */
999 g_object_notify_queue_freeze (object, FALSE);
1002 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1004 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
1005 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
1008 GOBJECT_IF_DEBUG (OBJECTS,
1010 G_LOCK (debug_objects);
1011 debug_objects_count++;
1012 g_hash_table_add (debug_objects_ht, object);
1013 G_UNLOCK (debug_objects);
1017 static void
1018 g_object_do_set_property (GObject *object,
1019 guint property_id,
1020 const GValue *value,
1021 GParamSpec *pspec)
1023 switch (property_id)
1025 default:
1026 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1027 break;
1031 static void
1032 g_object_do_get_property (GObject *object,
1033 guint property_id,
1034 GValue *value,
1035 GParamSpec *pspec)
1037 switch (property_id)
1039 default:
1040 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1041 break;
1045 static void
1046 g_object_real_dispose (GObject *object)
1048 g_signal_handlers_destroy (object);
1049 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1050 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1053 static void
1054 g_object_finalize (GObject *object)
1056 if (object_in_construction (object))
1058 g_critical ("object %s %p finalized while still in-construction",
1059 G_OBJECT_TYPE_NAME (object), object);
1062 g_datalist_clear (&object->qdata);
1064 GOBJECT_IF_DEBUG (OBJECTS,
1066 G_LOCK (debug_objects);
1067 g_assert (g_hash_table_contains (debug_objects_ht, object));
1068 g_hash_table_remove (debug_objects_ht, object);
1069 debug_objects_count--;
1070 G_UNLOCK (debug_objects);
1074 static void
1075 g_object_dispatch_properties_changed (GObject *object,
1076 guint n_pspecs,
1077 GParamSpec **pspecs)
1079 guint i;
1081 for (i = 0; i < n_pspecs; i++)
1082 g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1086 * g_object_run_dispose:
1087 * @object: a #GObject
1089 * Releases all references to other objects. This can be used to break
1090 * reference cycles.
1092 * This function should only be called from object system implementations.
1094 void
1095 g_object_run_dispose (GObject *object)
1097 g_return_if_fail (G_IS_OBJECT (object));
1098 g_return_if_fail (object->ref_count > 0);
1100 g_object_ref (object);
1101 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1102 G_OBJECT_GET_CLASS (object)->dispose (object);
1103 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1104 g_object_unref (object);
1108 * g_object_freeze_notify:
1109 * @object: a #GObject
1111 * Increases the freeze count on @object. If the freeze count is
1112 * non-zero, the emission of "notify" signals on @object is
1113 * stopped. The signals are queued until the freeze count is decreased
1114 * to zero. Duplicate notifications are squashed so that at most one
1115 * #GObject::notify signal is emitted for each property modified while the
1116 * object is frozen.
1118 * This is necessary for accessors that modify multiple properties to prevent
1119 * premature notification while the object is still being modified.
1121 void
1122 g_object_freeze_notify (GObject *object)
1124 g_return_if_fail (G_IS_OBJECT (object));
1126 if (g_atomic_int_get (&object->ref_count) == 0)
1127 return;
1129 g_object_ref (object);
1130 g_object_notify_queue_freeze (object, FALSE);
1131 g_object_unref (object);
1134 static GParamSpec *
1135 get_notify_pspec (GParamSpec *pspec)
1137 GParamSpec *redirected;
1139 /* we don't notify on non-READABLE parameters */
1140 if (~pspec->flags & G_PARAM_READABLE)
1141 return NULL;
1143 /* if the paramspec is redirected, notify on the target */
1144 redirected = g_param_spec_get_redirect_target (pspec);
1145 if (redirected != NULL)
1146 return redirected;
1148 /* else, notify normally */
1149 return pspec;
1152 static inline void
1153 g_object_notify_by_spec_internal (GObject *object,
1154 GParamSpec *pspec)
1156 GParamSpec *notify_pspec;
1158 notify_pspec = get_notify_pspec (pspec);
1160 if (notify_pspec != NULL)
1162 GObjectNotifyQueue *nqueue;
1164 /* conditional freeze: only increase freeze count if already frozen */
1165 nqueue = g_object_notify_queue_freeze (object, TRUE);
1167 if (nqueue != NULL)
1169 /* we're frozen, so add to the queue and release our freeze */
1170 g_object_notify_queue_add (object, nqueue, notify_pspec);
1171 g_object_notify_queue_thaw (object, nqueue);
1173 else
1174 /* not frozen, so just dispatch the notification directly */
1175 G_OBJECT_GET_CLASS (object)
1176 ->dispatch_properties_changed (object, 1, &notify_pspec);
1181 * g_object_notify:
1182 * @object: a #GObject
1183 * @property_name: the name of a property installed on the class of @object.
1185 * Emits a "notify" signal for the property @property_name on @object.
1187 * When possible, eg. when signaling a property change from within the class
1188 * that registered the property, you should use g_object_notify_by_pspec()
1189 * instead.
1191 * Note that emission of the notify signal may be blocked with
1192 * g_object_freeze_notify(). In this case, the signal emissions are queued
1193 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1194 * called.
1196 void
1197 g_object_notify (GObject *object,
1198 const gchar *property_name)
1200 GParamSpec *pspec;
1202 g_return_if_fail (G_IS_OBJECT (object));
1203 g_return_if_fail (property_name != NULL);
1204 if (g_atomic_int_get (&object->ref_count) == 0)
1205 return;
1207 g_object_ref (object);
1208 /* We don't need to get the redirect target
1209 * (by, e.g. calling g_object_class_find_property())
1210 * because g_object_notify_queue_add() does that
1212 pspec = g_param_spec_pool_lookup (pspec_pool,
1213 property_name,
1214 G_OBJECT_TYPE (object),
1215 TRUE);
1217 if (!pspec)
1218 g_warning ("%s: object class '%s' has no property named '%s'",
1219 G_STRFUNC,
1220 G_OBJECT_TYPE_NAME (object),
1221 property_name);
1222 else
1223 g_object_notify_by_spec_internal (object, pspec);
1224 g_object_unref (object);
1228 * g_object_notify_by_pspec:
1229 * @object: a #GObject
1230 * @pspec: the #GParamSpec of a property installed on the class of @object.
1232 * Emits a "notify" signal for the property specified by @pspec on @object.
1234 * This function omits the property name lookup, hence it is faster than
1235 * g_object_notify().
1237 * One way to avoid using g_object_notify() from within the
1238 * class that registered the properties, and using g_object_notify_by_pspec()
1239 * instead, is to store the GParamSpec used with
1240 * g_object_class_install_property() inside a static array, e.g.:
1242 *|[<!-- language="C" -->
1243 * enum
1245 * PROP_0,
1246 * PROP_FOO,
1247 * PROP_LAST
1248 * };
1250 * static GParamSpec *properties[PROP_LAST];
1252 * static void
1253 * my_object_class_init (MyObjectClass *klass)
1255 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1256 * 0, 100,
1257 * 50,
1258 * G_PARAM_READWRITE);
1259 * g_object_class_install_property (gobject_class,
1260 * PROP_FOO,
1261 * properties[PROP_FOO]);
1263 * ]|
1265 * and then notify a change on the "foo" property with:
1267 * |[<!-- language="C" -->
1268 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1269 * ]|
1271 * Since: 2.26
1273 void
1274 g_object_notify_by_pspec (GObject *object,
1275 GParamSpec *pspec)
1278 g_return_if_fail (G_IS_OBJECT (object));
1279 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1281 if (g_atomic_int_get (&object->ref_count) == 0)
1282 return;
1284 g_object_ref (object);
1285 g_object_notify_by_spec_internal (object, pspec);
1286 g_object_unref (object);
1290 * g_object_thaw_notify:
1291 * @object: a #GObject
1293 * Reverts the effect of a previous call to
1294 * g_object_freeze_notify(). The freeze count is decreased on @object
1295 * and when it reaches zero, queued "notify" signals are emitted.
1297 * Duplicate notifications for each property are squashed so that at most one
1298 * #GObject::notify signal is emitted for each property, in the reverse order
1299 * in which they have been queued.
1301 * It is an error to call this function when the freeze count is zero.
1303 void
1304 g_object_thaw_notify (GObject *object)
1306 GObjectNotifyQueue *nqueue;
1308 g_return_if_fail (G_IS_OBJECT (object));
1309 if (g_atomic_int_get (&object->ref_count) == 0)
1310 return;
1312 g_object_ref (object);
1314 /* FIXME: Freezing is the only way to get at the notify queue.
1315 * So we freeze once and then thaw twice.
1317 nqueue = g_object_notify_queue_freeze (object, FALSE);
1318 g_object_notify_queue_thaw (object, nqueue);
1319 g_object_notify_queue_thaw (object, nqueue);
1321 g_object_unref (object);
1324 static void
1325 consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
1327 static GHashTable *already_warned_table;
1328 static const gchar *enable_diagnostic;
1329 static GMutex already_warned_lock;
1330 gboolean already;
1332 if (!(pspec->flags & G_PARAM_DEPRECATED))
1333 return;
1335 if (g_once_init_enter (&enable_diagnostic))
1337 const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1339 if (!value)
1340 value = "0";
1342 g_once_init_leave (&enable_diagnostic, value);
1345 if (enable_diagnostic[0] == '0')
1346 return;
1348 /* We hash only on property names: this means that we could end up in
1349 * a situation where we fail to emit a warning about a pair of
1350 * same-named deprecated properties used on two separate types.
1351 * That's pretty unlikely to occur, and even if it does, you'll still
1352 * have seen the warning for the first one...
1354 * Doing it this way lets us hash directly on the (interned) property
1355 * name pointers.
1357 g_mutex_lock (&already_warned_lock);
1359 if (already_warned_table == NULL)
1360 already_warned_table = g_hash_table_new (NULL, NULL);
1362 already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
1363 if (!already)
1364 g_hash_table_add (already_warned_table, (gpointer) pspec->name);
1366 g_mutex_unlock (&already_warned_lock);
1368 if (!already)
1369 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1370 "anymore. It will be removed in a future version.",
1371 g_type_name (pspec->owner_type), pspec->name);
1374 static inline void
1375 object_get_property (GObject *object,
1376 GParamSpec *pspec,
1377 GValue *value)
1379 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1380 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1381 GParamSpec *redirect;
1383 if (class == NULL)
1385 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1386 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1387 return;
1390 redirect = g_param_spec_get_redirect_target (pspec);
1391 if (redirect)
1392 pspec = redirect;
1394 consider_issuing_property_deprecation_warning (pspec);
1396 class->get_property (object, param_id, value, pspec);
1399 static inline void
1400 object_set_property (GObject *object,
1401 GParamSpec *pspec,
1402 const GValue *value,
1403 GObjectNotifyQueue *nqueue)
1405 GValue tmp_value = G_VALUE_INIT;
1406 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1407 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1408 GParamSpec *redirect;
1410 if (class == NULL)
1412 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1413 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1414 return;
1417 redirect = g_param_spec_get_redirect_target (pspec);
1418 if (redirect)
1419 pspec = redirect;
1421 /* provide a copy to work from, convert (if necessary) and validate */
1422 g_value_init (&tmp_value, pspec->value_type);
1423 if (!g_value_transform (value, &tmp_value))
1424 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1425 pspec->name,
1426 g_type_name (pspec->value_type),
1427 G_VALUE_TYPE_NAME (value));
1428 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1430 gchar *contents = g_strdup_value_contents (value);
1432 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1433 contents,
1434 G_VALUE_TYPE_NAME (value),
1435 pspec->name,
1436 g_type_name (pspec->value_type));
1437 g_free (contents);
1439 else
1441 class->set_property (object, param_id, &tmp_value, pspec);
1443 if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY)
1445 GParamSpec *notify_pspec;
1447 notify_pspec = get_notify_pspec (pspec);
1449 if (notify_pspec != NULL)
1450 g_object_notify_queue_add (object, nqueue, notify_pspec);
1453 g_value_unset (&tmp_value);
1456 static void
1457 object_interface_check_properties (gpointer check_data,
1458 gpointer g_iface)
1460 GTypeInterface *iface_class = g_iface;
1461 GObjectClass *class;
1462 GType iface_type = iface_class->g_type;
1463 GParamSpec **pspecs;
1464 guint n;
1466 class = g_type_class_ref (iface_class->g_instance_type);
1468 if (class == NULL)
1469 return;
1471 if (!G_IS_OBJECT_CLASS (class))
1472 goto out;
1474 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1476 while (n--)
1478 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1479 pspecs[n]->name,
1480 G_OBJECT_CLASS_TYPE (class),
1481 TRUE);
1483 if (!class_pspec)
1485 g_critical ("Object class %s doesn't implement property "
1486 "'%s' from interface '%s'",
1487 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1488 pspecs[n]->name,
1489 g_type_name (iface_type));
1491 continue;
1494 /* We do a number of checks on the properties of an interface to
1495 * make sure that all classes implementing the interface are
1496 * overriding the properties in a sane way.
1498 * We do the checks in order of importance so that we can give
1499 * more useful error messages first.
1501 * First, we check that the implementation doesn't remove the
1502 * basic functionality (readability, writability) advertised by
1503 * the interface. Next, we check that it doesn't introduce
1504 * additional restrictions (such as construct-only). Finally, we
1505 * make sure the types are compatible.
1508 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1509 /* If the property on the interface is readable then the
1510 * implementation must be readable. If the interface is writable
1511 * then the implementation must be writable.
1513 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1515 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1516 "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 /* If the property on the interface is writable then we need to
1522 * make sure the implementation doesn't introduce new restrictions
1523 * on that writability (ie: construct-only).
1525 * If the interface was not writable to begin with then we don't
1526 * really have any problems here because "writable at construct
1527 * time only" is still more permissive than "read only".
1529 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1531 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1533 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1534 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1535 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1536 continue;
1539 #undef SUBSET
1541 /* If the property on the interface is readable then we are
1542 * effectively advertising that reading the property will return a
1543 * value of a specific type. All implementations of the interface
1544 * need to return items of this type -- but may be more
1545 * restrictive. For example, it is legal to have:
1547 * GtkWidget *get_item();
1549 * that is implemented by a function that always returns a
1550 * GtkEntry. In short: readability implies that the
1551 * implementation value type must be equal or more restrictive.
1553 * Similarly, if the property on the interface is writable then
1554 * must be able to accept the property being set to any value of
1555 * that type, including subclasses. In this case, we may also be
1556 * less restrictive. For example, it is legal to have:
1558 * set_item (GtkEntry *);
1560 * that is implemented by a function that will actually work with
1561 * any GtkWidget. In short: writability implies that the
1562 * implementation value type must be equal or less restrictive.
1564 * In the case that the property is both readable and writable
1565 * then the only way that both of the above can be satisfied is
1566 * with a type that is exactly equal.
1568 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1570 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1571 /* class pspec value type must have exact equality with interface */
1572 if (pspecs[n]->value_type != class_pspec->value_type)
1573 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1574 "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 case G_PARAM_READABLE:
1580 /* class pspec value type equal or more restrictive than interface */
1581 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1582 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1583 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1584 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1585 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1586 break;
1588 case G_PARAM_WRITABLE:
1589 /* class pspec value type equal or less restrictive than interface */
1590 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1591 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1592 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1593 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1594 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1595 break;
1597 default:
1598 g_assert_not_reached ();
1602 g_free (pspecs);
1604 out:
1605 g_type_class_unref (class);
1608 GType
1609 g_object_get_type (void)
1611 return G_TYPE_OBJECT;
1615 * g_object_new: (skip)
1616 * @object_type: the type id of the #GObject subtype to instantiate
1617 * @first_property_name: the name of the first property
1618 * @...: the value of the first property, followed optionally by more
1619 * name/value pairs, followed by %NULL
1621 * Creates a new instance of a #GObject subtype and sets its properties.
1623 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1624 * which are not explicitly specified are set to their default values.
1626 * Returns: (transfer full) (type GObject.Object): a new instance of
1627 * @object_type
1629 gpointer
1630 g_object_new (GType object_type,
1631 const gchar *first_property_name,
1632 ...)
1634 GObject *object;
1635 va_list var_args;
1637 /* short circuit for calls supplying no properties */
1638 if (!first_property_name)
1639 return g_object_new_with_properties (object_type, 0, NULL, NULL);
1641 va_start (var_args, first_property_name);
1642 object = g_object_new_valist (object_type, first_property_name, var_args);
1643 va_end (var_args);
1645 return object;
1648 static gpointer
1649 g_object_new_with_custom_constructor (GObjectClass *class,
1650 GObjectConstructParam *params,
1651 guint n_params)
1653 GObjectNotifyQueue *nqueue = NULL;
1654 gboolean newly_constructed;
1655 GObjectConstructParam *cparams;
1656 GObject *object;
1657 GValue *cvalues;
1658 gint n_cparams;
1659 gint cvals_used;
1660 GSList *node;
1661 gint i;
1663 /* If we have ->constructed() then we have to do a lot more work.
1664 * It's possible that this is a singleton and it's also possible
1665 * that the user's constructor() will attempt to modify the values
1666 * that we pass in, so we'll need to allocate copies of them.
1667 * It's also possible that the user may attempt to call
1668 * g_object_set() from inside of their constructor, so we need to
1669 * add ourselves to a list of objects for which that is allowed
1670 * while their constructor() is running.
1673 /* Create the array of GObjectConstructParams for constructor() */
1674 n_cparams = g_slist_length (class->construct_properties);
1675 cparams = g_new (GObjectConstructParam, n_cparams);
1676 cvalues = g_new0 (GValue, n_cparams);
1677 cvals_used = 0;
1678 i = 0;
1680 /* As above, we may find the value in the passed-in params list.
1682 * If we have the value passed in then we can use the GValue from
1683 * it directly because it is safe to modify. If we use the
1684 * default value from the class, we had better not pass that in
1685 * and risk it being modified, so we create a new one.
1686 * */
1687 for (node = class->construct_properties; node; node = node->next)
1689 GParamSpec *pspec;
1690 GValue *value;
1691 gint j;
1693 pspec = node->data;
1694 value = NULL; /* to silence gcc... */
1696 for (j = 0; j < n_params; j++)
1697 if (params[j].pspec == pspec)
1699 consider_issuing_property_deprecation_warning (pspec);
1700 value = params[j].value;
1701 break;
1704 if (j == n_params)
1706 value = &cvalues[cvals_used++];
1707 g_value_init (value, pspec->value_type);
1708 g_param_value_set_default (pspec, value);
1711 cparams[i].pspec = pspec;
1712 cparams[i].value = value;
1713 i++;
1716 /* construct object from construction parameters */
1717 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1718 /* free construction values */
1719 g_free (cparams);
1720 while (cvals_used--)
1721 g_value_unset (&cvalues[cvals_used]);
1722 g_free (cvalues);
1724 /* There is code in the wild that relies on being able to return NULL
1725 * from its custom constructor. This was never a supported operation,
1726 * but since the code is already out there...
1728 if (object == NULL)
1730 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1731 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1732 return NULL;
1735 /* g_object_init() will have marked the object as being in-construction.
1736 * Check if the returned object still is so marked, or if this is an
1737 * already-existing singleton (in which case we should not do 'constructed').
1739 newly_constructed = object_in_construction (object);
1740 if (newly_constructed)
1741 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1743 if (CLASS_HAS_PROPS (class))
1745 /* If this object was newly_constructed then g_object_init()
1746 * froze the queue. We need to freeze it here in order to get
1747 * the handle so that we can thaw it below (otherwise it will
1748 * be frozen forever).
1750 * We also want to do a freeze if we have any params to set,
1751 * even on a non-newly_constructed object.
1753 * It's possible that we have the case of non-newly created
1754 * singleton and all of the passed-in params were construct
1755 * properties so n_params > 0 but we will actually set no
1756 * properties. This is a pretty lame case to optimise, so
1757 * just ignore it and freeze anyway.
1759 if (newly_constructed || n_params)
1760 nqueue = g_object_notify_queue_freeze (object, FALSE);
1762 /* Remember: if it was newly_constructed then g_object_init()
1763 * already did a freeze, so we now have two. Release one.
1765 if (newly_constructed)
1766 g_object_notify_queue_thaw (object, nqueue);
1769 /* run 'constructed' handler if there is a custom one */
1770 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1771 class->constructed (object);
1773 /* set remaining properties */
1774 for (i = 0; i < n_params; i++)
1775 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1777 consider_issuing_property_deprecation_warning (params[i].pspec);
1778 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1781 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1782 if (nqueue)
1783 g_object_notify_queue_thaw (object, nqueue);
1785 return object;
1788 static gpointer
1789 g_object_new_internal (GObjectClass *class,
1790 GObjectConstructParam *params,
1791 guint n_params)
1793 GObjectNotifyQueue *nqueue = NULL;
1794 GObject *object;
1796 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1797 return g_object_new_with_custom_constructor (class, params, n_params);
1799 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1801 if (CLASS_HAS_PROPS (class))
1803 GSList *node;
1805 /* This will have been setup in g_object_init() */
1806 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1807 g_assert (nqueue != NULL);
1809 /* We will set exactly n_construct_properties construct
1810 * properties, but they may come from either the class default
1811 * values or the passed-in parameter list.
1813 for (node = class->construct_properties; node; node = node->next)
1815 const GValue *value;
1816 GParamSpec *pspec;
1817 gint j;
1819 pspec = node->data;
1820 value = NULL; /* to silence gcc... */
1822 for (j = 0; j < n_params; j++)
1823 if (params[j].pspec == pspec)
1825 consider_issuing_property_deprecation_warning (pspec);
1826 value = params[j].value;
1827 break;
1830 if (j == n_params)
1831 value = g_param_spec_get_default_value (pspec);
1833 object_set_property (object, pspec, value, nqueue);
1837 /* run 'constructed' handler if there is a custom one */
1838 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1839 class->constructed (object);
1841 if (nqueue)
1843 gint i;
1845 /* Set remaining properties. The construct properties will
1846 * already have been taken, so set only the non-construct
1847 * ones.
1849 for (i = 0; i < n_params; i++)
1850 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1852 consider_issuing_property_deprecation_warning (params[i].pspec);
1853 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1856 g_object_notify_queue_thaw (object, nqueue);
1859 return object;
1863 static inline gboolean
1864 g_object_new_is_valid_property (GType object_type,
1865 GParamSpec *pspec,
1866 const char *name,
1867 GObjectConstructParam *params,
1868 int n_params)
1870 gint i;
1871 if (G_UNLIKELY (pspec == NULL))
1873 g_critical ("%s: object class '%s' has no property named '%s'",
1874 G_STRFUNC, g_type_name (object_type), name);
1875 return FALSE;
1878 if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
1880 g_critical ("%s: property '%s' of object class '%s' is not writable",
1881 G_STRFUNC, pspec->name, g_type_name (object_type));
1882 return FALSE;
1885 if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1887 for (i = 0; i < n_params; i++)
1888 if (params[i].pspec == pspec)
1889 break;
1890 if (G_UNLIKELY (i != n_params))
1892 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1893 G_STRFUNC, name, g_type_name (object_type));
1894 return FALSE;
1897 return TRUE;
1902 * g_object_new_with_properties: (rename-to g_object_new)
1903 * @object_type: the object type to instantiate
1904 * @n_properties: the number of properties
1905 * @names: (array length=n_properties): the names of each property to be set
1906 * @values: (array length=n_properties): the values of each property to be set
1908 * Creates a new instance of a #GObject subtype and sets its properties using
1909 * the provided arrays. Both arrays must have exactly @n_properties elements,
1910 * and the names and values correspond by index.
1912 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
1913 * which are not explicitly specified are set to their default values.
1915 * Returns: (type GObject.Object) (transfer full): a new instance of
1916 * @object_type
1918 * Since: 2.54
1920 GObject *
1921 g_object_new_with_properties (GType object_type,
1922 guint n_properties,
1923 const char *names[],
1924 const GValue values[])
1926 GObjectClass *class, *unref_class = NULL;
1927 GObject *object;
1929 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1931 /* Try to avoid thrashing the ref_count if we don't need to (since
1932 * it's a locked operation).
1934 class = g_type_class_peek_static (object_type);
1936 if (class == NULL)
1937 class = unref_class = g_type_class_ref (object_type);
1939 if (n_properties > 0)
1941 guint i, count = 0;
1942 GObjectConstructParam *params;
1944 params = g_newa (GObjectConstructParam, n_properties);
1945 for (i = 0; i < n_properties; i++)
1947 GParamSpec *pspec;
1948 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], object_type, TRUE);
1949 if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
1950 continue;
1951 params[count].pspec = pspec;
1953 /* Init GValue */
1954 params[count].value = g_newa (GValue, 1);
1955 memset (params[count].value, 0, sizeof (GValue));
1956 g_value_init (params[count].value, G_VALUE_TYPE (&values[i]));
1958 g_value_copy (&values[i], params[count].value);
1959 count++;
1961 object = g_object_new_internal (class, params, count);
1963 while (count--)
1964 g_value_unset (params[count].value);
1966 else
1967 object = g_object_new_internal (class, NULL, 0);
1969 if (unref_class != NULL)
1970 g_type_class_unref (unref_class);
1972 return object;
1976 * g_object_newv:
1977 * @object_type: the type id of the #GObject subtype to instantiate
1978 * @n_parameters: the length of the @parameters array
1979 * @parameters: (array length=n_parameters): an array of #GParameter
1981 * Creates a new instance of a #GObject subtype and sets its properties.
1983 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1984 * which are not explicitly specified are set to their default values.
1986 * Returns: (type GObject.Object) (transfer full): a new instance of
1987 * @object_type
1989 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
1990 * deprecated. See #GParameter for more information.
1992 gpointer
1993 g_object_newv (GType object_type,
1994 guint n_parameters,
1995 GParameter *parameters)
1997 GObjectClass *class, *unref_class = NULL;
1998 GObject *object;
2000 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2001 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
2003 /* Try to avoid thrashing the ref_count if we don't need to (since
2004 * it's a locked operation).
2006 class = g_type_class_peek_static (object_type);
2008 if (!class)
2009 class = unref_class = g_type_class_ref (object_type);
2011 if (n_parameters)
2013 GObjectConstructParam *cparams;
2014 guint i, j;
2016 cparams = g_newa (GObjectConstructParam, n_parameters);
2017 j = 0;
2019 for (i = 0; i < n_parameters; i++)
2021 GParamSpec *pspec;
2023 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
2024 if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2025 continue;
2027 cparams[j].pspec = pspec;
2028 cparams[j].value = &parameters[i].value;
2029 j++;
2032 object = g_object_new_internal (class, cparams, j);
2034 else
2035 /* Fast case: no properties passed in. */
2036 object = g_object_new_internal (class, NULL, 0);
2038 if (unref_class)
2039 g_type_class_unref (unref_class);
2041 return object;
2045 * g_object_new_valist: (skip)
2046 * @object_type: the type id of the #GObject subtype to instantiate
2047 * @first_property_name: the name of the first property
2048 * @var_args: the value of the first property, followed optionally by more
2049 * name/value pairs, followed by %NULL
2051 * Creates a new instance of a #GObject subtype and sets its properties.
2053 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
2054 * which are not explicitly specified are set to their default values.
2056 * Returns: a new instance of @object_type
2058 GObject*
2059 g_object_new_valist (GType object_type,
2060 const gchar *first_property_name,
2061 va_list var_args)
2063 GObjectClass *class, *unref_class = NULL;
2064 GObject *object;
2066 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2068 /* Try to avoid thrashing the ref_count if we don't need to (since
2069 * it's a locked operation).
2071 class = g_type_class_peek_static (object_type);
2073 if (!class)
2074 class = unref_class = g_type_class_ref (object_type);
2076 if (first_property_name)
2078 GObjectConstructParam stack_params[16];
2079 GObjectConstructParam *params;
2080 const gchar *name;
2081 gint n_params = 0;
2083 name = first_property_name;
2084 params = stack_params;
2088 gchar *error = NULL;
2089 GParamSpec *pspec;
2091 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
2093 if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2094 break;
2096 if (n_params == 16)
2098 params = g_new (GObjectConstructParam, n_params + 1);
2099 memcpy (params, stack_params, sizeof stack_params);
2101 else if (n_params > 16)
2102 params = g_renew (GObjectConstructParam, params, n_params + 1);
2104 params[n_params].pspec = pspec;
2105 params[n_params].value = g_newa (GValue, 1);
2106 memset (params[n_params].value, 0, sizeof (GValue));
2108 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
2110 if (error)
2112 g_critical ("%s: %s", G_STRFUNC, error);
2113 g_value_unset (params[n_params].value);
2114 g_free (error);
2115 break;
2118 n_params++;
2120 while ((name = va_arg (var_args, const gchar *)));
2122 object = g_object_new_internal (class, params, n_params);
2124 while (n_params--)
2125 g_value_unset (params[n_params].value);
2127 if (params != stack_params)
2128 g_free (params);
2130 else
2131 /* Fast case: no properties passed in. */
2132 object = g_object_new_internal (class, NULL, 0);
2134 if (unref_class)
2135 g_type_class_unref (unref_class);
2137 return object;
2140 static GObject*
2141 g_object_constructor (GType type,
2142 guint n_construct_properties,
2143 GObjectConstructParam *construct_params)
2145 GObject *object;
2147 /* create object */
2148 object = (GObject*) g_type_create_instance (type);
2150 /* set construction parameters */
2151 if (n_construct_properties)
2153 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2155 /* set construct properties */
2156 while (n_construct_properties--)
2158 GValue *value = construct_params->value;
2159 GParamSpec *pspec = construct_params->pspec;
2161 construct_params++;
2162 object_set_property (object, pspec, value, nqueue);
2164 g_object_notify_queue_thaw (object, nqueue);
2165 /* the notification queue is still frozen from g_object_init(), so
2166 * we don't need to handle it here, g_object_newv() takes
2167 * care of that
2171 return object;
2174 static void
2175 g_object_constructed (GObject *object)
2177 /* empty default impl to allow unconditional upchaining */
2180 static inline gboolean
2181 g_object_set_is_valid_property (GObject *object,
2182 GParamSpec *pspec,
2183 const char *property_name)
2185 if (G_UNLIKELY (pspec == NULL))
2187 g_warning ("%s: object class '%s' has no property named '%s'",
2188 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2189 return FALSE;
2191 if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
2193 g_warning ("%s: property '%s' of object class '%s' is not writable",
2194 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2195 return FALSE;
2197 if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
2199 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2200 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2201 return FALSE;
2203 return TRUE;
2207 * g_object_setv: (skip)
2208 * @object: a #GObject
2209 * @n_properties: the number of properties
2210 * @names: (array length=n_properties): the names of each property to be set
2211 * @values: (array length=n_properties): the values of each property to be set
2213 * Sets @n_properties properties for an @object.
2214 * Properties to be set will be taken from @values. All properties must be
2215 * valid. Warnings will be emitted and undefined behaviour may result if invalid
2216 * properties are passed in.
2218 * Since: 2.54
2220 void
2221 g_object_setv (GObject *object,
2222 guint n_properties,
2223 const gchar *names[],
2224 const GValue values[])
2226 guint i;
2227 GObjectNotifyQueue *nqueue;
2228 GParamSpec *pspec;
2229 GType obj_type;
2231 g_return_if_fail (G_IS_OBJECT (object));
2233 if (n_properties == 0)
2234 return;
2236 g_object_ref (object);
2237 obj_type = G_OBJECT_TYPE (object);
2238 nqueue = g_object_notify_queue_freeze (object, FALSE);
2239 for (i = 0; i < n_properties; i++)
2241 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], obj_type, TRUE);
2243 if (!g_object_set_is_valid_property (object, pspec, names[i]))
2244 break;
2246 consider_issuing_property_deprecation_warning (pspec);
2247 object_set_property (object, pspec, &values[i], nqueue);
2250 g_object_notify_queue_thaw (object, nqueue);
2251 g_object_unref (object);
2255 * g_object_set_valist: (skip)
2256 * @object: a #GObject
2257 * @first_property_name: name of the first property to set
2258 * @var_args: value for the first property, followed optionally by more
2259 * name/value pairs, followed by %NULL
2261 * Sets properties on an object.
2263 void
2264 g_object_set_valist (GObject *object,
2265 const gchar *first_property_name,
2266 va_list var_args)
2268 GObjectNotifyQueue *nqueue;
2269 const gchar *name;
2271 g_return_if_fail (G_IS_OBJECT (object));
2273 g_object_ref (object);
2274 nqueue = g_object_notify_queue_freeze (object, FALSE);
2276 name = first_property_name;
2277 while (name)
2279 GValue value = G_VALUE_INIT;
2280 GParamSpec *pspec;
2281 gchar *error = NULL;
2283 pspec = g_param_spec_pool_lookup (pspec_pool,
2284 name,
2285 G_OBJECT_TYPE (object),
2286 TRUE);
2288 if (!g_object_set_is_valid_property (object, pspec, name))
2289 break;
2291 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2292 0, &error);
2293 if (error)
2295 g_warning ("%s: %s", G_STRFUNC, error);
2296 g_free (error);
2297 g_value_unset (&value);
2298 break;
2301 consider_issuing_property_deprecation_warning (pspec);
2302 object_set_property (object, pspec, &value, nqueue);
2303 g_value_unset (&value);
2305 name = va_arg (var_args, gchar*);
2308 g_object_notify_queue_thaw (object, nqueue);
2309 g_object_unref (object);
2312 static inline gboolean
2313 g_object_get_is_valid_property (GObject *object,
2314 GParamSpec *pspec,
2315 const char *property_name)
2317 if (G_UNLIKELY (pspec == NULL))
2319 g_warning ("%s: object class '%s' has no property named '%s'",
2320 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2321 return FALSE;
2323 if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
2325 g_warning ("%s: property '%s' of object class '%s' is not readable",
2326 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2327 return FALSE;
2329 return TRUE;
2333 * g_object_getv:
2334 * @object: a #GObject
2335 * @n_properties: the number of properties
2336 * @names: (array length=n_properties): the names of each property to get
2337 * @values: (array length=n_properties): the values of each property to get
2339 * Gets @n_properties properties for an @object.
2340 * Obtained properties will be set to @values. All properties must be valid.
2341 * Warnings will be emitted and undefined behaviour may result if invalid
2342 * properties are passed in.
2344 * Since: 2.54
2346 void
2347 g_object_getv (GObject *object,
2348 guint n_properties,
2349 const gchar *names[],
2350 GValue values[])
2352 guint i;
2353 GParamSpec *pspec;
2354 GType obj_type;
2356 g_return_if_fail (G_IS_OBJECT (object));
2358 if (n_properties == 0)
2359 return;
2361 g_object_ref (object);
2363 obj_type = G_OBJECT_TYPE (object);
2364 for (i = 0; i < n_properties; i++)
2366 pspec = g_param_spec_pool_lookup (pspec_pool,
2367 names[i],
2368 obj_type,
2369 TRUE);
2370 if (!g_object_get_is_valid_property (object, pspec, names[i]))
2371 break;
2373 memset (&values[i], 0, sizeof (GValue));
2374 g_value_init (&values[i], pspec->value_type);
2375 object_get_property (object, pspec, &values[i]);
2377 g_object_unref (object);
2381 * g_object_get_valist: (skip)
2382 * @object: a #GObject
2383 * @first_property_name: name of the first property to get
2384 * @var_args: return location for the first property, followed optionally by more
2385 * name/return location pairs, followed by %NULL
2387 * Gets properties of an object.
2389 * In general, a copy is made of the property contents and the caller
2390 * is responsible for freeing the memory in the appropriate manner for
2391 * the type, for instance by calling g_free() or g_object_unref().
2393 * See g_object_get().
2395 void
2396 g_object_get_valist (GObject *object,
2397 const gchar *first_property_name,
2398 va_list var_args)
2400 const gchar *name;
2402 g_return_if_fail (G_IS_OBJECT (object));
2404 g_object_ref (object);
2406 name = first_property_name;
2408 while (name)
2410 GValue value = G_VALUE_INIT;
2411 GParamSpec *pspec;
2412 gchar *error;
2414 pspec = g_param_spec_pool_lookup (pspec_pool,
2415 name,
2416 G_OBJECT_TYPE (object),
2417 TRUE);
2419 if (!g_object_get_is_valid_property (object, pspec, name))
2420 break;
2422 g_value_init (&value, pspec->value_type);
2424 object_get_property (object, pspec, &value);
2426 G_VALUE_LCOPY (&value, var_args, 0, &error);
2427 if (error)
2429 g_warning ("%s: %s", G_STRFUNC, error);
2430 g_free (error);
2431 g_value_unset (&value);
2432 break;
2435 g_value_unset (&value);
2437 name = va_arg (var_args, gchar*);
2440 g_object_unref (object);
2444 * g_object_set: (skip)
2445 * @object: (type GObject.Object): a #GObject
2446 * @first_property_name: name of the first property to set
2447 * @...: value for the first property, followed optionally by more
2448 * name/value pairs, followed by %NULL
2450 * Sets properties on an object.
2452 * Note that the "notify" signals are queued and only emitted (in
2453 * reverse order) after all properties have been set. See
2454 * g_object_freeze_notify().
2456 void
2457 g_object_set (gpointer _object,
2458 const gchar *first_property_name,
2459 ...)
2461 GObject *object = _object;
2462 va_list var_args;
2464 g_return_if_fail (G_IS_OBJECT (object));
2466 va_start (var_args, first_property_name);
2467 g_object_set_valist (object, first_property_name, var_args);
2468 va_end (var_args);
2472 * g_object_get: (skip)
2473 * @object: (type GObject.Object): a #GObject
2474 * @first_property_name: name of the first property to get
2475 * @...: return location for the first property, followed optionally by more
2476 * name/return location pairs, followed by %NULL
2478 * Gets properties of an object.
2480 * In general, a copy is made of the property contents and the caller
2481 * is responsible for freeing the memory in the appropriate manner for
2482 * the type, for instance by calling g_free() or g_object_unref().
2484 * Here is an example of using g_object_get() to get the contents
2485 * of three properties: an integer, a string and an object:
2486 * |[<!-- language="C" -->
2487 * gint intval;
2488 * gchar *strval;
2489 * GObject *objval;
2491 * g_object_get (my_object,
2492 * "int-property", &intval,
2493 * "str-property", &strval,
2494 * "obj-property", &objval,
2495 * NULL);
2497 * // Do something with intval, strval, objval
2499 * g_free (strval);
2500 * g_object_unref (objval);
2501 * ]|
2503 void
2504 g_object_get (gpointer _object,
2505 const gchar *first_property_name,
2506 ...)
2508 GObject *object = _object;
2509 va_list var_args;
2511 g_return_if_fail (G_IS_OBJECT (object));
2513 va_start (var_args, first_property_name);
2514 g_object_get_valist (object, first_property_name, var_args);
2515 va_end (var_args);
2519 * g_object_set_property:
2520 * @object: a #GObject
2521 * @property_name: the name of the property to set
2522 * @value: the value
2524 * Sets a property on an object.
2526 void
2527 g_object_set_property (GObject *object,
2528 const gchar *property_name,
2529 const GValue *value)
2531 g_object_setv (object, 1, &property_name, value);
2535 * g_object_get_property:
2536 * @object: a #GObject
2537 * @property_name: the name of the property to get
2538 * @value: return location for the property value
2540 * Gets a property of an object. @value must have been initialized to the
2541 * expected type of the property (or a type to which the expected type can be
2542 * transformed) using g_value_init().
2544 * In general, a copy is made of the property contents and the caller is
2545 * responsible for freeing the memory by calling g_value_unset().
2547 * Note that g_object_get_property() is really intended for language
2548 * bindings, g_object_get() is much more convenient for C programming.
2550 void
2551 g_object_get_property (GObject *object,
2552 const gchar *property_name,
2553 GValue *value)
2555 GParamSpec *pspec;
2557 g_return_if_fail (G_IS_OBJECT (object));
2558 g_return_if_fail (property_name != NULL);
2559 g_return_if_fail (G_IS_VALUE (value));
2561 g_object_ref (object);
2563 pspec = g_param_spec_pool_lookup (pspec_pool,
2564 property_name,
2565 G_OBJECT_TYPE (object),
2566 TRUE);
2568 if (g_object_get_is_valid_property (object, pspec, property_name))
2570 GValue *prop_value, tmp_value = G_VALUE_INIT;
2572 /* auto-conversion of the callers value type
2574 if (G_VALUE_TYPE (value) == pspec->value_type)
2576 g_value_reset (value);
2577 prop_value = value;
2579 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2581 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2582 G_STRFUNC, pspec->name,
2583 g_type_name (pspec->value_type),
2584 G_VALUE_TYPE_NAME (value));
2585 g_object_unref (object);
2586 return;
2588 else
2590 g_value_init (&tmp_value, pspec->value_type);
2591 prop_value = &tmp_value;
2593 object_get_property (object, pspec, prop_value);
2594 if (prop_value != value)
2596 g_value_transform (prop_value, value);
2597 g_value_unset (&tmp_value);
2601 g_object_unref (object);
2605 * g_object_connect: (skip)
2606 * @object: (type GObject.Object): a #GObject
2607 * @signal_spec: the spec for the first signal
2608 * @...: #GCallback for the first signal, followed by data for the
2609 * first signal, followed optionally by more signal
2610 * spec/callback/data triples, followed by %NULL
2612 * A convenience function to connect multiple signals at once.
2614 * The signal specs expected by this function have the form
2615 * "modifier::signal_name", where modifier can be one of the following:
2616 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2617 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2618 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2619 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2620 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2621 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2622 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2623 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2625 * |[<!-- language="C" -->
2626 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2627 * "type", GTK_WINDOW_POPUP,
2628 * "child", menu,
2629 * NULL),
2630 * "signal::event", gtk_menu_window_event, menu,
2631 * "signal::size_request", gtk_menu_window_size_request, menu,
2632 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2633 * NULL);
2634 * ]|
2636 * Returns: (transfer none) (type GObject.Object): @object
2638 gpointer
2639 g_object_connect (gpointer _object,
2640 const gchar *signal_spec,
2641 ...)
2643 GObject *object = _object;
2644 va_list var_args;
2646 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2647 g_return_val_if_fail (object->ref_count > 0, object);
2649 va_start (var_args, signal_spec);
2650 while (signal_spec)
2652 GCallback callback = va_arg (var_args, GCallback);
2653 gpointer data = va_arg (var_args, gpointer);
2655 if (strncmp (signal_spec, "signal::", 8) == 0)
2656 g_signal_connect_data (object, signal_spec + 8,
2657 callback, data, NULL,
2659 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2660 strncmp (signal_spec, "object-signal::", 15) == 0)
2661 g_signal_connect_object (object, signal_spec + 15,
2662 callback, data,
2664 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2665 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2666 g_signal_connect_data (object, signal_spec + 16,
2667 callback, data, NULL,
2668 G_CONNECT_SWAPPED);
2669 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2670 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2671 g_signal_connect_object (object, signal_spec + 23,
2672 callback, data,
2673 G_CONNECT_SWAPPED);
2674 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2675 strncmp (signal_spec, "signal-after::", 14) == 0)
2676 g_signal_connect_data (object, signal_spec + 14,
2677 callback, data, NULL,
2678 G_CONNECT_AFTER);
2679 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2680 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2681 g_signal_connect_object (object, signal_spec + 21,
2682 callback, data,
2683 G_CONNECT_AFTER);
2684 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2685 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2686 g_signal_connect_data (object, signal_spec + 22,
2687 callback, data, NULL,
2688 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2689 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2690 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2691 g_signal_connect_object (object, signal_spec + 29,
2692 callback, data,
2693 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2694 else
2696 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2697 break;
2699 signal_spec = va_arg (var_args, gchar*);
2701 va_end (var_args);
2703 return object;
2707 * g_object_disconnect: (skip)
2708 * @object: (type GObject.Object): a #GObject
2709 * @signal_spec: the spec for the first signal
2710 * @...: #GCallback for the first signal, followed by data for the first signal,
2711 * followed optionally by more signal spec/callback/data triples,
2712 * followed by %NULL
2714 * A convenience function to disconnect multiple signals at once.
2716 * The signal specs expected by this function have the form
2717 * "any_signal", which means to disconnect any signal with matching
2718 * callback and data, or "any_signal::signal_name", which only
2719 * disconnects the signal named "signal_name".
2721 void
2722 g_object_disconnect (gpointer _object,
2723 const gchar *signal_spec,
2724 ...)
2726 GObject *object = _object;
2727 va_list var_args;
2729 g_return_if_fail (G_IS_OBJECT (object));
2730 g_return_if_fail (object->ref_count > 0);
2732 va_start (var_args, signal_spec);
2733 while (signal_spec)
2735 GCallback callback = va_arg (var_args, GCallback);
2736 gpointer data = va_arg (var_args, gpointer);
2737 guint sid = 0, detail = 0, mask = 0;
2739 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2740 strncmp (signal_spec, "any-signal::", 12) == 0)
2742 signal_spec += 12;
2743 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2745 else if (strcmp (signal_spec, "any_signal") == 0 ||
2746 strcmp (signal_spec, "any-signal") == 0)
2748 signal_spec += 10;
2749 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2751 else
2753 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2754 break;
2757 if ((mask & G_SIGNAL_MATCH_ID) &&
2758 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2759 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2760 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2761 sid, detail,
2762 NULL, (gpointer)callback, data))
2763 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2764 signal_spec = va_arg (var_args, gchar*);
2766 va_end (var_args);
2769 typedef struct {
2770 GObject *object;
2771 guint n_weak_refs;
2772 struct {
2773 GWeakNotify notify;
2774 gpointer data;
2775 } weak_refs[1]; /* flexible array */
2776 } WeakRefStack;
2778 static void
2779 weak_refs_notify (gpointer data)
2781 WeakRefStack *wstack = data;
2782 guint i;
2784 for (i = 0; i < wstack->n_weak_refs; i++)
2785 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2786 g_free (wstack);
2790 * g_object_weak_ref: (skip)
2791 * @object: #GObject to reference weakly
2792 * @notify: callback to invoke before the object is freed
2793 * @data: extra data to pass to notify
2795 * Adds a weak reference callback to an object. Weak references are
2796 * used for notification when an object is finalized. They are called
2797 * "weak references" because they allow you to safely hold a pointer
2798 * to an object without calling g_object_ref() (g_object_ref() adds a
2799 * strong reference, that is, forces the object to stay alive).
2801 * Note that the weak references created by this method are not
2802 * thread-safe: they cannot safely be used in one thread if the
2803 * object's last g_object_unref() might happen in another thread.
2804 * Use #GWeakRef if thread-safety is required.
2806 void
2807 g_object_weak_ref (GObject *object,
2808 GWeakNotify notify,
2809 gpointer data)
2811 WeakRefStack *wstack;
2812 guint i;
2814 g_return_if_fail (G_IS_OBJECT (object));
2815 g_return_if_fail (notify != NULL);
2816 g_return_if_fail (object->ref_count >= 1);
2818 G_LOCK (weak_refs_mutex);
2819 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2820 if (wstack)
2822 i = wstack->n_weak_refs++;
2823 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2825 else
2827 wstack = g_renew (WeakRefStack, NULL, 1);
2828 wstack->object = object;
2829 wstack->n_weak_refs = 1;
2830 i = 0;
2832 wstack->weak_refs[i].notify = notify;
2833 wstack->weak_refs[i].data = data;
2834 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2835 G_UNLOCK (weak_refs_mutex);
2839 * g_object_weak_unref: (skip)
2840 * @object: #GObject to remove a weak reference from
2841 * @notify: callback to search for
2842 * @data: data to search for
2844 * Removes a weak reference callback to an object.
2846 void
2847 g_object_weak_unref (GObject *object,
2848 GWeakNotify notify,
2849 gpointer data)
2851 WeakRefStack *wstack;
2852 gboolean found_one = FALSE;
2854 g_return_if_fail (G_IS_OBJECT (object));
2855 g_return_if_fail (notify != NULL);
2857 G_LOCK (weak_refs_mutex);
2858 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2859 if (wstack)
2861 guint i;
2863 for (i = 0; i < wstack->n_weak_refs; i++)
2864 if (wstack->weak_refs[i].notify == notify &&
2865 wstack->weak_refs[i].data == data)
2867 found_one = TRUE;
2868 wstack->n_weak_refs -= 1;
2869 if (i != wstack->n_weak_refs)
2870 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2872 break;
2875 G_UNLOCK (weak_refs_mutex);
2876 if (!found_one)
2877 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2881 * g_object_add_weak_pointer: (skip)
2882 * @object: The object that should be weak referenced.
2883 * @weak_pointer_location: (inout) (not optional): The memory address
2884 * of a pointer.
2886 * Adds a weak reference from weak_pointer to @object to indicate that
2887 * the pointer located at @weak_pointer_location is only valid during
2888 * the lifetime of @object. When the @object is finalized,
2889 * @weak_pointer will be set to %NULL.
2891 * Note that as with g_object_weak_ref(), the weak references created by
2892 * this method are not thread-safe: they cannot safely be used in one
2893 * thread if the object's last g_object_unref() might happen in another
2894 * thread. Use #GWeakRef if thread-safety is required.
2896 void
2897 g_object_add_weak_pointer (GObject *object,
2898 gpointer *weak_pointer_location)
2900 g_return_if_fail (G_IS_OBJECT (object));
2901 g_return_if_fail (weak_pointer_location != NULL);
2903 g_object_weak_ref (object,
2904 (GWeakNotify) g_nullify_pointer,
2905 weak_pointer_location);
2909 * g_object_remove_weak_pointer: (skip)
2910 * @object: The object that is weak referenced.
2911 * @weak_pointer_location: (inout) (not optional): The memory address
2912 * of a pointer.
2914 * Removes a weak reference from @object that was previously added
2915 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2916 * to match the one used with g_object_add_weak_pointer().
2918 void
2919 g_object_remove_weak_pointer (GObject *object,
2920 gpointer *weak_pointer_location)
2922 g_return_if_fail (G_IS_OBJECT (object));
2923 g_return_if_fail (weak_pointer_location != NULL);
2925 g_object_weak_unref (object,
2926 (GWeakNotify) g_nullify_pointer,
2927 weak_pointer_location);
2930 static guint
2931 object_floating_flag_handler (GObject *object,
2932 gint job)
2934 switch (job)
2936 gpointer oldvalue;
2937 case +1: /* force floating if possible */
2939 oldvalue = g_atomic_pointer_get (&object->qdata);
2940 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2941 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2942 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2943 case -1: /* sink if possible */
2945 oldvalue = g_atomic_pointer_get (&object->qdata);
2946 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2947 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2948 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2949 default: /* check floating */
2950 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2955 * g_object_is_floating:
2956 * @object: (type GObject.Object): a #GObject
2958 * Checks whether @object has a [floating][floating-ref] reference.
2960 * Since: 2.10
2962 * Returns: %TRUE if @object has a floating reference
2964 gboolean
2965 g_object_is_floating (gpointer _object)
2967 GObject *object = _object;
2968 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2969 return floating_flag_handler (object, 0);
2973 * g_object_ref_sink:
2974 * @object: (type GObject.Object): a #GObject
2976 * Increase the reference count of @object, and possibly remove the
2977 * [floating][floating-ref] reference, if @object has a floating reference.
2979 * In other words, if the object is floating, then this call "assumes
2980 * ownership" of the floating reference, converting it to a normal
2981 * reference by clearing the floating flag while leaving the reference
2982 * count unchanged. If the object is not floating, then this call
2983 * adds a new normal reference increasing the reference count by one.
2985 * Since GLib 2.56, the type of @object will be propagated to the return type
2986 * under the same conditions as for g_object_ref().
2988 * Since: 2.10
2990 * Returns: (type GObject.Object) (transfer none): @object
2992 gpointer
2993 (g_object_ref_sink) (gpointer _object)
2995 GObject *object = _object;
2996 gboolean was_floating;
2997 g_return_val_if_fail (G_IS_OBJECT (object), object);
2998 g_return_val_if_fail (object->ref_count >= 1, object);
2999 g_object_ref (object);
3000 was_floating = floating_flag_handler (object, -1);
3001 if (was_floating)
3002 g_object_unref (object);
3003 return object;
3007 * g_object_force_floating:
3008 * @object: a #GObject
3010 * This function is intended for #GObject implementations to re-enforce
3011 * a [floating][floating-ref] object reference. Doing this is seldom
3012 * required: all #GInitiallyUnowneds are created with a floating reference
3013 * which usually just needs to be sunken by calling g_object_ref_sink().
3015 * Since: 2.10
3017 void
3018 g_object_force_floating (GObject *object)
3020 g_return_if_fail (G_IS_OBJECT (object));
3021 g_return_if_fail (object->ref_count >= 1);
3023 floating_flag_handler (object, +1);
3026 typedef struct {
3027 GObject *object;
3028 guint n_toggle_refs;
3029 struct {
3030 GToggleNotify notify;
3031 gpointer data;
3032 } toggle_refs[1]; /* flexible array */
3033 } ToggleRefStack;
3035 static void
3036 toggle_refs_notify (GObject *object,
3037 gboolean is_last_ref)
3039 ToggleRefStack tstack, *tstackptr;
3041 G_LOCK (toggle_refs_mutex);
3042 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3043 tstack = *tstackptr;
3044 G_UNLOCK (toggle_refs_mutex);
3046 /* Reentrancy here is not as tricky as it seems, because a toggle reference
3047 * will only be notified when there is exactly one of them.
3049 g_assert (tstack.n_toggle_refs == 1);
3050 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
3054 * g_object_add_toggle_ref: (skip)
3055 * @object: a #GObject
3056 * @notify: a function to call when this reference is the
3057 * last reference to the object, or is no longer
3058 * the last reference.
3059 * @data: data to pass to @notify
3061 * Increases the reference count of the object by one and sets a
3062 * callback to be called when all other references to the object are
3063 * dropped, or when this is already the last reference to the object
3064 * and another reference is established.
3066 * This functionality is intended for binding @object to a proxy
3067 * object managed by another memory manager. This is done with two
3068 * paired references: the strong reference added by
3069 * g_object_add_toggle_ref() and a reverse reference to the proxy
3070 * object which is either a strong reference or weak reference.
3072 * The setup is that when there are no other references to @object,
3073 * only a weak reference is held in the reverse direction from @object
3074 * to the proxy object, but when there are other references held to
3075 * @object, a strong reference is held. The @notify callback is called
3076 * when the reference from @object to the proxy object should be
3077 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
3078 * (@is_last_ref false).
3080 * Since a (normal) reference must be held to the object before
3081 * calling g_object_add_toggle_ref(), the initial state of the reverse
3082 * link is always strong.
3084 * Multiple toggle references may be added to the same gobject,
3085 * however if there are multiple toggle references to an object, none
3086 * of them will ever be notified until all but one are removed. For
3087 * this reason, you should only ever use a toggle reference if there
3088 * is important state in the proxy object.
3090 * Since: 2.8
3092 void
3093 g_object_add_toggle_ref (GObject *object,
3094 GToggleNotify notify,
3095 gpointer data)
3097 ToggleRefStack *tstack;
3098 guint i;
3100 g_return_if_fail (G_IS_OBJECT (object));
3101 g_return_if_fail (notify != NULL);
3102 g_return_if_fail (object->ref_count >= 1);
3104 g_object_ref (object);
3106 G_LOCK (toggle_refs_mutex);
3107 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
3108 if (tstack)
3110 i = tstack->n_toggle_refs++;
3111 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
3112 * in tstate->toggle_refs */
3113 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
3115 else
3117 tstack = g_renew (ToggleRefStack, NULL, 1);
3118 tstack->object = object;
3119 tstack->n_toggle_refs = 1;
3120 i = 0;
3123 /* Set a flag for fast lookup after adding the first toggle reference */
3124 if (tstack->n_toggle_refs == 1)
3125 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3127 tstack->toggle_refs[i].notify = notify;
3128 tstack->toggle_refs[i].data = data;
3129 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
3130 (GDestroyNotify)g_free);
3131 G_UNLOCK (toggle_refs_mutex);
3135 * g_object_remove_toggle_ref: (skip)
3136 * @object: a #GObject
3137 * @notify: a function to call when this reference is the
3138 * last reference to the object, or is no longer
3139 * the last reference.
3140 * @data: data to pass to @notify
3142 * Removes a reference added with g_object_add_toggle_ref(). The
3143 * reference count of the object is decreased by one.
3145 * Since: 2.8
3147 void
3148 g_object_remove_toggle_ref (GObject *object,
3149 GToggleNotify notify,
3150 gpointer data)
3152 ToggleRefStack *tstack;
3153 gboolean found_one = FALSE;
3155 g_return_if_fail (G_IS_OBJECT (object));
3156 g_return_if_fail (notify != NULL);
3158 G_LOCK (toggle_refs_mutex);
3159 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3160 if (tstack)
3162 guint i;
3164 for (i = 0; i < tstack->n_toggle_refs; i++)
3165 if (tstack->toggle_refs[i].notify == notify &&
3166 tstack->toggle_refs[i].data == data)
3168 found_one = TRUE;
3169 tstack->n_toggle_refs -= 1;
3170 if (i != tstack->n_toggle_refs)
3171 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3173 if (tstack->n_toggle_refs == 0)
3174 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3176 break;
3179 G_UNLOCK (toggle_refs_mutex);
3181 if (found_one)
3182 g_object_unref (object);
3183 else
3184 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3188 * g_object_ref:
3189 * @object: (type GObject.Object): a #GObject
3191 * Increases the reference count of @object.
3193 * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type
3194 * of @object will be propagated to the return type (using the GCC typeof()
3195 * extension), so any casting the caller needs to do on the return type must be
3196 * explicit.
3198 * Returns: (type GObject.Object) (transfer none): the same @object
3200 gpointer
3201 (g_object_ref) (gpointer _object)
3203 GObject *object = _object;
3204 gint old_val;
3206 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3207 g_return_val_if_fail (object->ref_count > 0, NULL);
3209 old_val = g_atomic_int_add (&object->ref_count, 1);
3211 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3212 toggle_refs_notify (object, FALSE);
3214 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3216 return object;
3220 * g_object_unref:
3221 * @object: (type GObject.Object): a #GObject
3223 * Decreases the reference count of @object. When its reference count
3224 * drops to 0, the object is finalized (i.e. its memory is freed).
3226 * If the pointer to the #GObject may be reused in future (for example, if it is
3227 * an instance variable of another object), it is recommended to clear the
3228 * pointer to %NULL rather than retain a dangling pointer to a potentially
3229 * invalid #GObject instance. Use g_clear_object() for this.
3231 void
3232 g_object_unref (gpointer _object)
3234 GObject *object = _object;
3235 gint old_ref;
3237 g_return_if_fail (G_IS_OBJECT (object));
3238 g_return_if_fail (object->ref_count > 0);
3240 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3241 retry_atomic_decrement1:
3242 old_ref = g_atomic_int_get (&object->ref_count);
3243 if (old_ref > 1)
3245 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3246 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3248 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3249 goto retry_atomic_decrement1;
3251 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3253 /* if we went from 2->1 we need to notify toggle refs if any */
3254 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3255 toggle_refs_notify (object, TRUE);
3257 else
3259 GSList **weak_locations;
3261 /* The only way that this object can live at this point is if
3262 * there are outstanding weak references already established
3263 * before we got here.
3265 * If there were not already weak references then no more can be
3266 * established at this time, because the other thread would have
3267 * to hold a strong ref in order to call
3268 * g_object_add_weak_pointer() and then we wouldn't be here.
3270 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3272 if (weak_locations != NULL)
3274 g_rw_lock_writer_lock (&weak_locations_lock);
3276 /* It is possible that one of the weak references beat us to
3277 * the lock. Make sure the refcount is still what we expected
3278 * it to be.
3280 old_ref = g_atomic_int_get (&object->ref_count);
3281 if (old_ref != 1)
3283 g_rw_lock_writer_unlock (&weak_locations_lock);
3284 goto retry_atomic_decrement1;
3287 /* We got the lock first, so the object will definitely die
3288 * now. Clear out all the weak references.
3290 while (*weak_locations)
3292 GWeakRef *weak_ref_location = (*weak_locations)->data;
3294 weak_ref_location->priv.p = NULL;
3295 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3298 g_rw_lock_writer_unlock (&weak_locations_lock);
3301 /* we are about to remove the last reference */
3302 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3303 G_OBJECT_GET_CLASS (object)->dispose (object);
3304 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3306 /* may have been re-referenced meanwhile */
3307 retry_atomic_decrement2:
3308 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3309 if (old_ref > 1)
3311 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3312 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3314 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3315 goto retry_atomic_decrement2;
3317 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3319 /* if we went from 2->1 we need to notify toggle refs if any */
3320 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3321 toggle_refs_notify (object, TRUE);
3323 return;
3326 /* we are still in the process of taking away the last ref */
3327 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3328 g_signal_handlers_destroy (object);
3329 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3331 /* decrement the last reference */
3332 old_ref = g_atomic_int_add (&object->ref_count, -1);
3334 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3336 /* may have been re-referenced meanwhile */
3337 if (G_LIKELY (old_ref == 1))
3339 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3340 G_OBJECT_GET_CLASS (object)->finalize (object);
3342 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3344 GOBJECT_IF_DEBUG (OBJECTS,
3346 /* catch objects not chaining finalize handlers */
3347 G_LOCK (debug_objects);
3348 g_assert (!g_hash_table_contains (debug_objects_ht, object));
3349 G_UNLOCK (debug_objects);
3351 g_type_free_instance ((GTypeInstance*) object);
3357 * g_clear_object: (skip)
3358 * @object_ptr: a pointer to a #GObject reference
3360 * Clears a reference to a #GObject.
3362 * @object_ptr must not be %NULL.
3364 * If the reference is %NULL then this function does nothing.
3365 * Otherwise, the reference count of the object is decreased and the
3366 * pointer is set to %NULL.
3368 * A macro is also included that allows this function to be used without
3369 * pointer casts.
3371 * Since: 2.28
3373 #undef g_clear_object
3374 void
3375 g_clear_object (volatile GObject **object_ptr)
3377 g_clear_pointer (object_ptr, g_object_unref);
3381 * g_object_get_qdata:
3382 * @object: The GObject to get a stored user data pointer from
3383 * @quark: A #GQuark, naming the user data pointer
3385 * This function gets back user data pointers stored via
3386 * g_object_set_qdata().
3388 * Returns: (transfer none) (nullable): The user data pointer set, or %NULL
3390 gpointer
3391 g_object_get_qdata (GObject *object,
3392 GQuark quark)
3394 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3396 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3400 * g_object_set_qdata: (skip)
3401 * @object: The GObject to set store a user data pointer
3402 * @quark: A #GQuark, naming the user data pointer
3403 * @data: (nullable): An opaque user data pointer
3405 * This sets an opaque, named pointer on an object.
3406 * The name is specified through a #GQuark (retrived e.g. via
3407 * g_quark_from_static_string()), and the pointer
3408 * can be gotten back from the @object with g_object_get_qdata()
3409 * until the @object is finalized.
3410 * Setting a previously set user data pointer, overrides (frees)
3411 * the old pointer set, using #NULL as pointer essentially
3412 * removes the data stored.
3414 void
3415 g_object_set_qdata (GObject *object,
3416 GQuark quark,
3417 gpointer data)
3419 g_return_if_fail (G_IS_OBJECT (object));
3420 g_return_if_fail (quark > 0);
3422 g_datalist_id_set_data (&object->qdata, quark, data);
3426 * g_object_dup_qdata: (skip)
3427 * @object: the #GObject to store user data on
3428 * @quark: a #GQuark, naming the user data pointer
3429 * @dup_func: (nullable): function to dup the value
3430 * @user_data: (nullable): passed as user_data to @dup_func
3432 * This is a variant of g_object_get_qdata() which returns
3433 * a 'duplicate' of the value. @dup_func defines the
3434 * meaning of 'duplicate' in this context, it could e.g.
3435 * take a reference on a ref-counted object.
3437 * If the @quark is not set on the object then @dup_func
3438 * will be called with a %NULL argument.
3440 * Note that @dup_func is called while user data of @object
3441 * is locked.
3443 * This function can be useful to avoid races when multiple
3444 * threads are using object data on the same key on the same
3445 * object.
3447 * Returns: the result of calling @dup_func on the value
3448 * associated with @quark on @object, or %NULL if not set.
3449 * If @dup_func is %NULL, the value is returned
3450 * unmodified.
3452 * Since: 2.34
3454 gpointer
3455 g_object_dup_qdata (GObject *object,
3456 GQuark quark,
3457 GDuplicateFunc dup_func,
3458 gpointer user_data)
3460 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3461 g_return_val_if_fail (quark > 0, NULL);
3463 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3467 * g_object_replace_qdata: (skip)
3468 * @object: the #GObject to store user data on
3469 * @quark: a #GQuark, naming the user data pointer
3470 * @oldval: (nullable): the old value to compare against
3471 * @newval: (nullable): the new value
3472 * @destroy: (nullable): a destroy notify for the new value
3473 * @old_destroy: (out) (optional): destroy notify for the existing value
3475 * Compares the user data for the key @quark on @object with
3476 * @oldval, and if they are the same, replaces @oldval with
3477 * @newval.
3479 * This is like a typical atomic compare-and-exchange
3480 * operation, for user data on an object.
3482 * If the previous value was replaced then ownership of the
3483 * old value (@oldval) is passed to the caller, including
3484 * the registered destroy notify for it (passed out in @old_destroy).
3485 * It’s up to the caller to free this as needed, which may
3486 * or may not include using @old_destroy as sometimes replacement
3487 * should not destroy the object in the normal way.
3489 * Returns: %TRUE if the existing value for @quark was replaced
3490 * by @newval, %FALSE otherwise.
3492 * Since: 2.34
3494 gboolean
3495 g_object_replace_qdata (GObject *object,
3496 GQuark quark,
3497 gpointer oldval,
3498 gpointer newval,
3499 GDestroyNotify destroy,
3500 GDestroyNotify *old_destroy)
3502 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3503 g_return_val_if_fail (quark > 0, FALSE);
3505 return g_datalist_id_replace_data (&object->qdata, quark,
3506 oldval, newval, destroy,
3507 old_destroy);
3511 * g_object_set_qdata_full: (skip)
3512 * @object: The GObject to set store a user data pointer
3513 * @quark: A #GQuark, naming the user data pointer
3514 * @data: (nullable): An opaque user data pointer
3515 * @destroy: (nullable): Function to invoke with @data as argument, when @data
3516 * needs to be freed
3518 * This function works like g_object_set_qdata(), but in addition,
3519 * a void (*destroy) (gpointer) function may be specified which is
3520 * called with @data as argument when the @object is finalized, or
3521 * the data is being overwritten by a call to g_object_set_qdata()
3522 * with the same @quark.
3524 void
3525 g_object_set_qdata_full (GObject *object,
3526 GQuark quark,
3527 gpointer data,
3528 GDestroyNotify destroy)
3530 g_return_if_fail (G_IS_OBJECT (object));
3531 g_return_if_fail (quark > 0);
3533 g_datalist_id_set_data_full (&object->qdata, quark, data,
3534 data ? destroy : (GDestroyNotify) NULL);
3538 * g_object_steal_qdata:
3539 * @object: The GObject to get a stored user data pointer from
3540 * @quark: A #GQuark, naming the user data pointer
3542 * This function gets back user data pointers stored via
3543 * g_object_set_qdata() and removes the @data from object
3544 * without invoking its destroy() function (if any was
3545 * set).
3546 * Usually, calling this function is only required to update
3547 * user data pointers with a destroy notifier, for example:
3548 * |[<!-- language="C" -->
3549 * void
3550 * object_add_to_user_list (GObject *object,
3551 * const gchar *new_string)
3553 * // the quark, naming the object data
3554 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3555 * // retrive the old string list
3556 * GList *list = g_object_steal_qdata (object, quark_string_list);
3558 * // prepend new string
3559 * list = g_list_prepend (list, g_strdup (new_string));
3560 * // this changed 'list', so we need to set it again
3561 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3563 * static void
3564 * free_string_list (gpointer data)
3566 * GList *node, *list = data;
3568 * for (node = list; node; node = node->next)
3569 * g_free (node->data);
3570 * g_list_free (list);
3572 * ]|
3573 * Using g_object_get_qdata() in the above example, instead of
3574 * g_object_steal_qdata() would have left the destroy function set,
3575 * and thus the partial string list would have been freed upon
3576 * g_object_set_qdata_full().
3578 * Returns: (transfer full) (nullable): The user data pointer set, or %NULL
3580 gpointer
3581 g_object_steal_qdata (GObject *object,
3582 GQuark quark)
3584 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3585 g_return_val_if_fail (quark > 0, NULL);
3587 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3591 * g_object_get_data:
3592 * @object: #GObject containing the associations
3593 * @key: name of the key for that association
3595 * Gets a named field from the objects table of associations (see g_object_set_data()).
3597 * Returns: (transfer none) (nullable): the data if found,
3598 * or %NULL if no such data exists.
3600 gpointer
3601 g_object_get_data (GObject *object,
3602 const gchar *key)
3604 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3605 g_return_val_if_fail (key != NULL, NULL);
3607 return g_datalist_get_data (&object->qdata, key);
3611 * g_object_set_data:
3612 * @object: #GObject containing the associations.
3613 * @key: name of the key
3614 * @data: (nullable): data to associate with that key
3616 * Each object carries around a table of associations from
3617 * strings to pointers. This function lets you set an association.
3619 * If the object already had an association with that name,
3620 * the old association will be destroyed.
3622 void
3623 g_object_set_data (GObject *object,
3624 const gchar *key,
3625 gpointer data)
3627 g_return_if_fail (G_IS_OBJECT (object));
3628 g_return_if_fail (key != NULL);
3630 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3634 * g_object_dup_data: (skip)
3635 * @object: the #GObject to store user data on
3636 * @key: a string, naming the user data pointer
3637 * @dup_func: (nullable): function to dup the value
3638 * @user_data: (nullable): passed as user_data to @dup_func
3640 * This is a variant of g_object_get_data() which returns
3641 * a 'duplicate' of the value. @dup_func defines the
3642 * meaning of 'duplicate' in this context, it could e.g.
3643 * take a reference on a ref-counted object.
3645 * If the @key is not set on the object then @dup_func
3646 * will be called with a %NULL argument.
3648 * Note that @dup_func is called while user data of @object
3649 * is locked.
3651 * This function can be useful to avoid races when multiple
3652 * threads are using object data on the same key on the same
3653 * object.
3655 * Returns: the result of calling @dup_func on the value
3656 * associated with @key on @object, or %NULL if not set.
3657 * If @dup_func is %NULL, the value is returned
3658 * unmodified.
3660 * Since: 2.34
3662 gpointer
3663 g_object_dup_data (GObject *object,
3664 const gchar *key,
3665 GDuplicateFunc dup_func,
3666 gpointer user_data)
3668 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3669 g_return_val_if_fail (key != NULL, NULL);
3671 return g_datalist_id_dup_data (&object->qdata,
3672 g_quark_from_string (key),
3673 dup_func, user_data);
3677 * g_object_replace_data: (skip)
3678 * @object: the #GObject to store user data on
3679 * @key: a string, naming the user data pointer
3680 * @oldval: (nullable): the old value to compare against
3681 * @newval: (nullable): the new value
3682 * @destroy: (nullable): a destroy notify for the new value
3683 * @old_destroy: (out) (optional): destroy notify for the existing value
3685 * Compares the user data for the key @key on @object with
3686 * @oldval, and if they are the same, replaces @oldval with
3687 * @newval.
3689 * This is like a typical atomic compare-and-exchange
3690 * operation, for user data on an object.
3692 * If the previous value was replaced then ownership of the
3693 * old value (@oldval) is passed to the caller, including
3694 * the registered destroy notify for it (passed out in @old_destroy).
3695 * It’s up to the caller to free this as needed, which may
3696 * or may not include using @old_destroy as sometimes replacement
3697 * should not destroy the object in the normal way.
3699 * Returns: %TRUE if the existing value for @key was replaced
3700 * by @newval, %FALSE otherwise.
3702 * Since: 2.34
3704 gboolean
3705 g_object_replace_data (GObject *object,
3706 const gchar *key,
3707 gpointer oldval,
3708 gpointer newval,
3709 GDestroyNotify destroy,
3710 GDestroyNotify *old_destroy)
3712 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3713 g_return_val_if_fail (key != NULL, FALSE);
3715 return g_datalist_id_replace_data (&object->qdata,
3716 g_quark_from_string (key),
3717 oldval, newval, destroy,
3718 old_destroy);
3722 * g_object_set_data_full: (skip)
3723 * @object: #GObject containing the associations
3724 * @key: name of the key
3725 * @data: (nullable): data to associate with that key
3726 * @destroy: (nullable): function to call when the association is destroyed
3728 * Like g_object_set_data() except it adds notification
3729 * for when the association is destroyed, either by setting it
3730 * to a different value or when the object is destroyed.
3732 * Note that the @destroy callback is not called if @data is %NULL.
3734 void
3735 g_object_set_data_full (GObject *object,
3736 const gchar *key,
3737 gpointer data,
3738 GDestroyNotify destroy)
3740 g_return_if_fail (G_IS_OBJECT (object));
3741 g_return_if_fail (key != NULL);
3743 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3744 data ? destroy : (GDestroyNotify) NULL);
3748 * g_object_steal_data:
3749 * @object: #GObject containing the associations
3750 * @key: name of the key
3752 * Remove a specified datum from the object's data associations,
3753 * without invoking the association's destroy handler.
3755 * Returns: (transfer full) (nullable): the data if found, or %NULL
3756 * if no such data exists.
3758 gpointer
3759 g_object_steal_data (GObject *object,
3760 const gchar *key)
3762 GQuark quark;
3764 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3765 g_return_val_if_fail (key != NULL, NULL);
3767 quark = g_quark_try_string (key);
3769 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3772 static void
3773 g_value_object_init (GValue *value)
3775 value->data[0].v_pointer = NULL;
3778 static void
3779 g_value_object_free_value (GValue *value)
3781 if (value->data[0].v_pointer)
3782 g_object_unref (value->data[0].v_pointer);
3785 static void
3786 g_value_object_copy_value (const GValue *src_value,
3787 GValue *dest_value)
3789 if (src_value->data[0].v_pointer)
3790 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3791 else
3792 dest_value->data[0].v_pointer = NULL;
3795 static void
3796 g_value_object_transform_value (const GValue *src_value,
3797 GValue *dest_value)
3799 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)))
3800 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3801 else
3802 dest_value->data[0].v_pointer = NULL;
3805 static gpointer
3806 g_value_object_peek_pointer (const GValue *value)
3808 return value->data[0].v_pointer;
3811 static gchar*
3812 g_value_object_collect_value (GValue *value,
3813 guint n_collect_values,
3814 GTypeCValue *collect_values,
3815 guint collect_flags)
3817 if (collect_values[0].v_pointer)
3819 GObject *object = collect_values[0].v_pointer;
3821 if (object->g_type_instance.g_class == NULL)
3822 return g_strconcat ("invalid unclassed object pointer for value type '",
3823 G_VALUE_TYPE_NAME (value),
3824 "'",
3825 NULL);
3826 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3827 return g_strconcat ("invalid object type '",
3828 G_OBJECT_TYPE_NAME (object),
3829 "' for value type '",
3830 G_VALUE_TYPE_NAME (value),
3831 "'",
3832 NULL);
3833 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3834 value->data[0].v_pointer = g_object_ref (object);
3836 else
3837 value->data[0].v_pointer = NULL;
3839 return NULL;
3842 static gchar*
3843 g_value_object_lcopy_value (const GValue *value,
3844 guint n_collect_values,
3845 GTypeCValue *collect_values,
3846 guint collect_flags)
3848 GObject **object_p = collect_values[0].v_pointer;
3850 if (!object_p)
3851 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3853 if (!value->data[0].v_pointer)
3854 *object_p = NULL;
3855 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3856 *object_p = value->data[0].v_pointer;
3857 else
3858 *object_p = g_object_ref (value->data[0].v_pointer);
3860 return NULL;
3864 * g_value_set_object:
3865 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3866 * @v_object: (type GObject.Object) (nullable): object value to be set
3868 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3870 * g_value_set_object() increases the reference count of @v_object
3871 * (the #GValue holds a reference to @v_object). If you do not wish
3872 * to increase the reference count of the object (i.e. you wish to
3873 * pass your current reference to the #GValue because you no longer
3874 * need it), use g_value_take_object() instead.
3876 * It is important that your #GValue holds a reference to @v_object (either its
3877 * own, or one it has taken) to ensure that the object won't be destroyed while
3878 * the #GValue still exists).
3880 void
3881 g_value_set_object (GValue *value,
3882 gpointer v_object)
3884 GObject *old;
3886 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3888 old = value->data[0].v_pointer;
3890 if (v_object)
3892 g_return_if_fail (G_IS_OBJECT (v_object));
3893 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3895 value->data[0].v_pointer = v_object;
3896 g_object_ref (value->data[0].v_pointer);
3898 else
3899 value->data[0].v_pointer = NULL;
3901 if (old)
3902 g_object_unref (old);
3906 * g_value_set_object_take_ownership: (skip)
3907 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3908 * @v_object: (nullable): object value to be set
3910 * This is an internal function introduced mainly for C marshallers.
3912 * Deprecated: 2.4: Use g_value_take_object() instead.
3914 void
3915 g_value_set_object_take_ownership (GValue *value,
3916 gpointer v_object)
3918 g_value_take_object (value, v_object);
3922 * g_value_take_object: (skip)
3923 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3924 * @v_object: (nullable): object value to be set
3926 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3927 * and takes over the ownership of the callers reference to @v_object;
3928 * the caller doesn't have to unref it any more (i.e. the reference
3929 * count of the object is not increased).
3931 * If you want the #GValue to hold its own reference to @v_object, use
3932 * g_value_set_object() instead.
3934 * Since: 2.4
3936 void
3937 g_value_take_object (GValue *value,
3938 gpointer v_object)
3940 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3942 if (value->data[0].v_pointer)
3944 g_object_unref (value->data[0].v_pointer);
3945 value->data[0].v_pointer = NULL;
3948 if (v_object)
3950 g_return_if_fail (G_IS_OBJECT (v_object));
3951 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3953 value->data[0].v_pointer = v_object; /* we take over the reference count */
3958 * g_value_get_object:
3959 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3961 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3963 * Returns: (type GObject.Object) (transfer none): object contents of @value
3965 gpointer
3966 g_value_get_object (const GValue *value)
3968 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3970 return value->data[0].v_pointer;
3974 * g_value_dup_object:
3975 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3977 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3978 * its reference count. If the contents of the #GValue are %NULL, then
3979 * %NULL will be returned.
3981 * Returns: (type GObject.Object) (transfer full): object content of @value,
3982 * should be unreferenced when no longer needed.
3984 gpointer
3985 g_value_dup_object (const GValue *value)
3987 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3989 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3993 * g_signal_connect_object: (skip)
3994 * @instance: (type GObject.TypeInstance): the instance to connect to.
3995 * @detailed_signal: a string of the form "signal-name::detail".
3996 * @c_handler: the #GCallback to connect.
3997 * @gobject: (type GObject.Object) (nullable): the object to pass as data
3998 * to @c_handler.
3999 * @connect_flags: a combination of #GConnectFlags.
4001 * This is similar to g_signal_connect_data(), but uses a closure which
4002 * ensures that the @gobject stays alive during the call to @c_handler
4003 * by temporarily adding a reference count to @gobject.
4005 * When the @gobject is destroyed the signal handler will be automatically
4006 * disconnected. Note that this is not currently threadsafe (ie:
4007 * emitting a signal while @gobject is being destroyed in another thread
4008 * is not safe).
4010 * Returns: the handler id.
4012 gulong
4013 g_signal_connect_object (gpointer instance,
4014 const gchar *detailed_signal,
4015 GCallback c_handler,
4016 gpointer gobject,
4017 GConnectFlags connect_flags)
4019 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
4020 g_return_val_if_fail (detailed_signal != NULL, 0);
4021 g_return_val_if_fail (c_handler != NULL, 0);
4023 if (gobject)
4025 GClosure *closure;
4027 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
4029 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
4031 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
4033 else
4034 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
4037 typedef struct {
4038 GObject *object;
4039 guint n_closures;
4040 GClosure *closures[1]; /* flexible array */
4041 } CArray;
4042 /* don't change this structure without supplying an accessor for
4043 * watched closures, e.g.:
4044 * GSList* g_object_list_watched_closures (GObject *object)
4046 * CArray *carray;
4047 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4048 * carray = g_object_get_data (object, "GObject-closure-array");
4049 * if (carray)
4051 * GSList *slist = NULL;
4052 * guint i;
4053 * for (i = 0; i < carray->n_closures; i++)
4054 * slist = g_slist_prepend (slist, carray->closures[i]);
4055 * return slist;
4057 * return NULL;
4061 static void
4062 object_remove_closure (gpointer data,
4063 GClosure *closure)
4065 GObject *object = data;
4066 CArray *carray;
4067 guint i;
4069 G_LOCK (closure_array_mutex);
4070 carray = g_object_get_qdata (object, quark_closure_array);
4071 for (i = 0; i < carray->n_closures; i++)
4072 if (carray->closures[i] == closure)
4074 carray->n_closures--;
4075 if (i < carray->n_closures)
4076 carray->closures[i] = carray->closures[carray->n_closures];
4077 G_UNLOCK (closure_array_mutex);
4078 return;
4080 G_UNLOCK (closure_array_mutex);
4081 g_assert_not_reached ();
4084 static void
4085 destroy_closure_array (gpointer data)
4087 CArray *carray = data;
4088 GObject *object = carray->object;
4089 guint i, n = carray->n_closures;
4091 for (i = 0; i < n; i++)
4093 GClosure *closure = carray->closures[i];
4095 /* removing object_remove_closure() upfront is probably faster than
4096 * letting it fiddle with quark_closure_array which is empty anyways
4098 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
4099 g_closure_invalidate (closure);
4101 g_free (carray);
4105 * g_object_watch_closure:
4106 * @object: GObject restricting lifetime of @closure
4107 * @closure: GClosure to watch
4109 * This function essentially limits the life time of the @closure to
4110 * the life time of the object. That is, when the object is finalized,
4111 * the @closure is invalidated by calling g_closure_invalidate() on
4112 * it, in order to prevent invocations of the closure with a finalized
4113 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
4114 * added as marshal guards to the @closure, to ensure that an extra
4115 * reference count is held on @object during invocation of the
4116 * @closure. Usually, this function will be called on closures that
4117 * use this @object as closure data.
4119 void
4120 g_object_watch_closure (GObject *object,
4121 GClosure *closure)
4123 CArray *carray;
4124 guint i;
4126 g_return_if_fail (G_IS_OBJECT (object));
4127 g_return_if_fail (closure != NULL);
4128 g_return_if_fail (closure->is_invalid == FALSE);
4129 g_return_if_fail (closure->in_marshal == FALSE);
4130 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
4132 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
4133 g_closure_add_marshal_guards (closure,
4134 object, (GClosureNotify) g_object_ref,
4135 object, (GClosureNotify) g_object_unref);
4136 G_LOCK (closure_array_mutex);
4137 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
4138 if (!carray)
4140 carray = g_renew (CArray, NULL, 1);
4141 carray->object = object;
4142 carray->n_closures = 1;
4143 i = 0;
4145 else
4147 i = carray->n_closures++;
4148 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4150 carray->closures[i] = closure;
4151 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4152 G_UNLOCK (closure_array_mutex);
4156 * g_closure_new_object:
4157 * @sizeof_closure: the size of the structure to allocate, must be at least
4158 * `sizeof (GClosure)`
4159 * @object: a #GObject pointer to store in the @data field of the newly
4160 * allocated #GClosure
4162 * A variant of g_closure_new_simple() which stores @object in the
4163 * @data field of the closure and calls g_object_watch_closure() on
4164 * @object and the created closure. This function is mainly useful
4165 * when implementing new types of closures.
4167 * Returns: (transfer full): a newly allocated #GClosure
4169 GClosure*
4170 g_closure_new_object (guint sizeof_closure,
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 */
4178 closure = g_closure_new_simple (sizeof_closure, object);
4179 g_object_watch_closure (object, closure);
4181 return closure;
4185 * g_cclosure_new_object: (skip)
4186 * @callback_func: the function to invoke
4187 * @object: a #GObject pointer to pass to @callback_func
4189 * A variant of g_cclosure_new() which uses @object as @user_data and
4190 * calls g_object_watch_closure() on @object and the created
4191 * closure. This function is useful when you have a callback closely
4192 * associated with a #GObject, and want the callback to no longer run
4193 * after the object is is freed.
4195 * Returns: a new #GCClosure
4197 GClosure*
4198 g_cclosure_new_object (GCallback callback_func,
4199 GObject *object)
4201 GClosure *closure;
4203 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4204 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4205 g_return_val_if_fail (callback_func != NULL, NULL);
4207 closure = g_cclosure_new (callback_func, object, NULL);
4208 g_object_watch_closure (object, closure);
4210 return closure;
4214 * g_cclosure_new_object_swap: (skip)
4215 * @callback_func: the function to invoke
4216 * @object: a #GObject pointer to pass to @callback_func
4218 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4219 * and calls g_object_watch_closure() on @object and the created
4220 * closure. This function is useful when you have a callback closely
4221 * associated with a #GObject, and want the callback to no longer run
4222 * after the object is is freed.
4224 * Returns: a new #GCClosure
4226 GClosure*
4227 g_cclosure_new_object_swap (GCallback callback_func,
4228 GObject *object)
4230 GClosure *closure;
4232 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4233 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4234 g_return_val_if_fail (callback_func != NULL, NULL);
4236 closure = g_cclosure_new_swap (callback_func, object, NULL);
4237 g_object_watch_closure (object, closure);
4239 return closure;
4242 gsize
4243 g_object_compat_control (gsize what,
4244 gpointer data)
4246 switch (what)
4248 gpointer *pp;
4249 case 1: /* floating base type */
4250 return G_TYPE_INITIALLY_UNOWNED;
4251 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4252 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4253 return 1;
4254 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4255 pp = data;
4256 *pp = floating_flag_handler;
4257 return 1;
4258 default:
4259 return 0;
4263 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
4265 static void
4266 g_initially_unowned_init (GInitiallyUnowned *object)
4268 g_object_force_floating (object);
4271 static void
4272 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4277 * GWeakRef:
4279 * A structure containing a weak reference to a #GObject. It can either
4280 * be empty (i.e. point to %NULL), or point to an object for as long as
4281 * at least one "strong" reference to that object exists. Before the
4282 * object's #GObjectClass.dispose method is called, every #GWeakRef
4283 * associated with becomes empty (i.e. points to %NULL).
4285 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4286 * heap-allocated, or embedded in larger structures.
4288 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4289 * reference is thread-safe: converting a weak pointer to a reference is
4290 * atomic with respect to invalidation of weak pointers to destroyed
4291 * objects.
4293 * If the object's #GObjectClass.dispose method results in additional
4294 * references to the object being held, any #GWeakRefs taken
4295 * before it was disposed will continue to point to %NULL. If
4296 * #GWeakRefs are taken after the object is disposed and
4297 * re-referenced, they will continue to point to it until its refcount
4298 * goes back to zero, at which point they too will be invalidated.
4302 * g_weak_ref_init: (skip)
4303 * @weak_ref: (inout): uninitialized or empty location for a weak
4304 * reference
4305 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4307 * Initialise a non-statically-allocated #GWeakRef.
4309 * This function also calls g_weak_ref_set() with @object on the
4310 * freshly-initialised weak reference.
4312 * This function should always be matched with a call to
4313 * g_weak_ref_clear(). It is not necessary to use this function for a
4314 * #GWeakRef in static storage because it will already be
4315 * properly initialised. Just use g_weak_ref_set() directly.
4317 * Since: 2.32
4319 void
4320 g_weak_ref_init (GWeakRef *weak_ref,
4321 gpointer object)
4323 weak_ref->priv.p = NULL;
4325 g_weak_ref_set (weak_ref, object);
4329 * g_weak_ref_clear: (skip)
4330 * @weak_ref: (inout): location of a weak reference, which
4331 * may be empty
4333 * Frees resources associated with a non-statically-allocated #GWeakRef.
4334 * After this call, the #GWeakRef is left in an undefined state.
4336 * You should only call this on a #GWeakRef that previously had
4337 * g_weak_ref_init() called on it.
4339 * Since: 2.32
4341 void
4342 g_weak_ref_clear (GWeakRef *weak_ref)
4344 g_weak_ref_set (weak_ref, NULL);
4346 /* be unkind */
4347 weak_ref->priv.p = (void *) 0xccccccccu;
4351 * g_weak_ref_get: (skip)
4352 * @weak_ref: (inout): location of a weak reference to a #GObject
4354 * If @weak_ref is not empty, atomically acquire a strong
4355 * reference to the object it points to, and return that reference.
4357 * This function is needed because of the potential race between taking
4358 * the pointer value and g_object_ref() on it, if the object was losing
4359 * its last reference at the same time in a different thread.
4361 * The caller should release the resulting reference in the usual way,
4362 * by using g_object_unref().
4364 * Returns: (transfer full) (type GObject.Object): the object pointed to
4365 * by @weak_ref, or %NULL if it was empty
4367 * Since: 2.32
4369 gpointer
4370 g_weak_ref_get (GWeakRef *weak_ref)
4372 gpointer object_or_null;
4374 g_return_val_if_fail (weak_ref!= NULL, NULL);
4376 g_rw_lock_reader_lock (&weak_locations_lock);
4378 object_or_null = weak_ref->priv.p;
4380 if (object_or_null != NULL)
4381 g_object_ref (object_or_null);
4383 g_rw_lock_reader_unlock (&weak_locations_lock);
4385 return object_or_null;
4389 * g_weak_ref_set: (skip)
4390 * @weak_ref: location for a weak reference
4391 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4393 * Change the object to which @weak_ref points, or set it to
4394 * %NULL.
4396 * You must own a strong reference on @object while calling this
4397 * function.
4399 * Since: 2.32
4401 void
4402 g_weak_ref_set (GWeakRef *weak_ref,
4403 gpointer object)
4405 GSList **weak_locations;
4406 GObject *new_object;
4407 GObject *old_object;
4409 g_return_if_fail (weak_ref != NULL);
4410 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4412 new_object = object;
4414 g_rw_lock_writer_lock (&weak_locations_lock);
4416 /* We use the extra level of indirection here so that if we have ever
4417 * had a weak pointer installed at any point in time on this object,
4418 * we can see that there is a non-NULL value associated with the
4419 * weak-pointer quark and know that this value will not change at any
4420 * point in the object's lifetime.
4422 * Both properties are important for reducing the amount of times we
4423 * need to acquire locks and for decreasing the duration of time the
4424 * lock is held while avoiding some rather tricky races.
4426 * Specifically: we can avoid having to do an extra unconditional lock
4427 * in g_object_unref() without worrying about some extremely tricky
4428 * races.
4431 old_object = weak_ref->priv.p;
4432 if (new_object != old_object)
4434 weak_ref->priv.p = new_object;
4436 /* Remove the weak ref from the old object */
4437 if (old_object != NULL)
4439 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4440 /* for it to point to an object, the object must have had it added once */
4441 g_assert (weak_locations != NULL);
4443 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4446 /* Add the weak ref to the new object */
4447 if (new_object != NULL)
4449 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4451 if (weak_locations == NULL)
4453 weak_locations = g_new0 (GSList *, 1);
4454 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4457 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4461 g_rw_lock_writer_unlock (&weak_locations_lock);