mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 04:21:13 +00:00
Merge pull request #217 from amoldeshpande/master
option to generate one file for C#
This commit is contained in:
@@ -58,6 +58,8 @@ be generated for each file processed:
|
||||
- `--gen-mutable` : Generate additional non-const accessors for mutating
|
||||
FlatBuffers in-place.
|
||||
|
||||
- `--gen-onefile` : Generate single output file (useful for C#)
|
||||
|
||||
- `--raw-binary` : Allow binaries without a file_indentifier to be read.
|
||||
This may crash flatc given a mismatched schema.
|
||||
|
||||
|
||||
@@ -427,6 +427,7 @@ struct GeneratorOptions {
|
||||
bool prefixed_enums;
|
||||
bool include_dependence_headers;
|
||||
bool mutable_buffer;
|
||||
bool one_file;
|
||||
|
||||
// Possible options for the more general generator below.
|
||||
enum Language { kJava, kCSharp, kGo, kMAX };
|
||||
@@ -439,6 +440,7 @@ struct GeneratorOptions {
|
||||
output_enum_identifiers(true), prefixed_enums(true),
|
||||
include_dependence_headers(true),
|
||||
mutable_buffer(false),
|
||||
one_file(false),
|
||||
lang(GeneratorOptions::kJava) {}
|
||||
};
|
||||
|
||||
|
||||
@@ -94,6 +94,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
|
||||
" --no-includes Don\'t generate include statements for included\n"
|
||||
" schemas the generated file depends on (C++).\n"
|
||||
" --gen-mutable Generate accessors that can mutate buffers in-place.\n"
|
||||
" --gen-onefile Generate single output file for C#\n"
|
||||
" --raw-binary Allow binaries without file_indentifier to be read.\n"
|
||||
" This may crash flatc given a mismatched schema.\n"
|
||||
" --proto Input is a .proto, translate to .fbs.\n"
|
||||
@@ -146,7 +147,9 @@ int main(int argc, const char *argv[]) {
|
||||
printf("warning: --gen-includes is deprecated (it is now default)\n");
|
||||
} else if(arg == "--no-includes") {
|
||||
opts.include_dependence_headers = false;
|
||||
} else if(arg == "--raw-binary") {
|
||||
} else if (arg == "--gen-onefile") {
|
||||
opts.one_file = true;
|
||||
} else if (arg == "--raw-binary") {
|
||||
raw_binary = true;
|
||||
} else if(arg == "--") { // Separator between text and binary inputs.
|
||||
binary_files_from = filenames.size();
|
||||
|
||||
@@ -858,8 +858,8 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
// Save out the generated code for a single class while adding
|
||||
// declaration boilerplate.
|
||||
static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
|
||||
const Definition &def, const std::string &classcode,
|
||||
const std::string &path, bool needs_includes) {
|
||||
const std::string &defname, const std::string &classcode,
|
||||
const std::string &path, bool needs_includes, bool onefile) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string namespace_general;
|
||||
@@ -870,7 +870,10 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
|
||||
namespace_general += ".";
|
||||
}
|
||||
namespace_general += *it;
|
||||
namespace_dir += *it + kPathSeparator;
|
||||
if (!onefile) {
|
||||
namespace_dir += *it + kPathSeparator;
|
||||
}
|
||||
|
||||
}
|
||||
EnsureDirExists(namespace_dir);
|
||||
|
||||
@@ -882,34 +885,48 @@ static bool SaveClass(const LanguageParameters &lang, const Parser &parser,
|
||||
if (needs_includes) code += lang.includes;
|
||||
code += classcode;
|
||||
if (!namespace_general.empty()) code += lang.namespace_end;
|
||||
auto filename = namespace_dir + def.name + lang.file_extension;
|
||||
auto filename = namespace_dir + defname + lang.file_extension;
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
bool GenerateGeneral(const Parser &parser,
|
||||
const std::string &path,
|
||||
const std::string & /*file_name*/,
|
||||
const std::string & file_name,
|
||||
const GeneratorOptions &opts) {
|
||||
|
||||
assert(opts.lang <= GeneratorOptions::kMAX);
|
||||
auto lang = language_parameters[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, **it, &enumcode);
|
||||
if (!SaveClass(lang, parser, **it, enumcode, path, false))
|
||||
return false;
|
||||
if (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 (!SaveClass(lang, parser, **it, declcode, path, true))
|
||||
return false;
|
||||
if (opts.one_file) {
|
||||
one_file_code += declcode;
|
||||
}
|
||||
else {
|
||||
if (!SaveClass(lang, parser, (**it).name, declcode, path, true, false))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.one_file) {
|
||||
return SaveClass(lang, parser, file_name, one_file_code,path, true, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user