// Returns the function name that is able to read a value of the given type. static std::string GenGetter(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: return "rcv._tab.ByteVector"; case BASE_TYPE_UNION: return "rcv._tab.Union"; case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); default: return "rcv._tab.Get" + MakeCamel(GenTypeGet(type)); } }
// Returns the function name that is able to read a value of the given type. static std::string GenGetter(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: return "self._tab.String("; case BASE_TYPE_UNION: return "self._tab.Union("; case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); default: return "self._tab.Get(flatbuffers.number_types." + \ MakeCamel(GenTypeGet(type)) + \ "Flags, "; } }
// Returns the function name that is able to read a value of the given type. static std::string GenGetter(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: return "__string"; case BASE_TYPE_STRUCT: return "__struct"; case BASE_TYPE_UNION: return "__union"; case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); default: return "bb.get" + (SizeOf(type.base_type) > 1 ? MakeCamel(GenTypeGet(type)) : ""); } }
static std::string GenTypePointer(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: return "[]byte"; case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType()); case BASE_TYPE_STRUCT: return type.struct_def->name; case BASE_TYPE_UNION: // fall through default: return "*flatbuffers.Table"; } }
// Generate an accessor struct with constructor for a flatbuffers struct. static void GenStruct(const Parser &parser, StructDef &struct_def, std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; // Generate an accessor struct, with private variables of the form: // type name_; // Generates manual padding and alignment. // Variables are private because they contain little endian data on all // platforms. GenComment(struct_def.doc_comment, code_ptr); code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") "; code += struct_def.name + " FLATBUFFERS_FINAL_CLASS {\n private:\n"; int padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; code += " " + GenTypeGet(parser, field.value.type, " ", "", " ", false); code += field.name + "_;\n"; GenPadding(field, [&code, &padding_id](int bits) { code += " int" + NumToString(bits) + "_t __padding" + NumToString(padding_id++) + ";\n"; }); } // Generate a constructor that takes all fields as arguments. code += "\n public:\n " + struct_def.name + "("; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (it != struct_def.fields.vec.begin()) code += ", "; code += GenTypeGet(parser, field.value.type, " ", "const ", " &", true); code += field.name; } code += ")\n : "; padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (it != struct_def.fields.vec.begin()) code += ", "; code += field.name + "_("; if (IsScalar(field.value.type.base_type)) { code += "flatbuffers::EndianScalar("; code += GenUnderlyingCast(parser, field, false, field.name); code += "))"; } else { code += field.name + ")"; } GenPadding(field, [&code, &padding_id](int bits) { (void)bits; code += ", __padding" + NumToString(padding_id++) + "(0)"; }); } code += " {"; padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; GenPadding(field, [&code, &padding_id](int bits) { (void)bits; code += " (void)__padding" + NumToString(padding_id++) + ";"; }); } code += " }\n\n"; // Generate accessor methods of the form: // type name() const { return flatbuffers::EndianScalar(name_); } for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; GenComment(field.doc_comment, code_ptr, " "); code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " &", true); code += field.name + "() const { return "; code += GenUnderlyingCast(parser, field, true, IsScalar(field.value.type.base_type) ? "flatbuffers::EndianScalar(" + field.name + "_)" : field.name + "_"); code += "; }\n"; } code += "};\nSTRUCT_END(" + struct_def.name + ", "; code += NumToString(struct_def.bytesize) + ");\n\n"; }
// Generate an accessor struct, builder structs & function for a table. static void GenTable(const Parser &parser, StructDef &struct_def, const GeneratorOptions &opts, std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; // Generate an accessor struct, with methods of the form: // type name() const { return GetField<type>(offset, defaultval); } GenComment(struct_def.doc_comment, code_ptr); code += "struct " + struct_def.name; code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " {\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { // Deprecated fields won't be accessible. GenComment(field.doc_comment, code_ptr, " "); code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " *", true); code += field.name + "() const { return "; // Call a different accessor for pointers, that indirects. std::string call = IsScalar(field.value.type.base_type) ? "GetField<" : (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<"); call += GenTypeGet(parser, field.value.type, "", "const ", " *", false); call += ">(" + NumToString(field.value.offset); // Default value as second arg for non-pointer types. if (IsScalar(field.value.type.base_type)) call += ", " + field.value.constant; call += ")"; code += GenUnderlyingCast(parser, field, true, call); code += "; }\n"; auto nested = field.attributes.Lookup("nested_flatbuffer"); if (nested) { auto nested_root = parser.structs_.Lookup(nested->constant); assert(nested_root); // Guaranteed to exist by parser. code += " const " + nested_root->name + " *" + field.name; code += "_nested_root() const { return flatbuffers::GetRoot<"; code += nested_root->name + ">(" + field.name + "()->Data()); }\n"; } // Generate a comparison function for this field if it is a key. if (field.key) { code += " bool KeyCompareLessThan(const " + struct_def.name; code += " *o) const { return "; if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; code += field.name + "() < "; if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; code += "o->" + field.name + "(); }\n"; code += " int KeyCompareWithValue("; if (field.value.type.base_type == BASE_TYPE_STRING) { code += "const char *val) const { return strcmp(" + field.name; code += "()->c_str(), val); }\n"; } else { code += GenTypeBasic(parser, field.value.type, false); code += " val) const { return " + field.name + "() < val ? -1 : "; code += field.name + "() > val; }\n"; } } } } // Generate a verifier function that can check a buffer from an untrusted // source will never cause reads outside the buffer. code += " bool Verify(flatbuffers::Verifier &verifier) const {\n"; code += " return VerifyTableStart(verifier)"; std::string prefix = " &&\n "; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += prefix + "VerifyField"; if (field.required) code += "Required"; code += "<" + GenTypeSize(parser, field.value.type); code += ">(verifier, " + NumToString(field.value.offset); code += " /* " + field.name + " */)"; switch (field.value.type.base_type) { case BASE_TYPE_UNION: code += prefix + "Verify" + field.value.type.enum_def->name; code += "(verifier, " + field.name + "(), " + field.name + "_type())"; break; case BASE_TYPE_STRUCT: if (!field.value.type.struct_def->fixed) { code += prefix + "verifier.VerifyTable(" + field.name; code += "())"; } break; case BASE_TYPE_STRING: code += prefix + "verifier.Verify(" + field.name + "())"; break; case BASE_TYPE_VECTOR: code += prefix + "verifier.Verify(" + field.name + "())"; switch (field.value.type.element) { case BASE_TYPE_STRING: { code += prefix + "verifier.VerifyVectorOfStrings(" + field.name; code += "())"; break; } case BASE_TYPE_STRUCT: { if (!field.value.type.struct_def->fixed) { code += prefix + "verifier.VerifyVectorOfTables(" + field.name; code += "())"; } break; } default: break; } break; default: break; } } } code += prefix + "verifier.EndTable()"; code += ";\n }\n"; code += "};\n\n"; // Generate a builder struct, with methods of the form: // void add_name(type name) { fbb_.AddElement<type>(offset, name, default); } code += "struct " + struct_def.name; code += "Builder {\n flatbuffers::FlatBufferBuilder &fbb_;\n"; code += " flatbuffers::uoffset_t start_;\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += " void add_" + field.name + "("; code += GenTypeWire(parser, field.value.type, " ", true) + field.name; code += ") { fbb_.Add"; if (IsScalar(field.value.type.base_type)) { code += "Element<" + GenTypeWire(parser, field.value.type, "", false); code += ">"; } else if (IsStruct(field.value.type)) { code += "Struct"; } else { code += "Offset"; } code += "(" + NumToString(field.value.offset) + ", "; code += GenUnderlyingCast(parser, field, false, field.name); if (IsScalar(field.value.type.base_type)) code += ", " + field.value.constant; code += "); }\n"; } } code += " " + struct_def.name; code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) "; code += "{ start_ = fbb_.StartTable(); }\n"; code += " " + struct_def.name + "Builder &operator=(const "; code += struct_def.name + "Builder &);\n"; code += " flatbuffers::Offset<" + struct_def.name; code += "> Finish() {\n auto o = flatbuffers::Offset<" + struct_def.name; code += ">(fbb_.EndTable(start_, "; code += NumToString(struct_def.fields.vec.size()) + "));\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated && field.required) { code += " fbb_.Required(o, " + NumToString(field.value.offset); code += "); // " + field.name + "\n"; } } code += " return o;\n }\n};\n\n"; // Generate a convenient CreateX function that uses the above builder // to create a table in one go. code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; code += "(flatbuffers::FlatBufferBuilder &_fbb"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += ",\n " + GenTypeWire(parser, field.value.type, " ", true); code += field.name + " = "; if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { auto ev = field.value.type.enum_def->ReverseLookup( static_cast<int>(StringToInt(field.value.constant.c_str())), false); if (ev) { code += WrapInNameSpace(parser, field.value.type.enum_def->defined_namespace, GenEnumVal(*field.value.type.enum_def, *ev, opts)); } else { code += GenUnderlyingCast(parser, field, true, field.value.constant); } } else { code += field.value.constant; } } } code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size; size /= 2) { for (auto it = struct_def.fields.vec.rbegin(); it != struct_def.fields.vec.rend(); ++it) { auto &field = **it; if (!field.deprecated && (!struct_def.sortbysize || size == SizeOf(field.value.type.base_type))) { code += " builder_.add_" + field.name + "(" + field.name + ");\n"; } } } code += " return builder_.Finish();\n}\n\n"; }
static std::string TypeName(const FieldDef &field) { return GenTypeGet(field.value.type); }
static void GenStruct(StructDef &struct_def, std::string *code_ptr, StructDef *root_struct_def) { if (struct_def.generated) return; std::string &code = *code_ptr; // Generate a struct accessor class, with methods of the form: // public type name() { return bb.getType(i + offset); } // or for tables of the form: // public type name() { // int o = __offset(offset); return o != 0 ? bb.getType(o + i) : default; // } GenComment(struct_def.doc_comment, code_ptr); code += "public class " + struct_def.name + " extends "; code += struct_def.fixed ? "Struct" : "Table"; code += " {\n"; if (&struct_def == root_struct_def) { // Generate a special accessor for the table that has been declared as // the root type. code += " public static " + struct_def.name + " getRootAs"; code += struct_def.name; code += "(ByteBuffer _bb, int offset) { "; code += "_bb.order(ByteOrder.LITTLE_ENDIAN); "; code += "return (new " + struct_def.name; code += "()).__init(_bb.getInt(offset) + offset, _bb); }\n"; } // Generate the __init method that sets the field in a pre-existing // accessor object. This is to allow object reuse. code += " public " + struct_def.name; code += " __init(int _i, ByteBuffer _bb) "; code += "{ bb_pos = _i; bb = _bb; return this; }\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (field.deprecated) continue; GenComment(field.doc_comment, code_ptr, " "); std::string type_name = GenTypeGet(field.value.type); std::string method_start = " public " + type_name + " " + MakeCamel(field.name, false); // Generate the accessors that don't do object reuse. if (field.value.type.base_type == BASE_TYPE_STRUCT) { // Calls the accessor that takes an accessor object with a new object. code += method_start + "() { return " + MakeCamel(field.name, false); code += "(new "; code += type_name + "()); }\n"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR && field.value.type.element == BASE_TYPE_STRUCT) { // Accessors for vectors of structs also take accessor objects, this // generates a variant without that argument. code += method_start + "(int j) { return " + MakeCamel(field.name, false); code += "(new "; code += type_name + "(), j); }\n"; } std::string getter = GenGetter(field.value.type); code += method_start + "("; // Most field accessors need to retrieve and test the field offset first, // this is the prefix code for that: auto offset_prefix = ") { int o = __offset(" + NumToString(field.value.offset) + "); return o != 0 ? "; if (IsScalar(field.value.type.base_type)) { if (struct_def.fixed) { code += ") { return " + getter; code += "(bb_pos + " + NumToString(field.value.offset) + ")"; } else { code += offset_prefix + getter; code += "(o + bb_pos) : " + field.value.constant; } } else { switch (field.value.type.base_type) { case BASE_TYPE_STRUCT: code += type_name + " obj"; if (struct_def.fixed) { code += ") { return obj.__init(bb_pos + "; code += NumToString(field.value.offset) + ", bb)"; } else { code += offset_prefix; code += "obj.__init("; code += field.value.type.struct_def->fixed ? "o + bb_pos" : "__indirect(o + bb_pos)"; code += ", bb) : null"; } break; case BASE_TYPE_STRING: code += offset_prefix + getter +"(o) : null"; break; case BASE_TYPE_VECTOR: { auto vectortype = field.value.type.VectorType(); if (vectortype.base_type == BASE_TYPE_STRUCT) { code += type_name + " obj, "; getter = "obj.__init"; } code += "int j" + offset_prefix + getter +"("; auto index = "__vector(o) + j * " + NumToString(InlineSize(vectortype)); if (vectortype.base_type == BASE_TYPE_STRUCT) { code += vectortype.struct_def->fixed ? index : "__indirect(" + index + ")"; code += ", bb"; } else { code += index; } code += ") : "; code += IsScalar(field.value.type.element) ? "0" : "null"; break; } case BASE_TYPE_UNION: code += type_name + " obj" + offset_prefix + getter; code += "(obj, o) : null"; break; default: assert(0); } } code += "; }\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { code += " public int " + MakeCamel(field.name, false) + "Length("; code += offset_prefix; code += "__vector_len(o) : 0; }\n"; } } code += "\n"; if (struct_def.fixed) { // create a struct constructor function code += " public static int create" + struct_def.name; code += "(FlatBufferBuilder builder"; GenStructArgs(struct_def, code_ptr, ""); code += ") {\n"; GenStructBody(struct_def, code_ptr, ""); code += " return builder.offset();\n }\n"; } else { // Create a set of static methods that allow table construction, // of the form: // public static void addName(FlatBufferBuilder builder, short name) // { builder.addShort(id, name, default); } code += " public static void start" + struct_def.name; code += "(FlatBufferBuilder builder) { builder.startObject("; code += NumToString(struct_def.fields.vec.size()) + "); }\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (field.deprecated) continue; code += " public static void add" + MakeCamel(field.name); code += "(FlatBufferBuilder builder, " + GenTypeBasic(field.value.type); auto argname = MakeCamel(field.name, false); if (!IsScalar(field.value.type.base_type)) argname += "Offset"; code += " " + argname + ") { builder.add"; code += GenMethod(field) + "("; code += NumToString(it - struct_def.fields.vec.begin()) + ", "; code += argname + ", " + field.value.constant; code += "); }\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { code += " public static void start" + MakeCamel(field.name); code += "Vector(FlatBufferBuilder builder, int numElems) "; code += "{ builder.startVector("; code += NumToString(InlineSize(field.value.type.VectorType())); code += ", numElems); }\n"; } } code += " public static int end" + struct_def.name; code += "(FlatBufferBuilder builder) { return builder.endObject(); }\n"; } code += "};\n\n"; }
// Generate an accessor struct with constructor for a flatbuffers struct. static void GenStruct(StructDef &struct_def, std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; // Generate an accessor struct, with private variables of the form: // type name_; // Generates manual padding and alignment. // Variables are private because they contain little endian data on all // platforms. GenComment(struct_def.doc_comment, code_ptr); code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") "; code += struct_def.name + " {\n private:\n"; int padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; code += " " + GenTypeGet(field.value.type, " ", "", " "); code += field.name + "_;\n"; if (field.padding) { for (int i = 0; i < 4; i++) if (static_cast<int>(field.padding) & (1 << i)) code += " int" + NumToString((1 << i) * 8) + "_t __padding" + NumToString(padding_id++) + ";\n"; assert(!(field.padding & ~0xF)); } } // Generate a constructor that takes all fields as arguments. code += "\n public:\n " + struct_def.name + "("; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (it != struct_def.fields.vec.begin()) code += ", "; code += GenTypeGet(field.value.type, " ", "const ", " &") + field.name; } code += ")\n : "; padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (it != struct_def.fields.vec.begin()) code += ", "; code += field.name + "_("; if (IsScalar(field.value.type.base_type)) code += "flatbuffers::EndianScalar(" + field.name + "))"; else code += field.name + ")"; if (field.padding) code += ", __padding" + NumToString(padding_id++) + "(0)"; } code += " {}\n\n"; // Generate accessor methods of the form: // type name() const { return flatbuffers::EndianScalar(name_); } for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; GenComment(field.doc_comment, code_ptr, " "); code += " " + GenTypeGet(field.value.type, " ", "const ", " &"); code += field.name + "() const { return "; if (IsScalar(field.value.type.base_type)) code += "flatbuffers::EndianScalar(" + field.name + "_)"; else code += field.name + "_"; code += "; }\n"; } code += "};\nSTRUCT_END(" + struct_def.name + ", "; code += NumToString(struct_def.bytesize) + ");\n\n"; }
// Generate an accessor struct, builder structs & function for a table. static void GenTable(StructDef &struct_def, std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; // Generate an accessor struct, with methods of the form: // type name() const { return GetField<type>(offset, defaultval); } GenComment(struct_def.doc_comment, code_ptr); code += "struct " + struct_def.name + " : private flatbuffers::Table"; code += " {\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { // Deprecated fields won't be accessible. GenComment(field.doc_comment, code_ptr, " "); code += " " + GenTypeGet(field.value.type, " ", "const ", " *"); code += field.name + "() const { return "; // Call a different accessor for pointers, that indirects. code += IsScalar(field.value.type.base_type) ? "GetField<" : (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<"); code += GenTypeGet(field.value.type, "", "const ", " *") + ">("; code += NumToString(field.value.offset); // Default value as second arg for non-pointer types. if (IsScalar(field.value.type.base_type)) code += ", " + field.value.constant; code += "); }\n"; } } // Generate a verifier function that can check a buffer from an untrusted // source will never cause reads outside the buffer. code += " bool Verify(const flatbuffers::Verifier &verifier) const {\n"; code += " return VerifyTable(verifier)"; std::string prefix = " &&\n "; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += prefix + "VerifyField<" + GenTypeSize(field.value.type); code += ">(verifier, " + NumToString(field.value.offset); code += " /* " + field.name + " */)"; switch (field.value.type.base_type) { case BASE_TYPE_UNION: code += prefix + "Verify" + field.value.type.enum_def->name; code += "(verifier, " + field.name + "(), " + field.name + "_type())"; break; case BASE_TYPE_STRUCT: if (!field.value.type.struct_def->fixed) { code += prefix + "verifier.VerifyTable(" + field.name; code += "())"; } break; case BASE_TYPE_STRING: code += prefix + "verifier.Verify(" + field.name + "())"; break; case BASE_TYPE_VECTOR: code += prefix + "verifier.Verify(" + field.name + "())"; switch (field.value.type.element) { case BASE_TYPE_STRING: { code += prefix + "verifier.VerifyVectorOfStrings(" + field.name; code += "())"; break; } case BASE_TYPE_STRUCT: { if (!field.value.type.struct_def->fixed) { code += prefix + "verifier.VerifyVectorOfTables(" + field.name; code += "())"; } break; } default: break; } break; default: break; } } } code += ";\n }\n"; code += "};\n\n"; // Generate a builder struct, with methods of the form: // void add_name(type name) { fbb_.AddElement<type>(offset, name, default); } code += "struct " + struct_def.name; code += "Builder {\n flatbuffers::FlatBufferBuilder &fbb_;\n"; code += " flatbuffers::uoffset_t start_;\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += " void add_" + field.name + "("; code += GenTypeWire(field.value.type, " ") + field.name + ") { fbb_.Add"; if (IsScalar(field.value.type.base_type)) code += "Element<" + GenTypeWire(field.value.type, "") + ">"; else if (IsStruct(field.value.type)) code += "Struct"; else code += "Offset"; code += "(" + NumToString(field.value.offset) + ", " + field.name; if (IsScalar(field.value.type.base_type)) code += ", " + field.value.constant; code += "); }\n"; } } code += " " + struct_def.name; code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) "; code += "{ start_ = fbb_.StartTable(); }\n"; code += " " + struct_def.name + "Builder &operator=(const "; code += struct_def.name + "Builder &);\n"; code += " flatbuffers::Offset<" + struct_def.name; code += "> Finish() { return flatbuffers::Offset<" + struct_def.name; code += ">(fbb_.EndTable(start_, "; code += NumToString(struct_def.fields.vec.size()) + ")); }\n};\n\n"; // Generate a convenient CreateX function that uses the above builder // to create a table in one go. code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; code += "(flatbuffers::FlatBufferBuilder &_fbb"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (!field.deprecated) { code += ", " + GenTypeWire(field.value.type, " ") + field.name; } } code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size; size /= 2) { for (auto it = struct_def.fields.vec.rbegin(); it != struct_def.fields.vec.rend(); ++it) { auto &field = **it; if (!field.deprecated && (!struct_def.sortbysize || size == SizeOf(field.value.type.base_type))) { code += " builder_.add_" + field.name + "(" + field.name + ");\n"; } } } code += " return builder_.Finish();\n}\n\n"; }