mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-22 20:58:53 +00:00
Merge pull request #3908 from Lakedaemon/intoTheClass
pulling methods inside the class... so as to share wrapInNamespace later
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -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 @@
|
|||||||
#include "flatbuffers/code_generators.h"
|
#include "flatbuffers/code_generators.h"
|
||||||
|
|
||||||
namespace flatbuffers {
|
namespace flatbuffers {
|
||||||
namespace js {
|
|
||||||
|
|
||||||
static void GenNamespaces(const Parser &parser, std::string *code_ptr,
|
static std::string GeneratedFileName(const std::string &path,
|
||||||
std::string *exports_ptr) {
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
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,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
|||||||
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,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
|||||||
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,
|
|||||||
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
|
||||||
|
|
||||||
|
|||||||
@@ -25,35 +25,74 @@
|
|||||||
|
|
||||||
namespace flatbuffers {
|
namespace flatbuffers {
|
||||||
namespace php {
|
namespace php {
|
||||||
|
|
||||||
static std::string GenGetter(const Type &type);
|
|
||||||
static std::string GenDefaultValue(const Value &value);
|
|
||||||
static std::string GenMethod(const FieldDef &field);
|
|
||||||
static void GenStructBuilder(const StructDef &struct_def,
|
|
||||||
std::string *code_ptr);
|
|
||||||
static std::string GenTypeBasic(const Type &type);
|
|
||||||
static std::string GenTypeGet(const Type &type);
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Hardcode spaces per indentation.
|
// Hardcode spaces per indentation.
|
||||||
const std::string Indent = " ";
|
const std::string Indent = " ";
|
||||||
|
class PhpGenerator : public BaseGenerator {
|
||||||
|
public:
|
||||||
|
PhpGenerator(const Parser &parser, const std::string &path,
|
||||||
|
const std::string &file_name)
|
||||||
|
: BaseGenerator(parser, path, file_name, "\\", "\\"){};
|
||||||
|
bool generate() {
|
||||||
|
if (!generateEnums()) return false;
|
||||||
|
if (!generateStructs()) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool generateEnums() {
|
||||||
|
for (auto it = parser_.enums_.vec.begin();
|
||||||
|
it != parser_.enums_.vec.end(); ++it) {
|
||||||
|
auto &enum_def = **it;
|
||||||
|
std::string enumcode;
|
||||||
|
GenEnum(enum_def, &enumcode);
|
||||||
|
if (!SaveType(enum_def, enumcode, false)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool generateStructs() {
|
||||||
|
for (auto it = parser_.structs_.vec.begin();
|
||||||
|
it != parser_.structs_.vec.end(); ++it) {
|
||||||
|
auto &struct_def = **it;
|
||||||
|
std::string declcode;
|
||||||
|
GenStruct(struct_def, &declcode);
|
||||||
|
if (!SaveType(struct_def, declcode, true)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Begin by declaring namespace and imports.
|
||||||
|
void BeginFile(const std::string name_space_name,
|
||||||
|
const bool needs_imports, std::string *code_ptr) {
|
||||||
|
std::string &code = *code_ptr;
|
||||||
|
code += "<?php\n";
|
||||||
|
code = code + "// " + FlatBuffersGeneratedWarning();
|
||||||
|
code += "namespace " + name_space_name + ";\n\n";
|
||||||
|
|
||||||
|
if (needs_imports) {
|
||||||
|
code += "use \\Google\\FlatBuffers\\Struct;\n";
|
||||||
|
code += "use \\Google\\FlatBuffers\\Table;\n";
|
||||||
|
code += "use \\Google\\FlatBuffers\\ByteBuffer;\n";
|
||||||
|
code += "use \\Google\\FlatBuffers\\FlatBufferBuilder;\n";
|
||||||
|
code += "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save out the generated code for a Php Table type.
|
||||||
|
bool SaveType(const Definition &def, const std::string &classcode,
|
||||||
|
bool needs_imports) {
|
||||||
|
if (!classcode.length()) return true;
|
||||||
|
|
||||||
|
std::string code = "";
|
||||||
|
BeginFile(FullNamespace("\\", *def.defined_namespace),
|
||||||
|
needs_imports, &code);
|
||||||
|
code += classcode;
|
||||||
|
|
||||||
|
std::string filename = NamespaceDir(*def.defined_namespace) +
|
||||||
|
kPathSeparator + def.name + ".php";
|
||||||
|
return SaveFile(filename.c_str(), code, false);
|
||||||
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
@@ -189,8 +228,7 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the value of a table's scalar.
|
// Get the value of a table's scalar.
|
||||||
static void GetScalarFieldOfTable(const FieldDef &field,
|
void GetScalarFieldOfTable(const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
std::string getter = GenGetter(field.value.type);
|
std::string getter = GenGetter(field.value.type);
|
||||||
|
|
||||||
@@ -213,8 +251,7 @@ namespace php {
|
|||||||
|
|
||||||
// Get a struct by initializing an existing struct.
|
// Get a struct by initializing an existing struct.
|
||||||
// Specific to Struct.
|
// Specific to Struct.
|
||||||
static void GetStructFieldOfStruct(const FieldDef &field,
|
void GetStructFieldOfStruct(const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
|
||||||
code += Indent + "/**\n";
|
code += Indent + "/**\n";
|
||||||
@@ -233,8 +270,7 @@ namespace php {
|
|||||||
|
|
||||||
// Get a struct by initializing an existing struct.
|
// Get a struct by initializing an existing struct.
|
||||||
// Specific to Table.
|
// Specific to Table.
|
||||||
static void GetStructFieldOfTable(const FieldDef &field,
|
void GetStructFieldOfTable(const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
|
||||||
code += Indent + "public function get";
|
code += Indent + "public function get";
|
||||||
@@ -260,8 +296,7 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the value of a string.
|
// Get the value of a string.
|
||||||
static void GetStringField(const FieldDef &field,
|
void GetStringField(const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
code += Indent + "public function get";
|
code += Indent + "public function get";
|
||||||
code += MakeCamel(field.name);
|
code += MakeCamel(field.name);
|
||||||
@@ -278,8 +313,7 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the value of a union from an object.
|
// Get the value of a union from an object.
|
||||||
static void GetUnionField(const FieldDef &field,
|
void GetUnionField(const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
|
||||||
code += Indent + "/**\n";
|
code += Indent + "/**\n";
|
||||||
@@ -298,9 +332,8 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the value of a vector's struct member.
|
// Get the value of a vector's struct member.
|
||||||
static void GetMemberOfVectorOfStruct(const StructDef &struct_def,
|
void GetMemberOfVectorOfStruct(const StructDef &struct_def,
|
||||||
const FieldDef &field,
|
const FieldDef &field, std::string *code_ptr) {
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
auto vectortype = field.value.type.VectorType();
|
auto vectortype = field.value.type.VectorType();
|
||||||
|
|
||||||
@@ -362,7 +395,7 @@ namespace php {
|
|||||||
|
|
||||||
// Get the value of a vector's non-struct member. Uses a named return
|
// Get the value of a vector's non-struct member. Uses a named return
|
||||||
// argument to conveniently set the zero value for the result.
|
// argument to conveniently set the zero value for the result.
|
||||||
static void GetMemberOfVectorOfNonStruct(const FieldDef &field,
|
void GetMemberOfVectorOfNonStruct(const FieldDef &field,
|
||||||
std::string *code_ptr) {
|
std::string *code_ptr) {
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
auto vectortype = field.value.type.VectorType();
|
auto vectortype = field.value.type.VectorType();
|
||||||
@@ -603,9 +636,7 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the offset of the end of a table.
|
// Get the offset of the end of a table.
|
||||||
static void GetEndOffsetOnTable(const Parser &parser,
|
void GetEndOffsetOnTable(const StructDef &struct_def, std::string *code_ptr) {
|
||||||
const StructDef &struct_def,
|
|
||||||
std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
|
|
||||||
|
|
||||||
@@ -632,7 +663,7 @@ namespace php {
|
|||||||
code += Indent + Indent + "return $o;\n";
|
code += Indent + Indent + "return $o;\n";
|
||||||
code += Indent + "}\n";
|
code += Indent + "}\n";
|
||||||
|
|
||||||
if (parser.root_struct_def_ == &struct_def) {
|
if (parser_.root_struct_def_ == &struct_def) {
|
||||||
code += "\n";
|
code += "\n";
|
||||||
code += Indent + "public static function finish";
|
code += Indent + "public static function finish";
|
||||||
code += struct_def.name;
|
code += struct_def.name;
|
||||||
@@ -640,16 +671,15 @@ namespace php {
|
|||||||
code += Indent + "{\n";
|
code += Indent + "{\n";
|
||||||
code += Indent + Indent + "$builder->finish($offset";
|
code += Indent + Indent + "$builder->finish($offset";
|
||||||
|
|
||||||
if (parser.file_identifier_.length())
|
if (parser_.file_identifier_.length())
|
||||||
code += ", \"" + parser.file_identifier_ + "\"";
|
code += ", \"" + parser_.file_identifier_ + "\"";
|
||||||
code += ");\n";
|
code += ");\n";
|
||||||
code += Indent + "}\n";
|
code += Indent + "}\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a struct field, conditioned on its child type(s).
|
// Generate a struct field, conditioned on its child type(s).
|
||||||
static void GenStructAccessor(const StructDef &struct_def,
|
void GenStructAccessor(const StructDef &struct_def, const FieldDef &field,
|
||||||
const FieldDef &field,
|
|
||||||
std::string *code_ptr) {
|
std::string *code_ptr) {
|
||||||
GenComment(field.doc_comment, code_ptr, nullptr);
|
GenComment(field.doc_comment, code_ptr, nullptr);
|
||||||
|
|
||||||
@@ -696,9 +726,7 @@ namespace php {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate table constructors, conditioned on its members' types.
|
// Generate table constructors, conditioned on its members' types.
|
||||||
static void GenTableBuilders(const Parser &parser,
|
void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) {
|
||||||
const StructDef &struct_def,
|
|
||||||
std::string *code_ptr) {
|
|
||||||
GetStartOfTable(struct_def, code_ptr);
|
GetStartOfTable(struct_def, code_ptr);
|
||||||
|
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
for (auto it = struct_def.fields.vec.begin();
|
||||||
@@ -725,11 +753,11 @@ namespace php {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GetEndOffsetOnTable(parser, struct_def, code_ptr);
|
GetEndOffsetOnTable(struct_def, code_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate struct or table methods.
|
// Generate struct or table methods.
|
||||||
static void GenStruct(const Parser &parser, const StructDef &struct_def,
|
void GenStruct(const StructDef &struct_def,
|
||||||
std::string *code_ptr) {
|
std::string *code_ptr) {
|
||||||
if (struct_def.generated) return;
|
if (struct_def.generated) return;
|
||||||
|
|
||||||
@@ -744,13 +772,13 @@ namespace php {
|
|||||||
|
|
||||||
std::string &code = *code_ptr;
|
std::string &code = *code_ptr;
|
||||||
if (!struct_def.fixed) {
|
if (!struct_def.fixed) {
|
||||||
if (parser.file_identifier_.length()) {
|
if (parser_.file_identifier_.length()) {
|
||||||
// Return the identifier
|
// Return the identifier
|
||||||
code += Indent + "public static function " + struct_def.name;
|
code += Indent + "public static function " + struct_def.name;
|
||||||
code += "Identifier()\n";
|
code += "Identifier()\n";
|
||||||
code += Indent + "{\n";
|
code += Indent + "{\n";
|
||||||
code += Indent + Indent + "return \"";
|
code += Indent + Indent + "return \"";
|
||||||
code += parser.file_identifier_ + "\";\n";
|
code += parser_.file_identifier_ + "\";\n";
|
||||||
code += Indent + "}\n\n";
|
code += Indent + "}\n\n";
|
||||||
|
|
||||||
// Check if a buffer has the identifier.
|
// Check if a buffer has the identifier.
|
||||||
@@ -763,12 +791,12 @@ namespace php {
|
|||||||
code += Indent + "}\n\n";
|
code += Indent + "}\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parser.file_extension_.length()) {
|
if (parser_.file_extension_.length()) {
|
||||||
// Return the extension
|
// Return the extension
|
||||||
code += Indent + "public static function " + struct_def.name;
|
code += Indent + "public static function " + struct_def.name;
|
||||||
code += "Extension()\n";
|
code += "Extension()\n";
|
||||||
code += Indent + "{\n";
|
code += Indent + "{\n";
|
||||||
code += Indent + Indent + "return \"" + parser.file_extension_;
|
code += Indent + Indent + "return \"" + parser_.file_extension_;
|
||||||
code += "\";\n";
|
code += "\";\n";
|
||||||
code += Indent + "}\n\n";
|
code += Indent + "}\n\n";
|
||||||
}
|
}
|
||||||
@@ -791,7 +819,7 @@ namespace php {
|
|||||||
GenStructBuilder(struct_def, code_ptr);
|
GenStructBuilder(struct_def, code_ptr);
|
||||||
} else {
|
} else {
|
||||||
// Create a set of functions that allow table construction.
|
// Create a set of functions that allow table construction.
|
||||||
GenTableBuilders(parser, struct_def, code_ptr);
|
GenTableBuilders(struct_def, code_ptr);
|
||||||
}
|
}
|
||||||
EndClass(code_ptr);
|
EndClass(code_ptr);
|
||||||
}
|
}
|
||||||
@@ -859,7 +887,7 @@ namespace php {
|
|||||||
return ctypename[type.base_type];
|
return ctypename[type.base_type];
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string GenDefaultValue(const Value &value) {
|
std::string GenDefaultValue(const Value &value) {
|
||||||
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)) {
|
||||||
@@ -927,72 +955,7 @@ namespace php {
|
|||||||
code += Indent + Indent + "return $builder->offset();\n";
|
code += Indent + Indent + "return $builder->offset();\n";
|
||||||
code += Indent + "}\n";
|
code += Indent + "}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
class PhpGenerator : public BaseGenerator {
|
|
||||||
public:
|
|
||||||
PhpGenerator(const Parser &parser, const std::string &path,
|
|
||||||
const std::string &file_name)
|
|
||||||
: BaseGenerator(parser, path, file_name){};
|
|
||||||
bool generate() {
|
|
||||||
if (!generateEnums()) return false;
|
|
||||||
if (!generateStructs()) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool generateEnums() {
|
|
||||||
for (auto it = parser_.enums_.vec.begin();
|
|
||||||
it != parser_.enums_.vec.end(); ++it) {
|
|
||||||
auto &enum_def = **it;
|
|
||||||
std::string enumcode;
|
|
||||||
GenEnum(enum_def, &enumcode);
|
|
||||||
if (!SaveType(enum_def, enumcode, false)) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool generateStructs() {
|
|
||||||
for (auto it = parser_.structs_.vec.begin();
|
|
||||||
it != parser_.structs_.vec.end(); ++it) {
|
|
||||||
auto &struct_def = **it;
|
|
||||||
std::string declcode;
|
|
||||||
GenStruct(parser_, struct_def, &declcode);
|
|
||||||
if (!SaveType(struct_def, declcode, true)) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Begin by declaring namespace and imports.
|
|
||||||
void BeginFile(const std::string name_space_name,
|
|
||||||
const bool needs_imports, std::string *code_ptr) {
|
|
||||||
std::string &code = *code_ptr;
|
|
||||||
code += "<?php\n";
|
|
||||||
code = code + "// " + FlatBuffersGeneratedWarning();
|
|
||||||
code += "namespace " + name_space_name + ";\n\n";
|
|
||||||
|
|
||||||
if (needs_imports) {
|
|
||||||
code += "use \\Google\\FlatBuffers\\Struct;\n";
|
|
||||||
code += "use \\Google\\FlatBuffers\\Table;\n";
|
|
||||||
code += "use \\Google\\FlatBuffers\\ByteBuffer;\n";
|
|
||||||
code += "use \\Google\\FlatBuffers\\FlatBufferBuilder;\n";
|
|
||||||
code += "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save out the generated code for a Php Table type.
|
|
||||||
bool SaveType(const Definition &def, const std::string &classcode,
|
|
||||||
bool needs_imports) {
|
|
||||||
if (!classcode.length()) return true;
|
|
||||||
|
|
||||||
std::string code = "";
|
|
||||||
BeginFile(FullNamespace("\\", *def.defined_namespace),
|
|
||||||
needs_imports, &code);
|
|
||||||
code += classcode;
|
|
||||||
|
|
||||||
std::string filename = NamespaceDir(*def.defined_namespace) +
|
|
||||||
kPathSeparator + def.name + ".php";
|
|
||||||
return SaveFile(filename.c_str(), code, false);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
} // namespace php
|
} // namespace php
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user