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

原文链接: https://www.cnblogs.com/hustcpp/p/12300014.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    protobuf 的enum与string转换

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193051

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午6:15
下一篇 2023年2月12日 下午6:15

相关推荐