Commit 61b101d4 authored by lakedaemon's avatar lakedaemon

sharing namespace_dir and the namespace string methods

parent cd1493b0
......@@ -21,15 +21,31 @@ 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";
}
......@@ -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
......
......@@ -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;
......
......@@ -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);
}
};
......
......@@ -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);
}
};
......
......@@ -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);
}
};
......
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