1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-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.
19 * gvaluecollector.h: GValue varargs stubs
22 * SECTION:value_collection
23 * @Short_description: Converting varargs to generic values
24 * @See_also:#GValueTable
25 * @Title: Varargs Value Collection
27 * The macros in this section provide the varargs parsing support needed
28 * in variadic GObject functions such as g_object_new() or g_object_set().
29 * They currently support the collection of integral types, floating point
32 #ifndef __G_VALUE_COLLECTOR_H__
33 #define __G_VALUE_COLLECTOR_H__
35 #include <glib-object.h>
39 /* we may want to add aggregate types here some day, if requested
40 * by users. the basic C types are covered already, everything
41 * smaller than an int is promoted to an integer and floats are
42 * always promoted to doubles for varargs call constructions.
46 G_VALUE_COLLECT_INT = 'i',
47 G_VALUE_COLLECT_LONG = 'l',
48 G_VALUE_COLLECT_INT64 = 'q',
49 G_VALUE_COLLECT_DOUBLE = 'd',
50 G_VALUE_COLLECT_POINTER = 'p'
54 /* vararg union holding actuall values collected
59 * A union holding one collected value.
71 * G_VALUE_COLLECT_INIT:
72 * @value: a #GValue return location. @value is supposed to be clean (freshly
73 * initialized and/or freed).
74 * @_value_type: the #GType to use for @value.
75 * @var_args: the va_list variable; it may be evaluated multiple times
76 * @flags: flags which are passed on to the collect_value() function of
77 * the #GTypeValueTable of @value.
78 * @__error: a #gchar** variable that will be modified to hold a g_new()
79 * allocated error messages if something fails
81 * Collects a variable argument value from a va_list. We have to
82 * implement the varargs collection as a macro, because on some systems
83 * va_list variables cannot be passed by reference.
87 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
89 GValue *_value = (value); \
90 guint _flags = (flags); \
91 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
92 gchar *_collect_format = _vtable->collect_format; \
93 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
94 guint _n_values = 0; \
96 _value->g_type = _value_type; /* value_meminit() from gvalue.c */ \
97 while (*_collect_format) \
99 GTypeCValue *_cvalue = _cvalues + _n_values++; \
101 switch (*_collect_format++) \
103 case G_VALUE_COLLECT_INT: \
104 _cvalue->v_int = va_arg ((var_args), gint); \
106 case G_VALUE_COLLECT_LONG: \
107 _cvalue->v_long = va_arg ((var_args), glong); \
109 case G_VALUE_COLLECT_INT64: \
110 _cvalue->v_int64 = va_arg ((var_args), gint64); \
112 case G_VALUE_COLLECT_DOUBLE: \
113 _cvalue->v_double = va_arg ((var_args), gdouble); \
115 case G_VALUE_COLLECT_POINTER: \
116 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
119 g_assert_not_reached (); \
122 *(__error) = _vtable->collect_value (_value, \
130 * @value: a #GValue return location. @value is supposed to be initialized
131 * according to the value type to be collected
132 * @var_args: the va_list variable; it may be evaluated multiple times
133 * @flags: flags which are passed on to the collect_value() function of
134 * the #GTypeValueTable of @value.
135 * @__error: a #gchar** variable that will be modified to hold a g_new()
136 * allocated error messages if something fails
138 * Collects a variable argument value from a va_list. We have to
139 * implement the varargs collection as a macro, because on some systems
140 * va_list variables cannot be passed by reference.
142 * Note: If you are creating the @value argument just before calling this macro,
143 * you should use the #G_VALUE_COLLECT_FULL variant and pass the unitialized
144 * #GValue. That variant is faster than #G_VALUE_COLLECT.
146 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
147 GValue *_value = (value); \
148 GType _value_type = G_VALUE_TYPE (_value); \
149 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
151 if (_vtable->value_free) \
152 _vtable->value_free (_value); \
153 memset (_value->data, 0, sizeof (_value->data)); \
155 G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error); \
160 * @value: a #GValue return location. @value is supposed to be initialized
161 * according to the value type to be collected
162 * @var_args: the va_list variable; it may be evaluated multiple times
163 * @flags: flags which are passed on to the lcopy_value() function of
164 * the #GTypeValueTable of @value.
165 * @__error: a #gchar** variable that will be modified to hold a g_new()
166 * allocated error messages if something fails
168 * Collects a value's variable argument locations from a va_list. Usage is
169 * analogous to G_VALUE_COLLECT().
171 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
173 const GValue *_value = (value); \
174 guint _flags = (flags); \
175 GType _value_type = G_VALUE_TYPE (_value); \
176 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
177 gchar *_lcopy_format = _vtable->lcopy_format; \
178 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
179 guint _n_values = 0; \
181 while (*_lcopy_format) \
183 GTypeCValue *_cvalue = _cvalues + _n_values++; \
185 switch (*_lcopy_format++) \
187 case G_VALUE_COLLECT_INT: \
188 _cvalue->v_int = va_arg ((var_args), gint); \
190 case G_VALUE_COLLECT_LONG: \
191 _cvalue->v_long = va_arg ((var_args), glong); \
193 case G_VALUE_COLLECT_INT64: \
194 _cvalue->v_int64 = va_arg ((var_args), gint64); \
196 case G_VALUE_COLLECT_DOUBLE: \
197 _cvalue->v_double = va_arg ((var_args), gdouble); \
199 case G_VALUE_COLLECT_POINTER: \
200 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
203 g_assert_not_reached (); \
206 *(__error) = _vtable->lcopy_value (_value, \
214 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
216 * The maximal number of #GTypeCValue<!-- -->s which can be collected for a
219 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
223 #endif /* __G_VALUE_COLLECTOR_H__ */