Merge branch 'signal-performance'
[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  * @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  * They currently support the collection of integral types, floating point 
29  * types and pointers.
30  */
31 #ifndef __G_VALUE_COLLECTOR_H__
32 #define __G_VALUE_COLLECTOR_H__
33
34 #include <glib-object.h>
35
36 G_BEGIN_DECLS
37
38 /* we may want to add aggregate types here some day, if requested
39  * by users. the basic C types are covered already, everything
40  * smaller than an int is promoted to an integer and floats are
41  * always promoted to doubles for varargs call constructions.
42  */
43 enum    /*< skip >*/
44 {
45   G_VALUE_COLLECT_INT           = 'i',
46   G_VALUE_COLLECT_LONG          = 'l',
47   G_VALUE_COLLECT_INT64         = 'q',
48   G_VALUE_COLLECT_DOUBLE        = 'd',
49   G_VALUE_COLLECT_POINTER       = 'p'
50 };
51
52
53 /* vararg union holding actual values collected
54  */
55 /**
56  * GTypeCValue:
57  * @v_int: the field for holding integer values
58  * @v_long: the field for holding long integer values
59  * @v_int64: the field for holding 64 bit integer values
60  * @v_double: the field for holding floating point values
61  * @v_pointer: the field for holding pointers
62  * 
63  * A union holding one collected value.
64  */
65 union _GTypeCValue
66 {
67   gint     v_int;
68   glong    v_long;
69   gint64   v_int64;
70   gdouble  v_double;
71   gpointer v_pointer;
72 };
73
74 /**
75  * G_VALUE_COLLECT_INIT:
76  * @value: a #GValue return location. @value must contain only 0 bytes.
77  * @_value_type: the #GType to use for @value.
78  * @var_args: the va_list variable; it may be evaluated multiple times
79  * @flags: flags which are passed on to the collect_value() function of
80  *  the #GTypeValueTable of @value.
81  * @__error: a #gchar** variable that will be modified to hold a g_new()
82  *  allocated error messages if something fails
83  * 
84  * Collects a variable argument value from a va_list. We have to
85  * implement the varargs collection as a macro, because on some systems
86  * va_list variables cannot be passed by reference.
87  *
88  * Since: 2.24
89  */
90 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error)              \
91 G_STMT_START {                                                                          \
92   GValue *_val = (value);                                                               \
93   guint _flags = (flags);                                                               \
94   GTypeValueTable *_vtab = g_type_value_table_peek (_value_type);                       \
95   gchar *_collect_format = _vtab->collect_format;                                       \
96   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
97   guint _n_values = 0;                                                                  \
98                                                                                         \
99   _val->g_type = _value_type;           /* value_meminit() from gvalue.c */             \
100   while (*_collect_format)                                                              \
101     {                                                                                   \
102       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
103                                                                                         \
104       switch (*_collect_format++)                                                       \
105         {                                                                               \
106         case G_VALUE_COLLECT_INT:                                                       \
107           _cvalue->v_int = va_arg ((var_args), gint);                                   \
108           break;                                                                        \
109         case G_VALUE_COLLECT_LONG:                                                      \
110           _cvalue->v_long = va_arg ((var_args), glong);                                 \
111           break;                                                                        \
112         case G_VALUE_COLLECT_INT64:                                                     \
113           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
114           break;                                                                        \
115         case G_VALUE_COLLECT_DOUBLE:                                                    \
116           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
117           break;                                                                        \
118         case G_VALUE_COLLECT_POINTER:                                                   \
119           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
120           break;                                                                        \
121         default:                                                                        \
122           g_assert_not_reached ();                                                      \
123         }                                                                               \
124     }                                                                                   \
125   *(__error) = _vtab->collect_value (_val,                                              \
126                                        _n_values,                                       \
127                                        _cvalues,                                        \
128                                        _flags);                                         \
129 } G_STMT_END
130
131 /**
132  * G_VALUE_COLLECT:
133  * @value: a #GValue return location. @value is supposed to be initialized
134  *  according to the value type to be collected
135  * @var_args: the va_list variable; it may be evaluated multiple times
136  * @flags: flags which are passed on to the collect_value() function of
137  *  the #GTypeValueTable of @value.
138  * @__error: a #gchar** variable that will be modified to hold a g_new()
139  *  allocated error messages if something fails
140  *
141  * Collects a variable argument value from a va_list. We have to
142  * implement the varargs collection as a macro, because on some systems
143  * va_list variables cannot be passed by reference.
144  *
145  * Note: If you are creating the @value argument just before calling this macro,
146  * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
147  * #GValue. That variant is faster than #G_VALUE_COLLECT.
148  */
149 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START {                 \
150   GValue *_value = (value);                                                             \
151   GType _value_type = G_VALUE_TYPE (_value);                                            \
152   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
153                                                                                         \
154   if (_vtable->value_free)                                                              \
155     _vtable->value_free (_value);                                                       \
156   memset (_value->data, 0, sizeof (_value->data));                                      \
157                                                                                         \
158   G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error);                   \
159 } G_STMT_END
160
161 #define G_VALUE_COLLECT_SKIP(_value_type, var_args)                                     \
162 G_STMT_START {                                                                          \
163   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
164   gchar *_collect_format = _vtable->collect_format;                                     \
165                                                                                         \
166   while (*_collect_format)                                                              \
167     {                                                                                   \
168       switch (*_collect_format++)                                                       \
169         {                                                                               \
170         case G_VALUE_COLLECT_INT:                                                       \
171           va_arg ((var_args), gint);                                                    \
172           break;                                                                        \
173         case G_VALUE_COLLECT_LONG:                                                      \
174           va_arg ((var_args), glong);                                                   \
175           break;                                                                        \
176         case G_VALUE_COLLECT_INT64:                                                     \
177           va_arg ((var_args), gint64);                                                  \
178           break;                                                                        \
179         case G_VALUE_COLLECT_DOUBLE:                                                    \
180           va_arg ((var_args), gdouble);                                                 \
181           break;                                                                        \
182         case G_VALUE_COLLECT_POINTER:                                                   \
183           va_arg ((var_args), gpointer);                                                \
184           break;                                                                        \
185         default:                                                                        \
186           g_assert_not_reached ();                                                      \
187         }                                                                               \
188     }                                                                                   \
189 } G_STMT_END
190
191 /**
192  * G_VALUE_LCOPY:
193  * @value: a #GValue return location. @value is supposed to be initialized 
194  *  according to the value type to be collected
195  * @var_args: the va_list variable; it may be evaluated multiple times
196  * @flags: flags which are passed on to the lcopy_value() function of
197  *  the #GTypeValueTable of @value.
198  * @__error: a #gchar** variable that will be modified to hold a g_new()
199  *  allocated error messages if something fails
200  * 
201  * Collects a value's variable argument locations from a va_list. Usage is
202  * analogous to G_VALUE_COLLECT().
203  */
204 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
205 G_STMT_START {                                                                          \
206   const GValue *_value = (value);                                                       \
207   guint _flags = (flags);                                                               \
208   GType _value_type = G_VALUE_TYPE (_value);                                            \
209   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
210   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
211   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
212   guint _n_values = 0;                                                                  \
213                                                                                         \
214   while (*_lcopy_format)                                                                \
215     {                                                                                   \
216       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
217                                                                                         \
218       switch (*_lcopy_format++)                                                         \
219         {                                                                               \
220         case G_VALUE_COLLECT_INT:                                                       \
221           _cvalue->v_int = va_arg ((var_args), gint);                                   \
222           break;                                                                        \
223         case G_VALUE_COLLECT_LONG:                                                      \
224           _cvalue->v_long = va_arg ((var_args), glong);                                 \
225           break;                                                                        \
226         case G_VALUE_COLLECT_INT64:                                                     \
227           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
228           break;                                                                        \
229         case G_VALUE_COLLECT_DOUBLE:                                                    \
230           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
231           break;                                                                        \
232         case G_VALUE_COLLECT_POINTER:                                                   \
233           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
234           break;                                                                        \
235         default:                                                                        \
236           g_assert_not_reached ();                                                      \
237         }                                                                               \
238     }                                                                                   \
239   *(__error) = _vtable->lcopy_value (_value,                                            \
240                                      _n_values,                                         \
241                                      _cvalues,                                          \
242                                      _flags);                                           \
243 } G_STMT_END
244
245
246 /**
247  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
248  * 
249  * The maximal number of #GTypeCValue<!-- -->s which can be collected for a 
250  * single #GValue.
251  */
252 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
253
254 G_END_DECLS
255
256 #endif /* __G_VALUE_COLLECTOR_H__ */