revert accidentally commited change
[glib.git] / gobject / gvaluetypes.c
blobe3406aa7139661069070562e3d4a9cda2a3a9c37
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997, 1998, 1999, 2000 Tim Janik and Red Hat, Inc.
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.
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.
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.
19 #include "gvaluetypes.h"
21 #include "gvaluecollector.h"
22 #include <string.h>
25 /* --- value functions --- */
26 static void
27 value_long0_init (GValue *value)
29 value->data[0].v_long = 0;
32 static void
33 value_long0_copy (const GValue *src_value,
34 GValue *dest_value)
36 dest_value->data[0].v_long = src_value->data[0].v_long;
39 static gchar*
40 value_char_lcopy_value (const GValue *value,
41 guint nth_value,
42 GType *collect_type,
43 GTypeCValue *collect_value)
45 gint8 *int8_p = collect_value->v_pointer;
47 if (!int8_p)
48 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
50 *int8_p = value->data[0].v_int;
52 *collect_type = 0;
53 return NULL;
56 static gchar*
57 value_boolean_lcopy_value (const GValue *value,
58 guint nth_value,
59 GType *collect_type,
60 GTypeCValue *collect_value)
62 gboolean *bool_p = collect_value->v_pointer;
64 if (!bool_p)
65 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
67 *bool_p = value->data[0].v_int;
69 *collect_type = 0;
70 return NULL;
73 static gchar*
74 value_int_collect_value (GValue *value,
75 guint nth_value,
76 GType *collect_type,
77 GTypeCValue *collect_value)
79 value->data[0].v_int = collect_value->v_int;
81 *collect_type = 0;
82 return NULL;
85 static gchar*
86 value_int_lcopy_value (const GValue *value,
87 guint nth_value,
88 GType *collect_type,
89 GTypeCValue *collect_value)
91 gint *int_p = collect_value->v_pointer;
93 if (!int_p)
94 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
96 *int_p = value->data[0].v_int;
98 *collect_type = 0;
99 return NULL;
102 static gchar*
103 value_long_collect_value (GValue *value,
104 guint nth_value,
105 GType *collect_type,
106 GTypeCValue *collect_value)
108 value->data[0].v_long = collect_value->v_long;
110 *collect_type = 0;
111 return NULL;
114 static gchar*
115 value_long_lcopy_value (const GValue *value,
116 guint nth_value,
117 GType *collect_type,
118 GTypeCValue *collect_value)
120 glong *long_p = collect_value->v_pointer;
122 if (!long_p)
123 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
125 *long_p = value->data[0].v_long;
127 *collect_type = 0;
128 return NULL;
131 static void
132 value_float_init (GValue *value)
134 value->data[0].v_float = 0.0;
137 static void
138 value_float_copy (const GValue *src_value,
139 GValue *dest_value)
141 dest_value->data[0].v_float = src_value->data[0].v_float;
144 static gchar*
145 value_float_collect_value (GValue *value,
146 guint nth_value,
147 GType *collect_type,
148 GTypeCValue *collect_value)
150 value->data[0].v_float = collect_value->v_double;
152 *collect_type = 0;
153 return NULL;
156 static gchar*
157 value_float_lcopy_value (const GValue *value,
158 guint nth_value,
159 GType *collect_type,
160 GTypeCValue *collect_value)
162 gfloat *float_p = collect_value->v_pointer;
164 if (!float_p)
165 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
167 *float_p = value->data[0].v_float;
169 *collect_type = 0;
170 return NULL;
173 static void
174 value_double_init (GValue *value)
176 value->data[0].v_double = 0.0;
179 static void
180 value_double_copy (const GValue *src_value,
181 GValue *dest_value)
183 dest_value->data[0].v_double = src_value->data[0].v_double;
186 static gchar*
187 value_double_collect_value (GValue *value,
188 guint nth_value,
189 GType *collect_type,
190 GTypeCValue *collect_value)
192 value->data[0].v_double = collect_value->v_double;
194 *collect_type = 0;
195 return NULL;
198 static gchar*
199 value_double_lcopy_value (const GValue *value,
200 guint nth_value,
201 GType *collect_type,
202 GTypeCValue *collect_value)
204 gdouble *double_p = collect_value->v_pointer;
206 if (!double_p)
207 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
209 *double_p = value->data[0].v_double;
211 *collect_type = 0;
212 return NULL;
215 static void
216 value_string_init (GValue *value)
218 value->data[0].v_pointer = NULL;
221 static void
222 value_string_free_value (GValue *value)
224 if (!(value->data[1].v_uint & G_VALUE_STATIC_TAG))
225 g_free (value->data[0].v_pointer);
228 static void
229 value_string_copy_value (const GValue *src_value,
230 GValue *dest_value)
232 dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
235 static gchar*
236 value_string_collect_value (GValue *value,
237 guint nth_value,
238 GType *collect_type,
239 GTypeCValue *collect_value)
241 value->data[0].v_pointer = g_strdup (collect_value->v_pointer);
243 *collect_type = 0;
244 return NULL;
247 static gchar*
248 value_string_lcopy_value (const GValue *value,
249 guint nth_value,
250 GType *collect_type,
251 GTypeCValue *collect_value)
253 gchar **string_p = collect_value->v_pointer;
255 if (!string_p)
256 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
258 *string_p = g_strdup (value->data[0].v_pointer);
260 *collect_type = 0;
261 return NULL;
264 static void
265 value_pointer_init (GValue *value)
267 value->data[0].v_pointer = 0;
270 static void
271 value_pointer_copy (const GValue *src_value,
272 GValue *dest_value)
274 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
277 static gpointer
278 value_pointer_peek_pointer (const GValue *value)
280 return value->data[0].v_pointer;
283 static gchar*
284 value_pointer_collect_value (GValue *value,
285 guint nth_value,
286 GType *collect_type,
287 GTypeCValue *collect_value)
289 value->data[0].v_pointer = collect_value->v_pointer;
291 *collect_type = 0;
292 return NULL;
295 static gchar*
296 value_pointer_lcopy_value (const GValue *value,
297 guint nth_value,
298 GType *collect_type,
299 GTypeCValue *collect_value)
301 gpointer *pointer_p = collect_value->v_pointer;
303 if (!pointer_p)
304 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
306 *pointer_p = value->data[0].v_pointer;
308 *collect_type = 0;
309 return NULL;
313 /* --- type initialization --- */
314 void
315 g_value_types_init (void) /* sync with gtype.c */
317 GTypeInfo info = {
318 0, /* class_size */
319 NULL, /* base_init */
320 NULL, /* base_destroy */
321 NULL, /* class_init */
322 NULL, /* class_destroy */
323 NULL, /* class_data */
324 0, /* instance_size */
325 0, /* n_preallocs */
326 NULL, /* instance_init */
327 NULL, /* value_table */
329 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
330 GType type;
332 /* G_TYPE_CHAR / G_TYPE_UCHAR
335 static const GTypeValueTable value_table = {
336 value_long0_init, /* value_init */
337 NULL, /* value_free */
338 value_long0_copy, /* value_copy */
339 NULL, /* value_peek_pointer */
340 G_VALUE_COLLECT_INT, /* collect_type */
341 value_int_collect_value, /* collect_value */
342 G_VALUE_COLLECT_POINTER, /* lcopy_type */
343 value_char_lcopy_value, /* lcopy_value */
345 info.value_table = &value_table;
346 type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo, 0);
347 g_assert (type == G_TYPE_CHAR);
348 type = g_type_register_fundamental (G_TYPE_UCHAR, "guchar", &info, &finfo, 0);
349 g_assert (type == G_TYPE_UCHAR);
352 /* G_TYPE_BOOLEAN
355 static const GTypeValueTable value_table = {
356 value_long0_init, /* value_init */
357 NULL, /* value_free */
358 value_long0_copy, /* value_copy */
359 NULL, /* value_peek_pointer */
360 G_VALUE_COLLECT_INT, /* collect_type */
361 value_int_collect_value, /* collect_value */
362 G_VALUE_COLLECT_POINTER, /* lcopy_type */
363 value_boolean_lcopy_value, /* lcopy_value */
365 info.value_table = &value_table;
366 type = g_type_register_fundamental (G_TYPE_BOOLEAN, "gboolean", &info, &finfo, 0);
367 g_assert (type == G_TYPE_BOOLEAN);
370 /* G_TYPE_INT / G_TYPE_UINT
373 static const GTypeValueTable value_table = {
374 value_long0_init, /* value_init */
375 NULL, /* value_free */
376 value_long0_copy, /* value_copy */
377 NULL, /* value_peek_pointer */
378 G_VALUE_COLLECT_INT, /* collect_type */
379 value_int_collect_value, /* collect_value */
380 G_VALUE_COLLECT_POINTER, /* lcopy_type */
381 value_int_lcopy_value, /* lcopy_value */
383 info.value_table = &value_table;
384 type = g_type_register_fundamental (G_TYPE_INT, "gint", &info, &finfo, 0);
385 g_assert (type == G_TYPE_INT);
386 type = g_type_register_fundamental (G_TYPE_UINT, "guint", &info, &finfo, 0);
387 g_assert (type == G_TYPE_UINT);
390 /* G_TYPE_LONG / G_TYPE_ULONG
393 static const GTypeValueTable value_table = {
394 value_long0_init, /* value_init */
395 NULL, /* value_free */
396 value_long0_copy, /* value_copy */
397 NULL, /* value_peek_pointer */
398 G_VALUE_COLLECT_LONG, /* collect_type */
399 value_long_collect_value, /* collect_value */
400 G_VALUE_COLLECT_POINTER, /* lcopy_type */
401 value_long_lcopy_value, /* lcopy_value */
403 info.value_table = &value_table;
404 type = g_type_register_fundamental (G_TYPE_LONG, "glong", &info, &finfo, 0);
405 g_assert (type == G_TYPE_LONG);
406 type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo, 0);
407 g_assert (type == G_TYPE_ULONG);
410 /* G_TYPE_FLOAT
413 static const GTypeValueTable value_table = {
414 value_float_init, /* value_init */
415 NULL, /* value_free */
416 value_float_copy, /* value_copy */
417 NULL, /* value_peek_pointer */
418 G_VALUE_COLLECT_DOUBLE, /* collect_type */
419 value_float_collect_value, /* collect_value */
420 G_VALUE_COLLECT_POINTER, /* lcopy_type */
421 value_float_lcopy_value, /* lcopy_value */
423 info.value_table = &value_table;
424 type = g_type_register_fundamental (G_TYPE_FLOAT, "gfloat", &info, &finfo, 0);
425 g_assert (type == G_TYPE_FLOAT);
428 /* G_TYPE_DOUBLE
431 static const GTypeValueTable value_table = {
432 value_double_init, /* value_init */
433 NULL, /* value_free */
434 value_double_copy, /* value_copy */
435 NULL, /* value_peek_pointer */
436 G_VALUE_COLLECT_DOUBLE, /* collect_type */
437 value_double_collect_value, /* collect_value */
438 G_VALUE_COLLECT_POINTER, /* lcopy_type */
439 value_double_lcopy_value, /* lcopy_value */
441 info.value_table = &value_table;
442 type = g_type_register_fundamental (G_TYPE_DOUBLE, "gdouble", &info, &finfo, 0);
443 g_assert (type == G_TYPE_DOUBLE);
446 /* G_TYPE_STRING
449 static const GTypeValueTable value_table = {
450 value_string_init, /* value_init */
451 value_string_free_value, /* value_free */
452 value_string_copy_value, /* value_copy */
453 value_pointer_peek_pointer, /* value_peek_pointer */
454 G_VALUE_COLLECT_POINTER, /* collect_type */
455 value_string_collect_value, /* collect_value */
456 G_VALUE_COLLECT_POINTER, /* lcopy_type */
457 value_string_lcopy_value, /* lcopy_value */
459 info.value_table = &value_table;
460 type = g_type_register_fundamental (G_TYPE_STRING, "gstring", &info, &finfo, 0);
461 g_assert (type == G_TYPE_STRING);
464 /* G_TYPE_POINTER
467 static const GTypeValueTable value_table = {
468 value_pointer_init, /* value_init */
469 NULL, /* value_free */
470 value_pointer_copy, /* value_copy */
471 value_pointer_peek_pointer, /* value_peek_pointer */
472 G_VALUE_COLLECT_POINTER, /* collect_type */
473 value_pointer_collect_value, /* collect_value */
474 G_VALUE_COLLECT_POINTER, /* lcopy_type */
475 value_pointer_lcopy_value, /* lcopy_value */
477 info.value_table = &value_table;
478 type = g_type_register_fundamental (G_TYPE_POINTER, "gpointer", &info, &finfo, 0);
479 g_assert (type == G_TYPE_POINTER);
484 /* --- GValue functions --- */
485 void
486 g_value_set_char (GValue *value,
487 gint8 v_char)
489 g_return_if_fail (G_IS_VALUE_CHAR (value));
491 value->data[0].v_int = v_char;
494 gint8
495 g_value_get_char (const GValue *value)
497 g_return_val_if_fail (G_IS_VALUE_CHAR (value), 0);
499 return value->data[0].v_int;
502 void
503 g_value_set_uchar (GValue *value,
504 guint8 v_uchar)
506 g_return_if_fail (G_IS_VALUE_UCHAR (value));
508 value->data[0].v_uint = v_uchar;
511 guint8
512 g_value_get_uchar (const GValue *value)
514 g_return_val_if_fail (G_IS_VALUE_UCHAR (value), 0);
516 return value->data[0].v_uint;
519 void
520 g_value_set_boolean (GValue *value,
521 gboolean v_boolean)
523 g_return_if_fail (G_IS_VALUE_BOOLEAN (value));
525 value->data[0].v_int = v_boolean;
528 gboolean
529 g_value_get_boolean (const GValue *value)
531 g_return_val_if_fail (G_IS_VALUE_BOOLEAN (value), 0);
533 return value->data[0].v_int;
536 void
537 g_value_set_int (GValue *value,
538 gint v_int)
540 g_return_if_fail (G_IS_VALUE_INT (value));
542 value->data[0].v_int = v_int;
545 gint
546 g_value_get_int (const GValue *value)
548 g_return_val_if_fail (G_IS_VALUE_INT (value), 0);
550 return value->data[0].v_int;
553 void
554 g_value_set_uint (GValue *value,
555 guint v_uint)
557 g_return_if_fail (G_IS_VALUE_UINT (value));
559 value->data[0].v_uint = v_uint;
562 guint
563 g_value_get_uint (const GValue *value)
565 g_return_val_if_fail (G_IS_VALUE_UINT (value), 0);
567 return value->data[0].v_uint;
570 void
571 g_value_set_long (GValue *value,
572 glong v_long)
574 g_return_if_fail (G_IS_VALUE_LONG (value));
576 value->data[0].v_long = v_long;
579 glong
580 g_value_get_long (const GValue *value)
582 g_return_val_if_fail (G_IS_VALUE_LONG (value), 0);
584 return value->data[0].v_long;
587 void
588 g_value_set_ulong (GValue *value,
589 gulong v_ulong)
591 g_return_if_fail (G_IS_VALUE_ULONG (value));
593 value->data[0].v_ulong = v_ulong;
596 gulong
597 g_value_get_ulong (const GValue *value)
599 g_return_val_if_fail (G_IS_VALUE_ULONG (value), 0);
601 return value->data[0].v_ulong;
604 void
605 g_value_set_float (GValue *value,
606 gfloat v_float)
608 g_return_if_fail (G_IS_VALUE_FLOAT (value));
610 value->data[0].v_float = v_float;
613 gfloat
614 g_value_get_float (const GValue *value)
616 g_return_val_if_fail (G_IS_VALUE_FLOAT (value), 0);
618 return value->data[0].v_float;
621 void
622 g_value_set_double (GValue *value,
623 gdouble v_double)
625 g_return_if_fail (G_IS_VALUE_DOUBLE (value));
627 value->data[0].v_double = v_double;
630 gdouble
631 g_value_get_double (const GValue *value)
633 g_return_val_if_fail (G_IS_VALUE_DOUBLE (value), 0);
635 return value->data[0].v_double;
638 void
639 g_value_set_string (GValue *value,
640 const gchar *v_string)
642 g_return_if_fail (G_IS_VALUE_STRING (value));
644 if (value->data[1].v_uint & G_VALUE_STATIC_TAG)
645 value->data[1].v_uint = 0;
646 else
647 g_free (value->data[0].v_pointer);
648 value->data[0].v_pointer = g_strdup (v_string);
651 void
652 g_value_set_static_string (GValue *value,
653 const gchar *v_string)
655 g_return_if_fail (G_IS_VALUE_STRING (value));
657 if (!(value->data[1].v_uint & G_VALUE_STATIC_TAG))
658 g_free (value->data[0].v_pointer);
659 value->data[1].v_uint = G_VALUE_STATIC_TAG;
660 value->data[0].v_pointer = (gchar*) v_string;
663 gchar*
664 g_value_get_string (const GValue *value)
666 g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
668 return value->data[0].v_pointer;
671 gchar*
672 g_value_dup_string (const GValue *value)
674 g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
676 return g_strdup (value->data[0].v_pointer);
679 void
680 g_value_set_pointer (GValue *value,
681 gpointer v_pointer)
683 g_return_if_fail (G_IS_VALUE_POINTER (value));
685 value->data[0].v_pointer = v_pointer;
688 gpointer
689 g_value_get_pointer (const GValue *value)
691 g_return_val_if_fail (G_IS_VALUE_POINTER (value), NULL);
693 return value->data[0].v_pointer;