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"
31 #include "gtype-private.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)))
59 /* --- prototypes --- */
60 static void g_param_spec_class_base_init (GParamSpecClass *class);
61 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
62 static void g_param_spec_class_init (GParamSpecClass *class,
64 static void g_param_spec_init (GParamSpec *pspec,
65 GParamSpecClass *class);
66 static void g_param_spec_finalize (GParamSpec *pspec);
67 static void value_param_init (GValue *value);
68 static void value_param_free_value (GValue *value);
69 static void value_param_copy_value (const GValue *src_value,
71 static void value_param_transform_value (const GValue *src_value,
73 static gpointer value_param_peek_pointer (const GValue *value);
74 static gchar* value_param_collect_value (GValue *value,
75 guint n_collect_values,
76 GTypeCValue *collect_values,
78 static gchar* value_param_lcopy_value (const GValue *value,
79 guint n_collect_values,
80 GTypeCValue *collect_values,
84 /* --- functions --- */
86 _g_param_type_init (void)
88 static const GTypeFundamentalInfo finfo = {
89 (G_TYPE_FLAG_CLASSED |
90 G_TYPE_FLAG_INSTANTIATABLE |
91 G_TYPE_FLAG_DERIVABLE |
92 G_TYPE_FLAG_DEEP_DERIVABLE),
94 static const GTypeValueTable param_value_table = {
95 value_param_init, /* value_init */
96 value_param_free_value, /* value_free */
97 value_param_copy_value, /* value_copy */
98 value_param_peek_pointer, /* value_peek_pointer */
99 "p", /* collect_format */
100 value_param_collect_value, /* collect_value */
101 "p", /* lcopy_format */
102 value_param_lcopy_value, /* lcopy_value */
104 const GTypeInfo param_spec_info = {
105 sizeof (GParamSpecClass),
107 (GBaseInitFunc) g_param_spec_class_base_init,
108 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
109 (GClassInitFunc) g_param_spec_class_init,
110 (GClassFinalizeFunc) NULL,
111 NULL, /* class_data */
115 (GInstanceInitFunc) g_param_spec_init,
121 /* This should be registred as GParamSpec instead of GParam, for
122 * consistency sake, so that type name can be mapped to struct name,
123 * However, some language bindings, most noticeable the python ones
124 * depends on the "GParam" identifier, see #548689
126 type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
127 g_assert (type == G_TYPE_PARAM);
128 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
132 g_param_spec_class_base_init (GParamSpecClass *class)
137 g_param_spec_class_base_finalize (GParamSpecClass *class)
142 g_param_spec_class_init (GParamSpecClass *class,
145 class->value_type = G_TYPE_NONE;
146 class->finalize = g_param_spec_finalize;
147 class->value_set_default = NULL;
148 class->value_validate = NULL;
149 class->values_cmp = NULL;
153 g_param_spec_init (GParamSpec *pspec,
154 GParamSpecClass *class)
158 pspec->_blurb = NULL;
160 pspec->value_type = class->value_type;
161 pspec->owner_type = 0;
163 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
164 pspec->ref_count = 1;
169 g_param_spec_finalize (GParamSpec *pspec)
171 g_datalist_clear (&pspec->qdata);
173 if (!(pspec->flags & G_PARAM_STATIC_NICK))
174 g_free (pspec->_nick);
176 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
177 g_free (pspec->_blurb);
179 g_type_free_instance ((GTypeInstance*) pspec);
183 * g_param_spec_ref: (skip)
184 * @pspec: a valid #GParamSpec
186 * Increments the reference count of @pspec.
188 * Returns: the #GParamSpec that was passed into this function
191 g_param_spec_ref (GParamSpec *pspec)
193 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
195 g_atomic_int_inc ((int *)&pspec->ref_count);
201 * g_param_spec_unref: (skip)
202 * @pspec: a valid #GParamSpec
204 * Decrements the reference count of a @pspec.
207 g_param_spec_unref (GParamSpec *pspec)
211 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
213 is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
215 if (G_UNLIKELY (is_zero))
217 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
223 * @pspec: a valid #GParamSpec
225 * The initial reference count of a newly created #GParamSpec is 1,
226 * even though no one has explicitly called g_param_spec_ref() on it
227 * yet. So the initial reference count is flagged as "floating", until
228 * someone calls <literal>g_param_spec_ref (pspec); g_param_spec_sink
229 * (pspec);</literal> in sequence on it, taking over the initial
230 * reference count (thus ending up with a @pspec that has a reference
231 * count of 1 still, but is not flagged "floating" anymore).
234 g_param_spec_sink (GParamSpec *pspec)
237 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
239 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
240 if (oldvalue & PARAM_FLOATING_FLAG)
241 g_param_spec_unref (pspec);
245 * g_param_spec_ref_sink: (skip)
246 * @pspec: a valid #GParamSpec
248 * Convenience function to ref and sink a #GParamSpec.
251 * Returns: the #GParamSpec that was passed into this function
254 g_param_spec_ref_sink (GParamSpec *pspec)
257 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
259 oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
260 if (!(oldvalue & PARAM_FLOATING_FLAG))
261 g_param_spec_ref (pspec);
267 * g_param_spec_get_name:
268 * @pspec: a valid #GParamSpec
270 * Get the name of a #GParamSpec.
272 * The name is always an "interned" string (as per g_intern_string()).
273 * This allows for pointer-value comparisons.
275 * Returns: the name of @pspec.
278 g_param_spec_get_name (GParamSpec *pspec)
280 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
286 * g_param_spec_get_nick:
287 * @pspec: a valid #GParamSpec
289 * Get the nickname of a #GParamSpec.
291 * Returns: the nickname of @pspec.
294 g_param_spec_get_nick (GParamSpec *pspec)
296 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
302 GParamSpec *redirect_target;
304 redirect_target = g_param_spec_get_redirect_target (pspec);
305 if (redirect_target && redirect_target->_nick)
306 return redirect_target->_nick;
313 * g_param_spec_get_blurb:
314 * @pspec: a valid #GParamSpec
316 * Get the short description of a #GParamSpec.
318 * Returns: the short description of @pspec.
321 g_param_spec_get_blurb (GParamSpec *pspec)
323 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
326 return pspec->_blurb;
329 GParamSpec *redirect_target;
331 redirect_target = g_param_spec_get_redirect_target (pspec);
332 if (redirect_target && redirect_target->_blurb)
333 return redirect_target->_blurb;
340 canonicalize_key (gchar *key)
344 for (p = key; *p != 0; p++)
349 (c < '0' || c > '9') &&
350 (c < 'A' || c > 'Z') &&
351 (c < 'a' || c > 'z'))
357 is_canonical (const gchar *key)
361 for (p = key; *p != 0; p++)
366 (c < '0' || c > '9') &&
367 (c < 'A' || c > 'Z') &&
368 (c < 'a' || c > 'z'))
376 * g_param_spec_internal: (skip)
377 * @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
378 * @name: the canonical name of the property
379 * @nick: the nickname of the property
380 * @blurb: a short description of the property
381 * @flags: a combination of #GParamFlags
383 * Creates a new #GParamSpec instance.
385 * A property name consists of segments consisting of ASCII letters and
386 * digits, separated by either the '-' or '_' character. The first
387 * character of a property name must be a letter. Names which violate these
388 * rules lead to undefined behaviour.
390 * When creating and looking up a #GParamSpec, either separator can be
391 * used, but they cannot be mixed. Using '-' is considerably more
392 * efficient and in fact required when using property names as detail
393 * strings for signals.
395 * Beyond the name, #GParamSpec<!-- -->s have two more descriptive
396 * strings associated with them, the @nick, which should be suitable
397 * for use as a label for the property in a property editor, and the
398 * @blurb, which should be a somewhat longer description, suitable for
399 * e.g. a tooltip. The @nick and @blurb should ideally be localized.
401 * Returns: a newly allocated #GParamSpec instance
404 g_param_spec_internal (GType param_type,
412 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
413 g_return_val_if_fail (name != NULL, NULL);
414 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
415 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
417 pspec = (gpointer) g_type_create_instance (param_type);
419 if (flags & G_PARAM_STATIC_NAME)
421 /* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
422 pspec->name = (gchar *) g_intern_static_string (name);
423 if (!is_canonical (pspec->name))
424 g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
428 if (is_canonical (name))
429 pspec->name = (gchar *) g_intern_string (name);
432 gchar *tmp = g_strdup (name);
433 canonicalize_key (tmp);
434 pspec->name = (gchar *) g_intern_string (tmp);
439 if (flags & G_PARAM_STATIC_NICK)
440 pspec->_nick = (gchar*) nick;
442 pspec->_nick = g_strdup (nick);
444 if (flags & G_PARAM_STATIC_BLURB)
445 pspec->_blurb = (gchar*) blurb;
447 pspec->_blurb = g_strdup (blurb);
449 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
455 * g_param_spec_get_qdata:
456 * @pspec: a valid #GParamSpec
457 * @quark: a #GQuark, naming the user data pointer
459 * Gets back user data pointers stored via g_param_spec_set_qdata().
461 * Returns: (transfer none): the user data pointer set, or %NULL
464 g_param_spec_get_qdata (GParamSpec *pspec,
467 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
469 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
473 * g_param_spec_set_qdata:
474 * @pspec: the #GParamSpec to set store a user data pointer
475 * @quark: a #GQuark, naming the user data pointer
476 * @data: an opaque user data pointer
478 * Sets an opaque, named pointer on a #GParamSpec. The name is
479 * specified through a #GQuark (retrieved e.g. via
480 * g_quark_from_static_string()), and the pointer can be gotten back
481 * from the @pspec with g_param_spec_get_qdata(). Setting a
482 * previously set user data pointer, overrides (frees) the old pointer
483 * set, using %NULL as pointer essentially removes the data stored.
486 g_param_spec_set_qdata (GParamSpec *pspec,
490 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
491 g_return_if_fail (quark > 0);
493 g_datalist_id_set_data (&pspec->qdata, quark, data);
497 * g_param_spec_set_qdata_full: (skip)
498 * @pspec: the #GParamSpec to set store a user data pointer
499 * @quark: a #GQuark, naming the user data pointer
500 * @data: an opaque user data pointer
501 * @destroy: function to invoke with @data as argument, when @data needs to
504 * This function works like g_param_spec_set_qdata(), but in addition,
505 * a <literal>void (*destroy) (gpointer)</literal> function may be
506 * specified which is called with @data as argument when the @pspec is
507 * finalized, or the data is being overwritten by a call to
508 * g_param_spec_set_qdata() with the same @quark.
511 g_param_spec_set_qdata_full (GParamSpec *pspec,
514 GDestroyNotify destroy)
516 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
517 g_return_if_fail (quark > 0);
519 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
523 * g_param_spec_steal_qdata:
524 * @pspec: the #GParamSpec to get a stored user data pointer from
525 * @quark: a #GQuark, naming the user data pointer
527 * Gets back user data pointers stored via g_param_spec_set_qdata()
528 * and removes the @data from @pspec without invoking its destroy()
529 * function (if any was set). Usually, calling this function is only
530 * required to update user data pointers with a destroy notifier.
532 * Returns: (transfer none): the user data pointer set, or %NULL
535 g_param_spec_steal_qdata (GParamSpec *pspec,
538 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
539 g_return_val_if_fail (quark > 0, NULL);
541 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
545 * g_param_spec_get_redirect_target:
546 * @pspec: a #GParamSpec
548 * If the paramspec redirects operations to another paramspec,
549 * returns that paramspec. Redirect is used typically for
550 * providing a new implementation of a property in a derived
551 * type while preserving all the properties from the parent
552 * type. Redirection is established by creating a property
553 * of type #GParamSpecOverride. See g_object_class_override_property()
554 * for an example of the use of this capability.
558 * Returns: (transfer none): paramspec to which requests on this
559 * paramspec should be redirected, or %NULL if none.
562 g_param_spec_get_redirect_target (GParamSpec *pspec)
564 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
566 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
568 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
570 return ospec->overridden;
577 * g_param_value_set_default:
578 * @pspec: a valid #GParamSpec
579 * @value: a #GValue of correct type for @pspec
581 * Sets @value to its default value as specified in @pspec.
584 g_param_value_set_default (GParamSpec *pspec,
587 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
588 g_return_if_fail (G_IS_VALUE (value));
589 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
591 g_value_reset (value);
592 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
596 * g_param_value_defaults:
597 * @pspec: a valid #GParamSpec
598 * @value: a #GValue of correct type for @pspec
600 * Checks whether @value contains the default value as specified in @pspec.
602 * Returns: whether @value contains the canonical default for this @pspec
605 g_param_value_defaults (GParamSpec *pspec,
608 GValue dflt_value = G_VALUE_INIT;
611 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
612 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
613 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
615 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
616 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
617 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
618 g_value_unset (&dflt_value);
624 * g_param_value_validate:
625 * @pspec: a valid #GParamSpec
626 * @value: a #GValue of correct type for @pspec
628 * Ensures that the contents of @value comply with the specifications
629 * set out by @pspec. For example, a #GParamSpecInt might require
630 * that integers stored in @value may not be smaller than -42 and not be
631 * greater than +42. If @value contains an integer outside of this range,
632 * it is modified accordingly, so the resulting value will fit into the
635 * Returns: whether modifying @value was necessary to ensure validity
638 g_param_value_validate (GParamSpec *pspec,
641 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
642 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
643 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
645 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
647 GValue oval = *value;
649 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
650 memcmp (&oval.data, &value->data, sizeof (oval.data)))
658 * g_param_value_convert:
659 * @pspec: a valid #GParamSpec
660 * @src_value: souce #GValue
661 * @dest_value: destination #GValue of correct type for @pspec
662 * @strict_validation: %TRUE requires @dest_value to conform to @pspec
663 * without modifications
665 * Transforms @src_value into @dest_value if possible, and then
666 * validates @dest_value, in order for it to conform to @pspec. If
667 * @strict_validation is %TRUE this function will only succeed if the
668 * transformed @dest_value complied to @pspec without modifications.
670 * See also g_value_type_transformable(), g_value_transform() and
671 * g_param_value_validate().
673 * Returns: %TRUE if transformation and validation were successful,
674 * %FALSE otherwise and @dest_value is left untouched.
677 g_param_value_convert (GParamSpec *pspec,
678 const GValue *src_value,
680 gboolean strict_validation)
682 GValue tmp_value = G_VALUE_INIT;
684 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
685 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
686 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
687 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
689 /* better leave dest_value untouched when returning FALSE */
691 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
692 if (g_value_transform (src_value, &tmp_value) &&
693 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
695 g_value_unset (dest_value);
697 /* values are relocatable */
698 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
704 g_value_unset (&tmp_value);
711 * g_param_values_cmp:
712 * @pspec: a valid #GParamSpec
713 * @value1: a #GValue of correct type for @pspec
714 * @value2: a #GValue of correct type for @pspec
716 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
717 * if @value1 is found to be less than, equal to or greater than @value2,
720 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
723 g_param_values_cmp (GParamSpec *pspec,
724 const GValue *value1,
725 const GValue *value2)
729 /* param_values_cmp() effectively does: value1 - value2
730 * so the return values are:
731 * -1) value1 < value2
732 * 0) value1 == value2
735 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
736 g_return_val_if_fail (G_IS_VALUE (value1), 0);
737 g_return_val_if_fail (G_IS_VALUE (value2), 0);
738 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
739 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
741 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
743 return CLAMP (cmp, -1, 1);
747 value_param_init (GValue *value)
749 value->data[0].v_pointer = NULL;
753 value_param_free_value (GValue *value)
755 if (value->data[0].v_pointer)
756 g_param_spec_unref (value->data[0].v_pointer);
760 value_param_copy_value (const GValue *src_value,
763 if (src_value->data[0].v_pointer)
764 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
766 dest_value->data[0].v_pointer = NULL;
770 value_param_transform_value (const GValue *src_value,
773 if (src_value->data[0].v_pointer &&
774 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
775 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
777 dest_value->data[0].v_pointer = NULL;
781 value_param_peek_pointer (const GValue *value)
783 return value->data[0].v_pointer;
787 value_param_collect_value (GValue *value,
788 guint n_collect_values,
789 GTypeCValue *collect_values,
792 if (collect_values[0].v_pointer)
794 GParamSpec *param = collect_values[0].v_pointer;
796 if (param->g_type_instance.g_class == NULL)
797 return g_strconcat ("invalid unclassed param spec pointer for value type `",
798 G_VALUE_TYPE_NAME (value),
801 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
802 return g_strconcat ("invalid param spec type `",
803 G_PARAM_SPEC_TYPE_NAME (param),
804 "' for value type `",
805 G_VALUE_TYPE_NAME (value),
808 value->data[0].v_pointer = g_param_spec_ref (param);
811 value->data[0].v_pointer = NULL;
817 value_param_lcopy_value (const GValue *value,
818 guint n_collect_values,
819 GTypeCValue *collect_values,
822 GParamSpec **param_p = collect_values[0].v_pointer;
825 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
827 if (!value->data[0].v_pointer)
829 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
830 *param_p = value->data[0].v_pointer;
832 *param_p = g_param_spec_ref (value->data[0].v_pointer);
838 /* --- param spec pool --- */
842 * A #GParamSpecPool maintains a collection of #GParamSpec<!-- -->s which can be
843 * quickly accessed by owner and name. The implementation of the #GObject property
844 * system uses such a pool to store the #GParamSpecs of the properties all object
847 struct _GParamSpecPool
850 gboolean type_prefixing;
851 GHashTable *hash_table;
855 param_spec_pool_hash (gconstpointer key_spec)
857 const GParamSpec *key = key_spec;
859 guint h = key->owner_type;
861 for (p = key->name; *p; p++)
862 h = (h << 5) - h + *p;
868 param_spec_pool_equals (gconstpointer key_spec_1,
869 gconstpointer key_spec_2)
871 const GParamSpec *key1 = key_spec_1;
872 const GParamSpec *key2 = key_spec_2;
874 return (key1->owner_type == key2->owner_type &&
875 strcmp (key1->name, key2->name) == 0);
879 * g_param_spec_pool_new:
880 * @type_prefixing: Whether the pool will support type-prefixed property names.
882 * Creates a new #GParamSpecPool.
884 * If @type_prefixing is %TRUE, lookups in the newly created pool will
885 * allow to specify the owner as a colon-separated prefix of the
886 * property name, like "GtkContainer:border-width". This feature is
887 * deprecated, so you should always set @type_prefixing to %FALSE.
889 * Returns: (transfer none): a newly allocated #GParamSpecPool.
892 g_param_spec_pool_new (gboolean type_prefixing)
894 static GMutex init_mutex;
895 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
897 memcpy (&pool->mutex, &init_mutex, sizeof (init_mutex));
898 pool->type_prefixing = type_prefixing != FALSE;
899 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
905 * g_param_spec_pool_insert:
906 * @pool: a #GParamSpecPool.
907 * @pspec: the #GParamSpec to insert
908 * @owner_type: a #GType identifying the owner of @pspec
910 * Inserts a #GParamSpec in the pool.
913 g_param_spec_pool_insert (GParamSpecPool *pool,
919 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
921 for (p = pspec->name; *p; p++)
923 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
925 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
929 g_mutex_lock (&pool->mutex);
930 pspec->owner_type = owner_type;
931 g_param_spec_ref (pspec);
932 g_hash_table_insert (pool->hash_table, pspec, pspec);
933 g_mutex_unlock (&pool->mutex);
937 g_return_if_fail (pool != NULL);
938 g_return_if_fail (pspec);
939 g_return_if_fail (owner_type > 0);
940 g_return_if_fail (pspec->owner_type == 0);
945 * g_param_spec_pool_remove:
946 * @pool: a #GParamSpecPool
947 * @pspec: the #GParamSpec to remove
949 * Removes a #GParamSpec from the pool.
952 g_param_spec_pool_remove (GParamSpecPool *pool,
957 g_mutex_lock (&pool->mutex);
958 if (g_hash_table_remove (pool->hash_table, pspec))
959 g_param_spec_unref (pspec);
961 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
962 g_mutex_unlock (&pool->mutex);
966 g_return_if_fail (pool != NULL);
967 g_return_if_fail (pspec);
971 static inline GParamSpec*
972 param_spec_ht_lookup (GHashTable *hash_table,
973 const gchar *param_name,
975 gboolean walk_ancestors)
977 GParamSpec key, *pspec;
979 key.owner_type = owner_type;
980 key.name = (gchar*) param_name;
984 pspec = g_hash_table_lookup (hash_table, &key);
987 key.owner_type = g_type_parent (key.owner_type);
989 while (key.owner_type);
991 pspec = g_hash_table_lookup (hash_table, &key);
993 if (!pspec && !is_canonical (param_name))
997 canonical = g_strdup (key.name);
998 canonicalize_key (canonical);
1000 /* try canonicalized form */
1001 key.name = canonical;
1002 key.owner_type = owner_type;
1007 pspec = g_hash_table_lookup (hash_table, &key);
1013 key.owner_type = g_type_parent (key.owner_type);
1015 while (key.owner_type);
1017 pspec = g_hash_table_lookup (hash_table, &key);
1026 * g_param_spec_pool_lookup:
1027 * @pool: a #GParamSpecPool
1028 * @param_name: the name to look for
1029 * @owner_type: the owner to look for
1030 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1031 * owned by an ancestor of @owner_type.
1033 * Looks up a #GParamSpec in the pool.
1035 * Returns: (transfer none): The found #GParamSpec, or %NULL if no
1036 * matching #GParamSpec was found.
1039 g_param_spec_pool_lookup (GParamSpecPool *pool,
1040 const gchar *param_name,
1042 gboolean walk_ancestors)
1047 if (!pool || !param_name)
1049 g_return_val_if_fail (pool != NULL, NULL);
1050 g_return_val_if_fail (param_name != NULL, NULL);
1053 g_mutex_lock (&pool->mutex);
1055 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1057 /* try quick and away, i.e. without prefix */
1060 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1061 g_mutex_unlock (&pool->mutex);
1066 /* strip type prefix */
1067 if (pool->type_prefixing && delim[1] == ':')
1069 guint l = delim - param_name;
1070 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1073 strncpy (buffer, param_name, delim - param_name);
1075 type = g_type_from_name (buffer);
1078 if (type) /* type==0 isn't a valid type pefix */
1080 /* sanity check, these cases don't make a whole lot of sense */
1081 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1083 g_mutex_unlock (&pool->mutex);
1088 param_name += l + 2;
1089 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1090 g_mutex_unlock (&pool->mutex);
1095 /* malformed param_name */
1097 g_mutex_unlock (&pool->mutex);
1103 pool_list (gpointer key,
1107 GParamSpec *pspec = value;
1108 gpointer *data = user_data;
1109 GType owner_type = (GType) data[1];
1111 if (owner_type == pspec->owner_type)
1112 data[0] = g_list_prepend (data[0], pspec);
1116 * g_param_spec_pool_list_owned:
1117 * @pool: a #GParamSpecPool
1118 * @owner_type: the owner to look for
1120 * Gets an #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1123 * Returns: (transfer container) (element-type GObject.ParamSpec): a
1124 * #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1125 * the pool#GParamSpec<!-- -->s.
1128 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1133 g_return_val_if_fail (pool != NULL, NULL);
1134 g_return_val_if_fail (owner_type > 0, NULL);
1136 g_mutex_lock (&pool->mutex);
1138 data[1] = (gpointer) owner_type;
1139 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1140 g_mutex_unlock (&pool->mutex);
1146 pspec_compare_id (gconstpointer a,
1149 const GParamSpec *pspec1 = a, *pspec2 = b;
1151 if (pspec1->param_id < pspec2->param_id)
1154 if (pspec1->param_id > pspec2->param_id)
1157 return strcmp (pspec1->name, pspec2->name);
1160 static inline GSList*
1161 pspec_list_remove_overridden_and_redirected (GSList *plist,
1166 GSList *rlist = NULL;
1170 GSList *tmp = plist->next;
1171 GParamSpec *pspec = plist->data;
1173 gboolean remove = FALSE;
1175 /* Remove paramspecs that are redirected, and also paramspecs
1176 * that have are overridden by non-redirected properties.
1177 * The idea is to get the single paramspec for each name that
1178 * best corresponds to what the application sees.
1180 if (g_param_spec_get_redirect_target (pspec))
1184 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1187 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1188 if (redirect != pspec)
1195 g_slist_free_1 (plist);
1199 plist->next = rlist;
1209 pool_depth_list (gpointer key,
1213 GParamSpec *pspec = value;
1214 gpointer *data = user_data;
1215 GSList **slists = data[0];
1216 GType owner_type = (GType) data[1];
1218 if (g_type_is_a (owner_type, pspec->owner_type))
1220 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1222 slists[0] = g_slist_prepend (slists[0], pspec);
1226 guint d = g_type_depth (pspec->owner_type);
1228 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1233 /* We handle interfaces specially since we don't want to
1234 * count interface prerequisites like normal inheritance;
1235 * the property comes from the direct inheritance from
1236 * the prerequisite class, not from the interface that
1239 * also 'depth' isn't a meaningful concept for interface
1243 pool_depth_list_for_interface (gpointer key,
1247 GParamSpec *pspec = value;
1248 gpointer *data = user_data;
1249 GSList **slists = data[0];
1250 GType owner_type = (GType) data[1];
1252 if (pspec->owner_type == owner_type)
1253 slists[0] = g_slist_prepend (slists[0], pspec);
1257 * g_param_spec_pool_list:
1258 * @pool: a #GParamSpecPool
1259 * @owner_type: the owner to look for
1260 * @n_pspecs_p: (out): return location for the length of the returned array
1262 * Gets an array of all #GParamSpec<!-- -->s owned by @owner_type in
1265 * Returns: (array length=n_pspecs_p) (transfer container): a newly
1266 * allocated array containing pointers to all #GParamSpecs
1267 * owned by @owner_type in the pool
1270 g_param_spec_pool_list (GParamSpecPool *pool,
1274 GParamSpec **pspecs, **p;
1275 GSList **slists, *node;
1279 g_return_val_if_fail (pool != NULL, NULL);
1280 g_return_val_if_fail (owner_type > 0, NULL);
1281 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1283 g_mutex_lock (&pool->mutex);
1285 d = g_type_depth (owner_type);
1286 slists = g_new0 (GSList*, d);
1288 data[1] = (gpointer) owner_type;
1290 g_hash_table_foreach (pool->hash_table,
1291 G_TYPE_IS_INTERFACE (owner_type) ?
1292 pool_depth_list_for_interface :
1296 for (i = 0; i < d; i++)
1297 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1298 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1300 for (i = 0; i < d; i++)
1302 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1303 for (node = slists[i]; node; node = node->next)
1305 g_slist_free (slists[i]);
1309 g_mutex_unlock (&pool->mutex);
1315 /* --- auxiliary functions --- */
1320 void (*finalize) (GParamSpec *pspec);
1321 void (*value_set_default) (GParamSpec *pspec,
1323 gboolean (*value_validate) (GParamSpec *pspec,
1325 gint (*values_cmp) (GParamSpec *pspec,
1326 const GValue *value1,
1327 const GValue *value2);
1328 } ParamSpecClassInfo;
1331 param_spec_generic_class_init (gpointer g_class,
1332 gpointer class_data)
1334 GParamSpecClass *class = g_class;
1335 ParamSpecClassInfo *info = class_data;
1337 class->value_type = info->value_type;
1339 class->finalize = info->finalize; /* optional */
1340 class->value_set_default = info->value_set_default;
1341 if (info->value_validate)
1342 class->value_validate = info->value_validate; /* optional */
1343 class->values_cmp = info->values_cmp;
1344 g_free (class_data);
1348 default_value_set_default (GParamSpec *pspec,
1351 /* value is already zero initialized */
1355 default_values_cmp (GParamSpec *pspec,
1356 const GValue *value1,
1357 const GValue *value2)
1359 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1363 * g_param_type_register_static:
1364 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1365 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1367 * Registers @name as the name of a new static type derived from
1368 * #G_TYPE_PARAM. The type system uses the information contained in
1369 * the #GParamSpecTypeInfo structure pointed to by @info to manage the
1370 * #GParamSpec type and its instances.
1372 * Returns: The new type identifier.
1375 g_param_type_register_static (const gchar *name,
1376 const GParamSpecTypeInfo *pspec_info)
1379 sizeof (GParamSpecClass), /* class_size */
1380 NULL, /* base_init */
1381 NULL, /* base_destroy */
1382 param_spec_generic_class_init, /* class_init */
1383 NULL, /* class_destroy */
1384 NULL, /* class_data */
1385 0, /* instance_size */
1386 16, /* n_preallocs */
1387 NULL, /* instance_init */
1389 ParamSpecClassInfo *cinfo;
1391 g_return_val_if_fail (name != NULL, 0);
1392 g_return_val_if_fail (pspec_info != NULL, 0);
1393 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1394 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1395 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1396 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1397 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1398 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1400 info.instance_size = pspec_info->instance_size;
1401 info.n_preallocs = pspec_info->n_preallocs;
1402 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1403 cinfo = g_new (ParamSpecClassInfo, 1);
1404 cinfo->value_type = pspec_info->value_type;
1405 cinfo->finalize = pspec_info->finalize;
1406 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1407 cinfo->value_validate = pspec_info->value_validate;
1408 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1409 info.class_data = cinfo;
1411 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1415 * g_value_set_param:
1416 * @value: a valid #GValue of type %G_TYPE_PARAM
1417 * @param: (allow-none): the #GParamSpec to be set
1419 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1422 g_value_set_param (GValue *value,
1425 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1427 g_return_if_fail (G_IS_PARAM_SPEC (param));
1429 if (value->data[0].v_pointer)
1430 g_param_spec_unref (value->data[0].v_pointer);
1431 value->data[0].v_pointer = param;
1432 if (value->data[0].v_pointer)
1433 g_param_spec_ref (value->data[0].v_pointer);
1437 * g_value_set_param_take_ownership: (skip)
1438 * @value: a valid #GValue of type %G_TYPE_PARAM
1439 * @param: (allow-none): the #GParamSpec to be set
1441 * This is an internal function introduced mainly for C marshallers.
1443 * Deprecated: 2.4: Use g_value_take_param() instead.
1446 g_value_set_param_take_ownership (GValue *value,
1449 g_value_take_param (value, param);
1453 * g_value_take_param: (skip)
1454 * @value: a valid #GValue of type %G_TYPE_PARAM
1455 * @param: (allow-none): the #GParamSpec to be set
1457 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1458 * over the ownership of the callers reference to @param; the caller
1459 * doesn't have to unref it any more.
1464 g_value_take_param (GValue *value,
1467 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1469 g_return_if_fail (G_IS_PARAM_SPEC (param));
1471 if (value->data[0].v_pointer)
1472 g_param_spec_unref (value->data[0].v_pointer);
1473 value->data[0].v_pointer = param; /* we take over the reference count */
1477 * g_value_get_param:
1478 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1480 * Get the contents of a %G_TYPE_PARAM #GValue.
1482 * Returns: (transfer none): #GParamSpec content of @value
1485 g_value_get_param (const GValue *value)
1487 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1489 return value->data[0].v_pointer;
1493 * g_value_dup_param: (skip)
1494 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1496 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1499 * Returns: #GParamSpec content of @value, should be unreferenced when
1503 g_value_dup_param (const GValue *value)
1505 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1507 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;