libGimbal 0.1.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 * - Macro DSL for auto registering and declaring enum at the same time
7 * - Finish docs (method descriptions)
8 *
9 * \author 2023 Falco Girgis
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
37
38//! Attributes for a single value of an enumeration
39typedef struct GblEnumEntry {
40 GblEnum value; //!< Value of the enumeration
41 const char* pName; //!< String name of the enumeration value
42 const char* pNick; //!< Alternate name of the enumeration value
43} GblEnumEntry;
44
45/*! \struct GblEnumClass
46 * \extends GblPrimitiveClass
47 * \brief Static GblClass for storing type data for enumerations
48 *
49 * GblEnumClass is a static/persistent class which is created
50 * from a list of GblEnumEntry structures, representing all of
51 * the valid values for the enumerated type.
52 */
53GBL_CLASS_DERIVE(GblEnum, GblPrimitive)
54 GblEnum valueMax; //!< Cached maximum value of the enum (used for validation)
55 GblEnum valueMin; //!< Cached minimum value of the enum (used for validation)
56 uint16_t entryCount; //!< Cached number of values in the entry table
58
59// ========== GblEnumClass ==========
60/*! \name Name Queries
61 * \brief Methods for querying name of enum values
62 * \relatesalso GblEnumClass
63 * @{
64 */
65GBL_EXPORT const char* GblEnumClass_nameFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
66GBL_EXPORT const char* GblEnumClass_nameFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
67GBL_EXPORT GblQuark GblEnumClass_nameQuarkFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
68GBL_EXPORT GblQuark GblEnumClass_nameQuarkFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
69//! @}
70
71/*! \name Nick Queries
72 * \brief Methods for querying nick of enum values
73 * \relatesalso GblEnumClass
74 * @{
75 */
76GBL_EXPORT const char* GblEnumClass_nickFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
77GBL_EXPORT const char* GblEnumClass_nickFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
78GBL_EXPORT GblQuark GblEnumClass_nickQuarkFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
79GBL_EXPORT GblQuark GblEnumClass_nickQuarkFromValue (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
80//! @}
81
82/*! \name Value Queries
83 * \brief Methods for querying value of enum values
84 * \relatesalso GblEnumClass
85 * @{
86 */
87GBL_EXPORT GblEnum GblEnumClass_valueFromIndex (GBL_CSELF, uint16_t index) GBL_NOEXCEPT;
88GBL_EXPORT GblEnum GblEnumClass_valueFromName (GBL_CSELF, const char* pString) GBL_NOEXCEPT;
89GBL_EXPORT GblEnum GblEnumClass_valueFromNick (GBL_CSELF, const char* pString) GBL_NOEXCEPT;
90GBL_EXPORT GblEnum GblEnumClass_valueFromNameQuark (GBL_CSELF, GblQuark quark) GBL_NOEXCEPT;
91GBL_EXPORT GblEnum GblEnumClass_valueFromNickQuark (GBL_CSELF, GblQuark quark) GBL_NOEXCEPT;
92GBL_EXPORT GblBool GblEnumClass_valueCheck (GBL_CSELF, GblEnum value) GBL_NOEXCEPT;
93//! @}
94
95// ========== GblEnum ==========
96GBL_EXPORT GblType GblEnum_type (void) GBL_NOEXCEPT;
97GBL_EXPORT GblType GblEnum_register (const char* pName,
98 const GblEnumEntry* pEntries) GBL_NOEXCEPT;
99
100/*! \name Enums from Strings
101 * \brief Methods for getting an enum value from string
102 * @{
103 */
104GBL_EXPORT GblEnum GblEnum_fromName (const char* pName, GblType type) GBL_NOEXCEPT;
105GBL_EXPORT GblEnum GblEnum_fromNameQuark (GblQuark name, GblType type) GBL_NOEXCEPT;
106GBL_EXPORT GblEnum GblEnum_fromNick (const char* pNick, GblType type) GBL_NOEXCEPT;
107GBL_EXPORT GblEnum GblEnum_fromNickQuark (GblQuark nick, GblType type) GBL_NOEXCEPT;
108//! @}
109
110/*! \name Enums to Strings
111 * \brief Methods for getting a string from an enum value
112 * @{
113 */
114GBL_EXPORT const char* GblEnum_name (GblEnum value, GblType type) GBL_NOEXCEPT;
115GBL_EXPORT GblQuark GblEnum_nameQuark (GblEnum value, GblType type) GBL_NOEXCEPT;
116GBL_EXPORT const char* GblEnum_nick (GblEnum value, GblType type) GBL_NOEXCEPT;
117GBL_EXPORT GblQuark GblEnum_nickQuark (GblEnum value, GblType type) GBL_NOEXCEPT;
118//! @}
119
120GBL_EXPORT GblBool GblEnum_check (GblEnum value, GblType type) GBL_NOEXCEPT;
121
123
124#if 0
125#define GBL_ENUM(name, ...) GBL_ENUM_(name, __VA_ARGS__)
126
127#define GBL_ENUM_ENTRY_(name, ...)
128 GBL_ENUM_ENTRY(__VA_ARGS__)
129
130#define GBL_ENUM_(name, ...)
131 GBL_DECLARE_ENUM(name) {
132 GBL_TUPLE_FOREACH(GBL_ENUM_ENTRY_, name, __VA_ARGS__)
133 GBL_ENUM_ENTRY_LAST()
134 };
135#endif
136
137#undef GBL_SELF_TYPE
138
139#endif // GIMBAL_ENUM_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_TYPEID(instanceStruct)
#define GBL_CLASS_DERIVE(...)
#define GBL_EXPORT
#define GBL_CLASS_END
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:51
uintptr_t GblQuark
Uniquely identifiable interned string type.
GblEnum valueMin
Cached minimum value of the enum (used for validation)
Definition gimbal_enum.h:55
GblEnum valueMax
Cached maximum value of the enum (used for validation)
Definition gimbal_enum.h:54
uint16_t entryCount
Cached number of values in the entry table.
Definition gimbal_enum.h:56
Attributes for a single value of an enumeration.
Definition gimbal_enum.h:39
GblEnum value
Value of the enumeration.
Definition gimbal_enum.h:40
const char * pName
String name of the enumeration value.
Definition gimbal_enum.h:41
const char * pNick
Alternate name of the enumeration value.
Definition gimbal_enum.h:42