Nguyen Duc Thanh Hung
Nguyen Duc Thanh Hung

Reputation: 21

How to copy a flatbuffer to other

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 ?

Upvotes: 2

Views: 1576

Answers (1)

Aardappel
Aardappel

Reputation: 6074

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.

Upvotes: 1

Related Questions