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