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();
enum函数auto number_1 = descriptor->FindValueByName("type1")->number();
可以得到1的字⾯string,在项⽬如果经常需要字符串和enum 转换可以考虑直接使⽤protobuf的内置enum