sl@0
|
1 |
/* GObject - GLib Type, Object, Parameter and Signal Library
|
sl@0
|
2 |
* Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General
|
sl@0
|
15 |
* Public License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*
|
sl@0
|
19 |
* gvaluecollector.h: GValue varargs stubs
|
sl@0
|
20 |
*/
|
sl@0
|
21 |
/**
|
sl@0
|
22 |
* SECTION:value_collection
|
sl@0
|
23 |
* @Short_description: Converting varargs to generic values
|
sl@0
|
24 |
* @See_also:#GValueTable
|
sl@0
|
25 |
* @Title: Varargs Value Collection
|
sl@0
|
26 |
*
|
sl@0
|
27 |
* The macros in this section provide the varargs parsing support needed
|
sl@0
|
28 |
* in variadic GObject functions such as g_object_new() or g_object_set().
|
sl@0
|
29 |
* They currently support the collection of integral types, floating point
|
sl@0
|
30 |
* types and pointers.
|
sl@0
|
31 |
*/
|
sl@0
|
32 |
#ifndef __G_VALUE_COLLECTOR_H__
|
sl@0
|
33 |
#define __G_VALUE_COLLECTOR_H__
|
sl@0
|
34 |
|
sl@0
|
35 |
#include <glib-object.h>
|
sl@0
|
36 |
|
sl@0
|
37 |
G_BEGIN_DECLS
|
sl@0
|
38 |
|
sl@0
|
39 |
/* we may want to add aggregate types here some day, if requested
|
sl@0
|
40 |
* by users. the basic C types are covered already, everything
|
sl@0
|
41 |
* smaller than an int is promoted to an integer and floats are
|
sl@0
|
42 |
* always promoted to doubles for varargs call constructions.
|
sl@0
|
43 |
*/
|
sl@0
|
44 |
enum /*< skip >*/
|
sl@0
|
45 |
{
|
sl@0
|
46 |
G_VALUE_COLLECT_INT = 'i',
|
sl@0
|
47 |
G_VALUE_COLLECT_LONG = 'l',
|
sl@0
|
48 |
G_VALUE_COLLECT_INT64 = 'q',
|
sl@0
|
49 |
G_VALUE_COLLECT_DOUBLE = 'd',
|
sl@0
|
50 |
G_VALUE_COLLECT_POINTER = 'p'
|
sl@0
|
51 |
};
|
sl@0
|
52 |
|
sl@0
|
53 |
|
sl@0
|
54 |
/* vararg union holding actuall values collected
|
sl@0
|
55 |
*/
|
sl@0
|
56 |
/**
|
sl@0
|
57 |
* GTypeCValue:
|
sl@0
|
58 |
*
|
sl@0
|
59 |
* A union holding one collected value.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
union _GTypeCValue
|
sl@0
|
62 |
{
|
sl@0
|
63 |
gint v_int;
|
sl@0
|
64 |
glong v_long;
|
sl@0
|
65 |
gint64 v_int64;
|
sl@0
|
66 |
gdouble v_double;
|
sl@0
|
67 |
gpointer v_pointer;
|
sl@0
|
68 |
};
|
sl@0
|
69 |
|
sl@0
|
70 |
/**
|
sl@0
|
71 |
* G_VALUE_COLLECT:
|
sl@0
|
72 |
* @value: a #GValue return location. @value is supposed to be initialized
|
sl@0
|
73 |
* according to the value type to be collected
|
sl@0
|
74 |
* @var_args: the va_list variable; it may be evaluated multiple times
|
sl@0
|
75 |
* @flags: flags which are passed on to the collect_value() function of
|
sl@0
|
76 |
* the #GTypeValueTable of @value.
|
sl@0
|
77 |
* @__error: a #gchar** variable that will be modified to hold a g_new()
|
sl@0
|
78 |
* allocated error messages if something fails
|
sl@0
|
79 |
*
|
sl@0
|
80 |
* Collects a variable argument value from a va_list. We have to
|
sl@0
|
81 |
* implement the varargs collection as a macro, because on some systems
|
sl@0
|
82 |
* va_list variables cannot be passed by reference.
|
sl@0
|
83 |
*/
|
sl@0
|
84 |
#define G_VALUE_COLLECT(value, var_args, flags, __error) \
|
sl@0
|
85 |
G_STMT_START { \
|
sl@0
|
86 |
GValue *_value = (value); \
|
sl@0
|
87 |
guint _flags = (flags); \
|
sl@0
|
88 |
GType _value_type = G_VALUE_TYPE (_value); \
|
sl@0
|
89 |
GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
|
sl@0
|
90 |
gchar *_collect_format = _vtable->collect_format; \
|
sl@0
|
91 |
GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
|
sl@0
|
92 |
guint _n_values = 0; \
|
sl@0
|
93 |
\
|
sl@0
|
94 |
if (_vtable->value_free) \
|
sl@0
|
95 |
_vtable->value_free (_value); \
|
sl@0
|
96 |
_value->g_type = _value_type; /* value_meminit() from gvalue.c */ \
|
sl@0
|
97 |
memset (_value->data, 0, sizeof (_value->data)); \
|
sl@0
|
98 |
while (*_collect_format) \
|
sl@0
|
99 |
{ \
|
sl@0
|
100 |
GTypeCValue *_cvalue = _cvalues + _n_values++; \
|
sl@0
|
101 |
\
|
sl@0
|
102 |
switch (*_collect_format++) \
|
sl@0
|
103 |
{ \
|
sl@0
|
104 |
case G_VALUE_COLLECT_INT: \
|
sl@0
|
105 |
_cvalue->v_int = va_arg ((var_args), gint); \
|
sl@0
|
106 |
break; \
|
sl@0
|
107 |
case G_VALUE_COLLECT_LONG: \
|
sl@0
|
108 |
_cvalue->v_long = va_arg ((var_args), glong); \
|
sl@0
|
109 |
break; \
|
sl@0
|
110 |
case G_VALUE_COLLECT_INT64: \
|
sl@0
|
111 |
_cvalue->v_int64 = va_arg ((var_args), gint64); \
|
sl@0
|
112 |
break; \
|
sl@0
|
113 |
case G_VALUE_COLLECT_DOUBLE: \
|
sl@0
|
114 |
_cvalue->v_double = va_arg ((var_args), gdouble); \
|
sl@0
|
115 |
break; \
|
sl@0
|
116 |
case G_VALUE_COLLECT_POINTER: \
|
sl@0
|
117 |
_cvalue->v_pointer = va_arg ((var_args), gpointer); \
|
sl@0
|
118 |
break; \
|
sl@0
|
119 |
default: \
|
sl@0
|
120 |
g_assert_not_reached (); \
|
sl@0
|
121 |
} \
|
sl@0
|
122 |
} \
|
sl@0
|
123 |
*(__error) = _vtable->collect_value (_value, \
|
sl@0
|
124 |
_n_values, \
|
sl@0
|
125 |
_cvalues, \
|
sl@0
|
126 |
_flags); \
|
sl@0
|
127 |
} G_STMT_END
|
sl@0
|
128 |
|
sl@0
|
129 |
|
sl@0
|
130 |
/**
|
sl@0
|
131 |
* G_VALUE_LCOPY:
|
sl@0
|
132 |
* @value: a #GValue return location. @value is supposed to be initialized
|
sl@0
|
133 |
* according to the value type to be collected
|
sl@0
|
134 |
* @var_args: the va_list variable; it may be evaluated multiple times
|
sl@0
|
135 |
* @flags: flags which are passed on to the lcopy_value() function of
|
sl@0
|
136 |
* the #GTypeValueTable of @value.
|
sl@0
|
137 |
* @__error: a #gchar** variable that will be modified to hold a g_new()
|
sl@0
|
138 |
* allocated error messages if something fails
|
sl@0
|
139 |
*
|
sl@0
|
140 |
* Collects a value's variable argument locations from a va_list. Usage is
|
sl@0
|
141 |
* analogous to G_VALUE_COLLECT().
|
sl@0
|
142 |
*/
|
sl@0
|
143 |
#define G_VALUE_LCOPY(value, var_args, flags, __error) \
|
sl@0
|
144 |
G_STMT_START { \
|
sl@0
|
145 |
const GValue *_value = (value); \
|
sl@0
|
146 |
guint _flags = (flags); \
|
sl@0
|
147 |
GType _value_type = G_VALUE_TYPE (_value); \
|
sl@0
|
148 |
GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
|
sl@0
|
149 |
gchar *_lcopy_format = _vtable->lcopy_format; \
|
sl@0
|
150 |
GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
|
sl@0
|
151 |
guint _n_values = 0; \
|
sl@0
|
152 |
\
|
sl@0
|
153 |
while (*_lcopy_format) \
|
sl@0
|
154 |
{ \
|
sl@0
|
155 |
GTypeCValue *_cvalue = _cvalues + _n_values++; \
|
sl@0
|
156 |
\
|
sl@0
|
157 |
switch (*_lcopy_format++) \
|
sl@0
|
158 |
{ \
|
sl@0
|
159 |
case G_VALUE_COLLECT_INT: \
|
sl@0
|
160 |
_cvalue->v_int = va_arg ((var_args), gint); \
|
sl@0
|
161 |
break; \
|
sl@0
|
162 |
case G_VALUE_COLLECT_LONG: \
|
sl@0
|
163 |
_cvalue->v_long = va_arg ((var_args), glong); \
|
sl@0
|
164 |
break; \
|
sl@0
|
165 |
case G_VALUE_COLLECT_INT64: \
|
sl@0
|
166 |
_cvalue->v_int64 = va_arg ((var_args), gint64); \
|
sl@0
|
167 |
break; \
|
sl@0
|
168 |
case G_VALUE_COLLECT_DOUBLE: \
|
sl@0
|
169 |
_cvalue->v_double = va_arg ((var_args), gdouble); \
|
sl@0
|
170 |
break; \
|
sl@0
|
171 |
case G_VALUE_COLLECT_POINTER: \
|
sl@0
|
172 |
_cvalue->v_pointer = va_arg ((var_args), gpointer); \
|
sl@0
|
173 |
break; \
|
sl@0
|
174 |
default: \
|
sl@0
|
175 |
g_assert_not_reached (); \
|
sl@0
|
176 |
} \
|
sl@0
|
177 |
} \
|
sl@0
|
178 |
*(__error) = _vtable->lcopy_value (_value, \
|
sl@0
|
179 |
_n_values, \
|
sl@0
|
180 |
_cvalues, \
|
sl@0
|
181 |
_flags); \
|
sl@0
|
182 |
} G_STMT_END
|
sl@0
|
183 |
|
sl@0
|
184 |
|
sl@0
|
185 |
/**
|
sl@0
|
186 |
* G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
|
sl@0
|
187 |
*
|
sl@0
|
188 |
* The maximal number of #GTypeCValue<!-- -->s which can be collected for a
|
sl@0
|
189 |
* single #GValue.
|
sl@0
|
190 |
*/
|
sl@0
|
191 |
#define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
|
sl@0
|
192 |
|
sl@0
|
193 |
G_END_DECLS
|
sl@0
|
194 |
|
sl@0
|
195 |
#endif /* __G_VALUE_COLLECTOR_H__ */
|