Dulaj Disanayaka
Dulaj Disanayaka

Reputation: 89

flatbuffers::Table* to buffer_pointer

Consider this scenario. I'm creating a struct(flatbuffers::Table) using CreateXXX generated code. This creates the struct on the FlatBuffer buffer and gives me the offset. Then I can get the memory block with GetBufferPointer() and transfer it. Reversely, if I get a memory block, I can use GetXXX to get my struct(flatbuffers::Table) from that.

But after I get this struct, if I need to serialize it again, how can I do it? After the serialization, I should be able to transfer that data and do GetXXX on that data like before.

flatbuffers::Parser parser;
parser.Parse(schema.c_str());
parser.SetRootType("license");
parser.Parse(j.c_str());

auto* buf = parser.builder_.GetBufferPointer();
auto li = flatbuffers::GetRoot<license>(buf);

std::cout << "ID: " << li->id()->c_str() << " Rand: " << li->rand()->c_str() << " Secret: " << li->secret()->c_str() << std::endl;

uint8_t* buf2 = ????????????
auto li2 = flatbuffers::GetRoot<license>(buf2);

std::cout << "ID: " << li2->id()->c_str() << " Rand: " << li2->rand()->c_str() << " Secret: " << li2->secret()->c_str() << std::endl;

Upvotes: 1

Views: 1310

Answers (1)

Aardappel
Aardappel

Reputation: 6074

The obvious answer is that you keep the original buffer pointer (and size) around. Then you can "re-serialize" it by just writing out the existing buffer.

There is a function GetBufferStartFromRootPointer if you really must use just the root (li in your example).

Upvotes: 1

Related Questions