zoukankan      html  css  js  c++  java
  • protobuf 的enum与string转换

    c/c++ enum 介绍

    说起c/c++ 的enum,比起python 真的是方便简洁

    enum type{
        type1 = 0,
        type2
    }
    

    enum的元素对应的int 默认从0 开始依次增加, 除非手动指定起始值。

    int val = type1; 
    assert(val == 0)
    

    enum 内的元素是全局的,意味着在其它地方直接使用 type type_1 = type1

    C++ 11 引入 enum class, 这样里面的元素不再是全局了

    enum class int32_t type{
        type1 = 0,
        type2
    }
    

    这样在使用的时候必须是 type type_1 = type::type1, 并且可以指定底层类型例如uint8 等。
    并且c++11 种enum 不能隐式转换了,必须强转

    type type_1 = type::type1;
    int32_t type_impliticy_convert = type_1;  // wrong
    int32_t type_impliticy_convert = static_cast<int32_t >(type_1);  // ok
    

    enum方便,但是如果要转换成string很麻烦,必须一个个匹配。而且与int 互转而且还要考虑索引边界

    引出正题,protocbuf 提供的enum 就比较方便了

    • protobuf 具体详情就不解释,有兴趣自己看官方文档。
      从protobuf 生成的pb.cc 文件可以看出,除了提供一个索引验证函数IsValid(int value) 可以检查边界。
      另外还提供了一个descriptor() ;
    // Force running AddDescriptors() at dynamic initialization time.
    static bool dynamic_init_dummy_XXX_2eproto = (  ::PROTOBUF_NAMESPACE_ID::internal::AddDescriptors(&descriptor_table_XXX_2eproto), true);
    namespace XXX {
    const ::PROTOBUF_NAMESPACE_ID::EnumDescriptor* XXX_descriptor() {
      ::PROTOBUF_NAMESPACE_ID::internal::AssignDescriptors(&descriptor_table_XXX_2eproto);
      return file_level_enum_descriptors_XXX_2eproto[0];
    }
    bool JobState_IsValid(int value) {
      switch (value) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
          return true;
        default:
          return false;
      }
    }
    
    

    descriptor() 返回一个google::protobuf::EnumDescriptor的指针,利用这个指针可以方便得得到enum的litteral 值和offset 例如

    auto descriptor = XXX:: xxx_descriptor();
    auto string_1 = descriptor->FindValueByNumber(1)->value();
    auto number_1 = descriptor->FindValueByName("type1")->number();
    

    可以得到1的字面string, 在项目如果经常需要字符串和enum 转换可以考虑直接使用protobuf的内置enum

  • 相关阅读:
    PSE Access Service
    The JXTA Migration
    JXSE 2.5 : What's Cool #6 PeerGroup Executor and ScheduledExcutor
    JXTA Kitchen
    LookupListener中的resultChanged方法是在EDT中执行么?
    同一台机器启动两个结点时的端口冲突问题
    (转)OpenSSL中对称加密算法的统一接口
    关于“未能加载文件或程序集“System.Core, Version=3.5.0.0
    暗香浮动的夜晚
    java xml序列化与反序列化
  • 原文地址:https://www.cnblogs.com/hustcpp/p/12300014.html
Copyright © 2011-2022 走看看