• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 actuall values collected
55  */
56 /**
57  * GTypeCValue:
58  *
59  * A union holding one collected value.
60  */
61 union _GTypeCValue
62 {
63   gint     v_int;
64   glong    v_long;
65   gint64   v_int64;
66   gdouble  v_double;
67   gpointer v_pointer;
68 };
69 
70 /**
71  * G_VALUE_COLLECT:
72  * @value: a #GValue return location. @value is supposed to be initialized
73  *  according to the value type to be collected
74  * @var_args: the va_list variable; it may be evaluated multiple times
75  * @flags: flags which are passed on to the collect_value() function of
76  *  the #GTypeValueTable of @value.
77  * @__error: a #gchar** variable that will be modified to hold a g_new()
78  *  allocated error messages if something fails
79  *
80  * Collects a variable argument value from a va_list. We have to
81  * implement the varargs collection as a macro, because on some systems
82  * va_list variables cannot be passed by reference.
83  */
84 #define G_VALUE_COLLECT(value, var_args, flags, __error)				\
85 G_STMT_START {										\
86   GValue *_value = (value);								\
87   guint _flags = (flags);								\
88   GType _value_type = G_VALUE_TYPE (_value);						\
89   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);			\
90   gchar *_collect_format = _vtable->collect_format;					\
91   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };		\
92   guint _n_values = 0;									\
93                                                                                         \
94   if (_vtable->value_free)								\
95     _vtable->value_free (_value);							\
96   _value->g_type = _value_type;		/* value_meminit() from gvalue.c */		\
97   memset (_value->data, 0, sizeof (_value->data));					\
98   while (*_collect_format)								\
99     {											\
100       GTypeCValue *_cvalue = _cvalues + _n_values++;					\
101                                                                                         \
102       switch (*_collect_format++)							\
103 	{										\
104 	case G_VALUE_COLLECT_INT:							\
105 	  _cvalue->v_int = va_arg ((var_args), gint);					\
106 	  break;									\
107 	case G_VALUE_COLLECT_LONG:							\
108 	  _cvalue->v_long = va_arg ((var_args), glong);					\
109 	  break;									\
110 	case G_VALUE_COLLECT_INT64:							\
111 	  _cvalue->v_int64 = va_arg ((var_args), gint64);				\
112 	  break;									\
113 	case G_VALUE_COLLECT_DOUBLE:							\
114 	  _cvalue->v_double = va_arg ((var_args), gdouble);				\
115 	  break;									\
116 	case G_VALUE_COLLECT_POINTER:							\
117 	  _cvalue->v_pointer = va_arg ((var_args), gpointer);				\
118 	  break;									\
119 	default:									\
120 	  g_assert_not_reached ();							\
121 	}										\
122     }											\
123   *(__error) = _vtable->collect_value (_value,						\
124 				       _n_values,					\
125 				       _cvalues,					\
126 				       _flags);						\
127 } G_STMT_END
128 
129 
130 /**
131  * G_VALUE_LCOPY:
132  * @value: a #GValue return location. @value is supposed to be initialized
133  *  according to the value type to be collected
134  * @var_args: the va_list variable; it may be evaluated multiple times
135  * @flags: flags which are passed on to the lcopy_value() function of
136  *  the #GTypeValueTable of @value.
137  * @__error: a #gchar** variable that will be modified to hold a g_new()
138  *  allocated error messages if something fails
139  *
140  * Collects a value's variable argument locations from a va_list. Usage is
141  * analogous to G_VALUE_COLLECT().
142  */
143 #define G_VALUE_LCOPY(value, var_args, flags, __error)					\
144 G_STMT_START {										\
145   const GValue *_value = (value);							\
146   guint _flags = (flags);								\
147   GType _value_type = G_VALUE_TYPE (_value);						\
148   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);			\
149   gchar *_lcopy_format = _vtable->lcopy_format;						\
150   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };		\
151   guint _n_values = 0;									\
152                                                                                         \
153   while (*_lcopy_format)								\
154     {											\
155       GTypeCValue *_cvalue = _cvalues + _n_values++;					\
156                                                                                         \
157       switch (*_lcopy_format++)								\
158 	{										\
159 	case G_VALUE_COLLECT_INT:							\
160 	  _cvalue->v_int = va_arg ((var_args), gint);					\
161 	  break;									\
162 	case G_VALUE_COLLECT_LONG:							\
163 	  _cvalue->v_long = va_arg ((var_args), glong);					\
164 	  break;									\
165 	case G_VALUE_COLLECT_INT64:							\
166 	  _cvalue->v_int64 = va_arg ((var_args), gint64);				\
167 	  break;									\
168 	case G_VALUE_COLLECT_DOUBLE:							\
169 	  _cvalue->v_double = va_arg ((var_args), gdouble);				\
170 	  break;									\
171 	case G_VALUE_COLLECT_POINTER:							\
172 	  _cvalue->v_pointer = va_arg ((var_args), gpointer);				\
173 	  break;									\
174 	default:									\
175 	  g_assert_not_reached ();							\
176 	}										\
177     }											\
178   *(__error) = _vtable->lcopy_value (_value,						\
179 				     _n_values,						\
180 				     _cvalues,						\
181 				     _flags);						\
182 } G_STMT_END
183 
184 
185 /**
186  * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
187  *
188  * The maximal number of #GTypeCValue<!-- -->s which can be collected for a
189  * single #GValue.
190  */
191 #define	G_VALUE_COLLECT_FORMAT_MAX_LENGTH	(8)
192 
193 G_END_DECLS
194 
195 #endif /* __G_VALUE_COLLECTOR_H__ */
196