Prefixing of enum value identifiers in C++ is now optional.

See -P option to flatc.

Bug: 16814856
Change-Id: I855973df6afa27e0efa27cf9c4b4aee8a1fcdd22
Tested: on OS X.
parent 51ba48ae
...@@ -34,3 +34,6 @@ be generated for each file processed: ...@@ -34,3 +34,6 @@ be generated for each file processed:
- `-S` : Generate strict JSON (field names are enclosed in quotes). - `-S` : Generate strict JSON (field names are enclosed in quotes).
By default, no quotes are generated. By default, no quotes are generated.
- `-P` : Don't prefix enum values in generated C++ by their enum type.
...@@ -342,9 +342,10 @@ struct GeneratorOptions { ...@@ -342,9 +342,10 @@ struct GeneratorOptions {
bool strict_json; bool strict_json;
int indent_step; int indent_step;
bool output_enum_identifiers; bool output_enum_identifiers;
bool prefixed_enums;
GeneratorOptions() : strict_json(false), indent_step(2), GeneratorOptions() : strict_json(false), indent_step(2),
output_enum_identifiers(true) {} output_enum_identifiers(true), prefixed_enums(true) {}
}; };
// Generate text (JSON) from a given FlatBuffer, and a given Parser // Generate text (JSON) from a given FlatBuffer, and a given Parser
...@@ -361,7 +362,8 @@ extern void GenerateText(const Parser &parser, ...@@ -361,7 +362,8 @@ extern void GenerateText(const Parser &parser,
// Generate a C++ header from the definitions in the Parser object. // Generate a C++ header from the definitions in the Parser object.
// See idl_gen_cpp. // See idl_gen_cpp.
extern std::string GenerateCPP(const Parser &parser, extern std::string GenerateCPP(const Parser &parser,
const std::string &include_guard_ident); const std::string &include_guard_ident,
const GeneratorOptions &opts);
extern bool GenerateCPP(const Parser &parser, extern bool GenerateCPP(const Parser &parser,
const std::string &path, const std::string &path,
const std::string &file_name, const std::string &file_name,
......
...@@ -90,6 +90,7 @@ static void Error(const char *err, const char *obj, bool usage) { ...@@ -90,6 +90,7 @@ static void Error(const char *err, const char *obj, bool usage) {
printf(" -%s %s.\n", generators[i].extension, generators[i].help); printf(" -%s %s.\n", generators[i].extension, generators[i].help);
printf(" -o PATH Prefix PATH to all generated files.\n" printf(" -o PATH Prefix PATH to all generated files.\n"
" -S Strict JSON: add quotes to field names.\n" " -S Strict JSON: add quotes to field names.\n"
" -P Don\'t prefix enum values with the enum name in C++.\n"
"FILEs may depend on declarations in earlier files.\n" "FILEs may depend on declarations in earlier files.\n"
"FILEs after the -- must be binary flatbuffer format files.\n" "FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input," "Output files are named using the base file name of the input,"
...@@ -129,6 +130,9 @@ int main(int argc, const char *argv[]) { ...@@ -129,6 +130,9 @@ int main(int argc, const char *argv[]) {
case 'S': case 'S':
opts.strict_json = true; opts.strict_json = true;
break; break;
case 'P':
opts.prefixed_enums = false;
break;
case '-': // Separator between text and binary input files. case '-': // Separator between text and binary input files.
binary_files_from = filenames.size(); binary_files_from = filenames.size();
break; break;
......
...@@ -106,9 +106,16 @@ static void GenComment(const std::string &dc, ...@@ -106,9 +106,16 @@ static void GenComment(const std::string &dc,
} }
} }
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;
}
// Generate an enum declaration and an enum string lookup table. // Generate an enum declaration and an enum string lookup table.
static void GenEnum(EnumDef &enum_def, std::string *code_ptr, static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
std::string *code_ptr_post) { std::string *code_ptr_post,
const GeneratorOptions &opts) {
if (enum_def.generated) return; if (enum_def.generated) return;
std::string &code = *code_ptr; std::string &code = *code_ptr;
std::string &code_post = *code_ptr_post; std::string &code_post = *code_ptr_post;
...@@ -119,7 +126,7 @@ static void GenComment(const std::string &dc, ...@@ -119,7 +126,7 @@ static void GenComment(const std::string &dc,
++it) { ++it) {
auto &ev = **it; auto &ev = **it;
GenComment(ev.doc_comment, code_ptr, " "); GenComment(ev.doc_comment, code_ptr, " ");
code += " " + enum_def.name + "_" + ev.name + " = "; code += " " + GenEnumVal(enum_def, ev, opts) + " = ";
code += NumToString(ev.value); code += NumToString(ev.value);
code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n"; code += (it + 1) != enum_def.vals.vec.end() ? ",\n" : "\n";
} }
...@@ -148,7 +155,7 @@ static void GenComment(const std::string &dc, ...@@ -148,7 +155,7 @@ static void GenComment(const std::string &dc,
code += "inline const char *EnumName" + enum_def.name; code += "inline const char *EnumName" + enum_def.name;
code += "(int e) { return EnumNames" + enum_def.name + "()[e"; code += "(int e) { return EnumNames" + enum_def.name + "()[e";
if (enum_def.vals.vec.front()->value) if (enum_def.vals.vec.front()->value)
code += " - " + enum_def.name + "_" + enum_def.vals.vec.front()->name; code += " - " + GenEnumVal(enum_def, *enum_def.vals.vec.front(), opts);
code += "]; }\n\n"; code += "]; }\n\n";
} }
...@@ -167,7 +174,7 @@ static void GenComment(const std::string &dc, ...@@ -167,7 +174,7 @@ static void GenComment(const std::string &dc,
it != enum_def.vals.vec.end(); it != enum_def.vals.vec.end();
++it) { ++it) {
auto &ev = **it; auto &ev = **it;
code_post += " case " + enum_def.name + "_" + ev.name; code_post += " case " + GenEnumVal(enum_def, ev, opts);
if (!ev.value) { if (!ev.value) {
code_post += ": return true;\n"; // "NONE" enum value. code_post += ": return true;\n"; // "NONE" enum value.
} else { } else {
...@@ -440,14 +447,16 @@ void CloseNestedNameSpaces(Namespace *ns, std::string *code_ptr) { ...@@ -440,14 +447,16 @@ void CloseNestedNameSpaces(Namespace *ns, std::string *code_ptr) {
// Iterate through all definitions we haven't generate code for (enums, structs, // Iterate through all definitions we haven't generate code for (enums, structs,
// and tables) and output them to a single file. // and tables) and output them to a single file.
std::string GenerateCPP(const Parser &parser, const std::string &include_guard_ident) { std::string GenerateCPP(const Parser &parser,
const std::string &include_guard_ident,
const GeneratorOptions &opts) {
using namespace cpp; using namespace cpp;
// Generate code for all the enum declarations. // Generate code for all the enum declarations.
std::string enum_code, enum_code_post; std::string enum_code, enum_code_post;
for (auto it = parser.enums_.vec.begin(); for (auto it = parser.enums_.vec.begin();
it != parser.enums_.vec.end(); ++it) { it != parser.enums_.vec.end(); ++it) {
GenEnum(**it, &enum_code, &enum_code_post); GenEnum(**it, &enum_code, &enum_code_post, opts);
} }
// Generate forward declarations for all structs/tables, since they may // Generate forward declarations for all structs/tables, since they may
...@@ -577,8 +586,8 @@ std::string GenerateCPP(const Parser &parser, const std::string &include_guard_i ...@@ -577,8 +586,8 @@ std::string GenerateCPP(const Parser &parser, const std::string &include_guard_i
bool GenerateCPP(const Parser &parser, bool GenerateCPP(const Parser &parser,
const std::string &path, const std::string &path,
const std::string &file_name, const std::string &file_name,
const GeneratorOptions & /*opts*/) { const GeneratorOptions &opts) {
auto code = GenerateCPP(parser, file_name); auto code = GenerateCPP(parser, file_name, opts);
return !code.length() || return !code.length() ||
SaveFile((path + file_name + "_generated.h").c_str(), code, false); SaveFile((path + file_name + "_generated.h").c_str(), code, false);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment