diff --git a/.gitignore b/.gitignore index 5d7a88540b7e4a203cee076523365915611f07c2..c8415516a3c5ea4fde00cd2bebdc9dd33520647e 100755 --- a/.gitignore +++ b/.gitignore @@ -31,10 +31,15 @@ proguard-project.txt linklint_results Makefile flatc +flatc.exe flathash +flathash.exe flattests +flattests.exe flatsamplebinary +flatsamplebinary.exe flatsampletext +flatsampletext.exe snapshot.sh tests/go_gen tests/monsterdata_java_wire.mon @@ -50,3 +55,4 @@ java/.idea java/*.iml java/target **/*.pyc +.idea \ No newline at end of file diff --git a/docs/source/JavaUsage.md b/docs/source/JavaUsage.md index 6ffb14b713f00ee9d5e57d5e85b70ca62bf9a4d2..4819e5b7927d30bd87b4205e63eb8eeff166f04e 100755 --- a/docs/source/JavaUsage.md +++ b/docs/source/JavaUsage.md @@ -177,3 +177,48 @@ There currently is no support for parsing text (Schema's and JSON) directly from Java or C#, though you could use the C++ parser through native call interfaces available to each language. Please see the C++ documentation for more on text parsing. + +### Mutating FlatBuffers + +As you saw above, typically once you have created a FlatBuffer, it is +read-only from that moment on. There are however cases where you have just +received a FlatBuffer, and you'd like to modify something about it before +sending it on to another recipient. With the above functionality, you'd have +to generate an entirely new FlatBuffer, while tracking what you modify in your +own data structures. This is inconvenient. + +For this reason FlatBuffers can also be mutated in-place. While this is great +for making small fixes to an existing buffer, you generally want to create +buffers from scratch whenever possible, since it is much more efficient and +the API is much more general purpose. + +To get non-const accessors, invoke `flatc` with `--gen-mutable`. + +You now can: + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.java} + Monster monster = Monster.getRootAsMonster(bb); + monster.mutateHp(10); // Set table field. + monster.pos().mutateZ(4); // Set struct field. + monster.mutateInventory(0, 1); // Set vector element. +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +We use the somewhat verbose term `mutate` instead of `set` to indicate that +this is a special use case, not to be confused with the default way of +constructing FlatBuffer data. + +After the above mutations, you can send on the FlatBuffer to a new recipient +without any further work! + +Note that any `mutate` functions on tables return a boolean, which is false +if the field we're trying to set isn't present in the buffer. Fields are not +present if they weren't set, or even if they happen to be equal to the +default value. For example, in the creation code above we set the `mana` field +to `150`, which is the default value, so it was never stored in the buffer. +Trying to call mutateMana() on such data will return false, and the value won't +actually be modified! + +One way to solve this is to call `forceDefaults()` on a +`FlatBufferBuilder` to force all fields you set to actually be written. This +of course increases the size of the buffer somewhat, but this may be +acceptable for a mutable buffer. diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs index c986269b9ccd863d90a0f23a166c0455205a6ca9..50c6fa960e4ea74a968368614d345c91dd7bb566 100755 --- a/net/FlatBuffers/ByteBuffer.cs +++ b/net/FlatBuffers/ByteBuffer.cs @@ -35,10 +35,17 @@ namespace FlatBuffers public byte[] Data { get { return _buffer; } } - public ByteBuffer(byte[] buffer) + public ByteBuffer(byte[] buffer) : this(buffer, 0) { } + + public ByteBuffer(byte[] buffer, int pos) { _buffer = buffer; - _pos = 0; + _pos = pos; + } + + public int Position { + get { return _pos; } + set { _pos = value; } } public void Reset() @@ -46,8 +53,6 @@ namespace FlatBuffers _pos = 0; } - public int Position { get { return _pos; } } - // Pre-allocated helper arrays for convertion. private float[] floathelper = new[] { 0.0f }; private int[] inthelper = new[] { 0 }; @@ -97,7 +102,6 @@ namespace FlatBuffers _buffer[offset + count - 1 - i] = (byte)(data >> i * 8); } } - _pos = offset; } protected ulong ReadLittleEndian(int offset, int count) @@ -134,14 +138,18 @@ namespace FlatBuffers { AssertOffsetAndLength(offset, sizeof(sbyte)); _buffer[offset] = (byte)value; - _pos = offset; } public void PutByte(int offset, byte value) { AssertOffsetAndLength(offset, sizeof(byte)); _buffer[offset] = value; - _pos = offset; + } + + // this method exists in order to conform with Java ByteBuffer standards + public void Put(int offset, byte value) + { + PutByte(offset, value); } #if UNSAFE_BYTEBUFFER diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index f184f52cccc283380e57cf37a69bfd2a9dc8db66..39d386f403d8780fbe2276e68f2eb081d774d02c 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -86,8 +86,7 @@ namespace FlatBuffers Buffer.BlockCopy(oldBuf, 0, newBuf, newBufSize - oldBufSize, oldBufSize); - - _bb = new ByteBuffer(newBuf); + _bb = new ByteBuffer(newBuf, newBufSize); } // Prepare to write an element of `size` after `additional_bytes` @@ -370,6 +369,7 @@ namespace FlatBuffers { Prep(_minAlign, sizeof(int)); AddOffset(rootTable); + _bb.Position = _space; } public ByteBuffer DataBuffer { get { return _bb; } } @@ -398,7 +398,7 @@ namespace FlatBuffers { AddByte((byte)fileIdentifier[i]); } - AddOffset(rootTable); + Finish(rootTable); } diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 00580d45a5cb756ec45373acefcae0a94e6dd167..917bf0bbde2f2b722e6429da17cc80ecd776c514 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -363,6 +363,33 @@ static std::string DestinationValue(const LanguageParameters &lang, } } +// Cast statements for mutator method parameters. +// In Java, parameters representing unsigned numbers need to be cast down to their respective type. +// For example, a long holding an unsigned int value would be cast down to int before being put onto the buffer. +// In C#, one cast directly cast an Enum to its underlying type, which is essential before putting it onto the buffer. +static std::string SourceCast(const LanguageParameters &lang, + const Type &type) { + if (type.base_type == BASE_TYPE_VECTOR) { + return SourceCast(lang, type.VectorType()); + } else { + switch (lang.language) { + case GeneratorOptions::kJava: + if (type.base_type == BASE_TYPE_UINT) return "(int)"; + else if (type.base_type == BASE_TYPE_USHORT) return "(short)"; + else if (type.base_type == BASE_TYPE_UCHAR) return "(byte)"; + break; + case GeneratorOptions::kCSharp: + if (type.enum_def != nullptr && + type.base_type != BASE_TYPE_UNION) + return "(" + GenTypeGet(lang, type) + ")"; + break; + default: + break; + } + return ""; + } +} + static std::string GenDefaultValue(const LanguageParameters &lang, const Value &value, bool for_buffer) { if(lang.language == GeneratorOptions::kCSharp && !for_buffer) { switch(value.type.base_type) { @@ -474,6 +501,22 @@ static std::string GenGetter(const LanguageParameters &lang, } } +// Direct mutation is only allowed for scalar fields. +// Hence a setter method will only be generated for such fields. +static std::string GenSetter(const LanguageParameters &lang, + const Type &type) { + if (IsScalar(type.base_type)) { + std::string setter = "bb." + FunctionStart(lang, 'P') + "ut"; + if (GenTypeBasic(lang, type) != "byte" && + type.base_type != BASE_TYPE_BOOL) { + setter += MakeCamel(GenTypeGet(lang, type)); + } + return setter; + } else { + return ""; + } +} + // Returns the method name for use with add/put calls. static std::string GenMethod(const LanguageParameters &lang, const Type &type) { return IsScalar(type.base_type) @@ -539,7 +582,8 @@ static void GenStructBody(const LanguageParameters &lang, } static void GenStruct(const LanguageParameters &lang, const Parser &parser, - StructDef &struct_def, std::string *code_ptr) { + StructDef &struct_def, const GeneratorOptions &opts, + std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; @@ -599,8 +643,10 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, std::string type_name_dest = GenTypeNameDest(lang, field.value.type); std::string dest_mask = DestinationMask(lang, field.value.type, true); std::string dest_cast = DestinationCast(lang, field.value.type); + std::string src_cast = SourceCast(lang, field.value.type); std::string method_start = " public " + type_name_dest + " " + MakeCamel(field.name, lang.first_camel_upper); + // 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(" + @@ -744,6 +790,41 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, InlineSize(field.value.type.VectorType())); code += "); }\n"; } + + // generate mutators for scalar fields or vectors of scalars + if (opts.mutable_buffer) { + auto underlying_type = field.value.type.base_type == BASE_TYPE_VECTOR + ? field.value.type.VectorType() + : field.value.type; + // boolean parameters have to be explicitly converted to byte representation + auto setter_parameter = underlying_type.base_type == BASE_TYPE_BOOL ? "(byte)(" + field.name + " ? 1 : 0)" : field.name; + auto mutator_prefix = MakeCamel("mutate", lang.first_camel_upper); + //a vector mutator also needs the index of the vector element it should mutate + auto mutator_params = (field.value.type.base_type == BASE_TYPE_VECTOR ? "(int j, " : "(") + + GenTypeNameDest(lang, underlying_type) + " " + + field.name + ") { "; + auto setter_index = field.value.type.base_type == BASE_TYPE_VECTOR + ? "__vector(o) + j * " + NumToString(InlineSize(underlying_type)) + : (struct_def.fixed ? "bb_pos + " + NumToString(field.value.offset) : "o + bb_pos"); + + + if (IsScalar(field.value.type.base_type) || + (field.value.type.base_type == BASE_TYPE_VECTOR && + IsScalar(field.value.type.VectorType().base_type))) { + code += " public "; + code += struct_def.fixed ? "void " : lang.bool_type; + code += mutator_prefix + MakeCamel(field.name, true); + code += mutator_params; + if (struct_def.fixed) { + code += GenSetter(lang, underlying_type) + "(" + setter_index + ", "; + code += src_cast + setter_parameter + "); }\n"; + } else { + code += "int o = __offset(" + NumToString(field.value.offset) + ");"; + code += " if (o != 0) { " + GenSetter(lang, underlying_type); + code += "(" + setter_index + ", " + src_cast + setter_parameter + "); return true; } else { return false; } }\n"; + } + } + } } code += "\n"; if (struct_def.fixed) { @@ -980,7 +1061,7 @@ bool GenerateGeneral(const Parser &parser, for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); ++it) { std::string declcode; - GenStruct(lang, parser, **it, &declcode); + GenStruct(lang, parser, **it, opts, &declcode); if (opts.one_file) { one_file_code += declcode; } diff --git a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs index c4bdf2507e11ff3509476b8d9be3c111c3c0f58a..666ac1c6ac466a804b93eadcedb7e9a3b3ad67da 100644 --- a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs +++ b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs @@ -65,7 +65,6 @@ namespace FlatBuffers.Test fbb.AddOffset(test1.Value); var testArrayOfString = fbb.EndVector(); - Monster.StartMonster(fbb); Monster.AddPos(fbb, Vec3.CreateVec3(fbb, 1.0f, 2.0f, 3.0f, 3.0, Color.Green, (short)5, (sbyte)6)); @@ -79,7 +78,8 @@ namespace FlatBuffers.Test Monster.AddTestbool(fbb, false); var mon = Monster.EndMonster(fbb); - fbb.Finish(mon.Value); + Monster.FinishMonsterBuffer(fbb, mon); + // Dump to output directory so we can inspect later, if needed using (var ms = new MemoryStream(fbb.DataBuffer.Data, fbb.DataBuffer.Position, fbb.Offset)) @@ -90,6 +90,51 @@ namespace FlatBuffers.Test // Now assert the buffer TestBuffer(fbb.DataBuffer); + + //Attempt to mutate Monster fields and check whether the buffer has been mutated properly + // revert to original values after testing + Monster monster = Monster.GetRootAsMonster(fbb.DataBuffer); + + // mana is optional and does not exist in the buffer so the mutation should fail + // the mana field should retain its default value + Assert.AreEqual(monster.MutateMana((short)10), false); + Assert.AreEqual(monster.Mana, (short)150); + + // testType is an existing field and mutating it should succeed + Assert.AreEqual(monster.TestType, Any.Monster); + Assert.AreEqual(monster.MutateTestType(Any.NONE), true); + Assert.AreEqual(monster.TestType, Any.NONE); + Assert.AreEqual(monster.MutateTestType(Any.Monster), true); + Assert.AreEqual(monster.TestType, Any.Monster); + + //mutate the inventory vector + Assert.AreEqual(monster.MutateInventory(0, 1), true); + Assert.AreEqual(monster.MutateInventory(1, 2), true); + Assert.AreEqual(monster.MutateInventory(2, 3), true); + Assert.AreEqual(monster.MutateInventory(3, 4), true); + Assert.AreEqual(monster.MutateInventory(4, 5), true); + + for (int i = 0; i < monster.InventoryLength; i++) + { + Assert.AreEqual(monster.GetInventory(i), i + 1); + } + + //reverse mutation + Assert.AreEqual(monster.MutateInventory(0, 0), true); + Assert.AreEqual(monster.MutateInventory(1, 1), true); + Assert.AreEqual(monster.MutateInventory(2, 2), true); + Assert.AreEqual(monster.MutateInventory(3, 3), true); + Assert.AreEqual(monster.MutateInventory(4, 4), true); + + // get a struct field and edit one of its fields + Vec3 pos = monster.Pos; + Assert.AreEqual(pos.X, 1.0f); + pos.MutateX(55.0f); + Assert.AreEqual(pos.X, 55.0f); + pos.MutateX(1.0f); + Assert.AreEqual(pos.X, 1.0f); + + TestBuffer(fbb.DataBuffer); } private void TestBuffer(ByteBuffer bb) diff --git a/tests/FlatBuffers.Test/Program.cs b/tests/FlatBuffers.Test/Program.cs index 6006004a72c8f9bc051889af7353a5f00aae6031..91cb6941e0f7b67d130e96470ba5c408d79520e4 100644 --- a/tests/FlatBuffers.Test/Program.cs +++ b/tests/FlatBuffers.Test/Program.cs @@ -56,6 +56,7 @@ namespace FlatBuffers.Test } + Console.WriteLine("FlatBuffers test: completed successfully"); return 0; } } diff --git a/tests/JavaTest.java b/tests/JavaTest.java index 75fb598207cd748cbf1bcf5ff039ca43f8866c76..e01701699a5e70b18b14cc452bb8896522052be3 100755 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -81,6 +81,7 @@ class JavaTest { Monster.addTest4(fbb, test4); Monster.addTestarrayofstring(fbb, testArrayOfString); Monster.addTestbool(fbb, false); + Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L); int mon = Monster.endMonster(fbb); Monster.finishMonsterBuffer(fbb, mon); @@ -101,15 +102,59 @@ class JavaTest { } // Test it: - TestBuffer(fbb.dataBuffer()); + TestExtendedBuffer(fbb.dataBuffer()); // Make sure it also works with read only ByteBuffers. This is slower, // since creating strings incurs an additional copy // (see Table.__string). - TestBuffer(fbb.dataBuffer().asReadOnlyBuffer()); + TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer()); TestEnums(); + //Attempt to mutate Monster fields and check whether the buffer has been mutated properly + // revert to original values after testing + Monster monster = Monster.getRootAsMonster(fbb.dataBuffer()); + + // mana is optional and does not exist in the buffer so the mutation should fail + // the mana field should retain its default value + TestEq(monster.mutateMana((short)10), false); + TestEq(monster.mana(), (short)150); + + // testType is an existing field and mutating it should succeed + TestEq(monster.testType(), (byte)Any.Monster); + TestEq(monster.mutateTestType(Any.NONE), true); + TestEq(monster.testType(), (byte)Any.NONE); + TestEq(monster.mutateTestType(Any.Monster), true); + TestEq(monster.testType(), (byte)Any.Monster); + + //mutate the inventory vector + TestEq(monster.mutateInventory(0, 1), true); + TestEq(monster.mutateInventory(1, 2), true); + TestEq(monster.mutateInventory(2, 3), true); + TestEq(monster.mutateInventory(3, 4), true); + TestEq(monster.mutateInventory(4, 5), true); + + for (int i = 0; i < monster.inventoryLength(); i++) { + TestEq(monster.inventory(i), i + 1); + } + + //reverse mutation + TestEq(monster.mutateInventory(0, 0), true); + TestEq(monster.mutateInventory(1, 1), true); + TestEq(monster.mutateInventory(2, 2), true); + TestEq(monster.mutateInventory(3, 3), true); + TestEq(monster.mutateInventory(4, 4), true); + + // get a struct field and edit one of its fields + Vec3 pos = monster.pos(); + TestEq(pos.x(), 1.0f); + pos.mutateX(55.0f); + TestEq(pos.x(), 55.0f); + pos.mutateX(1.0f); + TestEq(pos.x(), 1.0f); + + TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer()); + System.out.println("FlatBuffers test: completed successfully"); } @@ -122,7 +167,7 @@ class JavaTest { static void TestBuffer(ByteBuffer bb) { TestEq(Monster.MonsterBufferHasIdentifier(bb), true); - + Monster monster = Monster.getRootAsMonster(bb); TestEq(monster.hp(), (short)80); @@ -171,6 +216,16 @@ class JavaTest { TestEq(monster.testbool(), false); } + // this method checks additional fields not present in the binary buffer read from file + // these new tests are performed on top of the regular tests + static void TestExtendedBuffer(ByteBuffer bb) { + TestBuffer(bb); + + Monster monster = Monster.getRootAsMonster(bb); + + TestEq(monster.testhashu32Fnv1(), Integer.MAX_VALUE + 1L); + } + static <T> void TestEq(T a, T b) { if (!a.equals(b)) { System.out.println("" + a.getClass().getName() + " " + b.getClass().getName()); diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 54b60808506dd6f4643f2cbca6b167d75bc099ea..975e4d08532f35bd71ff5c0674d8fe00c9a13503 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -14,12 +14,17 @@ public sealed class Monster : Table { public Vec3 Pos { get { return GetPos(new Vec3()); } } public Vec3 GetPos(Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__init(o + bb_pos, bb) : null; } public short Mana { get { int o = __offset(6); return o != 0 ? bb.GetShort(o + bb_pos) : (short)150; } } + public bool MutateMana(short mana) { int o = __offset(6); if (o != 0) { bb.PutShort(o + bb_pos, mana); return true; } else { return false; } } public short Hp { get { int o = __offset(8); return o != 0 ? bb.GetShort(o + bb_pos) : (short)100; } } + public bool MutateHp(short hp) { int o = __offset(8); if (o != 0) { bb.PutShort(o + bb_pos, hp); return true; } else { return false; } } public string Name { get { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; } } public byte GetInventory(int j) { int o = __offset(14); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; } public int InventoryLength { get { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; } } + public bool MutateInventory(int j, byte inventory) { int o = __offset(14); if (o != 0) { bb.Put(__vector(o) + j * 1, inventory); return true; } else { return false; } } public Color Color { get { int o = __offset(16); return o != 0 ? (Color)bb.GetSbyte(o + bb_pos) : (Color)8; } } + public bool MutateColor(Color color) { int o = __offset(16); if (o != 0) { bb.PutSbyte(o + bb_pos, (sbyte)color); return true; } else { return false; } } public Any TestType { get { int o = __offset(18); return o != 0 ? (Any)bb.Get(o + bb_pos) : (Any)0; } } + public bool MutateTestType(Any test_type) { int o = __offset(18); if (o != 0) { bb.Put(o + bb_pos, (byte)test_type); return true; } else { return false; } } public TTable GetTest<TTable>(TTable obj) where TTable : Table { int o = __offset(20); return o != 0 ? __union(obj, o) : null; } public Test GetTest4(int j) { return GetTest4(new Test(), j); } public Test GetTest4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; } @@ -35,17 +40,27 @@ public sealed class Monster : Table { public Monster GetEnemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public byte GetTestnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; } public int TestnestedflatbufferLength { get { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } } + public bool MutateTestnestedflatbuffer(int j, byte testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.Put(__vector(o) + j * 1, testnestedflatbuffer); return true; } else { return false; } } public Stat Testempty { get { return GetTestempty(new Stat()); } } public Stat GetTestempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public bool Testbool { get { int o = __offset(34); return o != 0 ? 0!=bb.Get(o + bb_pos) : (bool)false; } } + public bool MutateTestbool(bool testbool) { int o = __offset(34); if (o != 0) { bb.Put(o + bb_pos, (byte)(testbool ? 1 : 0)); return true; } else { return false; } } public int Testhashs32Fnv1 { get { int o = __offset(36); return o != 0 ? bb.GetInt(o + bb_pos) : (int)0; } } + public bool MutateTesthashs32Fnv1(int testhashs32_fnv1) { int o = __offset(36); if (o != 0) { bb.PutInt(o + bb_pos, testhashs32_fnv1); return true; } else { return false; } } public uint Testhashu32Fnv1 { get { int o = __offset(38); return o != 0 ? bb.GetUint(o + bb_pos) : (uint)0; } } + public bool MutateTesthashu32Fnv1(uint testhashu32_fnv1) { int o = __offset(38); if (o != 0) { bb.PutUint(o + bb_pos, testhashu32_fnv1); return true; } else { return false; } } public long Testhashs64Fnv1 { get { int o = __offset(40); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; } } + public bool MutateTesthashs64Fnv1(long testhashs64_fnv1) { int o = __offset(40); if (o != 0) { bb.PutLong(o + bb_pos, testhashs64_fnv1); return true; } else { return false; } } public ulong Testhashu64Fnv1 { get { int o = __offset(42); return o != 0 ? bb.GetUlong(o + bb_pos) : (ulong)0; } } + public bool MutateTesthashu64Fnv1(ulong testhashu64_fnv1) { int o = __offset(42); if (o != 0) { bb.PutUlong(o + bb_pos, testhashu64_fnv1); return true; } else { return false; } } public int Testhashs32Fnv1a { get { int o = __offset(44); return o != 0 ? bb.GetInt(o + bb_pos) : (int)0; } } + public bool MutateTesthashs32Fnv1a(int testhashs32_fnv1a) { int o = __offset(44); if (o != 0) { bb.PutInt(o + bb_pos, testhashs32_fnv1a); return true; } else { return false; } } public uint Testhashu32Fnv1a { get { int o = __offset(46); return o != 0 ? bb.GetUint(o + bb_pos) : (uint)0; } } + public bool MutateTesthashu32Fnv1a(uint testhashu32_fnv1a) { int o = __offset(46); if (o != 0) { bb.PutUint(o + bb_pos, testhashu32_fnv1a); return true; } else { return false; } } public long Testhashs64Fnv1a { get { int o = __offset(48); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; } } + public bool MutateTesthashs64Fnv1a(long testhashs64_fnv1a) { int o = __offset(48); if (o != 0) { bb.PutLong(o + bb_pos, testhashs64_fnv1a); return true; } else { return false; } } public ulong Testhashu64Fnv1a { get { int o = __offset(50); return o != 0 ? bb.GetUlong(o + bb_pos) : (ulong)0; } } + public bool MutateTesthashu64Fnv1a(ulong testhashu64_fnv1a) { int o = __offset(50); if (o != 0) { bb.PutUlong(o + bb_pos, testhashu64_fnv1a); return true; } else { return false; } } public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(24); } public static void AddPos(FlatBufferBuilder builder, Offset<Vec3> posOffset) { builder.AddStruct(0, posOffset.Value, 0); } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 6d1c02f031e58527fbc86395f0a08060d2fa8aa6..0a5f7dde044f4d0c75c08e9450153fb6a858469f 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -16,14 +16,19 @@ public final class Monster extends Table { public Vec3 pos() { return pos(new Vec3()); } public Vec3 pos(Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__init(o + bb_pos, bb) : null; } public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; } + public boolean mutateMana(short mana) { int o = __offset(6); if (o != 0) { bb.putShort(o + bb_pos, mana); return true; } else { return false; } } public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; } + public boolean mutateHp(short hp) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, hp); return true; } else { return false; } } public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; } public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(10, 1); } public int inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; } public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; } public ByteBuffer inventoryAsByteBuffer() { return __vector_as_bytebuffer(14, 1); } + public boolean mutateInventory(int j, int inventory) { int o = __offset(14); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)inventory); return true; } else { return false; } } public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 8; } + public boolean mutateColor(byte color) { int o = __offset(16); if (o != 0) { bb.put(o + bb_pos, color); return true; } else { return false; } } public byte testType() { int o = __offset(18); return o != 0 ? bb.get(o + bb_pos) : 0; } + public boolean mutateTestType(byte test_type) { int o = __offset(18); if (o != 0) { bb.put(o + bb_pos, test_type); return true; } else { return false; } } public Table test(Table obj) { int o = __offset(20); return o != 0 ? __union(obj, o) : null; } public Test test4(int j) { return test4(new Test(), j); } public Test test4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; } @@ -42,17 +47,27 @@ public final class Monster extends Table { public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; } public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); } + public boolean mutateTestnestedflatbuffer(int j, int testnestedflatbuffer) { int o = __offset(30); if (o != 0) { bb.put(__vector(o) + j * 1, (byte)testnestedflatbuffer); return true; } else { return false; } } 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 boolean mutateTestbool(boolean testbool) { int o = __offset(34); if (o != 0) { bb.put(o + bb_pos, (byte)(testbool ? 1 : 0)); return true; } else { return false; } } public int testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.getInt(o + bb_pos) : 0; } + public boolean mutateTesthashs32Fnv1(int testhashs32_fnv1) { int o = __offset(36); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1); return true; } else { return false; } } public long testhashu32Fnv1() { int o = __offset(38); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; } + public boolean mutateTesthashu32Fnv1(long testhashu32_fnv1) { int o = __offset(38); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1); return true; } else { return false; } } public long testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public boolean mutateTesthashs64Fnv1(long testhashs64_fnv1) { int o = __offset(40); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1); return true; } else { return false; } } public long testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public boolean mutateTesthashu64Fnv1(long testhashu64_fnv1) { int o = __offset(42); if (o != 0) { bb.putLong(o + bb_pos, testhashu64_fnv1); return true; } else { return false; } } public int testhashs32Fnv1a() { int o = __offset(44); return o != 0 ? bb.getInt(o + bb_pos) : 0; } + public boolean mutateTesthashs32Fnv1a(int testhashs32_fnv1a) { int o = __offset(44); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1a); return true; } else { return false; } } public long testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; } + public boolean mutateTesthashu32Fnv1a(long testhashu32_fnv1a) { int o = __offset(46); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1a); return true; } else { return false; } } public long testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public boolean mutateTesthashs64Fnv1a(long testhashs64_fnv1a) { int o = __offset(48); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1a); return true; } else { return false; } } public long testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public boolean mutateTesthashu64Fnv1a(long testhashu64_fnv1a) { int o = __offset(50); if (o != 0) { bb.putLong(o + bb_pos, testhashu64_fnv1a); return true; } else { return false; } } public static void startMonster(FlatBufferBuilder builder) { builder.startObject(24); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } diff --git a/tests/MyGame/Example/Stat.cs b/tests/MyGame/Example/Stat.cs index a6b5836f19f0a1d665a1e95986e6322dd6c9abef..f3250e2fa58e22ea05bf83c3359c1d8a203c2f20 100644 --- a/tests/MyGame/Example/Stat.cs +++ b/tests/MyGame/Example/Stat.cs @@ -12,7 +12,9 @@ public sealed class Stat : Table { public string Id { get { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } } public long Val { get { int o = __offset(6); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; } } + public bool MutateVal(long val) { int o = __offset(6); if (o != 0) { bb.PutLong(o + bb_pos, val); return true; } else { return false; } } public ushort Count { get { int o = __offset(8); return o != 0 ? bb.GetUshort(o + bb_pos) : (ushort)0; } } + public bool MutateCount(ushort count) { int o = __offset(8); if (o != 0) { bb.PutUshort(o + bb_pos, count); return true; } else { return false; } } public static Offset<Stat> CreateStat(FlatBufferBuilder builder, StringOffset id = default(StringOffset), diff --git a/tests/MyGame/Example/Stat.java b/tests/MyGame/Example/Stat.java index e1663c19cc38185324bae110bc1b883852091d46..673729e199f6228a6313cc382caad90c473d3826 100644 --- a/tests/MyGame/Example/Stat.java +++ b/tests/MyGame/Example/Stat.java @@ -15,7 +15,9 @@ public final class Stat extends Table { public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } public ByteBuffer idAsByteBuffer() { return __vector_as_bytebuffer(4, 1); } public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + public boolean mutateVal(long val) { int o = __offset(6); if (o != 0) { bb.putLong(o + bb_pos, val); return true; } else { return false; } } public int count() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; } + public boolean mutateCount(int count) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, (short)count); return true; } else { return false; } } public static int createStat(FlatBufferBuilder builder, int id, diff --git a/tests/MyGame/Example/Test.cs b/tests/MyGame/Example/Test.cs index b3633a2bd6052a645d7dcb27716ce312a6c3920a..182c26b7b82b3f93c1960afb26e6d8e205dac608 100644 --- a/tests/MyGame/Example/Test.cs +++ b/tests/MyGame/Example/Test.cs @@ -9,7 +9,9 @@ public sealed class Test : Struct { public Test __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public short A { get { return bb.GetShort(bb_pos + 0); } } + public void MutateA(short a) { bb.PutShort(bb_pos + 0, a); } public sbyte B { get { return bb.GetSbyte(bb_pos + 2); } } + public void MutateB(sbyte b) { bb.PutSbyte(bb_pos + 2, b); } public static Offset<Test> CreateTest(FlatBufferBuilder builder, short A, sbyte B) { builder.Prep(2, 4); diff --git a/tests/MyGame/Example/Test.java b/tests/MyGame/Example/Test.java index d4e4094c2b9f39e4b8b162b41712e9932be55363..c4cd6aa9a308295a085006c8e6c2733671902374 100644 --- a/tests/MyGame/Example/Test.java +++ b/tests/MyGame/Example/Test.java @@ -11,7 +11,9 @@ public final class Test extends Struct { public Test __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public short a() { return bb.getShort(bb_pos + 0); } + public void mutateA(short a) { bb.putShort(bb_pos + 0, a); } public byte b() { return bb.get(bb_pos + 2); } + public void mutateB(byte b) { bb.put(bb_pos + 2, b); } public static int createTest(FlatBufferBuilder builder, short a, byte b) { builder.prep(2, 4); diff --git a/tests/MyGame/Example/Vec3.cs b/tests/MyGame/Example/Vec3.cs index 8a38342f97aa3c6fa68202bb7cf3b4e47b43e01d..9f643f4fa3577faa897687fab1e0387f9e24df6c 100644 --- a/tests/MyGame/Example/Vec3.cs +++ b/tests/MyGame/Example/Vec3.cs @@ -9,10 +9,15 @@ public sealed class Vec3 : Struct { public Vec3 __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public float X { get { return bb.GetFloat(bb_pos + 0); } } + public void MutateX(float x) { bb.PutFloat(bb_pos + 0, x); } public float Y { get { return bb.GetFloat(bb_pos + 4); } } + public void MutateY(float y) { bb.PutFloat(bb_pos + 4, y); } public float Z { get { return bb.GetFloat(bb_pos + 8); } } + public void MutateZ(float z) { bb.PutFloat(bb_pos + 8, z); } public double Test1 { get { return bb.GetDouble(bb_pos + 16); } } + public void MutateTest1(double test1) { bb.PutDouble(bb_pos + 16, test1); } public Color Test2 { get { return (Color)bb.GetSbyte(bb_pos + 24); } } + public void MutateTest2(Color test2) { bb.PutSbyte(bb_pos + 24, (sbyte)test2); } public Test Test3 { get { return GetTest3(new Test()); } } public Test GetTest3(Test obj) { return obj.__init(bb_pos + 26, bb); } diff --git a/tests/MyGame/Example/Vec3.java b/tests/MyGame/Example/Vec3.java index 794eea1d808f0ebae87abeeb8e798ee280a8057e..457f57d7702808ad61b0998d5973ecde4ffbacbd 100644 --- a/tests/MyGame/Example/Vec3.java +++ b/tests/MyGame/Example/Vec3.java @@ -11,10 +11,15 @@ public final class Vec3 extends Struct { public Vec3 __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public float x() { return bb.getFloat(bb_pos + 0); } + public void mutateX(float x) { bb.putFloat(bb_pos + 0, x); } public float y() { return bb.getFloat(bb_pos + 4); } + public void mutateY(float y) { bb.putFloat(bb_pos + 4, y); } public float z() { return bb.getFloat(bb_pos + 8); } + public void mutateZ(float z) { bb.putFloat(bb_pos + 8, z); } public double test1() { return bb.getDouble(bb_pos + 16); } + public void mutateTest1(double test1) { bb.putDouble(bb_pos + 16, test1); } public byte test2() { return bb.get(bb_pos + 24); } + public void mutateTest2(byte test2) { bb.put(bb_pos + 24, test2); } public Test test3() { return test3(new Test()); } public Test test3(Test obj) { return obj.__init(bb_pos + 26, bb); }