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.1 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, see <http://www.gnu.org/licenses/>. 16 * 17 * gvaluecollector.h: GValue varargs stubs 18 */ 19 /** 20 * SECTION:value_collection 21 * @Short_description: Converting varargs to generic values 22 * @Title: Varargs Value Collection 23 * 24 * The macros in this section provide the varargs parsing support needed 25 * in variadic GObject functions such as g_object_new() or g_object_set(). 26 * 27 * They currently support the collection of integral types, floating point 28 * types and pointers. 29 */ 30 #ifndef __G_VALUE_COLLECTOR_H__ 31 #define __G_VALUE_COLLECTOR_H__ 32 33 #include <glib-object.h> 34 35 G_BEGIN_DECLS 36 37 /* we may want to add aggregate types here some day, if requested 38 * by users. the basic C types are covered already, everything 39 * smaller than an int is promoted to an integer and floats are 40 * always promoted to doubles for varargs call constructions. 41 */ 42 enum /*< skip >*/ 43 { 44 G_VALUE_COLLECT_INT = 'i', 45 G_VALUE_COLLECT_LONG = 'l', 46 G_VALUE_COLLECT_INT64 = 'q', 47 G_VALUE_COLLECT_DOUBLE = 'd', 48 G_VALUE_COLLECT_POINTER = 'p' 49 }; 50 51 52 /* vararg union holding actual values collected 53 */ 54 /** 55 * GTypeCValue: 56 * @v_int: the field for holding integer values 57 * @v_long: the field for holding long integer values 58 * @v_int64: the field for holding 64 bit integer values 59 * @v_double: the field for holding floating point values 60 * @v_pointer: the field for holding pointers 61 * 62 * A union holding one collected value. 63 */ 64 union _GTypeCValue 65 { 66 gint v_int; 67 glong v_long; 68 gint64 v_int64; 69 gdouble v_double; 70 gpointer v_pointer; 71 }; 72 73 /** 74 * G_VALUE_COLLECT_INIT: 75 * @value: a #GValue return location. @value must contain only 0 bytes. 76 * @_value_type: the #GType to use for @value. 77 * @var_args: the va_list variable; it may be evaluated multiple times 78 * @flags: flags which are passed on to the collect_value() function of 79 * the #GTypeValueTable of @value. 80 * @__error: a #gchar** variable that will be modified to hold a g_new() 81 * allocated error messages if something fails 82 * 83 * Collects a variable argument value from a `va_list`. 84 * 85 * We have to implement the varargs collection as a macro, because on some 86 * systems `va_list` variables cannot be passed by reference. 87 * 88 * Since: 2.24 89 */ 90 #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \ 91 G_STMT_START { \ 92 GValue *g_vci_val = (value); \ 93 guint g_vci_flags = (flags); \ 94 GTypeValueTable *g_vci_vtab = g_type_value_table_peek (_value_type); \ 95 const gchar *g_vci_collect_format = g_vci_vtab->collect_format; \ 96 GTypeCValue g_vci_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \ 97 guint g_vci_n_values = 0; \ 98 \ 99 g_vci_val->g_type = _value_type; /* value_meminit() from gvalue.c */ \ 100 while (*g_vci_collect_format) \ 101 { \ 102 GTypeCValue *g_vci_cvalue = g_vci_cvalues + g_vci_n_values++; \ 103 \ 104 switch (*g_vci_collect_format++) \ 105 { \ 106 case G_VALUE_COLLECT_INT: \ 107 g_vci_cvalue->v_int = va_arg ((var_args), gint); \ 108 break; \ 109 case G_VALUE_COLLECT_LONG: \ 110 g_vci_cvalue->v_long = va_arg ((var_args), glong); \ 111 break; \ 112 case G_VALUE_COLLECT_INT64: \ 113 g_vci_cvalue->v_int64 = va_arg ((var_args), gint64); \ 114 break; \ 115 case G_VALUE_COLLECT_DOUBLE: \ 116 g_vci_cvalue->v_double = va_arg ((var_args), gdouble); \ 117 break; \ 118 case G_VALUE_COLLECT_POINTER: \ 119 g_vci_cvalue->v_pointer = va_arg ((var_args), gpointer); \ 120 break; \ 121 default: \ 122 g_assert_not_reached (); \ 123 } \ 124 } \ 125 *(__error) = g_vci_vtab->collect_value (g_vci_val, \ 126 g_vci_n_values, \ 127 g_vci_cvalues, \ 128 g_vci_flags); \ 129 } G_STMT_END 130 131 /** 132 * G_VALUE_COLLECT: 133 * @value: a #GValue return location. @value is supposed to be initialized 134 * according to the value type to be collected 135 * @var_args: the va_list variable; it may be evaluated multiple times 136 * @flags: flags which are passed on to the collect_value() function of 137 * the #GTypeValueTable of @value. 138 * @__error: a #gchar** variable that will be modified to hold a g_new() 139 * allocated error messages if something fails 140 * 141 * Collects a variable argument value from a `va_list`. 142 * 143 * We have to implement the varargs collection as a macro, because on some systems 144 * `va_list` variables cannot be passed by reference. 145 * 146 * Note: If you are creating the @value argument just before calling this macro, 147 * you should use the #G_VALUE_COLLECT_INIT variant and pass the uninitialized 148 * #GValue. That variant is faster than #G_VALUE_COLLECT. 149 */ 150 #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \ 151 GValue *g_vc_value = (value); \ 152 GType g_vc_value_type = G_VALUE_TYPE (g_vc_value); \ 153 GTypeValueTable *g_vc_vtable = g_type_value_table_peek (g_vc_value_type); \ 154 \ 155 if (g_vc_vtable->value_free) \ 156 g_vc_vtable->value_free (g_vc_value); \ 157 memset (g_vc_value->data, 0, sizeof (g_vc_value->data)); \ 158 \ 159 G_VALUE_COLLECT_INIT(value, g_vc_value_type, var_args, flags, __error); \ 160 } G_STMT_END 161 162 /** 163 * G_VALUE_COLLECT_SKIP: 164 * @_value_type: the #GType of the value to skip 165 * @var_args: the va_list variable; it may be evaluated multiple times 166 * 167 * Skip an argument of type @_value_type from @var_args. 168 */ 169 #define G_VALUE_COLLECT_SKIP(_value_type, var_args) \ 170 G_STMT_START { \ 171 GTypeValueTable *g_vcs_vtable = g_type_value_table_peek (_value_type); \ 172 const gchar *g_vcs_collect_format = g_vcs_vtable->collect_format; \ 173 \ 174 while (*g_vcs_collect_format) \ 175 { \ 176 switch (*g_vcs_collect_format++) \ 177 { \ 178 case G_VALUE_COLLECT_INT: \ 179 va_arg ((var_args), gint); \ 180 break; \ 181 case G_VALUE_COLLECT_LONG: \ 182 va_arg ((var_args), glong); \ 183 break; \ 184 case G_VALUE_COLLECT_INT64: \ 185 va_arg ((var_args), gint64); \ 186 break; \ 187 case G_VALUE_COLLECT_DOUBLE: \ 188 va_arg ((var_args), gdouble); \ 189 break; \ 190 case G_VALUE_COLLECT_POINTER: \ 191 va_arg ((var_args), gpointer); \ 192 break; \ 193 default: \ 194 g_assert_not_reached (); \ 195 } \ 196 } \ 197 } G_STMT_END 198 199 /** 200 * G_VALUE_LCOPY: 201 * @value: a #GValue to store into the @var_args; this must be initialized 202 * and set 203 * @var_args: the va_list variable; it may be evaluated multiple times 204 * @flags: flags which are passed on to the lcopy_value() function of 205 * the #GTypeValueTable of @value. 206 * @__error: a #gchar** variable that will be modified to hold a g_new() 207 * allocated error message if something fails 208 * 209 * Stores a value’s value into one or more argument locations from a `va_list`. 210 * 211 * This is the inverse of G_VALUE_COLLECT(). 212 */ 213 #define G_VALUE_LCOPY(value, var_args, flags, __error) \ 214 G_STMT_START { \ 215 const GValue *g_vl_value = (value); \ 216 guint g_vl_flags = (flags); \ 217 GType g_vl_value_type = G_VALUE_TYPE (g_vl_value); \ 218 GTypeValueTable *g_vl_vtable = g_type_value_table_peek (g_vl_value_type); \ 219 const gchar *g_vl_lcopy_format = g_vl_vtable->lcopy_format; \ 220 GTypeCValue g_vl_cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \ 221 guint g_vl_n_values = 0; \ 222 \ 223 while (*g_vl_lcopy_format) \ 224 { \ 225 GTypeCValue *g_vl_cvalue = g_vl_cvalues + g_vl_n_values++; \ 226 \ 227 switch (*g_vl_lcopy_format++) \ 228 { \ 229 case G_VALUE_COLLECT_INT: \ 230 g_vl_cvalue->v_int = va_arg ((var_args), gint); \ 231 break; \ 232 case G_VALUE_COLLECT_LONG: \ 233 g_vl_cvalue->v_long = va_arg ((var_args), glong); \ 234 break; \ 235 case G_VALUE_COLLECT_INT64: \ 236 g_vl_cvalue->v_int64 = va_arg ((var_args), gint64); \ 237 break; \ 238 case G_VALUE_COLLECT_DOUBLE: \ 239 g_vl_cvalue->v_double = va_arg ((var_args), gdouble); \ 240 break; \ 241 case G_VALUE_COLLECT_POINTER: \ 242 g_vl_cvalue->v_pointer = va_arg ((var_args), gpointer); \ 243 break; \ 244 default: \ 245 g_assert_not_reached (); \ 246 } \ 247 } \ 248 *(__error) = g_vl_vtable->lcopy_value (g_vl_value, \ 249 g_vl_n_values, \ 250 g_vl_cvalues, \ 251 g_vl_flags); \ 252 } G_STMT_END 253 254 255 /** 256 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH: 257 * 258 * The maximal number of #GTypeCValues which can be collected for a 259 * single #GValue. 260 */ 261 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8) 262 263 G_END_DECLS 264 265 #endif /* __G_VALUE_COLLECTOR_H__ */ 266