Skip to content

Commit

Permalink
Make MonsterExtra table a root table (google#5315)
Browse files Browse the repository at this point in the history
- MonsterExtra table a root table
- add mosterdata_extra.json
  • Loading branch information
vglavnyy authored and aardappel committed May 2, 2019
1 parent b8ef8c1 commit af74f87
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 17 deletions.
39 changes: 36 additions & 3 deletions tests/MyGame/MonsterExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -29,35 +30,67 @@ 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<byte> GetTestfVecBytes() { return __p.__vector_as_span(16); }
#else
public ArraySegment<byte>? GetTestfVecBytes() { return __p.__vector_as_arraysegment(16); }
#endif
public float[] GetTestfVecArray() { return __p.__vector_as_array<float>(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<byte> GetTestdVecBytes() { return __p.__vector_as_span(18); }
#else
public ArraySegment<byte>? GetTestdVecBytes() { return __p.__vector_as_arraysegment(18); }
#endif
public double[] GetTestdVecArray() { return __p.__vector_as_array<double>(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<MonsterExtra> CreateMonsterExtra(FlatBufferBuilder builder,
float testf_nan = Single.NaN,
float testf_pinf = Single.PositiveInfinity,
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<MonsterExtra> EndMonsterExtra(FlatBufferBuilder builder) {
int o = builder.EndObject();
return new Offset<MonsterExtra>(o);
}
public static void FinishMonsterExtraBuffer(FlatBufferBuilder builder, Offset<MonsterExtra> offset) { builder.Finish(offset.Value, "MONE"); }
public static void FinishSizePrefixedMonsterExtraBuffer(FlatBufferBuilder builder, Offset<MonsterExtra> offset) { builder.FinishSizePrefixed(offset.Value, "MONE"); }
};


Expand Down
29 changes: 26 additions & 3 deletions tests/MyGame/MonsterExtra.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -26,34 +27,56 @@ 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,
float testf_pinf,
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"); }
}

50 changes: 49 additions & 1 deletion tests/MyGame/MonsterExtra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion tests/generate_code.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/generate_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion tests/monster_extra.fbs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Loading

0 comments on commit af74f87

Please sign in to comment.