c++ - protobuf,如何在我不太了解的 protobuf 消息中遍历所有设置字段?(C++)

标签 c++ protocol-buffers

我想在一个未知的protobuf消息中设置旅行字段。 我试过了

for (int i = 0; i < chk_des->field_count(); ++i) {
    const ::google::protobuf::FieldDescriptor* chk_field = chk_des->field(i);
    (do somethin...)
}

它只是遍历所有领域。我想旅行固定领域。

最佳答案

你检索一个反射对象:

virtual const Reflection * 
    Message::GetReflection() const

Get the Reflection interface for this Message, which can be used to read and modify the fields of the Message dynamically (in other words, without knowing the message type at compile time).

之后你可以使用例如:

virtual bool Reflection::HasField(const Message & message, const FieldDescriptor * field) const = 0

Check if the given non-repeated field is set.

因此请注意,您还需要从代码片段中传递字段描述符。

const FieldDescriptor * 
    Descriptor::field(
        int index) const

Gets a field by index, where 0 <= index < field_count().

These are returned in the order they were defined in the .proto file.

关于c++ - protobuf,如何在我不太了解的 protobuf 消息中遍历所有设置字段?(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51646812/

相关文章:

c++ - 在交叉编译时我必须如何处理 C++ 库?

c++ 将 char* 转换为对象?

c - 二进制包中的转义字符 (0x1b/27) 不会通过 Wi-Fi 发送,并且在传输过程中会损坏消息

ios - Protocol buffer 代码针对 iOS 模拟器进行编译,但不适用于设备

c++ - 我应该如何将线程内部的值复制回主线程?

c++ - 使用字符串进行动态内存分配

java - Protobuf c# 到 java 反序列化多条消息

java - protobuf 2.3.0 和 2.6.0 之间的兼容性

c - 与 C 的二进制兼容性

c++ - 无法弄清楚我的合并排序 C++ 有什么问题