mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-05 13:08:58 +00:00
sharing namespace_dir and the namespace string methods
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user