added newly added gobject/ headers.
[platform/upstream/glib.git] / gobject / gvaluetypes.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 #include        "gvaluetypes.h"
20
21 #include        "gvaluecollector.h"
22 #include        <string.h>
23
24
25 /* --- value functions --- */
26 static void
27 value_long0_init (GValue *value)
28 {
29   value->data[0].v_long = 0;
30 }
31
32 static void
33 value_long0_copy (const GValue *src_value,
34                   GValue       *dest_value)
35 {
36   dest_value->data[0].v_long = src_value->data[0].v_long;
37 }
38
39 static gchar*
40 value_char_lcopy_value (const GValue *value,
41                         guint         nth_value,
42                         GType        *collect_type,
43                         GTypeCValue  *collect_value)
44 {
45   gint8 *int8_p = collect_value->v_pointer;
46   
47   if (!int8_p)
48     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
49   
50   *int8_p = value->data[0].v_int;
51   
52   *collect_type = 0;
53   return NULL;
54 }
55
56 static gchar*
57 value_boolean_lcopy_value (const GValue *value,
58                            guint         nth_value,
59                            GType        *collect_type,
60                            GTypeCValue  *collect_value)
61 {
62   gboolean *bool_p = collect_value->v_pointer;
63   
64   if (!bool_p)
65     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
66   
67   *bool_p = value->data[0].v_int;
68   
69   *collect_type = 0;
70   return NULL;
71 }
72
73 static gchar*
74 value_int_collect_value (GValue      *value,
75                          guint        nth_value,
76                          GType       *collect_type,
77                          GTypeCValue *collect_value)
78 {
79   value->data[0].v_int = collect_value->v_int;
80   
81   *collect_type = 0;
82   return NULL;
83 }
84
85 static gchar*
86 value_int_lcopy_value (const GValue *value,
87                        guint         nth_value,
88                        GType        *collect_type,
89                        GTypeCValue  *collect_value)
90 {
91   gint *int_p = collect_value->v_pointer;
92   
93   if (!int_p)
94     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
95   
96   *int_p = value->data[0].v_int;
97   
98   *collect_type = 0;
99   return NULL;
100 }
101
102 static gchar*
103 value_long_collect_value (GValue      *value,
104                           guint        nth_value,
105                           GType       *collect_type,
106                           GTypeCValue *collect_value)
107 {
108   value->data[0].v_long = collect_value->v_long;
109   
110   *collect_type = 0;
111   return NULL;
112 }
113
114 static gchar*
115 value_long_lcopy_value (const GValue *value,
116                         guint         nth_value,
117                         GType        *collect_type,
118                         GTypeCValue  *collect_value)
119 {
120   glong *long_p = collect_value->v_pointer;
121   
122   if (!long_p)
123     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
124   
125   *long_p = value->data[0].v_long;
126   
127   *collect_type = 0;
128   return NULL;
129 }
130
131 static void
132 value_float_init (GValue *value)
133 {
134   value->data[0].v_float = 0.0;
135 }
136
137 static void
138 value_float_copy (const GValue *src_value,
139                   GValue       *dest_value)
140 {
141   dest_value->data[0].v_float = src_value->data[0].v_float;
142 }
143
144 static gchar*
145 value_float_collect_value (GValue      *value,
146                            guint        nth_value,
147                            GType       *collect_type,
148                            GTypeCValue *collect_value)
149 {
150   value->data[0].v_float = collect_value->v_double;
151   
152   *collect_type = 0;
153   return NULL;
154 }
155
156 static gchar*
157 value_float_lcopy_value (const GValue *value,
158                          guint         nth_value,
159                          GType        *collect_type,
160                          GTypeCValue  *collect_value)
161 {
162   gfloat *float_p = collect_value->v_pointer;
163   
164   if (!float_p)
165     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
166   
167   *float_p = value->data[0].v_float;
168   
169   *collect_type = 0;
170   return NULL;
171 }
172
173 static void
174 value_double_init (GValue *value)
175 {
176   value->data[0].v_double = 0.0;
177 }
178
179 static void
180 value_double_copy (const GValue *src_value,
181                    GValue       *dest_value)
182 {
183   dest_value->data[0].v_double = src_value->data[0].v_double;
184 }
185
186 static gchar*
187 value_double_collect_value (GValue      *value,
188                             guint        nth_value,
189                             GType       *collect_type,
190                             GTypeCValue *collect_value)
191 {
192   value->data[0].v_double = collect_value->v_double;
193   
194   *collect_type = 0;
195   return NULL;
196 }
197
198 static gchar*
199 value_double_lcopy_value (const GValue *value,
200                           guint         nth_value,
201                           GType        *collect_type,
202                           GTypeCValue  *collect_value)
203 {
204   gdouble *double_p = collect_value->v_pointer;
205   
206   if (!double_p)
207     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
208   
209   *double_p = value->data[0].v_double;
210   
211   *collect_type = 0;
212   return NULL;
213 }
214
215 static void
216 value_string_init (GValue *value)
217 {
218   value->data[0].v_pointer = NULL;
219 }
220
221 static void
222 value_string_free_value (GValue *value)
223 {
224   g_free (value->data[0].v_pointer);
225 }
226
227 static void
228 value_string_copy_value (const GValue *src_value,
229                          GValue       *dest_value)
230 {
231   dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
232 }
233
234 static gchar*
235 value_string_collect_value (GValue      *value,
236                             guint        nth_value,
237                             GType       *collect_type,
238                             GTypeCValue *collect_value)
239 {
240   value->data[0].v_pointer = g_strdup (collect_value->v_pointer);
241   
242   *collect_type = 0;
243   return NULL;
244 }
245
246 static gchar*
247 value_string_lcopy_value (const GValue *value,
248                           guint         nth_value,
249                           GType        *collect_type,
250                           GTypeCValue  *collect_value)
251 {
252   gchar **string_p = collect_value->v_pointer;
253   
254   if (!string_p)
255     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
256   
257   *string_p = g_strdup (value->data[0].v_pointer);
258   
259   *collect_type = 0;
260   return NULL;
261 }
262
263 static void
264 value_pointer_init (GValue *value)
265 {
266   value->data[0].v_pointer = 0;
267 }
268
269 static void
270 value_pointer_copy (const GValue *src_value,
271                     GValue       *dest_value)
272 {
273   dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
274 }
275
276 static gpointer
277 value_pointer_peek_pointer (const GValue *value)
278 {
279   return value->data[0].v_pointer;
280 }
281
282 static gchar*
283 value_pointer_collect_value (GValue      *value,
284                              guint        nth_value,
285                              GType       *collect_type,
286                              GTypeCValue *collect_value)
287 {
288   value->data[0].v_pointer = collect_value->v_pointer;
289
290   *collect_type = 0;
291   return NULL;
292 }
293
294 static gchar*
295 value_pointer_lcopy_value (const GValue *value,
296                            guint         nth_value,
297                            GType        *collect_type,
298                            GTypeCValue  *collect_value)
299 {
300   gpointer *pointer_p = collect_value->v_pointer;
301
302   if (!pointer_p)
303     return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
304
305   *pointer_p = value->data[0].v_pointer;
306
307   *collect_type = 0;
308   return NULL;
309 }
310
311
312 /* --- type initialization --- */
313 void
314 g_value_types_init (void)  /* sync with gtype.c */
315 {
316   GTypeInfo info = {
317     0,                          /* class_size */
318     NULL,                       /* base_init */
319     NULL,                       /* base_destroy */
320     NULL,                       /* class_init */
321     NULL,                       /* class_destroy */
322     NULL,                       /* class_data */
323     0,                          /* instance_size */
324     0,                          /* n_preallocs */
325     NULL,                       /* instance_init */
326     NULL,                       /* value_table */
327   };
328   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
329   GType type;
330   
331   /* G_TYPE_CHAR / G_TYPE_UCHAR
332    */
333   {
334     static const GTypeValueTable value_table = {
335       value_long0_init,         /* value_init */
336       NULL,                     /* value_free */
337       value_long0_copy,         /* value_copy */
338       NULL,                     /* value_peek_pointer */
339       G_VALUE_COLLECT_INT,      /* collect_type */
340       value_int_collect_value,  /* collect_value */
341       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
342       value_char_lcopy_value,   /* lcopy_value */
343     };
344     info.value_table = &value_table;
345     type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo, 0);
346     g_assert (type == G_TYPE_CHAR);
347     type = g_type_register_fundamental (G_TYPE_UCHAR, "guchar", &info, &finfo, 0);
348     g_assert (type == G_TYPE_UCHAR);
349   }
350
351   /* G_TYPE_BOOLEAN
352    */
353   {
354     static const GTypeValueTable value_table = {
355       value_long0_init,          /* value_init */
356       NULL,                      /* value_free */
357       value_long0_copy,          /* value_copy */
358       NULL,                      /* value_peek_pointer */
359       G_VALUE_COLLECT_INT,       /* collect_type */
360       value_int_collect_value,   /* collect_value */
361       G_VALUE_COLLECT_POINTER,   /* lcopy_type */
362       value_boolean_lcopy_value, /* lcopy_value */
363     };
364     info.value_table = &value_table;
365     type = g_type_register_fundamental (G_TYPE_BOOLEAN, "gboolean", &info, &finfo, 0);
366     g_assert (type == G_TYPE_BOOLEAN);
367   }
368   
369   /* G_TYPE_INT / G_TYPE_UINT
370    */
371   {
372     static const GTypeValueTable value_table = {
373       value_long0_init,         /* value_init */
374       NULL,                     /* value_free */
375       value_long0_copy,         /* value_copy */
376       NULL,                     /* value_peek_pointer */
377       G_VALUE_COLLECT_INT,      /* collect_type */
378       value_int_collect_value,  /* collect_value */
379       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
380       value_int_lcopy_value,    /* lcopy_value */
381     };
382     info.value_table = &value_table;
383     type = g_type_register_fundamental (G_TYPE_INT, "gint", &info, &finfo, 0);
384     g_assert (type == G_TYPE_INT);
385     type = g_type_register_fundamental (G_TYPE_UINT, "guint", &info, &finfo, 0);
386     g_assert (type == G_TYPE_UINT);
387   }
388
389   /* G_TYPE_LONG / G_TYPE_ULONG
390    */
391   {
392     static const GTypeValueTable value_table = {
393       value_long0_init,         /* value_init */
394       NULL,                     /* value_free */
395       value_long0_copy,         /* value_copy */
396       NULL,                     /* value_peek_pointer */
397       G_VALUE_COLLECT_LONG,     /* collect_type */
398       value_long_collect_value, /* collect_value */
399       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
400       value_long_lcopy_value,   /* lcopy_value */
401     };
402     info.value_table = &value_table;
403     type = g_type_register_fundamental (G_TYPE_LONG, "glong", &info, &finfo, 0);
404     g_assert (type == G_TYPE_LONG);
405     type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo, 0);
406     g_assert (type == G_TYPE_ULONG);
407   }
408   
409   /* G_TYPE_FLOAT
410    */
411   {
412     static const GTypeValueTable value_table = {
413       value_float_init,          /* value_init */
414       NULL,                      /* value_free */
415       value_float_copy,          /* value_copy */
416       NULL,                      /* value_peek_pointer */
417       G_VALUE_COLLECT_DOUBLE,    /* collect_type */
418       value_float_collect_value, /* collect_value */
419       G_VALUE_COLLECT_POINTER,   /* lcopy_type */
420       value_float_lcopy_value,   /* lcopy_value */
421     };
422     info.value_table = &value_table;
423     type = g_type_register_fundamental (G_TYPE_FLOAT, "gfloat", &info, &finfo, 0);
424     g_assert (type == G_TYPE_FLOAT);
425   }
426   
427   /* G_TYPE_DOUBLE
428    */
429   {
430     static const GTypeValueTable value_table = {
431       value_double_init,          /* value_init */
432       NULL,                       /* value_free */
433       value_double_copy,          /* value_copy */
434       NULL,                       /* value_peek_pointer */
435       G_VALUE_COLLECT_DOUBLE,     /* collect_type */
436       value_double_collect_value, /* collect_value */
437       G_VALUE_COLLECT_POINTER,    /* lcopy_type */
438       value_double_lcopy_value,   /* lcopy_value */
439     };
440     info.value_table = &value_table;
441     type = g_type_register_fundamental (G_TYPE_DOUBLE, "gdouble", &info, &finfo, 0);
442     g_assert (type == G_TYPE_DOUBLE);
443   }
444
445   /* G_TYPE_STRING
446    */
447   {
448     static const GTypeValueTable value_table = {
449       value_string_init,          /* value_init */
450       value_string_free_value,    /* value_free */
451       value_string_copy_value,    /* value_copy */
452       value_pointer_peek_pointer, /* value_peek_pointer */
453       G_VALUE_COLLECT_POINTER,    /* collect_type */
454       value_string_collect_value, /* collect_value */
455       G_VALUE_COLLECT_POINTER,    /* lcopy_type */
456       value_string_lcopy_value,   /* lcopy_value */
457     };
458     info.value_table = &value_table;
459     type = g_type_register_fundamental (G_TYPE_STRING, "gstring", &info, &finfo, 0);
460     g_assert (type == G_TYPE_STRING);
461   }
462
463   /* G_TYPE_POINTER
464    */
465   {
466     static const GTypeValueTable value_table = {
467       value_pointer_init,          /* value_init */
468       NULL,                        /* value_free */
469       value_pointer_copy,          /* value_copy */
470       value_pointer_peek_pointer,  /* value_peek_pointer */
471       G_VALUE_COLLECT_POINTER,     /* collect_type */
472       value_pointer_collect_value, /* collect_value */
473       G_VALUE_COLLECT_POINTER,     /* lcopy_type */
474       value_pointer_lcopy_value,   /* lcopy_value */
475     };
476     info.value_table = &value_table;
477     type = g_type_register_fundamental (G_TYPE_POINTER, "gpointer", &info, &finfo, 0);
478     g_assert (type == G_TYPE_POINTER);
479   }
480 }
481
482
483 /* --- GValue functions --- */
484 void
485 g_value_set_char (GValue *value,
486                   gint8   v_char)
487 {
488   g_return_if_fail (G_IS_VALUE_CHAR (value));
489   
490   value->data[0].v_int = v_char;
491 }
492
493 gint8
494 g_value_get_char (const GValue *value)
495 {
496   g_return_val_if_fail (G_IS_VALUE_CHAR (value), 0);
497   
498   return value->data[0].v_int;
499 }
500
501 void
502 g_value_set_uchar (GValue *value,
503                    guint8  v_uchar)
504 {
505   g_return_if_fail (G_IS_VALUE_UCHAR (value));
506   
507   value->data[0].v_uint = v_uchar;
508 }
509
510 guint8
511 g_value_get_uchar (const GValue *value)
512 {
513   g_return_val_if_fail (G_IS_VALUE_UCHAR (value), 0);
514   
515   return value->data[0].v_uint;
516 }
517
518 void
519 g_value_set_boolean (GValue  *value,
520                      gboolean v_boolean)
521 {
522   g_return_if_fail (G_IS_VALUE_BOOLEAN (value));
523   
524   value->data[0].v_int = v_boolean;
525 }
526
527 gboolean
528 g_value_get_boolean (const GValue *value)
529 {
530   g_return_val_if_fail (G_IS_VALUE_BOOLEAN (value), 0);
531   
532   return value->data[0].v_int;
533 }
534
535 void
536 g_value_set_int (GValue *value,
537                  gint    v_int)
538 {
539   g_return_if_fail (G_IS_VALUE_INT (value));
540   
541   value->data[0].v_int = v_int;
542 }
543
544 gint
545 g_value_get_int (const GValue *value)
546 {
547   g_return_val_if_fail (G_IS_VALUE_INT (value), 0);
548   
549   return value->data[0].v_int;
550 }
551
552 void
553 g_value_set_uint (GValue *value,
554                   guint   v_uint)
555 {
556   g_return_if_fail (G_IS_VALUE_UINT (value));
557   
558   value->data[0].v_uint = v_uint;
559 }
560
561 guint
562 g_value_get_uint (const GValue *value)
563 {
564   g_return_val_if_fail (G_IS_VALUE_UINT (value), 0);
565   
566   return value->data[0].v_uint;
567 }
568
569 void
570 g_value_set_long (GValue *value,
571                   glong   v_long)
572 {
573   g_return_if_fail (G_IS_VALUE_LONG (value));
574   
575   value->data[0].v_long = v_long;
576 }
577
578 glong
579 g_value_get_long (const GValue *value)
580 {
581   g_return_val_if_fail (G_IS_VALUE_LONG (value), 0);
582   
583   return value->data[0].v_long;
584 }
585
586 void
587 g_value_set_ulong (GValue *value,
588                    gulong  v_ulong)
589 {
590   g_return_if_fail (G_IS_VALUE_ULONG (value));
591   
592   value->data[0].v_ulong = v_ulong;
593 }
594
595 gulong
596 g_value_get_ulong (const GValue *value)
597 {
598   g_return_val_if_fail (G_IS_VALUE_ULONG (value), 0);
599   
600   return value->data[0].v_ulong;
601 }
602
603 void
604 g_value_set_float (GValue *value,
605                    gfloat  v_float)
606 {
607   g_return_if_fail (G_IS_VALUE_FLOAT (value));
608   
609   value->data[0].v_float = v_float;
610 }
611
612 gfloat
613 g_value_get_float (const GValue *value)
614 {
615   g_return_val_if_fail (G_IS_VALUE_FLOAT (value), 0);
616   
617   return value->data[0].v_float;
618 }
619
620 void
621 g_value_set_double (GValue *value,
622                     gdouble v_double)
623 {
624   g_return_if_fail (G_IS_VALUE_DOUBLE (value));
625   
626   value->data[0].v_double = v_double;
627 }
628
629 gdouble
630 g_value_get_double (const GValue *value)
631 {
632   g_return_val_if_fail (G_IS_VALUE_DOUBLE (value), 0);
633   
634   return value->data[0].v_double;
635 }
636
637 void
638 g_value_set_string (GValue      *value,
639                     const gchar *v_string)
640 {
641   g_return_if_fail (G_IS_VALUE_STRING (value));
642   
643   g_free (value->data[0].v_pointer);
644   value->data[0].v_pointer = g_strdup (v_string);
645 }
646
647 gchar*
648 g_value_get_string (const GValue *value)
649 {
650   g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
651   
652   return value->data[0].v_pointer;
653 }
654
655 gchar*
656 g_value_dup_string (const GValue *value)
657 {
658   g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
659   
660   return g_strdup (value->data[0].v_pointer);
661 }
662
663 void
664 g_value_set_pointer (GValue  *value,
665                      gpointer v_pointer)
666 {
667   g_return_if_fail (G_IS_VALUE_POINTER (value));
668
669   value->data[0].v_pointer = v_pointer;
670 }
671
672 gpointer
673 g_value_get_pointer (GValue *value)
674 {
675   g_return_val_if_fail (G_IS_VALUE_POINTER (value), NULL);
676
677   return value->data[0].v_pointer;
678 }