如何在运行时从平面缓冲区获取数据值和数据类型
How to get the data values and data type from flatbuffer at runtime
我有一个像这样定义的平面缓冲区模式
enum Payload : uint8 { BLOB, STRPARAM } //type of payload
table Header
{
payload: Payload;
sender: string;
module: string;
command: string;
parameter: string;
timestamp: string;
status: string; // used for reply from Daemon: success or fail
message: string; // Status message, to get more info when request fails
}
table BlobPacket
{
value: [byte];
}
table StrParamPacket
{
value1: string; // e.g. "4"
value2: string; // e.g. "5.7"
}
union PacketData
{
BlobPacket, StrParamPacket
}
table DaemonRequest
{
header:Header; //Tells information about the packet
data:PacketData; //Actual data
}
root_type DaemonRequest;
使用以下代码打包为请求
flatbuffers::FlatBufferBuilder* _builder = nullptr;
DaemonRequestT request;
HeaderT header;
BlobPacketT blobPacket;
StrParamPacketT strParamPacket;
header.payload = 0;//str param
header.sender = sender;
header.module = module;
header.command = command;
header.parameter = parameter;
auto headerOffset = CreateHeader(*_builder, &header);
strParamPacket.value1 = value1;
strParamPacket.value2 = value2;
auto strParamPacketOffset = CreateStrParamPacket(*_builder, &strParamPacket);
auto req = CreateDaemonRequest(*_builder, &header, PacketData::StrParamPacket, strParamPacketOffset.Union());
然后将此请求发送到服务器,服务器解压缩请求 用
auto req= UnPackDaemonRequest(receivedBuffer);
通过此请求,我可以使用req->header->command
等命令获取标头详细信息。 但是,我在获取联合表的data_type和数据时遇到问题。
我尝试按照平面缓冲区教程进行操作,但是没有太多文档如何在解压缩后获取数据。 生成的文件 -> "https://pastebin.com/zLEyd8BE" (使用 --gen-object-api --scoped-enums --cpp)
我尝试使用它获取数据
auto union_type = req.data_type(); //error
if (union_type == PacketData::StrParamPacket)
{
auto strParamPacket = static_cast<const StrParamPacket*>(req->data());//error
auto command = req->header->command; //std::sting
auto parameter = req->header->parameter; //std::string
auto message = req.get()->header->message; //std::string&
auto value1 = strParamPacket.get()->value1; //std::string&
auto value2 = strParamPacket.get()->value2; //std::string&
}
这会导致以下错误
: error: ‘class std::unique_ptr<DaemonRequestT>’ has no member named ‘data_type’; did you mean ‘deleter_type’?
auto union_type = req.data_type(); //error
^~~~~~~~~
deleter_type
../Daemon/Daemon.cpp:155:76: error: no match for call to ‘(PacketDataUnion) ()’
auto strParamPacket = static_cast<const StrParamPacket*>(req->data());//error
(删除了以前的答案,因为这个问题已经澄清了)。
FlatBuffers有 2 个 API:默认的、高效的、FlatBuffers 的使用方式 API,以及一个可选的"对象 API",只有在您特殊需要时才应该使用。
您主要使用对象 API,尽管您尝试访问联合,就好像它是基本 API 一样。从错误中可以看出,req
属于std::unique_ptr<DaemonRequestT>
类型(不是DaemonRequest
),它有一个类型PacketDataUnion
的data
成员,而AsStrParamPacket
又具有像 这样的 covenience 方法,它为您进行强制转换(如果类型错误,则返回 nullptr)。
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 如何在 C++ 中获取中继器的委托数据?
- 从 Rcpp Catch 测试中获取有关"inst/extdata"的数据
- C++ - 忽略并从其他文件获取数据
- 如何获取C++字符数据类型的地址
- 从存储为 Windows 资源 (c++) 的 png 中获取 png 文件数据
- 很好的语法来获取对向量/数组数据的大小引用?
- 如何从网站获取数据并将其传输到数据库?
- 获取文件数据预处理器宏
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 如何在新的派生对象中获取基本对象的数据?
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 如何使用CAPL的诊断功能获取CAN传输的数据(256字节)?
- 获取 R 数据帧的内存地址
- 当 c++ 需要一种数据类型并获取另一种数据类型时会发生什么?
- 从更改日志获取最新的 USN 数据
- 套接字:在没有memcpy的情况下,使用recvfrom将UDP数据获取到字对齐的缓冲区
- 如何将控制台输出数据获取到命令提示符下
- 将零mq消息数据获取到std::vector<char>
- 从指定坐标[x, y]处的位图数据获取像素颜色