Commit 2fdafa9a authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen Committed by GitHub

Merge pull request #3908 from Lakedaemon/intoTheClass

pulling methods inside the class... so as to share wrapInNamespace later
parents 57ba8a4d f794f97d
...@@ -39,11 +39,15 @@ class BaseGenerator { ...@@ -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 { ...@@ -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 { ...@@ -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 { ...@@ -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.
......
This diff is collapsed.
...@@ -625,7 +625,8 @@ class GoGenerator : public BaseGenerator { ...@@ -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) {
......
...@@ -22,14 +22,71 @@ ...@@ -22,14 +22,71 @@
#include "flatbuffers/code_generators.h" #include "flatbuffers/code_generators.h"
namespace flatbuffers { namespace flatbuffers {
static std::string GeneratedFileName(const std::string &path,
const std::string &file_name) {
return path + file_name + "_generated.js";
}
namespace js { namespace js {
// Iterate through all definitions we haven't generate code for (enums, structs,
// and tables) and output them to a single file.
class JsGenerator : public BaseGenerator {
public:
JsGenerator(const Parser &parser, const std::string &path,
const std::string &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() {
if (IsEverythingGenerated()) return true;
std::string enum_code, struct_code, exports_code, code;
generateEnums(&enum_code, &exports_code);
generateStructs(&struct_code, &exports_code);
code = code + "// " + FlatBuffersGeneratedWarning();
// Generate code for all the namespace declarations.
GenNamespaces(&code, &exports_code);
// Output the main declaration code from above.
code += enum_code;
code += struct_code;
if (!exports_code.empty() && !parser_.opts.skip_js_exports) {
code += "// Exports for Node.js and RequireJS\n";
code += exports_code;
}
return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
}
private:
// Generate code for all enums.
void generateEnums(std::string *enum_code_ptr,
std::string *exports_code_ptr) {
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
auto &enum_def = **it;
GenEnum(enum_def, enum_code_ptr, exports_code_ptr);
}
}
static void GenNamespaces(const Parser &parser, std::string *code_ptr, // Generate code for all structs.
std::string *exports_ptr) { void generateStructs(std::string *decl_code_ptr,
std::string *exports_code_ptr) {
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
auto &struct_def = **it;
GenStruct(struct_def, decl_code_ptr, exports_code_ptr);
}
}
void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) {
std::set<std::string> namespaces; std::set<std::string> namespaces;
for (auto it = parser.namespaces_.begin(); for (auto it = parser_.namespaces_.begin();
it != parser.namespaces_.end(); ++it) { it != parser_.namespaces_.end(); ++it) {
std::string namespace_so_far; std::string namespace_so_far;
// Gather all parent namespaces for this namespace // Gather all parent namespaces for this namespace
...@@ -62,22 +119,6 @@ static void GenNamespaces(const Parser &parser, std::string *code_ptr, ...@@ -62,22 +119,6 @@ static void GenNamespaces(const Parser &parser, std::string *code_ptr,
} }
} }
// Ensure that a type is prefixed with its namespace whenever it is used
// outside of its namespace.
static 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;
}
static 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,
...@@ -123,7 +164,7 @@ static void GenDocComment(std::string *code_ptr, ...@@ -123,7 +164,7 @@ static void GenDocComment(std::string *code_ptr,
} }
// Generate an enum declaration and an enum string lookup table. // Generate an enum declaration and an enum string lookup table.
static void GenEnum(EnumDef &enum_def, std::string *code_ptr, void GenEnum(EnumDef &enum_def, std::string *code_ptr,
std::string *exports_ptr) { std::string *exports_ptr) {
if (enum_def.generated) return; if (enum_def.generated) return;
std::string &code = *code_ptr; std::string &code = *code_ptr;
...@@ -170,7 +211,7 @@ static std::string GenType(const Type &type) { ...@@ -170,7 +211,7 @@ static std::string GenType(const Type &type) {
} }
} }
static std::string GenGetter(const Type &type, const std::string &arguments) { std::string GenGetter(const Type &type, const std::string &arguments) {
switch (type.base_type) { switch (type.base_type) {
case BASE_TYPE_STRING: return "this.bb.__string" + arguments; case BASE_TYPE_STRING: return "this.bb.__string" + arguments;
case BASE_TYPE_STRUCT: return "this.bb.__struct" + arguments; case BASE_TYPE_STRUCT: return "this.bb.__struct" + arguments;
...@@ -190,7 +231,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) { ...@@ -190,7 +231,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) {
} }
} }
static std::string GenDefaultValue(const Value &value, const std::string &context) { std::string GenDefaultValue(const Value &value, const std::string &context) {
if (value.type.enum_def) { if (value.type.enum_def) {
if (auto val = value.type.enum_def->ReverseLookup( if (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) { atoi(value.constant.c_str()), false)) {
...@@ -217,7 +258,7 @@ static std::string GenDefaultValue(const Value &value, const std::string &contex ...@@ -217,7 +258,7 @@ static std::string GenDefaultValue(const Value &value, const std::string &contex
} }
} }
static std::string GenTypeName(const Type &type, bool input) { std::string GenTypeName(const Type &type, bool input) {
if (!input) { if (!input) {
if (type.base_type == BASE_TYPE_STRING) { if (type.base_type == BASE_TYPE_STRING) {
return "string|Uint8Array"; return "string|Uint8Array";
...@@ -269,7 +310,7 @@ static std::string MaybeScale(T value) { ...@@ -269,7 +310,7 @@ static std::string MaybeScale(T value) {
return value != 1 ? " * " + NumToString(value) : ""; return value != 1 ? " * " + NumToString(value) : "";
} }
static void GenStructArgs(const StructDef &struct_def, void GenStructArgs(const StructDef &struct_def,
std::string *annotations, std::string *annotations,
std::string *arguments, std::string *arguments,
const std::string &nameprefix) { const std::string &nameprefix) {
...@@ -320,8 +361,7 @@ static void GenStructBody(const StructDef &struct_def, ...@@ -320,8 +361,7 @@ static void GenStructBody(const StructDef &struct_def,
} }
// Generate an accessor struct with constructor for a flatbuffers struct. // Generate an accessor struct with constructor for a flatbuffers struct.
static void GenStruct(const Parser &parser, StructDef &struct_def, void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) {
std::string *code_ptr, std::string *exports_ptr) {
if (struct_def.generated) return; if (struct_def.generated) return;
std::string &code = *code_ptr; std::string &code = *code_ptr;
std::string &exports = *exports_ptr; std::string &exports = *exports_ptr;
...@@ -375,13 +415,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -375,13 +415,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "};\n\n"; code += "};\n\n";
// Generate the identifier check method // Generate the identifier check method
if (parser.root_struct_def_ == &struct_def && if (parser_.root_struct_def_ == &struct_def &&
!parser.file_identifier_.empty()) { !parser_.file_identifier_.empty()) {
GenDocComment(code_ptr, GenDocComment(code_ptr,
"@param {flatbuffers.ByteBuffer} bb\n" "@param {flatbuffers.ByteBuffer} bb\n"
"@returns {boolean}"); "@returns {boolean}");
code += object_name + ".bufferHasIdentifier = function(bb) {\n"; code += object_name + ".bufferHasIdentifier = function(bb) {\n";
code += " return bb.__has_identifier('" + parser.file_identifier_; code += " return bb.__has_identifier('" + parser_.file_identifier_;
code += "');\n};\n\n"; code += "');\n};\n\n";
} }
} }
...@@ -644,83 +684,21 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -644,83 +684,21 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "};\n\n"; code += "};\n\n";
// Generate the method to complete buffer construction // Generate the method to complete buffer construction
if (parser.root_struct_def_ == &struct_def) { if (parser_.root_struct_def_ == &struct_def) {
GenDocComment(code_ptr, GenDocComment(code_ptr,
"@param {flatbuffers.Builder} builder\n" "@param {flatbuffers.Builder} builder\n"
"@param {flatbuffers.Offset} offset"); "@param {flatbuffers.Offset} offset");
code += object_name + ".finish" + struct_def.name + "Buffer"; code += object_name + ".finish" + struct_def.name + "Buffer";
code += " = function(builder, offset) {\n"; code += " = function(builder, offset) {\n";
code += " builder.finish(offset"; code += " builder.finish(offset";
if (!parser.file_identifier_.empty()) { if (!parser_.file_identifier_.empty()) {
code += ", '" + parser.file_identifier_ + "'"; code += ", '" + parser_.file_identifier_ + "'";
} }
code += ");\n"; code += ");\n";
code += "};\n\n"; code += "};\n\n";
} }
} }
} }
} // namespace js
static std::string GeneratedFileName(const std::string &path,
const std::string &file_name) {
return path + file_name + "_generated.js";
}
namespace js {
// Iterate through all definitions we haven't generate code for (enums, structs,
// and tables) and output them to a single file.
class JsGenerator : public BaseGenerator {
public:
JsGenerator(const Parser &parser, const std::string &path,
const std::string &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() {
if (IsEverythingGenerated()) return true;
std::string enum_code, struct_code, exports_code, code;
generateEnums(&enum_code, &exports_code);
generateStructs(&struct_code, &exports_code);
code = code + "// " + FlatBuffersGeneratedWarning();
// Generate code for all the namespace declarations.
GenNamespaces(parser_, &code, &exports_code);
// Output the main declaration code from above.
code += enum_code;
code += struct_code;
if (!exports_code.empty() && !parser_.opts.skip_js_exports) {
code += "// Exports for Node.js and RequireJS\n";
code += exports_code;
}
return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
}
private:
// Generate code for all enums.
void generateEnums(std::string *enum_code_ptr,
std::string *exports_code_ptr) {
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
auto &enum_def = **it;
GenEnum(enum_def, enum_code_ptr, exports_code_ptr);
}
}
// Generate code for all structs.
void generateStructs(std::string *decl_code_ptr,
std::string *exports_code_ptr) {
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
auto &struct_def = **it;
GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr);
}
}
}; };
} // namespace js } // namespace js
......
This diff is collapsed.
...@@ -596,7 +596,8 @@ class PythonGenerator : public BaseGenerator { ...@@ -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;
......
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