mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-28 17:10:01 +00:00
- add flatbuffers::span - add new constructor for `struct` with `array` - add some test for flatbuffers::span and 'arrays_test.fbs'
This commit is contained in:
@@ -92,12 +92,24 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) NestedStruct FLATBUFFERS_FINAL_CLASS {
|
||||
padding0__(0),
|
||||
padding1__(0),
|
||||
d_() {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
}
|
||||
NestedStruct(flatbuffers::span<const int32_t, 2> _a, MyGame::Example::TestEnum _b, flatbuffers::span<const MyGame::Example::TestEnum, 2> _c, flatbuffers::span<const int64_t, 2> _d)
|
||||
: b_(flatbuffers::EndianScalar(static_cast<int8_t>(_b))),
|
||||
padding0__(0),
|
||||
padding1__(0) {
|
||||
flatbuffers::CastToArray(a_).CopyFromSpan(_a);
|
||||
flatbuffers::CastToArrayOfEnum<MyGame::Example::TestEnum>(c_).CopyFromSpan(_c);
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
flatbuffers::CastToArray(d_).CopyFromSpan(_d);
|
||||
}
|
||||
const flatbuffers::Array<int32_t, 2> *a() const {
|
||||
return reinterpret_cast<const flatbuffers::Array<int32_t, 2> *>(a_);
|
||||
return &flatbuffers::CastToArray(a_);
|
||||
}
|
||||
flatbuffers::Array<int32_t, 2> *mutable_a() {
|
||||
return reinterpret_cast<flatbuffers::Array<int32_t, 2> *>(a_);
|
||||
return &flatbuffers::CastToArray(a_);
|
||||
}
|
||||
MyGame::Example::TestEnum b() const {
|
||||
return static_cast<MyGame::Example::TestEnum>(flatbuffers::EndianScalar(b_));
|
||||
@@ -106,16 +118,16 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) NestedStruct FLATBUFFERS_FINAL_CLASS {
|
||||
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_);
|
||||
return &flatbuffers::CastToArrayOfEnum<MyGame::Example::TestEnum>(c_);
|
||||
}
|
||||
flatbuffers::Array<MyGame::Example::TestEnum, 2> *mutable_c() {
|
||||
return reinterpret_cast<flatbuffers::Array<MyGame::Example::TestEnum, 2> *>(c_);
|
||||
return &flatbuffers::CastToArrayOfEnum<MyGame::Example::TestEnum>(c_);
|
||||
}
|
||||
const flatbuffers::Array<int64_t, 2> *d() const {
|
||||
return reinterpret_cast<const flatbuffers::Array<int64_t, 2> *>(d_);
|
||||
return &flatbuffers::CastToArray(d_);
|
||||
}
|
||||
flatbuffers::Array<int64_t, 2> *mutable_d() {
|
||||
return reinterpret_cast<flatbuffers::Array<int64_t, 2> *>(d_);
|
||||
return &flatbuffers::CastToArray(d_);
|
||||
}
|
||||
};
|
||||
FLATBUFFERS_STRUCT_END(NestedStruct, 32);
|
||||
@@ -175,6 +187,26 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) ArrayStruct FLATBUFFERS_FINAL_CLASS {
|
||||
e_(flatbuffers::EndianScalar(_e)),
|
||||
padding3__(0),
|
||||
f_() {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
(void)padding2__;
|
||||
(void)padding3__;
|
||||
}
|
||||
ArrayStruct(float _a, flatbuffers::span<const int32_t, 15> _b, int8_t _c, flatbuffers::span<const MyGame::Example::NestedStruct, 2> _d, int32_t _e, flatbuffers::span<const int64_t, 2> _f)
|
||||
: a_(flatbuffers::EndianScalar(_a)),
|
||||
c_(flatbuffers::EndianScalar(_c)),
|
||||
padding0__(0),
|
||||
padding1__(0),
|
||||
padding2__(0),
|
||||
e_(flatbuffers::EndianScalar(_e)),
|
||||
padding3__(0) {
|
||||
flatbuffers::CastToArray(b_).CopyFromSpan(_b);
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
(void)padding2__;
|
||||
flatbuffers::CastToArray(d_).CopyFromSpan(_d);
|
||||
(void)padding3__;
|
||||
flatbuffers::CastToArray(f_).CopyFromSpan(_f);
|
||||
}
|
||||
float a() const {
|
||||
return flatbuffers::EndianScalar(a_);
|
||||
@@ -183,10 +215,10 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) ArrayStruct FLATBUFFERS_FINAL_CLASS {
|
||||
flatbuffers::WriteScalar(&a_, _a);
|
||||
}
|
||||
const flatbuffers::Array<int32_t, 15> *b() const {
|
||||
return reinterpret_cast<const flatbuffers::Array<int32_t, 15> *>(b_);
|
||||
return &flatbuffers::CastToArray(b_);
|
||||
}
|
||||
flatbuffers::Array<int32_t, 15> *mutable_b() {
|
||||
return reinterpret_cast<flatbuffers::Array<int32_t, 15> *>(b_);
|
||||
return &flatbuffers::CastToArray(b_);
|
||||
}
|
||||
int8_t c() const {
|
||||
return flatbuffers::EndianScalar(c_);
|
||||
@@ -195,10 +227,10 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) ArrayStruct FLATBUFFERS_FINAL_CLASS {
|
||||
flatbuffers::WriteScalar(&c_, _c);
|
||||
}
|
||||
const flatbuffers::Array<MyGame::Example::NestedStruct, 2> *d() const {
|
||||
return reinterpret_cast<const flatbuffers::Array<MyGame::Example::NestedStruct, 2> *>(d_);
|
||||
return &flatbuffers::CastToArray(d_);
|
||||
}
|
||||
flatbuffers::Array<MyGame::Example::NestedStruct, 2> *mutable_d() {
|
||||
return reinterpret_cast<flatbuffers::Array<MyGame::Example::NestedStruct, 2> *>(d_);
|
||||
return &flatbuffers::CastToArray(d_);
|
||||
}
|
||||
int32_t e() const {
|
||||
return flatbuffers::EndianScalar(e_);
|
||||
@@ -207,10 +239,10 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) ArrayStruct FLATBUFFERS_FINAL_CLASS {
|
||||
flatbuffers::WriteScalar(&e_, _e);
|
||||
}
|
||||
const flatbuffers::Array<int64_t, 2> *f() const {
|
||||
return reinterpret_cast<const flatbuffers::Array<int64_t, 2> *>(f_);
|
||||
return &flatbuffers::CastToArray(f_);
|
||||
}
|
||||
flatbuffers::Array<int64_t, 2> *mutable_f() {
|
||||
return reinterpret_cast<flatbuffers::Array<int64_t, 2> *>(f_);
|
||||
return &flatbuffers::CastToArray(f_);
|
||||
}
|
||||
};
|
||||
FLATBUFFERS_STRUCT_END(ArrayStruct, 160);
|
||||
|
||||
@@ -487,6 +487,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
|
||||
: a_(flatbuffers::EndianScalar(_a)),
|
||||
b_(flatbuffers::EndianScalar(_b)),
|
||||
padding0__(0) {
|
||||
(void)padding0__;
|
||||
}
|
||||
int16_t a() const {
|
||||
return flatbuffers::EndianScalar(a_);
|
||||
@@ -543,6 +544,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
|
||||
padding1__(0),
|
||||
test3_(_test3),
|
||||
padding2__(0) {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
(void)padding2__;
|
||||
}
|
||||
float x() const {
|
||||
return flatbuffers::EndianScalar(x_);
|
||||
|
||||
@@ -115,6 +115,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Struct FLATBUFFERS_FINAL_CLASS {
|
||||
: a_(flatbuffers::EndianScalar(_a)),
|
||||
padding0__(0),
|
||||
b_(flatbuffers::EndianScalar(_b)) {
|
||||
(void)padding0__;
|
||||
}
|
||||
int32_t a() const {
|
||||
return flatbuffers::EndianScalar(a_);
|
||||
|
||||
@@ -131,6 +131,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Struct FLATBUFFERS_FINAL_CLASS {
|
||||
: a_(flatbuffers::EndianScalar(_a)),
|
||||
padding0__(0),
|
||||
b_(flatbuffers::EndianScalar(_b)) {
|
||||
(void)padding0__;
|
||||
}
|
||||
int32_t a() const {
|
||||
return flatbuffers::EndianScalar(a_);
|
||||
|
||||
@@ -602,6 +602,7 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
|
||||
: a_(flatbuffers::EndianScalar(_a)),
|
||||
b_(flatbuffers::EndianScalar(_b)),
|
||||
padding0__(0) {
|
||||
(void)padding0__;
|
||||
}
|
||||
int16_t a() const {
|
||||
return flatbuffers::EndianScalar(a_);
|
||||
@@ -669,6 +670,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
|
||||
padding1__(0),
|
||||
test3_(_test3),
|
||||
padding2__(0) {
|
||||
(void)padding0__;
|
||||
(void)padding1__;
|
||||
(void)padding2__;
|
||||
}
|
||||
float x() const {
|
||||
return flatbuffers::EndianScalar(x_);
|
||||
|
||||
136
tests/test.cpp
136
tests/test.cpp
@@ -3214,6 +3214,89 @@ void CreateSharedStringTest() {
|
||||
TEST_EQ((*a[6]) < (*a[5]), true);
|
||||
}
|
||||
|
||||
#if !defined(FLATBUFFERS_SPAN_MINIMAL)
|
||||
void FlatbuffersSpanTest() {
|
||||
// Compile-time checking of non-const [] to const [] conversions.
|
||||
using flatbuffers::internal::is_span_convertable;
|
||||
(void)is_span_convertable<int, 1, int, 1>::type(123);
|
||||
(void)is_span_convertable<const int, 1, int, 1>::type(123);
|
||||
(void)is_span_convertable<const int64_t, 1, int64_t, 1>::type(123);
|
||||
(void)is_span_convertable<const uint64_t, 1, uint64_t, 1>::type(123);
|
||||
(void)is_span_convertable<const int, 1, const int, 1>::type(123);
|
||||
(void)is_span_convertable<const int64_t, 1, const int64_t, 1>::type(123);
|
||||
(void)is_span_convertable<const uint64_t, 1, const uint64_t, 1>::type(123);
|
||||
|
||||
using flatbuffers::span;
|
||||
span<char, 0> c1;
|
||||
TEST_EQ(c1.size(), 0);
|
||||
span<char, flatbuffers::dynamic_extent> c2;
|
||||
TEST_EQ(c2.size(), 0);
|
||||
span<char> c3;
|
||||
TEST_EQ(c3.size(), 0);
|
||||
TEST_ASSERT(c1.empty() && c2.empty() && c3.empty());
|
||||
|
||||
int i_data7[7] = { 0, 1, 2, 3, 4, 5, 6 };
|
||||
span<int, 7> i1(&i_data7[0], 7);
|
||||
span<int> i2(i1); // make dynamic from static
|
||||
TEST_EQ(i1.size(), 7);
|
||||
TEST_EQ(i1.empty(), false);
|
||||
TEST_EQ(i1.size(), i2.size());
|
||||
TEST_EQ(i1.data(), i_data7);
|
||||
TEST_EQ(i1[2], 2);
|
||||
// Make const span from a non-const one.
|
||||
span<const int, 7> i3(i1);
|
||||
// Construct from a C-array.
|
||||
span<int, 7> i4(i_data7);
|
||||
span<const int, 7> i5(i_data7);
|
||||
span<int> i6(i_data7);
|
||||
span<const int> i7(i_data7);
|
||||
TEST_EQ(i7.size(), 7);
|
||||
// Check construction from a const array.
|
||||
const int i_cdata5[5] = { 4, 3, 2, 1, 0 };
|
||||
span<const int, 5> i8(i_cdata5);
|
||||
span<const int> i9(i_cdata5);
|
||||
TEST_EQ(i9.size(), 5);
|
||||
// Construction from a (ptr, size) pair.
|
||||
span<int, 7> i10(i_data7, 7);
|
||||
span<int> i11(i_data7, 7);
|
||||
TEST_EQ(i11.size(), 7);
|
||||
span<const int, 5> i12(i_cdata5, 5);
|
||||
span<const int> i13(i_cdata5, 5);
|
||||
TEST_EQ(i13.size(), 5);
|
||||
// Construction from std::array.
|
||||
std::array<int, 6> i_arr6 = { { 0, 1, 2, 3, 4, 5 } };
|
||||
span<int, 6> i14(i_arr6);
|
||||
span<const int, 6> i15(i_arr6);
|
||||
span<int> i16(i_arr6);
|
||||
span<const int> i17(i_arr6);
|
||||
TEST_EQ(i17.size(), 6);
|
||||
const std::array<int, 8> i_carr8 = { { 0, 1, 2, 3, 4, 5, 6, 7 } };
|
||||
span<const int, 8> i18(i_carr8);
|
||||
span<const int> i19(i_carr8);
|
||||
TEST_EQ(i18.size(), 8);
|
||||
TEST_EQ(i19.size(), 8);
|
||||
TEST_EQ(i19[7], 7);
|
||||
// Check compatibility with flatbuffers::Array.
|
||||
int fbs_int3_underlaying[3] = { 0 };
|
||||
int fbs_int3_data[3] = { 1, 2, 3 };
|
||||
auto &fbs_int3 = flatbuffers::CastToArray(fbs_int3_underlaying);
|
||||
fbs_int3.CopyFromSpan(fbs_int3_data);
|
||||
TEST_EQ(fbs_int3.Get(1), 2);
|
||||
const int fbs_cint3_data[3] = { 2, 3, 4 };
|
||||
fbs_int3.CopyFromSpan(fbs_cint3_data);
|
||||
TEST_EQ(fbs_int3.Get(1), 3);
|
||||
// Check with Array<Enum, N>
|
||||
enum class Dummy : uint16_t { Zero = 0, One, Two };
|
||||
Dummy fbs_dummy3_underlaying[3] = {};
|
||||
Dummy fbs_dummy3_data[3] = { Dummy::One, Dummy::Two, Dummy::Two };
|
||||
auto &fbs_dummy3 = flatbuffers::CastToArray(fbs_dummy3_underlaying);
|
||||
fbs_dummy3.CopyFromSpan(fbs_dummy3_data);
|
||||
TEST_EQ(fbs_dummy3.Get(1), Dummy::Two);
|
||||
}
|
||||
#else
|
||||
void FlatbuffersSpanTest() {}
|
||||
#endif
|
||||
|
||||
void FixedLengthArrayTest() {
|
||||
// VS10 does not support typed enums, exclude from tests
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||
@@ -3322,6 +3405,57 @@ void FixedLengthArrayTest() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !defined(FLATBUFFERS_SPAN_MINIMAL) && (!defined(_MSC_VER) || _MSC_VER >= 1700)
|
||||
void FixedLengthArrayConstructorTest() {
|
||||
const int32_t nested_a[2] = { 1, 2 };
|
||||
MyGame::Example::TestEnum nested_c[2] = { MyGame::Example::TestEnum::A,
|
||||
MyGame::Example::TestEnum::B };
|
||||
const int64_t int64_2[2] = { -2, -1 };
|
||||
|
||||
std::array<MyGame::Example::NestedStruct, 2> init_d = {
|
||||
{ MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::B,
|
||||
nested_c, int64_2),
|
||||
MyGame::Example::NestedStruct(nested_a, MyGame::Example::TestEnum::A,
|
||||
nested_c,
|
||||
std::array<int64_t, 2>{ { 12, 13 } }) }
|
||||
};
|
||||
|
||||
MyGame::Example::ArrayStruct arr_struct(
|
||||
8.125,
|
||||
std::array<int32_t, 0xF>{
|
||||
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } },
|
||||
-17, init_d, 10, int64_2);
|
||||
TEST_EQ(arr_struct.a(), 8.125);
|
||||
TEST_EQ(arr_struct.b()->Get(2), 3);
|
||||
TEST_EQ(arr_struct.c(), -17);
|
||||
|
||||
TEST_NOTNULL(arr_struct.d());
|
||||
const auto &arr_d_0 = *arr_struct.d()->Get(0);
|
||||
TEST_EQ(arr_d_0.a()->Get(0), 1);
|
||||
TEST_EQ(arr_d_0.a()->Get(1), 2);
|
||||
TEST_EQ(arr_d_0.b(), MyGame::Example::TestEnum::B);
|
||||
TEST_EQ(arr_d_0.c()->Get(0), MyGame::Example::TestEnum::A);
|
||||
TEST_EQ(arr_d_0.c()->Get(1), MyGame::Example::TestEnum::B);
|
||||
TEST_EQ(arr_d_0.d()->Get(0), -2);
|
||||
TEST_EQ(arr_d_0.d()->Get(1), -1);
|
||||
const auto &arr_d_1 = *arr_struct.d()->Get(1);
|
||||
TEST_EQ(arr_d_1.a()->Get(0), 1);
|
||||
TEST_EQ(arr_d_1.a()->Get(1), 2);
|
||||
TEST_EQ(arr_d_1.b(), MyGame::Example::TestEnum::A);
|
||||
TEST_EQ(arr_d_1.c()->Get(0), MyGame::Example::TestEnum::A);
|
||||
TEST_EQ(arr_d_1.c()->Get(1), MyGame::Example::TestEnum::B);
|
||||
TEST_EQ(arr_d_1.d()->Get(0), 12);
|
||||
TEST_EQ(arr_d_1.d()->Get(1), 13);
|
||||
|
||||
TEST_EQ(arr_struct.e(), 10);
|
||||
TEST_EQ(arr_struct.f()->Get(0), -2);
|
||||
TEST_EQ(arr_struct.f()->Get(1), -1);
|
||||
}
|
||||
#else
|
||||
void FixedLengthArrayConstructorTest() {
|
||||
}
|
||||
#endif
|
||||
|
||||
void NativeTypeTest() {
|
||||
const int N = 3;
|
||||
|
||||
@@ -3666,6 +3800,8 @@ int FlatBufferTests() {
|
||||
NativeTypeTest();
|
||||
OptionalScalarsTest();
|
||||
ParseFlexbuffersFromJsonWithNullTest();
|
||||
FlatbuffersSpanTest();
|
||||
FixedLengthArrayConstructorTest();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user