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.
27 #include "gvaluecollector.h"
33 #define G_PARAM_SPEC_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_PARAM, GParamSpecClass))
34 #define G_PARAM_USER_MASK (~0 << G_PARAM_USER_SHIFT)
35 #define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
36 #define G_SLOCK(mutex) g_static_mutex_lock (mutex)
37 #define G_SUNLOCK(mutex) g_static_mutex_unlock (mutex)
40 /* --- prototypes --- */
41 static void g_param_spec_class_base_init (GParamSpecClass *class);
42 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
43 static void g_param_spec_class_init (GParamSpecClass *class,
45 static void g_param_spec_init (GParamSpec *pspec,
46 GParamSpecClass *class);
47 static void g_param_spec_finalize (GParamSpec *pspec);
48 static void value_param_init (GValue *value);
49 static void value_param_free_value (GValue *value);
50 static void value_param_copy_value (const GValue *src_value,
52 static void value_param_transform_value (const GValue *src_value,
54 static gpointer value_param_peek_pointer (const GValue *value);
55 static gchar* value_param_collect_value (GValue *value,
56 guint n_collect_values,
57 GTypeCValue *collect_values,
59 static gchar* value_param_lcopy_value (const GValue *value,
60 guint n_collect_values,
61 GTypeCValue *collect_values,
65 /* --- variables --- */
66 static GQuark quark_floating = 0;
67 G_LOCK_DEFINE_STATIC (pspec_ref_count);
70 /* --- functions --- */
72 g_param_type_init (void) /* sync with gtype.c */
74 static const GTypeFundamentalInfo finfo = {
75 (G_TYPE_FLAG_CLASSED |
76 G_TYPE_FLAG_INSTANTIATABLE |
77 G_TYPE_FLAG_DERIVABLE |
78 G_TYPE_FLAG_DEEP_DERIVABLE),
80 static const GTypeValueTable param_value_table = {
81 value_param_init, /* value_init */
82 value_param_free_value, /* value_free */
83 value_param_copy_value, /* value_copy */
84 value_param_peek_pointer, /* value_peek_pointer */
85 "p", /* collect_format */
86 value_param_collect_value, /* collect_value */
87 "p", /* lcopy_format */
88 value_param_lcopy_value, /* lcopy_value */
90 static const GTypeInfo param_spec_info = {
91 sizeof (GParamSpecClass),
93 (GBaseInitFunc) g_param_spec_class_base_init,
94 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
95 (GClassInitFunc) g_param_spec_class_init,
96 (GClassFinalizeFunc) NULL,
97 NULL, /* class_data */
101 (GInstanceInitFunc) g_param_spec_init,
107 type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
108 g_assert (type == G_TYPE_PARAM);
109 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
113 g_param_spec_class_base_init (GParamSpecClass *class)
118 g_param_spec_class_base_finalize (GParamSpecClass *class)
123 g_param_spec_class_init (GParamSpecClass *class,
126 quark_floating = g_quark_from_static_string ("GParamSpec-floating");
128 class->value_type = G_TYPE_NONE;
129 class->finalize = g_param_spec_finalize;
130 class->value_set_default = NULL;
131 class->value_validate = NULL;
132 class->values_cmp = NULL;
136 g_param_spec_init (GParamSpec *pspec,
137 GParamSpecClass *class)
143 pspec->value_type = class->value_type;
144 pspec->owner_type = 0;
146 pspec->ref_count = 1;
147 g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
151 g_param_spec_finalize (GParamSpec *pspec)
153 g_datalist_clear (&pspec->qdata);
155 g_free (pspec->name);
156 g_free (pspec->nick);
157 g_free (pspec->blurb);
159 g_type_free_instance ((GTypeInstance*) pspec);
163 g_param_spec_ref (GParamSpec *pspec)
165 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
167 G_LOCK (pspec_ref_count);
168 if (pspec->ref_count > 0)
170 pspec->ref_count += 1;
171 G_UNLOCK (pspec_ref_count);
175 G_UNLOCK (pspec_ref_count);
176 g_return_val_if_fail (pspec->ref_count > 0, NULL);
183 g_param_spec_unref (GParamSpec *pspec)
185 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
187 G_LOCK (pspec_ref_count);
188 if (pspec->ref_count > 0)
190 gboolean need_finalize;
192 /* sync with _sink */
193 pspec->ref_count -= 1;
194 need_finalize = pspec->ref_count == 0;
195 G_UNLOCK (pspec_ref_count);
197 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
201 G_UNLOCK (pspec_ref_count);
202 g_return_if_fail (pspec->ref_count > 0);
207 g_param_spec_sink (GParamSpec *pspec)
209 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
211 G_LOCK (pspec_ref_count);
212 if (pspec->ref_count > 0)
214 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
216 /* sync with _unref */
217 if (pspec->ref_count > 1)
218 pspec->ref_count -= 1;
221 G_UNLOCK (pspec_ref_count);
222 g_param_spec_unref (pspec);
227 G_UNLOCK (pspec_ref_count);
231 G_UNLOCK (pspec_ref_count);
232 g_return_if_fail (pspec->ref_count > 0);
237 g_param_spec_internal (GType param_type,
245 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
246 g_return_val_if_fail (name != NULL, NULL);
248 pspec = (gpointer) g_type_create_instance (param_type);
249 pspec->name = g_strdup (name);
250 g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
251 pspec->nick = g_strdup (nick ? nick : pspec->name);
252 pspec->blurb = g_strdup (blurb);
253 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
259 g_param_spec_get_qdata (GParamSpec *pspec,
262 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
264 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
268 g_param_spec_set_qdata (GParamSpec *pspec,
272 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
273 g_return_if_fail (quark > 0);
275 g_datalist_id_set_data (&pspec->qdata, quark, data);
279 g_param_spec_set_qdata_full (GParamSpec *pspec,
282 GDestroyNotify destroy)
284 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
285 g_return_if_fail (quark > 0);
287 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
291 g_param_spec_steal_qdata (GParamSpec *pspec,
294 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
295 g_return_val_if_fail (quark > 0, NULL);
297 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
301 g_param_value_set_default (GParamSpec *pspec,
304 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
305 g_return_if_fail (G_IS_VALUE (value));
306 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
308 g_value_reset (value);
309 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
313 g_param_value_defaults (GParamSpec *pspec,
316 GValue dflt_value = { 0, };
319 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
320 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
321 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
323 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
324 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
325 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
326 g_value_unset (&dflt_value);
332 g_param_value_validate (GParamSpec *pspec,
335 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
336 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
337 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
339 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
341 GValue oval = *value;
343 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
344 memcmp (&oval.data, &value->data, sizeof (oval.data)))
352 g_param_value_convert (GParamSpec *pspec,
353 const GValue *src_value,
355 gboolean strict_validation)
357 GValue tmp_value = { 0, };
359 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
360 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
361 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
362 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
364 /* better leave dest_value untouched when returning FALSE */
366 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
367 if (g_value_transform (src_value, &tmp_value) &&
368 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
370 g_value_unset (dest_value);
372 /* values are relocatable */
373 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
379 g_value_unset (&tmp_value);
386 g_param_values_cmp (GParamSpec *pspec,
387 const GValue *value1,
388 const GValue *value2)
392 /* param_values_cmp() effectively does: value1 - value2
393 * so the return values are:
394 * -1) value1 < value2
395 * 0) value1 == value2
398 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
399 g_return_val_if_fail (G_IS_VALUE (value1), 0);
400 g_return_val_if_fail (G_IS_VALUE (value2), 0);
401 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
402 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
404 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
406 return CLAMP (cmp, -1, 1);
410 value_param_init (GValue *value)
412 value->data[0].v_pointer = NULL;
416 value_param_free_value (GValue *value)
418 if (value->data[0].v_pointer)
419 g_param_spec_unref (value->data[0].v_pointer);
423 value_param_copy_value (const GValue *src_value,
426 if (src_value->data[0].v_pointer)
427 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
429 dest_value->data[0].v_pointer = NULL;
433 value_param_transform_value (const GValue *src_value,
436 if (src_value->data[0].v_pointer &&
437 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
438 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
440 dest_value->data[0].v_pointer = NULL;
444 value_param_peek_pointer (const GValue *value)
446 return value->data[0].v_pointer;
450 value_param_collect_value (GValue *value,
451 guint n_collect_values,
452 GTypeCValue *collect_values,
455 if (collect_values[0].v_pointer)
457 GParamSpec *param = collect_values[0].v_pointer;
459 if (param->g_type_instance.g_class == NULL)
460 return g_strconcat ("invalid unclassed param spec pointer for value type `",
461 G_VALUE_TYPE_NAME (value),
464 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
465 return g_strconcat ("invalid param spec type `",
466 G_PARAM_SPEC_TYPE_NAME (param),
467 "' for value type `",
468 G_VALUE_TYPE_NAME (value),
471 value->data[0].v_pointer = g_param_spec_ref (param);
474 value->data[0].v_pointer = NULL;
480 value_param_lcopy_value (const GValue *value,
481 guint n_collect_values,
482 GTypeCValue *collect_values,
485 GParamSpec **param_p = collect_values[0].v_pointer;
488 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
490 if (!value->data[0].v_pointer)
492 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
493 *param_p = value->data[0].v_pointer;
495 *param_p = g_param_spec_ref (value->data[0].v_pointer);
501 /* --- param spec pool --- */
502 struct _GParamSpecPool
505 gboolean type_prefixing;
506 GHashTable *hash_table;
510 param_spec_pool_hash (gconstpointer key_spec)
512 const GParamSpec *key = key_spec;
514 guint h = key->owner_type;
516 for (p = key->name; *p; p++)
517 h = (h << 5) - h + *p;
523 param_spec_pool_equals (gconstpointer key_spec_1,
524 gconstpointer key_spec_2)
526 const GParamSpec *key1 = key_spec_1;
527 const GParamSpec *key2 = key_spec_2;
529 return (key1->owner_type == key2->owner_type &&
530 strcmp (key1->name, key2->name) == 0);
534 g_param_spec_pool_new (gboolean type_prefixing)
536 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
537 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
539 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
540 pool->type_prefixing = type_prefixing != FALSE;
541 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
547 g_param_spec_pool_insert (GParamSpecPool *pool,
553 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
555 G_SLOCK (&pool->smutex);
556 for (p = pspec->name; *p; p++)
558 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
560 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
561 G_SUNLOCK (&pool->smutex);
566 pspec->owner_type = owner_type;
567 g_param_spec_ref (pspec);
568 g_hash_table_insert (pool->hash_table, pspec, pspec);
569 G_SUNLOCK (&pool->smutex);
573 g_return_if_fail (pool != NULL);
574 g_return_if_fail (pspec);
575 g_return_if_fail (owner_type > 0);
576 g_return_if_fail (pspec->owner_type == 0);
581 g_param_spec_pool_remove (GParamSpecPool *pool,
586 G_SLOCK (&pool->smutex);
587 if (g_hash_table_remove (pool->hash_table, pspec))
588 g_param_spec_unref (pspec);
590 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
591 G_SUNLOCK (&pool->smutex);
595 g_return_if_fail (pool != NULL);
596 g_return_if_fail (pspec);
600 static inline GParamSpec*
601 param_spec_ht_lookup (GHashTable *hash_table,
602 const gchar *param_name,
604 gboolean walk_ancestors)
606 GParamSpec key, *pspec;
608 key.owner_type = owner_type;
609 key.name = (gchar*) param_name;
613 pspec = g_hash_table_lookup (hash_table, &key);
616 key.owner_type = g_type_parent (key.owner_type);
618 while (key.owner_type);
620 pspec = g_hash_table_lookup (hash_table, &key);
624 /* sigh, try canonicalization */
625 key.name = g_strdup (param_name);
626 key.owner_type = owner_type;
628 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
632 pspec = g_hash_table_lookup (hash_table, &key);
638 key.owner_type = g_type_parent (key.owner_type);
640 while (key.owner_type);
642 pspec = g_hash_table_lookup (hash_table, &key);
650 g_param_spec_pool_lookup (GParamSpecPool *pool,
651 const gchar *param_name,
653 gboolean walk_ancestors)
658 if (!pool || !param_name)
660 g_return_val_if_fail (pool != NULL, NULL);
661 g_return_val_if_fail (param_name != NULL, NULL);
664 G_SLOCK (&pool->smutex);
666 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
668 /* try quick and away, i.e. without prefix */
671 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
672 G_SUNLOCK (&pool->smutex);
677 /* strip type prefix */
678 if (pool->type_prefixing && delim[1] == ':')
680 guint l = delim - param_name;
681 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
684 strncpy (buffer, param_name, delim - param_name);
686 type = g_type_from_name (buffer);
689 if (type) /* type==0 isn't a valid type pefix */
691 /* sanity check, these cases don't make a whole lot of sense */
692 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
694 G_SUNLOCK (&pool->smutex);
700 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
701 G_SUNLOCK (&pool->smutex);
706 /* malformed param_name */
708 G_SUNLOCK (&pool->smutex);
714 /* --- auxillary functions --- */
719 void (*finalize) (GParamSpec *pspec);
720 void (*value_set_default) (GParamSpec *pspec,
722 gboolean (*value_validate) (GParamSpec *pspec,
724 gint (*values_cmp) (GParamSpec *pspec,
725 const GValue *value1,
726 const GValue *value2);
727 } ParamSpecClassInfo;
730 param_spec_generic_class_init (gpointer g_class,
733 GParamSpecClass *class = g_class;
734 ParamSpecClassInfo *info = class_data;
736 class->value_type = info->value_type;
738 class->finalize = info->finalize; /* optional */
739 class->value_set_default = info->value_set_default;
740 if (info->value_validate)
741 class->value_validate = info->value_validate; /* optional */
742 class->values_cmp = info->values_cmp;
747 default_value_set_default (GParamSpec *pspec,
750 /* value is already zero initialized */
754 default_values_cmp (GParamSpec *pspec,
755 const GValue *value1,
756 const GValue *value2)
758 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
762 g_param_type_register_static (const gchar *name,
763 const GParamSpecTypeInfo *pspec_info)
766 sizeof (GParamSpecClass), /* class_size */
767 NULL, /* base_init */
768 NULL, /* base_destroy */
769 param_spec_generic_class_init, /* class_init */
770 NULL, /* class_destroy */
771 NULL, /* class_data */
772 0, /* instance_size */
773 16, /* n_preallocs */
774 NULL, /* instance_init */
776 ParamSpecClassInfo *cinfo;
778 g_return_val_if_fail (name != NULL, 0);
779 g_return_val_if_fail (pspec_info != NULL, 0);
780 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
781 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
782 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
783 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
784 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
785 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
787 info.instance_size = pspec_info->instance_size;
788 info.n_preallocs = pspec_info->n_preallocs;
789 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
790 cinfo = g_new (ParamSpecClassInfo, 1);
791 cinfo->value_type = pspec_info->value_type;
792 cinfo->finalize = pspec_info->finalize;
793 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
794 cinfo->value_validate = pspec_info->value_validate;
795 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
796 info.class_data = cinfo;
798 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
802 g_value_set_param (GValue *value,
805 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
807 g_return_if_fail (G_IS_PARAM_SPEC (param));
809 if (value->data[0].v_pointer)
810 g_param_spec_unref (value->data[0].v_pointer);
811 value->data[0].v_pointer = param;
812 if (value->data[0].v_pointer)
813 g_param_spec_ref (value->data[0].v_pointer);
817 g_value_get_param (const GValue *value)
819 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
821 return value->data[0].v_pointer;
825 g_value_dup_param (const GValue *value)
827 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
829 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;