all: remove use of 'register' keyword
[glib.git] / gobject / gparam.c
blobc69ee028b84975bf5203f55a2002ce7e9e685fa7
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * MT safe
22 #include "config.h"
24 #include <string.h>
26 #include "gparam.h"
27 #include "gparamspecs.h"
28 #include "gvaluecollector.h"
29 #include "gtype-private.h"
31 /**
32 * SECTION:gparamspec
33 * @short_description: Metadata for parameter specifications
34 * @see_also: g_object_class_install_property(), g_object_set(),
35 * g_object_get(), g_object_set_property(), g_object_get_property(),
36 * g_value_register_transform_func()
37 * @title: GParamSpec
39 * #GParamSpec is an object structure that encapsulates the metadata
40 * required to specify parameters, such as e.g. #GObject properties.
42 * ## Parameter names # {#canonical-parameter-names}
44 * Parameter names need to start with a letter (a-z or A-Z).
45 * Subsequent characters can be letters, numbers or a '-'.
46 * All other characters are replaced by a '-' during construction.
47 * The result of this replacement is called the canonical name of
48 * the parameter.
52 /* --- defines --- */
53 #define PARAM_FLOATING_FLAG 0x2
54 #define G_PARAM_USER_MASK (~0 << G_PARAM_USER_SHIFT)
55 #define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
57 /* --- prototypes --- */
58 static void g_param_spec_class_base_init (GParamSpecClass *class);
59 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
60 static void g_param_spec_class_init (GParamSpecClass *class,
61 gpointer class_data);
62 static void g_param_spec_init (GParamSpec *pspec,
63 GParamSpecClass *class);
64 static void g_param_spec_finalize (GParamSpec *pspec);
65 static void value_param_init (GValue *value);
66 static void value_param_free_value (GValue *value);
67 static void value_param_copy_value (const GValue *src_value,
68 GValue *dest_value);
69 static void value_param_transform_value (const GValue *src_value,
70 GValue *dest_value);
71 static gpointer value_param_peek_pointer (const GValue *value);
72 static gchar* value_param_collect_value (GValue *value,
73 guint n_collect_values,
74 GTypeCValue *collect_values,
75 guint collect_flags);
76 static gchar* value_param_lcopy_value (const GValue *value,
77 guint n_collect_values,
78 GTypeCValue *collect_values,
79 guint collect_flags);
81 typedef struct
83 GValue default_value;
84 } GParamSpecPrivate;
86 static gint g_param_private_offset;
88 /* --- functions --- */
89 static inline GParamSpecPrivate *
90 g_param_spec_get_private (GParamSpec *pspec)
92 return &G_STRUCT_MEMBER (GParamSpecPrivate, pspec, g_param_private_offset);
95 void
96 _g_param_type_init (void)
98 static const GTypeFundamentalInfo finfo = {
99 (G_TYPE_FLAG_CLASSED |
100 G_TYPE_FLAG_INSTANTIATABLE |
101 G_TYPE_FLAG_DERIVABLE |
102 G_TYPE_FLAG_DEEP_DERIVABLE),
104 static const GTypeValueTable param_value_table = {
105 value_param_init, /* value_init */
106 value_param_free_value, /* value_free */
107 value_param_copy_value, /* value_copy */
108 value_param_peek_pointer, /* value_peek_pointer */
109 "p", /* collect_format */
110 value_param_collect_value, /* collect_value */
111 "p", /* lcopy_format */
112 value_param_lcopy_value, /* lcopy_value */
114 const GTypeInfo param_spec_info = {
115 sizeof (GParamSpecClass),
117 (GBaseInitFunc) g_param_spec_class_base_init,
118 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
119 (GClassInitFunc) g_param_spec_class_init,
120 (GClassFinalizeFunc) NULL,
121 NULL, /* class_data */
123 sizeof (GParamSpec),
124 0, /* n_preallocs */
125 (GInstanceInitFunc) g_param_spec_init,
127 &param_value_table,
129 GType type;
131 /* This should be registered as GParamSpec instead of GParam, for
132 * consistency sake, so that type name can be mapped to struct name,
133 * However, some language bindings, most noticeable the python ones
134 * depends on the "GParam" identifier, see #548689
136 type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), &param_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
137 g_assert (type == G_TYPE_PARAM);
138 g_param_private_offset = g_type_add_instance_private (type, sizeof (GParamSpecPrivate));
139 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
142 static void
143 g_param_spec_class_base_init (GParamSpecClass *class)
147 static void
148 g_param_spec_class_base_finalize (GParamSpecClass *class)
152 static void
153 g_param_spec_class_init (GParamSpecClass *class,
154 gpointer class_data)
156 class->value_type = G_TYPE_NONE;
157 class->finalize = g_param_spec_finalize;
158 class->value_set_default = NULL;
159 class->value_validate = NULL;
160 class->values_cmp = NULL;
162 g_type_class_adjust_private_offset (class, &g_param_private_offset);
165 static void
166 g_param_spec_init (GParamSpec *pspec,
167 GParamSpecClass *class)
169 pspec->name = NULL;
170 pspec->_nick = NULL;
171 pspec->_blurb = NULL;
172 pspec->flags = 0;
173 pspec->value_type = class->value_type;
174 pspec->owner_type = 0;
175 pspec->qdata = NULL;
176 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
177 pspec->ref_count = 1;
178 pspec->param_id = 0;
181 static void
182 g_param_spec_finalize (GParamSpec *pspec)
184 GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
186 if (priv->default_value.g_type)
187 g_value_reset (&priv->default_value);
189 g_datalist_clear (&pspec->qdata);
191 if (!(pspec->flags & G_PARAM_STATIC_NICK))
192 g_free (pspec->_nick);
194 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
195 g_free (pspec->_blurb);
197 g_type_free_instance ((GTypeInstance*) pspec);
201 * g_param_spec_ref: (skip)
202 * @pspec: a valid #GParamSpec
204 * Increments the reference count of @pspec.
206 * Returns: the #GParamSpec that was passed into this function
208 GParamSpec*
209 g_param_spec_ref (GParamSpec *pspec)
211 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
213 g_atomic_int_inc ((int *)&pspec->ref_count);
215 return pspec;
219 * g_param_spec_unref: (skip)
220 * @pspec: a valid #GParamSpec
222 * Decrements the reference count of a @pspec.
224 void
225 g_param_spec_unref (GParamSpec *pspec)
227 gboolean is_zero;
229 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
231 is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
233 if (G_UNLIKELY (is_zero))
235 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
240 * g_param_spec_sink:
241 * @pspec: a valid #GParamSpec
243 * The initial reference count of a newly created #GParamSpec is 1,
244 * even though no one has explicitly called g_param_spec_ref() on it
245 * yet. So the initial reference count is flagged as "floating", until
246 * someone calls `g_param_spec_ref (pspec); g_param_spec_sink
247 * (pspec);` in sequence on it, taking over the initial
248 * reference count (thus ending up with a @pspec that has a reference
249 * count of 1 still, but is not flagged "floating" anymore).
251 void
252 g_param_spec_sink (GParamSpec *pspec)
254 gsize oldvalue;
255 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
257 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
258 if (oldvalue & PARAM_FLOATING_FLAG)
259 g_param_spec_unref (pspec);
263 * g_param_spec_ref_sink: (skip)
264 * @pspec: a valid #GParamSpec
266 * Convenience function to ref and sink a #GParamSpec.
268 * Since: 2.10
269 * Returns: the #GParamSpec that was passed into this function
271 GParamSpec*
272 g_param_spec_ref_sink (GParamSpec *pspec)
274 gsize oldvalue;
275 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
277 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
278 if (!(oldvalue & PARAM_FLOATING_FLAG))
279 g_param_spec_ref (pspec);
281 return pspec;
285 * g_param_spec_get_name:
286 * @pspec: a valid #GParamSpec
288 * Get the name of a #GParamSpec.
290 * The name is always an "interned" string (as per g_intern_string()).
291 * This allows for pointer-value comparisons.
293 * Returns: the name of @pspec.
295 const gchar *
296 g_param_spec_get_name (GParamSpec *pspec)
298 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
300 return pspec->name;
304 * g_param_spec_get_nick:
305 * @pspec: a valid #GParamSpec
307 * Get the nickname of a #GParamSpec.
309 * Returns: the nickname of @pspec.
311 const gchar *
312 g_param_spec_get_nick (GParamSpec *pspec)
314 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
316 if (pspec->_nick)
317 return pspec->_nick;
318 else
320 GParamSpec *redirect_target;
322 redirect_target = g_param_spec_get_redirect_target (pspec);
323 if (redirect_target && redirect_target->_nick)
324 return redirect_target->_nick;
327 return pspec->name;
331 * g_param_spec_get_blurb:
332 * @pspec: a valid #GParamSpec
334 * Get the short description of a #GParamSpec.
336 * Returns: the short description of @pspec.
338 const gchar *
339 g_param_spec_get_blurb (GParamSpec *pspec)
341 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
343 if (pspec->_blurb)
344 return pspec->_blurb;
345 else
347 GParamSpec *redirect_target;
349 redirect_target = g_param_spec_get_redirect_target (pspec);
350 if (redirect_target && redirect_target->_blurb)
351 return redirect_target->_blurb;
354 return NULL;
357 static void
358 canonicalize_key (gchar *key)
360 gchar *p;
362 for (p = key; *p != 0; p++)
364 gchar c = *p;
366 if (c != '-' &&
367 (c < '0' || c > '9') &&
368 (c < 'A' || c > 'Z') &&
369 (c < 'a' || c > 'z'))
370 *p = '-';
374 static gboolean
375 is_canonical (const gchar *key)
377 const gchar *p;
379 for (p = key; *p != 0; p++)
381 gchar c = *p;
383 if (c != '-' &&
384 (c < '0' || c > '9') &&
385 (c < 'A' || c > 'Z') &&
386 (c < 'a' || c > 'z'))
387 return FALSE;
390 return TRUE;
394 * g_param_spec_internal: (skip)
395 * @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
396 * @name: the canonical name of the property
397 * @nick: the nickname of the property
398 * @blurb: a short description of the property
399 * @flags: a combination of #GParamFlags
401 * Creates a new #GParamSpec instance.
403 * A property name consists of segments consisting of ASCII letters and
404 * digits, separated by either the '-' or '_' character. The first
405 * character of a property name must be a letter. Names which violate these
406 * rules lead to undefined behaviour.
408 * When creating and looking up a #GParamSpec, either separator can be
409 * used, but they cannot be mixed. Using '-' is considerably more
410 * efficient and in fact required when using property names as detail
411 * strings for signals.
413 * Beyond the name, #GParamSpecs have two more descriptive
414 * strings associated with them, the @nick, which should be suitable
415 * for use as a label for the property in a property editor, and the
416 * @blurb, which should be a somewhat longer description, suitable for
417 * e.g. a tooltip. The @nick and @blurb should ideally be localized.
419 * Returns: a newly allocated #GParamSpec instance
421 gpointer
422 g_param_spec_internal (GType param_type,
423 const gchar *name,
424 const gchar *nick,
425 const gchar *blurb,
426 GParamFlags flags)
428 GParamSpec *pspec;
430 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
431 g_return_val_if_fail (name != NULL, NULL);
432 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
433 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
435 pspec = (gpointer) g_type_create_instance (param_type);
437 if (flags & G_PARAM_STATIC_NAME)
439 /* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
440 pspec->name = (gchar *) g_intern_static_string (name);
441 if (!is_canonical (pspec->name))
442 g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
444 else
446 if (is_canonical (name))
447 pspec->name = (gchar *) g_intern_string (name);
448 else
450 gchar *tmp = g_strdup (name);
451 canonicalize_key (tmp);
452 pspec->name = (gchar *) g_intern_string (tmp);
453 g_free (tmp);
457 if (flags & G_PARAM_STATIC_NICK)
458 pspec->_nick = (gchar*) nick;
459 else
460 pspec->_nick = g_strdup (nick);
462 if (flags & G_PARAM_STATIC_BLURB)
463 pspec->_blurb = (gchar*) blurb;
464 else
465 pspec->_blurb = g_strdup (blurb);
467 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
469 return pspec;
473 * g_param_spec_get_qdata:
474 * @pspec: a valid #GParamSpec
475 * @quark: a #GQuark, naming the user data pointer
477 * Gets back user data pointers stored via g_param_spec_set_qdata().
479 * Returns: (transfer none): the user data pointer set, or %NULL
481 gpointer
482 g_param_spec_get_qdata (GParamSpec *pspec,
483 GQuark quark)
485 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
487 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
491 * g_param_spec_set_qdata:
492 * @pspec: the #GParamSpec to set store a user data pointer
493 * @quark: a #GQuark, naming the user data pointer
494 * @data: an opaque user data pointer
496 * Sets an opaque, named pointer on a #GParamSpec. The name is
497 * specified through a #GQuark (retrieved e.g. via
498 * g_quark_from_static_string()), and the pointer can be gotten back
499 * from the @pspec with g_param_spec_get_qdata(). Setting a
500 * previously set user data pointer, overrides (frees) the old pointer
501 * set, using %NULL as pointer essentially removes the data stored.
503 void
504 g_param_spec_set_qdata (GParamSpec *pspec,
505 GQuark quark,
506 gpointer data)
508 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
509 g_return_if_fail (quark > 0);
511 g_datalist_id_set_data (&pspec->qdata, quark, data);
515 * g_param_spec_set_qdata_full: (skip)
516 * @pspec: the #GParamSpec to set store a user data pointer
517 * @quark: a #GQuark, naming the user data pointer
518 * @data: an opaque user data pointer
519 * @destroy: function to invoke with @data as argument, when @data needs to
520 * be freed
522 * This function works like g_param_spec_set_qdata(), but in addition,
523 * a `void (*destroy) (gpointer)` function may be
524 * specified which is called with @data as argument when the @pspec is
525 * finalized, or the data is being overwritten by a call to
526 * g_param_spec_set_qdata() with the same @quark.
528 void
529 g_param_spec_set_qdata_full (GParamSpec *pspec,
530 GQuark quark,
531 gpointer data,
532 GDestroyNotify destroy)
534 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
535 g_return_if_fail (quark > 0);
537 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
541 * g_param_spec_steal_qdata:
542 * @pspec: the #GParamSpec to get a stored user data pointer from
543 * @quark: a #GQuark, naming the user data pointer
545 * Gets back user data pointers stored via g_param_spec_set_qdata()
546 * and removes the @data from @pspec without invoking its destroy()
547 * function (if any was set). Usually, calling this function is only
548 * required to update user data pointers with a destroy notifier.
550 * Returns: (transfer none): the user data pointer set, or %NULL
552 gpointer
553 g_param_spec_steal_qdata (GParamSpec *pspec,
554 GQuark quark)
556 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
557 g_return_val_if_fail (quark > 0, NULL);
559 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
563 * g_param_spec_get_redirect_target:
564 * @pspec: a #GParamSpec
566 * If the paramspec redirects operations to another paramspec,
567 * returns that paramspec. Redirect is used typically for
568 * providing a new implementation of a property in a derived
569 * type while preserving all the properties from the parent
570 * type. Redirection is established by creating a property
571 * of type #GParamSpecOverride. See g_object_class_override_property()
572 * for an example of the use of this capability.
574 * Since: 2.4
576 * Returns: (transfer none): paramspec to which requests on this
577 * paramspec should be redirected, or %NULL if none.
579 GParamSpec*
580 g_param_spec_get_redirect_target (GParamSpec *pspec)
582 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
584 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
586 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
588 return ospec->overridden;
590 else
591 return NULL;
595 * g_param_value_set_default:
596 * @pspec: a valid #GParamSpec
597 * @value: a #GValue of correct type for @pspec
599 * Sets @value to its default value as specified in @pspec.
601 void
602 g_param_value_set_default (GParamSpec *pspec,
603 GValue *value)
605 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
606 g_return_if_fail (G_IS_VALUE (value));
607 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
609 g_value_reset (value);
610 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
614 * g_param_value_defaults:
615 * @pspec: a valid #GParamSpec
616 * @value: a #GValue of correct type for @pspec
618 * Checks whether @value contains the default value as specified in @pspec.
620 * Returns: whether @value contains the canonical default for this @pspec
622 gboolean
623 g_param_value_defaults (GParamSpec *pspec,
624 GValue *value)
626 GValue dflt_value = G_VALUE_INIT;
627 gboolean defaults;
629 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
630 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
631 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
633 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
634 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
635 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
636 g_value_unset (&dflt_value);
638 return defaults;
642 * g_param_value_validate:
643 * @pspec: a valid #GParamSpec
644 * @value: a #GValue of correct type for @pspec
646 * Ensures that the contents of @value comply with the specifications
647 * set out by @pspec. For example, a #GParamSpecInt might require
648 * that integers stored in @value may not be smaller than -42 and not be
649 * greater than +42. If @value contains an integer outside of this range,
650 * it is modified accordingly, so the resulting value will fit into the
651 * range -42 .. +42.
653 * Returns: whether modifying @value was necessary to ensure validity
655 gboolean
656 g_param_value_validate (GParamSpec *pspec,
657 GValue *value)
659 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
660 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
661 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
663 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
665 GValue oval = *value;
667 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
668 memcmp (&oval.data, &value->data, sizeof (oval.data)))
669 return TRUE;
672 return FALSE;
676 * g_param_value_convert:
677 * @pspec: a valid #GParamSpec
678 * @src_value: souce #GValue
679 * @dest_value: destination #GValue of correct type for @pspec
680 * @strict_validation: %TRUE requires @dest_value to conform to @pspec
681 * without modifications
683 * Transforms @src_value into @dest_value if possible, and then
684 * validates @dest_value, in order for it to conform to @pspec. If
685 * @strict_validation is %TRUE this function will only succeed if the
686 * transformed @dest_value complied to @pspec without modifications.
688 * See also g_value_type_transformable(), g_value_transform() and
689 * g_param_value_validate().
691 * Returns: %TRUE if transformation and validation were successful,
692 * %FALSE otherwise and @dest_value is left untouched.
694 gboolean
695 g_param_value_convert (GParamSpec *pspec,
696 const GValue *src_value,
697 GValue *dest_value,
698 gboolean strict_validation)
700 GValue tmp_value = G_VALUE_INIT;
702 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
703 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
704 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
705 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
707 /* better leave dest_value untouched when returning FALSE */
709 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
710 if (g_value_transform (src_value, &tmp_value) &&
711 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
713 g_value_unset (dest_value);
715 /* values are relocatable */
716 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
718 return TRUE;
720 else
722 g_value_unset (&tmp_value);
724 return FALSE;
729 * g_param_values_cmp:
730 * @pspec: a valid #GParamSpec
731 * @value1: a #GValue of correct type for @pspec
732 * @value2: a #GValue of correct type for @pspec
734 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
735 * if @value1 is found to be less than, equal to or greater than @value2,
736 * respectively.
738 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
740 gint
741 g_param_values_cmp (GParamSpec *pspec,
742 const GValue *value1,
743 const GValue *value2)
745 gint cmp;
747 /* param_values_cmp() effectively does: value1 - value2
748 * so the return values are:
749 * -1) value1 < value2
750 * 0) value1 == value2
751 * 1) value1 > value2
753 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
754 g_return_val_if_fail (G_IS_VALUE (value1), 0);
755 g_return_val_if_fail (G_IS_VALUE (value2), 0);
756 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
757 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
759 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
761 return CLAMP (cmp, -1, 1);
764 static void
765 value_param_init (GValue *value)
767 value->data[0].v_pointer = NULL;
770 static void
771 value_param_free_value (GValue *value)
773 if (value->data[0].v_pointer)
774 g_param_spec_unref (value->data[0].v_pointer);
777 static void
778 value_param_copy_value (const GValue *src_value,
779 GValue *dest_value)
781 if (src_value->data[0].v_pointer)
782 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
783 else
784 dest_value->data[0].v_pointer = NULL;
787 static void
788 value_param_transform_value (const GValue *src_value,
789 GValue *dest_value)
791 if (src_value->data[0].v_pointer &&
792 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
793 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
794 else
795 dest_value->data[0].v_pointer = NULL;
798 static gpointer
799 value_param_peek_pointer (const GValue *value)
801 return value->data[0].v_pointer;
804 static gchar*
805 value_param_collect_value (GValue *value,
806 guint n_collect_values,
807 GTypeCValue *collect_values,
808 guint collect_flags)
810 if (collect_values[0].v_pointer)
812 GParamSpec *param = collect_values[0].v_pointer;
814 if (param->g_type_instance.g_class == NULL)
815 return g_strconcat ("invalid unclassed param spec pointer for value type '",
816 G_VALUE_TYPE_NAME (value),
817 "'",
818 NULL);
819 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
820 return g_strconcat ("invalid param spec type '",
821 G_PARAM_SPEC_TYPE_NAME (param),
822 "' for value type '",
823 G_VALUE_TYPE_NAME (value),
824 "'",
825 NULL);
826 value->data[0].v_pointer = g_param_spec_ref (param);
828 else
829 value->data[0].v_pointer = NULL;
831 return NULL;
834 static gchar*
835 value_param_lcopy_value (const GValue *value,
836 guint n_collect_values,
837 GTypeCValue *collect_values,
838 guint collect_flags)
840 GParamSpec **param_p = collect_values[0].v_pointer;
842 if (!param_p)
843 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
845 if (!value->data[0].v_pointer)
846 *param_p = NULL;
847 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
848 *param_p = value->data[0].v_pointer;
849 else
850 *param_p = g_param_spec_ref (value->data[0].v_pointer);
852 return NULL;
856 /* --- param spec pool --- */
858 * GParamSpecPool:
860 * A #GParamSpecPool maintains a collection of #GParamSpecs which can be
861 * quickly accessed by owner and name. The implementation of the #GObject property
862 * system uses such a pool to store the #GParamSpecs of the properties all object
863 * types.
865 struct _GParamSpecPool
867 GMutex mutex;
868 gboolean type_prefixing;
869 GHashTable *hash_table;
872 static guint
873 param_spec_pool_hash (gconstpointer key_spec)
875 const GParamSpec *key = key_spec;
876 const gchar *p;
877 guint h = key->owner_type;
879 for (p = key->name; *p; p++)
880 h = (h << 5) - h + *p;
882 return h;
885 static gboolean
886 param_spec_pool_equals (gconstpointer key_spec_1,
887 gconstpointer key_spec_2)
889 const GParamSpec *key1 = key_spec_1;
890 const GParamSpec *key2 = key_spec_2;
892 return (key1->owner_type == key2->owner_type &&
893 strcmp (key1->name, key2->name) == 0);
897 * g_param_spec_pool_new:
898 * @type_prefixing: Whether the pool will support type-prefixed property names.
900 * Creates a new #GParamSpecPool.
902 * If @type_prefixing is %TRUE, lookups in the newly created pool will
903 * allow to specify the owner as a colon-separated prefix of the
904 * property name, like "GtkContainer:border-width". This feature is
905 * deprecated, so you should always set @type_prefixing to %FALSE.
907 * Returns: (transfer none): a newly allocated #GParamSpecPool.
909 GParamSpecPool*
910 g_param_spec_pool_new (gboolean type_prefixing)
912 static GMutex init_mutex;
913 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
915 memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
916 pool->type_prefixing = type_prefixing != FALSE;
917 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
919 return pool;
923 * g_param_spec_pool_insert:
924 * @pool: a #GParamSpecPool.
925 * @pspec: the #GParamSpec to insert
926 * @owner_type: a #GType identifying the owner of @pspec
928 * Inserts a #GParamSpec in the pool.
930 void
931 g_param_spec_pool_insert (GParamSpecPool *pool,
932 GParamSpec *pspec,
933 GType owner_type)
935 const gchar *p;
937 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
939 for (p = pspec->name; *p; p++)
941 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
943 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
944 return;
947 g_mutex_lock (&pool->mutex);
948 pspec->owner_type = owner_type;
949 g_param_spec_ref (pspec);
950 g_hash_table_insert (pool->hash_table, pspec, pspec);
951 g_mutex_unlock (&pool->mutex);
953 else
955 g_return_if_fail (pool != NULL);
956 g_return_if_fail (pspec);
957 g_return_if_fail (owner_type > 0);
958 g_return_if_fail (pspec->owner_type == 0);
963 * g_param_spec_pool_remove:
964 * @pool: a #GParamSpecPool
965 * @pspec: the #GParamSpec to remove
967 * Removes a #GParamSpec from the pool.
969 void
970 g_param_spec_pool_remove (GParamSpecPool *pool,
971 GParamSpec *pspec)
973 if (pool && pspec)
975 g_mutex_lock (&pool->mutex);
976 if (g_hash_table_remove (pool->hash_table, pspec))
977 g_param_spec_unref (pspec);
978 else
979 g_warning (G_STRLOC ": attempt to remove unknown pspec '%s' from pool", pspec->name);
980 g_mutex_unlock (&pool->mutex);
982 else
984 g_return_if_fail (pool != NULL);
985 g_return_if_fail (pspec);
989 static inline GParamSpec*
990 param_spec_ht_lookup (GHashTable *hash_table,
991 const gchar *param_name,
992 GType owner_type,
993 gboolean walk_ancestors)
995 GParamSpec key, *pspec;
997 key.owner_type = owner_type;
998 key.name = (gchar*) param_name;
999 if (walk_ancestors)
1002 pspec = g_hash_table_lookup (hash_table, &key);
1003 if (pspec)
1004 return pspec;
1005 key.owner_type = g_type_parent (key.owner_type);
1007 while (key.owner_type);
1008 else
1009 pspec = g_hash_table_lookup (hash_table, &key);
1011 if (!pspec && !is_canonical (param_name))
1013 gchar *canonical;
1015 canonical = g_strdup (key.name);
1016 canonicalize_key (canonical);
1018 /* try canonicalized form */
1019 key.name = canonical;
1020 key.owner_type = owner_type;
1022 if (walk_ancestors)
1025 pspec = g_hash_table_lookup (hash_table, &key);
1026 if (pspec)
1028 g_free (canonical);
1029 return pspec;
1031 key.owner_type = g_type_parent (key.owner_type);
1033 while (key.owner_type);
1034 else
1035 pspec = g_hash_table_lookup (hash_table, &key);
1037 g_free (canonical);
1040 return pspec;
1044 * g_param_spec_pool_lookup:
1045 * @pool: a #GParamSpecPool
1046 * @param_name: the name to look for
1047 * @owner_type: the owner to look for
1048 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1049 * owned by an ancestor of @owner_type.
1051 * Looks up a #GParamSpec in the pool.
1053 * Returns: (transfer none): The found #GParamSpec, or %NULL if no
1054 * matching #GParamSpec was found.
1056 GParamSpec*
1057 g_param_spec_pool_lookup (GParamSpecPool *pool,
1058 const gchar *param_name,
1059 GType owner_type,
1060 gboolean walk_ancestors)
1062 GParamSpec *pspec;
1063 gchar *delim;
1065 g_return_val_if_fail (pool != NULL, NULL);
1066 g_return_val_if_fail (param_name != NULL, NULL);
1068 g_mutex_lock (&pool->mutex);
1070 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1072 /* try quick and away, i.e. without prefix */
1073 if (!delim)
1075 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1076 g_mutex_unlock (&pool->mutex);
1078 return pspec;
1081 /* strip type prefix */
1082 if (pool->type_prefixing && delim[1] == ':')
1084 guint l = delim - param_name;
1085 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1086 GType type;
1088 strncpy (buffer, param_name, delim - param_name);
1089 buffer[l] = 0;
1090 type = g_type_from_name (buffer);
1091 if (l >= 32)
1092 g_free (buffer);
1093 if (type) /* type==0 isn't a valid type pefix */
1095 /* sanity check, these cases don't make a whole lot of sense */
1096 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1098 g_mutex_unlock (&pool->mutex);
1100 return NULL;
1102 owner_type = type;
1103 param_name += l + 2;
1104 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1105 g_mutex_unlock (&pool->mutex);
1107 return pspec;
1110 /* malformed param_name */
1112 g_mutex_unlock (&pool->mutex);
1114 return NULL;
1117 static void
1118 pool_list (gpointer key,
1119 gpointer value,
1120 gpointer user_data)
1122 GParamSpec *pspec = value;
1123 gpointer *data = user_data;
1124 GType owner_type = (GType) data[1];
1126 if (owner_type == pspec->owner_type)
1127 data[0] = g_list_prepend (data[0], pspec);
1131 * g_param_spec_pool_list_owned:
1132 * @pool: a #GParamSpecPool
1133 * @owner_type: the owner to look for
1135 * Gets an #GList of all #GParamSpecs owned by @owner_type in
1136 * the pool.
1138 * Returns: (transfer container) (element-type GObject.ParamSpec): a
1139 * #GList of all #GParamSpecs owned by @owner_type in
1140 * the pool#GParamSpecs.
1142 GList*
1143 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1144 GType owner_type)
1146 gpointer data[2];
1148 g_return_val_if_fail (pool != NULL, NULL);
1149 g_return_val_if_fail (owner_type > 0, NULL);
1151 g_mutex_lock (&pool->mutex);
1152 data[0] = NULL;
1153 data[1] = (gpointer) owner_type;
1154 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1155 g_mutex_unlock (&pool->mutex);
1157 return data[0];
1160 static gint
1161 pspec_compare_id (gconstpointer a,
1162 gconstpointer b)
1164 const GParamSpec *pspec1 = a, *pspec2 = b;
1166 if (pspec1->param_id < pspec2->param_id)
1167 return -1;
1169 if (pspec1->param_id > pspec2->param_id)
1170 return 1;
1172 return strcmp (pspec1->name, pspec2->name);
1175 static inline GSList*
1176 pspec_list_remove_overridden_and_redirected (GSList *plist,
1177 GHashTable *ht,
1178 GType owner_type,
1179 guint *n_p)
1181 GSList *rlist = NULL;
1183 while (plist)
1185 GSList *tmp = plist->next;
1186 GParamSpec *pspec = plist->data;
1187 GParamSpec *found;
1188 gboolean remove = FALSE;
1190 /* Remove paramspecs that are redirected, and also paramspecs
1191 * that have are overridden by non-redirected properties.
1192 * The idea is to get the single paramspec for each name that
1193 * best corresponds to what the application sees.
1195 if (g_param_spec_get_redirect_target (pspec))
1196 remove = TRUE;
1197 else
1199 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1200 if (found != pspec)
1202 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1203 if (redirect != pspec)
1204 remove = TRUE;
1208 if (remove)
1210 g_slist_free_1 (plist);
1212 else
1214 plist->next = rlist;
1215 rlist = plist;
1216 *n_p += 1;
1218 plist = tmp;
1220 return rlist;
1223 static void
1224 pool_depth_list (gpointer key,
1225 gpointer value,
1226 gpointer user_data)
1228 GParamSpec *pspec = value;
1229 gpointer *data = user_data;
1230 GSList **slists = data[0];
1231 GType owner_type = (GType) data[1];
1233 if (g_type_is_a (owner_type, pspec->owner_type))
1235 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1237 slists[0] = g_slist_prepend (slists[0], pspec);
1239 else
1241 guint d = g_type_depth (pspec->owner_type);
1243 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1248 /* We handle interfaces specially since we don't want to
1249 * count interface prerequisites like normal inheritance;
1250 * the property comes from the direct inheritance from
1251 * the prerequisite class, not from the interface that
1252 * prerequires it.
1254 * also 'depth' isn't a meaningful concept for interface
1255 * prerequites.
1257 static void
1258 pool_depth_list_for_interface (gpointer key,
1259 gpointer value,
1260 gpointer user_data)
1262 GParamSpec *pspec = value;
1263 gpointer *data = user_data;
1264 GSList **slists = data[0];
1265 GType owner_type = (GType) data[1];
1267 if (pspec->owner_type == owner_type)
1268 slists[0] = g_slist_prepend (slists[0], pspec);
1272 * g_param_spec_pool_list:
1273 * @pool: a #GParamSpecPool
1274 * @owner_type: the owner to look for
1275 * @n_pspecs_p: (out): return location for the length of the returned array
1277 * Gets an array of all #GParamSpecs owned by @owner_type in
1278 * the pool.
1280 * Returns: (array length=n_pspecs_p) (transfer container): a newly
1281 * allocated array containing pointers to all #GParamSpecs
1282 * owned by @owner_type in the pool
1284 GParamSpec**
1285 g_param_spec_pool_list (GParamSpecPool *pool,
1286 GType owner_type,
1287 guint *n_pspecs_p)
1289 GParamSpec **pspecs, **p;
1290 GSList **slists, *node;
1291 gpointer data[2];
1292 guint d, i;
1294 g_return_val_if_fail (pool != NULL, NULL);
1295 g_return_val_if_fail (owner_type > 0, NULL);
1296 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1298 g_mutex_lock (&pool->mutex);
1299 *n_pspecs_p = 0;
1300 d = g_type_depth (owner_type);
1301 slists = g_new0 (GSList*, d);
1302 data[0] = slists;
1303 data[1] = (gpointer) owner_type;
1305 g_hash_table_foreach (pool->hash_table,
1306 G_TYPE_IS_INTERFACE (owner_type) ?
1307 pool_depth_list_for_interface :
1308 pool_depth_list,
1309 &data);
1311 for (i = 0; i < d; i++)
1312 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1313 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1314 p = pspecs;
1315 for (i = 0; i < d; i++)
1317 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1318 for (node = slists[i]; node; node = node->next)
1319 *p++ = node->data;
1320 g_slist_free (slists[i]);
1322 *p++ = NULL;
1323 g_free (slists);
1324 g_mutex_unlock (&pool->mutex);
1326 return pspecs;
1330 /* --- auxiliary functions --- */
1331 typedef struct
1333 /* class portion */
1334 GType value_type;
1335 void (*finalize) (GParamSpec *pspec);
1336 void (*value_set_default) (GParamSpec *pspec,
1337 GValue *value);
1338 gboolean (*value_validate) (GParamSpec *pspec,
1339 GValue *value);
1340 gint (*values_cmp) (GParamSpec *pspec,
1341 const GValue *value1,
1342 const GValue *value2);
1343 } ParamSpecClassInfo;
1345 static void
1346 param_spec_generic_class_init (gpointer g_class,
1347 gpointer class_data)
1349 GParamSpecClass *class = g_class;
1350 ParamSpecClassInfo *info = class_data;
1352 class->value_type = info->value_type;
1353 if (info->finalize)
1354 class->finalize = info->finalize; /* optional */
1355 class->value_set_default = info->value_set_default;
1356 if (info->value_validate)
1357 class->value_validate = info->value_validate; /* optional */
1358 class->values_cmp = info->values_cmp;
1359 g_free (class_data);
1362 static void
1363 default_value_set_default (GParamSpec *pspec,
1364 GValue *value)
1366 /* value is already zero initialized */
1369 static gint
1370 default_values_cmp (GParamSpec *pspec,
1371 const GValue *value1,
1372 const GValue *value2)
1374 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1378 * g_param_type_register_static:
1379 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1380 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1382 * Registers @name as the name of a new static type derived from
1383 * #G_TYPE_PARAM. The type system uses the information contained in
1384 * the #GParamSpecTypeInfo structure pointed to by @info to manage the
1385 * #GParamSpec type and its instances.
1387 * Returns: The new type identifier.
1389 GType
1390 g_param_type_register_static (const gchar *name,
1391 const GParamSpecTypeInfo *pspec_info)
1393 GTypeInfo info = {
1394 sizeof (GParamSpecClass), /* class_size */
1395 NULL, /* base_init */
1396 NULL, /* base_destroy */
1397 param_spec_generic_class_init, /* class_init */
1398 NULL, /* class_destroy */
1399 NULL, /* class_data */
1400 0, /* instance_size */
1401 16, /* n_preallocs */
1402 NULL, /* instance_init */
1404 ParamSpecClassInfo *cinfo;
1406 g_return_val_if_fail (name != NULL, 0);
1407 g_return_val_if_fail (pspec_info != NULL, 0);
1408 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1409 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1410 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1411 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1412 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1413 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1415 info.instance_size = pspec_info->instance_size;
1416 info.n_preallocs = pspec_info->n_preallocs;
1417 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1418 cinfo = g_new (ParamSpecClassInfo, 1);
1419 cinfo->value_type = pspec_info->value_type;
1420 cinfo->finalize = pspec_info->finalize;
1421 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1422 cinfo->value_validate = pspec_info->value_validate;
1423 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1424 info.class_data = cinfo;
1426 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1430 * g_value_set_param:
1431 * @value: a valid #GValue of type %G_TYPE_PARAM
1432 * @param: (allow-none): the #GParamSpec to be set
1434 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1436 void
1437 g_value_set_param (GValue *value,
1438 GParamSpec *param)
1440 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1441 if (param)
1442 g_return_if_fail (G_IS_PARAM_SPEC (param));
1444 if (value->data[0].v_pointer)
1445 g_param_spec_unref (value->data[0].v_pointer);
1446 value->data[0].v_pointer = param;
1447 if (value->data[0].v_pointer)
1448 g_param_spec_ref (value->data[0].v_pointer);
1452 * g_value_set_param_take_ownership: (skip)
1453 * @value: a valid #GValue of type %G_TYPE_PARAM
1454 * @param: (allow-none): the #GParamSpec to be set
1456 * This is an internal function introduced mainly for C marshallers.
1458 * Deprecated: 2.4: Use g_value_take_param() instead.
1460 void
1461 g_value_set_param_take_ownership (GValue *value,
1462 GParamSpec *param)
1464 g_value_take_param (value, param);
1468 * g_value_take_param: (skip)
1469 * @value: a valid #GValue of type %G_TYPE_PARAM
1470 * @param: (allow-none): the #GParamSpec to be set
1472 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1473 * over the ownership of the callers reference to @param; the caller
1474 * doesn't have to unref it any more.
1476 * Since: 2.4
1478 void
1479 g_value_take_param (GValue *value,
1480 GParamSpec *param)
1482 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1483 if (param)
1484 g_return_if_fail (G_IS_PARAM_SPEC (param));
1486 if (value->data[0].v_pointer)
1487 g_param_spec_unref (value->data[0].v_pointer);
1488 value->data[0].v_pointer = param; /* we take over the reference count */
1492 * g_value_get_param:
1493 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1495 * Get the contents of a %G_TYPE_PARAM #GValue.
1497 * Returns: (transfer none): #GParamSpec content of @value
1499 GParamSpec*
1500 g_value_get_param (const GValue *value)
1502 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1504 return value->data[0].v_pointer;
1508 * g_value_dup_param: (skip)
1509 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1511 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1512 * reference count.
1514 * Returns: #GParamSpec content of @value, should be unreferenced when
1515 * no longer needed.
1517 GParamSpec*
1518 g_value_dup_param (const GValue *value)
1520 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1522 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1526 * g_param_get_default_value:
1527 * @param: a #GParamSpec
1529 * Gets the default value of @param as a pointer to a #GValue.
1531 * The #GValue will remain value for the life of @param.
1533 * Returns: a pointer to a #GValue which must not be modified
1535 * Since: 2.38
1537 const GValue *
1538 g_param_spec_get_default_value (GParamSpec *pspec)
1540 GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
1542 /* We use the type field of the GValue as the key for the once because
1543 * it will be zero before it is initialised and non-zero after. We
1544 * have to take care that we don't write a non-zero value to the type
1545 * field before we are completely done, however, because then another
1546 * thread could come along and find the value partially-initialised.
1548 * In order to accomplish this we store the default value in a
1549 * stack-allocated GValue. We then set the type field in that value
1550 * to zero and copy the contents into place. We then end by storing
1551 * the type as the last step in order to ensure that we're completely
1552 * done before a g_once_init_enter() could take the fast path in
1553 * another thread.
1555 if (g_once_init_enter (&priv->default_value.g_type))
1557 GValue default_value = G_VALUE_INIT;
1559 g_value_init (&default_value, pspec->value_type);
1560 g_param_value_set_default (pspec, &default_value);
1562 /* store all but the type */
1563 default_value.g_type = 0;
1564 priv->default_value = default_value;
1566 g_once_init_leave (&priv->default_value.g_type, pspec->value_type);
1569 return &priv->default_value;