I have a const static std::map and I want to fill it with the preprocessor. I want a mapping between my enumeration and string-names of the enum type. For example:
enum Color {
RED,
BLUE,
GREEN
};
const static std::map<std::string, Color> = {
{"RED", Color::RED},
{"red", Color::RED},
{"BLUE", Color::BLUE},
{"blue", Color::BLUE},
{"GREEN", Color::GREEN}
{"green", Color::GREEN}
};
That is a simple example. I want to write something like:
enum Color {
RED,
BLUE,
GREEN
};
const static std::map<std::string, Color> = {
CREATE(RED),
CREATE(BLUE),
CREATE(GREEN)
};
I have lots of values in my enumeration and you see that it is a lot of work, even for a few values. I thought about using some preprocessor magic but I don't know how to get it working. My first thought was:
#define(name) {"name", Color::name}
My second thought was:
#define(name) {"(name)", Color::(name)}
In the first case I get the string "name" as the key, in the second case I get the string "(name)" as the key and a compiler error because of my value. It would be great to get a solution for this problem, even if this would not include a lower-case version of the string. That would just be a small bonus. Do you have any ideas how I could solve my problem?
edit: Thank's to @Gem Taylor. I have a solution for the first problem:
#define CREATE(name) {#name, Color::name}
If you have an idea how to create the version with the lowercase string, that would be great. So that:
CREATE(RED)
would create:
{"RED", Color::RED}, {"red", Color::RED}
Thank's