Update German translation
[glib.git] / gobject / gvaluecollector.h
blobe668394404b63f96ede0bc83c89809ef0cf8520d
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
19 /**
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
27 * types and pointers.
29 #ifndef __G_VALUE_COLLECTOR_H__
30 #define __G_VALUE_COLLECTOR_H__
32 #include <glib-object.h>
34 G_BEGIN_DECLS
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.
41 enum /*< skip >*/
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
53 /**
54 * GTypeCValue:
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.
63 union _GTypeCValue
65 gint v_int;
66 glong v_long;
67 gint64 v_int64;
68 gdouble v_double;
69 gpointer v_pointer;
72 /**
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.
86 * Since: 2.24
88 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
89 G_STMT_START { \
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) \
99 { \
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); \
106 break; \
107 case G_VALUE_COLLECT_LONG: \
108 _cvalue->v_long = va_arg ((var_args), glong); \
109 break; \
110 case G_VALUE_COLLECT_INT64: \
111 _cvalue->v_int64 = va_arg ((var_args), gint64); \
112 break; \
113 case G_VALUE_COLLECT_DOUBLE: \
114 _cvalue->v_double = va_arg ((var_args), gdouble); \
115 break; \
116 case G_VALUE_COLLECT_POINTER: \
117 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
118 break; \
119 default: \
120 g_assert_not_reached (); \
123 *(__error) = _vtab->collect_value (_val, \
124 _n_values, \
125 _cvalues, \
126 _flags); \
127 } G_STMT_END
130 * G_VALUE_COLLECT:
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); \
157 } G_STMT_END
160 * G_VALUE_COLLECT_SKIP:
161 * @_value_type: the #GType of the value to skip
162 * @var_args: the va_list variable; it may be evaluated multiple times
164 * Skip an argument of type @_value_type from @var_args.
166 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \
167 G_STMT_START { \
168 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
169 const gchar *_collect_format = _vtable->collect_format; \
171 while (*_collect_format) \
173 switch (*_collect_format++) \
175 case G_VALUE_COLLECT_INT: \
176 va_arg ((var_args), gint); \
177 break; \
178 case G_VALUE_COLLECT_LONG: \
179 va_arg ((var_args), glong); \
180 break; \
181 case G_VALUE_COLLECT_INT64: \
182 va_arg ((var_args), gint64); \
183 break; \
184 case G_VALUE_COLLECT_DOUBLE: \
185 va_arg ((var_args), gdouble); \
186 break; \
187 case G_VALUE_COLLECT_POINTER: \
188 va_arg ((var_args), gpointer); \
189 break; \
190 default: \
191 g_assert_not_reached (); \
194 } G_STMT_END
197 * G_VALUE_LCOPY:
198 * @value: a #GValue return location. @value is supposed to be initialized
199 * according to the value type to be collected
200 * @var_args: the va_list variable; it may be evaluated multiple times
201 * @flags: flags which are passed on to the lcopy_value() function of
202 * the #GTypeValueTable of @value.
203 * @__error: a #gchar** variable that will be modified to hold a g_new()
204 * allocated error messages if something fails
206 * Collects a value's variable argument locations from a va_list. Usage is
207 * analogous to G_VALUE_COLLECT().
209 #define G_VALUE_LCOPY(value, var_args, flags, __error) \
210 G_STMT_START { \
211 const GValue *_value = (value); \
212 guint _flags = (flags); \
213 GType _value_type = G_VALUE_TYPE (_value); \
214 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
215 const gchar *_lcopy_format = _vtable->lcopy_format; \
216 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
217 guint _n_values = 0; \
219 while (*_lcopy_format) \
221 GTypeCValue *_cvalue = _cvalues + _n_values++; \
223 switch (*_lcopy_format++) \
225 case G_VALUE_COLLECT_INT: \
226 _cvalue->v_int = va_arg ((var_args), gint); \
227 break; \
228 case G_VALUE_COLLECT_LONG: \
229 _cvalue->v_long = va_arg ((var_args), glong); \
230 break; \
231 case G_VALUE_COLLECT_INT64: \
232 _cvalue->v_int64 = va_arg ((var_args), gint64); \
233 break; \
234 case G_VALUE_COLLECT_DOUBLE: \
235 _cvalue->v_double = va_arg ((var_args), gdouble); \
236 break; \
237 case G_VALUE_COLLECT_POINTER: \
238 _cvalue->v_pointer = va_arg ((var_args), gpointer); \
239 break; \
240 default: \
241 g_assert_not_reached (); \
244 *(__error) = _vtable->lcopy_value (_value, \
245 _n_values, \
246 _cvalues, \
247 _flags); \
248 } G_STMT_END
252 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
254 * The maximal number of #GTypeCValues which can be collected for a
255 * single #GValue.
257 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
259 G_END_DECLS
261 #endif /* __G_VALUE_COLLECTOR_H__ */