I want to use flatbuffer to send events, which contain different data, over TCP (Using ZeroMQ). Therefore, I am using a Union.
// event.fbs
namespace event;
table ByteArray {
bytes:[byte];
}
table OtherData {
id:uint;
value:uint;
}
union EventData {
ByteArray,
OtherData,
String:string
}
table Event {
name:string (key);
timestamp:ulong = -1;
data:EventData;
}
root_type Event;
In my C++-class I want to create new events and transfer them to my Publisher-class that sends out the events via ZeroMQ. Is there a nice or common way of doing this? I was thinking about something like this:
mPublisher.publishEvent(event::Event("event1", 0, "dataString"));
mPublisher.publishEvent(event::Event("event2", 1, byteArray));
The example above is not working because there is no such constructor. Is there a nice way of creating multiple events with different data? How am I supposed to pass these flatbuffer-events to another class like my publisher? Should I pass the flatbuffer or the event-offset?
You just need to use the actual constructors provided, so it will look something like:
event::CreateEvent(fbb, "event1", 0, fbb.CreateString("dataString"));
There is also an event::CreateByteArray
etc. See the generated code, or the tutorial.
event::CreateEvent
returns an offset into an unfinished FlatBuffer, so is generally not suitable for passing to non-FlatBuffer functions. You'll want to call fbb.Finish()
on that offset, then pass the resulting buffer to other functions (again, see the tutorial).