1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1997-1999, 2000-2001 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.
24 #include "gvaluetypes.h"
26 #include "gvaluecollector.h"
33 #include "gobjectalias.h"
35 #include <stdlib.h> /* qsort() */
38 /* --- value functions --- */
40 value_init_long0 (GValue *value)
42 value->data[0].v_long = 0;
46 value_copy_long0 (const GValue *src_value,
49 dest_value->data[0].v_long = src_value->data[0].v_long;
53 value_lcopy_char (const GValue *value,
54 guint n_collect_values,
55 GTypeCValue *collect_values,
58 gint8 *int8_p = collect_values[0].v_pointer;
61 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
63 *int8_p = value->data[0].v_int;
69 value_lcopy_boolean (const GValue *value,
70 guint n_collect_values,
71 GTypeCValue *collect_values,
74 gboolean *bool_p = collect_values[0].v_pointer;
77 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
79 *bool_p = value->data[0].v_int;
85 value_collect_int (GValue *value,
86 guint n_collect_values,
87 GTypeCValue *collect_values,
90 value->data[0].v_int = collect_values[0].v_int;
96 value_lcopy_int (const GValue *value,
97 guint n_collect_values,
98 GTypeCValue *collect_values,
101 gint *int_p = collect_values[0].v_pointer;
104 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
106 *int_p = value->data[0].v_int;
112 value_collect_long (GValue *value,
113 guint n_collect_values,
114 GTypeCValue *collect_values,
117 value->data[0].v_long = collect_values[0].v_long;
123 value_lcopy_long (const GValue *value,
124 guint n_collect_values,
125 GTypeCValue *collect_values,
128 glong *long_p = collect_values[0].v_pointer;
131 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
133 *long_p = value->data[0].v_long;
139 value_init_int64 (GValue *value)
141 value->data[0].v_int64 = 0;
145 value_copy_int64 (const GValue *src_value,
148 dest_value->data[0].v_int64 = src_value->data[0].v_int64;
152 value_collect_int64 (GValue *value,
153 guint n_collect_values,
154 GTypeCValue *collect_values,
157 value->data[0].v_int64 = collect_values[0].v_int64;
163 value_lcopy_int64 (const GValue *value,
164 guint n_collect_values,
165 GTypeCValue *collect_values,
168 gint64 *int64_p = collect_values[0].v_pointer;
171 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
173 *int64_p = value->data[0].v_int64;
179 value_init_float (GValue *value)
181 value->data[0].v_float = 0.0;
185 value_copy_float (const GValue *src_value,
188 dest_value->data[0].v_float = src_value->data[0].v_float;
192 value_collect_float (GValue *value,
193 guint n_collect_values,
194 GTypeCValue *collect_values,
197 value->data[0].v_float = collect_values[0].v_double;
203 value_lcopy_float (const GValue *value,
204 guint n_collect_values,
205 GTypeCValue *collect_values,
208 gfloat *float_p = collect_values[0].v_pointer;
211 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
213 *float_p = value->data[0].v_float;
219 value_init_double (GValue *value)
221 value->data[0].v_double = 0.0;
225 value_copy_double (const GValue *src_value,
228 dest_value->data[0].v_double = src_value->data[0].v_double;
232 value_collect_double (GValue *value,
233 guint n_collect_values,
234 GTypeCValue *collect_values,
237 value->data[0].v_double = collect_values[0].v_double;
243 value_lcopy_double (const GValue *value,
244 guint n_collect_values,
245 GTypeCValue *collect_values,
248 gdouble *double_p = collect_values[0].v_pointer;
251 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
253 *double_p = value->data[0].v_double;
259 value_init_string (GValue *value)
261 value->data[0].v_pointer = NULL;
265 value_free_string (GValue *value)
267 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
268 g_free (value->data[0].v_pointer);
272 value_copy_string (const GValue *src_value,
275 dest_value->data[0].v_pointer = g_strdup (src_value->data[0].v_pointer);
279 value_collect_string (GValue *value,
280 guint n_collect_values,
281 GTypeCValue *collect_values,
284 if (!collect_values[0].v_pointer)
285 value->data[0].v_pointer = NULL;
286 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
288 value->data[0].v_pointer = collect_values[0].v_pointer;
289 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
292 value->data[0].v_pointer = g_strdup (collect_values[0].v_pointer);
298 value_lcopy_string (const GValue *value,
299 guint n_collect_values,
300 GTypeCValue *collect_values,
303 gchar **string_p = collect_values[0].v_pointer;
306 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
308 if (!value->data[0].v_pointer)
310 else if (collect_flags & G_VALUE_NOCOPY_CONTENTS)
311 *string_p = value->data[0].v_pointer;
313 *string_p = g_strdup (value->data[0].v_pointer);
319 value_init_pointer (GValue *value)
321 value->data[0].v_pointer = NULL;
325 value_copy_pointer (const GValue *src_value,
328 dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
332 value_peek_pointer0 (const GValue *value)
334 return value->data[0].v_pointer;
338 value_collect_pointer (GValue *value,
339 guint n_collect_values,
340 GTypeCValue *collect_values,
343 value->data[0].v_pointer = collect_values[0].v_pointer;
349 value_lcopy_pointer (const GValue *value,
350 guint n_collect_values,
351 GTypeCValue *collect_values,
354 gpointer *pointer_p = collect_values[0].v_pointer;
357 return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
359 *pointer_p = value->data[0].v_pointer;
365 /* --- type initialization --- */
367 g_value_types_init (void)
371 NULL, /* base_init */
372 NULL, /* base_destroy */
373 NULL, /* class_init */
374 NULL, /* class_destroy */
375 NULL, /* class_data */
376 0, /* instance_size */
378 NULL, /* instance_init */
379 NULL, /* value_table */
381 const GTypeFundamentalInfo finfo = { G_TYPE_FLAG_DERIVABLE, };
384 /* G_TYPE_CHAR / G_TYPE_UCHAR
387 static const GTypeValueTable value_table = {
388 value_init_long0, /* value_init */
389 NULL, /* value_free */
390 value_copy_long0, /* value_copy */
391 NULL, /* value_peek_pointer */
392 "i", /* collect_format */
393 value_collect_int, /* collect_value */
394 "p", /* lcopy_format */
395 value_lcopy_char, /* lcopy_value */
397 info.value_table = &value_table;
398 type = g_type_register_fundamental (G_TYPE_CHAR, g_intern_static_string ("gchar"), &info, &finfo, 0);
399 g_assert (type == G_TYPE_CHAR);
400 type = g_type_register_fundamental (G_TYPE_UCHAR, g_intern_static_string ("guchar"), &info, &finfo, 0);
401 g_assert (type == G_TYPE_UCHAR);
407 static const GTypeValueTable value_table = {
408 value_init_long0, /* value_init */
409 NULL, /* value_free */
410 value_copy_long0, /* value_copy */
411 NULL, /* value_peek_pointer */
412 "i", /* collect_format */
413 value_collect_int, /* collect_value */
414 "p", /* lcopy_format */
415 value_lcopy_boolean, /* lcopy_value */
417 info.value_table = &value_table;
418 type = g_type_register_fundamental (G_TYPE_BOOLEAN, g_intern_static_string ("gboolean"), &info, &finfo, 0);
419 g_assert (type == G_TYPE_BOOLEAN);
422 /* G_TYPE_INT / G_TYPE_UINT
425 static const GTypeValueTable value_table = {
426 value_init_long0, /* value_init */
427 NULL, /* value_free */
428 value_copy_long0, /* value_copy */
429 NULL, /* value_peek_pointer */
430 "i", /* collect_format */
431 value_collect_int, /* collect_value */
432 "p", /* lcopy_format */
433 value_lcopy_int, /* lcopy_value */
435 info.value_table = &value_table;
436 type = g_type_register_fundamental (G_TYPE_INT, g_intern_static_string ("gint"), &info, &finfo, 0);
437 g_assert (type == G_TYPE_INT);
438 type = g_type_register_fundamental (G_TYPE_UINT, g_intern_static_string ("guint"), &info, &finfo, 0);
439 g_assert (type == G_TYPE_UINT);
442 /* G_TYPE_LONG / G_TYPE_ULONG
445 static const GTypeValueTable value_table = {
446 value_init_long0, /* value_init */
447 NULL, /* value_free */
448 value_copy_long0, /* value_copy */
449 NULL, /* value_peek_pointer */
450 "l", /* collect_format */
451 value_collect_long, /* collect_value */
452 "p", /* lcopy_format */
453 value_lcopy_long, /* lcopy_value */
455 info.value_table = &value_table;
456 type = g_type_register_fundamental (G_TYPE_LONG, g_intern_static_string ("glong"), &info, &finfo, 0);
457 g_assert (type == G_TYPE_LONG);
458 type = g_type_register_fundamental (G_TYPE_ULONG, g_intern_static_string ("gulong"), &info, &finfo, 0);
459 g_assert (type == G_TYPE_ULONG);
462 /* G_TYPE_INT64 / G_TYPE_UINT64
465 static const GTypeValueTable value_table = {
466 value_init_int64, /* value_init */
467 NULL, /* value_free */
468 value_copy_int64, /* value_copy */
469 NULL, /* value_peek_pointer */
470 "q", /* collect_format */
471 value_collect_int64, /* collect_value */
472 "p", /* lcopy_format */
473 value_lcopy_int64, /* lcopy_value */
475 info.value_table = &value_table;
476 type = g_type_register_fundamental (G_TYPE_INT64, g_intern_static_string ("gint64"), &info, &finfo, 0);
477 g_assert (type == G_TYPE_INT64);
478 type = g_type_register_fundamental (G_TYPE_UINT64, g_intern_static_string ("guint64"), &info, &finfo, 0);
479 g_assert (type == G_TYPE_UINT64);
485 static const GTypeValueTable value_table = {
486 value_init_float, /* value_init */
487 NULL, /* value_free */
488 value_copy_float, /* value_copy */
489 NULL, /* value_peek_pointer */
490 "d", /* collect_format */
491 value_collect_float, /* collect_value */
492 "p", /* lcopy_format */
493 value_lcopy_float, /* lcopy_value */
495 info.value_table = &value_table;
496 type = g_type_register_fundamental (G_TYPE_FLOAT, g_intern_static_string ("gfloat"), &info, &finfo, 0);
497 g_assert (type == G_TYPE_FLOAT);
503 static const GTypeValueTable value_table = {
504 value_init_double, /* value_init */
505 NULL, /* value_free */
506 value_copy_double, /* value_copy */
507 NULL, /* value_peek_pointer */
508 "d", /* collect_format */
509 value_collect_double, /* collect_value */
510 "p", /* lcopy_format */
511 value_lcopy_double, /* lcopy_value */
513 info.value_table = &value_table;
514 type = g_type_register_fundamental (G_TYPE_DOUBLE, g_intern_static_string ("gdouble"), &info, &finfo, 0);
515 g_assert (type == G_TYPE_DOUBLE);
521 static const GTypeValueTable value_table = {
522 value_init_string, /* value_init */
523 value_free_string, /* value_free */
524 value_copy_string, /* value_copy */
525 value_peek_pointer0, /* value_peek_pointer */
526 "p", /* collect_format */
527 value_collect_string, /* collect_value */
528 "p", /* lcopy_format */
529 value_lcopy_string, /* lcopy_value */
531 info.value_table = &value_table;
532 type = g_type_register_fundamental (G_TYPE_STRING, g_intern_static_string ("gchararray"), &info, &finfo, 0);
533 g_assert (type == G_TYPE_STRING);
539 static const GTypeValueTable value_table = {
540 value_init_pointer, /* value_init */
541 NULL, /* value_free */
542 value_copy_pointer, /* value_copy */
543 value_peek_pointer0, /* value_peek_pointer */
544 "p", /* collect_format */
545 value_collect_pointer, /* collect_value */
546 "p", /* lcopy_format */
547 value_lcopy_pointer, /* lcopy_value */
549 info.value_table = &value_table;
550 type = g_type_register_fundamental (G_TYPE_POINTER, g_intern_static_string ("gpointer"), &info, &finfo, 0);
551 g_assert (type == G_TYPE_POINTER);
556 /* --- GValue functions --- */
558 g_value_set_char (GValue *value,
561 g_return_if_fail (G_VALUE_HOLDS_CHAR (value));
563 value->data[0].v_int = v_char;
567 g_value_get_char (const GValue *value)
569 g_return_val_if_fail (G_VALUE_HOLDS_CHAR (value), 0);
571 return value->data[0].v_int;
575 g_value_set_uchar (GValue *value,
578 g_return_if_fail (G_VALUE_HOLDS_UCHAR (value));
580 value->data[0].v_uint = v_uchar;
584 g_value_get_uchar (const GValue *value)
586 g_return_val_if_fail (G_VALUE_HOLDS_UCHAR (value), 0);
588 return value->data[0].v_uint;
592 g_value_set_boolean (GValue *value,
595 g_return_if_fail (G_VALUE_HOLDS_BOOLEAN (value));
597 value->data[0].v_int = v_boolean != FALSE;
601 g_value_get_boolean (const GValue *value)
603 g_return_val_if_fail (G_VALUE_HOLDS_BOOLEAN (value), 0);
605 return value->data[0].v_int;
609 g_value_set_int (GValue *value,
612 g_return_if_fail (G_VALUE_HOLDS_INT (value));
614 value->data[0].v_int = v_int;
618 g_value_get_int (const GValue *value)
620 g_return_val_if_fail (G_VALUE_HOLDS_INT (value), 0);
622 return value->data[0].v_int;
626 g_value_set_uint (GValue *value,
629 g_return_if_fail (G_VALUE_HOLDS_UINT (value));
631 value->data[0].v_uint = v_uint;
635 g_value_get_uint (const GValue *value)
637 g_return_val_if_fail (G_VALUE_HOLDS_UINT (value), 0);
639 return value->data[0].v_uint;
643 g_value_set_long (GValue *value,
646 g_return_if_fail (G_VALUE_HOLDS_LONG (value));
648 value->data[0].v_long = v_long;
652 g_value_get_long (const GValue *value)
654 g_return_val_if_fail (G_VALUE_HOLDS_LONG (value), 0);
656 return value->data[0].v_long;
660 g_value_set_ulong (GValue *value,
663 g_return_if_fail (G_VALUE_HOLDS_ULONG (value));
665 value->data[0].v_ulong = v_ulong;
669 g_value_get_ulong (const GValue *value)
671 g_return_val_if_fail (G_VALUE_HOLDS_ULONG (value), 0);
673 return value->data[0].v_ulong;
677 g_value_set_int64 (GValue *value,
680 g_return_if_fail (G_VALUE_HOLDS_INT64 (value));
682 value->data[0].v_int64 = v_int64;
686 g_value_get_int64 (const GValue *value)
688 g_return_val_if_fail (G_VALUE_HOLDS_INT64 (value), 0);
690 return value->data[0].v_int64;
694 g_value_set_uint64 (GValue *value,
697 g_return_if_fail (G_VALUE_HOLDS_UINT64 (value));
699 value->data[0].v_uint64 = v_uint64;
703 g_value_get_uint64 (const GValue *value)
705 g_return_val_if_fail (G_VALUE_HOLDS_UINT64 (value), 0);
707 return value->data[0].v_uint64;
711 g_value_set_float (GValue *value,
714 g_return_if_fail (G_VALUE_HOLDS_FLOAT (value));
716 value->data[0].v_float = v_float;
720 g_value_get_float (const GValue *value)
722 g_return_val_if_fail (G_VALUE_HOLDS_FLOAT (value), 0);
724 return value->data[0].v_float;
728 g_value_set_double (GValue *value,
731 g_return_if_fail (G_VALUE_HOLDS_DOUBLE (value));
733 value->data[0].v_double = v_double;
737 g_value_get_double (const GValue *value)
739 g_return_val_if_fail (G_VALUE_HOLDS_DOUBLE (value), 0);
741 return value->data[0].v_double;
745 g_value_set_string (GValue *value,
746 const gchar *v_string)
750 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
752 new_val = g_strdup (v_string);
754 if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
755 value->data[1].v_uint = 0;
757 g_free (value->data[0].v_pointer);
759 value->data[0].v_pointer = new_val;
763 g_value_set_static_string (GValue *value,
764 const gchar *v_string)
766 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
768 if (!(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS))
769 g_free (value->data[0].v_pointer);
770 value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS;
771 value->data[0].v_pointer = (gchar*) v_string;
775 g_value_set_string_take_ownership (GValue *value,
778 g_value_take_string (value, v_string);
782 g_value_take_string (GValue *value,
785 g_return_if_fail (G_VALUE_HOLDS_STRING (value));
787 if (value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)
788 value->data[1].v_uint = 0;
790 g_free (value->data[0].v_pointer);
791 value->data[0].v_pointer = v_string;
794 G_CONST_RETURN gchar*
795 g_value_get_string (const GValue *value)
797 g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
799 return value->data[0].v_pointer;
803 g_value_dup_string (const GValue *value)
805 g_return_val_if_fail (G_VALUE_HOLDS_STRING (value), NULL);
807 return g_strdup (value->data[0].v_pointer);
811 g_value_set_pointer (GValue *value,
814 g_return_if_fail (G_VALUE_HOLDS_POINTER (value));
816 value->data[0].v_pointer = v_pointer;
820 g_value_get_pointer (const GValue *value)
822 g_return_val_if_fail (G_VALUE_HOLDS_POINTER (value), NULL);
824 return value->data[0].v_pointer;
828 g_gtype_get_type (void)
830 static const GTypeInfo type_info = { 0, };
833 type = g_type_register_static (G_TYPE_POINTER, g_intern_static_string ("GType"), &type_info, 0);
838 g_value_set_gtype (GValue *value,
841 g_return_if_fail (G_VALUE_HOLDS_GTYPE (value));
843 value->data[0].v_long = v_gtype;
848 g_value_get_gtype (const GValue *value)
850 g_return_val_if_fail (G_VALUE_HOLDS_GTYPE (value), 0);
852 return value->data[0].v_long;
856 * g_strdup_value_contents:
857 * @value: #GValue which contents are to be described.
859 * Return a newly allocated string, which describes the contents of a #GValue.
860 * The main purpose of this function is to describe #GValue contents for
861 * debugging output, the way in which the contents are described may change
862 * between different GLib versions.
864 * Returns: Newly allocated string.
867 g_strdup_value_contents (const GValue *value)
872 g_return_val_if_fail (G_IS_VALUE (value), NULL);
874 if (G_VALUE_HOLDS_STRING (value))
876 src = g_value_get_string (value);
879 contents = g_strdup ("NULL");
882 gchar *s = g_strescape (src, NULL);
884 contents = g_strdup_printf ("\"%s\"", s);
888 else if (g_value_type_transformable (G_VALUE_TYPE (value), G_TYPE_STRING))
890 GValue tmp_value = { 0, };
893 g_value_init (&tmp_value, G_TYPE_STRING);
894 g_value_transform (value, &tmp_value);
895 s = g_strescape (g_value_get_string (&tmp_value), NULL);
896 g_value_unset (&tmp_value);
897 if (G_VALUE_HOLDS_ENUM (value) || G_VALUE_HOLDS_FLAGS (value))
898 contents = g_strdup_printf ("((%s) %s)",
899 g_type_name (G_VALUE_TYPE (value)),
902 contents = g_strdup (s ? s : "NULL");
905 else if (g_value_fits_pointer (value))
907 gpointer p = g_value_peek_pointer (value);
910 contents = g_strdup ("NULL");
911 else if (G_VALUE_HOLDS_OBJECT (value))
912 contents = g_strdup_printf ("((%s*) %p)", G_OBJECT_TYPE_NAME (p), p);
913 else if (G_VALUE_HOLDS_PARAM (value))
914 contents = g_strdup_printf ("((%s*) %p)", G_PARAM_SPEC_TYPE_NAME (p), p);
915 else if (G_VALUE_HOLDS_BOXED (value))
916 contents = g_strdup_printf ("((%s*) %p)", g_type_name (G_VALUE_TYPE (value)), p);
917 else if (G_VALUE_HOLDS_POINTER (value))
918 contents = g_strdup_printf ("((gpointer) %p)", p);
920 contents = g_strdup ("???");
923 contents = g_strdup ("???");
929 * g_pointer_type_register_static:
930 * @name: the name of the new pointer type.
932 * Creates a new %G_TYPE_POINTER derived type id for a new
933 * pointer type with name @name.
935 * Returns: a new %G_TYPE_POINTER derived type id for @name.
938 g_pointer_type_register_static (const gchar *name)
940 static const GTypeInfo type_info = {
942 NULL, /* base_init */
943 NULL, /* base_finalize */
944 NULL, /* class_init */
945 NULL, /* class_finalize */
946 NULL, /* class_data */
947 0, /* instance_size */
949 NULL, /* instance_init */
950 NULL /* value_table */
954 g_return_val_if_fail (name != NULL, 0);
955 g_return_val_if_fail (g_type_from_name (name) == 0, 0);
957 type = g_type_register_static (G_TYPE_POINTER, name, &type_info, 0);
962 #define __G_VALUETYPES_C__
963 #include "gobjectaliasdef.c"