define gstring in terms of gchar*. this typedef reflects the type name of
[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
264 /* --- type initialization --- */
265 void
266 g_value_types_init (void)  /* sync with gtype.c */
267 {
268   GTypeInfo info = {
269     0,                          /* class_size */
270     NULL,                       /* base_init */
271     NULL,                       /* base_destroy */
272     NULL,                       /* class_init */
273     NULL,                       /* class_destroy */
274     NULL,                       /* class_data */
275     0,                          /* instance_size */
276     0,                          /* n_preallocs */
277     NULL,                       /* instance_init */
278     NULL,                       /* value_table */
279   };
280   const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
281   GType type;
282   
283   /* G_TYPE_CHAR / G_TYPE_UCHAR
284    */
285   {
286     static const GTypeValueTable value_table = {
287       value_long0_init,         /* value_init */
288       NULL,                     /* value_free */
289       value_long0_copy,         /* value_copy */
290       G_VALUE_COLLECT_INT,      /* collect_type */
291       value_int_collect_value,  /* collect_value */
292       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
293       value_char_lcopy_value,   /* lcopy_value */
294     };
295     info.value_table = &value_table;
296     type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo);
297     g_assert (type == G_TYPE_CHAR);
298     type = g_type_register_fundamental (G_TYPE_UCHAR, "guchar", &info, &finfo);
299     g_assert (type == G_TYPE_UCHAR);
300   }
301
302   /* G_TYPE_BOOLEAN
303    */
304   {
305     static const GTypeValueTable value_table = {
306       value_long0_init,          /* value_init */
307       NULL,                      /* value_free */
308       value_long0_copy,          /* value_copy */
309       G_VALUE_COLLECT_INT,       /* collect_type */
310       value_int_collect_value,   /* collect_value */
311       G_VALUE_COLLECT_POINTER,   /* lcopy_type */
312       value_boolean_lcopy_value, /* lcopy_value */
313     };
314     info.value_table = &value_table;
315     type = g_type_register_fundamental (G_TYPE_BOOLEAN, "gboolean", &info, &finfo);
316     g_assert (type == G_TYPE_BOOLEAN);
317   }
318   
319   /* G_TYPE_INT / G_TYPE_UINT
320    */
321   {
322     static const GTypeValueTable value_table = {
323       value_long0_init,         /* value_init */
324       NULL,                     /* value_free */
325       value_long0_copy,         /* value_copy */
326       G_VALUE_COLLECT_INT,      /* collect_type */
327       value_int_collect_value,  /* collect_value */
328       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
329       value_int_lcopy_value,    /* lcopy_value */
330     };
331     info.value_table = &value_table;
332     type = g_type_register_fundamental (G_TYPE_INT, "gint", &info, &finfo);
333     g_assert (type == G_TYPE_INT);
334     type = g_type_register_fundamental (G_TYPE_UINT, "guint", &info, &finfo);
335     g_assert (type == G_TYPE_UINT);
336   }
337
338   /* G_TYPE_LONG / G_TYPE_ULONG
339    */
340   {
341     static const GTypeValueTable value_table = {
342       value_long0_init,         /* value_init */
343       NULL,                     /* value_free */
344       value_long0_copy,         /* value_copy */
345       G_VALUE_COLLECT_LONG,     /* collect_type */
346       value_long_collect_value, /* collect_value */
347       G_VALUE_COLLECT_POINTER,  /* lcopy_type */
348       value_long_lcopy_value,   /* lcopy_value */
349     };
350     info.value_table = &value_table;
351     type = g_type_register_fundamental (G_TYPE_LONG, "glong", &info, &finfo);
352     g_assert (type == G_TYPE_LONG);
353     type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo);
354     g_assert (type == G_TYPE_ULONG);
355   }
356   
357   /* G_TYPE_FLOAT
358    */
359   {
360     static const GTypeValueTable value_table = {
361       value_float_init,          /* value_init */
362       NULL,                      /* value_free */
363       value_float_copy,          /* value_copy */
364       G_VALUE_COLLECT_DOUBLE,    /* collect_type */
365       value_float_collect_value, /* collect_value */
366       G_VALUE_COLLECT_POINTER,   /* lcopy_type */
367       value_float_lcopy_value,   /* lcopy_value */
368     };
369     info.value_table = &value_table;
370     type = g_type_register_fundamental (G_TYPE_FLOAT, "gfloat", &info, &finfo);
371     g_assert (type == G_TYPE_FLOAT);
372   }
373   
374   /* G_TYPE_DOUBLE
375    */
376   {
377     static const GTypeValueTable value_table = {
378       value_double_init,          /* value_init */
379       NULL,                       /* value_free */
380       value_double_copy,          /* value_copy */
381       G_VALUE_COLLECT_DOUBLE,     /* collect_type */
382       value_double_collect_value, /* collect_value */
383       G_VALUE_COLLECT_POINTER,    /* lcopy_type */
384       value_double_lcopy_value,   /* lcopy_value */
385     };
386     info.value_table = &value_table;
387     type = g_type_register_fundamental (G_TYPE_DOUBLE, "gdouble", &info, &finfo);
388     g_assert (type == G_TYPE_DOUBLE);
389   }
390
391   /* G_TYPE_STRING
392    */
393   {
394     static const GTypeValueTable value_table = {
395       value_string_init,          /* value_init */
396       value_string_free_value,    /* value_free */
397       value_string_copy_value,    /* value_copy */
398       G_VALUE_COLLECT_POINTER,    /* collect_type */
399       value_string_collect_value, /* collect_value */
400       G_VALUE_COLLECT_POINTER,    /* lcopy_type */
401       value_string_lcopy_value,   /* lcopy_value */
402     };
403     info.value_table = &value_table;
404     type = g_type_register_fundamental (G_TYPE_STRING, "gstring", &info, &finfo);
405     g_assert (type == G_TYPE_STRING);
406   }
407 }
408
409
410 /* --- GValue functions --- */
411 void
412 g_value_set_char (GValue *value,
413                   gint8   v_char)
414 {
415   g_return_if_fail (G_IS_VALUE_CHAR (value));
416   
417   value->data[0].v_int = v_char;
418 }
419
420 gint8
421 g_value_get_char (GValue *value)
422 {
423   g_return_val_if_fail (G_IS_VALUE_CHAR (value), 0);
424   
425   return value->data[0].v_int;
426 }
427
428 void
429 g_value_set_uchar (GValue *value,
430                    guint8  v_uchar)
431 {
432   g_return_if_fail (G_IS_VALUE_UCHAR (value));
433   
434   value->data[0].v_uint = v_uchar;
435 }
436
437 guint8
438 g_value_get_uchar (GValue *value)
439 {
440   g_return_val_if_fail (G_IS_VALUE_UCHAR (value), 0);
441   
442   return value->data[0].v_uint;
443 }
444
445 void
446 g_value_set_boolean (GValue  *value,
447                      gboolean v_boolean)
448 {
449   g_return_if_fail (G_IS_VALUE_BOOLEAN (value));
450   
451   value->data[0].v_int = v_boolean;
452 }
453
454 gboolean
455 g_value_get_boolean (GValue *value)
456 {
457   g_return_val_if_fail (G_IS_VALUE_BOOLEAN (value), 0);
458   
459   return value->data[0].v_int;
460 }
461
462 void
463 g_value_set_int (GValue *value,
464                  gint    v_int)
465 {
466   g_return_if_fail (G_IS_VALUE_INT (value));
467   
468   value->data[0].v_int = v_int;
469 }
470
471 gint
472 g_value_get_int (GValue *value)
473 {
474   g_return_val_if_fail (G_IS_VALUE_INT (value), 0);
475   
476   return value->data[0].v_int;
477 }
478
479 void
480 g_value_set_uint (GValue *value,
481                   guint   v_uint)
482 {
483   g_return_if_fail (G_IS_VALUE_UINT (value));
484   
485   value->data[0].v_uint = v_uint;
486 }
487
488 guint
489 g_value_get_uint (GValue *value)
490 {
491   g_return_val_if_fail (G_IS_VALUE_UINT (value), 0);
492   
493   return value->data[0].v_uint;
494 }
495
496 void
497 g_value_set_long (GValue *value,
498                   glong   v_long)
499 {
500   g_return_if_fail (G_IS_VALUE_LONG (value));
501   
502   value->data[0].v_long = v_long;
503 }
504
505 glong
506 g_value_get_long (GValue *value)
507 {
508   g_return_val_if_fail (G_IS_VALUE_LONG (value), 0);
509   
510   return value->data[0].v_long;
511 }
512
513 void
514 g_value_set_ulong (GValue *value,
515                    gulong  v_ulong)
516 {
517   g_return_if_fail (G_IS_VALUE_ULONG (value));
518   
519   value->data[0].v_ulong = v_ulong;
520 }
521
522 gulong
523 g_value_get_ulong (GValue *value)
524 {
525   g_return_val_if_fail (G_IS_VALUE_ULONG (value), 0);
526   
527   return value->data[0].v_ulong;
528 }
529
530 void
531 g_value_set_float (GValue *value,
532                    gfloat  v_float)
533 {
534   g_return_if_fail (G_IS_VALUE_FLOAT (value));
535   
536   value->data[0].v_float = v_float;
537 }
538
539 gfloat
540 g_value_get_float (GValue *value)
541 {
542   g_return_val_if_fail (G_IS_VALUE_FLOAT (value), 0);
543   
544   return value->data[0].v_float;
545 }
546
547 void
548 g_value_set_double (GValue *value,
549                     gdouble v_double)
550 {
551   g_return_if_fail (G_IS_VALUE_DOUBLE (value));
552   
553   value->data[0].v_double = v_double;
554 }
555
556 gdouble
557 g_value_get_double (GValue *value)
558 {
559   g_return_val_if_fail (G_IS_VALUE_DOUBLE (value), 0);
560   
561   return value->data[0].v_double;
562 }
563
564 void
565 g_value_set_string (GValue      *value,
566                     const gchar *v_string)
567 {
568   g_return_if_fail (G_IS_VALUE_STRING (value));
569   
570   g_free (value->data[0].v_pointer);
571   value->data[0].v_pointer = g_strdup (v_string);
572 }
573
574 gchar*
575 g_value_get_string (GValue *value)
576 {
577   g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
578   
579   return value->data[0].v_pointer;
580 }
581
582 gchar*
583 g_value_dup_string (GValue *value)
584 {
585   g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
586   
587   return g_strdup (value->data[0].v_pointer);
588 }