changed collect_format, collect_value() and lcopy_format, lcopy_value() in
[platform/upstream/glib.git] / gobject / gvaluecollector.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 1998, 1999, 2000 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 #ifndef __G_VALUE_COLLECTOR_H__
22 #define __G_VALUE_COLLECTOR_H__
23
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif /* __cplusplus */
28
29
30 /* we may want to add aggregate types here some day, if requested
31  * by users. the basic C types are covered already, everything
32  * smaller than an int is promoted to an integer and floats are
33  * always promoted to doubles for varargs call constructions.
34  */
35 enum    /*< skip >*/
36 {
37   G_VALUE_COLLECT_INT           = 'i',
38   G_VALUE_COLLECT_LONG          = 'l',
39   G_VALUE_COLLECT_DOUBLE        = 'd',
40   G_VALUE_COLLECT_POINTER       = 'p'
41 };
42
43
44 /* vararg union holding actuall values collected
45  */
46 union _GTypeCValue
47 {
48   gint     v_int;
49   glong    v_long;
50   gdouble  v_double;
51   gpointer v_pointer;
52 };
53
54
55 /* G_VALUE_COLLECT() collects a variable argument value
56  * from a va_list. we have to implement the varargs collection as a
57  * macro, because on some systems va_list variables cannot be passed
58  * by reference.
59  * value is supposed to be initialized according to the value
60  * type to be collected.
61  * var_args is the va_list variable and may be evaluated multiple times.
62  * __error is a gchar** variable that will be modified to hold a g_new()
63  * allocated error messages if something fails.
64  */
65 #define G_VALUE_COLLECT(value, var_args, flags, __error)                                \
66 G_STMT_START {                                                                          \
67   GValue *_value = (value);                                                             \
68   guint _flags = (flags);                                                               \
69   GTypeValueTable *_vtable = g_type_value_table_peek (G_VALUE_TYPE (_value));           \
70   gchar *_collect_format = _vtable->collect_format;                                     \
71   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
72   guint _n_values = 0;                                                                  \
73                                                                                         \
74   g_value_reset (_value);                                                               \
75   while (*_collect_format)                                                              \
76     {                                                                                   \
77       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
78                                                                                         \
79       switch (*_collect_format++)                                                       \
80         {                                                                               \
81         case G_VALUE_COLLECT_INT:                                                       \
82           _cvalue->v_int = va_arg ((var_args), gint);                                   \
83           break;                                                                        \
84         case G_VALUE_COLLECT_LONG:                                                      \
85           _cvalue->v_long = va_arg ((var_args), glong);                                 \
86           break;                                                                        \
87         case G_VALUE_COLLECT_DOUBLE:                                                    \
88           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
89           break;                                                                        \
90         case G_VALUE_COLLECT_POINTER:                                                   \
91           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
92           break;                                                                        \
93         default:                                                                        \
94           g_assert_not_reached ();                                                      \
95         }                                                                               \
96     }                                                                                   \
97   *(__error) = _vtable->collect_value (_value,                                          \
98                                        _n_values,                                       \
99                                        _cvalues,                                        \
100                                        _flags);                                         \
101 } G_STMT_END
102
103
104 /* G_VALUE_LCOPY() collects a value's variable argument
105  * locations from a va_list. usage is analogous to G_VALUE_COLLECT().
106  */
107 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
108 G_STMT_START {                                                                          \
109   GValue *_value = (value);                                                             \
110   guint _flags = (flags);                                                               \
111   GTypeValueTable *_vtable = g_type_value_table_peek (G_VALUE_TYPE (_value));           \
112   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
113   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
114   guint _n_values = 0;                                                                  \
115                                                                                         \
116   while (*_lcopy_format)                                                                \
117     {                                                                                   \
118       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
119                                                                                         \
120       switch (*_lcopy_format++)                                                         \
121         {                                                                               \
122         case G_VALUE_COLLECT_INT:                                                       \
123           _cvalue->v_int = va_arg ((var_args), gint);                                   \
124           break;                                                                        \
125         case G_VALUE_COLLECT_LONG:                                                      \
126           _cvalue->v_long = va_arg ((var_args), glong);                                 \
127           break;                                                                        \
128         case G_VALUE_COLLECT_DOUBLE:                                                    \
129           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
130           break;                                                                        \
131         case G_VALUE_COLLECT_POINTER:                                                   \
132           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
133           break;                                                                        \
134         default:                                                                        \
135           g_assert_not_reached ();                                                      \
136         }                                                                               \
137     }                                                                                   \
138   *(__error) = _vtable->lcopy_value (_value,                                            \
139                                      _n_values,                                         \
140                                      _cvalues,                                          \
141                                      _flags);                                           \
142 } G_STMT_END
143
144
145 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
146
147
148
149 #ifdef __cplusplus
150 }
151 #endif /* __cplusplus */
152
153 #endif /* __G_VALUE_COLLECTOR_H__ */