Coverage Report

Created: 2026-05-30 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/irssi/subprojects/glib-2.74.7/gobject/gobject.c
Line
Count
Source
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/*
21
 * MT safe with regards to reference counting.
22
 */
23
24
#include "config.h"
25
26
#include <string.h>
27
#include <signal.h>
28
29
#include "../glib/glib-private.h"
30
31
#include "gobject.h"
32
#include "gtype-private.h"
33
#include "gvaluecollector.h"
34
#include "gsignal.h"
35
#include "gparamspecs.h"
36
#include "gvaluetypes.h"
37
#include "gobject_trace.h"
38
#include "gconstructor.h"
39
40
/**
41
 * SECTION:objects
42
 * @title: GObject
43
 * @short_description: The base object type
44
 * @see_also: #GParamSpecObject, g_param_spec_object()
45
 *
46
 * GObject is the fundamental type providing the common attributes and
47
 * methods for all object types in GTK+, Pango and other libraries
48
 * based on GObject.  The GObject class provides methods for object
49
 * construction and destruction, property access methods, and signal
50
 * support.  Signals are described in detail [here][gobject-Signals].
51
 *
52
 * For a tutorial on implementing a new GObject class, see [How to define and
53
 * implement a new GObject][howto-gobject]. For a list of naming conventions for
54
 * GObjects and their methods, see the [GType conventions][gtype-conventions].
55
 * For the high-level concepts behind GObject, read [Instantiatable classed types:
56
 * Objects][gtype-instantiatable-classed].
57
 *
58
 * ## Floating references # {#floating-ref}
59
 *
60
 * **Note**: Floating references are a C convenience API and should not be
61
 * used in modern GObject code. Language bindings in particular find the
62
 * concept highly problematic, as floating references are not identifiable
63
 * through annotations, and neither are deviations from the floating reference
64
 * behavior, like types that inherit from #GInitiallyUnowned and still return
65
 * a full reference from g_object_new().
66
 *
67
 * GInitiallyUnowned is derived from GObject. The only difference between
68
 * the two is that the initial reference of a GInitiallyUnowned is flagged
69
 * as a "floating" reference. This means that it is not specifically
70
 * claimed to be "owned" by any code portion. The main motivation for
71
 * providing floating references is C convenience. In particular, it
72
 * allows code to be written as:
73
 * 
74
 * |[<!-- language="C" --> 
75
 * container = create_container ();
76
 * container_add_child (container, create_child());
77
 * ]|
78
 * 
79
 * If container_add_child() calls g_object_ref_sink() on the passed-in child,
80
 * no reference of the newly created child is leaked. Without floating
81
 * references, container_add_child() can only g_object_ref() the new child,
82
 * so to implement this code without reference leaks, it would have to be
83
 * written as:
84
 *
85
 * |[<!-- language="C" --> 
86
 * Child *child;
87
 * container = create_container ();
88
 * child = create_child ();
89
 * container_add_child (container, child);
90
 * g_object_unref (child);
91
 * ]|
92
 *
93
 * The floating reference can be converted into an ordinary reference by
94
 * calling g_object_ref_sink(). For already sunken objects (objects that
95
 * don't have a floating reference anymore), g_object_ref_sink() is equivalent
96
 * to g_object_ref() and returns a new reference.
97
 *
98
 * Since floating references are useful almost exclusively for C convenience,
99
 * language bindings that provide automated reference and memory ownership
100
 * maintenance (such as smart pointers or garbage collection) should not
101
 * expose floating references in their API. The best practice for handling
102
 * types that have initially floating references is to immediately sink those
103
 * references after g_object_new() returns, by checking if the #GType
104
 * inherits from #GInitiallyUnowned. For instance:
105
 *
106
 * |[<!-- language="C" -->
107
 * GObject *res = g_object_new_with_properties (gtype,
108
 *                                              n_props,
109
 *                                              prop_names,
110
 *                                              prop_values);
111
 *
112
 * // or: if (g_type_is_a (gtype, G_TYPE_INITIALLY_UNOWNED))
113
 * if (G_IS_INITIALLY_UNOWNED (res))
114
 *   g_object_ref_sink (res);
115
 *
116
 * return res;
117
 * ]|
118
 *
119
 * Some object implementations may need to save an objects floating state
120
 * across certain code portions (an example is #GtkMenu), to achieve this,
121
 * the following sequence can be used:
122
 *
123
 * |[<!-- language="C" --> 
124
 * // save floating state
125
 * gboolean was_floating = g_object_is_floating (object);
126
 * g_object_ref_sink (object);
127
 * // protected code portion
128
 *
129
 * ...
130
 *
131
 * // restore floating state
132
 * if (was_floating)
133
 *   g_object_force_floating (object);
134
 * else
135
 *   g_object_unref (object); // release previously acquired reference
136
 * ]|
137
 */
138
139
140
/* --- macros --- */
141
0
#define PARAM_SPEC_PARAM_ID(pspec)    ((pspec)->param_id)
142
0
#define PARAM_SPEC_SET_PARAM_ID(pspec, id)  ((pspec)->param_id = (id))
143
144
0
#define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
145
#define OBJECT_HAS_TOGGLE_REF(object) \
146
0
    ((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
147
0
#define OBJECT_FLOATING_FLAG 0x2
148
149
0
#define CLASS_HAS_PROPS_FLAG 0x1
150
#define CLASS_HAS_PROPS(class) \
151
0
    ((class)->flags & CLASS_HAS_PROPS_FLAG)
152
#define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
153
    ((class)->constructor != g_object_constructor)
154
#define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
155
0
    ((class)->constructed != g_object_constructed)
156
0
#define CLASS_HAS_NOTIFY(class) ((class)->notify != NULL)
157
#define CLASS_HAS_CUSTOM_DISPATCH(class) \
158
0
    ((class)->dispatch_properties_changed != g_object_dispatch_properties_changed)
159
#define CLASS_NEEDS_NOTIFY(class) \
160
0
    (CLASS_HAS_NOTIFY(class) || CLASS_HAS_CUSTOM_DISPATCH(class))
161
162
0
#define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
163
#define CLASS_HAS_DERIVED_CLASS(class) \
164
0
    ((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
165
166
/* --- signals --- */
167
enum {
168
  NOTIFY,
169
  LAST_SIGNAL
170
};
171
172
173
/* --- properties --- */
174
enum {
175
  PROP_NONE
176
};
177
178
0
#define OPTIONAL_FLAG_IN_CONSTRUCTION    (1 << 0)
179
0
#define OPTIONAL_FLAG_HAS_SIGNAL_HANDLER (1 << 1) /* Set if object ever had a signal handler */
180
0
#define OPTIONAL_FLAG_HAS_NOTIFY_HANDLER (1 << 2) /* Same, specifically for "notify" */
181
182
#if SIZEOF_INT == 4 && GLIB_SIZEOF_VOID_P == 8
183
#define HAVE_OPTIONAL_FLAGS
184
#endif
185
186
typedef struct
187
{
188
  GTypeInstance  g_type_instance;
189
190
  /*< private >*/
191
  guint          ref_count;  /* (atomic) */
192
#ifdef HAVE_OPTIONAL_FLAGS
193
  guint          optional_flags;  /* (atomic) */
194
#endif
195
  GData         *qdata;
196
} GObjectReal;
197
198
G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal));
199
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count));
200
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, qdata) == G_STRUCT_OFFSET(GObjectReal, qdata));
201
202
203
/* --- prototypes --- */
204
static void g_object_base_class_init    (GObjectClass *class);
205
static void g_object_base_class_finalize    (GObjectClass *class);
206
static void g_object_do_class_init      (GObjectClass *class);
207
static void g_object_init       (GObject  *object,
208
               GObjectClass *class);
209
static GObject* g_object_constructor      (GType                  type,
210
               guint                  n_construct_properties,
211
               GObjectConstructParam *construct_params);
212
static void     g_object_constructed                    (GObject        *object);
213
static void g_object_real_dispose     (GObject  *object);
214
static void g_object_finalize     (GObject  *object);
215
static void g_object_do_set_property    (GObject        *object,
216
               guint           property_id,
217
               const GValue   *value,
218
               GParamSpec     *pspec);
219
static void g_object_do_get_property    (GObject        *object,
220
               guint           property_id,
221
               GValue         *value,
222
               GParamSpec     *pspec);
223
static void g_value_object_init     (GValue   *value);
224
static void g_value_object_free_value   (GValue   *value);
225
static void g_value_object_copy_value   (const GValue *src_value,
226
               GValue   *dest_value);
227
static void g_value_object_transform_value    (const GValue *src_value,
228
               GValue   *dest_value);
229
static gpointer g_value_object_peek_pointer             (const GValue   *value);
230
static gchar* g_value_object_collect_value    (GValue   *value,
231
               guint           n_collect_values,
232
               GTypeCValue    *collect_values,
233
               guint           collect_flags);
234
static gchar* g_value_object_lcopy_value    (const GValue *value,
235
               guint           n_collect_values,
236
               GTypeCValue    *collect_values,
237
               guint           collect_flags);
238
static void g_object_dispatch_properties_changed  (GObject  *object,
239
               guint     n_pspecs,
240
               GParamSpec    **pspecs);
241
static guint               object_floating_flag_handler (GObject        *object,
242
                                                         gint            job);
243
244
static void object_interface_check_properties           (gpointer        check_data,
245
               gpointer        g_iface);
246
static void                weak_locations_free_unlocked (GSList **weak_locations);
247
248
/* --- typedefs --- */
249
typedef struct _GObjectNotifyQueue            GObjectNotifyQueue;
250
251
struct _GObjectNotifyQueue
252
{
253
  GSList  *pspecs;
254
  guint16  n_pspecs;
255
  guint16  freeze_count;
256
};
257
258
/* --- variables --- */
259
G_LOCK_DEFINE_STATIC (closure_array_mutex);
260
G_LOCK_DEFINE_STATIC (weak_refs_mutex);
261
G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
262
static GQuark             quark_closure_array = 0;
263
static GQuark             quark_weak_refs = 0;
264
static GQuark             quark_toggle_refs = 0;
265
static GQuark               quark_notify_queue;
266
#ifndef HAVE_OPTIONAL_FLAGS
267
static GQuark               quark_in_construction;
268
#endif
269
static GParamSpecPool      *pspec_pool = NULL;
270
static gulong             gobject_signals[LAST_SIGNAL] = { 0, };
271
static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
272
/* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
273
static GQuark             quark_weak_locations = 0;
274
static GRWLock              weak_locations_lock;
275
276
G_LOCK_DEFINE_STATIC(notify_lock);
277
278
/* --- functions --- */
279
static void
280
g_object_notify_queue_free (gpointer data)
281
0
{
282
0
  GObjectNotifyQueue *nqueue = data;
283
284
0
  g_slist_free (nqueue->pspecs);
285
0
  g_slice_free (GObjectNotifyQueue, nqueue);
286
0
}
287
288
static GObjectNotifyQueue*
289
g_object_notify_queue_freeze (GObject  *object,
290
                              gboolean  conditional)
291
0
{
292
0
  GObjectNotifyQueue *nqueue;
293
294
0
  G_LOCK(notify_lock);
295
0
  nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
296
0
  if (!nqueue)
297
0
    {
298
0
      if (conditional)
299
0
        {
300
0
          G_UNLOCK(notify_lock);
301
0
          return NULL;
302
0
        }
303
304
0
      nqueue = g_slice_new0 (GObjectNotifyQueue);
305
0
      g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
306
0
                                   nqueue, g_object_notify_queue_free);
307
0
    }
308
309
0
  if (nqueue->freeze_count >= 65535)
310
0
    g_critical("Free queue for %s (%p) is larger than 65535,"
311
0
               " called g_object_freeze_notify() too often."
312
0
               " Forgot to call g_object_thaw_notify() or infinite loop",
313
0
               G_OBJECT_TYPE_NAME (object), object);
314
0
  else
315
0
    nqueue->freeze_count++;
316
317
0
  G_UNLOCK(notify_lock);
318
319
0
  return nqueue;
320
0
}
321
322
static void
323
g_object_notify_queue_thaw (GObject            *object,
324
                            GObjectNotifyQueue *nqueue)
325
0
{
326
0
  GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
327
0
  GSList *slist;
328
0
  guint n_pspecs = 0;
329
330
0
  G_LOCK(notify_lock);
331
332
  /* Just make sure we never get into some nasty race condition */
333
0
  if (G_UNLIKELY (nqueue->freeze_count == 0))
334
0
    {
335
0
      G_UNLOCK (notify_lock);
336
0
      g_warning ("%s: property-changed notification for %s(%p) is not frozen",
337
0
                 G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
338
0
      return;
339
0
    }
340
341
0
  nqueue->freeze_count--;
342
0
  if (nqueue->freeze_count)
343
0
    {
344
0
      G_UNLOCK (notify_lock);
345
0
      return;
346
0
    }
347
348
0
  pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
349
350
0
  for (slist = nqueue->pspecs; slist; slist = slist->next)
351
0
    {
352
0
      pspecs[n_pspecs++] = slist->data;
353
0
    }
354
0
  g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
355
356
0
  G_UNLOCK(notify_lock);
357
358
0
  if (n_pspecs)
359
0
    G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
360
0
  g_free (free_me);
361
0
}
362
363
static void
364
g_object_notify_queue_add (GObject            *object,
365
                           GObjectNotifyQueue *nqueue,
366
                           GParamSpec         *pspec)
367
0
{
368
0
  G_LOCK(notify_lock);
369
370
0
  g_assert (nqueue->n_pspecs < 65535);
371
372
0
  if (g_slist_find (nqueue->pspecs, pspec) == NULL)
373
0
    {
374
0
      nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
375
0
      nqueue->n_pspecs++;
376
0
    }
377
378
0
  G_UNLOCK(notify_lock);
379
0
}
380
381
#ifdef  G_ENABLE_DEBUG
382
G_LOCK_DEFINE_STATIC     (debug_objects);
383
static guint     debug_objects_count = 0;
384
static GHashTable *debug_objects_ht = NULL;
385
386
static void
387
debug_objects_foreach (gpointer key,
388
           gpointer value,
389
           gpointer user_data)
390
0
{
391
0
  GObject *object = value;
392
393
0
  g_message ("[%p] stale %s\tref_count=%u",
394
0
       object,
395
0
       G_OBJECT_TYPE_NAME (object),
396
0
       object->ref_count);
397
0
}
398
399
#ifdef G_HAS_CONSTRUCTORS
400
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
401
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
402
#endif
403
G_DEFINE_DESTRUCTOR(debug_objects_atexit)
404
#endif /* G_HAS_CONSTRUCTORS */
405
406
static void
407
debug_objects_atexit (void)
408
0
{
409
0
  GOBJECT_IF_DEBUG (OBJECTS,
410
0
    {
411
0
      G_LOCK (debug_objects);
412
0
      g_message ("stale GObjects: %u", debug_objects_count);
413
0
      g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
414
0
      G_UNLOCK (debug_objects);
415
0
    });
416
0
}
417
#endif  /* G_ENABLE_DEBUG */
418
419
void
420
_g_object_type_init (void)
421
8
{
422
8
  static gboolean initialized = FALSE;
423
8
  static const GTypeFundamentalInfo finfo = {
424
8
    G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
425
8
  };
426
8
  GTypeInfo info = {
427
8
    sizeof (GObjectClass),
428
8
    (GBaseInitFunc) g_object_base_class_init,
429
8
    (GBaseFinalizeFunc) g_object_base_class_finalize,
430
8
    (GClassInitFunc) g_object_do_class_init,
431
8
    NULL  /* class_destroy */,
432
8
    NULL  /* class_data */,
433
8
    sizeof (GObject),
434
8
    0   /* n_preallocs */,
435
8
    (GInstanceInitFunc) g_object_init,
436
8
    NULL, /* value_table */
437
8
  };
438
8
  static const GTypeValueTable value_table = {
439
8
    g_value_object_init,    /* value_init */
440
8
    g_value_object_free_value,    /* value_free */
441
8
    g_value_object_copy_value,    /* value_copy */
442
8
    g_value_object_peek_pointer,  /* value_peek_pointer */
443
8
    "p",        /* collect_format */
444
8
    g_value_object_collect_value, /* collect_value */
445
8
    "p",        /* lcopy_format */
446
8
    g_value_object_lcopy_value,   /* lcopy_value */
447
8
  };
448
8
  GType type G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
449
  
450
8
  g_return_if_fail (initialized == FALSE);
451
8
  initialized = TRUE;
452
  
453
  /* G_TYPE_OBJECT
454
   */
455
8
  info.value_table = &value_table;
456
8
  type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
457
8
  g_assert (type == G_TYPE_OBJECT);
458
8
  g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
459
460
8
#if G_ENABLE_DEBUG
461
  /* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS
462
   * conditional in between, as the C spec leaves conditionals inside macro
463
   * expansions as undefined behavior. Only GCC and Clang are known to work
464
   * but compilation breaks on MSVC.
465
   *
466
   * See: https://bugzilla.gnome.org/show_bug.cgi?id=769504
467
   */
468
8
  if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \
469
0
    {
470
0
      debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
471
# ifndef G_HAS_CONSTRUCTORS
472
      g_atexit (debug_objects_atexit);
473
# endif /* G_HAS_CONSTRUCTORS */
474
0
    }
475
8
#endif /* G_ENABLE_DEBUG */
476
8
}
477
478
static void
479
g_object_base_class_init (GObjectClass *class)
480
0
{
481
0
  GObjectClass *pclass = g_type_class_peek_parent (class);
482
483
  /* Don't inherit HAS_DERIVED_CLASS flag from parent class */
484
0
  class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
485
486
0
  if (pclass)
487
0
    pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
488
489
  /* reset instance specific fields and methods that don't get inherited */
490
0
  class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
491
0
  class->n_construct_properties = g_slist_length (class->construct_properties);
492
0
  class->get_property = NULL;
493
0
  class->set_property = NULL;
494
0
  class->pspecs = NULL;
495
0
  class->n_pspecs = 0;
496
0
}
497
498
static void
499
g_object_base_class_finalize (GObjectClass *class)
500
0
{
501
0
  GList *list, *node;
502
  
503
0
  _g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
504
505
0
  g_slist_free (class->construct_properties);
506
0
  class->construct_properties = NULL;
507
0
  class->n_construct_properties = 0;
508
0
  list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
509
0
  for (node = list; node; node = node->next)
510
0
    {
511
0
      GParamSpec *pspec = node->data;
512
      
513
0
      g_param_spec_pool_remove (pspec_pool, pspec);
514
0
      PARAM_SPEC_SET_PARAM_ID (pspec, 0);
515
0
      g_param_spec_unref (pspec);
516
0
    }
517
0
  g_list_free (list);
518
0
}
519
520
static void
521
g_object_do_class_init (GObjectClass *class)
522
0
{
523
  /* read the comment about typedef struct CArray; on why not to change this quark */
524
0
  quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
525
526
0
  quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
527
0
  quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
528
0
  quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
529
0
  quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
530
#ifndef HAVE_OPTIONAL_FLAGS
531
  quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
532
#endif
533
0
  pspec_pool = g_param_spec_pool_new (TRUE);
534
535
0
  class->constructor = g_object_constructor;
536
0
  class->constructed = g_object_constructed;
537
0
  class->set_property = g_object_do_set_property;
538
0
  class->get_property = g_object_do_get_property;
539
0
  class->dispose = g_object_real_dispose;
540
0
  class->finalize = g_object_finalize;
541
0
  class->dispatch_properties_changed = g_object_dispatch_properties_changed;
542
0
  class->notify = NULL;
543
544
  /**
545
   * GObject::notify:
546
   * @gobject: the object which received the signal.
547
   * @pspec: the #GParamSpec of the property which changed.
548
   *
549
   * The notify signal is emitted on an object when one of its properties has
550
   * its value set through g_object_set_property(), g_object_set(), et al.
551
   *
552
   * Note that getting this signal doesn’t itself guarantee that the value of
553
   * the property has actually changed. When it is emitted is determined by the
554
   * derived GObject class. If the implementor did not create the property with
555
   * %G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results
556
   * in ::notify being emitted, even if the new value is the same as the old.
557
   * If they did pass %G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only
558
   * when they explicitly call g_object_notify() or g_object_notify_by_pspec(),
559
   * and common practice is to do that only when the value has actually changed.
560
   *
561
   * This signal is typically used to obtain change notification for a
562
   * single property, by specifying the property name as a detail in the
563
   * g_signal_connect() call, like this:
564
   *
565
   * |[<!-- language="C" --> 
566
   * g_signal_connect (text_view->buffer, "notify::paste-target-list",
567
   *                   G_CALLBACK (gtk_text_view_target_list_notify),
568
   *                   text_view)
569
   * ]|
570
   *
571
   * It is important to note that you must use
572
   * [canonical parameter names][canonical-parameter-names] as
573
   * detail strings for the notify signal.
574
   */
575
0
  gobject_signals[NOTIFY] =
576
0
    g_signal_new (g_intern_static_string ("notify"),
577
0
      G_TYPE_FROM_CLASS (class),
578
0
      G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
579
0
      G_STRUCT_OFFSET (GObjectClass, notify),
580
0
      NULL, NULL,
581
0
      NULL,
582
0
      G_TYPE_NONE,
583
0
      1, G_TYPE_PARAM);
584
585
  /* Install a check function that we'll use to verify that classes that
586
   * implement an interface implement all properties for that interface
587
   */
588
0
  g_type_add_interface_check (NULL, object_interface_check_properties);
589
0
}
590
591
/* Sinks @pspec if it’s a floating ref. */
592
static inline gboolean
593
install_property_internal (GType       g_type,
594
         guint       property_id,
595
         GParamSpec *pspec)
596
0
{
597
0
  g_param_spec_ref_sink (pspec);
598
599
0
  if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
600
0
    {
601
0
      g_warning ("When installing property: type '%s' already has a property named '%s'",
602
0
     g_type_name (g_type),
603
0
     pspec->name);
604
0
      g_param_spec_unref (pspec);
605
0
      return FALSE;
606
0
    }
607
608
0
  PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
609
0
  g_param_spec_pool_insert (pspec_pool, g_steal_pointer (&pspec), g_type);
610
0
  return TRUE;
611
0
}
612
613
static gboolean
614
validate_pspec_to_install (GParamSpec *pspec)
615
0
{
616
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
617
0
  g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */
618
619
0
  g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE);
620
621
0
  if (pspec->flags & G_PARAM_CONSTRUCT)
622
0
    g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE);
623
624
0
  if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
625
0
    g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE);
626
627
0
  return TRUE;
628
0
}
629
630
/* Sinks @pspec if it’s a floating ref. */
631
static gboolean
632
validate_and_install_class_property (GObjectClass *class,
633
                                     GType         oclass_type,
634
                                     GType         parent_type,
635
                                     guint         property_id,
636
                                     GParamSpec   *pspec)
637
0
{
638
0
  if (!validate_pspec_to_install (pspec))
639
0
    {
640
0
      g_param_spec_ref_sink (pspec);
641
0
      g_param_spec_unref (pspec);
642
0
      return FALSE;
643
0
    }
644
645
0
  if (pspec->flags & G_PARAM_WRITABLE)
646
0
    g_return_val_if_fail (class->set_property != NULL, FALSE);
647
0
  if (pspec->flags & G_PARAM_READABLE)
648
0
    g_return_val_if_fail (class->get_property != NULL, FALSE);
649
650
0
  class->flags |= CLASS_HAS_PROPS_FLAG;
651
0
  if (install_property_internal (oclass_type, property_id, pspec))
652
0
    {
653
0
      if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
654
0
        {
655
0
          class->construct_properties = g_slist_append (class->construct_properties, pspec);
656
0
          class->n_construct_properties += 1;
657
0
        }
658
659
      /* for property overrides of construct properties, we have to get rid
660
       * of the overridden inherited construct property
661
       */
662
0
      pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
663
0
      if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
664
0
        {
665
0
          class->construct_properties = g_slist_remove (class->construct_properties, pspec);
666
0
          class->n_construct_properties -= 1;
667
0
        }
668
669
0
      return TRUE;
670
0
    }
671
0
  else
672
0
    return FALSE;
673
0
}
674
675
/**
676
 * g_object_class_install_property:
677
 * @oclass: a #GObjectClass
678
 * @property_id: the id for the new property
679
 * @pspec: the #GParamSpec for the new property
680
 *
681
 * Installs a new property.
682
 *
683
 * All properties should be installed during the class initializer.  It
684
 * is possible to install properties after that, but doing so is not
685
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
686
 * use of properties on the same type on other threads.
687
 *
688
 * Note that it is possible to redefine a property in a derived class,
689
 * by installing a property with the same name. This can be useful at times,
690
 * e.g. to change the range of allowed values or the default value.
691
 */
692
void
693
g_object_class_install_property (GObjectClass *class,
694
         guint         property_id,
695
         GParamSpec   *pspec)
696
0
{
697
0
  GType oclass_type, parent_type;
698
699
0
  g_return_if_fail (G_IS_OBJECT_CLASS (class));
700
0
  g_return_if_fail (property_id > 0);
701
702
0
  oclass_type = G_OBJECT_CLASS_TYPE (class);
703
0
  parent_type = g_type_parent (oclass_type);
704
705
0
  if (CLASS_HAS_DERIVED_CLASS (class))
706
0
    g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
707
708
0
  (void) validate_and_install_class_property (class,
709
0
                                              oclass_type,
710
0
                                              parent_type,
711
0
                                              property_id,
712
0
                                              pspec);
713
0
}
714
715
typedef struct {
716
  const char *name;
717
  GParamSpec *pspec;
718
} PspecEntry;
719
720
static int
721
compare_pspec_entry (const void *a,
722
                     const void *b)
723
0
{
724
0
  const PspecEntry *ae = a;
725
0
  const PspecEntry *be = b;
726
727
0
  return ae->name < be->name ? -1 : (ae->name > be->name ? 1 : 0);
728
0
}
729
730
/* This uses pointer comparisons with @property_name, so
731
 * will only work with string literals. */
732
static inline GParamSpec *
733
find_pspec (GObjectClass *class,
734
            const char   *property_name)
735
0
{
736
0
  const PspecEntry *pspecs = (const PspecEntry *)class->pspecs;
737
0
  gsize n_pspecs = class->n_pspecs;
738
739
0
  g_assert (n_pspecs <= G_MAXSSIZE);
740
741
  /* The limit for choosing between linear and binary search is
742
   * fairly arbitrary.
743
   *
744
   * Both searches use pointer comparisons against @property_name.
745
   * If this function is called with a non-static @property_name,
746
   * it will fall through to the g_param_spec_pool_lookup() case.
747
   * That’s OK; this is an opportunistic optimisation which relies
748
   * on the fact that *most* (but not all) property lookups use
749
   * static property names.
750
   */
751
0
  if (n_pspecs < 10)
752
0
    {
753
0
      for (gsize i = 0; i < n_pspecs; i++)
754
0
        {
755
0
          if (pspecs[i].name == property_name)
756
0
            return pspecs[i].pspec;
757
0
        }
758
0
    }
759
0
  else
760
0
    {
761
0
      gssize lower = 0;
762
0
      gssize upper = (int)class->n_pspecs - 1;
763
0
      gssize mid;
764
765
0
      while (lower <= upper)
766
0
        {
767
0
          mid = (lower + upper) / 2;
768
769
0
          if (property_name < pspecs[mid].name)
770
0
            upper = mid - 1;
771
0
          else if (property_name > pspecs[mid].name)
772
0
            lower = mid + 1;
773
0
          else
774
0
            return pspecs[mid].pspec;
775
0
        }
776
0
    }
777
778
0
  return g_param_spec_pool_lookup (pspec_pool,
779
0
                                   property_name,
780
0
                                   ((GTypeClass *)class)->g_type,
781
0
                                   TRUE);
782
0
}
783
784
/**
785
 * g_object_class_install_properties:
786
 * @oclass: a #GObjectClass
787
 * @n_pspecs: the length of the #GParamSpecs array
788
 * @pspecs: (array length=n_pspecs): the #GParamSpecs array
789
 *   defining the new properties
790
 *
791
 * Installs new properties from an array of #GParamSpecs.
792
 *
793
 * All properties should be installed during the class initializer.  It
794
 * is possible to install properties after that, but doing so is not
795
 * recommend, and specifically, is not guaranteed to be thread-safe vs.
796
 * use of properties on the same type on other threads.
797
 *
798
 * The property id of each property is the index of each #GParamSpec in
799
 * the @pspecs array.
800
 *
801
 * The property id of 0 is treated specially by #GObject and it should not
802
 * be used to store a #GParamSpec.
803
 *
804
 * This function should be used if you plan to use a static array of
805
 * #GParamSpecs and g_object_notify_by_pspec(). For instance, this
806
 * class initialization:
807
 *
808
 * |[<!-- language="C" --> 
809
 * typedef enum {
810
 *   PROP_FOO = 1,
811
 *   PROP_BAR,
812
 *   N_PROPERTIES
813
 * } MyObjectProperty;
814
 *
815
 * static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
816
 *
817
 * static void
818
 * my_object_class_init (MyObjectClass *klass)
819
 * {
820
 *   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
821
 *
822
 *   obj_properties[PROP_FOO] =
823
 *     g_param_spec_int ("foo", "Foo", "Foo",
824
 *                       -1, G_MAXINT,
825
 *                       0,
826
 *                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
827
 *
828
 *   obj_properties[PROP_BAR] =
829
 *     g_param_spec_string ("bar", "Bar", "Bar",
830
 *                          NULL,
831
 *                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
832
 *
833
 *   gobject_class->set_property = my_object_set_property;
834
 *   gobject_class->get_property = my_object_get_property;
835
 *   g_object_class_install_properties (gobject_class,
836
 *                                      G_N_ELEMENTS (obj_properties),
837
 *                                      obj_properties);
838
 * }
839
 * ]|
840
 *
841
 * allows calling g_object_notify_by_pspec() to notify of property changes:
842
 *
843
 * |[<!-- language="C" --> 
844
 * void
845
 * my_object_set_foo (MyObject *self, gint foo)
846
 * {
847
 *   if (self->foo != foo)
848
 *     {
849
 *       self->foo = foo;
850
 *       g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
851
 *     }
852
 *  }
853
 * ]|
854
 *
855
 * Since: 2.26
856
 */
857
void
858
g_object_class_install_properties (GObjectClass  *oclass,
859
                                   guint          n_pspecs,
860
                                   GParamSpec   **pspecs)
861
0
{
862
0
  GType oclass_type, parent_type;
863
0
  guint i;
864
865
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
866
0
  g_return_if_fail (n_pspecs > 1);
867
0
  g_return_if_fail (pspecs[0] == NULL);
868
869
0
  if (CLASS_HAS_DERIVED_CLASS (oclass))
870
0
    g_error ("Attempt to add properties to %s after it was derived",
871
0
             G_OBJECT_CLASS_NAME (oclass));
872
873
0
  oclass_type = G_OBJECT_CLASS_TYPE (oclass);
874
0
  parent_type = g_type_parent (oclass_type);
875
876
  /* we skip the first element of the array as it would have a 0 prop_id */
877
0
  for (i = 1; i < n_pspecs; i++)
878
0
    {
879
0
      GParamSpec *pspec = pspecs[i];
880
881
0
      if (!validate_and_install_class_property (oclass,
882
0
                                                oclass_type,
883
0
                                                parent_type,
884
0
                                                i,
885
0
                                                pspec))
886
0
        {
887
0
          break;
888
0
        }
889
0
    }
890
891
  /* Save a copy of the pspec array inside the class struct. This
892
   * makes it faster to look up pspecs for the class in future when
893
   * acting on those properties.
894
   *
895
   * If a pspec is not in this cache array, calling code will fall
896
   * back to using g_param_spec_pool_lookup(), so a pspec not being
897
   * in this array is a (potential) performance problem but not a
898
   * correctness problem. */
899
0
  if (oclass->pspecs == NULL)
900
0
    {
901
0
      PspecEntry *entries;
902
903
0
      entries = g_new (PspecEntry, n_pspecs - 1);
904
905
0
      for (i = 1; i < n_pspecs; i++)
906
0
        {
907
0
          entries[i - 1].name = pspecs[i]->name;
908
0
          entries[i - 1].pspec = pspecs[i];
909
0
        }
910
911
0
      qsort (entries, n_pspecs - 1, sizeof (PspecEntry), compare_pspec_entry);
912
913
0
      oclass->pspecs = entries;
914
0
      oclass->n_pspecs = n_pspecs - 1;
915
0
    }
916
0
}
917
918
/**
919
 * g_object_interface_install_property:
920
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
921
 *    interface, or the default
922
 *  vtable for the interface.
923
 * @pspec: the #GParamSpec for the new property
924
 *
925
 * Add a property to an interface; this is only useful for interfaces
926
 * that are added to GObject-derived types. Adding a property to an
927
 * interface forces all objects classes with that interface to have a
928
 * compatible property. The compatible property could be a newly
929
 * created #GParamSpec, but normally
930
 * g_object_class_override_property() will be used so that the object
931
 * class only needs to provide an implementation and inherits the
932
 * property description, default value, bounds, and so forth from the
933
 * interface property.
934
 *
935
 * This function is meant to be called from the interface's default
936
 * vtable initialization function (the @class_init member of
937
 * #GTypeInfo.) It must not be called after after @class_init has
938
 * been called for any object types implementing this interface.
939
 *
940
 * If @pspec is a floating reference, it will be consumed.
941
 *
942
 * Since: 2.4
943
 */
944
void
945
g_object_interface_install_property (gpointer      g_iface,
946
             GParamSpec   *pspec)
947
0
{
948
0
  GTypeInterface *iface_class = g_iface;
949
  
950
0
  g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
951
0
  g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
952
953
0
  if (!validate_pspec_to_install (pspec))
954
0
    {
955
0
      g_param_spec_ref_sink (pspec);
956
0
      g_param_spec_unref (pspec);
957
0
      return;
958
0
    }
959
960
0
  (void) install_property_internal (iface_class->g_type, 0, pspec);
961
0
}
962
963
/* Inlined version of g_param_spec_get_redirect_target(), for speed */
964
static inline void
965
param_spec_follow_override (GParamSpec **pspec)
966
0
{
967
0
  if (((GTypeInstance *) (*pspec))->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
968
0
    *pspec = ((GParamSpecOverride *) (*pspec))->overridden;
969
0
}
970
971
/**
972
 * g_object_class_find_property:
973
 * @oclass: a #GObjectClass
974
 * @property_name: the name of the property to look up
975
 *
976
 * Looks up the #GParamSpec for a property of a class.
977
 *
978
 * Returns: (transfer none): the #GParamSpec for the property, or
979
 *          %NULL if the class doesn't have a property of that name
980
 */
981
GParamSpec*
982
g_object_class_find_property (GObjectClass *class,
983
            const gchar  *property_name)
984
0
{
985
0
  GParamSpec *pspec;
986
987
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
988
0
  g_return_val_if_fail (property_name != NULL, NULL);
989
990
0
  pspec = find_pspec (class, property_name);
991
992
0
  if (pspec)
993
0
    param_spec_follow_override (&pspec);
994
995
0
  return pspec;
996
0
}
997
998
/**
999
 * g_object_interface_find_property:
1000
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
1001
 *  interface, or the default vtable for the interface
1002
 * @property_name: name of a property to look up.
1003
 *
1004
 * Find the #GParamSpec with the given name for an
1005
 * interface. Generally, the interface vtable passed in as @g_iface
1006
 * will be the default vtable from g_type_default_interface_ref(), or,
1007
 * if you know the interface has already been loaded,
1008
 * g_type_default_interface_peek().
1009
 *
1010
 * Since: 2.4
1011
 *
1012
 * Returns: (transfer none): the #GParamSpec for the property of the
1013
 *          interface with the name @property_name, or %NULL if no
1014
 *          such property exists.
1015
 */
1016
GParamSpec*
1017
g_object_interface_find_property (gpointer      g_iface,
1018
          const gchar  *property_name)
1019
0
{
1020
0
  GTypeInterface *iface_class = g_iface;
1021
  
1022
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
1023
0
  g_return_val_if_fail (property_name != NULL, NULL);
1024
  
1025
0
  return g_param_spec_pool_lookup (pspec_pool,
1026
0
           property_name,
1027
0
           iface_class->g_type,
1028
0
           FALSE);
1029
0
}
1030
1031
/**
1032
 * g_object_class_override_property:
1033
 * @oclass: a #GObjectClass
1034
 * @property_id: the new property ID
1035
 * @name: the name of a property registered in a parent class or
1036
 *  in an interface of this class.
1037
 *
1038
 * Registers @property_id as referring to a property with the name
1039
 * @name in a parent class or in an interface implemented by @oclass.
1040
 * This allows this class to "override" a property implementation in
1041
 * a parent class or to provide the implementation of a property from
1042
 * an interface.
1043
 *
1044
 * Internally, overriding is implemented by creating a property of type
1045
 * #GParamSpecOverride; generally operations that query the properties of
1046
 * the object class, such as g_object_class_find_property() or
1047
 * g_object_class_list_properties() will return the overridden
1048
 * property. However, in one case, the @construct_properties argument of
1049
 * the @constructor virtual function, the #GParamSpecOverride is passed
1050
 * instead, so that the @param_id field of the #GParamSpec will be
1051
 * correct.  For virtually all uses, this makes no difference. If you
1052
 * need to get the overridden property, you can call
1053
 * g_param_spec_get_redirect_target().
1054
 *
1055
 * Since: 2.4
1056
 */
1057
void
1058
g_object_class_override_property (GObjectClass *oclass,
1059
          guint         property_id,
1060
          const gchar  *name)
1061
0
{
1062
0
  GParamSpec *overridden = NULL;
1063
0
  GParamSpec *new;
1064
0
  GType parent_type;
1065
  
1066
0
  g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
1067
0
  g_return_if_fail (property_id > 0);
1068
0
  g_return_if_fail (name != NULL);
1069
1070
  /* Find the overridden property; first check parent types
1071
   */
1072
0
  parent_type = g_type_parent (G_OBJECT_CLASS_TYPE (oclass));
1073
0
  if (parent_type != G_TYPE_NONE)
1074
0
    overridden = g_param_spec_pool_lookup (pspec_pool,
1075
0
             name,
1076
0
             parent_type,
1077
0
             TRUE);
1078
0
  if (!overridden)
1079
0
    {
1080
0
      GType *ifaces;
1081
0
      guint n_ifaces;
1082
      
1083
      /* Now check interfaces
1084
       */
1085
0
      ifaces = g_type_interfaces (G_OBJECT_CLASS_TYPE (oclass), &n_ifaces);
1086
0
      while (n_ifaces-- && !overridden)
1087
0
  {
1088
0
    overridden = g_param_spec_pool_lookup (pspec_pool,
1089
0
             name,
1090
0
             ifaces[n_ifaces],
1091
0
             FALSE);
1092
0
  }
1093
      
1094
0
      g_free (ifaces);
1095
0
    }
1096
1097
0
  if (!overridden)
1098
0
    {
1099
0
      g_warning ("%s: Can't find property to override for '%s::%s'",
1100
0
     G_STRFUNC, G_OBJECT_CLASS_NAME (oclass), name);
1101
0
      return;
1102
0
    }
1103
1104
0
  new = g_param_spec_override (name, overridden);
1105
0
  g_object_class_install_property (oclass, property_id, new);
1106
0
}
1107
1108
/**
1109
 * g_object_class_list_properties:
1110
 * @oclass: a #GObjectClass
1111
 * @n_properties: (out): return location for the length of the returned array
1112
 *
1113
 * Get an array of #GParamSpec* for all properties of a class.
1114
 *
1115
 * Returns: (array length=n_properties) (transfer container): an array of
1116
 *          #GParamSpec* which should be freed after use
1117
 */
1118
GParamSpec** /* free result */
1119
g_object_class_list_properties (GObjectClass *class,
1120
        guint        *n_properties_p)
1121
0
{
1122
0
  GParamSpec **pspecs;
1123
0
  guint n;
1124
1125
0
  g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
1126
1127
0
  pspecs = g_param_spec_pool_list (pspec_pool,
1128
0
           G_OBJECT_CLASS_TYPE (class),
1129
0
           &n);
1130
0
  if (n_properties_p)
1131
0
    *n_properties_p = n;
1132
1133
0
  return pspecs;
1134
0
}
1135
1136
/**
1137
 * g_object_interface_list_properties:
1138
 * @g_iface: (type GObject.TypeInterface): any interface vtable for the
1139
 *  interface, or the default vtable for the interface
1140
 * @n_properties_p: (out): location to store number of properties returned.
1141
 *
1142
 * Lists the properties of an interface.Generally, the interface
1143
 * vtable passed in as @g_iface will be the default vtable from
1144
 * g_type_default_interface_ref(), or, if you know the interface has
1145
 * already been loaded, g_type_default_interface_peek().
1146
 *
1147
 * Since: 2.4
1148
 *
1149
 * Returns: (array length=n_properties_p) (transfer container): a
1150
 *          pointer to an array of pointers to #GParamSpec
1151
 *          structures. The paramspecs are owned by GLib, but the
1152
 *          array should be freed with g_free() when you are done with
1153
 *          it.
1154
 */
1155
GParamSpec**
1156
g_object_interface_list_properties (gpointer      g_iface,
1157
            guint        *n_properties_p)
1158
0
{
1159
0
  GTypeInterface *iface_class = g_iface;
1160
0
  GParamSpec **pspecs;
1161
0
  guint n;
1162
1163
0
  g_return_val_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type), NULL);
1164
1165
0
  pspecs = g_param_spec_pool_list (pspec_pool,
1166
0
           iface_class->g_type,
1167
0
           &n);
1168
0
  if (n_properties_p)
1169
0
    *n_properties_p = n;
1170
1171
0
  return pspecs;
1172
0
}
1173
1174
static inline guint
1175
object_get_optional_flags (GObject *object)
1176
0
{
1177
0
#ifdef HAVE_OPTIONAL_FLAGS
1178
0
  GObjectReal *real = (GObjectReal *)object;
1179
0
  return (guint)g_atomic_int_get (&real->optional_flags);
1180
#else
1181
  return 0;
1182
#endif
1183
0
}
1184
1185
/* Variant of object_get_optional_flags for when
1186
 * we know that we have exclusive access (during
1187
 * construction)
1188
 */
1189
static inline guint
1190
object_get_optional_flags_X (GObject *object)
1191
0
{
1192
0
#ifdef HAVE_OPTIONAL_FLAGS
1193
0
  GObjectReal *real = (GObjectReal *)object;
1194
0
  return real->optional_flags;
1195
#else
1196
  return 0;
1197
#endif
1198
0
}
1199
1200
#ifdef HAVE_OPTIONAL_FLAGS
1201
static inline void
1202
object_set_optional_flags (GObject *object,
1203
                          guint flags)
1204
0
{
1205
0
  GObjectReal *real = (GObjectReal *)object;
1206
0
  g_atomic_int_or (&real->optional_flags, flags);
1207
0
}
1208
1209
/* Variant for when we have exclusive access
1210
 * (during construction)
1211
 */
1212
static inline void
1213
object_set_optional_flags_X (GObject *object,
1214
                             guint flags)
1215
0
{
1216
0
  GObjectReal *real = (GObjectReal *)object;
1217
0
  real->optional_flags |= flags;
1218
0
}
1219
1220
/* Variant for when we have exclusive access
1221
 * (during construction)
1222
 */
1223
static inline void
1224
object_unset_optional_flags_X (GObject *object,
1225
                               guint flags)
1226
0
{
1227
0
  GObjectReal *real = (GObjectReal *)object;
1228
0
  real->optional_flags &= ~flags;
1229
0
}
1230
#endif
1231
1232
gboolean
1233
_g_object_has_signal_handler (GObject *object)
1234
0
{
1235
0
#ifdef HAVE_OPTIONAL_FLAGS
1236
0
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_SIGNAL_HANDLER) != 0;
1237
#else
1238
  return TRUE;
1239
#endif
1240
0
}
1241
1242
static inline gboolean
1243
_g_object_has_notify_handler (GObject *object)
1244
0
{
1245
0
#ifdef HAVE_OPTIONAL_FLAGS
1246
0
  return CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)) ||
1247
0
         (object_get_optional_flags (object) & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0;
1248
#else
1249
  return TRUE;
1250
#endif
1251
0
}
1252
1253
static inline gboolean
1254
_g_object_has_notify_handler_X (GObject *object)
1255
0
{
1256
0
#ifdef HAVE_OPTIONAL_FLAGS
1257
0
  return CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object)) ||
1258
0
         (object_get_optional_flags_X (object) & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0;
1259
#else
1260
  return TRUE;
1261
#endif
1262
0
}
1263
1264
void
1265
_g_object_set_has_signal_handler (GObject *object,
1266
                                  guint    signal_id)
1267
0
{
1268
0
#ifdef HAVE_OPTIONAL_FLAGS
1269
0
  guint flags = OPTIONAL_FLAG_HAS_SIGNAL_HANDLER;
1270
0
  if (signal_id == gobject_signals[NOTIFY])
1271
0
    flags |= OPTIONAL_FLAG_HAS_NOTIFY_HANDLER;
1272
0
  object_set_optional_flags (object, flags);
1273
0
#endif
1274
0
}
1275
1276
static inline gboolean
1277
object_in_construction (GObject *object)
1278
0
{
1279
0
#ifdef HAVE_OPTIONAL_FLAGS
1280
0
  return (object_get_optional_flags (object) & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0;
1281
#else
1282
  return g_datalist_id_get_data (&object->qdata, quark_in_construction) != NULL;
1283
#endif
1284
0
}
1285
1286
static inline void
1287
set_object_in_construction (GObject *object)
1288
0
{
1289
0
#ifdef HAVE_OPTIONAL_FLAGS
1290
0
  object_set_optional_flags_X (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1291
#else
1292
  g_datalist_id_set_data (&object->qdata, quark_in_construction, object);
1293
#endif
1294
0
}
1295
1296
static inline void
1297
unset_object_in_construction (GObject *object)
1298
0
{
1299
0
#ifdef HAVE_OPTIONAL_FLAGS
1300
0
  object_unset_optional_flags_X (object, OPTIONAL_FLAG_IN_CONSTRUCTION);
1301
#else
1302
  g_datalist_id_set_data (&object->qdata, quark_in_construction, NULL);
1303
#endif
1304
0
}
1305
1306
static void
1307
g_object_init (GObject    *object,
1308
         GObjectClass *class)
1309
0
{
1310
0
  object->ref_count = 1;
1311
0
  object->qdata = NULL;
1312
1313
0
  if (CLASS_HAS_PROPS (class) && CLASS_NEEDS_NOTIFY (class))
1314
0
    {
1315
      /* freeze object's notification queue, g_object_new_internal() preserves pairedness */
1316
0
      g_object_notify_queue_freeze (object, FALSE);
1317
0
    }
1318
1319
  /* mark object in-construction for notify_queue_thaw() and to allow construct-only properties */
1320
0
  set_object_in_construction (object);
1321
1322
0
  GOBJECT_IF_DEBUG (OBJECTS,
1323
0
    {
1324
0
      G_LOCK (debug_objects);
1325
0
      debug_objects_count++;
1326
0
      g_hash_table_add (debug_objects_ht, object);
1327
0
      G_UNLOCK (debug_objects);
1328
0
    });
1329
0
}
1330
1331
static void
1332
g_object_do_set_property (GObject      *object,
1333
        guint         property_id,
1334
        const GValue *value,
1335
        GParamSpec   *pspec)
1336
0
{
1337
0
  switch (property_id)
1338
0
    {
1339
0
    default:
1340
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1341
0
      break;
1342
0
    }
1343
0
}
1344
1345
static void
1346
g_object_do_get_property (GObject     *object,
1347
        guint        property_id,
1348
        GValue      *value,
1349
        GParamSpec  *pspec)
1350
0
{
1351
0
  switch (property_id)
1352
0
    {
1353
0
    default:
1354
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1355
0
      break;
1356
0
    }
1357
0
}
1358
1359
static void
1360
g_object_real_dispose (GObject *object)
1361
0
{
1362
0
  g_signal_handlers_destroy (object);
1363
0
  g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
1364
0
  g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
1365
0
  g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL);
1366
0
}
1367
1368
#ifdef G_ENABLE_DEBUG
1369
static gboolean
1370
floating_check (GObject *object)
1371
0
{
1372
0
  static const char *g_enable_diagnostic = NULL;
1373
1374
0
  if (G_UNLIKELY (g_enable_diagnostic == NULL))
1375
0
    {
1376
0
      g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
1377
0
      if (g_enable_diagnostic == NULL)
1378
0
        g_enable_diagnostic = "0";
1379
0
    }
1380
1381
0
  if (g_enable_diagnostic[0] == '1')
1382
0
    return g_object_is_floating (object);
1383
1384
0
  return FALSE;
1385
0
}
1386
#endif
1387
1388
static void
1389
g_object_finalize (GObject *object)
1390
0
{
1391
0
#ifdef G_ENABLE_DEBUG
1392
0
  if (object_in_construction (object))
1393
0
    {
1394
0
      g_critical ("object %s %p finalized while still in-construction",
1395
0
                  G_OBJECT_TYPE_NAME (object), object);
1396
0
    }
1397
1398
0
 if (floating_check (object))
1399
0
   {
1400
0
      g_critical ("A floating object %s %p was finalized. This means that someone\n"
1401
0
                  "called g_object_unref() on an object that had only a floating\n"
1402
0
                  "reference; the initial floating reference is not owned by anyone\n"
1403
0
                  "and must be removed with g_object_ref_sink().",
1404
0
                  G_OBJECT_TYPE_NAME (object), object);
1405
0
   }
1406
0
#endif
1407
1408
0
  g_datalist_clear (&object->qdata);
1409
  
1410
0
  GOBJECT_IF_DEBUG (OBJECTS,
1411
0
    {
1412
0
      G_LOCK (debug_objects);
1413
0
      g_assert (g_hash_table_contains (debug_objects_ht, object));
1414
0
      g_hash_table_remove (debug_objects_ht, object);
1415
0
      debug_objects_count--;
1416
0
      G_UNLOCK (debug_objects);
1417
0
    });
1418
0
}
1419
1420
static void
1421
g_object_dispatch_properties_changed (GObject     *object,
1422
              guint        n_pspecs,
1423
              GParamSpec **pspecs)
1424
0
{
1425
0
  guint i;
1426
1427
0
  for (i = 0; i < n_pspecs; i++)
1428
0
    g_signal_emit (object, gobject_signals[NOTIFY], g_param_spec_get_name_quark (pspecs[i]), pspecs[i]);
1429
0
}
1430
1431
/**
1432
 * g_object_run_dispose:
1433
 * @object: a #GObject
1434
 *
1435
 * Releases all references to other objects. This can be used to break
1436
 * reference cycles.
1437
 *
1438
 * This function should only be called from object system implementations.
1439
 */
1440
void
1441
g_object_run_dispose (GObject *object)
1442
0
{
1443
0
  g_return_if_fail (G_IS_OBJECT (object));
1444
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0);
1445
1446
0
  g_object_ref (object);
1447
0
  TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 0));
1448
0
  G_OBJECT_GET_CLASS (object)->dispose (object);
1449
0
  TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 0));
1450
0
  g_object_unref (object);
1451
0
}
1452
1453
/**
1454
 * g_object_freeze_notify:
1455
 * @object: a #GObject
1456
 *
1457
 * Increases the freeze count on @object. If the freeze count is
1458
 * non-zero, the emission of "notify" signals on @object is
1459
 * stopped. The signals are queued until the freeze count is decreased
1460
 * to zero. Duplicate notifications are squashed so that at most one
1461
 * #GObject::notify signal is emitted for each property modified while the
1462
 * object is frozen.
1463
 *
1464
 * This is necessary for accessors that modify multiple properties to prevent
1465
 * premature notification while the object is still being modified.
1466
 */
1467
void
1468
g_object_freeze_notify (GObject *object)
1469
0
{
1470
0
  g_return_if_fail (G_IS_OBJECT (object));
1471
1472
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1473
0
    return;
1474
1475
0
  g_object_ref (object);
1476
0
  g_object_notify_queue_freeze (object, FALSE);
1477
0
  g_object_unref (object);
1478
0
}
1479
1480
static inline void
1481
g_object_notify_by_spec_internal (GObject    *object,
1482
                                  GParamSpec *pspec)
1483
0
{
1484
0
#ifdef HAVE_OPTIONAL_FLAGS
1485
0
  guint object_flags;
1486
0
#endif
1487
0
  gboolean needs_notify;
1488
0
  gboolean in_init;
1489
1490
0
  if (G_UNLIKELY (~pspec->flags & G_PARAM_READABLE))
1491
0
    return;
1492
1493
0
  param_spec_follow_override (&pspec);
1494
1495
0
#ifdef HAVE_OPTIONAL_FLAGS
1496
  /* get all flags we need with a single atomic read */
1497
0
  object_flags = object_get_optional_flags (object);
1498
0
  needs_notify = ((object_flags & OPTIONAL_FLAG_HAS_NOTIFY_HANDLER) != 0) ||
1499
0
                  CLASS_NEEDS_NOTIFY (G_OBJECT_GET_CLASS (object));
1500
0
  in_init = (object_flags & OPTIONAL_FLAG_IN_CONSTRUCTION) != 0;
1501
#else
1502
  needs_notify = TRUE;
1503
  in_init = object_in_construction (object);
1504
#endif
1505
1506
0
  if (pspec != NULL && needs_notify)
1507
0
    {
1508
0
      GObjectNotifyQueue *nqueue;
1509
0
      gboolean need_thaw = TRUE;
1510
1511
      /* conditional freeze: only increase freeze count if already frozen */
1512
0
      nqueue = g_object_notify_queue_freeze (object, TRUE);
1513
0
      if (in_init && !nqueue)
1514
0
        {
1515
          /* We did not freeze the queue in g_object_init, but
1516
           * we gained a notify handler in instance init, so
1517
           * now we need to freeze just-in-time
1518
           */
1519
0
          nqueue = g_object_notify_queue_freeze (object, FALSE);
1520
0
          need_thaw = FALSE;
1521
0
        }
1522
1523
0
      if (nqueue != NULL)
1524
0
        {
1525
          /* we're frozen, so add to the queue and release our freeze */
1526
0
          g_object_notify_queue_add (object, nqueue, pspec);
1527
0
          if (need_thaw)
1528
0
            g_object_notify_queue_thaw (object, nqueue);
1529
0
        }
1530
0
      else
1531
0
        {
1532
          /*
1533
           * Coverity doesn’t understand the paired ref/unref here and seems to
1534
           * ignore the ref, thus reports every call to g_object_notify() as
1535
           * causing a double-free. That’s incorrect, but I can’t get a model
1536
           * file to work for avoiding the false positives, so instead comment
1537
           * out the ref/unref when doing static analysis.
1538
           */
1539
0
#ifndef __COVERITY__
1540
0
          g_object_ref (object);
1541
0
#endif
1542
1543
          /* not frozen, so just dispatch the notification directly */
1544
0
          G_OBJECT_GET_CLASS (object)
1545
0
              ->dispatch_properties_changed (object, 1, &pspec);
1546
1547
0
#ifndef __COVERITY__
1548
0
          g_object_unref (object);
1549
0
#endif
1550
0
        }
1551
0
    }
1552
0
}
1553
1554
/**
1555
 * g_object_notify:
1556
 * @object: a #GObject
1557
 * @property_name: the name of a property installed on the class of @object.
1558
 *
1559
 * Emits a "notify" signal for the property @property_name on @object.
1560
 *
1561
 * When possible, eg. when signaling a property change from within the class
1562
 * that registered the property, you should use g_object_notify_by_pspec()
1563
 * instead.
1564
 *
1565
 * Note that emission of the notify signal may be blocked with
1566
 * g_object_freeze_notify(). In this case, the signal emissions are queued
1567
 * and will be emitted (in reverse order) when g_object_thaw_notify() is
1568
 * called.
1569
 */
1570
void
1571
g_object_notify (GObject     *object,
1572
     const gchar *property_name)
1573
0
{
1574
0
  GParamSpec *pspec;
1575
  
1576
0
  g_return_if_fail (G_IS_OBJECT (object));
1577
0
  g_return_if_fail (property_name != NULL);
1578
  
1579
  /* We don't need to get the redirect target
1580
   * (by, e.g. calling g_object_class_find_property())
1581
   * because g_object_notify_queue_add() does that
1582
   */
1583
0
  pspec = g_param_spec_pool_lookup (pspec_pool,
1584
0
            property_name,
1585
0
            G_OBJECT_TYPE (object),
1586
0
            TRUE);
1587
1588
0
  if (!pspec)
1589
0
    g_warning ("%s: object class '%s' has no property named '%s'",
1590
0
         G_STRFUNC,
1591
0
         G_OBJECT_TYPE_NAME (object),
1592
0
         property_name);
1593
0
  else
1594
0
    g_object_notify_by_spec_internal (object, pspec);
1595
0
}
1596
1597
/**
1598
 * g_object_notify_by_pspec:
1599
 * @object: a #GObject
1600
 * @pspec: the #GParamSpec of a property installed on the class of @object.
1601
 *
1602
 * Emits a "notify" signal for the property specified by @pspec on @object.
1603
 *
1604
 * This function omits the property name lookup, hence it is faster than
1605
 * g_object_notify().
1606
 *
1607
 * One way to avoid using g_object_notify() from within the
1608
 * class that registered the properties, and using g_object_notify_by_pspec()
1609
 * instead, is to store the GParamSpec used with
1610
 * g_object_class_install_property() inside a static array, e.g.:
1611
 *
1612
 *|[<!-- language="C" --> 
1613
 *   typedef enum
1614
 *   {
1615
 *     PROP_FOO = 1,
1616
 *     PROP_LAST
1617
 *   } MyObjectProperty;
1618
 *
1619
 *   static GParamSpec *properties[PROP_LAST];
1620
 *
1621
 *   static void
1622
 *   my_object_class_init (MyObjectClass *klass)
1623
 *   {
1624
 *     properties[PROP_FOO] = g_param_spec_int ("foo", "Foo", "The foo",
1625
 *                                              0, 100,
1626
 *                                              50,
1627
 *                                              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1628
 *     g_object_class_install_property (gobject_class,
1629
 *                                      PROP_FOO,
1630
 *                                      properties[PROP_FOO]);
1631
 *   }
1632
 * ]|
1633
 *
1634
 * and then notify a change on the "foo" property with:
1635
 *
1636
 * |[<!-- language="C" --> 
1637
 *   g_object_notify_by_pspec (self, properties[PROP_FOO]);
1638
 * ]|
1639
 *
1640
 * Since: 2.26
1641
 */
1642
void
1643
g_object_notify_by_pspec (GObject    *object,
1644
        GParamSpec *pspec)
1645
0
{
1646
1647
0
  g_return_if_fail (G_IS_OBJECT (object));
1648
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1649
1650
0
  g_object_notify_by_spec_internal (object, pspec);
1651
0
}
1652
1653
/**
1654
 * g_object_thaw_notify:
1655
 * @object: a #GObject
1656
 *
1657
 * Reverts the effect of a previous call to
1658
 * g_object_freeze_notify(). The freeze count is decreased on @object
1659
 * and when it reaches zero, queued "notify" signals are emitted.
1660
 *
1661
 * Duplicate notifications for each property are squashed so that at most one
1662
 * #GObject::notify signal is emitted for each property, in the reverse order
1663
 * in which they have been queued.
1664
 *
1665
 * It is an error to call this function when the freeze count is zero.
1666
 */
1667
void
1668
g_object_thaw_notify (GObject *object)
1669
0
{
1670
0
  GObjectNotifyQueue *nqueue;
1671
  
1672
0
  g_return_if_fail (G_IS_OBJECT (object));
1673
0
  if (g_atomic_int_get (&object->ref_count) == 0)
1674
0
    return;
1675
  
1676
0
  g_object_ref (object);
1677
1678
  /* FIXME: Freezing is the only way to get at the notify queue.
1679
   * So we freeze once and then thaw twice.
1680
   */
1681
0
  nqueue = g_object_notify_queue_freeze (object, FALSE);
1682
0
  g_object_notify_queue_thaw (object, nqueue);
1683
0
  g_object_notify_queue_thaw (object, nqueue);
1684
1685
0
  g_object_unref (object);
1686
0
}
1687
1688
static void
1689
maybe_issue_property_deprecation_warning (const GParamSpec *pspec)
1690
0
{
1691
0
  static GHashTable *already_warned_table;
1692
0
  static const gchar *enable_diagnostic;
1693
0
  static GMutex already_warned_lock;
1694
0
  gboolean already;
1695
1696
0
  if (g_once_init_enter (&enable_diagnostic))
1697
0
    {
1698
0
      const gchar *value = g_getenv ("G_ENABLE_DIAGNOSTIC");
1699
1700
0
      if (!value)
1701
0
        value = "0";
1702
1703
0
      g_once_init_leave (&enable_diagnostic, value);
1704
0
    }
1705
1706
0
  if (enable_diagnostic[0] == '0')
1707
0
    return;
1708
1709
  /* We hash only on property names: this means that we could end up in
1710
   * a situation where we fail to emit a warning about a pair of
1711
   * same-named deprecated properties used on two separate types.
1712
   * That's pretty unlikely to occur, and even if it does, you'll still
1713
   * have seen the warning for the first one...
1714
   *
1715
   * Doing it this way lets us hash directly on the (interned) property
1716
   * name pointers.
1717
   */
1718
0
  g_mutex_lock (&already_warned_lock);
1719
1720
0
  if (already_warned_table == NULL)
1721
0
    already_warned_table = g_hash_table_new (NULL, NULL);
1722
1723
0
  already = g_hash_table_contains (already_warned_table, (gpointer) pspec->name);
1724
0
  if (!already)
1725
0
    g_hash_table_add (already_warned_table, (gpointer) pspec->name);
1726
1727
0
  g_mutex_unlock (&already_warned_lock);
1728
1729
0
  if (!already)
1730
0
    g_warning ("The property %s:%s is deprecated and shouldn't be used "
1731
0
               "anymore. It will be removed in a future version.",
1732
0
               g_type_name (pspec->owner_type), pspec->name);
1733
0
}
1734
1735
static inline void
1736
consider_issuing_property_deprecation_warning (const GParamSpec *pspec)
1737
0
{
1738
0
  if (G_UNLIKELY (pspec->flags & G_PARAM_DEPRECATED))
1739
0
    maybe_issue_property_deprecation_warning (pspec);
1740
0
}
1741
1742
static inline void
1743
object_get_property (GObject     *object,
1744
         GParamSpec  *pspec,
1745
         GValue      *value)
1746
0
{
1747
0
  GTypeInstance *inst = (GTypeInstance *) object;
1748
0
  GObjectClass *class;
1749
0
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1750
1751
0
  if (G_LIKELY (inst->g_class->g_type == pspec->owner_type))
1752
0
    class = (GObjectClass *) inst->g_class;
1753
0
  else
1754
0
    class = g_type_class_peek (pspec->owner_type);
1755
1756
0
  g_assert (class != NULL);
1757
1758
0
  param_spec_follow_override (&pspec);
1759
1760
0
  consider_issuing_property_deprecation_warning (pspec);
1761
1762
0
  class->get_property (object, param_id, value, pspec);
1763
0
}
1764
1765
static inline void
1766
object_set_property (GObject             *object,
1767
         GParamSpec          *pspec,
1768
         const GValue        *value,
1769
         GObjectNotifyQueue  *nqueue,
1770
         gboolean             user_specified)
1771
0
{
1772
0
  GTypeInstance *inst = (GTypeInstance *) object;
1773
0
  GObjectClass *class;
1774
0
  GParamSpecClass *pclass;
1775
0
  guint param_id = PARAM_SPEC_PARAM_ID (pspec);
1776
1777
0
  if (G_LIKELY (inst->g_class->g_type == pspec->owner_type))
1778
0
    class = (GObjectClass *) inst->g_class;
1779
0
  else
1780
0
    class = g_type_class_peek (pspec->owner_type);
1781
1782
0
  g_assert (class != NULL);
1783
1784
0
  param_spec_follow_override (&pspec);
1785
1786
0
  if (user_specified)
1787
0
    consider_issuing_property_deprecation_warning (pspec);
1788
1789
0
  pclass = G_PARAM_SPEC_GET_CLASS (pspec);
1790
0
  if (g_value_type_compatible (G_VALUE_TYPE (value), pspec->value_type) &&
1791
0
      (pclass->value_validate == NULL ||
1792
0
       (pclass->value_is_valid != NULL && pclass->value_is_valid (pspec, value))))
1793
0
    {
1794
0
      class->set_property (object, param_id, value, pspec);
1795
0
    }
1796
0
  else
1797
0
    {
1798
      /* provide a copy to work from, convert (if necessary) and validate */
1799
0
      GValue tmp_value = G_VALUE_INIT;
1800
1801
0
      g_value_init (&tmp_value, pspec->value_type);
1802
1803
0
      if (!g_value_transform (value, &tmp_value))
1804
0
        g_warning ("unable to set property '%s' of type '%s' from value of type '%s'",
1805
0
                   pspec->name,
1806
0
                   g_type_name (pspec->value_type),
1807
0
                   G_VALUE_TYPE_NAME (value));
1808
0
      else if (g_param_value_validate (pspec, &tmp_value) && !(pspec->flags & G_PARAM_LAX_VALIDATION))
1809
0
        {
1810
0
          gchar *contents = g_strdup_value_contents (value);
1811
1812
0
          g_warning ("value \"%s\" of type '%s' is invalid or out of range for property '%s' of type '%s'",
1813
0
                     contents,
1814
0
                     G_VALUE_TYPE_NAME (value),
1815
0
                     pspec->name,
1816
0
                     g_type_name (pspec->value_type));
1817
0
          g_free (contents);
1818
0
        }
1819
0
      else
1820
0
        {
1821
0
          class->set_property (object, param_id, &tmp_value, pspec);
1822
0
        }
1823
1824
0
      g_value_unset (&tmp_value);
1825
0
    }
1826
1827
0
  if ((pspec->flags & (G_PARAM_EXPLICIT_NOTIFY | G_PARAM_READABLE)) == G_PARAM_READABLE &&
1828
0
      nqueue != NULL)
1829
0
    g_object_notify_queue_add (object, nqueue, pspec);
1830
0
}
1831
1832
static void
1833
object_interface_check_properties (gpointer check_data,
1834
           gpointer g_iface)
1835
0
{
1836
0
  GTypeInterface *iface_class = g_iface;
1837
0
  GObjectClass *class;
1838
0
  GType iface_type = iface_class->g_type;
1839
0
  GParamSpec **pspecs;
1840
0
  guint n;
1841
1842
0
  class = g_type_class_ref (iface_class->g_instance_type);
1843
1844
0
  if (class == NULL)
1845
0
    return;
1846
1847
0
  if (!G_IS_OBJECT_CLASS (class))
1848
0
    goto out;
1849
1850
0
  pspecs = g_param_spec_pool_list (pspec_pool, iface_type, &n);
1851
1852
0
  while (n--)
1853
0
    {
1854
0
      GParamSpec *class_pspec = g_param_spec_pool_lookup (pspec_pool,
1855
0
                pspecs[n]->name,
1856
0
                G_OBJECT_CLASS_TYPE (class),
1857
0
                TRUE);
1858
1859
0
      if (!class_pspec)
1860
0
  {
1861
0
    g_critical ("Object class %s doesn't implement property "
1862
0
          "'%s' from interface '%s'",
1863
0
          g_type_name (G_OBJECT_CLASS_TYPE (class)),
1864
0
          pspecs[n]->name,
1865
0
          g_type_name (iface_type));
1866
1867
0
    continue;
1868
0
  }
1869
1870
      /* We do a number of checks on the properties of an interface to
1871
       * make sure that all classes implementing the interface are
1872
       * overriding the properties correctly.
1873
       *
1874
       * We do the checks in order of importance so that we can give
1875
       * more useful error messages first.
1876
       *
1877
       * First, we check that the implementation doesn't remove the
1878
       * basic functionality (readability, writability) advertised by
1879
       * the interface.  Next, we check that it doesn't introduce
1880
       * additional restrictions (such as construct-only).  Finally, we
1881
       * make sure the types are compatible.
1882
       */
1883
1884
0
#define SUBSET(a,b,mask) (((a) & ~(b) & (mask)) == 0)
1885
      /* If the property on the interface is readable then the
1886
       * implementation must be readable.  If the interface is writable
1887
       * then the implementation must be writable.
1888
       */
1889
0
      if (!SUBSET (pspecs[n]->flags, class_pspec->flags, G_PARAM_READABLE | G_PARAM_WRITABLE))
1890
0
        {
1891
0
          g_critical ("Flags for property '%s' on class '%s' remove functionality compared with the "
1892
0
                      "property on interface '%s'\n", pspecs[n]->name,
1893
0
                      g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1894
0
          continue;
1895
0
        }
1896
1897
      /* If the property on the interface is writable then we need to
1898
       * make sure the implementation doesn't introduce new restrictions
1899
       * on that writability (ie: construct-only).
1900
       *
1901
       * If the interface was not writable to begin with then we don't
1902
       * really have any problems here because "writable at construct
1903
       * time only" is still more permissive than "read only".
1904
       */
1905
0
      if (pspecs[n]->flags & G_PARAM_WRITABLE)
1906
0
        {
1907
0
          if (!SUBSET (class_pspec->flags, pspecs[n]->flags, G_PARAM_CONSTRUCT_ONLY))
1908
0
            {
1909
0
              g_critical ("Flags for property '%s' on class '%s' introduce additional restrictions on "
1910
0
                          "writability compared with the property on interface '%s'\n", pspecs[n]->name,
1911
0
                          g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (iface_type));
1912
0
              continue;
1913
0
            }
1914
0
        }
1915
0
#undef SUBSET
1916
1917
      /* If the property on the interface is readable then we are
1918
       * effectively advertising that reading the property will return a
1919
       * value of a specific type.  All implementations of the interface
1920
       * need to return items of this type -- but may be more
1921
       * restrictive.  For example, it is legal to have:
1922
       *
1923
       *   GtkWidget *get_item();
1924
       *
1925
       * that is implemented by a function that always returns a
1926
       * GtkEntry.  In short: readability implies that the
1927
       * implementation  value type must be equal or more restrictive.
1928
       *
1929
       * Similarly, if the property on the interface is writable then
1930
       * must be able to accept the property being set to any value of
1931
       * that type, including subclasses.  In this case, we may also be
1932
       * less restrictive.  For example, it is legal to have:
1933
       *
1934
       *   set_item (GtkEntry *);
1935
       *
1936
       * that is implemented by a function that will actually work with
1937
       * any GtkWidget.  In short: writability implies that the
1938
       * implementation value type must be equal or less restrictive.
1939
       *
1940
       * In the case that the property is both readable and writable
1941
       * then the only way that both of the above can be satisfied is
1942
       * with a type that is exactly equal.
1943
       */
1944
0
      switch (pspecs[n]->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE))
1945
0
        {
1946
0
        case G_PARAM_READABLE | G_PARAM_WRITABLE:
1947
          /* class pspec value type must have exact equality with interface */
1948
0
          if (pspecs[n]->value_type != class_pspec->value_type)
1949
0
            g_critical ("Read/writable property '%s' on class '%s' has type '%s' which is not exactly equal to the "
1950
0
                        "type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1951
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1952
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1953
0
          break;
1954
1955
0
        case G_PARAM_READABLE:
1956
          /* class pspec value type equal or more restrictive than interface */
1957
0
          if (!g_type_is_a (class_pspec->value_type, pspecs[n]->value_type))
1958
0
            g_critical ("Read-only property '%s' on class '%s' has type '%s' which is not equal to or more "
1959
0
                        "restrictive than the type '%s' of the property on the interface '%s'\n", pspecs[n]->name,
1960
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1961
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1962
0
          break;
1963
1964
0
        case G_PARAM_WRITABLE:
1965
          /* class pspec value type equal or less restrictive than interface */
1966
0
          if (!g_type_is_a (pspecs[n]->value_type, class_pspec->value_type))
1967
0
            g_critical ("Write-only property '%s' on class '%s' has type '%s' which is not equal to or less "
1968
0
                        "restrictive than the type '%s' of the property on the interface '%s' \n", pspecs[n]->name,
1969
0
                        g_type_name (G_OBJECT_CLASS_TYPE (class)), g_type_name (G_PARAM_SPEC_VALUE_TYPE (class_pspec)),
1970
0
                        g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspecs[n])), g_type_name (iface_type));
1971
0
          break;
1972
1973
0
        default:
1974
0
          g_assert_not_reached ();
1975
0
        }
1976
0
    }
1977
1978
0
  g_free (pspecs);
1979
1980
0
 out:
1981
0
  g_type_class_unref (class);
1982
0
}
1983
1984
GType
1985
g_object_get_type (void)
1986
0
{
1987
0
    return G_TYPE_OBJECT;
1988
0
}
1989
1990
/**
1991
 * g_object_new: (skip)
1992
 * @object_type: the type id of the #GObject subtype to instantiate
1993
 * @first_property_name: the name of the first property
1994
 * @...: the value of the first property, followed optionally by more
1995
 *  name/value pairs, followed by %NULL
1996
 *
1997
 * Creates a new instance of a #GObject subtype and sets its properties.
1998
 *
1999
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2000
 * which are not explicitly specified are set to their default values. Any
2001
 * private data for the object is guaranteed to be initialized with zeros, as
2002
 * per g_type_create_instance().
2003
 *
2004
 * Note that in C, small integer types in variable argument lists are promoted
2005
 * up to #gint or #guint as appropriate, and read back accordingly. #gint is 32
2006
 * bits on every platform on which GLib is currently supported. This means that
2007
 * you can use C expressions of type #gint with g_object_new() and properties of
2008
 * type #gint or #guint or smaller. Specifically, you can use integer literals
2009
 * with these property types.
2010
 *
2011
 * When using property types of #gint64 or #guint64, you must ensure that the
2012
 * value that you provide is 64 bit. This means that you should use a cast or
2013
 * make use of the %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
2014
 *
2015
 * Similarly, #gfloat is promoted to #gdouble, so you must ensure that the value
2016
 * you provide is a #gdouble, even for a property of type #gfloat.
2017
 *
2018
 * Since GLib 2.72, all #GObjects are guaranteed to be aligned to at least the
2019
 * alignment of the largest basic GLib type (typically this is #guint64 or
2020
 * #gdouble). If you need larger alignment for an element in a #GObject, you
2021
 * should allocate it on the heap (aligned), or arrange for your #GObject to be
2022
 * appropriately padded.
2023
 *
2024
 * Returns: (transfer full) (type GObject.Object): a new instance of
2025
 *   @object_type
2026
 */
2027
gpointer
2028
g_object_new (GType    object_type,
2029
        const gchar *first_property_name,
2030
        ...)
2031
0
{
2032
0
  GObject *object;
2033
0
  va_list var_args;
2034
  
2035
  /* short circuit for calls supplying no properties */
2036
0
  if (!first_property_name)
2037
0
    return g_object_new_with_properties (object_type, 0, NULL, NULL);
2038
2039
0
  va_start (var_args, first_property_name);
2040
0
  object = g_object_new_valist (object_type, first_property_name, var_args);
2041
0
  va_end (var_args);
2042
  
2043
0
  return object;
2044
0
}
2045
2046
/* Check alignment. (See https://gitlab.gnome.org/GNOME/glib/-/issues/1231.)
2047
 * This should never fail, since g_type_create_instance() uses g_slice_alloc0().
2048
 * The GSlice allocator always aligns to the next power of 2 greater than the
2049
 * allocation size. The allocation size for a GObject is
2050
 *   sizeof(GTypeInstance) + sizeof(guint) + sizeof(GData*)
2051
 * which is 12B on 32-bit platforms, and larger on 64-bit systems. In both
2052
 * cases, that’s larger than the 8B needed for a guint64 or gdouble.
2053
 *
2054
 * If GSlice falls back to malloc(), it’s documented to return something
2055
 * suitably aligned for any basic type. */
2056
static inline gboolean
2057
g_object_is_aligned (GObject *object)
2058
0
{
2059
0
  return ((((guintptr) (void *) object) %
2060
0
             MAX (G_ALIGNOF (gdouble),
2061
0
                  MAX (G_ALIGNOF (guint64),
2062
0
                       MAX (G_ALIGNOF (gint),
2063
0
                            G_ALIGNOF (glong))))) == 0);
2064
0
}
2065
2066
static gpointer
2067
g_object_new_with_custom_constructor (GObjectClass          *class,
2068
                                      GObjectConstructParam *params,
2069
                                      guint                  n_params)
2070
0
{
2071
0
  GObjectNotifyQueue *nqueue = NULL;
2072
0
  gboolean newly_constructed;
2073
0
  GObjectConstructParam *cparams;
2074
0
  gboolean free_cparams = FALSE;
2075
0
  GObject *object;
2076
0
  GValue *cvalues;
2077
0
  gint cvals_used;
2078
0
  GSList *node;
2079
0
  guint i;
2080
2081
  /* If we have ->constructed() then we have to do a lot more work.
2082
   * It's possible that this is a singleton and it's also possible
2083
   * that the user's constructor() will attempt to modify the values
2084
   * that we pass in, so we'll need to allocate copies of them.
2085
   * It's also possible that the user may attempt to call
2086
   * g_object_set() from inside of their constructor, so we need to
2087
   * add ourselves to a list of objects for which that is allowed
2088
   * while their constructor() is running.
2089
   */
2090
2091
  /* Create the array of GObjectConstructParams for constructor(),
2092
   * The 1024 here is an arbitrary, high limit that no sane code
2093
   * will ever hit, just to avoid the possibility of stack overflow.
2094
   */
2095
0
  if (G_LIKELY (class->n_construct_properties < 1024))
2096
0
    {
2097
0
      cparams = g_newa0 (GObjectConstructParam, class->n_construct_properties);
2098
0
      cvalues = g_newa0 (GValue, class->n_construct_properties);
2099
0
    }
2100
0
  else
2101
0
    {
2102
0
      cparams = g_new0 (GObjectConstructParam, class->n_construct_properties);
2103
0
      cvalues = g_new0 (GValue, class->n_construct_properties);
2104
0
      free_cparams = TRUE;
2105
0
    }
2106
0
  cvals_used = 0;
2107
0
  i = 0;
2108
2109
  /* As above, we may find the value in the passed-in params list.
2110
   *
2111
   * If we have the value passed in then we can use the GValue from
2112
   * it directly because it is safe to modify.  If we use the
2113
   * default value from the class, we had better not pass that in
2114
   * and risk it being modified, so we create a new one.
2115
   * */
2116
0
  for (node = class->construct_properties; node; node = node->next)
2117
0
    {
2118
0
      GParamSpec *pspec;
2119
0
      GValue *value;
2120
0
      guint j;
2121
2122
0
      pspec = node->data;
2123
0
      value = NULL; /* to silence gcc... */
2124
2125
0
      for (j = 0; j < n_params; j++)
2126
0
        if (params[j].pspec == pspec)
2127
0
          {
2128
0
            consider_issuing_property_deprecation_warning (pspec);
2129
0
            value = params[j].value;
2130
0
            break;
2131
0
          }
2132
2133
0
      if (value == NULL)
2134
0
        {
2135
0
          value = &cvalues[cvals_used++];
2136
0
          g_value_init (value, pspec->value_type);
2137
0
          g_param_value_set_default (pspec, value);
2138
0
        }
2139
2140
0
      cparams[i].pspec = pspec;
2141
0
      cparams[i].value = value;
2142
0
      i++;
2143
0
    }
2144
2145
  /* construct object from construction parameters */
2146
0
  object = class->constructor (class->g_type_class.g_type, class->n_construct_properties, cparams);
2147
  /* free construction values */
2148
0
  while (cvals_used--)
2149
0
    g_value_unset (&cvalues[cvals_used]);
2150
2151
0
  if (free_cparams)
2152
0
    {
2153
0
      g_free (cparams);
2154
0
      g_free (cvalues);
2155
0
    }
2156
2157
  /* There is code in the wild that relies on being able to return NULL
2158
   * from its custom constructor.  This was never a supported operation,
2159
   * but since the code is already out there...
2160
   */
2161
0
  if (object == NULL)
2162
0
    {
2163
0
      g_critical ("Custom constructor for class %s returned NULL (which is invalid). "
2164
0
                  "Please use GInitable instead.", G_OBJECT_CLASS_NAME (class));
2165
0
      return NULL;
2166
0
    }
2167
2168
0
  if (!g_object_is_aligned (object))
2169
0
    {
2170
0
      g_critical ("Custom constructor for class %s returned a non-aligned "
2171
0
                  "GObject (which is invalid since GLib 2.72). Assuming any "
2172
0
                  "code using this object doesn’t require it to be aligned. "
2173
0
                  "Please fix your constructor to align to the largest GLib "
2174
0
                  "basic type (typically gdouble or guint64).",
2175
0
                  G_OBJECT_CLASS_NAME (class));
2176
0
    }
2177
2178
  /* g_object_init() will have marked the object as being in-construction.
2179
   * Check if the returned object still is so marked, or if this is an
2180
   * already-existing singleton (in which case we should not do 'constructed').
2181
   */
2182
0
  newly_constructed = object_in_construction (object);
2183
0
  if (newly_constructed)
2184
0
    unset_object_in_construction (object);
2185
2186
0
  if (CLASS_HAS_PROPS (class))
2187
0
    {
2188
0
      if ((newly_constructed && _g_object_has_notify_handler_X (object)) ||
2189
0
          _g_object_has_notify_handler (object))
2190
0
        {
2191
          /* This may or may not have been setup in g_object_init().
2192
           * If it hasn't, we do it now.
2193
           */
2194
0
          nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
2195
0
          if (!nqueue)
2196
0
            nqueue = g_object_notify_queue_freeze (object, FALSE);
2197
0
        }
2198
0
    }
2199
2200
  /* run 'constructed' handler if there is a custom one */
2201
0
  if (newly_constructed && CLASS_HAS_CUSTOM_CONSTRUCTED (class))
2202
0
    class->constructed (object);
2203
2204
  /* set remaining properties */
2205
0
  for (i = 0; i < n_params; i++)
2206
0
    if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2207
0
      object_set_property (object, params[i].pspec, params[i].value, nqueue, TRUE);
2208
2209
  /* If nqueue is non-NULL then we are frozen.  Thaw it. */
2210
0
  if (nqueue)
2211
0
    g_object_notify_queue_thaw (object, nqueue);
2212
2213
0
  return object;
2214
0
}
2215
2216
static gpointer
2217
g_object_new_internal (GObjectClass          *class,
2218
                       GObjectConstructParam *params,
2219
                       guint                  n_params)
2220
0
{
2221
0
  GObjectNotifyQueue *nqueue = NULL;
2222
0
  GObject *object;
2223
0
  guint i;
2224
2225
0
  if G_UNLIKELY (CLASS_HAS_CUSTOM_CONSTRUCTOR (class))
2226
0
    return g_object_new_with_custom_constructor (class, params, n_params);
2227
2228
0
  object = (GObject *) g_type_create_instance (class->g_type_class.g_type);
2229
2230
0
  g_assert (g_object_is_aligned (object));
2231
2232
0
  unset_object_in_construction (object);
2233
2234
0
  if (CLASS_HAS_PROPS (class))
2235
0
    {
2236
0
      GSList *node;
2237
2238
0
      if (_g_object_has_notify_handler_X (object))
2239
0
        {
2240
          /* This may or may not have been setup in g_object_init().
2241
           * If it hasn't, we do it now.
2242
           */
2243
0
          nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
2244
0
          if (!nqueue)
2245
0
            nqueue = g_object_notify_queue_freeze (object, FALSE);
2246
0
        }
2247
2248
      /* We will set exactly n_construct_properties construct
2249
       * properties, but they may come from either the class default
2250
       * values or the passed-in parameter list.
2251
       */
2252
0
      for (node = class->construct_properties; node; node = node->next)
2253
0
        {
2254
0
          const GValue *value;
2255
0
          GParamSpec *pspec;
2256
0
          guint j;
2257
0
          gboolean user_specified = FALSE;
2258
2259
0
          pspec = node->data;
2260
0
          value = NULL; /* to silence gcc... */
2261
2262
0
          for (j = 0; j < n_params; j++)
2263
0
            if (params[j].pspec == pspec)
2264
0
              {
2265
0
                value = params[j].value;
2266
0
                user_specified = TRUE;
2267
0
                break;
2268
0
              }
2269
2270
0
          if (value == NULL)
2271
0
            value = g_param_spec_get_default_value (pspec);
2272
2273
0
          object_set_property (object, pspec, value, nqueue, user_specified);
2274
0
        }
2275
0
    }
2276
2277
  /* run 'constructed' handler if there is a custom one */
2278
0
  if (CLASS_HAS_CUSTOM_CONSTRUCTED (class))
2279
0
    class->constructed (object);
2280
2281
  /* Set remaining properties.  The construct properties will
2282
   * already have been taken, so set only the non-construct ones.
2283
   */
2284
0
  for (i = 0; i < n_params; i++)
2285
0
    if (!(params[i].pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2286
0
      object_set_property (object, params[i].pspec, params[i].value, nqueue, TRUE);
2287
2288
0
  if (nqueue)
2289
0
    g_object_notify_queue_thaw (object, nqueue);
2290
2291
0
  return object;
2292
0
}
2293
2294
2295
static inline gboolean
2296
g_object_new_is_valid_property (GType                  object_type,
2297
                                GParamSpec            *pspec,
2298
                                const char            *name,
2299
                                GObjectConstructParam *params,
2300
                                guint                  n_params)
2301
0
{
2302
0
  guint i;
2303
2304
0
  if (G_UNLIKELY (pspec == NULL))
2305
0
    {
2306
0
      g_critical ("%s: object class '%s' has no property named '%s'",
2307
0
                  G_STRFUNC, g_type_name (object_type), name);
2308
0
      return FALSE;
2309
0
    }
2310
2311
0
  if (G_UNLIKELY (~pspec->flags & G_PARAM_WRITABLE))
2312
0
    {
2313
0
      g_critical ("%s: property '%s' of object class '%s' is not writable",
2314
0
                  G_STRFUNC, pspec->name, g_type_name (object_type));
2315
0
      return FALSE;
2316
0
    }
2317
2318
0
  if (G_UNLIKELY (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
2319
0
    {
2320
0
      for (i = 0; i < n_params; i++)
2321
0
        if (params[i].pspec == pspec)
2322
0
          break;
2323
0
      if (G_UNLIKELY (i != n_params))
2324
0
        {
2325
0
          g_critical ("%s: property '%s' for type '%s' cannot be set twice",
2326
0
                      G_STRFUNC, name, g_type_name (object_type));
2327
0
          return FALSE;
2328
0
        }
2329
0
    }
2330
0
  return TRUE;
2331
0
}
2332
2333
2334
/**
2335
 * g_object_new_with_properties: (skip)
2336
 * @object_type: the object type to instantiate
2337
 * @n_properties: the number of properties
2338
 * @names: (array length=n_properties): the names of each property to be set
2339
 * @values: (array length=n_properties): the values of each property to be set
2340
 *
2341
 * Creates a new instance of a #GObject subtype and sets its properties using
2342
 * the provided arrays. Both arrays must have exactly @n_properties elements,
2343
 * and the names and values correspond by index.
2344
 *
2345
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2346
 * which are not explicitly specified are set to their default values.
2347
 *
2348
 * Returns: (type GObject.Object) (transfer full): a new instance of
2349
 * @object_type
2350
 *
2351
 * Since: 2.54
2352
 */
2353
GObject *
2354
g_object_new_with_properties (GType          object_type,
2355
                              guint          n_properties,
2356
                              const char    *names[],
2357
                              const GValue   values[])
2358
0
{
2359
0
  GObjectClass *class, *unref_class = NULL;
2360
0
  GObject *object;
2361
2362
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2363
2364
  /* Try to avoid thrashing the ref_count if we don't need to (since
2365
   * it's a locked operation).
2366
   */
2367
0
  class = g_type_class_peek_static (object_type);
2368
2369
0
  if (class == NULL)
2370
0
    class = unref_class = g_type_class_ref (object_type);
2371
2372
0
  if (n_properties > 0)
2373
0
    {
2374
0
      guint i, count = 0;
2375
0
      GObjectConstructParam *params;
2376
2377
0
      params = g_newa (GObjectConstructParam, n_properties);
2378
0
      for (i = 0; i < n_properties; i++)
2379
0
        {
2380
0
          GParamSpec *pspec = find_pspec (class, names[i]);
2381
2382
0
          if (!g_object_new_is_valid_property (object_type, pspec, names[i], params, count))
2383
0
            continue;
2384
0
          params[count].pspec = pspec;
2385
0
          params[count].value = (GValue *) &values[i];
2386
0
          count++;
2387
0
        }
2388
0
      object = g_object_new_internal (class, params, count);
2389
0
    }
2390
0
  else
2391
0
    object = g_object_new_internal (class, NULL, 0);
2392
2393
0
  if (unref_class != NULL)
2394
0
    g_type_class_unref (unref_class);
2395
2396
0
  return object;
2397
0
}
2398
2399
/**
2400
 * g_object_newv:
2401
 * @object_type: the type id of the #GObject subtype to instantiate
2402
 * @n_parameters: the length of the @parameters array
2403
 * @parameters: (array length=n_parameters): an array of #GParameter
2404
 *
2405
 * Creates a new instance of a #GObject subtype and sets its properties.
2406
 *
2407
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2408
 * which are not explicitly specified are set to their default values.
2409
 *
2410
 * Returns: (type GObject.Object) (transfer full): a new instance of
2411
 * @object_type
2412
 *
2413
 * Deprecated: 2.54: Use g_object_new_with_properties() instead.
2414
 * deprecated. See #GParameter for more information.
2415
 */
2416
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2417
gpointer
2418
g_object_newv (GType       object_type,
2419
               guint       n_parameters,
2420
               GParameter *parameters)
2421
0
{
2422
0
  GObjectClass *class, *unref_class = NULL;
2423
0
  GObject *object;
2424
2425
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2426
0
  g_return_val_if_fail (n_parameters == 0 || parameters != NULL, NULL);
2427
2428
  /* Try to avoid thrashing the ref_count if we don't need to (since
2429
   * it's a locked operation).
2430
   */
2431
0
  class = g_type_class_peek_static (object_type);
2432
2433
0
  if (!class)
2434
0
    class = unref_class = g_type_class_ref (object_type);
2435
2436
0
  if (n_parameters)
2437
0
    {
2438
0
      GObjectConstructParam *cparams;
2439
0
      guint i, j;
2440
2441
0
      cparams = g_newa (GObjectConstructParam, n_parameters);
2442
0
      j = 0;
2443
2444
0
      for (i = 0; i < n_parameters; i++)
2445
0
        {
2446
0
          GParamSpec *pspec = find_pspec (class, parameters[i].name);
2447
2448
0
          if (!g_object_new_is_valid_property (object_type, pspec, parameters[i].name, cparams, j))
2449
0
            continue;
2450
2451
0
          cparams[j].pspec = pspec;
2452
0
          cparams[j].value = &parameters[i].value;
2453
0
          j++;
2454
0
        }
2455
2456
0
      object = g_object_new_internal (class, cparams, j);
2457
0
    }
2458
0
  else
2459
    /* Fast case: no properties passed in. */
2460
0
    object = g_object_new_internal (class, NULL, 0);
2461
2462
0
  if (unref_class)
2463
0
    g_type_class_unref (unref_class);
2464
2465
0
  return object;
2466
0
}
2467
G_GNUC_END_IGNORE_DEPRECATIONS
2468
2469
/**
2470
 * g_object_new_valist: (skip)
2471
 * @object_type: the type id of the #GObject subtype to instantiate
2472
 * @first_property_name: the name of the first property
2473
 * @var_args: the value of the first property, followed optionally by more
2474
 *  name/value pairs, followed by %NULL
2475
 *
2476
 * Creates a new instance of a #GObject subtype and sets its properties.
2477
 *
2478
 * Construction parameters (see %G_PARAM_CONSTRUCT, %G_PARAM_CONSTRUCT_ONLY)
2479
 * which are not explicitly specified are set to their default values.
2480
 *
2481
 * Returns: a new instance of @object_type
2482
 */
2483
GObject*
2484
g_object_new_valist (GType        object_type,
2485
                     const gchar *first_property_name,
2486
                     va_list      var_args)
2487
0
{
2488
0
  GObjectClass *class, *unref_class = NULL;
2489
0
  GObject *object;
2490
2491
0
  g_return_val_if_fail (G_TYPE_IS_OBJECT (object_type), NULL);
2492
2493
  /* Try to avoid thrashing the ref_count if we don't need to (since
2494
   * it's a locked operation).
2495
   */
2496
0
  class = g_type_class_peek_static (object_type);
2497
2498
0
  if (!class)
2499
0
    class = unref_class = g_type_class_ref (object_type);
2500
2501
0
  if (first_property_name)
2502
0
    {
2503
0
      GObjectConstructParam params_stack[16];
2504
0
      GValue values_stack[G_N_ELEMENTS (params_stack)];
2505
0
      GTypeValueTable *vtabs_stack[G_N_ELEMENTS (params_stack)];
2506
0
      const gchar *name;
2507
0
      GObjectConstructParam *params = params_stack;
2508
0
      GValue *values = values_stack;
2509
0
      GTypeValueTable **vtabs = vtabs_stack;
2510
0
      guint n_params = 0;
2511
0
      guint n_params_alloc = G_N_ELEMENTS (params_stack);
2512
2513
0
      name = first_property_name;
2514
2515
0
      do
2516
0
        {
2517
0
          gchar *error = NULL;
2518
0
          GParamSpec *pspec = find_pspec (class, name);
2519
2520
0
          if (!g_object_new_is_valid_property (object_type, pspec, name, params, n_params))
2521
0
            break;
2522
2523
0
          if (G_UNLIKELY (n_params == n_params_alloc))
2524
0
            {
2525
0
              guint i;
2526
2527
0
              if (n_params_alloc == G_N_ELEMENTS (params_stack))
2528
0
                {
2529
0
                  n_params_alloc = G_N_ELEMENTS (params_stack) * 2u;
2530
0
                  params = g_new (GObjectConstructParam, n_params_alloc);
2531
0
                  values = g_new (GValue, n_params_alloc);
2532
0
                  vtabs = g_new (GTypeValueTable *, n_params_alloc);
2533
0
                  memcpy (params, params_stack, sizeof (GObjectConstructParam) * n_params);
2534
0
                  memcpy (values, values_stack, sizeof (GValue) * n_params);
2535
0
                  memcpy (vtabs, vtabs_stack, sizeof (GTypeValueTable *) * n_params);
2536
0
                }
2537
0
              else
2538
0
                {
2539
0
                  n_params_alloc *= 2u;
2540
0
                  params = g_realloc (params, sizeof (GObjectConstructParam) * n_params_alloc);
2541
0
                  values = g_realloc (values, sizeof (GValue) * n_params_alloc);
2542
0
                  vtabs = g_realloc (vtabs, sizeof (GTypeValueTable *) * n_params_alloc);
2543
0
                }
2544
2545
0
              for (i = 0; i < n_params; i++)
2546
0
                params[i].value = &values[i];
2547
0
            }
2548
2549
0
          params[n_params].pspec = pspec;
2550
0
          params[n_params].value = &values[n_params];
2551
0
          memset (&values[n_params], 0, sizeof (GValue));
2552
2553
0
          G_VALUE_COLLECT_INIT2 (&values[n_params], vtabs[n_params], pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
2554
2555
0
          if (error)
2556
0
            {
2557
0
              g_critical ("%s: %s", G_STRFUNC, error);
2558
0
              g_value_unset (&values[n_params]);
2559
0
              g_free (error);
2560
0
              break;
2561
0
            }
2562
2563
0
          n_params++;
2564
0
        }
2565
0
      while ((name = va_arg (var_args, const gchar *)));
2566
2567
0
      object = g_object_new_internal (class, params, n_params);
2568
2569
0
      while (n_params--)
2570
0
        {
2571
          /* We open-code g_value_unset() here to avoid the
2572
           * cost of looking up the GTypeValueTable again.
2573
           */
2574
0
          if (vtabs[n_params]->value_free)
2575
0
            vtabs[n_params]->value_free (params[n_params].value);
2576
0
        }
2577
2578
0
      if (G_UNLIKELY (n_params_alloc != G_N_ELEMENTS (params_stack)))
2579
0
        {
2580
0
          g_free (params);
2581
0
          g_free (values);
2582
0
          g_free (vtabs);
2583
0
        }
2584
0
    }
2585
0
  else
2586
    /* Fast case: no properties passed in. */
2587
0
    object = g_object_new_internal (class, NULL, 0);
2588
2589
0
  if (unref_class)
2590
0
    g_type_class_unref (unref_class);
2591
2592
0
  return object;
2593
0
}
2594
2595
static GObject*
2596
g_object_constructor (GType                  type,
2597
          guint                  n_construct_properties,
2598
          GObjectConstructParam *construct_params)
2599
0
{
2600
0
  GObject *object;
2601
2602
  /* create object */
2603
0
  object = (GObject*) g_type_create_instance (type);
2604
  
2605
  /* set construction parameters */
2606
0
  if (n_construct_properties)
2607
0
    {
2608
0
      GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (object, FALSE);
2609
      
2610
      /* set construct properties */
2611
0
      while (n_construct_properties--)
2612
0
  {
2613
0
    GValue *value = construct_params->value;
2614
0
    GParamSpec *pspec = construct_params->pspec;
2615
2616
0
    construct_params++;
2617
0
    object_set_property (object, pspec, value, nqueue, TRUE);
2618
0
  }
2619
0
      g_object_notify_queue_thaw (object, nqueue);
2620
      /* the notification queue is still frozen from g_object_init(), so
2621
       * we don't need to handle it here, g_object_newv() takes
2622
       * care of that
2623
       */
2624
0
    }
2625
2626
0
  return object;
2627
0
}
2628
2629
static void
2630
g_object_constructed (GObject *object)
2631
0
{
2632
  /* empty default impl to allow unconditional upchaining */
2633
0
}
2634
2635
static inline gboolean
2636
g_object_set_is_valid_property (GObject         *object,
2637
                                GParamSpec      *pspec,
2638
                                const char      *property_name)
2639
0
{
2640
0
  if (G_UNLIKELY (pspec == NULL))
2641
0
    {
2642
0
      g_warning ("%s: object class '%s' has no property named '%s'",
2643
0
                 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2644
0
      return FALSE;
2645
0
    }
2646
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_WRITABLE)))
2647
0
    {
2648
0
      g_warning ("%s: property '%s' of object class '%s' is not writable",
2649
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2650
0
      return FALSE;
2651
0
    }
2652
0
  if (G_UNLIKELY (((pspec->flags & G_PARAM_CONSTRUCT_ONLY) && !object_in_construction (object))))
2653
0
    {
2654
0
      g_warning ("%s: construct property \"%s\" for object '%s' can't be set after construction",
2655
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2656
0
      return FALSE;
2657
0
    }
2658
0
  return TRUE;
2659
0
}
2660
2661
/**
2662
 * g_object_setv: (skip)
2663
 * @object: a #GObject
2664
 * @n_properties: the number of properties
2665
 * @names: (array length=n_properties): the names of each property to be set
2666
 * @values: (array length=n_properties): the values of each property to be set
2667
 *
2668
 * Sets @n_properties properties for an @object.
2669
 * Properties to be set will be taken from @values. All properties must be
2670
 * valid. Warnings will be emitted and undefined behaviour may result if invalid
2671
 * properties are passed in.
2672
 *
2673
 * Since: 2.54
2674
 */
2675
void
2676
g_object_setv (GObject       *object,
2677
               guint          n_properties,
2678
               const gchar   *names[],
2679
               const GValue   values[])
2680
0
{
2681
0
  guint i;
2682
0
  GObjectNotifyQueue *nqueue = NULL;
2683
0
  GParamSpec *pspec;
2684
0
  GObjectClass *class;
2685
2686
0
  g_return_if_fail (G_IS_OBJECT (object));
2687
2688
0
  if (n_properties == 0)
2689
0
    return;
2690
2691
0
  g_object_ref (object);
2692
2693
0
  class = G_OBJECT_GET_CLASS (object);
2694
2695
0
  if (_g_object_has_notify_handler (object))
2696
0
    nqueue = g_object_notify_queue_freeze (object, FALSE);
2697
2698
0
  for (i = 0; i < n_properties; i++)
2699
0
    {
2700
0
      pspec = find_pspec (class, names[i]);
2701
2702
0
      if (!g_object_set_is_valid_property (object, pspec, names[i]))
2703
0
        break;
2704
2705
0
      object_set_property (object, pspec, &values[i], nqueue, TRUE);
2706
0
    }
2707
2708
0
  if (nqueue)
2709
0
    g_object_notify_queue_thaw (object, nqueue);
2710
2711
0
  g_object_unref (object);
2712
0
}
2713
2714
/**
2715
 * g_object_set_valist: (skip)
2716
 * @object: a #GObject
2717
 * @first_property_name: name of the first property to set
2718
 * @var_args: value for the first property, followed optionally by more
2719
 *  name/value pairs, followed by %NULL
2720
 *
2721
 * Sets properties on an object.
2722
 */
2723
void
2724
g_object_set_valist (GObject   *object,
2725
         const gchar *first_property_name,
2726
         va_list    var_args)
2727
0
{
2728
0
  GObjectNotifyQueue *nqueue = NULL;
2729
0
  const gchar *name;
2730
0
  GObjectClass *class;
2731
  
2732
0
  g_return_if_fail (G_IS_OBJECT (object));
2733
2734
0
  g_object_ref (object);
2735
2736
0
  if (_g_object_has_notify_handler (object))
2737
0
    nqueue = g_object_notify_queue_freeze (object, FALSE);
2738
2739
0
  class = G_OBJECT_GET_CLASS (object);
2740
2741
0
  name = first_property_name;
2742
0
  while (name)
2743
0
    {
2744
0
      GValue value = G_VALUE_INIT;
2745
0
      GParamSpec *pspec;
2746
0
      gchar *error = NULL;
2747
0
      GTypeValueTable *vtab;
2748
      
2749
0
      pspec = find_pspec (class, name);
2750
2751
0
      if (!g_object_set_is_valid_property (object, pspec, name))
2752
0
        break;
2753
2754
0
      G_VALUE_COLLECT_INIT2 (&value, vtab, pspec->value_type, var_args, G_VALUE_NOCOPY_CONTENTS, &error);
2755
0
      if (error)
2756
0
  {
2757
0
    g_warning ("%s: %s", G_STRFUNC, error);
2758
0
    g_free (error);
2759
0
          g_value_unset (&value);
2760
0
    break;
2761
0
  }
2762
2763
0
      object_set_property (object, pspec, &value, nqueue, TRUE);
2764
2765
      /* We open-code g_value_unset() here to avoid the
2766
       * cost of looking up the GTypeValueTable again.
2767
       */
2768
0
      if (vtab->value_free)
2769
0
        vtab->value_free (&value);
2770
2771
0
      name = va_arg (var_args, gchar*);
2772
0
    }
2773
2774
0
  if (nqueue)
2775
0
    g_object_notify_queue_thaw (object, nqueue);
2776
2777
0
  g_object_unref (object);
2778
0
}
2779
2780
static inline gboolean
2781
g_object_get_is_valid_property (GObject          *object,
2782
                                GParamSpec       *pspec,
2783
                                const char       *property_name)
2784
0
{
2785
0
  if (G_UNLIKELY (pspec == NULL))
2786
0
    {
2787
0
      g_warning ("%s: object class '%s' has no property named '%s'",
2788
0
                 G_STRFUNC, G_OBJECT_TYPE_NAME (object), property_name);
2789
0
      return FALSE;
2790
0
    }
2791
0
  if (G_UNLIKELY (!(pspec->flags & G_PARAM_READABLE)))
2792
0
    {
2793
0
      g_warning ("%s: property '%s' of object class '%s' is not readable",
2794
0
                 G_STRFUNC, pspec->name, G_OBJECT_TYPE_NAME (object));
2795
0
      return FALSE;
2796
0
    }
2797
0
  return TRUE;
2798
0
}
2799
2800
/**
2801
 * g_object_getv:
2802
 * @object: a #GObject
2803
 * @n_properties: the number of properties
2804
 * @names: (array length=n_properties): the names of each property to get
2805
 * @values: (array length=n_properties): the values of each property to get
2806
 *
2807
 * Gets @n_properties properties for an @object.
2808
 * Obtained properties will be set to @values. All properties must be valid.
2809
 * Warnings will be emitted and undefined behaviour may result if invalid
2810
 * properties are passed in.
2811
 *
2812
 * Since: 2.54
2813
 */
2814
void
2815
g_object_getv (GObject      *object,
2816
               guint         n_properties,
2817
               const gchar  *names[],
2818
               GValue        values[])
2819
0
{
2820
0
  guint i;
2821
0
  GParamSpec *pspec;
2822
0
  GObjectClass *class;
2823
2824
0
  g_return_if_fail (G_IS_OBJECT (object));
2825
2826
0
  if (n_properties == 0)
2827
0
    return;
2828
2829
0
  g_object_ref (object);
2830
2831
0
  class = G_OBJECT_GET_CLASS (object);
2832
2833
0
  memset (values, 0, n_properties * sizeof (GValue));
2834
2835
0
  for (i = 0; i < n_properties; i++)
2836
0
    {
2837
0
      pspec = find_pspec (class, names[i]);
2838
2839
0
      if (!g_object_get_is_valid_property (object, pspec, names[i]))
2840
0
        break;
2841
0
      g_value_init (&values[i], pspec->value_type);
2842
0
      object_get_property (object, pspec, &values[i]);
2843
0
    }
2844
0
  g_object_unref (object);
2845
0
}
2846
2847
/**
2848
 * g_object_get_valist: (skip)
2849
 * @object: a #GObject
2850
 * @first_property_name: name of the first property to get
2851
 * @var_args: return location for the first property, followed optionally by more
2852
 *  name/return location pairs, followed by %NULL
2853
 *
2854
 * Gets properties of an object.
2855
 *
2856
 * In general, a copy is made of the property contents and the caller
2857
 * is responsible for freeing the memory in the appropriate manner for
2858
 * the type, for instance by calling g_free() or g_object_unref().
2859
 *
2860
 * See g_object_get().
2861
 */
2862
void
2863
g_object_get_valist (GObject   *object,
2864
         const gchar *first_property_name,
2865
         va_list    var_args)
2866
0
{
2867
0
  const gchar *name;
2868
0
  GObjectClass *class;
2869
  
2870
0
  g_return_if_fail (G_IS_OBJECT (object));
2871
  
2872
0
  g_object_ref (object);
2873
2874
0
  class = G_OBJECT_GET_CLASS (object);
2875
2876
0
  name = first_property_name;
2877
2878
0
  while (name)
2879
0
    {
2880
0
      GValue value = G_VALUE_INIT;
2881
0
      GParamSpec *pspec;
2882
0
      gchar *error;
2883
2884
0
      pspec = find_pspec (class, name);
2885
2886
0
      if (!g_object_get_is_valid_property (object, pspec, name))
2887
0
        break;
2888
      
2889
0
      g_value_init (&value, pspec->value_type);
2890
      
2891
0
      object_get_property (object, pspec, &value);
2892
      
2893
0
      G_VALUE_LCOPY (&value, var_args, 0, &error);
2894
0
      if (error)
2895
0
  {
2896
0
    g_warning ("%s: %s", G_STRFUNC, error);
2897
0
    g_free (error);
2898
0
    g_value_unset (&value);
2899
0
    break;
2900
0
  }
2901
      
2902
0
      g_value_unset (&value);
2903
      
2904
0
      name = va_arg (var_args, gchar*);
2905
0
    }
2906
  
2907
0
  g_object_unref (object);
2908
0
}
2909
2910
/**
2911
 * g_object_set: (skip)
2912
 * @object: (type GObject.Object): a #GObject
2913
 * @first_property_name: name of the first property to set
2914
 * @...: value for the first property, followed optionally by more
2915
 *  name/value pairs, followed by %NULL
2916
 *
2917
 * Sets properties on an object.
2918
 *
2919
 * The same caveats about passing integer literals as varargs apply as with
2920
 * g_object_new(). In particular, any integer literals set as the values for
2921
 * properties of type #gint64 or #guint64 must be 64 bits wide, using the
2922
 * %G_GINT64_CONSTANT or %G_GUINT64_CONSTANT macros.
2923
 *
2924
 * Note that the "notify" signals are queued and only emitted (in
2925
 * reverse order) after all properties have been set. See
2926
 * g_object_freeze_notify().
2927
 */
2928
void
2929
g_object_set (gpointer     _object,
2930
        const gchar *first_property_name,
2931
        ...)
2932
0
{
2933
0
  GObject *object = _object;
2934
0
  va_list var_args;
2935
  
2936
0
  g_return_if_fail (G_IS_OBJECT (object));
2937
  
2938
0
  va_start (var_args, first_property_name);
2939
0
  g_object_set_valist (object, first_property_name, var_args);
2940
0
  va_end (var_args);
2941
0
}
2942
2943
/**
2944
 * g_object_get: (skip)
2945
 * @object: (type GObject.Object): a #GObject
2946
 * @first_property_name: name of the first property to get
2947
 * @...: return location for the first property, followed optionally by more
2948
 *  name/return location pairs, followed by %NULL
2949
 *
2950
 * Gets properties of an object.
2951
 *
2952
 * In general, a copy is made of the property contents and the caller
2953
 * is responsible for freeing the memory in the appropriate manner for
2954
 * the type, for instance by calling g_free() or g_object_unref().
2955
 *
2956
 * Here is an example of using g_object_get() to get the contents
2957
 * of three properties: an integer, a string and an object:
2958
 * |[<!-- language="C" --> 
2959
 *  gint intval;
2960
 *  guint64 uint64val;
2961
 *  gchar *strval;
2962
 *  GObject *objval;
2963
 *
2964
 *  g_object_get (my_object,
2965
 *                "int-property", &intval,
2966
 *                "uint64-property", &uint64val,
2967
 *                "str-property", &strval,
2968
 *                "obj-property", &objval,
2969
 *                NULL);
2970
 *
2971
 *  // Do something with intval, uint64val, strval, objval
2972
 *
2973
 *  g_free (strval);
2974
 *  g_object_unref (objval);
2975
 * ]|
2976
 */
2977
void
2978
g_object_get (gpointer     _object,
2979
        const gchar *first_property_name,
2980
        ...)
2981
0
{
2982
0
  GObject *object = _object;
2983
0
  va_list var_args;
2984
  
2985
0
  g_return_if_fail (G_IS_OBJECT (object));
2986
  
2987
0
  va_start (var_args, first_property_name);
2988
0
  g_object_get_valist (object, first_property_name, var_args);
2989
0
  va_end (var_args);
2990
0
}
2991
2992
/**
2993
 * g_object_set_property:
2994
 * @object: a #GObject
2995
 * @property_name: the name of the property to set
2996
 * @value: the value
2997
 *
2998
 * Sets a property on an object.
2999
 */
3000
void
3001
g_object_set_property (GObject      *object,
3002
           const gchar  *property_name,
3003
           const GValue *value)
3004
0
{
3005
0
  g_object_setv (object, 1, &property_name, value);
3006
0
}
3007
3008
/**
3009
 * g_object_get_property:
3010
 * @object: a #GObject
3011
 * @property_name: the name of the property to get
3012
 * @value: return location for the property value
3013
 *
3014
 * Gets a property of an object.
3015
 *
3016
 * The @value can be:
3017
 *
3018
 *  - an empty #GValue initialized by %G_VALUE_INIT, which will be
3019
 *    automatically initialized with the expected type of the property
3020
 *    (since GLib 2.60)
3021
 *  - a #GValue initialized with the expected type of the property
3022
 *  - a #GValue initialized with a type to which the expected type
3023
 *    of the property can be transformed
3024
 *
3025
 * In general, a copy is made of the property contents and the caller is
3026
 * responsible for freeing the memory by calling g_value_unset().
3027
 *
3028
 * Note that g_object_get_property() is really intended for language
3029
 * bindings, g_object_get() is much more convenient for C programming.
3030
 */
3031
void
3032
g_object_get_property (GObject     *object,
3033
           const gchar *property_name,
3034
           GValue    *value)
3035
0
{
3036
0
  GParamSpec *pspec;
3037
  
3038
0
  g_return_if_fail (G_IS_OBJECT (object));
3039
0
  g_return_if_fail (property_name != NULL);
3040
0
  g_return_if_fail (value != NULL);
3041
  
3042
0
  g_object_ref (object);
3043
  
3044
0
  pspec = find_pspec (G_OBJECT_GET_CLASS (object), property_name);
3045
3046
0
  if (g_object_get_is_valid_property (object, pspec, property_name))
3047
0
    {
3048
0
      GValue *prop_value, tmp_value = G_VALUE_INIT;
3049
      
3050
0
      if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
3051
0
        {
3052
          /* zero-initialized value */
3053
0
          g_value_init (value, pspec->value_type);
3054
0
          prop_value = value;
3055
0
        }
3056
0
      else if (G_VALUE_TYPE (value) == pspec->value_type)
3057
0
        {
3058
          /* auto-conversion of the callers value type */
3059
0
          g_value_reset (value);
3060
0
          prop_value = value;
3061
0
        }
3062
0
      else if (!g_value_type_transformable (pspec->value_type, G_VALUE_TYPE (value)))
3063
0
        {
3064
0
          g_warning ("%s: can't retrieve property '%s' of type '%s' as value of type '%s'",
3065
0
                     G_STRFUNC, pspec->name,
3066
0
                     g_type_name (pspec->value_type),
3067
0
                     G_VALUE_TYPE_NAME (value));
3068
0
          g_object_unref (object);
3069
0
          return;
3070
0
        }
3071
0
      else
3072
0
        {
3073
0
          g_value_init (&tmp_value, pspec->value_type);
3074
0
          prop_value = &tmp_value;
3075
0
        }
3076
0
      object_get_property (object, pspec, prop_value);
3077
0
      if (prop_value != value)
3078
0
        {
3079
0
          g_value_transform (prop_value, value);
3080
0
          g_value_unset (&tmp_value);
3081
0
        }
3082
0
    }
3083
  
3084
0
  g_object_unref (object);
3085
0
}
3086
3087
/**
3088
 * g_object_connect: (skip)
3089
 * @object: (type GObject.Object): a #GObject
3090
 * @signal_spec: the spec for the first signal
3091
 * @...: #GCallback for the first signal, followed by data for the
3092
 *       first signal, followed optionally by more signal
3093
 *       spec/callback/data triples, followed by %NULL
3094
 *
3095
 * A convenience function to connect multiple signals at once.
3096
 *
3097
 * The signal specs expected by this function have the form
3098
 * "modifier::signal_name", where modifier can be one of the following:
3099
 * - signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_DEFAULT)
3100
 * - object-signal, object_signal: equivalent to g_signal_connect_object (..., G_CONNECT_DEFAULT)
3101
 * - swapped-signal, swapped_signal: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED)
3102
 * - swapped_object_signal, swapped-object-signal: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED)
3103
 * - signal_after, signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_AFTER)
3104
 * - object_signal_after, object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_AFTER)
3105
 * - swapped_signal_after, swapped-signal-after: equivalent to g_signal_connect_data (..., NULL, G_CONNECT_SWAPPED | G_CONNECT_AFTER)
3106
 * - swapped_object_signal_after, swapped-object-signal-after: equivalent to g_signal_connect_object (..., G_CONNECT_SWAPPED | G_CONNECT_AFTER)
3107
 *
3108
 * |[<!-- language="C" --> 
3109
 *   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
3110
 *               "type", GTK_WINDOW_POPUP,
3111
 *               "child", menu,
3112
 *               NULL),
3113
 *             "signal::event", gtk_menu_window_event, menu,
3114
 *             "signal::size_request", gtk_menu_window_size_request, menu,
3115
 *             "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
3116
 *             NULL);
3117
 * ]|
3118
 *
3119
 * Returns: (transfer none) (type GObject.Object): @object
3120
 */
3121
gpointer
3122
g_object_connect (gpointer     _object,
3123
      const gchar *signal_spec,
3124
      ...)
3125
0
{
3126
0
  GObject *object = _object;
3127
0
  va_list var_args;
3128
3129
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3130
0
  g_return_val_if_fail (object->ref_count > 0, object);
3131
3132
0
  va_start (var_args, signal_spec);
3133
0
  while (signal_spec)
3134
0
    {
3135
0
      GCallback callback = va_arg (var_args, GCallback);
3136
0
      gpointer data = va_arg (var_args, gpointer);
3137
3138
0
      if (strncmp (signal_spec, "signal::", 8) == 0)
3139
0
  g_signal_connect_data (object, signal_spec + 8,
3140
0
             callback, data, NULL,
3141
0
             G_CONNECT_DEFAULT);
3142
0
      else if (strncmp (signal_spec, "object_signal::", 15) == 0 ||
3143
0
               strncmp (signal_spec, "object-signal::", 15) == 0)
3144
0
  g_signal_connect_object (object, signal_spec + 15,
3145
0
         callback, data,
3146
0
         G_CONNECT_DEFAULT);
3147
0
      else if (strncmp (signal_spec, "swapped_signal::", 16) == 0 ||
3148
0
               strncmp (signal_spec, "swapped-signal::", 16) == 0)
3149
0
  g_signal_connect_data (object, signal_spec + 16,
3150
0
             callback, data, NULL,
3151
0
             G_CONNECT_SWAPPED);
3152
0
      else if (strncmp (signal_spec, "swapped_object_signal::", 23) == 0 ||
3153
0
               strncmp (signal_spec, "swapped-object-signal::", 23) == 0)
3154
0
  g_signal_connect_object (object, signal_spec + 23,
3155
0
         callback, data,
3156
0
         G_CONNECT_SWAPPED);
3157
0
      else if (strncmp (signal_spec, "signal_after::", 14) == 0 ||
3158
0
               strncmp (signal_spec, "signal-after::", 14) == 0)
3159
0
  g_signal_connect_data (object, signal_spec + 14,
3160
0
             callback, data, NULL,
3161
0
             G_CONNECT_AFTER);
3162
0
      else if (strncmp (signal_spec, "object_signal_after::", 21) == 0 ||
3163
0
               strncmp (signal_spec, "object-signal-after::", 21) == 0)
3164
0
  g_signal_connect_object (object, signal_spec + 21,
3165
0
         callback, data,
3166
0
         G_CONNECT_AFTER);
3167
0
      else if (strncmp (signal_spec, "swapped_signal_after::", 22) == 0 ||
3168
0
               strncmp (signal_spec, "swapped-signal-after::", 22) == 0)
3169
0
  g_signal_connect_data (object, signal_spec + 22,
3170
0
             callback, data, NULL,
3171
0
             G_CONNECT_SWAPPED | G_CONNECT_AFTER);
3172
0
      else if (strncmp (signal_spec, "swapped_object_signal_after::", 29) == 0 ||
3173
0
               strncmp (signal_spec, "swapped-object-signal-after::", 29) == 0)
3174
0
  g_signal_connect_object (object, signal_spec + 29,
3175
0
         callback, data,
3176
0
         G_CONNECT_SWAPPED | G_CONNECT_AFTER);
3177
0
      else
3178
0
  {
3179
0
    g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
3180
0
    break;
3181
0
  }
3182
0
      signal_spec = va_arg (var_args, gchar*);
3183
0
    }
3184
0
  va_end (var_args);
3185
3186
0
  return object;
3187
0
}
3188
3189
/**
3190
 * g_object_disconnect: (skip)
3191
 * @object: (type GObject.Object): a #GObject
3192
 * @signal_spec: the spec for the first signal
3193
 * @...: #GCallback for the first signal, followed by data for the first signal,
3194
 *  followed optionally by more signal spec/callback/data triples,
3195
 *  followed by %NULL
3196
 *
3197
 * A convenience function to disconnect multiple signals at once.
3198
 *
3199
 * The signal specs expected by this function have the form
3200
 * "any_signal", which means to disconnect any signal with matching
3201
 * callback and data, or "any_signal::signal_name", which only
3202
 * disconnects the signal named "signal_name".
3203
 */
3204
void
3205
g_object_disconnect (gpointer     _object,
3206
         const gchar *signal_spec,
3207
         ...)
3208
0
{
3209
0
  GObject *object = _object;
3210
0
  va_list var_args;
3211
3212
0
  g_return_if_fail (G_IS_OBJECT (object));
3213
0
  g_return_if_fail (object->ref_count > 0);
3214
3215
0
  va_start (var_args, signal_spec);
3216
0
  while (signal_spec)
3217
0
    {
3218
0
      GCallback callback = va_arg (var_args, GCallback);
3219
0
      gpointer data = va_arg (var_args, gpointer);
3220
0
      guint sid = 0, detail = 0, mask = 0;
3221
3222
0
      if (strncmp (signal_spec, "any_signal::", 12) == 0 ||
3223
0
          strncmp (signal_spec, "any-signal::", 12) == 0)
3224
0
  {
3225
0
    signal_spec += 12;
3226
0
    mask = G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
3227
0
  }
3228
0
      else if (strcmp (signal_spec, "any_signal") == 0 ||
3229
0
               strcmp (signal_spec, "any-signal") == 0)
3230
0
  {
3231
0
    signal_spec += 10;
3232
0
    mask = G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA;
3233
0
  }
3234
0
      else
3235
0
  {
3236
0
    g_warning ("%s: invalid signal spec \"%s\"", G_STRFUNC, signal_spec);
3237
0
    break;
3238
0
  }
3239
3240
0
      if ((mask & G_SIGNAL_MATCH_ID) &&
3241
0
    !g_signal_parse_name (signal_spec, G_OBJECT_TYPE (object), &sid, &detail, FALSE))
3242
0
  g_warning ("%s: invalid signal name \"%s\"", G_STRFUNC, signal_spec);
3243
0
      else if (!g_signal_handlers_disconnect_matched (object, mask | (detail ? G_SIGNAL_MATCH_DETAIL : 0),
3244
0
                  sid, detail,
3245
0
                  NULL, (gpointer)callback, data))
3246
0
  g_warning ("%s: signal handler %p(%p) is not connected", G_STRFUNC, callback, data);
3247
0
      signal_spec = va_arg (var_args, gchar*);
3248
0
    }
3249
0
  va_end (var_args);
3250
0
}
3251
3252
typedef struct {
3253
  GObject *object;
3254
  guint n_weak_refs;
3255
  struct {
3256
    GWeakNotify notify;
3257
    gpointer    data;
3258
  } weak_refs[1];  /* flexible array */
3259
} WeakRefStack;
3260
3261
static void
3262
weak_refs_notify (gpointer data)
3263
0
{
3264
0
  WeakRefStack *wstack = data;
3265
0
  guint i;
3266
3267
0
  for (i = 0; i < wstack->n_weak_refs; i++)
3268
0
    wstack->weak_refs[i].notify (wstack->weak_refs[i].data, wstack->object);
3269
0
  g_free (wstack);
3270
0
}
3271
3272
/**
3273
 * g_object_weak_ref: (skip)
3274
 * @object: #GObject to reference weakly
3275
 * @notify: callback to invoke before the object is freed
3276
 * @data: extra data to pass to notify
3277
 *
3278
 * Adds a weak reference callback to an object. Weak references are
3279
 * used for notification when an object is disposed. They are called
3280
 * "weak references" because they allow you to safely hold a pointer
3281
 * to an object without calling g_object_ref() (g_object_ref() adds a
3282
 * strong reference, that is, forces the object to stay alive).
3283
 *
3284
 * Note that the weak references created by this method are not
3285
 * thread-safe: they cannot safely be used in one thread if the
3286
 * object's last g_object_unref() might happen in another thread.
3287
 * Use #GWeakRef if thread-safety is required.
3288
 */
3289
void
3290
g_object_weak_ref (GObject    *object,
3291
       GWeakNotify notify,
3292
       gpointer    data)
3293
0
{
3294
0
  WeakRefStack *wstack;
3295
0
  guint i;
3296
  
3297
0
  g_return_if_fail (G_IS_OBJECT (object));
3298
0
  g_return_if_fail (notify != NULL);
3299
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3300
3301
0
  G_LOCK (weak_refs_mutex);
3302
0
  wstack = g_datalist_id_remove_no_notify (&object->qdata, quark_weak_refs);
3303
0
  if (wstack)
3304
0
    {
3305
0
      i = wstack->n_weak_refs++;
3306
0
      wstack = g_realloc (wstack, sizeof (*wstack) + sizeof (wstack->weak_refs[0]) * i);
3307
0
    }
3308
0
  else
3309
0
    {
3310
0
      wstack = g_renew (WeakRefStack, NULL, 1);
3311
0
      wstack->object = object;
3312
0
      wstack->n_weak_refs = 1;
3313
0
      i = 0;
3314
0
    }
3315
0
  wstack->weak_refs[i].notify = notify;
3316
0
  wstack->weak_refs[i].data = data;
3317
0
  g_datalist_id_set_data_full (&object->qdata, quark_weak_refs, wstack, weak_refs_notify);
3318
0
  G_UNLOCK (weak_refs_mutex);
3319
0
}
3320
3321
/**
3322
 * g_object_weak_unref: (skip)
3323
 * @object: #GObject to remove a weak reference from
3324
 * @notify: callback to search for
3325
 * @data: data to search for
3326
 *
3327
 * Removes a weak reference callback to an object.
3328
 */
3329
void
3330
g_object_weak_unref (GObject    *object,
3331
         GWeakNotify notify,
3332
         gpointer    data)
3333
0
{
3334
0
  WeakRefStack *wstack;
3335
0
  gboolean found_one = FALSE;
3336
3337
0
  g_return_if_fail (G_IS_OBJECT (object));
3338
0
  g_return_if_fail (notify != NULL);
3339
3340
0
  G_LOCK (weak_refs_mutex);
3341
0
  wstack = g_datalist_id_get_data (&object->qdata, quark_weak_refs);
3342
0
  if (wstack)
3343
0
    {
3344
0
      guint i;
3345
3346
0
      for (i = 0; i < wstack->n_weak_refs; i++)
3347
0
  if (wstack->weak_refs[i].notify == notify &&
3348
0
      wstack->weak_refs[i].data == data)
3349
0
    {
3350
0
      found_one = TRUE;
3351
0
      wstack->n_weak_refs -= 1;
3352
0
      if (i != wstack->n_weak_refs)
3353
0
        wstack->weak_refs[i] = wstack->weak_refs[wstack->n_weak_refs];
3354
3355
0
      break;
3356
0
    }
3357
0
    }
3358
0
  G_UNLOCK (weak_refs_mutex);
3359
0
  if (!found_one)
3360
0
    g_warning ("%s: couldn't find weak ref %p(%p)", G_STRFUNC, notify, data);
3361
0
}
3362
3363
/**
3364
 * g_object_add_weak_pointer: (skip)
3365
 * @object: The object that should be weak referenced.
3366
 * @weak_pointer_location: (inout) (not optional): The memory address
3367
 *    of a pointer.
3368
 *
3369
 * Adds a weak reference from weak_pointer to @object to indicate that
3370
 * the pointer located at @weak_pointer_location is only valid during
3371
 * the lifetime of @object. When the @object is finalized,
3372
 * @weak_pointer will be set to %NULL.
3373
 *
3374
 * Note that as with g_object_weak_ref(), the weak references created by
3375
 * this method are not thread-safe: they cannot safely be used in one
3376
 * thread if the object's last g_object_unref() might happen in another
3377
 * thread. Use #GWeakRef if thread-safety is required.
3378
 */
3379
void
3380
g_object_add_weak_pointer (GObject  *object, 
3381
                           gpointer *weak_pointer_location)
3382
0
{
3383
0
  g_return_if_fail (G_IS_OBJECT (object));
3384
0
  g_return_if_fail (weak_pointer_location != NULL);
3385
3386
0
  g_object_weak_ref (object, 
3387
0
                     (GWeakNotify) g_nullify_pointer, 
3388
0
                     weak_pointer_location);
3389
0
}
3390
3391
/**
3392
 * g_object_remove_weak_pointer: (skip)
3393
 * @object: The object that is weak referenced.
3394
 * @weak_pointer_location: (inout) (not optional): The memory address
3395
 *    of a pointer.
3396
 *
3397
 * Removes a weak reference from @object that was previously added
3398
 * using g_object_add_weak_pointer(). The @weak_pointer_location has
3399
 * to match the one used with g_object_add_weak_pointer().
3400
 */
3401
void
3402
g_object_remove_weak_pointer (GObject  *object, 
3403
                              gpointer *weak_pointer_location)
3404
0
{
3405
0
  g_return_if_fail (G_IS_OBJECT (object));
3406
0
  g_return_if_fail (weak_pointer_location != NULL);
3407
3408
0
  g_object_weak_unref (object, 
3409
0
                       (GWeakNotify) g_nullify_pointer, 
3410
0
                       weak_pointer_location);
3411
0
}
3412
3413
static guint
3414
object_floating_flag_handler (GObject        *object,
3415
                              gint            job)
3416
0
{
3417
0
  switch (job)
3418
0
    {
3419
0
      gpointer oldvalue;
3420
0
    case +1:    /* force floating if possible */
3421
0
      do
3422
0
        oldvalue = g_atomic_pointer_get (&object->qdata);
3423
0
      while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
3424
0
                                                     (gpointer) ((gsize) oldvalue | OBJECT_FLOATING_FLAG)));
3425
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
3426
0
    case -1:    /* sink if possible */
3427
0
      do
3428
0
        oldvalue = g_atomic_pointer_get (&object->qdata);
3429
0
      while (!g_atomic_pointer_compare_and_exchange ((void**) &object->qdata, oldvalue,
3430
0
                                                     (gpointer) ((gsize) oldvalue & ~(gsize) OBJECT_FLOATING_FLAG)));
3431
0
      return (gsize) oldvalue & OBJECT_FLOATING_FLAG;
3432
0
    default:    /* check floating */
3433
0
      return 0 != ((gsize) g_atomic_pointer_get (&object->qdata) & OBJECT_FLOATING_FLAG);
3434
0
    }
3435
0
}
3436
3437
/**
3438
 * g_object_is_floating:
3439
 * @object: (type GObject.Object): a #GObject
3440
 *
3441
 * Checks whether @object has a [floating][floating-ref] reference.
3442
 *
3443
 * Since: 2.10
3444
 *
3445
 * Returns: %TRUE if @object has a floating reference
3446
 */
3447
gboolean
3448
g_object_is_floating (gpointer _object)
3449
0
{
3450
0
  GObject *object = _object;
3451
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
3452
0
  return floating_flag_handler (object, 0);
3453
0
}
3454
3455
/**
3456
 * g_object_ref_sink:
3457
 * @object: (type GObject.Object): a #GObject
3458
 *
3459
 * Increase the reference count of @object, and possibly remove the
3460
 * [floating][floating-ref] reference, if @object has a floating reference.
3461
 *
3462
 * In other words, if the object is floating, then this call "assumes
3463
 * ownership" of the floating reference, converting it to a normal
3464
 * reference by clearing the floating flag while leaving the reference
3465
 * count unchanged.  If the object is not floating, then this call
3466
 * adds a new normal reference increasing the reference count by one.
3467
 *
3468
 * Since GLib 2.56, the type of @object will be propagated to the return type
3469
 * under the same conditions as for g_object_ref().
3470
 *
3471
 * Since: 2.10
3472
 *
3473
 * Returns: (type GObject.Object) (transfer none): @object
3474
 */
3475
gpointer
3476
(g_object_ref_sink) (gpointer _object)
3477
0
{
3478
0
  GObject *object = _object;
3479
0
  gboolean was_floating;
3480
0
  g_return_val_if_fail (G_IS_OBJECT (object), object);
3481
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object);
3482
0
  g_object_ref (object);
3483
0
  was_floating = floating_flag_handler (object, -1);
3484
0
  if (was_floating)
3485
0
    g_object_unref (object);
3486
0
  return object;
3487
0
}
3488
3489
/**
3490
 * g_object_take_ref: (skip)
3491
 * @object: (type GObject.Object): a #GObject
3492
 *
3493
 * If @object is floating, sink it.  Otherwise, do nothing.
3494
 *
3495
 * In other words, this function will convert a floating reference (if
3496
 * present) into a full reference.
3497
 *
3498
 * Typically you want to use g_object_ref_sink() in order to
3499
 * automatically do the correct thing with respect to floating or
3500
 * non-floating references, but there is one specific scenario where
3501
 * this function is helpful.
3502
 *
3503
 * The situation where this function is helpful is when creating an API
3504
 * that allows the user to provide a callback function that returns a
3505
 * GObject. We certainly want to allow the user the flexibility to
3506
 * return a non-floating reference from this callback (for the case
3507
 * where the object that is being returned already exists).
3508
 *
3509
 * At the same time, the API style of some popular GObject-based
3510
 * libraries (such as Gtk) make it likely that for newly-created GObject
3511
 * instances, the user can be saved some typing if they are allowed to
3512
 * return a floating reference.
3513
 *
3514
 * Using this function on the return value of the user's callback allows
3515
 * the user to do whichever is more convenient for them. The caller will
3516
 * alway receives exactly one full reference to the value: either the
3517
 * one that was returned in the first place, or a floating reference
3518
 * that has been converted to a full reference.
3519
 *
3520
 * This function has an odd interaction when combined with
3521
 * g_object_ref_sink() running at the same time in another thread on
3522
 * the same #GObject instance. If g_object_ref_sink() runs first then
3523
 * the result will be that the floating reference is converted to a hard
3524
 * reference. If g_object_take_ref() runs first then the result will be
3525
 * that the floating reference is converted to a hard reference and an
3526
 * additional reference on top of that one is added. It is best to avoid
3527
 * this situation.
3528
 *
3529
 * Since: 2.70
3530
 *
3531
 * Returns: (type GObject.Object) (transfer full): @object
3532
 */
3533
gpointer
3534
g_object_take_ref (gpointer _object)
3535
0
{
3536
0
  GObject *object = _object;
3537
0
  g_return_val_if_fail (G_IS_OBJECT (object), object);
3538
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) >= 1, object);
3539
3540
0
  floating_flag_handler (object, -1);
3541
3542
0
  return object;
3543
0
}
3544
3545
/**
3546
 * g_object_force_floating:
3547
 * @object: a #GObject
3548
 *
3549
 * This function is intended for #GObject implementations to re-enforce
3550
 * a [floating][floating-ref] object reference. Doing this is seldom
3551
 * required: all #GInitiallyUnowneds are created with a floating reference
3552
 * which usually just needs to be sunken by calling g_object_ref_sink().
3553
 *
3554
 * Since: 2.10
3555
 */
3556
void
3557
g_object_force_floating (GObject *object)
3558
0
{
3559
0
  g_return_if_fail (G_IS_OBJECT (object));
3560
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3561
3562
0
  floating_flag_handler (object, +1);
3563
0
}
3564
3565
typedef struct {
3566
  GObject *object;
3567
  guint n_toggle_refs;
3568
  struct {
3569
    GToggleNotify notify;
3570
    gpointer    data;
3571
  } toggle_refs[1];  /* flexible array */
3572
} ToggleRefStack;
3573
3574
static void
3575
toggle_refs_notify (GObject *object,
3576
        gboolean is_last_ref)
3577
0
{
3578
0
  ToggleRefStack tstack, *tstackptr;
3579
3580
0
  G_LOCK (toggle_refs_mutex);
3581
  /* If another thread removed the toggle reference on the object, while
3582
   * we were waiting here, there's nothing to notify.
3583
   * So let's check again if the object has toggle reference and in case return.
3584
   */
3585
0
  if (!OBJECT_HAS_TOGGLE_REF (object))
3586
0
    {
3587
0
      G_UNLOCK (toggle_refs_mutex);
3588
0
      return;
3589
0
    }
3590
3591
0
  tstackptr = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3592
0
  tstack = *tstackptr;
3593
0
  G_UNLOCK (toggle_refs_mutex);
3594
3595
  /* Reentrancy here is not as tricky as it seems, because a toggle reference
3596
   * will only be notified when there is exactly one of them.
3597
   */
3598
0
  g_assert (tstack.n_toggle_refs == 1);
3599
0
  tstack.toggle_refs[0].notify (tstack.toggle_refs[0].data, tstack.object, is_last_ref);
3600
0
}
3601
3602
/**
3603
 * g_object_add_toggle_ref: (skip)
3604
 * @object: a #GObject
3605
 * @notify: a function to call when this reference is the
3606
 *  last reference to the object, or is no longer
3607
 *  the last reference.
3608
 * @data: data to pass to @notify
3609
 *
3610
 * Increases the reference count of the object by one and sets a
3611
 * callback to be called when all other references to the object are
3612
 * dropped, or when this is already the last reference to the object
3613
 * and another reference is established.
3614
 *
3615
 * This functionality is intended for binding @object to a proxy
3616
 * object managed by another memory manager. This is done with two
3617
 * paired references: the strong reference added by
3618
 * g_object_add_toggle_ref() and a reverse reference to the proxy
3619
 * object which is either a strong reference or weak reference.
3620
 *
3621
 * The setup is that when there are no other references to @object,
3622
 * only a weak reference is held in the reverse direction from @object
3623
 * to the proxy object, but when there are other references held to
3624
 * @object, a strong reference is held. The @notify callback is called
3625
 * when the reference from @object to the proxy object should be
3626
 * "toggled" from strong to weak (@is_last_ref true) or weak to strong
3627
 * (@is_last_ref false).
3628
 *
3629
 * Since a (normal) reference must be held to the object before
3630
 * calling g_object_add_toggle_ref(), the initial state of the reverse
3631
 * link is always strong.
3632
 *
3633
 * Multiple toggle references may be added to the same gobject,
3634
 * however if there are multiple toggle references to an object, none
3635
 * of them will ever be notified until all but one are removed.  For
3636
 * this reason, you should only ever use a toggle reference if there
3637
 * is important state in the proxy object.
3638
 *
3639
 * Since: 2.8
3640
 */
3641
void
3642
g_object_add_toggle_ref (GObject       *object,
3643
       GToggleNotify  notify,
3644
       gpointer       data)
3645
0
{
3646
0
  ToggleRefStack *tstack;
3647
0
  guint i;
3648
  
3649
0
  g_return_if_fail (G_IS_OBJECT (object));
3650
0
  g_return_if_fail (notify != NULL);
3651
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) >= 1);
3652
3653
0
  g_object_ref (object);
3654
3655
0
  G_LOCK (toggle_refs_mutex);
3656
0
  tstack = g_datalist_id_remove_no_notify (&object->qdata, quark_toggle_refs);
3657
0
  if (tstack)
3658
0
    {
3659
0
      i = tstack->n_toggle_refs++;
3660
      /* allocate i = tstate->n_toggle_refs - 1 positions beyond the 1 declared
3661
       * in tstate->toggle_refs */
3662
0
      tstack = g_realloc (tstack, sizeof (*tstack) + sizeof (tstack->toggle_refs[0]) * i);
3663
0
    }
3664
0
  else
3665
0
    {
3666
0
      tstack = g_renew (ToggleRefStack, NULL, 1);
3667
0
      tstack->object = object;
3668
0
      tstack->n_toggle_refs = 1;
3669
0
      i = 0;
3670
0
    }
3671
3672
  /* Set a flag for fast lookup after adding the first toggle reference */
3673
0
  if (tstack->n_toggle_refs == 1)
3674
0
    g_datalist_set_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3675
  
3676
0
  tstack->toggle_refs[i].notify = notify;
3677
0
  tstack->toggle_refs[i].data = data;
3678
0
  g_datalist_id_set_data_full (&object->qdata, quark_toggle_refs, tstack,
3679
0
             (GDestroyNotify)g_free);
3680
0
  G_UNLOCK (toggle_refs_mutex);
3681
0
}
3682
3683
/**
3684
 * g_object_remove_toggle_ref: (skip)
3685
 * @object: a #GObject
3686
 * @notify: a function to call when this reference is the
3687
 *  last reference to the object, or is no longer
3688
 *  the last reference.
3689
 * @data: (nullable): data to pass to @notify, or %NULL to
3690
 *  match any toggle refs with the @notify argument.
3691
 *
3692
 * Removes a reference added with g_object_add_toggle_ref(). The
3693
 * reference count of the object is decreased by one.
3694
 *
3695
 * Since: 2.8
3696
 */
3697
void
3698
g_object_remove_toggle_ref (GObject       *object,
3699
          GToggleNotify  notify,
3700
          gpointer       data)
3701
0
{
3702
0
  ToggleRefStack *tstack;
3703
0
  gboolean found_one = FALSE;
3704
3705
0
  g_return_if_fail (G_IS_OBJECT (object));
3706
0
  g_return_if_fail (notify != NULL);
3707
3708
0
  G_LOCK (toggle_refs_mutex);
3709
0
  tstack = g_datalist_id_get_data (&object->qdata, quark_toggle_refs);
3710
0
  if (tstack)
3711
0
    {
3712
0
      guint i;
3713
3714
0
      for (i = 0; i < tstack->n_toggle_refs; i++)
3715
0
  if (tstack->toggle_refs[i].notify == notify &&
3716
0
      (tstack->toggle_refs[i].data == data || data == NULL))
3717
0
    {
3718
0
      found_one = TRUE;
3719
0
      tstack->n_toggle_refs -= 1;
3720
0
      if (i != tstack->n_toggle_refs)
3721
0
        tstack->toggle_refs[i] = tstack->toggle_refs[tstack->n_toggle_refs];
3722
3723
0
      if (tstack->n_toggle_refs == 0)
3724
0
        g_datalist_unset_flags (&object->qdata, OBJECT_HAS_TOGGLE_REF_FLAG);
3725
3726
0
      break;
3727
0
    }
3728
0
    }
3729
0
  G_UNLOCK (toggle_refs_mutex);
3730
3731
0
  if (found_one)
3732
0
    g_object_unref (object);
3733
0
  else
3734
0
    g_warning ("%s: couldn't find toggle ref %p(%p)", G_STRFUNC, notify, data);
3735
0
}
3736
3737
/**
3738
 * g_object_ref:
3739
 * @object: (type GObject.Object): a #GObject
3740
 *
3741
 * Increases the reference count of @object.
3742
 *
3743
 * Since GLib 2.56, if `GLIB_VERSION_MAX_ALLOWED` is 2.56 or greater, the type
3744
 * of @object will be propagated to the return type (using the GCC typeof()
3745
 * extension), so any casting the caller needs to do on the return type must be
3746
 * explicit.
3747
 *
3748
 * Returns: (type GObject.Object) (transfer none): the same @object
3749
 */
3750
gpointer
3751
(g_object_ref) (gpointer _object)
3752
0
{
3753
0
  GObject *object = _object;
3754
0
  gint old_val;
3755
0
  gboolean object_already_finalized;
3756
3757
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3758
  
3759
0
  old_val = g_atomic_int_add (&object->ref_count, 1);
3760
0
  object_already_finalized = (old_val <= 0);
3761
0
  g_return_val_if_fail (!object_already_finalized, NULL);
3762
3763
0
  if (old_val == 1 && OBJECT_HAS_TOGGLE_REF (object))
3764
0
    toggle_refs_notify (object, FALSE);
3765
3766
0
  TRACE (GOBJECT_OBJECT_REF(object,G_TYPE_FROM_INSTANCE(object),old_val));
3767
3768
0
  return object;
3769
0
}
3770
3771
/**
3772
 * g_object_unref:
3773
 * @object: (type GObject.Object): a #GObject
3774
 *
3775
 * Decreases the reference count of @object. When its reference count
3776
 * drops to 0, the object is finalized (i.e. its memory is freed).
3777
 *
3778
 * If the pointer to the #GObject may be reused in future (for example, if it is
3779
 * an instance variable of another object), it is recommended to clear the
3780
 * pointer to %NULL rather than retain a dangling pointer to a potentially
3781
 * invalid #GObject instance. Use g_clear_object() for this.
3782
 */
3783
void
3784
g_object_unref (gpointer _object)
3785
0
{
3786
0
  GObject *object = _object;
3787
0
  gint old_ref;
3788
  
3789
0
  g_return_if_fail (G_IS_OBJECT (object));
3790
  
3791
  /* here we want to atomically do: if (ref_count>1) { ref_count--; return; } */
3792
0
 retry_atomic_decrement1:
3793
0
  old_ref = g_atomic_int_get (&object->ref_count);
3794
0
  if (old_ref > 1)
3795
0
    {
3796
      /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3797
0
      gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3798
3799
0
      if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3800
0
  goto retry_atomic_decrement1;
3801
3802
0
      TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3803
3804
      /* if we went from 2->1 we need to notify toggle refs if any */
3805
0
      if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3806
0
  toggle_refs_notify (object, TRUE);
3807
0
    }
3808
0
  else
3809
0
    {
3810
0
      GSList **weak_locations;
3811
0
      GObjectNotifyQueue *nqueue;
3812
3813
      /* The only way that this object can live at this point is if
3814
       * there are outstanding weak references already established
3815
       * before we got here.
3816
       *
3817
       * If there were not already weak references then no more can be
3818
       * established at this time, because the other thread would have
3819
       * to hold a strong ref in order to call
3820
       * g_object_add_weak_pointer() and then we wouldn't be here.
3821
       *
3822
       * Other GWeakRef's (weak locations) instead may still be added
3823
       * before the object is finalized, but in such case we'll unset
3824
       * them as part of the qdata removal.
3825
       */
3826
0
      weak_locations = g_datalist_id_get_data (&object->qdata, quark_weak_locations);
3827
3828
0
      if (weak_locations != NULL)
3829
0
        {
3830
0
          g_rw_lock_writer_lock (&weak_locations_lock);
3831
3832
          /* It is possible that one of the weak references beat us to
3833
           * the lock. Make sure the refcount is still what we expected
3834
           * it to be.
3835
           */
3836
0
          old_ref = g_atomic_int_get (&object->ref_count);
3837
0
          if (old_ref != 1)
3838
0
            {
3839
0
              g_rw_lock_writer_unlock (&weak_locations_lock);
3840
0
              goto retry_atomic_decrement1;
3841
0
            }
3842
3843
          /* We got the lock first, so the object will definitely die
3844
           * now. Clear out all the weak references, if they're still set.
3845
           */
3846
0
          weak_locations = g_datalist_id_remove_no_notify (&object->qdata,
3847
0
                                                           quark_weak_locations);
3848
0
          g_clear_pointer (&weak_locations, weak_locations_free_unlocked);
3849
3850
0
          g_rw_lock_writer_unlock (&weak_locations_lock);
3851
0
        }
3852
3853
      /* freeze the notification queue, so we don't accidentally emit
3854
       * notifications during dispose() and finalize().
3855
       *
3856
       * The notification queue stays frozen unless the instance acquires
3857
       * a reference during dispose(), in which case we thaw it and
3858
       * dispatch all the notifications. If the instance gets through
3859
       * to finalize(), the notification queue gets automatically
3860
       * drained when g_object_finalize() is reached and
3861
       * the qdata is cleared.
3862
       */
3863
0
      nqueue = g_object_notify_queue_freeze (object, FALSE);
3864
3865
      /* we are about to remove the last reference */
3866
0
      TRACE (GOBJECT_OBJECT_DISPOSE(object,G_TYPE_FROM_INSTANCE(object), 1));
3867
0
      G_OBJECT_GET_CLASS (object)->dispose (object);
3868
0
      TRACE (GOBJECT_OBJECT_DISPOSE_END(object,G_TYPE_FROM_INSTANCE(object), 1));
3869
3870
      /* may have been re-referenced meanwhile */
3871
0
    retry_atomic_decrement2:
3872
0
      old_ref = g_atomic_int_get ((int *)&object->ref_count);
3873
0
      if (old_ref > 1)
3874
0
        {
3875
          /* valid if last 2 refs are owned by this call to unref and the toggle_ref */
3876
0
          gboolean has_toggle_ref = OBJECT_HAS_TOGGLE_REF (object);
3877
3878
0
          if (!g_atomic_int_compare_and_exchange ((int *)&object->ref_count, old_ref, old_ref - 1))
3879
0
      goto retry_atomic_decrement2;
3880
3881
          /* emit all notifications that have been queued during dispose() */
3882
0
          g_object_notify_queue_thaw (object, nqueue);
3883
3884
0
    TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3885
3886
          /* if we went from 2->1 we need to notify toggle refs if any */
3887
0
          if (old_ref == 2 && has_toggle_ref) /* The last ref being held in this case is owned by the toggle_ref */
3888
0
      toggle_refs_notify (object, TRUE);
3889
3890
0
    return;
3891
0
  }
3892
3893
      /* we are still in the process of taking away the last ref */
3894
0
      g_datalist_id_set_data (&object->qdata, quark_closure_array, NULL);
3895
0
      g_signal_handlers_destroy (object);
3896
0
      g_datalist_id_set_data (&object->qdata, quark_weak_refs, NULL);
3897
0
      g_datalist_id_set_data (&object->qdata, quark_weak_locations, NULL);
3898
3899
      /* decrement the last reference */
3900
0
      old_ref = g_atomic_int_add (&object->ref_count, -1);
3901
0
      g_return_if_fail (old_ref > 0);
3902
3903
0
      TRACE (GOBJECT_OBJECT_UNREF(object,G_TYPE_FROM_INSTANCE(object),old_ref));
3904
3905
      /* may have been re-referenced meanwhile */
3906
0
      if (G_LIKELY (old_ref == 1))
3907
0
  {
3908
0
    TRACE (GOBJECT_OBJECT_FINALIZE(object,G_TYPE_FROM_INSTANCE(object)));
3909
0
          G_OBJECT_GET_CLASS (object)->finalize (object);
3910
0
    TRACE (GOBJECT_OBJECT_FINALIZE_END(object,G_TYPE_FROM_INSTANCE(object)));
3911
3912
0
          GOBJECT_IF_DEBUG (OBJECTS,
3913
0
      {
3914
0
              gboolean was_present;
3915
3916
              /* catch objects not chaining finalize handlers */
3917
0
              G_LOCK (debug_objects);
3918
0
              was_present = g_hash_table_remove (debug_objects_ht, object);
3919
0
              G_UNLOCK (debug_objects);
3920
3921
0
              if (was_present)
3922
0
                g_critical ("Object %p of type %s not finalized correctly.",
3923
0
                            object, G_OBJECT_TYPE_NAME (object));
3924
0
      });
3925
0
          g_type_free_instance ((GTypeInstance*) object);
3926
0
  }
3927
0
      else
3928
0
        {
3929
          /* The instance acquired a reference between dispose() and
3930
           * finalize(), so we need to thaw the notification queue
3931
           */
3932
0
          g_object_notify_queue_thaw (object, nqueue);
3933
0
        }
3934
0
    }
3935
0
}
3936
3937
/**
3938
 * g_clear_object: (skip)
3939
 * @object_ptr: a pointer to a #GObject reference
3940
 *
3941
 * Clears a reference to a #GObject.
3942
 *
3943
 * @object_ptr must not be %NULL.
3944
 *
3945
 * If the reference is %NULL then this function does nothing.
3946
 * Otherwise, the reference count of the object is decreased and the
3947
 * pointer is set to %NULL.
3948
 *
3949
 * A macro is also included that allows this function to be used without
3950
 * pointer casts.
3951
 *
3952
 * Since: 2.28
3953
 **/
3954
#undef g_clear_object
3955
void
3956
g_clear_object (GObject **object_ptr)
3957
0
{
3958
0
  g_clear_pointer (object_ptr, g_object_unref);
3959
0
}
3960
3961
/**
3962
 * g_object_get_qdata:
3963
 * @object: The GObject to get a stored user data pointer from
3964
 * @quark: A #GQuark, naming the user data pointer
3965
 * 
3966
 * This function gets back user data pointers stored via
3967
 * g_object_set_qdata().
3968
 * 
3969
 * Returns: (transfer none) (nullable): The user data pointer set, or %NULL
3970
 */
3971
gpointer
3972
g_object_get_qdata (GObject *object,
3973
        GQuark   quark)
3974
0
{
3975
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
3976
  
3977
0
  return quark ? g_datalist_id_get_data (&object->qdata, quark) : NULL;
3978
0
}
3979
3980
/**
3981
 * g_object_set_qdata: (skip)
3982
 * @object: The GObject to set store a user data pointer
3983
 * @quark: A #GQuark, naming the user data pointer
3984
 * @data: (nullable): An opaque user data pointer
3985
 *
3986
 * This sets an opaque, named pointer on an object.
3987
 * The name is specified through a #GQuark (retrieved e.g. via
3988
 * g_quark_from_static_string()), and the pointer
3989
 * can be gotten back from the @object with g_object_get_qdata()
3990
 * until the @object is finalized.
3991
 * Setting a previously set user data pointer, overrides (frees)
3992
 * the old pointer set, using #NULL as pointer essentially
3993
 * removes the data stored.
3994
 */
3995
void
3996
g_object_set_qdata (GObject *object,
3997
        GQuark   quark,
3998
        gpointer data)
3999
0
{
4000
0
  g_return_if_fail (G_IS_OBJECT (object));
4001
0
  g_return_if_fail (quark > 0);
4002
4003
0
  g_datalist_id_set_data (&object->qdata, quark, data);
4004
0
}
4005
4006
/**
4007
 * g_object_dup_qdata: (skip)
4008
 * @object: the #GObject to store user data on
4009
 * @quark: a #GQuark, naming the user data pointer
4010
 * @dup_func: (nullable): function to dup the value
4011
 * @user_data: (nullable): passed as user_data to @dup_func
4012
 *
4013
 * This is a variant of g_object_get_qdata() which returns
4014
 * a 'duplicate' of the value. @dup_func defines the
4015
 * meaning of 'duplicate' in this context, it could e.g.
4016
 * take a reference on a ref-counted object.
4017
 *
4018
 * If the @quark is not set on the object then @dup_func
4019
 * will be called with a %NULL argument.
4020
 *
4021
 * Note that @dup_func is called while user data of @object
4022
 * is locked.
4023
 *
4024
 * This function can be useful to avoid races when multiple
4025
 * threads are using object data on the same key on the same
4026
 * object.
4027
 *
4028
 * Returns: the result of calling @dup_func on the value
4029
 *     associated with @quark on @object, or %NULL if not set.
4030
 *     If @dup_func is %NULL, the value is returned
4031
 *     unmodified.
4032
 *
4033
 * Since: 2.34
4034
 */
4035
gpointer
4036
g_object_dup_qdata (GObject        *object,
4037
                    GQuark          quark,
4038
                    GDuplicateFunc   dup_func,
4039
                    gpointer         user_data)
4040
0
{
4041
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4042
0
  g_return_val_if_fail (quark > 0, NULL);
4043
4044
0
  return g_datalist_id_dup_data (&object->qdata, quark, dup_func, user_data);
4045
0
}
4046
4047
/**
4048
 * g_object_replace_qdata: (skip)
4049
 * @object: the #GObject to store user data on
4050
 * @quark: a #GQuark, naming the user data pointer
4051
 * @oldval: (nullable): the old value to compare against
4052
 * @newval: (nullable): the new value
4053
 * @destroy: (nullable): a destroy notify for the new value
4054
 * @old_destroy: (out) (optional): destroy notify for the existing value
4055
 *
4056
 * Compares the user data for the key @quark on @object with
4057
 * @oldval, and if they are the same, replaces @oldval with
4058
 * @newval.
4059
 *
4060
 * This is like a typical atomic compare-and-exchange
4061
 * operation, for user data on an object.
4062
 *
4063
 * If the previous value was replaced then ownership of the
4064
 * old value (@oldval) is passed to the caller, including
4065
 * the registered destroy notify for it (passed out in @old_destroy).
4066
 * It’s up to the caller to free this as needed, which may
4067
 * or may not include using @old_destroy as sometimes replacement
4068
 * should not destroy the object in the normal way.
4069
 *
4070
 * Returns: %TRUE if the existing value for @quark was replaced
4071
 *  by @newval, %FALSE otherwise.
4072
 *
4073
 * Since: 2.34
4074
 */
4075
gboolean
4076
g_object_replace_qdata (GObject        *object,
4077
                        GQuark          quark,
4078
                        gpointer        oldval,
4079
                        gpointer        newval,
4080
                        GDestroyNotify  destroy,
4081
                        GDestroyNotify *old_destroy)
4082
0
{
4083
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
4084
0
  g_return_val_if_fail (quark > 0, FALSE);
4085
4086
0
  return g_datalist_id_replace_data (&object->qdata, quark,
4087
0
                                     oldval, newval, destroy,
4088
0
                                     old_destroy);
4089
0
}
4090
4091
/**
4092
 * g_object_set_qdata_full: (skip)
4093
 * @object: The GObject to set store a user data pointer
4094
 * @quark: A #GQuark, naming the user data pointer
4095
 * @data: (nullable): An opaque user data pointer
4096
 * @destroy: (nullable): Function to invoke with @data as argument, when @data
4097
 *           needs to be freed
4098
 *
4099
 * This function works like g_object_set_qdata(), but in addition,
4100
 * a void (*destroy) (gpointer) function may be specified which is
4101
 * called with @data as argument when the @object is finalized, or
4102
 * the data is being overwritten by a call to g_object_set_qdata()
4103
 * with the same @quark.
4104
 */
4105
void
4106
g_object_set_qdata_full (GObject       *object,
4107
       GQuark   quark,
4108
       gpointer data,
4109
       GDestroyNotify destroy)
4110
0
{
4111
0
  g_return_if_fail (G_IS_OBJECT (object));
4112
0
  g_return_if_fail (quark > 0);
4113
  
4114
0
  g_datalist_id_set_data_full (&object->qdata, quark, data,
4115
0
             data ? destroy : (GDestroyNotify) NULL);
4116
0
}
4117
4118
/**
4119
 * g_object_steal_qdata:
4120
 * @object: The GObject to get a stored user data pointer from
4121
 * @quark: A #GQuark, naming the user data pointer
4122
 *
4123
 * This function gets back user data pointers stored via
4124
 * g_object_set_qdata() and removes the @data from object
4125
 * without invoking its destroy() function (if any was
4126
 * set).
4127
 * Usually, calling this function is only required to update
4128
 * user data pointers with a destroy notifier, for example:
4129
 * |[<!-- language="C" --> 
4130
 * void
4131
 * object_add_to_user_list (GObject     *object,
4132
 *                          const gchar *new_string)
4133
 * {
4134
 *   // the quark, naming the object data
4135
 *   GQuark quark_string_list = g_quark_from_static_string ("my-string-list");
4136
 *   // retrieve the old string list
4137
 *   GList *list = g_object_steal_qdata (object, quark_string_list);
4138
 *
4139
 *   // prepend new string
4140
 *   list = g_list_prepend (list, g_strdup (new_string));
4141
 *   // this changed 'list', so we need to set it again
4142
 *   g_object_set_qdata_full (object, quark_string_list, list, free_string_list);
4143
 * }
4144
 * static void
4145
 * free_string_list (gpointer data)
4146
 * {
4147
 *   GList *node, *list = data;
4148
 *
4149
 *   for (node = list; node; node = node->next)
4150
 *     g_free (node->data);
4151
 *   g_list_free (list);
4152
 * }
4153
 * ]|
4154
 * Using g_object_get_qdata() in the above example, instead of
4155
 * g_object_steal_qdata() would have left the destroy function set,
4156
 * and thus the partial string list would have been freed upon
4157
 * g_object_set_qdata_full().
4158
 *
4159
 * Returns: (transfer full) (nullable): The user data pointer set, or %NULL
4160
 */
4161
gpointer
4162
g_object_steal_qdata (GObject *object,
4163
          GQuark   quark)
4164
0
{
4165
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4166
0
  g_return_val_if_fail (quark > 0, NULL);
4167
  
4168
0
  return g_datalist_id_remove_no_notify (&object->qdata, quark);
4169
0
}
4170
4171
/**
4172
 * g_object_get_data:
4173
 * @object: #GObject containing the associations
4174
 * @key: name of the key for that association
4175
 * 
4176
 * Gets a named field from the objects table of associations (see g_object_set_data()).
4177
 * 
4178
 * Returns: (transfer none) (nullable): the data if found,
4179
 *          or %NULL if no such data exists.
4180
 */
4181
gpointer
4182
g_object_get_data (GObject     *object,
4183
                   const gchar *key)
4184
0
{
4185
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4186
0
  g_return_val_if_fail (key != NULL, NULL);
4187
4188
0
  return g_datalist_get_data (&object->qdata, key);
4189
0
}
4190
4191
/**
4192
 * g_object_set_data:
4193
 * @object: #GObject containing the associations.
4194
 * @key: name of the key
4195
 * @data: (nullable): data to associate with that key
4196
 *
4197
 * Each object carries around a table of associations from
4198
 * strings to pointers.  This function lets you set an association.
4199
 *
4200
 * If the object already had an association with that name,
4201
 * the old association will be destroyed.
4202
 *
4203
 * Internally, the @key is converted to a #GQuark using g_quark_from_string().
4204
 * This means a copy of @key is kept permanently (even after @object has been
4205
 * finalized) — so it is recommended to only use a small, bounded set of values
4206
 * for @key in your program, to avoid the #GQuark storage growing unbounded.
4207
 */
4208
void
4209
g_object_set_data (GObject     *object,
4210
                   const gchar *key,
4211
                   gpointer     data)
4212
0
{
4213
0
  g_return_if_fail (G_IS_OBJECT (object));
4214
0
  g_return_if_fail (key != NULL);
4215
4216
0
  g_datalist_id_set_data (&object->qdata, g_quark_from_string (key), data);
4217
0
}
4218
4219
/**
4220
 * g_object_dup_data: (skip)
4221
 * @object: the #GObject to store user data on
4222
 * @key: a string, naming the user data pointer
4223
 * @dup_func: (nullable): function to dup the value
4224
 * @user_data: (nullable): passed as user_data to @dup_func
4225
 *
4226
 * This is a variant of g_object_get_data() which returns
4227
 * a 'duplicate' of the value. @dup_func defines the
4228
 * meaning of 'duplicate' in this context, it could e.g.
4229
 * take a reference on a ref-counted object.
4230
 *
4231
 * If the @key is not set on the object then @dup_func
4232
 * will be called with a %NULL argument.
4233
 *
4234
 * Note that @dup_func is called while user data of @object
4235
 * is locked.
4236
 *
4237
 * This function can be useful to avoid races when multiple
4238
 * threads are using object data on the same key on the same
4239
 * object.
4240
 *
4241
 * Returns: the result of calling @dup_func on the value
4242
 *     associated with @key on @object, or %NULL if not set.
4243
 *     If @dup_func is %NULL, the value is returned
4244
 *     unmodified.
4245
 *
4246
 * Since: 2.34
4247
 */
4248
gpointer
4249
g_object_dup_data (GObject        *object,
4250
                   const gchar    *key,
4251
                   GDuplicateFunc   dup_func,
4252
                   gpointer         user_data)
4253
0
{
4254
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4255
0
  g_return_val_if_fail (key != NULL, NULL);
4256
4257
0
  return g_datalist_id_dup_data (&object->qdata,
4258
0
                                 g_quark_from_string (key),
4259
0
                                 dup_func, user_data);
4260
0
}
4261
4262
/**
4263
 * g_object_replace_data: (skip)
4264
 * @object: the #GObject to store user data on
4265
 * @key: a string, naming the user data pointer
4266
 * @oldval: (nullable): the old value to compare against
4267
 * @newval: (nullable): the new value
4268
 * @destroy: (nullable): a destroy notify for the new value
4269
 * @old_destroy: (out) (optional): destroy notify for the existing value
4270
 *
4271
 * Compares the user data for the key @key on @object with
4272
 * @oldval, and if they are the same, replaces @oldval with
4273
 * @newval.
4274
 *
4275
 * This is like a typical atomic compare-and-exchange
4276
 * operation, for user data on an object.
4277
 *
4278
 * If the previous value was replaced then ownership of the
4279
 * old value (@oldval) is passed to the caller, including
4280
 * the registered destroy notify for it (passed out in @old_destroy).
4281
 * It’s up to the caller to free this as needed, which may
4282
 * or may not include using @old_destroy as sometimes replacement
4283
 * should not destroy the object in the normal way.
4284
 *
4285
 * See g_object_set_data() for guidance on using a small, bounded set of values
4286
 * for @key.
4287
 *
4288
 * Returns: %TRUE if the existing value for @key was replaced
4289
 *  by @newval, %FALSE otherwise.
4290
 *
4291
 * Since: 2.34
4292
 */
4293
gboolean
4294
g_object_replace_data (GObject        *object,
4295
                       const gchar    *key,
4296
                       gpointer        oldval,
4297
                       gpointer        newval,
4298
                       GDestroyNotify  destroy,
4299
                       GDestroyNotify *old_destroy)
4300
0
{
4301
0
  g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
4302
0
  g_return_val_if_fail (key != NULL, FALSE);
4303
4304
0
  return g_datalist_id_replace_data (&object->qdata,
4305
0
                                     g_quark_from_string (key),
4306
0
                                     oldval, newval, destroy,
4307
0
                                     old_destroy);
4308
0
}
4309
4310
/**
4311
 * g_object_set_data_full: (skip)
4312
 * @object: #GObject containing the associations
4313
 * @key: name of the key
4314
 * @data: (nullable): data to associate with that key
4315
 * @destroy: (nullable): function to call when the association is destroyed
4316
 *
4317
 * Like g_object_set_data() except it adds notification
4318
 * for when the association is destroyed, either by setting it
4319
 * to a different value or when the object is destroyed.
4320
 *
4321
 * Note that the @destroy callback is not called if @data is %NULL.
4322
 */
4323
void
4324
g_object_set_data_full (GObject       *object,
4325
                        const gchar   *key,
4326
                        gpointer       data,
4327
                        GDestroyNotify destroy)
4328
0
{
4329
0
  g_return_if_fail (G_IS_OBJECT (object));
4330
0
  g_return_if_fail (key != NULL);
4331
4332
0
  g_datalist_id_set_data_full (&object->qdata, g_quark_from_string (key), data,
4333
0
             data ? destroy : (GDestroyNotify) NULL);
4334
0
}
4335
4336
/**
4337
 * g_object_steal_data:
4338
 * @object: #GObject containing the associations
4339
 * @key: name of the key
4340
 *
4341
 * Remove a specified datum from the object's data associations,
4342
 * without invoking the association's destroy handler.
4343
 *
4344
 * Returns: (transfer full) (nullable): the data if found, or %NULL
4345
 *          if no such data exists.
4346
 */
4347
gpointer
4348
g_object_steal_data (GObject     *object,
4349
                     const gchar *key)
4350
0
{
4351
0
  GQuark quark;
4352
4353
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4354
0
  g_return_val_if_fail (key != NULL, NULL);
4355
4356
0
  quark = g_quark_try_string (key);
4357
4358
0
  return quark ? g_datalist_id_remove_no_notify (&object->qdata, quark) : NULL;
4359
0
}
4360
4361
static void
4362
g_value_object_init (GValue *value)
4363
0
{
4364
0
  value->data[0].v_pointer = NULL;
4365
0
}
4366
4367
static void
4368
g_value_object_free_value (GValue *value)
4369
0
{
4370
0
  if (value->data[0].v_pointer)
4371
0
    g_object_unref (value->data[0].v_pointer);
4372
0
}
4373
4374
static void
4375
g_value_object_copy_value (const GValue *src_value,
4376
         GValue *dest_value)
4377
0
{
4378
0
  if (src_value->data[0].v_pointer)
4379
0
    dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
4380
0
  else
4381
0
    dest_value->data[0].v_pointer = NULL;
4382
0
}
4383
4384
static void
4385
g_value_object_transform_value (const GValue *src_value,
4386
        GValue       *dest_value)
4387
0
{
4388
0
  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)))
4389
0
    dest_value->data[0].v_pointer = g_object_ref (src_value->data[0].v_pointer);
4390
0
  else
4391
0
    dest_value->data[0].v_pointer = NULL;
4392
0
}
4393
4394
static gpointer
4395
g_value_object_peek_pointer (const GValue *value)
4396
0
{
4397
0
  return value->data[0].v_pointer;
4398
0
}
4399
4400
static gchar*
4401
g_value_object_collect_value (GValue    *value,
4402
            guint        n_collect_values,
4403
            GTypeCValue *collect_values,
4404
            guint        collect_flags)
4405
0
{
4406
0
  if (collect_values[0].v_pointer)
4407
0
    {
4408
0
      GObject *object = collect_values[0].v_pointer;
4409
      
4410
0
      if (object->g_type_instance.g_class == NULL)
4411
0
  return g_strconcat ("invalid unclassed object pointer for value type '",
4412
0
          G_VALUE_TYPE_NAME (value),
4413
0
          "'",
4414
0
          NULL);
4415
0
      else if (!g_value_type_compatible (G_OBJECT_TYPE (object), G_VALUE_TYPE (value)))
4416
0
  return g_strconcat ("invalid object type '",
4417
0
          G_OBJECT_TYPE_NAME (object),
4418
0
          "' for value type '",
4419
0
          G_VALUE_TYPE_NAME (value),
4420
0
          "'",
4421
0
          NULL);
4422
      /* never honour G_VALUE_NOCOPY_CONTENTS for ref-counted types */
4423
0
      value->data[0].v_pointer = g_object_ref (object);
4424
0
    }
4425
0
  else
4426
0
    value->data[0].v_pointer = NULL;
4427
  
4428
0
  return NULL;
4429
0
}
4430
4431
static gchar*
4432
g_value_object_lcopy_value (const GValue *value,
4433
          guint        n_collect_values,
4434
          GTypeCValue *collect_values,
4435
          guint        collect_flags)
4436
0
{
4437
0
  GObject **object_p = collect_values[0].v_pointer;
4438
4439
0
  g_return_val_if_fail (object_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
4440
4441
0
  if (!value->data[0].v_pointer)
4442
0
    *object_p = NULL;
4443
0
  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
4444
0
    *object_p = value->data[0].v_pointer;
4445
0
  else
4446
0
    *object_p = g_object_ref (value->data[0].v_pointer);
4447
  
4448
0
  return NULL;
4449
0
}
4450
4451
/**
4452
 * g_value_set_object:
4453
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4454
 * @v_object: (type GObject.Object) (nullable): object value to be set
4455
 *
4456
 * Set the contents of a %G_TYPE_OBJECT derived #GValue to @v_object.
4457
 *
4458
 * g_value_set_object() increases the reference count of @v_object
4459
 * (the #GValue holds a reference to @v_object).  If you do not wish
4460
 * to increase the reference count of the object (i.e. you wish to
4461
 * pass your current reference to the #GValue because you no longer
4462
 * need it), use g_value_take_object() instead.
4463
 *
4464
 * It is important that your #GValue holds a reference to @v_object (either its
4465
 * own, or one it has taken) to ensure that the object won't be destroyed while
4466
 * the #GValue still exists).
4467
 */
4468
void
4469
g_value_set_object (GValue   *value,
4470
        gpointer  v_object)
4471
0
{
4472
0
  GObject *old;
4473
  
4474
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
4475
4476
0
  old = value->data[0].v_pointer;
4477
  
4478
0
  if (v_object)
4479
0
    {
4480
0
      g_return_if_fail (G_IS_OBJECT (v_object));
4481
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
4482
4483
0
      value->data[0].v_pointer = v_object;
4484
0
      g_object_ref (value->data[0].v_pointer);
4485
0
    }
4486
0
  else
4487
0
    value->data[0].v_pointer = NULL;
4488
  
4489
0
  if (old)
4490
0
    g_object_unref (old);
4491
0
}
4492
4493
/**
4494
 * g_value_set_object_take_ownership: (skip)
4495
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4496
 * @v_object: (nullable): object value to be set
4497
 *
4498
 * This is an internal function introduced mainly for C marshallers.
4499
 *
4500
 * Deprecated: 2.4: Use g_value_take_object() instead.
4501
 */
4502
void
4503
g_value_set_object_take_ownership (GValue  *value,
4504
           gpointer v_object)
4505
0
{
4506
0
  g_value_take_object (value, v_object);
4507
0
}
4508
4509
/**
4510
 * g_value_take_object: (skip)
4511
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4512
 * @v_object: (nullable): object value to be set
4513
 *
4514
 * Sets the contents of a %G_TYPE_OBJECT derived #GValue to @v_object
4515
 * and takes over the ownership of the caller’s reference to @v_object;
4516
 * the caller doesn’t have to unref it any more (i.e. the reference
4517
 * count of the object is not increased).
4518
 *
4519
 * If you want the #GValue to hold its own reference to @v_object, use
4520
 * g_value_set_object() instead.
4521
 *
4522
 * Since: 2.4
4523
 */
4524
void
4525
g_value_take_object (GValue  *value,
4526
         gpointer v_object)
4527
0
{
4528
0
  g_return_if_fail (G_VALUE_HOLDS_OBJECT (value));
4529
4530
0
  if (value->data[0].v_pointer)
4531
0
    {
4532
0
      g_object_unref (value->data[0].v_pointer);
4533
0
      value->data[0].v_pointer = NULL;
4534
0
    }
4535
4536
0
  if (v_object)
4537
0
    {
4538
0
      g_return_if_fail (G_IS_OBJECT (v_object));
4539
0
      g_return_if_fail (g_value_type_compatible (G_OBJECT_TYPE (v_object), G_VALUE_TYPE (value)));
4540
4541
0
      value->data[0].v_pointer = v_object; /* we take over the reference count */
4542
0
    }
4543
0
}
4544
4545
/**
4546
 * g_value_get_object:
4547
 * @value: a valid #GValue of %G_TYPE_OBJECT derived type
4548
 * 
4549
 * Get the contents of a %G_TYPE_OBJECT derived #GValue.
4550
 * 
4551
 * Returns: (type GObject.Object) (transfer none): object contents of @value
4552
 */
4553
gpointer
4554
g_value_get_object (const GValue *value)
4555
0
{
4556
0
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
4557
  
4558
0
  return value->data[0].v_pointer;
4559
0
}
4560
4561
/**
4562
 * g_value_dup_object:
4563
 * @value: a valid #GValue whose type is derived from %G_TYPE_OBJECT
4564
 *
4565
 * Get the contents of a %G_TYPE_OBJECT derived #GValue, increasing
4566
 * its reference count. If the contents of the #GValue are %NULL, then
4567
 * %NULL will be returned.
4568
 *
4569
 * Returns: (type GObject.Object) (transfer full): object content of @value,
4570
 *          should be unreferenced when no longer needed.
4571
 */
4572
gpointer
4573
g_value_dup_object (const GValue *value)
4574
0
{
4575
0
  g_return_val_if_fail (G_VALUE_HOLDS_OBJECT (value), NULL);
4576
  
4577
0
  return value->data[0].v_pointer ? g_object_ref (value->data[0].v_pointer) : NULL;
4578
0
}
4579
4580
/**
4581
 * g_signal_connect_object: (skip)
4582
 * @instance: (type GObject.TypeInstance): the instance to connect to.
4583
 * @detailed_signal: a string of the form "signal-name::detail".
4584
 * @c_handler: the #GCallback to connect.
4585
 * @gobject: (type GObject.Object) (nullable): the object to pass as data
4586
 *    to @c_handler.
4587
 * @connect_flags: a combination of #GConnectFlags.
4588
 *
4589
 * This is similar to g_signal_connect_data(), but uses a closure which
4590
 * ensures that the @gobject stays alive during the call to @c_handler
4591
 * by temporarily adding a reference count to @gobject.
4592
 *
4593
 * When the @gobject is destroyed the signal handler will be automatically
4594
 * disconnected.  Note that this is not currently threadsafe (ie:
4595
 * emitting a signal while @gobject is being destroyed in another thread
4596
 * is not safe).
4597
 *
4598
 * Returns: the handler id.
4599
 */
4600
gulong
4601
g_signal_connect_object (gpointer      instance,
4602
       const gchar  *detailed_signal,
4603
       GCallback     c_handler,
4604
       gpointer      gobject,
4605
       GConnectFlags connect_flags)
4606
0
{
4607
0
  g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
4608
0
  g_return_val_if_fail (detailed_signal != NULL, 0);
4609
0
  g_return_val_if_fail (c_handler != NULL, 0);
4610
4611
0
  if (gobject)
4612
0
    {
4613
0
      GClosure *closure;
4614
4615
0
      g_return_val_if_fail (G_IS_OBJECT (gobject), 0);
4616
4617
0
      closure = ((connect_flags & G_CONNECT_SWAPPED) ? g_cclosure_new_object_swap : g_cclosure_new_object) (c_handler, gobject);
4618
4619
0
      return g_signal_connect_closure (instance, detailed_signal, closure, connect_flags & G_CONNECT_AFTER);
4620
0
    }
4621
0
  else
4622
0
    return g_signal_connect_data (instance, detailed_signal, c_handler, NULL, NULL, connect_flags);
4623
0
}
4624
4625
typedef struct {
4626
  GObject  *object;
4627
  guint     n_closures;
4628
  GClosure *closures[1]; /* flexible array */
4629
} CArray;
4630
/* don't change this structure without supplying an accessor for
4631
 * watched closures, e.g.:
4632
 * GSList* g_object_list_watched_closures (GObject *object)
4633
 * {
4634
 *   CArray *carray;
4635
 *   g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4636
 *   carray = g_object_get_data (object, "GObject-closure-array");
4637
 *   if (carray)
4638
 *     {
4639
 *       GSList *slist = NULL;
4640
 *       guint i;
4641
 *       for (i = 0; i < carray->n_closures; i++)
4642
 *         slist = g_slist_prepend (slist, carray->closures[i]);
4643
 *       return slist;
4644
 *     }
4645
 *   return NULL;
4646
 * }
4647
 */
4648
4649
static void
4650
object_remove_closure (gpointer  data,
4651
           GClosure *closure)
4652
0
{
4653
0
  GObject *object = data;
4654
0
  CArray *carray;
4655
0
  guint i;
4656
  
4657
0
  G_LOCK (closure_array_mutex);
4658
0
  carray = g_object_get_qdata (object, quark_closure_array);
4659
0
  for (i = 0; i < carray->n_closures; i++)
4660
0
    if (carray->closures[i] == closure)
4661
0
      {
4662
0
  carray->n_closures--;
4663
0
  if (i < carray->n_closures)
4664
0
    carray->closures[i] = carray->closures[carray->n_closures];
4665
0
  G_UNLOCK (closure_array_mutex);
4666
0
  return;
4667
0
      }
4668
0
  G_UNLOCK (closure_array_mutex);
4669
0
  g_assert_not_reached ();
4670
0
}
4671
4672
static void
4673
destroy_closure_array (gpointer data)
4674
0
{
4675
0
  CArray *carray = data;
4676
0
  GObject *object = carray->object;
4677
0
  guint i, n = carray->n_closures;
4678
  
4679
0
  for (i = 0; i < n; i++)
4680
0
    {
4681
0
      GClosure *closure = carray->closures[i];
4682
      
4683
      /* removing object_remove_closure() upfront is probably faster than
4684
       * letting it fiddle with quark_closure_array which is empty anyways
4685
       */
4686
0
      g_closure_remove_invalidate_notifier (closure, object, object_remove_closure);
4687
0
      g_closure_invalidate (closure);
4688
0
    }
4689
0
  g_free (carray);
4690
0
}
4691
4692
/**
4693
 * g_object_watch_closure:
4694
 * @object: #GObject restricting lifetime of @closure
4695
 * @closure: #GClosure to watch
4696
 *
4697
 * This function essentially limits the life time of the @closure to
4698
 * the life time of the object. That is, when the object is finalized,
4699
 * the @closure is invalidated by calling g_closure_invalidate() on
4700
 * it, in order to prevent invocations of the closure with a finalized
4701
 * (nonexisting) object. Also, g_object_ref() and g_object_unref() are
4702
 * added as marshal guards to the @closure, to ensure that an extra
4703
 * reference count is held on @object during invocation of the
4704
 * @closure.  Usually, this function will be called on closures that
4705
 * use this @object as closure data.
4706
 */
4707
void
4708
g_object_watch_closure (GObject  *object,
4709
      GClosure *closure)
4710
0
{
4711
0
  CArray *carray;
4712
0
  guint i;
4713
  
4714
0
  g_return_if_fail (G_IS_OBJECT (object));
4715
0
  g_return_if_fail (closure != NULL);
4716
0
  g_return_if_fail (closure->is_invalid == FALSE);
4717
0
  g_return_if_fail (closure->in_marshal == FALSE);
4718
0
  g_return_if_fail (g_atomic_int_get (&object->ref_count) > 0); /* this doesn't work on finalizing objects */
4719
  
4720
0
  g_closure_add_invalidate_notifier (closure, object, object_remove_closure);
4721
0
  g_closure_add_marshal_guards (closure,
4722
0
        object, (GClosureNotify) g_object_ref,
4723
0
        object, (GClosureNotify) g_object_unref);
4724
0
  G_LOCK (closure_array_mutex);
4725
0
  carray = g_datalist_id_remove_no_notify (&object->qdata, quark_closure_array);
4726
0
  if (!carray)
4727
0
    {
4728
0
      carray = g_renew (CArray, NULL, 1);
4729
0
      carray->object = object;
4730
0
      carray->n_closures = 1;
4731
0
      i = 0;
4732
0
    }
4733
0
  else
4734
0
    {
4735
0
      i = carray->n_closures++;
4736
0
      carray = g_realloc (carray, sizeof (*carray) + sizeof (carray->closures[0]) * i);
4737
0
    }
4738
0
  carray->closures[i] = closure;
4739
0
  g_datalist_id_set_data_full (&object->qdata, quark_closure_array, carray, destroy_closure_array);
4740
0
  G_UNLOCK (closure_array_mutex);
4741
0
}
4742
4743
/**
4744
 * g_closure_new_object:
4745
 * @sizeof_closure: the size of the structure to allocate, must be at least
4746
 *  `sizeof (GClosure)`
4747
 * @object: a #GObject pointer to store in the @data field of the newly
4748
 *  allocated #GClosure
4749
 *
4750
 * A variant of g_closure_new_simple() which stores @object in the
4751
 * @data field of the closure and calls g_object_watch_closure() on
4752
 * @object and the created closure. This function is mainly useful
4753
 * when implementing new types of closures.
4754
 *
4755
 * Returns: (transfer floating): a newly allocated #GClosure
4756
 */
4757
GClosure *
4758
g_closure_new_object (guint    sizeof_closure,
4759
          GObject *object)
4760
0
{
4761
0
  GClosure *closure;
4762
4763
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4764
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4765
4766
0
  closure = g_closure_new_simple (sizeof_closure, object);
4767
0
  g_object_watch_closure (object, closure);
4768
4769
0
  return closure;
4770
0
}
4771
4772
/**
4773
 * g_cclosure_new_object: (skip)
4774
 * @callback_func: the function to invoke
4775
 * @object: a #GObject pointer to pass to @callback_func
4776
 *
4777
 * A variant of g_cclosure_new() which uses @object as @user_data and
4778
 * calls g_object_watch_closure() on @object and the created
4779
 * closure. This function is useful when you have a callback closely
4780
 * associated with a #GObject, and want the callback to no longer run
4781
 * after the object is is freed.
4782
 *
4783
 * Returns: (transfer floating): a new #GCClosure
4784
 */
4785
GClosure *
4786
g_cclosure_new_object (GCallback callback_func,
4787
           GObject  *object)
4788
0
{
4789
0
  GClosure *closure;
4790
4791
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4792
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4793
0
  g_return_val_if_fail (callback_func != NULL, NULL);
4794
4795
0
  closure = g_cclosure_new (callback_func, object, NULL);
4796
0
  g_object_watch_closure (object, closure);
4797
4798
0
  return closure;
4799
0
}
4800
4801
/**
4802
 * g_cclosure_new_object_swap: (skip)
4803
 * @callback_func: the function to invoke
4804
 * @object: a #GObject pointer to pass to @callback_func
4805
 *
4806
 * A variant of g_cclosure_new_swap() which uses @object as @user_data
4807
 * and calls g_object_watch_closure() on @object and the created
4808
 * closure. This function is useful when you have a callback closely
4809
 * associated with a #GObject, and want the callback to no longer run
4810
 * after the object is is freed.
4811
 *
4812
 * Returns: (transfer floating): a new #GCClosure
4813
 */
4814
GClosure *
4815
g_cclosure_new_object_swap (GCallback callback_func,
4816
          GObject  *object)
4817
0
{
4818
0
  GClosure *closure;
4819
4820
0
  g_return_val_if_fail (G_IS_OBJECT (object), NULL);
4821
0
  g_return_val_if_fail (g_atomic_int_get (&object->ref_count) > 0, NULL);     /* this doesn't work on finalizing objects */
4822
0
  g_return_val_if_fail (callback_func != NULL, NULL);
4823
4824
0
  closure = g_cclosure_new_swap (callback_func, object, NULL);
4825
0
  g_object_watch_closure (object, closure);
4826
4827
0
  return closure;
4828
0
}
4829
4830
gsize
4831
g_object_compat_control (gsize           what,
4832
                         gpointer        data)
4833
0
{
4834
0
  switch (what)
4835
0
    {
4836
0
      gpointer *pp;
4837
0
    case 1:     /* floating base type */
4838
0
      return G_TYPE_INITIALLY_UNOWNED;
4839
0
    case 2:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
4840
0
      floating_flag_handler = (guint(*)(GObject*,gint)) data;
4841
0
      return 1;
4842
0
    case 3:     /* FIXME: remove this once GLib/Gtk+ break ABI again */
4843
0
      pp = data;
4844
0
      *pp = floating_flag_handler;
4845
0
      return 1;
4846
0
    default:
4847
0
      return 0;
4848
0
    }
4849
0
}
4850
4851
0
G_DEFINE_TYPE (GInitiallyUnowned, g_initially_unowned, G_TYPE_OBJECT)
4852
0
4853
0
static void
4854
0
g_initially_unowned_init (GInitiallyUnowned *object)
4855
0
{
4856
0
  g_object_force_floating (object);
4857
0
}
4858
4859
static void
4860
g_initially_unowned_class_init (GInitiallyUnownedClass *klass)
4861
0
{
4862
0
}
4863
4864
/**
4865
 * GWeakRef:
4866
 *
4867
 * A structure containing a weak reference to a #GObject.
4868
 *
4869
 * A `GWeakRef` can either be empty (i.e. point to %NULL), or point to an
4870
 * object for as long as at least one "strong" reference to that object
4871
 * exists. Before the object's #GObjectClass.dispose method is called,
4872
 * every #GWeakRef associated with becomes empty (i.e. points to %NULL).
4873
 *
4874
 * Like #GValue, #GWeakRef can be statically allocated, stack- or
4875
 * heap-allocated, or embedded in larger structures.
4876
 *
4877
 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
4878
 * reference is thread-safe: converting a weak pointer to a reference is
4879
 * atomic with respect to invalidation of weak pointers to destroyed
4880
 * objects.
4881
 *
4882
 * If the object's #GObjectClass.dispose method results in additional
4883
 * references to the object being held (‘re-referencing’), any #GWeakRefs taken
4884
 * before it was disposed will continue to point to %NULL.  Any #GWeakRefs taken
4885
 * during disposal and after re-referencing, or after disposal has returned due
4886
 * to the re-referencing, will continue to point to the object until its refcount
4887
 * goes back to zero, at which point they too will be invalidated.
4888
 *
4889
 * It is invalid to take a #GWeakRef on an object during #GObjectClass.dispose
4890
 * without first having or creating a strong reference to the object.
4891
 */
4892
4893
/**
4894
 * g_weak_ref_init: (skip)
4895
 * @weak_ref: (inout): uninitialized or empty location for a weak
4896
 *    reference
4897
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
4898
 *
4899
 * Initialise a non-statically-allocated #GWeakRef.
4900
 *
4901
 * This function also calls g_weak_ref_set() with @object on the
4902
 * freshly-initialised weak reference.
4903
 *
4904
 * This function should always be matched with a call to
4905
 * g_weak_ref_clear().  It is not necessary to use this function for a
4906
 * #GWeakRef in static storage because it will already be
4907
 * properly initialised.  Just use g_weak_ref_set() directly.
4908
 *
4909
 * Since: 2.32
4910
 */
4911
void
4912
g_weak_ref_init (GWeakRef *weak_ref,
4913
                 gpointer  object)
4914
0
{
4915
0
  weak_ref->priv.p = NULL;
4916
4917
0
  g_weak_ref_set (weak_ref, object);
4918
0
}
4919
4920
/**
4921
 * g_weak_ref_clear: (skip)
4922
 * @weak_ref: (inout): location of a weak reference, which
4923
 *  may be empty
4924
 *
4925
 * Frees resources associated with a non-statically-allocated #GWeakRef.
4926
 * After this call, the #GWeakRef is left in an undefined state.
4927
 *
4928
 * You should only call this on a #GWeakRef that previously had
4929
 * g_weak_ref_init() called on it.
4930
 *
4931
 * Since: 2.32
4932
 */
4933
void
4934
g_weak_ref_clear (GWeakRef *weak_ref)
4935
0
{
4936
0
  g_weak_ref_set (weak_ref, NULL);
4937
4938
  /* be unkind */
4939
0
  weak_ref->priv.p = (void *) 0xccccccccu;
4940
0
}
4941
4942
/**
4943
 * g_weak_ref_get: (skip)
4944
 * @weak_ref: (inout): location of a weak reference to a #GObject
4945
 *
4946
 * If @weak_ref is not empty, atomically acquire a strong
4947
 * reference to the object it points to, and return that reference.
4948
 *
4949
 * This function is needed because of the potential race between taking
4950
 * the pointer value and g_object_ref() on it, if the object was losing
4951
 * its last reference at the same time in a different thread.
4952
 *
4953
 * The caller should release the resulting reference in the usual way,
4954
 * by using g_object_unref().
4955
 *
4956
 * Returns: (transfer full) (type GObject.Object): the object pointed to
4957
 *     by @weak_ref, or %NULL if it was empty
4958
 *
4959
 * Since: 2.32
4960
 */
4961
gpointer
4962
g_weak_ref_get (GWeakRef *weak_ref)
4963
0
{
4964
0
  gpointer object_or_null;
4965
4966
0
  g_return_val_if_fail (weak_ref!= NULL, NULL);
4967
4968
0
  g_rw_lock_reader_lock (&weak_locations_lock);
4969
4970
0
  object_or_null = weak_ref->priv.p;
4971
4972
0
  if (object_or_null != NULL)
4973
0
    g_object_ref (object_or_null);
4974
4975
0
  g_rw_lock_reader_unlock (&weak_locations_lock);
4976
4977
0
  return object_or_null;
4978
0
}
4979
4980
static void
4981
weak_locations_free_unlocked (GSList **weak_locations)
4982
0
{
4983
0
  if (*weak_locations)
4984
0
    {
4985
0
      GSList *weak_location;
4986
4987
0
      for (weak_location = *weak_locations; weak_location;)
4988
0
        {
4989
0
          GWeakRef *weak_ref_location = weak_location->data;
4990
4991
0
          weak_ref_location->priv.p = NULL;
4992
0
          weak_location = g_slist_delete_link (weak_location, weak_location);
4993
0
        }
4994
0
    }
4995
4996
0
  g_free (weak_locations);
4997
0
}
4998
4999
static void
5000
weak_locations_free (gpointer data)
5001
0
{
5002
0
  GSList **weak_locations = data;
5003
5004
0
  g_rw_lock_writer_lock (&weak_locations_lock);
5005
0
  weak_locations_free_unlocked (weak_locations);
5006
0
  g_rw_lock_writer_unlock (&weak_locations_lock);
5007
0
}
5008
5009
/**
5010
 * g_weak_ref_set: (skip)
5011
 * @weak_ref: location for a weak reference
5012
 * @object: (type GObject.Object) (nullable): a #GObject or %NULL
5013
 *
5014
 * Change the object to which @weak_ref points, or set it to
5015
 * %NULL.
5016
 *
5017
 * You must own a strong reference on @object while calling this
5018
 * function.
5019
 *
5020
 * Since: 2.32
5021
 */
5022
void
5023
g_weak_ref_set (GWeakRef *weak_ref,
5024
                gpointer  object)
5025
0
{
5026
0
  GSList **weak_locations;
5027
0
  GObject *new_object;
5028
0
  GObject *old_object;
5029
5030
0
  g_return_if_fail (weak_ref != NULL);
5031
0
  g_return_if_fail (object == NULL || G_IS_OBJECT (object));
5032
5033
0
  new_object = object;
5034
5035
0
  g_rw_lock_writer_lock (&weak_locations_lock);
5036
5037
  /* We use the extra level of indirection here so that if we have ever
5038
   * had a weak pointer installed at any point in time on this object,
5039
   * we can see that there is a non-NULL value associated with the
5040
   * weak-pointer quark and know that this value will not change at any
5041
   * point in the object's lifetime.
5042
   *
5043
   * Both properties are important for reducing the amount of times we
5044
   * need to acquire locks and for decreasing the duration of time the
5045
   * lock is held while avoiding some rather tricky races.
5046
   *
5047
   * Specifically: we can avoid having to do an extra unconditional lock
5048
   * in g_object_unref() without worrying about some extremely tricky
5049
   * races.
5050
   */
5051
5052
0
  old_object = weak_ref->priv.p;
5053
0
  if (new_object != old_object)
5054
0
    {
5055
0
      weak_ref->priv.p = new_object;
5056
5057
      /* Remove the weak ref from the old object */
5058
0
      if (old_object != NULL)
5059
0
        {
5060
0
          weak_locations = g_datalist_id_get_data (&old_object->qdata, quark_weak_locations);
5061
0
          if (weak_locations == NULL)
5062
0
            {
5063
0
#ifndef G_DISABLE_ASSERT
5064
0
              gboolean in_weak_refs_notify =
5065
0
                  g_datalist_id_get_data (&old_object->qdata, quark_weak_refs) == NULL;
5066
0
              g_assert (in_weak_refs_notify);
5067
0
#endif /* G_DISABLE_ASSERT */
5068
0
            }
5069
0
          else
5070
0
            {
5071
0
              *weak_locations = g_slist_remove (*weak_locations, weak_ref);
5072
5073
0
              if (!*weak_locations)
5074
0
                {
5075
0
                  weak_locations_free_unlocked (weak_locations);
5076
0
                  g_datalist_id_remove_no_notify (&old_object->qdata, quark_weak_locations);
5077
0
                }
5078
0
            }
5079
0
        }
5080
5081
      /* Add the weak ref to the new object */
5082
0
      if (new_object != NULL)
5083
0
        {
5084
0
          weak_locations = g_datalist_id_get_data (&new_object->qdata, quark_weak_locations);
5085
5086
0
          if (weak_locations == NULL)
5087
0
            {
5088
0
              weak_locations = g_new0 (GSList *, 1);
5089
0
              g_datalist_id_set_data_full (&new_object->qdata, quark_weak_locations,
5090
0
                                           weak_locations, weak_locations_free);
5091
0
            }
5092
5093
0
          *weak_locations = g_slist_prepend (*weak_locations, weak_ref);
5094
0
        }
5095
0
    }
5096
5097
0
  g_rw_lock_writer_unlock (&weak_locations_lock);
5098
0
}