Generate a C++ function for EnumValues{{ENUM_NAME}} (#4337)
* enables "for each" logic on enumeration types
This commit is contained in:
parent
01c50d57a6
commit
dadd1a926e
|
@ -25,6 +25,15 @@ enum Color {
|
|||
Color_MAX = Color_Blue
|
||||
};
|
||||
|
||||
inline Color (&EnumValuesColor())[3] {
|
||||
static Color values[] = {
|
||||
Color_Red,
|
||||
Color_Green,
|
||||
Color_Blue
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = {
|
||||
"Red",
|
||||
|
@ -47,6 +56,14 @@ enum Equipment {
|
|||
Equipment_MAX = Equipment_Weapon
|
||||
};
|
||||
|
||||
inline Equipment (&EnumValuesEquipment())[2] {
|
||||
static Equipment values[] = {
|
||||
Equipment_NONE,
|
||||
Equipment_Weapon
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesEquipment() {
|
||||
static const char *names[] = {
|
||||
"NONE",
|
||||
|
|
|
@ -553,7 +553,9 @@ class CppGenerator : public BaseGenerator {
|
|||
(inclass ? " = nullptr" : "") + ") const";
|
||||
}
|
||||
|
||||
// Generate an enum declaration and an enum string lookup table.
|
||||
// Generate an enum declaration,
|
||||
// an enum string lookup table,
|
||||
// and an enum array of values
|
||||
void GenEnum(const EnumDef &enum_def) {
|
||||
code_.SetValue("ENUM_NAME", enum_def.name);
|
||||
code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false));
|
||||
|
@ -612,6 +614,22 @@ class CppGenerator : public BaseGenerator {
|
|||
}
|
||||
code_ += "";
|
||||
|
||||
// Generate an array of all enumeration values
|
||||
auto num_fields = NumToString(enum_def.vals.vec.size());
|
||||
code_ += "inline {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" + num_fields + "] {";
|
||||
code_ += " static {{ENUM_NAME}} values[] = {";
|
||||
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
|
||||
++it) {
|
||||
const auto &ev = **it;
|
||||
auto value = GetEnumValUse(enum_def, ev);
|
||||
auto suffix = *it != enum_def.vals.vec.back() ? "," : "";
|
||||
code_ += " " + value + suffix;
|
||||
}
|
||||
code_ += " };";
|
||||
code_ += " return values;";
|
||||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
// Generate a generate string table for enum values.
|
||||
// Problem is, if values are very sparse that could generate really big
|
||||
// tables. Ideally in that case we generate a map lookup instead, but for
|
||||
|
|
Binary file not shown.
|
@ -39,6 +39,15 @@ enum Color {
|
|||
Color_ANY = 11
|
||||
};
|
||||
|
||||
inline Color (&EnumValuesColor())[3] {
|
||||
static Color values[] = {
|
||||
Color_Red,
|
||||
Color_Green,
|
||||
Color_Blue
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesColor() {
|
||||
static const char *names[] = {
|
||||
"Red",
|
||||
|
@ -68,6 +77,16 @@ enum Any {
|
|||
Any_MAX = Any_MyGame_Example2_Monster
|
||||
};
|
||||
|
||||
inline Any (&EnumValuesAny())[4] {
|
||||
static Any values[] = {
|
||||
Any_NONE,
|
||||
Any_Monster,
|
||||
Any_TestSimpleTableWithEnum,
|
||||
Any_MyGame_Example2_Monster
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesAny() {
|
||||
static const char *names[] = {
|
||||
"NONE",
|
||||
|
|
|
@ -21,6 +21,15 @@ enum EnumInNestedNS {
|
|||
EnumInNestedNS_MAX = EnumInNestedNS_C
|
||||
};
|
||||
|
||||
inline EnumInNestedNS (&EnumValuesEnumInNestedNS())[3] {
|
||||
static EnumInNestedNS values[] = {
|
||||
EnumInNestedNS_A,
|
||||
EnumInNestedNS_B,
|
||||
EnumInNestedNS_C
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesEnumInNestedNS() {
|
||||
static const char *names[] = {
|
||||
"A",
|
||||
|
|
|
@ -28,6 +28,19 @@ enum Character {
|
|||
Character_MAX = Character_Unused
|
||||
};
|
||||
|
||||
inline Character (&EnumValuesCharacter())[7] {
|
||||
static Character values[] = {
|
||||
Character_NONE,
|
||||
Character_MuLan,
|
||||
Character_Rapunzel,
|
||||
Character_Belle,
|
||||
Character_BookFan,
|
||||
Character_Other,
|
||||
Character_Unused
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char **EnumNamesCharacter() {
|
||||
static const char *names[] = {
|
||||
"NONE",
|
||||
|
|
Loading…
Reference in New Issue