diff --git a/tests/MyGame/MonsterExtra.cs b/tests/MyGame/MonsterExtra.cs index fc9a32348ee..e67ecd90c63 100644 --- a/tests/MyGame/MonsterExtra.cs +++ b/tests/MyGame/MonsterExtra.cs @@ -14,6 +14,7 @@ public struct MonsterExtra : IFlatbufferObject public ByteBuffer ByteBuffer { get { return __p.bb; } } public static MonsterExtra GetRootAsMonsterExtra(ByteBuffer _bb) { return GetRootAsMonsterExtra(_bb, new MonsterExtra()); } public static MonsterExtra GetRootAsMonsterExtra(ByteBuffer _bb, MonsterExtra obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); } + public static bool MonsterExtraBufferHasIdentifier(ByteBuffer _bb) { return Table.__has_identifier(_bb, "MONE"); } public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; } public MonsterExtra __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } @@ -29,6 +30,24 @@ public struct MonsterExtra : IFlatbufferObject public bool MutateTestdPinf(double testd_pinf) { int o = __p.__offset(12); if (o != 0) { __p.bb.PutDouble(o + __p.bb_pos, testd_pinf); return true; } else { return false; } } public double TestdNinf { get { int o = __p.__offset(14); return o != 0 ? __p.bb.GetDouble(o + __p.bb_pos) : (double)Double.NegativeInfinity; } } public bool MutateTestdNinf(double testd_ninf) { int o = __p.__offset(14); if (o != 0) { __p.bb.PutDouble(o + __p.bb_pos, testd_ninf); return true; } else { return false; } } + public float TestfVec(int j) { int o = __p.__offset(16); return o != 0 ? __p.bb.GetFloat(__p.__vector(o) + j * 4) : (float)0; } + public int TestfVecLength { get { int o = __p.__offset(16); return o != 0 ? __p.__vector_len(o) : 0; } } +#if ENABLE_SPAN_T + public Span GetTestfVecBytes() { return __p.__vector_as_span(16); } +#else + public ArraySegment? GetTestfVecBytes() { return __p.__vector_as_arraysegment(16); } +#endif + public float[] GetTestfVecArray() { return __p.__vector_as_array(16); } + public bool MutateTestfVec(int j, float testf_vec) { int o = __p.__offset(16); if (o != 0) { __p.bb.PutFloat(__p.__vector(o) + j * 4, testf_vec); return true; } else { return false; } } + public double TestdVec(int j) { int o = __p.__offset(18); return o != 0 ? __p.bb.GetDouble(__p.__vector(o) + j * 8) : (double)0; } + public int TestdVecLength { get { int o = __p.__offset(18); return o != 0 ? __p.__vector_len(o) : 0; } } +#if ENABLE_SPAN_T + public Span GetTestdVecBytes() { return __p.__vector_as_span(18); } +#else + public ArraySegment? GetTestdVecBytes() { return __p.__vector_as_arraysegment(18); } +#endif + public double[] GetTestdVecArray() { return __p.__vector_as_array(18); } + public bool MutateTestdVec(int j, double testd_vec) { int o = __p.__offset(18); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, testd_vec); return true; } else { return false; } } public static Offset CreateMonsterExtra(FlatBufferBuilder builder, float testf_nan = Single.NaN, @@ -36,28 +55,42 @@ public static Offset CreateMonsterExtra(FlatBufferBuilder builder, float testf_ninf = Single.NegativeInfinity, double testd_nan = Double.NaN, double testd_pinf = Double.PositiveInfinity, - double testd_ninf = Double.NegativeInfinity) { - builder.StartObject(6); + double testd_ninf = Double.NegativeInfinity, + VectorOffset testf_vecOffset = default(VectorOffset), + VectorOffset testd_vecOffset = default(VectorOffset)) { + builder.StartObject(8); MonsterExtra.AddTestdNinf(builder, testd_ninf); MonsterExtra.AddTestdPinf(builder, testd_pinf); MonsterExtra.AddTestdNan(builder, testd_nan); + MonsterExtra.AddTestdVec(builder, testd_vecOffset); + MonsterExtra.AddTestfVec(builder, testf_vecOffset); MonsterExtra.AddTestfNinf(builder, testf_ninf); MonsterExtra.AddTestfPinf(builder, testf_pinf); MonsterExtra.AddTestfNan(builder, testf_nan); return MonsterExtra.EndMonsterExtra(builder); } - public static void StartMonsterExtra(FlatBufferBuilder builder) { builder.StartObject(6); } + public static void StartMonsterExtra(FlatBufferBuilder builder) { builder.StartObject(8); } public static void AddTestfNan(FlatBufferBuilder builder, float testfNan) { builder.AddFloat(0, testfNan, Single.NaN); } public static void AddTestfPinf(FlatBufferBuilder builder, float testfPinf) { builder.AddFloat(1, testfPinf, Single.PositiveInfinity); } public static void AddTestfNinf(FlatBufferBuilder builder, float testfNinf) { builder.AddFloat(2, testfNinf, Single.NegativeInfinity); } public static void AddTestdNan(FlatBufferBuilder builder, double testdNan) { builder.AddDouble(3, testdNan, Double.NaN); } public static void AddTestdPinf(FlatBufferBuilder builder, double testdPinf) { builder.AddDouble(4, testdPinf, Double.PositiveInfinity); } public static void AddTestdNinf(FlatBufferBuilder builder, double testdNinf) { builder.AddDouble(5, testdNinf, Double.NegativeInfinity); } + public static void AddTestfVec(FlatBufferBuilder builder, VectorOffset testfVecOffset) { builder.AddOffset(6, testfVecOffset.Value, 0); } + public static VectorOffset CreateTestfVecVector(FlatBufferBuilder builder, float[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddFloat(data[i]); return builder.EndVector(); } + public static VectorOffset CreateTestfVecVectorBlock(FlatBufferBuilder builder, float[] data) { builder.StartVector(4, data.Length, 4); builder.Add(data); return builder.EndVector(); } + public static void StartTestfVecVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); } + public static void AddTestdVec(FlatBufferBuilder builder, VectorOffset testdVecOffset) { builder.AddOffset(7, testdVecOffset.Value, 0); } + public static VectorOffset CreateTestdVecVector(FlatBufferBuilder builder, double[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddDouble(data[i]); return builder.EndVector(); } + public static VectorOffset CreateTestdVecVectorBlock(FlatBufferBuilder builder, double[] data) { builder.StartVector(8, data.Length, 8); builder.Add(data); return builder.EndVector(); } + public static void StartTestdVecVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); } public static Offset EndMonsterExtra(FlatBufferBuilder builder) { int o = builder.EndObject(); return new Offset(o); } + public static void FinishMonsterExtraBuffer(FlatBufferBuilder builder, Offset offset) { builder.Finish(offset.Value, "MONE"); } + public static void FinishSizePrefixedMonsterExtraBuffer(FlatBufferBuilder builder, Offset offset) { builder.FinishSizePrefixed(offset.Value, "MONE"); } }; diff --git a/tests/MyGame/MonsterExtra.java b/tests/MyGame/MonsterExtra.java index fecfaddefd9..330d61ff7bb 100644 --- a/tests/MyGame/MonsterExtra.java +++ b/tests/MyGame/MonsterExtra.java @@ -11,6 +11,7 @@ public final class MonsterExtra extends Table { public static MonsterExtra getRootAsMonsterExtra(ByteBuffer _bb) { return getRootAsMonsterExtra(_bb, new MonsterExtra()); } public static MonsterExtra getRootAsMonsterExtra(ByteBuffer _bb, MonsterExtra obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public static boolean MonsterExtraBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONE"); } public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } public MonsterExtra __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } @@ -26,6 +27,16 @@ public final class MonsterExtra extends Table { public boolean mutateTestdPinf(double testd_pinf) { int o = __offset(12); if (o != 0) { bb.putDouble(o + bb_pos, testd_pinf); return true; } else { return false; } } public double testdNinf() { int o = __offset(14); return o != 0 ? bb.getDouble(o + bb_pos) : Double.NEGATIVE_INFINITY; } public boolean mutateTestdNinf(double testd_ninf) { int o = __offset(14); if (o != 0) { bb.putDouble(o + bb_pos, testd_ninf); return true; } else { return false; } } + public float testfVec(int j) { int o = __offset(16); return o != 0 ? bb.getFloat(__vector(o) + j * 4) : 0; } + public int testfVecLength() { int o = __offset(16); return o != 0 ? __vector_len(o) : 0; } + public ByteBuffer testfVecAsByteBuffer() { return __vector_as_bytebuffer(16, 4); } + public ByteBuffer testfVecInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 16, 4); } + public boolean mutateTestfVec(int j, float testf_vec) { int o = __offset(16); if (o != 0) { bb.putFloat(__vector(o) + j * 4, testf_vec); return true; } else { return false; } } + public double testdVec(int j) { int o = __offset(18); return o != 0 ? bb.getDouble(__vector(o) + j * 8) : 0; } + public int testdVecLength() { int o = __offset(18); return o != 0 ? __vector_len(o) : 0; } + public ByteBuffer testdVecAsByteBuffer() { return __vector_as_bytebuffer(18, 8); } + public ByteBuffer testdVecInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 18, 8); } + public boolean mutateTestdVec(int j, double testd_vec) { int o = __offset(18); if (o != 0) { bb.putDouble(__vector(o) + j * 8, testd_vec); return true; } else { return false; } } public static int createMonsterExtra(FlatBufferBuilder builder, float testf_nan, @@ -33,27 +44,39 @@ public static int createMonsterExtra(FlatBufferBuilder builder, float testf_ninf, double testd_nan, double testd_pinf, - double testd_ninf) { - builder.startObject(6); + double testd_ninf, + int testf_vecOffset, + int testd_vecOffset) { + builder.startObject(8); MonsterExtra.addTestdNinf(builder, testd_ninf); MonsterExtra.addTestdPinf(builder, testd_pinf); MonsterExtra.addTestdNan(builder, testd_nan); + MonsterExtra.addTestdVec(builder, testd_vecOffset); + MonsterExtra.addTestfVec(builder, testf_vecOffset); MonsterExtra.addTestfNinf(builder, testf_ninf); MonsterExtra.addTestfPinf(builder, testf_pinf); MonsterExtra.addTestfNan(builder, testf_nan); return MonsterExtra.endMonsterExtra(builder); } - public static void startMonsterExtra(FlatBufferBuilder builder) { builder.startObject(6); } + public static void startMonsterExtra(FlatBufferBuilder builder) { builder.startObject(8); } public static void addTestfNan(FlatBufferBuilder builder, float testfNan) { builder.addFloat(0, testfNan, Float.NaN); } public static void addTestfPinf(FlatBufferBuilder builder, float testfPinf) { builder.addFloat(1, testfPinf, Float.POSITIVE_INFINITY); } public static void addTestfNinf(FlatBufferBuilder builder, float testfNinf) { builder.addFloat(2, testfNinf, Float.NEGATIVE_INFINITY); } public static void addTestdNan(FlatBufferBuilder builder, double testdNan) { builder.addDouble(3, testdNan, Double.NaN); } public static void addTestdPinf(FlatBufferBuilder builder, double testdPinf) { builder.addDouble(4, testdPinf, Double.POSITIVE_INFINITY); } public static void addTestdNinf(FlatBufferBuilder builder, double testdNinf) { builder.addDouble(5, testdNinf, Double.NEGATIVE_INFINITY); } + public static void addTestfVec(FlatBufferBuilder builder, int testfVecOffset) { builder.addOffset(6, testfVecOffset, 0); } + public static int createTestfVecVector(FlatBufferBuilder builder, float[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addFloat(data[i]); return builder.endVector(); } + public static void startTestfVecVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } + public static void addTestdVec(FlatBufferBuilder builder, int testdVecOffset) { builder.addOffset(7, testdVecOffset, 0); } + public static int createTestdVecVector(FlatBufferBuilder builder, double[] data) { builder.startVector(8, data.length, 8); for (int i = data.length - 1; i >= 0; i--) builder.addDouble(data[i]); return builder.endVector(); } + public static void startTestdVecVector(FlatBufferBuilder builder, int numElems) { builder.startVector(8, numElems, 8); } public static int endMonsterExtra(FlatBufferBuilder builder) { int o = builder.endObject(); return o; } + public static void finishMonsterExtraBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "MONE"); } + public static void finishSizePrefixedMonsterExtraBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset, "MONE"); } } diff --git a/tests/MyGame/MonsterExtra.py b/tests/MyGame/MonsterExtra.py index 3e8b26d3098..768fb5744a2 100644 --- a/tests/MyGame/MonsterExtra.py +++ b/tests/MyGame/MonsterExtra.py @@ -60,11 +60,59 @@ def TestdNinf(self): return self._tab.Get(flatbuffers.number_types.Float64Flags, o + self._tab.Pos) return float('-inf') -def MonsterExtraStart(builder): builder.StartObject(6) + # MonsterExtra + def TestfVec(self, j): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16)) + if o != 0: + a = self._tab.Vector(o) + return self._tab.Get(flatbuffers.number_types.Float32Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 4)) + return 0 + + # MonsterExtra + def TestfVecAsNumpy(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16)) + if o != 0: + return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Float32Flags, o) + return 0 + + # MonsterExtra + def TestfVecLength(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(16)) + if o != 0: + return self._tab.VectorLen(o) + return 0 + + # MonsterExtra + def TestdVec(self, j): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(18)) + if o != 0: + a = self._tab.Vector(o) + return self._tab.Get(flatbuffers.number_types.Float64Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 8)) + return 0 + + # MonsterExtra + def TestdVecAsNumpy(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(18)) + if o != 0: + return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Float64Flags, o) + return 0 + + # MonsterExtra + def TestdVecLength(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(18)) + if o != 0: + return self._tab.VectorLen(o) + return 0 + +def MonsterExtraStart(builder): builder.StartObject(8) def MonsterExtraAddTestfNan(builder, testfNan): builder.PrependFloat32Slot(0, testfNan, float('nan')) def MonsterExtraAddTestfPinf(builder, testfPinf): builder.PrependFloat32Slot(1, testfPinf, float('inf')) def MonsterExtraAddTestfNinf(builder, testfNinf): builder.PrependFloat32Slot(2, testfNinf, float('-inf')) def MonsterExtraAddTestdNan(builder, testdNan): builder.PrependFloat64Slot(3, testdNan, float('nan')) def MonsterExtraAddTestdPinf(builder, testdPinf): builder.PrependFloat64Slot(4, testdPinf, float('inf')) def MonsterExtraAddTestdNinf(builder, testdNinf): builder.PrependFloat64Slot(5, testdNinf, float('-inf')) +def MonsterExtraAddTestfVec(builder, testfVec): builder.PrependUOffsetTRelativeSlot(6, flatbuffers.number_types.UOffsetTFlags.py_type(testfVec), 0) +def MonsterExtraStartTestfVecVector(builder, numElems): return builder.StartVector(4, numElems, 4) +def MonsterExtraAddTestdVec(builder, testdVec): builder.PrependUOffsetTRelativeSlot(7, flatbuffers.number_types.UOffsetTFlags.py_type(testdVec), 0) +def MonsterExtraStartTestdVecVector(builder, numElems): return builder.StartVector(8, numElems, 8) def MonsterExtraEnd(builder): return builder.EndObject() diff --git a/tests/generate_code.bat b/tests/generate_code.bat index fd335512cc0..0ddcddddc34 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -23,7 +23,7 @@ if "%1"=="-b" set buildtype=%2 IF NOT "%MONSTER_EXTRA%"=="skip" ( @echo Generate MosterExtra - ..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs || goto FAIL + ..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs monsterdata_extra.json || goto FAIL ) else ( @echo monster_extra.fbs skipped (the strtod function from MSVC2013 or older doesn't support NaN/Inf arguments) ) diff --git a/tests/generate_code.sh b/tests/generate_code.sh index 40373a34e0a..07aa72001e3 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -20,7 +20,7 @@ set -e ../flatc --cpp --java --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs ../flatc --jsonschema --schema -I include_test monster_test.fbs -../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs || goto FAIL +../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs monsterdata_extra.json || goto FAIL cd ../samples ../flatc --cpp --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs diff --git a/tests/monster_extra.fbs b/tests/monster_extra.fbs index 89f1cc8d0ac..60a9ab99c78 100644 --- a/tests/monster_extra.fbs +++ b/tests/monster_extra.fbs @@ -1,6 +1,6 @@ namespace MyGame; -// Not all programmining languages support this extra table. +// Not all programming languages support this extra table. table MonsterExtra { // Float-point values with NaN and Inf defaults. testf_nan:float = nan; @@ -9,4 +9,11 @@ table MonsterExtra { testd_nan:double = nan; testd_pinf:double = +inf; testd_ninf:double = -inf; + testf_vec : [float]; + testd_vec : [double]; } + +root_type MonsterExtra; + +file_identifier "MONE"; +file_extension "mon"; diff --git a/tests/monster_extra_generated.h b/tests/monster_extra_generated.h index 8599b8112f1..87e6d42814e 100644 --- a/tests/monster_extra_generated.h +++ b/tests/monster_extra_generated.h @@ -24,6 +24,8 @@ struct MonsterExtraT : public flatbuffers::NativeTable { double testd_nan; double testd_pinf; double testd_ninf; + std::vector testf_vec; + std::vector testd_vec; MonsterExtraT() : testf_nan(std::numeric_limits::quiet_NaN()), testf_pinf(std::numeric_limits::infinity()), @@ -41,7 +43,9 @@ inline bool operator==(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { (lhs.testf_ninf == rhs.testf_ninf) && (lhs.testd_nan == rhs.testd_nan) && (lhs.testd_pinf == rhs.testd_pinf) && - (lhs.testd_ninf == rhs.testd_ninf); + (lhs.testd_ninf == rhs.testd_ninf) && + (lhs.testf_vec == rhs.testf_vec) && + (lhs.testd_vec == rhs.testd_vec); } inline bool operator!=(const MonsterExtraT &lhs, const MonsterExtraT &rhs) { @@ -60,7 +64,9 @@ struct MonsterExtra FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_TESTF_NINF = 8, VT_TESTD_NAN = 10, VT_TESTD_PINF = 12, - VT_TESTD_NINF = 14 + VT_TESTD_NINF = 14, + VT_TESTF_VEC = 16, + VT_TESTD_VEC = 18 }; float testf_nan() const { return GetField(VT_TESTF_NAN, std::numeric_limits::quiet_NaN()); @@ -98,6 +104,18 @@ struct MonsterExtra FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_testd_ninf(double _testd_ninf) { return SetField(VT_TESTD_NINF, _testd_ninf, -std::numeric_limits::infinity()); } + const flatbuffers::Vector *testf_vec() const { + return GetPointer *>(VT_TESTF_VEC); + } + flatbuffers::Vector *mutable_testf_vec() { + return GetPointer *>(VT_TESTF_VEC); + } + const flatbuffers::Vector *testd_vec() const { + return GetPointer *>(VT_TESTD_VEC); + } + flatbuffers::Vector *mutable_testd_vec() { + return GetPointer *>(VT_TESTD_VEC); + } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_TESTF_NAN) && @@ -106,6 +124,10 @@ struct MonsterExtra FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_TESTD_NAN) && VerifyField(verifier, VT_TESTD_PINF) && VerifyField(verifier, VT_TESTD_NINF) && + VerifyOffset(verifier, VT_TESTF_VEC) && + verifier.VerifyVector(testf_vec()) && + VerifyOffset(verifier, VT_TESTD_VEC) && + verifier.VerifyVector(testd_vec()) && verifier.EndTable(); } MonsterExtraT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; @@ -134,6 +156,12 @@ struct MonsterExtraBuilder { void add_testd_ninf(double testd_ninf) { fbb_.AddElement(MonsterExtra::VT_TESTD_NINF, testd_ninf, -std::numeric_limits::infinity()); } + void add_testf_vec(flatbuffers::Offset> testf_vec) { + fbb_.AddOffset(MonsterExtra::VT_TESTF_VEC, testf_vec); + } + void add_testd_vec(flatbuffers::Offset> testd_vec) { + fbb_.AddOffset(MonsterExtra::VT_TESTD_VEC, testd_vec); + } explicit MonsterExtraBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); @@ -153,17 +181,45 @@ inline flatbuffers::Offset CreateMonsterExtra( float testf_ninf = -std::numeric_limits::infinity(), double testd_nan = std::numeric_limits::quiet_NaN(), double testd_pinf = std::numeric_limits::infinity(), - double testd_ninf = -std::numeric_limits::infinity()) { + double testd_ninf = -std::numeric_limits::infinity(), + flatbuffers::Offset> testf_vec = 0, + flatbuffers::Offset> testd_vec = 0) { MonsterExtraBuilder builder_(_fbb); builder_.add_testd_ninf(testd_ninf); builder_.add_testd_pinf(testd_pinf); builder_.add_testd_nan(testd_nan); + builder_.add_testd_vec(testd_vec); + builder_.add_testf_vec(testf_vec); builder_.add_testf_ninf(testf_ninf); builder_.add_testf_pinf(testf_pinf); builder_.add_testf_nan(testf_nan); return builder_.Finish(); } +inline flatbuffers::Offset CreateMonsterExtraDirect( + flatbuffers::FlatBufferBuilder &_fbb, + float testf_nan = std::numeric_limits::quiet_NaN(), + float testf_pinf = std::numeric_limits::infinity(), + float testf_ninf = -std::numeric_limits::infinity(), + double testd_nan = std::numeric_limits::quiet_NaN(), + double testd_pinf = std::numeric_limits::infinity(), + double testd_ninf = -std::numeric_limits::infinity(), + const std::vector *testf_vec = nullptr, + const std::vector *testd_vec = nullptr) { + auto testf_vec__ = testf_vec ? _fbb.CreateVector(*testf_vec) : 0; + auto testd_vec__ = testd_vec ? _fbb.CreateVector(*testd_vec) : 0; + return MyGame::CreateMonsterExtra( + _fbb, + testf_nan, + testf_pinf, + testf_ninf, + testd_nan, + testd_pinf, + testd_ninf, + testf_vec__, + testd_vec__); +} + flatbuffers::Offset CreateMonsterExtra(flatbuffers::FlatBufferBuilder &_fbb, const MonsterExtraT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); inline MonsterExtraT *MonsterExtra::UnPack(const flatbuffers::resolver_function_t *_resolver) const { @@ -181,6 +237,8 @@ inline void MonsterExtra::UnPackTo(MonsterExtraT *_o, const flatbuffers::resolve { auto _e = testd_nan(); _o->testd_nan = _e; }; { auto _e = testd_pinf(); _o->testd_pinf = _e; }; { auto _e = testd_ninf(); _o->testd_ninf = _e; }; + { auto _e = testf_vec(); if (_e) { _o->testf_vec.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testf_vec[_i] = _e->Get(_i); } } }; + { auto _e = testd_vec(); if (_e) { _o->testd_vec.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testd_vec[_i] = _e->Get(_i); } } }; } inline flatbuffers::Offset MonsterExtra::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterExtraT* _o, const flatbuffers::rehasher_function_t *_rehasher) { @@ -197,6 +255,8 @@ inline flatbuffers::Offset CreateMonsterExtra(flatbuffers::FlatBuf auto _testd_nan = _o->testd_nan; auto _testd_pinf = _o->testd_pinf; auto _testd_ninf = _o->testd_ninf; + auto _testf_vec = _o->testf_vec.size() ? _fbb.CreateVector(_o->testf_vec) : 0; + auto _testd_vec = _o->testd_vec.size() ? _fbb.CreateVector(_o->testd_vec) : 0; return MyGame::CreateMonsterExtra( _fbb, _testf_nan, @@ -204,7 +264,9 @@ inline flatbuffers::Offset CreateMonsterExtra(flatbuffers::FlatBuf _testf_ninf, _testd_nan, _testd_pinf, - _testd_ninf); + _testd_ninf, + _testf_vec, + _testd_vec); } inline const flatbuffers::TypeTable *MonsterExtraTypeTable() { @@ -214,7 +276,9 @@ inline const flatbuffers::TypeTable *MonsterExtraTypeTable() { { flatbuffers::ET_FLOAT, 0, -1 }, { flatbuffers::ET_DOUBLE, 0, -1 }, { flatbuffers::ET_DOUBLE, 0, -1 }, - { flatbuffers::ET_DOUBLE, 0, -1 } + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_FLOAT, 1, -1 }, + { flatbuffers::ET_DOUBLE, 1, -1 } }; static const char * const names[] = { "testf_nan", @@ -222,14 +286,69 @@ inline const flatbuffers::TypeTable *MonsterExtraTypeTable() { "testf_ninf", "testd_nan", "testd_pinf", - "testd_ninf" + "testd_ninf", + "testf_vec", + "testd_vec" }; static const flatbuffers::TypeTable tt = { - flatbuffers::ST_TABLE, 6, type_codes, nullptr, nullptr, names + flatbuffers::ST_TABLE, 8, type_codes, nullptr, nullptr, names }; return &tt; } +inline const MyGame::MonsterExtra *GetMonsterExtra(const void *buf) { + return flatbuffers::GetRoot(buf); +} + +inline const MyGame::MonsterExtra *GetSizePrefixedMonsterExtra(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + +inline MonsterExtra *GetMutableMonsterExtra(void *buf) { + return flatbuffers::GetMutableRoot(buf); +} + +inline const char *MonsterExtraIdentifier() { + return "MONE"; +} + +inline bool MonsterExtraBufferHasIdentifier(const void *buf) { + return flatbuffers::BufferHasIdentifier( + buf, MonsterExtraIdentifier()); +} + +inline bool VerifyMonsterExtraBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifyBuffer(MonsterExtraIdentifier()); +} + +inline bool VerifySizePrefixedMonsterExtraBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(MonsterExtraIdentifier()); +} + +inline const char *MonsterExtraExtension() { + return "mon"; +} + +inline void FinishMonsterExtraBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.Finish(root, MonsterExtraIdentifier()); +} + +inline void FinishSizePrefixedMonsterExtraBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root, MonsterExtraIdentifier()); +} + +inline std::unique_ptr UnPackMonsterExtra( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetMonsterExtra(buf)->UnPack(res)); +} + } // namespace MyGame #endif // FLATBUFFERS_GENERATED_MONSTEREXTRA_MYGAME_H_ diff --git a/tests/monsterdata_extra.json b/tests/monsterdata_extra.json new file mode 100644 index 00000000000..e290d478ccd --- /dev/null +++ b/tests/monsterdata_extra.json @@ -0,0 +1,11 @@ +{ + // Float-point values with NaN and Inf defaults. + testf_nan : nan, + testf_pinf : +inf, + testf_ninf : -inf, + testd_nan : nan, + testd_pinf : +inf, + testd_ninf : -inf, + testf_vec : [-1.0, 2.0, -inf, +inf, nan], + testd_vec : [-1.0, 4.0, -inf, +inf, nan] +} diff --git a/tests/test.cpp b/tests/test.cpp index c8d4577d119..4bf042047ee 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -590,6 +590,41 @@ void JsonDefaultTest() { TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true); } +#if defined(FLATBUFFERS_HAS_NEW_STRTOD) +void TestMonsterExtraFloats() { + using namespace MyGame; + // Load FlatBuffer schema (.fbs) from disk. + std::string schemafile; + TEST_EQ(flatbuffers::LoadFile((test_data_path + "monster_extra.fbs").c_str(), + false, &schemafile), + true); + // Parse schema first, so we can use it to parse the data after. + flatbuffers::Parser parser; + auto include_test_path = + flatbuffers::ConCatPathFileName(test_data_path, "include_test"); + const char *include_directories[] = { test_data_path.c_str(), + include_test_path.c_str(), nullptr }; + TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true); + // Create empty extra and store to json. + parser.opts.output_default_scalars_in_json = true; + parser.opts.output_enum_identifiers = true; + flatbuffers::FlatBufferBuilder builder; + MonsterExtraBuilder extra(builder); + FinishMonsterExtraBuffer(builder, extra.Finish()); + std::string jsongen; + auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen); + TEST_EQ(result, true); + TEST_EQ(std::string::npos != jsongen.find("testf_nan: nan"), true); + TEST_EQ(std::string::npos != jsongen.find("testf_pinf: inf"), true); + TEST_EQ(std::string::npos != jsongen.find("testf_ninf: -inf"), true); + TEST_EQ(std::string::npos != jsongen.find("testd_nan: nan"), true); + TEST_EQ(std::string::npos != jsongen.find("testd_pinf: inf"), true); + TEST_EQ(std::string::npos != jsongen.find("testd_ninf: -inf"), true); +} +#else +void TestMonsterExtraFloats() {} +#endif + // example of parsing text straight into a buffer, and generating // text back from it: void ParseAndGenerateTextTest(bool binary) { @@ -2703,6 +2738,7 @@ int FlatBufferTests() { IsAsciiUtilsTest(); ValidFloatTest(); InvalidFloatTest(); + TestMonsterExtraFloats(); return 0; }