fluter
fluter

Reputation: 13846

flatbuffer unio only generate enum

I have a flatbuffer schema file:

namespace market;

enum MessageType : ubyte {
    Null = 0,
    MarketDate = 1,
    MarketInfo,
}

table MarketDate {
    date           : string;
}

table MarketInfo {
        marketID           : string;
        marketName         : string;
        marketType         : byte;
}

union MessageData {
    marketDate           :     MarketDate,
    marketInfo           :     MarketInfo,
}

table Message {
    type        : MessageType;
    data        : MessageData;
}

root_type Message;

When I generate the access files for c++, with

flatc -c --scoped-enums market.fbs

The output does not have any code to construct and store MessageData, it only generates an enum. The generated market_generated.h is at http://sprunge.us/lTfX0H.

I read from the internet that union would generate at least something like: struct MessageDataUnion and createMessageDataDirect or similar, but in my case it generate nothing like this, is there anything wrong with my code?

Upvotes: 0

Views: 497

Answers (1)

Aardappel
Aardappel

Reputation: 6074

You create the union by initializing the data field in Message. This actually generates two fields in C++, data and data_type. You thus don't need the type field nor the MessageType enum.. that is already included in the union.

You should be able to call something like fbb.Finish(CreateMessage(fbb, MessageData_marketDate, CreateMarketDate(fbb, fbb.CreateString("my date")))) (not tested).

Upvotes: 1

Related Questions