changed prototype of g_boxed_type_register_static() to contain an optional
[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 #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   GType _value_type = G_VALUE_TYPE (_value);                                            \
70   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
71   gchar *_collect_format = _vtable->collect_format;                                     \
72   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
73   guint _n_values = 0;                                                                  \
74                                                                                         \
75   if (_vtable->value_free)                                                              \
76     _vtable->value_free (_value);                                                       \
77   _value->g_type = _value_type;         /* value_meminit() from gvalue.c */             \
78   memset (_value->data, 0, sizeof (_value->data));                                      \
79   while (*_collect_format)                                                              \
80     {                                                                                   \
81       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
82                                                                                         \
83       switch (*_collect_format++)                                                       \
84         {                                                                               \
85         case G_VALUE_COLLECT_INT:                                                       \
86           _cvalue->v_int = va_arg ((var_args), gint);                                   \
87           break;                                                                        \
88         case G_VALUE_COLLECT_LONG:                                                      \
89           _cvalue->v_long = va_arg ((var_args), glong);                                 \
90           break;                                                                        \
91         case G_VALUE_COLLECT_DOUBLE:                                                    \
92           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
93           break;                                                                        \
94         case G_VALUE_COLLECT_POINTER:                                                   \
95           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
96           break;                                                                        \
97         default:                                                                        \
98           g_assert_not_reached ();                                                      \
99         }                                                                               \
100     }                                                                                   \
101   *(__error) = _vtable->collect_value (_value,                                          \
102                                        _n_values,                                       \
103                                        _cvalues,                                        \
104                                        _flags);                                         \
105 } G_STMT_END
106
107
108 /* G_VALUE_LCOPY() collects a value's variable argument
109  * locations from a va_list. usage is analogous to G_VALUE_COLLECT().
110  */
111 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
112 G_STMT_START {                                                                          \
113   GValue *_value = (value);                                                             \
114   guint _flags = (flags);                                                               \
115   GType _value_type = G_VALUE_TYPE (_value);                                            \
116   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
117   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
118   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
119   guint _n_values = 0;                                                                  \
120                                                                                         \
121   while (*_lcopy_format)                                                                \
122     {                                                                                   \
123       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
124                                                                                         \
125       switch (*_lcopy_format++)                                                         \
126         {                                                                               \
127         case G_VALUE_COLLECT_INT:                                                       \
128           _cvalue->v_int = va_arg ((var_args), gint);                                   \
129           break;                                                                        \
130         case G_VALUE_COLLECT_LONG:                                                      \
131           _cvalue->v_long = va_arg ((var_args), glong);                                 \
132           break;                                                                        \
133         case G_VALUE_COLLECT_DOUBLE:                                                    \
134           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
135           break;                                                                        \
136         case G_VALUE_COLLECT_POINTER:                                                   \
137           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
138           break;                                                                        \
139         default:                                                                        \
140           g_assert_not_reached ();                                                      \
141         }                                                                               \
142     }                                                                                   \
143   *(__error) = _vtable->lcopy_value (_value,                                            \
144                                      _n_values,                                         \
145                                      _cvalues,                                          \
146                                      _flags);                                           \
147 } G_STMT_END
148
149
150 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
151
152
153
154 #ifdef __cplusplus
155 }
156 #endif /* __cplusplus */
157
158 #endif /* __G_VALUE_COLLECTOR_H__ */