diff --git a/samples/monster_generated.h b/samples/monster_generated.h index fff866e1f..f4bdbb341 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -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", diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index cbbd1ffcb..ce5932b8d 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -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 diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index 78871d504..516b764ef 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index fc43f5d43..6d6868f32 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -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", diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 2a71fbcad..796d9c2a9 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -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", diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index c871a28a2..98f96615c 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -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",