工作中的总结:
Protobuf是一款与平台无关、语言无关、可扩展的序列化结构数据格式。很适合用做数据存储和作为不同应用,不同语言之间进行相互通信的数据交换格式。一个proto文件可以声明多个message,在编译的时候他们将会编译成不同的类。其中每个message中声明字段的类型有required、optional 、repeated三种类型,required声明的字段其值时必须要设置的,optional 声明的字段其值是可选择的,repeated声明的字段其值是可以重复使用的,这里可以理解为c++中的数组。另外,protobuf的灵魂就是它的反射机制,Descriptor类用来描述消息,可以通过该类获取到该消息中字段的个数以及通过字段名获取字段等。FieldDescriptor类用来描述字段,可以通过该类获取字段的一些属性。Reflection类可以对字段的内容进行存取。
附录:
如何遍历整个字段
const ::google::protobuf::Descriptor *descriptor = protobuf.GetDescriptor();
const ::google::protobuf::Reflection *reflection = protobuf.GetReflection();
if (!descriptor || !reflection)
{
return;
}
int counts = descriptor->field_count();
for (int i = 0; i < counts; i++)
{
const ::google::protobuf::FieldDescriptor *fieldDesc = descriptor->field(i);
if (!fieldDesc)
{
continue;
}
if (fieldDesc->type() == ::google::protobuf::FieldDescriptor::TYPE_UINT32)
{
reflection->SetUInt32(&protobuf, fieldDesc, 0);
}
}
3870




被折叠的 条评论
为什么被折叠?



