shared method that exits early if everything is generated
This commit is contained in:
parent
6765c19d45
commit
4a8801da34
|
@ -29,6 +29,18 @@ class BaseGenerator {
|
||||||
protected:
|
protected:
|
||||||
virtual ~BaseGenerator(){};
|
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 Parser &parser_;
|
||||||
const std::string &path_;
|
const std::string &path_;
|
||||||
const std::string &file_name_;
|
const std::string &file_name_;
|
||||||
|
|
|
@ -724,21 +724,7 @@ class CppGenerator : public BaseGenerator {
|
||||||
// structs,
|
// structs,
|
||||||
// and tables) and output them to a single file.
|
// and tables) and output them to a single file.
|
||||||
bool generate() {
|
bool generate() {
|
||||||
// Check if we have any code to generate at all, to avoid an empty header.
|
if (IsEverythingGenerated()) return true;
|
||||||
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;
|
|
||||||
|
|
||||||
std::string code;
|
std::string code;
|
||||||
code =
|
code =
|
||||||
|
|
|
@ -678,31 +678,29 @@ class JsGenerator : public BaseGenerator {
|
||||||
// Iterate through all definitions we haven't generate code for (enums,
|
// Iterate through all definitions we haven't generate code for (enums,
|
||||||
// structs, and tables) and output them to a single file.
|
// structs, and tables) and output them to a single file.
|
||||||
bool generate() {
|
bool generate() {
|
||||||
|
if (IsEverythingGenerated()) return true;
|
||||||
|
|
||||||
std::string enum_code, struct_code, exports_code, code;
|
std::string enum_code, struct_code, exports_code, code;
|
||||||
generateEnums(&enum_code, &exports_code);
|
generateEnums(&enum_code, &exports_code);
|
||||||
generateStructs(&struct_code, &exports_code);
|
generateStructs(&struct_code, &exports_code);
|
||||||
|
|
||||||
// Only output file-level code if there were any declarations.
|
code +=
|
||||||
if (enum_code.length() || struct_code.length()) {
|
"// automatically generated by the FlatBuffers compiler, do not "
|
||||||
code +=
|
"modify\n\n";
|
||||||
"// automatically generated by the FlatBuffers compiler, do not "
|
|
||||||
"modify\n\n";
|
|
||||||
|
|
||||||
// Generate code for all the namespace declarations.
|
// Generate code for all the namespace declarations.
|
||||||
GenNamespaces(parser_, &code, &exports_code);
|
GenNamespaces(parser_, &code, &exports_code);
|
||||||
|
|
||||||
// Output the main declaration code from above.
|
// Output the main declaration code from above.
|
||||||
code += enum_code;
|
code += enum_code;
|
||||||
code += struct_code;
|
code += struct_code;
|
||||||
|
|
||||||
if (!exports_code.empty() && !parser_.opts.skip_js_exports) {
|
if (!exports_code.empty() && !parser_.opts.skip_js_exports) {
|
||||||
code += "// Exports for Node.js and RequireJS\n";
|
code += "// Exports for Node.js and RequireJS\n";
|
||||||
code += exports_code;
|
code += exports_code;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return !code.length() ||
|
return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
|
||||||
SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue