2

Is it possible to mutate array of tables when using C++ (not object) API without recreating entire flatbuffer?

Here is an example of my schema

table Document {
  root_layer:Layer;
  bitmaps:[Bitmap];
  subdocuments:[Document];
}

table Layer {
  id:int;
  sublayers:[Layer];
}

...

Interesting here part is document → root_layer → sublayers

Let's say I want to modify entire sublayers array: re-write it completely, not just to replace element at index. Mutability API as far as I saw only allows to replace element at index, not to replace entire array with a new array, right?

Today I end up recreating entire document copying all the fields from the original one and once I get to the nested sublayers I replace it with new array

Is there a better way of doing it?

Sash Zats
  • 5,376
  • 2
  • 28
  • 42

1 Answers1

1

No, not really.

Using the reflection API, it is possible to append new tables to an existing buffer, and then resize a vector such as sublayers, and make the elements point to new (or old) tables. But there is currently no way to remove old tables, and the API is pretty painful to use, and resizing is slow (in-place mutation of complex flat structures is hard).

Or you could create a new buffer from scratch, and if you want to save yourself writing manual copying code for parts that you're not modifying, you could again use the reflection API (CopyTable) or the object API to copy the remainder.

If you find that you're frequently wanting to change one part of a buffer but not the rest, maybe.. they should be in 2 different buffers?

If static data has to sit in the same buffer as dynamic data for some reason, one approach is to put the static data in a nested_flatbuffer, that way you can transfer it to a new buffer with a single memcpy.

I'm afraid using the object API is your best bet on average.

Aardappel
  • 5,559
  • 1
  • 19
  • 22