【问题标题】:flatbuffer unio only generate enumflatbuffer unio 只生成枚举
【发布时间】:2019-08-06 06:00:13
【问题描述】:

我有一个平面缓冲区架构文件:

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;

当我为 c++ 生成访问文件时,使用

flatc -c --scoped-enums market.fbs

输出没有任何代码来构造和存储MessageData,它只生成一个枚举。生成的market_generated.h 位于http://sprunge.us/lTfX0H

我从互联网上读到 union 至少会产生类似:struct MessageDataUnioncreateMessageDataDirect 或类似的东西,但在我的情况下它不会产生类似的东西,我的代码有什么问题吗?

【问题讨论】:

    标签: c++ c++11 serialization union flatbuffers


    【解决方案1】:

    您通过初始化Message 中的data 字段来创建联合。这实际上在 C++ 中生成了两个字段,datadata_type。因此,您不需要 type 字段或 MessageType 枚举.. 已经包含在联合中。

    您应该可以拨打类似fbb.Finish(CreateMessage(fbb, MessageData_marketDate, CreateMarketDate(fbb, fbb.CreateString("my date")))) 的电话(未测试)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多