gvaluecollector.h 9.1 KB

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