Very simple solution with one big constraint: you can't assign custom values to enum
values, but with the right regex, you could. you could also add a map to translate them back to enum
values without much more effort:
#include
#include
#include
#include
std::vector split(const std::string& s,
const std::regex& delim = std::regex(",\\s*"))
{
using namespace std;
vector cont;
copy(regex_token_iterator(s.begin(), s.end(), delim, -1),
regex_token_iterator(),
back_inserter(cont));
return cont;
}
#define EnumType(Type, ...) enum class Type { __VA_ARGS__ }
#define EnumStrings(Type, ...) static const std::vector \
Type##Strings = split(#__VA_ARGS__);
#define EnumToString(Type, ...) EnumType(Type, __VA_ARGS__); \
EnumStrings(Type, __VA_ARGS__)
Usage example:
EnumToString(MyEnum, Red, Green, Blue);