1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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().
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 actual values collected
58 * @v_int: the field for holding integer values
59 * @v_long: the field for holding long integer values
60 * @v_int64: the field for holding 64 bit integer values
61 * @v_double: the field for holding floating point values
62 * @v_pointer: the field for holding pointers
64 * A union holding one collected value.
76 * G_VALUE_COLLECT_INIT:
77 * @value: a #GValue return location. @value must contain only 0 bytes.
78 * @_value_type: the #GType to use for @value.
79 * @var_args: the va_list variable; it may be evaluated multiple times
80 * @flags: flags which are passed on to the collect_value() function of
81 * the #GTypeValueTable of @value.
82 * @__error: a #gchar** variable that will be modified to hold a g_new()
83 * allocated error messages if something fails
85 * Collects a variable argument value from a `va_list`.
87 * We have to implement the varargs collection as a macro, because on some
88 * systems `va_list` variables cannot be passed by reference.
92 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
94 GTypeValueTable *g_vci_vtab; \
95 G_VALUE_COLLECT_INIT2(value, g_vci_vtab, _value_type, var_args, flags, __error); \
99 * G_VALUE_COLLECT_INIT2:
100 * @value: a #GValue return location. @value must contain only 0 bytes.
101 * @g_vci_vtab: a #GTypeValueTable pointer that will be set to the value table
103 * @_value_type: the #GType to use for @value.
104 * @var_args: the va_list variable; it may be evaluated multiple times
105 * @flags: flags which are passed on to the collect_value() function of
106 * the #GTypeValueTable of @value.
107 * @__error: a #gchar** variable that will be modified to hold a g_new()
108 * allocated error messages if something fails
110 * A variant of G_VALUE_COLLECT_INIT() that provides the #GTypeValueTable
115 #define G_VALUE_COLLECT_INIT2(value, g_vci_vtab, _value_type, var_args, flags, __error) \
117 GValue *g_vci_val = (value); \
118 guint g_vci_flags = (flags); \
119 const gchar *g_vci_collect_format; \
120 GTypeCValue g_vci_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
121 guint g_vci_n_values = 0; \
122 g_vci_vtab = g_type_value_table_peek (_value_type); \
123 g_vci_collect_format = g_vci_vtab->collect_format; \
124 g_vci_val->g_type = _value_type; /* value_meminit() from gvalue.c */ \
125 while (*g_vci_collect_format) \
127 GTypeCValue *g_vci_cvalue = g_vci_cvalues + g_vci_n_values++; \
129 switch (*g_vci_collect_format++) \
131 case G_VALUE_COLLECT_INT: \
132 g_vci_cvalue->v_int = va_arg ((var_args), gint); \
134 case G_VALUE_COLLECT_LONG: \
135 g_vci_cvalue->v_long = va_arg ((var_args), glong); \
137 case G_VALUE_COLLECT_INT64: \
138 g_vci_cvalue->v_int64 = va_arg ((var_args), gint64); \
140 case G_VALUE_COLLECT_DOUBLE: \
141 g_vci_cvalue->v_double = va_arg ((var_args), gdouble); \
143 case G_VALUE_COLLECT_POINTER: \
144 g_vci_cvalue->v_pointer = va_arg ((var_args), gpointer); \
147 g_assert_not_reached (); \
150 *(__error) = g_vci_vtab->collect_value (g_vci_val, \
158 * @value: a #GValue return location. @value is supposed to be initialized
159 * according to the value type to be collected
160 * @var_args: the va_list variable; it may be evaluated multiple times
161 * @flags: flags which are passed on to the collect_value() function of
162 * the #GTypeValueTable of @value.
163 * @__error: a #gchar** variable that will be modified to hold a g_new()
164 * allocated error messages if something fails
166 * Collects a variable argument value from a `va_list`.
168 * We have to implement the varargs collection as a macro, because on some systems
169 * `va_list` variables cannot be passed by reference.
171 * Note: If you are creating the @value argument just before calling this macro,
172 * you should use the G_VALUE_COLLECT_INIT() variant and pass the uninitialized
173 * #GValue. That variant is faster than G_VALUE_COLLECT().
175 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
176 GValue *g_vc_value = (value); \
177 GType g_vc_value_type = G_VALUE_TYPE (g_vc_value); \
178 GTypeValueTable *g_vc_vtable = g_type_value_table_peek (g_vc_value_type); \
180 if (g_vc_vtable->value_free) \
181 g_vc_vtable->value_free (g_vc_value); \
182 memset (g_vc_value->data, 0, sizeof (g_vc_value->data)); \
184 G_VALUE_COLLECT_INIT(value, g_vc_value_type, var_args, flags, __error); \
188 * G_VALUE_COLLECT_SKIP:
189 * @_value_type: the #GType of the value to skip
190 * @var_args: the va_list variable; it may be evaluated multiple times
192 * Skip an argument of type @_value_type from @var_args.
194 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \
196 GTypeValueTable *g_vcs_vtable = g_type_value_table_peek (_value_type); \
197 const gchar *g_vcs_collect_format = g_vcs_vtable->collect_format; \
199 while (*g_vcs_collect_format) \
201 switch (*g_vcs_collect_format++) \
203 case G_VALUE_COLLECT_INT: \
204 va_arg ((var_args), gint); \
206 case G_VALUE_COLLECT_LONG: \
207 va_arg ((var_args), glong); \
209 case G_VALUE_COLLECT_INT64: \
210 va_arg ((var_args), gint64); \
212 case G_VALUE_COLLECT_DOUBLE: \
213 va_arg ((var_args), gdouble); \
215 case G_VALUE_COLLECT_POINTER: \
216 va_arg ((var_args), gpointer); \
219 g_assert_not_reached (); \
226 * @value: a #GValue to store into the @var_args; this must be initialized
228 * @var_args: the va_list variable; it may be evaluated multiple times
229 * @flags: flags which are passed on to the lcopy_value() function of
230 * the #GTypeValueTable of @value.
231 * @__error: a #gchar** variable that will be modified to hold a g_new()
232 * allocated error message if something fails
234 * Stores a value’s value into one or more argument locations from a `va_list`.
236 * This is the inverse of G_VALUE_COLLECT().
238 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
240 const GValue *g_vl_value = (value); \
241 guint g_vl_flags = (flags); \
242 GType g_vl_value_type = G_VALUE_TYPE (g_vl_value); \
243 GTypeValueTable *g_vl_vtable = g_type_value_table_peek (g_vl_value_type); \
244 const gchar *g_vl_lcopy_format = g_vl_vtable->lcopy_format; \
245 GTypeCValue g_vl_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
246 guint g_vl_n_values = 0; \
248 while (*g_vl_lcopy_format) \
250 GTypeCValue *g_vl_cvalue = g_vl_cvalues + g_vl_n_values++; \
252 switch (*g_vl_lcopy_format++) \
254 case G_VALUE_COLLECT_INT: \
255 g_vl_cvalue->v_int = va_arg ((var_args), gint); \
257 case G_VALUE_COLLECT_LONG: \
258 g_vl_cvalue->v_long = va_arg ((var_args), glong); \
260 case G_VALUE_COLLECT_INT64: \
261 g_vl_cvalue->v_int64 = va_arg ((var_args), gint64); \
263 case G_VALUE_COLLECT_DOUBLE: \
264 g_vl_cvalue->v_double = va_arg ((var_args), gdouble); \
266 case G_VALUE_COLLECT_POINTER: \
267 g_vl_cvalue->v_pointer = va_arg ((var_args), gpointer); \
270 g_assert_not_reached (); \
273 *(__error) = g_vl_vtable->lcopy_value (g_vl_value, \
281 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
283 * The maximal number of #GTypeCValues which can be collected for a
286 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
290 #endif /* __G_VALUE_COLLECTOR_H__ */