gvaluecollector.h

Go to the documentation of this file.
00001 /* GObject - GLib Type, Object, Parameter and Signal Library
00002  * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General
00015  * Public License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  *
00019  * gvaluecollector.h: GValue varargs stubs
00020  */
00021 #ifndef __G_VALUE_COLLECTOR_H__
00022 #define __G_VALUE_COLLECTOR_H__
00023 
00024 #include <glib-object.h>
00025 
00026 G_BEGIN_DECLS
00027 
00028 /* we may want to add aggregate types here some day, if requested
00029  * by users. the basic C types are covered already, everything
00030  * smaller than an int is promoted to an integer and floats are
00031  * always promoted to doubles for varargs call constructions.
00032  */
00033 enum    /*< skip >*/
00034 {
00035   G_VALUE_COLLECT_INT           = 'i',
00036   G_VALUE_COLLECT_LONG          = 'l',
00037   G_VALUE_COLLECT_INT64         = 'q',
00038   G_VALUE_COLLECT_DOUBLE        = 'd',
00039   G_VALUE_COLLECT_POINTER       = 'p'
00040 };
00041 
00042 
00043 /* vararg union holding actuall values collected
00044  */
00045 union _GTypeCValue
00046 {
00047   gint     v_int;
00048   glong    v_long;
00049   gint64   v_int64;
00050   gdouble  v_double;
00051   gpointer v_pointer;
00052 };
00053 
00054 
00055 /* G_VALUE_COLLECT() collects a variable argument value
00056  * from a va_list. we have to implement the varargs collection as a
00057  * macro, because on some systems va_list variables cannot be passed
00058  * by reference.
00059  * value is supposed to be initialized according to the value
00060  * type to be collected.
00061  * var_args is the va_list variable and may be evaluated multiple times.
00062  * __error is a gchar** variable that will be modified to hold a g_new()
00063  * allocated error messages if something fails.
00064  */
00065 #define G_VALUE_COLLECT(value, var_args, flags, __error)                                \
00066 G_STMT_START {                                                                          \
00067   GValue *_value = (value);                                                             \
00068   guint _flags = (flags);                                                               \
00069   GType _value_type = G_VALUE_TYPE (_value);                                            \
00070   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
00071   gchar *_collect_format = _vtable->collect_format;                                     \
00072   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
00073   guint _n_values = 0;                                                                  \
00074                                                                                         \
00075   if (_vtable->value_free)                                                              \
00076     _vtable->value_free (_value);                                                       \
00077   _value->g_type = _value_type;         /* value_meminit() from gvalue.c */             \
00078   memset (_value->data, 0, sizeof (_value->data));                                      \
00079   while (*_collect_format)                                                              \
00080     {                                                                                   \
00081       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
00082                                                                                         \
00083       switch (*_collect_format++)                                                       \
00084         {                                                                               \
00085         case G_VALUE_COLLECT_INT:                                                       \
00086           _cvalue->v_int = va_arg ((var_args), gint);                                   \
00087           break;                                                                        \
00088         case G_VALUE_COLLECT_LONG:                                                      \
00089           _cvalue->v_long = va_arg ((var_args), glong);                                 \
00090           break;                                                                        \
00091         case G_VALUE_COLLECT_INT64:                                                     \
00092           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
00093           break;                                                                        \
00094         case G_VALUE_COLLECT_DOUBLE:                                                    \
00095           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
00096           break;                                                                        \
00097         case G_VALUE_COLLECT_POINTER:                                                   \
00098           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
00099           break;                                                                        \
00100         default:                                                                        \
00101           g_assert_not_reached ();                                                      \
00102         }                                                                               \
00103     }                                                                                   \
00104   *(__error) = _vtable->collect_value (_value,                                          \
00105                                        _n_values,                                       \
00106                                        _cvalues,                                        \
00107                                        _flags);                                         \
00108 } G_STMT_END
00109 
00110 
00111 /* G_VALUE_LCOPY() collects a value's variable argument
00112  * locations from a va_list. usage is analogous to G_VALUE_COLLECT().
00113  */
00114 #define G_VALUE_LCOPY(value, var_args, flags, __error)                                  \
00115 G_STMT_START {                                                                          \
00116   const GValue *_value = (value);                                                       \
00117   guint _flags = (flags);                                                               \
00118   GType _value_type = G_VALUE_TYPE (_value);                                            \
00119   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);                     \
00120   gchar *_lcopy_format = _vtable->lcopy_format;                                         \
00121   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };                \
00122   guint _n_values = 0;                                                                  \
00123                                                                                         \
00124   while (*_lcopy_format)                                                                \
00125     {                                                                                   \
00126       GTypeCValue *_cvalue = _cvalues + _n_values++;                                    \
00127                                                                                         \
00128       switch (*_lcopy_format++)                                                         \
00129         {                                                                               \
00130         case G_VALUE_COLLECT_INT:                                                       \
00131           _cvalue->v_int = va_arg ((var_args), gint);                                   \
00132           break;                                                                        \
00133         case G_VALUE_COLLECT_LONG:                                                      \
00134           _cvalue->v_long = va_arg ((var_args), glong);                                 \
00135           break;                                                                        \
00136         case G_VALUE_COLLECT_INT64:                                                     \
00137           _cvalue->v_int64 = va_arg ((var_args), gint64);                               \
00138           break;                                                                        \
00139         case G_VALUE_COLLECT_DOUBLE:                                                    \
00140           _cvalue->v_double = va_arg ((var_args), gdouble);                             \
00141           break;                                                                        \
00142         case G_VALUE_COLLECT_POINTER:                                                   \
00143           _cvalue->v_pointer = va_arg ((var_args), gpointer);                           \
00144           break;                                                                        \
00145         default:                                                                        \
00146           g_assert_not_reached ();                                                      \
00147         }                                                                               \
00148     }                                                                                   \
00149   *(__error) = _vtable->lcopy_value (_value,                                            \
00150                                      _n_values,                                         \
00151                                      _cvalues,                                          \
00152                                      _flags);                                           \
00153 } G_STMT_END
00154 
00155 
00156 #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH       (8)
00157 
00158 G_END_DECLS
00159 
00160 #endif /* __G_VALUE_COLLECTOR_H__ */

Copyright © Nokia Corporation 2001-2008
Back to top