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)
endif()
set(FlatBuffers_Library_SRCS
include/flatbuffers/code_generators.h
include/flatbuffers/flatbuffers.h
include/flatbuffers/hash.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_
This diff is collapsed.
......@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#include <algorithm>
namespace flatbuffers {
......@@ -1148,44 +1149,56 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
return SaveFile(filename.c_str(), code, false);
}
bool GenerateGeneral(const Parser &parser,
const std::string &path,
const std::string & file_name) {
assert(parser.opts.lang <= IDLOptions::kMAX);
auto lang = language_parameters[parser.opts.lang];
std::string one_file_code;
for (auto it = parser.enums_.vec.begin();
it != parser.enums_.vec.end(); ++it) {
std::string enumcode;
GenEnum(lang, parser, **it, &enumcode);
if (parser.opts.one_file) {
one_file_code += enumcode;
}
else {
if (!SaveClass(lang, parser, (**it).name, enumcode, path, false, false))
return false;
namespace general {
class GeneralGenerator : public BaseGenerator {
public:
GeneralGenerator(const Parser &parser, const std::string &path,
const std::string &file_name)
: BaseGenerator(parser, path, file_name){};
bool generate() {
assert(parser_.opts.lang <= IDLOptions::kMAX);
auto lang = language_parameters[parser_.opts.lang];
std::string one_file_code;
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
++it) {
std::string enumcode;
GenEnum(lang, parser_, **it, &enumcode);
if (parser_.opts.one_file) {
one_file_code += enumcode;
} else {
if (!SaveClass(lang, parser_, (**it).name, enumcode, path_, false,
false))
return false;
}
}
}
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
GenStruct(lang, parser, **it, &declcode);
if (parser.opts.one_file) {
one_file_code += declcode;
for (auto it = parser_.structs_.vec.begin();
it != parser_.structs_.vec.end(); ++it) {
std::string declcode;
GenStruct(lang, parser_, **it, &declcode);
if (parser_.opts.one_file) {
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))
return false;
if (parser_.opts.one_file) {
return SaveClass(lang, parser_, file_name_, one_file_code, path_, true,
true);
}
return true;
}
};
} // namespace general
if (parser.opts.one_file) {
return SaveClass(lang, parser, file_name, one_file_code,path, true, true);
}
return true;
bool GenerateGeneral(const Parser &parser, const std::string &path,
const std::string &file_name) {
general::GeneralGenerator generator(parser, path, file_name);
return generator.generate();
}
static std::string ClassFileName(const LanguageParameters &lang,
......
......@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
#ifdef _WIN32
#include <direct.h>
......@@ -660,28 +661,35 @@ static void GenStructBuilder(const StructDef &struct_def,
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,
const std::string &path,
const std::string & /*file_name*/) {
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;
}
for (auto it = parser_.structs_.vec.begin();
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;
}
for (auto it = parser.structs_.vec.begin();
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;
return true;
}
};
} // 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
......
......@@ -19,6 +19,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
namespace js {
......@@ -661,60 +662,76 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
} // 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.
std::string GenerateJS(const Parser &parser) {
using namespace js;
// Generate code for all the enum declarations.
std::string enum_code, exports_code;
for (auto it = parser.enums_.vec.begin();
it != parser.enums_.vec.end(); ++it) {
GenEnum(**it, &enum_code, &exports_code);
}
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() {
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.
std::string decl_code;
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
GenStruct(parser, **it, &decl_code, &exports_code);
return !code.length() ||
SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false);
}
// Only output file-level code if there were any declarations.
if (enum_code.length() || decl_code.length()) {
std::string code;
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 += decl_code;
if (!exports_code.empty() && !parser.opts.skip_js_exports) {
code += "// Exports for Node.js and RequireJS\n";
code += exports_code;
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);
}
return code;
}
return std::string();
}
static std::string GeneratedFileName(const std::string &path,
const std::string &file_name) {
return path + file_name + "_generated.js";
}
// 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
bool GenerateJS(const Parser &parser,
const std::string &path,
bool GenerateJS(const Parser &parser, const std::string &path,
const std::string &file_name) {
auto code = GenerateJS(parser);
return !code.length() ||
SaveFile(GeneratedFileName(path, file_name).c_str(), code, false);
js::JsGenerator generator(parser, path, file_name);
return generator.generate();
}
std::string JSMakeRule(const Parser &parser,
......
......@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
namespace php {
......@@ -974,29 +975,47 @@ namespace php {
code += Indent + "}\n";
}
} // namespace php
bool GeneratePhp(const Parser &parser,
const std::string &path,
const std::string & /*file_name*/) {
for (auto it = parser.enums_.vec.begin();
it != parser.enums_.vec.end(); ++it) {
std::string enumcode;
php::GenEnum(**it, &enumcode);
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;
}
if (!php::SaveType(parser, **it, enumcode, path, false))
return false;
}
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(parser_, enum_def, enumcode, path_, false))
return false;
}
return true;
}
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
php::GenStruct(parser, **it, &declcode);
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(parser_, struct_def, declcode, path_, true))
return false;
}
return true;
}
};
} // namespace php
if (!php::SaveType(parser, **it, declcode, path, true))
return false;
bool GeneratePhp(const Parser &parser, const std::string &path,
const std::string &file_name) {
php::PhpGenerator generator(parser, path, file_name);
return generator.generate();
}
return true;
}
} // namespace flatbuffers
} // namespace flatbuffers
......@@ -21,6 +21,7 @@
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "flatbuffers/code_generators.h"
namespace flatbuffers {
namespace python {
......@@ -634,28 +635,47 @@ static void GenStructBuilder(const StructDef &struct_def,
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,
const std::string &path,
const std::string & /*file_name*/) {
for (auto it = parser.enums_.vec.begin();
it != parser.enums_.vec.end(); ++it) {
std::string enumcode;
python::GenEnum(**it, &enumcode);
if (!python::SaveType(parser, **it, enumcode, path, false))
return false;
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(parser_, enum_def, enumcode, path_, false)) return false;
}
return true;
}
for (auto it = parser.structs_.vec.begin();
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
python::GenStruct(**it, &declcode, parser.root_struct_def_);
if (!python::SaveType(parser, **it, declcode, path, true))
return false;
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, 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
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