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 "gobjectalias.h"
36 * @Short_description: Metadata for parameter specifications
37 * @See_also:g_object_class_install_property(), g_object_set(), g_object_get(),
38 * g_object_set_property(), g_object_get_property(), 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 type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
125 g_assert (type == G_TYPE_PARAM);
126 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
130 g_param_spec_class_base_init (GParamSpecClass *class)
135 g_param_spec_class_base_finalize (GParamSpecClass *class)
140 g_param_spec_class_init (GParamSpecClass *class,
143 class->value_type = G_TYPE_NONE;
144 class->finalize = g_param_spec_finalize;
145 class->value_set_default = NULL;
146 class->value_validate = NULL;
147 class->values_cmp = NULL;
151 g_param_spec_init (GParamSpec *pspec,
152 GParamSpecClass *class)
156 pspec->_blurb = NULL;
158 pspec->value_type = class->value_type;
159 pspec->owner_type = 0;
161 g_datalist_init (&pspec->qdata);
162 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
163 pspec->ref_count = 1;
168 g_param_spec_finalize (GParamSpec *pspec)
170 g_datalist_clear (&pspec->qdata);
172 if (!(pspec->flags & G_PARAM_STATIC_NAME))
173 g_free (pspec->name);
175 if (!(pspec->flags & G_PARAM_STATIC_NICK))
176 g_free (pspec->_nick);
178 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
179 g_free (pspec->_blurb);
181 g_type_free_instance ((GTypeInstance*) pspec);
186 * @pspec: a valid #GParamSpec
188 * Increments the reference count of @pspec.
190 * Returns: the #GParamSpec that was passed into this function
193 g_param_spec_ref (GParamSpec *pspec)
195 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
196 g_return_val_if_fail (pspec->ref_count > 0, NULL);
198 g_atomic_int_inc (&pspec->ref_count);
204 * g_param_spec_unref:
205 * @pspec: a valid #GParamSpec
207 * Decrements the reference count of a @pspec.
210 g_param_spec_unref (GParamSpec *pspec)
214 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
215 g_return_if_fail (pspec->ref_count > 0);
217 is_zero = g_atomic_int_dec_and_test (&pspec->ref_count);
219 if (G_UNLIKELY (is_zero))
221 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
227 * @pspec: a valid #GParamSpec
229 * The initial reference count of a newly created #GParamSpec is 1, even
230 * though no one has explicitly called g_param_spec_ref() on it yet. So the
231 * initial reference count is flagged as "floating", until someone calls
232 * <literal>g_param_spec_ref (pspec); g_param_spec_sink (pspec);</literal>
233 * in sequence on it, taking over the initial reference count (thus
234 * ending up with a @pspec that has a reference count of 1 still, but is
235 * not flagged "floating" anymore).
238 g_param_spec_sink (GParamSpec *pspec)
241 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
242 g_return_if_fail (pspec->ref_count > 0);
245 oldvalue = g_atomic_pointer_get (&pspec->qdata);
246 while (!g_atomic_pointer_compare_and_exchange ((void**) &pspec->qdata, oldvalue,
247 (gpointer) ((gsize) oldvalue & ~(gsize) PARAM_FLOATING_FLAG)));
248 if ((gsize) oldvalue & PARAM_FLOATING_FLAG)
249 g_param_spec_unref (pspec);
253 * g_param_spec_ref_sink:
254 * @pspec: a valid #GParamSpec
256 * Convenience function to ref and sink a #GParamSpec.
259 * Returns: the #GParamSpec that was passed into this function
262 g_param_spec_ref_sink (GParamSpec *pspec)
264 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
265 g_return_val_if_fail (pspec->ref_count > 0, NULL);
267 g_param_spec_ref (pspec);
268 g_param_spec_sink (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:
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 used,
394 * but they cannot be mixed. Using '-' is considerably more efficient and in
395 * fact required when using property names as detail strings for signals.
397 * Beyond the name, #GParamSpec<!-- -->s have two more descriptive strings
398 * associated with them, the @nick, which should be suitable for use as
399 * a label for the property in a property editor, and the @blurb, which should
400 * be a somewhat longer description, suitable for e.g. a tooltip. The @nick
401 * and @blurb should ideally be localized.
403 * Returns: a newly allocated #GParamSpec instance
406 g_param_spec_internal (GType param_type,
414 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
415 g_return_val_if_fail (name != NULL, NULL);
416 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
417 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
419 pspec = (gpointer) g_type_create_instance (param_type);
421 if (flags & G_PARAM_STATIC_NAME)
423 pspec->name = g_intern_static_string (name);
424 if (!is_canonical (pspec->name))
425 g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
429 pspec->name = g_strdup (name);
430 canonicalize_key (pspec->name);
431 g_intern_string (pspec->name);
434 if (flags & G_PARAM_STATIC_NICK)
435 pspec->_nick = (gchar*) nick;
437 pspec->_nick = g_strdup (nick);
439 if (flags & G_PARAM_STATIC_BLURB)
440 pspec->_blurb = (gchar*) blurb;
442 pspec->_blurb = g_strdup (blurb);
444 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
450 * g_param_spec_get_qdata:
451 * @pspec: a valid #GParamSpec
452 * @quark: a #GQuark, naming the user data pointer
454 * Gets back user data pointers stored via g_param_spec_set_qdata().
456 * Returns: the user data pointer set, or %NULL
459 g_param_spec_get_qdata (GParamSpec *pspec,
462 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
464 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
468 * g_param_spec_set_qdata:
469 * @pspec: the #GParamSpec to set store a user data pointer
470 * @quark: a #GQuark, naming the user data pointer
471 * @data: an opaque user data pointer
473 * Sets an opaque, named pointer on a #GParamSpec. The name is specified
474 * through a #GQuark (retrieved e.g. via g_quark_from_static_string()), and
475 * the pointer can be gotten back from the @pspec with g_param_spec_get_qdata().
476 * Setting a previously set user data pointer, overrides (frees)
477 * the old pointer set, using %NULL as pointer essentially
478 * removes the data stored.
481 g_param_spec_set_qdata (GParamSpec *pspec,
485 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
486 g_return_if_fail (quark > 0);
488 g_datalist_id_set_data (&pspec->qdata, quark, data);
492 * g_param_spec_set_qdata_full:
493 * @pspec: the #GParamSpec to set store a user data pointer
494 * @quark: a #GQuark, naming the user data pointer
495 * @data: an opaque user data pointer
496 * @destroy: function to invoke with @data as argument, when @data needs to
499 * This function works like g_param_spec_set_qdata(), but in addition,
500 * a <literal>void (*destroy) (gpointer)</literal> function may be
501 * specified which is called with @data as argument when the @pspec is
502 * finalized, or the data is being overwritten by a call to
503 * g_param_spec_set_qdata() with the same @quark.
506 g_param_spec_set_qdata_full (GParamSpec *pspec,
509 GDestroyNotify destroy)
511 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
512 g_return_if_fail (quark > 0);
514 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
518 * g_param_spec_steal_qdata:
519 * @pspec: the #GParamSpec to get a stored user data pointer from
520 * @quark: a #GQuark, naming the user data pointer
522 * Gets back user data pointers stored via g_param_spec_set_qdata() and
523 * removes the @data from @pspec without invoking it's destroy() function
525 * Usually, calling this function is only required to update
526 * user data pointers with a destroy notifier.
528 * Returns: 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.
553 * Returns: paramspec to which requests on this paramspec should
554 * be redirected, or %NULL if none.
557 g_param_spec_get_redirect_target (GParamSpec *pspec)
559 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
561 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
563 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
565 return ospec->overridden;
572 * g_param_value_set_default:
573 * @pspec: a valid #GParamSpec
574 * @value: a #GValue of correct type for @pspec
576 * Sets @value to its default value as specified in @pspec.
579 g_param_value_set_default (GParamSpec *pspec,
582 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
583 g_return_if_fail (G_IS_VALUE (value));
584 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
586 g_value_reset (value);
587 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
591 * g_param_value_defaults:
592 * @pspec: a valid #GParamSpec
593 * @value: a #GValue of correct type for @pspec
595 * Checks whether @value contains the default value as specified in @pspec.
597 * Returns: whether @value contains the canonical default for this @pspec
600 g_param_value_defaults (GParamSpec *pspec,
603 GValue dflt_value = { 0, };
606 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
607 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
608 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
610 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
611 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
612 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
613 g_value_unset (&dflt_value);
619 * g_param_value_validate:
620 * @pspec: a valid #GParamSpec
621 * @value: a #GValue of correct type for @pspec
623 * Ensures that the contents of @value comply with the specifications
624 * set out by @pspec. For example, a #GParamSpecInt might require
625 * that integers stored in @value may not be smaller than -42 and not be
626 * greater than +42. If @value contains an integer outside of this range,
627 * it is modified accordingly, so the resulting value will fit into the
630 * Returns: whether modifying @value was necessary to ensure validity
633 g_param_value_validate (GParamSpec *pspec,
636 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
637 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
638 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
640 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
642 GValue oval = *value;
644 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
645 memcmp (&oval.data, &value->data, sizeof (oval.data)))
653 * g_param_value_convert:
654 * @pspec: a valid #GParamSpec
655 * @src_value: souce #GValue
656 * @dest_value: destination #GValue of correct type for @pspec
657 * @strict_validation: %TRUE requires @dest_value to conform to @pspec without modifications
659 * Transforms @src_value into @dest_value if possible, and then validates
660 * @dest_value, in order for it to conform to @pspec.
661 * If @strict_validation is %TRUE this function will only succeed if
662 * the transformed @dest_value complied to @pspec without modifications.
664 * See also g_value_type_transformable(), g_value_transform() and
665 * g_param_value_validate().
667 * Returns: %TRUE if transformation and validation were successful,
668 * %FALSE otherwise and @dest_value is left untouched.
671 g_param_value_convert (GParamSpec *pspec,
672 const GValue *src_value,
674 gboolean strict_validation)
676 GValue tmp_value = { 0, };
678 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
679 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
680 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
681 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
683 /* better leave dest_value untouched when returning FALSE */
685 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
686 if (g_value_transform (src_value, &tmp_value) &&
687 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
689 g_value_unset (dest_value);
691 /* values are relocatable */
692 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
698 g_value_unset (&tmp_value);
705 * g_param_values_cmp:
706 * @pspec: a valid #GParamSpec
707 * @value1: a #GValue of correct type for @pspec
708 * @value2: a #GValue of correct type for @pspec
710 * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
711 * if @value1 is found to be less than, equal to or greater than @value2,
714 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
717 g_param_values_cmp (GParamSpec *pspec,
718 const GValue *value1,
719 const GValue *value2)
723 /* param_values_cmp() effectively does: value1 - value2
724 * so the return values are:
725 * -1) value1 < value2
726 * 0) value1 == value2
729 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
730 g_return_val_if_fail (G_IS_VALUE (value1), 0);
731 g_return_val_if_fail (G_IS_VALUE (value2), 0);
732 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
733 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
735 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
737 return CLAMP (cmp, -1, 1);
741 value_param_init (GValue *value)
743 value->data[0].v_pointer = NULL;
747 value_param_free_value (GValue *value)
749 if (value->data[0].v_pointer)
750 g_param_spec_unref (value->data[0].v_pointer);
754 value_param_copy_value (const GValue *src_value,
757 if (src_value->data[0].v_pointer)
758 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
760 dest_value->data[0].v_pointer = NULL;
764 value_param_transform_value (const GValue *src_value,
767 if (src_value->data[0].v_pointer &&
768 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
769 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
771 dest_value->data[0].v_pointer = NULL;
775 value_param_peek_pointer (const GValue *value)
777 return value->data[0].v_pointer;
781 value_param_collect_value (GValue *value,
782 guint n_collect_values,
783 GTypeCValue *collect_values,
786 if (collect_values[0].v_pointer)
788 GParamSpec *param = collect_values[0].v_pointer;
790 if (param->g_type_instance.g_class == NULL)
791 return g_strconcat ("invalid unclassed param spec pointer for value type `",
792 G_VALUE_TYPE_NAME (value),
795 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
796 return g_strconcat ("invalid param spec type `",
797 G_PARAM_SPEC_TYPE_NAME (param),
798 "' for value type `",
799 G_VALUE_TYPE_NAME (value),
802 value->data[0].v_pointer = g_param_spec_ref (param);
805 value->data[0].v_pointer = NULL;
811 value_param_lcopy_value (const GValue *value,
812 guint n_collect_values,
813 GTypeCValue *collect_values,
816 GParamSpec **param_p = collect_values[0].v_pointer;
819 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
821 if (!value->data[0].v_pointer)
823 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
824 *param_p = value->data[0].v_pointer;
826 *param_p = g_param_spec_ref (value->data[0].v_pointer);
832 /* --- param spec pool --- */
836 * A #GParamSpecPool maintains a collection of #GParamSpec<!-- -->s which can be
837 * quickly accessed by owner and name. The implementation of the #GObject property
838 * system uses such a pool to store the #GParamSpecs of the properties all object
841 struct _GParamSpecPool
844 gboolean type_prefixing;
845 GHashTable *hash_table;
849 param_spec_pool_hash (gconstpointer key_spec)
851 const GParamSpec *key = key_spec;
853 guint h = key->owner_type;
855 for (p = key->name; *p; p++)
856 h = (h << 5) - h + *p;
862 param_spec_pool_equals (gconstpointer key_spec_1,
863 gconstpointer key_spec_2)
865 const GParamSpec *key1 = key_spec_1;
866 const GParamSpec *key2 = key_spec_2;
868 return (key1->owner_type == key2->owner_type &&
869 strcmp (key1->name, key2->name) == 0);
873 * g_param_spec_pool_new:
874 * @type_prefixing: Whether the pool will support type-prefixed property names.
876 * Creates a new #GParamSpecPool.
878 * If @type_prefixing is %TRUE, lookups in the newly created pool will
879 * allow to specify the owner as a colon-separated prefix of the property name,
880 * like "GtkContainer:border-width". This feature is deprecated, so you should
881 * always set @type_prefixing to %FALSE.
883 * Returns: a newly allocated #GParamSpecPool.
886 g_param_spec_pool_new (gboolean type_prefixing)
888 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
889 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
891 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
892 pool->type_prefixing = type_prefixing != FALSE;
893 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
899 * g_param_spec_pool_insert:
900 * @pool: a #GParamSpecPool.
901 * @pspec: the #GParamSpec to insert
902 * @owner_type: a #GType identifying the owner of @pspec
904 * Inserts a #GParamSpec in the pool.
906 g_param_spec_pool_insert (GParamSpecPool *pool,
912 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
914 G_SLOCK (&pool->smutex);
915 for (p = pspec->name; *p; p++)
917 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
919 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
920 G_SUNLOCK (&pool->smutex);
925 pspec->owner_type = owner_type;
926 g_param_spec_ref (pspec);
927 g_hash_table_insert (pool->hash_table, pspec, pspec);
928 G_SUNLOCK (&pool->smutex);
932 g_return_if_fail (pool != NULL);
933 g_return_if_fail (pspec);
934 g_return_if_fail (owner_type > 0);
935 g_return_if_fail (pspec->owner_type == 0);
940 * g_param_spec_pool_remove:
941 * @pool: a #GParamSpecPool
942 * @pspec: the #GParamSpec to remove
944 * Removes a #GParamSpec from the pool.
947 g_param_spec_pool_remove (GParamSpecPool *pool,
952 G_SLOCK (&pool->smutex);
953 if (g_hash_table_remove (pool->hash_table, pspec))
954 g_param_spec_unref (pspec);
956 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
957 G_SUNLOCK (&pool->smutex);
961 g_return_if_fail (pool != NULL);
962 g_return_if_fail (pspec);
966 static inline GParamSpec*
967 param_spec_ht_lookup (GHashTable *hash_table,
968 const gchar *param_name,
970 gboolean walk_ancestors)
972 GParamSpec key, *pspec;
974 key.owner_type = owner_type;
975 key.name = (gchar*) param_name;
979 pspec = g_hash_table_lookup (hash_table, &key);
982 key.owner_type = g_type_parent (key.owner_type);
984 while (key.owner_type);
986 pspec = g_hash_table_lookup (hash_table, &key);
988 if (!pspec && !is_canonical (param_name))
990 /* try canonicalized form */
991 key.name = g_strdup (param_name);
992 key.owner_type = owner_type;
994 canonicalize_key (key.name);
998 pspec = g_hash_table_lookup (hash_table, &key);
1004 key.owner_type = g_type_parent (key.owner_type);
1006 while (key.owner_type);
1008 pspec = g_hash_table_lookup (hash_table, &key);
1016 * g_param_spec_pool_lookup:
1017 * @pool: a #GParamSpecPool
1018 * @param_name: the name to look for
1019 * @owner_type: the owner to look for
1020 * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1021 * owned by an ancestor of @owner_type.
1023 * Looks up a #GParamSpec in the pool.
1025 * Returns: The found #GParamSpec, or %NULL if no matching #GParamSpec was found.
1028 g_param_spec_pool_lookup (GParamSpecPool *pool,
1029 const gchar *param_name,
1031 gboolean walk_ancestors)
1036 if (!pool || !param_name)
1038 g_return_val_if_fail (pool != NULL, NULL);
1039 g_return_val_if_fail (param_name != NULL, NULL);
1042 G_SLOCK (&pool->smutex);
1044 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1046 /* try quick and away, i.e. without prefix */
1049 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1050 G_SUNLOCK (&pool->smutex);
1055 /* strip type prefix */
1056 if (pool->type_prefixing && delim[1] == ':')
1058 guint l = delim - param_name;
1059 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1062 strncpy (buffer, param_name, delim - param_name);
1064 type = g_type_from_name (buffer);
1067 if (type) /* type==0 isn't a valid type pefix */
1069 /* sanity check, these cases don't make a whole lot of sense */
1070 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1072 G_SUNLOCK (&pool->smutex);
1077 param_name += l + 2;
1078 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1079 G_SUNLOCK (&pool->smutex);
1084 /* malformed param_name */
1086 G_SUNLOCK (&pool->smutex);
1092 pool_list (gpointer key,
1096 GParamSpec *pspec = value;
1097 gpointer *data = user_data;
1098 GType owner_type = (GType) data[1];
1100 if (owner_type == pspec->owner_type)
1101 data[0] = g_list_prepend (data[0], pspec);
1105 * g_param_spec_pool_list_owned:
1106 * @pool: a #GParamSpecPool
1107 * @owner_type: the owner to look for
1109 * Gets an #GList of all #GParamSpec<!-- -->s owned by @owner_type in the pool.
1111 * Returns: a #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1112 * the pool#GParamSpec<!-- -->s.
1115 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1120 g_return_val_if_fail (pool != NULL, NULL);
1121 g_return_val_if_fail (owner_type > 0, NULL);
1123 G_SLOCK (&pool->smutex);
1125 data[1] = (gpointer) owner_type;
1126 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1127 G_SUNLOCK (&pool->smutex);
1133 pspec_compare_id (gconstpointer a,
1136 const GParamSpec *pspec1 = a, *pspec2 = b;
1138 return pspec1->param_id < pspec2->param_id ? -1 : pspec1->param_id > pspec2->param_id;
1141 static inline GSList*
1142 pspec_list_remove_overridden_and_redirected (GSList *plist,
1147 GSList *rlist = NULL;
1151 GSList *tmp = plist->next;
1152 GParamSpec *pspec = plist->data;
1154 gboolean remove = FALSE;
1156 /* Remove paramspecs that are redirected, and also paramspecs
1157 * that have are overridden by non-redirected properties.
1158 * The idea is to get the single paramspec for each name that
1159 * best corresponds to what the application sees.
1161 if (g_param_spec_get_redirect_target (pspec))
1165 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1168 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1169 if (redirect != pspec)
1176 g_slist_free_1 (plist);
1180 plist->next = rlist;
1190 pool_depth_list (gpointer key,
1194 GParamSpec *pspec = value;
1195 gpointer *data = user_data;
1196 GSList **slists = data[0];
1197 GType owner_type = (GType) data[1];
1199 if (g_type_is_a (owner_type, pspec->owner_type))
1201 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1203 slists[0] = g_slist_prepend (slists[0], pspec);
1207 guint d = g_type_depth (pspec->owner_type);
1209 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1214 /* We handle interfaces specially since we don't want to
1215 * count interface prerequisites like normal inheritance;
1216 * the property comes from the direct inheritance from
1217 * the prerequisite class, not from the interface that
1220 * also 'depth' isn't a meaningful concept for interface
1224 pool_depth_list_for_interface (gpointer key,
1228 GParamSpec *pspec = value;
1229 gpointer *data = user_data;
1230 GSList **slists = data[0];
1231 GType owner_type = (GType) data[1];
1233 if (pspec->owner_type == owner_type)
1234 slists[0] = g_slist_prepend (slists[0], pspec);
1238 * g_param_spec_pool_list:
1239 * @pool: a #GParamSpecPool
1240 * @owner_type: the owner to look for
1241 * @n_pspecs_p: return location for the length of the returned array
1243 * Gets an array of all #GParamSpec<!-- -->s owned by @owner_type in the pool.
1245 * Returns: a newly allocated array containing pointers to all
1246 * #GParamSpec<!-- -->s owned by @owner_type in the pool
1249 g_param_spec_pool_list (GParamSpecPool *pool,
1253 GParamSpec **pspecs, **p;
1254 GSList **slists, *node;
1258 g_return_val_if_fail (pool != NULL, NULL);
1259 g_return_val_if_fail (owner_type > 0, NULL);
1260 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1262 G_SLOCK (&pool->smutex);
1264 d = g_type_depth (owner_type);
1265 slists = g_new0 (GSList*, d);
1267 data[1] = (gpointer) owner_type;
1269 g_hash_table_foreach (pool->hash_table,
1270 G_TYPE_IS_INTERFACE (owner_type) ?
1271 pool_depth_list_for_interface :
1275 for (i = 0; i < d; i++)
1276 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1277 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1279 for (i = 0; i < d; i++)
1281 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1282 for (node = slists[i]; node; node = node->next)
1284 g_slist_free (slists[i]);
1288 G_SUNLOCK (&pool->smutex);
1294 /* --- auxillary functions --- */
1299 void (*finalize) (GParamSpec *pspec);
1300 void (*value_set_default) (GParamSpec *pspec,
1302 gboolean (*value_validate) (GParamSpec *pspec,
1304 gint (*values_cmp) (GParamSpec *pspec,
1305 const GValue *value1,
1306 const GValue *value2);
1307 } ParamSpecClassInfo;
1310 param_spec_generic_class_init (gpointer g_class,
1311 gpointer class_data)
1313 GParamSpecClass *class = g_class;
1314 ParamSpecClassInfo *info = class_data;
1316 class->value_type = info->value_type;
1318 class->finalize = info->finalize; /* optional */
1319 class->value_set_default = info->value_set_default;
1320 if (info->value_validate)
1321 class->value_validate = info->value_validate; /* optional */
1322 class->values_cmp = info->values_cmp;
1323 g_free (class_data);
1327 default_value_set_default (GParamSpec *pspec,
1330 /* value is already zero initialized */
1334 default_values_cmp (GParamSpec *pspec,
1335 const GValue *value1,
1336 const GValue *value2)
1338 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1342 * g_param_type_register_static:
1343 * @name: 0-terminated string used as the name of the new #GParamSpec type.
1344 * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1346 * Registers @name as the name of a new static type derived from
1347 * #G_TYPE_PARAM. The type system uses the information contained in the
1348 * #GParamSpecTypeInfo structure pointed to by @info to manage the #GParamSpec
1349 * type and its instances.
1351 * Returns: The new type identifier.
1354 g_param_type_register_static (const gchar *name,
1355 const GParamSpecTypeInfo *pspec_info)
1358 sizeof (GParamSpecClass), /* class_size */
1359 NULL, /* base_init */
1360 NULL, /* base_destroy */
1361 param_spec_generic_class_init, /* class_init */
1362 NULL, /* class_destroy */
1363 NULL, /* class_data */
1364 0, /* instance_size */
1365 16, /* n_preallocs */
1366 NULL, /* instance_init */
1368 ParamSpecClassInfo *cinfo;
1370 g_return_val_if_fail (name != NULL, 0);
1371 g_return_val_if_fail (pspec_info != NULL, 0);
1372 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1373 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1374 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1375 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1376 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1377 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1379 info.instance_size = pspec_info->instance_size;
1380 info.n_preallocs = pspec_info->n_preallocs;
1381 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1382 cinfo = g_new (ParamSpecClassInfo, 1);
1383 cinfo->value_type = pspec_info->value_type;
1384 cinfo->finalize = pspec_info->finalize;
1385 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1386 cinfo->value_validate = pspec_info->value_validate;
1387 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1388 info.class_data = cinfo;
1390 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1394 * g_value_set_param:
1395 * @value: a valid #GValue of type %G_TYPE_PARAM
1396 * @param: the #GParamSpec to be set
1398 * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1401 g_value_set_param (GValue *value,
1404 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1406 g_return_if_fail (G_IS_PARAM_SPEC (param));
1408 if (value->data[0].v_pointer)
1409 g_param_spec_unref (value->data[0].v_pointer);
1410 value->data[0].v_pointer = param;
1411 if (value->data[0].v_pointer)
1412 g_param_spec_ref (value->data[0].v_pointer);
1416 * g_value_set_param_take_ownership:
1417 * @value: a valid #GValue of type %G_TYPE_PARAM
1418 * @param: the #GParamSpec to be set
1420 * This is an internal function introduced mainly for C marshallers.
1422 * Deprecated: 2.4: Use g_value_take_param() instead.
1425 g_value_set_param_take_ownership (GValue *value,
1428 g_value_take_param (value, param);
1432 * g_value_take_param:
1433 * @value: a valid #GValue of type %G_TYPE_PARAM
1434 * @param: the #GParamSpec to be set
1436 * Sets the contents of a %G_TYPE_PARAM #GValue to @param and
1437 * takes over the ownership of the callers reference to @param;
1438 * the caller doesn't have to unref it any more.
1443 g_value_take_param (GValue *value,
1446 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1448 g_return_if_fail (G_IS_PARAM_SPEC (param));
1450 if (value->data[0].v_pointer)
1451 g_param_spec_unref (value->data[0].v_pointer);
1452 value->data[0].v_pointer = param; /* we take over the reference count */
1456 * g_value_get_param:
1457 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1459 * Get the contents of a %G_TYPE_PARAM #GValue.
1461 * Returns: #GParamSpec content of @value
1464 g_value_get_param (const GValue *value)
1466 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1468 return value->data[0].v_pointer;
1472 * g_value_dup_param:
1473 * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1475 * Get the contents of a %G_TYPE_PARAM #GValue, increasing its reference count.
1477 * Returns: #GParamSpec content of @value, should be unreferenced when no longer needed.
1480 g_value_dup_param (const GValue *value)
1482 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1484 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1487 #define __G_PARAM_C__
1488 #include "gobjectaliasdef.c"