1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997, 1998, 1999, 2000 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 ((1 << G_PARAM_USER_SHIFT) - 1)
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 gpointer value_param_peek_pointer (const GValue *value);
53 static gchar* value_param_collect_value (GValue *value,
54 guint n_collect_values,
55 GTypeCValue *collect_values,
57 static gchar* value_param_lcopy_value (const GValue *value,
58 guint n_collect_values,
59 GTypeCValue *collect_values,
63 /* --- variables --- */
64 static GQuark quark_floating = 0;
65 G_LOCK_DEFINE_STATIC (pspec_ref_count);
68 /* --- functions --- */
70 g_param_type_init (void) /* sync with gtype.c */
72 static const GTypeFundamentalInfo finfo = {
73 (G_TYPE_FLAG_CLASSED |
74 G_TYPE_FLAG_INSTANTIATABLE |
75 G_TYPE_FLAG_DERIVABLE |
76 G_TYPE_FLAG_DEEP_DERIVABLE),
78 static const GTypeValueTable param_value_table = {
79 value_param_init, /* value_init */
80 value_param_free_value, /* value_free */
81 value_param_copy_value, /* value_copy */
82 value_param_peek_pointer, /* value_peek_pointer */
83 "p", /* collect_format */
84 value_param_collect_value, /* collect_value */
85 "p", /* lcopy_format */
86 value_param_lcopy_value, /* lcopy_value */
88 static const GTypeInfo param_spec_info = {
89 sizeof (GParamSpecClass),
91 (GBaseInitFunc) g_param_spec_class_base_init,
92 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
93 (GClassInitFunc) g_param_spec_class_init,
94 (GClassFinalizeFunc) NULL,
95 NULL, /* class_data */
99 (GInstanceInitFunc) g_param_spec_init,
105 type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
106 g_assert (type == G_TYPE_PARAM);
110 g_param_spec_class_base_init (GParamSpecClass *class)
115 g_param_spec_class_base_finalize (GParamSpecClass *class)
120 g_param_spec_class_init (GParamSpecClass *class,
123 quark_floating = g_quark_from_static_string ("GParamSpec-floating");
125 class->value_type = G_TYPE_NONE;
126 class->finalize = g_param_spec_finalize;
127 class->value_set_default = NULL;
128 class->value_validate = NULL;
129 class->values_cmp = NULL;
133 g_param_spec_init (GParamSpec *pspec,
134 GParamSpecClass *class)
140 pspec->value_type = class->value_type;
141 pspec->owner_type = 0;
143 pspec->ref_count = 1;
144 g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
148 g_param_spec_finalize (GParamSpec *pspec)
150 g_datalist_clear (&pspec->qdata);
152 g_free (pspec->name);
153 g_free (pspec->nick);
154 g_free (pspec->blurb);
156 g_type_free_instance ((GTypeInstance*) pspec);
160 g_param_spec_ref (GParamSpec *pspec)
162 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
164 G_LOCK (pspec_ref_count);
165 if (pspec->ref_count > 0)
167 pspec->ref_count += 1;
168 G_UNLOCK (pspec_ref_count);
172 G_UNLOCK (pspec_ref_count);
173 g_return_val_if_fail (pspec->ref_count > 0, NULL);
180 g_param_spec_unref (GParamSpec *pspec)
182 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
184 G_LOCK (pspec_ref_count);
185 if (pspec->ref_count > 0)
187 gboolean need_finalize;
189 /* sync with _sink */
190 pspec->ref_count -= 1;
191 need_finalize = pspec->ref_count == 0;
192 G_UNLOCK (pspec_ref_count);
194 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
198 G_UNLOCK (pspec_ref_count);
199 g_return_if_fail (pspec->ref_count > 0);
204 g_param_spec_sink (GParamSpec *pspec)
206 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
208 G_LOCK (pspec_ref_count);
209 if (pspec->ref_count > 0)
211 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
213 /* sync with _unref */
214 if (pspec->ref_count > 1)
215 pspec->ref_count -= 1;
218 G_UNLOCK (pspec_ref_count);
219 g_param_spec_unref (pspec);
224 G_UNLOCK (pspec_ref_count);
228 G_UNLOCK (pspec_ref_count);
229 g_return_if_fail (pspec->ref_count > 0);
234 g_param_spec_internal (GType param_type,
242 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
243 g_return_val_if_fail (name != NULL, NULL);
245 pspec = (gpointer) g_type_create_instance (param_type);
246 pspec->name = g_strdup (name);
247 g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
248 pspec->nick = g_strdup (nick ? nick : pspec->name);
249 pspec->blurb = g_strdup (blurb);
250 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
256 g_param_spec_get_qdata (GParamSpec *pspec,
259 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
261 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
265 g_param_spec_set_qdata (GParamSpec *pspec,
269 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
270 g_return_if_fail (quark > 0);
272 g_datalist_id_set_data (&pspec->qdata, quark, data);
276 g_param_spec_set_qdata_full (GParamSpec *pspec,
279 GDestroyNotify destroy)
281 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
282 g_return_if_fail (quark > 0);
284 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
288 g_param_spec_steal_qdata (GParamSpec *pspec,
291 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
292 g_return_val_if_fail (quark > 0, NULL);
294 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
298 g_param_value_set_default (GParamSpec *pspec,
301 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
302 g_return_if_fail (G_IS_VALUE (value));
303 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
305 g_value_reset (value);
306 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
310 g_param_value_defaults (GParamSpec *pspec,
313 GValue dflt_value = { 0, };
316 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
317 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
318 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
320 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
321 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
322 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
323 g_value_unset (&dflt_value);
329 g_param_value_validate (GParamSpec *pspec,
332 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
333 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
334 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
336 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
338 GValue oval = *value;
340 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
341 memcmp (&oval.data, &value->data, sizeof (oval.data)))
349 g_param_values_cmp (GParamSpec *pspec,
350 const GValue *value1,
351 const GValue *value2)
355 /* param_values_cmp() effectively does: value1 - value2
356 * so the return values are:
357 * -1) value1 < value2
358 * 0) value1 == value2
361 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
362 g_return_val_if_fail (G_IS_VALUE (value1), 0);
363 g_return_val_if_fail (G_IS_VALUE (value2), 0);
364 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
365 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
367 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
369 return CLAMP (cmp, -1, 1);
373 value_param_init (GValue *value)
375 value->data[0].v_pointer = NULL;
379 value_param_free_value (GValue *value)
381 if (value->data[0].v_pointer)
382 g_param_spec_unref (value->data[0].v_pointer);
386 value_param_copy_value (const GValue *src_value,
389 dest_value->data[0].v_pointer = (src_value->data[0].v_pointer
390 ? g_param_spec_ref (src_value->data[0].v_pointer)
395 value_param_peek_pointer (const GValue *value)
397 return value->data[0].v_pointer;
401 value_param_collect_value (GValue *value,
402 guint n_collect_values,
403 GTypeCValue *collect_values,
406 if (collect_values[0].v_pointer)
408 GParamSpec *param = collect_values[0].v_pointer;
410 if (param->g_type_instance.g_class == NULL)
411 return g_strconcat ("invalid unclassed param spec pointer for value type `",
412 G_VALUE_TYPE_NAME (value),
415 else if (!g_type_is_a (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
416 return g_strconcat ("invalid param spec type `",
417 G_PARAM_SPEC_TYPE_NAME (param),
418 "' for value type `",
419 G_VALUE_TYPE_NAME (value),
422 value->data[0].v_pointer = g_param_spec_ref (param);
425 value->data[0].v_pointer = NULL;
431 value_param_lcopy_value (const GValue *value,
432 guint n_collect_values,
433 GTypeCValue *collect_values,
436 GParamSpec **param_p = collect_values[0].v_pointer;
439 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
441 if (!value->data[0].v_pointer)
443 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
444 *param_p = value->data[0].v_pointer;
446 *param_p = g_param_spec_ref (value->data[0].v_pointer);
452 /* --- param spec pool --- */
453 struct _GParamSpecPool
456 gboolean type_prefixing;
457 GHashTable *hash_table;
461 param_spec_pool_hash (gconstpointer key_spec)
463 const GParamSpec *key = key_spec;
465 guint h = key->owner_type;
467 for (p = key->name; *p; p++)
468 h = (h << 5) - h + *p;
474 param_spec_pool_equals (gconstpointer key_spec_1,
475 gconstpointer key_spec_2)
477 const GParamSpec *key1 = key_spec_1;
478 const GParamSpec *key2 = key_spec_2;
480 return (key1->owner_type == key2->owner_type &&
481 strcmp (key1->name, key2->name) == 0);
485 g_param_spec_pool_new (gboolean type_prefixing)
487 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
488 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
490 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
491 pool->type_prefixing = type_prefixing != FALSE;
492 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
498 g_param_spec_pool_insert (GParamSpecPool *pool,
504 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
506 G_SLOCK (&pool->smutex);
507 for (p = pspec->name; *p; p++)
509 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
511 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
512 G_SUNLOCK (&pool->smutex);
517 pspec->owner_type = owner_type;
518 g_param_spec_ref (pspec);
519 g_hash_table_insert (pool->hash_table, pspec, pspec);
520 G_SUNLOCK (&pool->smutex);
524 g_return_if_fail (pool != NULL);
525 g_return_if_fail (pspec);
526 g_return_if_fail (owner_type > 0);
527 g_return_if_fail (pspec->owner_type == 0);
532 g_param_spec_pool_remove (GParamSpecPool *pool,
537 G_SLOCK (&pool->smutex);
538 if (g_hash_table_remove (pool->hash_table, pspec))
539 g_param_spec_unref (pspec);
541 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
542 G_SUNLOCK (&pool->smutex);
546 g_return_if_fail (pool != NULL);
547 g_return_if_fail (pspec);
551 static inline GParamSpec*
552 param_spec_ht_lookup (GHashTable *hash_table,
553 const gchar *param_name,
555 gboolean walk_ancestors)
557 GParamSpec key, *pspec;
559 key.owner_type = owner_type;
560 key.name = (gchar*) param_name;
564 pspec = g_hash_table_lookup (hash_table, &key);
567 key.owner_type = g_type_parent (key.owner_type);
569 while (key.owner_type);
571 pspec = g_hash_table_lookup (hash_table, &key);
575 /* sigh, try canonicalization */
576 key.name = g_strdup (param_name);
577 key.owner_type = owner_type;
579 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
583 pspec = g_hash_table_lookup (hash_table, &key);
589 key.owner_type = g_type_parent (key.owner_type);
591 while (key.owner_type);
593 pspec = g_hash_table_lookup (hash_table, &key);
601 g_param_spec_pool_lookup (GParamSpecPool *pool,
602 const gchar *param_name,
604 gboolean walk_ancestors,
605 const gchar **trailer_p)
610 if (!pool || !param_name)
612 g_return_val_if_fail (pool != NULL, NULL);
613 g_return_val_if_fail (param_name != NULL, NULL);
616 G_SLOCK (&pool->smutex);
618 delim = strchr (param_name, ':');
620 /* try quick and away, i.e. no prefix, no trailer */
625 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
626 G_SUNLOCK (&pool->smutex);
631 /* strip type prefix */
632 if (pool->type_prefixing && delim[1] == ':')
634 guint l = delim - param_name;
635 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
638 strncpy (buffer, param_name, delim - param_name);
640 type = g_type_from_name (buffer);
643 if (type) /* type==0 isn't a valid type pefix */
645 /* sanity check, these cases don't make a whole lot of sense */
646 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
650 G_SUNLOCK (&pool->smutex);
656 delim = strchr (param_name, ':');
657 if (!delim) /* good, can still forget about trailer */
661 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
662 G_SUNLOCK (&pool->smutex);
669 /* ok, no prefix, handle trailer */
672 guint l = delim - param_name;
673 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
675 strncpy (buffer, param_name, delim - param_name);
677 pspec = param_spec_ht_lookup (pool->hash_table, buffer, owner_type, walk_ancestors);
681 *trailer_p = pspec ? delim + 2 : NULL;
682 G_SUNLOCK (&pool->smutex);
687 /* malformed param_name */
690 G_SUNLOCK (&pool->smutex);
696 /* --- auxillary functions --- */
701 void (*finalize) (GParamSpec *pspec);
702 void (*value_set_default) (GParamSpec *pspec,
704 gboolean (*value_validate) (GParamSpec *pspec,
706 gint (*values_cmp) (GParamSpec *pspec,
707 const GValue *value1,
708 const GValue *value2);
709 } ParamSpecClassInfo;
712 param_spec_generic_class_init (gpointer g_class,
715 GParamSpecClass *class = g_class;
716 ParamSpecClassInfo *info = class_data;
718 class->value_type = info->value_type;
720 class->finalize = info->finalize; /* optional */
721 class->value_set_default = info->value_set_default;
722 if (info->value_validate)
723 class->value_validate = info->value_validate; /* optional */
724 class->values_cmp = info->values_cmp;
729 default_value_set_default (GParamSpec *pspec,
732 /* value is already zero initialized */
736 default_values_cmp (GParamSpec *pspec,
737 const GValue *value1,
738 const GValue *value2)
740 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
744 g_param_type_register_static (const gchar *name,
745 const GParamSpecTypeInfo *pspec_info)
748 sizeof (GParamSpecClass), /* class_size */
749 NULL, /* base_init */
750 NULL, /* base_destroy */
751 param_spec_generic_class_init, /* class_init */
752 NULL, /* class_destroy */
753 NULL, /* class_data */
754 0, /* instance_size */
755 16, /* n_preallocs */
756 NULL, /* instance_init */
758 ParamSpecClassInfo *cinfo;
760 g_return_val_if_fail (name != NULL, 0);
761 g_return_val_if_fail (pspec_info != NULL, 0);
762 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
763 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
764 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
765 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
766 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
767 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
769 info.instance_size = pspec_info->instance_size;
770 info.n_preallocs = pspec_info->n_preallocs;
771 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
772 cinfo = g_new (ParamSpecClassInfo, 1);
773 cinfo->value_type = pspec_info->value_type;
774 cinfo->finalize = pspec_info->finalize;
775 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
776 cinfo->value_validate = pspec_info->value_validate;
777 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
778 info.class_data = cinfo;
780 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
784 g_value_set_param (GValue *value,
787 g_return_if_fail (G_IS_VALUE_PARAM (value));
789 g_return_if_fail (G_IS_PARAM_SPEC (param));
791 if (value->data[0].v_pointer)
792 g_param_spec_unref (value->data[0].v_pointer);
793 value->data[0].v_pointer = param;
794 if (value->data[0].v_pointer)
795 g_param_spec_ref (value->data[0].v_pointer);
799 g_value_get_param (const GValue *value)
801 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
803 return value->data[0].v_pointer;
807 g_value_dup_param (const GValue *value)
809 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
811 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;