From 0d1559bdd4eb248c4f5505ce98a48aa3cdff6cd0 Mon Sep 17 00:00:00 2001 From: cubeleo <41136688+cubeleo@users.noreply.github.com> Date: Fri, 27 Jul 2018 08:32:19 -0700 Subject: [PATCH] For sparse enums, use a switch statement to generate EnumNameXXX(). (#4845) --- src/idl_gen_cpp.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 5db8e5a..b8f1495 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -983,6 +983,23 @@ class CppGenerator : public BaseGenerator { code_ += " return EnumNames{{ENUM_NAME}}()[index];"; code_ += "}"; code_ += ""; + } else { + code_ += "inline const char *EnumName{{ENUM_NAME}}({{ENUM_NAME}} e) {"; + + code_ += " switch (e) {"; + + for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + ++it) { + const auto &ev = **it; + code_ += " case {{ENUM_NAME}}_" + Name(ev) + ": return \"" + + Name(ev) + "\";"; + } + + code_ += " default: return \"\";"; + code_ += " }"; + + code_ += "}"; + code_ += ""; } // Generate type traits for unions to map from a type to union enum value. -- 2.7.4