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: