Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

c++ - 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 ?

question from:https://stackoverflow.com/questions/65932935/how-to-copy-a-flatbuffer-to-other

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...