Enum utilities#
Generate functions for enumerations.
Defines
-
DECLARE_ENUM_WITH_VALUES(NAME, ...)#
Declare an enum with custom values, with string conversions.
Example#
Evaluates to:DECLARE_ENUM_WITH_VALUES( cardinal, (EAST, 0b0001), (WEST, 0b0010), (NORTH, 0b0100), (SOUTH, 0b1000) )
enum cardinal { EAST = 0b0001, WEST = 0b0010, NORTH = 0b0100, SOUTH = 0b1000, cardinal_upper_bound }; static inline const char* cardinal_to_cstring(enum cardinal value) { switch (value) { case (0b0001): return "EAST"; case (0b0010): return "WEST"; case (0b0100): return "NORTH"; case (0b1000): return "SOUTH"; default: return NULL; } } static inline bool cardinal_is_valid(long value) { return cardinal_to_cstring((enum cardinal)value) != NULL; } static inline bool cardinal_has_next(enum cardinal* it) { do { *it = (enum cardinal)(1 + *it); } while ((long)*it < (long)cardinal_upper_bound && !cardinal_is_valid((long)*it)); return (long)*it < (long)cardinal_upper_bound; } static inline enum cardinal cardinal_from_cstring(const char* string) { if (strcmp("EAST", string) == 0) return 0b0001; if (strcmp("WEST", string) == 0) return 0b0010; if (strcmp("NORTH", string) == 0) return 0b0100; if (strcmp("SOUTH", string) == 0) return 0b1000; return cardinal_upper_bound; }
- Since
0.1
-
DECLARE_ENUM_WITH_STRINGS(NAME, ...)#
Declare an enum with custom strings.
Example#
Evaluates to:DECLARE_ENUM_WITH_STRINGS( grocery, (ITEM_EGG, "Egg"), (ITEM_MILK, "Milk"), (ITEM_BREAD, "Bread"), (ITEM_SOUP, "Soup"), (ITEM_YEAST, "Yeast"), (ITEM_PASTA, "Pasta") )
enum grocery { ITEM_EGG, ITEM_MILK, ITEM_BREAD, ITEM_SOUP, ITEM_YEAST, ITEM_PASTA, grocery_count }; static inline bool grocery_is_valid(long value) { return (0 <= value && value < grocery_count); } static inline const char* grocery_to_cstring(enum grocery value) { static const char* const table[] = { "Egg", "Milk", "Bread", "Soup", "Yeast", "Pasta", }; if (!grocery_is_valid((long)value)) return NULL; return table[value]; } static inline bool grocery_has_next(enum grocery* it) { do { *it = (enum grocery)(1 + *it); } while ((long)*it < (long)grocery_count && !grocery_is_valid((long)*it)); return (long)*it < (long)grocery_count; } static inline enum grocery grocery_from_cstring(const char* string) { if (strcmp("Egg", string) == 0) return ITEM_EGG; if (strcmp("Milk", string) == 0) return ITEM_MILK; if (strcmp("Bread", string) == 0) return ITEM_BREAD; if (strcmp("Soup", string) == 0) return ITEM_SOUP; if (strcmp("Yeast", string) == 0) return ITEM_YEAST; if (strcmp("Pasta", string) == 0) return ITEM_PASTA; return grocery_upper_bound; }
- Since
0.1