Blame gobject/gvaluecollector.h

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * gvaluecollector.h: GValue varargs stubs
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * SECTION:value_collection
Packit ae235b
 * @Short_description: Converting varargs to generic values
Packit ae235b
 * @Title: Varargs Value Collection
Packit ae235b
 * 
Packit ae235b
 * The macros in this section provide the varargs parsing support needed
Packit ae235b
 * in variadic GObject functions such as g_object_new() or g_object_set().
Packit ae235b
 * They currently support the collection of integral types, floating point 
Packit ae235b
 * types and pointers.
Packit ae235b
 */
Packit ae235b
#ifndef __G_VALUE_COLLECTOR_H__
Packit ae235b
#define __G_VALUE_COLLECTOR_H__
Packit ae235b
Packit ae235b
#include <glib-object.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
/* we may want to add aggregate types here some day, if requested
Packit ae235b
 * by users. the basic C types are covered already, everything
Packit ae235b
 * smaller than an int is promoted to an integer and floats are
Packit ae235b
 * always promoted to doubles for varargs call constructions.
Packit ae235b
 */
Packit ae235b
enum	/*< skip >*/
Packit ae235b
{
Packit ae235b
  G_VALUE_COLLECT_INT		= 'i',
Packit ae235b
  G_VALUE_COLLECT_LONG		= 'l',
Packit ae235b
  G_VALUE_COLLECT_INT64         = 'q',
Packit ae235b
  G_VALUE_COLLECT_DOUBLE	= 'd',
Packit ae235b
  G_VALUE_COLLECT_POINTER	= 'p'
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
/* vararg union holding actual values collected
Packit ae235b
 */
Packit ae235b
/**
Packit ae235b
 * GTypeCValue:
Packit ae235b
 * @v_int: the field for holding integer values
Packit ae235b
 * @v_long: the field for holding long integer values
Packit ae235b
 * @v_int64: the field for holding 64 bit integer values
Packit ae235b
 * @v_double: the field for holding floating point values
Packit ae235b
 * @v_pointer: the field for holding pointers
Packit ae235b
 * 
Packit ae235b
 * A union holding one collected value.
Packit ae235b
 */
Packit ae235b
union _GTypeCValue
Packit ae235b
{
Packit ae235b
  gint     v_int;
Packit ae235b
  glong    v_long;
Packit ae235b
  gint64   v_int64;
Packit ae235b
  gdouble  v_double;
Packit ae235b
  gpointer v_pointer;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_VALUE_COLLECT_INIT:
Packit ae235b
 * @value: a #GValue return location. @value must contain only 0 bytes.
Packit ae235b
 * @_value_type: the #GType to use for @value.
Packit ae235b
 * @var_args: the va_list variable; it may be evaluated multiple times
Packit ae235b
 * @flags: flags which are passed on to the collect_value() function of
Packit ae235b
 *  the #GTypeValueTable of @value.
Packit ae235b
 * @__error: a #gchar** variable that will be modified to hold a g_new()
Packit ae235b
 *  allocated error messages if something fails
Packit ae235b
 * 
Packit ae235b
 * Collects a variable argument value from a va_list. We have to
Packit ae235b
 * implement the varargs collection as a macro, because on some systems
Packit ae235b
 * va_list variables cannot be passed by reference.
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 */
Packit ae235b
#define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error)		\
Packit ae235b
G_STMT_START {										\
Packit ae235b
  GValue *_val = (value);								\
Packit ae235b
  guint _flags = (flags);								\
Packit ae235b
  GTypeValueTable *_vtab = g_type_value_table_peek (_value_type);			\
Packit ae235b
  const gchar *_collect_format = _vtab->collect_format;					\
Packit ae235b
  GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };		\
Packit ae235b
  guint _n_values = 0;									\
Packit ae235b
                                                                                        \
Packit ae235b
  _val->g_type = _value_type;		/* value_meminit() from gvalue.c */		\
Packit ae235b
  while (*_collect_format)								\
Packit ae235b
    {											\
Packit ae235b
      GTypeCValue *_cvalue = _cvalues + _n_values++;					\
Packit ae235b
                                                                                        \
Packit ae235b
      switch (*_collect_format++)							\
Packit ae235b
	{										\
Packit ae235b
	case G_VALUE_COLLECT_INT:							\
Packit ae235b
	  _cvalue->v_int = va_arg ((var_args), gint);					\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_LONG:							\
Packit ae235b
	  _cvalue->v_long = va_arg ((var_args), glong);					\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_INT64:							\
Packit ae235b
	  _cvalue->v_int64 = va_arg ((var_args), gint64);				\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_DOUBLE:							\
Packit ae235b
	  _cvalue->v_double = va_arg ((var_args), gdouble);				\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_POINTER:							\
Packit ae235b
	  _cvalue->v_pointer = va_arg ((var_args), gpointer);				\
Packit ae235b
	  break;									\
Packit ae235b
	default:									\
Packit ae235b
	  g_assert_not_reached ();							\
Packit ae235b
	}										\
Packit ae235b
    }											\
Packit ae235b
  *(__error) = _vtab->collect_value (_val,						\
Packit ae235b
				       _n_values,					\
Packit ae235b
				       _cvalues,					\
Packit ae235b
				       _flags);						\
Packit ae235b
} G_STMT_END
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_VALUE_COLLECT:
Packit ae235b
 * @value: a #GValue return location. @value is supposed to be initialized
Packit ae235b
 *  according to the value type to be collected
Packit ae235b
 * @var_args: the va_list variable; it may be evaluated multiple times
Packit ae235b
 * @flags: flags which are passed on to the collect_value() function of
Packit ae235b
 *  the #GTypeValueTable of @value.
Packit ae235b
 * @__error: a #gchar** variable that will be modified to hold a g_new()
Packit ae235b
 *  allocated error messages if something fails
Packit ae235b
 *
Packit ae235b
 * Collects a variable argument value from a va_list. We have to
Packit ae235b
 * implement the varargs collection as a macro, because on some systems
Packit ae235b
 * va_list variables cannot be passed by reference.
Packit ae235b
 *
Packit ae235b
 * Note: If you are creating the @value argument just before calling this macro,
Packit ae235b
 * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
Packit ae235b
 * #GValue. That variant is faster than #G_VALUE_COLLECT.
Packit ae235b
 */
Packit ae235b
#define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START {			\
Packit ae235b
  GValue *_value = (value);								\
Packit ae235b
  GType _value_type = G_VALUE_TYPE (_value);						\
Packit ae235b
  GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);			\
Packit ae235b
											\
Packit ae235b
  if (_vtable->value_free)								\
Packit ae235b
    _vtable->value_free (_value);							\
Packit ae235b
  memset (_value->data, 0, sizeof (_value->data));					\
Packit ae235b
											\
Packit ae235b
  G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error);			\
Packit ae235b
} G_STMT_END
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_VALUE_COLLECT_SKIP:
Packit ae235b
 * @_value_type: the #GType of the value to skip
Packit ae235b
 * @var_args: the va_list variable; it may be evaluated multiple times
Packit ae235b
 *
Packit ae235b
 * Skip an argument of type @_value_type from @var_args.
Packit ae235b
 */
Packit ae235b
#define G_VALUE_COLLECT_SKIP(_value_type, var_args)					\
Packit ae235b
G_STMT_START {										\
Packit ae235b
  GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);			\
Packit ae235b
  const gchar *_collect_format = _vtable->collect_format;				\
Packit ae235b
                                                                                        \
Packit ae235b
  while (*_collect_format)								\
Packit ae235b
    {											\
Packit ae235b
      switch (*_collect_format++)							\
Packit ae235b
	{										\
Packit ae235b
	case G_VALUE_COLLECT_INT:							\
Packit ae235b
	  va_arg ((var_args), gint);							\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_LONG:							\
Packit ae235b
	  va_arg ((var_args), glong);							\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_INT64:							\
Packit ae235b
	  va_arg ((var_args), gint64);							\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_DOUBLE:							\
Packit ae235b
	  va_arg ((var_args), gdouble);							\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_POINTER:							\
Packit ae235b
	  va_arg ((var_args), gpointer);						\
Packit ae235b
	  break;									\
Packit ae235b
	default:									\
Packit ae235b
	  g_assert_not_reached ();							\
Packit ae235b
	}										\
Packit ae235b
    }											\
Packit ae235b
} G_STMT_END
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_VALUE_LCOPY:
Packit ae235b
 * @value: a #GValue return location. @value is supposed to be initialized 
Packit ae235b
 *  according to the value type to be collected
Packit ae235b
 * @var_args: the va_list variable; it may be evaluated multiple times
Packit ae235b
 * @flags: flags which are passed on to the lcopy_value() function of
Packit ae235b
 *  the #GTypeValueTable of @value.
Packit ae235b
 * @__error: a #gchar** variable that will be modified to hold a g_new()
Packit ae235b
 *  allocated error messages if something fails
Packit ae235b
 * 
Packit ae235b
 * Collects a value's variable argument locations from a va_list. Usage is
Packit ae235b
 * analogous to G_VALUE_COLLECT().
Packit ae235b
 */
Packit ae235b
#define G_VALUE_LCOPY(value, var_args, flags, __error)					\
Packit ae235b
G_STMT_START {										\
Packit ae235b
  const GValue *_value = (value);							\
Packit ae235b
  guint _flags = (flags);								\
Packit ae235b
  GType _value_type = G_VALUE_TYPE (_value);						\
Packit ae235b
  GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);			\
Packit ae235b
  const gchar *_lcopy_format = _vtable->lcopy_format;					\
Packit ae235b
  GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };		\
Packit ae235b
  guint _n_values = 0;									\
Packit ae235b
                                                                                        \
Packit ae235b
  while (*_lcopy_format)								\
Packit ae235b
    {											\
Packit ae235b
      GTypeCValue *_cvalue = _cvalues + _n_values++;					\
Packit ae235b
                                                                                        \
Packit ae235b
      switch (*_lcopy_format++)								\
Packit ae235b
	{										\
Packit ae235b
	case G_VALUE_COLLECT_INT:							\
Packit ae235b
	  _cvalue->v_int = va_arg ((var_args), gint);					\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_LONG:							\
Packit ae235b
	  _cvalue->v_long = va_arg ((var_args), glong);					\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_INT64:							\
Packit ae235b
	  _cvalue->v_int64 = va_arg ((var_args), gint64);				\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_DOUBLE:							\
Packit ae235b
	  _cvalue->v_double = va_arg ((var_args), gdouble);				\
Packit ae235b
	  break;									\
Packit ae235b
	case G_VALUE_COLLECT_POINTER:							\
Packit ae235b
	  _cvalue->v_pointer = va_arg ((var_args), gpointer);				\
Packit ae235b
	  break;									\
Packit ae235b
	default:									\
Packit ae235b
	  g_assert_not_reached ();							\
Packit ae235b
	}										\
Packit ae235b
    }											\
Packit ae235b
  *(__error) = _vtable->lcopy_value (_value,						\
Packit ae235b
				     _n_values,						\
Packit ae235b
				     _cvalues,						\
Packit ae235b
				     _flags);						\
Packit ae235b
} G_STMT_END
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
Packit ae235b
 * 
Packit ae235b
 * The maximal number of #GTypeCValues which can be collected for a 
Packit ae235b
 * single #GValue.
Packit ae235b
 */
Packit ae235b
#define	G_VALUE_COLLECT_FORMAT_MAX_LENGTH	(8)
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __G_VALUE_COLLECTOR_H__ */