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.1 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().
27 * They currently support the collection of integral types, floating point
30 #ifndef __G_VALUE_COLLECTOR_H__
31 #define __G_VALUE_COLLECTOR_H__
33 #include <glib-object.h>
37 /* we may want to add aggregate types here some day, if requested
38 * by users. the basic C types are covered already, everything
39 * smaller than an int is promoted to an integer and floats are
40 * always promoted to doubles for varargs call constructions.
44 G_VALUE_COLLECT_INT = 'i',
45 G_VALUE_COLLECT_LONG = 'l',
46 G_VALUE_COLLECT_INT64 = 'q',
47 G_VALUE_COLLECT_DOUBLE = 'd',
48 G_VALUE_COLLECT_POINTER = 'p'
52 /* vararg union holding actual values collected
56 * @v_int: the field for holding integer values
57 * @v_long: the field for holding long integer values
58 * @v_int64: the field for holding 64 bit integer values
59 * @v_double: the field for holding floating point values
60 * @v_pointer: the field for holding pointers
62 * A union holding one collected value.
74 * G_VALUE_COLLECT_INIT:
75 * @value: a #GValue return location. @value must contain only 0 bytes.
76 * @_value_type: the #GType to use for @value.
77 * @var_args: the va_list variable; it may be evaluated multiple times
78 * @flags: flags which are passed on to the collect_value() function of
79 * the #GTypeValueTable of @value.
80 * @__error: a #gchar** variable that will be modified to hold a g_new()
81 * allocated error messages if something fails
83 * Collects a variable argument value from a `va_list`.
85 * We have to implement the varargs collection as a macro, because on some
86 * systems `va_list` variables cannot be passed by reference.
90 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
92 GValue *g_vci_val = (value); \
93 guint g_vci_flags = (flags); \
94 GTypeValueTable *g_vci_vtab = g_type_value_table_peek (_value_type); \
95 const gchar *g_vci_collect_format = g_vci_vtab->collect_format; \
96 GTypeCValue g_vci_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
97 guint g_vci_n_values = 0; \
99 g_vci_val->g_type = _value_type; /* value_meminit() from gvalue.c */ \
100 while (*g_vci_collect_format) \
102 GTypeCValue *g_vci_cvalue = g_vci_cvalues + g_vci_n_values++; \
104 switch (*g_vci_collect_format++) \
106 case G_VALUE_COLLECT_INT: \
107 g_vci_cvalue->v_int = va_arg ((var_args), gint); \
109 case G_VALUE_COLLECT_LONG: \
110 g_vci_cvalue->v_long = va_arg ((var_args), glong); \
112 case G_VALUE_COLLECT_INT64: \
113 g_vci_cvalue->v_int64 = va_arg ((var_args), gint64); \
115 case G_VALUE_COLLECT_DOUBLE: \
116 g_vci_cvalue->v_double = va_arg ((var_args), gdouble); \
118 case G_VALUE_COLLECT_POINTER: \
119 g_vci_cvalue->v_pointer = va_arg ((var_args), gpointer); \
122 g_assert_not_reached (); \
125 *(__error) = g_vci_vtab->collect_value (g_vci_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`.
143 * We have to implement the varargs collection as a macro, because on some systems
144 * `va_list` variables cannot be passed by reference.
146 * Note: If you are creating the @value argument just before calling this macro,
147 * you should use the G_VALUE_COLLECT_INIT() variant and pass the uninitialized
148 * #GValue. That variant is faster than G_VALUE_COLLECT().
150 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
151 GValue *g_vc_value = (value); \
152 GType g_vc_value_type = G_VALUE_TYPE (g_vc_value); \
153 GTypeValueTable *g_vc_vtable = g_type_value_table_peek (g_vc_value_type); \
155 if (g_vc_vtable->value_free) \
156 g_vc_vtable->value_free (g_vc_value); \
157 memset (g_vc_value->data, 0, sizeof (g_vc_value->data)); \
159 G_VALUE_COLLECT_INIT(value, g_vc_value_type, var_args, flags, __error); \
163 * G_VALUE_COLLECT_SKIP:
164 * @_value_type: the #GType of the value to skip
165 * @var_args: the va_list variable; it may be evaluated multiple times
167 * Skip an argument of type @_value_type from @var_args.
169 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \
171 GTypeValueTable *g_vcs_vtable = g_type_value_table_peek (_value_type); \
172 const gchar *g_vcs_collect_format = g_vcs_vtable->collect_format; \
174 while (*g_vcs_collect_format) \
176 switch (*g_vcs_collect_format++) \
178 case G_VALUE_COLLECT_INT: \
179 va_arg ((var_args), gint); \
181 case G_VALUE_COLLECT_LONG: \
182 va_arg ((var_args), glong); \
184 case G_VALUE_COLLECT_INT64: \
185 va_arg ((var_args), gint64); \
187 case G_VALUE_COLLECT_DOUBLE: \
188 va_arg ((var_args), gdouble); \
190 case G_VALUE_COLLECT_POINTER: \
191 va_arg ((var_args), gpointer); \
194 g_assert_not_reached (); \
201 * @value: a #GValue to store into the @var_args; this must be initialized
203 * @var_args: the va_list variable; it may be evaluated multiple times
204 * @flags: flags which are passed on to the lcopy_value() function of
205 * the #GTypeValueTable of @value.
206 * @__error: a #gchar** variable that will be modified to hold a g_new()
207 * allocated error message if something fails
209 * Stores a value’s value into one or more argument locations from a `va_list`.
211 * This is the inverse of G_VALUE_COLLECT().
213 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
215 const GValue *g_vl_value = (value); \
216 guint g_vl_flags = (flags); \
217 GType g_vl_value_type = G_VALUE_TYPE (g_vl_value); \
218 GTypeValueTable *g_vl_vtable = g_type_value_table_peek (g_vl_value_type); \
219 const gchar *g_vl_lcopy_format = g_vl_vtable->lcopy_format; \
220 GTypeCValue g_vl_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
221 guint g_vl_n_values = 0; \
223 while (*g_vl_lcopy_format) \
225 GTypeCValue *g_vl_cvalue = g_vl_cvalues + g_vl_n_values++; \
227 switch (*g_vl_lcopy_format++) \
229 case G_VALUE_COLLECT_INT: \
230 g_vl_cvalue->v_int = va_arg ((var_args), gint); \
232 case G_VALUE_COLLECT_LONG: \
233 g_vl_cvalue->v_long = va_arg ((var_args), glong); \
235 case G_VALUE_COLLECT_INT64: \
236 g_vl_cvalue->v_int64 = va_arg ((var_args), gint64); \
238 case G_VALUE_COLLECT_DOUBLE: \
239 g_vl_cvalue->v_double = va_arg ((var_args), gdouble); \
241 case G_VALUE_COLLECT_POINTER: \
242 g_vl_cvalue->v_pointer = va_arg ((var_args), gpointer); \
245 g_assert_not_reached (); \
248 *(__error) = g_vl_vtable->lcopy_value (g_vl_value, \
256 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
258 * The maximal number of #GTypeCValues which can be collected for a
261 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
265 #endif /* __G_VALUE_COLLECTOR_H__ */