From c02b16e19590bb53baf0482d254109030442eaed Mon Sep 17 00:00:00 2001 From: vijairaj Date: Tue, 15 Sep 2015 13:41:29 +0530 Subject: [PATCH] Implemented option --scoped-enum for C++ generator This generates C++11 style scoped and strongly typed enums. Enabling this option also implies --no-prefix. --- docs/html/md__compiler.html | 1 + docs/source/Compiler.md | 3 +++ include/flatbuffers/idl.h | 3 ++- src/flatc.cpp | 5 +++++ src/idl_gen_cpp.cpp | 26 +++++++++++++++++++++----- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/docs/html/md__compiler.html b/docs/html/md__compiler.html index 043cb23b0..6ae87ed39 100644 --- a/docs/html/md__compiler.html +++ b/docs/html/md__compiler.html @@ -78,6 +78,7 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
  • --strict-json : Require & generate strict JSON (field names are enclosed in quotes, no trailing commas in tables/vectors). By default, no quotes are required/generated, and trailing commas are allowed.
  • --defaults-json : Output fields whose value is equal to the default value when writing JSON text.
  • --no-prefix : Don't prefix enum values in generated C++ by their enum type.
  • +
  • --scoped-enums : Use C++11 style scoped and strongly typed enums in generated C++. This also implies --no-prefix.
  • --gen-includes : (deprecated), this is the default behavior. If the original behavior is required (no include statements) use --no-includes.
  • --no-includes : Don't generate include statements for included schemas the generated file depends on (C++).
  • --gen-mutable : Generate additional non-const accessors for mutating FlatBuffers in-place.
  • diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 5d254dfa5..e4663e172 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -51,6 +51,9 @@ be generated for each file processed: - `--no-prefix` : Don't prefix enum values in generated C++ by their enum type. +- `--scoped-enums` : Use C++11 style scoped and strongly typed enums in + generated C++. This also implies `--no-prefix`. + - `--gen-includes` : (deprecated), this is the default behavior. If the original behavior is required (no include statements) use `--no-includes.` diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 8a912de5c..44154ebac 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -425,6 +425,7 @@ struct GeneratorOptions { int indent_step; bool output_enum_identifiers; bool prefixed_enums; + bool scoped_enums; bool include_dependence_headers; bool mutable_buffer; bool one_file; @@ -437,7 +438,7 @@ struct GeneratorOptions { GeneratorOptions() : strict_json(false), output_default_scalars_in_json(false), indent_step(2), - output_enum_identifiers(true), prefixed_enums(true), + output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false), include_dependence_headers(true), mutable_buffer(false), one_file(false), diff --git a/src/flatc.cpp b/src/flatc.cpp index 9b8b50d70..7f654b0ba 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -90,6 +90,8 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " --defaults-json Output fields whose value is the default when\n" " writing JSON\n" " --no-prefix Don\'t prefix enum values with the enum type in C++.\n" + " --scoped-enums Use C++11 style scoped and strongly typed enums.\n" + " also implies --no-prefix.\n" " --gen-includes (deprecated), this is the default behavior.\n" " If the original behavior is required (no include\n" " statements) use --no-includes.\n" @@ -142,6 +144,9 @@ int main(int argc, const char *argv[]) { opts.output_default_scalars_in_json = true; } else if(arg == "--no-prefix") { opts.prefixed_enums = false; + } else if(arg == "--scoped-enums") { + opts.prefixed_enums = false; + opts.scoped_enums = true; } else if(arg == "--gen-mutable") { opts.mutable_buffer = true; } else if(arg == "--gen-includes") { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 2cb15b56c..acdc63040 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -124,12 +124,28 @@ static std::string GenTypeGet(const Parser &parser, const Type &type, : beforeptr + GenTypePointer(parser, type) + afterptr; } +static std::string GenEnumDecl(const EnumDef &enum_def, + const GeneratorOptions &opts) { + return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name; +} + static std::string GenEnumVal(const EnumDef &enum_def, const EnumVal &enum_val, const GeneratorOptions &opts) { return opts.prefixed_enums ? enum_def.name + "_" + enum_val.name : enum_val.name; } +static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val, + const GeneratorOptions &opts) { + if (opts.scoped_enums) { + return enum_def.name + "::" + enum_val.name; + } else if (opts.prefixed_enums) { + return enum_def.name + "_" + enum_val.name; + } else { + return enum_val.name; + } +} + // Generate an enum declaration and an enum string lookup table. static void GenEnum(const Parser &parser, EnumDef &enum_def, std::string *code_ptr, std::string *code_ptr_post, @@ -138,7 +154,7 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, std::string &code = *code_ptr; std::string &code_post = *code_ptr_post; GenComment(enum_def.doc_comment, code_ptr, nullptr); - code += "enum " + enum_def.name + " {\n"; + code += GenEnumDecl(enum_def, opts) + " {\n"; for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); ++it) { @@ -171,9 +187,9 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, } code += "nullptr };\n return names;\n}\n\n"; code += "inline const char *EnumName" + enum_def.name; - code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name + "()[e"; + code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name + "()[static_cast(e)"; if (enum_def.vals.vec.front()->value) - code += " - " + GenEnumVal(enum_def, *enum_def.vals.vec.front(), opts); + code += " - static_cast(" + GetEnumVal(enum_def, *enum_def.vals.vec.front(), opts) +")"; code += "]; }\n\n"; } @@ -192,7 +208,7 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, it != enum_def.vals.vec.end(); ++it) { auto &ev = **it; - code_post += " case " + GenEnumVal(enum_def, ev, opts); + code_post += " case " + GetEnumVal(enum_def, ev, opts); if (!ev.value) { code_post += ": return true;\n"; // "NONE" enum value. } else { @@ -425,7 +441,7 @@ static void GenTable(const Parser &parser, StructDef &struct_def, if (ev) { code += WrapInNameSpace(parser, field.value.type.enum_def->defined_namespace, - GenEnumVal(*field.value.type.enum_def, *ev, + GetEnumVal(*field.value.type.enum_def, *ev, opts)); } else { code += GenUnderlyingCast(parser, field, true, field.value.constant);