TazGraph Project v0.1.0
Loading...
Searching...
No Matches
DefineFactory.h
1#pragma once
2#include <cstring>
3
4// Macro to extract just the enum name
5#define ENUM_NAME(name) name,
6
7// Macro to extract the string representation
8#define ENUM_STRING(name) #name,
9
10// Macro to create switch case for enum to string
11#define ENUM_TO_STRING_CASE(name) case name: return #name;
12
13// Macro to create string comparison for string to enum
14#define STRING_TO_ENUM_COMPARE(name) if (strcmp(str, #name) == 0) return name;
15
17#define DECLARE_ENUM(EnumType, ENUM_LIST) \
18 enum EnumType { \
19 ENUM_LIST(ENUM_NAME) \
20 }; \
21 const char* EnumType##_ToString(EnumType value); \
22 EnumType EnumType##_FromString(const char* str);
23
25#define DEFINE_ENUM(EnumType, ENUM_LIST) \
26 const char* EnumType##_ToString(EnumType value) { \
27 switch(value) { \
28 ENUM_LIST(ENUM_TO_STRING_CASE) \
29 default: return "UNKNOWN"; \
30 } \
31 } \
32 EnumType EnumType##_FromString(const char* str) { \
33 if (str == nullptr) return (EnumType)0; \
34 ENUM_LIST(STRING_TO_ENUM_COMPARE) \
35 return (EnumType)0; \
36 }
37
38// Example usage:
39#define NODE_PORTS(XX) \
40 XX(TOP) \
41 XX(RIGHT) \
42 XX(BOTTOM) \
43 XX(LEFT)
44
45#define LINK_CHILDREN(XX) \
46 XX(NONE) \
47 XX(ARROWHEAD)
48
49// In header file (.h):
50DECLARE_ENUM(NodePorts, NODE_PORTS)
51DECLARE_ENUM(LinkChildren, LINK_CHILDREN)