ProtoBuf中的反射

2,607 阅读2分钟

点击上方蓝色文字“后端技术小黑屋”,关注茄子拯救世界的公众号吧~


同时写Python和C++的程序员,往往会觉得C++写起来很累。(所谓“累守恒定律”: 程序执行时的累 + 程序员写代码时的累 = 恒定的累^_^)

在处理ProtoBuf Message数据时,经常会有这样的需求,根据一个输入的字符串,找到Message中对应属性的取值;或者根据输入的字符串和一个值,设置Message中对应属性的取值。

这种需求放在Python中,往往直接通过getattr/setattr就能一步搞定。但是在C++中,我们不得不借助于ProtoBuf的反射机制。

这里,需要介绍如下几个概念:

google :: protobuf :: Message

Message是protobuf中的基本类型,protobuf中所有自定义对象都继承自Message。

通过Message我们可以获得Message的Descriptor和Reflection。

google :: protobuf :: Descriptor

Descriptor正如其名,是对Message的描述,包括字段个数,所有字段的描述等等。

通过Descriptor,我们可以获得一个字段的FieldDescriptor。

google :: protobuf :: Reflection

Reflection就是具体执行相关反射操作,比如当拿到了Message的FieldDescriptor,就可以通过Reflection来读写这个字段。

google :: protobuf :: FieldDescriptor

FieldDescriptor是用于描述字段的,比如字段的类型、名字、修饰符(repeated/required/optional)等。

code snippet

有了上面的这四大金刚,根据输入的字符串,动态的从Message中获得属性的值,可以用以下这段代码片段搞定:

bool GetInt32FieldValue(const google::protobuf::Message& message,
                        const std::string &field_name,
                        int32_t* value) {
  if (value == NULL) {
    return false;
  }
  const Reflection *reflection = message.GetReflection();
  const Descriptor *descriptor = message.GetDescriptor();
  const FieldDescriptor *field = descriptor->FindFieldByName(field_name);
  if (field->cpp_type() != FieldDescriptor::CPPTYPE_INT32) {
    return false;
  }
  if (field->is_repeated()) {
    return false;
  }
  *value = reflection->GetInt32(message, field);
  return true;
}

这里只是示意了获取简单类型的方式,获取Repeated类型可以用Reflection的GetRepeatedInt32函数;而获取嵌套的Message则可以用Reflection的GetMessage函数。

 


推荐阅读:

线程数与多核CPU的关系

踩坑记:临界区内要小心

protobuf中set_allocated_xxx排雷


题图:mambeau

授权:CC0协议