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.
22 #include "gvaluecollector.h"
28 #define G_PARAM_SPEC_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_PARAM, GParamSpecClass))
29 #define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
32 /* --- prototypes --- */
33 static void g_param_spec_class_base_init (GParamSpecClass *class);
34 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
35 static void g_param_spec_class_init (GParamSpecClass *class,
37 static void g_param_spec_init (GParamSpec *pspec);
38 static void g_param_spec_finalize (GParamSpec *pspec);
39 static void value_param_init (GValue *value);
40 static void value_param_free_value (GValue *value);
41 static void value_param_copy_value (const GValue *src_value,
43 static gpointer value_param_peek_pointer (const GValue *value);
44 static gchar* value_param_collect_value (GValue *value,
47 GTypeCValue *collect_value);
48 static gchar* value_param_lcopy_value (const GValue *value,
51 GTypeCValue *collect_value);
54 /* --- variables --- */
55 static GQuark quark_floating = 0;
58 /* --- functions --- */
60 g_param_type_init (void) /* sync with gtype.c */
62 static const GTypeFundamentalInfo finfo = {
63 (G_TYPE_FLAG_CLASSED |
64 G_TYPE_FLAG_INSTANTIATABLE |
65 G_TYPE_FLAG_DERIVABLE |
66 G_TYPE_FLAG_DEEP_DERIVABLE),
68 static const GTypeValueTable param_value_table = {
69 value_param_init, /* value_init */
70 value_param_free_value, /* value_free */
71 value_param_copy_value, /* value_copy */
72 value_param_peek_pointer, /* value_peek_pointer */
73 G_VALUE_COLLECT_POINTER, /* collect_type */
74 value_param_collect_value, /* collect_value */
75 G_VALUE_COLLECT_POINTER, /* lcopy_type */
76 value_param_lcopy_value, /* lcopy_value */
78 static const GTypeInfo param_spec_info = {
79 sizeof (GParamSpecClass),
81 (GBaseInitFunc) g_param_spec_class_base_init,
82 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
83 (GClassInitFunc) g_param_spec_class_init,
84 (GClassFinalizeFunc) NULL,
85 NULL, /* class_data */
89 (GInstanceInitFunc) g_param_spec_init,
95 type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
96 g_assert (type == G_TYPE_PARAM);
100 g_param_spec_class_base_init (GParamSpecClass *class)
105 g_param_spec_class_base_finalize (GParamSpecClass *class)
110 g_param_spec_class_init (GParamSpecClass *class,
113 quark_floating = g_quark_from_static_string ("GParamSpec-floating");
115 class->value_type = G_TYPE_NONE;
116 class->finalize = g_param_spec_finalize;
117 class->value_set_default = NULL;
118 class->value_validate = NULL;
119 class->values_cmp = NULL;
123 g_param_spec_init (GParamSpec *pspec)
129 pspec->owner_type = 0;
131 pspec->ref_count = 1;
132 g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
136 g_param_spec_finalize (GParamSpec *pspec)
138 g_datalist_clear (&pspec->qdata);
140 g_free (pspec->name);
141 g_free (pspec->nick);
142 g_free (pspec->blurb);
144 g_type_free_instance ((GTypeInstance*) pspec);
148 g_param_spec_ref (GParamSpec *pspec)
150 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
151 g_return_val_if_fail (pspec->ref_count > 0, NULL);
153 pspec->ref_count += 1;
159 g_param_spec_unref (GParamSpec *pspec)
161 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
162 g_return_if_fail (pspec->ref_count > 0);
164 /* sync with _sink */
165 pspec->ref_count -= 1;
166 if (pspec->ref_count == 0)
167 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
171 g_param_spec_sink (GParamSpec *pspec)
173 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
174 g_return_if_fail (pspec->ref_count > 0);
176 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
178 /* sync with _unref */
179 if (pspec->ref_count > 1)
180 pspec->ref_count -= 1;
182 g_param_spec_unref (pspec);
187 g_param_spec_internal (GType param_type,
195 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
196 g_return_val_if_fail (name != NULL, NULL);
198 pspec = (gpointer) g_type_create_instance (param_type);
199 pspec->name = g_strdup (name);
200 g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
201 pspec->nick = g_strdup (nick ? nick : pspec->name);
202 pspec->blurb = g_strdup (blurb);
203 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
209 g_param_spec_get_qdata (GParamSpec *pspec,
212 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
214 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
218 g_param_spec_set_qdata (GParamSpec *pspec,
222 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
223 g_return_if_fail (quark > 0);
225 g_datalist_id_set_data (&pspec->qdata, quark, data);
229 g_param_spec_set_qdata_full (GParamSpec *pspec,
232 GDestroyNotify destroy)
234 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
235 g_return_if_fail (quark > 0);
237 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
241 g_param_spec_steal_qdata (GParamSpec *pspec,
244 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
245 g_return_val_if_fail (quark > 0, NULL);
247 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
251 g_param_value_set_default (GParamSpec *pspec,
254 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
255 g_return_if_fail (G_IS_VALUE (value));
256 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
258 g_value_reset (value);
259 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
263 g_param_value_defaults (GParamSpec *pspec,
266 GValue dflt_value = { 0, };
269 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
270 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
271 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
273 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
274 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
275 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
276 g_value_unset (&dflt_value);
282 g_param_value_validate (GParamSpec *pspec,
285 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
286 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
287 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
289 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
291 GValue oval = *value;
293 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
294 memcmp (&oval.data, &value->data, sizeof (oval.data)))
302 g_param_values_cmp (GParamSpec *pspec,
303 const GValue *value1,
304 const GValue *value2)
308 /* param_values_cmp() effectively does: value1 - value2
309 * so the return values are:
310 * -1) value1 < value2
311 * 0) value1 == value2
314 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
315 g_return_val_if_fail (G_IS_VALUE (value1), 0);
316 g_return_val_if_fail (G_IS_VALUE (value2), 0);
317 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
318 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
320 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
322 return CLAMP (cmp, -1, 1);
326 value_param_init (GValue *value)
328 value->data[0].v_pointer = NULL;
332 value_param_free_value (GValue *value)
334 if (value->data[0].v_pointer)
335 g_param_spec_unref (value->data[0].v_pointer);
339 value_param_copy_value (const GValue *src_value,
342 dest_value->data[0].v_pointer = (src_value->data[0].v_pointer
343 ? g_param_spec_ref (src_value->data[0].v_pointer)
348 value_param_peek_pointer (const GValue *value)
350 return value->data[0].v_pointer;
354 value_param_collect_value (GValue *value,
357 GTypeCValue *collect_value)
359 if (collect_value->v_pointer)
361 GParamSpec *param = collect_value->v_pointer;
363 if (param->g_type_instance.g_class == NULL)
364 return g_strconcat ("invalid unclassed param spec pointer for value type `",
365 G_VALUE_TYPE_NAME (value),
368 else if (!g_type_is_a (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
369 return g_strconcat ("invalid param spec type `",
370 G_PARAM_SPEC_TYPE_NAME (param),
371 "' for value type `",
372 G_VALUE_TYPE_NAME (value),
375 value->data[0].v_pointer = g_param_spec_ref (param);
378 value->data[0].v_pointer = NULL;
385 value_param_lcopy_value (const GValue *value,
388 GTypeCValue *collect_value)
390 GParamSpec **param_p = collect_value->v_pointer;
393 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
395 *param_p = value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
402 /* --- param spec pool --- */
403 struct _GParamSpecPool
405 gboolean type_prefixing;
406 GHashTable *hash_table;
410 param_spec_pool_hash (gconstpointer key_spec)
412 const GParamSpec *key = key_spec;
414 guint h = key->owner_type;
416 for (p = key->name; *p; p++)
417 h = (h << 5) - h + *p;
423 param_spec_pool_equals (gconstpointer key_spec_1,
424 gconstpointer key_spec_2)
426 const GParamSpec *key1 = key_spec_1;
427 const GParamSpec *key2 = key_spec_2;
429 return (key1->owner_type == key2->owner_type &&
430 strcmp (key1->name, key2->name) == 0);
434 g_param_spec_pool_new (gboolean type_prefixing)
436 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
438 pool->type_prefixing = type_prefixing != FALSE;
439 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
445 g_param_spec_pool_insert (GParamSpecPool *pool,
451 g_return_if_fail (pool != NULL);
452 g_return_if_fail (pspec);
453 g_return_if_fail (owner_type > 0);
454 g_return_if_fail (pspec->owner_type == 0);
456 for (p = pspec->name; *p; p++)
458 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
460 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
465 pspec->owner_type = owner_type;
466 g_param_spec_ref (pspec);
467 g_hash_table_insert (pool->hash_table, pspec, pspec);
471 g_param_spec_pool_remove (GParamSpecPool *pool,
474 g_return_if_fail (pool != NULL);
475 g_return_if_fail (pspec);
477 if (g_hash_table_remove (pool->hash_table, pspec))
478 g_param_spec_unref (pspec);
480 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
483 static inline GParamSpec*
484 param_spec_ht_lookup (GHashTable *hash_table,
485 const gchar *param_name,
487 gboolean walk_ancestors)
489 GParamSpec key, *pspec;
491 key.owner_type = owner_type;
492 key.name = (gchar*) param_name;
496 pspec = g_hash_table_lookup (hash_table, &key);
499 key.owner_type = g_type_parent (key.owner_type);
501 while (key.owner_type);
503 pspec = g_hash_table_lookup (hash_table, &key);
507 /* sigh, try canonicalization */
508 key.name = g_strdup (param_name);
509 key.owner_type = owner_type;
511 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
515 pspec = g_hash_table_lookup (hash_table, &key);
521 key.owner_type = g_type_parent (key.owner_type);
523 while (key.owner_type);
525 pspec = g_hash_table_lookup (hash_table, &key);
533 g_param_spec_pool_lookup (GParamSpecPool *pool,
534 const gchar *param_name,
536 gboolean walk_ancestors,
537 const gchar **trailer_p)
542 g_return_val_if_fail (pool != NULL, NULL);
543 g_return_val_if_fail (param_name, NULL);
545 delim = strchr (param_name, ':');
547 /* try quick and away, i.e. no prefix, no trailer */
552 return param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
555 /* strip type prefix */
556 if (pool->type_prefixing && delim[1] == ':')
558 guint l = delim - param_name;
559 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
562 strncpy (buffer, param_name, delim - param_name);
564 type = g_type_from_name (buffer);
567 if (type) /* type==0 isn't a valid type pefix */
569 /* sanity check, these cases don't make a whole lot of sense */
570 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
578 delim = strchr (param_name, ':');
579 if (!delim) /* good, can still forget about trailer */
583 return param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
588 /* ok, no prefix, handle trailer */
591 guint l = delim - param_name;
592 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
594 strncpy (buffer, param_name, delim - param_name);
596 pspec = param_spec_ht_lookup (pool->hash_table, buffer, owner_type, walk_ancestors);
600 *trailer_p = pspec ? delim + 2 : NULL;
604 /* malformed param_name */
611 /* --- auxillary functions --- */
616 void (*finalize) (GParamSpec *pspec);
617 void (*value_set_default) (GParamSpec *pspec,
619 gboolean (*value_validate) (GParamSpec *pspec,
621 gint (*values_cmp) (GParamSpec *pspec,
622 const GValue *value1,
623 const GValue *value2);
624 } ParamSpecClassInfo;
627 param_spec_generic_class_init (gpointer g_class,
630 GParamSpecClass *class = g_class;
631 ParamSpecClassInfo *info = class_data;
633 class->value_type = info->value_type;
635 class->finalize = info->finalize; /* optional */
636 class->value_set_default = info->value_set_default;
637 if (info->value_validate)
638 class->value_validate = info->value_validate; /* optional */
639 class->values_cmp = info->values_cmp;
644 default_value_set_default (GParamSpec *pspec,
647 /* value is already zero initialized */
651 default_values_cmp (GParamSpec *pspec,
652 const GValue *value1,
653 const GValue *value2)
655 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
659 g_param_type_register_static (const gchar *name,
660 const GParamSpecTypeInfo *pspec_info)
663 sizeof (GParamSpecClass), /* class_size */
664 NULL, /* base_init */
665 NULL, /* base_destroy */
666 param_spec_generic_class_init, /* class_init */
667 NULL, /* class_destroy */
668 NULL, /* class_data */
669 0, /* instance_size */
670 16, /* n_preallocs */
671 NULL, /* instance_init */
673 ParamSpecClassInfo *cinfo;
675 g_return_val_if_fail (name != NULL, 0);
676 g_return_val_if_fail (pspec_info != NULL, 0);
677 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
678 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
679 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
680 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
681 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
682 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
684 info.instance_size = pspec_info->instance_size;
685 info.n_preallocs = pspec_info->n_preallocs;
686 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
687 cinfo = g_new (ParamSpecClassInfo, 1);
688 cinfo->value_type = pspec_info->value_type;
689 cinfo->finalize = pspec_info->finalize;
690 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
691 cinfo->value_validate = pspec_info->value_validate;
692 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
693 info.class_data = cinfo;
695 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
699 g_value_set_param (GValue *value,
702 g_return_if_fail (G_IS_VALUE_PARAM (value));
704 g_return_if_fail (G_IS_PARAM_SPEC (param));
706 if (value->data[0].v_pointer)
707 g_param_spec_unref (value->data[0].v_pointer);
708 value->data[0].v_pointer = param;
709 if (value->data[0].v_pointer)
710 g_param_spec_ref (value->data[0].v_pointer);
714 g_value_get_param (const GValue *value)
716 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
718 return value->data[0].v_pointer;
722 g_value_dup_param (const GValue *value)
724 g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
726 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;