1 | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. |
3 | * |
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. |
8 | * |
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. |
13 | * |
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/>. |
16 | * |
17 | * gvaluecollector.h: GValue varargs stubs |
18 | */ |
19 | /** |
20 | * SECTION:value_collection |
21 | * @Short_description: Converting varargs to generic values |
22 | * @Title: Varargs Value Collection |
23 | * |
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. |
28 | */ |
29 | #ifndef __G_VALUE_COLLECTOR_H__ |
30 | #define __G_VALUE_COLLECTOR_H__ |
31 | |
32 | #include <glib-object.h> |
33 | |
34 | G_BEGIN_DECLS |
35 | |
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. |
40 | */ |
41 | enum /*< skip >*/ |
42 | { |
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' |
48 | }; |
49 | |
50 | |
51 | /* vararg union holding actual values collected |
52 | */ |
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 |
60 | * |
61 | * A union holding one collected value. |
62 | */ |
63 | union _GTypeCValue |
64 | { |
65 | gint v_int; |
66 | glong v_long; |
67 | gint64 v_int64; |
68 | gdouble v_double; |
69 | gpointer v_pointer; |
70 | }; |
71 | |
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 |
81 | * |
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. |
85 | * |
86 | * Since: 2.24 |
87 | */ |
88 | #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \ |
89 | G_STMT_START { \ |
90 | GValue *g_vci_val = (value); \ |
91 | guint g_vci_flags = (flags); \ |
92 | GTypeValueTable *g_vci_vtab = g_type_value_table_peek (_value_type); \ |
93 | const gchar *g_vci_collect_format = g_vci_vtab->collect_format; \ |
94 | GTypeCValue g_vci_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \ |
95 | guint g_vci_n_values = 0; \ |
96 | \ |
97 | g_vci_val->g_type = _value_type; /* value_meminit() from gvalue.c */ \ |
98 | while (*g_vci_collect_format) \ |
99 | { \ |
100 | GTypeCValue *g_vci_cvalue = g_vci_cvalues + g_vci_n_values++; \ |
101 | \ |
102 | switch (*g_vci_collect_format++) \ |
103 | { \ |
104 | case G_VALUE_COLLECT_INT: \ |
105 | g_vci_cvalue->v_int = va_arg ((var_args), gint); \ |
106 | break; \ |
107 | case G_VALUE_COLLECT_LONG: \ |
108 | g_vci_cvalue->v_long = va_arg ((var_args), glong); \ |
109 | break; \ |
110 | case G_VALUE_COLLECT_INT64: \ |
111 | g_vci_cvalue->v_int64 = va_arg ((var_args), gint64); \ |
112 | break; \ |
113 | case G_VALUE_COLLECT_DOUBLE: \ |
114 | g_vci_cvalue->v_double = va_arg ((var_args), gdouble); \ |
115 | break; \ |
116 | case G_VALUE_COLLECT_POINTER: \ |
117 | g_vci_cvalue->v_pointer = va_arg ((var_args), gpointer); \ |
118 | break; \ |
119 | default: \ |
120 | g_assert_not_reached (); \ |
121 | } \ |
122 | } \ |
123 | *(__error) = g_vci_vtab->collect_value (g_vci_val, \ |
124 | g_vci_n_values, \ |
125 | g_vci_cvalues, \ |
126 | g_vci_flags); \ |
127 | } G_STMT_END |
128 | |
129 | /** |
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 |
138 | * |
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. |
142 | * |
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. |
146 | */ |
147 | #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \ |
148 | GValue *g_vc_value = (value); \ |
149 | GType g_vc_value_type = G_VALUE_TYPE (g_vc_value); \ |
150 | GTypeValueTable *g_vc_vtable = g_type_value_table_peek (g_vc_value_type); \ |
151 | \ |
152 | if (g_vc_vtable->value_free) \ |
153 | g_vc_vtable->value_free (g_vc_value); \ |
154 | memset (g_vc_value->data, 0, sizeof (g_vc_value->data)); \ |
155 | \ |
156 | G_VALUE_COLLECT_INIT(value, g_vc_value_type, var_args, flags, __error); \ |
157 | } G_STMT_END |
158 | |
159 | /** |
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 |
163 | * |
164 | * Skip an argument of type @_value_type from @var_args. |
165 | */ |
166 | #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \ |
167 | G_STMT_START { \ |
168 | GTypeValueTable *g_vcs_vtable = g_type_value_table_peek (_value_type); \ |
169 | const gchar *g_vcs_collect_format = g_vcs_vtable->collect_format; \ |
170 | \ |
171 | while (*g_vcs_collect_format) \ |
172 | { \ |
173 | switch (*g_vcs_collect_format++) \ |
174 | { \ |
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 (); \ |
192 | } \ |
193 | } \ |
194 | } G_STMT_END |
195 | |
196 | /** |
197 | * G_VALUE_LCOPY: |
198 | * @value: a #GValue to store into the @var_args; this must be initialized |
199 | * and set |
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 message if something fails |
205 | * |
206 | * Stores a value’s value into one or more argument locations from a va_list. |
207 | * This is the inverse of G_VALUE_COLLECT(). |
208 | */ |
209 | #define G_VALUE_LCOPY(value, var_args, flags, __error) \ |
210 | G_STMT_START { \ |
211 | const GValue *g_vl_value = (value); \ |
212 | guint g_vl_flags = (flags); \ |
213 | GType g_vl_value_type = G_VALUE_TYPE (g_vl_value); \ |
214 | GTypeValueTable *g_vl_vtable = g_type_value_table_peek (g_vl_value_type); \ |
215 | const gchar *g_vl_lcopy_format = g_vl_vtable->lcopy_format; \ |
216 | GTypeCValue g_vl_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \ |
217 | guint g_vl_n_values = 0; \ |
218 | \ |
219 | while (*g_vl_lcopy_format) \ |
220 | { \ |
221 | GTypeCValue *g_vl_cvalue = g_vl_cvalues + g_vl_n_values++; \ |
222 | \ |
223 | switch (*g_vl_lcopy_format++) \ |
224 | { \ |
225 | case G_VALUE_COLLECT_INT: \ |
226 | g_vl_cvalue->v_int = va_arg ((var_args), gint); \ |
227 | break; \ |
228 | case G_VALUE_COLLECT_LONG: \ |
229 | g_vl_cvalue->v_long = va_arg ((var_args), glong); \ |
230 | break; \ |
231 | case G_VALUE_COLLECT_INT64: \ |
232 | g_vl_cvalue->v_int64 = va_arg ((var_args), gint64); \ |
233 | break; \ |
234 | case G_VALUE_COLLECT_DOUBLE: \ |
235 | g_vl_cvalue->v_double = va_arg ((var_args), gdouble); \ |
236 | break; \ |
237 | case G_VALUE_COLLECT_POINTER: \ |
238 | g_vl_cvalue->v_pointer = va_arg ((var_args), gpointer); \ |
239 | break; \ |
240 | default: \ |
241 | g_assert_not_reached (); \ |
242 | } \ |
243 | } \ |
244 | *(__error) = g_vl_vtable->lcopy_value (g_vl_value, \ |
245 | g_vl_n_values, \ |
246 | g_vl_cvalues, \ |
247 | g_vl_flags); \ |
248 | } G_STMT_END |
249 | |
250 | |
251 | /** |
252 | * G_VALUE_COLLECT_FORMAT_MAX_LENGTH: |
253 | * |
254 | * The maximal number of #GTypeCValues which can be collected for a |
255 | * single #GValue. |
256 | */ |
257 | #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8) |
258 | |
259 | G_END_DECLS |
260 | |
261 | #endif /* __G_VALUE_COLLECTOR_H__ */ |
262 | |