From dadd1a926ee75cad9d9ef792edcbc902b1f1744a Mon Sep 17 00:00:00 2001 From: rufeooo Date: Wed, 7 Jun 2017 13:49:56 -0700 Subject: [PATCH] Generate a C++ function for EnumValues{{ENUM_NAME}} (#4337) * enables "for each" logic on enumeration types --- samples/monster_generated.h | 17 +++++++++++++++ src/idl_gen_cpp.cpp | 20 +++++++++++++++++- tests/monster_test.bfbs | Bin 4288 -> 4320 bytes tests/monster_test_generated.h | 19 +++++++++++++++++ .../namespace_test1_generated.h | 9 ++++++++ tests/union_vector/union_vector_generated.h | 13 ++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) 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 78871d504af00b89b914a99875abdce6801b38b7..516b764ef2ffb9e3aae7c083ed55f57bafe5ea12 100644 GIT binary patch literal 4320 zcmai2TWC~A82&fcO|rSDX-qXOM+qV#izXpR5Ye>WiYBG8wuqRX?4ETu?4GlnJ)4>$ zc?=>&N+~{+B7G=Q`c!=Cg9H&NrSzc>r4L1X@X3cF_GW7@ZohBlO!jzoX$Swz%s=z} zxB2IvvyO3wO z=R^G1{7KKP1db0oMqtN)vXwSb-o;)m5ALnEzMqwi{frCcopLI{T!FTnvKNZZ8-*Z$ z*sYczk5R76v#|FT{Gn{+Ao??*EY3SYbz4$lOn6H57+rf>t5NUF?95?wvKxNyK_8Ff z4K25oav6(vL2rU~gLuZV7{25cJs<4QW@^-WhcRM2nG>dMhfdlsU939f-Nl?Ti866I z0$w}jM}5a})!HEZsmnF{O&pf6pLV|HWTB6Hsge5!l>VI<#eKZ`i#mrc>ZBaz@HSBB z+iZ|`{264*T-nYz=_9^ZI__qQ)hvxpA?xWw+)t72kk65(5OvxvVh)|(fPVr+os?nv zy%<}zYdJbGt*n=+mK-;*gM#N;8L!02l!YtBbhEsLMJE#g^CD~GG9^PZ(a^o1t7TCvJZ4>gDSGo;g zg{%9-LT~a-uO$z%e;Ahs-9%XbA@6yaRCU4J3wL$vqbJ+C4id>W{hPKCN26^-|0Q_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UvaTmSZ zWHl_m8Z7$tJPoE_4(RT$x)&YR#w=vhFY5!fIiRhe1U={anBa}!Gi!Sx$HtqoKL+gr zt&6waM%jzC{YajY?tQEQV!H zjygD=JO)mp)EW#Ta+u z)R}e{_yL?|?xF*t44&&WKB|_2b0Z?3K21ImvB`tB;N}^z8*NAX);hV%&?}-&9`; zH)n9jYMeQYcglfha5gS!|HymU;MBj)KjOFt6)mkVjkEp{ciZ69ze(VB$l#_p>mPBe z2B-dA0`+XndPGl(cklz8( zaPoWQN`&i8F8!(UyA*(VKjzy}MTp_~83Hk1i3y0y7koEbs22*jz|bE4LwqNyhR7VS zyvK;O=Yu)y1zwrDeoAy@FlJl9d>c*x`5h2-*}O{5d&CQliJZf^<5?$`w$gVh<@bYA zuKQ8RF9n_xg(2!G-~E66iHfC~^LEMi9WTf`^@>vp9F%tF9PF*S@D!y=YkKZU9B)Mg zt(}a^Iy+I3P*?DN6?Y`8(z;Rrt^?JYIND2`J=0^T^I24bHte7#A$&s#)pixQc0@28 zNBYV=Aj62(F1@0Z|2y!vq0*6m{XWk}%M}mr3kG9DFXv%rD@53RqHUU-=xO6(a)Dnp zxW;{b6!R^JdO8-&sZy|nq8?V4;Z3{xW$rJV8>R;Go6q{IDGmL7S_2^>!#kVj&eqk} zF{Vw0Y6&~!GlgW**waKuXoIP$?aD`9`oq3bErr#Rrnr#z5XiKC-N`jOB(b(Pu_Kdd z*1a*Z7*EDNzr27j2|2lSlO+!g$r##p_QlftS8myU0(wmhb^I zXzbGZ%6Rl9G8IU$HWQDuZytQ32Y5%S5gT`K!v6`}dNRg;( z=#PLl*qb?TaTHNC`!a=|K0g560(}5tdkUFbU>kLFoo9{v#M&~7{<79DY1E>e|6=3YC9 zo_lPYIUD>*;yu8;uys)yvE1&(r1u 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",