From 61b101d4429748e4723d384e5dbf69c451a32282 Mon Sep 17 00:00:00 2001 From: lakedaemon Date: Sat, 28 May 2016 08:15:43 +0200 Subject: [PATCH] sharing namespace_dir and the namespace string methods --- include/flatbuffers/code_generators.h | 46 +++++++++++++--- src/idl_gen_general.cpp | 76 ++++++++------------------- src/idl_gen_go.cpp | 16 +----- src/idl_gen_php.cpp | 18 +------ src/idl_gen_python.cpp | 13 ++--- 5 files changed, 67 insertions(+), 102 deletions(-) diff --git a/include/flatbuffers/code_generators.h b/include/flatbuffers/code_generators.h index 6c902a454..a832334ec 100644 --- a/include/flatbuffers/code_generators.h +++ b/include/flatbuffers/code_generators.h @@ -21,19 +21,35 @@ namespace flatbuffers { class BaseGenerator { public: - BaseGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : parser_(parser), path_(path), file_name_(file_name){}; virtual bool generate() = 0; + static const std::string NamespaceDir(const Parser &parser, + const std::string &path) { + EnsureDirExists(path.c_str()); + if (parser.opts.one_file) return path; + std::string namespace_dir = path; // Either empty or ends in separator. + auto &namespaces = parser.namespaces_.back()->components; + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { + namespace_dir += *it + kPathSeparator; + EnsureDirExists(namespace_dir.c_str()); + } + return namespace_dir; + } + protected: + BaseGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : parser_(parser), + path_(path), + file_name_(file_name), + namespace_dir_(BaseGenerator::NamespaceDir(parser, path)){}; virtual ~BaseGenerator(){}; - const char * FlatBuffersGeneratedWarning() { + const char *FlatBuffersGeneratedWarning() { return "automatically generated by the FlatBuffers compiler," - " do not modify\n\n"; - } - + " do not modify\n\n"; + } + bool IsEverythingGenerated() { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { @@ -46,9 +62,25 @@ class BaseGenerator { return true; } + std::string FullNamespace(const char *separator) { + std::string namespace_name; + auto &namespaces = parser_.namespaces_.back()->components; + for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { + if (namespace_name.length()) namespace_name += separator; + namespace_name += *it; + } + return namespace_name; + } + + const std::string LastNamespacePart() { + auto &namespaces = parser_.namespaces_.back()->components; + if (namespaces.size()) return *(namespaces.end() - 1); else return std::string(""); + } + const Parser &parser_; const std::string &path_; const std::string &file_name_; + const std::string namespace_dir_; }; } // namespace flatbuffers diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 71ea19ed1..a0b837a20 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -1133,7 +1133,7 @@ class GeneralGenerator : public BaseGenerator { if (parser_.opts.one_file) { one_file_code += enumcode; } else { - if (!SaveType(lang, (**it).name, enumcode, false, false)) return false; + if (!SaveType(lang, (**it).name, enumcode, false)) return false; } } @@ -1144,12 +1144,12 @@ class GeneralGenerator : public BaseGenerator { if (parser_.opts.one_file) { one_file_code += declcode; } else { - if (!SaveType(lang, (**it).name, declcode, true, false)) return false; + if (!SaveType(lang, (**it).name, declcode, true)) return false; } } if (parser_.opts.one_file) { - return SaveType(lang, file_name_, one_file_code, true, true); + return SaveType(lang, file_name_, one_file_code, true); } return true; } @@ -1157,34 +1157,20 @@ class GeneralGenerator : public BaseGenerator { // Save out the generated code for a single class while adding // declaration boilerplate. bool SaveType(const LanguageParameters &lang, const std::string &defname, - const std::string &classcode, bool needs_includes, - bool onefile) { + const std::string &classcode, bool needs_includes) { if (!classcode.length()) return true; - std::string namespace_general; - std::string namespace_dir = path_; // Either empty or ends in separator. - auto &namespaces = parser_.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_general.length()) { - namespace_general += "."; - } - namespace_general += *it; - if (!onefile) { - namespace_dir += *it + kPathSeparator; - } - } - EnsureDirExists(namespace_dir); - std::string code; code = code + "// " + FlatBuffersGeneratedWarning(); - if (!namespace_general.empty()) { - code += lang.namespace_ident + namespace_general + lang.namespace_begin; + std::string namespace_name = FullNamespace("."); + if (!namespace_name.empty()) { + code += lang.namespace_ident + namespace_name + lang.namespace_begin; code += "\n\n"; } if (needs_includes) code += lang.includes; code += classcode; - if (!namespace_general.empty()) code += lang.namespace_end; - auto filename = namespace_dir + defname + lang.file_extension; + if (!namespace_name.empty()) code += lang.namespace_end; + auto filename = namespace_dir_ + defname + lang.file_extension; return SaveFile(filename.c_str(), code, false); } }; @@ -1196,50 +1182,30 @@ bool GenerateGeneral(const Parser &parser, const std::string &path, return generator.generate(); } -static std::string ClassFileName(const LanguageParameters &lang, - const Parser &parser, const Definition &def, - const std::string &path) { - std::string namespace_general; - std::string namespace_dir = path; - auto &namespaces = parser.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_general.length()) { - namespace_general += "."; - namespace_dir += kPathSeparator; - } - namespace_general += *it; - namespace_dir += *it; - } - - return namespace_dir + kPathSeparator + def.name + lang.file_extension; -} - -std::string GeneralMakeRule(const Parser &parser, - const std::string &path, +std::string GeneralMakeRule(const Parser &parser, const std::string &path, const std::string &file_name) { assert(parser.opts.lang <= IDLOptions::kMAX); auto lang = language_parameters[parser.opts.lang]; std::string make_rule; + std::string directory = + BaseGenerator::NamespaceDir(parser, path) + kPathSeparator; - for (auto it = parser.enums_.vec.begin(); - it != parser.enums_.vec.end(); ++it) { - if (make_rule != "") - make_rule += " "; - make_rule += ClassFileName(lang, parser, **it, path); + for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); + ++it) { + if (make_rule != "") make_rule += " "; + make_rule += directory + (**it).name + lang.file_extension; } - for (auto it = parser.structs_.vec.begin(); - it != parser.structs_.vec.end(); ++it) { - if (make_rule != "") - make_rule += " "; - make_rule += ClassFileName(lang, parser, **it, path); + for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); + ++it) { + if (make_rule != "") make_rule += " "; + make_rule += directory + (**it).name + lang.file_extension; } make_rule += ": "; auto included_files = parser.GetIncludedFilesRecursive(file_name); - for (auto it = included_files.begin(); - it != included_files.end(); ++it) { + for (auto it = included_files.begin(); it != included_files.end(); ++it) { make_rule += " " + *it; } return make_rule; diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index be437c7b0..5841e121c 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -663,22 +663,10 @@ class GoGenerator : public BaseGenerator { bool needs_imports) { if (!classcode.length()) return true; - std::string namespace_name; - std::string namespace_dir = path_; // Either empty or ends in separator. - auto &namespaces = parser_.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "."; - } - namespace_name = *it; - namespace_dir += *it + kPathSeparator; - } - EnsureDirExists(namespace_dir); - std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); + BeginFile(LastNamespacePart(), needs_imports, &code); code += classcode; - std::string filename = namespace_dir + def.name + ".go"; + std::string filename = namespace_dir_ + def.name + ".go"; return SaveFile(filename.c_str(), code, false); } }; diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 5fb412434..599d5571d 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -984,26 +984,12 @@ namespace php { bool needs_imports) { if (!classcode.length()) return true; - std::string namespace_name; - std::string namespace_dir = path_; - - auto &namespaces = parser_.namespaces_.back()->components; - for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "\\"; - namespace_dir += kPathSeparator; - } - namespace_name += *it; - namespace_dir += *it; - EnsureDirExists(namespace_dir.c_str()); - } - std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); + BeginFile(FullNamespace("\\"), needs_imports, &code); code += classcode; std::string filename = - namespace_dir + kPathSeparator + def.name + ".php"; + namespace_dir_ + kPathSeparator + def.name + ".php"; return SaveFile(filename.c_str(), code, false); } }; diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 291aada0e..7b8d168c1 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -642,26 +642,19 @@ class PythonGenerator : public BaseGenerator { bool needs_imports) { if (!classcode.length()) return true; - std::string namespace_name; std::string namespace_dir = path_; auto &namespaces = parser_.namespaces_.back()->components; for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { - if (namespace_name.length()) { - namespace_name += "."; - namespace_dir += kPathSeparator; - } - namespace_name = *it; + if (it != namespaces.begin()) namespace_dir += kPathSeparator; namespace_dir += *it; - EnsureDirExists(namespace_dir.c_str()); - std::string init_py_filename = namespace_dir + "/__init__.py"; SaveFile(init_py_filename.c_str(), "", false); } std::string code = ""; - BeginFile(namespace_name, needs_imports, &code); + BeginFile(LastNamespacePart(), needs_imports, &code); code += classcode; - std::string filename = namespace_dir + kPathSeparator + def.name + ".py"; + std::string filename = namespace_dir_ + kPathSeparator + def.name + ".py"; return SaveFile(filename.c_str(), code, false); } };