gvaluecollector: Add variant of G_VALUE_COLLECT for most used cases.
[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 is supposed to be clean (freshly
73  * initialized and/or freed).
74  * @_value_type: the #GType to use for @value.
75  * @var_args: the va_list variable; it may be evaluated multiple times
76  * @flags: flags which are passed on to the collect_value() function of
77  *  the #GTypeValueTable of @value.
78  * @__error: a #gchar** variable that will be modified to hold a g_new()
79  *  allocated error messages if something fails
80  * 
81  * Collects a variable argument value from a va_list. We have to
82  * implement the varargs collection as a macro, because on some systems
83  * va_list variables cannot be passed by reference.
84  *
85  * Since: 2.23.1
86  */
87 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error)              \
88 G_STMT_START {                                                                          \
89   GValue *_value = (value);                                                             \
90   guint _flags = (flags);                                                               \
91   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
92   gchar *_collect_format = _vtable->collect_format;                                     \
93   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
94   guint _n_values = 0;                                                                  \
95                                                                                         \
96   _value->g_type = _value_type;         /* value_meminit() from gvalue.c */             \
97   while (*_collect_format)                                                              \
98     {                                                                                   \
99       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
100                                                                                         \
101       switch (*_collect_format++)                                                       \
102         {                                                                               \
103         case G_VALUE_COLLECT_INT:                                                       \
104           _cvalue->v_int = va_arg ((var_args), gint);                                   \
105           break;                                                                        \
106         case G_VALUE_COLLECT_LONG:                                                      \
107           _cvalue->v_long = va_arg ((var_args), glong);                                 \
108           break;                                                                        \
109         case G_VALUE_COLLECT_INT64:                                                     \
110           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
111           break;                                                                        \
112         case G_VALUE_COLLECT_DOUBLE:                                                    \
113           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
114           break;                                                                        \
115         case G_VALUE_COLLECT_POINTER:                                                   \
116           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
117           break;                                                                        \
118         default:                                                                        \
119           g_assert_not_reached ();                                                      \
120         }                                                                               \
121     }                                                                                   \
122   *(__error) = _vtable->collect_value (_value,                                          \
123                                        _n_values,                                       \
124                                        _cvalues,                                        \
125                                        _flags);                                         \
126 } G_STMT_END
127
128 /**
129  * G_VALUE_COLLECT:
130  * @value: a #GValue return location. @value is supposed to be initialized
131  *  according to the value type to be collected
132  * @var_args: the va_list variable; it may be evaluated multiple times
133  * @flags: flags which are passed on to the collect_value() function of
134  *  the #GTypeValueTable of @value.
135  * @__error: a #gchar** variable that will be modified to hold a g_new()
136  *  allocated error messages if something fails
137  *
138  * Collects a variable argument value from a va_list. We have to
139  * implement the varargs collection as a macro, because on some systems
140  * va_list variables cannot be passed by reference.
141  *
142  * Note: If you are creating the @value argument just before calling this macro,
143  * you should use the #G_VALUE_COLLECT_FULL variant and pass the unitialized
144  * #GValue. That variant is faster than #G_VALUE_COLLECT.
145  */
146 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START {                 \
147   GValue *_value = (value);                                                             \
148   GType _value_type = G_VALUE_TYPE (_value);                                            \
149   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
150                                                                                         \
151   if (_vtable->value_free)                                                              \
152     _vtable->value_free (_value);                                                       \
153   memset (_value->data, 0, sizeof (_value->data));                                      \
154                                                                                         \
155   G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error);                   \
156 } G_STMT_END
157
158 /**
159  * G_VALUE_LCOPY:
160  * @value: a #GValue return location. @value is supposed to be initialized 
161  *  according to the value type to be collected
162  * @var_args: the va_list variable; it may be evaluated multiple times
163  * @flags: flags which are passed on to the lcopy_value() function of
164  *  the #GTypeValueTable of @value.
165  * @__error: a #gchar** variable that will be modified to hold a g_new()
166  *  allocated error messages if something fails
167  * 
168  * Collects a value's variable argument locations from a va_list. Usage is
169  * analogous to G_VALUE_COLLECT().
170  */
171 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
172 G_STMT_START {                                                                          \
173   const GValue *_value = (value);                                                       \
174   guint _flags = (flags);                                                               \
175   GType _value_type = G_VALUE_TYPE (_value);                                            \
176   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
177   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
178   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
179   guint _n_values = 0;                                                                  \
180                                                                                         \
181   while (*_lcopy_format)                                                                \
182     {                                                                                   \
183       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
184                                                                                         \
185       switch (*_lcopy_format++)                                                         \
186         {                                                                               \
187         case G_VALUE_COLLECT_INT:                                                       \
188           _cvalue->v_int = va_arg ((var_args), gint);                                   \
189           break;                                                                        \
190         case G_VALUE_COLLECT_LONG:                                                      \
191           _cvalue->v_long = va_arg ((var_args), glong);                                 \
192           break;                                                                        \
193         case G_VALUE_COLLECT_INT64:                                                     \
194           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
195           break;                                                                        \
196         case G_VALUE_COLLECT_DOUBLE:                                                    \
197           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
198           break;                                                                        \
199         case G_VALUE_COLLECT_POINTER:                                                   \
200           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
201           break;                                                                        \
202         default:                                                                        \
203           g_assert_not_reached ();                                                      \
204         }                                                                               \
205     }                                                                                   \
206   *(__error) = _vtable->lcopy_value (_value,                                            \
207                                      _n_values,                                         \
208                                      _cvalues,                                          \
209                                      _flags);                                           \
210 } G_STMT_END
211
212
213 /**
214  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
215  * 
216  * The maximal number of #GTypeCValue<!-- -->s which can be collected for a 
217  * single #GValue.
218  */
219 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
220
221 G_END_DECLS
222
223 #endif /* __G_VALUE_COLLECTOR_H__ */