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 #define G_PARAM_SPEC_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_PARAM, GParamSpecClass))
30 /* --- prototypes --- */
31 extern void g_param_types_init (void);
32 extern void g_param_spec_types_init (void); /* sync with glib-gparamspecs.c */
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);
41 /* --- functions --- */
43 g_param_types_init (void) /* sync with glib-gtype.c */
45 static const GTypeFundamentalInfo finfo = {
46 (G_TYPE_FLAG_CLASSED |
47 G_TYPE_FLAG_INSTANTIATABLE |
48 G_TYPE_FLAG_DERIVABLE |
49 G_TYPE_FLAG_DEEP_DERIVABLE),
50 0 /* n_collect_bytes */,
51 NULL /* GTypeParamCollector */,
53 static const GTypeInfo param_spec_info = {
54 sizeof (GParamSpecClass),
56 (GBaseInitFunc) g_param_spec_class_base_init,
57 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
58 (GClassInitFunc) g_param_spec_class_init,
59 (GClassFinalizeFunc) NULL,
60 NULL /* class_data */,
64 (GInstanceInitFunc) g_param_spec_init,
68 type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", &finfo, ¶m_spec_info);
69 g_assert (type == G_TYPE_PARAM);
71 /* derived param specs
73 g_param_spec_types_init ();
77 g_param_spec_class_base_init (GParamSpecClass *class)
82 g_param_spec_class_base_finalize (GParamSpecClass *class)
87 g_param_spec_class_init (GParamSpecClass *class,
90 class->finalize = g_param_spec_finalize;
91 class->param_init = NULL;
92 class->param_free_value = NULL;
93 class->param_validate = NULL;
94 class->param_values_cmp = NULL;
95 class->param_copy_value = NULL;
96 class->collect_type = 0;
97 class->param_collect_value = NULL;
98 class->lcopy_type = 0;
99 class->param_lcopy_value = NULL;
103 g_param_spec_init (GParamSpec *pspec)
109 pspec->owner_type = 0;
111 pspec->ref_count = 1;
115 g_param_spec_finalize (GParamSpec *pspec)
117 g_datalist_clear (&pspec->qdata);
119 g_free (pspec->name);
120 g_free (pspec->nick);
121 g_free (pspec->blurb);
123 g_type_free_instance ((GTypeInstance*) pspec);
127 g_param_spec_ref (GParamSpec *pspec)
129 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
130 g_return_val_if_fail (pspec->ref_count > 0, NULL);
132 pspec->ref_count += 1;
138 g_param_spec_unref (GParamSpec *pspec)
140 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
141 g_return_if_fail (pspec->ref_count > 0);
143 pspec->ref_count -= 1;
144 if (pspec->ref_count == 0)
145 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
149 g_param_spec_internal (GType param_type,
157 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
158 g_return_val_if_fail (name != NULL, NULL);
160 pspec = (gpointer) g_type_create_instance (param_type);
161 pspec->name = g_strdup (name);
162 g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
163 pspec->nick = g_strdup (nick ? nick : pspec->name);
164 pspec->blurb = g_strdup (blurb);
165 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
171 g_param_spec_get_qdata (GParamSpec *pspec,
174 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
176 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
180 g_param_spec_set_qdata (GParamSpec *pspec,
184 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
185 g_return_if_fail (quark > 0);
187 g_datalist_id_set_data (&pspec->qdata, quark, data);
191 g_param_spec_set_qdata_full (GParamSpec *pspec,
194 GDestroyNotify destroy)
196 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
197 g_return_if_fail (quark > 0);
199 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : NULL);
203 g_param_spec_steal_qdata (GParamSpec *pspec,
206 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
207 g_return_val_if_fail (quark > 0, NULL);
209 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
213 param_spec_hash (gconstpointer key_spec)
215 const GParamSpec *key = key_spec;
217 guint h = key->owner_type;
219 for (p = key->name; *p; p++)
220 h = (h << 5) - h + *p;
226 param_spec_equals (gconstpointer key_spec_1,
227 gconstpointer key_spec_2)
229 const GParamSpec *key1 = key_spec_1;
230 const GParamSpec *key2 = key_spec_2;
232 return (key1->owner_type == key2->owner_type &&
233 strcmp (key1->name, key2->name) == 0);
237 g_param_spec_hash_table_new (void)
239 return g_hash_table_new (param_spec_hash, param_spec_equals);
243 g_param_spec_hash_table_insert (GHashTable *hash_table,
247 g_return_if_fail (hash_table != NULL);
248 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
249 g_return_if_fail (pspec->name != NULL);
250 if (pspec->owner_type != owner_type)
251 g_return_if_fail (pspec->owner_type == 0);
253 if (strchr (pspec->name, ':'))
254 g_warning (G_STRLOC ": parameter name `%s' contains field-delimeter",
258 pspec->owner_type = owner_type;
259 g_hash_table_insert (hash_table, pspec, pspec);
264 g_param_spec_hash_table_remove (GHashTable *hash_table,
267 g_return_if_fail (hash_table != NULL);
268 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
270 g_assert (g_param_spec_hash_table_lookup (hash_table, pspec->name, pspec->owner_type, FALSE, NULL) != NULL); // FIXME: paranoid
272 g_hash_table_remove (hash_table, pspec);
273 g_assert (g_param_spec_hash_table_lookup (hash_table, pspec->name, pspec->owner_type, FALSE, NULL) == NULL); // FIXME: paranoid
274 pspec->owner_type = 0;
278 g_param_spec_hash_table_lookup (GHashTable *hash_table,
279 const gchar *param_name,
281 gboolean try_ancestors,
282 const gchar **trailer)
288 g_return_val_if_fail (hash_table != NULL, NULL);
289 g_return_val_if_fail (param_name != NULL, NULL);
291 key.owner_type = owner_type;
292 delim = strchr (param_name, ':');
294 key.name = g_strndup (param_name, delim - param_name);
296 key.name = g_strdup (param_name);
297 g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
302 pspec = g_hash_table_lookup (hash_table, &key);
303 if (!pspec && try_ancestors)
305 key.owner_type = g_type_parent (key.owner_type);
306 while (key.owner_type)
308 pspec = g_hash_table_lookup (hash_table, &key);
311 key.owner_type = g_type_parent (key.owner_type);