[glib/ebassi/enum-type-macros: 1/3] Add G_DEFINE macros for enum and flags types




commit 18ae64f991dbf563198e642986404d32b9a16c42
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Jun 29 14:02:51 2022 +0100

    Add G_DEFINE macros for enum and flags types
    
    While you might want to use automated tools like glib-mkenums to
    generate enumeration types for your library, it's often not entirely
    necessary to complicate your build system in order to handle a couple of
    enumerations with few values.
    
    Just like we have G_DEFINE macros for object, interface, pointer, and
    boxed types, we should provide macros for defining enum and flags types.

 gobject/genums.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
---
diff --git a/gobject/genums.h b/gobject/genums.h
index c66ce45c00..1e9859963b 100644
--- a/gobject/genums.h
+++ b/gobject/genums.h
@@ -274,6 +274,94 @@ void       g_flags_complete_type_info (GType              g_flags_type,
                                    GTypeInfo         *info,
                                    const GFlagsValue *const_values);
 
+/* {{{ Macros */
+
+/**
+ * G_DEFINE_ENUM_VALUE:
+ * @EnumValue: an enumeration value
+ * @EnumNick: a short string representing the enumeration value
+ *
+ * Defines an enumeration value, and maps it to a "nickname".
+ *
+ * This macro can only be used with G_DEFINE_ENUM_TYPE() and
+ * G_DEFINE_FLAGS_TYPE().
+ *
+ * Since: 2.74
+ */
+#define G_DEFINE_ENUM_VALUE(EnumValue, EnumNick) { EnumValue, #EnumValue, EnumNick },
+
+/**
+ * G_DEFINE_ENUM_TYPE:
+ * @TypeName: the enumeration type, in `CamelCase`
+ * @type_name: the enumeration type prefixed, in `snake_case`
+ * @value: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
+ *
+ * A convenience macro for defining enumeration types.
+ *
+ * This macro will generate a `*_get_type()` function for the
+ * given @TypeName, using @type_name as the function prefix.
+ *
+ * |[<!-- language="C" -->
+ * G_DEFINE_ENUM_TYPE (GtkOrientation, gtk_orientation,
+ *   G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_HORIZONTAL, "horizontal")
+ *   G_DEFINE_ENUM_VALUE (GTK_ORIENTATION_VERTICAL, "vertical"))
+ * ]|
+ *
+ * Since: 2.74
+ */
+#define G_DEFINE_ENUM_TYPE(TypeName, type_name, values) \
+GType \
+type_name ## _get_type (void) { \
+  static gsize g_define_type__static = 0; \
+  if (g_once_init_enter (&g_define_type__static)) { \
+    static const GEnumValue enum_values[] = { \
+      values \
+      { 0, NULL, NULL }, \
+    }; \
+    GType g_define_type = g_enum_register_static (g_intern_static_string (#TypeName), enum_values); \
+    g_once_init_leave (&g_define_type__static, g_define_type); \
+  } \
+  return g_define_type__static; \
+}
+
+/**
+ * G_DEFINE_FLAGS_TYPE:
+ * @TypeName: the enumeration type, in `CamelCase`
+ * @type_name: the enumeration type prefixed, in `snake_case`
+ * @value: a list of enumeration values, defined using G_DEFINE_ENUM_VALUE()
+ *
+ * A convenience macro for defining flag types.
+ *
+ * This macro will generate a `*_get_type()` function for the
+ * given @TypeName, using @type_name as the function prefix.
+ *
+ * |[<!-- language="C" -->
+ * G_DEFINE_FLAGS_TYPE (GSettingsBindFlags, g_settings_bind_flags,
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_DEFAULT, "default")
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET, "get")
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_SET, "set")
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_NO_SENSITIVITY, "no-sensitivity")
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_GET_NO_CHANGES, "get-no-changes")
+ *   G_DEFINE_ENUM_VALUE (G_SETTINGS_BIND_INVERT_BOOLEAN, "invert-boolean"))
+ * ]|
+ *
+ * Since: 2.74
+ */
+#define G_DEFINE_FLAGS_TYPE(TypeName, type_name, values) \
+GType \
+type_name ## _get_type (void) { \
+  static gsize g_define_type__static = 0; \
+  if (g_once_init_enter (&g_define_type__static)) { \
+    static const GFlagsValue flags_values[] = { \
+      values \
+      { 0, NULL, NULL }, \
+    }; \
+    GType g_define_type = g_flags_register_static (g_intern_static_string (#TypeName), flags_values); \
+    g_once_init_leave (&g_define_type__static, g_define_type); \
+  } \
+  return g_define_type__static; \
+}
+
 G_END_DECLS
 
 #endif /* __G_ENUMS_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]