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