I am looking to only print out the "grouped" enums but having trouble getting the expected behaviour. So basically printing out all the enums from the specified base Values
until there isn't any subsequent consecutive enum value. Each "group" of enum can be determined by ANDing with a mask 0xFFFF0000
The trick is I could iterate over _map
enum but then there won't be an easy way to check whether the corresponding key exists. find
method takes a key so that won't help.
P.S: _map
already exists for 'other' purposes so I can't change that
enum class Values : uint32_t
{
one = 0x00000000,
oneOne = 0x00000001,
oneTwo = 0x00000002,
two = 0x00010000,
twoOne = 0x00010001,
twoTwo = 0x00010002,
three = 0x00020000,
threeOne = 0x00020001,
threeTwo = 0x00020002,
//...
MAX
};
std::unordered_map<std::string, Values> _map =
{
{"one", Values::one},
{"oneOne", Values::oneOne},
{"oneTwo", Values::oneTwo},
{"two", Values::two},
{"twoOne", Values::twoOne},
{"twoTwo", Values::twoTwo}
};
What I came up with is the following but there isn't a way to "break" where the enum value doesn't exist.
void foo(Values base)
{
uint32_t mask = static_cast<uint32_t>(base) & 0xffff0000;
for (Values i = base; i < Values::MAX; i = static_cast<Values>(static_cast<uint32_t>(i) + 1))
{
uint32_t curMask = static_cast<uint32_t>(i) & 0xffff0000;
if (curMask != mask)
{
break; // stop if we've reached a different upper 16 bits value
}
std::cout << std::hex << static_cast<uint32_t>(i) << "\n";
}
}
// expected calls with expected output
foo(Values::one); // should print: one, oneOne, oneTwo
foo(Values::oneOne); // should print: oneOne, oneTwo
foo(Values::twoTwo); // should print: twoTwo