patterncppMinor
Map enum values
Viewed 0 times
valuesenummap
Problem
I want to map values of a enum to some other values. For instance, here I map
One particular interesting case is to map enum to a string (serialize), but I'm interested in the general case. I came out with three possibilities of doing this:
Questions
Color to Group:enum class Color {
Red, Green, Blue, Cyan, Magenta, Yellow, White, Black
};
enum class Group {
Primary, Secondary, Neutral
};One particular interesting case is to map enum to a string (serialize), but I'm interested in the general case. I came out with three possibilities of doing this:
Group GetColorGroupMap(Color color)
{
static const std::map color2group = {
{ Color::Red, Group::Primary },
{ Color::Green, Group::Primary },
{ Color::Blue, Group::Primary },
{ Color::Cyan, Group::Secondary },
{ Color::Magenta, Group::Secondary },
{ Color::Yellow, Group::Secondary },
{ Color::White, Group::Neutral },
{ Color::Black, Group::Neutral }
};
// Shall I check the iterator here?
return color2group.find(color)->second;
}
Group GetColorGroupArr(Color color)
{
static const Group color2group[] = {
Group::Primary, // Red
Group::Primary, // Green
Group::Primary, // Blue
Group::Secondary, // Cyan
Group::Secondary, // Magenta
Group::Secondary, // Yellow
Group::Neutral, // White
Group::Neutral // Black
};
// Shall I check the index here?
return color2group[size_t(color)];
}
Group GetColorGroupSwitch(Color color)
{
switch (color)
{
case Color::Red: return Group::Primary;
case Color::Green: return Group::Primary;
case Color::Blue: return Group::Primary;
case Color::Cyan: return Group::Secondary;
case Color::Magenta: return Group::Secondary;
case Color::Yellow: return Group::Secondary;
case Color::White: return Group::Neutral;
case Color::Black: return Group::Neutral;
}
// Shall I handle this branch here?
throw std::logic_error("How did we get here?");
}Questions
- What is the best solution among these and why? Maybe there are others?
- Shall I do the checks, mentioned in each function?
- Is there a way to make these functions robust
Solution
Each of these three are obviously valid, but they also have their problems.
Possibility 1:
This, in my opinion, is the best option as it explicitly matches a color with a group. I have no experience with maps in C++, so I don't know how slow it is, but for this small of a data set, I do not think this will be an issue.
Possibility 2:
I do not recommend this option. It does not show any correlation between the two groups, and it will be easy to break by accidentally adding any updates to the wrong location.
Possibility 3:
This is also a valid option, but I would only use it if the color can be used independent of the group and you need to pair/convert them only in certain uses. Also, this code can be cleaned up a little:
Edit:
Oops, I missed the three questions.
Possibility 1:
This, in my opinion, is the best option as it explicitly matches a color with a group. I have no experience with maps in C++, so I don't know how slow it is, but for this small of a data set, I do not think this will be an issue.
Possibility 2:
I do not recommend this option. It does not show any correlation between the two groups, and it will be easy to break by accidentally adding any updates to the wrong location.
Possibility 3:
This is also a valid option, but I would only use it if the color can be used independent of the group and you need to pair/convert them only in certain uses. Also, this code can be cleaned up a little:
switch (color)
{
case Color::Red:
case Color::Green:
case Color::Blue:
return Group::Primary;
case Color::Cyan:
case Color::Magenta:
case Color::Yellow:
return Group::Secondary;
case Color::White:
case Color::Black:
return Group::Neutral;
}Edit:
Oops, I missed the three questions.
- I think the top item is the best.
- It would not be a bad idea to do the tests, especially if this code will eventually be maintained by others. However, if it is one-time code that will never be touched after it is tested, it may not be necessary.
- I do not know of any way to make the tests more robust. However, I think the first and third are the least likely to be messed up when updating because it is immediately obvious what you are returning.
Code Snippets
switch (color)
{
case Color::Red:
case Color::Green:
case Color::Blue:
return Group::Primary;
case Color::Cyan:
case Color::Magenta:
case Color::Yellow:
return Group::Secondary;
case Color::White:
case Color::Black:
return Group::Neutral;
}Context
StackExchange Code Review Q#80754, answer score: 2
Revisions (0)
No revisions yet.