我想在一个未知的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/