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 {
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
......
......@@ -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.
......
This diff is collapsed.
......@@ -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) {
......
......@@ -22,14 +22,71 @@
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
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(&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,
std::string *exports_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(struct_def, decl_code_ptr, exports_code_ptr);
}
}
void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) {
std::set<std::string> namespaces;
for (auto it = parser.namespaces_.begin();
it != parser.namespaces_.end(); ++it) {
for (auto it = parser_.namespaces_.begin();
it != parser_.namespaces_.end(); ++it) {
std::string namespace_so_far;
// Gather all parent namespaces for this namespace
......@@ -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.
static void GenDocComment(const std::vector<std::string> &dc,
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.
static void GenEnum(EnumDef &enum_def, std::string *code_ptr,
void GenEnum(EnumDef &enum_def, std::string *code_ptr,
std::string *exports_ptr) {
if (enum_def.generated) return;
std::string &code = *code_ptr;
......@@ -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) {
case BASE_TYPE_STRING: return "this.bb.__string" + 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) {
}
}
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 (auto val = value.type.enum_def->ReverseLookup(
atoi(value.constant.c_str()), false)) {
......@@ -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 (type.base_type == BASE_TYPE_STRING) {
return "string|Uint8Array";
......@@ -269,7 +310,7 @@ static std::string MaybeScale(T value) {
return value != 1 ? " * " + NumToString(value) : "";
}
static void GenStructArgs(const StructDef &struct_def,
void GenStructArgs(const StructDef &struct_def,
std::string *annotations,
std::string *arguments,
const std::string &nameprefix) {
......@@ -320,8 +361,7 @@ static void GenStructBody(const StructDef &struct_def,
}
// Generate an accessor struct with constructor for a flatbuffers struct.
static void GenStruct(const Parser &parser, StructDef &struct_def,
std::string *code_ptr, std::string *exports_ptr) {
void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) {
if (struct_def.generated) return;
std::string &code = *code_ptr;
std::string &exports = *exports_ptr;
......@@ -375,13 +415,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "};\n\n";
// Generate the identifier check method
if (parser.root_struct_def_ == &struct_def &&
!parser.file_identifier_.empty()) {
if (parser_.root_struct_def_ == &struct_def &&
!parser_.file_identifier_.empty()) {
GenDocComment(code_ptr,
"@param {flatbuffers.ByteBuffer} bb\n"
"@returns {boolean}");
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";
}
}
......@@ -644,83 +684,21 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
code += "};\n\n";
// Generate the method to complete buffer construction
if (parser.root_struct_def_ == &struct_def) {
if (parser_.root_struct_def_ == &struct_def) {
GenDocComment(code_ptr,
"@param {flatbuffers.Builder} builder\n"
"@param {flatbuffers.Offset} offset");
code += object_name + ".finish" + struct_def.name + "Buffer";
code += " = function(builder, offset) {\n";
code += " builder.finish(offset";
if (!parser.file_identifier_.empty()) {
code += ", '" + parser.file_identifier_ + "'";
if (!parser_.file_identifier_.empty()) {
code += ", '" + parser_.file_identifier_ + "'";
}
code += ");\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
......
This diff is collapsed.
......@@ -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;
......
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