Add (allow-none) annotation for GValue setters.
[platform/upstream/glib.git] / gobject / gparam.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1997-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
21  * MT safe
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27
28 #include "gparam.h"
29 #include "gparamspecs.h"
30 #include "gvaluecollector.h"
31
32
33 /**
34  * SECTION:gparamspec
35  * @short_description: Metadata for parameter specifications
36  * @see_also: g_object_class_install_property(), g_object_set(),
37  *     g_object_get(), g_object_set_property(), g_object_get_property(),
38  *     g_value_register_transform_func()
39  * @title: GParamSpec
40  *
41  * #GParamSpec is an object structure that encapsulates the metadata
42  * required to specify parameters, such as e.g. #GObject properties.
43  *
44  * <para id="canonical-parameter-name">
45  * Parameter names need to start with a letter (a-z or A-Z). Subsequent
46  * characters can be letters, numbers or a '-'.
47  * All other characters are replaced by a '-' during construction.
48  * The result of this replacement is called the canonical name of the
49  * parameter.
50  * </para>
51  */
52
53
54 /* --- defines --- */
55 #define PARAM_FLOATING_FLAG                     0x2
56 #define G_PARAM_USER_MASK                       (~0 << G_PARAM_USER_SHIFT)
57 #define PSPEC_APPLIES_TO_VALUE(pspec, value)    (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
58 #define G_SLOCK(mutex)                          g_static_mutex_lock (mutex)
59 #define G_SUNLOCK(mutex)                        g_static_mutex_unlock (mutex)
60
61
62 /* --- prototypes --- */
63 static void     g_param_spec_class_base_init     (GParamSpecClass       *class);
64 static void     g_param_spec_class_base_finalize (GParamSpecClass       *class);
65 static void     g_param_spec_class_init          (GParamSpecClass       *class,
66                                                   gpointer               class_data);
67 static void     g_param_spec_init                (GParamSpec            *pspec,
68                                                   GParamSpecClass       *class);
69 static void     g_param_spec_finalize            (GParamSpec            *pspec);
70 static void     value_param_init                (GValue         *value);
71 static void     value_param_free_value          (GValue         *value);
72 static void     value_param_copy_value          (const GValue   *src_value,
73                                                  GValue         *dest_value);
74 static void     value_param_transform_value     (const GValue   *src_value,
75                                                  GValue         *dest_value);
76 static gpointer value_param_peek_pointer        (const GValue   *value);
77 static gchar*   value_param_collect_value       (GValue         *value,
78                                                  guint           n_collect_values,
79                                                  GTypeCValue    *collect_values,
80                                                  guint           collect_flags);
81 static gchar*   value_param_lcopy_value         (const GValue   *value,
82                                                  guint           n_collect_values,
83                                                  GTypeCValue    *collect_values,
84                                                  guint           collect_flags);
85
86
87 /* --- functions --- */
88 void
89 _g_param_type_init (void)
90 {
91   static const GTypeFundamentalInfo finfo = {
92     (G_TYPE_FLAG_CLASSED |
93      G_TYPE_FLAG_INSTANTIATABLE |
94      G_TYPE_FLAG_DERIVABLE |
95      G_TYPE_FLAG_DEEP_DERIVABLE),
96   };
97   static const GTypeValueTable param_value_table = {
98     value_param_init,           /* value_init */
99     value_param_free_value,     /* value_free */
100     value_param_copy_value,     /* value_copy */
101     value_param_peek_pointer,   /* value_peek_pointer */
102     "p",                        /* collect_format */
103     value_param_collect_value,  /* collect_value */
104     "p",                        /* lcopy_format */
105     value_param_lcopy_value,    /* lcopy_value */
106   };
107   static const GTypeInfo param_spec_info = {
108     sizeof (GParamSpecClass),
109
110     (GBaseInitFunc) g_param_spec_class_base_init,
111     (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
112     (GClassInitFunc) g_param_spec_class_init,
113     (GClassFinalizeFunc) NULL,
114     NULL,       /* class_data */
115
116     sizeof (GParamSpec),
117     0,          /* n_preallocs */
118     (GInstanceInitFunc) g_param_spec_init,
119
120     &param_value_table,
121   };
122   GType type;
123
124   /* This should be registred as GParamSpec instead of GParam, for
125    * consistency sake, so that type name can be mapped to struct name,
126    * However, some language bindings, most noticable the python ones
127    * depends on the "GParam" identifier, see #548689
128    */
129   type = g_type_register_fundamental (G_TYPE_PARAM, g_intern_static_string ("GParam"), &param_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
130   g_assert (type == G_TYPE_PARAM);
131   g_value_register_transform_func (G_TYPE_PARAM, G_TYPE_PARAM, value_param_transform_value);
132 }
133
134 static void
135 g_param_spec_class_base_init (GParamSpecClass *class)
136 {
137 }
138
139 static void
140 g_param_spec_class_base_finalize (GParamSpecClass *class)
141 {
142 }
143
144 static void
145 g_param_spec_class_init (GParamSpecClass *class,
146                          gpointer         class_data)
147 {
148   class->value_type = G_TYPE_NONE;
149   class->finalize = g_param_spec_finalize;
150   class->value_set_default = NULL;
151   class->value_validate = NULL;
152   class->values_cmp = NULL;
153 }
154
155 static void
156 g_param_spec_init (GParamSpec      *pspec,
157                    GParamSpecClass *class)
158 {
159   pspec->name = NULL;
160   pspec->_nick = NULL;
161   pspec->_blurb = NULL;
162   pspec->flags = 0;
163   pspec->value_type = class->value_type;
164   pspec->owner_type = 0;
165   pspec->qdata = NULL;
166   g_datalist_set_flags (&pspec->qdata, PARAM_FLOATING_FLAG);
167   pspec->ref_count = 1;
168   pspec->param_id = 0;
169 }
170
171 static void
172 g_param_spec_finalize (GParamSpec *pspec)
173 {
174   g_datalist_clear (&pspec->qdata);
175
176   if (!(pspec->flags & G_PARAM_STATIC_NICK))
177     g_free (pspec->_nick);
178
179   if (!(pspec->flags & G_PARAM_STATIC_BLURB))
180     g_free (pspec->_blurb);
181
182   g_type_free_instance ((GTypeInstance*) pspec);
183 }
184
185 /**
186  * g_param_spec_ref: (skip)
187  * @pspec: a valid #GParamSpec
188  *
189  * Increments the reference count of @pspec.
190  *
191  * Returns: the #GParamSpec that was passed into this function
192  */
193 GParamSpec*
194 g_param_spec_ref (GParamSpec *pspec)
195 {
196   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
197
198   g_atomic_int_inc ((int *)&pspec->ref_count);
199
200   return pspec;
201 }
202
203 /**
204  * g_param_spec_unref: (skip)
205  * @pspec: a valid #GParamSpec
206  *
207  * Decrements the reference count of a @pspec.
208  */
209 void
210 g_param_spec_unref (GParamSpec *pspec)
211 {
212   gboolean is_zero;
213
214   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
215
216   is_zero = g_atomic_int_dec_and_test ((int *)&pspec->ref_count);
217
218   if (G_UNLIKELY (is_zero))
219     {
220       G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
221     }
222 }
223
224 /**
225  * g_param_spec_sink:
226  * @pspec: a valid #GParamSpec
227  *
228  * The initial reference count of a newly created #GParamSpec is 1,
229  * even though no one has explicitly called g_param_spec_ref() on it
230  * yet. So the initial reference count is flagged as "floating", until
231  * someone calls <literal>g_param_spec_ref (pspec); g_param_spec_sink
232  * (pspec);</literal> in sequence on it, taking over the initial
233  * reference count (thus ending up with a @pspec that has a reference
234  * count of 1 still, but is not flagged "floating" anymore).
235  */
236 void
237 g_param_spec_sink (GParamSpec *pspec)
238 {
239   gsize oldvalue;
240   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
241
242   oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
243   if (oldvalue & PARAM_FLOATING_FLAG)
244     g_param_spec_unref (pspec);
245 }
246
247 /**
248  * g_param_spec_ref_sink: (skip)
249  * @pspec: a valid #GParamSpec
250  *
251  * Convenience function to ref and sink a #GParamSpec.
252  *
253  * Since: 2.10
254  * Returns: the #GParamSpec that was passed into this function
255  */
256 GParamSpec*
257 g_param_spec_ref_sink (GParamSpec *pspec)
258 {
259   gsize oldvalue;
260   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
261
262   oldvalue = g_atomic_pointer_and (&pspec->qdata, ~(gsize)PARAM_FLOATING_FLAG);
263   if (!(oldvalue & PARAM_FLOATING_FLAG))
264     g_param_spec_ref (pspec);
265
266   return pspec;
267 }
268
269 /**
270  * g_param_spec_get_name:
271  * @pspec: a valid #GParamSpec
272  *
273  * Get the name of a #GParamSpec.
274  *
275  * The name is always an "interned" string (as per g_intern_string()).
276  * This allows for pointer-value comparisons.
277  *
278  * Returns: the name of @pspec.
279  */
280 const gchar *
281 g_param_spec_get_name (GParamSpec *pspec)
282 {
283   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
284
285   return pspec->name;
286 }
287
288 /**
289  * g_param_spec_get_nick:
290  * @pspec: a valid #GParamSpec
291  *
292  * Get the nickname of a #GParamSpec.
293  *
294  * Returns: the nickname of @pspec.
295  */
296 const gchar *
297 g_param_spec_get_nick (GParamSpec *pspec)
298 {
299   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
300
301   if (pspec->_nick)
302     return pspec->_nick;
303   else
304     {
305       GParamSpec *redirect_target;
306
307       redirect_target = g_param_spec_get_redirect_target (pspec);
308       if (redirect_target && redirect_target->_nick)
309         return redirect_target->_nick;
310     }
311
312   return pspec->name;
313 }
314
315 /**
316  * g_param_spec_get_blurb:
317  * @pspec: a valid #GParamSpec
318  *
319  * Get the short description of a #GParamSpec.
320  *
321  * Returns: the short description of @pspec.
322  */
323 const gchar *
324 g_param_spec_get_blurb (GParamSpec *pspec)
325 {
326   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
327
328   if (pspec->_blurb)
329     return pspec->_blurb;
330   else
331     {
332       GParamSpec *redirect_target;
333
334       redirect_target = g_param_spec_get_redirect_target (pspec);
335       if (redirect_target && redirect_target->_blurb)
336         return redirect_target->_blurb;
337     }
338
339   return NULL;
340 }
341
342 static void
343 canonicalize_key (gchar *key)
344 {
345   gchar *p;
346   
347   for (p = key; *p != 0; p++)
348     {
349       gchar c = *p;
350       
351       if (c != '-' &&
352           (c < '0' || c > '9') &&
353           (c < 'A' || c > 'Z') &&
354           (c < 'a' || c > 'z'))
355         *p = '-';
356     }
357 }
358
359 static gboolean
360 is_canonical (const gchar *key)
361 {
362   const gchar *p;
363
364   for (p = key; *p != 0; p++)
365     {
366       gchar c = *p;
367       
368       if (c != '-' &&
369           (c < '0' || c > '9') &&
370           (c < 'A' || c > 'Z') &&
371           (c < 'a' || c > 'z'))
372         return FALSE;
373     }
374
375   return TRUE;
376 }
377
378 /**
379  * g_param_spec_internal: (skip)
380  * @param_type: the #GType for the property; must be derived from #G_TYPE_PARAM
381  * @name: the canonical name of the property
382  * @nick: the nickname of the property
383  * @blurb: a short description of the property
384  * @flags: a combination of #GParamFlags
385  *
386  * Creates a new #GParamSpec instance.
387  *
388  * A property name consists of segments consisting of ASCII letters and
389  * digits, separated by either the '-' or '_' character. The first
390  * character of a property name must be a letter. Names which violate these
391  * rules lead to undefined behaviour.
392  *
393  * When creating and looking up a #GParamSpec, either separator can be
394  * used, but they cannot be mixed. Using '-' is considerably more
395  * efficient and in fact required when using property names as detail
396  * strings for signals.
397  *
398  * Beyond the name, #GParamSpec<!-- -->s have two more descriptive
399  * strings associated with them, the @nick, which should be suitable
400  * for use as a label for the property in a property editor, and the
401  * @blurb, which should be a somewhat longer description, suitable for
402  * e.g. a tooltip. The @nick and @blurb should ideally be localized.
403  *
404  * Returns: a newly allocated #GParamSpec instance
405  */
406 gpointer
407 g_param_spec_internal (GType        param_type,
408                        const gchar *name,
409                        const gchar *nick,
410                        const gchar *blurb,
411                        GParamFlags  flags)
412 {
413   GParamSpec *pspec;
414   
415   g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
416   g_return_val_if_fail (name != NULL, NULL);
417   g_return_val_if_fail ((name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z'), NULL);
418   g_return_val_if_fail (!(flags & G_PARAM_STATIC_NAME) || is_canonical (name), NULL);
419   
420   pspec = (gpointer) g_type_create_instance (param_type);
421
422   if (flags & G_PARAM_STATIC_NAME)
423     {
424       /* pspec->name is not freed if (flags & G_PARAM_STATIC_NAME) */
425       pspec->name = (gchar *) g_intern_static_string (name);
426       if (!is_canonical (pspec->name))
427         g_warning ("G_PARAM_STATIC_NAME used with non-canonical pspec name: %s", pspec->name);
428     }
429   else
430     {
431       if (is_canonical (name))
432         pspec->name = (gchar *) g_intern_string (name);
433       else
434         {
435           gchar *tmp = g_strdup (name);
436           canonicalize_key (tmp);
437           pspec->name = (gchar *) g_intern_string (tmp);
438           g_free (tmp);
439         }
440     }
441
442   if (flags & G_PARAM_STATIC_NICK)
443     pspec->_nick = (gchar*) nick;
444   else
445     pspec->_nick = g_strdup (nick);
446
447   if (flags & G_PARAM_STATIC_BLURB)
448     pspec->_blurb = (gchar*) blurb;
449   else
450     pspec->_blurb = g_strdup (blurb);
451
452   pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
453   
454   return pspec;
455 }
456
457 /**
458  * g_param_spec_get_qdata:
459  * @pspec: a valid #GParamSpec
460  * @quark: a #GQuark, naming the user data pointer
461  *
462  * Gets back user data pointers stored via g_param_spec_set_qdata().
463  *
464  * Returns: (transfer none): the user data pointer set, or %NULL
465  */
466 gpointer
467 g_param_spec_get_qdata (GParamSpec *pspec,
468                         GQuark      quark)
469 {
470   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
471   
472   return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
473 }
474
475 /**
476  * g_param_spec_set_qdata:
477  * @pspec: the #GParamSpec to set store a user data pointer
478  * @quark: a #GQuark, naming the user data pointer
479  * @data: an opaque user data pointer
480  *
481  * Sets an opaque, named pointer on a #GParamSpec. The name is
482  * specified through a #GQuark (retrieved e.g. via
483  * g_quark_from_static_string()), and the pointer can be gotten back
484  * from the @pspec with g_param_spec_get_qdata().  Setting a
485  * previously set user data pointer, overrides (frees) the old pointer
486  * set, using %NULL as pointer essentially removes the data stored.
487  */
488 void
489 g_param_spec_set_qdata (GParamSpec *pspec,
490                         GQuark      quark,
491                         gpointer    data)
492 {
493   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
494   g_return_if_fail (quark > 0);
495
496   g_datalist_id_set_data (&pspec->qdata, quark, data);
497 }
498
499 /**
500  * g_param_spec_set_qdata_full: (skip)
501  * @pspec: the #GParamSpec to set store a user data pointer
502  * @quark: a #GQuark, naming the user data pointer
503  * @data: an opaque user data pointer
504  * @destroy: function to invoke with @data as argument, when @data needs to
505  *  be freed
506  *
507  * This function works like g_param_spec_set_qdata(), but in addition,
508  * a <literal>void (*destroy) (gpointer)</literal> function may be
509  * specified which is called with @data as argument when the @pspec is
510  * finalized, or the data is being overwritten by a call to
511  * g_param_spec_set_qdata() with the same @quark.
512  */
513 void
514 g_param_spec_set_qdata_full (GParamSpec    *pspec,
515                              GQuark         quark,
516                              gpointer       data,
517                              GDestroyNotify destroy)
518 {
519   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
520   g_return_if_fail (quark > 0);
521
522   g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
523 }
524
525 /**
526  * g_param_spec_steal_qdata:
527  * @pspec: the #GParamSpec to get a stored user data pointer from
528  * @quark: a #GQuark, naming the user data pointer
529  *
530  * Gets back user data pointers stored via g_param_spec_set_qdata()
531  * and removes the @data from @pspec without invoking its destroy()
532  * function (if any was set).  Usually, calling this function is only
533  * required to update user data pointers with a destroy notifier.
534  *
535  * Returns: (transfer none): the user data pointer set, or %NULL
536  */
537 gpointer
538 g_param_spec_steal_qdata (GParamSpec *pspec,
539                           GQuark      quark)
540 {
541   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
542   g_return_val_if_fail (quark > 0, NULL);
543   
544   return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
545 }
546
547 /**
548  * g_param_spec_get_redirect_target:
549  * @pspec: a #GParamSpec
550  *
551  * If the paramspec redirects operations to another paramspec,
552  * returns that paramspec. Redirect is used typically for
553  * providing a new implementation of a property in a derived
554  * type while preserving all the properties from the parent
555  * type. Redirection is established by creating a property
556  * of type #GParamSpecOverride. See g_object_class_override_property()
557  * for an example of the use of this capability.
558  *
559  * Since: 2.4
560  *
561  * Returns: (transfer none): paramspec to which requests on this
562  *          paramspec should be redirected, or %NULL if none.
563  */
564 GParamSpec*
565 g_param_spec_get_redirect_target (GParamSpec *pspec)
566 {
567   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
568
569   if (G_IS_PARAM_SPEC_OVERRIDE (pspec))
570     {
571       GParamSpecOverride *ospec = G_PARAM_SPEC_OVERRIDE (pspec);
572
573       return ospec->overridden;
574     }
575   else
576     return NULL;
577 }
578
579 /**
580  * g_param_value_set_default:
581  * @pspec: a valid #GParamSpec
582  * @value: a #GValue of correct type for @pspec
583  *
584  * Sets @value to its default value as specified in @pspec.
585  */
586 void
587 g_param_value_set_default (GParamSpec *pspec,
588                            GValue     *value)
589 {
590   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
591   g_return_if_fail (G_IS_VALUE (value));
592   g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
593
594   g_value_reset (value);
595   G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
596 }
597
598 /**
599  * g_param_value_defaults:
600  * @pspec: a valid #GParamSpec
601  * @value: a #GValue of correct type for @pspec
602  *
603  * Checks whether @value contains the default value as specified in @pspec.
604  *
605  * Returns: whether @value contains the canonical default for this @pspec
606  */
607 gboolean
608 g_param_value_defaults (GParamSpec *pspec,
609                         GValue     *value)
610 {
611   GValue dflt_value = { 0, };
612   gboolean defaults;
613
614   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
615   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
616   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
617
618   g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
619   G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
620   defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
621   g_value_unset (&dflt_value);
622
623   return defaults;
624 }
625
626 /**
627  * g_param_value_validate:
628  * @pspec: a valid #GParamSpec
629  * @value: a #GValue of correct type for @pspec
630  *
631  * Ensures that the contents of @value comply with the specifications
632  * set out by @pspec. For example, a #GParamSpecInt might require
633  * that integers stored in @value may not be smaller than -42 and not be
634  * greater than +42. If @value contains an integer outside of this range,
635  * it is modified accordingly, so the resulting value will fit into the
636  * range -42 .. +42.
637  *
638  * Returns: whether modifying @value was necessary to ensure validity
639  */
640 gboolean
641 g_param_value_validate (GParamSpec *pspec,
642                         GValue     *value)
643 {
644   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
645   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
646   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
647
648   if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
649     {
650       GValue oval = *value;
651
652       if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
653           memcmp (&oval.data, &value->data, sizeof (oval.data)))
654         return TRUE;
655     }
656
657   return FALSE;
658 }
659
660 /**
661  * g_param_value_convert:
662  * @pspec: a valid #GParamSpec
663  * @src_value: souce #GValue
664  * @dest_value: destination #GValue of correct type for @pspec
665  * @strict_validation: %TRUE requires @dest_value to conform to @pspec
666  * without modifications
667  *
668  * Transforms @src_value into @dest_value if possible, and then
669  * validates @dest_value, in order for it to conform to @pspec.  If
670  * @strict_validation is %TRUE this function will only succeed if the
671  * transformed @dest_value complied to @pspec without modifications.
672  *
673  * See also g_value_type_transformable(), g_value_transform() and
674  * g_param_value_validate().
675  *
676  * Returns: %TRUE if transformation and validation were successful,
677  *  %FALSE otherwise and @dest_value is left untouched.
678  */
679 gboolean
680 g_param_value_convert (GParamSpec   *pspec,
681                        const GValue *src_value,
682                        GValue       *dest_value,
683                        gboolean      strict_validation)
684 {
685   GValue tmp_value = { 0, };
686
687   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
688   g_return_val_if_fail (G_IS_VALUE (src_value), FALSE);
689   g_return_val_if_fail (G_IS_VALUE (dest_value), FALSE);
690   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, dest_value), FALSE);
691
692   /* better leave dest_value untouched when returning FALSE */
693
694   g_value_init (&tmp_value, G_VALUE_TYPE (dest_value));
695   if (g_value_transform (src_value, &tmp_value) &&
696       (!g_param_value_validate (pspec, &tmp_value) || !strict_validation))
697     {
698       g_value_unset (dest_value);
699       
700       /* values are relocatable */
701       memcpy (dest_value, &tmp_value, sizeof (tmp_value));
702       
703       return TRUE;
704     }
705   else
706     {
707       g_value_unset (&tmp_value);
708       
709       return FALSE;
710     }
711 }
712
713 /**
714  * g_param_values_cmp:
715  * @pspec: a valid #GParamSpec
716  * @value1: a #GValue of correct type for @pspec
717  * @value2: a #GValue of correct type for @pspec
718  *
719  * Compares @value1 with @value2 according to @pspec, and return -1, 0 or +1,
720  * if @value1 is found to be less than, equal to or greater than @value2,
721  * respectively.
722  *
723  * Returns: -1, 0 or +1, for a less than, equal to or greater than result
724  */
725 gint
726 g_param_values_cmp (GParamSpec   *pspec,
727                     const GValue *value1,
728                     const GValue *value2)
729 {
730   gint cmp;
731
732   /* param_values_cmp() effectively does: value1 - value2
733    * so the return values are:
734    * -1)  value1 < value2
735    *  0)  value1 == value2
736    *  1)  value1 > value2
737    */
738   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
739   g_return_val_if_fail (G_IS_VALUE (value1), 0);
740   g_return_val_if_fail (G_IS_VALUE (value2), 0);
741   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
742   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
743
744   cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
745
746   return CLAMP (cmp, -1, 1);
747 }
748
749 static void
750 value_param_init (GValue *value)
751 {
752   value->data[0].v_pointer = NULL;
753 }
754
755 static void
756 value_param_free_value (GValue *value)
757 {
758   if (value->data[0].v_pointer)
759     g_param_spec_unref (value->data[0].v_pointer);
760 }
761
762 static void
763 value_param_copy_value (const GValue *src_value,
764                         GValue       *dest_value)
765 {
766   if (src_value->data[0].v_pointer)
767     dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
768   else
769     dest_value->data[0].v_pointer = NULL;
770 }
771
772 static void
773 value_param_transform_value (const GValue *src_value,
774                              GValue       *dest_value)
775 {
776   if (src_value->data[0].v_pointer &&
777       g_type_is_a (G_PARAM_SPEC_TYPE (dest_value->data[0].v_pointer), G_VALUE_TYPE (dest_value)))
778     dest_value->data[0].v_pointer = g_param_spec_ref (src_value->data[0].v_pointer);
779   else
780     dest_value->data[0].v_pointer = NULL;
781 }
782
783 static gpointer
784 value_param_peek_pointer (const GValue *value)
785 {
786   return value->data[0].v_pointer;
787 }
788
789 static gchar*
790 value_param_collect_value (GValue      *value,
791                            guint        n_collect_values,
792                            GTypeCValue *collect_values,
793                            guint        collect_flags)
794 {
795   if (collect_values[0].v_pointer)
796     {
797       GParamSpec *param = collect_values[0].v_pointer;
798
799       if (param->g_type_instance.g_class == NULL)
800         return g_strconcat ("invalid unclassed param spec pointer for value type `",
801                             G_VALUE_TYPE_NAME (value),
802                             "'",
803                             NULL);
804       else if (!g_value_type_compatible (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
805         return g_strconcat ("invalid param spec type `",
806                             G_PARAM_SPEC_TYPE_NAME (param),
807                             "' for value type `",
808                             G_VALUE_TYPE_NAME (value),
809                             "'",
810                             NULL);
811       value->data[0].v_pointer = g_param_spec_ref (param);
812     }
813   else
814     value->data[0].v_pointer = NULL;
815
816   return NULL;
817 }
818
819 static gchar*
820 value_param_lcopy_value (const GValue *value,
821                          guint         n_collect_values,
822                          GTypeCValue  *collect_values,
823                          guint         collect_flags)
824 {
825   GParamSpec **param_p = collect_values[0].v_pointer;
826
827   if (!param_p)
828     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
829
830   if (!value->data[0].v_pointer)
831     *param_p = NULL;
832   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
833     *param_p = value->data[0].v_pointer;
834   else
835     *param_p = g_param_spec_ref (value->data[0].v_pointer);
836
837   return NULL;
838 }
839
840
841 /* --- param spec pool --- */
842 /**
843  * GParamSpecPool:
844  *
845  * A #GParamSpecPool maintains a collection of #GParamSpec<!-- -->s which can be
846  * quickly accessed by owner and name. The implementation of the #GObject property
847  * system uses such a pool to store the #GParamSpecs of the properties all object
848  * types.
849  */
850 struct _GParamSpecPool
851 {
852   GStaticMutex smutex;
853   gboolean     type_prefixing;
854   GHashTable  *hash_table;
855 };
856
857 static guint
858 param_spec_pool_hash (gconstpointer key_spec)
859 {
860   const GParamSpec *key = key_spec;
861   const gchar *p;
862   guint h = key->owner_type;
863
864   for (p = key->name; *p; p++)
865     h = (h << 5) - h + *p;
866
867   return h;
868 }
869
870 static gboolean
871 param_spec_pool_equals (gconstpointer key_spec_1,
872                         gconstpointer key_spec_2)
873 {
874   const GParamSpec *key1 = key_spec_1;
875   const GParamSpec *key2 = key_spec_2;
876
877   return (key1->owner_type == key2->owner_type &&
878           strcmp (key1->name, key2->name) == 0);
879 }
880
881 /**
882  * g_param_spec_pool_new:
883  * @type_prefixing: Whether the pool will support type-prefixed property names.
884  *
885  * Creates a new #GParamSpecPool.
886  *
887  * If @type_prefixing is %TRUE, lookups in the newly created pool will
888  * allow to specify the owner as a colon-separated prefix of the
889  * property name, like "GtkContainer:border-width". This feature is
890  * deprecated, so you should always set @type_prefixing to %FALSE.
891  *
892  * Returns: (transfer none): a newly allocated #GParamSpecPool.
893  */
894 GParamSpecPool*
895 g_param_spec_pool_new (gboolean type_prefixing)
896 {
897   static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
898   GParamSpecPool *pool = g_new (GParamSpecPool, 1);
899
900   memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
901   pool->type_prefixing = type_prefixing != FALSE;
902   pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
903
904   return pool;
905 }
906
907 /**
908  * g_param_spec_pool_insert:
909  * @pool: a #GParamSpecPool.
910  * @pspec: the #GParamSpec to insert
911  * @owner_type: a #GType identifying the owner of @pspec
912  *
913  * Inserts a #GParamSpec in the pool.
914  */
915 void
916 g_param_spec_pool_insert (GParamSpecPool *pool,
917                           GParamSpec     *pspec,
918                           GType           owner_type)
919 {
920   const gchar *p;
921   
922   if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
923     {
924       for (p = pspec->name; *p; p++)
925         {
926           if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
927             {
928               g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
929               return;
930             }
931         }
932       G_SLOCK (&pool->smutex);
933       pspec->owner_type = owner_type;
934       g_param_spec_ref (pspec);
935       g_hash_table_insert (pool->hash_table, pspec, pspec);
936       G_SUNLOCK (&pool->smutex);
937     }
938   else
939     {
940       g_return_if_fail (pool != NULL);
941       g_return_if_fail (pspec);
942       g_return_if_fail (owner_type > 0);
943       g_return_if_fail (pspec->owner_type == 0);
944     }
945 }
946
947 /**
948  * g_param_spec_pool_remove:
949  * @pool: a #GParamSpecPool
950  * @pspec: the #GParamSpec to remove
951  *
952  * Removes a #GParamSpec from the pool.
953  */
954 void
955 g_param_spec_pool_remove (GParamSpecPool *pool,
956                           GParamSpec     *pspec)
957 {
958   if (pool && pspec)
959     {
960       G_SLOCK (&pool->smutex);
961       if (g_hash_table_remove (pool->hash_table, pspec))
962         g_param_spec_unref (pspec);
963       else
964         g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
965       G_SUNLOCK (&pool->smutex);
966     }
967   else
968     {
969       g_return_if_fail (pool != NULL);
970       g_return_if_fail (pspec);
971     }
972 }
973
974 static inline GParamSpec*
975 param_spec_ht_lookup (GHashTable  *hash_table,
976                       const gchar *param_name,
977                       GType        owner_type,
978                       gboolean     walk_ancestors)
979 {
980   GParamSpec key, *pspec;
981
982   key.owner_type = owner_type;
983   key.name = (gchar*) param_name;
984   if (walk_ancestors)
985     do
986       {
987         pspec = g_hash_table_lookup (hash_table, &key);
988         if (pspec)
989           return pspec;
990         key.owner_type = g_type_parent (key.owner_type);
991       }
992     while (key.owner_type);
993   else
994     pspec = g_hash_table_lookup (hash_table, &key);
995
996   if (!pspec && !is_canonical (param_name))
997     {
998       gchar *canonical;
999
1000       canonical = g_strdup (key.name);
1001       canonicalize_key (canonical);
1002
1003       /* try canonicalized form */
1004       key.name = canonical;
1005       key.owner_type = owner_type;
1006
1007       if (walk_ancestors)
1008         do
1009           {
1010             pspec = g_hash_table_lookup (hash_table, &key);
1011             if (pspec)
1012               {
1013                 g_free (canonical);
1014                 return pspec;
1015               }
1016             key.owner_type = g_type_parent (key.owner_type);
1017           }
1018         while (key.owner_type);
1019       else
1020         pspec = g_hash_table_lookup (hash_table, &key);
1021
1022       g_free (canonical);
1023     }
1024
1025   return pspec;
1026 }
1027
1028 /**
1029  * g_param_spec_pool_lookup:
1030  * @pool: a #GParamSpecPool
1031  * @param_name: the name to look for
1032  * @owner_type: the owner to look for
1033  * @walk_ancestors: If %TRUE, also try to find a #GParamSpec with @param_name
1034  *  owned by an ancestor of @owner_type.
1035  *
1036  * Looks up a #GParamSpec in the pool.
1037  *
1038  * Returns: (transfer none): The found #GParamSpec, or %NULL if no
1039  * matching #GParamSpec was found.
1040  */
1041 GParamSpec*
1042 g_param_spec_pool_lookup (GParamSpecPool *pool,
1043                           const gchar    *param_name,
1044                           GType           owner_type,
1045                           gboolean        walk_ancestors)
1046 {
1047   GParamSpec *pspec;
1048   gchar *delim;
1049
1050   if (!pool || !param_name)
1051     {
1052       g_return_val_if_fail (pool != NULL, NULL);
1053       g_return_val_if_fail (param_name != NULL, NULL);
1054     }
1055
1056   G_SLOCK (&pool->smutex);
1057
1058   delim = pool->type_prefixing ? strchr (param_name, ':') : NULL;
1059
1060   /* try quick and away, i.e. without prefix */
1061   if (!delim)
1062     {
1063       pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1064       G_SUNLOCK (&pool->smutex);
1065
1066       return pspec;
1067     }
1068
1069   /* strip type prefix */
1070   if (pool->type_prefixing && delim[1] == ':')
1071     {
1072       guint l = delim - param_name;
1073       gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
1074       GType type;
1075       
1076       strncpy (buffer, param_name, delim - param_name);
1077       buffer[l] = 0;
1078       type = g_type_from_name (buffer);
1079       if (l >= 32)
1080         g_free (buffer);
1081       if (type)         /* type==0 isn't a valid type pefix */
1082         {
1083           /* sanity check, these cases don't make a whole lot of sense */
1084           if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
1085             {
1086               G_SUNLOCK (&pool->smutex);
1087
1088               return NULL;
1089             }
1090           owner_type = type;
1091           param_name += l + 2;
1092           pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
1093           G_SUNLOCK (&pool->smutex);
1094
1095           return pspec;
1096         }
1097     }
1098   /* malformed param_name */
1099
1100   G_SUNLOCK (&pool->smutex);
1101
1102   return NULL;
1103 }
1104
1105 static void
1106 pool_list (gpointer key,
1107            gpointer value,
1108            gpointer user_data)
1109 {
1110   GParamSpec *pspec = value;
1111   gpointer *data = user_data;
1112   GType owner_type = (GType) data[1];
1113
1114   if (owner_type == pspec->owner_type)
1115     data[0] = g_list_prepend (data[0], pspec);
1116 }
1117
1118 /**
1119  * g_param_spec_pool_list_owned:
1120  * @pool: a #GParamSpecPool
1121  * @owner_type: the owner to look for
1122  *
1123  * Gets an #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1124  * the pool.
1125  *
1126  * Returns: (transfer container) (element-type GObject.ParamSpec): a
1127  *          #GList of all #GParamSpec<!-- -->s owned by @owner_type in
1128  *          the pool#GParamSpec<!-- -->s.
1129  */
1130 GList*
1131 g_param_spec_pool_list_owned (GParamSpecPool *pool,
1132                               GType           owner_type)
1133 {
1134   gpointer data[2];
1135
1136   g_return_val_if_fail (pool != NULL, NULL);
1137   g_return_val_if_fail (owner_type > 0, NULL);
1138   
1139   G_SLOCK (&pool->smutex);
1140   data[0] = NULL;
1141   data[1] = (gpointer) owner_type;
1142   g_hash_table_foreach (pool->hash_table, pool_list, &data);
1143   G_SUNLOCK (&pool->smutex);
1144
1145   return data[0];
1146 }
1147
1148 static gint
1149 pspec_compare_id (gconstpointer a,
1150                   gconstpointer b)
1151 {
1152   const GParamSpec *pspec1 = a, *pspec2 = b;
1153
1154   if (pspec1->param_id < pspec2->param_id)
1155     return -1;
1156
1157   if (pspec1->param_id > pspec2->param_id)
1158     return 1;
1159
1160   return strcmp (pspec1->name, pspec2->name);
1161 }
1162
1163 static inline GSList*
1164 pspec_list_remove_overridden_and_redirected (GSList     *plist,
1165                                              GHashTable *ht,
1166                                              GType       owner_type,
1167                                              guint      *n_p)
1168 {
1169   GSList *rlist = NULL;
1170
1171   while (plist)
1172     {
1173       GSList *tmp = plist->next;
1174       GParamSpec *pspec = plist->data;
1175       GParamSpec *found;
1176       gboolean remove = FALSE;
1177
1178       /* Remove paramspecs that are redirected, and also paramspecs
1179        * that have are overridden by non-redirected properties.
1180        * The idea is to get the single paramspec for each name that
1181        * best corresponds to what the application sees.
1182        */
1183       if (g_param_spec_get_redirect_target (pspec))
1184         remove = TRUE;
1185       else
1186         {
1187           found = param_spec_ht_lookup (ht, pspec->name, owner_type, TRUE);
1188           if (found != pspec)
1189             {
1190               GParamSpec *redirect = g_param_spec_get_redirect_target (found);
1191               if (redirect != pspec)
1192                 remove = TRUE;
1193             }
1194         }
1195
1196       if (remove)
1197         {
1198           g_slist_free_1 (plist);
1199         }
1200       else
1201         {
1202           plist->next = rlist;
1203           rlist = plist;
1204           *n_p += 1;
1205         }
1206       plist = tmp;
1207     }
1208   return rlist;
1209 }
1210
1211 static void
1212 pool_depth_list (gpointer key,
1213                  gpointer value,
1214                  gpointer user_data)
1215 {
1216   GParamSpec *pspec = value;
1217   gpointer *data = user_data;
1218   GSList **slists = data[0];
1219   GType owner_type = (GType) data[1];
1220
1221   if (g_type_is_a (owner_type, pspec->owner_type))
1222     {
1223       if (G_TYPE_IS_INTERFACE (pspec->owner_type))
1224         {
1225           slists[0] = g_slist_prepend (slists[0], pspec);
1226         }
1227       else
1228         {
1229           guint d = g_type_depth (pspec->owner_type);
1230
1231           slists[d - 1] = g_slist_prepend (slists[d - 1], pspec);
1232         }
1233     }
1234 }
1235
1236 /* We handle interfaces specially since we don't want to
1237  * count interface prerequisites like normal inheritance;
1238  * the property comes from the direct inheritance from
1239  * the prerequisite class, not from the interface that
1240  * prerequires it.
1241  * 
1242  * also 'depth' isn't a meaningful concept for interface
1243  * prerequites.
1244  */
1245 static void
1246 pool_depth_list_for_interface (gpointer key,
1247                                gpointer value,
1248                                gpointer user_data)
1249 {
1250   GParamSpec *pspec = value;
1251   gpointer *data = user_data;
1252   GSList **slists = data[0];
1253   GType owner_type = (GType) data[1];
1254
1255   if (pspec->owner_type == owner_type)
1256     slists[0] = g_slist_prepend (slists[0], pspec);
1257 }
1258
1259 /**
1260  * g_param_spec_pool_list:
1261  * @pool: a #GParamSpecPool
1262  * @owner_type: the owner to look for
1263  * @n_pspecs_p: (out): return location for the length of the returned array
1264  *
1265  * Gets an array of all #GParamSpec<!-- -->s owned by @owner_type in
1266  * the pool.
1267  *
1268  * Returns: (array length=n_pspecs_p) (transfer container): a newly
1269  *          allocated array containing pointers to all #GParamSpecs
1270  *          owned by @owner_type in the pool
1271  */
1272 GParamSpec**
1273 g_param_spec_pool_list (GParamSpecPool *pool,
1274                         GType           owner_type,
1275                         guint          *n_pspecs_p)
1276 {
1277   GParamSpec **pspecs, **p;
1278   GSList **slists, *node;
1279   gpointer data[2];
1280   guint d, i;
1281
1282   g_return_val_if_fail (pool != NULL, NULL);
1283   g_return_val_if_fail (owner_type > 0, NULL);
1284   g_return_val_if_fail (n_pspecs_p != NULL, NULL);
1285   
1286   G_SLOCK (&pool->smutex);
1287   *n_pspecs_p = 0;
1288   d = g_type_depth (owner_type);
1289   slists = g_new0 (GSList*, d);
1290   data[0] = slists;
1291   data[1] = (gpointer) owner_type;
1292
1293   g_hash_table_foreach (pool->hash_table,
1294                         G_TYPE_IS_INTERFACE (owner_type) ?
1295                            pool_depth_list_for_interface :
1296                            pool_depth_list,
1297                         &data);
1298   
1299   for (i = 0; i < d; i++)
1300     slists[i] = pspec_list_remove_overridden_and_redirected (slists[i], pool->hash_table, owner_type, n_pspecs_p);
1301   pspecs = g_new (GParamSpec*, *n_pspecs_p + 1);
1302   p = pspecs;
1303   for (i = 0; i < d; i++)
1304     {
1305       slists[i] = g_slist_sort (slists[i], pspec_compare_id);
1306       for (node = slists[i]; node; node = node->next)
1307         *p++ = node->data;
1308       g_slist_free (slists[i]);
1309     }
1310   *p++ = NULL;
1311   g_free (slists);
1312   G_SUNLOCK (&pool->smutex);
1313
1314   return pspecs;
1315 }
1316
1317
1318 /* --- auxillary functions --- */
1319 typedef struct
1320 {
1321   /* class portion */
1322   GType           value_type;
1323   void          (*finalize)             (GParamSpec   *pspec);
1324   void          (*value_set_default)    (GParamSpec   *pspec,
1325                                          GValue       *value);
1326   gboolean      (*value_validate)       (GParamSpec   *pspec,
1327                                          GValue       *value);
1328   gint          (*values_cmp)           (GParamSpec   *pspec,
1329                                          const GValue *value1,
1330                                          const GValue *value2);
1331 } ParamSpecClassInfo;
1332
1333 static void
1334 param_spec_generic_class_init (gpointer g_class,
1335                                gpointer class_data)
1336 {
1337   GParamSpecClass *class = g_class;
1338   ParamSpecClassInfo *info = class_data;
1339
1340   class->value_type = info->value_type;
1341   if (info->finalize)
1342     class->finalize = info->finalize;                   /* optional */
1343   class->value_set_default = info->value_set_default;
1344   if (info->value_validate)
1345     class->value_validate = info->value_validate;       /* optional */
1346   class->values_cmp = info->values_cmp;
1347   g_free (class_data);
1348 }
1349
1350 static void
1351 default_value_set_default (GParamSpec *pspec,
1352                            GValue     *value)
1353 {
1354   /* value is already zero initialized */
1355 }
1356
1357 static gint
1358 default_values_cmp (GParamSpec   *pspec,
1359                     const GValue *value1,
1360                     const GValue *value2)
1361 {
1362   return memcmp (&value1->data, &value2->data, sizeof (value1->data));
1363 }
1364
1365 /**
1366  * g_param_type_register_static:
1367  * @name: 0-terminated string used as the name of the new #GParamSpec type.
1368  * @pspec_info: The #GParamSpecTypeInfo for this #GParamSpec type.
1369  *
1370  * Registers @name as the name of a new static type derived from
1371  * #G_TYPE_PARAM. The type system uses the information contained in
1372  * the #GParamSpecTypeInfo structure pointed to by @info to manage the
1373  * #GParamSpec type and its instances.
1374  *
1375  * Returns: The new type identifier.
1376  */
1377 GType
1378 g_param_type_register_static (const gchar              *name,
1379                               const GParamSpecTypeInfo *pspec_info)
1380 {
1381   GTypeInfo info = {
1382     sizeof (GParamSpecClass),      /* class_size */
1383     NULL,                          /* base_init */
1384     NULL,                          /* base_destroy */
1385     param_spec_generic_class_init, /* class_init */
1386     NULL,                          /* class_destroy */
1387     NULL,                          /* class_data */
1388     0,                             /* instance_size */
1389     16,                            /* n_preallocs */
1390     NULL,                          /* instance_init */
1391   };
1392   ParamSpecClassInfo *cinfo;
1393
1394   g_return_val_if_fail (name != NULL, 0);
1395   g_return_val_if_fail (pspec_info != NULL, 0);
1396   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
1397   g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
1398   g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
1399   /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
1400   /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
1401   /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
1402
1403   info.instance_size = pspec_info->instance_size;
1404   info.n_preallocs = pspec_info->n_preallocs;
1405   info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
1406   cinfo = g_new (ParamSpecClassInfo, 1);
1407   cinfo->value_type = pspec_info->value_type;
1408   cinfo->finalize = pspec_info->finalize;
1409   cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
1410   cinfo->value_validate = pspec_info->value_validate;
1411   cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
1412   info.class_data = cinfo;
1413
1414   return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
1415 }
1416
1417 /**
1418  * g_value_set_param:
1419  * @value: a valid #GValue of type %G_TYPE_PARAM
1420  * @param: (allow-none): the #GParamSpec to be set
1421  *
1422  * Set the contents of a %G_TYPE_PARAM #GValue to @param.
1423  */
1424 void
1425 g_value_set_param (GValue     *value,
1426                    GParamSpec *param)
1427 {
1428   g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1429   if (param)
1430     g_return_if_fail (G_IS_PARAM_SPEC (param));
1431
1432   if (value->data[0].v_pointer)
1433     g_param_spec_unref (value->data[0].v_pointer);
1434   value->data[0].v_pointer = param;
1435   if (value->data[0].v_pointer)
1436     g_param_spec_ref (value->data[0].v_pointer);
1437 }
1438
1439 /**
1440  * g_value_set_param_take_ownership: (skip)
1441  * @value: a valid #GValue of type %G_TYPE_PARAM
1442  * @param: (allow-none): the #GParamSpec to be set
1443  *
1444  * This is an internal function introduced mainly for C marshallers.
1445  *
1446  * Deprecated: 2.4: Use g_value_take_param() instead.
1447  */
1448 void
1449 g_value_set_param_take_ownership (GValue     *value,
1450                                   GParamSpec *param)
1451 {
1452   g_value_take_param (value, param);
1453 }
1454
1455 /**
1456  * g_value_take_param: (skip)
1457  * @value: a valid #GValue of type %G_TYPE_PARAM
1458  * @param: (allow-none): the #GParamSpec to be set
1459  *
1460  * Sets the contents of a %G_TYPE_PARAM #GValue to @param and takes
1461  * over the ownership of the callers reference to @param; the caller
1462  * doesn't have to unref it any more.
1463  *
1464  * Since: 2.4
1465  */
1466 void
1467 g_value_take_param (GValue     *value,
1468                     GParamSpec *param)
1469 {
1470   g_return_if_fail (G_VALUE_HOLDS_PARAM (value));
1471   if (param)
1472     g_return_if_fail (G_IS_PARAM_SPEC (param));
1473
1474   if (value->data[0].v_pointer)
1475     g_param_spec_unref (value->data[0].v_pointer);
1476   value->data[0].v_pointer = param; /* we take over the reference count */
1477 }
1478
1479 /**
1480  * g_value_get_param:
1481  * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1482  *
1483  * Get the contents of a %G_TYPE_PARAM #GValue.
1484  *
1485  * Returns: (transfer none): #GParamSpec content of @value
1486  */
1487 GParamSpec*
1488 g_value_get_param (const GValue *value)
1489 {
1490   g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1491
1492   return value->data[0].v_pointer;
1493 }
1494
1495 /**
1496  * g_value_dup_param: (skip)
1497  * @value: a valid #GValue whose type is derived from %G_TYPE_PARAM
1498  *
1499  * Get the contents of a %G_TYPE_PARAM #GValue, increasing its
1500  * reference count.
1501  *
1502  * Returns: #GParamSpec content of @value, should be unreferenced when
1503  *          no longer needed.
1504  */
1505 GParamSpec*
1506 g_value_dup_param (const GValue *value)
1507 {
1508   g_return_val_if_fail (G_VALUE_HOLDS_PARAM (value), NULL);
1509
1510   return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
1511 }