gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:52 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com