Coverage Report

Created: 2025-07-11 06:47

/src/tinysparql/subprojects/glib-2.80.3/gobject/gparam.c
Line
Count
Source (jump to first uncovered line)
1
/* GObject - GLib Type, Object, Parameter and Signal Library
2
 * Copyright (C) 1997-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
22
 */
23
24
#include "config.h"
25
26
#include <string.h>
27
28
#include "gparam.h"
29
#include "gparamspecs.h"
30
#include "gvaluecollector.h"
31
#include "gtype-private.h"
32
33
/**
34
 * GParamSpec: (ref-func g_param_spec_ref_sink) (unref-func g_param_spec_unref) (set-value-func g_value_set_param) (get-value-func g_value_get_param)
35
 * @g_type_instance: private `GTypeInstance` portion
36
 * @name: name of this parameter: always an interned string
37
 * @flags: `GParamFlags` flags for this parameter
38
 * @value_type: the `GValue` type for this parameter
39
 * @owner_type: `GType` type that uses (introduces) this parameter
40
 *
41
 * `GParamSpec` encapsulates the metadata required to specify parameters, such as `GObject` properties.
42
 *
43
 * ## Parameter names
44
 *
45
 * A property name consists of one or more segments consisting of ASCII letters
46
 * and digits, separated by either the `-` or `_` character. The first
47
 * character of a property name must be a letter. These are the same rules as
48
 * for signal naming (see [func@GObject.signal_new]).
49
 *
50
 * When creating and looking up a `GParamSpec`, either separator can be
51
 * used, but they cannot be mixed. Using `-` is considerably more
52
 * efficient, and is the ‘canonical form’. Using `_` is discouraged.
53
 */
54
55
/* --- defines --- */
56
42
#define PARAM_FLOATING_FLAG                     0x2
57
21
#define G_PARAM_USER_MASK     (~0U << G_PARAM_USER_SHIFT)
58
#define PSPEC_APPLIES_TO_VALUE(pspec, value)  (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
59
60
/* --- prototypes --- */
61
static void g_param_spec_class_base_init   (GParamSpecClass *class);
62
static void g_param_spec_class_base_finalize (GParamSpecClass *class);
63
static void g_param_spec_class_init    (GParamSpecClass *class,
64
              gpointer               class_data);
65
static void g_param_spec_init    (GParamSpec    *pspec,
66
              GParamSpecClass *class);
67
static void g_param_spec_finalize    (GParamSpec    *pspec);
68
static void value_param_init    (GValue   *value);
69
static void value_param_free_value    (GValue   *value);
70
static void value_param_copy_value    (const GValue *src_value,
71
             GValue   *dest_value);
72
static void value_param_transform_value (const GValue *src_value,
73
             GValue   *dest_value);
74
static gpointer value_param_peek_pointer  (const GValue *value);
75
static gchar* value_param_collect_value (GValue   *value,
76
             guint           n_collect_values,
77
             GTypeCValue    *collect_values,
78
             guint           collect_flags);
79
static gchar* value_param_lcopy_value   (const GValue *value,
80
             guint           n_collect_values,
81
             GTypeCValue    *collect_values,
82
             guint           collect_flags);
83
84
typedef struct
85
{
86
  GValue default_value;
87
  GQuark name_quark;
88
} GParamSpecPrivate;
89
90
static gint g_param_private_offset;
91
92
/* --- functions --- */
93
static inline GParamSpecPrivate *
94
g_param_spec_get_private (GParamSpec *pspec)
95
57
{
96
57
  return &G_STRUCT_MEMBER (GParamSpecPrivate, pspec, g_param_private_offset);
97
57
}
98
99
void
100
_g_param_type_init (void)
101
2
{
102
2
  static const GTypeFundamentalInfo finfo = {
103
2
    (G_TYPE_FLAG_CLASSED |
104
2
     G_TYPE_FLAG_INSTANTIATABLE |
105
2
     G_TYPE_FLAG_DERIVABLE |
106
2
     G_TYPE_FLAG_DEEP_DERIVABLE),
107
2
  };
108
2
  static const GTypeValueTable param_value_table = {
109
2
    value_param_init,           /* value_init */
110
2
    value_param_free_value,     /* value_free */
111
2
    value_param_copy_value,     /* value_copy */
112
2
    value_param_peek_pointer,   /* value_peek_pointer */
113
2
    "p",      /* collect_format */
114
2
    value_param_collect_value,  /* collect_value */
115
2
    "p",      /* lcopy_format */
116
2
    value_param_lcopy_value,    /* lcopy_value */
117
2
  };
118
2
  const GTypeInfo param_spec_info = {
119
2
    sizeof (GParamSpecClass),
120
121
2
    (GBaseInitFunc) g_param_spec_class_base_init,
122
2
    (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
123
2
    (GClassInitFunc) g_param_spec_class_init,
124
2
    (GClassFinalizeFunc) NULL,
125
2
    NULL, /* class_data */
126
127
2
    sizeof (GParamSpec),
128
2
    0,    /* n_preallocs */
129
2
    (GInstanceInitFunc) g_param_spec_init,
130
131
2
    &param_value_table,
132
2
  };
133
2
  GType type;
134
135
  /* This should be registered as GParamSpec instead of GParam, for
136
   * consistency sake, so that type name can be mapped to struct name,
137
   * However, some language bindings, most noticeable the python ones
138
   * depends on the "GParam" identifier, see #548689
139
   */
140
2
  type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), &param_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
141
2
  g_assert (type == G_TYPE_PARAM);
142
2
  g_param_private_offset = g_type_add_instance_private (type, sizeof (GParamSpecPrivate));
143
2
  g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
144
2
}
145
146
static void
147
g_param_spec_class_base_init (GParamSpecClass *class)
148
42
{
149
42
}
150
151
static void
152
g_param_spec_class_base_finalize (GParamSpecClass *class)
153
0
{
154
0
}
155
156
static void
157
g_param_spec_class_init (GParamSpecClass *class,
158
       gpointer         class_data)
159
2
{
160
2
  class->value_type = G_TYPE_NONE;
161
2
  class->finalize = g_param_spec_finalize;
162
2
  class->value_set_default = NULL;
163
2
  class->value_validate = NULL;
164
2
  class->values_cmp = NULL;
165
166
2
  g_type_class_adjust_private_offset (class, &g_param_private_offset);
167
2
}
168
169
static void
170
g_param_spec_init (GParamSpec      *pspec,
171
       GParamSpecClass *class)
172
21
{
173
21
  pspec->name = NULL;
174
21
  pspec->_nick = NULL;
175
21
  pspec->_blurb = NULL;
176
21
  pspec->flags = 0;
177
21
  pspec->value_type = class->value_type;
178
21
  pspec->owner_type = 0;
179
21
  pspec->qdata = NULL;
180
21
  g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
181
21
  pspec->ref_count = 1;
182
21
  pspec->param_id = 0;
183
21
}
184
185
static void
186
g_param_spec_finalize (GParamSpec *pspec)
187
0
{
188
0
  GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
189
190
0
  if (priv->default_value.g_type)
191
0
    g_value_reset (&priv->default_value);
192
193
0
  g_datalist_clear (&pspec->qdata);
194
195
0
  if (!(pspec->flags & G_PARAM_STATIC_NICK))
196
0
    g_free (pspec->_nick);
197
198
0
  if (!(pspec->flags & G_PARAM_STATIC_BLURB))
199
0
    g_free (pspec->_blurb);
200
201
0
  g_type_free_instance ((GTypeInstance*) pspec);
202
0
}
203
204
/**
205
 * g_param_spec_ref: (skip)
206
 * @pspec: (transfer none) (not nullable): a valid #GParamSpec
207
 *
208
 * Increments the reference count of @pspec.
209
 *
210
 * Returns: (transfer full) (not nullable): the #GParamSpec that was passed into this function
211
 */
212
GParamSpec*
213
g_param_spec_ref (GParamSpec *pspec)
214
21
{
215
21
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
216
217
21
  g_atomic_int_inc ((int *)&pspec->ref_count);
218
219
21
  return pspec;
220
21
}
221
222
/**
223
 * g_param_spec_unref: (skip)
224
 * @pspec: a valid #GParamSpec
225
 *
226
 * Decrements the reference count of a @pspec.
227
 */
228
void
229
g_param_spec_unref (GParamSpec *pspec)
230
0
{
231
0
  gboolean is_zero;
232
233
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
234
235
0
  is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
236
237
0
  if (G_UNLIKELY (is_zero))
238
0
    {
239
0
      G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
240
0
    }
241
0
}
242
243
/**
244
 * g_param_spec_sink:
245
 * @pspec: a valid #GParamSpec
246
 *
247
 * The initial reference count of a newly created #GParamSpec is 1,
248
 * even though no one has explicitly called g_param_spec_ref() on it
249
 * yet. So the initial reference count is flagged as "floating", until
250
 * someone calls `g_param_spec_ref (pspec); g_param_spec_sink
251
 * (pspec);` in sequence on it, taking over the initial
252
 * reference count (thus ending up with a @pspec that has a reference
253
 * count of 1 still, but is not flagged "floating" anymore).
254
 */
255
void
256
g_param_spec_sink (GParamSpec *pspec)
257
0
{
258
0
  guintptr oldvalue;
259
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
260
261
0
  oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
262
0
  if (oldvalue & PARAM_FLOATING_FLAG)
263
0
    g_param_spec_unref (pspec);
264
0
}
265
266
/**
267
 * g_param_spec_ref_sink: (skip)
268
 * @pspec: a valid #GParamSpec
269
 *
270
 * Convenience function to ref and sink a #GParamSpec.
271
 *
272
 * Since: 2.10
273
 * Returns: (transfer full) (not nullable): the #GParamSpec that was passed into this function
274
 */
275
GParamSpec*
276
g_param_spec_ref_sink (GParamSpec *pspec)
277
21
{
278
21
  guintptr oldvalue;
279
21
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
280
281
21
  oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
282
21
  if (!(oldvalue & PARAM_FLOATING_FLAG))
283
0
    g_param_spec_ref (pspec);
284
285
21
  return pspec;
286
21
}
287
288
/**
289
 * g_param_spec_get_name:
290
 * @pspec: a valid #GParamSpec
291
 *
292
 * Get the name of a #GParamSpec.
293
 *
294
 * The name is always an "interned" string (as per g_intern_string()).
295
 * This allows for pointer-value comparisons.
296
 *
297
 * Returns: the name of @pspec.
298
 */
299
const gchar *
300
g_param_spec_get_name (GParamSpec *pspec)
301
0
{
302
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
303
304
0
  return pspec->name;
305
0
}
306
307
/**
308
 * g_param_spec_get_nick:
309
 * @pspec: a valid #GParamSpec
310
 *
311
 * Get the nickname of a #GParamSpec.
312
 *
313
 * Returns: the nickname of @pspec.
314
 */
315
const gchar *
316
g_param_spec_get_nick (GParamSpec *pspec)
317
0
{
318
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
319
320
0
  if (pspec->_nick)
321
0
    return pspec->_nick;
322
0
  else
323
0
    {
324
0
      GParamSpec *redirect_target;
325
326
0
      redirect_target = g_param_spec_get_redirect_target (pspec);
327
0
      if (redirect_target && redirect_target->_nick)
328
0
  return redirect_target->_nick;
329
0
    }
330
331
0
  return pspec->name;
332
0
}
333
334
/**
335
 * g_param_spec_get_blurb:
336
 * @pspec: a valid #GParamSpec
337
 *
338
 * Get the short description of a #GParamSpec.
339
 *
340
 * Returns: (nullable): the short description of @pspec.
341
 */
342
const gchar *
343
g_param_spec_get_blurb (GParamSpec *pspec)
344
0
{
345
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
346
347
0
  if (pspec->_blurb)
348
0
    return pspec->_blurb;
349
0
  else
350
0
    {
351
0
      GParamSpec *redirect_target;
352
353
0
      redirect_target = g_param_spec_get_redirect_target (pspec);
354
0
      if (redirect_target && redirect_target->_blurb)
355
0
  return redirect_target->_blurb;
356
0
    }
357
358
0
  return NULL;
359
0
}
360
361
/* @key must have already been validated with is_valid()
362
 * Modifies @key in place. */
363
static void
364
canonicalize_key (gchar *key)
365
0
{
366
0
  gchar *p;
367
  
368
0
  for (p = key; *p != 0; p++)
369
0
    {
370
0
      gchar c = *p;
371
      
372
0
      if (c == '_')
373
0
        *p = '-';
374
0
    }
375
0
}
376
377
/* @key must have already been validated with is_valid() */
378
static gboolean
379
is_canonical (const gchar *key)
380
77
{
381
77
  return (strchr (key, '_') == NULL);
382
77
}
383
384
/**
385
 * g_param_spec_is_valid_name:
386
 * @name: the canonical name of the property
387
 *
388
 * Validate a property name for a #GParamSpec. This can be useful for
389
 * dynamically-generated properties which need to be validated at run-time
390
 * before actually trying to create them.
391
 *
392
 * See [canonical parameter names][canonical-parameter-names] for details of
393
 * the rules for valid names.
394
 *
395
 * Returns: %TRUE if @name is a valid property name, %FALSE otherwise.
396
 * Since: 2.66
397
 */
398
gboolean
399
g_param_spec_is_valid_name (const gchar *name)
400
24
{
401
24
  const gchar *p;
402
403
  /* First character must be a letter. */
404
24
  if ((name[0] < 'A' || name[0] > 'Z') &&
405
24
      (name[0] < 'a' || name[0] > 'z'))
406
0
    return FALSE;
407
408
272
  for (p = name; *p != 0; p++)
409
248
    {
410
248
      const gchar c = *p;
411
412
248
      if (c != '-' && c != '_' &&
413
248
          (c < '0' || c > '9') &&
414
248
          (c < 'A' || c > 'Z') &&
415
248
          (c < 'a' || c > 'z'))
416
0
        return FALSE;
417
248
    }
418
419
24
  return TRUE;
420
24
}
421
422
/**
423
 * g_param_spec_internal: (skip)
424
 * @param_type: the #GType for the property; must be derived from %G_TYPE_PARAM
425
 * @name: the canonical name of the property
426
 * @nick: (nullable): the nickname of the property
427
 * @blurb: (nullable): a short description of the property
428
 * @flags: a combination of #GParamFlags
429
 *
430
 * Creates a new #GParamSpec instance.
431
 *
432
 * See [canonical parameter names][canonical-parameter-names] for details of
433
 * the rules for @name. Names which violate these rules lead to undefined
434
 * behaviour.
435
 *
436
 * Beyond the name, #GParamSpecs have two more descriptive strings, the
437
 * @nick and @blurb, which may be used as a localized label and description.
438
 * For GTK and related libraries these are considered deprecated and may be
439
 * omitted, while for other libraries such as GStreamer and its plugins they
440
 * are essential. When in doubt, follow the conventions used in the
441
 * surrounding code and supporting libraries.
442
 *
443
 * Returns: (type GObject.ParamSpec): (transfer floating): a newly allocated
444
 *     #GParamSpec instance, which is initially floating
445
 */
446
gpointer
447
g_param_spec_internal (GType        param_type,
448
           const gchar *name,
449
           const gchar *nick,
450
           const gchar *blurb,
451
           GParamFlags  flags)
452
21
{
453
21
  GParamSpec *pspec;
454
21
  GParamSpecPrivate *priv;
455
  
456
21
  g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
457
21
  g_return_val_if_fail (name != NULL, NULL);
458
21
  g_return_val_if_fail (g_param_spec_is_valid_name (name), NULL);
459
21
  g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
460
  
461
21
  pspec = (gpointer) g_type_create_instance (param_type);
462
463
21
  if (flags & G_PARAM_STATIC_NAME)
464
14
    {
465
      /* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
466
14
      pspec->name = (gchar *) g_intern_static_string (name);
467
14
      if (!is_canonical (pspec->name))
468
0
        g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
469
14
    }
470
7
  else
471
7
    {
472
7
      if (is_canonical (name))
473
7
        pspec->name = (gchar *) g_intern_string (name);
474
0
      else
475
0
        {
476
0
          gchar *tmp = g_strdup (name);
477
0
          canonicalize_key (tmp);
478
0
          pspec->name = (gchar *) g_intern_string (tmp);
479
0
          g_free (tmp);
480
0
        }
481
7
    }
482
483
21
  priv = g_param_spec_get_private (pspec);
484
21
  priv->name_quark = g_quark_from_string (pspec->name);
485
486
21
  if (flags & G_PARAM_STATIC_NICK)
487
14
    pspec->_nick = (gchar*) nick;
488
7
  else
489
7
    pspec->_nick = g_strdup (nick);
490
491
21
  if (flags & G_PARAM_STATIC_BLURB)
492
14
    pspec->_blurb = (gchar*) blurb;
493
7
  else
494
7
    pspec->_blurb = g_strdup (blurb);
495
496
21
  pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
497
  
498
21
  return pspec;
499
21
}
500
501
/**
502
 * g_param_spec_get_qdata:
503
 * @pspec: a valid #GParamSpec
504
 * @quark: a #GQuark, naming the user data pointer
505
 *
506
 * Gets back user data pointers stored via g_param_spec_set_qdata().
507
 *
508
 * Returns: (transfer none) (nullable): the user data pointer set, or %NULL
509
 */
510
gpointer
511
g_param_spec_get_qdata (GParamSpec *pspec,
512
      GQuark      quark)
513
0
{
514
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
515
  
516
0
  return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
517
0
}
518
519
/**
520
 * g_param_spec_set_qdata:
521
 * @pspec: the #GParamSpec to set store a user data pointer
522
 * @quark: a #GQuark, naming the user data pointer
523
 * @data: (nullable): an opaque user data pointer
524
 *
525
 * Sets an opaque, named pointer on a #GParamSpec. The name is
526
 * specified through a #GQuark (retrieved e.g. via
527
 * g_quark_from_static_string()), and the pointer can be gotten back
528
 * from the @pspec with g_param_spec_get_qdata().  Setting a
529
 * previously set user data pointer, overrides (frees) the old pointer
530
 * set, using %NULL as pointer essentially removes the data stored.
531
 */
532
void
533
g_param_spec_set_qdata (GParamSpec *pspec,
534
      GQuark      quark,
535
      gpointer    data)
536
0
{
537
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
538
0
  g_return_if_fail (quark > 0);
539
540
0
  g_datalist_id_set_data (&pspec->qdata, quark, data);
541
0
}
542
543
/**
544
 * g_param_spec_set_qdata_full: (skip)
545
 * @pspec: the #GParamSpec to set store a user data pointer
546
 * @quark: a #GQuark, naming the user data pointer
547
 * @data: (nullable): an opaque user data pointer
548
 * @destroy: (nullable): function to invoke with @data as argument, when @data needs to
549
 *  be freed
550
 *
551
 * This function works like g_param_spec_set_qdata(), but in addition,
552
 * a `void (*destroy) (gpointer)` function may be
553
 * specified which is called with @data as argument when the @pspec is
554
 * finalized, or the data is being overwritten by a call to
555
 * g_param_spec_set_qdata() with the same @quark.
556
 */
557
void
558
g_param_spec_set_qdata_full (GParamSpec    *pspec,
559
           GQuark         quark,
560
           gpointer       data,
561
           GDestroyNotify destroy)
562
0
{
563
0
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
564
0
  g_return_if_fail (quark > 0);
565
566
0
  g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
567
0
}
568
569
/**
570
 * g_param_spec_steal_qdata:
571
 * @pspec: the #GParamSpec to get a stored user data pointer from
572
 * @quark: a #GQuark, naming the user data pointer
573
 *
574
 * Gets back user data pointers stored via g_param_spec_set_qdata()
575
 * and removes the @data from @pspec without invoking its destroy()
576
 * function (if any was set).  Usually, calling this function is only
577
 * required to update user data pointers with a destroy notifier.
578
 *
579
 * Returns: (transfer none) (nullable): the user data pointer set, or %NULL
580
 */
581
gpointer
582
g_param_spec_steal_qdata (GParamSpec *pspec,
583
        GQuark      quark)
584
0
{
585
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
586
0
  g_return_val_if_fail (quark > 0, NULL);
587
  
588
0
  return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
589
0
}
590
591
/**
592
 * g_param_spec_get_redirect_target:
593
 * @pspec: a #GParamSpec
594
 *
595
 * If the paramspec redirects operations to another paramspec,
596
 * returns that paramspec. Redirect is used typically for
597
 * providing a new implementation of a property in a derived
598
 * type while preserving all the properties from the parent
599
 * type. Redirection is established by creating a property
600
 * of type #GParamSpecOverride. See g_object_class_override_property()
601
 * for an example of the use of this capability.
602
 *
603
 * Since: 2.4
604
 *
605
 * Returns: (transfer none) (nullable): paramspec to which requests on this
606
 *          paramspec should be redirected, or %NULL if none.
607
 */
608
GParamSpec*
609
g_param_spec_get_redirect_target (GParamSpec *pspec)
610
0
{
611
0
  GTypeInstance *inst = (GTypeInstance *)pspec;
612
613
0
  if (inst && inst->g_class && inst->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
614
0
    return ((GParamSpecOverride*)pspec)->overridden;
615
0
  else
616
0
    return NULL;
617
0
}
618
619
/**
620
 * g_param_value_set_default:
621
 * @pspec: a valid #GParamSpec
622
 * @value: a #GValue of correct type for @pspec; since 2.64, you
623
 *   can also pass an empty #GValue, initialized with %G_VALUE_INIT
624
 *
625
 * Sets @value to its default value as specified in @pspec.
626
 */
627
void
628
g_param_value_set_default (GParamSpec *pspec,
629
         GValue     *value)
630
3
{
631
3
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));
632
633
3
  if (G_VALUE_TYPE (value) == G_TYPE_INVALID)
634
0
    {
635
0
      g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
636
0
    }
637
3
  else
638
3
    {
639
3
      g_return_if_fail (G_IS_VALUE (value));
640
3
      g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
641
3
      g_value_reset (value);
642
3
    }
643
644
3
  G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
645
3
}
646
647
/**
648
 * g_param_value_defaults:
649
 * @pspec: a valid #GParamSpec
650
 * @value: a #GValue of correct type for @pspec
651
 *
652
 * Checks whether @value contains the default value as specified in @pspec.
653
 *
654
 * Returns: whether @value contains the canonical default for this @pspec
655
 */
656
gboolean
657
g_param_value_defaults (GParamSpec   *pspec,
658
      const GValue *value)
659
0
{
660
0
  GValue dflt_value = G_VALUE_INIT;
661
0
  gboolean defaults;
662
663
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
664
0
  g_return_val_if_fail (G_IS_VALUE (value), FALSE);
665
0
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
666
667
0
  g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
668
0
  G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
669
0
  defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
670
0
  g_value_unset (&dflt_value);
671
672
0
  return defaults;
673
0
}
674
675
/**
676
 * g_param_value_validate:
677
 * @pspec: a valid #GParamSpec
678
 * @value: a #GValue of correct type for @pspec
679
 *
680
 * Ensures that the contents of @value comply with the specifications
681
 * set out by @pspec. For example, a #GParamSpecInt might require
682
 * that integers stored in @value may not be smaller than -42 and not be
683
 * greater than +42. If @value contains an integer outside of this range,
684
 * it is modified accordingly, so the resulting value will fit into the
685
 * range -42 .. +42.
686
 *
687
 * Returns: whether modifying @value was necessary to ensure validity
688
 */
689
gboolean
690
g_param_value_validate (GParamSpec *pspec,
691
      GValue     *value)
692
17
{
693
17
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
694
17
  g_return_val_if_fail (G_IS_VALUE (value), FALSE);
695
17
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
696
697
17
  if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
698
17
    {
699
17
      GValue oval = *value;
700
701
17
      if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
702
17
    memcmp (&oval.data, &value->data, sizeof (oval.data)))
703
0
  return TRUE;
704
17
    }
705
706
17
  return FALSE;
707
17
}
708
709
/**
710
 * g_param_value_is_valid:
711
 * @pspec: a valid #GParamSpec
712
 * @value: a #GValue of correct type for @pspec
713
 *
714
 * Return whether the contents of @value comply with the specifications
715
 * set out by @pspec.
716
 *
717
 * Returns: whether the contents of @value comply with the specifications
718
 *   set out by @pspec.
719
 *
720
 * Since: 2.74
721
 */
722
gboolean
723
g_param_value_is_valid (GParamSpec *pspec,
724
                        const GValue *value)
725
0
{
726
0
  GParamSpecClass *class;
727
728
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), TRUE);
729
0
  g_return_val_if_fail (G_IS_VALUE (value), TRUE);
730
0
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), TRUE);
731
732
0
  class = G_PARAM_SPEC_GET_CLASS (pspec);
733
734
0
  if (class->value_is_valid)
735
0
    return class->value_is_valid (pspec, value);
736
0
  else if (class->value_validate)
737
0
    {
738
0
      GValue val = G_VALUE_INIT;
739
0
      gboolean changed;
740
741
0
      g_value_init (&val, G_VALUE_TYPE (value));
742
0
      g_value_copy (value, &val);
743
744
0
      changed = class->value_validate (pspec, &val);
745
746
0
      g_value_unset (&val);
747
748
0
      return !changed;
749
0
    }
750
751
0
  return TRUE;
752
0
}
753
754
/**
755
 * g_param_value_convert:
756
 * @pspec: a valid #GParamSpec
757
 * @src_value: source #GValue
758
 * @dest_value: destination #GValue of correct type for @pspec
759
 * @strict_validation: %TRUE requires @dest_value to conform to @pspec
760
 * without modifications
761
 *
762
 * Transforms @src_value into @dest_value if possible, and then
763
 * validates @dest_value, in order for it to conform to @pspec.  If
764
 * @strict_validation is %TRUE this function will only succeed if the
765
 * transformed @dest_value complied to @pspec without modifications.
766
 *
767
 * See also g_value_type_transformable(), g_value_transform() and
768
 * g_param_value_validate().
769
 *
770
 * Returns: %TRUE if transformation and validation were successful,
771
 *  %FALSE otherwise and @dest_value is left untouched.
772
 */
773
gboolean
774
g_param_value_convert (GParamSpec   *pspec,
775
           const GValue *src_value,
776
           GValue       *dest_value,
777
           gboolean      strict_validation)
778
0
{
779
0
  GValue tmp_value = G_VALUE_INIT;
780
781
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
782
0
  g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
783
0
  g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
784
0
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
785
786
  /* better leave dest_value untouched when returning FALSE */
787
788
0
  g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
789
0
  if (g_value_transform (src_value, &tmp_value) &&
790
0
      (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
791
0
    {
792
0
      g_value_unset (dest_value);
793
      
794
      /* values are relocatable */
795
0
      memcpy (dest_value, &tmp_value, sizeof (tmp_value));
796
      
797
0
      return TRUE;
798
0
    }
799
0
  else
800
0
    {
801
0
      g_value_unset (&tmp_value);
802
      
803
0
      return FALSE;
804
0
    }
805
0
}
806
807
/**
808
 * g_param_values_cmp:
809
 * @pspec: a valid #GParamSpec
810
 * @value1: a #GValue of correct type for @pspec
811
 * @value2: a #GValue of correct type for @pspec
812
 *
813
 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
814
 * if @value1 is found to be less than, equal to or greater than @value2,
815
 * respectively.
816
 *
817
 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
818
 */
819
gint
820
g_param_values_cmp (GParamSpec   *pspec,
821
        const GValue *value1,
822
        const GValue *value2)
823
0
{
824
0
  gint cmp;
825
826
  /* param_values_cmp() effectively does: value1 - value2
827
   * so the return values are:
828
   * -1)  value1 < value2
829
   *  0)  value1 == value2
830
   *  1)  value1 > value2
831
   */
832
0
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
833
0
  g_return_val_if_fail (G_IS_VALUE (value1), 0);
834
0
  g_return_val_if_fail (G_IS_VALUE (value2), 0);
835
0
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
836
0
  g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
837
838
0
  cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
839
840
0
  return CLAMP (cmp, -1, 1);
841
0
}
842
843
static void
844
value_param_init (GValue *value)
845
0
{
846
0
  value->data[0].v_pointer = NULL;
847
0
}
848
849
static void
850
value_param_free_value (GValue *value)
851
0
{
852
0
  if (value->data[0].v_pointer)
853
0
    g_param_spec_unref (value->data[0].v_pointer);
854
0
}
855
856
static void
857
value_param_copy_value (const GValue *src_value,
858
      GValue       *dest_value)
859
0
{
860
0
  if (src_value->data[0].v_pointer)
861
0
    dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
862
0
  else
863
0
    dest_value->data[0].v_pointer = NULL;
864
0
}
865
866
static void
867
value_param_transform_value (const GValue *src_value,
868
           GValue       *dest_value)
869
0
{
870
0
  if (src_value->data[0].v_pointer &&
871
0
      g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
872
0
    dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
873
0
  else
874
0
    dest_value->data[0].v_pointer = NULL;
875
0
}
876
877
static gpointer
878
value_param_peek_pointer (const GValue *value)
879
0
{
880
0
  return value->data[0].v_pointer;
881
0
}
882
883
static gchar*
884
value_param_collect_value (GValue      *value,
885
         guint        n_collect_values,
886
         GTypeCValue *collect_values,
887
         guint        collect_flags)
888
0
{
889
0
  if (collect_values[0].v_pointer)
890
0
    {
891
0
      GParamSpec *param = collect_values[0].v_pointer;
892
893
0
      if (param->g_type_instance.g_class == NULL)
894
0
  return g_strconcat ("invalid unclassed param spec pointer for value type '",
895
0
          G_VALUE_TYPE_NAME (value),
896
0
          "'",
897
0
          NULL);
898
0
      else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
899
0
  return g_strconcat ("invalid param spec type '",
900
0
          G_PARAM_SPEC_TYPE_NAME (param),
901
0
          "' for value type '",
902
0
          G_VALUE_TYPE_NAME (value),
903
0
          "'",
904
0
          NULL);
905
0
      value->data[0].v_pointer = g_param_spec_ref (param);
906
0
    }
907
0
  else
908
0
    value->data[0].v_pointer = NULL;
909
910
0
  return NULL;
911
0
}
912
913
static gchar*
914
value_param_lcopy_value (const GValue *value,
915
       guint         n_collect_values,
916
       GTypeCValue  *collect_values,
917
       guint         collect_flags)
918
0
{
919
0
  GParamSpec **param_p = collect_values[0].v_pointer;
920
921
0
  g_return_val_if_fail (param_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value)));
922
923
0
  if (!value->data[0].v_pointer)
924
0
    *param_p = NULL;
925
0
  else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
926
0
    *param_p = value->data[0].v_pointer;
927
0
  else
928
0
    *param_p = g_param_spec_ref (value->data[0].v_pointer);
929
930
0
  return NULL;
931
0
}
932
933
934
/* --- param spec pool --- */
935
/**
936
 * GParamSpecPool:
937
 *
938
 * A #GParamSpecPool maintains a collection of #GParamSpecs which can be
939
 * quickly accessed by owner and name.
940
 *
941
 * The implementation of the #GObject property system uses such a pool to
942
 * store the #GParamSpecs of the properties all object types.
943
 */
944
struct _GParamSpecPool
945
{
946
  GMutex       mutex;
947
  gboolean     type_prefixing;
948
  GHashTable  *hash_table;
949
};
950
951
static guint
952
param_spec_pool_hash (gconstpointer key_spec)
953
1.14k
{
954
1.14k
  const GParamSpec *key = key_spec;
955
1.14k
  const gchar *p;
956
1.14k
  guint h = (guint) key->owner_type;
957
958
11.1k
  for (p = key->name; *p; p++)
959
10.0k
    h = (h << 5) - h + *p;
960
961
1.14k
  return h;
962
1.14k
}
963
964
static gboolean
965
param_spec_pool_equals (gconstpointer key_spec_1,
966
      gconstpointer key_spec_2)
967
547
{
968
547
  const GParamSpec *key1 = key_spec_1;
969
547
  const GParamSpec *key2 = key_spec_2;
970
971
547
  return (key1->owner_type == key2->owner_type &&
972
547
          (key1->name == key2->name ||
973
547
     strcmp (key1->name, key2->name) == 0));
974
547
}
975
976
/**
977
 * g_param_spec_pool_new:
978
 * @type_prefixing: Whether the pool will support type-prefixed property names.
979
 *
980
 * Creates a new #GParamSpecPool.
981
 *
982
 * If @type_prefixing is %TRUE, lookups in the newly created pool will
983
 * allow to specify the owner as a colon-separated prefix of the
984
 * property name, like "GtkContainer:border-width". This feature is
985
 * deprecated, so you should always set @type_prefixing to %FALSE.
986
 *
987
 * Returns: (transfer full): a newly allocated #GParamSpecPool.
988
 */
989
GParamSpecPool*
990
g_param_spec_pool_new (gboolean type_prefixing)
991
1
{
992
1
  static GMutex init_mutex;
993
1
  GParamSpecPool *pool = g_new (GParamSpecPool, 1);
994
995
1
  memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
996
1
  pool->type_prefixing = type_prefixing != FALSE;
997
1
  pool->hash_table = g_hash_table_new_full (param_spec_pool_hash,
998
1
                                            param_spec_pool_equals,
999
1
                                            (GDestroyNotify) g_param_spec_unref,
1000
1
                                            NULL);
1001
1002
1
  return pool;
1003
1
}
1004
1005
/**
1006
 * g_param_spec_pool_free:
1007
 * @pool: (transfer full): a #GParamSpecPool
1008
 *
1009
 * Frees the resources allocated by a #GParamSpecPool.
1010
 *
1011
 * Since: 2.80
1012
 */
1013
void
1014
g_param_spec_pool_free (GParamSpecPool *pool)
1015
0
{
1016
0
  g_mutex_lock (&pool->mutex);
1017
0
  g_hash_table_unref (pool->hash_table);
1018
0
  g_mutex_unlock (&pool->mutex);
1019
0
  g_mutex_clear (&pool->mutex);
1020
0
  g_free (pool);
1021
0
}
1022
1023
/**
1024
 * g_param_spec_pool_insert:
1025
 * @pool: a #GParamSpecPool.
1026
 * @pspec: (transfer none) (not nullable): the #GParamSpec to insert
1027
 * @owner_type: a #GType identifying the owner of @pspec
1028
 *
1029
 * Inserts a #GParamSpec in the pool.
1030
 */
1031
void
1032
g_param_spec_pool_insert (GParamSpecPool *pool,
1033
        GParamSpec     *pspec,
1034
        GType           owner_type)
1035
21
{
1036
21
  const gchar *p;
1037
  
1038
21
  if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
1039
21
    {
1040
232
      for (p = pspec->name; *p; p++)
1041
211
  {
1042
211
    if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
1043
0
      {
1044
0
        g_critical (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
1045
0
        return;
1046
0
      }
1047
211
  }
1048
21
      g_mutex_lock (&pool->mutex);
1049
21
      pspec->owner_type = owner_type;
1050
21
      g_param_spec_ref (pspec);
1051
21
      g_hash_table_add (pool->hash_table, pspec);
1052
21
      g_mutex_unlock (&pool->mutex);
1053
21
    }
1054
0
  else
1055
0
    {
1056
0
      g_return_if_fail (pool != NULL);
1057
0
      g_return_if_fail (pspec);
1058
0
      g_return_if_fail (owner_type > 0);
1059
0
      g_return_if_fail (pspec->owner_type == 0);
1060
0
    }
1061
21
}
1062
1063
/**
1064
 * g_param_spec_pool_remove:
1065
 * @pool: a #GParamSpecPool
1066
 * @pspec: (transfer none) (not nullable): the #GParamSpec to remove
1067
 *
1068
 * Removes a #GParamSpec from the pool.
1069
 */
1070
void
1071
g_param_spec_pool_remove (GParamSpecPool *pool,
1072
        GParamSpec     *pspec)
1073
0
{
1074
0
  if (pool && pspec)
1075
0
    {
1076
0
      g_mutex_lock (&pool->mutex);
1077
0
      if (!g_hash_table_remove (pool->hash_table, pspec))
1078
0
  g_critical (G_STRLOC ": attempt to remove unknown pspec '%s' from pool", pspec->name);
1079
0
      g_mutex_unlock (&pool->mutex);
1080
0
    }
1081
0
  else
1082
0
    {
1083
0
      g_return_if_fail (pool != NULL);
1084
0
      g_return_if_fail (pspec);
1085
0
    }
1086
0
}
1087
1088
static inline GParamSpec*
1089
param_spec_ht_lookup (GHashTable  *hash_table,
1090
          const gchar *param_name,
1091
          GType        owner_type,
1092
          gboolean     walk_ancestors)
1093
589
{
1094
589
  GParamSpec key, *pspec;
1095
1096
589
  key.owner_type = owner_type;
1097
589
  key.name = (gchar*) param_name;
1098
589
  if (walk_ancestors)
1099
568
    do
1100
1.09k
      {
1101
1.09k
  pspec = g_hash_table_lookup (hash_table, &key);
1102
1.09k
  if (pspec)
1103
547
    return pspec;
1104
552
  key.owner_type = g_type_parent (key.owner_type);
1105
552
      }
1106
568
    while (key.owner_type);
1107
21
  else
1108
21
    pspec = g_hash_table_lookup (hash_table, &key);
1109
1110
42
  if (!pspec && !is_canonical (param_name))
1111
0
    {
1112
0
      gchar *canonical;
1113
1114
0
      canonical = g_strdup (key.name);
1115
0
      canonicalize_key (canonical);
1116
1117
      /* try canonicalized form */
1118
0
      key.name = canonical;
1119
0
      key.owner_type = owner_type;
1120
1121
0
      if (walk_ancestors)
1122
0
        do
1123
0
          {
1124
0
            pspec = g_hash_table_lookup (hash_table, &key);
1125
0
            if (pspec)
1126
0
              {
1127
0
                g_free (canonical);
1128
0
                return pspec;
1129
0
              }
1130
0
            key.owner_type = g_type_parent (key.owner_type);
1131
0
          }
1132
0
        while (key.owner_type);
1133
0
      else
1134
0
        pspec = g_hash_table_lookup (hash_table, &key);
1135
1136
0
      g_free (canonical);
1137
0
    }
1138
1139
42
  return pspec;
1140
42
}
1141
1142
/**
1143
 * g_param_spec_pool_lookup:
1144
 * @pool: a #GParamSpecPool
1145
 * @param_name: the name to look for
1146
 * @owner_type: the owner to look for
1147
 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1148
 *  owned by an ancestor of @owner_type.
1149
 *
1150
 * Looks up a #GParamSpec in the pool.
1151
 *
1152
 * Returns: (transfer none) (nullable): The found #GParamSpec, or %NULL if no
1153
 * matching #GParamSpec was found.
1154
 */
1155
GParamSpec*
1156
g_param_spec_pool_lookup (GParamSpecPool *pool,
1157
        const gchar    *param_name,
1158
        GType           owner_type,
1159
        gboolean        walk_ancestors)
1160
589
{
1161
589
  GParamSpec *pspec;
1162
1163
589
  g_return_val_if_fail (pool != NULL, NULL);
1164
589
  g_return_val_if_fail (param_name != NULL, NULL);
1165
1166
589
  g_mutex_lock (&pool->mutex);
1167
1168
  /* try quick and away, i.e. without prefix */
1169
589
  pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1170
589
  if (pspec)
1171
547
    {
1172
547
      g_mutex_unlock (&pool->mutex);
1173
547
      return pspec;
1174
547
    }
1175
1176
42
  if (pool->type_prefixing)
1177
42
    {
1178
42
      char *delim;
1179
1180
42
      delim = strchr (param_name, ':');
1181
1182
      /* strip type prefix */
1183
42
      if (delim && delim[1] == ':')
1184
0
        {
1185
0
          guint l = delim - param_name;
1186
0
          gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1187
0
          GType type;
1188
1189
0
          strncpy (buffer, param_name, delim - param_name);
1190
0
          buffer[l] = 0;
1191
0
          type = g_type_from_name (buffer);
1192
0
          if (l >= 32)
1193
0
            g_free (buffer);
1194
0
          if (type)         /* type==0 isn't a valid type pefix */
1195
0
            {
1196
              /* sanity check, these cases don't make a whole lot of sense */
1197
0
              if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1198
0
                {
1199
0
                  g_mutex_unlock (&pool->mutex);
1200
1201
0
                  return NULL;
1202
0
                }
1203
0
              owner_type = type;
1204
0
              param_name += l + 2;
1205
0
              pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1206
0
              g_mutex_unlock (&pool->mutex);
1207
1208
0
              return pspec;
1209
0
            }
1210
0
        }
1211
42
    }
1212
1213
  /* malformed param_name */
1214
1215
42
  g_mutex_unlock (&pool->mutex);
1216
1217
42
  return NULL;
1218
42
}
1219
1220
static void
1221
pool_list (gpointer key,
1222
     gpointer value,
1223
     gpointer user_data)
1224
0
{
1225
0
  GParamSpec *pspec = value;
1226
0
  gpointer *data = user_data;
1227
0
  GType owner_type = (GType) data[1];
1228
1229
0
  if (owner_type == pspec->owner_type)
1230
0
    data[0] = g_list_prepend (data[0], pspec);
1231
0
}
1232
1233
/**
1234
 * g_param_spec_pool_list_owned:
1235
 * @pool: a #GParamSpecPool
1236
 * @owner_type: the owner to look for
1237
 *
1238
 * Gets an #GList of all #GParamSpecs owned by @owner_type in
1239
 * the pool.
1240
 *
1241
 * Returns: (transfer container) (element-type GObject.ParamSpec): a
1242
 *          #GList of all #GParamSpecs owned by @owner_type in
1243
 *          the pool#GParamSpecs.
1244
 */
1245
GList*
1246
g_param_spec_pool_list_owned (GParamSpecPool *pool,
1247
            GType           owner_type)
1248
0
{
1249
0
  gpointer data[2];
1250
1251
0
  g_return_val_if_fail (pool != NULL, NULL);
1252
0
  g_return_val_if_fail (owner_type > 0, NULL);
1253
  
1254
0
  g_mutex_lock (&pool->mutex);
1255
0
  data[0] = NULL;
1256
0
  data[1] = GTYPE_TO_POINTER (owner_type);
1257
0
  g_hash_table_foreach (pool->hash_table, pool_list, &data);
1258
0
  g_mutex_unlock (&pool->mutex);
1259
1260
0
  return data[0];
1261
0
}
1262
1263
static gint
1264
pspec_compare_id (gconstpointer a,
1265
      gconstpointer b)
1266
0
{
1267
0
  const GParamSpec *pspec1 = a, *pspec2 = b;
1268
1269
0
  if (pspec1->param_id < pspec2->param_id)
1270
0
    return -1;
1271
1272
0
  if (pspec1->param_id > pspec2->param_id)
1273
0
    return 1;
1274
1275
0
  return strcmp (pspec1->name, pspec2->name);
1276
0
}
1277
1278
static inline gboolean
1279
should_list_pspec (GParamSpec *pspec,
1280
                   GType      owner_type,
1281
                   GHashTable *ht)
1282
0
{
1283
0
  GParamSpec *found;
1284
1285
  /* Remove paramspecs that are redirected, and also paramspecs
1286
   * that have are overridden by non-redirected properties.
1287
   * The idea is to get the single paramspec for each name that
1288
   * best corresponds to what the application sees.
1289
   */
1290
0
  if (g_param_spec_get_redirect_target (pspec))
1291
0
    return FALSE;
1292
1293
0
  found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1294
0
  if (found != pspec)
1295
0
    {
1296
0
      GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1297
0
      if (redirect != pspec)
1298
0
        return FALSE;
1299
0
    }
1300
1301
0
  return TRUE;
1302
0
}
1303
1304
static void
1305
pool_depth_list (gpointer key,
1306
     gpointer value,
1307
     gpointer user_data)
1308
0
{
1309
0
  GParamSpec *pspec = value;
1310
0
  gpointer *data = user_data;
1311
0
  GSList **slists = data[0];
1312
0
  GType owner_type = (GType) data[1];
1313
0
  GHashTable *ht = data[2];
1314
0
  int *count = data[3];
1315
1316
0
  if (g_type_is_a (owner_type, pspec->owner_type) &&
1317
0
      should_list_pspec (pspec, owner_type, ht))
1318
0
    {
1319
0
      if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1320
0
  {
1321
0
    slists[0] = g_slist_prepend (slists[0], pspec);
1322
0
          *count = *count + 1;
1323
0
  }
1324
0
      else
1325
0
  {
1326
0
          guint d = g_type_depth (pspec->owner_type);
1327
1328
0
          slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1329
0
          *count = *count + 1;
1330
0
  }
1331
0
    }
1332
0
}
1333
1334
/* We handle interfaces specially since we don't want to
1335
 * count interface prerequisites like normal inheritance;
1336
 * the property comes from the direct inheritance from
1337
 * the prerequisite class, not from the interface that
1338
 * prerequires it.
1339
 * 
1340
 * also 'depth' isn't a meaningful concept for interface
1341
 * prerequites.
1342
 */
1343
static void
1344
pool_depth_list_for_interface (gpointer key,
1345
             gpointer value,
1346
             gpointer user_data)
1347
87
{
1348
87
  GParamSpec *pspec = value;
1349
87
  gpointer *data = user_data;
1350
87
  GSList **slists = data[0];
1351
87
  GType owner_type = (GType) data[1];
1352
87
  GHashTable *ht = data[2];
1353
87
  int *count = data[3];
1354
1355
87
  if (pspec->owner_type == owner_type &&
1356
87
      should_list_pspec (pspec, owner_type, ht))
1357
0
    {
1358
0
      slists[0] = g_slist_prepend (slists[0], pspec);
1359
0
      *count = *count + 1;
1360
0
    }
1361
87
}
1362
1363
/**
1364
 * g_param_spec_pool_list:
1365
 * @pool: a #GParamSpecPool
1366
 * @owner_type: the owner to look for
1367
 * @n_pspecs_p: (out): return location for the length of the returned array
1368
 *
1369
 * Gets an array of all #GParamSpecs owned by @owner_type in
1370
 * the pool.
1371
 *
1372
 * Returns: (array length=n_pspecs_p) (transfer container): a newly
1373
 *          allocated array containing pointers to all #GParamSpecs
1374
 *          owned by @owner_type in the pool
1375
 */
1376
GParamSpec**
1377
g_param_spec_pool_list (GParamSpecPool *pool,
1378
      GType           owner_type,
1379
      guint          *n_pspecs_p)
1380
11
{
1381
11
  GParamSpec **pspecs, **p;
1382
11
  GSList **slists, *node;
1383
11
  gpointer data[4];
1384
11
  guint d, i;
1385
11
  int n_pspecs = 0;
1386
1387
11
  g_return_val_if_fail (pool != NULL, NULL);
1388
11
  g_return_val_if_fail (owner_type > 0, NULL);
1389
11
  g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1390
  
1391
11
  g_mutex_lock (&pool->mutex);
1392
11
  d = g_type_depth (owner_type);
1393
11
  slists = g_new0 (GSList*, d);
1394
11
  data[0] = slists;
1395
11
  data[1] = GTYPE_TO_POINTER (owner_type);
1396
11
  data[2] = pool->hash_table;
1397
11
  data[3] = &n_pspecs;
1398
1399
11
  g_hash_table_foreach (pool->hash_table,
1400
11
                        G_TYPE_IS_INTERFACE (owner_type) ?
1401
11
                          pool_depth_list_for_interface :
1402
11
                          pool_depth_list,
1403
11
                        &data);
1404
1405
11
  pspecs = g_new (GParamSpec*, n_pspecs + 1);
1406
11
  p = pspecs;
1407
33
  for (i = 0; i < d; i++)
1408
22
    {
1409
22
      slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1410
22
      for (node = slists[i]; node; node = node->next)
1411
0
  *p++ = node->data;
1412
22
      g_slist_free (slists[i]);
1413
22
    }
1414
11
  *p++ = NULL;
1415
11
  g_free (slists);
1416
11
  g_mutex_unlock (&pool->mutex);
1417
1418
11
  *n_pspecs_p = n_pspecs;
1419
1420
11
  return pspecs;
1421
11
}
1422
1423
/* --- auxiliary functions --- */
1424
typedef struct
1425
{
1426
  /* class portion */
1427
  GType           value_type;
1428
  void          (*finalize)             (GParamSpec   *pspec);
1429
  void          (*value_set_default)    (GParamSpec   *pspec,
1430
           GValue       *value);
1431
  gboolean      (*value_validate)       (GParamSpec   *pspec,
1432
           GValue       *value);
1433
  gint          (*values_cmp)           (GParamSpec   *pspec,
1434
           const GValue *value1,
1435
           const GValue *value2);
1436
} ParamSpecClassInfo;
1437
1438
static void
1439
param_spec_generic_class_init (gpointer g_class,
1440
             gpointer class_data)
1441
40
{
1442
40
  GParamSpecClass *class = g_class;
1443
40
  ParamSpecClassInfo *info = class_data;
1444
1445
40
  class->value_type = info->value_type;
1446
40
  if (info->finalize)
1447
10
    class->finalize = info->finalize;     /* optional */
1448
40
  class->value_set_default = info->value_set_default;
1449
40
  if (info->value_validate)
1450
40
    class->value_validate = info->value_validate; /* optional */
1451
40
  class->values_cmp = info->values_cmp;
1452
40
  g_free (class_data);
1453
40
}
1454
1455
static void
1456
default_value_set_default (GParamSpec *pspec,
1457
         GValue     *value)
1458
0
{
1459
  /* value is already zero initialized */
1460
0
}
1461
1462
static gint
1463
default_values_cmp (GParamSpec   *pspec,
1464
        const GValue *value1,
1465
        const GValue *value2)
1466
0
{
1467
0
  return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1468
0
}
1469
1470
/**
1471
 * g_param_type_register_static:
1472
 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1473
 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1474
 *
1475
 * Registers @name as the name of a new static type derived
1476
 * from %G_TYPE_PARAM.
1477
 *
1478
 * The type system uses the information contained in the #GParamSpecTypeInfo
1479
 * structure pointed to by @info to manage the #GParamSpec type and its
1480
 * instances.
1481
 *
1482
 * Returns: The new type identifier.
1483
 */
1484
GType
1485
g_param_type_register_static (const gchar              *name,
1486
            const GParamSpecTypeInfo *pspec_info)
1487
46
{
1488
46
  GTypeInfo info = {
1489
46
    sizeof (GParamSpecClass),      /* class_size */
1490
46
    NULL,                          /* base_init */
1491
46
    NULL,                          /* base_destroy */
1492
46
    param_spec_generic_class_init, /* class_init */
1493
46
    NULL,                          /* class_destroy */
1494
46
    NULL,                          /* class_data */
1495
46
    0,                             /* instance_size */
1496
46
    16,                            /* n_preallocs */
1497
46
    NULL,                          /* instance_init */
1498
46
    NULL,                          /* value_table */
1499
46
  };
1500
46
  ParamSpecClassInfo *cinfo;
1501
1502
46
  g_return_val_if_fail (name != NULL, 0);
1503
46
  g_return_val_if_fail (pspec_info != NULL, 0);
1504
46
  g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1505
46
  g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1506
46
  g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1507
  /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1508
  /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1509
  /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1510
1511
46
  info.instance_size = pspec_info->instance_size;
1512
46
  info.n_preallocs = pspec_info->n_preallocs;
1513
46
  info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1514
46
  cinfo = g_new (ParamSpecClassInfo, 1);
1515
46
  cinfo->value_type = pspec_info->value_type;
1516
46
  cinfo->finalize = pspec_info->finalize;
1517
46
  cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1518
46
  cinfo->value_validate = pspec_info->value_validate;
1519
46
  cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1520
46
  info.class_data = cinfo;
1521
1522
46
  return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1523
46
}
1524
1525
/**
1526
 * g_value_set_param:
1527
 * @value: a valid #GValue of type %G_TYPE_PARAM
1528
 * @param: (nullable): the #GParamSpec to be set
1529
 *
1530
 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1531
 */
1532
void
1533
g_value_set_param (GValue     *value,
1534
       GParamSpec *param)
1535
0
{
1536
0
  g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1537
0
  if (param)
1538
0
    g_return_if_fail (G_IS_PARAM_SPEC (param));
1539
1540
0
  if (value->data[0].v_pointer)
1541
0
    g_param_spec_unref (value->data[0].v_pointer);
1542
0
  value->data[0].v_pointer = param;
1543
0
  if (value->data[0].v_pointer)
1544
0
    g_param_spec_ref (value->data[0].v_pointer);
1545
0
}
1546
1547
/**
1548
 * g_value_set_param_take_ownership: (skip)
1549
 * @value: a valid #GValue of type %G_TYPE_PARAM
1550
 * @param: (nullable): the #GParamSpec to be set
1551
 *
1552
 * This is an internal function introduced mainly for C marshallers.
1553
 *
1554
 * Deprecated: 2.4: Use g_value_take_param() instead.
1555
 */
1556
void
1557
g_value_set_param_take_ownership (GValue     *value,
1558
          GParamSpec *param)
1559
0
{
1560
0
  g_value_take_param (value, param);
1561
0
}
1562
1563
/**
1564
 * g_value_take_param: (skip)
1565
 * @value: a valid #GValue of type %G_TYPE_PARAM
1566
 * @param: (nullable): the #GParamSpec to be set
1567
 *
1568
 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1569
 * over the ownership of the caller’s reference to @param; the caller
1570
 * doesn’t have to unref it any more.
1571
 *
1572
 * Since: 2.4
1573
 */
1574
void
1575
g_value_take_param (GValue     *value,
1576
        GParamSpec *param)
1577
0
{
1578
0
  g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1579
0
  if (param)
1580
0
    g_return_if_fail (G_IS_PARAM_SPEC (param));
1581
1582
0
  if (value->data[0].v_pointer)
1583
0
    g_param_spec_unref (value->data[0].v_pointer);
1584
0
  value->data[0].v_pointer = param; /* we take over the reference count */
1585
0
}
1586
1587
/**
1588
 * g_value_get_param:
1589
 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1590
 *
1591
 * Get the contents of a %G_TYPE_PARAM #GValue.
1592
 *
1593
 * Returns: (transfer none): #GParamSpec content of @value
1594
 */
1595
GParamSpec*
1596
g_value_get_param (const GValue *value)
1597
0
{
1598
0
  g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1599
1600
0
  return value->data[0].v_pointer;
1601
0
}
1602
1603
/**
1604
 * g_value_dup_param: (skip)
1605
 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1606
 *
1607
 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1608
 * reference count.
1609
 *
1610
 * Returns: (transfer full): #GParamSpec content of @value, should be
1611
 *     unreferenced when no longer needed.
1612
 */
1613
GParamSpec*
1614
g_value_dup_param (const GValue *value)
1615
0
{
1616
0
  g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1617
1618
0
  return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1619
0
}
1620
1621
/**
1622
 * g_param_spec_get_default_value:
1623
 * @pspec: a #GParamSpec
1624
 *
1625
 * Gets the default value of @pspec as a pointer to a #GValue.
1626
 *
1627
 * The #GValue will remain valid for the life of @pspec.
1628
 *
1629
 * Returns: a pointer to a #GValue which must not be modified
1630
 *
1631
 * Since: 2.38
1632
 **/
1633
const GValue *
1634
g_param_spec_get_default_value (GParamSpec *pspec)
1635
36
{
1636
36
  GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
1637
1638
  /* We use the type field of the GValue as the key for the once because
1639
   * it will be zero before it is initialised and non-zero after.  We
1640
   * have to take care that we don't write a non-zero value to the type
1641
   * field before we are completely done, however, because then another
1642
   * thread could come along and find the value partially-initialised.
1643
   *
1644
   * In order to accomplish this we store the default value in a
1645
   * stack-allocated GValue.  We then set the type field in that value
1646
   * to zero and copy the contents into place.  We then end by storing
1647
   * the type as the last step in order to ensure that we're completely
1648
   * done before a g_once_init_enter() could take the fast path in
1649
   * another thread.
1650
   */
1651
36
  if (g_once_init_enter_pointer (&priv->default_value.g_type))
1652
3
    {
1653
3
      GValue default_value = G_VALUE_INIT;
1654
1655
3
      g_value_init (&default_value, pspec->value_type);
1656
3
      g_param_value_set_default (pspec, &default_value);
1657
1658
      /* store all but the type */
1659
3
      memcpy (priv->default_value.data, default_value.data, sizeof (default_value.data));
1660
1661
3
      g_once_init_leave_pointer (&priv->default_value.g_type, pspec->value_type);
1662
3
    }
1663
1664
36
  return &priv->default_value;
1665
36
}
1666
1667
/**
1668
 * g_param_spec_get_name_quark:
1669
 * @pspec: a #GParamSpec
1670
 *
1671
 * Gets the GQuark for the name.
1672
 *
1673
 * Returns: the GQuark for @pspec->name.
1674
 *
1675
 * Since: 2.46
1676
 */
1677
GQuark
1678
g_param_spec_get_name_quark (GParamSpec *pspec)
1679
0
{
1680
0
  GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
1681
1682
  /* Return the quark that we've stashed away at creation time.
1683
   * This lets us avoid a lock and a hash table lookup when
1684
   * dispatching property change notification.
1685
   */
1686
1687
0
  return priv->name_quark;
1688
0
}