Ensure values are memset to 0 when calling G_VALUE_COLLECT_INIT()
[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  * 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.
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * gvaluecollector.h: GValue varargs stubs
20  */
21 /**
22  * SECTION:value_collection
23  * @Short_description: Converting varargs to generic values
24  * @See_also:#GValueTable
25  * @Title: Varargs Value Collection
26  * 
27  * The macros in this section provide the varargs parsing support needed
28  * in variadic GObject functions such as g_object_new() or g_object_set().
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 actuall values collected
55  */
56 /**
57  * GTypeCValue:
58  * 
59  * A union holding one collected value.
60  */
61 union _GTypeCValue
62 {
63   gint     v_int;
64   glong    v_long;
65   gint64   v_int64;
66   gdouble  v_double;
67   gpointer v_pointer;
68 };
69
70 /**
71  * G_VALUE_COLLECT_INIT:
72  * @value: a #GValue return location. @value must contain only 0 bytes.
73  * @_value_type: the #GType to use for @value.
74  * @var_args: the va_list variable; it may be evaluated multiple times
75  * @flags: flags which are passed on to the collect_value() function of
76  *  the #GTypeValueTable of @value.
77  * @__error: a #gchar** variable that will be modified to hold a g_new()
78  *  allocated error messages if something fails
79  * 
80  * Collects a variable argument value from a va_list. We have to
81  * implement the varargs collection as a macro, because on some systems
82  * va_list variables cannot be passed by reference.
83  *
84  * Since: 2.24
85  */
86 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error)              \
87 G_STMT_START {                                                                          \
88   GValue *_value = (value);                                                             \
89   guint _flags = (flags);                                                               \
90   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
91   gchar *_collect_format = _vtable->collect_format;                                     \
92   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
93   guint _n_values = 0;                                                                  \
94                                                                                         \
95   _value->g_type = _value_type;         /* value_meminit() from gvalue.c */             \
96   while (*_collect_format)                                                              \
97     {                                                                                   \
98       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
99                                                                                         \
100       switch (*_collect_format++)                                                       \
101         {                                                                               \
102         case G_VALUE_COLLECT_INT:                                                       \
103           _cvalue->v_int = va_arg ((var_args), gint);                                   \
104           break;                                                                        \
105         case G_VALUE_COLLECT_LONG:                                                      \
106           _cvalue->v_long = va_arg ((var_args), glong);                                 \
107           break;                                                                        \
108         case G_VALUE_COLLECT_INT64:                                                     \
109           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
110           break;                                                                        \
111         case G_VALUE_COLLECT_DOUBLE:                                                    \
112           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
113           break;                                                                        \
114         case G_VALUE_COLLECT_POINTER:                                                   \
115           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
116           break;                                                                        \
117         default:                                                                        \
118           g_assert_not_reached ();                                                      \
119         }                                                                               \
120     }                                                                                   \
121   *(__error) = _vtable->collect_value (_value,                                          \
122                                        _n_values,                                       \
123                                        _cvalues,                                        \
124                                        _flags);                                         \
125 } G_STMT_END
126
127 /**
128  * G_VALUE_COLLECT:
129  * @value: a #GValue return location. @value is supposed to be initialized
130  *  according to the value type to be collected
131  * @var_args: the va_list variable; it may be evaluated multiple times
132  * @flags: flags which are passed on to the collect_value() function of
133  *  the #GTypeValueTable of @value.
134  * @__error: a #gchar** variable that will be modified to hold a g_new()
135  *  allocated error messages if something fails
136  *
137  * Collects a variable argument value from a va_list. We have to
138  * implement the varargs collection as a macro, because on some systems
139  * va_list variables cannot be passed by reference.
140  *
141  * Note: If you are creating the @value argument just before calling this macro,
142  * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
143  * #GValue. That variant is faster than #G_VALUE_COLLECT.
144  */
145 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START {                 \
146   GValue *_value = (value);                                                             \
147   GType _value_type = G_VALUE_TYPE (_value);                                            \
148   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
149                                                                                         \
150   if (_vtable->value_free)                                                              \
151     _vtable->value_free (_value);                                                       \
152   memset (_value->data, 0, sizeof (_value->data));                                      \
153                                                                                         \
154   G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error);                   \
155 } G_STMT_END
156
157 /**
158  * G_VALUE_LCOPY:
159  * @value: a #GValue return location. @value is supposed to be initialized 
160  *  according to the value type to be collected
161  * @var_args: the va_list variable; it may be evaluated multiple times
162  * @flags: flags which are passed on to the lcopy_value() function of
163  *  the #GTypeValueTable of @value.
164  * @__error: a #gchar** variable that will be modified to hold a g_new()
165  *  allocated error messages if something fails
166  * 
167  * Collects a value's variable argument locations from a va_list. Usage is
168  * analogous to G_VALUE_COLLECT().
169  */
170 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
171 G_STMT_START {                                                                          \
172   const GValue *_value = (value);                                                       \
173   guint _flags = (flags);                                                               \
174   GType _value_type = G_VALUE_TYPE (_value);                                            \
175   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
176   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
177   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
178   guint _n_values = 0;                                                                  \
179                                                                                         \
180   while (*_lcopy_format)                                                                \
181     {                                                                                   \
182       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
183                                                                                         \
184       switch (*_lcopy_format++)                                                         \
185         {                                                                               \
186         case G_VALUE_COLLECT_INT:                                                       \
187           _cvalue->v_int = va_arg ((var_args), gint);                                   \
188           break;                                                                        \
189         case G_VALUE_COLLECT_LONG:                                                      \
190           _cvalue->v_long = va_arg ((var_args), glong);                                 \
191           break;                                                                        \
192         case G_VALUE_COLLECT_INT64:                                                     \
193           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
194           break;                                                                        \
195         case G_VALUE_COLLECT_DOUBLE:                                                    \
196           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
197           break;                                                                        \
198         case G_VALUE_COLLECT_POINTER:                                                   \
199           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
200           break;                                                                        \
201         default:                                                                        \
202           g_assert_not_reached ();                                                      \
203         }                                                                               \
204     }                                                                                   \
205   *(__error) = _vtable->lcopy_value (_value,                                            \
206                                      _n_values,                                         \
207                                      _cvalues,                                          \
208                                      _flags);                                           \
209 } G_STMT_END
210
211
212 /**
213  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
214  * 
215  * The maximal number of #GTypeCValue<!-- -->s which can be collected for a 
216  * single #GValue.
217  */
218 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
219
220 G_END_DECLS
221
222 #endif /* __G_VALUE_COLLECTOR_H__ */