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