Add support for fixed-size arrays (#5313)

This commit is contained in:
svenk177
2019-06-18 00:15:13 +02:00
committed by Wouter van Oortmerssen
parent 0d2cebccfe
commit e635141d5b
40 changed files with 2113 additions and 220 deletions

View File

@@ -99,6 +99,18 @@
<Compile Include="..\MyGame\Example\Ability.cs">
<Link>MyGame\Example\Ability.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\ArrayTable.cs">
<Link>MyGame\Example\ArrayTable.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\ArrayStruct.cs">
<Link>MyGame\Example\ArrayStruct.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\NestedStruct.cs">
<Link>MyGame\Example\NestedStruct.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\TestEnum.cs">
<Link>MyGame\Example\TestEnum.cs</Link>
</Compile>
<Compile Include="..\MyGame\InParentNamespace.cs">
<Link>MyGame\InParentNamespace.cs</Link>
</Compile>

View File

@@ -330,5 +330,58 @@ namespace FlatBuffers.Test
Assert.AreEqual(nestedMonsterHp, nestedMonster.Hp);
Assert.AreEqual(nestedMonsterName, nestedMonster.Name);
}
[FlatBuffersTestMethod]
public void TestFixedLenghtArrays()
{
FlatBufferBuilder builder = new FlatBufferBuilder(100);
float a;
int[] b = new int[15];
sbyte c;
int[,] d_a = new int[2, 2];
TestEnum[] d_b = new TestEnum[2];
TestEnum[,] d_c = new TestEnum[2, 2];
a = 0.5f;
for (int i = 0; i < 15; i++) b[i] = i;
c = 1;
d_a[0, 0] = 1;
d_a[0, 1] = 2;
d_a[1, 0] = 3;
d_a[1, 1] = 4;
d_b[0] = TestEnum.B;
d_b[1] = TestEnum.C;
d_c[0, 0] = TestEnum.A;
d_c[0, 1] = TestEnum.B;
d_c[1, 0] = TestEnum.C;
d_c[1, 1] = TestEnum.B;
Offset<ArrayStruct> arrayOffset = ArrayStruct.CreateArrayStruct(
builder, a, b, c, d_a, d_b, d_c);
// Create a table with the ArrayStruct.
ArrayTable.StartArrayTable(builder);
ArrayTable.AddA(builder, arrayOffset);
Offset<ArrayTable> tableOffset = ArrayTable.EndArrayTable(builder);
ArrayTable.FinishArrayTableBuffer(builder, tableOffset);
ArrayTable table = ArrayTable.GetRootAsArrayTable(builder.DataBuffer);
Assert.AreEqual(table.A?.A, 0.5f);
for (int i = 0; i < 15; i++) Assert.AreEqual(table.A?.B(i), i);
Assert.AreEqual(table.A?.C, (sbyte)1);
Assert.AreEqual(table.A?.D(0).A(0), 1);
Assert.AreEqual(table.A?.D(0).A(1), 2);
Assert.AreEqual(table.A?.D(1).A(0), 3);
Assert.AreEqual(table.A?.D(1).A(1), 4);
Assert.AreEqual(table.A?.D(0).B, TestEnum.B);
Assert.AreEqual(table.A?.D(1).B, TestEnum.C);
Assert.AreEqual(table.A?.D(0).C(0), TestEnum.A);
Assert.AreEqual(table.A?.D(0).C(1), TestEnum.B);
Assert.AreEqual(table.A?.D(1).C(0), TestEnum.C);
Assert.AreEqual(table.A?.D(1).C(1), TestEnum.B);
}
}
}

View File

@@ -75,6 +75,8 @@ class JavaTest {
TestVectorOfUnions();
TestFixedLengthArrays();
System.out.println("FlatBuffers test: completed successfully");
}
@@ -452,6 +454,58 @@ class JavaTest {
TestEq(((Attacker)movie.characters(new Attacker(), 0)).swordAttackDamage(), swordAttackDamage);
}
static void TestFixedLengthArrays() {
FlatBufferBuilder builder = new FlatBufferBuilder(0);
float a;
int[] b = new int[15];
byte c;
int[][] d_a = new int[2][2];
byte[] d_b = new byte[2];
byte[][] d_c = new byte[2][2];
a = 0.5f;
for (int i = 0; i < 15; i++) b[i] = i;
c = 1;
d_a[0][0] = 1;
d_a[0][1] = 2;
d_a[1][0] = 3;
d_a[1][1] = 4;
d_b[0] = TestEnum.B;
d_b[1] = TestEnum.C;
d_c[0][0] = TestEnum.A;
d_c[0][1] = TestEnum.B;
d_c[1][0] = TestEnum.C;
d_c[1][1] = TestEnum.B;
int arrayOffset = ArrayStruct.createArrayStruct(builder,
a, b, c, d_a, d_b, d_c);
// Create a table with the ArrayStruct.
ArrayTable.startArrayTable(builder);
ArrayTable.addA(builder, arrayOffset);
int tableOffset = ArrayTable.endArrayTable(builder);
ArrayTable.finishArrayTableBuffer(builder, tableOffset);
ArrayTable table = ArrayTable.getRootAsArrayTable(builder.dataBuffer());
NestedStruct nested = new NestedStruct();
TestEq(table.a().a(), 0.5f);
for (int i = 0; i < 15; i++) TestEq(table.a().b(i), i);
TestEq(table.a().c(), (byte)1);
TestEq(table.a().d(nested, 0).a(0), 1);
TestEq(table.a().d(nested, 0).a(1), 2);
TestEq(table.a().d(nested, 1).a(0), 3);
TestEq(table.a().d(nested, 1).a(1), 4);
TestEq(table.a().d(nested, 0).b(), TestEnum.B);
TestEq(table.a().d(nested, 1).b(), TestEnum.C);
TestEq(table.a().d(nested, 0).c(0), TestEnum.A);
TestEq(table.a().d(nested, 0).c(1), TestEnum.B);
TestEq(table.a().d(nested, 1).c(0), TestEnum.C);
TestEq(table.a().d(nested, 1).c(1), TestEnum.B);
}
static <T> void TestEq(T a, T b) {
if (!a.equals(b)) {
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());

View File

@@ -0,0 +1,50 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::FlatBuffers;
public struct ArrayStruct : IFlatbufferObject
{
private Struct __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public ArrayStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public float A { get { return __p.bb.GetFloat(__p.bb_pos + 0); } }
public void MutateA(float a) { __p.bb.PutFloat(__p.bb_pos + 0, a); }
public int B(int j) { return __p.bb.GetInt(__p.bb_pos + 4 + j * 4); }
public void MutateB(int j, int b) { __p.bb.PutInt(__p.bb_pos + 4 + j * 4, b); }
public sbyte C { get { return __p.bb.GetSbyte(__p.bb_pos + 64); } }
public void MutateC(sbyte c) { __p.bb.PutSbyte(__p.bb_pos + 64, c); }
public MyGame.Example.NestedStruct D(int j) { return (new MyGame.Example.NestedStruct()).__assign(__p.bb_pos + 68 + j * 12, __p.bb); }
public static Offset<MyGame.Example.ArrayStruct> CreateArrayStruct(FlatBufferBuilder builder, float A, int[] B, sbyte C, int[,] d_A, MyGame.Example.TestEnum[] d_B, MyGame.Example.TestEnum[,] d_C) {
builder.Prep(4, 92);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.Prep(4, 12);
builder.Pad(1);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.PutSbyte((sbyte)d_C[_idx0-1,_idx1-1]);
}
builder.PutSbyte((sbyte)d_B[_idx0-1]);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.PutInt(d_A[_idx0-1,_idx1-1]);
}
}
builder.Pad(3);
builder.PutSbyte(C);
for (int _idx0 = 15; _idx0 > 0; _idx0--) {
builder.PutInt(B[_idx0-1]);
}
builder.PutFloat(A);
return new Offset<MyGame.Example.ArrayStruct>(builder.Offset);
}
};
}

View File

@@ -0,0 +1,45 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class ArrayStruct extends Struct {
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
public ArrayStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public float a() { return bb.getFloat(bb_pos + 0); }
public void mutateA(float a) { bb.putFloat(bb_pos + 0, a); }
public int b(int j) { return bb.getInt(bb_pos + 4 + j * 4); }
public void mutateB(int j, int b) { bb.putInt(bb_pos + 4 + j * 4, b); }
public byte c() { return bb.get(bb_pos + 64); }
public void mutateC(byte c) { bb.put(bb_pos + 64, c); }
public MyGame.Example.NestedStruct d(MyGame.Example.NestedStruct obj, int j) { return obj.__assign(bb_pos + 68 + j * 12, bb); }
public static int createArrayStruct(FlatBufferBuilder builder, float a, int[] b, byte c, int[][] d_a, byte[] d_b, byte[][] d_c) {
builder.prep(4, 92);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.prep(4, 12);
builder.pad(1);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.putByte(d_c[_idx0-1][_idx1-1]);
}
builder.putByte(d_b[_idx0-1]);
for (int _idx1 = 2; _idx1 > 0; _idx1--) {
builder.putInt(d_a[_idx0-1][_idx1-1]);
}
}
builder.pad(3);
builder.putByte(c);
for (int _idx0 = 15; _idx0 > 0; _idx0--) {
builder.putInt(b[_idx0-1]);
}
builder.putFloat(a);
return builder.offset();
}
}

View File

@@ -0,0 +1,41 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
class ArrayStruct(object):
__slots__ = ['_tab']
# ArrayStruct
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# ArrayStruct
def A(self): return self._tab.Get(flatbuffers.number_types.Float32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(0))
# ArrayStruct
def B(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(4 + i * 4)) for i in range(15)]
# ArrayStruct
def C(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(64))
# ArrayStruct
def D(self, obj, i):
obj.Init(self._tab.Bytes, self._tab.Pos + 68 + i * 12)
return obj
def CreateArrayStruct(builder, a, b, c, d_a, d_b, d_c):
builder.Prep(4, 92)
for _idx0 in range(2 , 0, -1):
builder.Prep(4, 12)
builder.Pad(1)
for _idx1 in range(2 , 0, -1):
builder.PrependInt8(d_c[_idx0-1][_idx1-1])
builder.PrependInt8(d_b[_idx0-1])
for _idx1 in range(2 , 0, -1):
builder.PrependInt32(d_a[_idx0-1][_idx1-1])
builder.Pad(3)
builder.PrependInt8(c)
for _idx0 in range(15 , 0, -1):
builder.PrependInt32(b[_idx0-1])
builder.PrependFloat32(a)
return builder.Offset()

View File

@@ -0,0 +1,34 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::FlatBuffers;
public struct ArrayTable : IFlatbufferObject
{
private Table __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public static ArrayTable GetRootAsArrayTable(ByteBuffer _bb) { return GetRootAsArrayTable(_bb, new ArrayTable()); }
public static ArrayTable GetRootAsArrayTable(ByteBuffer _bb, ArrayTable obj) { FlatBufferConstants.FLATBUFFERS_1_11_1(); return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
public static bool ArrayTableBufferHasIdentifier(ByteBuffer _bb) { return Table.__has_identifier(_bb, "ARRT"); }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public ArrayTable __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.ArrayStruct? A { get { int o = __p.__offset(4); return o != 0 ? (MyGame.Example.ArrayStruct?)(new MyGame.Example.ArrayStruct()).__assign(o + __p.bb_pos, __p.bb) : null; } }
public static void StartArrayTable(FlatBufferBuilder builder) { builder.StartTable(1); }
public static void AddA(FlatBufferBuilder builder, Offset<MyGame.Example.ArrayStruct> aOffset) { builder.AddStruct(0, aOffset.Value, 0); }
public static Offset<MyGame.Example.ArrayTable> EndArrayTable(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<MyGame.Example.ArrayTable>(o);
}
public static void FinishArrayTableBuffer(FlatBufferBuilder builder, Offset<MyGame.Example.ArrayTable> offset) { builder.Finish(offset.Value, "ARRT"); }
public static void FinishSizePrefixedArrayTableBuffer(FlatBufferBuilder builder, Offset<MyGame.Example.ArrayTable> offset) { builder.FinishSizePrefixed(offset.Value, "ARRT"); }
};
}

View File

@@ -0,0 +1,30 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class ArrayTable extends Table {
public static ArrayTable getRootAsArrayTable(ByteBuffer _bb) { return getRootAsArrayTable(_bb, new ArrayTable()); }
public static ArrayTable getRootAsArrayTable(ByteBuffer _bb, ArrayTable obj) { Constants.FLATBUFFERS_1_11_1(); _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public static boolean ArrayTableBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "ARRT"); }
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 ArrayTable __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Example.ArrayStruct a() { return a(new MyGame.Example.ArrayStruct()); }
public MyGame.Example.ArrayStruct a(MyGame.Example.ArrayStruct obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
public static void startArrayTable(FlatBufferBuilder builder) { builder.startTable(1); }
public static void addA(FlatBufferBuilder builder, int aOffset) { builder.addStruct(0, aOffset, 0); }
public static int endArrayTable(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static void finishArrayTableBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset, "ARRT"); }
public static void finishSizePrefixedArrayTableBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset, "ARRT"); }
}

View File

@@ -0,0 +1,34 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
class ArrayTable(object):
__slots__ = ['_tab']
@classmethod
def GetRootAsArrayTable(cls, buf, offset):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = ArrayTable()
x.Init(buf, n + offset)
return x
# ArrayTable
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# ArrayTable
def A(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
x = o + self._tab.Pos
from .ArrayStruct import ArrayStruct
obj = ArrayStruct()
obj.Init(self._tab.Bytes, x)
return obj
return None
def ArrayTableStart(builder): builder.StartObject(1)
def ArrayTableAddA(builder, a): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(a), 0)
def ArrayTableEnd(builder): return builder.EndObject()

View File

@@ -0,0 +1,40 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
using global::System;
using global::FlatBuffers;
public struct NestedStruct : IFlatbufferObject
{
private Struct __p;
public ByteBuffer ByteBuffer { get { return __p.bb; } }
public void __init(int _i, ByteBuffer _bb) { __p.bb_pos = _i; __p.bb = _bb; }
public NestedStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public int A(int j) { return __p.bb.GetInt(__p.bb_pos + 0 + j * 4); }
public void MutateA(int j, int a) { __p.bb.PutInt(__p.bb_pos + 0 + j * 4, a); }
public MyGame.Example.TestEnum B { get { return (MyGame.Example.TestEnum)__p.bb.GetSbyte(__p.bb_pos + 8); } }
public void MutateB(MyGame.Example.TestEnum b) { __p.bb.PutSbyte(__p.bb_pos + 8, (sbyte)b); }
public MyGame.Example.TestEnum C(int j) { return (MyGame.Example.TestEnum)__p.bb.GetSbyte(__p.bb_pos + 9 + j * 1); }
public void MutateC(int j, MyGame.Example.TestEnum c) { __p.bb.PutSbyte(__p.bb_pos + 9 + j * 1, (sbyte)c); }
public static Offset<MyGame.Example.NestedStruct> CreateNestedStruct(FlatBufferBuilder builder, int[] A, MyGame.Example.TestEnum B, MyGame.Example.TestEnum[] C) {
builder.Prep(4, 12);
builder.Pad(1);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.PutSbyte((sbyte)C[_idx0-1]);
}
builder.PutSbyte((sbyte)B);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.PutInt(A[_idx0-1]);
}
return new Offset<MyGame.Example.NestedStruct>(builder.Offset);
}
};
}

View File

@@ -0,0 +1,35 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class NestedStruct extends Struct {
public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; }
public NestedStruct __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public int a(int j) { return bb.getInt(bb_pos + 0 + j * 4); }
public void mutateA(int j, int a) { bb.putInt(bb_pos + 0 + j * 4, a); }
public byte b() { return bb.get(bb_pos + 8); }
public void mutateB(byte b) { bb.put(bb_pos + 8, b); }
public byte c(int j) { return bb.get(bb_pos + 9 + j * 1); }
public void mutateC(int j, byte c) { bb.put(bb_pos + 9 + j * 1, c); }
public static int createNestedStruct(FlatBufferBuilder builder, int[] a, byte b, byte[] c) {
builder.prep(4, 12);
builder.pad(1);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.putByte(c[_idx0-1]);
}
builder.putByte(b);
for (int _idx0 = 2; _idx0 > 0; _idx0--) {
builder.putInt(a[_idx0-1]);
}
return builder.offset();
}
}

View File

@@ -0,0 +1,29 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
import flatbuffers
class NestedStruct(object):
__slots__ = ['_tab']
# NestedStruct
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# NestedStruct
def A(self): return [self._tab.Get(flatbuffers.number_types.Int32Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(0 + i * 4)) for i in range(2)]
# NestedStruct
def B(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(8))
# NestedStruct
def C(self): return [self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(9 + i * 1)) for i in range(2)]
def CreateNestedStruct(builder, a, b, c):
builder.Prep(4, 12)
builder.Pad(1)
for _idx0 in range(2 , 0, -1):
builder.PrependInt8(c[_idx0-1])
builder.PrependInt8(b)
for _idx0 in range(2 , 0, -1):
builder.PrependInt32(a[_idx0-1])
return builder.Offset()

View File

@@ -0,0 +1,16 @@
// <auto-generated>
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
namespace MyGame.Example
{
public enum TestEnum : sbyte
{
A = 0,
B = 1,
C = 2,
};
}

View File

@@ -0,0 +1,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Example;
public final class TestEnum {
private TestEnum() { }
public static final byte A = 0;
public static final byte B = 1;
public static final byte C = 2;
public static final String[] names = { "A", "B", "C", };
public static String name(int e) { return names[e]; }
}

View File

@@ -0,0 +1,9 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: Example
class TestEnum(object):
A = 0
B = 1
C = 2

BIN
tests/arrays_test.bfbs Normal file

Binary file not shown.

24
tests/arrays_test.fbs Normal file
View File

@@ -0,0 +1,24 @@
namespace MyGame.Example;
enum TestEnum : byte { A, B, C }
struct NestedStruct{
a:[int:2];
b:TestEnum;
c:[TestEnum:2];
}
struct ArrayStruct{
a:float;
b:[int:0xF];
c:byte;
d:[NestedStruct:2];
}
table ArrayTable{
a:ArrayStruct;
}
root_type ArrayTable;
file_identifier "ARRT";
file_extension "mon";

19
tests/arrays_test.golden Normal file
View File

@@ -0,0 +1,19 @@
{
a : {
a: 12.34,
b: [1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF],
c: -127,
d: [
{
a : [-1,2],
b : A,
c : [C, B]
},
{
a : [3,-4],
b : B,
c : [B, A]
}
]
}
}

View File

@@ -0,0 +1,60 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"MyGame_Example_TestEnum" : {
"type" : "string",
"enum": ["A", "B", "C"]
},
"MyGame_Example_NestedStruct" : {
"type" : "object",
"properties" : {
"a" : {
"type" : "array", "items" : { "type" : "number" },
"minItems": 2,
"maxItems": 2
},
"b" : {
"$ref" : "#/definitions/MyGame_Example_TestEnum"
},
"c" : {
"$ref" : "#/definitions/MyGame_Example_TestEnum",
"minItems": 2,
"maxItems": 2
}
},
"additionalProperties" : false
},
"MyGame_Example_ArrayStruct" : {
"type" : "object",
"properties" : {
"a" : {
"type" : "number"
},
"b" : {
"type" : "array", "items" : { "type" : "number" },
"minItems": 15,
"maxItems": 15
},
"c" : {
"type" : "number"
},
"d" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_NestedStruct" },
"minItems": 2,
"maxItems": 2
}
},
"additionalProperties" : false
},
"MyGame_Example_ArrayTable" : {
"type" : "object",
"properties" : {
"a" : {
"$ref" : "#/definitions/MyGame_Example_ArrayStruct"
}
},
"additionalProperties" : false
}
},
"$ref" : "#/definitions/MyGame_Example_ArrayTable"
}

View File

@@ -0,0 +1,419 @@
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_ARRAYSTEST_MYGAME_EXAMPLE_H_
#define FLATBUFFERS_GENERATED_ARRAYSTEST_MYGAME_EXAMPLE_H_
#include "flatbuffers/flatbuffers.h"
namespace MyGame {
namespace Example {
struct NestedStruct;
struct ArrayStruct;
struct ArrayTable;
struct ArrayTableT;
bool operator==(const NestedStruct &lhs, const NestedStruct &rhs);
bool operator!=(const NestedStruct &lhs, const NestedStruct &rhs);
bool operator==(const ArrayStruct &lhs, const ArrayStruct &rhs);
bool operator!=(const ArrayStruct &lhs, const ArrayStruct &rhs);
bool operator==(const ArrayTableT &lhs, const ArrayTableT &rhs);
bool operator!=(const ArrayTableT &lhs, const ArrayTableT &rhs);
inline const flatbuffers::TypeTable *NestedStructTypeTable();
inline const flatbuffers::TypeTable *ArrayStructTypeTable();
inline const flatbuffers::TypeTable *ArrayTableTypeTable();
enum class TestEnum : int8_t {
A = 0,
B = 1,
C = 2,
MIN = A,
MAX = C
};
inline const TestEnum (&EnumValuesTestEnum())[3] {
static const TestEnum values[] = {
TestEnum::A,
TestEnum::B,
TestEnum::C
};
return values;
}
inline const char * const *EnumNamesTestEnum() {
static const char * const names[4] = {
"A",
"B",
"C",
nullptr
};
return names;
}
inline const char *EnumNameTestEnum(TestEnum e) {
if (e < TestEnum::A || e > TestEnum::C) return "";
const size_t index = static_cast<size_t>(e);
return EnumNamesTestEnum()[index];
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) NestedStruct FLATBUFFERS_FINAL_CLASS {
private:
int32_t a_[2];
int8_t b_;
int8_t c_[2];
int8_t padding0__;
public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return NestedStructTypeTable();
}
NestedStruct() {
memset(static_cast<void *>(this), 0, sizeof(NestedStruct));
}
NestedStruct(MyGame::Example::TestEnum _b)
: b_(flatbuffers::EndianScalar(static_cast<int8_t>(_b))) {
std::memset(a_, 0, sizeof(a_));
std::memset(c_, 0, sizeof(c_));
(void)padding0__;
}
const flatbuffers::Array<int32_t, 2> *a() const {
return reinterpret_cast<const flatbuffers::Array<int32_t, 2> *>(a_);
}
flatbuffers::Array<int32_t, 2> *mutable_a() {
return reinterpret_cast<flatbuffers::Array<int32_t, 2> *>(a_);
}
MyGame::Example::TestEnum b() const {
return static_cast<MyGame::Example::TestEnum>(flatbuffers::EndianScalar(b_));
}
void mutate_b(MyGame::Example::TestEnum _b) {
flatbuffers::WriteScalar(&b_, static_cast<int8_t>(_b));
}
const flatbuffers::Array<MyGame::Example::TestEnum, 2> *c() const {
return reinterpret_cast<const flatbuffers::Array<MyGame::Example::TestEnum, 2> *>(c_);
}
flatbuffers::Array<MyGame::Example::TestEnum, 2> *mutable_c() {
return reinterpret_cast<flatbuffers::Array<MyGame::Example::TestEnum, 2> *>(c_);
}
};
FLATBUFFERS_STRUCT_END(NestedStruct, 12);
inline bool operator==(const NestedStruct &lhs, const NestedStruct &rhs) {
return
(lhs.a() == rhs.a()) &&
(lhs.b() == rhs.b()) &&
(lhs.c() == rhs.c());
}
inline bool operator!=(const NestedStruct &lhs, const NestedStruct &rhs) {
return !(lhs == rhs);
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) ArrayStruct FLATBUFFERS_FINAL_CLASS {
private:
float a_;
int32_t b_[15];
int8_t c_;
int8_t padding0__; int16_t padding1__;
MyGame::Example::NestedStruct d_[2];
public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return ArrayStructTypeTable();
}
ArrayStruct() {
memset(static_cast<void *>(this), 0, sizeof(ArrayStruct));
}
ArrayStruct(float _a, int8_t _c)
: a_(flatbuffers::EndianScalar(_a)),
c_(flatbuffers::EndianScalar(_c)),
padding0__(0),
padding1__(0) {
std::memset(b_, 0, sizeof(b_));
(void)padding0__; (void)padding1__;
std::memset(d_, 0, sizeof(d_));
}
float a() const {
return flatbuffers::EndianScalar(a_);
}
void mutate_a(float _a) {
flatbuffers::WriteScalar(&a_, _a);
}
const flatbuffers::Array<int32_t, 15> *b() const {
return reinterpret_cast<const flatbuffers::Array<int32_t, 15> *>(b_);
}
flatbuffers::Array<int32_t, 15> *mutable_b() {
return reinterpret_cast<flatbuffers::Array<int32_t, 15> *>(b_);
}
int8_t c() const {
return flatbuffers::EndianScalar(c_);
}
void mutate_c(int8_t _c) {
flatbuffers::WriteScalar(&c_, _c);
}
const flatbuffers::Array<MyGame::Example::NestedStruct, 2> *d() const {
return reinterpret_cast<const flatbuffers::Array<MyGame::Example::NestedStruct, 2> *>(d_);
}
flatbuffers::Array<MyGame::Example::NestedStruct, 2> *mutable_d() {
return reinterpret_cast<flatbuffers::Array<MyGame::Example::NestedStruct, 2> *>(d_);
}
};
FLATBUFFERS_STRUCT_END(ArrayStruct, 92);
inline bool operator==(const ArrayStruct &lhs, const ArrayStruct &rhs) {
return
(lhs.a() == rhs.a()) &&
(lhs.b() == rhs.b()) &&
(lhs.c() == rhs.c()) &&
(lhs.d() == rhs.d());
}
inline bool operator!=(const ArrayStruct &lhs, const ArrayStruct &rhs) {
return !(lhs == rhs);
}
struct ArrayTableT : public flatbuffers::NativeTable {
typedef ArrayTable TableType;
flatbuffers::unique_ptr<MyGame::Example::ArrayStruct> a;
ArrayTableT() {
}
};
inline bool operator==(const ArrayTableT &lhs, const ArrayTableT &rhs) {
return
(lhs.a == rhs.a);
}
inline bool operator!=(const ArrayTableT &lhs, const ArrayTableT &rhs) {
return !(lhs == rhs);
}
struct ArrayTable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef ArrayTableT NativeTableType;
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return ArrayTableTypeTable();
}
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_A = 4
};
const MyGame::Example::ArrayStruct *a() const {
return GetStruct<const MyGame::Example::ArrayStruct *>(VT_A);
}
MyGame::Example::ArrayStruct *mutable_a() {
return GetStruct<MyGame::Example::ArrayStruct *>(VT_A);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<MyGame::Example::ArrayStruct>(verifier, VT_A) &&
verifier.EndTable();
}
ArrayTableT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const;
void UnPackTo(ArrayTableT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const;
static flatbuffers::Offset<ArrayTable> Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArrayTableT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
};
struct ArrayTableBuilder {
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_a(const MyGame::Example::ArrayStruct *a) {
fbb_.AddStruct(ArrayTable::VT_A, a);
}
explicit ArrayTableBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
ArrayTableBuilder &operator=(const ArrayTableBuilder &);
flatbuffers::Offset<ArrayTable> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<ArrayTable>(end);
return o;
}
};
inline flatbuffers::Offset<ArrayTable> CreateArrayTable(
flatbuffers::FlatBufferBuilder &_fbb,
const MyGame::Example::ArrayStruct *a = 0) {
ArrayTableBuilder builder_(_fbb);
builder_.add_a(a);
return builder_.Finish();
}
flatbuffers::Offset<ArrayTable> CreateArrayTable(flatbuffers::FlatBufferBuilder &_fbb, const ArrayTableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr);
inline ArrayTableT *ArrayTable::UnPack(const flatbuffers::resolver_function_t *_resolver) const {
auto _o = new ArrayTableT();
UnPackTo(_o, _resolver);
return _o;
}
inline void ArrayTable::UnPackTo(ArrayTableT *_o, const flatbuffers::resolver_function_t *_resolver) const {
(void)_o;
(void)_resolver;
{ auto _e = a(); if (_e) _o->a = flatbuffers::unique_ptr<MyGame::Example::ArrayStruct>(new MyGame::Example::ArrayStruct(*_e)); };
}
inline flatbuffers::Offset<ArrayTable> ArrayTable::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ArrayTableT* _o, const flatbuffers::rehasher_function_t *_rehasher) {
return CreateArrayTable(_fbb, _o, _rehasher);
}
inline flatbuffers::Offset<ArrayTable> CreateArrayTable(flatbuffers::FlatBufferBuilder &_fbb, const ArrayTableT *_o, const flatbuffers::rehasher_function_t *_rehasher) {
(void)_rehasher;
(void)_o;
struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ArrayTableT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va;
auto _a = _o->a ? _o->a.get() : 0;
return MyGame::Example::CreateArrayTable(
_fbb,
_a);
}
inline const flatbuffers::TypeTable *TestEnumTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_CHAR, 0, 0 },
{ flatbuffers::ET_CHAR, 0, 0 },
{ flatbuffers::ET_CHAR, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::TestEnumTypeTable
};
static const char * const names[] = {
"A",
"B",
"C"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_ENUM, 3, type_codes, type_refs, nullptr, names
};
return &tt;
}
inline const flatbuffers::TypeTable *NestedStructTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, -1 },
{ flatbuffers::ET_CHAR, 0, 0 },
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::TestEnumTypeTable
};
static const int64_t values[] = { 0, 8, 9, 12 };
static const char * const names[] = {
"a",
"b",
"c"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_STRUCT, 3, type_codes, type_refs, values, names
};
return &tt;
}
inline const flatbuffers::TypeTable *ArrayStructTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_FLOAT, 0, -1 },
{ flatbuffers::ET_SEQUENCE, 0, -1 },
{ flatbuffers::ET_CHAR, 0, -1 },
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::NestedStructTypeTable
};
static const int64_t values[] = { 0, 4, 64, 68, 92 };
static const char * const names[] = {
"a",
"b",
"c",
"d"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_STRUCT, 4, type_codes, type_refs, values, names
};
return &tt;
}
inline const flatbuffers::TypeTable *ArrayTableTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_SEQUENCE, 0, 0 }
};
static const flatbuffers::TypeFunction type_refs[] = {
MyGame::Example::ArrayStructTypeTable
};
static const char * const names[] = {
"a"
};
static const flatbuffers::TypeTable tt = {
flatbuffers::ST_TABLE, 1, type_codes, type_refs, nullptr, names
};
return &tt;
}
inline const MyGame::Example::ArrayTable *GetArrayTable(const void *buf) {
return flatbuffers::GetRoot<MyGame::Example::ArrayTable>(buf);
}
inline const MyGame::Example::ArrayTable *GetSizePrefixedArrayTable(const void *buf) {
return flatbuffers::GetSizePrefixedRoot<MyGame::Example::ArrayTable>(buf);
}
inline ArrayTable *GetMutableArrayTable(void *buf) {
return flatbuffers::GetMutableRoot<ArrayTable>(buf);
}
inline const char *ArrayTableIdentifier() {
return "ARRT";
}
inline bool ArrayTableBufferHasIdentifier(const void *buf) {
return flatbuffers::BufferHasIdentifier(
buf, ArrayTableIdentifier());
}
inline bool VerifyArrayTableBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<MyGame::Example::ArrayTable>(ArrayTableIdentifier());
}
inline bool VerifySizePrefixedArrayTableBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<MyGame::Example::ArrayTable>(ArrayTableIdentifier());
}
inline const char *ArrayTableExtension() {
return "mon";
}
inline void FinishArrayTableBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<MyGame::Example::ArrayTable> root) {
fbb.Finish(root, ArrayTableIdentifier());
}
inline void FinishSizePrefixedArrayTableBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<MyGame::Example::ArrayTable> root) {
fbb.FinishSizePrefixed(root, ArrayTableIdentifier());
}
inline flatbuffers::unique_ptr<MyGame::Example::ArrayTableT> UnPackArrayTable(
const void *buf,
const flatbuffers::resolver_function_t *res = nullptr) {
return flatbuffers::unique_ptr<MyGame::Example::ArrayTableT>(GetArrayTable(buf)->UnPack(res));
}
inline flatbuffers::unique_ptr<MyGame::Example::ArrayTableT> UnPackSizePrefixedArrayTable(
const void *buf,
const flatbuffers::resolver_function_t *res = nullptr) {
return flatbuffers::unique_ptr<MyGame::Example::ArrayTableT>(GetSizePrefixedArrayTable(buf)->UnPack(res));
}
} // namespace Example
} // namespace MyGame
#endif // FLATBUFFERS_GENERATED_ARRAYSTEST_MYGAME_EXAMPLE_H_

View File

@@ -19,7 +19,9 @@ if "%1"=="-b" set buildtype=%2
..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL
..\%buildtype%\flatc.exe --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 || goto FAIL
..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs || goto FAIL
..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL
IF NOT "%MONSTER_EXTRA%"=="skip" (
@echo Generate MosterExtra

View File

@@ -19,8 +19,10 @@ set -e
../flatc --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs
../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 -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_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 monsterdata_extra.json || goto FAIL
../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes monster_extra.fbs monsterdata_extra.json
../flatc --cpp --java --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs
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

View File

@@ -24,21 +24,27 @@
"MyGame_OtherNameSpace_Unused" : {
"type" : "object",
"properties" : {
"a" : { "type" : "number" }
"a" : {
"type" : "number"
}
},
"additionalProperties" : false
},
"MyGame_OtherNameSpace_TableB" : {
"type" : "object",
"properties" : {
"a" : { "$ref" : "#/definitions/TableA" }
"a" : {
"$ref" : "#/definitions/TableA"
}
},
"additionalProperties" : false
},
"TableA" : {
"type" : "object",
"properties" : {
"b" : { "$ref" : "#/definitions/MyGame_OtherNameSpace_TableB" }
"b" : {
"$ref" : "#/definitions/MyGame_OtherNameSpace_TableB"
}
},
"additionalProperties" : false
},
@@ -57,51 +63,81 @@
"MyGame_Example_Test" : {
"type" : "object",
"properties" : {
"a" : { "type" : "number" },
"b" : { "type" : "number" }
"a" : {
"type" : "number"
},
"b" : {
"type" : "number"
}
},
"additionalProperties" : false
},
"MyGame_Example_TestSimpleTableWithEnum" : {
"type" : "object",
"properties" : {
"color" : { "$ref" : "#/definitions/MyGame_Example_Color" }
"color" : {
"$ref" : "#/definitions/MyGame_Example_Color"
}
},
"additionalProperties" : false
},
"MyGame_Example_Vec3" : {
"type" : "object",
"properties" : {
"x" : { "type" : "number" },
"y" : { "type" : "number" },
"z" : { "type" : "number" },
"test1" : { "type" : "number" },
"test2" : { "$ref" : "#/definitions/MyGame_Example_Color" },
"test3" : { "$ref" : "#/definitions/MyGame_Example_Test" }
"x" : {
"type" : "number"
},
"y" : {
"type" : "number"
},
"z" : {
"type" : "number"
},
"test1" : {
"type" : "number"
},
"test2" : {
"$ref" : "#/definitions/MyGame_Example_Color"
},
"test3" : {
"$ref" : "#/definitions/MyGame_Example_Test"
}
},
"additionalProperties" : false
},
"MyGame_Example_Ability" : {
"type" : "object",
"properties" : {
"id" : { "type" : "number" },
"distance" : { "type" : "number" }
"id" : {
"type" : "number"
},
"distance" : {
"type" : "number"
}
},
"additionalProperties" : false
},
"MyGame_Example_Stat" : {
"type" : "object",
"properties" : {
"id" : { "type" : "string" },
"val" : { "type" : "number" },
"count" : { "type" : "number" }
"id" : {
"type" : "string"
},
"val" : {
"type" : "number"
},
"count" : {
"type" : "number"
}
},
"additionalProperties" : false
},
"MyGame_Example_Referrable" : {
"type" : "object",
"properties" : {
"id" : { "type" : "number" }
"id" : {
"type" : "number"
}
},
"additionalProperties" : false
},
@@ -109,54 +145,150 @@
"type" : "object",
"description" : " an example documentation comment: monster object",
"properties" : {
"pos" : { "$ref" : "#/definitions/MyGame_Example_Vec3" },
"mana" : { "type" : "number" },
"hp" : { "type" : "number" },
"name" : { "type" : "string" },
"friendly" : { "type" : "boolean" },
"inventory" : { "type" : "array", "items" : { "type" : "number" } },
"color" : { "$ref" : "#/definitions/MyGame_Example_Color" },
"test_type" : { "$ref" : "#/definitions/MyGame_Example_Any" },
"test" : { "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }] },
"test4" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Test" } },
"testarrayofstring" : { "type" : "array", "items" : { "type" : "string" } },
"testarrayoftables" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Monster" } },
"enemy" : { "$ref" : "#/definitions/MyGame_Example_Monster" },
"testnestedflatbuffer" : { "type" : "array", "items" : { "type" : "number" } },
"testempty" : { "$ref" : "#/definitions/MyGame_Example_Stat" },
"testbool" : { "type" : "boolean" },
"testhashs32_fnv1" : { "type" : "number" },
"testhashu32_fnv1" : { "type" : "number" },
"testhashs64_fnv1" : { "type" : "number" },
"testhashu64_fnv1" : { "type" : "number" },
"testhashs32_fnv1a" : { "type" : "number" },
"testhashu32_fnv1a" : { "type" : "number" },
"testhashs64_fnv1a" : { "type" : "number" },
"testhashu64_fnv1a" : { "type" : "number" },
"testarrayofbools" : { "type" : "array", "items" : { "type" : "boolean" } },
"testf" : { "type" : "number" },
"testf2" : { "type" : "number" },
"testf3" : { "type" : "number" },
"testarrayofstring2" : { "type" : "array", "items" : { "type" : "string" } },
"testarrayofsortedstruct" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Ability" } },
"flex" : { "type" : "array", "items" : { "type" : "number" } },
"test5" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Test" } },
"vector_of_longs" : { "type" : "array", "items" : { "type" : "number" } },
"vector_of_doubles" : { "type" : "array", "items" : { "type" : "number" } },
"parent_namespace_test" : { "$ref" : "#/definitions/MyGame_InParentNamespace" },
"vector_of_referrables" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Referrable" } },
"single_weak_reference" : { "type" : "number" },
"vector_of_weak_references" : { "type" : "array", "items" : { "type" : "number" } },
"vector_of_strong_referrables" : { "type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Referrable" } },
"co_owning_reference" : { "type" : "number" },
"vector_of_co_owning_references" : { "type" : "array", "items" : { "type" : "number" } },
"non_owning_reference" : { "type" : "number" },
"vector_of_non_owning_references" : { "type" : "array", "items" : { "type" : "number" } },
"any_unique_type" : { "$ref" : "#/definitions/MyGame_Example_AnyUniqueAliases" },
"any_unique" : { "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }] },
"any_ambiguous_type" : { "$ref" : "#/definitions/MyGame_Example_AnyAmbiguousAliases" },
"any_ambiguous" : { "anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" }] },
"vector_of_enums" : { "$ref" : "#/definitions/MyGame_Example_Color" }
"pos" : {
"$ref" : "#/definitions/MyGame_Example_Vec3"
},
"mana" : {
"type" : "number"
},
"hp" : {
"type" : "number"
},
"name" : {
"type" : "string"
},
"friendly" : {
"type" : "boolean"
},
"inventory" : {
"type" : "array", "items" : { "type" : "number" }
},
"color" : {
"$ref" : "#/definitions/MyGame_Example_Color"
},
"test_type" : {
"$ref" : "#/definitions/MyGame_Example_Any"
},
"test" : {
"anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }]
},
"test4" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Test" }
},
"testarrayofstring" : {
"type" : "array", "items" : { "type" : "string" }
},
"testarrayoftables" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Monster" }
},
"enemy" : {
"$ref" : "#/definitions/MyGame_Example_Monster"
},
"testnestedflatbuffer" : {
"type" : "array", "items" : { "type" : "number" }
},
"testempty" : {
"$ref" : "#/definitions/MyGame_Example_Stat"
},
"testbool" : {
"type" : "boolean"
},
"testhashs32_fnv1" : {
"type" : "number"
},
"testhashu32_fnv1" : {
"type" : "number"
},
"testhashs64_fnv1" : {
"type" : "number"
},
"testhashu64_fnv1" : {
"type" : "number"
},
"testhashs32_fnv1a" : {
"type" : "number"
},
"testhashu32_fnv1a" : {
"type" : "number"
},
"testhashs64_fnv1a" : {
"type" : "number"
},
"testhashu64_fnv1a" : {
"type" : "number"
},
"testarrayofbools" : {
"type" : "array", "items" : { "type" : "boolean" }
},
"testf" : {
"type" : "number"
},
"testf2" : {
"type" : "number"
},
"testf3" : {
"type" : "number"
},
"testarrayofstring2" : {
"type" : "array", "items" : { "type" : "string" }
},
"testarrayofsortedstruct" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Ability" }
},
"flex" : {
"type" : "array", "items" : { "type" : "number" }
},
"test5" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Test" }
},
"vector_of_longs" : {
"type" : "array", "items" : { "type" : "number" }
},
"vector_of_doubles" : {
"type" : "array", "items" : { "type" : "number" }
},
"parent_namespace_test" : {
"$ref" : "#/definitions/MyGame_InParentNamespace"
},
"vector_of_referrables" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Referrable" }
},
"single_weak_reference" : {
"type" : "number"
},
"vector_of_weak_references" : {
"type" : "array", "items" : { "type" : "number" }
},
"vector_of_strong_referrables" : {
"type" : "array", "items" : { "$ref" : "#/definitions/MyGame_Example_Referrable" }
},
"co_owning_reference" : {
"type" : "number"
},
"vector_of_co_owning_references" : {
"type" : "array", "items" : { "type" : "number" }
},
"non_owning_reference" : {
"type" : "number"
},
"vector_of_non_owning_references" : {
"type" : "array", "items" : { "type" : "number" }
},
"any_unique_type" : {
"$ref" : "#/definitions/MyGame_Example_AnyUniqueAliases"
},
"any_unique" : {
"anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_TestSimpleTableWithEnum" },{ "$ref" : "#/definitions/MyGame_Example2_Monster" }]
},
"any_ambiguous_type" : {
"$ref" : "#/definitions/MyGame_Example_AnyAmbiguousAliases"
},
"any_ambiguous" : {
"anyOf": [{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" },{ "$ref" : "#/definitions/MyGame_Example_Monster" }]
},
"vector_of_enums" : {
"$ref" : "#/definitions/MyGame_Example_Color"
}
},
"required" : ["name"],
"additionalProperties" : false
@@ -164,18 +296,42 @@
"MyGame_Example_TypeAliases" : {
"type" : "object",
"properties" : {
"i8" : { "type" : "number" },
"u8" : { "type" : "number" },
"i16" : { "type" : "number" },
"u16" : { "type" : "number" },
"i32" : { "type" : "number" },
"u32" : { "type" : "number" },
"i64" : { "type" : "number" },
"u64" : { "type" : "number" },
"f32" : { "type" : "number" },
"f64" : { "type" : "number" },
"v8" : { "type" : "array", "items" : { "type" : "number" } },
"vf64" : { "type" : "array", "items" : { "type" : "number" } }
"i8" : {
"type" : "number"
},
"u8" : {
"type" : "number"
},
"i16" : {
"type" : "number"
},
"u16" : {
"type" : "number"
},
"i32" : {
"type" : "number"
},
"u32" : {
"type" : "number"
},
"i64" : {
"type" : "number"
},
"u64" : {
"type" : "number"
},
"f32" : {
"type" : "number"
},
"f64" : {
"type" : "number"
},
"v8" : {
"type" : "array", "items" : { "type" : "number" }
},
"vf64" : {
"type" : "array", "items" : { "type" : "number" }
}
},
"additionalProperties" : false
}

View File

@@ -42,7 +42,10 @@ import MyGame.Example.Test # refers to generated code
import MyGame.Example.Stat # refers to generated code
import MyGame.Example.Vec3 # refers to generated code
import MyGame.MonsterExtra # refers to generated code
import MyGame.Example.ArrayTable # refers to generated code
import MyGame.Example.ArrayStruct # refers to generated code
import MyGame.Example.NestedStruct # refers to generated code
import MyGame.Example.TestEnum # refers to generated code
def assertRaises(test_case, fn, exception_class):
''' Backwards-compatible assertion for exceptions raised. '''
@@ -1544,6 +1547,55 @@ class TestExceptions(unittest.TestCase):
flatbuffers.builder.BuilderNotFinishedError)
class TestFixedLengthArrays(unittest.TestCase):
def test_fixed_length_array(self):
builder = flatbuffers.Builder(0)
a = 0.5
b = range(0, 15)
c = 1
d_a = [[1, 2], [3, 4]]
d_b = [MyGame.Example.TestEnum.TestEnum.B, \
MyGame.Example.TestEnum.TestEnum.C]
d_c = [[MyGame.Example.TestEnum.TestEnum.A, \
MyGame.Example.TestEnum.TestEnum.B], \
[MyGame.Example.TestEnum.TestEnum.C, \
MyGame.Example.TestEnum.TestEnum.B]]
arrayOffset = MyGame.Example.ArrayStruct.CreateArrayStruct(builder, \
a, b, c, d_a, d_b, d_c)
# Create a table with the ArrayStruct.
MyGame.Example.ArrayTable.ArrayTableStart(builder)
MyGame.Example.ArrayTable.ArrayTableAddA(builder, arrayOffset)
tableOffset = MyGame.Example.ArrayTable.ArrayTableEnd(builder)
builder.Finish(tableOffset)
buf = builder.Output()
table = MyGame.Example.ArrayTable.ArrayTable.GetRootAsArrayTable(buf, 0)
# Verify structure.
nested = MyGame.Example.NestedStruct.NestedStruct()
self.assertEqual(table.A().A(), 0.5)
self.assertEqual(table.A().B(), \
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
self.assertEqual(table.A().C(), 1)
self.assertEqual(table.A().D(nested, 0).A(), [1, 2])
self.assertEqual(table.A().D(nested, 1).A(), [3, 4])
self.assertEqual(table.A().D(nested, 0).B(), \
MyGame.Example.TestEnum.TestEnum.B)
self.assertEqual(table.A().D(nested, 1).B(), \
MyGame.Example.TestEnum.TestEnum.C)
self.assertEqual(table.A().D(nested, 0).C(), \
[MyGame.Example.TestEnum.TestEnum.A, \
MyGame.Example.TestEnum.TestEnum.B])
self.assertEqual(table.A().D(nested, 1).C(), \
[MyGame.Example.TestEnum.TestEnum.C, \
MyGame.Example.TestEnum.TestEnum.B])
def CheckAgainstGoldDataGo():
try:
gen_buf, gen_off = make_monster_from_generated_code()

View File

@@ -34,6 +34,9 @@
#include "namespace_test/namespace_test2_generated.h"
#include "union_vector/union_vector_generated.h"
#include "monster_extra_generated.h"
#if !defined(_MSC_VER) || _MSC_VER >= 1700
# include "arrays_test_generated.h"
#endif
#include "test_assert.h"
#include "flatbuffers/flexbuffers.h"
@@ -1203,6 +1206,15 @@ void FuzzTest2() {
AddToSchemaAndInstances(
"bool", deprecated ? "" : (lcg_rand() % 2 ? "true" : "false"));
break;
case flatbuffers::BASE_TYPE_ARRAY:
if (!is_struct) {
AddToSchemaAndInstances(
"ubyte",
deprecated ? "" : "255"); // No fixed-length arrays in tables.
} else {
AddToSchemaAndInstances("[int:3]", deprecated ? "" : "[\n,\n,\n]");
}
break;
default:
// All the scalar types.
schema += flatbuffers::kTypeNames[base_type];
@@ -2671,6 +2683,142 @@ void CreateSharedStringTest() {
TEST_EQ((*a[6]) < (*a[5]), true);
}
void FixedLengthArrayTest() {
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
// Generate an ArrayTable containing one ArrayStruct.
flatbuffers::FlatBufferBuilder fbb;
MyGame::Example::NestedStruct nStruct0(MyGame::Example::TestEnum::B);
TEST_NOTNULL(nStruct0.mutable_a());
nStruct0.mutable_a()->Mutate(0, 1);
nStruct0.mutable_a()->Mutate(1, 2);
TEST_NOTNULL(nStruct0.mutable_c());
nStruct0.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
nStruct0.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
MyGame::Example::NestedStruct nStruct1(MyGame::Example::TestEnum::C);
TEST_NOTNULL(nStruct1.mutable_a());
nStruct1.mutable_a()->Mutate(0, 3);
nStruct1.mutable_a()->Mutate(1, 4);
TEST_NOTNULL(nStruct1.mutable_c());
nStruct1.mutable_c()->Mutate(0, MyGame::Example::TestEnum::C);
nStruct1.mutable_c()->Mutate(1, MyGame::Example::TestEnum::A);
MyGame::Example::ArrayStruct aStruct(2, 12);
TEST_NOTNULL(aStruct.b());
TEST_NOTNULL(aStruct.mutable_b());
TEST_NOTNULL(aStruct.mutable_d());
for (int i = 0; i < aStruct.b()->size(); i++)
aStruct.mutable_b()->Mutate(i, i + 1);
aStruct.mutable_d()->Mutate(0, nStruct0);
aStruct.mutable_d()->Mutate(1, nStruct1);
auto aTable = MyGame::Example::CreateArrayTable(fbb, &aStruct);
fbb.Finish(aTable);
// Verify correctness of the ArrayTable.
flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
MyGame::Example::VerifyArrayTableBuffer(verifier);
auto p = MyGame::Example::GetMutableArrayTable(fbb.GetBufferPointer());
auto mArStruct = p->mutable_a();
TEST_NOTNULL(mArStruct);
TEST_NOTNULL(mArStruct->b());
TEST_NOTNULL(mArStruct->d());
TEST_NOTNULL(mArStruct->mutable_b());
TEST_NOTNULL(mArStruct->mutable_d());
mArStruct->mutable_b()->Mutate(14, -14);
TEST_EQ(mArStruct->a(), 2);
TEST_EQ(mArStruct->b()->size(), 15);
TEST_EQ(mArStruct->b()->Get(aStruct.b()->size() - 1), -14);
TEST_EQ(mArStruct->c(), 12);
TEST_NOTNULL(mArStruct->d()->Get(0).a());
TEST_EQ(mArStruct->d()->Get(0).a()->Get(0), 1);
TEST_EQ(mArStruct->d()->Get(0).a()->Get(1), 2);
TEST_NOTNULL(mArStruct->d()->Get(1).a());
TEST_EQ(mArStruct->d()->Get(1).a()->Get(0), 3);
TEST_EQ(mArStruct->d()->Get(1).a()->Get(1), 4);
TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1));
TEST_NOTNULL(mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a());
mArStruct->mutable_d()->GetMutablePointer(1)->mutable_a()->Mutate(1, 5);
TEST_EQ(mArStruct->d()->Get(1).a()->Get(1), 5);
TEST_EQ(mArStruct->d()->Get(0).b() == MyGame::Example::TestEnum::B, true);
TEST_NOTNULL(mArStruct->d()->Get(0).c());
TEST_EQ(mArStruct->d()->Get(0).c()->Get(0) == MyGame::Example::TestEnum::C,
true);
TEST_EQ(mArStruct->d()->Get(0).c()->Get(1) == MyGame::Example::TestEnum::A,
true);
TEST_EQ(mArStruct->d()->Get(1).b() == MyGame::Example::TestEnum::C, true);
TEST_NOTNULL(mArStruct->d()->Get(1).c());
TEST_EQ(mArStruct->d()->Get(1).c()->Get(0) == MyGame::Example::TestEnum::C,
true);
TEST_EQ(mArStruct->d()->Get(1).c()->Get(1) == MyGame::Example::TestEnum::A,
true);
for (int i = 0; i < mArStruct->b()->size() - 1; i++)
TEST_EQ(mArStruct->b()->Get(i), i + 1);
#endif
}
void FixedLengthArrayJsonTest(bool binary) {
// VS10 does not support typed enums, exclude from tests
#if !defined(_MSC_VER) || _MSC_VER >= 1700
// load FlatBuffer schema (.fbs) and JSON from disk
std::string schemafile;
std::string jsonfile;
TEST_EQ(
flatbuffers::LoadFile(
(test_data_path + "arrays_test." + (binary ? "bfbs" : "fbs")).c_str(),
binary, &schemafile),
true);
TEST_EQ(flatbuffers::LoadFile((test_data_path + "arrays_test.golden").c_str(),
false, &jsonfile),
true);
// parse schema first, so we can use it to parse the data after
flatbuffers::Parser parserOrg, parserGen;
if (binary) {
flatbuffers::Verifier verifier(
reinterpret_cast<const uint8_t *>(schemafile.c_str()),
schemafile.size());
TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
TEST_EQ(parserOrg.Deserialize((const uint8_t *)schemafile.c_str(),
schemafile.size()),
true);
TEST_EQ(parserGen.Deserialize((const uint8_t *)schemafile.c_str(),
schemafile.size()),
true);
} else {
TEST_EQ(parserOrg.Parse(schemafile.c_str()), true);
TEST_EQ(parserGen.Parse(schemafile.c_str()), true);
}
TEST_EQ(parserOrg.Parse(jsonfile.c_str()), true);
// First, verify it, just in case:
flatbuffers::Verifier verifierOrg(parserOrg.builder_.GetBufferPointer(),
parserOrg.builder_.GetSize());
TEST_EQ(VerifyArrayTableBuffer(verifierOrg), true);
// Export to JSON
std::string jsonGen;
TEST_EQ(
GenerateText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen),
true);
// Import from JSON
TEST_EQ(parserGen.Parse(jsonGen.c_str()), true);
// Verify buffer from generated JSON
flatbuffers::Verifier verifierGen(parserGen.builder_.GetBufferPointer(),
parserGen.builder_.GetSize());
TEST_EQ(VerifyArrayTableBuffer(verifierGen), true);
// Compare generated buffer to original
TEST_EQ(parserOrg.builder_.GetSize(), parserGen.builder_.GetSize());
TEST_EQ(std::memcmp(parserOrg.builder_.GetBufferPointer(),
parserGen.builder_.GetBufferPointer(),
parserOrg.builder_.GetSize()),
0);
#else
(void)binary;
#endif
}
int FlatBufferTests() {
// clang-format off
@@ -2705,6 +2853,8 @@ int FlatBufferTests() {
#endif
ParseAndGenerateTextTest(false);
ParseAndGenerateTextTest(true);
FixedLengthArrayJsonTest(false);
FixedLengthArrayJsonTest(true);
ReflectionTest(flatbuf.data(), flatbuf.size());
ParseProtoTest();
UnionVectorTest();
@@ -2747,6 +2897,7 @@ int FlatBufferTests() {
ValidFloatTest();
InvalidFloatTest();
TestMonsterExtraFloats();
FixedLengthArrayTest();
return 0;
}