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, see <http://www.gnu.org/licenses/>.
17 * gvaluecollector.h: GValue varargs stubs
20 * SECTION:value_collection
21 * @Short_description: Converting varargs to generic values
22 * @Title: Varargs Value Collection
24 * The macros in this section provide the varargs parsing support needed
25 * in variadic GObject functions such as g_object_new() or g_object_set().
26 * They currently support the collection of integral types, floating point
29 #ifndef __G_VALUE_COLLECTOR_H__
30 #define __G_VALUE_COLLECTOR_H__
32 #include <glib-object.h>
36 /* we may want to add aggregate types here some day, if requested
37 * by users. the basic C types are covered already, everything
38 * smaller than an int is promoted to an integer and floats are
39 * always promoted to doubles for varargs call constructions.
43 G_VALUE_COLLECT_INT = 'i',
44 G_VALUE_COLLECT_LONG = 'l',
45 G_VALUE_COLLECT_INT64 = 'q',
46 G_VALUE_COLLECT_DOUBLE = 'd',
47 G_VALUE_COLLECT_POINTER = 'p'
51 /* vararg union holding actual values collected
55 * @v_int: the field for holding integer values
56 * @v_long: the field for holding long integer values
57 * @v_int64: the field for holding 64 bit integer values
58 * @v_double: the field for holding floating point values
59 * @v_pointer: the field for holding pointers
61 * A union holding one collected value.
73 * G_VALUE_COLLECT_INIT:
74 * @value: a #GValue return location. @value must contain only 0 bytes.
75 * @_value_type: the #GType to use for @value.
76 * @var_args: the va_list variable; it may be evaluated multiple times
77 * @flags: flags which are passed on to the collect_value() function of
78 * the #GTypeValueTable of @value.
79 * @__error: a #gchar** variable that will be modified to hold a g_new()
80 * allocated error messages if something fails
82 * Collects a variable argument value from a va_list. We have to
83 * implement the varargs collection as a macro, because on some systems
84 * va_list variables cannot be passed by reference.
88 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
90 GValue *_val = (value); \
91 guint _flags = (flags); \
92 GTypeValueTable *_vtab = g_type_value_table_peek (_value_type); \
93 const gchar *_collect_format = _vtab->collect_format; \
94 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
95 guint _n_values = 0; \
97 _val->g_type = _value_type; /* value_meminit() from gvalue.c */ \
98 while (*_collect_format) \
100 GTypeCValue *_cvalue = _cvalues + _n_values++; \
102 switch (*_collect_format++) \
104 case G_VALUE_COLLECT_INT: \
105 _cvalue->v_int = va_arg ((var_args), gint); \
107 case G_VALUE_COLLECT_LONG: \
108 _cvalue->v_long = va_arg ((var_args), glong); \
110 case G_VALUE_COLLECT_INT64: \
111 _cvalue->v_int64 = va_arg ((var_args), gint64); \
113 case G_VALUE_COLLECT_DOUBLE: \
114 _cvalue->v_double = va_arg ((var_args), gdouble); \
116 case G_VALUE_COLLECT_POINTER: \
117 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
120 g_assert_not_reached (); \
123 *(__error) = _vtab->collect_value (_val, \
131 * @value: a #GValue return location. @value is supposed to be initialized
132 * according to the value type to be collected
133 * @var_args: the va_list variable; it may be evaluated multiple times
134 * @flags: flags which are passed on to the collect_value() function of
135 * the #GTypeValueTable of @value.
136 * @__error: a #gchar** variable that will be modified to hold a g_new()
137 * allocated error messages if something fails
139 * Collects a variable argument value from a va_list. We have to
140 * implement the varargs collection as a macro, because on some systems
141 * va_list variables cannot be passed by reference.
143 * Note: If you are creating the @value argument just before calling this macro,
144 * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
145 * #GValue. That variant is faster than #G_VALUE_COLLECT.
147 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
148 GValue *_value = (value); \
149 GType _value_type = G_VALUE_TYPE (_value); \
150 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
152 if (_vtable->value_free) \
153 _vtable->value_free (_value); \
154 memset (_value->data, 0, sizeof (_value->data)); \
156 G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error); \
159 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \
161 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
162 const gchar *_collect_format = _vtable->collect_format; \
164 while (*_collect_format) \
166 switch (*_collect_format++) \
168 case G_VALUE_COLLECT_INT: \
169 va_arg ((var_args), gint); \
171 case G_VALUE_COLLECT_LONG: \
172 va_arg ((var_args), glong); \
174 case G_VALUE_COLLECT_INT64: \
175 va_arg ((var_args), gint64); \
177 case G_VALUE_COLLECT_DOUBLE: \
178 va_arg ((var_args), gdouble); \
180 case G_VALUE_COLLECT_POINTER: \
181 va_arg ((var_args), gpointer); \
184 g_assert_not_reached (); \
191 * @value: a #GValue return location. @value is supposed to be initialized
192 * according to the value type to be collected
193 * @var_args: the va_list variable; it may be evaluated multiple times
194 * @flags: flags which are passed on to the lcopy_value() function of
195 * the #GTypeValueTable of @value.
196 * @__error: a #gchar** variable that will be modified to hold a g_new()
197 * allocated error messages if something fails
199 * Collects a value's variable argument locations from a va_list. Usage is
200 * analogous to G_VALUE_COLLECT().
202 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
204 const GValue *_value = (value); \
205 guint _flags = (flags); \
206 GType _value_type = G_VALUE_TYPE (_value); \
207 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
208 const gchar *_lcopy_format = _vtable->lcopy_format; \
209 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
210 guint _n_values = 0; \
212 while (*_lcopy_format) \
214 GTypeCValue *_cvalue = _cvalues + _n_values++; \
216 switch (*_lcopy_format++) \
218 case G_VALUE_COLLECT_INT: \
219 _cvalue->v_int = va_arg ((var_args), gint); \
221 case G_VALUE_COLLECT_LONG: \
222 _cvalue->v_long = va_arg ((var_args), glong); \
224 case G_VALUE_COLLECT_INT64: \
225 _cvalue->v_int64 = va_arg ((var_args), gint64); \
227 case G_VALUE_COLLECT_DOUBLE: \
228 _cvalue->v_double = va_arg ((var_args), gdouble); \
230 case G_VALUE_COLLECT_POINTER: \
231 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
234 g_assert_not_reached (); \
237 *(__error) = _vtable->lcopy_value (_value, \
245 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
247 * The maximal number of #GTypeCValues which can be collected for a
250 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
254 #endif /* __G_VALUE_COLLECTOR_H__ */