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"
25 /* --- value functions --- */
27 value_long0_init (GValue *value)
29 value->data[0].v_long = 0;
33 value_long0_copy (const GValue *src_value,
36 dest_value->data[0].v_long = src_value->data[0].v_long;
40 value_char_lcopy_value (const GValue *value,
43 GTypeCValue *collect_value)
45 gint8 *int8_p = collect_value->v_pointer;
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;
57 value_boolean_lcopy_value (const GValue *value,
60 GTypeCValue *collect_value)
62 gboolean *bool_p = collect_value->v_pointer;
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;
74 value_int_collect_value (GValue *value,
77 GTypeCValue *collect_value)
79 value->data[0].v_int = collect_value->v_int;
86 value_int_lcopy_value (const GValue *value,
89 GTypeCValue *collect_value)
91 gint *int_p = collect_value->v_pointer;
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;
103 value_long_collect_value (GValue *value,
106 GTypeCValue *collect_value)
108 value->data[0].v_long = collect_value->v_long;
115 value_long_lcopy_value (const GValue *value,
118 GTypeCValue *collect_value)
120 glong *long_p = collect_value->v_pointer;
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;
132 value_float_init (GValue *value)
134 value->data[0].v_float = 0.0;
138 value_float_copy (const GValue *src_value,
141 dest_value->data[0].v_float = src_value->data[0].v_float;
145 value_float_collect_value (GValue *value,
148 GTypeCValue *collect_value)
150 value->data[0].v_float = collect_value->v_double;
157 value_float_lcopy_value (const GValue *value,
160 GTypeCValue *collect_value)
162 gfloat *float_p = collect_value->v_pointer;
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;
174 value_double_init (GValue *value)
176 value->data[0].v_double = 0.0;
180 value_double_copy (const GValue *src_value,
183 dest_value->data[0].v_double = src_value->data[0].v_double;
187 value_double_collect_value (GValue *value,
190 GTypeCValue *collect_value)
192 value->data[0].v_double = collect_value->v_double;
199 value_double_lcopy_value (const GValue *value,
202 GTypeCValue *collect_value)
204 gdouble *double_p = collect_value->v_pointer;
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;
216 value_string_init (GValue *value)
218 value->data[0].v_pointer = NULL;
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);
229 value_string_copy_value (const GValue *src_value,
232 dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
236 value_string_collect_value (GValue *value,
239 GTypeCValue *collect_value)
241 value->data[0].v_pointer = g_strdup (collect_value->v_pointer);
248 value_string_lcopy_value (const GValue *value,
251 GTypeCValue *collect_value)
253 gchar **string_p = collect_value->v_pointer;
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);
265 value_pointer_init (GValue *value)
267 value->data[0].v_pointer = NULL;
271 value_pointer_copy (const GValue *src_value,
274 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
278 value_pointer_peek_pointer (const GValue *value)
280 return value->data[0].v_pointer;
284 value_pointer_collect_value (GValue *value,
287 GTypeCValue *collect_value)
289 value->data[0].v_pointer = collect_value->v_pointer;
296 value_pointer_lcopy_value (const GValue *value,
299 GTypeCValue *collect_value)
301 gpointer *pointer_p = collect_value->v_pointer;
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;
313 value_ccallback_init (GValue *value)
315 value->data[0].v_pointer = NULL;
316 value->data[1].v_pointer = NULL;
320 value_ccallback_copy (const GValue *src_value,
323 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
324 dest_value->data[1].v_pointer = src_value->data[1].v_pointer;
328 value_ccallback_collect_value (GValue *value,
331 GTypeCValue *collect_value)
338 value->data[0].v_pointer = collect_value->v_pointer;
339 *collect_type = G_VALUE_COLLECT_POINTER;
340 if (!value->data[0].v_pointer)
341 error = g_strconcat ("invalid (NULL) pointer callback function for value type `",
342 G_VALUE_TYPE_NAME (value),
347 value->data[1].v_pointer = collect_value->v_pointer;
356 value_ccallback_lcopy_value (const GValue *value,
359 GTypeCValue *collect_value)
361 gpointer *pointer_p = collect_value->v_pointer;
364 return g_strdup_printf ("%s location for `%s' passed as NULL",
365 nth_value ? "data" : "callback",
366 G_VALUE_TYPE_NAME (value));
370 *pointer_p = value->data[0].v_pointer;
371 *collect_type = G_VALUE_COLLECT_POINTER;
374 *pointer_p = value->data[1].v_pointer;
383 /* --- type initialization --- */
385 g_value_types_init (void) /* sync with gtype.c */
389 NULL, /* base_init */
390 NULL, /* base_destroy */
391 NULL, /* class_init */
392 NULL, /* class_destroy */
393 NULL, /* class_data */
394 0, /* instance_size */
396 NULL, /* instance_init */
397 NULL, /* value_table */
399 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
402 /* G_TYPE_CHAR / G_TYPE_UCHAR
405 static const GTypeValueTable value_table = {
406 value_long0_init, /* value_init */
407 NULL, /* value_free */
408 value_long0_copy, /* value_copy */
409 NULL, /* value_peek_pointer */
410 G_VALUE_COLLECT_INT, /* collect_type */
411 value_int_collect_value, /* collect_value */
412 G_VALUE_COLLECT_POINTER, /* lcopy_type */
413 value_char_lcopy_value, /* lcopy_value */
415 info.value_table = &value_table;
416 type = g_type_register_fundamental (G_TYPE_CHAR, "gchar", &info, &finfo, 0);
417 g_assert (type == G_TYPE_CHAR);
418 type = g_type_register_fundamental (G_TYPE_UCHAR, "guchar", &info, &finfo, 0);
419 g_assert (type == G_TYPE_UCHAR);
425 static const GTypeValueTable value_table = {
426 value_long0_init, /* value_init */
427 NULL, /* value_free */
428 value_long0_copy, /* value_copy */
429 NULL, /* value_peek_pointer */
430 G_VALUE_COLLECT_INT, /* collect_type */
431 value_int_collect_value, /* collect_value */
432 G_VALUE_COLLECT_POINTER, /* lcopy_type */
433 value_boolean_lcopy_value, /* lcopy_value */
435 info.value_table = &value_table;
436 type = g_type_register_fundamental (G_TYPE_BOOLEAN, "gboolean", &info, &finfo, 0);
437 g_assert (type == G_TYPE_BOOLEAN);
440 /* G_TYPE_INT / G_TYPE_UINT
443 static const GTypeValueTable value_table = {
444 value_long0_init, /* value_init */
445 NULL, /* value_free */
446 value_long0_copy, /* value_copy */
447 NULL, /* value_peek_pointer */
448 G_VALUE_COLLECT_INT, /* collect_type */
449 value_int_collect_value, /* collect_value */
450 G_VALUE_COLLECT_POINTER, /* lcopy_type */
451 value_int_lcopy_value, /* lcopy_value */
453 info.value_table = &value_table;
454 type = g_type_register_fundamental (G_TYPE_INT, "gint", &info, &finfo, 0);
455 g_assert (type == G_TYPE_INT);
456 type = g_type_register_fundamental (G_TYPE_UINT, "guint", &info, &finfo, 0);
457 g_assert (type == G_TYPE_UINT);
460 /* G_TYPE_LONG / G_TYPE_ULONG
463 static const GTypeValueTable value_table = {
464 value_long0_init, /* value_init */
465 NULL, /* value_free */
466 value_long0_copy, /* value_copy */
467 NULL, /* value_peek_pointer */
468 G_VALUE_COLLECT_LONG, /* collect_type */
469 value_long_collect_value, /* collect_value */
470 G_VALUE_COLLECT_POINTER, /* lcopy_type */
471 value_long_lcopy_value, /* lcopy_value */
473 info.value_table = &value_table;
474 type = g_type_register_fundamental (G_TYPE_LONG, "glong", &info, &finfo, 0);
475 g_assert (type == G_TYPE_LONG);
476 type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo, 0);
477 g_assert (type == G_TYPE_ULONG);
483 static const GTypeValueTable value_table = {
484 value_float_init, /* value_init */
485 NULL, /* value_free */
486 value_float_copy, /* value_copy */
487 NULL, /* value_peek_pointer */
488 G_VALUE_COLLECT_DOUBLE, /* collect_type */
489 value_float_collect_value, /* collect_value */
490 G_VALUE_COLLECT_POINTER, /* lcopy_type */
491 value_float_lcopy_value, /* lcopy_value */
493 info.value_table = &value_table;
494 type = g_type_register_fundamental (G_TYPE_FLOAT, "gfloat", &info, &finfo, 0);
495 g_assert (type == G_TYPE_FLOAT);
501 static const GTypeValueTable value_table = {
502 value_double_init, /* value_init */
503 NULL, /* value_free */
504 value_double_copy, /* value_copy */
505 NULL, /* value_peek_pointer */
506 G_VALUE_COLLECT_DOUBLE, /* collect_type */
507 value_double_collect_value, /* collect_value */
508 G_VALUE_COLLECT_POINTER, /* lcopy_type */
509 value_double_lcopy_value, /* lcopy_value */
511 info.value_table = &value_table;
512 type = g_type_register_fundamental (G_TYPE_DOUBLE, "gdouble", &info, &finfo, 0);
513 g_assert (type == G_TYPE_DOUBLE);
519 static const GTypeValueTable value_table = {
520 value_string_init, /* value_init */
521 value_string_free_value, /* value_free */
522 value_string_copy_value, /* value_copy */
523 value_pointer_peek_pointer, /* value_peek_pointer */
524 G_VALUE_COLLECT_POINTER, /* collect_type */
525 value_string_collect_value, /* collect_value */
526 G_VALUE_COLLECT_POINTER, /* lcopy_type */
527 value_string_lcopy_value, /* lcopy_value */
529 info.value_table = &value_table;
530 type = g_type_register_fundamental (G_TYPE_STRING, "gstring", &info, &finfo, 0);
531 g_assert (type == G_TYPE_STRING);
537 static const GTypeValueTable value_table = {
538 value_pointer_init, /* value_init */
539 NULL, /* value_free */
540 value_pointer_copy, /* value_copy */
541 value_pointer_peek_pointer, /* value_peek_pointer */
542 G_VALUE_COLLECT_POINTER, /* collect_type */
543 value_pointer_collect_value, /* collect_value */
544 G_VALUE_COLLECT_POINTER, /* lcopy_type */
545 value_pointer_lcopy_value, /* lcopy_value */
547 info.value_table = &value_table;
548 type = g_type_register_fundamental (G_TYPE_POINTER, "gpointer", &info, &finfo, 0);
549 g_assert (type == G_TYPE_POINTER);
555 static const GTypeValueTable value_table = {
556 value_ccallback_init, /* value_init */
557 NULL, /* value_free */
558 value_ccallback_copy, /* value_copy */
559 NULL, /* value_peek_pointer */
560 G_VALUE_COLLECT_POINTER, /* collect_type */
561 value_ccallback_collect_value, /* collect_value */
562 G_VALUE_COLLECT_POINTER, /* lcopy_type */
563 value_ccallback_lcopy_value, /* lcopy_value */
565 info.value_table = &value_table;
566 type = g_type_register_fundamental (G_TYPE_CCALLBACK, "GCCallback", &info, &finfo, 0);
567 g_assert (type == G_TYPE_CCALLBACK);
572 /* --- GValue functions --- */
574 g_value_set_char (GValue *value,
577 g_return_if_fail (G_IS_VALUE_CHAR (value));
579 value->data[0].v_int = v_char;
583 g_value_get_char (const GValue *value)
585 g_return_val_if_fail (G_IS_VALUE_CHAR (value), 0);
587 return value->data[0].v_int;
591 g_value_set_uchar (GValue *value,
594 g_return_if_fail (G_IS_VALUE_UCHAR (value));
596 value->data[0].v_uint = v_uchar;
600 g_value_get_uchar (const GValue *value)
602 g_return_val_if_fail (G_IS_VALUE_UCHAR (value), 0);
604 return value->data[0].v_uint;
608 g_value_set_boolean (GValue *value,
611 g_return_if_fail (G_IS_VALUE_BOOLEAN (value));
613 value->data[0].v_int = v_boolean;
617 g_value_get_boolean (const GValue *value)
619 g_return_val_if_fail (G_IS_VALUE_BOOLEAN (value), 0);
621 return value->data[0].v_int;
625 g_value_set_int (GValue *value,
628 g_return_if_fail (G_IS_VALUE_INT (value));
630 value->data[0].v_int = v_int;
634 g_value_get_int (const GValue *value)
636 g_return_val_if_fail (G_IS_VALUE_INT (value), 0);
638 return value->data[0].v_int;
642 g_value_set_uint (GValue *value,
645 g_return_if_fail (G_IS_VALUE_UINT (value));
647 value->data[0].v_uint = v_uint;
651 g_value_get_uint (const GValue *value)
653 g_return_val_if_fail (G_IS_VALUE_UINT (value), 0);
655 return value->data[0].v_uint;
659 g_value_set_long (GValue *value,
662 g_return_if_fail (G_IS_VALUE_LONG (value));
664 value->data[0].v_long = v_long;
668 g_value_get_long (const GValue *value)
670 g_return_val_if_fail (G_IS_VALUE_LONG (value), 0);
672 return value->data[0].v_long;
676 g_value_set_ulong (GValue *value,
679 g_return_if_fail (G_IS_VALUE_ULONG (value));
681 value->data[0].v_ulong = v_ulong;
685 g_value_get_ulong (const GValue *value)
687 g_return_val_if_fail (G_IS_VALUE_ULONG (value), 0);
689 return value->data[0].v_ulong;
693 g_value_set_float (GValue *value,
696 g_return_if_fail (G_IS_VALUE_FLOAT (value));
698 value->data[0].v_float = v_float;
702 g_value_get_float (const GValue *value)
704 g_return_val_if_fail (G_IS_VALUE_FLOAT (value), 0);
706 return value->data[0].v_float;
710 g_value_set_double (GValue *value,
713 g_return_if_fail (G_IS_VALUE_DOUBLE (value));
715 value->data[0].v_double = v_double;
719 g_value_get_double (const GValue *value)
721 g_return_val_if_fail (G_IS_VALUE_DOUBLE (value), 0);
723 return value->data[0].v_double;
727 g_value_set_string (GValue *value,
728 const gchar *v_string)
730 g_return_if_fail (G_IS_VALUE_STRING (value));
732 if (value->data[1].v_uint & G_VALUE_STATIC_TAG)
733 value->data[1].v_uint = 0;
735 g_free (value->data[0].v_pointer);
736 value->data[0].v_pointer = g_strdup (v_string);
740 g_value_set_static_string (GValue *value,
741 const gchar *v_string)
743 g_return_if_fail (G_IS_VALUE_STRING (value));
745 if (!(value->data[1].v_uint & G_VALUE_STATIC_TAG))
746 g_free (value->data[0].v_pointer);
747 value->data[1].v_uint = G_VALUE_STATIC_TAG;
748 value->data[0].v_pointer = (gchar*) v_string;
752 g_value_get_string (const GValue *value)
754 g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
756 return value->data[0].v_pointer;
760 g_value_dup_string (const GValue *value)
762 g_return_val_if_fail (G_IS_VALUE_STRING (value), NULL);
764 return g_strdup (value->data[0].v_pointer);
768 g_value_set_pointer (GValue *value,
771 g_return_if_fail (G_IS_VALUE_POINTER (value));
773 value->data[0].v_pointer = v_pointer;
777 g_value_get_pointer (const GValue *value)
779 g_return_val_if_fail (G_IS_VALUE_POINTER (value), NULL);
781 return value->data[0].v_pointer;
785 g_value_set_ccallback (GValue *value,
786 gpointer callback_func,
787 gpointer callback_data)
789 g_return_if_fail (G_IS_VALUE_CCALLBACK (value));
791 value->data[0].v_pointer = callback_func;
792 value->data[1].v_pointer = callback_data;
796 g_value_get_ccallback (const GValue *value,
797 gpointer *callback_func,
798 gpointer *callback_data)
800 g_return_if_fail (G_IS_VALUE_CCALLBACK (value));
803 *callback_func = value->data[0].v_pointer;
805 *callback_data = value->data[1].v_pointer;