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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
29 #include "gparamspecs.h"
30 #include "gvaluecollector.h"
35 * @short_description: Metadata for parameter specifications
36 * @see_also: g_object_class_install_property(), g_object_set(),
37 * g_object_get(), g_object_set_property(), g_object_get_property(),
38 * g_value_register_transform_func()
41 * #GParamSpec is an object structure that encapsulates the metadata
42 * required to specify parameters, such as e.g. #GObject properties.
44 * <para id="canonical-parameter-name">
45 * Parameter names need to start with a letter (a-z or A-Z). Subsequent
46 * characters can be letters, numbers or a '-'.
47 * All other characters are replaced by a '-' during construction.
48 * The result of this replacement is called the canonical name of the
55 #define PARAM_FLOATING_FLAG 0x2
56 #define G_PARAM_USER_MASK (~0 << G_PARAM_USER_SHIFT)
57 #define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
58 #define G_SLOCK(mutex) g_static_mutex_lock (mutex)
59 #define G_SUNLOCK(mutex) g_static_mutex_unlock (mutex)
62 /* --- prototypes --- */
63 static void g_param_spec_class_base_init (GParamSpecClass *class);
64 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
65 static void g_param_spec_class_init (GParamSpecClass *class,
67 static void g_param_spec_init (GParamSpec *pspec,
68 GParamSpecClass *class);
69 static void g_param_spec_finalize (GParamSpec *pspec);
70 static void value_param_init (GValue *value);
71 static void value_param_free_value (GValue *value);
72 static void value_param_copy_value (const GValue *src_value,
74 static void value_param_transform_value (const GValue *src_value,
76 static gpointer value_param_peek_pointer (const GValue *value);
77 static gchar* value_param_collect_value (GValue *value,
78 guint n_collect_values,
79 GTypeCValue *collect_values,
81 static gchar* value_param_lcopy_value (const GValue *value,
82 guint n_collect_values,
83 GTypeCValue *collect_values,
87 /* --- functions --- */
89 _g_param_type_init (void)
91 static const GTypeFundamentalInfo finfo = {
92 (G_TYPE_FLAG_CLASSED |
93 G_TYPE_FLAG_INSTANTIATABLE |
94 G_TYPE_FLAG_DERIVABLE |
95 G_TYPE_FLAG_DEEP_DERIVABLE),
97 static const GTypeValueTable param_value_table = {
98 value_param_init, /* value_init */
99 value_param_free_value, /* value_free */
100 value_param_copy_value, /* value_copy */
101 value_param_peek_pointer, /* value_peek_pointer */
102 "p", /* collect_format */
103 value_param_collect_value, /* collect_value */
104 "p", /* lcopy_format */
105 value_param_lcopy_value, /* lcopy_value */
107 static const GTypeInfo param_spec_info = {
108 sizeof (GParamSpecClass),
110 (GBaseInitFunc) g_param_spec_class_base_init,
111 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
112 (GClassInitFunc) g_param_spec_class_init,
113 (GClassFinalizeFunc) NULL,
114 NULL, /* class_data */
118 (GInstanceInitFunc) g_param_spec_init,
124 /* This should be registred as GParamSpec instead of GParam, for
125 * consistency sake, so that type name can be mapped to struct name,
126 * However, some language bindings, most noticable the python ones
127 * depends on the "GParam" identifier, see #548689
129 type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
130 g_assert (type == G_TYPE_PARAM);
131 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
135 g_param_spec_class_base_init (GParamSpecClass *class)
140 g_param_spec_class_base_finalize (GParamSpecClass *class)
145 g_param_spec_class_init (GParamSpecClass *class,
148 class->value_type = G_TYPE_NONE;
149 class->finalize = g_param_spec_finalize;
150 class->value_set_default = NULL;
151 class->value_validate = NULL;
152 class->values_cmp = NULL;
156 g_param_spec_init (GParamSpec *pspec,
157 GParamSpecClass *class)
161 pspec->_blurb = NULL;
163 pspec->value_type = class->value_type;
164 pspec->owner_type = 0;
166 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
167 pspec->ref_count = 1;
172 g_param_spec_finalize (GParamSpec *pspec)
174 g_datalist_clear (&pspec->qdata);
176 if (!(pspec->flags & G_PARAM_STATIC_NAME))
177 g_free (pspec->name);
179 if (!(pspec->flags & G_PARAM_STATIC_NICK))
180 g_free (pspec->_nick);
182 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
183 g_free (pspec->_blurb);
185 g_type_free_instance ((GTypeInstance*) pspec);
189 * g_param_spec_ref: (skip)
190 * @pspec: a valid #GParamSpec
192 * Increments the reference count of @pspec.
194 * Returns: the #GParamSpec that was passed into this function
197 g_param_spec_ref (GParamSpec *pspec)
199 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
201 g_atomic_int_inc ((int *)&pspec->ref_count);
207 * g_param_spec_unref: (skip)
208 * @pspec: a valid #GParamSpec
210 * Decrements the reference count of a @pspec.
213 g_param_spec_unref (GParamSpec *pspec)
217 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
219 is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
221 if (G_UNLIKELY (is_zero))
223 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
229 * @pspec: a valid #GParamSpec
231 * The initial reference count of a newly created #GParamSpec is 1,
232 * even though no one has explicitly called g_param_spec_ref() on it
233 * yet. So the initial reference count is flagged as "floating", until
234 * someone calls <literal>g_param_spec_ref (pspec); g_param_spec_sink
235 * (pspec);</literal> in sequence on it, taking over the initial
236 * reference count (thus ending up with a @pspec that has a reference
237 * count of 1 still, but is not flagged "floating" anymore).
240 g_param_spec_sink (GParamSpec *pspec)
243 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
245 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
246 if (oldvalue & PARAM_FLOATING_FLAG)
247 g_param_spec_unref (pspec);
251 * g_param_spec_ref_sink: (skip)
252 * @pspec: a valid #GParamSpec
254 * Convenience function to ref and sink a #GParamSpec.
257 * Returns: the #GParamSpec that was passed into this function
260 g_param_spec_ref_sink (GParamSpec *pspec)
263 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
265 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
266 if (!(oldvalue & PARAM_FLOATING_FLAG))
267 g_param_spec_ref (pspec);
273 * g_param_spec_get_name:
274 * @pspec: a valid #GParamSpec
276 * Get the name of a #GParamSpec.
278 * Returns: the name of @pspec.
280 G_CONST_RETURN gchar*
281 g_param_spec_get_name (GParamSpec *pspec)
283 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
289 * g_param_spec_get_nick:
290 * @pspec: a valid #GParamSpec
292 * Get the nickname of a #GParamSpec.
294 * Returns: the nickname of @pspec.
296 G_CONST_RETURN gchar*
297 g_param_spec_get_nick (GParamSpec *pspec)
299 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
305 GParamSpec *redirect_target;
307 redirect_target = g_param_spec_get_redirect_target (pspec);
308 if (redirect_target && redirect_target->_nick)
309 return redirect_target->_nick;
316 * g_param_spec_get_blurb:
317 * @pspec: a valid #GParamSpec
319 * Get the short description of a #GParamSpec.
321 * Returns: the short description of @pspec.
323 G_CONST_RETURN gchar*
324 g_param_spec_get_blurb (GParamSpec *pspec)
326 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
329 return pspec->_blurb;
332 GParamSpec *redirect_target;
334 redirect_target = g_param_spec_get_redirect_target (pspec);
335 if (redirect_target && redirect_target->_blurb)
336 return redirect_target->_blurb;
343 canonicalize_key (gchar *key)
347 for (p = key; *p != 0; p++)
352 (c < '0' || c > '9') &&
353 (c < 'A' || c > 'Z') &&
354 (c < 'a' || c > 'z'))
360 is_canonical (const gchar *key)
364 for (p = key; *p != 0; p++)
369 (c < '0' || c > '9') &&
370 (c < 'A' || c > 'Z') &&
371 (c < 'a' || c > 'z'))
379 * g_param_spec_internal: (skip)
380 * @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
381 * @name: the canonical name of the property
382 * @nick: the nickname of the property
383 * @blurb: a short description of the property
384 * @flags: a combination of #GParamFlags
386 * Creates a new #GParamSpec instance.
388 * A property name consists of segments consisting of ASCII letters and
389 * digits, separated by either the '-' or '_' character. The first
390 * character of a property name must be a letter. Names which violate these
391 * rules lead to undefined behaviour.
393 * When creating and looking up a #GParamSpec, either separator can be
394 * used, but they cannot be mixed. Using '-' is considerably more
395 * efficient and in fact required when using property names as detail
396 * strings for signals.
398 * Beyond the name, #GParamSpec<!-- -->s have two more descriptive
399 * strings associated with them, the @nick, which should be suitable
400 * for use as a label for the property in a property editor, and the
401 * @blurb, which should be a somewhat longer description, suitable for
402 * e.g. a tooltip. The @nick and @blurb should ideally be localized.
404 * Returns: a newly allocated #GParamSpec instance
407 g_param_spec_internal (GType param_type,
415 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
416 g_return_val_if_fail (name != NULL, NULL);
417 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
418 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
420 pspec = (gpointer) g_type_create_instance (param_type);
422 if (flags & G_PARAM_STATIC_NAME)
424 pspec->name = g_intern_static_string (name);
425 if (!is_canonical (pspec->name))
426 g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
430 pspec->name = g_strdup (name);
431 canonicalize_key (pspec->name);
432 g_intern_string (pspec->name);
435 if (flags & G_PARAM_STATIC_NICK)
436 pspec->_nick = (gchar*) nick;
438 pspec->_nick = g_strdup (nick);
440 if (flags & G_PARAM_STATIC_BLURB)
441 pspec->_blurb = (gchar*) blurb;
443 pspec->_blurb = g_strdup (blurb);
445 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
451 * g_param_spec_get_qdata:
452 * @pspec: a valid #GParamSpec
453 * @quark: a #GQuark, naming the user data pointer
455 * Gets back user data pointers stored via g_param_spec_set_qdata().
457 * Returns: (transfer none): the user data pointer set, or %NULL
460 g_param_spec_get_qdata (GParamSpec *pspec,
463 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
465 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
469 * g_param_spec_set_qdata:
470 * @pspec: the #GParamSpec to set store a user data pointer
471 * @quark: a #GQuark, naming the user data pointer
472 * @data: an opaque user data pointer
474 * Sets an opaque, named pointer on a #GParamSpec. The name is
475 * specified through a #GQuark (retrieved e.g. via
476 * g_quark_from_static_string()), and the pointer can be gotten back
477 * from the @pspec with g_param_spec_get_qdata(). Setting a
478 * previously set user data pointer, overrides (frees) the old pointer
479 * set, using %NULL as pointer essentially removes the data stored.
482 g_param_spec_set_qdata (GParamSpec *pspec,
486 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
487 g_return_if_fail (quark > 0);
489 g_datalist_id_set_data (&pspec->qdata, quark, data);
493 * g_param_spec_set_qdata_full: (skip)
494 * @pspec: the #GParamSpec to set store a user data pointer
495 * @quark: a #GQuark, naming the user data pointer
496 * @data: an opaque user data pointer
497 * @destroy: function to invoke with @data as argument, when @data needs to
500 * This function works like g_param_spec_set_qdata(), but in addition,
501 * a <literal>void (*destroy) (gpointer)</literal> function may be
502 * specified which is called with @data as argument when the @pspec is
503 * finalized, or the data is being overwritten by a call to
504 * g_param_spec_set_qdata() with the same @quark.
507 g_param_spec_set_qdata_full (GParamSpec *pspec,
510 GDestroyNotify destroy)
512 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
513 g_return_if_fail (quark > 0);
515 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
519 * g_param_spec_steal_qdata:
520 * @pspec: the #GParamSpec to get a stored user data pointer from
521 * @quark: a #GQuark, naming the user data pointer
523 * Gets back user data pointers stored via g_param_spec_set_qdata()
524 * and removes the @data from @pspec without invoking its destroy()
525 * function (if any was set). Usually, calling this function is only
526 * required to update user data pointers with a destroy notifier.
528 * Returns: (transfer none): the user data pointer set, or %NULL
531 g_param_spec_steal_qdata (GParamSpec *pspec,
534 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
535 g_return_val_if_fail (quark > 0, NULL);
537 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
541 * g_param_spec_get_redirect_target:
542 * @pspec: a #GParamSpec
544 * If the paramspec redirects operations to another paramspec,
545 * returns that paramspec. Redirect is used typically for
546 * providing a new implementation of a property in a derived
547 * type while preserving all the properties from the parent
548 * type. Redirection is established by creating a property
549 * of type #GParamSpecOverride. See g_object_class_override_property()
550 * for an example of the use of this capability.
554 * Returns: (transfer none): paramspec to which requests on this
555 * paramspec should be redirected, or %NULL if none.
558 g_param_spec_get_redirect_target (GParamSpec *pspec)
560 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
562 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
564 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
566 return ospec->overridden;
573 * g_param_value_set_default:
574 * @pspec: a valid #GParamSpec
575 * @value: a #GValue of correct type for @pspec
577 * Sets @value to its default value as specified in @pspec.
580 g_param_value_set_default (GParamSpec *pspec,
583 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
584 g_return_if_fail (G_IS_VALUE (value));
585 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
587 g_value_reset (value);
588 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
592 * g_param_value_defaults:
593 * @pspec: a valid #GParamSpec
594 * @value: a #GValue of correct type for @pspec
596 * Checks whether @value contains the default value as specified in @pspec.
598 * Returns: whether @value contains the canonical default for this @pspec
601 g_param_value_defaults (GParamSpec *pspec,
604 GValue dflt_value = { 0, };
607 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
608 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
609 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
611 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
612 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
613 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
614 g_value_unset (&dflt_value);
620 * g_param_value_validate:
621 * @pspec: a valid #GParamSpec
622 * @value: a #GValue of correct type for @pspec
624 * Ensures that the contents of @value comply with the specifications
625 * set out by @pspec. For example, a #GParamSpecInt might require
626 * that integers stored in @value may not be smaller than -42 and not be
627 * greater than +42. If @value contains an integer outside of this range,
628 * it is modified accordingly, so the resulting value will fit into the
631 * Returns: whether modifying @value was necessary to ensure validity
634 g_param_value_validate (GParamSpec *pspec,
637 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
638 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
639 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
641 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
643 GValue oval = *value;
645 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
646 memcmp (&oval.data, &value->data, sizeof (oval.data)))
654 * g_param_value_convert:
655 * @pspec: a valid #GParamSpec
656 * @src_value: souce #GValue
657 * @dest_value: destination #GValue of correct type for @pspec
658 * @strict_validation: %TRUE requires @dest_value to conform to @pspec
659 * without modifications
661 * Transforms @src_value into @dest_value if possible, and then
662 * validates @dest_value, in order for it to conform to @pspec. If
663 * @strict_validation is %TRUE this function will only succeed if the
664 * transformed @dest_value complied to @pspec without modifications.
666 * See also g_value_type_transformable(), g_value_transform() and
667 * g_param_value_validate().
669 * Returns: %TRUE if transformation and validation were successful,
670 * %FALSE otherwise and @dest_value is left untouched.
673 g_param_value_convert (GParamSpec *pspec,
674 const GValue *src_value,
676 gboolean strict_validation)
678 GValue tmp_value = { 0, };
680 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
681 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
682 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
683 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
685 /* better leave dest_value untouched when returning FALSE */
687 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
688 if (g_value_transform (src_value, &tmp_value) &&
689 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
691 g_value_unset (dest_value);
693 /* values are relocatable */
694 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
700 g_value_unset (&tmp_value);
707 * g_param_values_cmp:
708 * @pspec: a valid #GParamSpec
709 * @value1: a #GValue of correct type for @pspec
710 * @value2: a #GValue of correct type for @pspec
712 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
713 * if @value1 is found to be less than, equal to or greater than @value2,
716 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
719 g_param_values_cmp (GParamSpec *pspec,
720 const GValue *value1,
721 const GValue *value2)
725 /* param_values_cmp() effectively does: value1 - value2
726 * so the return values are:
727 * -1) value1 < value2
728 * 0) value1 == value2
731 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
732 g_return_val_if_fail (G_IS_VALUE (value1), 0);
733 g_return_val_if_fail (G_IS_VALUE (value2), 0);
734 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
735 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
737 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
739 return CLAMP (cmp, -1, 1);
743 value_param_init (GValue *value)
745 value->data[0].v_pointer = NULL;
749 value_param_free_value (GValue *value)
751 if (value->data[0].v_pointer)
752 g_param_spec_unref (value->data[0].v_pointer);
756 value_param_copy_value (const GValue *src_value,
759 if (src_value->data[0].v_pointer)
760 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
762 dest_value->data[0].v_pointer = NULL;
766 value_param_transform_value (const GValue *src_value,
769 if (src_value->data[0].v_pointer &&
770 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
771 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
773 dest_value->data[0].v_pointer = NULL;
777 value_param_peek_pointer (const GValue *value)
779 return value->data[0].v_pointer;
783 value_param_collect_value (GValue *value,
784 guint n_collect_values,
785 GTypeCValue *collect_values,
788 if (collect_values[0].v_pointer)
790 GParamSpec *param = collect_values[0].v_pointer;
792 if (param->g_type_instance.g_class == NULL)
793 return g_strconcat ("invalid unclassed param spec pointer for value type `",
794 G_VALUE_TYPE_NAME (value),
797 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
798 return g_strconcat ("invalid param spec type `",
799 G_PARAM_SPEC_TYPE_NAME (param),
800 "' for value type `",
801 G_VALUE_TYPE_NAME (value),
804 value->data[0].v_pointer = g_param_spec_ref (param);
807 value->data[0].v_pointer = NULL;
813 value_param_lcopy_value (const GValue *value,
814 guint n_collect_values,
815 GTypeCValue *collect_values,
818 GParamSpec **param_p = collect_values[0].v_pointer;
821 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
823 if (!value->data[0].v_pointer)
825 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
826 *param_p = value->data[0].v_pointer;
828 *param_p = g_param_spec_ref (value->data[0].v_pointer);
834 /* --- param spec pool --- */
838 * A #GParamSpecPool maintains a collection of #GParamSpec<!-- -->s which can be
839 * quickly accessed by owner and name. The implementation of the #GObject property
840 * system uses such a pool to store the #GParamSpecs of the properties all object
843 struct _GParamSpecPool
846 gboolean type_prefixing;
847 GHashTable *hash_table;
851 param_spec_pool_hash (gconstpointer key_spec)
853 const GParamSpec *key = key_spec;
855 guint h = key->owner_type;
857 for (p = key->name; *p; p++)
858 h = (h << 5) - h + *p;
864 param_spec_pool_equals (gconstpointer key_spec_1,
865 gconstpointer key_spec_2)
867 const GParamSpec *key1 = key_spec_1;
868 const GParamSpec *key2 = key_spec_2;
870 return (key1->owner_type == key2->owner_type &&
871 strcmp (key1->name, key2->name) == 0);
875 * g_param_spec_pool_new:
876 * @type_prefixing: Whether the pool will support type-prefixed property names.
878 * Creates a new #GParamSpecPool.
880 * If @type_prefixing is %TRUE, lookups in the newly created pool will
881 * allow to specify the owner as a colon-separated prefix of the
882 * property name, like "GtkContainer:border-width". This feature is
883 * deprecated, so you should always set @type_prefixing to %FALSE.
885 * Returns: (transfer none): a newly allocated #GParamSpecPool.
888 g_param_spec_pool_new (gboolean type_prefixing)
890 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
891 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
893 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
894 pool->type_prefixing = type_prefixing != FALSE;
895 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
901 * g_param_spec_pool_insert:
902 * @pool: a #GParamSpecPool.
903 * @pspec: the #GParamSpec to insert
904 * @owner_type: a #GType identifying the owner of @pspec
906 * Inserts a #GParamSpec in the pool.
909 g_param_spec_pool_insert (GParamSpecPool *pool,
915 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
917 for (p = pspec->name; *p; p++)
919 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
921 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
925 G_SLOCK (&pool->smutex);
926 pspec->owner_type = owner_type;
927 g_param_spec_ref (pspec);
928 g_hash_table_insert (pool->hash_table, pspec, pspec);
929 G_SUNLOCK (&pool->smutex);
933 g_return_if_fail (pool != NULL);
934 g_return_if_fail (pspec);
935 g_return_if_fail (owner_type > 0);
936 g_return_if_fail (pspec->owner_type == 0);
941 * g_param_spec_pool_remove:
942 * @pool: a #GParamSpecPool
943 * @pspec: the #GParamSpec to remove
945 * Removes a #GParamSpec from the pool.
948 g_param_spec_pool_remove (GParamSpecPool *pool,
953 G_SLOCK (&pool->smutex);
954 if (g_hash_table_remove (pool->hash_table, pspec))
955 g_param_spec_unref (pspec);
957 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
958 G_SUNLOCK (&pool->smutex);
962 g_return_if_fail (pool != NULL);
963 g_return_if_fail (pspec);
967 static inline GParamSpec*
968 param_spec_ht_lookup (GHashTable *hash_table,
969 const gchar *param_name,
971 gboolean walk_ancestors)
973 GParamSpec key, *pspec;
975 key.owner_type = owner_type;
976 key.name = (gchar*) param_name;
980 pspec = g_hash_table_lookup (hash_table, &key);
983 key.owner_type = g_type_parent (key.owner_type);
985 while (key.owner_type);
987 pspec = g_hash_table_lookup (hash_table, &key);
989 if (!pspec && !is_canonical (param_name))
991 /* try canonicalized form */
992 key.name = g_strdup (param_name);
993 key.owner_type = owner_type;
995 canonicalize_key (key.name);
999 pspec = g_hash_table_lookup (hash_table, &key);
1005 key.owner_type = g_type_parent (key.owner_type);
1007 while (key.owner_type);
1009 pspec = g_hash_table_lookup (hash_table, &key);
1017 * g_param_spec_pool_lookup:
1018 * @pool: a #GParamSpecPool
1019 * @param_name: the name to look for
1020 * @owner_type: the owner to look for
1021 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1022 * owned by an ancestor of @owner_type.
1024 * Looks up a #GParamSpec in the pool.
1026 * Returns: (transfer none): The found #GParamSpec, or %NULL if no
1027 * matching #GParamSpec was found.
1030 g_param_spec_pool_lookup (GParamSpecPool *pool,
1031 const gchar *param_name,
1033 gboolean walk_ancestors)
1038 if (!pool || !param_name)
1040 g_return_val_if_fail (pool != NULL, NULL);
1041 g_return_val_if_fail (param_name != NULL, NULL);
1044 G_SLOCK (&pool->smutex);
1046 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1048 /* try quick and away, i.e. without prefix */
1051 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1052 G_SUNLOCK (&pool->smutex);
1057 /* strip type prefix */
1058 if (pool->type_prefixing && delim[1] == ':')
1060 guint l = delim - param_name;
1061 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1064 strncpy (buffer, param_name, delim - param_name);
1066 type = g_type_from_name (buffer);
1069 if (type) /* type==0 isn't a valid type pefix */
1071 /* sanity check, these cases don't make a whole lot of sense */
1072 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1074 G_SUNLOCK (&pool->smutex);
1079 param_name += l + 2;
1080 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1081 G_SUNLOCK (&pool->smutex);
1086 /* malformed param_name */
1088 G_SUNLOCK (&pool->smutex);
1094 pool_list (gpointer key,
1098 GParamSpec *pspec = value;
1099 gpointer *data = user_data;
1100 GType owner_type = (GType) data[1];
1102 if (owner_type == pspec->owner_type)
1103 data[0] = g_list_prepend (data[0], pspec);
1107 * g_param_spec_pool_list_owned:
1108 * @pool: a #GParamSpecPool
1109 * @owner_type: the owner to look for
1111 * Gets an #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1114 * Returns: (transfer container) (element-type GObject.ParamSpec): a
1115 * #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1116 * the pool#GParamSpec<!-- -->s.
1119 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1124 g_return_val_if_fail (pool != NULL, NULL);
1125 g_return_val_if_fail (owner_type > 0, NULL);
1127 G_SLOCK (&pool->smutex);
1129 data[1] = (gpointer) owner_type;
1130 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1131 G_SUNLOCK (&pool->smutex);
1137 pspec_compare_id (gconstpointer a,
1140 const GParamSpec *pspec1 = a, *pspec2 = b;
1142 if (pspec1->param_id < pspec2->param_id)
1145 if (pspec1->param_id > pspec2->param_id)
1148 return strcmp (pspec1->name, pspec2->name);
1151 static inline GSList*
1152 pspec_list_remove_overridden_and_redirected (GSList *plist,
1157 GSList *rlist = NULL;
1161 GSList *tmp = plist->next;
1162 GParamSpec *pspec = plist->data;
1164 gboolean remove = FALSE;
1166 /* Remove paramspecs that are redirected, and also paramspecs
1167 * that have are overridden by non-redirected properties.
1168 * The idea is to get the single paramspec for each name that
1169 * best corresponds to what the application sees.
1171 if (g_param_spec_get_redirect_target (pspec))
1175 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1178 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1179 if (redirect != pspec)
1186 g_slist_free_1 (plist);
1190 plist->next = rlist;
1200 pool_depth_list (gpointer key,
1204 GParamSpec *pspec = value;
1205 gpointer *data = user_data;
1206 GSList **slists = data[0];
1207 GType owner_type = (GType) data[1];
1209 if (g_type_is_a (owner_type, pspec->owner_type))
1211 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1213 slists[0] = g_slist_prepend (slists[0], pspec);
1217 guint d = g_type_depth (pspec->owner_type);
1219 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1224 /* We handle interfaces specially since we don't want to
1225 * count interface prerequisites like normal inheritance;
1226 * the property comes from the direct inheritance from
1227 * the prerequisite class, not from the interface that
1230 * also 'depth' isn't a meaningful concept for interface
1234 pool_depth_list_for_interface (gpointer key,
1238 GParamSpec *pspec = value;
1239 gpointer *data = user_data;
1240 GSList **slists = data[0];
1241 GType owner_type = (GType) data[1];
1243 if (pspec->owner_type == owner_type)
1244 slists[0] = g_slist_prepend (slists[0], pspec);
1248 * g_param_spec_pool_list:
1249 * @pool: a #GParamSpecPool
1250 * @owner_type: the owner to look for
1251 * @n_pspecs_p: (out): return location for the length of the returned array
1253 * Gets an array of all #GParamSpec<!-- -->s owned by @owner_type in
1256 * Returns: (array length=n_pspecs_p) (transfer container): a newly
1257 * allocated array containing pointers to all #GParamSpecs
1258 * owned by @owner_type in the pool
1261 g_param_spec_pool_list (GParamSpecPool *pool,
1265 GParamSpec **pspecs, **p;
1266 GSList **slists, *node;
1270 g_return_val_if_fail (pool != NULL, NULL);
1271 g_return_val_if_fail (owner_type > 0, NULL);
1272 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1274 G_SLOCK (&pool->smutex);
1276 d = g_type_depth (owner_type);
1277 slists = g_new0 (GSList*, d);
1279 data[1] = (gpointer) owner_type;
1281 g_hash_table_foreach (pool->hash_table,
1282 G_TYPE_IS_INTERFACE (owner_type) ?
1283 pool_depth_list_for_interface :
1287 for (i = 0; i < d; i++)
1288 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1289 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1291 for (i = 0; i < d; i++)
1293 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1294 for (node = slists[i]; node; node = node->next)
1296 g_slist_free (slists[i]);
1300 G_SUNLOCK (&pool->smutex);
1306 /* --- auxillary functions --- */
1311 void (*finalize) (GParamSpec *pspec);
1312 void (*value_set_default) (GParamSpec *pspec,
1314 gboolean (*value_validate) (GParamSpec *pspec,
1316 gint (*values_cmp) (GParamSpec *pspec,
1317 const GValue *value1,
1318 const GValue *value2);
1319 } ParamSpecClassInfo;
1322 param_spec_generic_class_init (gpointer g_class,
1323 gpointer class_data)
1325 GParamSpecClass *class = g_class;
1326 ParamSpecClassInfo *info = class_data;
1328 class->value_type = info->value_type;
1330 class->finalize = info->finalize; /* optional */
1331 class->value_set_default = info->value_set_default;
1332 if (info->value_validate)
1333 class->value_validate = info->value_validate; /* optional */
1334 class->values_cmp = info->values_cmp;
1335 g_free (class_data);
1339 default_value_set_default (GParamSpec *pspec,
1342 /* value is already zero initialized */
1346 default_values_cmp (GParamSpec *pspec,
1347 const GValue *value1,
1348 const GValue *value2)
1350 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1354 * g_param_type_register_static:
1355 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1356 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1358 * Registers @name as the name of a new static type derived from
1359 * #G_TYPE_PARAM. The type system uses the information contained in
1360 * the #GParamSpecTypeInfo structure pointed to by @info to manage the
1361 * #GParamSpec type and its instances.
1363 * Returns: The new type identifier.
1366 g_param_type_register_static (const gchar *name,
1367 const GParamSpecTypeInfo *pspec_info)
1370 sizeof (GParamSpecClass), /* class_size */
1371 NULL, /* base_init */
1372 NULL, /* base_destroy */
1373 param_spec_generic_class_init, /* class_init */
1374 NULL, /* class_destroy */
1375 NULL, /* class_data */
1376 0, /* instance_size */
1377 16, /* n_preallocs */
1378 NULL, /* instance_init */
1380 ParamSpecClassInfo *cinfo;
1382 g_return_val_if_fail (name != NULL, 0);
1383 g_return_val_if_fail (pspec_info != NULL, 0);
1384 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1385 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1386 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1387 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1388 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1389 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1391 info.instance_size = pspec_info->instance_size;
1392 info.n_preallocs = pspec_info->n_preallocs;
1393 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1394 cinfo = g_new (ParamSpecClassInfo, 1);
1395 cinfo->value_type = pspec_info->value_type;
1396 cinfo->finalize = pspec_info->finalize;
1397 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1398 cinfo->value_validate = pspec_info->value_validate;
1399 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1400 info.class_data = cinfo;
1402 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1406 * g_value_set_param:
1407 * @value: a valid #GValue of type %G_TYPE_PARAM
1408 * @param: the #GParamSpec to be set
1410 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1413 g_value_set_param (GValue *value,
1416 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1418 g_return_if_fail (G_IS_PARAM_SPEC (param));
1420 if (value->data[0].v_pointer)
1421 g_param_spec_unref (value->data[0].v_pointer);
1422 value->data[0].v_pointer = param;
1423 if (value->data[0].v_pointer)
1424 g_param_spec_ref (value->data[0].v_pointer);
1428 * g_value_set_param_take_ownership: (skip)
1429 * @value: a valid #GValue of type %G_TYPE_PARAM
1430 * @param: the #GParamSpec to be set
1432 * This is an internal function introduced mainly for C marshallers.
1434 * Deprecated: 2.4: Use g_value_take_param() instead.
1437 g_value_set_param_take_ownership (GValue *value,
1440 g_value_take_param (value, param);
1444 * g_value_take_param: (skip)
1445 * @value: a valid #GValue of type %G_TYPE_PARAM
1446 * @param: the #GParamSpec to be set
1448 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1449 * over the ownership of the callers reference to @param; the caller
1450 * doesn't have to unref it any more.
1455 g_value_take_param (GValue *value,
1458 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1460 g_return_if_fail (G_IS_PARAM_SPEC (param));
1462 if (value->data[0].v_pointer)
1463 g_param_spec_unref (value->data[0].v_pointer);
1464 value->data[0].v_pointer = param; /* we take over the reference count */
1468 * g_value_get_param:
1469 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1471 * Get the contents of a %G_TYPE_PARAM #GValue.
1473 * Returns: (transfer none): #GParamSpec content of @value
1476 g_value_get_param (const GValue *value)
1478 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1480 return value->data[0].v_pointer;
1484 * g_value_dup_param: (skip)
1485 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1487 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1490 * Returns: #GParamSpec content of @value, should be unreferenced when
1494 g_value_dup_param (const GValue *value)
1496 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1498 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;