From 4a8801da34b0edd03277b26b6fd280658c585ca6 Mon Sep 17 00:00:00 2001 From: Lakedaemon Date: Wed, 25 May 2016 15:15:56 +0200 Subject: [PATCH] shared method that exits early if everything is generated --- include/flatbuffers/code_generators.h | 12 +++++++++++ src/idl_gen_cpp.cpp | 16 +------------- src/idl_gen_js.cpp | 30 +++++++++++++-------------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/include/flatbuffers/code_generators.h b/include/flatbuffers/code_generators.h index 6b27ea780..5770932cc 100644 --- a/include/flatbuffers/code_generators.h +++ b/include/flatbuffers/code_generators.h @@ -29,6 +29,18 @@ class BaseGenerator { protected: virtual ~BaseGenerator(){}; + bool IsEverythingGenerated() { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + if (!(*it)->generated) return false; + } + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + if (!(*it)->generated) return false; + } + return true; + } + const Parser &parser_; const std::string &path_; const std::string &file_name_; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 0e5d823f3..33024df1c 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -724,21 +724,7 @@ class CppGenerator : public BaseGenerator { // structs, // and tables) and output them to a single file. bool generate() { - // Check if we have any code to generate at all, to avoid an empty header. - for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); - ++it) { - if (!(*it)->generated) goto generate_code; - } - for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); - ++it) { - if (!(*it)->generated) goto generate_code; - } - // No code to generate, exit: - return true; - - generate_code: - - using namespace cpp; + if (IsEverythingGenerated()) return true; std::string code; code = diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index de81f2764..0265394ed 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -678,31 +678,29 @@ class JsGenerator : public BaseGenerator { // Iterate through all definitions we haven't generate code for (enums, // structs, and tables) and output them to a single file. bool generate() { + if (IsEverythingGenerated()) return true; + std::string enum_code, struct_code, exports_code, code; generateEnums(&enum_code, &exports_code); generateStructs(&struct_code, &exports_code); - // Only output file-level code if there were any declarations. - if (enum_code.length() || struct_code.length()) { - code += - "// automatically generated by the FlatBuffers compiler, do not " - "modify\n\n"; + code += + "// automatically generated by the FlatBuffers compiler, do not " + "modify\n\n"; - // Generate code for all the namespace declarations. - GenNamespaces(parser_, &code, &exports_code); + // Generate code for all the namespace declarations. + GenNamespaces(parser_, &code, &exports_code); - // Output the main declaration code from above. - code += enum_code; - code += struct_code; + // Output the main declaration code from above. + code += enum_code; + code += struct_code; - if (!exports_code.empty() && !parser_.opts.skip_js_exports) { - code += "// Exports for Node.js and RequireJS\n"; - code += exports_code; - } + if (!exports_code.empty() && !parser_.opts.skip_js_exports) { + code += "// Exports for Node.js and RequireJS\n"; + code += exports_code; } - return !code.length() || - SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); + return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); } private: