I need to define an enum like ID and also provide converting utility for it to the corresponding string and vice versa. So I am choosing unordered_map to save such 1-on-1 mapping. It effectively would translate to belowing:
enum COLOR_ID {
UNKNOWN,
GREEN,
RED,
BLUE,
YELLOW
};
std::unordered_map<std::string, COLOR_ID> str_id_map;
str_id_map["GREEN"] = GREEN;
...
std::unordered_map<COLOR_ID, std::string, COLOR_ID> id_str_map;
id_str_map[GREEN] = "GREEN";
...
The reason I am doing this is I will have a very long and expandable list of enums/IDs, and I want to make it easy to maintain to add a new enum/ID. The user only need to add the ID in one place, and it gets automatically populated to the two maps.
Any ideas?
I wonder whether there is a more succinct way to use a MACRO that I can only give the COLOR_ID list once instead putting it in three places, so that would be easier to maintain.