gio: Annotate GDBusObjectManagerClient signal appropriately
[glib.git] / gobject / gobject.c
blobc3328197cebcf9b6fc185e0569aaef8cf36d8043
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 * Since: 2.4
754 void
755 g_object_interface_install_property (gpointer g_iface,
756 GParamSpec *pspec)
758 GTypeInterface *iface_class = g_iface;
760 g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
761 g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
763 if (!validate_pspec_to_install (pspec))
764 return;
766 (void) install_property_internal (iface_class->g_type, 0, pspec);
770 * g_object_class_find_property:
771 * @oclass: a #GObjectClass
772 * @property_name: the name of the property to look up
774 * Looks up the #GParamSpec for a property of a class.
776 * Returns: (transfer none): the #GParamSpec for the property, or
777 * %NULL if the class doesn't have a property of that name
779 GParamSpec*
780 g_object_class_find_property (GObjectClass *class,
781 const gchar *property_name)
783 GParamSpec *pspec;
784 GParamSpec *redirect;
786 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
787 g_return_val_if_fail (property_name != NULL, NULL);
789 pspec = g_param_spec_pool_lookup (pspec_pool,
790 property_name,
791 G_OBJECT_CLASS_TYPE (class),
792 TRUE);
793 if (pspec)
795 redirect = g_param_spec_get_redirect_target (pspec);
796 if (redirect)
797 return redirect;
798 else
799 return pspec;
801 else
802 return NULL;
806 * g_object_interface_find_property:
807 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
808 * interface, or the default vtable for the interface
809 * @property_name: name of a property to lookup.
811 * Find the #GParamSpec with the given name for an
812 * interface. Generally, the interface vtable passed in as @g_iface
813 * will be the default vtable from g_type_default_interface_ref(), or,
814 * if you know the interface has already been loaded,
815 * g_type_default_interface_peek().
817 * Since: 2.4
819 * Returns: (transfer none): the #GParamSpec for the property of the
820 * interface with the name @property_name, or %NULL if no
821 * such property exists.
823 GParamSpec*
824 g_object_interface_find_property (gpointer g_iface,
825 const gchar *property_name)
827 GTypeInterface *iface_class = g_iface;
829 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
830 g_return_val_if_fail (property_name != NULL, NULL);
832 return g_param_spec_pool_lookup (pspec_pool,
833 property_name,
834 iface_class->g_type,
835 FALSE);
839 * g_object_class_override_property:
840 * @oclass: a #GObjectClass
841 * @property_id: the new property ID
842 * @name: the name of a property registered in a parent class or
843 * in an interface of this class.
845 * Registers @property_id as referring to a property with the name
846 * @name in a parent class or in an interface implemented by @oclass.
847 * This allows this class to "override" a property implementation in
848 * a parent class or to provide the implementation of a property from
849 * an interface.
851 * Internally, overriding is implemented by creating a property of type
852 * #GParamSpecOverride; generally operations that query the properties of
853 * the object class, such as g_object_class_find_property() or
854 * g_object_class_list_properties() will return the overridden
855 * property. However, in one case, the @construct_properties argument of
856 * the @constructor virtual function, the #GParamSpecOverride is passed
857 * instead, so that the @param_id field of the #GParamSpec will be
858 * correct. For virtually all uses, this makes no difference. If you
859 * need to get the overridden property, you can call
860 * g_param_spec_get_redirect_target().
862 * Since: 2.4
864 void
865 g_object_class_override_property (GObjectClass *oclass,
866 guint property_id,
867 const gchar *name)
869 GParamSpec *overridden = NULL;
870 GParamSpec *new;
871 GType parent_type;
873 g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
874 g_return_if_fail (property_id > 0);
875 g_return_if_fail (name != NULL);
877 /* Find the overridden property; first check parent types
879 parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
880 if (parent_type != G_TYPE_NONE)
881 overridden = g_param_spec_pool_lookup (pspec_pool,
882 name,
883 parent_type,
884 TRUE);
885 if (!overridden)
887 GType *ifaces;
888 guint n_ifaces;
890 /* Now check interfaces
892 ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
893 while (n_ifaces-- && !overridden)
895 overridden = g_param_spec_pool_lookup (pspec_pool,
896 name,
897 ifaces[n_ifaces],
898 FALSE);
901 g_free (ifaces);
904 if (!overridden)
906 g_warning ("%s: Can't find property to override for '%s::%s'",
907 G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
908 return;
911 new = g_param_spec_override (name, overridden);
912 g_object_class_install_property (oclass, property_id, new);
916 * g_object_class_list_properties:
917 * @oclass: a #GObjectClass
918 * @n_properties: (out): return location for the length of the returned array
920 * Get an array of #GParamSpec* for all properties of a class.
922 * Returns: (array length=n_properties) (transfer container): an array of
923 * #GParamSpec* which should be freed after use
925 GParamSpec** /* free result */
926 g_object_class_list_properties (GObjectClass *class,
927 guint *n_properties_p)
929 GParamSpec **pspecs;
930 guint n;
932 g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
934 pspecs = g_param_spec_pool_list (pspec_pool,
935 G_OBJECT_CLASS_TYPE (class),
936 &n);
937 if (n_properties_p)
938 *n_properties_p = n;
940 return pspecs;
944 * g_object_interface_list_properties:
945 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
946 * interface, or the default vtable for the interface
947 * @n_properties_p: (out): location to store number of properties returned.
949 * Lists the properties of an interface.Generally, the interface
950 * vtable passed in as @g_iface will be the default vtable from
951 * g_type_default_interface_ref(), or, if you know the interface has
952 * already been loaded, g_type_default_interface_peek().
954 * Since: 2.4
956 * Returns: (array length=n_properties_p) (transfer container): a
957 * pointer to an array of pointers to #GParamSpec
958 * structures. The paramspecs are owned by GLib, but the
959 * array should be freed with g_free() when you are done with
960 * it.
962 GParamSpec**
963 g_object_interface_list_properties (gpointer g_iface,
964 guint *n_properties_p)
966 GTypeInterface *iface_class = g_iface;
967 GParamSpec **pspecs;
968 guint n;
970 g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
972 pspecs = g_param_spec_pool_list (pspec_pool,
973 iface_class->g_type,
974 &n);
975 if (n_properties_p)
976 *n_properties_p = n;
978 return pspecs;
981 static inline gboolean
982 object_in_construction (GObject *object)
984 return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
987 static void
988 g_object_init (GObject *object,
989 GObjectClass *class)
991 object->ref_count = 1;
992 object->qdata = NULL;
994 if (CLASS_HAS_PROPS (class))
996 /* freeze object's notification queue, g_object_newv() preserves pairedness */
997 g_object_notify_queue_freeze (object, FALSE);
1000 if (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1002 /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
1003 g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
1006 GOBJECT_IF_DEBUG (OBJECTS,
1008 G_LOCK (debug_objects);
1009 debug_objects_count++;
1010 g_hash_table_insert (debug_objects_ht, object, object);
1011 G_UNLOCK (debug_objects);
1015 static void
1016 g_object_do_set_property (GObject *object,
1017 guint property_id,
1018 const GValue *value,
1019 GParamSpec *pspec)
1021 switch (property_id)
1023 default:
1024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1025 break;
1029 static void
1030 g_object_do_get_property (GObject *object,
1031 guint property_id,
1032 GValue *value,
1033 GParamSpec *pspec)
1035 switch (property_id)
1037 default:
1038 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1039 break;
1043 static void
1044 g_object_real_dispose (GObject *object)
1046 g_signal_handlers_destroy (object);
1047 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1048 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1051 static void
1052 g_object_finalize (GObject *object)
1054 if (object_in_construction (object))
1056 g_critical ("object %s %p finalized while still in-construction",
1057 G_OBJECT_TYPE_NAME (object), object);
1060 g_datalist_clear (&object->qdata);
1062 GOBJECT_IF_DEBUG (OBJECTS,
1064 G_LOCK (debug_objects);
1065 g_assert (g_hash_table_lookup (debug_objects_ht, object) == object);
1066 g_hash_table_remove (debug_objects_ht, object);
1067 debug_objects_count--;
1068 G_UNLOCK (debug_objects);
1072 static void
1073 g_object_dispatch_properties_changed (GObject *object,
1074 guint n_pspecs,
1075 GParamSpec **pspecs)
1077 guint i;
1079 for (i = 0; i < n_pspecs; i++)
1080 g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1084 * g_object_run_dispose:
1085 * @object: a #GObject
1087 * Releases all references to other objects. This can be used to break
1088 * reference cycles.
1090 * This function should only be called from object system implementations.
1092 void
1093 g_object_run_dispose (GObject *object)
1095 g_return_if_fail (G_IS_OBJECT (object));
1096 g_return_if_fail (object->ref_count > 0);
1098 g_object_ref (object);
1099 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1100 G_OBJECT_GET_CLASS (object)->dispose (object);
1101 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1102 g_object_unref (object);
1106 * g_object_freeze_notify:
1107 * @object: a #GObject
1109 * Increases the freeze count on @object. If the freeze count is
1110 * non-zero, the emission of "notify" signals on @object is
1111 * stopped. The signals are queued until the freeze count is decreased
1112 * to zero. Duplicate notifications are squashed so that at most one
1113 * #GObject::notify signal is emitted for each property modified while the
1114 * object is frozen.
1116 * This is necessary for accessors that modify multiple properties to prevent
1117 * premature notification while the object is still being modified.
1119 void
1120 g_object_freeze_notify (GObject *object)
1122 g_return_if_fail (G_IS_OBJECT (object));
1124 if (g_atomic_int_get (&object->ref_count) == 0)
1125 return;
1127 g_object_ref (object);
1128 g_object_notify_queue_freeze (object, FALSE);
1129 g_object_unref (object);
1132 static GParamSpec *
1133 get_notify_pspec (GParamSpec *pspec)
1135 GParamSpec *redirected;
1137 /* we don't notify on non-READABLE parameters */
1138 if (~pspec->flags & G_PARAM_READABLE)
1139 return NULL;
1141 /* if the paramspec is redirected, notify on the target */
1142 redirected = g_param_spec_get_redirect_target (pspec);
1143 if (redirected != NULL)
1144 return redirected;
1146 /* else, notify normally */
1147 return pspec;
1150 static inline void
1151 g_object_notify_by_spec_internal (GObject *object,
1152 GParamSpec *pspec)
1154 GParamSpec *notify_pspec;
1156 notify_pspec = get_notify_pspec (pspec);
1158 if (notify_pspec != NULL)
1160 GObjectNotifyQueue *nqueue;
1162 /* conditional freeze: only increase freeze count if already frozen */
1163 nqueue = g_object_notify_queue_freeze (object, TRUE);
1165 if (nqueue != NULL)
1167 /* we're frozen, so add to the queue and release our freeze */
1168 g_object_notify_queue_add (object, nqueue, notify_pspec);
1169 g_object_notify_queue_thaw (object, nqueue);
1171 else
1172 /* not frozen, so just dispatch the notification directly */
1173 G_OBJECT_GET_CLASS (object)
1174 ->dispatch_properties_changed (object, 1, &notify_pspec);
1179 * g_object_notify:
1180 * @object: a #GObject
1181 * @property_name: the name of a property installed on the class of @object.
1183 * Emits a "notify" signal for the property @property_name on @object.
1185 * When possible, eg. when signaling a property change from within the class
1186 * that registered the property, you should use g_object_notify_by_pspec()
1187 * instead.
1189 * Note that emission of the notify signal may be blocked with
1190 * g_object_freeze_notify(). In this case, the signal emissions are queued
1191 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1192 * called.
1194 void
1195 g_object_notify (GObject *object,
1196 const gchar *property_name)
1198 GParamSpec *pspec;
1200 g_return_if_fail (G_IS_OBJECT (object));
1201 g_return_if_fail (property_name != NULL);
1202 if (g_atomic_int_get (&object->ref_count) == 0)
1203 return;
1205 g_object_ref (object);
1206 /* We don't need to get the redirect target
1207 * (by, e.g. calling g_object_class_find_property())
1208 * because g_object_notify_queue_add() does that
1210 pspec = g_param_spec_pool_lookup (pspec_pool,
1211 property_name,
1212 G_OBJECT_TYPE (object),
1213 TRUE);
1215 if (!pspec)
1216 g_warning ("%s: object class '%s' has no property named '%s'",
1217 G_STRFUNC,
1218 G_OBJECT_TYPE_NAME (object),
1219 property_name);
1220 else
1221 g_object_notify_by_spec_internal (object, pspec);
1222 g_object_unref (object);
1226 * g_object_notify_by_pspec:
1227 * @object: a #GObject
1228 * @pspec: the #GParamSpec of a property installed on the class of @object.
1230 * Emits a "notify" signal for the property specified by @pspec on @object.
1232 * This function omits the property name lookup, hence it is faster than
1233 * g_object_notify().
1235 * One way to avoid using g_object_notify() from within the
1236 * class that registered the properties, and using g_object_notify_by_pspec()
1237 * instead, is to store the GParamSpec used with
1238 * g_object_class_install_property() inside a static array, e.g.:
1240 *|[<!-- language="C" -->
1241 * enum
1243 * PROP_0,
1244 * PROP_FOO,
1245 * PROP_LAST
1246 * };
1248 * static GParamSpec *properties[PROP_LAST];
1250 * static void
1251 * my_object_class_init (MyObjectClass *klass)
1253 * properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1254 * 0, 100,
1255 * 50,
1256 * G_PARAM_READWRITE);
1257 * g_object_class_install_property (gobject_class,
1258 * PROP_FOO,
1259 * properties[PROP_FOO]);
1261 * ]|
1263 * and then notify a change on the "foo" property with:
1265 * |[<!-- language="C" -->
1266 * g_object_notify_by_pspec (self, properties[PROP_FOO]);
1267 * ]|
1269 * Since: 2.26
1271 void
1272 g_object_notify_by_pspec (GObject *object,
1273 GParamSpec *pspec)
1276 g_return_if_fail (G_IS_OBJECT (object));
1277 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1279 if (g_atomic_int_get (&object->ref_count) == 0)
1280 return;
1282 g_object_ref (object);
1283 g_object_notify_by_spec_internal (object, pspec);
1284 g_object_unref (object);
1288 * g_object_thaw_notify:
1289 * @object: a #GObject
1291 * Reverts the effect of a previous call to
1292 * g_object_freeze_notify(). The freeze count is decreased on @object
1293 * and when it reaches zero, queued "notify" signals are emitted.
1295 * Duplicate notifications for each property are squashed so that at most one
1296 * #GObject::notify signal is emitted for each property, in the reverse order
1297 * in which they have been queued.
1299 * It is an error to call this function when the freeze count is zero.
1301 void
1302 g_object_thaw_notify (GObject *object)
1304 GObjectNotifyQueue *nqueue;
1306 g_return_if_fail (G_IS_OBJECT (object));
1307 if (g_atomic_int_get (&object->ref_count) == 0)
1308 return;
1310 g_object_ref (object);
1312 /* FIXME: Freezing is the only way to get at the notify queue.
1313 * So we freeze once and then thaw twice.
1315 nqueue = g_object_notify_queue_freeze (object, FALSE);
1316 g_object_notify_queue_thaw (object, nqueue);
1317 g_object_notify_queue_thaw (object, nqueue);
1319 g_object_unref (object);
1322 static void
1323 consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
1325 static GHashTable *already_warned_table;
1326 static const gchar *enable_diagnostic;
1327 static GMutex already_warned_lock;
1328 gboolean already;
1330 if (!(pspec->flags & G_PARAM_DEPRECATED))
1331 return;
1333 if (g_once_init_enter (&enable_diagnostic))
1335 const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1337 if (!value)
1338 value = "0";
1340 g_once_init_leave (&enable_diagnostic, value);
1343 if (enable_diagnostic[0] == '0')
1344 return;
1346 /* We hash only on property names: this means that we could end up in
1347 * a situation where we fail to emit a warning about a pair of
1348 * same-named deprecated properties used on two separate types.
1349 * That's pretty unlikely to occur, and even if it does, you'll still
1350 * have seen the warning for the first one...
1352 * Doing it this way lets us hash directly on the (interned) property
1353 * name pointers.
1355 g_mutex_lock (&already_warned_lock);
1357 if (already_warned_table == NULL)
1358 already_warned_table = g_hash_table_new (NULL, NULL);
1360 already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
1361 if (!already)
1362 g_hash_table_add (already_warned_table, (gpointer) pspec->name);
1364 g_mutex_unlock (&already_warned_lock);
1366 if (!already)
1367 g_warning ("The property %s:%s is deprecated and shouldn't be used "
1368 "anymore. It will be removed in a future version.",
1369 g_type_name (pspec->owner_type), pspec->name);
1372 static inline void
1373 object_get_property (GObject *object,
1374 GParamSpec *pspec,
1375 GValue *value)
1377 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1378 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1379 GParamSpec *redirect;
1381 if (class == NULL)
1383 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1384 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1385 return;
1388 redirect = g_param_spec_get_redirect_target (pspec);
1389 if (redirect)
1390 pspec = redirect;
1392 consider_issuing_property_deprecation_warning (pspec);
1394 class->get_property (object, param_id, value, pspec);
1397 static inline void
1398 object_set_property (GObject *object,
1399 GParamSpec *pspec,
1400 const GValue *value,
1401 GObjectNotifyQueue *nqueue)
1403 GValue tmp_value = G_VALUE_INIT;
1404 GObjectClass *class = g_type_class_peek (pspec->owner_type);
1405 guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1406 GParamSpec *redirect;
1408 if (class == NULL)
1410 g_warning ("'%s::%s' is not a valid property name; '%s' is not a GObject subtype",
1411 g_type_name (pspec->owner_type), pspec->name, g_type_name (pspec->owner_type));
1412 return;
1415 redirect = g_param_spec_get_redirect_target (pspec);
1416 if (redirect)
1417 pspec = redirect;
1419 /* provide a copy to work from, convert (if necessary) and validate */
1420 g_value_init (&tmp_value, pspec->value_type);
1421 if (!g_value_transform (value, &tmp_value))
1422 g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1423 pspec->name,
1424 g_type_name (pspec->value_type),
1425 G_VALUE_TYPE_NAME (value));
1426 else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1428 gchar *contents = g_strdup_value_contents (value);
1430 g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1431 contents,
1432 G_VALUE_TYPE_NAME (value),
1433 pspec->name,
1434 g_type_name (pspec->value_type));
1435 g_free (contents);
1437 else
1439 class->set_property (object, param_id, &tmp_value, pspec);
1441 if (~pspec->flags & G_PARAM_EXPLICIT_NOTIFY)
1443 GParamSpec *notify_pspec;
1445 notify_pspec = get_notify_pspec (pspec);
1447 if (notify_pspec != NULL)
1448 g_object_notify_queue_add (object, nqueue, notify_pspec);
1451 g_value_unset (&tmp_value);
1454 static void
1455 object_interface_check_properties (gpointer check_data,
1456 gpointer g_iface)
1458 GTypeInterface *iface_class = g_iface;
1459 GObjectClass *class;
1460 GType iface_type = iface_class->g_type;
1461 GParamSpec **pspecs;
1462 guint n;
1464 class = g_type_class_ref (iface_class->g_instance_type);
1466 if (class == NULL)
1467 return;
1469 if (!G_IS_OBJECT_CLASS (class))
1470 goto out;
1472 pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1474 while (n--)
1476 GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1477 pspecs[n]->name,
1478 G_OBJECT_CLASS_TYPE (class),
1479 TRUE);
1481 if (!class_pspec)
1483 g_critical ("Object class %s doesn't implement property "
1484 "'%s' from interface '%s'",
1485 g_type_name (G_OBJECT_CLASS_TYPE (class)),
1486 pspecs[n]->name,
1487 g_type_name (iface_type));
1489 continue;
1492 /* We do a number of checks on the properties of an interface to
1493 * make sure that all classes implementing the interface are
1494 * overriding the properties in a sane way.
1496 * We do the checks in order of importance so that we can give
1497 * more useful error messages first.
1499 * First, we check that the implementation doesn't remove the
1500 * basic functionality (readability, writability) advertised by
1501 * the interface. Next, we check that it doesn't introduce
1502 * additional restrictions (such as construct-only). Finally, we
1503 * make sure the types are compatible.
1506 #define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1507 /* If the property on the interface is readable then the
1508 * implementation must be readable. If the interface is writable
1509 * then the implementation must be writable.
1511 if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1513 g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1514 "property on interface '%s'\n", pspecs[n]->name,
1515 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1516 continue;
1519 /* If the property on the interface is writable then we need to
1520 * make sure the implementation doesn't introduce new restrictions
1521 * on that writability (ie: construct-only).
1523 * If the interface was not writable to begin with then we don't
1524 * really have any problems here because "writable at construct
1525 * time only" is still more permissive than "read only".
1527 if (pspecs[n]->flags & G_PARAM_WRITABLE)
1529 if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1531 g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1532 "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1533 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1534 continue;
1537 #undef SUBSET
1539 /* If the property on the interface is readable then we are
1540 * effectively advertising that reading the property will return a
1541 * value of a specific type. All implementations of the interface
1542 * need to return items of this type -- but may be more
1543 * restrictive. For example, it is legal to have:
1545 * GtkWidget *get_item();
1547 * that is implemented by a function that always returns a
1548 * GtkEntry. In short: readability implies that the
1549 * implementation value type must be equal or more restrictive.
1551 * Similarly, if the property on the interface is writable then
1552 * must be able to accept the property being set to any value of
1553 * that type, including subclasses. In this case, we may also be
1554 * less restrictive. For example, it is legal to have:
1556 * set_item (GtkEntry *);
1558 * that is implemented by a function that will actually work with
1559 * any GtkWidget. In short: writability implies that the
1560 * implementation value type must be equal or less restrictive.
1562 * In the case that the property is both readable and writable
1563 * then the only way that both of the above can be satisfied is
1564 * with a type that is exactly equal.
1566 switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1568 case G_PARAM_READABLE | G_PARAM_WRITABLE:
1569 /* class pspec value type must have exact equality with interface */
1570 if (pspecs[n]->value_type != class_pspec->value_type)
1571 g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1572 "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1573 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1574 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1575 break;
1577 case G_PARAM_READABLE:
1578 /* class pspec value type equal or more restrictive than interface */
1579 if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1580 g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1581 "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1582 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1583 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1584 break;
1586 case G_PARAM_WRITABLE:
1587 /* class pspec value type equal or less restrictive than interface */
1588 if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1589 g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1590 "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1591 g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1592 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1593 break;
1595 default:
1596 g_assert_not_reached ();
1600 g_free (pspecs);
1602 out:
1603 g_type_class_unref (class);
1606 GType
1607 g_object_get_type (void)
1609 return G_TYPE_OBJECT;
1613 * g_object_new: (skip)
1614 * @object_type: the type id of the #GObject subtype to instantiate
1615 * @first_property_name: the name of the first property
1616 * @...: the value of the first property, followed optionally by more
1617 * name/value pairs, followed by %NULL
1619 * Creates a new instance of a #GObject subtype and sets its properties.
1621 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1622 * which are not explicitly specified are set to their default values.
1624 * Returns: (transfer full) (type GObject.Object): a new instance of
1625 * @object_type
1627 gpointer
1628 g_object_new (GType object_type,
1629 const gchar *first_property_name,
1630 ...)
1632 GObject *object;
1633 va_list var_args;
1635 /* short circuit for calls supplying no properties */
1636 if (!first_property_name)
1637 return g_object_new_with_properties (object_type, 0, NULL, NULL);
1639 va_start (var_args, first_property_name);
1640 object = g_object_new_valist (object_type, first_property_name, var_args);
1641 va_end (var_args);
1643 return object;
1646 static gpointer
1647 g_object_new_with_custom_constructor (GObjectClass *class,
1648 GObjectConstructParam *params,
1649 guint n_params)
1651 GObjectNotifyQueue *nqueue = NULL;
1652 gboolean newly_constructed;
1653 GObjectConstructParam *cparams;
1654 GObject *object;
1655 GValue *cvalues;
1656 gint n_cparams;
1657 gint cvals_used;
1658 GSList *node;
1659 gint i;
1661 /* If we have ->constructed() then we have to do a lot more work.
1662 * It's possible that this is a singleton and it's also possible
1663 * that the user's constructor() will attempt to modify the values
1664 * that we pass in, so we'll need to allocate copies of them.
1665 * It's also possible that the user may attempt to call
1666 * g_object_set() from inside of their constructor, so we need to
1667 * add ourselves to a list of objects for which that is allowed
1668 * while their constructor() is running.
1671 /* Create the array of GObjectConstructParams for constructor() */
1672 n_cparams = g_slist_length (class->construct_properties);
1673 cparams = g_new (GObjectConstructParam, n_cparams);
1674 cvalues = g_new0 (GValue, n_cparams);
1675 cvals_used = 0;
1676 i = 0;
1678 /* As above, we may find the value in the passed-in params list.
1680 * If we have the value passed in then we can use the GValue from
1681 * it directly because it is safe to modify. If we use the
1682 * default value from the class, we had better not pass that in
1683 * and risk it being modified, so we create a new one.
1684 * */
1685 for (node = class->construct_properties; node; node = node->next)
1687 GParamSpec *pspec;
1688 GValue *value;
1689 gint j;
1691 pspec = node->data;
1692 value = NULL; /* to silence gcc... */
1694 for (j = 0; j < n_params; j++)
1695 if (params[j].pspec == pspec)
1697 consider_issuing_property_deprecation_warning (pspec);
1698 value = params[j].value;
1699 break;
1702 if (j == n_params)
1704 value = &cvalues[cvals_used++];
1705 g_value_init (value, pspec->value_type);
1706 g_param_value_set_default (pspec, value);
1709 cparams[i].pspec = pspec;
1710 cparams[i].value = value;
1711 i++;
1714 /* construct object from construction parameters */
1715 object = class->constructor (class->g_type_class.g_type, n_cparams, cparams);
1716 /* free construction values */
1717 g_free (cparams);
1718 while (cvals_used--)
1719 g_value_unset (&cvalues[cvals_used]);
1720 g_free (cvalues);
1722 /* There is code in the wild that relies on being able to return NULL
1723 * from its custom constructor. This was never a supported operation,
1724 * but since the code is already out there...
1726 if (object == NULL)
1728 g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
1729 "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
1730 return NULL;
1733 /* g_object_init() will have marked the object as being in-construction.
1734 * Check if the returned object still is so marked, or if this is an
1735 * already-existing singleton (in which case we should not do 'constructed').
1737 newly_constructed = object_in_construction (object);
1738 if (newly_constructed)
1739 g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1741 if (CLASS_HAS_PROPS (class))
1743 /* If this object was newly_constructed then g_object_init()
1744 * froze the queue. We need to freeze it here in order to get
1745 * the handle so that we can thaw it below (otherwise it will
1746 * be frozen forever).
1748 * We also want to do a freeze if we have any params to set,
1749 * even on a non-newly_constructed object.
1751 * It's possible that we have the case of non-newly created
1752 * singleton and all of the passed-in params were construct
1753 * properties so n_params > 0 but we will actually set no
1754 * properties. This is a pretty lame case to optimise, so
1755 * just ignore it and freeze anyway.
1757 if (newly_constructed || n_params)
1758 nqueue = g_object_notify_queue_freeze (object, FALSE);
1760 /* Remember: if it was newly_constructed then g_object_init()
1761 * already did a freeze, so we now have two. Release one.
1763 if (newly_constructed)
1764 g_object_notify_queue_thaw (object, nqueue);
1767 /* run 'constructed' handler if there is a custom one */
1768 if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1769 class->constructed (object);
1771 /* set remaining properties */
1772 for (i = 0; i < n_params; i++)
1773 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1775 consider_issuing_property_deprecation_warning (params[i].pspec);
1776 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1779 /* If nqueue is non-NULL then we are frozen. Thaw it. */
1780 if (nqueue)
1781 g_object_notify_queue_thaw (object, nqueue);
1783 return object;
1786 static gpointer
1787 g_object_new_internal (GObjectClass *class,
1788 GObjectConstructParam *params,
1789 guint n_params)
1791 GObjectNotifyQueue *nqueue = NULL;
1792 GObject *object;
1794 if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
1795 return g_object_new_with_custom_constructor (class, params, n_params);
1797 object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
1799 if (CLASS_HAS_PROPS (class))
1801 GSList *node;
1803 /* This will have been setup in g_object_init() */
1804 nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
1805 g_assert (nqueue != NULL);
1807 /* We will set exactly n_construct_properties construct
1808 * properties, but they may come from either the class default
1809 * values or the passed-in parameter list.
1811 for (node = class->construct_properties; node; node = node->next)
1813 const GValue *value;
1814 GParamSpec *pspec;
1815 gint j;
1817 pspec = node->data;
1818 value = NULL; /* to silence gcc... */
1820 for (j = 0; j < n_params; j++)
1821 if (params[j].pspec == pspec)
1823 consider_issuing_property_deprecation_warning (pspec);
1824 value = params[j].value;
1825 break;
1828 if (j == n_params)
1829 value = g_param_spec_get_default_value (pspec);
1831 object_set_property (object, pspec, value, nqueue);
1835 /* run 'constructed' handler if there is a custom one */
1836 if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
1837 class->constructed (object);
1839 if (nqueue)
1841 gint i;
1843 /* Set remaining properties. The construct properties will
1844 * already have been taken, so set only the non-construct
1845 * ones.
1847 for (i = 0; i < n_params; i++)
1848 if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1850 consider_issuing_property_deprecation_warning (params[i].pspec);
1851 object_set_property (object, params[i].pspec, params[i].value, nqueue);
1854 g_object_notify_queue_thaw (object, nqueue);
1857 return object;
1861 static inline gboolean
1862 g_object_new_is_valid_property (GType object_type,
1863 GParamSpec *pspec,
1864 const char *name,
1865 GObjectConstructParam *params,
1866 int n_params)
1868 gint i;
1869 if (G_UNLIKELY (pspec == NULL))
1871 g_critical ("%s: object class '%s' has no property named '%s'",
1872 G_STRFUNC, g_type_name (object_type), name);
1873 return FALSE;
1876 if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
1878 g_critical ("%s: property '%s' of object class '%s' is not writable",
1879 G_STRFUNC, pspec->name, g_type_name (object_type));
1880 return FALSE;
1883 if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
1885 for (i = 0; i < n_params; i++)
1886 if (params[i].pspec == pspec)
1887 break;
1888 if (G_UNLIKELY (i != n_params))
1890 g_critical ("%s: property '%s' for type '%s' cannot be set twice",
1891 G_STRFUNC, name, g_type_name (object_type));
1892 return FALSE;
1895 return TRUE;
1900 * g_object_new_with_properties: (rename-to g_object_new)
1901 * @object_type: the object type to instantiate
1902 * @n_properties: the number of properties
1903 * @names: (array length=n_properties): the names of each property to be set
1904 * @values: (array length=n_properties): the values of each property to be set
1906 * Creates a new instance of a #GObject subtype and sets its properties using
1907 * the provided arrays. Both arrays must have exactly @n_properties elements,
1908 * and the names and values correspond by index.
1910 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
1911 * which are not explicitly specified are set to their default values.
1913 * Returns: (type GObject.Object) (transfer full): a new instance of
1914 * @object_type
1916 * Since: 2.54
1918 GObject *
1919 g_object_new_with_properties (GType object_type,
1920 guint n_properties,
1921 const char *names[],
1922 const GValue values[])
1924 GObjectClass *class, *unref_class = NULL;
1925 GObject *object;
1927 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1929 /* Try to avoid thrashing the ref_count if we don't need to (since
1930 * it's a locked operation).
1932 class = g_type_class_peek_static (object_type);
1934 if (class == NULL)
1935 class = unref_class = g_type_class_ref (object_type);
1937 if (n_properties > 0)
1939 guint i, count = 0;
1940 GObjectConstructParam *params;
1942 params = g_newa (GObjectConstructParam, n_properties);
1943 for (i = 0; i < n_properties; i++)
1945 GParamSpec *pspec;
1946 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], object_type, TRUE);
1947 if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
1948 continue;
1949 params[count].pspec = pspec;
1951 /* Init GValue */
1952 params[count].value = g_newa (GValue, 1);
1953 memset (params[count].value, 0, sizeof (GValue));
1954 g_value_init (params[count].value, G_VALUE_TYPE (&values[i]));
1956 g_value_copy (&values[i], params[count].value);
1957 count++;
1959 object = g_object_new_internal (class, params, count);
1961 while (count--)
1962 g_value_unset (params[count].value);
1964 else
1965 object = g_object_new_internal (class, NULL, 0);
1967 if (unref_class != NULL)
1968 g_type_class_unref (unref_class);
1970 return object;
1974 * g_object_newv:
1975 * @object_type: the type id of the #GObject subtype to instantiate
1976 * @n_parameters: the length of the @parameters array
1977 * @parameters: (array length=n_parameters): an array of #GParameter
1979 * Creates a new instance of a #GObject subtype and sets its properties.
1981 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
1982 * which are not explicitly specified are set to their default values.
1984 * Returns: (type GObject.Object) (transfer full): a new instance of
1985 * @object_type
1987 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
1988 * deprecated. See #GParameter for more information.
1990 gpointer
1991 g_object_newv (GType object_type,
1992 guint n_parameters,
1993 GParameter *parameters)
1995 GObjectClass *class, *unref_class = NULL;
1996 GObject *object;
1998 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
1999 g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
2001 /* Try to avoid thrashing the ref_count if we don't need to (since
2002 * it's a locked operation).
2004 class = g_type_class_peek_static (object_type);
2006 if (!class)
2007 class = unref_class = g_type_class_ref (object_type);
2009 if (n_parameters)
2011 GObjectConstructParam *cparams;
2012 guint i, j;
2014 cparams = g_newa (GObjectConstructParam, n_parameters);
2015 j = 0;
2017 for (i = 0; i < n_parameters; i++)
2019 GParamSpec *pspec;
2021 pspec = g_param_spec_pool_lookup (pspec_pool, parameters[i].name, object_type, TRUE);
2022 if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2023 continue;
2025 cparams[j].pspec = pspec;
2026 cparams[j].value = &parameters[i].value;
2027 j++;
2030 object = g_object_new_internal (class, cparams, j);
2032 else
2033 /* Fast case: no properties passed in. */
2034 object = g_object_new_internal (class, NULL, 0);
2036 if (unref_class)
2037 g_type_class_unref (unref_class);
2039 return object;
2043 * g_object_new_valist: (skip)
2044 * @object_type: the type id of the #GObject subtype to instantiate
2045 * @first_property_name: the name of the first property
2046 * @var_args: the value of the first property, followed optionally by more
2047 * name/value pairs, followed by %NULL
2049 * Creates a new instance of a #GObject subtype and sets its properties.
2051 * Construction parameters (see #G_PARAM_CONSTRUCT, #G_PARAM_CONSTRUCT_ONLY)
2052 * which are not explicitly specified are set to their default values.
2054 * Returns: a new instance of @object_type
2056 GObject*
2057 g_object_new_valist (GType object_type,
2058 const gchar *first_property_name,
2059 va_list var_args)
2061 GObjectClass *class, *unref_class = NULL;
2062 GObject *object;
2064 g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2066 /* Try to avoid thrashing the ref_count if we don't need to (since
2067 * it's a locked operation).
2069 class = g_type_class_peek_static (object_type);
2071 if (!class)
2072 class = unref_class = g_type_class_ref (object_type);
2074 if (first_property_name)
2076 GObjectConstructParam stack_params[16];
2077 GObjectConstructParam *params;
2078 const gchar *name;
2079 gint n_params = 0;
2081 name = first_property_name;
2082 params = stack_params;
2086 gchar *error = NULL;
2087 GParamSpec *pspec;
2089 pspec = g_param_spec_pool_lookup (pspec_pool, name, object_type, TRUE);
2091 if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2092 break;
2094 if (n_params == 16)
2096 params = g_new (GObjectConstructParam, n_params + 1);
2097 memcpy (params, stack_params, sizeof stack_params);
2099 else if (n_params > 16)
2100 params = g_renew (GObjectConstructParam, params, n_params + 1);
2102 params[n_params].pspec = pspec;
2103 params[n_params].value = g_newa (GValue, 1);
2104 memset (params[n_params].value, 0, sizeof (GValue));
2106 G_VALUE_COLLECT_INIT (params[n_params].value, pspec->value_type, var_args, 0, &error);
2108 if (error)
2110 g_critical ("%s: %s", G_STRFUNC, error);
2111 g_value_unset (params[n_params].value);
2112 g_free (error);
2113 break;
2116 n_params++;
2118 while ((name = va_arg (var_args, const gchar *)));
2120 object = g_object_new_internal (class, params, n_params);
2122 while (n_params--)
2123 g_value_unset (params[n_params].value);
2125 if (params != stack_params)
2126 g_free (params);
2128 else
2129 /* Fast case: no properties passed in. */
2130 object = g_object_new_internal (class, NULL, 0);
2132 if (unref_class)
2133 g_type_class_unref (unref_class);
2135 return object;
2138 static GObject*
2139 g_object_constructor (GType type,
2140 guint n_construct_properties,
2141 GObjectConstructParam *construct_params)
2143 GObject *object;
2145 /* create object */
2146 object = (GObject*) g_type_create_instance (type);
2148 /* set construction parameters */
2149 if (n_construct_properties)
2151 GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2153 /* set construct properties */
2154 while (n_construct_properties--)
2156 GValue *value = construct_params->value;
2157 GParamSpec *pspec = construct_params->pspec;
2159 construct_params++;
2160 object_set_property (object, pspec, value, nqueue);
2162 g_object_notify_queue_thaw (object, nqueue);
2163 /* the notification queue is still frozen from g_object_init(), so
2164 * we don't need to handle it here, g_object_newv() takes
2165 * care of that
2169 return object;
2172 static void
2173 g_object_constructed (GObject *object)
2175 /* empty default impl to allow unconditional upchaining */
2178 static inline gboolean
2179 g_object_set_is_valid_property (GObject *object,
2180 GParamSpec *pspec,
2181 const char *property_name)
2183 if (G_UNLIKELY (pspec == NULL))
2185 g_warning ("%s: object class '%s' has no property named '%s'",
2186 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2187 return FALSE;
2189 if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
2191 g_warning ("%s: property '%s' of object class '%s' is not writable",
2192 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2193 return FALSE;
2195 if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
2197 g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2198 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2199 return FALSE;
2201 return TRUE;
2205 * g_object_setv: (skip)
2206 * @object: a #GObject
2207 * @n_properties: the number of properties
2208 * @names: (array length=n_properties): the names of each property to be set
2209 * @values: (array length=n_properties): the values of each property to be set
2211 * Sets @n_properties properties for an @object.
2212 * Properties to be set will be taken from @values. All properties must be
2213 * valid. Warnings will be emitted and undefined behaviour may result if invalid
2214 * properties are passed in.
2216 * Since: 2.54
2218 void
2219 g_object_setv (GObject *object,
2220 guint n_properties,
2221 const gchar *names[],
2222 const GValue values[])
2224 guint i;
2225 GObjectNotifyQueue *nqueue;
2226 GParamSpec *pspec;
2227 GType obj_type;
2229 g_return_if_fail (G_IS_OBJECT (object));
2231 if (n_properties == 0)
2232 return;
2234 g_object_ref (object);
2235 obj_type = G_OBJECT_TYPE (object);
2236 nqueue = g_object_notify_queue_freeze (object, FALSE);
2237 for (i = 0; i < n_properties; i++)
2239 pspec = g_param_spec_pool_lookup (pspec_pool, names[i], obj_type, TRUE);
2241 if (!g_object_set_is_valid_property (object, pspec, names[i]))
2242 break;
2244 consider_issuing_property_deprecation_warning (pspec);
2245 object_set_property (object, pspec, &values[i], nqueue);
2248 g_object_notify_queue_thaw (object, nqueue);
2249 g_object_unref (object);
2253 * g_object_set_valist: (skip)
2254 * @object: a #GObject
2255 * @first_property_name: name of the first property to set
2256 * @var_args: value for the first property, followed optionally by more
2257 * name/value pairs, followed by %NULL
2259 * Sets properties on an object.
2261 void
2262 g_object_set_valist (GObject *object,
2263 const gchar *first_property_name,
2264 va_list var_args)
2266 GObjectNotifyQueue *nqueue;
2267 const gchar *name;
2269 g_return_if_fail (G_IS_OBJECT (object));
2271 g_object_ref (object);
2272 nqueue = g_object_notify_queue_freeze (object, FALSE);
2274 name = first_property_name;
2275 while (name)
2277 GValue value = G_VALUE_INIT;
2278 GParamSpec *pspec;
2279 gchar *error = NULL;
2281 pspec = g_param_spec_pool_lookup (pspec_pool,
2282 name,
2283 G_OBJECT_TYPE (object),
2284 TRUE);
2286 if (!g_object_set_is_valid_property (object, pspec, name))
2287 break;
2289 G_VALUE_COLLECT_INIT (&value, pspec->value_type, var_args,
2290 0, &error);
2291 if (error)
2293 g_warning ("%s: %s", G_STRFUNC, error);
2294 g_free (error);
2295 g_value_unset (&value);
2296 break;
2299 consider_issuing_property_deprecation_warning (pspec);
2300 object_set_property (object, pspec, &value, nqueue);
2301 g_value_unset (&value);
2303 name = va_arg (var_args, gchar*);
2306 g_object_notify_queue_thaw (object, nqueue);
2307 g_object_unref (object);
2310 static inline gboolean
2311 g_object_get_is_valid_property (GObject *object,
2312 GParamSpec *pspec,
2313 const char *property_name)
2315 if (G_UNLIKELY (pspec == NULL))
2317 g_warning ("%s: object class '%s' has no property named '%s'",
2318 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2319 return FALSE;
2321 if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
2323 g_warning ("%s: property '%s' of object class '%s' is not readable",
2324 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2325 return FALSE;
2327 return TRUE;
2331 * g_object_getv:
2332 * @object: a #GObject
2333 * @n_properties: the number of properties
2334 * @names: (array length=n_properties): the names of each property to get
2335 * @values: (array length=n_properties): the values of each property to get
2337 * Gets @n_properties properties for an @object.
2338 * Obtained properties will be set to @values. All properties must be valid.
2339 * Warnings will be emitted and undefined behaviour may result if invalid
2340 * properties are passed in.
2342 * Since: 2.54
2344 void
2345 g_object_getv (GObject *object,
2346 guint n_properties,
2347 const gchar *names[],
2348 GValue values[])
2350 guint i;
2351 GParamSpec *pspec;
2352 GType obj_type;
2354 g_return_if_fail (G_IS_OBJECT (object));
2356 if (n_properties == 0)
2357 return;
2359 g_object_ref (object);
2361 obj_type = G_OBJECT_TYPE (object);
2362 for (i = 0; i < n_properties; i++)
2364 pspec = g_param_spec_pool_lookup (pspec_pool,
2365 names[i],
2366 obj_type,
2367 TRUE);
2368 if (!g_object_get_is_valid_property (object, pspec, names[i]))
2369 break;
2371 memset (&values[i], 0, sizeof (GValue));
2372 g_value_init (&values[i], pspec->value_type);
2373 object_get_property (object, pspec, &values[i]);
2375 g_object_unref (object);
2379 * g_object_get_valist: (skip)
2380 * @object: a #GObject
2381 * @first_property_name: name of the first property to get
2382 * @var_args: return location for the first property, followed optionally by more
2383 * name/return location pairs, followed by %NULL
2385 * Gets properties of an object.
2387 * In general, a copy is made of the property contents and the caller
2388 * is responsible for freeing the memory in the appropriate manner for
2389 * the type, for instance by calling g_free() or g_object_unref().
2391 * See g_object_get().
2393 void
2394 g_object_get_valist (GObject *object,
2395 const gchar *first_property_name,
2396 va_list var_args)
2398 const gchar *name;
2400 g_return_if_fail (G_IS_OBJECT (object));
2402 g_object_ref (object);
2404 name = first_property_name;
2406 while (name)
2408 GValue value = G_VALUE_INIT;
2409 GParamSpec *pspec;
2410 gchar *error;
2412 pspec = g_param_spec_pool_lookup (pspec_pool,
2413 name,
2414 G_OBJECT_TYPE (object),
2415 TRUE);
2417 if (!g_object_get_is_valid_property (object, pspec, name))
2418 break;
2420 g_value_init (&value, pspec->value_type);
2422 object_get_property (object, pspec, &value);
2424 G_VALUE_LCOPY (&value, var_args, 0, &error);
2425 if (error)
2427 g_warning ("%s: %s", G_STRFUNC, error);
2428 g_free (error);
2429 g_value_unset (&value);
2430 break;
2433 g_value_unset (&value);
2435 name = va_arg (var_args, gchar*);
2438 g_object_unref (object);
2442 * g_object_set: (skip)
2443 * @object: (type GObject.Object): a #GObject
2444 * @first_property_name: name of the first property to set
2445 * @...: value for the first property, followed optionally by more
2446 * name/value pairs, followed by %NULL
2448 * Sets properties on an object.
2450 * Note that the "notify" signals are queued and only emitted (in
2451 * reverse order) after all properties have been set. See
2452 * g_object_freeze_notify().
2454 void
2455 g_object_set (gpointer _object,
2456 const gchar *first_property_name,
2457 ...)
2459 GObject *object = _object;
2460 va_list var_args;
2462 g_return_if_fail (G_IS_OBJECT (object));
2464 va_start (var_args, first_property_name);
2465 g_object_set_valist (object, first_property_name, var_args);
2466 va_end (var_args);
2470 * g_object_get: (skip)
2471 * @object: (type GObject.Object): a #GObject
2472 * @first_property_name: name of the first property to get
2473 * @...: return location for the first property, followed optionally by more
2474 * name/return location pairs, followed by %NULL
2476 * Gets properties of an object.
2478 * In general, a copy is made of the property contents and the caller
2479 * is responsible for freeing the memory in the appropriate manner for
2480 * the type, for instance by calling g_free() or g_object_unref().
2482 * Here is an example of using g_object_get() to get the contents
2483 * of three properties: an integer, a string and an object:
2484 * |[<!-- language="C" -->
2485 * gint intval;
2486 * gchar *strval;
2487 * GObject *objval;
2489 * g_object_get (my_object,
2490 * "int-property", &intval,
2491 * "str-property", &strval,
2492 * "obj-property", &objval,
2493 * NULL);
2495 * // Do something with intval, strval, objval
2497 * g_free (strval);
2498 * g_object_unref (objval);
2499 * ]|
2501 void
2502 g_object_get (gpointer _object,
2503 const gchar *first_property_name,
2504 ...)
2506 GObject *object = _object;
2507 va_list var_args;
2509 g_return_if_fail (G_IS_OBJECT (object));
2511 va_start (var_args, first_property_name);
2512 g_object_get_valist (object, first_property_name, var_args);
2513 va_end (var_args);
2517 * g_object_set_property:
2518 * @object: a #GObject
2519 * @property_name: the name of the property to set
2520 * @value: the value
2522 * Sets a property on an object.
2524 void
2525 g_object_set_property (GObject *object,
2526 const gchar *property_name,
2527 const GValue *value)
2529 g_object_setv (object, 1, &property_name, value);
2533 * g_object_get_property:
2534 * @object: a #GObject
2535 * @property_name: the name of the property to get
2536 * @value: return location for the property value
2538 * Gets a property of an object. @value must have been initialized to the
2539 * expected type of the property (or a type to which the expected type can be
2540 * transformed) using g_value_init().
2542 * In general, a copy is made of the property contents and the caller is
2543 * responsible for freeing the memory by calling g_value_unset().
2545 * Note that g_object_get_property() is really intended for language
2546 * bindings, g_object_get() is much more convenient for C programming.
2548 void
2549 g_object_get_property (GObject *object,
2550 const gchar *property_name,
2551 GValue *value)
2553 GParamSpec *pspec;
2555 g_return_if_fail (G_IS_OBJECT (object));
2556 g_return_if_fail (property_name != NULL);
2557 g_return_if_fail (G_IS_VALUE (value));
2559 g_object_ref (object);
2561 pspec = g_param_spec_pool_lookup (pspec_pool,
2562 property_name,
2563 G_OBJECT_TYPE (object),
2564 TRUE);
2566 if (g_object_get_is_valid_property (object, pspec, property_name))
2568 GValue *prop_value, tmp_value = G_VALUE_INIT;
2570 /* auto-conversion of the callers value type
2572 if (G_VALUE_TYPE (value) == pspec->value_type)
2574 g_value_reset (value);
2575 prop_value = value;
2577 else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
2579 g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
2580 G_STRFUNC, pspec->name,
2581 g_type_name (pspec->value_type),
2582 G_VALUE_TYPE_NAME (value));
2583 g_object_unref (object);
2584 return;
2586 else
2588 g_value_init (&tmp_value, pspec->value_type);
2589 prop_value = &tmp_value;
2591 object_get_property (object, pspec, prop_value);
2592 if (prop_value != value)
2594 g_value_transform (prop_value, value);
2595 g_value_unset (&tmp_value);
2599 g_object_unref (object);
2603 * g_object_connect: (skip)
2604 * @object: (type GObject.Object): a #GObject
2605 * @signal_spec: the spec for the first signal
2606 * @...: #GCallback for the first signal, followed by data for the
2607 * first signal, followed optionally by more signal
2608 * spec/callback/data triples, followed by %NULL
2610 * A convenience function to connect multiple signals at once.
2612 * The signal specs expected by this function have the form
2613 * "modifier::signal_name", where modifier can be one of the following:
2614 * * - signal: equivalent to g_signal_connect_data (..., NULL, 0)
2615 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., 0)
2616 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
2617 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
2618 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
2619 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
2620 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2621 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
2623 * |[<!-- language="C" -->
2624 * menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
2625 * "type", GTK_WINDOW_POPUP,
2626 * "child", menu,
2627 * NULL),
2628 * "signal::event", gtk_menu_window_event, menu,
2629 * "signal::size_request", gtk_menu_window_size_request, menu,
2630 * "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
2631 * NULL);
2632 * ]|
2634 * Returns: (transfer none) (type GObject.Object): @object
2636 gpointer
2637 g_object_connect (gpointer _object,
2638 const gchar *signal_spec,
2639 ...)
2641 GObject *object = _object;
2642 va_list var_args;
2644 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
2645 g_return_val_if_fail (object->ref_count > 0, object);
2647 va_start (var_args, signal_spec);
2648 while (signal_spec)
2650 GCallback callback = va_arg (var_args, GCallback);
2651 gpointer data = va_arg (var_args, gpointer);
2653 if (strncmp (signal_spec, "signal::", 8) == 0)
2654 g_signal_connect_data (object, signal_spec + 8,
2655 callback, data, NULL,
2657 else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
2658 strncmp (signal_spec, "object-signal::", 15) == 0)
2659 g_signal_connect_object (object, signal_spec + 15,
2660 callback, data,
2662 else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
2663 strncmp (signal_spec, "swapped-signal::", 16) == 0)
2664 g_signal_connect_data (object, signal_spec + 16,
2665 callback, data, NULL,
2666 G_CONNECT_SWAPPED);
2667 else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
2668 strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
2669 g_signal_connect_object (object, signal_spec + 23,
2670 callback, data,
2671 G_CONNECT_SWAPPED);
2672 else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
2673 strncmp (signal_spec, "signal-after::", 14) == 0)
2674 g_signal_connect_data (object, signal_spec + 14,
2675 callback, data, NULL,
2676 G_CONNECT_AFTER);
2677 else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
2678 strncmp (signal_spec, "object-signal-after::", 21) == 0)
2679 g_signal_connect_object (object, signal_spec + 21,
2680 callback, data,
2681 G_CONNECT_AFTER);
2682 else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
2683 strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
2684 g_signal_connect_data (object, signal_spec + 22,
2685 callback, data, NULL,
2686 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2687 else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
2688 strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
2689 g_signal_connect_object (object, signal_spec + 29,
2690 callback, data,
2691 G_CONNECT_SWAPPED | G_CONNECT_AFTER);
2692 else
2694 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2695 break;
2697 signal_spec = va_arg (var_args, gchar*);
2699 va_end (var_args);
2701 return object;
2705 * g_object_disconnect: (skip)
2706 * @object: (type GObject.Object): a #GObject
2707 * @signal_spec: the spec for the first signal
2708 * @...: #GCallback for the first signal, followed by data for the first signal,
2709 * followed optionally by more signal spec/callback/data triples,
2710 * followed by %NULL
2712 * A convenience function to disconnect multiple signals at once.
2714 * The signal specs expected by this function have the form
2715 * "any_signal", which means to disconnect any signal with matching
2716 * callback and data, or "any_signal::signal_name", which only
2717 * disconnects the signal named "signal_name".
2719 void
2720 g_object_disconnect (gpointer _object,
2721 const gchar *signal_spec,
2722 ...)
2724 GObject *object = _object;
2725 va_list var_args;
2727 g_return_if_fail (G_IS_OBJECT (object));
2728 g_return_if_fail (object->ref_count > 0);
2730 va_start (var_args, signal_spec);
2731 while (signal_spec)
2733 GCallback callback = va_arg (var_args, GCallback);
2734 gpointer data = va_arg (var_args, gpointer);
2735 guint sid = 0, detail = 0, mask = 0;
2737 if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
2738 strncmp (signal_spec, "any-signal::", 12) == 0)
2740 signal_spec += 12;
2741 mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2743 else if (strcmp (signal_spec, "any_signal") == 0 ||
2744 strcmp (signal_spec, "any-signal") == 0)
2746 signal_spec += 10;
2747 mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
2749 else
2751 g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
2752 break;
2755 if ((mask & G_SIGNAL_MATCH_ID) &&
2756 !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
2757 g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
2758 else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
2759 sid, detail,
2760 NULL, (gpointer)callback, data))
2761 g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
2762 signal_spec = va_arg (var_args, gchar*);
2764 va_end (var_args);
2767 typedef struct {
2768 GObject *object;
2769 guint n_weak_refs;
2770 struct {
2771 GWeakNotify notify;
2772 gpointer data;
2773 } weak_refs[1]; /* flexible array */
2774 } WeakRefStack;
2776 static void
2777 weak_refs_notify (gpointer data)
2779 WeakRefStack *wstack = data;
2780 guint i;
2782 for (i = 0; i < wstack->n_weak_refs; i++)
2783 wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
2784 g_free (wstack);
2788 * g_object_weak_ref: (skip)
2789 * @object: #GObject to reference weakly
2790 * @notify: callback to invoke before the object is freed
2791 * @data: extra data to pass to notify
2793 * Adds a weak reference callback to an object. Weak references are
2794 * used for notification when an object is finalized. They are called
2795 * "weak references" because they allow you to safely hold a pointer
2796 * to an object without calling g_object_ref() (g_object_ref() adds a
2797 * strong reference, that is, forces the object to stay alive).
2799 * Note that the weak references created by this method are not
2800 * thread-safe: they cannot safely be used in one thread if the
2801 * object's last g_object_unref() might happen in another thread.
2802 * Use #GWeakRef if thread-safety is required.
2804 void
2805 g_object_weak_ref (GObject *object,
2806 GWeakNotify notify,
2807 gpointer data)
2809 WeakRefStack *wstack;
2810 guint i;
2812 g_return_if_fail (G_IS_OBJECT (object));
2813 g_return_if_fail (notify != NULL);
2814 g_return_if_fail (object->ref_count >= 1);
2816 G_LOCK (weak_refs_mutex);
2817 wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
2818 if (wstack)
2820 i = wstack->n_weak_refs++;
2821 wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
2823 else
2825 wstack = g_renew (WeakRefStack, NULL, 1);
2826 wstack->object = object;
2827 wstack->n_weak_refs = 1;
2828 i = 0;
2830 wstack->weak_refs[i].notify = notify;
2831 wstack->weak_refs[i].data = data;
2832 g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
2833 G_UNLOCK (weak_refs_mutex);
2837 * g_object_weak_unref: (skip)
2838 * @object: #GObject to remove a weak reference from
2839 * @notify: callback to search for
2840 * @data: data to search for
2842 * Removes a weak reference callback to an object.
2844 void
2845 g_object_weak_unref (GObject *object,
2846 GWeakNotify notify,
2847 gpointer data)
2849 WeakRefStack *wstack;
2850 gboolean found_one = FALSE;
2852 g_return_if_fail (G_IS_OBJECT (object));
2853 g_return_if_fail (notify != NULL);
2855 G_LOCK (weak_refs_mutex);
2856 wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
2857 if (wstack)
2859 guint i;
2861 for (i = 0; i < wstack->n_weak_refs; i++)
2862 if (wstack->weak_refs[i].notify == notify &&
2863 wstack->weak_refs[i].data == data)
2865 found_one = TRUE;
2866 wstack->n_weak_refs -= 1;
2867 if (i != wstack->n_weak_refs)
2868 wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
2870 break;
2873 G_UNLOCK (weak_refs_mutex);
2874 if (!found_one)
2875 g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
2879 * g_object_add_weak_pointer: (skip)
2880 * @object: The object that should be weak referenced.
2881 * @weak_pointer_location: (inout) (not optional): The memory address
2882 * of a pointer.
2884 * Adds a weak reference from weak_pointer to @object to indicate that
2885 * the pointer located at @weak_pointer_location is only valid during
2886 * the lifetime of @object. When the @object is finalized,
2887 * @weak_pointer will be set to %NULL.
2889 * Note that as with g_object_weak_ref(), the weak references created by
2890 * this method are not thread-safe: they cannot safely be used in one
2891 * thread if the object's last g_object_unref() might happen in another
2892 * thread. Use #GWeakRef if thread-safety is required.
2894 void
2895 g_object_add_weak_pointer (GObject *object,
2896 gpointer *weak_pointer_location)
2898 g_return_if_fail (G_IS_OBJECT (object));
2899 g_return_if_fail (weak_pointer_location != NULL);
2901 g_object_weak_ref (object,
2902 (GWeakNotify) g_nullify_pointer,
2903 weak_pointer_location);
2907 * g_object_remove_weak_pointer: (skip)
2908 * @object: The object that is weak referenced.
2909 * @weak_pointer_location: (inout) (not optional): The memory address
2910 * of a pointer.
2912 * Removes a weak reference from @object that was previously added
2913 * using g_object_add_weak_pointer(). The @weak_pointer_location has
2914 * to match the one used with g_object_add_weak_pointer().
2916 void
2917 g_object_remove_weak_pointer (GObject *object,
2918 gpointer *weak_pointer_location)
2920 g_return_if_fail (G_IS_OBJECT (object));
2921 g_return_if_fail (weak_pointer_location != NULL);
2923 g_object_weak_unref (object,
2924 (GWeakNotify) g_nullify_pointer,
2925 weak_pointer_location);
2928 static guint
2929 object_floating_flag_handler (GObject *object,
2930 gint job)
2932 switch (job)
2934 gpointer oldvalue;
2935 case +1: /* force floating if possible */
2937 oldvalue = g_atomic_pointer_get (&object->qdata);
2938 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2939 (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
2940 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2941 case -1: /* sink if possible */
2943 oldvalue = g_atomic_pointer_get (&object->qdata);
2944 while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
2945 (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
2946 return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
2947 default: /* check floating */
2948 return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
2953 * g_object_is_floating:
2954 * @object: (type GObject.Object): a #GObject
2956 * Checks whether @object has a [floating][floating-ref] reference.
2958 * Since: 2.10
2960 * Returns: %TRUE if @object has a floating reference
2962 gboolean
2963 g_object_is_floating (gpointer _object)
2965 GObject *object = _object;
2966 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
2967 return floating_flag_handler (object, 0);
2971 * g_object_ref_sink:
2972 * @object: (type GObject.Object): a #GObject
2974 * Increase the reference count of @object, and possibly remove the
2975 * [floating][floating-ref] reference, if @object has a floating reference.
2977 * In other words, if the object is floating, then this call "assumes
2978 * ownership" of the floating reference, converting it to a normal
2979 * reference by clearing the floating flag while leaving the reference
2980 * count unchanged. If the object is not floating, then this call
2981 * adds a new normal reference increasing the reference count by one.
2983 * Since: 2.10
2985 * Returns: (type GObject.Object) (transfer none): @object
2987 gpointer
2988 g_object_ref_sink (gpointer _object)
2990 GObject *object = _object;
2991 gboolean was_floating;
2992 g_return_val_if_fail (G_IS_OBJECT (object), object);
2993 g_return_val_if_fail (object->ref_count >= 1, object);
2994 g_object_ref (object);
2995 was_floating = floating_flag_handler (object, -1);
2996 if (was_floating)
2997 g_object_unref (object);
2998 return object;
3002 * g_object_force_floating:
3003 * @object: a #GObject
3005 * This function is intended for #GObject implementations to re-enforce
3006 * a [floating][floating-ref] object reference. Doing this is seldom
3007 * required: all #GInitiallyUnowneds are created with a floating reference
3008 * which usually just needs to be sunken by calling g_object_ref_sink().
3010 * Since: 2.10
3012 void
3013 g_object_force_floating (GObject *object)
3015 g_return_if_fail (G_IS_OBJECT (object));
3016 g_return_if_fail (object->ref_count >= 1);
3018 floating_flag_handler (object, +1);
3021 typedef struct {
3022 GObject *object;
3023 guint n_toggle_refs;
3024 struct {
3025 GToggleNotify notify;
3026 gpointer data;
3027 } toggle_refs[1]; /* flexible array */
3028 } ToggleRefStack;
3030 static void
3031 toggle_refs_notify (GObject *object,
3032 gboolean is_last_ref)
3034 ToggleRefStack tstack, *tstackptr;
3036 G_LOCK (toggle_refs_mutex);
3037 tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3038 tstack = *tstackptr;
3039 G_UNLOCK (toggle_refs_mutex);
3041 /* Reentrancy here is not as tricky as it seems, because a toggle reference
3042 * will only be notified when there is exactly one of them.
3044 g_assert (tstack.n_toggle_refs == 1);
3045 tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
3049 * g_object_add_toggle_ref: (skip)
3050 * @object: a #GObject
3051 * @notify: a function to call when this reference is the
3052 * last reference to the object, or is no longer
3053 * the last reference.
3054 * @data: data to pass to @notify
3056 * Increases the reference count of the object by one and sets a
3057 * callback to be called when all other references to the object are
3058 * dropped, or when this is already the last reference to the object
3059 * and another reference is established.
3061 * This functionality is intended for binding @object to a proxy
3062 * object managed by another memory manager. This is done with two
3063 * paired references: the strong reference added by
3064 * g_object_add_toggle_ref() and a reverse reference to the proxy
3065 * object which is either a strong reference or weak reference.
3067 * The setup is that when there are no other references to @object,
3068 * only a weak reference is held in the reverse direction from @object
3069 * to the proxy object, but when there are other references held to
3070 * @object, a strong reference is held. The @notify callback is called
3071 * when the reference from @object to the proxy object should be
3072 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
3073 * (@is_last_ref false).
3075 * Since a (normal) reference must be held to the object before
3076 * calling g_object_add_toggle_ref(), the initial state of the reverse
3077 * link is always strong.
3079 * Multiple toggle references may be added to the same gobject,
3080 * however if there are multiple toggle references to an object, none
3081 * of them will ever be notified until all but one are removed. For
3082 * this reason, you should only ever use a toggle reference if there
3083 * is important state in the proxy object.
3085 * Since: 2.8
3087 void
3088 g_object_add_toggle_ref (GObject *object,
3089 GToggleNotify notify,
3090 gpointer data)
3092 ToggleRefStack *tstack;
3093 guint i;
3095 g_return_if_fail (G_IS_OBJECT (object));
3096 g_return_if_fail (notify != NULL);
3097 g_return_if_fail (object->ref_count >= 1);
3099 g_object_ref (object);
3101 G_LOCK (toggle_refs_mutex);
3102 tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
3103 if (tstack)
3105 i = tstack->n_toggle_refs++;
3106 /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
3107 * in tstate->toggle_refs */
3108 tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
3110 else
3112 tstack = g_renew (ToggleRefStack, NULL, 1);
3113 tstack->object = object;
3114 tstack->n_toggle_refs = 1;
3115 i = 0;
3118 /* Set a flag for fast lookup after adding the first toggle reference */
3119 if (tstack->n_toggle_refs == 1)
3120 g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3122 tstack->toggle_refs[i].notify = notify;
3123 tstack->toggle_refs[i].data = data;
3124 g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
3125 (GDestroyNotify)g_free);
3126 G_UNLOCK (toggle_refs_mutex);
3130 * g_object_remove_toggle_ref: (skip)
3131 * @object: a #GObject
3132 * @notify: a function to call when this reference is the
3133 * last reference to the object, or is no longer
3134 * the last reference.
3135 * @data: data to pass to @notify
3137 * Removes a reference added with g_object_add_toggle_ref(). The
3138 * reference count of the object is decreased by one.
3140 * Since: 2.8
3142 void
3143 g_object_remove_toggle_ref (GObject *object,
3144 GToggleNotify notify,
3145 gpointer data)
3147 ToggleRefStack *tstack;
3148 gboolean found_one = FALSE;
3150 g_return_if_fail (G_IS_OBJECT (object));
3151 g_return_if_fail (notify != NULL);
3153 G_LOCK (toggle_refs_mutex);
3154 tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3155 if (tstack)
3157 guint i;
3159 for (i = 0; i < tstack->n_toggle_refs; i++)
3160 if (tstack->toggle_refs[i].notify == notify &&
3161 tstack->toggle_refs[i].data == data)
3163 found_one = TRUE;
3164 tstack->n_toggle_refs -= 1;
3165 if (i != tstack->n_toggle_refs)
3166 tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3168 if (tstack->n_toggle_refs == 0)
3169 g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3171 break;
3174 G_UNLOCK (toggle_refs_mutex);
3176 if (found_one)
3177 g_object_unref (object);
3178 else
3179 g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3183 * g_object_ref:
3184 * @object: (type GObject.Object): a #GObject
3186 * Increases the reference count of @object.
3188 * Returns: (type GObject.Object) (transfer none): the same @object
3190 gpointer
3191 g_object_ref (gpointer _object)
3193 GObject *object = _object;
3194 gint old_val;
3196 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3197 g_return_val_if_fail (object->ref_count > 0, NULL);
3199 old_val = g_atomic_int_add (&object->ref_count, 1);
3201 if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3202 toggle_refs_notify (object, FALSE);
3204 TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3206 return object;
3210 * g_object_unref:
3211 * @object: (type GObject.Object): a #GObject
3213 * Decreases the reference count of @object. When its reference count
3214 * drops to 0, the object is finalized (i.e. its memory is freed).
3216 * If the pointer to the #GObject may be reused in future (for example, if it is
3217 * an instance variable of another object), it is recommended to clear the
3218 * pointer to %NULL rather than retain a dangling pointer to a potentially
3219 * invalid #GObject instance. Use g_clear_object() for this.
3221 void
3222 g_object_unref (gpointer _object)
3224 GObject *object = _object;
3225 gint old_ref;
3227 g_return_if_fail (G_IS_OBJECT (object));
3228 g_return_if_fail (object->ref_count > 0);
3230 /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3231 retry_atomic_decrement1:
3232 old_ref = g_atomic_int_get (&object->ref_count);
3233 if (old_ref > 1)
3235 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3236 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3238 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3239 goto retry_atomic_decrement1;
3241 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3243 /* if we went from 2->1 we need to notify toggle refs if any */
3244 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3245 toggle_refs_notify (object, TRUE);
3247 else
3249 GSList **weak_locations;
3251 /* The only way that this object can live at this point is if
3252 * there are outstanding weak references already established
3253 * before we got here.
3255 * If there were not already weak references then no more can be
3256 * established at this time, because the other thread would have
3257 * to hold a strong ref in order to call
3258 * g_object_add_weak_pointer() and then we wouldn't be here.
3260 weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3262 if (weak_locations != NULL)
3264 g_rw_lock_writer_lock (&weak_locations_lock);
3266 /* It is possible that one of the weak references beat us to
3267 * the lock. Make sure the refcount is still what we expected
3268 * it to be.
3270 old_ref = g_atomic_int_get (&object->ref_count);
3271 if (old_ref != 1)
3273 g_rw_lock_writer_unlock (&weak_locations_lock);
3274 goto retry_atomic_decrement1;
3277 /* We got the lock first, so the object will definitely die
3278 * now. Clear out all the weak references.
3280 while (*weak_locations)
3282 GWeakRef *weak_ref_location = (*weak_locations)->data;
3284 weak_ref_location->priv.p = NULL;
3285 *weak_locations = g_slist_delete_link (*weak_locations, *weak_locations);
3288 g_rw_lock_writer_unlock (&weak_locations_lock);
3291 /* we are about to remove the last reference */
3292 TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3293 G_OBJECT_GET_CLASS (object)->dispose (object);
3294 TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3296 /* may have been re-referenced meanwhile */
3297 retry_atomic_decrement2:
3298 old_ref = g_atomic_int_get ((int *)&object->ref_count);
3299 if (old_ref > 1)
3301 /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3302 gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3304 if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3305 goto retry_atomic_decrement2;
3307 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3309 /* if we went from 2->1 we need to notify toggle refs if any */
3310 if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3311 toggle_refs_notify (object, TRUE);
3313 return;
3316 /* we are still in the process of taking away the last ref */
3317 g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3318 g_signal_handlers_destroy (object);
3319 g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3321 /* decrement the last reference */
3322 old_ref = g_atomic_int_add (&object->ref_count, -1);
3324 TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3326 /* may have been re-referenced meanwhile */
3327 if (G_LIKELY (old_ref == 1))
3329 TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3330 G_OBJECT_GET_CLASS (object)->finalize (object);
3332 TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3334 GOBJECT_IF_DEBUG (OBJECTS,
3336 /* catch objects not chaining finalize handlers */
3337 G_LOCK (debug_objects);
3338 g_assert (g_hash_table_lookup (debug_objects_ht, object) == NULL);
3339 G_UNLOCK (debug_objects);
3341 g_type_free_instance ((GTypeInstance*) object);
3347 * g_clear_object: (skip)
3348 * @object_ptr: a pointer to a #GObject reference
3350 * Clears a reference to a #GObject.
3352 * @object_ptr must not be %NULL.
3354 * If the reference is %NULL then this function does nothing.
3355 * Otherwise, the reference count of the object is decreased and the
3356 * pointer is set to %NULL.
3358 * A macro is also included that allows this function to be used without
3359 * pointer casts.
3361 * Since: 2.28
3363 #undef g_clear_object
3364 void
3365 g_clear_object (volatile GObject **object_ptr)
3367 g_clear_pointer (object_ptr, g_object_unref);
3371 * g_object_get_qdata:
3372 * @object: The GObject to get a stored user data pointer from
3373 * @quark: A #GQuark, naming the user data pointer
3375 * This function gets back user data pointers stored via
3376 * g_object_set_qdata().
3378 * Returns: (transfer none) (nullable): The user data pointer set, or %NULL
3380 gpointer
3381 g_object_get_qdata (GObject *object,
3382 GQuark quark)
3384 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3386 return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3390 * g_object_set_qdata: (skip)
3391 * @object: The GObject to set store a user data pointer
3392 * @quark: A #GQuark, naming the user data pointer
3393 * @data: (nullable): An opaque user data pointer
3395 * This sets an opaque, named pointer on an object.
3396 * The name is specified through a #GQuark (retrived e.g. via
3397 * g_quark_from_static_string()), and the pointer
3398 * can be gotten back from the @object with g_object_get_qdata()
3399 * until the @object is finalized.
3400 * Setting a previously set user data pointer, overrides (frees)
3401 * the old pointer set, using #NULL as pointer essentially
3402 * removes the data stored.
3404 void
3405 g_object_set_qdata (GObject *object,
3406 GQuark quark,
3407 gpointer data)
3409 g_return_if_fail (G_IS_OBJECT (object));
3410 g_return_if_fail (quark > 0);
3412 g_datalist_id_set_data (&object->qdata, quark, data);
3416 * g_object_dup_qdata: (skip)
3417 * @object: the #GObject to store user data on
3418 * @quark: a #GQuark, naming the user data pointer
3419 * @dup_func: (nullable): function to dup the value
3420 * @user_data: (nullable): passed as user_data to @dup_func
3422 * This is a variant of g_object_get_qdata() which returns
3423 * a 'duplicate' of the value. @dup_func defines the
3424 * meaning of 'duplicate' in this context, it could e.g.
3425 * take a reference on a ref-counted object.
3427 * If the @quark is not set on the object then @dup_func
3428 * will be called with a %NULL argument.
3430 * Note that @dup_func is called while user data of @object
3431 * is locked.
3433 * This function can be useful to avoid races when multiple
3434 * threads are using object data on the same key on the same
3435 * object.
3437 * Returns: the result of calling @dup_func on the value
3438 * associated with @quark on @object, or %NULL if not set.
3439 * If @dup_func is %NULL, the value is returned
3440 * unmodified.
3442 * Since: 2.34
3444 gpointer
3445 g_object_dup_qdata (GObject *object,
3446 GQuark quark,
3447 GDuplicateFunc dup_func,
3448 gpointer user_data)
3450 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3451 g_return_val_if_fail (quark > 0, NULL);
3453 return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
3457 * g_object_replace_qdata: (skip)
3458 * @object: the #GObject to store user data on
3459 * @quark: a #GQuark, naming the user data pointer
3460 * @oldval: (nullable): the old value to compare against
3461 * @newval: (nullable): the new value
3462 * @destroy: (nullable): a destroy notify for the new value
3463 * @old_destroy: (out) (optional): destroy notify for the existing value
3465 * Compares the user data for the key @quark on @object with
3466 * @oldval, and if they are the same, replaces @oldval with
3467 * @newval.
3469 * This is like a typical atomic compare-and-exchange
3470 * operation, for user data on an object.
3472 * If the previous value was replaced then ownership of the
3473 * old value (@oldval) is passed to the caller, including
3474 * the registered destroy notify for it (passed out in @old_destroy).
3475 * Its up to the caller to free this as he wishes, which may
3476 * or may not include using @old_destroy as sometimes replacement
3477 * should not destroy the object in the normal way.
3479 * Returns: %TRUE if the existing value for @quark was replaced
3480 * by @newval, %FALSE otherwise.
3482 * Since: 2.34
3484 gboolean
3485 g_object_replace_qdata (GObject *object,
3486 GQuark quark,
3487 gpointer oldval,
3488 gpointer newval,
3489 GDestroyNotify destroy,
3490 GDestroyNotify *old_destroy)
3492 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3493 g_return_val_if_fail (quark > 0, FALSE);
3495 return g_datalist_id_replace_data (&object->qdata, quark,
3496 oldval, newval, destroy,
3497 old_destroy);
3501 * g_object_set_qdata_full: (skip)
3502 * @object: The GObject to set store a user data pointer
3503 * @quark: A #GQuark, naming the user data pointer
3504 * @data: (nullable): An opaque user data pointer
3505 * @destroy: (nullable): Function to invoke with @data as argument, when @data
3506 * needs to be freed
3508 * This function works like g_object_set_qdata(), but in addition,
3509 * a void (*destroy) (gpointer) function may be specified which is
3510 * called with @data as argument when the @object is finalized, or
3511 * the data is being overwritten by a call to g_object_set_qdata()
3512 * with the same @quark.
3514 void
3515 g_object_set_qdata_full (GObject *object,
3516 GQuark quark,
3517 gpointer data,
3518 GDestroyNotify destroy)
3520 g_return_if_fail (G_IS_OBJECT (object));
3521 g_return_if_fail (quark > 0);
3523 g_datalist_id_set_data_full (&object->qdata, quark, data,
3524 data ? destroy : (GDestroyNotify) NULL);
3528 * g_object_steal_qdata:
3529 * @object: The GObject to get a stored user data pointer from
3530 * @quark: A #GQuark, naming the user data pointer
3532 * This function gets back user data pointers stored via
3533 * g_object_set_qdata() and removes the @data from object
3534 * without invoking its destroy() function (if any was
3535 * set).
3536 * Usually, calling this function is only required to update
3537 * user data pointers with a destroy notifier, for example:
3538 * |[<!-- language="C" -->
3539 * void
3540 * object_add_to_user_list (GObject *object,
3541 * const gchar *new_string)
3543 * // the quark, naming the object data
3544 * GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
3545 * // retrive the old string list
3546 * GList *list = g_object_steal_qdata (object, quark_string_list);
3548 * // prepend new string
3549 * list = g_list_prepend (list, g_strdup (new_string));
3550 * // this changed 'list', so we need to set it again
3551 * g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
3553 * static void
3554 * free_string_list (gpointer data)
3556 * GList *node, *list = data;
3558 * for (node = list; node; node = node->next)
3559 * g_free (node->data);
3560 * g_list_free (list);
3562 * ]|
3563 * Using g_object_get_qdata() in the above example, instead of
3564 * g_object_steal_qdata() would have left the destroy function set,
3565 * and thus the partial string list would have been freed upon
3566 * g_object_set_qdata_full().
3568 * Returns: (transfer full) (nullable): The user data pointer set, or %NULL
3570 gpointer
3571 g_object_steal_qdata (GObject *object,
3572 GQuark quark)
3574 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3575 g_return_val_if_fail (quark > 0, NULL);
3577 return g_datalist_id_remove_no_notify (&object->qdata, quark);
3581 * g_object_get_data:
3582 * @object: #GObject containing the associations
3583 * @key: name of the key for that association
3585 * Gets a named field from the objects table of associations (see g_object_set_data()).
3587 * Returns: (transfer none) (nullable): the data if found,
3588 * or %NULL if no such data exists.
3590 gpointer
3591 g_object_get_data (GObject *object,
3592 const gchar *key)
3594 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3595 g_return_val_if_fail (key != NULL, NULL);
3597 return g_datalist_get_data (&object->qdata, key);
3601 * g_object_set_data:
3602 * @object: #GObject containing the associations.
3603 * @key: name of the key
3604 * @data: (nullable): data to associate with that key
3606 * Each object carries around a table of associations from
3607 * strings to pointers. This function lets you set an association.
3609 * If the object already had an association with that name,
3610 * the old association will be destroyed.
3612 void
3613 g_object_set_data (GObject *object,
3614 const gchar *key,
3615 gpointer data)
3617 g_return_if_fail (G_IS_OBJECT (object));
3618 g_return_if_fail (key != NULL);
3620 g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
3624 * g_object_dup_data: (skip)
3625 * @object: the #GObject to store user data on
3626 * @key: a string, naming the user data pointer
3627 * @dup_func: (nullable): function to dup the value
3628 * @user_data: (nullable): passed as user_data to @dup_func
3630 * This is a variant of g_object_get_data() which returns
3631 * a 'duplicate' of the value. @dup_func defines the
3632 * meaning of 'duplicate' in this context, it could e.g.
3633 * take a reference on a ref-counted object.
3635 * If the @key is not set on the object then @dup_func
3636 * will be called with a %NULL argument.
3638 * Note that @dup_func is called while user data of @object
3639 * is locked.
3641 * This function can be useful to avoid races when multiple
3642 * threads are using object data on the same key on the same
3643 * object.
3645 * Returns: the result of calling @dup_func on the value
3646 * associated with @key on @object, or %NULL if not set.
3647 * If @dup_func is %NULL, the value is returned
3648 * unmodified.
3650 * Since: 2.34
3652 gpointer
3653 g_object_dup_data (GObject *object,
3654 const gchar *key,
3655 GDuplicateFunc dup_func,
3656 gpointer user_data)
3658 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3659 g_return_val_if_fail (key != NULL, NULL);
3661 return g_datalist_id_dup_data (&object->qdata,
3662 g_quark_from_string (key),
3663 dup_func, user_data);
3667 * g_object_replace_data: (skip)
3668 * @object: the #GObject to store user data on
3669 * @key: a string, naming the user data pointer
3670 * @oldval: (nullable): the old value to compare against
3671 * @newval: (nullable): the new value
3672 * @destroy: (nullable): a destroy notify for the new value
3673 * @old_destroy: (out) (optional): destroy notify for the existing value
3675 * Compares the user data for the key @key on @object with
3676 * @oldval, and if they are the same, replaces @oldval with
3677 * @newval.
3679 * This is like a typical atomic compare-and-exchange
3680 * operation, for user data on an object.
3682 * If the previous value was replaced then ownership of the
3683 * old value (@oldval) is passed to the caller, including
3684 * the registered destroy notify for it (passed out in @old_destroy).
3685 * Its up to the caller to free this as he wishes, which may
3686 * or may not include using @old_destroy as sometimes replacement
3687 * should not destroy the object in the normal way.
3689 * Returns: %TRUE if the existing value for @key was replaced
3690 * by @newval, %FALSE otherwise.
3692 * Since: 2.34
3694 gboolean
3695 g_object_replace_data (GObject *object,
3696 const gchar *key,
3697 gpointer oldval,
3698 gpointer newval,
3699 GDestroyNotify destroy,
3700 GDestroyNotify *old_destroy)
3702 g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3703 g_return_val_if_fail (key != NULL, FALSE);
3705 return g_datalist_id_replace_data (&object->qdata,
3706 g_quark_from_string (key),
3707 oldval, newval, destroy,
3708 old_destroy);
3712 * g_object_set_data_full: (skip)
3713 * @object: #GObject containing the associations
3714 * @key: name of the key
3715 * @data: (nullable): data to associate with that key
3716 * @destroy: (nullable): function to call when the association is destroyed
3718 * Like g_object_set_data() except it adds notification
3719 * for when the association is destroyed, either by setting it
3720 * to a different value or when the object is destroyed.
3722 * Note that the @destroy callback is not called if @data is %NULL.
3724 void
3725 g_object_set_data_full (GObject *object,
3726 const gchar *key,
3727 gpointer data,
3728 GDestroyNotify destroy)
3730 g_return_if_fail (G_IS_OBJECT (object));
3731 g_return_if_fail (key != NULL);
3733 g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
3734 data ? destroy : (GDestroyNotify) NULL);
3738 * g_object_steal_data:
3739 * @object: #GObject containing the associations
3740 * @key: name of the key
3742 * Remove a specified datum from the object's data associations,
3743 * without invoking the association's destroy handler.
3745 * Returns: (transfer full) (nullable): the data if found, or %NULL
3746 * if no such data exists.
3748 gpointer
3749 g_object_steal_data (GObject *object,
3750 const gchar *key)
3752 GQuark quark;
3754 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3755 g_return_val_if_fail (key != NULL, NULL);
3757 quark = g_quark_try_string (key);
3759 return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
3762 static void
3763 g_value_object_init (GValue *value)
3765 value->data[0].v_pointer = NULL;
3768 static void
3769 g_value_object_free_value (GValue *value)
3771 if (value->data[0].v_pointer)
3772 g_object_unref (value->data[0].v_pointer);
3775 static void
3776 g_value_object_copy_value (const GValue *src_value,
3777 GValue *dest_value)
3779 if (src_value->data[0].v_pointer)
3780 dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
3781 else
3782 dest_value->data[0].v_pointer = NULL;
3785 static void
3786 g_value_object_transform_value (const GValue *src_value,
3787 GValue *dest_value)
3789 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)))
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 gpointer
3796 g_value_object_peek_pointer (const GValue *value)
3798 return value->data[0].v_pointer;
3801 static gchar*
3802 g_value_object_collect_value (GValue *value,
3803 guint n_collect_values,
3804 GTypeCValue *collect_values,
3805 guint collect_flags)
3807 if (collect_values[0].v_pointer)
3809 GObject *object = collect_values[0].v_pointer;
3811 if (object->g_type_instance.g_class == NULL)
3812 return g_strconcat ("invalid unclassed object pointer for value type '",
3813 G_VALUE_TYPE_NAME (value),
3814 "'",
3815 NULL);
3816 else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
3817 return g_strconcat ("invalid object type '",
3818 G_OBJECT_TYPE_NAME (object),
3819 "' for value type '",
3820 G_VALUE_TYPE_NAME (value),
3821 "'",
3822 NULL);
3823 /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
3824 value->data[0].v_pointer = g_object_ref (object);
3826 else
3827 value->data[0].v_pointer = NULL;
3829 return NULL;
3832 static gchar*
3833 g_value_object_lcopy_value (const GValue *value,
3834 guint n_collect_values,
3835 GTypeCValue *collect_values,
3836 guint collect_flags)
3838 GObject **object_p = collect_values[0].v_pointer;
3840 if (!object_p)
3841 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
3843 if (!value->data[0].v_pointer)
3844 *object_p = NULL;
3845 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
3846 *object_p = value->data[0].v_pointer;
3847 else
3848 *object_p = g_object_ref (value->data[0].v_pointer);
3850 return NULL;
3854 * g_value_set_object:
3855 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3856 * @v_object: (type GObject.Object) (nullable): object value to be set
3858 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
3860 * g_value_set_object() increases the reference count of @v_object
3861 * (the #GValue holds a reference to @v_object). If you do not wish
3862 * to increase the reference count of the object (i.e. you wish to
3863 * pass your current reference to the #GValue because you no longer
3864 * need it), use g_value_take_object() instead.
3866 * It is important that your #GValue holds a reference to @v_object (either its
3867 * own, or one it has taken) to ensure that the object won't be destroyed while
3868 * the #GValue still exists).
3870 void
3871 g_value_set_object (GValue *value,
3872 gpointer v_object)
3874 GObject *old;
3876 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3878 old = value->data[0].v_pointer;
3880 if (v_object)
3882 g_return_if_fail (G_IS_OBJECT (v_object));
3883 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3885 value->data[0].v_pointer = v_object;
3886 g_object_ref (value->data[0].v_pointer);
3888 else
3889 value->data[0].v_pointer = NULL;
3891 if (old)
3892 g_object_unref (old);
3896 * g_value_set_object_take_ownership: (skip)
3897 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3898 * @v_object: (nullable): object value to be set
3900 * This is an internal function introduced mainly for C marshallers.
3902 * Deprecated: 2.4: Use g_value_take_object() instead.
3904 void
3905 g_value_set_object_take_ownership (GValue *value,
3906 gpointer v_object)
3908 g_value_take_object (value, v_object);
3912 * g_value_take_object: (skip)
3913 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3914 * @v_object: (nullable): object value to be set
3916 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
3917 * and takes over the ownership of the callers reference to @v_object;
3918 * the caller doesn't have to unref it any more (i.e. the reference
3919 * count of the object is not increased).
3921 * If you want the #GValue to hold its own reference to @v_object, use
3922 * g_value_set_object() instead.
3924 * Since: 2.4
3926 void
3927 g_value_take_object (GValue *value,
3928 gpointer v_object)
3930 g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
3932 if (value->data[0].v_pointer)
3934 g_object_unref (value->data[0].v_pointer);
3935 value->data[0].v_pointer = NULL;
3938 if (v_object)
3940 g_return_if_fail (G_IS_OBJECT (v_object));
3941 g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
3943 value->data[0].v_pointer = v_object; /* we take over the reference count */
3948 * g_value_get_object:
3949 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
3951 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
3953 * Returns: (type GObject.Object) (transfer none): object contents of @value
3955 gpointer
3956 g_value_get_object (const GValue *value)
3958 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3960 return value->data[0].v_pointer;
3964 * g_value_dup_object:
3965 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
3967 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
3968 * its reference count. If the contents of the #GValue are %NULL, then
3969 * %NULL will be returned.
3971 * Returns: (type GObject.Object) (transfer full): object content of @value,
3972 * should be unreferenced when no longer needed.
3974 gpointer
3975 g_value_dup_object (const GValue *value)
3977 g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
3979 return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
3983 * g_signal_connect_object: (skip)
3984 * @instance: (type GObject.TypeInstance): the instance to connect to.
3985 * @detailed_signal: a string of the form "signal-name::detail".
3986 * @c_handler: the #GCallback to connect.
3987 * @gobject: (type GObject.Object) (nullable): the object to pass as data
3988 * to @c_handler.
3989 * @connect_flags: a combination of #GConnectFlags.
3991 * This is similar to g_signal_connect_data(), but uses a closure which
3992 * ensures that the @gobject stays alive during the call to @c_handler
3993 * by temporarily adding a reference count to @gobject.
3995 * When the @gobject is destroyed the signal handler will be automatically
3996 * disconnected. Note that this is not currently threadsafe (ie:
3997 * emitting a signal while @gobject is being destroyed in another thread
3998 * is not safe).
4000 * Returns: the handler id.
4002 gulong
4003 g_signal_connect_object (gpointer instance,
4004 const gchar *detailed_signal,
4005 GCallback c_handler,
4006 gpointer gobject,
4007 GConnectFlags connect_flags)
4009 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
4010 g_return_val_if_fail (detailed_signal != NULL, 0);
4011 g_return_val_if_fail (c_handler != NULL, 0);
4013 if (gobject)
4015 GClosure *closure;
4017 g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
4019 closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
4021 return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
4023 else
4024 return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
4027 typedef struct {
4028 GObject *object;
4029 guint n_closures;
4030 GClosure *closures[1]; /* flexible array */
4031 } CArray;
4032 /* don't change this structure without supplying an accessor for
4033 * watched closures, e.g.:
4034 * GSList* g_object_list_watched_closures (GObject *object)
4036 * CArray *carray;
4037 * g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4038 * carray = g_object_get_data (object, "GObject-closure-array");
4039 * if (carray)
4041 * GSList *slist = NULL;
4042 * guint i;
4043 * for (i = 0; i < carray->n_closures; i++)
4044 * slist = g_slist_prepend (slist, carray->closures[i]);
4045 * return slist;
4047 * return NULL;
4051 static void
4052 object_remove_closure (gpointer data,
4053 GClosure *closure)
4055 GObject *object = data;
4056 CArray *carray;
4057 guint i;
4059 G_LOCK (closure_array_mutex);
4060 carray = g_object_get_qdata (object, quark_closure_array);
4061 for (i = 0; i < carray->n_closures; i++)
4062 if (carray->closures[i] == closure)
4064 carray->n_closures--;
4065 if (i < carray->n_closures)
4066 carray->closures[i] = carray->closures[carray->n_closures];
4067 G_UNLOCK (closure_array_mutex);
4068 return;
4070 G_UNLOCK (closure_array_mutex);
4071 g_assert_not_reached ();
4074 static void
4075 destroy_closure_array (gpointer data)
4077 CArray *carray = data;
4078 GObject *object = carray->object;
4079 guint i, n = carray->n_closures;
4081 for (i = 0; i < n; i++)
4083 GClosure *closure = carray->closures[i];
4085 /* removing object_remove_closure() upfront is probably faster than
4086 * letting it fiddle with quark_closure_array which is empty anyways
4088 g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
4089 g_closure_invalidate (closure);
4091 g_free (carray);
4095 * g_object_watch_closure:
4096 * @object: GObject restricting lifetime of @closure
4097 * @closure: GClosure to watch
4099 * This function essentially limits the life time of the @closure to
4100 * the life time of the object. That is, when the object is finalized,
4101 * the @closure is invalidated by calling g_closure_invalidate() on
4102 * it, in order to prevent invocations of the closure with a finalized
4103 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
4104 * added as marshal guards to the @closure, to ensure that an extra
4105 * reference count is held on @object during invocation of the
4106 * @closure. Usually, this function will be called on closures that
4107 * use this @object as closure data.
4109 void
4110 g_object_watch_closure (GObject *object,
4111 GClosure *closure)
4113 CArray *carray;
4114 guint i;
4116 g_return_if_fail (G_IS_OBJECT (object));
4117 g_return_if_fail (closure != NULL);
4118 g_return_if_fail (closure->is_invalid == FALSE);
4119 g_return_if_fail (closure->in_marshal == FALSE);
4120 g_return_if_fail (object->ref_count > 0); /* this doesn't work on finalizing objects */
4122 g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
4123 g_closure_add_marshal_guards (closure,
4124 object, (GClosureNotify) g_object_ref,
4125 object, (GClosureNotify) g_object_unref);
4126 G_LOCK (closure_array_mutex);
4127 carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
4128 if (!carray)
4130 carray = g_renew (CArray, NULL, 1);
4131 carray->object = object;
4132 carray->n_closures = 1;
4133 i = 0;
4135 else
4137 i = carray->n_closures++;
4138 carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4140 carray->closures[i] = closure;
4141 g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4142 G_UNLOCK (closure_array_mutex);
4146 * g_closure_new_object:
4147 * @sizeof_closure: the size of the structure to allocate, must be at least
4148 * `sizeof (GClosure)`
4149 * @object: a #GObject pointer to store in the @data field of the newly
4150 * allocated #GClosure
4152 * A variant of g_closure_new_simple() which stores @object in the
4153 * @data field of the closure and calls g_object_watch_closure() on
4154 * @object and the created closure. This function is mainly useful
4155 * when implementing new types of closures.
4157 * Returns: (transfer full): a newly allocated #GClosure
4159 GClosure*
4160 g_closure_new_object (guint sizeof_closure,
4161 GObject *object)
4163 GClosure *closure;
4165 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4166 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4168 closure = g_closure_new_simple (sizeof_closure, object);
4169 g_object_watch_closure (object, closure);
4171 return closure;
4175 * g_cclosure_new_object: (skip)
4176 * @callback_func: the function to invoke
4177 * @object: a #GObject pointer to pass to @callback_func
4179 * A variant of g_cclosure_new() which uses @object as @user_data and
4180 * calls g_object_watch_closure() on @object and the created
4181 * closure. This function is useful when you have a callback closely
4182 * associated with a #GObject, and want the callback to no longer run
4183 * after the object is is freed.
4185 * Returns: a new #GCClosure
4187 GClosure*
4188 g_cclosure_new_object (GCallback callback_func,
4189 GObject *object)
4191 GClosure *closure;
4193 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4194 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4195 g_return_val_if_fail (callback_func != NULL, NULL);
4197 closure = g_cclosure_new (callback_func, object, NULL);
4198 g_object_watch_closure (object, closure);
4200 return closure;
4204 * g_cclosure_new_object_swap: (skip)
4205 * @callback_func: the function to invoke
4206 * @object: a #GObject pointer to pass to @callback_func
4208 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4209 * and calls g_object_watch_closure() on @object and the created
4210 * closure. This function is useful when you have a callback closely
4211 * associated with a #GObject, and want the callback to no longer run
4212 * after the object is is freed.
4214 * Returns: a new #GCClosure
4216 GClosure*
4217 g_cclosure_new_object_swap (GCallback callback_func,
4218 GObject *object)
4220 GClosure *closure;
4222 g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4223 g_return_val_if_fail (object->ref_count > 0, NULL); /* this doesn't work on finalizing objects */
4224 g_return_val_if_fail (callback_func != NULL, NULL);
4226 closure = g_cclosure_new_swap (callback_func, object, NULL);
4227 g_object_watch_closure (object, closure);
4229 return closure;
4232 gsize
4233 g_object_compat_control (gsize what,
4234 gpointer data)
4236 switch (what)
4238 gpointer *pp;
4239 case 1: /* floating base type */
4240 return G_TYPE_INITIALLY_UNOWNED;
4241 case 2: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4242 floating_flag_handler = (guint(*)(GObject*,gint)) data;
4243 return 1;
4244 case 3: /* FIXME: remove this once GLib/Gtk+ break ABI again */
4245 pp = data;
4246 *pp = floating_flag_handler;
4247 return 1;
4248 default:
4249 return 0;
4253 G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
4255 static void
4256 g_initially_unowned_init (GInitiallyUnowned *object)
4258 g_object_force_floating (object);
4261 static void
4262 g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4267 * GWeakRef:
4269 * A structure containing a weak reference to a #GObject. It can either
4270 * be empty (i.e. point to %NULL), or point to an object for as long as
4271 * at least one "strong" reference to that object exists. Before the
4272 * object's #GObjectClass.dispose method is called, every #GWeakRef
4273 * associated with becomes empty (i.e. points to %NULL).
4275 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4276 * heap-allocated, or embedded in larger structures.
4278 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4279 * reference is thread-safe: converting a weak pointer to a reference is
4280 * atomic with respect to invalidation of weak pointers to destroyed
4281 * objects.
4283 * If the object's #GObjectClass.dispose method results in additional
4284 * references to the object being held, any #GWeakRefs taken
4285 * before it was disposed will continue to point to %NULL. If
4286 * #GWeakRefs are taken after the object is disposed and
4287 * re-referenced, they will continue to point to it until its refcount
4288 * goes back to zero, at which point they too will be invalidated.
4292 * g_weak_ref_init: (skip)
4293 * @weak_ref: (inout): uninitialized or empty location for a weak
4294 * reference
4295 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4297 * Initialise a non-statically-allocated #GWeakRef.
4299 * This function also calls g_weak_ref_set() with @object on the
4300 * freshly-initialised weak reference.
4302 * This function should always be matched with a call to
4303 * g_weak_ref_clear(). It is not necessary to use this function for a
4304 * #GWeakRef in static storage because it will already be
4305 * properly initialised. Just use g_weak_ref_set() directly.
4307 * Since: 2.32
4309 void
4310 g_weak_ref_init (GWeakRef *weak_ref,
4311 gpointer object)
4313 weak_ref->priv.p = NULL;
4315 g_weak_ref_set (weak_ref, object);
4319 * g_weak_ref_clear: (skip)
4320 * @weak_ref: (inout): location of a weak reference, which
4321 * may be empty
4323 * Frees resources associated with a non-statically-allocated #GWeakRef.
4324 * After this call, the #GWeakRef is left in an undefined state.
4326 * You should only call this on a #GWeakRef that previously had
4327 * g_weak_ref_init() called on it.
4329 * Since: 2.32
4331 void
4332 g_weak_ref_clear (GWeakRef *weak_ref)
4334 g_weak_ref_set (weak_ref, NULL);
4336 /* be unkind */
4337 weak_ref->priv.p = (void *) 0xccccccccu;
4341 * g_weak_ref_get: (skip)
4342 * @weak_ref: (inout): location of a weak reference to a #GObject
4344 * If @weak_ref is not empty, atomically acquire a strong
4345 * reference to the object it points to, and return that reference.
4347 * This function is needed because of the potential race between taking
4348 * the pointer value and g_object_ref() on it, if the object was losing
4349 * its last reference at the same time in a different thread.
4351 * The caller should release the resulting reference in the usual way,
4352 * by using g_object_unref().
4354 * Returns: (transfer full) (type GObject.Object): the object pointed to
4355 * by @weak_ref, or %NULL if it was empty
4357 * Since: 2.32
4359 gpointer
4360 g_weak_ref_get (GWeakRef *weak_ref)
4362 gpointer object_or_null;
4364 g_return_val_if_fail (weak_ref!= NULL, NULL);
4366 g_rw_lock_reader_lock (&weak_locations_lock);
4368 object_or_null = weak_ref->priv.p;
4370 if (object_or_null != NULL)
4371 g_object_ref (object_or_null);
4373 g_rw_lock_reader_unlock (&weak_locations_lock);
4375 return object_or_null;
4379 * g_weak_ref_set: (skip)
4380 * @weak_ref: location for a weak reference
4381 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4383 * Change the object to which @weak_ref points, or set it to
4384 * %NULL.
4386 * You must own a strong reference on @object while calling this
4387 * function.
4389 * Since: 2.32
4391 void
4392 g_weak_ref_set (GWeakRef *weak_ref,
4393 gpointer object)
4395 GSList **weak_locations;
4396 GObject *new_object;
4397 GObject *old_object;
4399 g_return_if_fail (weak_ref != NULL);
4400 g_return_if_fail (object == NULL || G_IS_OBJECT (object));
4402 new_object = object;
4404 g_rw_lock_writer_lock (&weak_locations_lock);
4406 /* We use the extra level of indirection here so that if we have ever
4407 * had a weak pointer installed at any point in time on this object,
4408 * we can see that there is a non-NULL value associated with the
4409 * weak-pointer quark and know that this value will not change at any
4410 * point in the object's lifetime.
4412 * Both properties are important for reducing the amount of times we
4413 * need to acquire locks and for decreasing the duration of time the
4414 * lock is held while avoiding some rather tricky races.
4416 * Specifically: we can avoid having to do an extra unconditional lock
4417 * in g_object_unref() without worrying about some extremely tricky
4418 * races.
4421 old_object = weak_ref->priv.p;
4422 if (new_object != old_object)
4424 weak_ref->priv.p = new_object;
4426 /* Remove the weak ref from the old object */
4427 if (old_object != NULL)
4429 weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
4430 /* for it to point to an object, the object must have had it added once */
4431 g_assert (weak_locations != NULL);
4433 *weak_locations = g_slist_remove (*weak_locations, weak_ref);
4436 /* Add the weak ref to the new object */
4437 if (new_object != NULL)
4439 weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
4441 if (weak_locations == NULL)
4443 weak_locations = g_new0 (GSList *, 1);
4444 g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations, weak_locations, g_free);
4447 *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
4451 g_rw_lock_writer_unlock (&weak_locations_lock);