Evo has helpers for efficiently converting between strings and enums.
- Enum string list is built at compile-time – doesn't allocate memory
- Lookups are fast – binary search is used to find the enum string in a pre-sorted list with SubStringMapList
- This is done using EVO_ENUM_MAP_PREFIXED() or EVO_ENUM_CLASS_MAP(), or a related variant
- Caution: This requires string values to be pre-sorted, and no gaps between enum values
Additionally, these helpers are useful for mapping enums to "trait" objects:
This example defines an enum, then uses the EVO_ENUM_MAP_PREFIXED() helper to create string mappings.
- This requires the enum to have the expected first/last guard values (
UNKNOWN
, ENUM_END
– with a prefix where applicable) to deduce the mappings
- These enum values have a lowercase prefix to avoid collisions with other enums – referred to as a "prefixed" enum
- The enum map also has a nested
Iter
type for iterating through enum values
enum Color {
cUNKNOWN = 0,
cBLUE,
cGREEN,
cRED,
cENUM_END
};
"blue",
"green",
"red"
);
int main() {
Color color_val1 = ColorEnum::get_enum("green");
Color color_val2 = ColorEnum::get_enum(2);
SubString color_str = ColorEnum::get_string(cGREEN);
int color_num = ColorEnum::get_int(cGREEN);
for (ColorEnum::Iter iter; iter; ++iter)
c.
out <<
"num:" << iter.value_num() <<
", str:" << iter.value_str() <<
NL;
return 0;
}
Output (from iterator):
num:1, str:blue
num:2, str:green
num:3, str:red
- Unsorted Enum Values
This example is similar but uses an enum with unsorted values, which must be "remapped" to match the sorted string values.
- This is useful when the enum values must be in a specific order that doesn't match the pre-sorted strings
enum Color {
cUNKNOWN = 0,
cRED,
cBLUE,
cGREEN,
cENUM_END
};
static const Color COLOR_REMAP[] = {
cBLUE,
cGREEN,
cRED
};
"blue",
"green",
"red"
);
int main() {
Color color_val1 = ColorEnum::get_enum("green");
Color color_val2 = ColorEnum::get_enum(2);
SubString color_str = ColorEnum::get_string(cGREEN);
int color_num = ColorEnum::get_int(cGREEN);
return 0;
}
- Enum Traits
This example creates traits for the above Color
enum using EVO_ENUM_TRAITS():
- This requires the enum to have the expected first guard value (
UNKNOWN
– with prefix where applicable) to deduce the mappings
- The above enum map example and the below enum traits example may be combined on the same enum, creating
ColorEnum
and ColorEnumTraits
helper types
enum Color {
cUNKNOWN = 0,
cBLUE,
cGREEN,
cRED,
cENUM_END
};
struct ColorTraits {
int index;
};
{ 0, "?" },
{ 1, "B" },
{ 2, "G" },
{ 3, "R" }
);
int main() {
const ColorTraits& unknown_traits = ColorEnumTraits::get(cUNKNOWN);
const ColorTraits& green_traits = ColorEnumTraits::get(cGREEN);
return 0;
}
- Enum Class (C++11)
This is simplied further with C++11 enum class and EVO_ENUM_CLASS_MAP(), as long as the enum has the expected first/last guard values (UNKNOWN
, ENUM_END
).
enum class Color {
UNKNOWN = 0,
BLUE,
GREEN,
RED,
ENUM_END
};
"blue",
"green",
"red"
);
int main() {
Color color_val1 = ColorEnum::get_enum("green");
Color color_val2 = ColorEnum::get_enum(2);
SubString color_str = ColorEnum::get_string(Color::GREEN);
int color_num = ColorEnum::get_int(Color::GREEN);
for (ColorEnum::Iter iter; iter; ++iter)
c.
out <<
"num:" << iter.value_num() <<
", str:" << iter.value_str() <<
NL;
return 0;
}
Output (from iterator):
num:1, str:blue
num:2, str:green
num:3, str:red
- Unsorted Enum Class Values (C++11)
This example is similar but uses an enum class with unsorted values, which must be "remapped" to match the sorted string values.
- This is useful when the enum values must be in a specific order that doesn't match the pre-sorted strings
enum class Color {
UNKNOWN = 0,
RED,
BLUE,
GREEN,
ENUM_END
};
static const Color COLOR_REMAP[] = {
Color::BLUE,
Color::GREEN,
Color::RED
};
"blue",
"green",
"red"
);
int main() {
Color color_val1 = ColorEnum::get_enum("green");
Color color_val2 = ColorEnum::get_enum(2);
SubString color_str = ColorEnum::get_string(Color::GREEN);
int color_num = ColorEnum::get_int(Color::GREEN);
return 0;
}
- Enum Class Traits (C++11)
This example creates traits for the above Color
enum class using EVO_ENUM_CLASS_TRAITS():
- This requires the enum class to have the expected first guard value (
UNKNOWN
) to deduce the mappings
- The above enum class map example and the below enum traits example may be combined on the same enum, creating
ColorEnum
and ColorEnumTraits
helper types
enum class Color {
UNKNOWN = 0,
BLUE,
GREEN,
RED,
ENUM_END
};
struct ColorTraits {
int index;
};
{ 0, "?" },
{ 1, "B" },
{ 2, "G" },
{ 3, "R" }
);
int main() {
const ColorTraits& unknown_traits = ColorEnumTraits::get(Color::UNKNOWN);
const ColorTraits& green_traits = ColorEnumTraits::get(Color::GREEN);
return 0;
}