1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
22 #if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
23 #error "Only <glib-object.h> can be included directly."
26 #include <gobject/gtype.h>
30 /* --- type macros --- */
35 * Checks whether @type "is a" %G_TYPE_ENUM.
37 * Returns: %TRUE if @type "is a" %G_TYPE_ENUM.
39 #define G_TYPE_IS_ENUM(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_ENUM)
42 * @class: a valid #GEnumClass
44 * Casts a derived #GEnumClass structure into a #GEnumClass structure.
46 #define G_ENUM_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_ENUM, GEnumClass))
49 * @class: a #GEnumClass
51 * Checks whether @class "is a" valid #GEnumClass structure of type %G_TYPE_ENUM
54 #define G_IS_ENUM_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_ENUM))
57 * @class: a #GEnumClass
59 * Get the type identifier from a given #GEnumClass structure.
63 #define G_ENUM_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
65 * G_ENUM_CLASS_TYPE_NAME:
66 * @class: a #GEnumClass
68 * Get the static type name from a given #GEnumClass structure.
70 * Returns: the type name.
72 #define G_ENUM_CLASS_TYPE_NAME(class) (g_type_name (G_ENUM_CLASS_TYPE (class)))
79 * Checks whether @type "is a" %G_TYPE_FLAGS.
81 * Returns: %TRUE if @type "is a" %G_TYPE_FLAGS.
83 #define G_TYPE_IS_FLAGS(type) (G_TYPE_FUNDAMENTAL (type) == G_TYPE_FLAGS)
86 * @class: a valid #GFlagsClass
88 * Casts a derived #GFlagsClass structure into a #GFlagsClass structure.
90 #define G_FLAGS_CLASS(class) (G_TYPE_CHECK_CLASS_CAST ((class), G_TYPE_FLAGS, GFlagsClass))
93 * @class: a #GFlagsClass
95 * Checks whether @class "is a" valid #GFlagsClass structure of type %G_TYPE_FLAGS
98 #define G_IS_FLAGS_CLASS(class) (G_TYPE_CHECK_CLASS_TYPE ((class), G_TYPE_FLAGS))
100 * G_FLAGS_CLASS_TYPE:
101 * @class: a #GFlagsClass
103 * Get the type identifier from a given #GFlagsClass structure.
105 * Returns: the #GType
107 #define G_FLAGS_CLASS_TYPE(class) (G_TYPE_FROM_CLASS (class))
109 * G_FLAGS_CLASS_TYPE_NAME:
110 * @class: a #GFlagsClass
112 * Get the static type name from a given #GFlagsClass structure.
114 * Returns: the type name.
116 #define G_FLAGS_CLASS_TYPE_NAME(class) (g_type_name (G_FLAGS_CLASS_TYPE (class)))
120 * G_VALUE_HOLDS_ENUM:
121 * @value: a valid #GValue structure
123 * Checks whether the given #GValue can hold values derived from type %G_TYPE_ENUM.
125 * Returns: %TRUE on success.
127 #define G_VALUE_HOLDS_ENUM(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_ENUM))
129 * G_VALUE_HOLDS_FLAGS:
130 * @value: a valid #GValue structure
132 * Checks whether the given #GValue can hold values derived from type %G_TYPE_FLAGS.
134 * Returns: %TRUE on success.
136 #define G_VALUE_HOLDS_FLAGS(value) (G_TYPE_CHECK_VALUE_TYPE ((value), G_TYPE_FLAGS))
139 /* --- enum/flag values & classes --- */
140 typedef struct _GEnumClass GEnumClass;
141 typedef struct _GFlagsClass GFlagsClass;
142 typedef struct _GEnumValue GEnumValue;
143 typedef struct _GFlagsValue GFlagsValue;
147 * @g_type_class: the parent class
148 * @minimum: the smallest possible value.
149 * @maximum: the largest possible value.
150 * @n_values: the number of possible values.
151 * @values: an array of #GEnumValue structs describing the
154 * The class of an enumeration type holds information about its
159 GTypeClass g_type_class;
169 * @g_type_class: the parent class
170 * @mask: a mask covering all possible values.
171 * @n_values: the number of possible values.
172 * @values: an array of #GFlagsValue structs describing the
175 * The class of a flags type holds information about its
180 GTypeClass g_type_class;
189 * @value: the enum value
190 * @value_name: the name of the value
191 * @value_nick: the nickname of the value
193 * A structure which contains a single enum value, its name, and its
199 const gchar *value_name;
200 const gchar *value_nick;
204 * @value: the flags value
205 * @value_name: the name of the value
206 * @value_nick: the nickname of the value
208 * A structure which contains a single flags value, its name, and its
214 const gchar *value_name;
215 const gchar *value_nick;
219 /* --- prototypes --- */
220 GOBJECT_AVAILABLE_IN_ALL
221 GEnumValue* g_enum_get_value (GEnumClass *enum_class,
223 GOBJECT_AVAILABLE_IN_ALL
224 GEnumValue* g_enum_get_value_by_name (GEnumClass *enum_class,
226 GOBJECT_AVAILABLE_IN_ALL
227 GEnumValue* g_enum_get_value_by_nick (GEnumClass *enum_class,
229 GOBJECT_AVAILABLE_IN_ALL
230 GFlagsValue* g_flags_get_first_value (GFlagsClass *flags_class,
232 GOBJECT_AVAILABLE_IN_ALL
233 GFlagsValue* g_flags_get_value_by_name (GFlagsClass *flags_class,
235 GOBJECT_AVAILABLE_IN_ALL
236 GFlagsValue* g_flags_get_value_by_nick (GFlagsClass *flags_class,
238 GOBJECT_AVAILABLE_IN_2_54
239 gchar *g_enum_to_string (GType g_enum_type,
241 GOBJECT_AVAILABLE_IN_2_54
242 gchar *g_flags_to_string (GType flags_type,
244 GOBJECT_AVAILABLE_IN_ALL
245 void g_value_set_enum (GValue *value,
247 GOBJECT_AVAILABLE_IN_ALL
248 gint g_value_get_enum (const GValue *value);
249 GOBJECT_AVAILABLE_IN_ALL
250 void g_value_set_flags (GValue *value,
252 GOBJECT_AVAILABLE_IN_ALL
253 guint g_value_get_flags (const GValue *value);
257 /* --- registration functions --- */
258 /* const_static_values is a NULL terminated array of enum/flags
259 * values that is taken over!
261 GOBJECT_AVAILABLE_IN_ALL
262 GType g_enum_register_static (const gchar *name,
263 const GEnumValue *const_static_values);
264 GOBJECT_AVAILABLE_IN_ALL
265 GType g_flags_register_static (const gchar *name,
266 const GFlagsValue *const_static_values);
267 /* functions to complete the type information
268 * for enums/flags implemented by plugins
270 GOBJECT_AVAILABLE_IN_ALL
271 void g_enum_complete_type_info (GType g_enum_type,
273 const GEnumValue *const_values);
274 GOBJECT_AVAILABLE_IN_ALL
275 void g_flags_complete_type_info (GType g_flags_type,
277 const GFlagsValue *const_values);
282 * G_DEFINE_ENUM_VALUE:
283 * @EnumValue: an enumeration value
284 * @EnumNick: a short string representing the enumeration value
286 * Defines an enumeration value, and maps it to a "nickname".
288 * This macro can only be used with G_DEFINE_ENUM_TYPE() and
289 * G_DEFINE_FLAGS_TYPE().
293 #define G_DEFINE_ENUM_VALUE(EnumValue, EnumNick) \
294 { EnumValue, #EnumValue, EnumNick } \
295 GOBJECT_AVAILABLE_MACRO_IN_2_74
298 * G_DEFINE_ENUM_TYPE:
299 * @TypeName: the enumeration type, in `CamelCase`
300 * @type_name: the enumeration type prefixed, in `snake_case`
301 * @...: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
303 * A convenience macro for defining enumeration types.
305 * This macro will generate a `*_get_type()` function for the
306 * given @TypeName, using @type_name as the function prefix.
308 * |[<!-- language="C" -->
309 * G_DEFINE_ENUM_TYPE (GtkOrientation, gtk_orientation,
310 * G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_HORIZONTAL, "horizontal"),
311 * G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_VERTICAL, "vertical"))
314 * For projects that have multiple enumeration types, or enumeration
315 * types with many values, you should consider using glib-mkenums to
316 * generate the type function.
320 #define G_DEFINE_ENUM_TYPE(TypeName, type_name, ...) \
322 type_name ## _get_type (void) { \
323 static gsize g_define_type__static = 0; \
324 if (g_once_init_enter (&g_define_type__static)) { \
325 static const GEnumValue enum_values[] = { \
329 GType g_define_type = g_enum_register_static (g_intern_static_string (#TypeName), enum_values); \
330 g_once_init_leave (&g_define_type__static, g_define_type); \
332 return g_define_type__static; \
334 GOBJECT_AVAILABLE_MACRO_IN_2_74
337 * G_DEFINE_FLAGS_TYPE:
338 * @TypeName: the enumeration type, in `CamelCase`
339 * @type_name: the enumeration type prefixed, in `snake_case`
340 * @...: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
342 * A convenience macro for defining flag types.
344 * This macro will generate a `*_get_type()` function for the
345 * given @TypeName, using @type_name as the function prefix.
347 * |[<!-- language="C" -->
348 * G_DEFINE_FLAGS_TYPE (GSettingsBindFlags, g_settings_bind_flags,
349 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_DEFAULT, "default"),
350 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET, "get"),
351 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_SET, "set"),
352 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_NO_SENSITIVITY, "no-sensitivity"),
353 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET_NO_CHANGES, "get-no-changes"),
354 * G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_INVERT_BOOLEAN, "invert-boolean"))
357 * For projects that have multiple enumeration types, or enumeration
358 * types with many values, you should consider using glib-mkenums to
359 * generate the type function.
363 #define G_DEFINE_FLAGS_TYPE(TypeName, type_name, ...) \
365 type_name ## _get_type (void) { \
366 static gsize g_define_type__static = 0; \
367 if (g_once_init_enter (&g_define_type__static)) { \
368 static const GFlagsValue flags_values[] = { \
372 GType g_define_type = g_flags_register_static (g_intern_static_string (#TypeName), flags_values); \
373 g_once_init_leave (&g_define_type__static, g_define_type); \
375 return g_define_type__static; \
377 GOBJECT_AVAILABLE_MACRO_IN_2_74
381 #endif /* __G_ENUMS_H__ */