Closed exhau closed 7 years ago
Yes, unions are intended for this purpose.
You could indeed copy it, from another FlatBufferBuilder
.
It is also possible to without copying, but there is no nice API for that at the moment. You would do:
fbb.Finish(CreateMyRootObject(..));
At this point, you have a complete FlatBuffer in the builder, but you can still prefix it.
auto size = fbb.GetSize();
fbb.StartVector(size, sizeof(uint8_t));
assert(size == fbb.GetSize());
auto vec = fbb.EndVector(size);
The StartVector
won't do anything, since there is nothing to align. You can now finish the buffer a second time.
Thank you. The method that finishes flatbufferbuilder twice works .
A lot of buffers have been defined.
Now I wish to add a type to wrap them. For example, assign a time to each buffer.
table Message { time:long; contentType:string; content:Any; }
It is impossible to add all types to a Union.
One potential solution could be
table Message { time:long; contentType:string; content:[ubyte]; }
The problem is, how to build the message without copy/rebuild after the content(eg. monster) is ready?
Take your sample code as an example,
auto orc = CreateMonster(builder, &position, 150, 80, name, inventory, Color_Red, weapons, Equipment_Weapon, axe.Union());
auto content = builder.CreateVector(... ); // if we do this way, the monster buffer would be copied // it seems that i can manually use StartVector/EndVector(). Not sure there is any issue.
auto msg = CreateMessage(builder, 11111, content);
As for read, I think we may cast it by
auto enty = GetMessage(builder.GetBufferPointer()); auto buf = enty->content(); auto monster = GetMonster(buf->data());
Could you please hint what is the best practice to do this?