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.
25 #include "gparamspecs.h"
27 #include "gvaluecollector.h"
28 #include "gobjectalias.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 if (!(pspec->flags & G_PARAM_STATIC_NAME))
157 g_free (pspec->name);
159 if (!(pspec->flags & G_PARAM_STATIC_NICK))
160 g_free (pspec->_nick);
162 if (!(pspec->flags & G_PARAM_STATIC_BLURB))
163 g_free (pspec->_blurb);
165 g_type_free_instance ((GTypeInstance*) pspec);
169 g_param_spec_ref (GParamSpec *pspec)
171 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
173 G_LOCK (pspec_ref_count);
174 if (pspec->ref_count > 0)
176 pspec->ref_count += 1;
177 G_UNLOCK (pspec_ref_count);
181 G_UNLOCK (pspec_ref_count);
182 g_return_val_if_fail (pspec->ref_count > 0, NULL);
189 g_param_spec_unref (GParamSpec *pspec)
191 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
193 G_LOCK (pspec_ref_count);
194 if (pspec->ref_count > 0)
196 gboolean need_finalize;
198 /* sync with _sink */
199 pspec->ref_count -= 1;
200 need_finalize = pspec->ref_count == 0;
201 G_UNLOCK (pspec_ref_count);
203 G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
207 G_UNLOCK (pspec_ref_count);
208 g_return_if_fail (pspec->ref_count > 0);
213 g_param_spec_sink (GParamSpec *pspec)
215 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
217 G_LOCK (pspec_ref_count);
218 if (pspec->ref_count > 0)
220 if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
222 /* sync with _unref */
223 if (pspec->ref_count > 1)
224 pspec->ref_count -= 1;
227 G_UNLOCK (pspec_ref_count);
228 g_param_spec_unref (pspec);
233 G_UNLOCK (pspec_ref_count);
237 G_UNLOCK (pspec_ref_count);
238 g_return_if_fail (pspec->ref_count > 0);
242 G_CONST_RETURN gchar*
243 g_param_spec_get_name (GParamSpec *pspec)
245 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
250 G_CONST_RETURN gchar*
251 g_param_spec_get_nick (GParamSpec *pspec)
253 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
259 GParamSpec *redirect_target;
261 redirect_target = g_param_spec_get_redirect_target (pspec);
262 if (redirect_target && redirect_target->_nick)
263 return redirect_target->_nick;
269 G_CONST_RETURN gchar*
270 g_param_spec_get_blurb (GParamSpec *pspec)
272 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
275 return pspec->_blurb;
278 GParamSpec *redirect_target;
280 redirect_target = g_param_spec_get_redirect_target (pspec);
281 if (redirect_target && redirect_target->_blurb)
282 return redirect_target->_blurb;
289 canonicalize_key (gchar *key)
293 for (p = key; *p != 0; p++)
298 (c < '0' || c > '9') &&
299 (c < 'A' || c > 'Z') &&
300 (c < 'a' || c > 'z'))
306 is_canonical (gchar *key)
310 for (p = key; *p != 0; p++)
315 (c < '0' || c > '9') &&
316 (c < 'A' || c > 'Z') &&
317 (c < 'a' || c > 'z'))
325 g_param_spec_internal (GType param_type,
333 g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
334 g_return_val_if_fail (name != NULL, NULL);
335 g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
336 g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
338 pspec = (gpointer) g_type_create_instance (param_type);
340 if ((flags & G_PARAM_STATIC_NAME))
344 pspec->name = g_strdup (name);
345 canonicalize_key (pspec->name);
348 if (flags & G_PARAM_STATIC_NICK)
351 pspec->_nick = g_strdup (nick);
353 if (flags & G_PARAM_STATIC_BLURB)
354 pspec->_blurb = blurb;
356 pspec->_blurb = g_strdup (blurb);
358 pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
364 g_param_spec_get_qdata (GParamSpec *pspec,
367 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
369 return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
373 g_param_spec_set_qdata (GParamSpec *pspec,
377 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
378 g_return_if_fail (quark > 0);
380 g_datalist_id_set_data (&pspec->qdata, quark, data);
384 g_param_spec_set_qdata_full (GParamSpec *pspec,
387 GDestroyNotify destroy)
389 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
390 g_return_if_fail (quark > 0);
392 g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
396 g_param_spec_steal_qdata (GParamSpec *pspec,
399 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
400 g_return_val_if_fail (quark > 0, NULL);
402 return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
406 g_param_spec_get_redirect_target (GParamSpec *pspec)
408 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
410 if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
412 GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
414 return ospec->overridden;
421 g_param_value_set_default (GParamSpec *pspec,
424 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
425 g_return_if_fail (G_IS_VALUE (value));
426 g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
428 g_value_reset (value);
429 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
433 g_param_value_defaults (GParamSpec *pspec,
436 GValue dflt_value = { 0, };
439 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
440 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
441 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
443 g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
444 G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
445 defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
446 g_value_unset (&dflt_value);
452 g_param_value_validate (GParamSpec *pspec,
455 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
456 g_return_val_if_fail (G_IS_VALUE (value), FALSE);
457 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
459 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
461 GValue oval = *value;
463 if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
464 memcmp (&oval.data, &value->data, sizeof (oval.data)))
472 g_param_value_convert (GParamSpec *pspec,
473 const GValue *src_value,
475 gboolean strict_validation)
477 GValue tmp_value = { 0, };
479 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
480 g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
481 g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
482 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
484 /* better leave dest_value untouched when returning FALSE */
486 g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
487 if (g_value_transform (src_value, &tmp_value) &&
488 (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
490 g_value_unset (dest_value);
492 /* values are relocatable */
493 memcpy (dest_value, &tmp_value, sizeof (tmp_value));
499 g_value_unset (&tmp_value);
506 g_param_values_cmp (GParamSpec *pspec,
507 const GValue *value1,
508 const GValue *value2)
512 /* param_values_cmp() effectively does: value1 - value2
513 * so the return values are:
514 * -1) value1 < value2
515 * 0) value1 == value2
518 g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
519 g_return_val_if_fail (G_IS_VALUE (value1), 0);
520 g_return_val_if_fail (G_IS_VALUE (value2), 0);
521 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
522 g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
524 cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
526 return CLAMP (cmp, -1, 1);
530 value_param_init (GValue *value)
532 value->data[0].v_pointer = NULL;
536 value_param_free_value (GValue *value)
538 if (value->data[0].v_pointer)
539 g_param_spec_unref (value->data[0].v_pointer);
543 value_param_copy_value (const GValue *src_value,
546 if (src_value->data[0].v_pointer)
547 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
549 dest_value->data[0].v_pointer = NULL;
553 value_param_transform_value (const GValue *src_value,
556 if (src_value->data[0].v_pointer &&
557 g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
558 dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
560 dest_value->data[0].v_pointer = NULL;
564 value_param_peek_pointer (const GValue *value)
566 return value->data[0].v_pointer;
570 value_param_collect_value (GValue *value,
571 guint n_collect_values,
572 GTypeCValue *collect_values,
575 if (collect_values[0].v_pointer)
577 GParamSpec *param = collect_values[0].v_pointer;
579 if (param->g_type_instance.g_class == NULL)
580 return g_strconcat ("invalid unclassed param spec pointer for value type `",
581 G_VALUE_TYPE_NAME (value),
584 else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
585 return g_strconcat ("invalid param spec type `",
586 G_PARAM_SPEC_TYPE_NAME (param),
587 "' for value type `",
588 G_VALUE_TYPE_NAME (value),
591 value->data[0].v_pointer = g_param_spec_ref (param);
594 value->data[0].v_pointer = NULL;
600 value_param_lcopy_value (const GValue *value,
601 guint n_collect_values,
602 GTypeCValue *collect_values,
605 GParamSpec **param_p = collect_values[0].v_pointer;
608 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
610 if (!value->data[0].v_pointer)
612 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
613 *param_p = value->data[0].v_pointer;
615 *param_p = g_param_spec_ref (value->data[0].v_pointer);
621 /* --- param spec pool --- */
622 struct _GParamSpecPool
625 gboolean type_prefixing;
626 GHashTable *hash_table;
630 param_spec_pool_hash (gconstpointer key_spec)
632 const GParamSpec *key = key_spec;
634 guint h = key->owner_type;
636 for (p = key->name; *p; p++)
637 h = (h << 5) - h + *p;
643 param_spec_pool_equals (gconstpointer key_spec_1,
644 gconstpointer key_spec_2)
646 const GParamSpec *key1 = key_spec_1;
647 const GParamSpec *key2 = key_spec_2;
649 return (key1->owner_type == key2->owner_type &&
650 strcmp (key1->name, key2->name) == 0);
654 g_param_spec_pool_new (gboolean type_prefixing)
656 static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
657 GParamSpecPool *pool = g_new (GParamSpecPool, 1);
659 memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
660 pool->type_prefixing = type_prefixing != FALSE;
661 pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
667 g_param_spec_pool_insert (GParamSpecPool *pool,
673 if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
675 G_SLOCK (&pool->smutex);
676 for (p = pspec->name; *p; p++)
678 if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
680 g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
681 G_SUNLOCK (&pool->smutex);
686 pspec->owner_type = owner_type;
687 g_param_spec_ref (pspec);
688 g_hash_table_insert (pool->hash_table, pspec, pspec);
689 G_SUNLOCK (&pool->smutex);
693 g_return_if_fail (pool != NULL);
694 g_return_if_fail (pspec);
695 g_return_if_fail (owner_type > 0);
696 g_return_if_fail (pspec->owner_type == 0);
701 g_param_spec_pool_remove (GParamSpecPool *pool,
706 G_SLOCK (&pool->smutex);
707 if (g_hash_table_remove (pool->hash_table, pspec))
708 g_param_spec_unref (pspec);
710 g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
711 G_SUNLOCK (&pool->smutex);
715 g_return_if_fail (pool != NULL);
716 g_return_if_fail (pspec);
720 static inline GParamSpec*
721 param_spec_ht_lookup (GHashTable *hash_table,
722 const gchar *param_name,
724 gboolean walk_ancestors)
726 GParamSpec key, *pspec;
728 key.owner_type = owner_type;
729 key.name = (gchar*) param_name;
733 pspec = g_hash_table_lookup (hash_table, &key);
736 key.owner_type = g_type_parent (key.owner_type);
738 while (key.owner_type);
740 pspec = g_hash_table_lookup (hash_table, &key);
744 /* try canonicalized form */
745 key.name = g_strdup (param_name);
746 key.owner_type = owner_type;
748 canonicalize_key (key.name);
752 pspec = g_hash_table_lookup (hash_table, &key);
758 key.owner_type = g_type_parent (key.owner_type);
760 while (key.owner_type);
762 pspec = g_hash_table_lookup (hash_table, &key);
770 g_param_spec_pool_lookup (GParamSpecPool *pool,
771 const gchar *param_name,
773 gboolean walk_ancestors)
778 if (!pool || !param_name)
780 g_return_val_if_fail (pool != NULL, NULL);
781 g_return_val_if_fail (param_name != NULL, NULL);
784 G_SLOCK (&pool->smutex);
786 delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
788 /* try quick and away, i.e. without prefix */
791 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
792 G_SUNLOCK (&pool->smutex);
797 /* strip type prefix */
798 if (pool->type_prefixing && delim[1] == ':')
800 guint l = delim - param_name;
801 gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
804 strncpy (buffer, param_name, delim - param_name);
806 type = g_type_from_name (buffer);
809 if (type) /* type==0 isn't a valid type pefix */
811 /* sanity check, these cases don't make a whole lot of sense */
812 if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
814 G_SUNLOCK (&pool->smutex);
820 pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
821 G_SUNLOCK (&pool->smutex);
826 /* malformed param_name */
828 G_SUNLOCK (&pool->smutex);
834 pool_list (gpointer key,
838 GParamSpec *pspec = value;
839 gpointer *data = user_data;
840 GType owner_type = (GType) data[1];
842 if (owner_type == pspec->owner_type)
843 data[0] = g_list_prepend (data[0], pspec);
847 g_param_spec_pool_list_owned (GParamSpecPool *pool,
852 g_return_val_if_fail (pool != NULL, NULL);
853 g_return_val_if_fail (owner_type > 0, NULL);
855 G_SLOCK (&pool->smutex);
857 data[1] = (gpointer) owner_type;
858 g_hash_table_foreach (pool->hash_table, pool_list, &data);
859 G_SUNLOCK (&pool->smutex);
865 pspec_compare_id (gconstpointer a,
868 const GParamSpec *pspec1 = a, *pspec2 = b;
870 return pspec1->param_id < pspec2->param_id ? -1 : pspec1->param_id > pspec2->param_id;
873 static inline GSList*
874 pspec_list_remove_overridden_and_redirected (GSList *plist,
879 GSList *rlist = NULL;
883 GSList *tmp = plist->next;
884 GParamSpec *pspec = plist->data;
886 gboolean remove = FALSE;
888 /* Remove paramspecs that are redirected, and also paramspecs
889 * that have are overridden by non-redirected properties.
890 * The idea is to get the single paramspec for each name that
891 * best corresponds to what the application sees.
893 if (g_param_spec_get_redirect_target (pspec))
897 found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
900 GParamSpec *redirect = g_param_spec_get_redirect_target (found);
901 if (redirect != pspec)
908 g_slist_free_1 (plist);
922 pool_depth_list (gpointer key,
926 GParamSpec *pspec = value;
927 gpointer *data = user_data;
928 GSList **slists = data[0];
929 GType owner_type = (GType) data[1];
931 if (g_type_is_a (owner_type, pspec->owner_type))
933 if (G_TYPE_IS_INTERFACE (pspec->owner_type))
935 slists[0] = g_slist_prepend (slists[0], pspec);
939 guint d = g_type_depth (pspec->owner_type);
941 slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
946 /* We handle interfaces specially since we don't want to
947 * count interface prerequsites like normal inheritance;
948 * the property comes from the direct inheritance from
949 * the prerequisite class, not from the interface that
952 * also 'depth' isn't a meaningful concept for interface
956 pool_depth_list_for_interface (gpointer key,
960 GParamSpec *pspec = value;
961 gpointer *data = user_data;
962 GSList **slists = data[0];
963 GType owner_type = (GType) data[1];
965 if (pspec->owner_type == owner_type)
966 slists[0] = g_slist_prepend (slists[0], pspec);
969 GParamSpec** /* free result */
970 g_param_spec_pool_list (GParamSpecPool *pool,
974 GParamSpec **pspecs, **p;
975 GSList **slists, *node;
979 g_return_val_if_fail (pool != NULL, NULL);
980 g_return_val_if_fail (owner_type > 0, NULL);
981 g_return_val_if_fail (n_pspecs_p != NULL, NULL);
983 G_SLOCK (&pool->smutex);
985 d = g_type_depth (owner_type);
986 slists = g_new0 (GSList*, d);
988 data[1] = (gpointer) owner_type;
990 g_hash_table_foreach (pool->hash_table,
991 G_TYPE_IS_INTERFACE (owner_type) ?
992 pool_depth_list_for_interface :
996 for (i = 0; i < d; i++)
997 slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
998 pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1000 for (i = 0; i < d; i++)
1002 slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1003 for (node = slists[i]; node; node = node->next)
1005 g_slist_free (slists[i]);
1009 G_SUNLOCK (&pool->smutex);
1015 /* --- auxillary functions --- */
1020 void (*finalize) (GParamSpec *pspec);
1021 void (*value_set_default) (GParamSpec *pspec,
1023 gboolean (*value_validate) (GParamSpec *pspec,
1025 gint (*values_cmp) (GParamSpec *pspec,
1026 const GValue *value1,
1027 const GValue *value2);
1028 } ParamSpecClassInfo;
1031 param_spec_generic_class_init (gpointer g_class,
1032 gpointer class_data)
1034 GParamSpecClass *class = g_class;
1035 ParamSpecClassInfo *info = class_data;
1037 class->value_type = info->value_type;
1039 class->finalize = info->finalize; /* optional */
1040 class->value_set_default = info->value_set_default;
1041 if (info->value_validate)
1042 class->value_validate = info->value_validate; /* optional */
1043 class->values_cmp = info->values_cmp;
1044 g_free (class_data);
1048 default_value_set_default (GParamSpec *pspec,
1051 /* value is already zero initialized */
1055 default_values_cmp (GParamSpec *pspec,
1056 const GValue *value1,
1057 const GValue *value2)
1059 return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1063 g_param_type_register_static (const gchar *name,
1064 const GParamSpecTypeInfo *pspec_info)
1067 sizeof (GParamSpecClass), /* class_size */
1068 NULL, /* base_init */
1069 NULL, /* base_destroy */
1070 param_spec_generic_class_init, /* class_init */
1071 NULL, /* class_destroy */
1072 NULL, /* class_data */
1073 0, /* instance_size */
1074 16, /* n_preallocs */
1075 NULL, /* instance_init */
1077 ParamSpecClassInfo *cinfo;
1079 g_return_val_if_fail (name != NULL, 0);
1080 g_return_val_if_fail (pspec_info != NULL, 0);
1081 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1082 g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1083 g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1084 /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1085 /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1086 /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1088 info.instance_size = pspec_info->instance_size;
1089 info.n_preallocs = pspec_info->n_preallocs;
1090 info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1091 cinfo = g_new (ParamSpecClassInfo, 1);
1092 cinfo->value_type = pspec_info->value_type;
1093 cinfo->finalize = pspec_info->finalize;
1094 cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1095 cinfo->value_validate = pspec_info->value_validate;
1096 cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1097 info.class_data = cinfo;
1099 return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1103 g_value_set_param (GValue *value,
1106 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1108 g_return_if_fail (G_IS_PARAM_SPEC (param));
1110 if (value->data[0].v_pointer)
1111 g_param_spec_unref (value->data[0].v_pointer);
1112 value->data[0].v_pointer = param;
1113 if (value->data[0].v_pointer)
1114 g_param_spec_ref (value->data[0].v_pointer);
1118 g_value_set_param_take_ownership (GValue *value,
1121 g_value_take_param (value, param);
1125 g_value_take_param (GValue *value,
1128 g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1130 g_return_if_fail (G_IS_PARAM_SPEC (param));
1132 if (value->data[0].v_pointer)
1133 g_param_spec_unref (value->data[0].v_pointer);
1134 value->data[0].v_pointer = param; /* we take over the reference count */
1138 g_value_get_param (const GValue *value)
1140 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1142 return value->data[0].v_pointer;
1146 g_value_dup_param (const GValue *value)
1148 g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1150 return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1153 #define __G_PARAM_C__
1154 #include "gobjectaliasdef.c"