diff --git a/include/flatbuffers/code_generators.h b/include/flatbuffers/code_generators.h index dc5f6bdd8..e7450a7a0 100644 --- a/include/flatbuffers/code_generators.h +++ b/include/flatbuffers/code_generators.h @@ -39,11 +39,15 @@ class BaseGenerator { protected: 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), path_(path), - file_name_(file_name) {}; - virtual ~BaseGenerator() {}; + file_name_(file_name), + qualifying_start_(qualifying_start), + qualifying_separator_(qualifying_separator){}; + virtual ~BaseGenerator(){}; // No copy/assign. BaseGenerator &operator=(const BaseGenerator &); @@ -85,9 +89,31 @@ class BaseGenerator { 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 std::string &path_; const std::string &file_name_; + const std::string qualifying_start_; + const std::string qualifying_separator_; }; } // namespace flatbuffers diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 6c7981417..3a4476fec 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -39,7 +39,7 @@ class CppGenerator : public BaseGenerator { public: CppGenerator(const Parser &parser, const std::string &path, 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, // structs, // 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. const Namespace *cur_name_space_ = 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 (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); - } + const Namespace * CurrentNameSpace() {return cur_name_space_;} // Translates a qualified name in flatbuffer text format to the same name in // the equivalent C++ namespace. diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 7567bdcf4..a96609c54 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -193,7 +193,7 @@ class GeneralGenerator : public BaseGenerator { public: GeneralGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){ + : BaseGenerator(parser, path, file_name, "", "."){ assert(parser_.opts.lang <= IDLOptions::kMAX); }; bool generate() { @@ -251,38 +251,19 @@ class GeneralGenerator : public BaseGenerator { auto filename = NamespaceDir(ns) + defname + lang_.file_extension; return SaveFile(filename.c_str(), code, false); } - -std::string FunctionStart(char upper) { - return std::string() + - (lang_.language == IDLOptions::kJava - ? static_cast(tolower(upper)) - : upper); + const Namespace *CurrentNameSpace() { return parser_.namespaces_.back(); } + + std::string FunctionStart(char upper) { + return std::string() + (lang_.language == IDLOptions::kJava + ? static_cast(tolower(upper)) + : upper); } static bool IsEnum(const Type& 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) { static const char *gtypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 5c60c1052..a04b6ba20 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -625,7 +625,8 @@ class GoGenerator : public BaseGenerator { public: GoGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; + : BaseGenerator(parser, path, file_name, "" /* not used*/, + "" /* not used */){}; bool generate() { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index a29337772..32a06f1ef 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -35,7 +35,7 @@ class JsGenerator : public BaseGenerator { public: JsGenerator(const Parser &parser, const std::string &path, 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, // structs, and tables) and output them to a single file. 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. static void GenDocComment(const std::vector &dc, std::string *code_ptr, diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 11eb0fd1c..af3b06d57 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -31,7 +31,7 @@ namespace php { public: PhpGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; + : BaseGenerator(parser, path, file_name, "\\", "\\"){}; bool generate() { if (!generateEnums()) return false; if (!generateStructs()) return false; @@ -93,23 +93,6 @@ namespace php { 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. static void BeginClass(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 5f5c6be3b..7e8dc0ebd 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -596,7 +596,8 @@ class PythonGenerator : public BaseGenerator { public: PythonGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; + : BaseGenerator(parser, path, file_name, "" /* not used */, + "" /* not used */){}; bool generate() { if (!generateEnums()) return false; if (!generateStructs()) return false;