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 * @Title: Varargs Value Collection
26 * The macros in this section provide the varargs parsing support needed
27 * in variadic GObject functions such as g_object_new() or g_object_set().
28 * They currently support the collection of integral types, floating point
31 #ifndef __G_VALUE_COLLECTOR_H__
32 #define __G_VALUE_COLLECTOR_H__
34 #include <glib-object.h>
38 /* we may want to add aggregate types here some day, if requested
39 * by users. the basic C types are covered already, everything
40 * smaller than an int is promoted to an integer and floats are
41 * always promoted to doubles for varargs call constructions.
45 G_VALUE_COLLECT_INT = 'i',
46 G_VALUE_COLLECT_LONG = 'l',
47 G_VALUE_COLLECT_INT64 = 'q',
48 G_VALUE_COLLECT_DOUBLE = 'd',
49 G_VALUE_COLLECT_POINTER = 'p'
53 /* vararg union holding actual values collected
57 * @v_int: the field for holding integer values
58 * @v_long: the field for holding long integer values
59 * @v_int64: the field for holding 64 bit integer values
60 * @v_double: the field for holding floating point values
61 * @v_pointer: the field for holding pointers
63 * A union holding one collected value.
75 * G_VALUE_COLLECT_INIT:
76 * @value: a #GValue return location. @value must contain only 0 bytes.
77 * @_value_type: the #GType to use for @value.
78 * @var_args: the va_list variable; it may be evaluated multiple times
79 * @flags: flags which are passed on to the collect_value() function of
80 * the #GTypeValueTable of @value.
81 * @__error: a #gchar** variable that will be modified to hold a g_new()
82 * allocated error messages if something fails
84 * Collects a variable argument value from a va_list. We have to
85 * implement the varargs collection as a macro, because on some systems
86 * va_list variables cannot be passed by reference.
90 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
92 GValue *_val = (value); \
93 guint _flags = (flags); \
94 GTypeValueTable *_vtab = g_type_value_table_peek (_value_type); \
95 gchar *_collect_format = _vtab->collect_format; \
96 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
97 guint _n_values = 0; \
99 _val->g_type = _value_type; /* value_meminit() from gvalue.c */ \
100 while (*_collect_format) \
102 GTypeCValue *_cvalue = _cvalues + _n_values++; \
104 switch (*_collect_format++) \
106 case G_VALUE_COLLECT_INT: \
107 _cvalue->v_int = va_arg ((var_args), gint); \
109 case G_VALUE_COLLECT_LONG: \
110 _cvalue->v_long = va_arg ((var_args), glong); \
112 case G_VALUE_COLLECT_INT64: \
113 _cvalue->v_int64 = va_arg ((var_args), gint64); \
115 case G_VALUE_COLLECT_DOUBLE: \
116 _cvalue->v_double = va_arg ((var_args), gdouble); \
118 case G_VALUE_COLLECT_POINTER: \
119 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
122 g_assert_not_reached (); \
125 *(__error) = _vtab->collect_value (_val, \
133 * @value: a #GValue return location. @value is supposed to be initialized
134 * according to the value type to be collected
135 * @var_args: the va_list variable; it may be evaluated multiple times
136 * @flags: flags which are passed on to the collect_value() function of
137 * the #GTypeValueTable of @value.
138 * @__error: a #gchar** variable that will be modified to hold a g_new()
139 * allocated error messages if something fails
141 * Collects a variable argument value from a va_list. We have to
142 * implement the varargs collection as a macro, because on some systems
143 * va_list variables cannot be passed by reference.
145 * Note: If you are creating the @value argument just before calling this macro,
146 * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
147 * #GValue. That variant is faster than #G_VALUE_COLLECT.
149 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
150 GValue *_value = (value); \
151 GType _value_type = G_VALUE_TYPE (_value); \
152 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
154 if (_vtable->value_free) \
155 _vtable->value_free (_value); \
156 memset (_value->data, 0, sizeof (_value->data)); \
158 G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error); \
161 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \
163 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
164 gchar *_collect_format = _vtable->collect_format; \
166 while (*_collect_format) \
168 switch (*_collect_format++) \
170 case G_VALUE_COLLECT_INT: \
171 va_arg ((var_args), gint); \
173 case G_VALUE_COLLECT_LONG: \
174 va_arg ((var_args), glong); \
176 case G_VALUE_COLLECT_INT64: \
177 va_arg ((var_args), gint64); \
179 case G_VALUE_COLLECT_DOUBLE: \
180 va_arg ((var_args), gdouble); \
182 case G_VALUE_COLLECT_POINTER: \
183 va_arg ((var_args), gpointer); \
186 g_assert_not_reached (); \
193 * @value: a #GValue return location. @value is supposed to be initialized
194 * according to the value type to be collected
195 * @var_args: the va_list variable; it may be evaluated multiple times
196 * @flags: flags which are passed on to the lcopy_value() function of
197 * the #GTypeValueTable of @value.
198 * @__error: a #gchar** variable that will be modified to hold a g_new()
199 * allocated error messages if something fails
201 * Collects a value's variable argument locations from a va_list. Usage is
202 * analogous to G_VALUE_COLLECT().
204 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
206 const GValue *_value = (value); \
207 guint _flags = (flags); \
208 GType _value_type = G_VALUE_TYPE (_value); \
209 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
210 gchar *_lcopy_format = _vtable->lcopy_format; \
211 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
212 guint _n_values = 0; \
214 while (*_lcopy_format) \
216 GTypeCValue *_cvalue = _cvalues + _n_values++; \
218 switch (*_lcopy_format++) \
220 case G_VALUE_COLLECT_INT: \
221 _cvalue->v_int = va_arg ((var_args), gint); \
223 case G_VALUE_COLLECT_LONG: \
224 _cvalue->v_long = va_arg ((var_args), glong); \
226 case G_VALUE_COLLECT_INT64: \
227 _cvalue->v_int64 = va_arg ((var_args), gint64); \
229 case G_VALUE_COLLECT_DOUBLE: \
230 _cvalue->v_double = va_arg ((var_args), gdouble); \
232 case G_VALUE_COLLECT_POINTER: \
233 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
236 g_assert_not_reached (); \
239 *(__error) = _vtable->lcopy_value (_value, \
247 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
249 * The maximal number of #GTypeCValue<!-- -->s which can be collected for a
252 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
256 #endif /* __G_VALUE_COLLECTOR_H__ */