sharing the WrapInNameSpace methods
This commit is contained in:
parent
43fedfa8ba
commit
03ad8fa4d9
|
@ -39,11 +39,15 @@ class BaseGenerator {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
BaseGenerator(const Parser &parser, const std::string &path,
|
BaseGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name,
|
||||||
|
const std::string qualifying_start,
|
||||||
|
const std::string qualifying_separator)
|
||||||
: parser_(parser),
|
: parser_(parser),
|
||||||
path_(path),
|
path_(path),
|
||||||
file_name_(file_name) {};
|
file_name_(file_name),
|
||||||
virtual ~BaseGenerator() {};
|
qualifying_start_(qualifying_start),
|
||||||
|
qualifying_separator_(qualifying_separator){};
|
||||||
|
virtual ~BaseGenerator(){};
|
||||||
|
|
||||||
// No copy/assign.
|
// No copy/assign.
|
||||||
BaseGenerator &operator=(const BaseGenerator &);
|
BaseGenerator &operator=(const BaseGenerator &);
|
||||||
|
@ -85,9 +89,31 @@ class BaseGenerator {
|
||||||
if (namespaces.size()) return *(namespaces.end() - 1); else return std::string("");
|
if (namespaces.size()) return *(namespaces.end() - 1); else return std::string("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tracks the current namespace for early exit in WrapInNameSpace
|
||||||
|
// c++, java and csharp returns a different namespace from
|
||||||
|
// the following default (no early exit, always fully qualify),
|
||||||
|
// which works for js and php
|
||||||
|
virtual const Namespace *CurrentNameSpace() { return nullptr; }
|
||||||
|
|
||||||
|
// Ensure that a type is prefixed with its namespace whenever it is used
|
||||||
|
// outside of its namespace.
|
||||||
|
std::string WrapInNameSpace(const Namespace *ns, const std::string &name) {
|
||||||
|
if (CurrentNameSpace() == ns) return name;
|
||||||
|
std::string qualified_name = qualifying_start_;
|
||||||
|
for (auto it = ns->components.begin(); it != ns->components.end(); ++it)
|
||||||
|
qualified_name += *it + qualifying_separator_;
|
||||||
|
return qualified_name + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string WrapInNameSpace(const Definition &def) {
|
||||||
|
return WrapInNameSpace(def.defined_namespace, def.name);
|
||||||
|
}
|
||||||
|
|
||||||
const Parser &parser_;
|
const Parser &parser_;
|
||||||
const std::string &path_;
|
const std::string &path_;
|
||||||
const std::string &file_name_;
|
const std::string &file_name_;
|
||||||
|
const std::string qualifying_start_;
|
||||||
|
const std::string qualifying_separator_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace flatbuffers
|
} // namespace flatbuffers
|
||||||
|
|
|
@ -39,7 +39,7 @@ class CppGenerator : public BaseGenerator {
|
||||||
public:
|
public:
|
||||||
CppGenerator(const Parser &parser, const std::string &path,
|
CppGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){};
|
: BaseGenerator(parser, path, file_name, "", "::"){};
|
||||||
// Iterate through all definitions we haven't generate code for (enums,
|
// Iterate through all definitions we haven't generate code for (enums,
|
||||||
// structs,
|
// structs,
|
||||||
// and tables) and output them to a single file.
|
// and tables) and output them to a single file.
|
||||||
|
@ -208,23 +208,7 @@ class CppGenerator : public BaseGenerator {
|
||||||
// This tracks the current namespace so we can insert namespace declarations.
|
// This tracks the current namespace so we can insert namespace declarations.
|
||||||
const Namespace *cur_name_space_ = nullptr;
|
const Namespace *cur_name_space_ = nullptr;
|
||||||
|
|
||||||
// Ensure that a type is prefixed with its namespace whenever it is used
|
const Namespace * CurrentNameSpace() {return cur_name_space_;}
|
||||||
// outside of its namespace.
|
|
||||||
std::string WrapInNameSpace(const Namespace *ns, const std::string &name) {
|
|
||||||
if (cur_name_space_ != ns) {
|
|
||||||
std::string qualified_name;
|
|
||||||
for (auto it = ns->components.begin(); it != ns->components.end(); ++it) {
|
|
||||||
qualified_name += *it + "::";
|
|
||||||
}
|
|
||||||
return qualified_name + name;
|
|
||||||
} else {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WrapInNameSpace(const Definition &def) {
|
|
||||||
return WrapInNameSpace(def.defined_namespace, def.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translates a qualified name in flatbuffer text format to the same name in
|
// Translates a qualified name in flatbuffer text format to the same name in
|
||||||
// the equivalent C++ namespace.
|
// the equivalent C++ namespace.
|
||||||
|
|
|
@ -193,7 +193,7 @@ class GeneralGenerator : public BaseGenerator {
|
||||||
public:
|
public:
|
||||||
GeneralGenerator(const Parser &parser, const std::string &path,
|
GeneralGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){
|
: BaseGenerator(parser, path, file_name, "", "."){
|
||||||
assert(parser_.opts.lang <= IDLOptions::kMAX);
|
assert(parser_.opts.lang <= IDLOptions::kMAX);
|
||||||
};
|
};
|
||||||
bool generate() {
|
bool generate() {
|
||||||
|
@ -252,37 +252,18 @@ class GeneralGenerator : public BaseGenerator {
|
||||||
return SaveFile(filename.c_str(), code, false);
|
return SaveFile(filename.c_str(), code, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Namespace *CurrentNameSpace() { return parser_.namespaces_.back(); }
|
||||||
|
|
||||||
std::string FunctionStart(char upper) {
|
std::string FunctionStart(char upper) {
|
||||||
return std::string() +
|
return std::string() + (lang_.language == IDLOptions::kJava
|
||||||
(lang_.language == IDLOptions::kJava
|
? static_cast<char>(tolower(upper))
|
||||||
? static_cast<char>(tolower(upper))
|
: upper);
|
||||||
: upper);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsEnum(const Type& type) {
|
static bool IsEnum(const Type& type) {
|
||||||
return type.enum_def != nullptr && IsInteger(type.base_type);
|
return type.enum_def != nullptr && IsInteger(type.base_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that a type is prefixed with its namespace whenever it is used
|
|
||||||
// outside of its namespace.
|
|
||||||
std::string WrapInNameSpace(const Namespace *ns, const std::string &name) {
|
|
||||||
if (parser_.namespaces_.back() != ns) {
|
|
||||||
std::string qualified_name;
|
|
||||||
for (auto it = ns->components.begin();
|
|
||||||
it != ns->components.end(); ++it) {
|
|
||||||
qualified_name += *it + ".";
|
|
||||||
}
|
|
||||||
return qualified_name + name;
|
|
||||||
} else {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WrapInNameSpace(const Definition &def) {
|
|
||||||
return WrapInNameSpace(def.defined_namespace, def.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) {
|
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) {
|
||||||
static const char *gtypename[] = {
|
static const char *gtypename[] = {
|
||||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||||
|
|
|
@ -625,7 +625,8 @@ class GoGenerator : public BaseGenerator {
|
||||||
public:
|
public:
|
||||||
GoGenerator(const Parser &parser, const std::string &path,
|
GoGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){};
|
: BaseGenerator(parser, path, file_name, "" /* not used*/,
|
||||||
|
"" /* not used */){};
|
||||||
bool generate() {
|
bool generate() {
|
||||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||||
++it) {
|
++it) {
|
||||||
|
|
|
@ -35,7 +35,7 @@ class JsGenerator : public BaseGenerator {
|
||||||
public:
|
public:
|
||||||
JsGenerator(const Parser &parser, const std::string &path,
|
JsGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){};
|
: BaseGenerator(parser, path, file_name, "", "."){};
|
||||||
// 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() {
|
||||||
|
@ -119,22 +119,6 @@ class JsGenerator : public BaseGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that a type is prefixed with its namespace whenever it is used
|
|
||||||
// outside of its namespace.
|
|
||||||
std::string WrapInNameSpace(const Namespace *ns,
|
|
||||||
const std::string &name) {
|
|
||||||
std::string qualified_name;
|
|
||||||
for (auto it = ns->components.begin();
|
|
||||||
it != ns->components.end(); ++it) {
|
|
||||||
qualified_name += *it + ".";
|
|
||||||
}
|
|
||||||
return qualified_name + name;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WrapInNameSpace(const Definition &def) {
|
|
||||||
return WrapInNameSpace(def.defined_namespace, def.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a documentation comment, if available.
|
// Generate a documentation comment, if available.
|
||||||
static void GenDocComment(const std::vector<std::string> &dc,
|
static void GenDocComment(const std::vector<std::string> &dc,
|
||||||
std::string *code_ptr,
|
std::string *code_ptr,
|
||||||
|
|
|
@ -31,7 +31,7 @@ namespace php {
|
||||||
public:
|
public:
|
||||||
PhpGenerator(const Parser &parser, const std::string &path,
|
PhpGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){};
|
: BaseGenerator(parser, path, file_name, "\\", "\\"){};
|
||||||
bool generate() {
|
bool generate() {
|
||||||
if (!generateEnums()) return false;
|
if (!generateEnums()) return false;
|
||||||
if (!generateStructs()) return false;
|
if (!generateStructs()) return false;
|
||||||
|
@ -93,23 +93,6 @@ namespace php {
|
||||||
return SaveFile(filename.c_str(), code, false);
|
return SaveFile(filename.c_str(), code, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Ensure that a type is prefixed with its namespace whenever it is used
|
|
||||||
// outside of its namespace.
|
|
||||||
std::string WrapInNameSpace(const Namespace *ns, const std::string &name) {
|
|
||||||
std::string qualified_name = "\\";
|
|
||||||
for (auto it = ns->components.begin();
|
|
||||||
it != ns->components.end(); ++it) {
|
|
||||||
qualified_name += *it + "\\";
|
|
||||||
}
|
|
||||||
return qualified_name + name;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WrapInNameSpace(const Definition &def) {
|
|
||||||
return WrapInNameSpace(def.defined_namespace, def.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Begin a class declaration.
|
// Begin a class declaration.
|
||||||
static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
|
|
@ -596,7 +596,8 @@ class PythonGenerator : public BaseGenerator {
|
||||||
public:
|
public:
|
||||||
PythonGenerator(const Parser &parser, const std::string &path,
|
PythonGenerator(const Parser &parser, const std::string &path,
|
||||||
const std::string &file_name)
|
const std::string &file_name)
|
||||||
: BaseGenerator(parser, path, file_name){};
|
: BaseGenerator(parser, path, file_name, "" /* not used */,
|
||||||
|
"" /* not used */){};
|
||||||
bool generate() {
|
bool generate() {
|
||||||
if (!generateEnums()) return false;
|
if (!generateEnums()) return false;
|
||||||
if (!generateStructs()) return false;
|
if (!generateStructs()) return false;
|
||||||
|
|
Loading…
Reference in New Issue