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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM