欢迎访问Ningto's博客

Menu
  • 首页
  • 归档
  • 关于
  • 书签
  • 必应壁纸
  • IT聚合
  • 工具
    • 我的工具列表
    • 我的网盘
    • 必应每日壁纸API
    • Html转Markdown
    • 仙尘光标
Menu

protobuf反射

最后更新 2017-09-30 09:50:16   阅读量 3082

Table of Contents

  • 1. 通过proto对象的名字来创建一个对象
  • 2. 通过message初始化和获取成员变量的值

C++本身是不支持反射的,但protobuf可以,下面介绍反射的两种主要用途。

通过proto对象的名字来创建一个对象

google::protobuf::Message* ProtoHelp::createMessage(const std::string &typeName)
{
    google::protobuf::Message *message = nullptr;
    const google::protobuf::Descriptor* desc = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(typeName);
    if (desc) {
        const google::protobuf::Message *prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(desc);
        if (prototype) {
            message = prototype->New();
        }
    }
    return message;
}

通过Message初始化和获取成员变量的值

我们知道Message是基类要给它初始化成员变量一般先转型为具体的子类对象(也就是我们在proto文件中定义的类),这时候就能调用set_xxx或者mutable_xxxx来设置值,反过来直接通过名字就能获取值。

但是,当我们不知道这个Message具体是哪个子类时怎么获取值呢?

通过Message的key获取值

下面举个例子只是获取int类型的值

bool getInt32(google::protobuf::Message& msg, int key, int &out)
{
    const google::protobuf::FieldDescriptor* field = msg.GetDescriptor()->FindFieldByNumber(key);
    if (field && field->type() == google::protobuf::FieldDescriptor::TYPE_INT32 && !field->is_repeated()) {
        out = msg.GetReflection()->GetInt32(msg, field);
        return true;
    }
    return false;
}

通过key给Message设置值

void setInt32(google::protobuf::Message& msg, int key, int value)
{
    const google::protobuf::FieldDescriptor* field = msg.GetDescriptor()->FindFieldByNumber(key);
    if (field && field->type() == google::protobuf::FieldDescriptor::TYPE_INT32 && !field->is_repeated())
    {
        msg.GetReflection()->SetInt32(&msg, field, value);
    }
}

上面只是演示了最简单的类型,其他类型同理。

(转载本站文章请注明作者和出处:泞途 - ningto.com)

下一篇 – 10个React小模式
上一篇 – mongodb部署分片集群

  1. C/C++

toningto@outlook.com

标签云

IOS Qt Life Linux Java MongoDB Boost MQ Node.js Mac C/C++ Tools Design Python Bug Others Javascript Tips React Android Web Mobile Shell Product Go Database Windows

推广链接

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

多谢支持,用了好几年,服务很稳定支持多设备!

其他

文章RSS

Copyright © 2016 Welcome To Ningto Blog | 鄂ICP备17003086号-2