From ec20233fab99c8ca9d5a1259eb005acdf7852d1a Mon Sep 17 00:00:00 2001 From: gonzaloserrano Date: Thu, 7 Jul 2016 12:55:36 +0200 Subject: [PATCH 1/7] Make most of the Go code comply with gofmt (except enums). Enums are not fixed because depending on they have comments or not they will be aligned alogn the asssignment character. --- src/idl_gen_go.cpp | 54 +++--- tests/GoTest.sh | 20 ++- tests/MyGame/Example/Monster.go | 166 ++++++++++++------ tests/MyGame/Example/Stat.go | 23 ++- tests/MyGame/Example/Test.go | 28 +-- .../MyGame/Example/TestSimpleTableWithEnum.go | 15 +- tests/MyGame/Example/Vec3.go | 71 +++++--- tests/MyGame/Example2/Monster.go | 11 +- 8 files changed, 262 insertions(+), 126 deletions(-) diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 5f1bcf5e8..573300980 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -126,7 +126,7 @@ static void NewRootTypeFromBuffer(const StructDef &struct_def, code += " {\n"; code += "\tn := flatbuffers.GetUOffsetT(buf[offset:])\n"; code += "\tx := &" + struct_def.name + "{}\n"; - code += "\tx.Init(buf, n + offset)\n"; + code += "\tx.Init(buf, n+offset)\n"; code += "\treturn x\n"; code += "}\n\n"; } @@ -178,9 +178,10 @@ static void GetScalarFieldOfStruct(const StructDef &struct_def, std::string getter = GenGetter(field.value.type); GenReceiver(struct_def, code_ptr); code += " " + MakeCamel(field.name); - code += "() " + TypeName(field) + " { return " + getter; + code += "() " + TypeName(field) + " {\n"; + code +="\treturn " + getter; code += "(rcv._tab.Pos + flatbuffers.UOffsetT("; - code += NumToString(field.value.offset) + ")) }\n"; + code += NumToString(field.value.offset) + "))\n}\n"; } // Get the value of a table's scalar. @@ -212,7 +213,7 @@ static void GetStructFieldOfStruct(const StructDef &struct_def, code += "\tif obj == nil {\n"; code += "\t\tobj = new(" + TypeName(field) + ")\n"; code += "\t}\n"; - code += "\tobj.Init(rcv._tab.Bytes, rcv._tab.Pos + "; + code += "\tobj.Init(rcv._tab.Bytes, rcv._tab.Pos+"; code += NumToString(field.value.offset) + ")"; code += "\n\treturn obj\n"; code += "}\n"; @@ -287,9 +288,9 @@ static void GetMemberOfVectorOfStruct(const StructDef &struct_def, if (!(vectortype.struct_def->fixed)) { code += "\t\tx = rcv._tab.Indirect(x)\n"; } - code += "\tif obj == nil {\n"; - code += "\t\tobj = new(" + TypeName(field) + ")\n"; - code += "\t}\n"; + code += "\t\tif obj == nil {\n"; + code += "\t\t\tobj = new(" + TypeName(field) + ")\n"; + code += "\t\t}\n"; code += "\t\tobj.Init(rcv._tab.Bytes, x)\n"; code += "\t\treturn true\n\t}\n"; code += "\treturn false\n"; @@ -310,7 +311,7 @@ static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, code += OffsetPrefix(field); code += "\t\ta := rcv._tab.Vector(o)\n"; code += "\t\treturn " + GenGetter(field.value.type) + "("; - code += "a + flatbuffers.UOffsetT(j * "; + code += "a + flatbuffers.UOffsetT(j*"; code += NumToString(InlineSize(vectortype)) + "))\n"; code += "\t}\n"; if (vectortype.base_type == BASE_TYPE_STRING) { @@ -326,7 +327,10 @@ static void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; - code += "\n"; + if (code.substr(code.length() - 2) != "\n\n") { + // a previous mutate has not put an extra new line + code += "\n"; + } code += "func Create" + struct_def.name; code += "(builder *flatbuffers.Builder"; } @@ -368,20 +372,20 @@ static void StructBuilderBody(const StructDef &struct_def, const char *nameprefix, std::string *code_ptr) { std::string &code = *code_ptr; - code += " builder.Prep(" + NumToString(struct_def.minalign) + ", "; + code += "\tbuilder.Prep(" + NumToString(struct_def.minalign) + ", "; code += NumToString(struct_def.bytesize) + ")\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"; + code += "\tbuilder.Pad(" + NumToString(field.padding) + ")\n"; if (IsStruct(field.value.type)) { StructBuilderBody(*field.value.type.struct_def, (nameprefix + (field.name + "_")).c_str(), code_ptr); } else { - code += " builder.Prepend" + GenMethod(field) + "("; + code += "\tbuilder.Prepend" + GenMethod(field) + "("; code += nameprefix + MakeCamel(field.name, false) + ")\n"; } } @@ -389,7 +393,7 @@ static void StructBuilderBody(const StructDef &struct_def, static void EndBuilderBody(std::string *code_ptr) { std::string &code = *code_ptr; - code += " return builder.Offset()\n"; + code += "\treturn builder.Offset()\n"; code += "}\n"; } @@ -398,10 +402,10 @@ static void GetStartOfTable(const StructDef &struct_def, std::string *code_ptr) { std::string &code = *code_ptr; code += "func " + struct_def.name + "Start"; - code += "(builder *flatbuffers.Builder) { "; - code += "builder.StartObject("; + code += "(builder *flatbuffers.Builder) {\n"; + code += "\tbuilder.StartObject("; code += NumToString(struct_def.fields.vec.size()); - code += ") }\n"; + code += ")\n}\n"; } // Set the value of a table's field. @@ -418,8 +422,8 @@ static void BuildFieldOfTable(const StructDef &struct_def, } else { code += GenTypeBasic(field.value.type); } - code += ") "; - code += "{ builder.Prepend"; + code += ") {\n"; + code += "\tbuilder.Prepend"; code += GenMethod(field) + "Slot("; code += NumToString(offset) + ", "; if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) { @@ -430,7 +434,7 @@ static void BuildFieldOfTable(const StructDef &struct_def, code += MakeCamel(field.name, false); } code += ", " + field.value.constant; - code += ") }\n"; + code += ")\n}\n"; } // Set the value of one of the members of a table's vector. @@ -441,7 +445,7 @@ static void BuildVectorOfTable(const StructDef &struct_def, code += "func " + struct_def.name + "Start"; code += MakeCamel(field.name); code += "Vector(builder *flatbuffers.Builder, numElems int) "; - code += "flatbuffers.UOffsetT { return builder.StartVector("; + code += "flatbuffers.UOffsetT {\n\treturn builder.StartVector("; auto vector_type = field.value.type.VectorType(); auto alignment = InlineAlignment(vector_type); auto elem_size = InlineSize(vector_type); @@ -456,7 +460,7 @@ static void GetEndOffsetOnTable(const StructDef &struct_def, std::string &code = *code_ptr; code += "func " + struct_def.name + "End"; code += "(builder *flatbuffers.Builder) flatbuffers.UOffsetT "; - code += "{ return builder.EndObject() }\n"; + code += "{\n\treturn builder.EndObject()\n}\n"; } // Generate the receiver for function signatures. @@ -521,9 +525,9 @@ static void MutateScalarFieldOfStruct(const StructDef &struct_def, std::string setter = "rcv._tab.Mutate" + type; GenReceiver(struct_def, code_ptr); code += " Mutate" + MakeCamel(field.name); - code += "(n " + TypeName(field) + ") bool { return " + setter; - code += "(rcv._tab.Pos + flatbuffers.UOffsetT("; - code += NumToString(field.value.offset) + "), n) }\n\n"; + code += "(n " + TypeName(field) + ") bool {\n\treturn " + setter; + code += "(rcv._tab.Pos+flatbuffers.UOffsetT("; + code += NumToString(field.value.offset) + "), n)\n}\n\n"; } // Mutate the value of a table's scalar. @@ -732,7 +736,7 @@ class GoGenerator : public BaseGenerator { if (needs_imports) { code += "import (\n"; code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; - code += ")\n"; + code += ")\n\n"; } } diff --git a/tests/GoTest.sh b/tests/GoTest.sh index 353930a61..7be4affb7 100755 --- a/tests/GoTest.sh +++ b/tests/GoTest.sh @@ -29,9 +29,9 @@ mkdir -p ${go_src}/MyGame/Example mkdir -p ${go_src}/github.com/google/flatbuffers/go mkdir -p ${go_src}/flatbuffers_test -cp -u MyGame/Example/*.go ./go_gen/src/MyGame/Example/ -cp -u ../go/* ./go_gen/src/github.com/google/flatbuffers/go -cp -u ./go_test.go ./go_gen/src/flatbuffers_test/ +cp -a MyGame/Example/*.go ./go_gen/src/MyGame/Example/ +cp -a ../go/* ./go_gen/src/github.com/google/flatbuffers/go +cp -a ./go_test.go ./go_gen/src/flatbuffers_test/ # Run tests with necessary flags. # Developers may wish to see more detail by appending the verbosity flag @@ -50,6 +50,18 @@ GOPATH=${go_path} go test flatbuffers_test \ --fuzz_fields=4 \ --fuzz_objects=10000 +GO_TEST_RESULT=$? rm -rf ${go_path}/{pkg,src} +if [[ $GO_TEST_RESULT == 0 ]]; then + echo "OK: Go tests passed." +else + echo "KO: Go tests failed." + exit 1 +fi -echo "OK: Go tests passed." +NOT_FMT_FILES=$(gofmt -l MyGame) +if [[ ${NOT_FMT_FILES} != "" ]]; then + echo "These files are not well gofmt'ed:\n\n${NOT_FMT_FILES}" + # enable this when enums are properly formated + # exit 1 +fi diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index f4cd18c18..7ba062fdc 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -5,6 +5,7 @@ package Example import ( flatbuffers "github.com/google/flatbuffers/go" ) + /// an example documentation comment: monster object type Monster struct { _tab flatbuffers.Table @@ -13,7 +14,7 @@ type Monster struct { func GetRootAsMonster(buf []byte, offset flatbuffers.UOffsetT) *Monster { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Monster{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -71,7 +72,7 @@ func (rcv *Monster) Inventory(j int) byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(14)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1)) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) } return 0 } @@ -130,9 +131,9 @@ func (rcv *Monster) Test4(obj *Test, j int) bool { if o != 0 { x := rcv._tab.Vector(o) x += flatbuffers.UOffsetT(j) * 4 - if obj == nil { - obj = new(Test) - } + if obj == nil { + obj = new(Test) + } obj.Init(rcv._tab.Bytes, x) return true } @@ -151,7 +152,7 @@ func (rcv *Monster) Testarrayofstring(j int) []byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(24)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.ByteVector(a + flatbuffers.UOffsetT(j * 4)) + return rcv._tab.ByteVector(a + flatbuffers.UOffsetT(j*4)) } return nil } @@ -172,9 +173,9 @@ func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool { x := rcv._tab.Vector(o) x += flatbuffers.UOffsetT(j) * 4 x = rcv._tab.Indirect(x) - if obj == nil { - obj = new(Monster) - } + if obj == nil { + obj = new(Monster) + } obj.Init(rcv._tab.Bytes, x) return true } @@ -208,7 +209,7 @@ func (rcv *Monster) Testnestedflatbuffer(j int) byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(30)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1)) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) } return 0 } @@ -354,7 +355,7 @@ func (rcv *Monster) Testarrayofbools(j int) byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(52)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1)) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) } return 0 } @@ -407,7 +408,7 @@ func (rcv *Monster) Testarrayofstring2(j int) []byte { o := flatbuffers.UOffsetT(rcv._tab.Offset(60)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.ByteVector(a + flatbuffers.UOffsetT(j * 4)) + return rcv._tab.ByteVector(a + flatbuffers.UOffsetT(j*4)) } return nil } @@ -420,47 +421,114 @@ func (rcv *Monster) Testarrayofstring2Length() int { return 0 } -func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(29) } -func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) } -func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) } -func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) } -func MonsterAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(name), 0) } -func MonsterAddInventory(builder *flatbuffers.Builder, inventory flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(5, flatbuffers.UOffsetT(inventory), 0) } -func MonsterStartInventoryVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1) +func MonsterStart(builder *flatbuffers.Builder) { + builder.StartObject(29) } -func MonsterAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(6, color, 8) } -func MonsterAddTestType(builder *flatbuffers.Builder, testType byte) { builder.PrependByteSlot(7, testType, 0) } -func MonsterAddTest(builder *flatbuffers.Builder, test flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(test), 0) } -func MonsterAddTest4(builder *flatbuffers.Builder, test4 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(test4), 0) } -func MonsterStartTest4Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 2) +func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { + builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) } -func MonsterAddTestarrayofstring(builder *flatbuffers.Builder, testarrayofstring flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(10, flatbuffers.UOffsetT(testarrayofstring), 0) } -func MonsterStartTestarrayofstringVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) +func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { + builder.PrependInt16Slot(1, mana, 150) } -func MonsterAddTestarrayoftables(builder *flatbuffers.Builder, testarrayoftables flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(11, flatbuffers.UOffsetT(testarrayoftables), 0) } -func MonsterStartTestarrayoftablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) +func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { + builder.PrependInt16Slot(2, hp, 100) } -func MonsterAddEnemy(builder *flatbuffers.Builder, enemy flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(12, flatbuffers.UOffsetT(enemy), 0) } -func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflatbuffer flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(13, flatbuffers.UOffsetT(testnestedflatbuffer), 0) } -func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1) +func MonsterAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(name), 0) } -func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) } -func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { builder.PrependByteSlot(15, testbool, 0) } -func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) { builder.PrependInt32Slot(16, testhashs32Fnv1, 0) } -func MonsterAddTesthashu32Fnv1(builder *flatbuffers.Builder, testhashu32Fnv1 uint32) { builder.PrependUint32Slot(17, testhashu32Fnv1, 0) } -func MonsterAddTesthashs64Fnv1(builder *flatbuffers.Builder, testhashs64Fnv1 int64) { builder.PrependInt64Slot(18, testhashs64Fnv1, 0) } -func MonsterAddTesthashu64Fnv1(builder *flatbuffers.Builder, testhashu64Fnv1 uint64) { builder.PrependUint64Slot(19, testhashu64Fnv1, 0) } -func MonsterAddTesthashs32Fnv1a(builder *flatbuffers.Builder, testhashs32Fnv1a int32) { builder.PrependInt32Slot(20, testhashs32Fnv1a, 0) } -func MonsterAddTesthashu32Fnv1a(builder *flatbuffers.Builder, testhashu32Fnv1a uint32) { builder.PrependUint32Slot(21, testhashu32Fnv1a, 0) } -func MonsterAddTesthashs64Fnv1a(builder *flatbuffers.Builder, testhashs64Fnv1a int64) { builder.PrependInt64Slot(22, testhashs64Fnv1a, 0) } -func MonsterAddTesthashu64Fnv1a(builder *flatbuffers.Builder, testhashu64Fnv1a uint64) { builder.PrependUint64Slot(23, testhashu64Fnv1a, 0) } -func MonsterAddTestarrayofbools(builder *flatbuffers.Builder, testarrayofbools flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(24, flatbuffers.UOffsetT(testarrayofbools), 0) } -func MonsterStartTestarrayofboolsVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1) +func MonsterAddInventory(builder *flatbuffers.Builder, inventory flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(5, flatbuffers.UOffsetT(inventory), 0) } -func MonsterAddTestf(builder *flatbuffers.Builder, testf float32) { builder.PrependFloat32Slot(25, testf, 3.14159) } -func MonsterAddTestf2(builder *flatbuffers.Builder, testf2 float32) { builder.PrependFloat32Slot(26, testf2, 3.0) } -func MonsterAddTestf3(builder *flatbuffers.Builder, testf3 float32) { builder.PrependFloat32Slot(27, testf3, 0.0) } -func MonsterAddTestarrayofstring2(builder *flatbuffers.Builder, testarrayofstring2 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(28, flatbuffers.UOffsetT(testarrayofstring2), 0) } -func MonsterStartTestarrayofstring2Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) +func MonsterStartInventoryVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} +func MonsterAddColor(builder *flatbuffers.Builder, color int8) { + builder.PrependInt8Slot(6, color, 8) +} +func MonsterAddTestType(builder *flatbuffers.Builder, testType byte) { + builder.PrependByteSlot(7, testType, 0) +} +func MonsterAddTest(builder *flatbuffers.Builder, test flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(test), 0) +} +func MonsterAddTest4(builder *flatbuffers.Builder, test4 flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(test4), 0) +} +func MonsterStartTest4Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(4, numElems, 2) +} +func MonsterAddTestarrayofstring(builder *flatbuffers.Builder, testarrayofstring flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(10, flatbuffers.UOffsetT(testarrayofstring), 0) +} +func MonsterStartTestarrayofstringVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(4, numElems, 4) +} +func MonsterAddTestarrayoftables(builder *flatbuffers.Builder, testarrayoftables flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(11, flatbuffers.UOffsetT(testarrayoftables), 0) +} +func MonsterStartTestarrayoftablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(4, numElems, 4) +} +func MonsterAddEnemy(builder *flatbuffers.Builder, enemy flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(12, flatbuffers.UOffsetT(enemy), 0) +} +func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflatbuffer flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(13, flatbuffers.UOffsetT(testnestedflatbuffer), 0) +} +func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} +func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) +} +func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { + builder.PrependByteSlot(15, testbool, 0) +} +func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) { + builder.PrependInt32Slot(16, testhashs32Fnv1, 0) +} +func MonsterAddTesthashu32Fnv1(builder *flatbuffers.Builder, testhashu32Fnv1 uint32) { + builder.PrependUint32Slot(17, testhashu32Fnv1, 0) +} +func MonsterAddTesthashs64Fnv1(builder *flatbuffers.Builder, testhashs64Fnv1 int64) { + builder.PrependInt64Slot(18, testhashs64Fnv1, 0) +} +func MonsterAddTesthashu64Fnv1(builder *flatbuffers.Builder, testhashu64Fnv1 uint64) { + builder.PrependUint64Slot(19, testhashu64Fnv1, 0) +} +func MonsterAddTesthashs32Fnv1a(builder *flatbuffers.Builder, testhashs32Fnv1a int32) { + builder.PrependInt32Slot(20, testhashs32Fnv1a, 0) +} +func MonsterAddTesthashu32Fnv1a(builder *flatbuffers.Builder, testhashu32Fnv1a uint32) { + builder.PrependUint32Slot(21, testhashu32Fnv1a, 0) +} +func MonsterAddTesthashs64Fnv1a(builder *flatbuffers.Builder, testhashs64Fnv1a int64) { + builder.PrependInt64Slot(22, testhashs64Fnv1a, 0) +} +func MonsterAddTesthashu64Fnv1a(builder *flatbuffers.Builder, testhashu64Fnv1a uint64) { + builder.PrependUint64Slot(23, testhashu64Fnv1a, 0) +} +func MonsterAddTestarrayofbools(builder *flatbuffers.Builder, testarrayofbools flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(24, flatbuffers.UOffsetT(testarrayofbools), 0) +} +func MonsterStartTestarrayofboolsVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(1, numElems, 1) +} +func MonsterAddTestf(builder *flatbuffers.Builder, testf float32) { + builder.PrependFloat32Slot(25, testf, 3.14159) +} +func MonsterAddTestf2(builder *flatbuffers.Builder, testf2 float32) { + builder.PrependFloat32Slot(26, testf2, 3.0) +} +func MonsterAddTestf3(builder *flatbuffers.Builder, testf3 float32) { + builder.PrependFloat32Slot(27, testf3, 0.0) +} +func MonsterAddTestarrayofstring2(builder *flatbuffers.Builder, testarrayofstring2 flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(28, flatbuffers.UOffsetT(testarrayofstring2), 0) +} +func MonsterStartTestarrayofstring2Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { + return builder.StartVector(4, numElems, 4) +} +func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() } -func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Stat.go b/tests/MyGame/Example/Stat.go index 8c56bfb07..022c4f036 100644 --- a/tests/MyGame/Example/Stat.go +++ b/tests/MyGame/Example/Stat.go @@ -5,6 +5,7 @@ package Example import ( flatbuffers "github.com/google/flatbuffers/go" ) + type Stat struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type Stat struct { func GetRootAsStat(buf []byte, offset flatbuffers.UOffsetT) *Stat { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Stat{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -53,8 +54,18 @@ func (rcv *Stat) MutateCount(n uint16) bool { return rcv._tab.MutateUint16Slot(8, n) } -func StatStart(builder *flatbuffers.Builder) { builder.StartObject(3) } -func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) } -func StatAddVal(builder *flatbuffers.Builder, val int64) { builder.PrependInt64Slot(1, val, 0) } -func StatAddCount(builder *flatbuffers.Builder, count uint16) { builder.PrependUint16Slot(2, count, 0) } -func StatEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func StatStart(builder *flatbuffers.Builder) { + builder.StartObject(3) +} +func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) +} +func StatAddVal(builder *flatbuffers.Builder, val int64) { + builder.PrependInt64Slot(1, val, 0) +} +func StatAddCount(builder *flatbuffers.Builder, count uint16) { + builder.PrependUint16Slot(2, count, 0) +} +func StatEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/tests/MyGame/Example/Test.go b/tests/MyGame/Example/Test.go index ee0d9aaec..781db8b09 100644 --- a/tests/MyGame/Example/Test.go +++ b/tests/MyGame/Example/Test.go @@ -5,6 +5,7 @@ package Example import ( flatbuffers "github.com/google/flatbuffers/go" ) + type Test struct { _tab flatbuffers.Struct } @@ -14,17 +15,24 @@ func (rcv *Test) Init(buf []byte, i flatbuffers.UOffsetT) { rcv._tab.Pos = i } -func (rcv *Test) A() int16 { return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) } -func (rcv *Test) MutateA(n int16) bool { return rcv._tab.MutateInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) } - -func (rcv *Test) B() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) } -func (rcv *Test) MutateB(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2), n) } +func (rcv *Test) A() int16 { + return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) +} +func (rcv *Test) MutateA(n int16) bool { + return rcv._tab.MutateInt16(rcv._tab.Pos+flatbuffers.UOffsetT(0), n) +} +func (rcv *Test) B() int8 { + return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) +} +func (rcv *Test) MutateB(n int8) bool { + return rcv._tab.MutateInt8(rcv._tab.Pos+flatbuffers.UOffsetT(2), n) +} func CreateTest(builder *flatbuffers.Builder, a int16, b int8) flatbuffers.UOffsetT { - builder.Prep(2, 4) - builder.Pad(1) - builder.PrependInt8(b) - builder.PrependInt16(a) - return builder.Offset() + builder.Prep(2, 4) + builder.Pad(1) + builder.PrependInt8(b) + builder.PrependInt16(a) + return builder.Offset() } diff --git a/tests/MyGame/Example/TestSimpleTableWithEnum.go b/tests/MyGame/Example/TestSimpleTableWithEnum.go index 96218e58b..be50d5ca1 100644 --- a/tests/MyGame/Example/TestSimpleTableWithEnum.go +++ b/tests/MyGame/Example/TestSimpleTableWithEnum.go @@ -5,6 +5,7 @@ package Example import ( flatbuffers "github.com/google/flatbuffers/go" ) + type TestSimpleTableWithEnum struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type TestSimpleTableWithEnum struct { func GetRootAsTestSimpleTableWithEnum(buf []byte, offset flatbuffers.UOffsetT) *TestSimpleTableWithEnum { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TestSimpleTableWithEnum{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -33,6 +34,12 @@ func (rcv *TestSimpleTableWithEnum) MutateColor(n int8) bool { return rcv._tab.MutateInt8Slot(4, n) } -func TestSimpleTableWithEnumStart(builder *flatbuffers.Builder) { builder.StartObject(1) } -func TestSimpleTableWithEnumAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(0, color, 2) } -func TestSimpleTableWithEnumEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func TestSimpleTableWithEnumStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func TestSimpleTableWithEnumAddColor(builder *flatbuffers.Builder, color int8) { + builder.PrependInt8Slot(0, color, 2) +} +func TestSimpleTableWithEnumEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/tests/MyGame/Example/Vec3.go b/tests/MyGame/Example/Vec3.go index 8bf97da32..1838e5606 100644 --- a/tests/MyGame/Example/Vec3.go +++ b/tests/MyGame/Example/Vec3.go @@ -5,6 +5,7 @@ package Example import ( flatbuffers "github.com/google/flatbuffers/go" ) + type Vec3 struct { _tab flatbuffers.Struct } @@ -14,42 +15,62 @@ func (rcv *Vec3) Init(buf []byte, i flatbuffers.UOffsetT) { rcv._tab.Pos = i } -func (rcv *Vec3) X() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) } -func (rcv *Vec3) MutateX(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) } +func (rcv *Vec3) X() float32 { + return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) +} +func (rcv *Vec3) MutateX(n float32) bool { + return rcv._tab.MutateFloat32(rcv._tab.Pos+flatbuffers.UOffsetT(0), n) +} -func (rcv *Vec3) Y() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) } -func (rcv *Vec3) MutateY(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4), n) } +func (rcv *Vec3) Y() float32 { + return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) +} +func (rcv *Vec3) MutateY(n float32) bool { + return rcv._tab.MutateFloat32(rcv._tab.Pos+flatbuffers.UOffsetT(4), n) +} -func (rcv *Vec3) Z() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) } -func (rcv *Vec3) MutateZ(n float32) bool { return rcv._tab.MutateFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8), n) } +func (rcv *Vec3) Z() float32 { + return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) +} +func (rcv *Vec3) MutateZ(n float32) bool { + return rcv._tab.MutateFloat32(rcv._tab.Pos+flatbuffers.UOffsetT(8), n) +} -func (rcv *Vec3) Test1() float64 { return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) } -func (rcv *Vec3) MutateTest1(n float64) bool { return rcv._tab.MutateFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16), n) } +func (rcv *Vec3) Test1() float64 { + return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) +} +func (rcv *Vec3) MutateTest1(n float64) bool { + return rcv._tab.MutateFloat64(rcv._tab.Pos+flatbuffers.UOffsetT(16), n) +} -func (rcv *Vec3) Test2() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) } -func (rcv *Vec3) MutateTest2(n int8) bool { return rcv._tab.MutateInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24), n) } +func (rcv *Vec3) Test2() int8 { + return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) +} +func (rcv *Vec3) MutateTest2(n int8) bool { + return rcv._tab.MutateInt8(rcv._tab.Pos+flatbuffers.UOffsetT(24), n) +} func (rcv *Vec3) Test3(obj *Test) *Test { if obj == nil { obj = new(Test) } - obj.Init(rcv._tab.Bytes, rcv._tab.Pos + 26) + obj.Init(rcv._tab.Bytes, rcv._tab.Pos+26) return obj } func CreateVec3(builder *flatbuffers.Builder, x float32, y float32, z float32, test1 float64, test2 int8, test3_a int16, test3_b int8) flatbuffers.UOffsetT { - builder.Prep(16, 32) - builder.Pad(2) - builder.Prep(2, 4) - builder.Pad(1) - builder.PrependInt8(test3_b) - builder.PrependInt16(test3_a) - builder.Pad(1) - builder.PrependInt8(test2) - builder.PrependFloat64(test1) - builder.Pad(4) - builder.PrependFloat32(z) - builder.PrependFloat32(y) - builder.PrependFloat32(x) - return builder.Offset() + builder.Prep(16, 32) + builder.Pad(2) + builder.Prep(2, 4) + builder.Pad(1) + builder.PrependInt8(test3_b) + builder.PrependInt16(test3_a) + builder.Pad(1) + builder.PrependInt8(test2) + builder.PrependFloat64(test1) + builder.Pad(4) + builder.PrependFloat32(z) + builder.PrependFloat32(y) + builder.PrependFloat32(x) + return builder.Offset() } diff --git a/tests/MyGame/Example2/Monster.go b/tests/MyGame/Example2/Monster.go index 789d46782..698c2ef56 100644 --- a/tests/MyGame/Example2/Monster.go +++ b/tests/MyGame/Example2/Monster.go @@ -5,6 +5,7 @@ package Example2 import ( flatbuffers "github.com/google/flatbuffers/go" ) + type Monster struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type Monster struct { func GetRootAsMonster(buf []byte, offset flatbuffers.UOffsetT) *Monster { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Monster{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -21,5 +22,9 @@ func (rcv *Monster) Init(buf []byte, i flatbuffers.UOffsetT) { rcv._tab.Pos = i } -func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(0) } -func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func MonsterStart(builder *flatbuffers.Builder) { + builder.StartObject(0) +} +func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} From 3eebba789f6447b342a308b0ad38ed0a6fb51de4 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 8 Aug 2016 12:16:10 -0700 Subject: [PATCH 2/7] Fixed incorrect generated code on convenient vector constructors. Change-Id: Ib283eade157a436e0f3ca0e68d74e7dc6c51c65b --- samples/monster_generated.h | 4 ++-- src/idl_gen_cpp.cpp | 11 ++++++----- tests/monster_test_generated.h | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 783d1c7f0..60a8d3f61 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -198,7 +198,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder const std::vector> *weapons = nullptr, Equipment equipped_type = Equipment_NONE, flatbuffers::Offset equipped = 0) { - return CreateMonster(_fbb, pos, mana, hp, name ? 0 : _fbb.CreateString(name), inventory ? 0 : _fbb.CreateVector(*inventory), color, weapons ? 0 : _fbb.CreateVector>(*weapons), equipped_type, equipped); + return CreateMonster(_fbb, pos, mana, hp, name ? _fbb.CreateString(name) : 0, inventory ? _fbb.CreateVector(*inventory) : 0, color, weapons ? _fbb.CreateVector>(*weapons) : 0, equipped_type, equipped); } inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o); @@ -252,7 +252,7 @@ inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder & inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, int16_t damage = 0) { - return CreateWeapon(_fbb, name ? 0 : _fbb.CreateString(name), damage); + return CreateWeapon(_fbb, name ? _fbb.CreateString(name) : 0, damage); } inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, const WeaponT *_o); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 6eaab1445..9326dd896 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -927,15 +927,16 @@ class CppGenerator : public BaseGenerator { auto &field = **it; if (!field.deprecated) { if (field.value.type.base_type == BASE_TYPE_STRING) { - code += ", " + field.name + " ? 0 : "; - code += "_fbb.CreateString(" + field.name + ")"; + code += ", " + field.name + " ? "; + code += "_fbb.CreateString(" + field.name + ") : 0"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += ", " + field.name + " ? 0 : "; + code += ", " + field.name + " ? "; code += "_fbb.CreateVector<"; code += GenTypeWire(field.value.type.VectorType(), "", false); - code += ">(*" + field.name + ")"; - } else + code += ">(*" + field.name + ") : 0"; + } else { code += ", " + field.name; + } } } code += ");\n}\n\n"; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 49aba1133..6353658b6 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -264,7 +264,7 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb const char *id = nullptr, int64_t val = 0, uint16_t count = 0) { - return CreateStat(_fbb, id ? 0 : _fbb.CreateString(id), val, count); + return CreateStat(_fbb, id ? _fbb.CreateString(id) : 0, val, count); } inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const StatT *_o); @@ -571,7 +571,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder float testf2 = 3.0f, float testf3 = 0.0f, const std::vector> *testarrayofstring2 = nullptr) { - return CreateMonster(_fbb, pos, mana, hp, name ? 0 : _fbb.CreateString(name), inventory ? 0 : _fbb.CreateVector(*inventory), color, test_type, test, test4 ? 0 : _fbb.CreateVector(*test4), testarrayofstring ? 0 : _fbb.CreateVector>(*testarrayofstring), testarrayoftables ? 0 : _fbb.CreateVector>(*testarrayoftables), enemy, testnestedflatbuffer ? 0 : _fbb.CreateVector(*testnestedflatbuffer), testempty, testbool, testhashs32_fnv1, testhashu32_fnv1, testhashs64_fnv1, testhashu64_fnv1, testhashs32_fnv1a, testhashu32_fnv1a, testhashs64_fnv1a, testhashu64_fnv1a, testarrayofbools ? 0 : _fbb.CreateVector(*testarrayofbools), testf, testf2, testf3, testarrayofstring2 ? 0 : _fbb.CreateVector>(*testarrayofstring2)); + return CreateMonster(_fbb, pos, mana, hp, name ? _fbb.CreateString(name) : 0, inventory ? _fbb.CreateVector(*inventory) : 0, color, test_type, test, test4 ? _fbb.CreateVector(*test4) : 0, testarrayofstring ? _fbb.CreateVector>(*testarrayofstring) : 0, testarrayoftables ? _fbb.CreateVector>(*testarrayoftables) : 0, enemy, testnestedflatbuffer ? _fbb.CreateVector(*testnestedflatbuffer) : 0, testempty, testbool, testhashs32_fnv1, testhashu32_fnv1, testhashs64_fnv1, testhashu64_fnv1, testhashs32_fnv1a, testhashu32_fnv1a, testhashs64_fnv1a, testhashu64_fnv1a, testarrayofbools ? _fbb.CreateVector(*testarrayofbools) : 0, testf, testf2, testf3, testarrayofstring2 ? _fbb.CreateVector>(*testarrayofstring2) : 0); } inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o); From c96fc7fcef4b415f561d5e1b6434cb708cbeb592 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 8 Aug 2016 12:18:07 -0700 Subject: [PATCH 3/7] Added missing generated code from previous commits. Change-Id: Ie2f1f65be451979d9f8d4da999b1c10737e867af --- include/flatbuffers/reflection_generated.h | 12 ++++----- .../NamespaceA/NamespaceB/EnumInNestedNS.go | 7 +++++ .../NamespaceA/NamespaceB/StructInNestedNS.go | 26 ++++++++++++------- .../NamespaceA/NamespaceB/TableInNestedNS.go | 15 ++++++++--- .../NamespaceA/SecondTableInA.go | 15 ++++++++--- .../NamespaceA/TableInFirstNS.go | 23 +++++++++++----- tests/namespace_test/NamespaceC/TableInC.go | 19 ++++++++++---- 7 files changed, 83 insertions(+), 34 deletions(-) diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index a459aa6a6..4fac4d1d5 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -136,7 +136,7 @@ inline flatbuffers::Offset CreateKeyValue(flatbuffers::FlatBufferBuild inline flatbuffers::Offset CreateKeyValue(flatbuffers::FlatBufferBuilder &_fbb, const char *key = nullptr, const char *value = nullptr) { - return CreateKeyValue(_fbb, key ? 0 : _fbb.CreateString(key), value ? 0 : _fbb.CreateString(value)); + return CreateKeyValue(_fbb, key ? _fbb.CreateString(key) : 0, value ? _fbb.CreateString(value) : 0); } struct EnumVal FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -191,7 +191,7 @@ inline flatbuffers::Offset CreateEnumVal(flatbuffers::FlatBufferBuilder const char *name = nullptr, int64_t value = 0, flatbuffers::Offset object = 0) { - return CreateEnumVal(_fbb, name ? 0 : _fbb.CreateString(name), value, object); + return CreateEnumVal(_fbb, name ? _fbb.CreateString(name) : 0, value, object); } struct Enum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -266,7 +266,7 @@ inline flatbuffers::Offset CreateEnum(flatbuffers::FlatBufferBuilder &_fbb bool is_union = false, flatbuffers::Offset underlying_type = 0, const std::vector> *attributes = nullptr) { - return CreateEnum(_fbb, name ? 0 : _fbb.CreateString(name), values ? 0 : _fbb.CreateVector>(*values), is_union, underlying_type, attributes ? 0 : _fbb.CreateVector>(*attributes)); + return CreateEnum(_fbb, name ? _fbb.CreateString(name) : 0, values ? _fbb.CreateVector>(*values) : 0, is_union, underlying_type, attributes ? _fbb.CreateVector>(*attributes) : 0); } struct Field FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -373,7 +373,7 @@ inline flatbuffers::Offset CreateField(flatbuffers::FlatBufferBuilder &_f bool required = false, bool key = false, const std::vector> *attributes = nullptr) { - return CreateField(_fbb, name ? 0 : _fbb.CreateString(name), type, id, offset, default_integer, default_real, deprecated, required, key, attributes ? 0 : _fbb.CreateVector>(*attributes)); + return CreateField(_fbb, name ? _fbb.CreateString(name) : 0, type, id, offset, default_integer, default_real, deprecated, required, key, attributes ? _fbb.CreateVector>(*attributes) : 0); } struct Object FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -453,7 +453,7 @@ inline flatbuffers::Offset CreateObject(flatbuffers::FlatBufferBuilder & int32_t minalign = 0, int32_t bytesize = 0, const std::vector> *attributes = nullptr) { - return CreateObject(_fbb, name ? 0 : _fbb.CreateString(name), fields ? 0 : _fbb.CreateVector>(*fields), is_struct, minalign, bytesize, attributes ? 0 : _fbb.CreateVector>(*attributes)); + return CreateObject(_fbb, name ? _fbb.CreateString(name) : 0, fields ? _fbb.CreateVector>(*fields) : 0, is_struct, minalign, bytesize, attributes ? _fbb.CreateVector>(*attributes) : 0); } struct Schema FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { @@ -526,7 +526,7 @@ inline flatbuffers::Offset CreateSchema(flatbuffers::FlatBufferBuilder & const char *file_ident = nullptr, const char *file_ext = nullptr, flatbuffers::Offset root_table = 0) { - return CreateSchema(_fbb, objects ? 0 : _fbb.CreateVector>(*objects), enums ? 0 : _fbb.CreateVector>(*enums), file_ident ? 0 : _fbb.CreateString(file_ident), file_ext ? 0 : _fbb.CreateString(file_ext), root_table); + return CreateSchema(_fbb, objects ? _fbb.CreateVector>(*objects) : 0, enums ? _fbb.CreateVector>(*enums) : 0, file_ident ? _fbb.CreateString(file_ident) : 0, file_ext ? _fbb.CreateString(file_ext) : 0, root_table); } inline const reflection::Schema *GetSchema(const void *buf) { return flatbuffers::GetRoot(buf); } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/EnumInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/EnumInNestedNS.go index a1fe117c5..2b68991e4 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/EnumInNestedNS.go +++ b/tests/namespace_test/NamespaceA/NamespaceB/EnumInNestedNS.go @@ -7,3 +7,10 @@ const ( EnumInNestedNSB = 1 EnumInNestedNSC = 2 ) + +var EnumNamesEnumInNestedNS = map[int]string{ + EnumInNestedNSA:"A", + EnumInNestedNSB:"B", + EnumInNestedNSC:"C", +} + diff --git a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go index f834a721a..3de874b25 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go +++ b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go @@ -5,6 +5,7 @@ package NamespaceB import ( flatbuffers "github.com/google/flatbuffers/go" ) + type StructInNestedNS struct { _tab flatbuffers.Struct } @@ -14,16 +15,23 @@ func (rcv *StructInNestedNS) Init(buf []byte, i flatbuffers.UOffsetT) { rcv._tab.Pos = i } -func (rcv *StructInNestedNS) A() int32 { return rcv._tab.GetInt32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) } -func (rcv *StructInNestedNS) MutateA(n int32) bool { return rcv._tab.MutateInt32(rcv._tab.Pos + flatbuffers.UOffsetT(0), n) } - -func (rcv *StructInNestedNS) B() int32 { return rcv._tab.GetInt32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) } -func (rcv *StructInNestedNS) MutateB(n int32) bool { return rcv._tab.MutateInt32(rcv._tab.Pos + flatbuffers.UOffsetT(4), n) } +func (rcv *StructInNestedNS) A() int32 { + return rcv._tab.GetInt32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) +} +func (rcv *StructInNestedNS) MutateA(n int32) bool { + return rcv._tab.MutateInt32(rcv._tab.Pos+flatbuffers.UOffsetT(0), n) +} +func (rcv *StructInNestedNS) B() int32 { + return rcv._tab.GetInt32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) +} +func (rcv *StructInNestedNS) MutateB(n int32) bool { + return rcv._tab.MutateInt32(rcv._tab.Pos+flatbuffers.UOffsetT(4), n) +} func CreateStructInNestedNS(builder *flatbuffers.Builder, a int32, b int32) flatbuffers.UOffsetT { - builder.Prep(4, 8) - builder.PrependInt32(b) - builder.PrependInt32(a) - return builder.Offset() + builder.Prep(4, 8) + builder.PrependInt32(b) + builder.PrependInt32(a) + return builder.Offset() } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go index 2d7705730..a25ae7be4 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go +++ b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go @@ -5,6 +5,7 @@ package NamespaceB import ( flatbuffers "github.com/google/flatbuffers/go" ) + type TableInNestedNS struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type TableInNestedNS struct { func GetRootAsTableInNestedNS(buf []byte, offset flatbuffers.UOffsetT) *TableInNestedNS { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TableInNestedNS{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -33,6 +34,12 @@ func (rcv *TableInNestedNS) MutateFoo(n int32) bool { return rcv._tab.MutateInt32Slot(4, n) } -func TableInNestedNSStart(builder *flatbuffers.Builder) { builder.StartObject(1) } -func TableInNestedNSAddFoo(builder *flatbuffers.Builder, foo int32) { builder.PrependInt32Slot(0, foo, 0) } -func TableInNestedNSEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func TableInNestedNSStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func TableInNestedNSAddFoo(builder *flatbuffers.Builder, foo int32) { + builder.PrependInt32Slot(0, foo, 0) +} +func TableInNestedNSEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/tests/namespace_test/NamespaceA/SecondTableInA.go b/tests/namespace_test/NamespaceA/SecondTableInA.go index 40454419f..f53a1ead1 100644 --- a/tests/namespace_test/NamespaceA/SecondTableInA.go +++ b/tests/namespace_test/NamespaceA/SecondTableInA.go @@ -5,6 +5,7 @@ package NamespaceA import ( flatbuffers "github.com/google/flatbuffers/go" ) + type SecondTableInA struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type SecondTableInA struct { func GetRootAsSecondTableInA(buf []byte, offset flatbuffers.UOffsetT) *SecondTableInA { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &SecondTableInA{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -34,6 +35,12 @@ func (rcv *SecondTableInA) ReferToC(obj *TableInC) *TableInC { return nil } -func SecondTableInAStart(builder *flatbuffers.Builder) { builder.StartObject(1) } -func SecondTableInAAddReferToC(builder *flatbuffers.Builder, referToC flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(referToC), 0) } -func SecondTableInAEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func SecondTableInAStart(builder *flatbuffers.Builder) { + builder.StartObject(1) +} +func SecondTableInAAddReferToC(builder *flatbuffers.Builder, referToC flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(referToC), 0) +} +func SecondTableInAEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.go b/tests/namespace_test/NamespaceA/TableInFirstNS.go index 5122fd7ee..4820a8ae5 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.go +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.go @@ -5,6 +5,7 @@ package NamespaceA import ( flatbuffers "github.com/google/flatbuffers/go" ) + type TableInFirstNS struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type TableInFirstNS struct { func GetRootAsTableInFirstNS(buf []byte, offset flatbuffers.UOffsetT) *TableInFirstNS { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TableInFirstNS{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -59,8 +60,18 @@ func (rcv *TableInFirstNS) FooStruct(obj *StructInNestedNS) *StructInNestedNS { return nil } -func TableInFirstNSStart(builder *flatbuffers.Builder) { builder.StartObject(3) } -func TableInFirstNSAddFooTable(builder *flatbuffers.Builder, fooTable flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(fooTable), 0) } -func TableInFirstNSAddFooEnum(builder *flatbuffers.Builder, fooEnum int8) { builder.PrependInt8Slot(1, fooEnum, 0) } -func TableInFirstNSAddFooStruct(builder *flatbuffers.Builder, fooStruct flatbuffers.UOffsetT) { builder.PrependStructSlot(2, flatbuffers.UOffsetT(fooStruct), 0) } -func TableInFirstNSEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func TableInFirstNSStart(builder *flatbuffers.Builder) { + builder.StartObject(3) +} +func TableInFirstNSAddFooTable(builder *flatbuffers.Builder, fooTable flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(fooTable), 0) +} +func TableInFirstNSAddFooEnum(builder *flatbuffers.Builder, fooEnum int8) { + builder.PrependInt8Slot(1, fooEnum, 0) +} +func TableInFirstNSAddFooStruct(builder *flatbuffers.Builder, fooStruct flatbuffers.UOffsetT) { + builder.PrependStructSlot(2, flatbuffers.UOffsetT(fooStruct), 0) +} +func TableInFirstNSEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} diff --git a/tests/namespace_test/NamespaceC/TableInC.go b/tests/namespace_test/NamespaceC/TableInC.go index 81c59f17e..d0d8b002f 100644 --- a/tests/namespace_test/NamespaceC/TableInC.go +++ b/tests/namespace_test/NamespaceC/TableInC.go @@ -5,6 +5,7 @@ package NamespaceC import ( flatbuffers "github.com/google/flatbuffers/go" ) + type TableInC struct { _tab flatbuffers.Table } @@ -12,7 +13,7 @@ type TableInC struct { func GetRootAsTableInC(buf []byte, offset flatbuffers.UOffsetT) *TableInC { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TableInC{} - x.Init(buf, n + offset) + x.Init(buf, n+offset) return x } @@ -47,7 +48,15 @@ func (rcv *TableInC) ReferToA2(obj *SecondTableInA) *SecondTableInA { return nil } -func TableInCStart(builder *flatbuffers.Builder) { builder.StartObject(2) } -func TableInCAddReferToA1(builder *flatbuffers.Builder, referToA1 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(referToA1), 0) } -func TableInCAddReferToA2(builder *flatbuffers.Builder, referToA2 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(referToA2), 0) } -func TableInCEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } +func TableInCStart(builder *flatbuffers.Builder) { + builder.StartObject(2) +} +func TableInCAddReferToA1(builder *flatbuffers.Builder, referToA1 flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(referToA1), 0) +} +func TableInCAddReferToA2(builder *flatbuffers.Builder, referToA2 flatbuffers.UOffsetT) { + builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(referToA2), 0) +} +func TableInCEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { + return builder.EndObject() +} From be908939dae62d1dd128de6538b5492943315706 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 10 Aug 2016 10:09:39 -0700 Subject: [PATCH 4/7] Fixed __builtin_bswap16 not being available on GCC <4.8 See e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 Change-Id: Idae99196c47e015a2ba2416aaf1526f77498ce5d --- include/flatbuffers/flatbuffers.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 1a6908261..4644f9c60 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -153,32 +153,34 @@ inline void EndianCheck() { template T EndianSwap(T t) { #if defined(_MSC_VER) - #pragma push_macro("__builtin_bswap16") - #pragma push_macro("__builtin_bswap32") - #pragma push_macro("__builtin_bswap64") - #define __builtin_bswap16 _byteswap_ushort - #define __builtin_bswap32 _byteswap_ulong - #define __builtin_bswap64 _byteswap_uint64 + #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort + #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong + #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 + #else + #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 + // __builtin_bswap16 was missing prior to GCC 4.8. + #define FLATBUFFERS_BYTESWAP16(x) \ + static_cast(__builtin_bswap32(static_cast(x) << 16)) + #else + #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 + #endif + #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 + #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 #endif if (sizeof(T) == 1) { // Compile-time if-then's. return t; } else if (sizeof(T) == 2) { - auto r = __builtin_bswap16(*reinterpret_cast(&t)); + auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast(&t)); return *reinterpret_cast(&r); } else if (sizeof(T) == 4) { - auto r = __builtin_bswap32(*reinterpret_cast(&t)); + auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast(&t)); return *reinterpret_cast(&r); } else if (sizeof(T) == 8) { - auto r = __builtin_bswap64(*reinterpret_cast(&t)); + auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast(&t)); return *reinterpret_cast(&r); } else { assert(0); } - #if defined(_MSC_VER) - #pragma pop_macro("__builtin_bswap16") - #pragma pop_macro("__builtin_bswap32") - #pragma pop_macro("__builtin_bswap64") - #endif } template T EndianScalar(T t) { From 205ddb175f259d8fd9bae1cbfaf88162996799aa Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 10 Aug 2016 11:41:38 -0700 Subject: [PATCH 5/7] Added union namespacing test case. Change-Id: I9a4f90889c311c454a9c19e78c7db3c79800b44e Tested: on Linux. --- tests/test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test.cpp b/tests/test.cpp index cd3723756..fd2352bd1 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1169,6 +1169,11 @@ void ParseUnionTest() { "table V { X:U; }" "root_type V;" "{ X:{ A:1 }, X_type: T }"), true); + // Unions must be parsable with prefixed namespace. + flatbuffers::Parser parser2; + TEST_EQ(parser2.Parse("namespace N; table A {} namespace; union U { N.A }" + "table B { e:U; } root_type B;" + "{ e_type: N_A, e: {} }"), true); } void ConformTest() { From 18d67ed83bfd54de4d248f4650f5ff1900b1ed6b Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 10 Aug 2016 17:53:40 -0700 Subject: [PATCH 6/7] Clarified in the docs how to get to the serialized bytes. This was a frequent source of confusion, since in all implementations the data doesn't start at offset 0 in the buffer. Change-Id: I045966e65928e9acd9def84e215914ecb5510653 --- docs/source/Tutorial.md | 16 +++++++++++++++- js/flatbuffers.js | 5 ++--- net/FlatBuffers/FlatBufferBuilder.cs | 2 ++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/source/Tutorial.md b/docs/source/Tutorial.md index 100191d7a..c71a9fad1 100644 --- a/docs/source/Tutorial.md +++ b/docs/source/Tutorial.md @@ -1161,12 +1161,23 @@ like so: ~~~{.java} // This must be called after `finish()`. java.nio.ByteBuffer buf = builder.dataBuffer(); + // The data in this ByteBuffer does NOT start at 0, but at buf.position(). + // The number of bytes is buf.remaining(). + + // Alternatively this copies the above data out of the ByteBuffer for you: + bytes[] buf = builder.sizedByteArray(); ~~~
~~~{.cs} // This must be called after `Finish()`. var buf = builder.DataBuffer; // Of type `FlatBuffers.ByteBuffer`. + // The data in this ByteBuffer does NOT start at 0, but at buf.Position. + // The end of the data is marked by buf.Length, so the size is + // buf.Length - buf.Position. + + // Alternatively this copies the above data out of the ByteBuffer for you: + bytes[] buf = builder.SizedByteArray(); ~~~
@@ -1184,13 +1195,16 @@ like so:
~~~{.js} // This must be called after `finish()`. - var buf = builder.dataBuffer(); // Of type `flatbuffers.ByteBuffer`. + var buf = builder.asUint8Array(); // Of type `Uint8Array`. ~~~
~~~{.php} // This must be called after `finish()`. $buf = $builder->dataBuffer(); // Of type `Google\FlatBuffers\ByteBuffer` + // The data in this ByteBuffer does NOT start at 0, but at buf->getPosition(). + // The end of the data is marked by buf->capacity(), so the size is + // buf->capacity() - buf->getPosition(). ~~~
diff --git a/js/flatbuffers.js b/js/flatbuffers.js index 511c9a216..69bb783e9 100644 --- a/js/flatbuffers.js +++ b/js/flatbuffers.js @@ -238,9 +238,8 @@ flatbuffers.Builder.prototype.dataBuffer = function() { }; /** - * Get the ByteBuffer representing the FlatBuffer. Only call this after you've - * called finish(). The actual data starts at the ByteBuffer's current position, - * not necessarily at 0. + * Get the bytes representing the FlatBuffer. Only call this after you've + * called finish(). * * @returns {Uint8Array} */ diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 590e6ac0d..8c57abec1 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -582,6 +582,8 @@ namespace FlatBuffers /// /// /// This is typically only called after you call `Finish()`. + /// The actual data starts at the ByteBuffer's current position, + /// not necessarily at `0`. /// /// /// Returns the ByteBuffer for this FlatBuffer. From a0b8f669eeab24e0a23a8cc93b89d53117d0c26e Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Fri, 12 Aug 2016 11:12:12 -0700 Subject: [PATCH 7/7] Added "Direct" suffix to vector/string object creation functions. They were overloaded, but also had default arguments, so would become ambiguous when used with few arguments. Change-Id: Ifac7f3ea3a6391d971dfeda8e33129c8c38d6f12 Tested: on Linux. --- include/flatbuffers/reflection_generated.h | 12 ++++++------ samples/monster_generated.h | 4 ++-- src/idl_gen_cpp.cpp | 4 ++-- tests/monster_test_generated.h | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index 4fac4d1d5..3bd1590f6 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -133,7 +133,7 @@ inline flatbuffers::Offset CreateKeyValue(flatbuffers::FlatBufferBuild return builder_.Finish(); } -inline flatbuffers::Offset CreateKeyValue(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateKeyValueDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *key = nullptr, const char *value = nullptr) { return CreateKeyValue(_fbb, key ? _fbb.CreateString(key) : 0, value ? _fbb.CreateString(value) : 0); @@ -187,7 +187,7 @@ inline flatbuffers::Offset CreateEnumVal(flatbuffers::FlatBufferBuilder return builder_.Finish(); } -inline flatbuffers::Offset CreateEnumVal(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateEnumValDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, int64_t value = 0, flatbuffers::Offset object = 0) { @@ -260,7 +260,7 @@ inline flatbuffers::Offset CreateEnum(flatbuffers::FlatBufferBuilder &_fbb return builder_.Finish(); } -inline flatbuffers::Offset CreateEnum(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateEnumDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, const std::vector> *values = nullptr, bool is_union = false, @@ -362,7 +362,7 @@ inline flatbuffers::Offset CreateField(flatbuffers::FlatBufferBuilder &_f return builder_.Finish(); } -inline flatbuffers::Offset CreateField(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateFieldDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, flatbuffers::Offset type = 0, uint16_t id = 0, @@ -446,7 +446,7 @@ inline flatbuffers::Offset CreateObject(flatbuffers::FlatBufferBuilder & return builder_.Finish(); } -inline flatbuffers::Offset CreateObject(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateObjectDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, const std::vector> *fields = nullptr, bool is_struct = false, @@ -520,7 +520,7 @@ inline flatbuffers::Offset CreateSchema(flatbuffers::FlatBufferBuilder & return builder_.Finish(); } -inline flatbuffers::Offset CreateSchema(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateSchemaDirect(flatbuffers::FlatBufferBuilder &_fbb, const std::vector> *objects = nullptr, const std::vector> *enums = nullptr, const char *file_ident = nullptr, diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 60a8d3f61..84e56224f 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -188,7 +188,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } -inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateMonsterDirect(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos = 0, int16_t mana = 150, int16_t hp = 100, @@ -249,7 +249,7 @@ inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder & return builder_.Finish(); } -inline flatbuffers::Offset CreateWeapon(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateWeaponDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *name = nullptr, int16_t damage = 0) { return CreateWeapon(_fbb, name ? _fbb.CreateString(name) : 0, damage); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 9326dd896..edb14cc6b 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -897,11 +897,11 @@ class CppGenerator : public BaseGenerator { } code += " return builder_.Finish();\n}\n\n"; - // Generate a CreateX function with vector types as parameters + // Generate a CreateXDirect function with vector types as parameters if (gen_vector_pars) { code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; code += struct_def.name; - code += "(flatbuffers::FlatBufferBuilder &_fbb"; + code += "Direct(flatbuffers::FlatBufferBuilder &_fbb"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 6353658b6..bb37b9536 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -260,7 +260,7 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb return builder_.Finish(); } -inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateStatDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *id = nullptr, int64_t val = 0, uint16_t count = 0) { @@ -542,7 +542,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } -inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, +inline flatbuffers::Offset CreateMonsterDirect(flatbuffers::FlatBufferBuilder &_fbb, const Vec3 *pos = 0, int16_t mana = 150, int16_t hp = 100,