forked from GNOME/glib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobject.c
5106 lines (4501 loc) · 161 KB
/
gobject.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GObject - GLib Type, Object, Parameter and Signal Library
* Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* MT safe with regards to reference counting.
*/
#include "config.h"
#include <string.h>
#include <signal.h>
#include "../glib/glib-private.h"
#include "gobject.h"
#include "gtype-private.h"
#include "gvaluecollector.h"
#include "gsignal.h"
#include "gparamspecs.h"
#include "gvaluetypes.h"
#include "gobject_trace.h"
#include "gconstructor.h"
/**
* GObject:
*
* The base object type.
*
* `GObject` is the fundamental type providing the common attributes and
* methods for all object types in GTK, Pango and other libraries
* based on GObject. The `GObject` class provides methods for object
* construction and destruction, property access methods, and signal
* support. Signals are described in detail [here][gobject-Signals].
*
* For a tutorial on implementing a new `GObject` class, see [How to define and
* implement a new GObject](tutorial.html#how-to-define-and-implement-a-new-gobject).
* For a list of naming conventions for GObjects and their methods, see the
* [GType conventions](concepts.html#conventions). For the high-level concepts
* behind GObject, read
* [Instantiatable classed types: Objects](concepts.html#instantiatable-classed-types-objects).
*
* Since GLib 2.72, all `GObject`s are guaranteed to be aligned to at least the
* alignment of the largest basic GLib type (typically this is `guint64` or
* `gdouble`). If you need larger alignment for an element in a `GObject`, you
* should allocate it on the heap (aligned), or arrange for your `GObject` to be
* appropriately padded. This guarantee applies to the `GObject` (or derived)
* struct, the `GObjectClass` (or derived) struct, and any private data allocated
* by `G_ADD_PRIVATE()`.
*/
/* --- macros --- */
#define PARAM_SPEC_PARAM_ID(pspec) ((pspec)->param_id)
#define PARAM_SPEC_SET_PARAM_ID(pspec, id) ((pspec)->param_id = (id))
#define OBJECT_HAS_TOGGLE_REF_FLAG 0x1
#define OBJECT_HAS_TOGGLE_REF(object) \
((g_datalist_get_flags (&(object)->qdata) & OBJECT_HAS_TOGGLE_REF_FLAG) != 0)
#define OBJECT_FLOATING_FLAG 0x2
#define CLASS_HAS_PROPS_FLAG 0x1
#define CLASS_HAS_PROPS(class) \
((class)->flags & CLASS_HAS_PROPS_FLAG)
#define CLASS_HAS_CUSTOM_CONSTRUCTOR(class) \
((class)->constructor != g_object_constructor)
#define CLASS_HAS_CUSTOM_CONSTRUCTED(class) \
((class)->constructed != g_object_constructed)
#define CLASS_HAS_NOTIFY(class) ((class)->notify != NULL)
#define CLASS_HAS_CUSTOM_DISPATCH(class) \
((class)->dispatch_properties_changed != g_object_dispatch_properties_changed)
#define CLASS_NEEDS_NOTIFY(class) \
(CLASS_HAS_NOTIFY(class) || CLASS_HAS_CUSTOM_DISPATCH(class))
#define CLASS_HAS_DERIVED_CLASS_FLAG 0x2
#define CLASS_HAS_DERIVED_CLASS(class) \
((class)->flags & CLASS_HAS_DERIVED_CLASS_FLAG)
/* --- signals --- */
enum {
NOTIFY,
LAST_SIGNAL
};
/* --- properties --- */
enum {
PROP_NONE
};
#define OPTIONAL_FLAG_IN_CONSTRUCTION (1 << 0)
#define OPTIONAL_FLAG_HAS_SIGNAL_HANDLER (1 << 1) /* Set if object ever had a signal handler */
#define OPTIONAL_FLAG_HAS_NOTIFY_HANDLER (1 << 2) /* Same, specifically for "notify" */
#if SIZEOF_INT == 4 && GLIB_SIZEOF_VOID_P == 8
#define HAVE_OPTIONAL_FLAGS
#endif
typedef struct
{
GTypeInstance g_type_instance;
/*< private >*/
guint ref_count; /* (atomic) */
#ifdef HAVE_OPTIONAL_FLAGS
guint optional_flags; /* (atomic) */
#endif
GData *qdata;
} GObjectReal;
G_STATIC_ASSERT(sizeof(GObject) == sizeof(GObjectReal));
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, ref_count) == G_STRUCT_OFFSET(GObjectReal, ref_count));
G_STATIC_ASSERT(G_STRUCT_OFFSET(GObject, qdata) == G_STRUCT_OFFSET(GObjectReal, qdata));
/* --- prototypes --- */
static void g_object_base_class_init (GObjectClass *class);
static void g_object_base_class_finalize (GObjectClass *class);
static void g_object_do_class_init (GObjectClass *class);
static void g_object_init (GObject *object,
GObjectClass *class);
static GObject* g_object_constructor (GType type,
guint n_construct_properties,
GObjectConstructParam *construct_params);
static void g_object_constructed (GObject *object);
static void g_object_real_dispose (GObject *object);
static void g_object_finalize (GObject *object);
static void g_object_do_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void g_object_do_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void g_value_object_init (GValue *value);
static void g_value_object_free_value (GValue *value);
static void g_value_object_copy_value (const GValue *src_value,
GValue *dest_value);
static void g_value_object_transform_value (const GValue *src_value,
GValue *dest_value);
static gpointer g_value_object_peek_pointer (const GValue *value);
static gchar* g_value_object_collect_value (GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags);
static gchar* g_value_object_lcopy_value (const GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags);
static void g_object_dispatch_properties_changed (GObject *object,
guint n_pspecs,
GParamSpec **pspecs);
static guint object_floating_flag_handler (GObject *object,
gint job);
static void object_interface_check_properties (gpointer check_data,
gpointer g_iface);
static void weak_locations_free_unlocked (GSList **weak_locations);
/* --- typedefs --- */
typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
struct _GObjectNotifyQueue
{
GSList *pspecs;
guint16 n_pspecs;
guint16 freeze_count;
};
/* --- variables --- */
G_LOCK_DEFINE_STATIC (closure_array_mutex);
G_LOCK_DEFINE_STATIC (weak_refs_mutex);
G_LOCK_DEFINE_STATIC (toggle_refs_mutex);
static GQuark quark_closure_array = 0;
static GQuark quark_weak_refs = 0;
static GQuark quark_weak_notifies = 0;
static GQuark quark_toggle_refs = 0;
static GQuark quark_notify_queue;
#ifndef HAVE_OPTIONAL_FLAGS
static GQuark quark_in_construction;
#endif
static GParamSpecPool *pspec_pool = NULL;
static gulong gobject_signals[LAST_SIGNAL] = { 0, };
static guint (*floating_flag_handler) (GObject*, gint) = object_floating_flag_handler;
/* qdata pointing to GSList<GWeakRef *>, protected by weak_locations_lock */
static GQuark quark_weak_locations = 0;
static GRWLock weak_locations_lock;
G_LOCK_DEFINE_STATIC(notify_lock);
/* --- functions --- */
static void
g_object_notify_queue_free (gpointer data)
{
GObjectNotifyQueue *nqueue = data;
g_slist_free (nqueue->pspecs);
g_free_sized (nqueue, sizeof (GObjectNotifyQueue));
}
static GObjectNotifyQueue *
g_object_notify_queue_create_queue_frozen (GObject *object)
{
GObjectNotifyQueue *nqueue;
nqueue = g_new0 (GObjectNotifyQueue, 1);
*nqueue = (GObjectNotifyQueue){
.freeze_count = 1,
};
g_datalist_id_set_data_full (&object->qdata, quark_notify_queue,
nqueue, g_object_notify_queue_free);
return nqueue;
}
static GObjectNotifyQueue *
g_object_notify_queue_freeze (GObject *object)
{
GObjectNotifyQueue *nqueue;
G_LOCK(notify_lock);
nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
if (!nqueue)
{
nqueue = g_object_notify_queue_create_queue_frozen (object);
goto out;
}
if (nqueue->freeze_count >= 65535)
g_critical("Free queue for %s (%p) is larger than 65535,"
" called g_object_freeze_notify() too often."
" Forgot to call g_object_thaw_notify() or infinite loop",
G_OBJECT_TYPE_NAME (object), object);
else
nqueue->freeze_count++;
out:
G_UNLOCK(notify_lock);
return nqueue;
}
static void
g_object_notify_queue_thaw (GObject *object,
GObjectNotifyQueue *nqueue,
gboolean take_ref)
{
GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
GSList *slist;
guint n_pspecs = 0;
G_LOCK(notify_lock);
if (!nqueue)
{
/* Caller didn't look up the queue yet. Do it now. */
nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
}
/* Just make sure we never get into some nasty race condition */
if (G_UNLIKELY (!nqueue || nqueue->freeze_count == 0))
{
G_UNLOCK (notify_lock);
g_critical ("%s: property-changed notification for %s(%p) is not frozen",
G_STRFUNC, G_OBJECT_TYPE_NAME (object), object);
return;
}
nqueue->freeze_count--;
if (nqueue->freeze_count)
{
G_UNLOCK (notify_lock);
return;
}
pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
for (slist = nqueue->pspecs; slist; slist = slist->next)
{
pspecs[n_pspecs++] = slist->data;
}
g_datalist_id_set_data (&object->qdata, quark_notify_queue, NULL);
G_UNLOCK(notify_lock);
if (n_pspecs)
{
if (take_ref)
g_object_ref (object);
G_OBJECT_GET_CLASS (object)->dispatch_properties_changed (object, n_pspecs, pspecs);
if (take_ref)
g_object_unref (object);
}
g_free (free_me);
}
static gboolean
g_object_notify_queue_add (GObject *object,
GObjectNotifyQueue *nqueue,
GParamSpec *pspec,
gboolean in_init)
{
G_LOCK(notify_lock);
if (!nqueue)
{
/* We are called without an nqueue. Figure out whether a notification
* should be queued. */
nqueue = g_datalist_id_get_data (&object->qdata, quark_notify_queue);
if (!nqueue)
{
if (!in_init)
{
/* We don't have a notify queue and are not in_init. The event
* is not to be queued. The caller will dispatch directly. */
G_UNLOCK (notify_lock);
return FALSE;
}
/* We are "in_init", but did not freeze the queue in g_object_init
* yet. Instead, we gained a notify handler in instance init, so now
* we need to freeze just-in-time.
*
* Note that this freeze will be balanced at the end of object
* initialization.
*/
nqueue = g_object_notify_queue_create_queue_frozen (object);
}
}
g_assert (nqueue->n_pspecs < 65535);
if (g_slist_find (nqueue->pspecs, pspec) == NULL)
{
nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
nqueue->n_pspecs++;
}
G_UNLOCK(notify_lock);
return TRUE;
}
#ifdef G_ENABLE_DEBUG
G_LOCK_DEFINE_STATIC (debug_objects);
static guint debug_objects_count = 0;
static GHashTable *debug_objects_ht = NULL;
static void
debug_objects_foreach (gpointer key,
gpointer value,
gpointer user_data)
{
GObject *object = value;
g_message ("[%p] stale %s\tref_count=%u",
object,
G_OBJECT_TYPE_NAME (object),
object->ref_count);
}
#ifdef G_HAS_CONSTRUCTORS
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(debug_objects_atexit)
#endif
G_DEFINE_DESTRUCTOR(debug_objects_atexit)
#endif /* G_HAS_CONSTRUCTORS */
static void
debug_objects_atexit (void)
{
GOBJECT_IF_DEBUG (OBJECTS,
{
G_LOCK (debug_objects);
g_message ("stale GObjects: %u", debug_objects_count);
g_hash_table_foreach (debug_objects_ht, debug_objects_foreach, NULL);
G_UNLOCK (debug_objects);
});
}
#endif /* G_ENABLE_DEBUG */
void
_g_object_type_init (void)
{
static gboolean initialized = FALSE;
static const GTypeFundamentalInfo finfo = {
G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE,
};
GTypeInfo info = {
sizeof (GObjectClass),
(GBaseInitFunc) g_object_base_class_init,
(GBaseFinalizeFunc) g_object_base_class_finalize,
(GClassInitFunc) g_object_do_class_init,
NULL /* class_destroy */,
NULL /* class_data */,
sizeof (GObject),
0 /* n_preallocs */,
(GInstanceInitFunc) g_object_init,
NULL, /* value_table */
};
static const GTypeValueTable value_table = {
g_value_object_init, /* value_init */
g_value_object_free_value, /* value_free */
g_value_object_copy_value, /* value_copy */
g_value_object_peek_pointer, /* value_peek_pointer */
"p", /* collect_format */
g_value_object_collect_value, /* collect_value */
"p", /* lcopy_format */
g_value_object_lcopy_value, /* lcopy_value */
};
GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
g_return_if_fail (initialized == FALSE);
initialized = TRUE;
/* G_TYPE_OBJECT
*/
info.value_table = &value_table;
type = g_type_register_fundamental (G_TYPE_OBJECT, g_intern_static_string ("GObject"), &info, &finfo, 0);
g_assert (type == G_TYPE_OBJECT);
g_value_register_transform_func (G_TYPE_OBJECT, G_TYPE_OBJECT, g_value_object_transform_value);
#if G_ENABLE_DEBUG
/* We cannot use GOBJECT_IF_DEBUG here because of the G_HAS_CONSTRUCTORS
* conditional in between, as the C spec leaves conditionals inside macro
* expansions as undefined behavior. Only GCC and Clang are known to work
* but compilation breaks on MSVC.
*
* See: https://bugzilla.gnome.org/show_bug.cgi?id=769504
*/
if (_g_type_debug_flags & G_TYPE_DEBUG_OBJECTS) \
{
debug_objects_ht = g_hash_table_new (g_direct_hash, NULL);
# ifndef G_HAS_CONSTRUCTORS
g_atexit (debug_objects_atexit);
# endif /* G_HAS_CONSTRUCTORS */
}
#endif /* G_ENABLE_DEBUG */
}
/* Initialize the global GParamSpecPool; this function needs to be
* called whenever we access the GParamSpecPool and we cannot guarantee
* that g_object_do_class_init() has been called: for instance, by the
* interface property API.
*
* To avoid yet another global lock, we use atomic pointer checks: the
* first caller of this function will win the race. Any other access to
* the GParamSpecPool is done under its own mutex.
*/
static inline void
g_object_init_pspec_pool (void)
{
if (G_UNLIKELY (g_atomic_pointer_get (&pspec_pool) == NULL))
{
GParamSpecPool *pool = g_param_spec_pool_new (TRUE);
if (!g_atomic_pointer_compare_and_exchange (&pspec_pool, NULL, pool))
g_param_spec_pool_free (pool);
}
}
static void
g_object_base_class_init (GObjectClass *class)
{
GObjectClass *pclass = g_type_class_peek_parent (class);
/* Don't inherit HAS_DERIVED_CLASS flag from parent class */
class->flags &= ~CLASS_HAS_DERIVED_CLASS_FLAG;
if (pclass)
pclass->flags |= CLASS_HAS_DERIVED_CLASS_FLAG;
/* reset instance specific fields and methods that don't get inherited */
class->construct_properties = pclass ? g_slist_copy (pclass->construct_properties) : NULL;
class->n_construct_properties = g_slist_length (class->construct_properties);
class->get_property = NULL;
class->set_property = NULL;
class->pspecs = NULL;
class->n_pspecs = 0;
}
static void
g_object_base_class_finalize (GObjectClass *class)
{
GList *list, *node;
_g_signals_destroy (G_OBJECT_CLASS_TYPE (class));
g_slist_free (class->construct_properties);
class->construct_properties = NULL;
class->n_construct_properties = 0;
list = g_param_spec_pool_list_owned (pspec_pool, G_OBJECT_CLASS_TYPE (class));
for (node = list; node; node = node->next)
{
GParamSpec *pspec = node->data;
g_param_spec_pool_remove (pspec_pool, pspec);
PARAM_SPEC_SET_PARAM_ID (pspec, 0);
g_param_spec_unref (pspec);
}
g_list_free (list);
}
static void
g_object_do_class_init (GObjectClass *class)
{
/* read the comment about typedef struct CArray; on why not to change this quark */
quark_closure_array = g_quark_from_static_string ("GObject-closure-array");
quark_weak_refs = g_quark_from_static_string ("GObject-weak-references");
quark_weak_notifies = g_quark_from_static_string ("GObject-weak-notifies");
quark_weak_locations = g_quark_from_static_string ("GObject-weak-locations");
quark_toggle_refs = g_quark_from_static_string ("GObject-toggle-references");
quark_notify_queue = g_quark_from_static_string ("GObject-notify-queue");
#ifndef HAVE_OPTIONAL_FLAGS
quark_in_construction = g_quark_from_static_string ("GObject-in-construction");
#endif
g_object_init_pspec_pool ();
class->constructor = g_object_constructor;
class->constructed = g_object_constructed;
class->set_property = g_object_do_set_property;
class->get_property = g_object_do_get_property;
class->dispose = g_object_real_dispose;
class->finalize = g_object_finalize;
class->dispatch_properties_changed = g_object_dispatch_properties_changed;
class->notify = NULL;
/**
* GObject::notify:
* @gobject: the object which received the signal.
* @pspec: the #GParamSpec of the property which changed.
*
* The notify signal is emitted on an object when one of its properties has
* its value set through g_object_set_property(), g_object_set(), et al.
*
* Note that getting this signal doesn’t itself guarantee that the value of
* the property has actually changed. When it is emitted is determined by the
* derived GObject class. If the implementor did not create the property with
* %G_PARAM_EXPLICIT_NOTIFY, then any call to g_object_set_property() results
* in ::notify being emitted, even if the new value is the same as the old.
* If they did pass %G_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only
* when they explicitly call g_object_notify() or g_object_notify_by_pspec(),
* and common practice is to do that only when the value has actually changed.
*
* This signal is typically used to obtain change notification for a
* single property, by specifying the property name as a detail in the
* g_signal_connect() call, like this:
*
* |[<!-- language="C" -->
* g_signal_connect (text_view->buffer, "notify::paste-target-list",
* G_CALLBACK (gtk_text_view_target_list_notify),
* text_view)
* ]|
*
* It is important to note that you must use
* [canonical parameter names][canonical-parameter-names] as
* detail strings for the notify signal.
*/
gobject_signals[NOTIFY] =
g_signal_new (g_intern_static_string ("notify"),
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GObjectClass, notify),
NULL, NULL,
NULL,
G_TYPE_NONE,
1, G_TYPE_PARAM);
/* Install a check function that we'll use to verify that classes that
* implement an interface implement all properties for that interface
*/
g_type_add_interface_check (NULL, object_interface_check_properties);
}
/* Sinks @pspec if it’s a floating ref. */
static inline gboolean
install_property_internal (GType g_type,
guint property_id,
GParamSpec *pspec)
{
g_param_spec_ref_sink (pspec);
g_object_init_pspec_pool ();
if (g_param_spec_pool_lookup (pspec_pool, pspec->name, g_type, FALSE))
{
g_critical ("When installing property: type '%s' already has a property named '%s'",
g_type_name (g_type),
pspec->name);
g_param_spec_unref (pspec);
return FALSE;
}
PARAM_SPEC_SET_PARAM_ID (pspec, property_id);
g_param_spec_pool_insert (pspec_pool, g_steal_pointer (&pspec), g_type);
return TRUE;
}
static gboolean
validate_pspec_to_install (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
g_return_val_if_fail (PARAM_SPEC_PARAM_ID (pspec) == 0, FALSE); /* paranoid */
g_return_val_if_fail (pspec->flags & (G_PARAM_READABLE | G_PARAM_WRITABLE), FALSE);
if (pspec->flags & G_PARAM_CONSTRUCT)
g_return_val_if_fail ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0, FALSE);
if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
g_return_val_if_fail (pspec->flags & G_PARAM_WRITABLE, FALSE);
return TRUE;
}
/* Sinks @pspec if it’s a floating ref. */
static gboolean
validate_and_install_class_property (GObjectClass *class,
GType oclass_type,
GType parent_type,
guint property_id,
GParamSpec *pspec)
{
if (!validate_pspec_to_install (pspec))
{
g_param_spec_ref_sink (pspec);
g_param_spec_unref (pspec);
return FALSE;
}
if (pspec->flags & G_PARAM_WRITABLE)
g_return_val_if_fail (class->set_property != NULL, FALSE);
if (pspec->flags & G_PARAM_READABLE)
g_return_val_if_fail (class->get_property != NULL, FALSE);
class->flags |= CLASS_HAS_PROPS_FLAG;
if (install_property_internal (oclass_type, property_id, pspec))
{
if (pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
{
class->construct_properties = g_slist_append (class->construct_properties, pspec);
class->n_construct_properties += 1;
}
/* for property overrides of construct properties, we have to get rid
* of the overridden inherited construct property
*/
pspec = g_param_spec_pool_lookup (pspec_pool, pspec->name, parent_type, TRUE);
if (pspec && pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY))
{
class->construct_properties = g_slist_remove (class->construct_properties, pspec);
class->n_construct_properties -= 1;
}
return TRUE;
}
else
return FALSE;
}
/**
* g_object_class_install_property:
* @oclass: a #GObjectClass
* @property_id: the id for the new property
* @pspec: the #GParamSpec for the new property
*
* Installs a new property.
*
* All properties should be installed during the class initializer. It
* is possible to install properties after that, but doing so is not
* recommend, and specifically, is not guaranteed to be thread-safe vs.
* use of properties on the same type on other threads.
*
* Note that it is possible to redefine a property in a derived class,
* by installing a property with the same name. This can be useful at times,
* e.g. to change the range of allowed values or the default value.
*/
void
g_object_class_install_property (GObjectClass *class,
guint property_id,
GParamSpec *pspec)
{
GType oclass_type, parent_type;
g_return_if_fail (G_IS_OBJECT_CLASS (class));
g_return_if_fail (property_id > 0);
oclass_type = G_OBJECT_CLASS_TYPE (class);
parent_type = g_type_parent (oclass_type);
if (CLASS_HAS_DERIVED_CLASS (class))
g_error ("Attempt to add property %s::%s to class after it was derived", G_OBJECT_CLASS_NAME (class), pspec->name);
(void) validate_and_install_class_property (class,
oclass_type,
parent_type,
property_id,
pspec);
}
typedef struct {
const char *name;
GParamSpec *pspec;
} PspecEntry;
static int
compare_pspec_entry (const void *a,
const void *b)
{
const PspecEntry *ae = a;
const PspecEntry *be = b;
return ae->name < be->name ? -1 : (ae->name > be->name ? 1 : 0);
}
/* This uses pointer comparisons with @property_name, so
* will only work with string literals. */
static inline GParamSpec *
find_pspec (GObjectClass *class,
const char *property_name)
{
const PspecEntry *pspecs = (const PspecEntry *)class->pspecs;
gsize n_pspecs = class->n_pspecs;
g_assert (n_pspecs <= G_MAXSSIZE);
/* The limit for choosing between linear and binary search is
* fairly arbitrary.
*
* Both searches use pointer comparisons against @property_name.
* If this function is called with a non-static @property_name,
* it will fall through to the g_param_spec_pool_lookup() case.
* That’s OK; this is an opportunistic optimisation which relies
* on the fact that *most* (but not all) property lookups use
* static property names.
*/
if (n_pspecs < 10)
{
for (gsize i = 0; i < n_pspecs; i++)
{
if (pspecs[i].name == property_name)
return pspecs[i].pspec;
}
}
else
{
gssize lower = 0;
gssize upper = (int)class->n_pspecs - 1;
gssize mid;
while (lower <= upper)
{
mid = (lower + upper) / 2;
if (property_name < pspecs[mid].name)
upper = mid - 1;
else if (property_name > pspecs[mid].name)
lower = mid + 1;
else
return pspecs[mid].pspec;
}
}
return g_param_spec_pool_lookup (pspec_pool,
property_name,
((GTypeClass *)class)->g_type,
TRUE);
}
/**
* g_object_class_install_properties:
* @oclass: a #GObjectClass
* @n_pspecs: the length of the #GParamSpecs array
* @pspecs: (array length=n_pspecs): the #GParamSpecs array
* defining the new properties
*
* Installs new properties from an array of #GParamSpecs.
*
* All properties should be installed during the class initializer. It
* is possible to install properties after that, but doing so is not
* recommend, and specifically, is not guaranteed to be thread-safe vs.
* use of properties on the same type on other threads.
*
* The property id of each property is the index of each #GParamSpec in
* the @pspecs array.
*
* The property id of 0 is treated specially by #GObject and it should not
* be used to store a #GParamSpec.
*
* This function should be used if you plan to use a static array of
* #GParamSpecs and g_object_notify_by_pspec(). For instance, this
* class initialization:
*
* |[<!-- language="C" -->
* typedef enum {
* PROP_FOO = 1,
* PROP_BAR,
* N_PROPERTIES
* } MyObjectProperty;
*
* static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
*
* static void
* my_object_class_init (MyObjectClass *klass)
* {
* GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
*
* obj_properties[PROP_FOO] =
* g_param_spec_int ("foo", NULL, NULL,
* -1, G_MAXINT,
* 0,
* G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
*
* obj_properties[PROP_BAR] =
* g_param_spec_string ("bar", NULL, NULL,
* NULL,
* G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
*
* gobject_class->set_property = my_object_set_property;
* gobject_class->get_property = my_object_get_property;
* g_object_class_install_properties (gobject_class,
* G_N_ELEMENTS (obj_properties),
* obj_properties);
* }
* ]|
*
* allows calling g_object_notify_by_pspec() to notify of property changes:
*
* |[<!-- language="C" -->
* void
* my_object_set_foo (MyObject *self, gint foo)
* {
* if (self->foo != foo)
* {
* self->foo = foo;
* g_object_notify_by_pspec (G_OBJECT (self), obj_properties[PROP_FOO]);
* }
* }
* ]|
*
* Since: 2.26
*/
void
g_object_class_install_properties (GObjectClass *oclass,
guint n_pspecs,
GParamSpec **pspecs)
{
GType oclass_type, parent_type;
guint i;
g_return_if_fail (G_IS_OBJECT_CLASS (oclass));
g_return_if_fail (n_pspecs > 1);
g_return_if_fail (pspecs[0] == NULL);
if (CLASS_HAS_DERIVED_CLASS (oclass))
g_error ("Attempt to add properties to %s after it was derived",
G_OBJECT_CLASS_NAME (oclass));
oclass_type = G_OBJECT_CLASS_TYPE (oclass);
parent_type = g_type_parent (oclass_type);
/* we skip the first element of the array as it would have a 0 prop_id */
for (i = 1; i < n_pspecs; i++)
{
GParamSpec *pspec = pspecs[i];
if (!validate_and_install_class_property (oclass,
oclass_type,
parent_type,
i,
pspec))
{
break;
}
}
/* Save a copy of the pspec array inside the class struct. This
* makes it faster to look up pspecs for the class in future when
* acting on those properties.
*
* If a pspec is not in this cache array, calling code will fall
* back to using g_param_spec_pool_lookup(), so a pspec not being
* in this array is a (potential) performance problem but not a
* correctness problem. */
if (oclass->pspecs == NULL)
{
PspecEntry *entries;
entries = g_new (PspecEntry, n_pspecs - 1);
for (i = 1; i < n_pspecs; i++)
{
entries[i - 1].name = pspecs[i]->name;
entries[i - 1].pspec = pspecs[i];
}
qsort (entries, n_pspecs - 1, sizeof (PspecEntry), compare_pspec_entry);
oclass->pspecs = entries;
oclass->n_pspecs = n_pspecs - 1;
}
}
/**
* g_object_interface_install_property:
* @g_iface: (type GObject.TypeInterface): any interface vtable for the
* interface, or the default
* vtable for the interface.
* @pspec: the #GParamSpec for the new property
*
* Add a property to an interface; this is only useful for interfaces
* that are added to GObject-derived types. Adding a property to an
* interface forces all objects classes with that interface to have a
* compatible property. The compatible property could be a newly
* created #GParamSpec, but normally
* g_object_class_override_property() will be used so that the object
* class only needs to provide an implementation and inherits the
* property description, default value, bounds, and so forth from the
* interface property.
*
* This function is meant to be called from the interface's default
* vtable initialization function (the @class_init member of
* #GTypeInfo.) It must not be called after after @class_init has
* been called for any object types implementing this interface.
*
* If @pspec is a floating reference, it will be consumed.
*
* Since: 2.4
*/
void
g_object_interface_install_property (gpointer g_iface,
GParamSpec *pspec)
{
GTypeInterface *iface_class = g_iface;
g_return_if_fail (G_TYPE_IS_INTERFACE (iface_class->g_type));
g_return_if_fail (!G_IS_PARAM_SPEC_OVERRIDE (pspec)); /* paranoid */
if (!validate_pspec_to_install (pspec))
{
g_param_spec_ref_sink (pspec);
g_param_spec_unref (pspec);
return;
}
(void) install_property_internal (iface_class->g_type, 0, pspec);
}
/* Inlined version of g_param_spec_get_redirect_target(), for speed */
static inline void
param_spec_follow_override (GParamSpec **pspec)
{
if (((GTypeInstance *) (*pspec))->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
*pspec = ((GParamSpecOverride *) (*pspec))->overridden;
}
/**
* g_object_class_find_property:
* @oclass: a #GObjectClass
* @property_name: the name of the property to look up
*
* Looks up the #GParamSpec for a property of a class.
*
* Returns: (transfer none): the #GParamSpec for the property, or
* %NULL if the class doesn't have a property of that name
*/
GParamSpec*
g_object_class_find_property (GObjectClass *class,
const gchar *property_name)
{
GParamSpec *pspec;
g_return_val_if_fail (G_IS_OBJECT_CLASS (class), NULL);
g_return_val_if_fail (property_name != NULL, NULL);
pspec = find_pspec (class, property_name);
if (pspec)
param_spec_follow_override (&pspec);