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.
27 #include "gvaluecollector.h"
33 #define G_PARAM_USER_MASK (~0 << G_PARAM_USER_SHIFT)
34 #define PSPEC_APPLIES_TO_VALUE(pspec, value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
35 #define G_SLOCK(mutex) g_static_mutex_lock (mutex)
36 #define G_SUNLOCK(mutex) g_static_mutex_unlock (mutex)
39 /* --- prototypes --- */
40 static void g_param_spec_class_base_init (GParamSpecClass *class);
41 static void g_param_spec_class_base_finalize (GParamSpecClass *class);
42 static void g_param_spec_class_init (GParamSpecClass *class,
44 static void g_param_spec_init (GParamSpec *pspec,
45 GParamSpecClass *class);
46 static void g_param_spec_finalize (GParamSpec *pspec);
47 static void value_param_init (GValue *value);
48 static void value_param_free_value (GValue *value);
49 static void value_param_copy_value (const GValue *src_value,
51 static void value_param_transform_value (const GValue *src_value,
53 static gpointer value_param_peek_pointer (const GValue *value);
54 static gchar* value_param_collect_value (GValue *value,
55 guint n_collect_values,
56 GTypeCValue *collect_values,
58 static gchar* value_param_lcopy_value (const GValue *value,
59 guint n_collect_values,
60 GTypeCValue *collect_values,
64 /* --- variables --- */
65 static GQuark quark_floating = 0;
66 G_LOCK_DEFINE_STATIC (pspec_ref_count);
69 /* --- functions --- */
71 g_param_type_init (void) /* sync with gtype.c */
73 static const GTypeFundamentalInfo finfo = {
74 (G_TYPE_FLAG_CLASSED |
75 G_TYPE_FLAG_INSTANTIATABLE |
76 G_TYPE_FLAG_DERIVABLE |
77 G_TYPE_FLAG_DEEP_DERIVABLE),
79 static const GTypeValueTable param_value_table = {
80 value_param_init, /* value_init */
81 value_param_free_value, /* value_free */
82 value_param_copy_value, /* value_copy */
83 value_param_peek_pointer, /* value_peek_pointer */
84 "p", /* collect_format */
85 value_param_collect_value, /* collect_value */
86 "p", /* lcopy_format */
87 value_param_lcopy_value, /* lcopy_value */
89 static const GTypeInfo param_spec_info = {
90 sizeof (GParamSpecClass),
92 (GBaseInitFunc) g_param_spec_class_base_init,
93 (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
94 (GClassInitFunc) g_param_spec_class_init,
95 (GClassFinalizeFunc) NULL,
96 NULL, /* class_data */
100 (GInstanceInitFunc) g_param_spec_init,
106 type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", ¶m_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
107 g_assert (type == G_TYPE_PARAM);
108 g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
112 g_param_spec_class_base_init (GParamSpecClass *class)
117 g_param_spec_class_base_finalize (GParamSpecClass *class)
122 g_param_spec_class_init (GParamSpecClass *class,
125 quark_floating = g_quark_from_static_string ("GParamSpec-floating");
127 class->value_type = G_TYPE_NONE;
128 class->finalize = g_param_spec_finalize;
129 class->value_set_default = NULL;
130 class->value_validate = NULL;
131 class->values_cmp = NULL;
135 g_param_spec_init (GParamSpec *pspec,
136 GParamSpecClass *class)
140 pspec->_blurb = NULL;
142 pspec->value_type = class->value_type;
143 pspec->owner_type = 0;
145 pspec->ref_count = 1;
147 g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
151 g_param_spec_finalize (GParamSpec *pspec)
153 g_datalist_clear (&pspec->qdata);
155 g_free (pspec->name);
156 g_free (pspec->_nick);
157 g_free (pspec->_blurb);
159 g_type_free_instance ((GTypeInstance*) pspec);
163 g_param_spec_ref (GParamSpec *pspec)
165 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
167 G_LOCK (pspec_ref_count);
168 if (pspec->ref_count > 0)
170 pspec->ref_count += 1;
171 G_UNLOCK (pspec_ref_count);
175 G_UNLOCK (pspec_ref_count);
176 g_return_val_if_fail (pspec->ref_count > 0, NULL);
183 g_param_spec_unref (GParamSpec *pspec)
185 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
187 G_LOCK (pspec_ref_count);
188 if (pspec->ref_count > 0)
190 gboolean need_finalize;
192 /* sync with _sink */
193 pspec->ref_count -= 1;
194 need_finalize = pspec->ref_count == 0;
195 G_UNLOCK (pspec_ref_count);
197 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
201 G_UNLOCK (pspec_ref_count);
202 g_return_if_fail (pspec->ref_count > 0);
207 g_param_spec_sink (GParamSpec *pspec)
209 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
211 G_LOCK (pspec_ref_count);
212 if (pspec->ref_count > 0)
214 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
216 /* sync with _unref */
217 if (pspec->ref_count > 1)
218 pspec->ref_count -= 1;
221 G_UNLOCK (pspec_ref_count);
222 g_param_spec_unref (pspec);
227 G_UNLOCK (pspec_ref_count);
231 G_UNLOCK (pspec_ref_count);
232 g_return_if_fail (pspec->ref_count > 0);
236 G_CONST_RETURN gchar*
237 g_param_spec_get_name (GParamSpec *pspec)
239 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
244 G_CONST_RETURN gchar*
245 g_param_spec_get_nick (GParamSpec *pspec)
247 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
249 return pspec->_nick ? pspec->_nick : pspec->name;
252 G_CONST_RETURN gchar*
253 g_param_spec_get_blurb (GParamSpec *pspec)
255 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
257 return pspec->_blurb;
261 canonalize_key(gchar *key)
265 for (p = key; (c = *p) != 0; p++)
268 (c < '0' || c > '9') &&
269 (c < 'A' || c > 'Z') &&
270 (c < 'a' || c > 'z'))
276 g_param_spec_internal (GType param_type,
284 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
285 g_return_val_if_fail (name != NULL, NULL);
286 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
288 pspec = (gpointer) g_type_create_instance (param_type);
289 pspec->name = g_strdup (name);
290 canonalize_key (pspec->name);
291 pspec->_nick = g_strdup (nick);
292 pspec->_blurb = g_strdup (blurb);
293 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
299 g_param_spec_get_qdata (GParamSpec *pspec,
302 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
304 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
308 g_param_spec_set_qdata (GParamSpec *pspec,
312 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
313 g_return_if_fail (quark > 0);
315 g_datalist_id_set_data (&pspec->qdata, quark, data);
319 g_param_spec_set_qdata_full (GParamSpec *pspec,
322 GDestroyNotify destroy)
324 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
325 g_return_if_fail (quark > 0);
327 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
331 g_param_spec_steal_qdata (GParamSpec *pspec,
334 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
335 g_return_val_if_fail (quark > 0, NULL);
337 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
341 g_param_value_set_default (GParamSpec *pspec,
344 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
345 g_return_if_fail (G_IS_VALUE (value));
346 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
348 g_value_reset (value);
349 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
353 g_param_value_defaults (GParamSpec *pspec,
356 GValue dflt_value = { 0, };
359 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
360 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
361 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
363 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
364 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
365 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
366 g_value_unset (&dflt_value);
372 g_param_value_validate (GParamSpec *pspec,
375 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
376 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
377 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
379 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
381 GValue oval = *value;
383 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
384 memcmp (&oval.data, &value->data, sizeof (oval.data)))
392 g_param_value_convert (GParamSpec *pspec,
393 const GValue *src_value,
395 gboolean strict_validation)
397 GValue tmp_value = { 0, };
399 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
400 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
401 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
402 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
404 /* better leave dest_value untouched when returning FALSE */
406 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
407 if (g_value_transform (src_value, &tmp_value) &&
408 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
410 g_value_unset (dest_value);
412 /* values are relocatable */
413 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
419 g_value_unset (&tmp_value);
426 g_param_values_cmp (GParamSpec *pspec,
427 const GValue *value1,
428 const GValue *value2)
432 /* param_values_cmp() effectively does: value1 - value2
433 * so the return values are:
434 * -1) value1 < value2
435 * 0) value1 == value2
438 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
439 g_return_val_if_fail (G_IS_VALUE (value1), 0);
440 g_return_val_if_fail (G_IS_VALUE (value2), 0);
441 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
442 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
444 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
446 return CLAMP (cmp, -1, 1);
450 value_param_init (GValue *value)
452 value->data[0].v_pointer = NULL;
456 value_param_free_value (GValue *value)
458 if (value->data[0].v_pointer)
459 g_param_spec_unref (value->data[0].v_pointer);
463 value_param_copy_value (const GValue *src_value,
466 if (src_value->data[0].v_pointer)
467 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
469 dest_value->data[0].v_pointer = NULL;
473 value_param_transform_value (const GValue *src_value,
476 if (src_value->data[0].v_pointer &&
477 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
478 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
480 dest_value->data[0].v_pointer = NULL;
484 value_param_peek_pointer (const GValue *value)
486 return value->data[0].v_pointer;
490 value_param_collect_value (GValue *value,
491 guint n_collect_values,
492 GTypeCValue *collect_values,
495 if (collect_values[0].v_pointer)
497 GParamSpec *param = collect_values[0].v_pointer;
499 if (param->g_type_instance.g_class == NULL)
500 return g_strconcat ("invalid unclassed param spec pointer for value type `",
501 G_VALUE_TYPE_NAME (value),
504 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
505 return g_strconcat ("invalid param spec type `",
506 G_PARAM_SPEC_TYPE_NAME (param),
507 "' for value type `",
508 G_VALUE_TYPE_NAME (value),
511 value->data[0].v_pointer = g_param_spec_ref (param);
514 value->data[0].v_pointer = NULL;
520 value_param_lcopy_value (const GValue *value,
521 guint n_collect_values,
522 GTypeCValue *collect_values,
525 GParamSpec **param_p = collect_values[0].v_pointer;
528 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
530 if (!value->data[0].v_pointer)
532 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
533 *param_p = value->data[0].v_pointer;
535 *param_p = g_param_spec_ref (value->data[0].v_pointer);
541 /* --- param spec pool --- */
542 struct _GParamSpecPool
545 gboolean type_prefixing;
546 GHashTable *hash_table;
550 param_spec_pool_hash (gconstpointer key_spec)
552 const GParamSpec *key = key_spec;
554 guint h = key->owner_type;
556 for (p = key->name; *p; p++)
557 h = (h << 5) - h + *p;
563 param_spec_pool_equals (gconstpointer key_spec_1,
564 gconstpointer key_spec_2)
566 const GParamSpec *key1 = key_spec_1;
567 const GParamSpec *key2 = key_spec_2;
569 return (key1->owner_type == key2->owner_type &&
570 strcmp (key1->name, key2->name) == 0);
574 g_param_spec_pool_new (gboolean type_prefixing)
576 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
577 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
579 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
580 pool->type_prefixing = type_prefixing != FALSE;
581 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
587 g_param_spec_pool_insert (GParamSpecPool *pool,
593 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
595 G_SLOCK (&pool->smutex);
596 for (p = pspec->name; *p; p++)
598 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
600 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
601 G_SUNLOCK (&pool->smutex);
606 pspec->owner_type = owner_type;
607 g_param_spec_ref (pspec);
608 g_hash_table_insert (pool->hash_table, pspec, pspec);
609 G_SUNLOCK (&pool->smutex);
613 g_return_if_fail (pool != NULL);
614 g_return_if_fail (pspec);
615 g_return_if_fail (owner_type > 0);
616 g_return_if_fail (pspec->owner_type == 0);
621 g_param_spec_pool_remove (GParamSpecPool *pool,
626 G_SLOCK (&pool->smutex);
627 if (g_hash_table_remove (pool->hash_table, pspec))
628 g_param_spec_unref (pspec);
630 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
631 G_SUNLOCK (&pool->smutex);
635 g_return_if_fail (pool != NULL);
636 g_return_if_fail (pspec);
640 static inline GParamSpec*
641 param_spec_ht_lookup (GHashTable *hash_table,
642 const gchar *param_name,
644 gboolean walk_ancestors)
646 GParamSpec key, *pspec;
648 key.owner_type = owner_type;
649 key.name = (gchar*) param_name;
653 pspec = g_hash_table_lookup (hash_table, &key);
656 key.owner_type = g_type_parent (key.owner_type);
658 while (key.owner_type);
660 pspec = g_hash_table_lookup (hash_table, &key);
664 /* try canonicalized form */
665 key.name = g_strdup (param_name);
666 key.owner_type = owner_type;
668 canonalize_key (key.name);
672 pspec = g_hash_table_lookup (hash_table, &key);
678 key.owner_type = g_type_parent (key.owner_type);
680 while (key.owner_type);
682 pspec = g_hash_table_lookup (hash_table, &key);
690 g_param_spec_pool_lookup (GParamSpecPool *pool,
691 const gchar *param_name,
693 gboolean walk_ancestors)
698 if (!pool || !param_name)
700 g_return_val_if_fail (pool != NULL, NULL);
701 g_return_val_if_fail (param_name != NULL, NULL);
704 G_SLOCK (&pool->smutex);
706 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
708 /* try quick and away, i.e. without prefix */
711 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
712 G_SUNLOCK (&pool->smutex);
717 /* strip type prefix */
718 if (pool->type_prefixing && delim[1] == ':')
720 guint l = delim - param_name;
721 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
724 strncpy (buffer, param_name, delim - param_name);
726 type = g_type_from_name (buffer);
729 if (type) /* type==0 isn't a valid type pefix */
731 /* sanity check, these cases don't make a whole lot of sense */
732 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
734 G_SUNLOCK (&pool->smutex);
740 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
741 G_SUNLOCK (&pool->smutex);
746 /* malformed param_name */
748 G_SUNLOCK (&pool->smutex);
754 pool_list (gpointer key,
758 GParamSpec *pspec = value;
759 gpointer *data = user_data;
760 GType owner_type = (GType) data[1];
762 if (owner_type == pspec->owner_type)
763 data[0] = g_list_prepend (data[0], pspec);
767 g_param_spec_pool_list_owned (GParamSpecPool *pool,
772 g_return_val_if_fail (pool != NULL, NULL);
773 g_return_val_if_fail (owner_type > 0, NULL);
775 G_SLOCK (&pool->smutex);
777 data[1] = (gpointer) owner_type;
778 g_hash_table_foreach (pool->hash_table, pool_list, &data);
779 G_SUNLOCK (&pool->smutex);
785 pspec_compare_id (gconstpointer a,
788 const GParamSpec *pspec1 = a, *pspec2 = b;
790 return pspec1->param_id < pspec2->param_id ? -1 : pspec1->param_id > pspec2->param_id;
793 static inline GSList*
794 pspec_list_remove_overridden (GSList *plist,
799 GSList *rlist = NULL;
803 GSList *tmp = plist->next;
804 GParamSpec *pspec = plist->data;
806 if (param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE) != pspec)
807 g_slist_free_1 (plist);
820 pool_depth_list (gpointer key,
824 GParamSpec *pspec = value;
825 gpointer *data = user_data;
826 GSList **slists = data[0];
827 GType owner_type = (GType) data[1];
829 if (g_type_is_a (owner_type, pspec->owner_type))
831 guint d = g_type_depth (pspec->owner_type);
833 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
837 GParamSpec** /* free result */
838 g_param_spec_pool_list (GParamSpecPool *pool,
842 GParamSpec **pspecs, **p;
843 GSList **slists, *node;
847 g_return_val_if_fail (pool != NULL, NULL);
848 g_return_val_if_fail (owner_type > 0, NULL);
849 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
851 G_SLOCK (&pool->smutex);
853 d = g_type_depth (owner_type);
854 slists = g_new0 (GSList*, d);
856 data[1] = (gpointer) owner_type;
857 g_hash_table_foreach (pool->hash_table, pool_depth_list, &data);
858 for (i = 0; i < d - 1; i++)
859 slists[i] = pspec_list_remove_overridden (slists[i], pool->hash_table, owner_type, n_pspecs_p);
860 *n_pspecs_p += g_slist_length (slists[i]);
861 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
863 for (i = 0; i < d; i++)
865 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
866 for (node = slists[i]; node; node = node->next)
868 g_slist_free (slists[i]);
872 G_SUNLOCK (&pool->smutex);
878 /* --- auxillary functions --- */
883 void (*finalize) (GParamSpec *pspec);
884 void (*value_set_default) (GParamSpec *pspec,
886 gboolean (*value_validate) (GParamSpec *pspec,
888 gint (*values_cmp) (GParamSpec *pspec,
889 const GValue *value1,
890 const GValue *value2);
891 } ParamSpecClassInfo;
894 param_spec_generic_class_init (gpointer g_class,
897 GParamSpecClass *class = g_class;
898 ParamSpecClassInfo *info = class_data;
900 class->value_type = info->value_type;
902 class->finalize = info->finalize; /* optional */
903 class->value_set_default = info->value_set_default;
904 if (info->value_validate)
905 class->value_validate = info->value_validate; /* optional */
906 class->values_cmp = info->values_cmp;
911 default_value_set_default (GParamSpec *pspec,
914 /* value is already zero initialized */
918 default_values_cmp (GParamSpec *pspec,
919 const GValue *value1,
920 const GValue *value2)
922 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
926 g_param_type_register_static (const gchar *name,
927 const GParamSpecTypeInfo *pspec_info)
930 sizeof (GParamSpecClass), /* class_size */
931 NULL, /* base_init */
932 NULL, /* base_destroy */
933 param_spec_generic_class_init, /* class_init */
934 NULL, /* class_destroy */
935 NULL, /* class_data */
936 0, /* instance_size */
937 16, /* n_preallocs */
938 NULL, /* instance_init */
940 ParamSpecClassInfo *cinfo;
942 g_return_val_if_fail (name != NULL, 0);
943 g_return_val_if_fail (pspec_info != NULL, 0);
944 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
945 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
946 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
947 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
948 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
949 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
951 info.instance_size = pspec_info->instance_size;
952 info.n_preallocs = pspec_info->n_preallocs;
953 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
954 cinfo = g_new (ParamSpecClassInfo, 1);
955 cinfo->value_type = pspec_info->value_type;
956 cinfo->finalize = pspec_info->finalize;
957 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
958 cinfo->value_validate = pspec_info->value_validate;
959 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
960 info.class_data = cinfo;
962 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
966 g_value_set_param (GValue *value,
969 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
971 g_return_if_fail (G_IS_PARAM_SPEC (param));
973 if (value->data[0].v_pointer)
974 g_param_spec_unref (value->data[0].v_pointer);
975 value->data[0].v_pointer = param;
976 if (value->data[0].v_pointer)
977 g_param_spec_ref (value->data[0].v_pointer);
981 g_value_set_param_take_ownership (GValue *value,
984 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
986 g_return_if_fail (G_IS_PARAM_SPEC (param));
988 if (value->data[0].v_pointer)
989 g_param_spec_unref (value->data[0].v_pointer);
990 value->data[0].v_pointer = param; /* we take over the reference count */
994 g_value_get_param (const GValue *value)
996 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
998 return value->data[0].v_pointer;
1002 g_value_dup_param (const GValue *value)
1004 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1006 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;