'Add or modify a nullptr Vector from a partially filled flatbuffer?
This builds off of the monster schema example. If I partially fill a flatbuffer such as in the official test.cpp.
Relevant lines also copied below
// Create a mostly empty FlatBuffer.
flatbuffers::FlatBufferBuilder nested_builder;
auto nmloc = CreateMonster(nested_builder, nullptr, 0, 0,
nested_builder.CreateString("NestedMonster"));
FinishMonsterBuffer(nested_builder, nmloc);
How can I modify the inventory part of the schema from nested_builder after already calling FinishMonsterBuffer(nested_builder, nmloc);?
Things I've tried (related to test.cpp):
std::string bfbsfile;
bool found = flatbuffers::LoadFile( "./monster_schema.bfbs", true, &bfbsfile );
const reflection::Schema& schema = *reflection::GetSchema( bfbsfile.c_str() );
auto root_table = schema.root_table();
auto fields = root_table->fields();
// The above works fine I've tested the fields and verified the schema.
// I'm unsure of the code below.
// First we put the FlatBuffer inside an std::vector.
std::vector<uint8_t> resizingbuf(
nested_builder.GetBufferPointer(),
nested_builder.GetBufferPointer() + nested_builder.GetSize());
// Get the root.
// This time we wrap the result from GetAnyRoot in a smartpointer that
// will keep rroot valid as resizingbuf resizes.
auto rroot = flatbuffers::piv(flatbuffers::GetAnyRoot(resizingbuf.data()),
resizingbuf);
// Now lets try to extend a vector by 100 elements (0 -> 110).
auto &inventory_field = *fields->LookupByKey("inventory");
// I don't think this works either if it doesn't already exist.
auto rinventory = flatbuffers::piv(
flatbuffers::GetFieldV<uint8_t>(**rroot, inventory_field), resizingbuf);
// This line silently fails probably because *rinventory is probably a nullptr
// but I don't know how to set it after having already
// called FinishMonsterBuffer(nested_builder, nmloc);.
flatbuffers::ResizeVector<uint8_t>(schema, 110, 50, *rinventory,
&resizingbuf);
// Try to print it but prints nothing.
// printf("%hhu", rinventory->Get(10));
Apologies in advance for the title I'm not quite sure how to phrase the exact problem or question. Let me know if more context is needed or part of the question doesn't make sense.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
