oops
[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, 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 *_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;                                                                  \
96                                                                                         \
97   _val->g_type = _value_type;           /* value_meminit() from gvalue.c */             \
98   while (*_collect_format)                                                              \
99     {                                                                                   \
100       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
101                                                                                         \
102       switch (*_collect_format++)                                                       \
103         {                                                                               \
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 ();                                                      \
121         }                                                                               \
122     }                                                                                   \
123   *(__error) = _vtab->collect_value (_val,                                              \
124                                        _n_values,                                       \
125                                        _cvalues,                                        \
126                                        _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 *_value = (value);                                                             \
149   GType _value_type = G_VALUE_TYPE (_value);                                            \
150   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
151                                                                                         \
152   if (_vtable->value_free)                                                              \
153     _vtable->value_free (_value);                                                       \
154   memset (_value->data, 0, sizeof (_value->data));                                      \
155                                                                                         \
156   G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error);                   \
157 } G_STMT_END
158
159 #define G_VALUE_COLLECT_SKIP(_value_type, var_args)                                     \
160 G_STMT_START {                                                                          \
161   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
162   const gchar *_collect_format = _vtable->collect_format;                               \
163                                                                                         \
164   while (*_collect_format)                                                              \
165     {                                                                                   \
166       switch (*_collect_format++)                                                       \
167         {                                                                               \
168         case G_VALUE_COLLECT_INT:                                                       \
169           va_arg ((var_args), gint);                                                    \
170           break;                                                                        \
171         case G_VALUE_COLLECT_LONG:                                                      \
172           va_arg ((var_args), glong);                                                   \
173           break;                                                                        \
174         case G_VALUE_COLLECT_INT64:                                                     \
175           va_arg ((var_args), gint64);                                                  \
176           break;                                                                        \
177         case G_VALUE_COLLECT_DOUBLE:                                                    \
178           va_arg ((var_args), gdouble);                                                 \
179           break;                                                                        \
180         case G_VALUE_COLLECT_POINTER:                                                   \
181           va_arg ((var_args), gpointer);                                                \
182           break;                                                                        \
183         default:                                                                        \
184           g_assert_not_reached ();                                                      \
185         }                                                                               \
186     }                                                                                   \
187 } G_STMT_END
188
189 /**
190  * G_VALUE_LCOPY:
191  * @value: a #GValue return location. @value is supposed to be initialized 
192  *  according to the value type to be collected
193  * @var_args: the va_list variable; it may be evaluated multiple times
194  * @flags: flags which are passed on to the lcopy_value() function of
195  *  the #GTypeValueTable of @value.
196  * @__error: a #gchar** variable that will be modified to hold a g_new()
197  *  allocated error messages if something fails
198  * 
199  * Collects a value's variable argument locations from a va_list. Usage is
200  * analogous to G_VALUE_COLLECT().
201  */
202 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
203 G_STMT_START {                                                                          \
204   const GValue *_value = (value);                                                       \
205   guint _flags = (flags);                                                               \
206   GType _value_type = G_VALUE_TYPE (_value);                                            \
207   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
208   const gchar *_lcopy_format = _vtable->lcopy_format;                                   \
209   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
210   guint _n_values = 0;                                                                  \
211                                                                                         \
212   while (*_lcopy_format)                                                                \
213     {                                                                                   \
214       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
215                                                                                         \
216       switch (*_lcopy_format++)                                                         \
217         {                                                                               \
218         case G_VALUE_COLLECT_INT:                                                       \
219           _cvalue->v_int = va_arg ((var_args), gint);                                   \
220           break;                                                                        \
221         case G_VALUE_COLLECT_LONG:                                                      \
222           _cvalue->v_long = va_arg ((var_args), glong);                                 \
223           break;                                                                        \
224         case G_VALUE_COLLECT_INT64:                                                     \
225           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
226           break;                                                                        \
227         case G_VALUE_COLLECT_DOUBLE:                                                    \
228           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
229           break;                                                                        \
230         case G_VALUE_COLLECT_POINTER:                                                   \
231           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
232           break;                                                                        \
233         default:                                                                        \
234           g_assert_not_reached ();                                                      \
235         }                                                                               \
236     }                                                                                   \
237   *(__error) = _vtable->lcopy_value (_value,                                            \
238                                      _n_values,                                         \
239                                      _cvalues,                                          \
240                                      _flags);                                           \
241 } G_STMT_END
242
243
244 /**
245  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
246  * 
247  * The maximal number of #GTypeCValues which can be collected for a 
248  * single #GValue.
249  */
250 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
251
252 G_END_DECLS
253
254 #endif /* __G_VALUE_COLLECTOR_H__ */