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

posted @   SnailRush  阅读(7908)  评论(0编辑  收藏  举报
编辑推荐:
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
阅读排行:
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现
点击右上角即可分享
微信分享提示