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);
247 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
249 pspec = (gpointer) g_type_create_instance (param_type);
250 pspec->name = g_strdup (name);
251 g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
252 pspec->nick = g_strdup (nick ? nick : pspec->name);
253 pspec->blurb = g_strdup (blurb);
254 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
260 g_param_spec_get_qdata (GParamSpec *pspec,
263 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
265 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
269 g_param_spec_set_qdata (GParamSpec *pspec,
273 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
274 g_return_if_fail (quark > 0);
276 g_datalist_id_set_data (&pspec->qdata, quark, data);
280 g_param_spec_set_qdata_full (GParamSpec *pspec,
283 GDestroyNotify destroy)
285 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
286 g_return_if_fail (quark > 0);
288 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
292 g_param_spec_steal_qdata (GParamSpec *pspec,
295 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
296 g_return_val_if_fail (quark > 0, NULL);
298 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
302 g_param_value_set_default (GParamSpec *pspec,
305 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
306 g_return_if_fail (G_IS_VALUE (value));
307 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
309 g_value_reset (value);
310 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
314 g_param_value_defaults (GParamSpec *pspec,
317 GValue dflt_value = { 0, };
320 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
321 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
322 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
324 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
325 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
326 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
327 g_value_unset (&dflt_value);
333 g_param_value_validate (GParamSpec *pspec,
336 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
337 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
338 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
340 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
342 GValue oval = *value;
344 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
345 memcmp (&oval.data, &value->data, sizeof (oval.data)))
353 g_param_value_convert (GParamSpec *pspec,
354 const GValue *src_value,
356 gboolean strict_validation)
358 GValue tmp_value = { 0, };
360 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
361 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
362 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
363 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
365 /* better leave dest_value untouched when returning FALSE */
367 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
368 if (g_value_transform (src_value, &tmp_value) &&
369 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
371 g_value_unset (dest_value);
373 /* values are relocatable */
374 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
380 g_value_unset (&tmp_value);
387 g_param_values_cmp (GParamSpec *pspec,
388 const GValue *value1,
389 const GValue *value2)
393 /* param_values_cmp() effectively does: value1 - value2
394 * so the return values are:
395 * -1) value1 < value2
396 * 0) value1 == value2
399 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
400 g_return_val_if_fail (G_IS_VALUE (value1), 0);
401 g_return_val_if_fail (G_IS_VALUE (value2), 0);
402 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
403 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
405 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
407 return CLAMP (cmp, -1, 1);
411 value_param_init (GValue *value)
413 value->data[0].v_pointer = NULL;
417 value_param_free_value (GValue *value)
419 if (value->data[0].v_pointer)
420 g_param_spec_unref (value->data[0].v_pointer);
424 value_param_copy_value (const GValue *src_value,
427 if (src_value->data[0].v_pointer)
428 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
430 dest_value->data[0].v_pointer = NULL;
434 value_param_transform_value (const GValue *src_value,
437 if (src_value->data[0].v_pointer &&
438 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
439 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
441 dest_value->data[0].v_pointer = NULL;
445 value_param_peek_pointer (const GValue *value)
447 return value->data[0].v_pointer;
451 value_param_collect_value (GValue *value,
452 guint n_collect_values,
453 GTypeCValue *collect_values,
456 if (collect_values[0].v_pointer)
458 GParamSpec *param = collect_values[0].v_pointer;
460 if (param->g_type_instance.g_class == NULL)
461 return g_strconcat ("invalid unclassed param spec pointer for value type `",
462 G_VALUE_TYPE_NAME (value),
465 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
466 return g_strconcat ("invalid param spec type `",
467 G_PARAM_SPEC_TYPE_NAME (param),
468 "' for value type `",
469 G_VALUE_TYPE_NAME (value),
472 value->data[0].v_pointer = g_param_spec_ref (param);
475 value->data[0].v_pointer = NULL;
481 value_param_lcopy_value (const GValue *value,
482 guint n_collect_values,
483 GTypeCValue *collect_values,
486 GParamSpec **param_p = collect_values[0].v_pointer;
489 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
491 if (!value->data[0].v_pointer)
493 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
494 *param_p = value->data[0].v_pointer;
496 *param_p = g_param_spec_ref (value->data[0].v_pointer);
502 /* --- param spec pool --- */
503 struct _GParamSpecPool
506 gboolean type_prefixing;
507 GHashTable *hash_table;
511 param_spec_pool_hash (gconstpointer key_spec)
513 const GParamSpec *key = key_spec;
515 guint h = key->owner_type;
517 for (p = key->name; *p; p++)
518 h = (h << 5) - h + *p;
524 param_spec_pool_equals (gconstpointer key_spec_1,
525 gconstpointer key_spec_2)
527 const GParamSpec *key1 = key_spec_1;
528 const GParamSpec *key2 = key_spec_2;
530 return (key1->owner_type == key2->owner_type &&
531 strcmp (key1->name, key2->name) == 0);
535 g_param_spec_pool_new (gboolean type_prefixing)
537 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
538 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
540 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
541 pool->type_prefixing = type_prefixing != FALSE;
542 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
548 g_param_spec_pool_insert (GParamSpecPool *pool,
554 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
556 G_SLOCK (&pool->smutex);
557 for (p = pspec->name; *p; p++)
559 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
561 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
562 G_SUNLOCK (&pool->smutex);
567 pspec->owner_type = owner_type;
568 g_param_spec_ref (pspec);
569 g_hash_table_insert (pool->hash_table, pspec, pspec);
570 G_SUNLOCK (&pool->smutex);
574 g_return_if_fail (pool != NULL);
575 g_return_if_fail (pspec);
576 g_return_if_fail (owner_type > 0);
577 g_return_if_fail (pspec->owner_type == 0);
582 g_param_spec_pool_remove (GParamSpecPool *pool,
587 G_SLOCK (&pool->smutex);
588 if (g_hash_table_remove (pool->hash_table, pspec))
589 g_param_spec_unref (pspec);
591 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
592 G_SUNLOCK (&pool->smutex);
596 g_return_if_fail (pool != NULL);
597 g_return_if_fail (pspec);
601 static inline GParamSpec*
602 param_spec_ht_lookup (GHashTable *hash_table,
603 const gchar *param_name,
605 gboolean walk_ancestors)
607 GParamSpec key, *pspec;
609 key.owner_type = owner_type;
610 key.name = (gchar*) param_name;
614 pspec = g_hash_table_lookup (hash_table, &key);
617 key.owner_type = g_type_parent (key.owner_type);
619 while (key.owner_type);
621 pspec = g_hash_table_lookup (hash_table, &key);
625 /* try canonicalized form */
626 key.name = g_strdup (param_name);
627 key.owner_type = owner_type;
629 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
633 pspec = g_hash_table_lookup (hash_table, &key);
639 key.owner_type = g_type_parent (key.owner_type);
641 while (key.owner_type);
643 pspec = g_hash_table_lookup (hash_table, &key);
651 g_param_spec_pool_lookup (GParamSpecPool *pool,
652 const gchar *param_name,
654 gboolean walk_ancestors)
659 if (!pool || !param_name)
661 g_return_val_if_fail (pool != NULL, NULL);
662 g_return_val_if_fail (param_name != NULL, NULL);
665 G_SLOCK (&pool->smutex);
667 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
669 /* try quick and away, i.e. without prefix */
672 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
673 G_SUNLOCK (&pool->smutex);
678 /* strip type prefix */
679 if (pool->type_prefixing && delim[1] == ':')
681 guint l = delim - param_name;
682 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
685 strncpy (buffer, param_name, delim - param_name);
687 type = g_type_from_name (buffer);
690 if (type) /* type==0 isn't a valid type pefix */
692 /* sanity check, these cases don't make a whole lot of sense */
693 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
695 G_SUNLOCK (&pool->smutex);
701 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
702 G_SUNLOCK (&pool->smutex);
707 /* malformed param_name */
709 G_SUNLOCK (&pool->smutex);
715 /* --- auxillary functions --- */
720 void (*finalize) (GParamSpec *pspec);
721 void (*value_set_default) (GParamSpec *pspec,
723 gboolean (*value_validate) (GParamSpec *pspec,
725 gint (*values_cmp) (GParamSpec *pspec,
726 const GValue *value1,
727 const GValue *value2);
728 } ParamSpecClassInfo;
731 param_spec_generic_class_init (gpointer g_class,
734 GParamSpecClass *class = g_class;
735 ParamSpecClassInfo *info = class_data;
737 class->value_type = info->value_type;
739 class->finalize = info->finalize; /* optional */
740 class->value_set_default = info->value_set_default;
741 if (info->value_validate)
742 class->value_validate = info->value_validate; /* optional */
743 class->values_cmp = info->values_cmp;
748 default_value_set_default (GParamSpec *pspec,
751 /* value is already zero initialized */
755 default_values_cmp (GParamSpec *pspec,
756 const GValue *value1,
757 const GValue *value2)
759 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
763 g_param_type_register_static (const gchar *name,
764 const GParamSpecTypeInfo *pspec_info)
767 sizeof (GParamSpecClass), /* class_size */
768 NULL, /* base_init */
769 NULL, /* base_destroy */
770 param_spec_generic_class_init, /* class_init */
771 NULL, /* class_destroy */
772 NULL, /* class_data */
773 0, /* instance_size */
774 16, /* n_preallocs */
775 NULL, /* instance_init */
777 ParamSpecClassInfo *cinfo;
779 g_return_val_if_fail (name != NULL, 0);
780 g_return_val_if_fail (pspec_info != NULL, 0);
781 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
782 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
783 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
784 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
785 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
786 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
788 info.instance_size = pspec_info->instance_size;
789 info.n_preallocs = pspec_info->n_preallocs;
790 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
791 cinfo = g_new (ParamSpecClassInfo, 1);
792 cinfo->value_type = pspec_info->value_type;
793 cinfo->finalize = pspec_info->finalize;
794 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
795 cinfo->value_validate = pspec_info->value_validate;
796 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
797 info.class_data = cinfo;
799 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
803 g_value_set_param (GValue *value,
806 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
808 g_return_if_fail (G_IS_PARAM_SPEC (param));
810 if (value->data[0].v_pointer)
811 g_param_spec_unref (value->data[0].v_pointer);
812 value->data[0].v_pointer = param;
813 if (value->data[0].v_pointer)
814 g_param_spec_ref (value->data[0].v_pointer);
818 g_value_get_param (const GValue *value)
820 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
822 return value->data[0].v_pointer;
826 g_value_dup_param (const GValue *value)
828 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
830 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;