This commit is contained in:
Wouter van Oortmerssen
2016-04-04 16:55:25 -07:00
11 changed files with 114 additions and 38 deletions

View File

@@ -96,31 +96,32 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
: " ",
generators[i].generator_help);
printf(
" -o PATH Prefix PATH to all generated files.\n"
" -I PATH Search for includes in the specified path.\n"
" -M Print make rules for generated files.\n"
" --version Print the version number of flatc and exit.\n"
" --strict-json Strict JSON: field names must be / will be quoted,\n"
" no trailing commas in tables/vectors.\n"
" --defaults-json Output fields whose value is the default when\n"
" writing JSON\n"
" --unknown-json Allow fields in JSON that are not defined in the\n"
" schema. These fields will be discared when generating\n"
" binaries.\n"
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
" also implies --no-prefix.\n"
" --gen-includes (deprecated), this is the default behavior.\n"
" If the original behavior is required (no include\n"
" statements) use --no-includes.\n"
" --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"
" --schema Serialize schemas instead of JSON (use with -b)\n"
" -o PATH Prefix PATH to all generated files.\n"
" -I PATH Search for includes in the specified path.\n"
" -M Print make rules for generated files.\n"
" --version Print the version number of flatc and exit.\n"
" --strict-json Strict JSON: field names must be / will be quoted,\n"
" no trailing commas in tables/vectors.\n"
" --defaults-json Output fields whose value is the default when\n"
" writing JSON\n"
" --unknown-json Allow fields in JSON that are not defined in the\n"
" schema. These fields will be discared when generating\n"
" binaries.\n"
" --no-prefix Don\'t prefix enum values with the enum type in C++.\n"
" --scoped-enums Use C++11 style scoped and strongly typed enums.\n"
" also implies --no-prefix.\n"
" --gen-includes (deprecated), this is the default behavior.\n"
" If the original behavior is required (no include\n"
" statements) use --no-includes.\n"
" --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"
" --gen-name-strings Generate type name functions 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"
" --schema Serialize schemas instead of JSON (use with -b)\n"
"FILEs may be schemas, or JSON files (conforming to preceding schema)\n"
"FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n"
@@ -171,6 +172,8 @@ int main(int argc, const char *argv[]) {
opts.scoped_enums = true;
} else if(arg == "--gen-mutable") {
opts.mutable_buffer = true;
} else if(arg == "--gen-name-strings") {
opts.generate_name_strings = true;
} else if(arg == "--gen-all") {
opts.generate_all = true;
opts.include_dependence_headers = false;

View File

@@ -267,6 +267,14 @@ std::string GenFieldOffsetName(const FieldDef &field) {
return "VT_" + uname;
}
static void GenFullyQualifiedNameGetter(const Parser &parser, const std::string& name, std::string &code) {
if (parser.opts.generate_name_strings) {
code += " static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {\n";
code += " return \"" + parser.namespaces_.back()->GetFullyQualifiedName(name) + "\";\n";
code += " }\n";
}
}
// Generate an accessor struct, builder structs & function for a table.
static void GenTable(const Parser &parser, StructDef &struct_def,
std::string *code_ptr) {
@@ -277,6 +285,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
code += "struct " + struct_def.name;
code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table";
code += " {\n";
// Generate GetFullyQualifiedName
GenFullyQualifiedNameGetter(parser, struct_def.name, code);
// Generate field id constants.
if (struct_def.fields.vec.size() > 0) {
code += " enum {\n";
@@ -583,8 +593,12 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
GenPadding(field, code, padding_id, PaddingDefinition);
}
// Generate GetFullyQualifiedName
code += "\n public:\n";
GenFullyQualifiedNameGetter(parser, struct_def.name, code);
// Generate a constructor that takes all fields as arguments.
code += "\n public:\n " + struct_def.name + "(";
code += " " + struct_def.name + "(";
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end();
++it) {

View File

@@ -165,6 +165,22 @@ namespace php {
code += Indent + "}\n\n";
}
// Get a [ubyte] vector as a byte array.
static void GetUByte(const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
code += Indent + "/**\n";
code += Indent + " * @return string\n";
code += Indent + " */\n";
code += Indent + "public function get";
code += MakeCamel(field.name) + "Bytes()\n";
code += Indent + "{\n";
code += Indent + Indent + "return $this->__vector_as_bytes(";
code += NumToString(field.value.offset) + ");\n";
code += Indent + "}\n\n";
}
// Get the value of a struct's scalar.
static void GetScalarFieldOfStruct(const FieldDef &field,
std::string *code_ptr) {
@@ -250,7 +266,7 @@ namespace php {
");\n";
code += Indent + Indent;
code += "return $o != 0 ? $obj->init(";
if (field.value.type.struct_def->fixed)
if (field.value.type.struct_def->fixed)
{
code += "$o + $this->bb_pos, $this->bb) : ";
} else {
@@ -690,6 +706,9 @@ namespace php {
}
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
GetVectorLen(field, code_ptr);
if (field.value.type.element == BASE_TYPE_UCHAR) {
GetUByte(field, code_ptr);
}
}
}