changed collect_format, collect_value() and lcopy_format, lcopy_value() in
[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           n_collect_values,
55                                                  GTypeCValue    *collect_values,
56                                                  guint           collect_flags);
57 static gchar*   value_param_lcopy_value         (const GValue   *value,
58                                                  guint           n_collect_values,
59                                                  GTypeCValue    *collect_values,
60                                                  guint           collect_flags);
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     "p",                        /* collect_format */
84     value_param_collect_value,  /* collect_value */
85     "p",                        /* lcopy_format */
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        n_collect_values,
403                            GTypeCValue *collect_values,
404                            guint        collect_flags)
405 {
406   if (collect_values[0].v_pointer)
407     {
408       GParamSpec *param = collect_values[0].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   return NULL;
428 }
429
430 static gchar*
431 value_param_lcopy_value (const GValue *value,
432                          guint         n_collect_values,
433                          GTypeCValue  *collect_values,
434                          guint         collect_flags)
435 {
436   GParamSpec **param_p = collect_values[0].v_pointer;
437
438   if (!param_p)
439     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
440
441   if (!value->data[0].v_pointer)
442     *param_p = NULL;
443   else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
444     *param_p = value->data[0].v_pointer;
445   else
446     *param_p = g_param_spec_ref (value->data[0].v_pointer);
447
448   return NULL;
449 }
450
451
452 /* --- param spec pool --- */
453 struct _GParamSpecPool
454 {
455   GStaticMutex smutex;
456   gboolean     type_prefixing;
457   GHashTable  *hash_table;
458 };
459
460 static guint
461 param_spec_pool_hash (gconstpointer key_spec)
462 {
463   const GParamSpec *key = key_spec;
464   const gchar *p;
465   guint h = key->owner_type;
466
467   for (p = key->name; *p; p++)
468     h = (h << 5) - h + *p;
469
470   return h;
471 }
472
473 static gboolean
474 param_spec_pool_equals (gconstpointer key_spec_1,
475                         gconstpointer key_spec_2)
476 {
477   const GParamSpec *key1 = key_spec_1;
478   const GParamSpec *key2 = key_spec_2;
479
480   return (key1->owner_type == key2->owner_type &&
481           strcmp (key1->name, key2->name) == 0);
482 }
483
484 GParamSpecPool*
485 g_param_spec_pool_new (gboolean type_prefixing)
486 {
487   static GStaticMutex init_smutex = G_STATIC_MUTEX_INIT;
488   GParamSpecPool *pool = g_new (GParamSpecPool, 1);
489
490   memcpy (&pool->smutex, &init_smutex, sizeof (init_smutex));
491   pool->type_prefixing = type_prefixing != FALSE;
492   pool->hash_table = g_hash_table_new (param_spec_pool_hash, param_spec_pool_equals);
493
494   return pool;
495 }
496
497 void
498 g_param_spec_pool_insert (GParamSpecPool *pool,
499                           GParamSpec     *pspec,
500                           GType           owner_type)
501 {
502   gchar *p;
503   
504   if (pool && pspec && owner_type > 0 && pspec->owner_type == 0)
505     {
506       G_SLOCK (&pool->smutex);
507       for (p = pspec->name; *p; p++)
508         {
509           if (!strchr (G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_", *p))
510             {
511               g_warning (G_STRLOC ": pspec name \"%s\" contains invalid characters", pspec->name);
512               G_SUNLOCK (&pool->smutex);
513               return;
514             }
515         }
516       
517       pspec->owner_type = owner_type;
518       g_param_spec_ref (pspec);
519       g_hash_table_insert (pool->hash_table, pspec, pspec);
520       G_SUNLOCK (&pool->smutex);
521     }
522   else
523     {
524       g_return_if_fail (pool != NULL);
525       g_return_if_fail (pspec);
526       g_return_if_fail (owner_type > 0);
527       g_return_if_fail (pspec->owner_type == 0);
528     }
529 }
530
531 void
532 g_param_spec_pool_remove (GParamSpecPool *pool,
533                           GParamSpec     *pspec)
534 {
535   if (pool && pspec)
536     {
537       G_SLOCK (&pool->smutex);
538       if (g_hash_table_remove (pool->hash_table, pspec))
539         g_param_spec_unref (pspec);
540       else
541         g_warning (G_STRLOC ": attempt to remove unknown pspec `%s' from pool", pspec->name);
542       G_SUNLOCK (&pool->smutex);
543     }
544   else
545     {
546       g_return_if_fail (pool != NULL);
547       g_return_if_fail (pspec);
548     }
549 }
550
551 static inline GParamSpec*
552 param_spec_ht_lookup (GHashTable  *hash_table,
553                       const gchar *param_name,
554                       GType        owner_type,
555                       gboolean     walk_ancestors)
556 {
557   GParamSpec key, *pspec;
558
559   key.owner_type = owner_type;
560   key.name = (gchar*) param_name;
561   if (walk_ancestors)
562     do
563       {
564         pspec = g_hash_table_lookup (hash_table, &key);
565         if (pspec)
566           return pspec;
567         key.owner_type = g_type_parent (key.owner_type);
568       }
569     while (key.owner_type);
570   else
571     pspec = g_hash_table_lookup (hash_table, &key);
572
573   if (!pspec)
574     {
575       /* sigh, try canonicalization */
576       key.name = g_strdup (param_name);
577       key.owner_type = owner_type;
578       
579       g_strcanon (key.name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
580       if (walk_ancestors)
581         do
582           {
583             pspec = g_hash_table_lookup (hash_table, &key);
584             if (pspec)
585               {
586                 g_free (key.name);
587                 return pspec;
588               }
589             key.owner_type = g_type_parent (key.owner_type);
590           }
591         while (key.owner_type);
592       else
593         pspec = g_hash_table_lookup (hash_table, &key);
594       g_free (key.name);
595     }
596
597   return pspec;
598 }
599
600 GParamSpec*
601 g_param_spec_pool_lookup (GParamSpecPool *pool,
602                           const gchar    *param_name,
603                           GType           owner_type,
604                           gboolean        walk_ancestors,
605                           const gchar   **trailer_p)
606 {
607   GParamSpec *pspec;
608   gchar *delim;
609
610   if (!pool || !param_name)
611     {
612       g_return_val_if_fail (pool != NULL, NULL);
613       g_return_val_if_fail (param_name != NULL, NULL);
614     }
615
616   G_SLOCK (&pool->smutex);
617
618   delim = strchr (param_name, ':');
619
620   /* try quick and away, i.e. no prefix, no trailer */
621   if (!delim)
622     {
623       if (trailer_p)
624         *trailer_p = NULL;
625       pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
626       G_SUNLOCK (&pool->smutex);
627
628       return pspec;
629     }
630
631   /* strip type prefix */
632   if (pool->type_prefixing && delim[1] == ':')
633     {
634       guint l = delim - param_name;
635       gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
636       GType type;
637       
638       strncpy (buffer, param_name, delim - param_name);
639       buffer[l] = 0;
640       type = g_type_from_name (buffer);
641       if (l >= 32)
642         g_free (buffer);
643       if (type)         /* type==0 isn't a valid type pefix */
644         {
645           /* sanity check, these cases don't make a whole lot of sense */
646           if ((!walk_ancestors && type != owner_type) || !g_type_is_a (owner_type, type))
647             {
648               if (trailer_p)
649                 *trailer_p = NULL;
650               G_SUNLOCK (&pool->smutex);
651
652               return NULL;
653             }
654           owner_type = type;
655           param_name += l + 2;
656           delim = strchr (param_name, ':');
657           if (!delim)           /* good, can still forget about trailer */
658             {
659               if (trailer_p)
660                 *trailer_p = NULL;
661               pspec = param_spec_ht_lookup (pool->hash_table, param_name, owner_type, walk_ancestors);
662               G_SUNLOCK (&pool->smutex);
663
664               return pspec;
665             }
666         }
667     }
668
669   /* ok, no prefix, handle trailer */
670   if (delim[1] == ':')
671     {
672       guint l = delim - param_name;
673       gchar stack_buffer[32], *buffer = l < 32 ? stack_buffer : g_new (gchar, l + 1);
674       
675       strncpy (buffer, param_name, delim - param_name);
676       buffer[l] = 0;
677       pspec = param_spec_ht_lookup (pool->hash_table, buffer, owner_type, walk_ancestors);
678       if (l >= 32)
679         g_free (buffer);
680       if (trailer_p)
681         *trailer_p = pspec ? delim + 2 : NULL;
682       G_SUNLOCK (&pool->smutex);
683
684       return pspec;
685     }
686
687   /* malformed param_name */
688   if (trailer_p)
689     *trailer_p = NULL;
690   G_SUNLOCK (&pool->smutex);
691
692   return NULL;
693 }
694
695
696 /* --- auxillary functions --- */
697 typedef struct
698 {
699   /* class portion */
700   GType           value_type;
701   void          (*finalize)             (GParamSpec   *pspec);
702   void          (*value_set_default)    (GParamSpec   *pspec,
703                                          GValue       *value);
704   gboolean      (*value_validate)       (GParamSpec   *pspec,
705                                          GValue       *value);
706   gint          (*values_cmp)           (GParamSpec   *pspec,
707                                          const GValue *value1,
708                                          const GValue *value2);
709 } ParamSpecClassInfo;
710
711 static void
712 param_spec_generic_class_init (gpointer g_class,
713                                gpointer class_data)
714 {
715   GParamSpecClass *class = g_class;
716   ParamSpecClassInfo *info = class_data;
717
718   class->value_type = info->value_type;
719   if (info->finalize)
720     class->finalize = info->finalize;                   /* optional */
721   class->value_set_default = info->value_set_default;
722   if (info->value_validate)
723     class->value_validate = info->value_validate;       /* optional */
724   class->values_cmp = info->values_cmp;
725   g_free (class_data);
726 }
727
728 static void
729 default_value_set_default (GParamSpec *pspec,
730                            GValue     *value)
731 {
732   /* value is already zero initialized */
733 }
734
735 static gint
736 default_values_cmp (GParamSpec   *pspec,
737                     const GValue *value1,
738                     const GValue *value2)
739 {
740   return memcmp (&value1->data, &value2->data, sizeof (value1->data));
741 }
742
743 GType
744 g_param_type_register_static (const gchar              *name,
745                               const GParamSpecTypeInfo *pspec_info)
746 {
747   GTypeInfo info = {
748     sizeof (GParamSpecClass),      /* class_size */
749     NULL,                          /* base_init */
750     NULL,                          /* base_destroy */
751     param_spec_generic_class_init, /* class_init */
752     NULL,                          /* class_destroy */
753     NULL,                          /* class_data */
754     0,                             /* instance_size */
755     16,                            /* n_preallocs */
756     NULL,                          /* instance_init */
757   };
758   ParamSpecClassInfo *cinfo;
759
760   g_return_val_if_fail (name != NULL, 0);
761   g_return_val_if_fail (pspec_info != NULL, 0);
762   g_return_val_if_fail (g_type_from_name (name) == 0, 0);
763   g_return_val_if_fail (pspec_info->instance_size >= sizeof (GParamSpec), 0);
764   g_return_val_if_fail (g_type_name (pspec_info->value_type) != NULL, 0);
765   /* default: g_return_val_if_fail (pspec_info->value_set_default != NULL, 0); */
766   /* optional: g_return_val_if_fail (pspec_info->value_validate != NULL, 0); */
767   /* default: g_return_val_if_fail (pspec_info->values_cmp != NULL, 0); */
768
769   info.instance_size = pspec_info->instance_size;
770   info.n_preallocs = pspec_info->n_preallocs;
771   info.instance_init = (GInstanceInitFunc) pspec_info->instance_init;
772   cinfo = g_new (ParamSpecClassInfo, 1);
773   cinfo->value_type = pspec_info->value_type;
774   cinfo->finalize = pspec_info->finalize;
775   cinfo->value_set_default = pspec_info->value_set_default ? pspec_info->value_set_default : default_value_set_default;
776   cinfo->value_validate = pspec_info->value_validate;
777   cinfo->values_cmp = pspec_info->values_cmp ? pspec_info->values_cmp : default_values_cmp;
778   info.class_data = cinfo;
779
780   return g_type_register_static (G_TYPE_PARAM, name, &info, 0);
781 }
782
783 void
784 g_value_set_param (GValue     *value,
785                    GParamSpec *param)
786 {
787   g_return_if_fail (G_IS_VALUE_PARAM (value));
788   if (param)
789     g_return_if_fail (G_IS_PARAM_SPEC (param));
790
791   if (value->data[0].v_pointer)
792     g_param_spec_unref (value->data[0].v_pointer);
793   value->data[0].v_pointer = param;
794   if (value->data[0].v_pointer)
795     g_param_spec_ref (value->data[0].v_pointer);
796 }
797
798 GParamSpec*
799 g_value_get_param (const GValue *value)
800 {
801   g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
802
803   return value->data[0].v_pointer;
804 }
805
806 GParamSpec*
807 g_value_dup_param (const GValue *value)
808 {
809   g_return_val_if_fail (G_IS_VALUE_PARAM (value), NULL);
810
811   return value->data[0].v_pointer ? g_param_spec_ref (value->data[0].v_pointer) : NULL;
812 }