0c99722f8bee6bd3fa12643ec1a99b6fb334acd4
[platform/upstream/glib.git] / gobject / gparam.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1997, 1998, 1999, 2000 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        "gparam.h"
25
26
27 #include        "gvaluecollector.h"
28 #include        <string.h>
29
30
31
32 /* --- defines --- */
33 #define G_PARAM_SPEC_CLASS(class)               (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_PARAM, GParamSpecClass))
34 #define G_PARAM_USER_MASK                       ((1 << G_PARAM_USER_SHIFT) - 1)
35 #define PSPEC_APPLIES_TO_VALUE(pspec, value)    (G_TYPE_CHECK_VALUE_TYPE ((value), G_PARAM_SPEC_VALUE_TYPE (pspec)))
36 #define G_SLOCK(mutex)                          g_static_mutex_lock (mutex)
37 #define G_SUNLOCK(mutex)                        g_static_mutex_unlock (mutex)
38
39
40 /* --- prototypes --- */
41 static void     g_param_spec_class_base_init     (GParamSpecClass       *class);
42 static void     g_param_spec_class_base_finalize (GParamSpecClass       *class);
43 static void     g_param_spec_class_init          (GParamSpecClass       *class,
44                                                   gpointer               class_data);
45 static void     g_param_spec_init                (GParamSpec            *pspec,
46                                                   GParamSpecClass       *class);
47 static void     g_param_spec_finalize            (GParamSpec            *pspec);
48 static void     value_param_init                (GValue         *value);
49 static void     value_param_free_value          (GValue         *value);
50 static void     value_param_copy_value          (const GValue   *src_value,
51                                                  GValue         *dest_value);
52 static gpointer value_param_peek_pointer        (const GValue   *value);
53 static gchar*   value_param_collect_value       (GValue         *value,
54                                                  guint           nth_value,
55                                                  GType          *collect_type,
56                                                  GTypeCValue    *collect_value);
57 static gchar*   value_param_lcopy_value         (const GValue   *value,
58                                                  guint           nth_value,
59                                                  GType          *collect_type,
60                                                  GTypeCValue    *collect_value);
61
62
63 /* --- variables --- */
64 static GQuark quark_floating = 0;
65 G_LOCK_DEFINE_STATIC (pspec_ref_count);
66
67
68 /* --- functions --- */
69 void
70 g_param_type_init (void)        /* sync with gtype.c */
71 {
72   static const GTypeFundamentalInfo finfo = {
73     (G_TYPE_FLAG_CLASSED |
74      G_TYPE_FLAG_INSTANTIATABLE |
75      G_TYPE_FLAG_DERIVABLE |
76      G_TYPE_FLAG_DEEP_DERIVABLE),
77   };
78   static const GTypeValueTable param_value_table = {
79     value_param_init,           /* value_init */
80     value_param_free_value,     /* value_free */
81     value_param_copy_value,     /* value_copy */
82     value_param_peek_pointer,   /* value_peek_pointer */
83     G_VALUE_COLLECT_POINTER,    /* collect_type */
84     value_param_collect_value,  /* collect_value */
85     G_VALUE_COLLECT_POINTER,    /* lcopy_type */
86     value_param_lcopy_value,    /* lcopy_value */
87   };
88   static const GTypeInfo param_spec_info = {
89     sizeof (GParamSpecClass),
90
91     (GBaseInitFunc) g_param_spec_class_base_init,
92     (GBaseFinalizeFunc) g_param_spec_class_base_finalize,
93     (GClassInitFunc) g_param_spec_class_init,
94     (GClassFinalizeFunc) NULL,
95     NULL,       /* class_data */
96
97     sizeof (GParamSpec),
98     0,          /* n_preallocs */
99     (GInstanceInitFunc) g_param_spec_init,
100
101     &param_value_table,
102   };
103   GType type;
104
105   type = g_type_register_fundamental (G_TYPE_PARAM, "GParam", &param_spec_info, &finfo, G_TYPE_FLAG_ABSTRACT);
106   g_assert (type == G_TYPE_PARAM);
107 }
108
109 static void
110 g_param_spec_class_base_init (GParamSpecClass *class)
111 {
112 }
113
114 static void
115 g_param_spec_class_base_finalize (GParamSpecClass *class)
116 {
117 }
118
119 static void
120 g_param_spec_class_init (GParamSpecClass *class,
121                          gpointer         class_data)
122 {
123   quark_floating = g_quark_from_static_string ("GParamSpec-floating");
124
125   class->value_type = G_TYPE_NONE;
126   class->finalize = g_param_spec_finalize;
127   class->value_set_default = NULL;
128   class->value_validate = NULL;
129   class->values_cmp = NULL;
130 }
131
132 static void
133 g_param_spec_init (GParamSpec      *pspec,
134                    GParamSpecClass *class)
135 {
136   pspec->name = NULL;
137   pspec->nick = NULL;
138   pspec->blurb = NULL;
139   pspec->flags = 0;
140   pspec->value_type = class->value_type;
141   pspec->owner_type = 0;
142   pspec->qdata = NULL;
143   pspec->ref_count = 1;
144   g_datalist_id_set_data (&pspec->qdata, quark_floating, GUINT_TO_POINTER (TRUE));
145 }
146
147 static void
148 g_param_spec_finalize (GParamSpec *pspec)
149 {
150   g_datalist_clear (&pspec->qdata);
151   
152   g_free (pspec->name);
153   g_free (pspec->nick);
154   g_free (pspec->blurb);
155
156   g_type_free_instance ((GTypeInstance*) pspec);
157 }
158
159 GParamSpec*
160 g_param_spec_ref (GParamSpec *pspec)
161 {
162   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
163
164   G_LOCK (pspec_ref_count);
165   if (pspec->ref_count > 0)
166     {
167       pspec->ref_count += 1;
168       G_UNLOCK (pspec_ref_count);
169     }
170   else
171     {
172       G_UNLOCK (pspec_ref_count);
173       g_return_val_if_fail (pspec->ref_count > 0, NULL);
174     }
175   
176   return pspec;
177 }
178
179 void
180 g_param_spec_unref (GParamSpec *pspec)
181 {
182   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
183
184   G_LOCK (pspec_ref_count);
185   if (pspec->ref_count > 0)
186     {
187       gboolean need_finalize;
188
189       /* sync with _sink */
190       pspec->ref_count -= 1;
191       need_finalize = pspec->ref_count == 0;
192       G_UNLOCK (pspec_ref_count);
193       if (need_finalize)
194         G_PARAM_SPEC_GET_CLASS (pspec)->finalize (pspec);
195     }
196   else
197     {
198       G_UNLOCK (pspec_ref_count);
199       g_return_if_fail (pspec->ref_count > 0);
200     }
201 }
202
203 void
204 g_param_spec_sink (GParamSpec *pspec)
205 {
206   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
207
208   G_LOCK (pspec_ref_count);
209   if (pspec->ref_count > 0)
210     {
211       if (g_datalist_id_remove_no_notify (&pspec->qdata, quark_floating))
212         {
213           /* sync with _unref */
214           if (pspec->ref_count > 1)
215             pspec->ref_count -= 1;
216           else
217             {
218               G_UNLOCK (pspec_ref_count);
219               g_param_spec_unref (pspec);
220
221               return;
222             }
223         }
224       G_UNLOCK (pspec_ref_count);
225     }
226   else
227     {
228       G_UNLOCK (pspec_ref_count);
229       g_return_if_fail (pspec->ref_count > 0);
230     }
231 }
232
233 gpointer
234 g_param_spec_internal (GType        param_type,
235                        const gchar *name,
236                        const gchar *nick,
237                        const gchar *blurb,
238                        GParamFlags  flags)
239 {
240   GParamSpec *pspec;
241
242   g_return_val_if_fail (G_TYPE_IS_PARAM (param_type) && param_type != G_TYPE_PARAM, NULL);
243   g_return_val_if_fail (name != NULL, NULL);
244
245   pspec = (gpointer) g_type_create_instance (param_type);
246   pspec->name = g_strdup (name);
247   g_strcanon (pspec->name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
248   pspec->nick = g_strdup (nick ? nick : pspec->name);
249   pspec->blurb = g_strdup (blurb);
250   pspec->flags = (flags & G_PARAM_USER_MASK) | (flags & G_PARAM_MASK);
251
252   return pspec;
253 }
254
255 gpointer
256 g_param_spec_get_qdata (GParamSpec *pspec,
257                         GQuark      quark)
258 {
259   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
260   
261   return quark ? g_datalist_id_get_data (&pspec->qdata, quark) : NULL;
262 }
263
264 void
265 g_param_spec_set_qdata (GParamSpec *pspec,
266                         GQuark      quark,
267                         gpointer    data)
268 {
269   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
270   g_return_if_fail (quark > 0);
271
272   g_datalist_id_set_data (&pspec->qdata, quark, data);
273 }
274
275 void
276 g_param_spec_set_qdata_full (GParamSpec    *pspec,
277                              GQuark         quark,
278                              gpointer       data,
279                              GDestroyNotify destroy)
280 {
281   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
282   g_return_if_fail (quark > 0);
283
284   g_datalist_id_set_data_full (&pspec->qdata, quark, data, data ? destroy : (GDestroyNotify) NULL);
285 }
286
287 gpointer
288 g_param_spec_steal_qdata (GParamSpec *pspec,
289                           GQuark      quark)
290 {
291   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
292   g_return_val_if_fail (quark > 0, NULL);
293   
294   return g_datalist_id_remove_no_notify (&pspec->qdata, quark);
295 }
296
297 void
298 g_param_value_set_default (GParamSpec *pspec,
299                            GValue     *value)
300 {
301   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
302   g_return_if_fail (G_IS_VALUE (value));
303   g_return_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value));
304
305   g_value_reset (value);
306   G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, value);
307 }
308
309 gboolean
310 g_param_value_defaults (GParamSpec *pspec,
311                         GValue     *value)
312 {
313   GValue dflt_value = { 0, };
314   gboolean defaults;
315
316   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
317   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
318   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
319
320   g_value_init (&dflt_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
321   G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);
322   defaults = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value, &dflt_value) == 0;
323   g_value_unset (&dflt_value);
324
325   return defaults;
326 }
327
328 gboolean
329 g_param_value_validate (GParamSpec *pspec,
330                         GValue     *value)
331 {
332   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
333   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
334   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value), FALSE);
335
336   if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate)
337     {
338       GValue oval = *value;
339
340       if (G_PARAM_SPEC_GET_CLASS (pspec)->value_validate (pspec, value) ||
341           memcmp (&oval.data, &value->data, sizeof (oval.data)))
342         return TRUE;
343     }
344
345   return FALSE;
346 }
347
348 gint
349 g_param_values_cmp (GParamSpec   *pspec,
350                     const GValue *value1,
351                     const GValue *value2)
352 {
353   gint cmp;
354
355   /* param_values_cmp() effectively does: value1 - value2
356    * so the return values are:
357    * -1)  value1 < value2
358    *  0)  value1 == value2
359    *  1)  value1 > value2
360    */
361   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), 0);
362   g_return_val_if_fail (G_IS_VALUE (value1), 0);
363   g_return_val_if_fail (G_IS_VALUE (value2), 0);
364   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value1), 0);
365   g_return_val_if_fail (PSPEC_APPLIES_TO_VALUE (pspec, value2), 0);
366
367   cmp = G_PARAM_SPEC_GET_CLASS (pspec)->values_cmp (pspec, value1, value2);
368
369   return CLAMP (cmp, -1, 1);
370 }
371
372 static void
373 value_param_init (GValue *value)
374 {
375   value->data[0].v_pointer = NULL;
376 }
377
378 static void
379 value_param_free_value (GValue *value)
380 {
381   if (value->data[0].v_pointer)
382     g_param_spec_unref (value->data[0].v_pointer);
383 }
384
385 static void
386 value_param_copy_value (const GValue *src_value,
387                         GValue       *dest_value)
388 {
389   dest_value->data[0].v_pointer = (src_value->data[0].v_pointer
390                                    ? g_param_spec_ref (src_value->data[0].v_pointer)
391                                    : NULL);
392 }
393
394 static gpointer
395 value_param_peek_pointer (const GValue *value)
396 {
397   return value->data[0].v_pointer;
398 }
399
400 static gchar*
401 value_param_collect_value (GValue      *value,
402                            guint        nth_value,
403                            GType       *collect_type,
404                            GTypeCValue *collect_value)
405 {
406   if (collect_value->v_pointer)
407     {
408       GParamSpec *param = collect_value->v_pointer;
409
410       if (param->g_type_instance.g_class == NULL)
411         return g_strconcat ("invalid unclassed param spec pointer for value type `",
412                             G_VALUE_TYPE_NAME (value),
413                             "'",
414                             NULL);
415       else if (!g_type_is_a (G_PARAM_SPEC_TYPE (param), G_VALUE_TYPE (value)))
416         return g_strconcat ("invalid param spec type `",
417                             G_PARAM_SPEC_TYPE_NAME (param),
418                             "' for value type `",
419                             G_VALUE_TYPE_NAME (value),
420                             "'",
421                             NULL);
422       value->data[0].v_pointer = g_param_spec_ref (param);
423     }
424   else
425     value->data[0].v_pointer = NULL;
426
427   *collect_type = 0;
428   return NULL;
429 }
430
431 static gchar*
432 value_param_lcopy_value (const GValue *value,
433                          guint         nth_value,
434                          GType        *collect_type,
435                          GTypeCValue  *collect_value)
436 {
437   GParamSpec **param_p = collect_value->v_pointer;
438
439   if (!param_p)
440     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
441
442   *param_p = value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
443
444   *collect_type = 0;
445   return NULL;
446 }
447
448
449 /* --- param spec pool --- */
450 struct _GParamSpecPool
451 {
452   GStaticMutex smutex;
453   gboolean     type_prefixing;
454   GHashTable  *hash_table;
455 };
456
457 static guint
458 param_spec_pool_hash (gconstpointer key_spec)
459 {
460   const GParamSpec *key = key_spec;
461   const gchar *p;
462   guint h = key->owner_type;
463
464   for (p = key->name; *p; p++)
465     h = (h << 5) - h + *p;
466
467   return h;
468 }
469
470 static gboolean
471 param_spec_pool_equals (gconstpointer key_spec_1,
472                         gconstpointer key_spec_2)
473 {
474   const GParamSpec *key1 = key_spec_1;
475   const GParamSpec *key2 = key_spec_2;
476
477   return (key1->owner_type == key2->owner_type &&
478           strcmp (key1->name, key2->name) == 0);
479 }
480
481 GParamSpecPool*
482 g_param_spec_pool_new (gboolean type_prefixing)
483 {
484   static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
485   GParamSpecPool *pool = g_new (GParamSpecPool, 1);
486
487   memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
488   pool->type_prefixing = type_prefixing != FALSE;
489   pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
490
491   return pool;
492 }
493
494 void
495 g_param_spec_pool_insert (GParamSpecPool *pool,
496                           GParamSpec     *pspec,
497                           GType           owner_type)
498 {
499   gchar *p;
500   
501   if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
502     {
503       G_SLOCK (&pool->smutex);
504       for (p = pspec->name; *p; p++)
505         {
506           if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
507             {
508               g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
509               G_SUNLOCK (&pool->smutex);
510               return;
511             }
512         }
513       
514       pspec->owner_type = owner_type;
515       g_param_spec_ref (pspec);
516       g_hash_table_insert (pool->hash_table, pspec, pspec);
517       G_SUNLOCK (&pool->smutex);
518     }
519   else
520     {
521       g_return_if_fail (pool != NULL);
522       g_return_if_fail (pspec);
523       g_return_if_fail (owner_type > 0);
524       g_return_if_fail (pspec->owner_type == 0);
525     }
526 }
527
528 void
529 g_param_spec_pool_remove (GParamSpecPool *pool,
530                           GParamSpec     *pspec)
531 {
532   if (pool && pspec)
533     {
534       G_SLOCK (&pool->smutex);
535       if (g_hash_table_remove (pool->hash_table, pspec))
536         g_param_spec_unref (pspec);
537       else
538         g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
539       G_SUNLOCK (&pool->smutex);
540     }
541   else
542     {
543       g_return_if_fail (pool != NULL);
544       g_return_if_fail (pspec);
545     }
546 }
547
548 static inline GParamSpec*
549 param_spec_ht_lookup (GHashTable  *hash_table,
550                       const gchar *param_name,
551                       GType        owner_type,
552                       gboolean     walk_ancestors)
553 {
554   GParamSpec key, *pspec;
555
556   key.owner_type = owner_type;
557   key.name = (gchar*) param_name;
558   if (walk_ancestors)
559     do
560       {
561         pspec = g_hash_table_lookup (hash_table, &key);
562         if (pspec)
563           return pspec;
564         key.owner_type = g_type_parent (key.owner_type);
565       }
566     while (key.owner_type);
567   else
568     pspec = g_hash_table_lookup (hash_table, &key);
569
570   if (!pspec)
571     {
572       /* sigh, try canonicalization */
573       key.name = g_strdup (param_name);
574       key.owner_type = owner_type;
575       
576       g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
577       if (walk_ancestors)
578         do
579           {
580             pspec = g_hash_table_lookup (hash_table, &key);
581             if (pspec)
582               {
583                 g_free (key.name);
584                 return pspec;
585               }
586             key.owner_type = g_type_parent (key.owner_type);
587           }
588         while (key.owner_type);
589       else
590         pspec = g_hash_table_lookup (hash_table, &key);
591       g_free (key.name);
592     }
593
594   return pspec;
595 }
596
597 GParamSpec*
598 g_param_spec_pool_lookup (GParamSpecPool *pool,
599                           const gchar    *param_name,
600                           GType           owner_type,
601                           gboolean        walk_ancestors,
602                           const gchar   **trailer_p)
603 {
604   GParamSpec *pspec;
605   gchar *delim;
606
607   if (!pool || !param_name)
608     {
609       g_return_val_if_fail (pool != NULL, NULL);
610       g_return_val_if_fail (param_name != NULL, NULL);
611     }
612
613   G_SLOCK (&pool->smutex);
614
615   delim = strchr (param_name, ':');
616
617   /* try quick and away, i.e. no prefix, no trailer */
618   if (!delim)
619     {
620       if (trailer_p)
621         *trailer_p = NULL;
622       pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
623       G_SUNLOCK (&pool->smutex);
624
625       return pspec;
626     }
627
628   /* strip type prefix */
629   if (pool->type_prefixing && delim[1] == ':')
630     {
631       guint l = delim - param_name;
632       gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
633       GType type;
634       
635       strncpy (buffer, param_name, delim - param_name);
636       buffer[l] = 0;
637       type = g_type_from_name (buffer);
638       if (l >= 32)
639         g_free (buffer);
640       if (type)         /* type==0 isn't a valid type pefix */
641         {
642           /* sanity check, these cases don't make a whole lot of sense */
643           if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
644             {
645               if (trailer_p)
646                 *trailer_p = NULL;
647               G_SUNLOCK (&pool->smutex);
648
649               return NULL;
650             }
651           owner_type = type;
652           param_name += l + 2;
653           delim = strchr (param_name, ':');
654           if (!delim)           /* good, can still forget about trailer */
655             {
656               if (trailer_p)
657                 *trailer_p = NULL;
658               pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
659               G_SUNLOCK (&pool->smutex);
660
661               return pspec;
662             }
663         }
664     }
665
666   /* ok, no prefix, handle trailer */
667   if (delim[1] == ':')
668     {
669       guint l = delim - param_name;
670       gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
671       
672       strncpy (buffer, param_name, delim - param_name);
673       buffer[l] = 0;
674       pspec = param_spec_ht_lookup (pool->hash_table, buffer, owner_type, walk_ancestors);
675       if (l >= 32)
676         g_free (buffer);
677       if (trailer_p)
678         *trailer_p = pspec ? delim + 2 : NULL;
679       G_SUNLOCK (&pool->smutex);
680
681       return pspec;
682     }
683
684   /* malformed param_name */
685   if (trailer_p)
686     *trailer_p = NULL;
687   G_SUNLOCK (&pool->smutex);
688
689   return NULL;
690 }
691
692
693 /* --- auxillary functions --- */
694 typedef struct
695 {
696   /* class portion */
697   GType           value_type;
698   void          (*finalize)             (GParamSpec   *pspec);
699   void          (*value_set_default)    (GParamSpec   *pspec,
700                                          GValue       *value);
701   gboolean      (*value_validate)       (GParamSpec   *pspec,
702                                          GValue       *value);
703   gint          (*values_cmp)           (GParamSpec   *pspec,
704                                          const GValue *value1,
705                                          const GValue *value2);
706 } ParamSpecClassInfo;
707
708 static void
709 param_spec_generic_class_init (gpointer g_class,
710                                gpointer class_data)
711 {
712   GParamSpecClass *class = g_class;
713   ParamSpecClassInfo *info = class_data;
714
715   class->value_type = info->value_type;
716   if (info->finalize)
717     class->finalize = info->finalize;                   /* optional */
718   class->value_set_default = info->value_set_default;
719   if (info->value_validate)
720     class->value_validate = info->value_validate;       /* optional */
721   class->values_cmp = info->values_cmp;
722   g_free (class_data);
723 }
724
725 static void
726 default_value_set_default (GParamSpec *pspec,
727                            GValue     *value)
728 {
729   /* value is already zero initialized */
730 }
731
732 static gint
733 default_values_cmp (GParamSpec   *pspec,
734                     const GValue *value1,
735                     const GValue *value2)
736 {
737   return memcmp (&value1->data, &value2->data, sizeof (value1->data));
738 }
739
740 GType
741 g_param_type_register_static (const gchar              *name,
742                               const GParamSpecTypeInfo *pspec_info)
743 {
744   GTypeInfo info = {
745     sizeof (GParamSpecClass),      /* class_size */
746     NULL,                          /* base_init */
747     NULL,                          /* base_destroy */
748     param_spec_generic_class_init, /* class_init */
749     NULL,                          /* class_destroy */
750     NULL,                          /* class_data */
751     0,                             /* instance_size */
752     16,                            /* n_preallocs */
753     NULL,                          /* instance_init */
754   };
755   ParamSpecClassInfo *cinfo;
756
757   g_return_val_if_fail (name != NULL, 0);
758   g_return_val_if_fail (pspec_info != NULL, 0);
759   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
760   g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
761   g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
762   /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
763   /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
764   /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
765
766   info.instance_size = pspec_info->instance_size;
767   info.n_preallocs = pspec_info->n_preallocs;
768   info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
769   cinfo = g_new (ParamSpecClassInfo, 1);
770   cinfo->value_type = pspec_info->value_type;
771   cinfo->finalize = pspec_info->finalize;
772   cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
773   cinfo->value_validate = pspec_info->value_validate;
774   cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
775   info.class_data = cinfo;
776
777   return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
778 }
779
780 void
781 g_value_set_param (GValue     *value,
782                    GParamSpec *param)
783 {
784   g_return_if_fail (G_IS_VALUE_PARAM (value));
785   if (param)
786     g_return_if_fail (G_IS_PARAM_SPEC (param));
787
788   if (value->data[0].v_pointer)
789     g_param_spec_unref (value->data[0].v_pointer);
790   value->data[0].v_pointer = param;
791   if (value->data[0].v_pointer)
792     g_param_spec_ref (value->data[0].v_pointer);
793 }
794
795 GParamSpec*
796 g_value_get_param (const GValue *value)
797 {
798   g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
799
800   return value->data[0].v_pointer;
801 }
802
803 GParamSpec*
804 g_value_dup_param (const GValue *value)
805 {
806   g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
807
808   return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
809 }