Add TAGS files to .gitignore
[glib.git] / gobject / gparam.c
blob6d136f3fcb3fcbc60d6124e60434cf48817a8cb1
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.
21 * MT safe
24 #include "config.h"
26 #include <string.h>
28 #include "gparam.h"
29 #include "gparamspecs.h"
30 #include "gvaluecollector.h"
31 #include "gtype-private.h"
33 /**
34 * SECTION:gparamspec
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()
39 * @title: GParamSpec
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
49 * parameter.
50 * </para>
54 /* --- defines --- */
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,
63 gpointer class_data);
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,
70 GValue *dest_value);
71 static void value_param_transform_value (const GValue *src_value,
72 GValue *dest_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,
77 guint collect_flags);
78 static gchar* value_param_lcopy_value (const GValue *value,
79 guint n_collect_values,
80 GTypeCValue *collect_values,
81 guint collect_flags);
84 /* --- functions --- */
85 void
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 static 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 */
113 sizeof (GParamSpec),
114 0, /* n_preallocs */
115 (GInstanceInitFunc) g_param_spec_init,
117 &param_value_table,
119 GType type;
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"), &param_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);
131 static void
132 g_param_spec_class_base_init (GParamSpecClass *class)
136 static void
137 g_param_spec_class_base_finalize (GParamSpecClass *class)
141 static void
142 g_param_spec_class_init (GParamSpecClass *class,
143 gpointer class_data)
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;
152 static void
153 g_param_spec_init (GParamSpec *pspec,
154 GParamSpecClass *class)
156 pspec->name = NULL;
157 pspec->_nick = NULL;
158 pspec->_blurb = NULL;
159 pspec->flags = 0;
160 pspec->value_type = class->value_type;
161 pspec->owner_type = 0;
162 pspec->qdata = NULL;
163 g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
164 pspec->ref_count = 1;
165 pspec->param_id = 0;
168 static void
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
190 GParamSpec*
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);
197 return pspec;
201 * g_param_spec_unref: (skip)
202 * @pspec: a valid #GParamSpec
204 * Decrements the reference count of a @pspec.
206 void
207 g_param_spec_unref (GParamSpec *pspec)
209 gboolean is_zero;
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);
222 * g_param_spec_sink:
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).
233 void
234 g_param_spec_sink (GParamSpec *pspec)
236 gsize oldvalue;
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.
250 * Since: 2.10
251 * Returns: the #GParamSpec that was passed into this function
253 GParamSpec*
254 g_param_spec_ref_sink (GParamSpec *pspec)
256 gsize oldvalue;
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);
263 return 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.
277 const gchar *
278 g_param_spec_get_name (GParamSpec *pspec)
280 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
282 return pspec->name;
286 * g_param_spec_get_nick:
287 * @pspec: a valid #GParamSpec
289 * Get the nickname of a #GParamSpec.
291 * Returns: the nickname of @pspec.
293 const gchar *
294 g_param_spec_get_nick (GParamSpec *pspec)
296 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
298 if (pspec->_nick)
299 return pspec->_nick;
300 else
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;
309 return pspec->name;
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.
320 const gchar *
321 g_param_spec_get_blurb (GParamSpec *pspec)
323 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
325 if (pspec->_blurb)
326 return pspec->_blurb;
327 else
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;
336 return NULL;
339 static void
340 canonicalize_key (gchar *key)
342 gchar *p;
344 for (p = key; *p != 0; p++)
346 gchar c = *p;
348 if (c != '-' &&
349 (c < '0' || c > '9') &&
350 (c < 'A' || c > 'Z') &&
351 (c < 'a' || c > 'z'))
352 *p = '-';
356 static gboolean
357 is_canonical (const gchar *key)
359 const gchar *p;
361 for (p = key; *p != 0; p++)
363 gchar c = *p;
365 if (c != '-' &&
366 (c < '0' || c > '9') &&
367 (c < 'A' || c > 'Z') &&
368 (c < 'a' || c > 'z'))
369 return FALSE;
372 return TRUE;
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
403 gpointer
404 g_param_spec_internal (GType param_type,
405 const gchar *name,
406 const gchar *nick,
407 const gchar *blurb,
408 GParamFlags flags)
410 GParamSpec *pspec;
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);
426 else
428 if (is_canonical (name))
429 pspec->name = (gchar *) g_intern_string (name);
430 else
432 gchar *tmp = g_strdup (name);
433 canonicalize_key (tmp);
434 pspec->name = (gchar *) g_intern_string (tmp);
435 g_free (tmp);
439 if (flags & G_PARAM_STATIC_NICK)
440 pspec->_nick = (gchar*) nick;
441 else
442 pspec->_nick = g_strdup (nick);
444 if (flags & G_PARAM_STATIC_BLURB)
445 pspec->_blurb = (gchar*) blurb;
446 else
447 pspec->_blurb = g_strdup (blurb);
449 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
451 return pspec;
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
463 gpointer
464 g_param_spec_get_qdata (GParamSpec *pspec,
465 GQuark quark)
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.
485 void
486 g_param_spec_set_qdata (GParamSpec *pspec,
487 GQuark quark,
488 gpointer data)
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
502 * be freed
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.
510 void
511 g_param_spec_set_qdata_full (GParamSpec *pspec,
512 GQuark quark,
513 gpointer data,
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
534 gpointer
535 g_param_spec_steal_qdata (GParamSpec *pspec,
536 GQuark quark)
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.
556 * Since: 2.4
558 * Returns: (transfer none): paramspec to which requests on this
559 * paramspec should be redirected, or %NULL if none.
561 GParamSpec*
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;
572 else
573 return NULL;
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.
583 void
584 g_param_value_set_default (GParamSpec *pspec,
585 GValue *value)
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
604 gboolean
605 g_param_value_defaults (GParamSpec *pspec,
606 GValue *value)
608 GValue dflt_value = G_VALUE_INIT;
609 gboolean defaults;
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);
620 return defaults;
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
633 * range -42 .. +42.
635 * Returns: whether modifying @value was necessary to ensure validity
637 gboolean
638 g_param_value_validate (GParamSpec *pspec,
639 GValue *value)
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)))
651 return TRUE;
654 return FALSE;
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.
676 gboolean
677 g_param_value_convert (GParamSpec *pspec,
678 const GValue *src_value,
679 GValue *dest_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));
700 return TRUE;
702 else
704 g_value_unset (&tmp_value);
706 return FALSE;
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,
718 * respectively.
720 * Returns: -1, 0 or +1, for a less than, equal to or greater than result
722 gint
723 g_param_values_cmp (GParamSpec *pspec,
724 const GValue *value1,
725 const GValue *value2)
727 gint cmp;
729 /* param_values_cmp() effectively does: value1 - value2
730 * so the return values are:
731 * -1) value1 < value2
732 * 0) value1 == value2
733 * 1) 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);
746 static void
747 value_param_init (GValue *value)
749 value->data[0].v_pointer = NULL;
752 static void
753 value_param_free_value (GValue *value)
755 if (value->data[0].v_pointer)
756 g_param_spec_unref (value->data[0].v_pointer);
759 static void
760 value_param_copy_value (const GValue *src_value,
761 GValue *dest_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);
765 else
766 dest_value->data[0].v_pointer = NULL;
769 static void
770 value_param_transform_value (const GValue *src_value,
771 GValue *dest_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);
776 else
777 dest_value->data[0].v_pointer = NULL;
780 static gpointer
781 value_param_peek_pointer (const GValue *value)
783 return value->data[0].v_pointer;
786 static gchar*
787 value_param_collect_value (GValue *value,
788 guint n_collect_values,
789 GTypeCValue *collect_values,
790 guint collect_flags)
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),
799 "'",
800 NULL);
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),
806 "'",
807 NULL);
808 value->data[0].v_pointer = g_param_spec_ref (param);
810 else
811 value->data[0].v_pointer = NULL;
813 return NULL;
816 static gchar*
817 value_param_lcopy_value (const GValue *value,
818 guint n_collect_values,
819 GTypeCValue *collect_values,
820 guint collect_flags)
822 GParamSpec **param_p = collect_values[0].v_pointer;
824 if (!param_p)
825 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
827 if (!value->data[0].v_pointer)
828 *param_p = NULL;
829 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
830 *param_p = value->data[0].v_pointer;
831 else
832 *param_p = g_param_spec_ref (value->data[0].v_pointer);
834 return NULL;
838 /* --- param spec pool --- */
840 * GParamSpecPool:
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
845 * types.
847 struct _GParamSpecPool
849 GMutex mutex;
850 gboolean type_prefixing;
851 GHashTable *hash_table;
854 static guint
855 param_spec_pool_hash (gconstpointer key_spec)
857 const GParamSpec *key = key_spec;
858 const gchar *p;
859 guint h = key->owner_type;
861 for (p = key->name; *p; p++)
862 h = (h << 5) - h + *p;
864 return h;
867 static gboolean
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.
891 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);
901 return pool;
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.
912 void
913 g_param_spec_pool_insert (GParamSpecPool *pool,
914 GParamSpec *pspec,
915 GType owner_type)
917 const gchar *p;
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);
926 return;
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);
935 else
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.
951 void
952 g_param_spec_pool_remove (GParamSpecPool *pool,
953 GParamSpec *pspec)
955 if (pool && pspec)
957 g_mutex_lock (&pool->mutex);
958 if (g_hash_table_remove (pool->hash_table, pspec))
959 g_param_spec_unref (pspec);
960 else
961 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
962 g_mutex_unlock (&pool->mutex);
964 else
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,
974 GType owner_type,
975 gboolean walk_ancestors)
977 GParamSpec key, *pspec;
979 key.owner_type = owner_type;
980 key.name = (gchar*) param_name;
981 if (walk_ancestors)
984 pspec = g_hash_table_lookup (hash_table, &key);
985 if (pspec)
986 return pspec;
987 key.owner_type = g_type_parent (key.owner_type);
989 while (key.owner_type);
990 else
991 pspec = g_hash_table_lookup (hash_table, &key);
993 if (!pspec && !is_canonical (param_name))
995 gchar *canonical;
997 canonical = g_strdup (key.name);
998 canonicalize_key (canonical);
1000 /* try canonicalized form */
1001 key.name = canonical;
1002 key.owner_type = owner_type;
1004 if (walk_ancestors)
1007 pspec = g_hash_table_lookup (hash_table, &key);
1008 if (pspec)
1010 g_free (canonical);
1011 return pspec;
1013 key.owner_type = g_type_parent (key.owner_type);
1015 while (key.owner_type);
1016 else
1017 pspec = g_hash_table_lookup (hash_table, &key);
1019 g_free (canonical);
1022 return pspec;
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.
1038 GParamSpec*
1039 g_param_spec_pool_lookup (GParamSpecPool *pool,
1040 const gchar *param_name,
1041 GType owner_type,
1042 gboolean walk_ancestors)
1044 GParamSpec *pspec;
1045 gchar *delim;
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 */
1058 if (!delim)
1060 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1061 g_mutex_unlock (&pool->mutex);
1063 return pspec;
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);
1071 GType type;
1073 strncpy (buffer, param_name, delim - param_name);
1074 buffer[l] = 0;
1075 type = g_type_from_name (buffer);
1076 if (l >= 32)
1077 g_free (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);
1085 return NULL;
1087 owner_type = type;
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);
1092 return pspec;
1095 /* malformed param_name */
1097 g_mutex_unlock (&pool->mutex);
1099 return NULL;
1102 static void
1103 pool_list (gpointer key,
1104 gpointer value,
1105 gpointer user_data)
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
1121 * the pool.
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.
1127 GList*
1128 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1129 GType owner_type)
1131 gpointer data[2];
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);
1137 data[0] = NULL;
1138 data[1] = (gpointer) owner_type;
1139 g_hash_table_foreach (pool->hash_table, pool_list, &data);
1140 g_mutex_unlock (&pool->mutex);
1142 return data[0];
1145 static gint
1146 pspec_compare_id (gconstpointer a,
1147 gconstpointer b)
1149 const GParamSpec *pspec1 = a, *pspec2 = b;
1151 if (pspec1->param_id < pspec2->param_id)
1152 return -1;
1154 if (pspec1->param_id > pspec2->param_id)
1155 return 1;
1157 return strcmp (pspec1->name, pspec2->name);
1160 static inline GSList*
1161 pspec_list_remove_overridden_and_redirected (GSList *plist,
1162 GHashTable *ht,
1163 GType owner_type,
1164 guint *n_p)
1166 GSList *rlist = NULL;
1168 while (plist)
1170 GSList *tmp = plist->next;
1171 GParamSpec *pspec = plist->data;
1172 GParamSpec *found;
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))
1181 remove = TRUE;
1182 else
1184 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1185 if (found != pspec)
1187 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1188 if (redirect != pspec)
1189 remove = TRUE;
1193 if (remove)
1195 g_slist_free_1 (plist);
1197 else
1199 plist->next = rlist;
1200 rlist = plist;
1201 *n_p += 1;
1203 plist = tmp;
1205 return rlist;
1208 static void
1209 pool_depth_list (gpointer key,
1210 gpointer value,
1211 gpointer user_data)
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);
1224 else
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
1237 * prerequires it.
1239 * also 'depth' isn't a meaningful concept for interface
1240 * prerequites.
1242 static void
1243 pool_depth_list_for_interface (gpointer key,
1244 gpointer value,
1245 gpointer user_data)
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
1263 * the pool.
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
1269 GParamSpec**
1270 g_param_spec_pool_list (GParamSpecPool *pool,
1271 GType owner_type,
1272 guint *n_pspecs_p)
1274 GParamSpec **pspecs, **p;
1275 GSList **slists, *node;
1276 gpointer data[2];
1277 guint d, i;
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);
1284 *n_pspecs_p = 0;
1285 d = g_type_depth (owner_type);
1286 slists = g_new0 (GSList*, d);
1287 data[0] = slists;
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 :
1293 pool_depth_list,
1294 &data);
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);
1299 p = pspecs;
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)
1304 *p++ = node->data;
1305 g_slist_free (slists[i]);
1307 *p++ = NULL;
1308 g_free (slists);
1309 g_mutex_unlock (&pool->mutex);
1311 return pspecs;
1315 /* --- auxiliary functions --- */
1316 typedef struct
1318 /* class portion */
1319 GType value_type;
1320 void (*finalize) (GParamSpec *pspec);
1321 void (*value_set_default) (GParamSpec *pspec,
1322 GValue *value);
1323 gboolean (*value_validate) (GParamSpec *pspec,
1324 GValue *value);
1325 gint (*values_cmp) (GParamSpec *pspec,
1326 const GValue *value1,
1327 const GValue *value2);
1328 } ParamSpecClassInfo;
1330 static void
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;
1338 if (info->finalize)
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);
1347 static void
1348 default_value_set_default (GParamSpec *pspec,
1349 GValue *value)
1351 /* value is already zero initialized */
1354 static gint
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.
1374 GType
1375 g_param_type_register_static (const gchar *name,
1376 const GParamSpecTypeInfo *pspec_info)
1378 GTypeInfo 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.
1421 void
1422 g_value_set_param (GValue *value,
1423 GParamSpec *param)
1425 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1426 if (param)
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.
1445 void
1446 g_value_set_param_take_ownership (GValue *value,
1447 GParamSpec *param)
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.
1461 * Since: 2.4
1463 void
1464 g_value_take_param (GValue *value,
1465 GParamSpec *param)
1467 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1468 if (param)
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
1484 GParamSpec*
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
1497 * reference count.
1499 * Returns: #GParamSpec content of @value, should be unreferenced when
1500 * no longer needed.
1502 GParamSpec*
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;