Change LGPL-2.1+ to LGPL-2.1-or-later
[platform/upstream/glib.git] / gobject / gvaluecollector.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * gvaluecollector.h: GValue varargs stubs
20  */
21 /**
22  * SECTION:value_collection
23  * @Short_description: Converting varargs to generic values
24  * @Title: Varargs Value Collection
25  * 
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  *
29  * They currently support the collection of integral types, floating point 
30  * types and pointers.
31  */
32 #ifndef __G_VALUE_COLLECTOR_H__
33 #define __G_VALUE_COLLECTOR_H__
34
35 #include <glib-object.h>
36
37 G_BEGIN_DECLS
38
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.
43  */
44 enum    /*< skip >*/
45 {
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'
51 };
52
53
54 /* vararg union holding actual values collected
55  */
56 /**
57  * GTypeCValue:
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
63  * 
64  * A union holding one collected value.
65  */
66 union _GTypeCValue
67 {
68   gint     v_int;
69   glong    v_long;
70   gint64   v_int64;
71   gdouble  v_double;
72   gpointer v_pointer;
73 };
74
75 /**
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
84  * 
85  * Collects a variable argument value from a `va_list`.
86  *
87  * We have to implement the varargs collection as a macro, because on some
88  * systems `va_list` variables cannot be passed by reference.
89  *
90  * Since: 2.24
91  */
92 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
93   G_STMT_START { \
94     GTypeValueTable *g_vci_vtab; \
95     G_VALUE_COLLECT_INIT2(value, g_vci_vtab, _value_type, var_args, flags, __error); \
96 } G_STMT_END
97
98 /**
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
102  *   for @_value_type
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
109  *
110  * A variant of G_VALUE_COLLECT_INIT() that provides the #GTypeValueTable
111  * to the caller.
112  *
113  * Since: 2.74
114  */
115 #define G_VALUE_COLLECT_INIT2(value, g_vci_vtab, _value_type, var_args, flags, __error)         \
116 G_STMT_START {                                                                          \
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)                                                         \
126     {                                                                                   \
127       GTypeCValue *g_vci_cvalue = g_vci_cvalues + g_vci_n_values++;                                     \
128                                                                                         \
129       switch (*g_vci_collect_format++)                                                  \
130         {                                                                               \
131         case G_VALUE_COLLECT_INT:                                                       \
132           g_vci_cvalue->v_int = va_arg ((var_args), gint);                                      \
133           break;                                                                        \
134         case G_VALUE_COLLECT_LONG:                                                      \
135           g_vci_cvalue->v_long = va_arg ((var_args), glong);                                    \
136           break;                                                                        \
137         case G_VALUE_COLLECT_INT64:                                                     \
138           g_vci_cvalue->v_int64 = va_arg ((var_args), gint64);                          \
139           break;                                                                        \
140         case G_VALUE_COLLECT_DOUBLE:                                                    \
141           g_vci_cvalue->v_double = va_arg ((var_args), gdouble);                                \
142           break;                                                                        \
143         case G_VALUE_COLLECT_POINTER:                                                   \
144           g_vci_cvalue->v_pointer = va_arg ((var_args), gpointer);                              \
145           break;                                                                        \
146         default:                                                                        \
147           g_assert_not_reached ();                                                      \
148         }                                                                               \
149     }                                                                                   \
150   *(__error) = g_vci_vtab->collect_value (g_vci_val,                                            \
151                                        g_vci_n_values,                                  \
152                                        g_vci_cvalues,                                   \
153                                        g_vci_flags);                                            \
154 } G_STMT_END
155
156 /**
157  * G_VALUE_COLLECT:
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
165  *
166  * Collects a variable argument value from a `va_list`.
167  *
168  * We have to implement the varargs collection as a macro, because on some systems
169  * `va_list` variables cannot be passed by reference.
170  *
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().
174  */
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);                     \
179                                                                                         \
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));                                      \
183                                                                                         \
184   G_VALUE_COLLECT_INIT(value, g_vc_value_type, var_args, flags, __error);                       \
185 } G_STMT_END
186
187 /**
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
191  *
192  * Skip an argument of type @_value_type from @var_args.
193  */
194 #define G_VALUE_COLLECT_SKIP(_value_type, var_args)                                     \
195 G_STMT_START {                                                                          \
196   GTypeValueTable *g_vcs_vtable = g_type_value_table_peek (_value_type);                        \
197   const gchar *g_vcs_collect_format = g_vcs_vtable->collect_format;                             \
198                                                                                         \
199   while (*g_vcs_collect_format)                                                         \
200     {                                                                                   \
201       switch (*g_vcs_collect_format++)                                                  \
202         {                                                                               \
203         case G_VALUE_COLLECT_INT:                                                       \
204           va_arg ((var_args), gint);                                                    \
205           break;                                                                        \
206         case G_VALUE_COLLECT_LONG:                                                      \
207           va_arg ((var_args), glong);                                                   \
208           break;                                                                        \
209         case G_VALUE_COLLECT_INT64:                                                     \
210           va_arg ((var_args), gint64);                                                  \
211           break;                                                                        \
212         case G_VALUE_COLLECT_DOUBLE:                                                    \
213           va_arg ((var_args), gdouble);                                                 \
214           break;                                                                        \
215         case G_VALUE_COLLECT_POINTER:                                                   \
216           va_arg ((var_args), gpointer);                                                \
217           break;                                                                        \
218         default:                                                                        \
219           g_assert_not_reached ();                                                      \
220         }                                                                               \
221     }                                                                                   \
222 } G_STMT_END
223
224 /**
225  * G_VALUE_LCOPY:
226  * @value: a #GValue to store into the @var_args; this must be initialized
227  *  and set
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
233  *
234  * Stores a value’s value into one or more argument locations from a `va_list`.
235  *
236  * This is the inverse of G_VALUE_COLLECT().
237  */
238 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
239 G_STMT_START {                                                                          \
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;                                                                      \
247                                                                                         \
248   while (*g_vl_lcopy_format)                                                            \
249     {                                                                                   \
250       GTypeCValue *g_vl_cvalue = g_vl_cvalues + g_vl_n_values++;                                        \
251                                                                                         \
252       switch (*g_vl_lcopy_format++)                                                             \
253         {                                                                               \
254         case G_VALUE_COLLECT_INT:                                                       \
255           g_vl_cvalue->v_int = va_arg ((var_args), gint);                                       \
256           break;                                                                        \
257         case G_VALUE_COLLECT_LONG:                                                      \
258           g_vl_cvalue->v_long = va_arg ((var_args), glong);                                     \
259           break;                                                                        \
260         case G_VALUE_COLLECT_INT64:                                                     \
261           g_vl_cvalue->v_int64 = va_arg ((var_args), gint64);                           \
262           break;                                                                        \
263         case G_VALUE_COLLECT_DOUBLE:                                                    \
264           g_vl_cvalue->v_double = va_arg ((var_args), gdouble);                         \
265           break;                                                                        \
266         case G_VALUE_COLLECT_POINTER:                                                   \
267           g_vl_cvalue->v_pointer = va_arg ((var_args), gpointer);                               \
268           break;                                                                        \
269         default:                                                                        \
270           g_assert_not_reached ();                                                      \
271         }                                                                               \
272     }                                                                                   \
273   *(__error) = g_vl_vtable->lcopy_value (g_vl_value,                                            \
274                                      g_vl_n_values,                                             \
275                                      g_vl_cvalues,                                              \
276                                      g_vl_flags);                                               \
277 } G_STMT_END
278
279
280 /**
281  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
282  * 
283  * The maximal number of #GTypeCValues which can be collected for a 
284  * single #GValue.
285  */
286 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
287
288 G_END_DECLS
289
290 #endif /* __G_VALUE_COLLECTOR_H__ */