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