c++flatbuffers

Can I avoid deserializing subtables when modifying a trailing element of a top-level flatbuffer?


If I am going to modify a flatbuffer, the recommended way is to deserialize the contents and then re-serialize the contents. Fine. But I am hoping there is some way to avoid going all the way to the deepest levels.

I have a table that contains two subtables and a string. I want to modify that string. Is there some way to initialize the builder with the flattened bytes of the first two tables directly lifted out of the existing buffer without recreating them?

I am hoping for something like this:

table Alpha {
   ... contents ...
}
table Beta {
   ... contents ...
}
table Alphabet {
   alpha:Alpha;
   beta:Beta;
   comment:string;
}

Assuming that raw_data is a uint8_t array of flattened form of Alphabet:

const auto* current = GetAlphabet(raw_data);
flatbuffers::FlatBufferBuilder builder;
builder.CreateAlphabetDirect(builder, 
                             current->alpha(), // somehow get the bytes
                             current->beta(),  // for alpha & beta here
                                               // and just copy them over.
                             "NewString");

Solution

  • You can't get just the bytes for a sub-table like Alpha, as the data may be discontinuous, and it may share vtables with the rest of the buffer.

    If you absolutely must do this without re-serializing, there are two options I see: