// Recusively generate struct construction statements of the form: // builder.putType(name); // and insert manual padding. static void GenStructBody(const StructDef &struct_def, std::string *code_ptr, const char *nameprefix) { std::string &code = *code_ptr; code += " builder.prep(" + NumToString(struct_def.minalign) + ", 0);\n"; for (auto it = struct_def.fields.vec.rbegin(); it != struct_def.fields.vec.rend(); ++it) { auto &field = **it; if (field.padding) code += " builder.pad(" + NumToString(field.padding) + ");\n"; if (IsStruct(field.value.type)) { GenStructBody(*field.value.type.struct_def, code_ptr, (field.value.type.struct_def->name + "_").c_str()); } else { code += " builder.put" + GenMethod(field) + "("; code += nameprefix + MakeCamel(field.name, false) + ");\n"; } } }
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"; }