genums.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* GObject - GLib Type, Object, Parameter and Signal Library
  2. * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General
  17. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __G_ENUMS_H__
  20. #define __G_ENUMS_H__
  21. #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
  22. #error "Only <glib-object.h> can be included directly."
  23. #endif
  24. #include <gobject/gtype.h>
  25. G_BEGIN_DECLS
  26. /* --- type macros --- */
  27. /**
  28. * G_TYPE_IS_ENUM:
  29. * @type: a #GType ID.
  30. *
  31. * Checks whether @type "is a" %G_TYPE_ENUM.
  32. *
  33. * Returns: %TRUE if @type "is a" %G_TYPE_ENUM.
  34. */
  35. #define G_TYPE_IS_ENUM(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_ENUM)
  36. /**
  37. * G_ENUM_CLASS:
  38. * @class: a valid #GEnumClass
  39. *
  40. * Casts a derived #GEnumClass structure into a #GEnumClass structure.
  41. */
  42. #define G_ENUM_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_ENUM, GEnumClass))
  43. /**
  44. * G_IS_ENUM_CLASS:
  45. * @class: a #GEnumClass
  46. *
  47. * Checks whether @class "is a" valid #GEnumClass structure of type %G_TYPE_ENUM
  48. * or derived.
  49. */
  50. #define G_IS_ENUM_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_ENUM))
  51. /**
  52. * G_ENUM_CLASS_TYPE:
  53. * @class: a #GEnumClass
  54. *
  55. * Get the type identifier from a given #GEnumClass structure.
  56. *
  57. * Returns: the #GType
  58. */
  59. #define G_ENUM_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
  60. /**
  61. * G_ENUM_CLASS_TYPE_NAME:
  62. * @class: a #GEnumClass
  63. *
  64. * Get the static type name from a given #GEnumClass structure.
  65. *
  66. * Returns: the type name.
  67. */
  68. #define G_ENUM_CLASS_TYPE_NAME(class) (g_type_name (G_ENUM_CLASS_TYPE (class)))
  69. /**
  70. * G_TYPE_IS_FLAGS:
  71. * @type: a #GType ID.
  72. *
  73. * Checks whether @type "is a" %G_TYPE_FLAGS.
  74. *
  75. * Returns: %TRUE if @type "is a" %G_TYPE_FLAGS.
  76. */
  77. #define G_TYPE_IS_FLAGS(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_FLAGS)
  78. /**
  79. * G_FLAGS_CLASS:
  80. * @class: a valid #GFlagsClass
  81. *
  82. * Casts a derived #GFlagsClass structure into a #GFlagsClass structure.
  83. */
  84. #define G_FLAGS_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_FLAGS, GFlagsClass))
  85. /**
  86. * G_IS_FLAGS_CLASS:
  87. * @class: a #GFlagsClass
  88. *
  89. * Checks whether @class "is a" valid #GFlagsClass structure of type %G_TYPE_FLAGS
  90. * or derived.
  91. */
  92. #define G_IS_FLAGS_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_FLAGS))
  93. /**
  94. * G_FLAGS_CLASS_TYPE:
  95. * @class: a #GFlagsClass
  96. *
  97. * Get the type identifier from a given #GFlagsClass structure.
  98. *
  99. * Returns: the #GType
  100. */
  101. #define G_FLAGS_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
  102. /**
  103. * G_FLAGS_CLASS_TYPE_NAME:
  104. * @class: a #GFlagsClass
  105. *
  106. * Get the static type name from a given #GFlagsClass structure.
  107. *
  108. * Returns: the type name.
  109. */
  110. #define G_FLAGS_CLASS_TYPE_NAME(class) (g_type_name (G_FLAGS_CLASS_TYPE (class)))
  111. /**
  112. * G_VALUE_HOLDS_ENUM:
  113. * @value: a valid #GValue structure
  114. *
  115. * Checks whether the given #GValue can hold values derived from type %G_TYPE_ENUM.
  116. *
  117. * Returns: %TRUE on success.
  118. */
  119. #define G_VALUE_HOLDS_ENUM(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_ENUM))
  120. /**
  121. * G_VALUE_HOLDS_FLAGS:
  122. * @value: a valid #GValue structure
  123. *
  124. * Checks whether the given #GValue can hold values derived from type %G_TYPE_FLAGS.
  125. *
  126. * Returns: %TRUE on success.
  127. */
  128. #define G_VALUE_HOLDS_FLAGS(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_FLAGS))
  129. /* --- enum/flag values & classes --- */
  130. typedef struct _GEnumClass GEnumClass;
  131. typedef struct _GFlagsClass GFlagsClass;
  132. typedef struct _GEnumValue GEnumValue;
  133. typedef struct _GFlagsValue GFlagsValue;
  134. /**
  135. * GEnumClass:
  136. * @g_type_class: the parent class
  137. * @minimum: the smallest possible value.
  138. * @maximum: the largest possible value.
  139. * @n_values: the number of possible values.
  140. * @values: an array of #GEnumValue structs describing the
  141. * individual values.
  142. *
  143. * The class of an enumeration type holds information about its
  144. * possible values.
  145. */
  146. struct _GEnumClass
  147. {
  148. GTypeClass g_type_class;
  149. /*< public >*/
  150. gint minimum;
  151. gint maximum;
  152. guint n_values;
  153. GEnumValue *values;
  154. };
  155. /**
  156. * GFlagsClass:
  157. * @g_type_class: the parent class
  158. * @mask: a mask covering all possible values.
  159. * @n_values: the number of possible values.
  160. * @values: an array of #GFlagsValue structs describing the
  161. * individual values.
  162. *
  163. * The class of a flags type holds information about its
  164. * possible values.
  165. */
  166. struct _GFlagsClass
  167. {
  168. GTypeClass g_type_class;
  169. /*< public >*/
  170. guint mask;
  171. guint n_values;
  172. GFlagsValue *values;
  173. };
  174. /**
  175. * GEnumValue:
  176. * @value: the enum value
  177. * @value_name: the name of the value
  178. * @value_nick: the nickname of the value
  179. *
  180. * A structure which contains a single enum value, its name, and its
  181. * nickname.
  182. */
  183. struct _GEnumValue
  184. {
  185. gint value;
  186. const gchar *value_name;
  187. const gchar *value_nick;
  188. };
  189. /**
  190. * GFlagsValue:
  191. * @value: the flags value
  192. * @value_name: the name of the value
  193. * @value_nick: the nickname of the value
  194. *
  195. * A structure which contains a single flags value, its name, and its
  196. * nickname.
  197. */
  198. struct _GFlagsValue
  199. {
  200. guint value;
  201. const gchar *value_name;
  202. const gchar *value_nick;
  203. };
  204. /* --- prototypes --- */
  205. GOBJECT_AVAILABLE_IN_ALL
  206. GEnumValue* g_enum_get_value (GEnumClass *enum_class,
  207. gint value);
  208. GOBJECT_AVAILABLE_IN_ALL
  209. GEnumValue* g_enum_get_value_by_name (GEnumClass *enum_class,
  210. const gchar *name);
  211. GOBJECT_AVAILABLE_IN_ALL
  212. GEnumValue* g_enum_get_value_by_nick (GEnumClass *enum_class,
  213. const gchar *nick);
  214. GOBJECT_AVAILABLE_IN_ALL
  215. GFlagsValue* g_flags_get_first_value (GFlagsClass *flags_class,
  216. guint value);
  217. GOBJECT_AVAILABLE_IN_ALL
  218. GFlagsValue* g_flags_get_value_by_name (GFlagsClass *flags_class,
  219. const gchar *name);
  220. GOBJECT_AVAILABLE_IN_ALL
  221. GFlagsValue* g_flags_get_value_by_nick (GFlagsClass *flags_class,
  222. const gchar *nick);
  223. GOBJECT_AVAILABLE_IN_2_54
  224. gchar *g_enum_to_string (GType g_enum_type,
  225. gint value);
  226. GOBJECT_AVAILABLE_IN_2_54
  227. gchar *g_flags_to_string (GType flags_type,
  228. guint value);
  229. GOBJECT_AVAILABLE_IN_ALL
  230. void g_value_set_enum (GValue *value,
  231. gint v_enum);
  232. GOBJECT_AVAILABLE_IN_ALL
  233. gint g_value_get_enum (const GValue *value);
  234. GOBJECT_AVAILABLE_IN_ALL
  235. void g_value_set_flags (GValue *value,
  236. guint v_flags);
  237. GOBJECT_AVAILABLE_IN_ALL
  238. guint g_value_get_flags (const GValue *value);
  239. /* --- registration functions --- */
  240. /* const_static_values is a NULL terminated array of enum/flags
  241. * values that is taken over!
  242. */
  243. GOBJECT_AVAILABLE_IN_ALL
  244. GType g_enum_register_static (const gchar *name,
  245. const GEnumValue *const_static_values);
  246. GOBJECT_AVAILABLE_IN_ALL
  247. GType g_flags_register_static (const gchar *name,
  248. const GFlagsValue *const_static_values);
  249. /* functions to complete the type information
  250. * for enums/flags implemented by plugins
  251. */
  252. GOBJECT_AVAILABLE_IN_ALL
  253. void g_enum_complete_type_info (GType g_enum_type,
  254. GTypeInfo *info,
  255. const GEnumValue *const_values);
  256. GOBJECT_AVAILABLE_IN_ALL
  257. void g_flags_complete_type_info (GType g_flags_type,
  258. GTypeInfo *info,
  259. const GFlagsValue *const_values);
  260. /* {{{ Macros */
  261. /**
  262. * G_DEFINE_ENUM_VALUE:
  263. * @EnumValue: an enumeration value
  264. * @EnumNick: a short string representing the enumeration value
  265. *
  266. * Defines an enumeration value, and maps it to a "nickname".
  267. *
  268. * This macro can only be used with G_DEFINE_ENUM_TYPE() and
  269. * G_DEFINE_FLAGS_TYPE().
  270. *
  271. * Since: 2.74
  272. */
  273. #define G_DEFINE_ENUM_VALUE(EnumValue, EnumNick) \
  274. { EnumValue, #EnumValue, EnumNick } \
  275. GOBJECT_AVAILABLE_MACRO_IN_2_74
  276. /**
  277. * G_DEFINE_ENUM_TYPE:
  278. * @TypeName: the enumeration type, in `CamelCase`
  279. * @type_name: the enumeration type prefixed, in `snake_case`
  280. * @...: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
  281. *
  282. * A convenience macro for defining enumeration types.
  283. *
  284. * This macro will generate a `*_get_type()` function for the
  285. * given @TypeName, using @type_name as the function prefix.
  286. *
  287. * |[<!-- language="C" -->
  288. * G_DEFINE_ENUM_TYPE (GtkOrientation, gtk_orientation,
  289. * G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_HORIZONTAL, "horizontal"),
  290. * G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_VERTICAL, "vertical"))
  291. * ]|
  292. *
  293. * For projects that have multiple enumeration types, or enumeration
  294. * types with many values, you should consider using glib-mkenums to
  295. * generate the type function.
  296. *
  297. * Since: 2.74
  298. */
  299. #define G_DEFINE_ENUM_TYPE(TypeName, type_name, ...) \
  300. GType \
  301. type_name ## _get_type (void) { \
  302. static _g_type_once_init_type g_define_type__static = 0; \
  303. if (_g_type_once_init_enter (&g_define_type__static)) { \
  304. static const GEnumValue enum_values[] = { \
  305. __VA_ARGS__ , \
  306. { 0, NULL, NULL }, \
  307. }; \
  308. GType g_define_type = g_enum_register_static (g_intern_static_string (#TypeName), enum_values); \
  309. _g_type_once_init_leave (&g_define_type__static, g_define_type); \
  310. } \
  311. return g_define_type__static; \
  312. } \
  313. GOBJECT_AVAILABLE_MACRO_IN_2_74
  314. /**
  315. * G_DEFINE_FLAGS_TYPE:
  316. * @TypeName: the enumeration type, in `CamelCase`
  317. * @type_name: the enumeration type prefixed, in `snake_case`
  318. * @...: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
  319. *
  320. * A convenience macro for defining flag types.
  321. *
  322. * This macro will generate a `*_get_type()` function for the
  323. * given @TypeName, using @type_name as the function prefix.
  324. *
  325. * |[<!-- language="C" -->
  326. * G_DEFINE_FLAGS_TYPE (GSettingsBindFlags, g_settings_bind_flags,
  327. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_DEFAULT, "default"),
  328. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET, "get"),
  329. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_SET, "set"),
  330. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_NO_SENSITIVITY, "no-sensitivity"),
  331. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET_NO_CHANGES, "get-no-changes"),
  332. * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_INVERT_BOOLEAN, "invert-boolean"))
  333. * ]|
  334. *
  335. * For projects that have multiple enumeration types, or enumeration
  336. * types with many values, you should consider using glib-mkenums to
  337. * generate the type function.
  338. *
  339. * Since: 2.74
  340. */
  341. #define G_DEFINE_FLAGS_TYPE(TypeName, type_name, ...) \
  342. GType \
  343. type_name ## _get_type (void) { \
  344. static _g_type_once_init_type g_define_type__static = 0; \
  345. if (_g_type_once_init_enter (&g_define_type__static)) { \
  346. static const GFlagsValue flags_values[] = { \
  347. __VA_ARGS__ , \
  348. { 0, NULL, NULL }, \
  349. }; \
  350. GType g_define_type = g_flags_register_static (g_intern_static_string (#TypeName), flags_values); \
  351. _g_type_once_init_leave (&g_define_type__static, g_define_type); \
  352. } \
  353. return g_define_type__static; \
  354. } \
  355. GOBJECT_AVAILABLE_MACRO_IN_2_74
  356. G_END_DECLS
  357. #endif /* __G_ENUMS_H__ */