Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/flatbuffers/flatbuffer_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,16 @@ class FlatBufferBuilderImpl {
return Offset<Vector<uint8_t>>(EndVector(v.size()));
}

Offset64<Vector64<uint8_t>> CreateVector64(const std::vector<bool>& v) {
StartVector<uint8_t, Offset64, Vector64<uint8_t>::size_type>(v.size());
for (auto i = v.size(); i > 0;) {
PushElement(static_cast<uint8_t>(v[--i]));
}
return Offset64<Vector64<uint8_t>>(
EndVector<Vector64<uint8_t>::size_type,
Offset64<Vector64<uint8_t>>::offset_type>(v.size()));
}

/// @brief Serialize values returned by a function into a FlatBuffer `vector`.
/// This is a convenience function that takes care of iteration for you.
/// @tparam T The data type of the `std::vector` elements.
Expand Down
24 changes: 15 additions & 9 deletions tests/64bit/offset64_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ void Offset64Test() {
far_data[0] = 4;
far_data[far_vector_size - 1] = 2;

std::vector<bool> big_bool_data = {true, false, true, true,
false, false, true};

std::vector<uint8_t> big_data;
big_data.resize(big_vector_size);
big_data[0] = 8;
Expand All @@ -49,15 +52,18 @@ void Offset64Test() {
const Offset64<Vector64<uint8_t>> big_vector_offset =
builder.CreateVector64(big_data);

const Offset64<Vector64<uint8_t>> big_bool_vector_offset =
builder.CreateVector64(big_bool_data);

// Now that we are done with the 64-bit fields, we can create and add the
// normal fields.
const Offset<String> near_string_offset =
builder.CreateString("some near string");

// Finish by building the root table by passing in all the offsets.
const Offset<RootTable> root_table_offset =
CreateRootTable(builder, far_vector_offset, 0, far_string_offset,
big_vector_offset, near_string_offset);
const Offset<RootTable> root_table_offset = CreateRootTable(
builder, far_vector_offset, 0, far_string_offset,
big_bool_vector_offset, big_vector_offset, near_string_offset);

// Finish the buffer.
builder.Finish(root_table_offset);
Expand Down Expand Up @@ -118,7 +124,7 @@ void Offset64NestedFlatBuffer() {

// Finish by building the root table by passing in all the offsets.
const Offset<RootTable> root_table_offset =
CreateRootTable(fbb, 0, 0, 0, 0, near_string_offset, 0);
CreateRootTable(fbb, 0, 0, 0, 0, 0, near_string_offset, 0);

// Finish the buffer.
fbb.Finish(root_table_offset);
Expand All @@ -145,7 +151,7 @@ void Offset64NestedFlatBuffer() {

// Finish by building the root table by passing in all the offsets.
const Offset<RootTable> root_table_offset = CreateRootTable(
fbb, 0, 0, 0, 0, near_string_offset, nested_flatbuffer_offset);
fbb, 0, 0, 0, 0, 0, near_string_offset, nested_flatbuffer_offset);

// Finish the buffer.
fbb.Finish(root_table_offset);
Expand Down Expand Up @@ -184,7 +190,7 @@ void Offset64CreateDirect() {
// Call the "Direct" creation method to ensure that things are added to the
// buffer in the correct order, Offset64 first followed by any Offsets.
const Offset<RootTable> root_table_offset = CreateRootTableDirect(
fbb, &data, 0, "some far string", &data, "some near string");
fbb, &data, 0, "some far string", nullptr, &data, "some near string");

// Finish the buffer.
fbb.Finish(root_table_offset);
Expand Down Expand Up @@ -306,7 +312,7 @@ void Offset64VectorOfStructs() {
// Add the two vectors of leaf structs.
const Offset<RootTable> root_table_offset =
CreateRootTableDirect(builder, nullptr, 0, nullptr, nullptr, nullptr,
nullptr, &far_leaves, &big_leaves);
nullptr, nullptr, &far_leaves, &big_leaves);

// Finish the buffer.
builder.Finish(root_table_offset);
Expand Down Expand Up @@ -346,7 +352,7 @@ void Offset64SizePrefix() {

// Finish by building the root table by passing in all the offsets.
const Offset<RootTable> root_table_offset =
CreateRootTable(builder, 0, 0, 0, 0, near_string_offset, 0);
CreateRootTable(builder, 0, 0, 0, 0, 0, near_string_offset, 0);

// Finish the buffer.
FinishSizePrefixedRootTableBuffer(builder, root_table_offset);
Expand Down Expand Up @@ -448,7 +454,7 @@ void Offset64ForceAlign() {
// Use the CreateDirect which calls the ForceVectorAlign
const auto root_table_offset =
CreateRootTableDirect(builder, nullptr, 0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, &data);
nullptr, nullptr, nullptr, nullptr, nullptr, &data);

// Finish the buffer.
FinishRootTableBuffer(builder, root_table_offset);
Expand Down
118 changes: 67 additions & 51 deletions tests/64bit/test_64bit.afb
Original file line number Diff line number Diff line change
@@ -1,74 +1,90 @@
// Annotated Flatbuffer Binary
//
// Schema file: tests/64bit/test_64bit.fbs
// Schema file: tests/64bit/test_64bit.bfbs
// Binary file: tests/64bit/test_64bit.bin

header:
+0x00 | 1C 00 00 00 | UOffset32 | 0x0000001C (28) Loc: 0x1C | offset to root table `RootTable`
+0x0000 | 1C 00 00 00 | UOffset32 | 0x0000001C (28) Loc: 0x001C | offset to root table `RootTable`

padding:
+0x04 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x0004 | 00 00 | uint8_t[2] | .. | padding

vtable (RootTable):
+0x08 | 14 00 | uint16_t | 0x0014 (20) | size of this vtable
+0x0A | 34 00 | uint16_t | 0x0034 (52) | size of referring table
+0x0C | 04 00 | VOffset16 | 0x0004 (4) | offset to field `far_vector` (id: 0)
+0x0E | 10 00 | VOffset16 | 0x0010 (16) | offset to field `a` (id: 1)
+0x10 | 14 00 | VOffset16 | 0x0014 (20) | offset to field `far_string` (id: 2)
+0x12 | 24 00 | VOffset16 | 0x0024 (36) | offset to field `big_vector` (id: 3)
+0x14 | 20 00 | VOffset16 | 0x0020 (32) | offset to field `near_string` (id: 4)
+0x16 | 00 00 | VOffset16 | 0x0000 (0) | offset to field `nested_root` (id: 5) <null> (Vector64)
+0x18 | 00 00 | VOffset16 | 0x0000 (0) | offset to field `far_struct_vector` (id: 6) <null> (Vector)
+0x1A | 2C 00 | VOffset16 | 0x002C (44) | offset to field `big_struct_vector` (id: 7)
+0x0006 | 16 00 | uint16_t | 0x0016 (22) | size of this vtable
+0x0008 | 3C 00 | uint16_t | 0x003C (60) | size of referring table
+0x000A | 04 00 | VOffset16 | 0x0004 (4) | offset to field `far_vector` (id: 0)
+0x000C | 10 00 | VOffset16 | 0x0010 (16) | offset to field `a` (id: 1)
+0x000E | 14 00 | VOffset16 | 0x0014 (20) | offset to field `far_string` (id: 2)
+0x0010 | 24 00 | VOffset16 | 0x0024 (36) | offset to field `big_bool_vector` (id: 3)
+0x0012 | 2C 00 | VOffset16 | 0x002C (44) | offset to field `big_vector` (id: 4)
+0x0014 | 20 00 | VOffset16 | 0x0020 (32) | offset to field `near_string` (id: 5)
+0x0016 | 00 00 | VOffset16 | 0x0000 (0) | offset to field `nested_root` (id: 6) <null> (Vector64)
+0x0018 | 00 00 | VOffset16 | 0x0000 (0) | offset to field `far_struct_vector` (id: 7) <null> (Vector)
+0x001A | 34 00 | VOffset16 | 0x0034 (52) | offset to field `big_struct_vector` (id: 8)

root_table (RootTable):
+0x1C | 14 00 00 00 | SOffset32 | 0x00000014 (20) Loc: 0x08 | offset to vtable
+0x20 | D0 00 00 00 00 00 00 00 | UOffset64 | 0x00000000000000D0 (208) Loc: 0xF0 | offset to field `far_vector` (vector)
+0x28 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x2C | D2 04 00 00 | uint32_t | 0x000004D2 (1234) | table field `a` (Int)
+0x30 | 8C 00 00 00 00 00 00 00 | UOffset64 | 0x000000000000008C (140) Loc: 0xBC | offset to field `far_string` (string)
+0x38 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x3C | 40 00 00 00 | UOffset32 | 0x00000040 (64) Loc: 0x7C | offset to field `near_string` (string)
+0x40 | 70 00 00 00 00 00 00 00 | UOffset64 | 0x0000000000000070 (112) Loc: 0xB0 | offset to field `big_vector` (vector64)
+0x48 | 08 00 00 00 00 00 00 00 | UOffset64 | 0x0000000000000008 (8) Loc: 0x50 | offset to field `big_struct_vector` (vector64)
+0x001C | 16 00 00 00 | SOffset32 | 0x00000016 (22) Loc: 0x0006 | offset to vtable
+0x0020 | E8 00 00 00 00 00 00 00 | UOffset64 | 0x00000000000000E8 (232) Loc: 0x0108 | offset to field `far_vector` (vector)
+0x0028 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x002C | D2 04 00 00 | uint32_t | 0x000004D2 (1234) | table field `a` (Int)
+0x0030 | A4 00 00 00 00 00 00 00 | UOffset64 | 0x00000000000000A4 (164) Loc: 0x00D4 | offset to field `far_string` (string)
+0x0038 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x003C | 48 00 00 00 | UOffset32 | 0x00000048 (72) Loc: 0x0084 | offset to field `near_string` (string)
+0x0040 | 78 00 00 00 00 00 00 00 | UOffset64 | 0x0000000000000078 (120) Loc: 0x00B8 | offset to field `big_bool_vector` (vector64)
+0x0048 | 80 00 00 00 00 00 00 00 | UOffset64 | 0x0000000000000080 (128) Loc: 0x00C8 | offset to field `big_vector` (vector64)
+0x0050 | 08 00 00 00 00 00 00 00 | UOffset64 | 0x0000000000000008 (8) Loc: 0x0058 | offset to field `big_struct_vector` (vector64)

vector64 (RootTable.big_struct_vector):
+0x50 | 02 00 00 00 00 00 00 00 | uint64_t | 0x0000000000000002 (2) | length of vector (# items)
+0x58 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `[0].a` of 'LeafStruct' (Int)
<4 regions omitted>
+0x70 | 33 33 33 33 33 33 22 40 | double | 0x4022333333333333 (9.1) | struct field `[1].b` of 'LeafStruct' (Double)
+0x0058 | 02 00 00 00 00 00 00 00 | uint64_t | 0x0000000000000002 (2) | length of vector (# items)
+0x0060 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `[0].a` of 'LeafStruct' (Int)
+0x0064 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x0068 | D9 CE F7 53 E3 A5 0B 40 | double | 0x400BA5E353F7CED9 (3.456) | struct field `[0].b` of 'LeafStruct' (Double)
+0x0070 | 4E 00 00 00 | uint32_t | 0x0000004E (78) | struct field `[1].a` of 'LeafStruct' (Int)
+0x0074 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x0078 | 33 33 33 33 33 33 22 40 | double | 0x4022333333333333 (9.1) | struct field `[1].b` of 'LeafStruct' (Double)

padding:
+0x78 | 00 00 00 00 | uint8_t[4] | .... | padding
+0x0080 | 00 00 00 00 | uint8_t[4] | .... | padding

string (RootTable.near_string):
+0x7C | 2F 00 00 00 | uint32_t | 0x0000002F (47) | length of string
+0x80 | 74 68 69 73 20 69 73 20 | char[47] | this is | string literal
+0x88 | 61 20 6E 65 61 72 20 73 | | a near s
+0x90 | 74 72 69 6E 67 20 77 68 | | tring wh
+0x98 | 69 63 68 20 68 61 73 20 | | ich has
+0xA0 | 61 20 33 32 2D 62 69 74 | | a 32-bit
+0xA8 | 20 6F 66 66 73 65 74 | | offset
+0xAF | 00 | char | 0x00 (0) | string terminator
+0x0084 | 2F 00 00 00 | uint32_t | 0x0000002F (47) | length of string
+0x0088 | 74 68 69 73 20 69 73 20 | char[47] | this is | string literal
+0x0090 | 61 20 6E 65 61 72 20 73 | | a near s
+0x0098 | 74 72 69 6E 67 20 77 68 | | tring wh
+0x00A0 | 69 63 68 20 68 61 73 20 | | ich has
+0x00A8 | 61 20 33 32 2D 62 69 74 | | a 32-bit
+0x00B0 | 20 6F 66 66 73 65 74 | | offset
+0x00B7 | 00 | char | 0x00 (0) | string terminator

vector64 (RootTable.big_bool_vector):
+0x00B8 | 07 00 00 00 00 00 00 00 | uint64_t | 0x0000000000000007 (7) | length of vector (# items)
+0x00C0 | 01 | uint8_t | 0x01 (1) | value[0]
+0x00C1 | 00 | uint8_t | 0x00 (0) | value[1]
+0x00C2 | 01 | uint8_t | 0x01 (1) | value[2]
+0x00C3 | 01 | uint8_t | 0x01 (1) | value[3]
+0x00C4 | 00 | uint8_t | 0x00 (0) | value[4]
+0x00C5 | 00 | uint8_t | 0x00 (0) | value[5]
+0x00C6 | 01 | uint8_t | 0x01 (1) | value[6]

vector64 (RootTable.big_vector):
+0xB0 | 04 00 00 00 00 00 00 00 | uint64_t | 0x0000000000000004 (4) | length of vector (# items)
+0xB8 | 05 | uint8_t | 0x05 (5) | value[0]
<2 regions omitted>
+0xBB | 08 | uint8_t | 0x08 (8) | value[3]
+0x00C8 | 04 00 00 00 00 00 00 00 | uint64_t | 0x0000000000000004 (4) | length of vector (# items)
+0x00D0 | 05 | uint8_t | 0x05 (5) | value[0]
+0x00D1 | 06 | uint8_t | 0x06 (6) | value[1]
+0x00D2 | 07 | uint8_t | 0x07 (7) | value[2]
+0x00D3 | 08 | uint8_t | 0x08 (8) | value[3]

string (RootTable.far_string):
+0xBC | 2E 00 00 00 | uint32_t | 0x0000002E (46) | length of string
+0xC0 | 74 68 69 73 20 69 73 20 | char[46] | this is | string literal
+0xC8 | 61 20 66 61 72 20 73 74 | | a far st
+0xD0 | 72 69 6E 67 20 77 68 69 | | ring whi
+0xD8 | 63 68 20 68 61 73 20 61 | | ch has a
+0xE0 | 20 36 34 2D 62 69 74 20 | | 64-bit
+0xE8 | 6F 66 66 73 65 74 | | offset
+0xEE | 00 | char | 0x00 (0) | string terminator
+0x00D4 | 2E 00 00 00 | uint32_t | 0x0000002E (46) | length of string
+0x00D8 | 74 68 69 73 20 69 73 20 | char[46] | this is | string literal
+0x00E0 | 61 20 66 61 72 20 73 74 | | a far st
+0x00E8 | 72 69 6E 67 20 77 68 69 | | ring whi
+0x00F0 | 63 68 20 68 61 73 20 61 | | ch has a
+0x00F8 | 20 36 34 2D 62 69 74 20 | | 64-bit
+0x0100 | 6F 66 66 73 65 74 | | offset
+0x0106 | 00 | char | 0x00 (0) | string terminator

vector (RootTable.far_vector):
+0xF0 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
+0xF4 | 01 | uint8_t | 0x01 (1) | value[0]
+0xF5 | 02 | uint8_t | 0x02 (2) | value[1]
+0xF6 | 03 | uint8_t | 0x03 (3) | value[2]
+0x0108 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
+0x010C | 01 | uint8_t | 0x01 (1) | value[0]
+0x010D | 02 | uint8_t | 0x02 (2) | value[1]
+0x010E | 03 | uint8_t | 0x03 (3) | value[2]
Binary file modified tests/64bit/test_64bit.bfbs
Binary file not shown.
Binary file modified tests/64bit/test_64bit.bin
Binary file not shown.
14 changes: 9 additions & 5 deletions tests/64bit/test_64bit.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ table RootTable {
// A normal 32-bit sized string, that could be very far away (64-bit address).
far_string:string (offset64);

// a big vector of bool, that could be very far away (64-bit address).
// tests vector<bool> specialization with 64-bit offsets.
big_bool_vector:[ubyte] (vector64);

// A big 64-bit sized vector, that could be very far away (64-bit address).
big_vector:[ubyte] (vector64);

Expand All @@ -27,19 +31,19 @@ table RootTable {
// A big 64-bit sized vector that is a nested flatbuffers (64-bit address).
nested_root:[ubyte] (vector64, nested_flatbuffer: "RootTable");

// A normal 32-bit size vector of structs, that could be very far away
// A normal 32-bit size vector of structs, that could be very far away
// (64-bit address)
far_struct_vector:[LeafStruct] (offset64);

// A big 64-bit size vector of structs that could be very far away
// A big 64-bit size vector of structs that could be very far away
// (64-bit address)
big_struct_vector:[LeafStruct] (vector64);

// A normal 32-bit size vector of tables. Currently 64-bit vectors don't
// A normal 32-bit size vector of tables. Currently 64-bit vectors don't
// support tables as it would require serializing a table (32-bit) before the
// vector (64-bit), which is not allowed.
//
// This demonstrates how you could have many vectors in the buffer, by
// This demonstrates how you could have many vectors in the buffer, by
// effectively having a vector of 64-bit vectors. The IDL doesn't support
// nested vecotrs (e.g.: [[type]] ), so going through a wrapper table allows
// this.
Expand All @@ -50,4 +54,4 @@ table RootTable {
forced_aligned_vector:[ubyte] (vector64, force_align:32);
}

root_type RootTable;
root_type RootTable;
11 changes: 10 additions & 1 deletion tests/64bit/test_64bit.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
"a": 1234,
"far_string": "this is a far string which has a 64-bit offset",
"big_vector": [5, 6, 7, 8],
"big_bool_vector": [
1,
0,
1,
1,
0,
0,
1
],
"near_string": "this is a near string which has a 32-bit offset",
"big_struct_vector": [
{
"a": 12,
"b": 3.456
},
{
{
"a": 78,
"b": 9.10
}
Expand Down
Loading
Loading