libGimbal 0.2.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_enum.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblEnumClass and API for managing enumerated types
3 * \ingroup meta
4 *
5 * \todo
6 * - Finish docs (method descriptions)
7 *
8 * \author 2023 Falco Girgis
9 * \author 2026 Agustín Bellagamba
10 * \copyright MIT License
11 */
12#ifndef GIMBAL_ENUM_H
13#define GIMBAL_ENUM_H
14
16#include "../../strings/gimbal_quark.h"
17
18/*! \name Type System
19 * \brief Type UUID and cast operators
20 * @{
21 */
22#define GBL_ENUM_TYPE (GBL_TYPEID(GblEnum)) //!< Type UUID of GblEnumClass
23#define GBL_ENUM_CLASS(klass) (GBL_CLASS_CAST(GblEnum, klass)) //!< Function-style GblClass cast
24//! @}
25
26/*! \name Helper Macros
27 * \brief Utility macros to aid in representing GblEnumEntry lists
28 * @{
29 */
30#define GBL_ENUM_ENTRY(enumValue, nick) { enumValue, #enumValue, nick } //!< Utility GblEnumEntry initializer, using the \p enumValue
31#define GBL_ENUM_ENTRY_LAST() { 0, NULL, NULL } //!< Utility GblEnumEntry initializer for end list designator
32//! @}
33
34#define GBL_SELF_TYPE GblEnumClass
35
36/*! \brief DSL macro used to declare and register a GblEnum
37 *
38 * \param name Name of the GblEnum
39 * \param ... Enum entries in the form:
40 * (value, nick, number)
41 */
42#define GBL_ENUM(name, ...) GBL_ENUM_(name, __VA_ARGS__)
43
45
46//! Attributes for a single value of an enumeration
47typedef struct GblEnumEntry {
48 GblEnum value; //!< Value of the enumeration
49 const char* pName; //!< String name of the enumeration value
50 const char* pNick; //!< Alternate name of the enumeration value
51} GblEnumEntry;
52
53/*! \struct GblEnumClass
54 * \extends GblPrimitiveClass
55 * \brief Static GblClass for storing type data for enumerations
56 *
57 * GblEnumClass is a static/persistent class which is created
58 * from a list of GblEnumEntry structures, representing all of
59 * the valid values for the enumerated type.
60 */
61GBL_CLASS_DERIVE(GblEnum, GblPrimitive)
62 GblEnum valueMax; //!< Cached maximum value of the enum (used for validation)
63 GblEnum valueMin; //!< Cached minimum value of the enum (used for validation)
64 uint16_t entryCount; //!< Cached number of values in the entry table
66
67// ========== GblEnumClass ==========
68/*! \name Name Queries
69 * \brief Methods for querying name of enum values
70 * \relatesalso GblEnumClass
71 * @{
72 */
73GBL_EXPORT const char* GblEnumClass_nameFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
74GBL_EXPORT const char* GblEnumClass_nameFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
75GBL_EXPORT GblQuark GblEnumClass_nameQuarkFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
76GBL_EXPORT GblQuark GblEnumClass_nameQuarkFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
77//! @}
78
79/*! \name Nick Queries
80 * \brief Methods for querying nick of enum values
81 * \relatesalso GblEnumClass
82 * @{
83 */
84GBL_EXPORT const char* GblEnumClass_nickFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
85GBL_EXPORT const char* GblEnumClass_nickFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
86GBL_EXPORT GblQuark GblEnumClass_nickQuarkFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
87GBL_EXPORT GblQuark GblEnumClass_nickQuarkFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
88//! @}
89
90/*! \name Value Queries
91 * \brief Methods for querying value of enum values
92 * \relatesalso GblEnumClass
93 * @{
94 */
95GBL_EXPORT GblEnum GblEnumClass_valueFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
96GBL_EXPORT GblEnum GblEnumClass_valueFromName (GBL_CSELF, const char* pString) GBL_NOEXCEPT;
97GBL_EXPORT GblEnum GblEnumClass_valueFromNick (GBL_CSELF, const char* pString) GBL_NOEXCEPT;
98GBL_EXPORT GblEnum GblEnumClass_valueFromNameQuark (GBL_CSELF, GblQuark quark) GBL_NOEXCEPT;
99GBL_EXPORT GblEnum GblEnumClass_valueFromNickQuark (GBL_CSELF, GblQuark quark) GBL_NOEXCEPT;
100GBL_EXPORT GblBool GblEnumClass_valueCheck (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
101//! @}
102
103/*! \name Index Queries
104 * \brief Methods for querying enum entry indices, returning UINT16_MAX when not found.
105 * \relatesalso GblEnumClass
106 * @{
107 */
108GBL_EXPORT uint16_t GblEnumClass_indexFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
109GBL_EXPORT uint16_t GblEnumClass_indexFromName (GBL_CSELF, const char* pName) GBL_NOEXCEPT;
110GBL_EXPORT uint16_t GblEnumClass_indexFromNick (GBL_CSELF, const char* pNick) GBL_NOEXCEPT;
111GBL_EXPORT uint16_t GblEnumClass_indexFromNameQuark (GBL_CSELF, GblQuark name) GBL_NOEXCEPT;
112GBL_EXPORT uint16_t GblEnumClass_indexFromNickQuark (GBL_CSELF, GblQuark nick) GBL_NOEXCEPT;
113//! @}
114
115// ========== GblEnum ==========
116GBL_EXPORT GblType GblEnum_type (void) GBL_NOEXCEPT;
117GBL_EXPORT GblType GblEnum_register (const char* pName,
118 const GblEnumEntry* pEntries) GBL_NOEXCEPT;
119
120/*! \name Enums from Strings
121 * \brief Methods for getting an enum value from string
122 * @{
123 */
124GBL_EXPORT GblEnum GblEnum_fromName (const char* pName, GblType type) GBL_NOEXCEPT;
125GBL_EXPORT GblEnum GblEnum_fromNameQuark (GblQuark name, GblType type) GBL_NOEXCEPT;
126GBL_EXPORT GblEnum GblEnum_fromNick (const char* pNick, GblType type) GBL_NOEXCEPT;
127GBL_EXPORT GblEnum GblEnum_fromNickQuark (GblQuark nick, GblType type) GBL_NOEXCEPT;
128//! @}
129
130/*! \name Enums to Strings
131 * \brief Methods for getting a string from an enum value
132 * @{
133 */
134GBL_EXPORT const char* GblEnum_name (GblEnum value, GblType type) GBL_NOEXCEPT;
135GBL_EXPORT GblQuark GblEnum_nameQuark (GblEnum value, GblType type) GBL_NOEXCEPT;
136GBL_EXPORT const char* GblEnum_nick (GblEnum value, GblType type) GBL_NOEXCEPT;
137GBL_EXPORT GblQuark GblEnum_nickQuark (GblEnum value, GblType type) GBL_NOEXCEPT;
138//! @}
139
140GBL_EXPORT GblBool GblEnum_check (GblEnum value, GblType type) GBL_NOEXCEPT;
141
143
144//! \cond GRUG_FREE
145
146#define GBL_ENUM_(name, ...)
147 GBL_DECLARE_ENUM(name) {
148 GBL_TUPLE_FOREACH(GBL_ENUM_DECL_, name, (__VA_ARGS__))
149 };
150 GBL_ENUM_REGISTER_(name, __VA_ARGS__)
151
152#define GBL_ENUM_REGISTER_(name, ...)
153 GBL_INLINE GblType name##_type(void) GBL_NOEXCEPT {
154 static GblType type_ = GBL_INVALID_TYPE;
155 if(type_ == GBL_INVALID_TYPE) {
156 type_ = GblType_find(GBL_STRINGIFY(name));
157 if (type_ == GBL_INVALID_TYPE) {
158 static const GblEnumEntry entries_[] = {
159 GBL_TUPLE_FOREACH(GBL_ENUM_TABLE_, name, (__VA_ARGS__))
161 };
162 type_ = GblEnum_register(GBL_STRINGIFY(name), entries_);
163 }
164 }
165 return type_;
166 }
167
168#define GBL_ENUM_DECL_(name, entry) GBL_ENUM_DECL__(GBL_EVAL entry)
169#define GBL_ENUM_DECL__(...) GBL_ENUM_DECL___(__VA_ARGS__)
170#define GBL_ENUM_DECL___(value, nick, num)
171 value = num,
172
173#define GBL_ENUM_TABLE_(name, entry) GBL_ENUM_TABLE__(GBL_EVAL entry)
174#define GBL_ENUM_TABLE__(...) GBL_ENUM_TABLE___(__VA_ARGS__)
175#define GBL_ENUM_TABLE___(value, nick, num)
176 GBL_ENUM_ENTRY(value, nick),
177
178//! \endcond
179
180#undef GBL_SELF_TYPE
181
182#endif // GIMBAL_ENUM_H
#define GBL_NOEXCEPT
#define GBL_INLINE
#define GBL_DECLS_BEGIN
#define GBL_TYPEID(instanceStruct)
#define GBL_DECLARE_ENUM(E)
#define GBL_CLASS_DERIVE(...)
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_ENUM_ENTRY_LAST()
Utility GblEnumEntry initializer for end list designator.
Definition gimbal_enum.h:31
#define GBL_ENUM_ENTRY(enumValue, nick)
Utility GblEnumEntry initializer, using the enumValue.
Definition gimbal_enum.h:30
#define GBL_STRINGIFY(a)
#define GBL_TUPLE_FOREACH(MACRO_, DATA_, TUPLE_)
#define GBL_EVAL(...)
#define GBL_INVALID_TYPE
GblType UUID of the invalid type.
Definition gimbal_type.h:31
uint32_t GblEnum
Standard-sized enum type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:52
uintptr_t GblQuark
Uniquely identifiable interned string type.
GblEnum valueMin
Cached minimum value of the enum (used for validation)
Definition gimbal_enum.h:63
GblEnum valueMax
Cached maximum value of the enum (used for validation)
Definition gimbal_enum.h:62
uint16_t entryCount
Cached number of values in the entry table.
Definition gimbal_enum.h:64
Attributes for a single value of an enumeration.
Definition gimbal_enum.h:47
GblEnum value
Value of the enumeration.
Definition gimbal_enum.h:48
const char * pName
String name of the enumeration value.
Definition gimbal_enum.h:49
const char * pNick
Alternate name of the enumeration value.
Definition gimbal_enum.h:50