Update German translation
[glib.git] / gobject / gparam.c
blobf50793ae5f055d4372fb85c8d478d3c6441a2bc2
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 (~0U << 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 GQuark name_quark;
85 } GParamSpecPrivate;
87 static gint g_param_private_offset;
89 /* --- functions --- */
90 static inline GParamSpecPrivate *
91 g_param_spec_get_private (GParamSpec *pspec)
93 return &G_STRUCT_MEMBER (GParamSpecPrivate, pspec, g_param_private_offset);
96 void
97 _g_param_type_init (void)
99 static const GTypeFundamentalInfo finfo = {
100 (G_TYPE_FLAG_CLASSED |
101 G_TYPE_FLAG_INSTANTIATABLE |
102 G_TYPE_FLAG_DERIVABLE |
103 G_TYPE_FLAG_DEEP_DERIVABLE),
105 static const GTypeValueTable param_value_table = {
106 value_param_init, /* value_init */
107 value_param_free_value, /* value_free */
108 value_param_copy_value, /* value_copy */
109 value_param_peek_pointer, /* value_peek_pointer */
110 "p", /* collect_format */
111 value_param_collect_value, /* collect_value */
112 "p", /* lcopy_format */
113 value_param_lcopy_value, /* lcopy_value */
115 const GTypeInfo param_spec_info = {
116 sizeof (GParamSpecClass),
118 (GBaseInitFunc) g_param_spec_class_base_init,
119 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
120 (GClassInitFunc) g_param_spec_class_init,
121 (GClassFinalizeFunc) NULL,
122 NULL, /* class_data */
124 sizeof (GParamSpec),
125 0, /* n_preallocs */
126 (GInstanceInitFunc) g_param_spec_init,
128 &param_value_table,
130 GType type;
132 /* This should be registered as GParamSpec instead of GParam, for
133 * consistency sake, so that type name can be mapped to struct name,
134 * However, some language bindings, most noticeable the python ones
135 * depends on the "GParam" identifier, see #548689
137 type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), &param_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
138 g_assert (type == G_TYPE_PARAM);
139 g_param_private_offset = g_type_add_instance_private (type, sizeof (GParamSpecPrivate));
140 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
143 static void
144 g_param_spec_class_base_init (GParamSpecClass *class)
148 static void
149 g_param_spec_class_base_finalize (GParamSpecClass *class)
153 static void
154 g_param_spec_class_init (GParamSpecClass *class,
155 gpointer class_data)
157 class->value_type = G_TYPE_NONE;
158 class->finalize = g_param_spec_finalize;
159 class->value_set_default = NULL;
160 class->value_validate = NULL;
161 class->values_cmp = NULL;
163 g_type_class_adjust_private_offset (class, &g_param_private_offset);
166 static void
167 g_param_spec_init (GParamSpec *pspec,
168 GParamSpecClass *class)
170 pspec->name = NULL;
171 pspec->_nick = NULL;
172 pspec->_blurb = NULL;
173 pspec->flags = 0;
174 pspec->value_type = class->value_type;
175 pspec->owner_type = 0;
176 pspec->qdata = NULL;
177 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
178 pspec->ref_count = 1;
179 pspec->param_id = 0;
182 static void
183 g_param_spec_finalize (GParamSpec *pspec)
185 GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
187 if (priv->default_value.g_type)
188 g_value_reset (&priv->default_value);
190 g_datalist_clear (&pspec->qdata);
192 if (!(pspec->flags & G_PARAM_STATIC_NICK))
193 g_free (pspec->_nick);
195 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
196 g_free (pspec->_blurb);
198 g_type_free_instance ((GTypeInstance*) pspec);
202 * g_param_spec_ref: (skip)
203 * @pspec: a valid #GParamSpec
205 * Increments the reference count of @pspec.
207 * Returns: the #GParamSpec that was passed into this function
209 GParamSpec*
210 g_param_spec_ref (GParamSpec *pspec)
212 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
214 g_atomic_int_inc ((int *)&pspec->ref_count);
216 return pspec;
220 * g_param_spec_unref: (skip)
221 * @pspec: a valid #GParamSpec
223 * Decrements the reference count of a @pspec.
225 void
226 g_param_spec_unref (GParamSpec *pspec)
228 gboolean is_zero;
230 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
232 is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
234 if (G_UNLIKELY (is_zero))
236 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
241 * g_param_spec_sink:
242 * @pspec: a valid #GParamSpec
244 * The initial reference count of a newly created #GParamSpec is 1,
245 * even though no one has explicitly called g_param_spec_ref() on it
246 * yet. So the initial reference count is flagged as "floating", until
247 * someone calls `g_param_spec_ref (pspec); g_param_spec_sink
248 * (pspec);` in sequence on it, taking over the initial
249 * reference count (thus ending up with a @pspec that has a reference
250 * count of 1 still, but is not flagged "floating" anymore).
252 void
253 g_param_spec_sink (GParamSpec *pspec)
255 gsize oldvalue;
256 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
258 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
259 if (oldvalue & PARAM_FLOATING_FLAG)
260 g_param_spec_unref (pspec);
264 * g_param_spec_ref_sink: (skip)
265 * @pspec: a valid #GParamSpec
267 * Convenience function to ref and sink a #GParamSpec.
269 * Since: 2.10
270 * Returns: the #GParamSpec that was passed into this function
272 GParamSpec*
273 g_param_spec_ref_sink (GParamSpec *pspec)
275 gsize oldvalue;
276 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
278 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
279 if (!(oldvalue & PARAM_FLOATING_FLAG))
280 g_param_spec_ref (pspec);
282 return pspec;
286 * g_param_spec_get_name:
287 * @pspec: a valid #GParamSpec
289 * Get the name of a #GParamSpec.
291 * The name is always an "interned" string (as per g_intern_string()).
292 * This allows for pointer-value comparisons.
294 * Returns: the name of @pspec.
296 const gchar *
297 g_param_spec_get_name (GParamSpec *pspec)
299 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
301 return pspec->name;
305 * g_param_spec_get_nick:
306 * @pspec: a valid #GParamSpec
308 * Get the nickname of a #GParamSpec.
310 * Returns: the nickname of @pspec.
312 const gchar *
313 g_param_spec_get_nick (GParamSpec *pspec)
315 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
317 if (pspec->_nick)
318 return pspec->_nick;
319 else
321 GParamSpec *redirect_target;
323 redirect_target = g_param_spec_get_redirect_target (pspec);
324 if (redirect_target && redirect_target->_nick)
325 return redirect_target->_nick;
328 return pspec->name;
332 * g_param_spec_get_blurb:
333 * @pspec: a valid #GParamSpec
335 * Get the short description of a #GParamSpec.
337 * Returns: the short description of @pspec.
339 const gchar *
340 g_param_spec_get_blurb (GParamSpec *pspec)
342 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
344 if (pspec->_blurb)
345 return pspec->_blurb;
346 else
348 GParamSpec *redirect_target;
350 redirect_target = g_param_spec_get_redirect_target (pspec);
351 if (redirect_target && redirect_target->_blurb)
352 return redirect_target->_blurb;
355 return NULL;
358 static void
359 canonicalize_key (gchar *key)
361 gchar *p;
363 for (p = key; *p != 0; p++)
365 gchar c = *p;
367 if (c != '-' &&
368 (c < '0' || c > '9') &&
369 (c < 'A' || c > 'Z') &&
370 (c < 'a' || c > 'z'))
371 *p = '-';
375 static gboolean
376 is_canonical (const gchar *key)
378 const gchar *p;
380 for (p = key; *p != 0; p++)
382 gchar c = *p;
384 if (c != '-' &&
385 (c < '0' || c > '9') &&
386 (c < 'A' || c > 'Z') &&
387 (c < 'a' || c > 'z'))
388 return FALSE;
391 return TRUE;
395 * g_param_spec_internal: (skip)
396 * @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
397 * @name: the canonical name of the property
398 * @nick: the nickname of the property
399 * @blurb: a short description of the property
400 * @flags: a combination of #GParamFlags
402 * Creates a new #GParamSpec instance.
404 * A property name consists of segments consisting of ASCII letters and
405 * digits, separated by either the '-' or '_' character. The first
406 * character of a property name must be a letter. Names which violate these
407 * rules lead to undefined behaviour.
409 * When creating and looking up a #GParamSpec, either separator can be
410 * used, but they cannot be mixed. Using '-' is considerably more
411 * efficient and in fact required when using property names as detail
412 * strings for signals.
414 * Beyond the name, #GParamSpecs have two more descriptive
415 * strings associated with them, the @nick, which should be suitable
416 * for use as a label for the property in a property editor, and the
417 * @blurb, which should be a somewhat longer description, suitable for
418 * e.g. a tooltip. The @nick and @blurb should ideally be localized.
420 * Returns: (type GObject.ParamSpec): a newly allocated #GParamSpec instance
422 gpointer
423 g_param_spec_internal (GType param_type,
424 const gchar *name,
425 const gchar *nick,
426 const gchar *blurb,
427 GParamFlags flags)
429 GParamSpec *pspec;
430 GParamSpecPrivate *priv;
432 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
433 g_return_val_if_fail (name != NULL, NULL);
434 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
435 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
437 pspec = (gpointer) g_type_create_instance (param_type);
439 if (flags & G_PARAM_STATIC_NAME)
441 /* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
442 pspec->name = (gchar *) g_intern_static_string (name);
443 if (!is_canonical (pspec->name))
444 g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
446 else
448 if (is_canonical (name))
449 pspec->name = (gchar *) g_intern_string (name);
450 else
452 gchar *tmp = g_strdup (name);
453 canonicalize_key (tmp);
454 pspec->name = (gchar *) g_intern_string (tmp);
455 g_free (tmp);
459 priv = g_param_spec_get_private (pspec);
460 priv->name_quark = g_quark_from_string (pspec->name);
462 if (flags & G_PARAM_STATIC_NICK)
463 pspec->_nick = (gchar*) nick;
464 else
465 pspec->_nick = g_strdup (nick);
467 if (flags & G_PARAM_STATIC_BLURB)
468 pspec->_blurb = (gchar*) blurb;
469 else
470 pspec->_blurb = g_strdup (blurb);
472 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
474 return pspec;
478 * g_param_spec_get_qdata:
479 * @pspec: a valid #GParamSpec
480 * @quark: a #GQuark, naming the user data pointer
482 * Gets back user data pointers stored via g_param_spec_set_qdata().
484 * Returns: (transfer none): the user data pointer set, or %NULL
486 gpointer
487 g_param_spec_get_qdata (GParamSpec *pspec,
488 GQuark quark)
490 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
492 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
496 * g_param_spec_set_qdata:
497 * @pspec: the #GParamSpec to set store a user data pointer
498 * @quark: a #GQuark, naming the user data pointer
499 * @data: an opaque user data pointer
501 * Sets an opaque, named pointer on a #GParamSpec. The name is
502 * specified through a #GQuark (retrieved e.g. via
503 * g_quark_from_static_string()), and the pointer can be gotten back
504 * from the @pspec with g_param_spec_get_qdata(). Setting a
505 * previously set user data pointer, overrides (frees) the old pointer
506 * set, using %NULL as pointer essentially removes the data stored.
508 void
509 g_param_spec_set_qdata (GParamSpec *pspec,
510 GQuark quark,
511 gpointer data)
513 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
514 g_return_if_fail (quark > 0);
516 g_datalist_id_set_data (&pspec->qdata, quark, data);
520 * g_param_spec_set_qdata_full: (skip)
521 * @pspec: the #GParamSpec to set store a user data pointer
522 * @quark: a #GQuark, naming the user data pointer
523 * @data: an opaque user data pointer
524 * @destroy: function to invoke with @data as argument, when @data needs to
525 * be freed
527 * This function works like g_param_spec_set_qdata(), but in addition,
528 * a `void (*destroy) (gpointer)` function may be
529 * specified which is called with @data as argument when the @pspec is
530 * finalized, or the data is being overwritten by a call to
531 * g_param_spec_set_qdata() with the same @quark.
533 void
534 g_param_spec_set_qdata_full (GParamSpec *pspec,
535 GQuark quark,
536 gpointer data,
537 GDestroyNotify destroy)
539 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
540 g_return_if_fail (quark > 0);
542 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
546 * g_param_spec_steal_qdata:
547 * @pspec: the #GParamSpec to get a stored user data pointer from
548 * @quark: a #GQuark, naming the user data pointer
550 * Gets back user data pointers stored via g_param_spec_set_qdata()
551 * and removes the @data from @pspec without invoking its destroy()
552 * function (if any was set). Usually, calling this function is only
553 * required to update user data pointers with a destroy notifier.
555 * Returns: (transfer none): the user data pointer set, or %NULL
557 gpointer
558 g_param_spec_steal_qdata (GParamSpec *pspec,
559 GQuark quark)
561 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
562 g_return_val_if_fail (quark > 0, NULL);
564 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
568 * g_param_spec_get_redirect_target:
569 * @pspec: a #GParamSpec
571 * If the paramspec redirects operations to another paramspec,
572 * returns that paramspec. Redirect is used typically for
573 * providing a new implementation of a property in a derived
574 * type while preserving all the properties from the parent
575 * type. Redirection is established by creating a property
576 * of type #GParamSpecOverride. See g_object_class_override_property()
577 * for an example of the use of this capability.
579 * Since: 2.4
581 * Returns: (transfer none): paramspec to which requests on this
582 * paramspec should be redirected, or %NULL if none.
584 GParamSpec*
585 g_param_spec_get_redirect_target (GParamSpec *pspec)
587 GTypeInstance *inst = (GTypeInstance *)pspec;
589 if (inst && inst->g_class && inst->g_class->g_type == G_TYPE_PARAM_OVERRIDE)
590 return ((GParamSpecOverride*)pspec)->overridden;
591 else
592 return NULL;
596 * g_param_value_set_default:
597 * @pspec: a valid #GParamSpec
598 * @value: a #GValue of correct type for @pspec
600 * Sets @value to its default value as specified in @pspec.
602 void
603 g_param_value_set_default (GParamSpec *pspec,
604 GValue *value)
606 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
607 g_return_if_fail (G_IS_VALUE (value));
608 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
610 g_value_reset (value);
611 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
615 * g_param_value_defaults:
616 * @pspec: a valid #GParamSpec
617 * @value: a #GValue of correct type for @pspec
619 * Checks whether @value contains the default value as specified in @pspec.
621 * Returns: whether @value contains the canonical default for this @pspec
623 gboolean
624 g_param_value_defaults (GParamSpec *pspec,
625 GValue *value)
627 GValue dflt_value = G_VALUE_INIT;
628 gboolean defaults;
630 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
631 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
632 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
634 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
635 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
636 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
637 g_value_unset (&dflt_value);
639 return defaults;
643 * g_param_value_validate:
644 * @pspec: a valid #GParamSpec
645 * @value: a #GValue of correct type for @pspec
647 * Ensures that the contents of @value comply with the specifications
648 * set out by @pspec. For example, a #GParamSpecInt might require
649 * that integers stored in @value may not be smaller than -42 and not be
650 * greater than +42. If @value contains an integer outside of this range,
651 * it is modified accordingly, so the resulting value will fit into the
652 * range -42 .. +42.
654 * Returns: whether modifying @value was necessary to ensure validity
656 gboolean
657 g_param_value_validate (GParamSpec *pspec,
658 GValue *value)
660 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
661 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
662 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
664 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
666 GValue oval = *value;
668 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
669 memcmp (&oval.data, &value->data, sizeof (oval.data)))
670 return TRUE;
673 return FALSE;
677 * g_param_value_convert:
678 * @pspec: a valid #GParamSpec
679 * @src_value: souce #GValue
680 * @dest_value: destination #GValue of correct type for @pspec
681 * @strict_validation: %TRUE requires @dest_value to conform to @pspec
682 * without modifications
684 * Transforms @src_value into @dest_value if possible, and then
685 * validates @dest_value, in order for it to conform to @pspec. If
686 * @strict_validation is %TRUE this function will only succeed if the
687 * transformed @dest_value complied to @pspec without modifications.
689 * See also g_value_type_transformable(), g_value_transform() and
690 * g_param_value_validate().
692 * Returns: %TRUE if transformation and validation were successful,
693 * %FALSE otherwise and @dest_value is left untouched.
695 gboolean
696 g_param_value_convert (GParamSpec *pspec,
697 const GValue *src_value,
698 GValue *dest_value,
699 gboolean strict_validation)
701 GValue tmp_value = G_VALUE_INIT;
703 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
704 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
705 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
706 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
708 /* better leave dest_value untouched when returning FALSE */
710 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
711 if (g_value_transform (src_value, &tmp_value) &&
712 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
714 g_value_unset (dest_value);
716 /* values are relocatable */
717 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
719 return TRUE;
721 else
723 g_value_unset (&tmp_value);
725 return FALSE;
730 * g_param_values_cmp:
731 * @pspec: a valid #GParamSpec
732 * @value1: a #GValue of correct type for @pspec
733 * @value2: a #GValue of correct type for @pspec
735 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
736 * if @value1 is found to be less than, equal to or greater than @value2,
737 * respectively.
739 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
741 gint
742 g_param_values_cmp (GParamSpec *pspec,
743 const GValue *value1,
744 const GValue *value2)
746 gint cmp;
748 /* param_values_cmp() effectively does: value1 - value2
749 * so the return values are:
750 * -1) value1 < value2
751 * 0) value1 == value2
752 * 1) value1 > value2
754 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
755 g_return_val_if_fail (G_IS_VALUE (value1), 0);
756 g_return_val_if_fail (G_IS_VALUE (value2), 0);
757 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
758 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
760 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
762 return CLAMP (cmp, -1, 1);
765 static void
766 value_param_init (GValue *value)
768 value->data[0].v_pointer = NULL;
771 static void
772 value_param_free_value (GValue *value)
774 if (value->data[0].v_pointer)
775 g_param_spec_unref (value->data[0].v_pointer);
778 static void
779 value_param_copy_value (const GValue *src_value,
780 GValue *dest_value)
782 if (src_value->data[0].v_pointer)
783 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
784 else
785 dest_value->data[0].v_pointer = NULL;
788 static void
789 value_param_transform_value (const GValue *src_value,
790 GValue *dest_value)
792 if (src_value->data[0].v_pointer &&
793 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
794 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
795 else
796 dest_value->data[0].v_pointer = NULL;
799 static gpointer
800 value_param_peek_pointer (const GValue *value)
802 return value->data[0].v_pointer;
805 static gchar*
806 value_param_collect_value (GValue *value,
807 guint n_collect_values,
808 GTypeCValue *collect_values,
809 guint collect_flags)
811 if (collect_values[0].v_pointer)
813 GParamSpec *param = collect_values[0].v_pointer;
815 if (param->g_type_instance.g_class == NULL)
816 return g_strconcat ("invalid unclassed param spec pointer for value type '",
817 G_VALUE_TYPE_NAME (value),
818 "'",
819 NULL);
820 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
821 return g_strconcat ("invalid param spec type '",
822 G_PARAM_SPEC_TYPE_NAME (param),
823 "' for value type '",
824 G_VALUE_TYPE_NAME (value),
825 "'",
826 NULL);
827 value->data[0].v_pointer = g_param_spec_ref (param);
829 else
830 value->data[0].v_pointer = NULL;
832 return NULL;
835 static gchar*
836 value_param_lcopy_value (const GValue *value,
837 guint n_collect_values,
838 GTypeCValue *collect_values,
839 guint collect_flags)
841 GParamSpec **param_p = collect_values[0].v_pointer;
843 if (!param_p)
844 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
846 if (!value->data[0].v_pointer)
847 *param_p = NULL;
848 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
849 *param_p = value->data[0].v_pointer;
850 else
851 *param_p = g_param_spec_ref (value->data[0].v_pointer);
853 return NULL;
857 /* --- param spec pool --- */
859 * GParamSpecPool:
861 * A #GParamSpecPool maintains a collection of #GParamSpecs which can be
862 * quickly accessed by owner and name. The implementation of the #GObject property
863 * system uses such a pool to store the #GParamSpecs of the properties all object
864 * types.
866 struct _GParamSpecPool
868 GMutex mutex;
869 gboolean type_prefixing;
870 GHashTable *hash_table;
873 static guint
874 param_spec_pool_hash (gconstpointer key_spec)
876 const GParamSpec *key = key_spec;
877 const gchar *p;
878 guint h = key->owner_type;
880 for (p = key->name; *p; p++)
881 h = (h << 5) - h + *p;
883 return h;
886 static gboolean
887 param_spec_pool_equals (gconstpointer key_spec_1,
888 gconstpointer key_spec_2)
890 const GParamSpec *key1 = key_spec_1;
891 const GParamSpec *key2 = key_spec_2;
893 return (key1->owner_type == key2->owner_type &&
894 strcmp (key1->name, key2->name) == 0);
898 * g_param_spec_pool_new:
899 * @type_prefixing: Whether the pool will support type-prefixed property names.
901 * Creates a new #GParamSpecPool.
903 * If @type_prefixing is %TRUE, lookups in the newly created pool will
904 * allow to specify the owner as a colon-separated prefix of the
905 * property name, like "GtkContainer:border-width". This feature is
906 * deprecated, so you should always set @type_prefixing to %FALSE.
908 * Returns: (transfer none): a newly allocated #GParamSpecPool.
910 GParamSpecPool*
911 g_param_spec_pool_new (gboolean type_prefixing)
913 static GMutex init_mutex;
914 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
916 memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
917 pool->type_prefixing = type_prefixing != FALSE;
918 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
920 return pool;
924 * g_param_spec_pool_insert:
925 * @pool: a #GParamSpecPool.
926 * @pspec: the #GParamSpec to insert
927 * @owner_type: a #GType identifying the owner of @pspec
929 * Inserts a #GParamSpec in the pool.
931 void
932 g_param_spec_pool_insert (GParamSpecPool *pool,
933 GParamSpec *pspec,
934 GType owner_type)
936 const gchar *p;
938 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
940 for (p = pspec->name; *p; p++)
942 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
944 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
945 return;
948 g_mutex_lock (&pool->mutex);
949 pspec->owner_type = owner_type;
950 g_param_spec_ref (pspec);
951 g_hash_table_insert (pool->hash_table, pspec, pspec);
952 g_mutex_unlock (&pool->mutex);
954 else
956 g_return_if_fail (pool != NULL);
957 g_return_if_fail (pspec);
958 g_return_if_fail (owner_type > 0);
959 g_return_if_fail (pspec->owner_type == 0);
964 * g_param_spec_pool_remove:
965 * @pool: a #GParamSpecPool
966 * @pspec: the #GParamSpec to remove
968 * Removes a #GParamSpec from the pool.
970 void
971 g_param_spec_pool_remove (GParamSpecPool *pool,
972 GParamSpec *pspec)
974 if (pool && pspec)
976 g_mutex_lock (&pool->mutex);
977 if (g_hash_table_remove (pool->hash_table, pspec))
978 g_param_spec_unref (pspec);
979 else
980 g_warning (G_STRLOC ": attempt to remove unknown pspec '%s' from pool", pspec->name);
981 g_mutex_unlock (&pool->mutex);
983 else
985 g_return_if_fail (pool != NULL);
986 g_return_if_fail (pspec);
990 static inline GParamSpec*
991 param_spec_ht_lookup (GHashTable *hash_table,
992 const gchar *param_name,
993 GType owner_type,
994 gboolean walk_ancestors)
996 GParamSpec key, *pspec;
998 key.owner_type = owner_type;
999 key.name = (gchar*) param_name;
1000 if (walk_ancestors)
1003 pspec = g_hash_table_lookup (hash_table, &key);
1004 if (pspec)
1005 return pspec;
1006 key.owner_type = g_type_parent (key.owner_type);
1008 while (key.owner_type);
1009 else
1010 pspec = g_hash_table_lookup (hash_table, &key);
1012 if (!pspec && !is_canonical (param_name))
1014 gchar *canonical;
1016 canonical = g_strdup (key.name);
1017 canonicalize_key (canonical);
1019 /* try canonicalized form */
1020 key.name = canonical;
1021 key.owner_type = owner_type;
1023 if (walk_ancestors)
1026 pspec = g_hash_table_lookup (hash_table, &key);
1027 if (pspec)
1029 g_free (canonical);
1030 return pspec;
1032 key.owner_type = g_type_parent (key.owner_type);
1034 while (key.owner_type);
1035 else
1036 pspec = g_hash_table_lookup (hash_table, &key);
1038 g_free (canonical);
1041 return pspec;
1045 * g_param_spec_pool_lookup:
1046 * @pool: a #GParamSpecPool
1047 * @param_name: the name to look for
1048 * @owner_type: the owner to look for
1049 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1050 * owned by an ancestor of @owner_type.
1052 * Looks up a #GParamSpec in the pool.
1054 * Returns: (transfer none): The found #GParamSpec, or %NULL if no
1055 * matching #GParamSpec was found.
1057 GParamSpec*
1058 g_param_spec_pool_lookup (GParamSpecPool *pool,
1059 const gchar *param_name,
1060 GType owner_type,
1061 gboolean walk_ancestors)
1063 GParamSpec *pspec;
1064 gchar *delim;
1066 g_return_val_if_fail (pool != NULL, NULL);
1067 g_return_val_if_fail (param_name != NULL, NULL);
1069 g_mutex_lock (&pool->mutex);
1071 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1073 /* try quick and away, i.e. without prefix */
1074 if (!delim)
1076 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1077 g_mutex_unlock (&pool->mutex);
1079 return pspec;
1082 /* strip type prefix */
1083 if (pool->type_prefixing && delim[1] == ':')
1085 guint l = delim - param_name;
1086 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1087 GType type;
1089 strncpy (buffer, param_name, delim - param_name);
1090 buffer[l] = 0;
1091 type = g_type_from_name (buffer);
1092 if (l >= 32)
1093 g_free (buffer);
1094 if (type) /* type==0 isn't a valid type pefix */
1096 /* sanity check, these cases don't make a whole lot of sense */
1097 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1099 g_mutex_unlock (&pool->mutex);
1101 return NULL;
1103 owner_type = type;
1104 param_name += l + 2;
1105 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1106 g_mutex_unlock (&pool->mutex);
1108 return pspec;
1111 /* malformed param_name */
1113 g_mutex_unlock (&pool->mutex);
1115 return NULL;
1118 static void
1119 pool_list (gpointer key,
1120 gpointer value,
1121 gpointer user_data)
1123 GParamSpec *pspec = value;
1124 gpointer *data = user_data;
1125 GType owner_type = (GType) data[1];
1127 if (owner_type == pspec->owner_type)
1128 data[0] = g_list_prepend (data[0], pspec);
1132 * g_param_spec_pool_list_owned:
1133 * @pool: a #GParamSpecPool
1134 * @owner_type: the owner to look for
1136 * Gets an #GList of all #GParamSpecs owned by @owner_type in
1137 * the pool.
1139 * Returns: (transfer container) (element-type GObject.ParamSpec): a
1140 * #GList of all #GParamSpecs owned by @owner_type in
1141 * the pool#GParamSpecs.
1143 GList*
1144 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1145 GType owner_type)
1147 gpointer data[2];
1149 g_return_val_if_fail (pool != NULL, NULL);
1150 g_return_val_if_fail (owner_type > 0, NULL);
1152 g_mutex_lock (&pool->mutex);
1153 data[0] = NULL;
1154 data[1] = (gpointer) owner_type;
1155 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1156 g_mutex_unlock (&pool->mutex);
1158 return data[0];
1161 static gint
1162 pspec_compare_id (gconstpointer a,
1163 gconstpointer b)
1165 const GParamSpec *pspec1 = a, *pspec2 = b;
1167 if (pspec1->param_id < pspec2->param_id)
1168 return -1;
1170 if (pspec1->param_id > pspec2->param_id)
1171 return 1;
1173 return strcmp (pspec1->name, pspec2->name);
1176 static inline GSList*
1177 pspec_list_remove_overridden_and_redirected (GSList *plist,
1178 GHashTable *ht,
1179 GType owner_type,
1180 guint *n_p)
1182 GSList *rlist = NULL;
1184 while (plist)
1186 GSList *tmp = plist->next;
1187 GParamSpec *pspec = plist->data;
1188 GParamSpec *found;
1189 gboolean remove = FALSE;
1191 /* Remove paramspecs that are redirected, and also paramspecs
1192 * that have are overridden by non-redirected properties.
1193 * The idea is to get the single paramspec for each name that
1194 * best corresponds to what the application sees.
1196 if (g_param_spec_get_redirect_target (pspec))
1197 remove = TRUE;
1198 else
1200 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1201 if (found != pspec)
1203 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1204 if (redirect != pspec)
1205 remove = TRUE;
1209 if (remove)
1211 g_slist_free_1 (plist);
1213 else
1215 plist->next = rlist;
1216 rlist = plist;
1217 *n_p += 1;
1219 plist = tmp;
1221 return rlist;
1224 static void
1225 pool_depth_list (gpointer key,
1226 gpointer value,
1227 gpointer user_data)
1229 GParamSpec *pspec = value;
1230 gpointer *data = user_data;
1231 GSList **slists = data[0];
1232 GType owner_type = (GType) data[1];
1234 if (g_type_is_a (owner_type, pspec->owner_type))
1236 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1238 slists[0] = g_slist_prepend (slists[0], pspec);
1240 else
1242 guint d = g_type_depth (pspec->owner_type);
1244 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1249 /* We handle interfaces specially since we don't want to
1250 * count interface prerequisites like normal inheritance;
1251 * the property comes from the direct inheritance from
1252 * the prerequisite class, not from the interface that
1253 * prerequires it.
1255 * also 'depth' isn't a meaningful concept for interface
1256 * prerequites.
1258 static void
1259 pool_depth_list_for_interface (gpointer key,
1260 gpointer value,
1261 gpointer user_data)
1263 GParamSpec *pspec = value;
1264 gpointer *data = user_data;
1265 GSList **slists = data[0];
1266 GType owner_type = (GType) data[1];
1268 if (pspec->owner_type == owner_type)
1269 slists[0] = g_slist_prepend (slists[0], pspec);
1273 * g_param_spec_pool_list:
1274 * @pool: a #GParamSpecPool
1275 * @owner_type: the owner to look for
1276 * @n_pspecs_p: (out): return location for the length of the returned array
1278 * Gets an array of all #GParamSpecs owned by @owner_type in
1279 * the pool.
1281 * Returns: (array length=n_pspecs_p) (transfer container): a newly
1282 * allocated array containing pointers to all #GParamSpecs
1283 * owned by @owner_type in the pool
1285 GParamSpec**
1286 g_param_spec_pool_list (GParamSpecPool *pool,
1287 GType owner_type,
1288 guint *n_pspecs_p)
1290 GParamSpec **pspecs, **p;
1291 GSList **slists, *node;
1292 gpointer data[2];
1293 guint d, i;
1295 g_return_val_if_fail (pool != NULL, NULL);
1296 g_return_val_if_fail (owner_type > 0, NULL);
1297 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1299 g_mutex_lock (&pool->mutex);
1300 *n_pspecs_p = 0;
1301 d = g_type_depth (owner_type);
1302 slists = g_new0 (GSList*, d);
1303 data[0] = slists;
1304 data[1] = (gpointer) owner_type;
1306 g_hash_table_foreach (pool->hash_table,
1307 G_TYPE_IS_INTERFACE (owner_type) ?
1308 pool_depth_list_for_interface :
1309 pool_depth_list,
1310 &data);
1312 for (i = 0; i < d; i++)
1313 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1314 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1315 p = pspecs;
1316 for (i = 0; i < d; i++)
1318 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1319 for (node = slists[i]; node; node = node->next)
1320 *p++ = node->data;
1321 g_slist_free (slists[i]);
1323 *p++ = NULL;
1324 g_free (slists);
1325 g_mutex_unlock (&pool->mutex);
1327 return pspecs;
1331 /* --- auxiliary functions --- */
1332 typedef struct
1334 /* class portion */
1335 GType value_type;
1336 void (*finalize) (GParamSpec *pspec);
1337 void (*value_set_default) (GParamSpec *pspec,
1338 GValue *value);
1339 gboolean (*value_validate) (GParamSpec *pspec,
1340 GValue *value);
1341 gint (*values_cmp) (GParamSpec *pspec,
1342 const GValue *value1,
1343 const GValue *value2);
1344 } ParamSpecClassInfo;
1346 static void
1347 param_spec_generic_class_init (gpointer g_class,
1348 gpointer class_data)
1350 GParamSpecClass *class = g_class;
1351 ParamSpecClassInfo *info = class_data;
1353 class->value_type = info->value_type;
1354 if (info->finalize)
1355 class->finalize = info->finalize; /* optional */
1356 class->value_set_default = info->value_set_default;
1357 if (info->value_validate)
1358 class->value_validate = info->value_validate; /* optional */
1359 class->values_cmp = info->values_cmp;
1360 g_free (class_data);
1363 static void
1364 default_value_set_default (GParamSpec *pspec,
1365 GValue *value)
1367 /* value is already zero initialized */
1370 static gint
1371 default_values_cmp (GParamSpec *pspec,
1372 const GValue *value1,
1373 const GValue *value2)
1375 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1379 * g_param_type_register_static:
1380 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1381 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1383 * Registers @name as the name of a new static type derived from
1384 * #G_TYPE_PARAM. The type system uses the information contained in
1385 * the #GParamSpecTypeInfo structure pointed to by @info to manage the
1386 * #GParamSpec type and its instances.
1388 * Returns: The new type identifier.
1390 GType
1391 g_param_type_register_static (const gchar *name,
1392 const GParamSpecTypeInfo *pspec_info)
1394 GTypeInfo info = {
1395 sizeof (GParamSpecClass), /* class_size */
1396 NULL, /* base_init */
1397 NULL, /* base_destroy */
1398 param_spec_generic_class_init, /* class_init */
1399 NULL, /* class_destroy */
1400 NULL, /* class_data */
1401 0, /* instance_size */
1402 16, /* n_preallocs */
1403 NULL, /* instance_init */
1405 ParamSpecClassInfo *cinfo;
1407 g_return_val_if_fail (name != NULL, 0);
1408 g_return_val_if_fail (pspec_info != NULL, 0);
1409 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1410 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1411 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1412 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1413 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1414 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1416 info.instance_size = pspec_info->instance_size;
1417 info.n_preallocs = pspec_info->n_preallocs;
1418 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1419 cinfo = g_new (ParamSpecClassInfo, 1);
1420 cinfo->value_type = pspec_info->value_type;
1421 cinfo->finalize = pspec_info->finalize;
1422 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1423 cinfo->value_validate = pspec_info->value_validate;
1424 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1425 info.class_data = cinfo;
1427 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1431 * g_value_set_param:
1432 * @value: a valid #GValue of type %G_TYPE_PARAM
1433 * @param: (nullable): the #GParamSpec to be set
1435 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1437 void
1438 g_value_set_param (GValue *value,
1439 GParamSpec *param)
1441 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1442 if (param)
1443 g_return_if_fail (G_IS_PARAM_SPEC (param));
1445 if (value->data[0].v_pointer)
1446 g_param_spec_unref (value->data[0].v_pointer);
1447 value->data[0].v_pointer = param;
1448 if (value->data[0].v_pointer)
1449 g_param_spec_ref (value->data[0].v_pointer);
1453 * g_value_set_param_take_ownership: (skip)
1454 * @value: a valid #GValue of type %G_TYPE_PARAM
1455 * @param: (nullable): the #GParamSpec to be set
1457 * This is an internal function introduced mainly for C marshallers.
1459 * Deprecated: 2.4: Use g_value_take_param() instead.
1461 void
1462 g_value_set_param_take_ownership (GValue *value,
1463 GParamSpec *param)
1465 g_value_take_param (value, param);
1469 * g_value_take_param: (skip)
1470 * @value: a valid #GValue of type %G_TYPE_PARAM
1471 * @param: (nullable): the #GParamSpec to be set
1473 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1474 * over the ownership of the callers reference to @param; the caller
1475 * doesn't have to unref it any more.
1477 * Since: 2.4
1479 void
1480 g_value_take_param (GValue *value,
1481 GParamSpec *param)
1483 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1484 if (param)
1485 g_return_if_fail (G_IS_PARAM_SPEC (param));
1487 if (value->data[0].v_pointer)
1488 g_param_spec_unref (value->data[0].v_pointer);
1489 value->data[0].v_pointer = param; /* we take over the reference count */
1493 * g_value_get_param:
1494 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1496 * Get the contents of a %G_TYPE_PARAM #GValue.
1498 * Returns: (transfer none): #GParamSpec content of @value
1500 GParamSpec*
1501 g_value_get_param (const GValue *value)
1503 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1505 return value->data[0].v_pointer;
1509 * g_value_dup_param: (skip)
1510 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1512 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1513 * reference count.
1515 * Returns: #GParamSpec content of @value, should be unreferenced when
1516 * no longer needed.
1518 GParamSpec*
1519 g_value_dup_param (const GValue *value)
1521 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1523 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1527 * g_param_spec_get_default_value:
1528 * @pspec: a #GParamSpec
1530 * Gets the default value of @pspec as a pointer to a #GValue.
1532 * The #GValue will remain value for the life of @pspec.
1534 * Returns: a pointer to a #GValue which must not be modified
1536 * Since: 2.38
1538 const GValue *
1539 g_param_spec_get_default_value (GParamSpec *pspec)
1541 GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
1543 /* We use the type field of the GValue as the key for the once because
1544 * it will be zero before it is initialised and non-zero after. We
1545 * have to take care that we don't write a non-zero value to the type
1546 * field before we are completely done, however, because then another
1547 * thread could come along and find the value partially-initialised.
1549 * In order to accomplish this we store the default value in a
1550 * stack-allocated GValue. We then set the type field in that value
1551 * to zero and copy the contents into place. We then end by storing
1552 * the type as the last step in order to ensure that we're completely
1553 * done before a g_once_init_enter() could take the fast path in
1554 * another thread.
1556 if (g_once_init_enter (&priv->default_value.g_type))
1558 GValue default_value = G_VALUE_INIT;
1560 g_value_init (&default_value, pspec->value_type);
1561 g_param_value_set_default (pspec, &default_value);
1563 /* store all but the type */
1564 default_value.g_type = 0;
1565 priv->default_value = default_value;
1567 g_once_init_leave (&priv->default_value.g_type, pspec->value_type);
1570 return &priv->default_value;
1574 * g_param_spec_get_name_quark:
1575 * @pspec: a #GParamSpec
1577 * Gets the GQuark for the name.
1579 * Returns: the GQuark for @pspec->name.
1581 * Since: 2.46
1583 GQuark
1584 g_param_spec_get_name_quark (GParamSpec *pspec)
1586 GParamSpecPrivate *priv = g_param_spec_get_private (pspec);
1588 /* Return the quark that we've stashed away at creation time.
1589 * This lets us avoid a lock and a hash table lookup when
1590 * dispatching property change notification.
1593 return priv->name_quark;