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.
24 #include "gobjectalias.h"
26 #include "gparamspecs.h"
28 #include "gvaluecollector.h"
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)
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)
141 pspec->_blurb = NULL;
143 pspec->value_type = class->value_type;
144 pspec->owner_type = 0;
146 pspec->ref_count = 1;
148 g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
152 g_param_spec_finalize (GParamSpec *pspec)
154 g_datalist_clear (&pspec->qdata);
156 g_free (pspec->name);
157 g_free (pspec->_nick);
158 g_free (pspec->_blurb);
160 g_type_free_instance ((GTypeInstance*) pspec);
164 g_param_spec_ref (GParamSpec *pspec)
166 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
168 G_LOCK (pspec_ref_count);
169 if (pspec->ref_count > 0)
171 pspec->ref_count += 1;
172 G_UNLOCK (pspec_ref_count);
176 G_UNLOCK (pspec_ref_count);
177 g_return_val_if_fail (pspec->ref_count > 0, NULL);
184 g_param_spec_unref (GParamSpec *pspec)
186 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
188 G_LOCK (pspec_ref_count);
189 if (pspec->ref_count > 0)
191 gboolean need_finalize;
193 /* sync with _sink */
194 pspec->ref_count -= 1;
195 need_finalize = pspec->ref_count == 0;
196 G_UNLOCK (pspec_ref_count);
198 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
202 G_UNLOCK (pspec_ref_count);
203 g_return_if_fail (pspec->ref_count > 0);
208 g_param_spec_sink (GParamSpec *pspec)
210 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
212 G_LOCK (pspec_ref_count);
213 if (pspec->ref_count > 0)
215 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
217 /* sync with _unref */
218 if (pspec->ref_count > 1)
219 pspec->ref_count -= 1;
222 G_UNLOCK (pspec_ref_count);
223 g_param_spec_unref (pspec);
228 G_UNLOCK (pspec_ref_count);
232 G_UNLOCK (pspec_ref_count);
233 g_return_if_fail (pspec->ref_count > 0);
237 G_CONST_RETURN gchar*
238 g_param_spec_get_name (GParamSpec *pspec)
240 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
245 G_CONST_RETURN gchar*
246 g_param_spec_get_nick (GParamSpec *pspec)
248 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
254 GParamSpec *redirect_target;
256 redirect_target = g_param_spec_get_redirect_target (pspec);
257 if (redirect_target && redirect_target->_nick)
258 return redirect_target->_nick;
264 G_CONST_RETURN gchar*
265 g_param_spec_get_blurb (GParamSpec *pspec)
267 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
270 return pspec->_blurb;
273 GParamSpec *redirect_target;
275 redirect_target = g_param_spec_get_redirect_target (pspec);
276 if (redirect_target && redirect_target->_blurb)
277 return redirect_target->_blurb;
284 canonicalize_key (gchar *key)
288 for (p = key; *p != 0; p++)
293 (c < '0' || c > '9') &&
294 (c < 'A' || c > 'Z') &&
295 (c < 'a' || c > 'z'))
301 g_param_spec_internal (GType param_type,
309 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
310 g_return_val_if_fail (name != NULL, NULL);
311 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
313 pspec = (gpointer) g_type_create_instance (param_type);
314 pspec->name = g_strdup (name);
315 canonicalize_key (pspec->name);
316 pspec->_nick = g_strdup (nick);
317 pspec->_blurb = g_strdup (blurb);
318 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
324 g_param_spec_get_qdata (GParamSpec *pspec,
327 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
329 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
333 g_param_spec_set_qdata (GParamSpec *pspec,
337 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
338 g_return_if_fail (quark > 0);
340 g_datalist_id_set_data (&pspec->qdata, quark, data);
344 g_param_spec_set_qdata_full (GParamSpec *pspec,
347 GDestroyNotify destroy)
349 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
350 g_return_if_fail (quark > 0);
352 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
356 g_param_spec_steal_qdata (GParamSpec *pspec,
359 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
360 g_return_val_if_fail (quark > 0, NULL);
362 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
366 g_param_spec_get_redirect_target (GParamSpec *pspec)
368 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
370 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
372 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
374 return ospec->overridden;
381 g_param_value_set_default (GParamSpec *pspec,
384 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
385 g_return_if_fail (G_IS_VALUE (value));
386 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
388 g_value_reset (value);
389 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
393 g_param_value_defaults (GParamSpec *pspec,
396 GValue dflt_value = { 0, };
399 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
400 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
401 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
403 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
404 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
405 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
406 g_value_unset (&dflt_value);
412 g_param_value_validate (GParamSpec *pspec,
415 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
416 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
417 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
419 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
421 GValue oval = *value;
423 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
424 memcmp (&oval.data, &value->data, sizeof (oval.data)))
432 g_param_value_convert (GParamSpec *pspec,
433 const GValue *src_value,
435 gboolean strict_validation)
437 GValue tmp_value = { 0, };
439 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
440 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
441 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
442 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
444 /* better leave dest_value untouched when returning FALSE */
446 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
447 if (g_value_transform (src_value, &tmp_value) &&
448 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
450 g_value_unset (dest_value);
452 /* values are relocatable */
453 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
459 g_value_unset (&tmp_value);
466 g_param_values_cmp (GParamSpec *pspec,
467 const GValue *value1,
468 const GValue *value2)
472 /* param_values_cmp() effectively does: value1 - value2
473 * so the return values are:
474 * -1) value1 < value2
475 * 0) value1 == value2
478 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
479 g_return_val_if_fail (G_IS_VALUE (value1), 0);
480 g_return_val_if_fail (G_IS_VALUE (value2), 0);
481 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
482 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
484 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
486 return CLAMP (cmp, -1, 1);
490 value_param_init (GValue *value)
492 value->data[0].v_pointer = NULL;
496 value_param_free_value (GValue *value)
498 if (value->data[0].v_pointer)
499 g_param_spec_unref (value->data[0].v_pointer);
503 value_param_copy_value (const GValue *src_value,
506 if (src_value->data[0].v_pointer)
507 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
509 dest_value->data[0].v_pointer = NULL;
513 value_param_transform_value (const GValue *src_value,
516 if (src_value->data[0].v_pointer &&
517 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
518 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
520 dest_value->data[0].v_pointer = NULL;
524 value_param_peek_pointer (const GValue *value)
526 return value->data[0].v_pointer;
530 value_param_collect_value (GValue *value,
531 guint n_collect_values,
532 GTypeCValue *collect_values,
535 if (collect_values[0].v_pointer)
537 GParamSpec *param = collect_values[0].v_pointer;
539 if (param->g_type_instance.g_class == NULL)
540 return g_strconcat ("invalid unclassed param spec pointer for value type `",
541 G_VALUE_TYPE_NAME (value),
544 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
545 return g_strconcat ("invalid param spec type `",
546 G_PARAM_SPEC_TYPE_NAME (param),
547 "' for value type `",
548 G_VALUE_TYPE_NAME (value),
551 value->data[0].v_pointer = g_param_spec_ref (param);
554 value->data[0].v_pointer = NULL;
560 value_param_lcopy_value (const GValue *value,
561 guint n_collect_values,
562 GTypeCValue *collect_values,
565 GParamSpec **param_p = collect_values[0].v_pointer;
568 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
570 if (!value->data[0].v_pointer)
572 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
573 *param_p = value->data[0].v_pointer;
575 *param_p = g_param_spec_ref (value->data[0].v_pointer);
581 /* --- param spec pool --- */
582 struct _GParamSpecPool
585 gboolean type_prefixing;
586 GHashTable *hash_table;
590 param_spec_pool_hash (gconstpointer key_spec)
592 const GParamSpec *key = key_spec;
594 guint h = key->owner_type;
596 for (p = key->name; *p; p++)
597 h = (h << 5) - h + *p;
603 param_spec_pool_equals (gconstpointer key_spec_1,
604 gconstpointer key_spec_2)
606 const GParamSpec *key1 = key_spec_1;
607 const GParamSpec *key2 = key_spec_2;
609 return (key1->owner_type == key2->owner_type &&
610 strcmp (key1->name, key2->name) == 0);
614 g_param_spec_pool_new (gboolean type_prefixing)
616 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
617 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
619 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
620 pool->type_prefixing = type_prefixing != FALSE;
621 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
627 g_param_spec_pool_insert (GParamSpecPool *pool,
633 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
635 G_SLOCK (&pool->smutex);
636 for (p = pspec->name; *p; p++)
638 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
640 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
641 G_SUNLOCK (&pool->smutex);
646 pspec->owner_type = owner_type;
647 g_param_spec_ref (pspec);
648 g_hash_table_insert (pool->hash_table, pspec, pspec);
649 G_SUNLOCK (&pool->smutex);
653 g_return_if_fail (pool != NULL);
654 g_return_if_fail (pspec);
655 g_return_if_fail (owner_type > 0);
656 g_return_if_fail (pspec->owner_type == 0);
661 g_param_spec_pool_remove (GParamSpecPool *pool,
666 G_SLOCK (&pool->smutex);
667 if (g_hash_table_remove (pool->hash_table, pspec))
668 g_param_spec_unref (pspec);
670 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
671 G_SUNLOCK (&pool->smutex);
675 g_return_if_fail (pool != NULL);
676 g_return_if_fail (pspec);
680 static inline GParamSpec*
681 param_spec_ht_lookup (GHashTable *hash_table,
682 const gchar *param_name,
684 gboolean walk_ancestors)
686 GParamSpec key, *pspec;
688 key.owner_type = owner_type;
689 key.name = (gchar*) param_name;
693 pspec = g_hash_table_lookup (hash_table, &key);
696 key.owner_type = g_type_parent (key.owner_type);
698 while (key.owner_type);
700 pspec = g_hash_table_lookup (hash_table, &key);
704 /* try canonicalized form */
705 key.name = g_strdup (param_name);
706 key.owner_type = owner_type;
708 canonicalize_key (key.name);
712 pspec = g_hash_table_lookup (hash_table, &key);
718 key.owner_type = g_type_parent (key.owner_type);
720 while (key.owner_type);
722 pspec = g_hash_table_lookup (hash_table, &key);
730 g_param_spec_pool_lookup (GParamSpecPool *pool,
731 const gchar *param_name,
733 gboolean walk_ancestors)
738 if (!pool || !param_name)
740 g_return_val_if_fail (pool != NULL, NULL);
741 g_return_val_if_fail (param_name != NULL, NULL);
744 G_SLOCK (&pool->smutex);
746 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
748 /* try quick and away, i.e. without prefix */
751 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
752 G_SUNLOCK (&pool->smutex);
757 /* strip type prefix */
758 if (pool->type_prefixing && delim[1] == ':')
760 guint l = delim - param_name;
761 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
764 strncpy (buffer, param_name, delim - param_name);
766 type = g_type_from_name (buffer);
769 if (type) /* type==0 isn't a valid type pefix */
771 /* sanity check, these cases don't make a whole lot of sense */
772 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
774 G_SUNLOCK (&pool->smutex);
780 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
781 G_SUNLOCK (&pool->smutex);
786 /* malformed param_name */
788 G_SUNLOCK (&pool->smutex);
794 pool_list (gpointer key,
798 GParamSpec *pspec = value;
799 gpointer *data = user_data;
800 GType owner_type = (GType) data[1];
802 if (owner_type == pspec->owner_type)
803 data[0] = g_list_prepend (data[0], pspec);
807 g_param_spec_pool_list_owned (GParamSpecPool *pool,
812 g_return_val_if_fail (pool != NULL, NULL);
813 g_return_val_if_fail (owner_type > 0, NULL);
815 G_SLOCK (&pool->smutex);
817 data[1] = (gpointer) owner_type;
818 g_hash_table_foreach (pool->hash_table, pool_list, &data);
819 G_SUNLOCK (&pool->smutex);
825 pspec_compare_id (gconstpointer a,
828 const GParamSpec *pspec1 = a, *pspec2 = b;
830 return pspec1->param_id < pspec2->param_id ? -1 : pspec1->param_id > pspec2->param_id;
833 static inline GSList*
834 pspec_list_remove_overridden_and_redirected (GSList *plist,
839 GSList *rlist = NULL;
843 GSList *tmp = plist->next;
844 GParamSpec *pspec = plist->data;
846 gboolean remove = FALSE;
848 /* Remove paramspecs that are redirected, and also paramspecs
849 * that have are overridden by non-redirected properties.
850 * The idea is to get the single paramspec for each name that
851 * best corresponds to what the application sees.
853 if (g_param_spec_get_redirect_target (pspec))
857 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
860 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
861 if (redirect != pspec)
868 g_slist_free_1 (plist);
882 pool_depth_list (gpointer key,
886 GParamSpec *pspec = value;
887 gpointer *data = user_data;
888 GSList **slists = data[0];
889 GType owner_type = (GType) data[1];
891 if (g_type_is_a (owner_type, pspec->owner_type))
893 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
895 slists[0] = g_slist_prepend (slists[0], pspec);
899 guint d = g_type_depth (pspec->owner_type);
901 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
906 /* We handle interfaces specially since we don't want to
907 * count interface prerequsites like normal inheritance;
908 * the property comes from the direct inheritance from
909 * the prerequisite class, not from the interface that
912 * also 'depth' isn't a meaningful concept for interface
916 pool_depth_list_for_interface (gpointer key,
920 GParamSpec *pspec = value;
921 gpointer *data = user_data;
922 GSList **slists = data[0];
923 GType owner_type = (GType) data[1];
925 if (pspec->owner_type == owner_type)
926 slists[0] = g_slist_prepend (slists[0], pspec);
929 GParamSpec** /* free result */
930 g_param_spec_pool_list (GParamSpecPool *pool,
934 GParamSpec **pspecs, **p;
935 GSList **slists, *node;
939 g_return_val_if_fail (pool != NULL, NULL);
940 g_return_val_if_fail (owner_type > 0, NULL);
941 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
943 G_SLOCK (&pool->smutex);
945 d = g_type_depth (owner_type);
946 slists = g_new0 (GSList*, d);
948 data[1] = (gpointer) owner_type;
950 g_hash_table_foreach (pool->hash_table,
951 G_TYPE_IS_INTERFACE (owner_type) ?
952 pool_depth_list_for_interface :
956 for (i = 0; i < d; i++)
957 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
958 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
960 for (i = 0; i < d; i++)
962 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
963 for (node = slists[i]; node; node = node->next)
965 g_slist_free (slists[i]);
969 G_SUNLOCK (&pool->smutex);
975 /* --- auxillary functions --- */
980 void (*finalize) (GParamSpec *pspec);
981 void (*value_set_default) (GParamSpec *pspec,
983 gboolean (*value_validate) (GParamSpec *pspec,
985 gint (*values_cmp) (GParamSpec *pspec,
986 const GValue *value1,
987 const GValue *value2);
988 } ParamSpecClassInfo;
991 param_spec_generic_class_init (gpointer g_class,
994 GParamSpecClass *class = g_class;
995 ParamSpecClassInfo *info = class_data;
997 class->value_type = info->value_type;
999 class->finalize = info->finalize; /* optional */
1000 class->value_set_default = info->value_set_default;
1001 if (info->value_validate)
1002 class->value_validate = info->value_validate; /* optional */
1003 class->values_cmp = info->values_cmp;
1004 g_free (class_data);
1008 default_value_set_default (GParamSpec *pspec,
1011 /* value is already zero initialized */
1015 default_values_cmp (GParamSpec *pspec,
1016 const GValue *value1,
1017 const GValue *value2)
1019 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1023 g_param_type_register_static (const gchar *name,
1024 const GParamSpecTypeInfo *pspec_info)
1027 sizeof (GParamSpecClass), /* class_size */
1028 NULL, /* base_init */
1029 NULL, /* base_destroy */
1030 param_spec_generic_class_init, /* class_init */
1031 NULL, /* class_destroy */
1032 NULL, /* class_data */
1033 0, /* instance_size */
1034 16, /* n_preallocs */
1035 NULL, /* instance_init */
1037 ParamSpecClassInfo *cinfo;
1039 g_return_val_if_fail (name != NULL, 0);
1040 g_return_val_if_fail (pspec_info != NULL, 0);
1041 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1042 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1043 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1044 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1045 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1046 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1048 info.instance_size = pspec_info->instance_size;
1049 info.n_preallocs = pspec_info->n_preallocs;
1050 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1051 cinfo = g_new (ParamSpecClassInfo, 1);
1052 cinfo->value_type = pspec_info->value_type;
1053 cinfo->finalize = pspec_info->finalize;
1054 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1055 cinfo->value_validate = pspec_info->value_validate;
1056 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1057 info.class_data = cinfo;
1059 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1063 g_value_set_param (GValue *value,
1066 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1068 g_return_if_fail (G_IS_PARAM_SPEC (param));
1070 if (value->data[0].v_pointer)
1071 g_param_spec_unref (value->data[0].v_pointer);
1072 value->data[0].v_pointer = param;
1073 if (value->data[0].v_pointer)
1074 g_param_spec_ref (value->data[0].v_pointer);
1078 g_value_set_param_take_ownership (GValue *value,
1081 g_value_take_param (value, param);
1085 g_value_take_param (GValue *value,
1088 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1090 g_return_if_fail (G_IS_PARAM_SPEC (param));
1092 if (value->data[0].v_pointer)
1093 g_param_spec_unref (value->data[0].v_pointer);
1094 value->data[0].v_pointer = param; /* we take over the reference count */
1098 g_value_get_param (const GValue *value)
1100 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1102 return value->data[0].v_pointer;
1106 g_value_dup_param (const GValue *value)
1108 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1110 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;