// 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)); } }
// Get the value of a struct's scalar. static void GetScalarFieldOfStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; std::string getter = GenGetter(field.value.type); GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name); code += "() " + TypeName(field) + " { return " + getter; code += "(rcv._tab.Pos + flatbuffers.UOffsetT("; code += NumToString(field.value.offset) + ")) }\n"; }
// Get the value of a string. static void GetStringField(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name); code += "() " + TypeName(field) + " "; code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type); code += "(o + rcv._tab.Pos)\n\t}\n\treturn nil\n"; code += "}\n\n"; }
// 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, "; } }
// Get the value of a struct's scalar. static void GetScalarFieldOfStruct(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; std::string getter = GenGetter(field.value.type); GenReceiver(struct_def, code_ptr); code += MakeCamel(field.name); code += "(self): return " + getter; code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type("; code += NumToString(field.value.offset) + "))\n"; }
// 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)) : ""); } }
// Get the value of a string. static void GetStringField(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; GenReceiver(struct_def, code_ptr); code += MakeCamel(field.name); code += "(self):"; code += OffsetPrefix(field); code += Indent + Indent + Indent + "return " + GenGetter(field.value.type); code += "o + self._tab.Pos)\n"; code += Indent + Indent + "return \"\"\n\n"; }
// Get the value of a union from an object. static void GetUnionField(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name) + "("; code += "obj " + TypeName(field) + ") bool "; code += OffsetPrefix(field); code += "\t\t" + GenGetter(field.value.type); code += "(obj, o)\n\t\treturn true\n\t}\n"; code += "\treturn false\n"; code += "}\n\n"; }
// Get the value of a union from an object. static void GetUnionField(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { std::string &code = *code_ptr; GenReceiver(struct_def, code_ptr); code += MakeCamel(field.name) + "(self):"; code += OffsetPrefix(field); // TODO(rw): this works and is not the good way to it: bool is_native_table = TypeName(field) == "*flatbuffers.Table"; if (is_native_table) { code += Indent + Indent + Indent + "from flatbuffers.table import Table\n"; } else { code += Indent + Indent + Indent; code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n"; } code += Indent + Indent + Indent + "obj = Table(bytearray(), 0)\n"; code += Indent + Indent + Indent + GenGetter(field.value.type); code += "obj, o)\n" + Indent + Indent + Indent + "return obj\n"; code += Indent + Indent + "return None\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"; }
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"; }