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,
56 GTypeCValue *collect_value);
57 static gchar* value_param_lcopy_value (const GValue *value,
60 GTypeCValue *collect_value);
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 G_VALUE_COLLECT_POINTER, /* collect_type */
84 value_param_collect_value, /* collect_value */
85 G_VALUE_COLLECT_POINTER, /* lcopy_type */
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,
404 GTypeCValue *collect_value)
406 if (collect_value->v_pointer)
408 GParamSpec *param = collect_value->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;
432 value_param_lcopy_value (const GValue *value,
435 GTypeCValue *collect_value)
437 GParamSpec **param_p = collect_value->v_pointer;
440 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
442 *param_p = value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
449 /* --- param spec pool --- */
450 struct _GParamSpecPool
453 gboolean type_prefixing;
454 GHashTable *hash_table;
458 param_spec_pool_hash (gconstpointer key_spec)
460 const GParamSpec *key = key_spec;
462 guint h = key->owner_type;
464 for (p = key->name; *p; p++)
465 h = (h << 5) - h + *p;
471 param_spec_pool_equals (gconstpointer key_spec_1,
472 gconstpointer key_spec_2)
474 const GParamSpec *key1 = key_spec_1;
475 const GParamSpec *key2 = key_spec_2;
477 return (key1->owner_type == key2->owner_type &&
478 strcmp (key1->name, key2->name) == 0);
482 g_param_spec_pool_new (gboolean type_prefixing)
484 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
485 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
487 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
488 pool->type_prefixing = type_prefixing != FALSE;
489 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
495 g_param_spec_pool_insert (GParamSpecPool *pool,
501 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
503 G_SLOCK (&pool->smutex);
504 for (p = pspec->name; *p; p++)
506 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
508 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
509 G_SUNLOCK (&pool->smutex);
514 pspec->owner_type = owner_type;
515 g_param_spec_ref (pspec);
516 g_hash_table_insert (pool->hash_table, pspec, pspec);
517 G_SUNLOCK (&pool->smutex);
521 g_return_if_fail (pool != NULL);
522 g_return_if_fail (pspec);
523 g_return_if_fail (owner_type > 0);
524 g_return_if_fail (pspec->owner_type == 0);
529 g_param_spec_pool_remove (GParamSpecPool *pool,
534 G_SLOCK (&pool->smutex);
535 if (g_hash_table_remove (pool->hash_table, pspec))
536 g_param_spec_unref (pspec);
538 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
539 G_SUNLOCK (&pool->smutex);
543 g_return_if_fail (pool != NULL);
544 g_return_if_fail (pspec);
548 static inline GParamSpec*
549 param_spec_ht_lookup (GHashTable *hash_table,
550 const gchar *param_name,
552 gboolean walk_ancestors)
554 GParamSpec key, *pspec;
556 key.owner_type = owner_type;
557 key.name = (gchar*) param_name;
561 pspec = g_hash_table_lookup (hash_table, &key);
564 key.owner_type = g_type_parent (key.owner_type);
566 while (key.owner_type);
568 pspec = g_hash_table_lookup (hash_table, &key);
572 /* sigh, try canonicalization */
573 key.name = g_strdup (param_name);
574 key.owner_type = owner_type;
576 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
580 pspec = g_hash_table_lookup (hash_table, &key);
586 key.owner_type = g_type_parent (key.owner_type);
588 while (key.owner_type);
590 pspec = g_hash_table_lookup (hash_table, &key);
598 g_param_spec_pool_lookup (GParamSpecPool *pool,
599 const gchar *param_name,
601 gboolean walk_ancestors,
602 const gchar **trailer_p)
607 if (!pool || !param_name)
609 g_return_val_if_fail (pool != NULL, NULL);
610 g_return_val_if_fail (param_name != NULL, NULL);
613 G_SLOCK (&pool->smutex);
615 delim = strchr (param_name, ':');
617 /* try quick and away, i.e. no prefix, no trailer */
622 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
623 G_SUNLOCK (&pool->smutex);
628 /* strip type prefix */
629 if (pool->type_prefixing && delim[1] == ':')
631 guint l = delim - param_name;
632 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
635 strncpy (buffer, param_name, delim - param_name);
637 type = g_type_from_name (buffer);
640 if (type) /* type==0 isn't a valid type pefix */
642 /* sanity check, these cases don't make a whole lot of sense */
643 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
647 G_SUNLOCK (&pool->smutex);
653 delim = strchr (param_name, ':');
654 if (!delim) /* good, can still forget about trailer */
658 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
659 G_SUNLOCK (&pool->smutex);
666 /* ok, no prefix, handle trailer */
669 guint l = delim - param_name;
670 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
672 strncpy (buffer, param_name, delim - param_name);
674 pspec = param_spec_ht_lookup (pool->hash_table, buffer, owner_type, walk_ancestors);
678 *trailer_p = pspec ? delim + 2 : NULL;
679 G_SUNLOCK (&pool->smutex);
684 /* malformed param_name */
687 G_SUNLOCK (&pool->smutex);
693 /* --- auxillary functions --- */
698 void (*finalize) (GParamSpec *pspec);
699 void (*value_set_default) (GParamSpec *pspec,
701 gboolean (*value_validate) (GParamSpec *pspec,
703 gint (*values_cmp) (GParamSpec *pspec,
704 const GValue *value1,
705 const GValue *value2);
706 } ParamSpecClassInfo;
709 param_spec_generic_class_init (gpointer g_class,
712 GParamSpecClass *class = g_class;
713 ParamSpecClassInfo *info = class_data;
715 class->value_type = info->value_type;
717 class->finalize = info->finalize; /* optional */
718 class->value_set_default = info->value_set_default;
719 if (info->value_validate)
720 class->value_validate = info->value_validate; /* optional */
721 class->values_cmp = info->values_cmp;
726 default_value_set_default (GParamSpec *pspec,
729 /* value is already zero initialized */
733 default_values_cmp (GParamSpec *pspec,
734 const GValue *value1,
735 const GValue *value2)
737 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
741 g_param_type_register_static (const gchar *name,
742 const GParamSpecTypeInfo *pspec_info)
745 sizeof (GParamSpecClass), /* class_size */
746 NULL, /* base_init */
747 NULL, /* base_destroy */
748 param_spec_generic_class_init, /* class_init */
749 NULL, /* class_destroy */
750 NULL, /* class_data */
751 0, /* instance_size */
752 16, /* n_preallocs */
753 NULL, /* instance_init */
755 ParamSpecClassInfo *cinfo;
757 g_return_val_if_fail (name != NULL, 0);
758 g_return_val_if_fail (pspec_info != NULL, 0);
759 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
760 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
761 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
762 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
763 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
764 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
766 info.instance_size = pspec_info->instance_size;
767 info.n_preallocs = pspec_info->n_preallocs;
768 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
769 cinfo = g_new (ParamSpecClassInfo, 1);
770 cinfo->value_type = pspec_info->value_type;
771 cinfo->finalize = pspec_info->finalize;
772 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
773 cinfo->value_validate = pspec_info->value_validate;
774 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
775 info.class_data = cinfo;
777 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
781 g_value_set_param (GValue *value,
784 g_return_if_fail (G_IS_VALUE_PARAM (value));
786 g_return_if_fail (G_IS_PARAM_SPEC (param));
788 if (value->data[0].v_pointer)
789 g_param_spec_unref (value->data[0].v_pointer);
790 value->data[0].v_pointer = param;
791 if (value->data[0].v_pointer)
792 g_param_spec_ref (value->data[0].v_pointer);
796 g_value_get_param (const GValue *value)
798 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
800 return value->data[0].v_pointer;
804 g_value_dup_param (const GValue *value)
806 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
808 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;