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