如何使用 protobuf 描述符来读取枚举
How to use protobuf descriptor to read enums
假设我有一个 .proto 文件:
message Foo {
optional int32 x = 1;
enum y {
MOBILE = 0;
HOME = 1;
}
optional string z = 3;
}
然后我有这个C++代码,它打印所有类型:
const Reflection *refl = Foo.GetReflection();
const Descriptor *desc = Foo.GetDescriptor();
int fieldCount = desc->field_count();
for(int i=0;i<fieldCount;i++){
const FieldDescriptor *field = desc->field(i);
cout << field->name().c_str() << " the type is "
<<field->type_name()<< ": Type Number "<< field->type() <<endl;
if(field->type()==FieldDescriptor::TYPE_ENUM){
//do something
}
然后输出为:
x the type is int32: Type Number 5
z the type is string: Type Number 9
如输出所示,枚举被跳过,我如何让字段描述符也解析枚举?
您没有枚举类型的字段,您只定义了一个类型。因此,对字段的迭代不会产生与枚举相关的任何内容。
如果添加给定类型的字段,您将在那里看到您的枚举。
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- 如何使用 protobuf 描述符来读取枚举
- 读取协议缓冲区中枚举扩展的值
- 从.cpp文件(C++)中的头文件读取枚举
- 如何逐行枚举从文件中读取的文本,并找到 C 中字符和单词最多的行
- 将存储在多映射中的私有枚举作为值读取
- 从控制台读取:运算符>>用于模板类中的枚举
- 试图读取枚举状态时c++枚举错误
- 如何将从文件读取的字符串与枚举值相关联