// Get the value of a vector's struct member. static void GetMemberOfVectorOfStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name); code += "(obj *" + TypeName(field); code += ", j int) bool " + OffsetPrefix(field); code += "\t\tx := rcv._tab.Vector(o)\n"; code += "\t\tx += flatbuffers.UOffsetT(j) * "; code += NumToString(InlineSize(vectortype)) + "\n"; if (!(vectortype.struct_def->fixed)) { code += "\t\tx = rcv._tab.Indirect(x)\n"; } code += "\t\tobj.Init(rcv._tab.Bytes, x)\n"; code += "\t\treturn true\n\t}\n"; code += "\treturn false\n"; code += "}\n\n"; }
// Get the value of a vector's non-struct member. Uses a named return // argument to conveniently set the zero value for the result. static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name); code += "(j int) " + TypeName(field) + " "; code += OffsetPrefix(field); code += "\t\ta := rcv._tab.Vector(o)\n"; code += "\t\treturn " + GenGetter(field.value.type) + "("; code += "a + flatbuffers.UOffsetT(j*"; code += NumToString(InlineSize(vectortype)) + "))\n"; code += "\t}\n"; if (vectortype.base_type == BASE_TYPE_STRING) { code += "\treturn nil\n"; } else { code += "\treturn 0\n"; } code += "}\n\n"; }
// Get the value of a vector's non-struct member. Uses a named return // argument to conveniently set the zero value for the result. static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); code += MakeCamel(field.name); code += "(self, j):"; code += OffsetPrefix(field); code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n"; code += Indent + Indent + Indent; code += "return " + GenGetter(field.value.type); code += "a + flatbuffers.number_types.UOffsetTFlags.py_type(j * "; code += NumToString(InlineSize(vectortype)) + "))\n"; if (vectortype.base_type == BASE_TYPE_STRING) { code += Indent + Indent + "return \"\"\n"; } else { code += Indent + Indent + "return 0\n"; } code += "\n"; }
// Get the value of a vector's struct member. static void GetMemberOfVectorOfStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; auto vectortype = field.value.type.VectorType(); GenReceiver(struct_def, code_ptr); code += MakeCamel(field.name); code += "(self, j):" + OffsetPrefix(field); code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n"; code += Indent + Indent + Indent; code += "x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * "; code += NumToString(InlineSize(vectortype)) + "\n"; if (!(vectortype.struct_def->fixed)) { code += Indent + Indent + Indent + "x = self._tab.Indirect(x)\n"; } code += Indent + Indent + Indent; code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n"; code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n"; code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n"; code += Indent + Indent + Indent + "return obj\n"; code += Indent + Indent + "return None\n\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"; }