diff --git a/.gitignore b/.gitignore index 48d8d9c56e84b6c3ffbe3810a2375eba9e19dcf2..eab9d79f8d1178a744f0769049c766f4d738ce86 100755 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ flatsamplebinary flatsampletext snapshot.sh tests/go_gen +tests/monsterdata_java_wire.mon CMakeLists.txt.user CMakeScripts/** build/Xcode/FlatBuffers.xcodeproj/project.xcworkspace/** diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 54feca9d8784818ab051a774a475a6034cd7c59a..3caf790a35e165e4b491c6b453532494734ca221 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -35,7 +35,7 @@ namespace flatbuffers { #define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \ TD(NONE, "", uint8_t, byte, byte, byte) \ TD(UTYPE, "", uint8_t, byte, byte, byte) /* begin scalar/int */ \ - TD(BOOL, "bool", uint8_t, byte, byte, byte) \ + TD(BOOL, "bool", uint8_t, boolean,byte, bool) \ TD(CHAR, "byte", int8_t, byte, int8, sbyte) \ TD(UCHAR, "ubyte", uint8_t, byte, byte, byte) \ TD(SHORT, "short", int16_t, short, int16, short) \ diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 695a29a4a965f4963cddb874adee58d601690df1..303996ff44669fa0e1f3185d10641a0897cbe5e7 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -102,21 +102,23 @@ public class FlatBufferBuilder { // Add a scalar to the buffer, backwards from the current location. // Doesn't align nor check for space. - public void putByte (byte x) { bb.put (space -= 1, x); } - public void putShort (short x) { bb.putShort (space -= 2, x); } - public void putInt (int x) { bb.putInt (space -= 4, x); } - public void putLong (long x) { bb.putLong (space -= 8, x); } - public void putFloat (float x) { bb.putFloat (space -= 4, x); } - public void putDouble(double x) { bb.putDouble(space -= 8, x); } + public void putBoolean(boolean x) { bb.put (space -= 1, (byte)(x ? 1 : 0)); } + public void putByte (byte x) { bb.put (space -= 1, x); } + public void putShort (short x) { bb.putShort (space -= 2, x); } + public void putInt (int x) { bb.putInt (space -= 4, x); } + public void putLong (long x) { bb.putLong (space -= 8, x); } + public void putFloat (float x) { bb.putFloat (space -= 4, x); } + public void putDouble (double x) { bb.putDouble(space -= 8, x); } // Adds a scalar to the buffer, properly aligned, and the buffer grown // if needed. - public void addByte (byte x) { prep(1, 0); putByte (x); } - public void addShort (short x) { prep(2, 0); putShort (x); } - public void addInt (int x) { prep(4, 0); putInt (x); } - public void addLong (long x) { prep(8, 0); putLong (x); } - public void addFloat (float x) { prep(4, 0); putFloat (x); } - public void addDouble(double x) { prep(8, 0); putDouble(x); } + public void addBoolean(boolean x) { prep(1, 0); putBoolean(x); } + public void addByte (byte x) { prep(1, 0); putByte (x); } + public void addShort (short x) { prep(2, 0); putShort (x); } + public void addInt (int x) { prep(4, 0); putInt (x); } + public void addLong (long x) { prep(8, 0); putLong (x); } + public void addFloat (float x) { prep(4, 0); putFloat (x); } + public void addDouble (double x) { prep(8, 0); putDouble (x); } // Adds on offset, relative to where it will be written. public void addOffset(int off) { @@ -169,13 +171,14 @@ public class FlatBufferBuilder { } // Add a scalar to a table at `o` into its vtable, with value `x` and default `d` - public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } } - public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } } - public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } } - public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } } - public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } } - public void addDouble(int o, double x, double d) { if(x != d) { addDouble(x); slot(o); } } - public void addOffset(int o, int x, int d) { if(x != d) { addOffset(x); slot(o); } } + public void addBoolean(int o, boolean x, boolean d) { if(x != d) { addBoolean(x); slot(o); } } + public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } } + public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } } + public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } } + public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } } + public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } } + public void addDouble (int o, double x, double d) { if(x != d) { addDouble (x); slot(o); } } + public void addOffset (int o, int x, int d) { if(x != d) { addOffset (x); slot(o); } } // Structs are stored inline, so nothing additional is being added. `d` is always 0. public void addStruct(int voffset, int x, int d) { diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index a91b8f27e9d33542d6db34eeb0a35ae8ad66f4f9..d5e02b3f4dda03df10fc280af5eb9ca8088468f5 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -105,6 +105,11 @@ namespace FlatBuffers Pad(alignSize); } + public void PutBool(bool x) + { + _bb.PutByte(_space -= sizeof(byte), (byte)(x ? 1 : 0)); + } + public void PutSbyte(sbyte x) { _bb.PutSbyte(_space -= sizeof(sbyte), x); @@ -157,6 +162,7 @@ namespace FlatBuffers // Adds a scalar to the buffer, properly aligned, and the buffer grown // if needed. + public void AddBool(bool x) { Prep(sizeof(byte), 0); PutBool(x); } public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); } public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); } public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); } @@ -231,6 +237,7 @@ namespace FlatBuffers } // Add a scalar to a table at `o` into its vtable, with value `x` and default `d` + public void AddBool(int o, bool x, bool d) { if (x != d) { AddBool(x); Slot(o); } } public void AddSbyte(int o, sbyte x, sbyte d) { if (x != d) { AddSbyte(x); Slot(o); } } public void AddByte(int o, byte x, byte d) { if (x != d) { AddByte(x); Slot(o); } } public void AddShort(int o, short x, int d) { if (x != d) { AddShort(x); Slot(o); } } diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index f0244c50aa5b56988a60c59453ea18a691b9e08d..fb9610643710ef2b7aaf77e90bcbf1f7f2add9c0 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -214,13 +214,17 @@ static std::string GenGetter(const LanguageParameters &lang, 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_UNION: return "__union"; case BASE_TYPE_VECTOR: return GenGetter(lang, type.VectorType()); - default: - return "bb." + FunctionStart(lang, 'G') + "et" + - (GenTypeBasic(lang, type) != "byte" - ? MakeCamel(GenTypeGet(lang, type)) - : ""); + default: { + std::string getter = "bb." + FunctionStart(lang, 'G') + "et"; + if (type.base_type == BASE_TYPE_BOOL) { + getter = "0!=" + getter; + } else if (GenTypeBasic(lang, type) != "byte") { + getter += MakeCamel(GenTypeGet(lang, type)); + } + return getter; + } } } @@ -364,7 +368,10 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += "(bb_pos + " + NumToString(field.value.offset) + ")"; } else { code += offset_prefix + getter; - code += "(o + bb_pos) : " + default_cast + field.value.constant; + code += "(o + bb_pos) : " + default_cast; + code += field.value.type.base_type == BASE_TYPE_BOOL + ? (field.value.constant == "0" ? "false" : "true") + : field.value.constant; } } else { switch (field.value.type.base_type) { @@ -524,7 +531,12 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd"; code += GenMethod(lang, field.value.type) + "("; code += NumToString(it - struct_def.fields.vec.begin()) + ", "; - code += argname + ", " + field.value.constant; + code += argname + ", "; + if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } else { + code += field.value.constant; + } code += "); }\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { auto vector_type = field.value.type.VectorType(); diff --git a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs index 4fcc83ea7f244c2ab7be867d95366c733dbba266..1db1352ed8e1aad3c349b88f00c77d350b227cdf 100644 --- a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs +++ b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs @@ -76,6 +76,7 @@ namespace FlatBuffers.Test Monster.AddTest(fbb, mon2); Monster.AddTest4(fbb, test4); Monster.AddTestarrayofstring(fbb, testArrayOfString); + Monster.AddTestbool(fbb, false); var mon = Monster.EndMonster(fbb); fbb.Finish(mon); @@ -134,6 +135,8 @@ namespace FlatBuffers.Test Assert.AreEqual(2, monster.TestarrayofstringLength()); Assert.AreEqual("test1", monster.Testarrayofstring(0)); Assert.AreEqual("test2", monster.Testarrayofstring(1)); + + Assert.AreEqual(false, monster.Testbool()); } public void CanReadCppGeneratedWireFile() diff --git a/tests/JavaTest.java b/tests/JavaTest.java index f6dfb6ab1478bfe75472a8579ddbe5c3f48952e7..75fb598207cd748cbf1bcf5ff039ca43f8866c76 100755 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -80,6 +80,7 @@ class JavaTest { Monster.addTest(fbb, mon2); Monster.addTest4(fbb, test4); Monster.addTestarrayofstring(fbb, testArrayOfString); + Monster.addTestbool(fbb, false); int mon = Monster.endMonster(fbb); Monster.finishMonsterBuffer(fbb, mon); @@ -166,6 +167,8 @@ class JavaTest { TestEq(monster.testarrayofstringLength(), 2); TestEq(monster.testarrayofstring(0),"test1"); TestEq(monster.testarrayofstring(1),"test2"); + + TestEq(monster.testbool(), false); } static <T> void TestEq(T a, T b) { diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 2b36eed3b239cc65030bc49a7e3f1fdccb811cd8..da56bea8f795da22c23e81e1c749df293cff8809 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -36,8 +36,9 @@ public class Monster : Table { public int TestnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } public Stat Testempty() { return Testempty(new Stat()); } public Stat Testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public bool Testbool() { int o = __offset(34); return o != 0 ? 0!=bb.Get(o + bb_pos) : (bool)false; } - public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(15); } + public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(16); } public static void AddPos(FlatBufferBuilder builder, int posOffset) { builder.AddStruct(0, posOffset, 0); } public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); } public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); } @@ -61,6 +62,7 @@ public class Monster : Table { public static int CreateTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte(data[i]); return builder.EndVector(); } public static void StartTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); } public static void AddTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.AddOffset(14, testemptyOffset, 0); } + public static void AddTestbool(FlatBufferBuilder builder, bool testbool) { builder.AddBool(15, testbool, false); } public static int EndMonster(FlatBufferBuilder builder) { int o = builder.EndObject(); builder.Required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 38f5871d5bac68e12338332d705f384765da4334..a47f9ee3bb3faec94c7e596ad4587c8b4ff870c0 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -207,7 +207,15 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat { return nil } -func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(15) } +func (rcv *Monster) Testbool() byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(34)) + if o != 0 { + return rcv._tab.GetByte(o + rcv._tab.Pos) + } + return 0 +} + +func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(16) } 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) } @@ -232,4 +240,5 @@ func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflat 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 MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index dad4d590da31150e34112552461edca01fd4b7d4..25f3515d3bf242a96766ce394971aaf3e3f0a86d 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -41,8 +41,9 @@ public class Monster extends Table { public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); } public Stat testempty() { return testempty(new Stat()); } public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public boolean testbool() { int o = __offset(34); return o != 0 ? 0!=bb.get(o + bb_pos) : false; } - public static void startMonster(FlatBufferBuilder builder) { builder.startObject(15); } + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(16); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); } public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); } @@ -66,6 +67,7 @@ public class Monster extends Table { public static int createTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addByte(data[i]); return builder.endVector(); } public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); } public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); } + public static void addTestbool(FlatBufferBuilder builder, boolean testbool) { builder.addBoolean(15, testbool, false); } public static int endMonster(FlatBufferBuilder builder) { int o = builder.endObject(); builder.required(o, 10); // name diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index a29b5e61304b3b7e786f7a6d2c54fb965e0d86d0..01b9e5965ecf378c5f26881349bdedfababcb6f1 100755 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -43,6 +43,7 @@ table Monster { test4:[Test] (id: 9); testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster"); testempty:Stat (id:14); + testbool:bool (id:15); } root_type Monster; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 2fedc9fa58eafa63628a08b4274021b1af8ad616..f3ff2467d11723fc19e4478067f5406d126bab16 100755 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -140,6 +140,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const flatbuffers::Vector<uint8_t> *testnestedflatbuffer() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(30); } const Monster *testnestedflatbuffer_nested_root() { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); } const Stat *testempty() const { return GetPointer<const Stat *>(32); } + uint8_t testbool() const { return GetField<uint8_t>(34, 0); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField<Vec3>(verifier, 4 /* pos */) && @@ -167,6 +168,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.Verify(testnestedflatbuffer()) && VerifyField<flatbuffers::uoffset_t>(verifier, 32 /* testempty */) && verifier.VerifyTable(testempty()) && + VerifyField<uint8_t>(verifier, 34 /* testbool */) && verifier.EndTable(); } }; @@ -188,10 +190,11 @@ struct MonsterBuilder { void add_enemy(flatbuffers::Offset<Monster> enemy) { fbb_.AddOffset(28, enemy); } void add_testnestedflatbuffer(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); } void add_testempty(flatbuffers::Offset<Stat> testempty) { fbb_.AddOffset(32, testempty); } + void add_testbool(uint8_t testbool) { fbb_.AddElement<uint8_t>(34, testbool, 0); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset<Monster> Finish() { - auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 15)); + auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 16)); fbb_.Required(o, 10); // name return o; } @@ -211,7 +214,8 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables = 0, flatbuffers::Offset<Monster> enemy = 0, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer = 0, - flatbuffers::Offset<Stat> testempty = 0) { + flatbuffers::Offset<Stat> testempty = 0, + uint8_t testbool = 0) { MonsterBuilder builder_(_fbb); builder_.add_testempty(testempty); builder_.add_testnestedflatbuffer(testnestedflatbuffer); @@ -225,6 +229,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder builder_.add_pos(pos); builder_.add_hp(hp); builder_.add_mana(mana); + builder_.add_testbool(testbool); builder_.add_test_type(test_type); builder_.add_color(color); return builder_.Finish(); diff --git a/tests/monsterdata_test.mon b/tests/monsterdata_test.mon index e20e1a9856cb659147c2eb1f7c2d888e95d99c3b..1b79b7eb408e0d76ceeee7675b271904bd15cfd8 100644 Binary files a/tests/monsterdata_test.mon and b/tests/monsterdata_test.mon differ