Commit 933c195e authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen

Merge pull request #3843 from Lakedaemon/refactoring

Transition 1 (nice diffs) Go, C++ and General generators with class
parents d0898fd0 6765c19d
...@@ -17,6 +17,7 @@ if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) ...@@ -17,6 +17,7 @@ if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
endif() endif()
set(FlatBuffers_Library_SRCS set(FlatBuffers_Library_SRCS
include/flatbuffers/code_generators.h
include/flatbuffers/flatbuffers.h include/flatbuffers/flatbuffers.h
include/flatbuffers/hash.h include/flatbuffers/hash.h
include/flatbuffers/idl.h include/flatbuffers/idl.h
......
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_CODE_GENERATORS_H_
#define FLATBUFFERS_CODE_GENERATORS_H_
namespace flatbuffers {
class BaseGenerator {
public:
BaseGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
: parser_(parser), path_(path), file_name_(file_name){};
virtual bool generate() = 0;
protected:
virtual ~BaseGenerator(){};
const Parser &parser_;
const std::string &path_;
const std::string &file_name_;
};
} // namespace flatbuffers
#endif // FLATBUFFERS_CODE_GENERATORS_H_
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers { namespace flatbuffers {
namespace cpp { namespace cpp {
...@@ -714,197 +715,205 @@ struct IsAlnum { ...@@ -714,197 +715,205 @@ struct IsAlnum {
} }
}; };
// Iterate through all definitions we haven't generate code for (enums, structs, static std::string GeneratedFileName(const std::string &path,
// and tables) and output them to a single file. const std::string &file_name) {
std::string GenerateCPP(const Parser &parser, return path + file_name + "_generated.h";
const std::string &file_name) { }
// Check if we have any code to generate at all, to avoid an empty header.
for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); namespace cpp {
++it) { class CppGenerator : public BaseGenerator {
if (!(*it)->generated) goto generate_code; public:
} CppGenerator(const Parser &parser, const std::string &path,
for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); const std::string &file_name)
++it) { : BaseGenerator(parser, path, file_name){};
if (!(*it)->generated) goto generate_code; // Iterate through all definitions we haven't generate code for (enums,
} // structs,
// No code to generate, exit: // and tables) and output them to a single file.
return std::string(); bool generate() {
// Check if we have any code to generate at all, to avoid an empty header.
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
if (!(*it)->generated) goto generate_code;
}
for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end();
++it) {
if (!(*it)->generated) goto generate_code;
}
// No code to generate, exit:
return true;
generate_code: generate_code:
using namespace cpp; using namespace cpp;
std::string code; std::string code;
code = "// automatically generated by the FlatBuffers compiler," code =
" do not modify\n\n"; "// automatically generated by the FlatBuffers compiler,"
" do not modify\n\n";
// Generate include guard.
std::string include_guard_ident = file_name; // Generate include guard.
// Remove any non-alpha-numeric characters that may appear in a filename. std::string include_guard_ident = file_name_;
include_guard_ident.erase( // Remove any non-alpha-numeric characters that may appear in a filename.
std::remove_if(include_guard_ident.begin(), include_guard_ident.erase(
include_guard_ident.end(), std::remove_if(include_guard_ident.begin(), include_guard_ident.end(),
IsAlnum()), IsAlnum()),
include_guard_ident.end()); include_guard_ident.end());
std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident; std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident;
include_guard += "_"; include_guard += "_";
// For further uniqueness, also add the namespace. // For further uniqueness, also add the namespace.
auto name_space = parser.namespaces_.back(); auto name_space = parser_.namespaces_.back();
for (auto it = name_space->components.begin(); for (auto it = name_space->components.begin();
it != name_space->components.end(); ++it) { it != name_space->components.end(); ++it) {
include_guard += *it + "_"; include_guard += *it + "_";
} }
include_guard += "H_"; include_guard += "H_";
std::transform(include_guard.begin(), include_guard.end(), std::transform(include_guard.begin(), include_guard.end(),
include_guard.begin(), ::toupper); include_guard.begin(), ::toupper);
code += "#ifndef " + include_guard + "\n"; code += "#ifndef " + include_guard + "\n";
code += "#define " + include_guard + "\n\n"; code += "#define " + include_guard + "\n\n";
code += "#include \"flatbuffers/flatbuffers.h\"\n\n"; code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
if (parser.opts.include_dependence_headers) { if (parser_.opts.include_dependence_headers) {
int num_includes = 0; int num_includes = 0;
for (auto it = parser.included_files_.begin(); for (auto it = parser_.included_files_.begin();
it != parser.included_files_.end(); ++it) { it != parser_.included_files_.end(); ++it) {
auto basename = flatbuffers::StripPath( auto basename =
flatbuffers::StripExtension(it->first)); flatbuffers::StripPath(flatbuffers::StripExtension(it->first));
if (basename != file_name) { if (basename != file_name_) {
code += "#include \"" + basename + "_generated.h\"\n"; code += "#include \"" + basename + "_generated.h\"\n";
num_includes++; num_includes++;
}
} }
if (num_includes) code += "\n";
} }
if (num_includes) code += "\n";
}
assert(!code_generator_cur_name_space); assert(!code_generator_cur_name_space);
// Generate forward declarations for all structs/tables, since they may // Generate forward declarations for all structs/tables, since they may
// have circular references. // have circular references.
for (auto it = parser.structs_.vec.begin(); for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end();
it != parser.structs_.vec.end(); ++it) { ++it) {
auto &struct_def = **it; auto &struct_def = **it;
if (!struct_def.generated) { if (!struct_def.generated) {
CheckNameSpace(struct_def, &code); CheckNameSpace(struct_def, &code);
code += "struct " + struct_def.name + ";\n\n"; code += "struct " + struct_def.name + ";\n\n";
}
} }
}
// Generate code for all the enum declarations. // Generate code for all the enum declarations.
for (auto it = parser.enums_.vec.begin(); for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
it != parser.enums_.vec.end(); ++it) { ++it) {
auto &enum_def = **it; auto &enum_def = **it;
if (!enum_def.generated) { if (!enum_def.generated) {
CheckNameSpace(**it, &code); CheckNameSpace(**it, &code);
GenEnum(parser, **it, &code); GenEnum(parser_, **it, &code);
}
} }
}
// Generate code for all structs, then all tables. // Generate code for all structs, then all tables.
for (auto it = parser.structs_.vec.begin(); for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end();
it != parser.structs_.vec.end(); ++it) { ++it) {
auto &struct_def = **it; auto &struct_def = **it;
if (struct_def.fixed && !struct_def.generated) { if (struct_def.fixed && !struct_def.generated) {
CheckNameSpace(struct_def, &code); CheckNameSpace(struct_def, &code);
GenStruct(parser, struct_def, &code); GenStruct(parser_, struct_def, &code);
}
} }
} for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end();
for (auto it = parser.structs_.vec.begin(); ++it) {
it != parser.structs_.vec.end(); ++it) { auto &struct_def = **it;
auto &struct_def = **it; if (!struct_def.fixed && !struct_def.generated) {
if (!struct_def.fixed && !struct_def.generated) { CheckNameSpace(struct_def, &code);
CheckNameSpace(struct_def, &code); GenTable(parser_, struct_def, &code);
GenTable(parser, struct_def, &code); }
} }
}
// Generate code for union verifiers. // Generate code for union verifiers.
for (auto it = parser.enums_.vec.begin(); for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
it != parser.enums_.vec.end(); ++it) { ++it) {
auto &enum_def = **it; auto &enum_def = **it;
if (enum_def.is_union && !enum_def.generated) { if (enum_def.is_union && !enum_def.generated) {
CheckNameSpace(enum_def, &code); CheckNameSpace(enum_def, &code);
GenEnumPost(parser, enum_def, &code); GenEnumPost(parser_, enum_def, &code);
}
} }
}
// Generate convenient global helper functions: // Generate convenient global helper functions:
if (parser.root_struct_def_) { if (parser_.root_struct_def_) {
CheckNameSpace(*parser.root_struct_def_, &code); CheckNameSpace(*parser_.root_struct_def_, &code);
auto &name = parser.root_struct_def_->name; auto &name = parser_.root_struct_def_->name;
std::string qualified_name = std::string qualified_name =
parser.namespaces_.back()->GetFullyQualifiedName(name); parser_.namespaces_.back()->GetFullyQualifiedName(name);
std::string cpp_qualified_name = TranslateNameSpace(qualified_name); std::string cpp_qualified_name = TranslateNameSpace(qualified_name);
// The root datatype accessor:
code += "inline const " + cpp_qualified_name + " *Get";
code += name;
code += "(const void *buf) { return flatbuffers::GetRoot<";
code += cpp_qualified_name + ">(buf); }\n\n";
if (parser.opts.mutable_buffer) {
code += "inline " + name + " *GetMutable";
code += name;
code += "(void *buf) { return flatbuffers::GetMutableRoot<";
code += name + ">(buf); }\n\n";
}
// The root verifier: // The root datatype accessor:
code += "inline bool Verify"; code += "inline const " + cpp_qualified_name + " *Get";
code += name; code += name;
code += "Buffer(flatbuffers::Verifier &verifier) { " code += "(const void *buf) { return flatbuffers::GetRoot<";
"return verifier.VerifyBuffer<"; code += cpp_qualified_name + ">(buf); }\n\n";
code += cpp_qualified_name + ">(); }\n\n"; if (parser_.opts.mutable_buffer) {
code += "inline " + name + " *GetMutable";
if (parser.file_identifier_.length()) { code += name;
// Return the identifier code += "(void *buf) { return flatbuffers::GetMutableRoot<";
code += "inline const char *" + name; code += name + ">(buf); }\n\n";
code += "Identifier() { return \"" + parser.file_identifier_; }
code += "\"; }\n\n";
// Check if a buffer has the identifier.
code += "inline bool " + name;
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
code += "BufferHasIdentifier(buf, ";
code += name + "Identifier()); }\n\n";
}
if (parser.file_extension_.length()) { // The root verifier:
// Return the extension code += "inline bool Verify";
code += "inline const char *" + name; code += name;
code += "Extension() { return \"" + parser.file_extension_; code +=
code += "\"; }\n\n"; "Buffer(flatbuffers::Verifier &verifier) { "
} "return verifier.VerifyBuffer<";
code += cpp_qualified_name + ">(); }\n\n";
if (parser_.file_identifier_.length()) {
// Return the identifier
code += "inline const char *" + name;
code += "Identifier() { return \"" + parser_.file_identifier_;
code += "\"; }\n\n";
// Check if a buffer has the identifier.
code += "inline bool " + name;
code += "BufferHasIdentifier(const void *buf) { return flatbuffers::";
code += "BufferHasIdentifier(buf, ";
code += name + "Identifier()); }\n\n";
}
// Finish a buffer with a given root object: if (parser_.file_extension_.length()) {
code += "inline void Finish" + name; // Return the extension
code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<"; code += "inline const char *" + name;
code += cpp_qualified_name + "> root) { fbb.Finish(root"; code += "Extension() { return \"" + parser_.file_extension_;
if (parser.file_identifier_.length()) code += "\"; }\n\n";
code += ", " + name + "Identifier()"; }
code += "); }\n\n";
}
assert(code_generator_cur_name_space); // Finish a buffer with a given root object:
CloseNestedNameSpaces(code_generator_cur_name_space, &code); code += "inline void Finish" + name;
code +=
"Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<";
code += cpp_qualified_name + "> root) { fbb.Finish(root";
if (parser_.file_identifier_.length())
code += ", " + name + "Identifier()";
code += "); }\n\n";
}
code_generator_cur_name_space = nullptr; assert(code_generator_cur_name_space);
CloseNestedNameSpaces(code_generator_cur_name_space, &code);
// Close the include guard. code_generator_cur_name_space = nullptr;
code += "\n#endif // " + include_guard + "\n";
return code; // Close the include guard.
} code += "\n#endif // " + include_guard + "\n";
static std::string GeneratedFileName(const std::string &path, return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
const std::string &file_name) { }
return path + file_name + "_generated.h"; };
} } // namespace cpp
bool GenerateCPP(const Parser &parser, bool GenerateCPP(const Parser &parser, const std::string &path,
const std::string &path,
const std::string &file_name) { const std::string &file_name) {
auto code = GenerateCPP(parser, file_name); cpp::CppGenerator generator(parser, path, file_name);
return !code.length() || return generator.generate();
SaveFile(GeneratedFileName(path, file_name).c_str(), code, false);
} }
std::string CPPMakeRule(const Parser &parser, std::string CPPMakeRule(const Parser &parser,
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#include <algorithm> #include <algorithm>
namespace flatbuffers { namespace flatbuffers {
...@@ -1148,44 +1149,56 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser, ...@@ -1148,44 +1149,56 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
return SaveFile(filename.c_str(), code, false); return SaveFile(filename.c_str(), code, false);
} }
bool GenerateGeneral(const Parser &parser, namespace general {
const std::string &path, class GeneralGenerator : public BaseGenerator {
const std::string & file_name) { public:
GeneralGenerator(const Parser &parser, const std::string &path,
assert(parser.opts.lang <= IDLOptions::kMAX); const std::string &file_name)
auto lang = language_parameters[parser.opts.lang]; : BaseGenerator(parser, path, file_name){};
std::string one_file_code; bool generate() {
assert(parser_.opts.lang <= IDLOptions::kMAX);
for (auto it = parser.enums_.vec.begin(); auto lang = language_parameters[parser_.opts.lang];
it != parser.enums_.vec.end(); ++it) { std::string one_file_code;
std::string enumcode;
GenEnum(lang, parser, **it, &enumcode); for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
if (parser.opts.one_file) { ++it) {
one_file_code += enumcode; std::string enumcode;
} GenEnum(lang, parser_, **it, &enumcode);
else { if (parser_.opts.one_file) {
if (!SaveClass(lang, parser, (**it).name, enumcode, path, false, false)) one_file_code += enumcode;
return false; } else {
if (!SaveClass(lang, parser_, (**it).name, enumcode, path_, false,
false))
return false;
}
} }
}
for (auto it = parser.structs_.vec.begin(); for (auto it = parser_.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) { it != parser_.structs_.vec.end(); ++it) {
std::string declcode; std::string declcode;
GenStruct(lang, parser, **it, &declcode); GenStruct(lang, parser_, **it, &declcode);
if (parser.opts.one_file) { if (parser_.opts.one_file) {
one_file_code += declcode; one_file_code += declcode;
} else {
if (!SaveClass(lang, parser_, (**it).name, declcode, path_, true,
false))
return false;
}
} }
else {
if (!SaveClass(lang, parser, (**it).name, declcode, path, true, false)) if (parser_.opts.one_file) {
return false; return SaveClass(lang, parser_, file_name_, one_file_code, path_, true,
true);
} }
return true;
} }
};
} // namespace general
if (parser.opts.one_file) { bool GenerateGeneral(const Parser &parser, const std::string &path,
return SaveClass(lang, parser, file_name, one_file_code,path, true, true); const std::string &file_name) {
} general::GeneralGenerator generator(parser, path, file_name);
return true; return generator.generate();
} }
static std::string ClassFileName(const LanguageParameters &lang, static std::string ClassFileName(const LanguageParameters &lang,
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#ifdef _WIN32 #ifdef _WIN32
#include <direct.h> #include <direct.h>
...@@ -660,28 +661,35 @@ static void GenStructBuilder(const StructDef &struct_def, ...@@ -660,28 +661,35 @@ static void GenStructBuilder(const StructDef &struct_def,
EndBuilderBody(code_ptr); EndBuilderBody(code_ptr);
} }
} // namespace go class GoGenerator : public BaseGenerator {
public:
GoGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
: BaseGenerator(parser, path, file_name){};
bool generate() {
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
std::string enumcode;
go::GenEnum(**it, &enumcode);
if (!go::SaveType(parser_, **it, enumcode, path_, false)) return false;
}
bool GenerateGo(const Parser &parser, for (auto it = parser_.structs_.vec.begin();
const std::string &path, it != parser_.structs_.vec.end(); ++it) {
const std::string & /*file_name*/) { std::string declcode;
for (auto it = parser.enums_.vec.begin(); go::GenStruct(**it, &declcode, parser_.root_struct_def_);
it != parser.enums_.vec.end(); ++it) { if (!go::SaveType(parser_, **it, declcode, path_, true)) return false;
std::string enumcode; }
go::GenEnum(**it, &enumcode);
if (!go::SaveType(parser, **it, enumcode, path, false))
return false;
}
for (auto it = parser.structs_.vec.begin(); return true;
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
go::GenStruct(**it, &declcode, parser.root_struct_def_);
if (!go::SaveType(parser, **it, declcode, path, true))
return false;
} }
};
} // namespace go
return true; bool GenerateGo(const Parser &parser, const std::string &path,
const std::string &file_name) {
go::GoGenerator generator(parser, path, file_name);
return generator.generate();
} }
} // namespace flatbuffers } // namespace flatbuffers
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers { namespace flatbuffers {
namespace js { namespace js {
...@@ -661,60 +662,76 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -661,60 +662,76 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
} // namespace js } // 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, // Iterate through all definitions we haven't generate code for (enums, structs,
// and tables) and output them to a single file. // and tables) and output them to a single file.
std::string GenerateJS(const Parser &parser) { class JsGenerator : public BaseGenerator {
using namespace js; public:
JsGenerator(const Parser &parser, const std::string &path,
// Generate code for all the enum declarations. const std::string &file_name)
std::string enum_code, exports_code; : BaseGenerator(parser, path, file_name){};
for (auto it = parser.enums_.vec.begin(); // Iterate through all definitions we haven't generate code for (enums,
it != parser.enums_.vec.end(); ++it) { // structs, and tables) and output them to a single file.
GenEnum(**it, &enum_code, &exports_code); bool generate() {
} std::string enum_code, struct_code, exports_code, code;
generateEnums(&enum_code, &exports_code);
generateStructs(&struct_code, &exports_code);
// Only output file-level code if there were any declarations.
if (enum_code.length() || struct_code.length()) {
code +=
"// automatically generated by the FlatBuffers compiler, do not "
"modify\n\n";
// 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;
}
}
// Generate code for all structs, then all tables. return !code.length() ||
std::string decl_code; SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
GenStruct(parser, **it, &decl_code, &exports_code);
} }
// Only output file-level code if there were any declarations. private:
if (enum_code.length() || decl_code.length()) { // Generate code for all enums.
std::string code; void generateEnums(std::string *enum_code_ptr,
code = "// automatically generated by the FlatBuffers compiler," std::string *exports_code_ptr) {
" do not modify\n\n"; for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
// Generate code for all the namespace declarations. auto &enum_def = **it;
GenNamespaces(parser, &code, &exports_code); GenEnum(enum_def, enum_code_ptr, exports_code_ptr);
// Output the main declaration code from above.
code += enum_code;
code += decl_code;
if (!exports_code.empty() && !parser.opts.skip_js_exports) {
code += "// Exports for Node.js and RequireJS\n";
code += exports_code;
} }
return code;
} }
return std::string(); // Generate code for all structs.
} void generateStructs(std::string *decl_code_ptr,
std::string *exports_code_ptr) {
static std::string GeneratedFileName(const std::string &path, for (auto it = parser_.structs_.vec.begin();
const std::string &file_name) { it != parser_.structs_.vec.end(); ++it) {
return path + file_name + "_generated.js"; auto &struct_def = **it;
} GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr);
}
}
};
} // namespace js
bool GenerateJS(const Parser &parser, bool GenerateJS(const Parser &parser, const std::string &path,
const std::string &path,
const std::string &file_name) { const std::string &file_name) {
auto code = GenerateJS(parser); js::JsGenerator generator(parser, path, file_name);
return !code.length() || return generator.generate();
SaveFile(GeneratedFileName(path, file_name).c_str(), code, false);
} }
std::string JSMakeRule(const Parser &parser, std::string JSMakeRule(const Parser &parser,
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers { namespace flatbuffers {
namespace php { namespace php {
...@@ -974,29 +975,47 @@ namespace php { ...@@ -974,29 +975,47 @@ namespace php {
code += Indent + "}\n"; code += Indent + "}\n";
} }
} // namespace php class PhpGenerator : public BaseGenerator {
public:
bool GeneratePhp(const Parser &parser, PhpGenerator(const Parser &parser, const std::string &path,
const std::string &path, const std::string &file_name)
const std::string & /*file_name*/) { : BaseGenerator(parser, path, file_name){};
for (auto it = parser.enums_.vec.begin(); bool generate() {
it != parser.enums_.vec.end(); ++it) { if (!generateEnums()) return false;
std::string enumcode; if (!generateStructs()) return false;
php::GenEnum(**it, &enumcode); return true;
}
if (!php::SaveType(parser, **it, enumcode, path, false)) private:
return false; 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(parser_, enum_def, enumcode, path_, false))
return false;
}
return true;
}
for (auto it = parser.structs_.vec.begin(); bool generateStructs() {
it != parser.structs_.vec.end(); ++it) { for (auto it = parser_.structs_.vec.begin();
std::string declcode; it != parser_.structs_.vec.end(); ++it) {
php::GenStruct(parser, **it, &declcode); auto &struct_def = **it;
std::string declcode;
GenStruct(parser_, struct_def, &declcode);
if (!SaveType(parser_, struct_def, declcode, path_, true))
return false;
}
return true;
}
};
} // namespace php
if (!php::SaveType(parser, **it, declcode, path, true)) bool GeneratePhp(const Parser &parser, const std::string &path,
return false; const std::string &file_name) {
php::PhpGenerator generator(parser, path, file_name);
return generator.generate();
} }
} // namespace flatbuffers
return true;
}
} // namespace flatbuffers
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers { namespace flatbuffers {
namespace python { namespace python {
...@@ -634,28 +635,47 @@ static void GenStructBuilder(const StructDef &struct_def, ...@@ -634,28 +635,47 @@ static void GenStructBuilder(const StructDef &struct_def,
EndBuilderBody(code_ptr); EndBuilderBody(code_ptr);
} }
} // namespace python class PythonGenerator : public BaseGenerator {
public:
PythonGenerator(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;
}
bool GeneratePython(const Parser &parser, private:
const std::string &path, bool generateEnums() {
const std::string & /*file_name*/) { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
for (auto it = parser.enums_.vec.begin(); ++it) {
it != parser.enums_.vec.end(); ++it) { auto &enum_def = **it;
std::string enumcode; std::string enumcode;
python::GenEnum(**it, &enumcode); GenEnum(enum_def, &enumcode);
if (!python::SaveType(parser, **it, enumcode, path, false)) if (!SaveType(parser_, enum_def, enumcode, path_, false)) return false;
return false; }
return true;
} }
for (auto it = parser.structs_.vec.begin(); bool generateStructs() {
it != parser.structs_.vec.end(); ++it) { for (auto it = parser_.structs_.vec.begin();
std::string declcode; it != parser_.structs_.vec.end(); ++it) {
python::GenStruct(**it, &declcode, parser.root_struct_def_); auto &struct_def = **it;
if (!python::SaveType(parser, **it, declcode, path, true)) std::string declcode;
return false; GenStruct(struct_def, &declcode, parser_.root_struct_def_);
if (!SaveType(parser_, struct_def, declcode, path_, true)) return false;
}
return true;
} }
};
} // namespace python
return true; bool GeneratePython(const Parser &parser, const std::string &path,
const std::string &file_name) {
python::PythonGenerator generator(parser, path, file_name);
return generator.generate();
} }
} // namespace flatbuffers } // namespace flatbuffers
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