forked from GNOME/glib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgparam.c
1593 lines (1399 loc) · 43.8 KB
/
gparam.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) 1997-1999, 2000-2001 Tim Janik and Red Hat, Inc.
*
* 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
*/
#include "config.h"
#include <string.h>
#include "gparam.h"
#include "gparamspecs.h"
#include "gvaluecollector.h"
#include "gtype-private.h"
/**
* SECTION:gparamspec
* @short_description: Metadata for parameter specifications
* @see_also: g_object_class_install_property(), g_object_set(),
* g_object_get(), g_object_set_property(), g_object_get_property(),
* g_value_register_transform_func()
* @title: GParamSpec
*
* #GParamSpec is an object structure that encapsulates the metadata
* required to specify parameters, such as e.g. #GObject properties.
*
* ## Parameter names # {#canonical-parameter-names}
*
* Parameter names need to start with a letter (a-z or A-Z).
* Subsequent characters can be letters, numbers or a '-'.
* All other characters are replaced by a '-' during construction.
* The result of this replacement is called the canonical name of
* the parameter.
*/
/* --- defines --- */
#define PARAM_FLOATING_FLAG 0x2
#define G_PARAM_USER_MASK (~0U << G_PARAM_USER_SHIFT)
#define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
/* --- prototypes --- */
static void g_param_spec_class_base_init (GParamSpecClass *class);
static void g_param_spec_class_base_finalize (GParamSpecClass *class);
static void g_param_spec_class_init (GParamSpecClass *class,
gpointer class_data);
static void g_param_spec_init (GParamSpec *pspec,
GParamSpecClass *class);
static void g_param_spec_finalize (GParamSpec *pspec);
static void value_param_init (GValue *value);
static void value_param_free_value (GValue *value);
static void value_param_copy_value (const GValue *src_value,
GValue *dest_value);
static void value_param_transform_value (const GValue *src_value,
GValue *dest_value);
static gpointer value_param_peek_pointer (const GValue *value);
static gchar* value_param_collect_value (GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags);
static gchar* value_param_lcopy_value (const GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags);
typedef struct
{
GValue default_value;
GQuark name_quark;
} GParamSpecPrivate;
static gint g_param_private_offset;
/* --- functions --- */
static inline GParamSpecPrivate *
g_param_spec_get_private (GParamSpec *pspec)
{
return &G_STRUCT_MEMBER (GParamSpecPrivate, pspec, g_param_private_offset);
}
void
_g_param_type_init (void)
{
static const GTypeFundamentalInfo finfo = {
(G_TYPE_FLAG_CLASSED |
G_TYPE_FLAG_INSTANTIATABLE |
G_TYPE_FLAG_DERIVABLE |
G_TYPE_FLAG_DEEP_DERIVABLE),
};
static const GTypeValueTable param_value_table = {
value_param_init, /* value_init */
value_param_free_value, /* value_free */
value_param_copy_value, /* value_copy */
value_param_peek_pointer, /* value_peek_pointer */
"p", /* collect_format */
value_param_collect_value, /* collect_value */
"p", /* lcopy_format */
value_param_lcopy_value, /* lcopy_value */
};
const GTypeInfo param_spec_info = {
sizeof (GParamSpecClass),
(GBaseInitFunc) g_param_spec_class_base_init,
(GBaseFinalizeFunc) g_param_spec_class_base_finalize,
(GClassInitFunc) g_param_spec_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (GParamSpec),
0, /* n_preallocs */
(GInstanceInitFunc) g_param_spec_init,
¶m_value_table,
};
GType type;
/* This should be registered as GParamSpec instead of GParam, for
* consistency sake, so that type name can be mapped to struct name,
* However, some language bindings, most noticeable the python ones
* depends on the "GParam" identifier, see #548689
*/
type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
g_assert (type == G_TYPE_PARAM);
g_param_private_offset = g_type_add_instance_private (type, sizeof (GParamSpecPrivate));
g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
}
static void
g_param_spec_class_base_init (GParamSpecClass *class)
{
}
static void
g_param_spec_class_base_finalize (GParamSpecClass *class)
{
}
static void
g_param_spec_class_init (GParamSpecClass *class,
gpointer class_data)
{
class->value_type = G_TYPE_NONE;
class->finalize = g_param_spec_finalize;
class->value_set_default = NULL;
class->value_validate = NULL;
class->values_cmp = NULL;
g_type_class_adjust_private_offset (class, &g_param_private_offset);
}
static void
g_param_spec_init (GParamSpec *pspec,
GParamSpecClass *class)
{
pspec->name = NULL;
pspec->_nick = NULL;
pspec->_blurb = NULL;
pspec->flags = 0;
pspec->value_type = class->value_type;
pspec->owner_type = 0;
pspec->qdata = NULL;
g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
pspec->ref_count = 1;
pspec->param_id = 0;
}
static void
g_param_spec_finalize (GParamSpec *pspec)
{
GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
if (priv->default_value.g_type)
g_value_reset (&priv->default_value);
g_datalist_clear (&pspec->qdata);
if (!(pspec->flags & G_PARAM_STATIC_NICK))
g_free (pspec->_nick);
if (!(pspec->flags & G_PARAM_STATIC_BLURB))
g_free (pspec->_blurb);
g_type_free_instance ((GTypeInstance*) pspec);
}
/**
* g_param_spec_ref: (skip)
* @pspec: a valid #GParamSpec
*
* Increments the reference count of @pspec.
*
* Returns: the #GParamSpec that was passed into this function
*/
GParamSpec*
g_param_spec_ref (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
g_atomic_int_inc ((int *)&pspec->ref_count);
return pspec;
}
/**
* g_param_spec_unref: (skip)
* @pspec: a valid #GParamSpec
*
* Decrements the reference count of a @pspec.
*/
void
g_param_spec_unref (GParamSpec *pspec)
{
gboolean is_zero;
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
if (G_UNLIKELY (is_zero))
{
G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
}
}
/**
* g_param_spec_sink:
* @pspec: a valid #GParamSpec
*
* The initial reference count of a newly created #GParamSpec is 1,
* even though no one has explicitly called g_param_spec_ref() on it
* yet. So the initial reference count is flagged as "floating", until
* someone calls `g_param_spec_ref (pspec); g_param_spec_sink
* (pspec);` in sequence on it, taking over the initial
* reference count (thus ending up with a @pspec that has a reference
* count of 1 still, but is not flagged "floating" anymore).
*/
void
g_param_spec_sink (GParamSpec *pspec)
{
gsize oldvalue;
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
if (oldvalue & PARAM_FLOATING_FLAG)
g_param_spec_unref (pspec);
}
/**
* g_param_spec_ref_sink: (skip)
* @pspec: a valid #GParamSpec
*
* Convenience function to ref and sink a #GParamSpec.
*
* Since: 2.10
* Returns: the #GParamSpec that was passed into this function
*/
GParamSpec*
g_param_spec_ref_sink (GParamSpec *pspec)
{
gsize oldvalue;
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
if (!(oldvalue & PARAM_FLOATING_FLAG))
g_param_spec_ref (pspec);
return pspec;
}
/**
* g_param_spec_get_name:
* @pspec: a valid #GParamSpec
*
* Get the name of a #GParamSpec.
*
* The name is always an "interned" string (as per g_intern_string()).
* This allows for pointer-value comparisons.
*
* Returns: the name of @pspec.
*/
const gchar *
g_param_spec_get_name (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
return pspec->name;
}
/**
* g_param_spec_get_nick:
* @pspec: a valid #GParamSpec
*
* Get the nickname of a #GParamSpec.
*
* Returns: the nickname of @pspec.
*/
const gchar *
g_param_spec_get_nick (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
if (pspec->_nick)
return pspec->_nick;
else
{
GParamSpec *redirect_target;
redirect_target = g_param_spec_get_redirect_target (pspec);
if (redirect_target && redirect_target->_nick)
return redirect_target->_nick;
}
return pspec->name;
}
/**
* g_param_spec_get_blurb:
* @pspec: a valid #GParamSpec
*
* Get the short description of a #GParamSpec.
*
* Returns: the short description of @pspec.
*/
const gchar *
g_param_spec_get_blurb (GParamSpec *pspec)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
if (pspec->_blurb)
return pspec->_blurb;
else
{
GParamSpec *redirect_target;
redirect_target = g_param_spec_get_redirect_target (pspec);
if (redirect_target && redirect_target->_blurb)
return redirect_target->_blurb;
}
return NULL;
}
static void
canonicalize_key (gchar *key)
{
gchar *p;
for (p = key; *p != 0; p++)
{
gchar c = *p;
if (c != '-' &&
(c < '0' || c > '9') &&
(c < 'A' || c > 'Z') &&
(c < 'a' || c > 'z'))
*p = '-';
}
}
static gboolean
is_canonical (const gchar *key)
{
const gchar *p;
for (p = key; *p != 0; p++)
{
gchar c = *p;
if (c != '-' &&
(c < '0' || c > '9') &&
(c < 'A' || c > 'Z') &&
(c < 'a' || c > 'z'))
return FALSE;
}
return TRUE;
}
/**
* g_param_spec_internal: (skip)
* @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
* @name: the canonical name of the property
* @nick: the nickname of the property
* @blurb: a short description of the property
* @flags: a combination of #GParamFlags
*
* Creates a new #GParamSpec instance.
*
* A property name consists of segments consisting of ASCII letters and
* digits, separated by either the '-' or '_' character. The first
* character of a property name must be a letter. Names which violate these
* rules lead to undefined behaviour.
*
* When creating and looking up a #GParamSpec, either separator can be
* used, but they cannot be mixed. Using '-' is considerably more
* efficient and in fact required when using property names as detail
* strings for signals.
*
* Beyond the name, #GParamSpecs have two more descriptive
* strings associated with them, the @nick, which should be suitable
* for use as a label for the property in a property editor, and the
* @blurb, which should be a somewhat longer description, suitable for
* e.g. a tooltip. The @nick and @blurb should ideally be localized.
*
* Returns: (type GObject.ParamSpec): a newly allocated #GParamSpec instance
*/
gpointer
g_param_spec_internal (GType param_type,
const gchar *name,
const gchar *nick,
const gchar *blurb,
GParamFlags flags)
{
GParamSpec *pspec;
GParamSpecPrivate *priv;
g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
pspec = (gpointer) g_type_create_instance (param_type);
if (flags & G_PARAM_STATIC_NAME)
{
/* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
pspec->name = (gchar *) g_intern_static_string (name);
if (!is_canonical (pspec->name))
g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
}
else
{
if (is_canonical (name))
pspec->name = (gchar *) g_intern_string (name);
else
{
gchar *tmp = g_strdup (name);
canonicalize_key (tmp);
pspec->name = (gchar *) g_intern_string (tmp);
g_free (tmp);
}
}
priv = g_param_spec_get_private (pspec);
priv->name_quark = g_quark_from_string (pspec->name);
if (flags & G_PARAM_STATIC_NICK)
pspec->_nick = (gchar*) nick;
else
pspec->_nick = g_strdup (nick);
if (flags & G_PARAM_STATIC_BLURB)
pspec->_blurb = (gchar*) blurb;
else
pspec->_blurb = g_strdup (blurb);
pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
return pspec;
}
/**
* g_param_spec_get_qdata:
* @pspec: a valid #GParamSpec
* @quark: a #GQuark, naming the user data pointer
*
* Gets back user data pointers stored via g_param_spec_set_qdata().
*
* Returns: (transfer none): the user data pointer set, or %NULL
*/
gpointer
g_param_spec_get_qdata (GParamSpec *pspec,
GQuark quark)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
}
/**
* g_param_spec_set_qdata:
* @pspec: the #GParamSpec to set store a user data pointer
* @quark: a #GQuark, naming the user data pointer
* @data: an opaque user data pointer
*
* Sets an opaque, named pointer on a #GParamSpec. The name is
* specified through a #GQuark (retrieved e.g. via
* g_quark_from_static_string()), and the pointer can be gotten back
* from the @pspec with g_param_spec_get_qdata(). Setting a
* previously set user data pointer, overrides (frees) the old pointer
* set, using %NULL as pointer essentially removes the data stored.
*/
void
g_param_spec_set_qdata (GParamSpec *pspec,
GQuark quark,
gpointer data)
{
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
g_return_if_fail (quark > 0);
g_datalist_id_set_data (&pspec->qdata, quark, data);
}
/**
* g_param_spec_set_qdata_full: (skip)
* @pspec: the #GParamSpec to set store a user data pointer
* @quark: a #GQuark, naming the user data pointer
* @data: an opaque user data pointer
* @destroy: function to invoke with @data as argument, when @data needs to
* be freed
*
* This function works like g_param_spec_set_qdata(), but in addition,
* a `void (*destroy) (gpointer)` function may be
* specified which is called with @data as argument when the @pspec is
* finalized, or the data is being overwritten by a call to
* g_param_spec_set_qdata() with the same @quark.
*/
void
g_param_spec_set_qdata_full (GParamSpec *pspec,
GQuark quark,
gpointer data,
GDestroyNotify destroy)
{
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
g_return_if_fail (quark > 0);
g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
}
/**
* g_param_spec_steal_qdata:
* @pspec: the #GParamSpec to get a stored user data pointer from
* @quark: a #GQuark, naming the user data pointer
*
* Gets back user data pointers stored via g_param_spec_set_qdata()
* and removes the @data from @pspec without invoking its destroy()
* function (if any was set). Usually, calling this function is only
* required to update user data pointers with a destroy notifier.
*
* Returns: (transfer none): the user data pointer set, or %NULL
*/
gpointer
g_param_spec_steal_qdata (GParamSpec *pspec,
GQuark quark)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
g_return_val_if_fail (quark > 0, NULL);
return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
}
/**
* g_param_spec_get_redirect_target:
* @pspec: a #GParamSpec
*
* If the paramspec redirects operations to another paramspec,
* returns that paramspec. Redirect is used typically for
* providing a new implementation of a property in a derived
* type while preserving all the properties from the parent
* type. Redirection is established by creating a property
* of type #GParamSpecOverride. See g_object_class_override_property()
* for an example of the use of this capability.
*
* Since: 2.4
*
* Returns: (transfer none): paramspec to which requests on this
* paramspec should be redirected, or %NULL if none.
*/
GParamSpec*
g_param_spec_get_redirect_target (GParamSpec *pspec)
{
GTypeInstance *inst = (GTypeInstance *)pspec;
if (inst && inst->g_class && inst->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
return ((GParamSpecOverride*)pspec)->overridden;
else
return NULL;
}
/**
* g_param_value_set_default:
* @pspec: a valid #GParamSpec
* @value: a #GValue of correct type for @pspec
*
* Sets @value to its default value as specified in @pspec.
*/
void
g_param_value_set_default (GParamSpec *pspec,
GValue *value)
{
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
g_return_if_fail (G_IS_VALUE (value));
g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
g_value_reset (value);
G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
}
/**
* g_param_value_defaults:
* @pspec: a valid #GParamSpec
* @value: a #GValue of correct type for @pspec
*
* Checks whether @value contains the default value as specified in @pspec.
*
* Returns: whether @value contains the canonical default for this @pspec
*/
gboolean
g_param_value_defaults (GParamSpec *pspec,
GValue *value)
{
GValue dflt_value = G_VALUE_INIT;
gboolean defaults;
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
g_return_val_if_fail (G_IS_VALUE (value), FALSE);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
g_value_unset (&dflt_value);
return defaults;
}
/**
* g_param_value_validate:
* @pspec: a valid #GParamSpec
* @value: a #GValue of correct type for @pspec
*
* Ensures that the contents of @value comply with the specifications
* set out by @pspec. For example, a #GParamSpecInt might require
* that integers stored in @value may not be smaller than -42 and not be
* greater than +42. If @value contains an integer outside of this range,
* it is modified accordingly, so the resulting value will fit into the
* range -42 .. +42.
*
* Returns: whether modifying @value was necessary to ensure validity
*/
gboolean
g_param_value_validate (GParamSpec *pspec,
GValue *value)
{
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
g_return_val_if_fail (G_IS_VALUE (value), FALSE);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
{
GValue oval = *value;
if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
memcmp (&oval.data, &value->data, sizeof (oval.data)))
return TRUE;
}
return FALSE;
}
/**
* g_param_value_convert:
* @pspec: a valid #GParamSpec
* @src_value: souce #GValue
* @dest_value: destination #GValue of correct type for @pspec
* @strict_validation: %TRUE requires @dest_value to conform to @pspec
* without modifications
*
* Transforms @src_value into @dest_value if possible, and then
* validates @dest_value, in order for it to conform to @pspec. If
* @strict_validation is %TRUE this function will only succeed if the
* transformed @dest_value complied to @pspec without modifications.
*
* See also g_value_type_transformable(), g_value_transform() and
* g_param_value_validate().
*
* Returns: %TRUE if transformation and validation were successful,
* %FALSE otherwise and @dest_value is left untouched.
*/
gboolean
g_param_value_convert (GParamSpec *pspec,
const GValue *src_value,
GValue *dest_value,
gboolean strict_validation)
{
GValue tmp_value = G_VALUE_INIT;
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
/* better leave dest_value untouched when returning FALSE */
g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
if (g_value_transform (src_value, &tmp_value) &&
(!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
{
g_value_unset (dest_value);
/* values are relocatable */
memcpy (dest_value, &tmp_value, sizeof (tmp_value));
return TRUE;
}
else
{
g_value_unset (&tmp_value);
return FALSE;
}
}
/**
* g_param_values_cmp:
* @pspec: a valid #GParamSpec
* @value1: a #GValue of correct type for @pspec
* @value2: a #GValue of correct type for @pspec
*
* Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
* if @value1 is found to be less than, equal to or greater than @value2,
* respectively.
*
* Returns: -1, 0 or +1, for a less than, equal to or greater than result
*/
gint
g_param_values_cmp (GParamSpec *pspec,
const GValue *value1,
const GValue *value2)
{
gint cmp;
/* param_values_cmp() effectively does: value1 - value2
* so the return values are:
* -1) value1 < value2
* 0) value1 == value2
* 1) value1 > value2
*/
g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
g_return_val_if_fail (G_IS_VALUE (value1), 0);
g_return_val_if_fail (G_IS_VALUE (value2), 0);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
return CLAMP (cmp, -1, 1);
}
static void
value_param_init (GValue *value)
{
value->data[0].v_pointer = NULL;
}
static void
value_param_free_value (GValue *value)
{
if (value->data[0].v_pointer)
g_param_spec_unref (value->data[0].v_pointer);
}
static void
value_param_copy_value (const GValue *src_value,
GValue *dest_value)
{
if (src_value->data[0].v_pointer)
dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
else
dest_value->data[0].v_pointer = NULL;
}
static void
value_param_transform_value (const GValue *src_value,
GValue *dest_value)
{
if (src_value->data[0].v_pointer &&
g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
else
dest_value->data[0].v_pointer = NULL;
}
static gpointer
value_param_peek_pointer (const GValue *value)
{
return value->data[0].v_pointer;
}
static gchar*
value_param_collect_value (GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags)
{
if (collect_values[0].v_pointer)
{
GParamSpec *param = collect_values[0].v_pointer;
if (param->g_type_instance.g_class == NULL)
return g_strconcat ("invalid unclassed param spec pointer for value type '",
G_VALUE_TYPE_NAME (value),
"'",
NULL);
else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
return g_strconcat ("invalid param spec type '",
G_PARAM_SPEC_TYPE_NAME (param),
"' for value type '",
G_VALUE_TYPE_NAME (value),
"'",
NULL);
value->data[0].v_pointer = g_param_spec_ref (param);
}
else
value->data[0].v_pointer = NULL;
return NULL;
}
static gchar*
value_param_lcopy_value (const GValue *value,
guint n_collect_values,
GTypeCValue *collect_values,
guint collect_flags)
{
GParamSpec **param_p = collect_values[0].v_pointer;
if (!param_p)
return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
if (!value->data[0].v_pointer)
*param_p = NULL;
else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
*param_p = value->data[0].v_pointer;
else
*param_p = g_param_spec_ref (value->data[0].v_pointer);
return NULL;
}
/* --- param spec pool --- */
/**
* GParamSpecPool:
*
* A #GParamSpecPool maintains a collection of #GParamSpecs which can be
* quickly accessed by owner and name. The implementation of the #GObject property
* system uses such a pool to store the #GParamSpecs of the properties all object
* types.
*/
struct _GParamSpecPool
{
GMutex mutex;
gboolean type_prefixing;
GHashTable *hash_table;
};
static guint
param_spec_pool_hash (gconstpointer key_spec)
{
const GParamSpec *key = key_spec;
const gchar *p;
guint h = key->owner_type;
for (p = key->name; *p; p++)
h = (h << 5) - h + *p;
return h;
}
static gboolean
param_spec_pool_equals (gconstpointer key_spec_1,
gconstpointer key_spec_2)
{
const GParamSpec *key1 = key_spec_1;
const GParamSpec *key2 = key_spec_2;
return (key1->owner_type == key2->owner_type &&
strcmp (key1->name, key2->name) == 0);
}
/**
* g_param_spec_pool_new:
* @type_prefixing: Whether the pool will support type-prefixed property names.
*
* Creates a new #GParamSpecPool.
*
* If @type_prefixing is %TRUE, lookups in the newly created pool will
* allow to specify the owner as a colon-separated prefix of the
* property name, like "GtkContainer:border-width". This feature is
* deprecated, so you should always set @type_prefixing to %FALSE.
*
* Returns: (transfer none): a newly allocated #GParamSpecPool.
*/
GParamSpecPool*
g_param_spec_pool_new (gboolean type_prefixing)
{
static GMutex init_mutex;
GParamSpecPool *pool = g_new (GParamSpecPool, 1);
memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
pool->type_prefixing = type_prefixing != FALSE;
pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
return pool;
}
/**
* g_param_spec_pool_insert:
* @pool: a #GParamSpecPool.
* @pspec: the #GParamSpec to insert
* @owner_type: a #GType identifying the owner of @pspec
*
* Inserts a #GParamSpec in the pool.
*/
void
g_param_spec_pool_insert (GParamSpecPool *pool,
GParamSpec *pspec,
GType owner_type)
{
const gchar *p;
if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
{
for (p = pspec->name; *p; p++)
{
if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
{
g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
return;
}
}
g_mutex_lock (&pool->mutex);
pspec->owner_type = owner_type;
g_param_spec_ref (pspec);
g_hash_table_add (pool->hash_table, pspec);
g_mutex_unlock (&pool->mutex);
}
else
{
g_return_if_fail (pool != NULL);
g_return_if_fail (pspec);
g_return_if_fail (owner_type > 0);
g_return_if_fail (pspec->owner_type == 0);
}
}
/**
* g_param_spec_pool_remove:
* @pool: a #GParamSpecPool
* @pspec: the #GParamSpec to remove
*
* Removes a #GParamSpec from the pool.
*/
void
g_param_spec_pool_remove (GParamSpecPool *pool,
GParamSpec *pspec)
{
if (pool && pspec)
{
g_mutex_lock (&pool->mutex);
if (g_hash_table_remove (pool->hash_table, pspec))
g_param_spec_unref (pspec);
else
g_warning (G_STRLOC ": attempt to remove unknown pspec '%s' from pool", pspec->name);
g_mutex_unlock (&pool->mutex);
}
else
{
g_return_if_fail (pool != NULL);
g_return_if_fail (pspec);
}
}
static inline GParamSpec*
param_spec_ht_lookup (GHashTable *hash_table,
const gchar *param_name,
GType owner_type,
gboolean walk_ancestors)
{
GParamSpec key, *pspec;
key.owner_type = owner_type;
key.name = (gchar*) param_name;
if (walk_ancestors)