7bbdfc0b466ae24bc5e7c1f383e6ba9f586e530d
[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_NONE,
38   G_VALUE_COLLECT_INT,
39   G_VALUE_COLLECT_LONG,
40   G_VALUE_COLLECT_DOUBLE,
41   G_VALUE_COLLECT_POINTER
42 };
43
44 union _GTypeCValue
45 {
46   gint     v_int;
47   glong    v_long;
48   gdouble  v_double;
49   gpointer v_pointer;
50 };
51
52
53 /* G_VALUE_COLLECT() collects a variable argument value
54  * from a va_list. we have to implement the varargs collection as a
55  * macro, because on some systems va_list variables cannot be passed
56  * by reference.
57  * value is supposed to be initialized according to the value
58  * type to be collected.
59  * var_args is the va_list variable and may be evaluated multiple times.
60  * __error is a gchar** variable that will be modified to hold a g_new()
61  * allocated error messages if something fails.
62  */
63 #define G_VALUE_COLLECT(value, var_args, __error)                                       \
64 G_STMT_START {                                                                          \
65   GValue *_value = (value);                                                             \
66   GTypeValueTable *_vtable = g_type_value_table_peek (G_VALUE_TYPE (_value));           \
67   gchar *_error_msg = NULL;                                                             \
68   guint _collect_type = _vtable->collect_type;                                          \
69   guint _nth_value = 0;                                                                 \
70                                                                                         \
71   g_value_reset (_value);                                                               \
72   while (_collect_type && !_error_msg)                                                  \
73     {                                                                                   \
74       GTypeCValue _cvalue;                                                              \
75                                                                                         \
76       memset (&_cvalue, 0, sizeof (_cvalue));                                           \
77       switch (_collect_type)                                                            \
78         {                                                                               \
79         case G_VALUE_COLLECT_INT:                                                       \
80           _cvalue.v_int = va_arg ((var_args), gint);                                    \
81           break;                                                                        \
82         case G_VALUE_COLLECT_LONG:                                                      \
83           _cvalue.v_long = va_arg ((var_args), glong);                                  \
84           break;                                                                        \
85         case G_VALUE_COLLECT_DOUBLE:                                                    \
86           _cvalue.v_double = va_arg ((var_args), gdouble);                              \
87           break;                                                                        \
88         case G_VALUE_COLLECT_POINTER:                                                   \
89           _cvalue.v_pointer = va_arg ((var_args), gpointer);                            \
90           break;                                                                        \
91         default:                                                                        \
92           _error_msg  = g_strdup_printf ("%s: invalid collect type (%d) used for %s",   \
93                                          G_STRLOC,                                      \
94                                          _collect_type,                                 \
95                                          "G_VALUE_COLLECT()");                          \
96           continue;                                                                     \
97         }                                                                               \
98       _error_msg = _vtable->collect_value (_value,                                      \
99                                            _nth_value++,                                \
100                                            &_collect_type,                              \
101                                            &_cvalue);                                   \
102     }                                                                                   \
103   *(__error) = _error_msg;                                                              \
104 } G_STMT_END
105
106
107 /* G_VALUE_LCOPY() collects a value's variable argument
108  * locations from a va_list. usage is analogous to G_VALUE_COLLECT().
109  */
110 #define G_VALUE_LCOPY(value, var_args, __error)                                         \
111 G_STMT_START {                                                                          \
112   GValue *_value = (value);                                                             \
113   GTypeValueTable *_vtable = g_type_value_table_peek (G_VALUE_TYPE (_value));           \
114   gchar *_error_msg = NULL;                                                             \
115   guint _lcopy_type = _vtable->lcopy_type;                                              \
116   guint _nth_value = 0;                                                                 \
117                                                                                         \
118   while (_lcopy_type && !_error_msg)                                                    \
119     {                                                                                   \
120       GTypeCValue _cvalue;                                                              \
121                                                                                         \
122       memset (&_cvalue, 0, sizeof (_cvalue));                                           \
123       switch (_lcopy_type)                                                              \
124         {                                                                               \
125         case G_VALUE_COLLECT_INT:                                                       \
126           _cvalue.v_int = va_arg ((var_args), gint);                                    \
127           break;                                                                        \
128         case G_VALUE_COLLECT_LONG:                                                      \
129           _cvalue.v_long = va_arg ((var_args), glong);                                  \
130           break;                                                                        \
131         case G_VALUE_COLLECT_DOUBLE:                                                    \
132           _cvalue.v_double = va_arg ((var_args), gdouble);                              \
133           break;                                                                        \
134         case G_VALUE_COLLECT_POINTER:                                                   \
135           _cvalue.v_pointer = va_arg ((var_args), gpointer);                            \
136           break;                                                                        \
137         default:                                                                        \
138           _error_msg  = g_strdup_printf ("%s: invalid collect type (%d) used for %s",   \
139                                          G_STRLOC,                                      \
140                                          _lcopy_type,                                   \
141                                          "G_VALUE_LCOPY()");                            \
142           continue;                                                                     \
143         }                                                                               \
144       _error_msg = _vtable->lcopy_value (_value,                                        \
145                                          _nth_value++,                                  \
146                                          &_lcopy_type,                                  \
147                                          &_cvalue);                                     \
148     }                                                                                   \
149   *(__error) = _error_msg;                                                              \
150 } G_STMT_END
151
152
153
154 #ifdef __cplusplus
155 }
156 #endif /* __cplusplus */
157
158 #endif /* __G_VALUE_COLLECTOR_H__ */