Debugging MSVC interception
Metamacros is causing all sorts of chaos on Intellisense and the like, but they can make it easier ...
#define MY_ENUMS(e_) \
e_(Enum_A), \
e_(Enum_B), \
e_(Enum_C), \
#define ENUM_EXPANDER(e_) e
enum MyEnums
{
MY_ENUMS(ENUM_EXPANDER)
CountOfMyEnums
};
#define STRING_EXPANDER(e_) #e_
const char* g_myEnumStrings[] =
{
MY_ENUMS(STRING_EXPANDER)
};
Maybe even
#define CASE_EXPANDER(e_) case e_: return #e_;
const char* GetEnumName(MyEnums e)
{
switch (e)
{
MY_ENUMS(CASE_EXPANDER)
default:
return "Invalid enum value";
}
}
Various "expander macros" can be used to populate maps or other data structures of your choice. I used this horror to parse enums from config files (so the person who created the config file could use the enumeration, not the index).
a source to share
I just put the enumeration names in the lookup table (or you could use map<>
) with the enum value as the key and have a function doing the lookup.
It's low tech, but usually not too much of a pain.
In some projects I will have a weird header / macro layout that could build an enum definition using one declaration-like element for each enum name. My opinion on how this technique works unfolds between "comfy" or "shreds".
a source to share
This is a common problem in C ++ and is solved with the "type enumeration pattern". This is usually done using some crazy precompiler definitions or code generators. A quick search on "Typafe enum pattern C ++" can give you these ways. Personally, I have my own code generator for C ++ enums that runs as a custom MSVC build step for h files with enums.
a source to share