2

I have a nested flatbuffer as following:

struct Flat_A {
    // s.t
}

struct Flat_B {
    // s.t
}

struct Flat_C {

    flata : Flat_A

    flatb : Flat_B
}

I want to create flatbuffer of flata : Flat_A first (in a difference flatbufferbuilder with its parent), and then copy this flatbuffer to the parent buffer(Flat_C).

I see function CopyTable, but it seem that it just copy to a NEW flatbuffer, not to an EXIST buffer like Flat_C. Is there any way to do that ?

1 Answers1

1

CopyTable actually works for this purpose, it can copy from an existing buffer into a different builder. But it requires reflection, which is very clumsy, and I'd recommend against using it.

If you really can't construct C in one go, the best alternative is to change the flata field into flata : [ubyte] (nested_flatbuffer: "Flat_A"). You can copy the entire serialized bytes of A into flata, and you can then access it by getting the root of flata (there should be an accessor generated for you that does this automatically). This is actually almost as efficient as having a Flat_A type directly, since it is still accessing this nested buffer in place.

Aardappel
  • 5,559
  • 1
  • 19
  • 22
  • Hi @Aardappel, thank you for your answer. if I change the flata field into flata : [ubyte], but still keep it as a field of flatc, is it possible if I copy flatbuffer of flata into without using CopyTable ? – Nguyen Duc Thanh Hung Feb 02 '21 at 10:35
  • Yes, it's just an array of bytes. You can use `memcpy`. – Aardappel Feb 02 '21 at 16:01