You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

228 lines
8.2 KiB

  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. #include <glib-object.h>
  35. G_BEGIN_DECLS
  36. /* we may want to add aggregate types here some day, if requested
  37. * by users. the basic C types are covered already, everything
  38. * smaller than an int is promoted to an integer and floats are
  39. * always promoted to doubles for varargs call constructions.
  40. */
  41. enum /*< skip >*/
  42. {
  43. G_VALUE_COLLECT_INT = 'i',
  44. G_VALUE_COLLECT_LONG = 'l',
  45. G_VALUE_COLLECT_INT64 = 'q',
  46. G_VALUE_COLLECT_DOUBLE = 'd',
  47. G_VALUE_COLLECT_POINTER = 'p'
  48. };
  49. /* vararg union holding actual values collected
  50. */
  51. /**
  52. * GTypeCValue:
  53. * @v_int: the field for holding integer values
  54. * @v_long: the field for holding long integer values
  55. * @v_int64: the field for holding 64 bit integer values
  56. * @v_double: the field for holding floating point values
  57. * @v_pointer: the field for holding pointers
  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. * G_VALUE_COLLECT_INIT:
  71. * @value: a #GValue return location. @value must contain only 0 bytes.
  72. * @_value_type: the #GType to use for @value.
  73. * @var_args: the va_list variable; it may be evaluated multiple times
  74. * @flags: flags which are passed on to the collect_value() function of
  75. * the #GTypeValueTable of @value.
  76. * @__error: a #gchar** variable that will be modified to hold a g_new()
  77. * allocated error messages if something fails
  78. *
  79. * Collects a variable argument value from a va_list. We have to
  80. * implement the varargs collection as a macro, because on some systems
  81. * va_list variables cannot be passed by reference.
  82. *
  83. * Since: 2.24
  84. */
  85. #define G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error) \
  86. G_STMT_START { \
  87. GValue *_val = (value); \
  88. guint _flags = (flags); \
  89. GTypeValueTable *_vtab = g_type_value_table_peek (_value_type); \
  90. gchar *_collect_format = _vtab->collect_format; \
  91. GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
  92. guint _n_values = 0; \
  93. \
  94. _val->g_type = _value_type; /* value_meminit() from gvalue.c */ \
  95. while (*_collect_format) \
  96. { \
  97. GTypeCValue *_cvalue = _cvalues + _n_values++; \
  98. \
  99. switch (*_collect_format++) \
  100. { \
  101. case G_VALUE_COLLECT_INT: \
  102. _cvalue->v_int = va_arg ((var_args), gint); \
  103. break; \
  104. case G_VALUE_COLLECT_LONG: \
  105. _cvalue->v_long = va_arg ((var_args), glong); \
  106. break; \
  107. case G_VALUE_COLLECT_INT64: \
  108. _cvalue->v_int64 = va_arg ((var_args), gint64); \
  109. break; \
  110. case G_VALUE_COLLECT_DOUBLE: \
  111. _cvalue->v_double = va_arg ((var_args), gdouble); \
  112. break; \
  113. case G_VALUE_COLLECT_POINTER: \
  114. _cvalue->v_pointer = va_arg ((var_args), gpointer); \
  115. break; \
  116. default: \
  117. g_assert_not_reached (); \
  118. } \
  119. } \
  120. *(__error) = _vtab->collect_value (_val, \
  121. _n_values, \
  122. _cvalues, \
  123. _flags); \
  124. } G_STMT_END
  125. /**
  126. * G_VALUE_COLLECT:
  127. * @value: a #GValue return location. @value is supposed to be initialized
  128. * according to the value type to be collected
  129. * @var_args: the va_list variable; it may be evaluated multiple times
  130. * @flags: flags which are passed on to the collect_value() function of
  131. * the #GTypeValueTable of @value.
  132. * @__error: a #gchar** variable that will be modified to hold a g_new()
  133. * allocated error messages if something fails
  134. *
  135. * Collects a variable argument value from a va_list. We have to
  136. * implement the varargs collection as a macro, because on some systems
  137. * va_list variables cannot be passed by reference.
  138. *
  139. * Note: If you are creating the @value argument just before calling this macro,
  140. * you should use the #G_VALUE_COLLECT_INIT variant and pass the unitialized
  141. * #GValue. That variant is faster than #G_VALUE_COLLECT.
  142. */
  143. #define G_VALUE_COLLECT(value, var_args, flags, __error) G_STMT_START { \
  144. GValue *_value = (value); \
  145. GType _value_type = G_VALUE_TYPE (_value); \
  146. GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
  147. \
  148. if (_vtable->value_free) \
  149. _vtable->value_free (_value); \
  150. memset (_value->data, 0, sizeof (_value->data)); \
  151. \
  152. G_VALUE_COLLECT_INIT(value, _value_type, var_args, flags, __error); \
  153. } G_STMT_END
  154. /**
  155. * G_VALUE_LCOPY:
  156. * @value: a #GValue return location. @value is supposed to be initialized
  157. * according to the value type to be collected
  158. * @var_args: the va_list variable; it may be evaluated multiple times
  159. * @flags: flags which are passed on to the lcopy_value() function of
  160. * the #GTypeValueTable of @value.
  161. * @__error: a #gchar** variable that will be modified to hold a g_new()
  162. * allocated error messages if something fails
  163. *
  164. * Collects a value's variable argument locations from a va_list. Usage is
  165. * analogous to G_VALUE_COLLECT().
  166. */
  167. #define G_VALUE_LCOPY(value, var_args, flags, __error) \
  168. G_STMT_START { \
  169. const GValue *_value = (value); \
  170. guint _flags = (flags); \
  171. GType _value_type = G_VALUE_TYPE (_value); \
  172. GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
  173. gchar *_lcopy_format = _vtable->lcopy_format; \
  174. GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
  175. guint _n_values = 0; \
  176. \
  177. while (*_lcopy_format) \
  178. { \
  179. GTypeCValue *_cvalue = _cvalues + _n_values++; \
  180. \
  181. switch (*_lcopy_format++) \
  182. { \
  183. case G_VALUE_COLLECT_INT: \
  184. _cvalue->v_int = va_arg ((var_args), gint); \
  185. break; \
  186. case G_VALUE_COLLECT_LONG: \
  187. _cvalue->v_long = va_arg ((var_args), glong); \
  188. break; \
  189. case G_VALUE_COLLECT_INT64: \
  190. _cvalue->v_int64 = va_arg ((var_args), gint64); \
  191. break; \
  192. case G_VALUE_COLLECT_DOUBLE: \
  193. _cvalue->v_double = va_arg ((var_args), gdouble); \
  194. break; \
  195. case G_VALUE_COLLECT_POINTER: \
  196. _cvalue->v_pointer = va_arg ((var_args), gpointer); \
  197. break; \
  198. default: \
  199. g_assert_not_reached (); \
  200. } \
  201. } \
  202. *(__error) = _vtable->lcopy_value (_value, \
  203. _n_values, \
  204. _cvalues, \
  205. _flags); \
  206. } G_STMT_END
  207. /**
  208. * G_VALUE_COLLECT_FORMAT_MAX_LENGTH:
  209. *
  210. * The maximal number of #GTypeCValue<!-- -->s which can be collected for a
  211. * single #GValue.
  212. */
  213. #define G_VALUE_COLLECT_FORMAT_MAX_LENGTH (8)
  214. G_END_DECLS
  215. #endif /* __G_VALUE_COLLECTOR_H__ */