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
protobuf 的enum与string转换的更多相关文章
- 在C#中将String转换成Enum:
一: 在C#中将String转换成Enum: object Enum.Parse(System.Type enumType, string value, bool ignoreCase); 所以,我 ...
- C# Enum,Int,String的互相转换 [转]
C# Enum,Int,String的互相转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名 ...
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...
- string转换成color转
string转换成color string col = "#FF8400"; this.BackColor = System.Draw ...
- C# 中怎么将string转换成int型
int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...
- JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
<pre name="code" class="java"></pre><pre name="code" cl ...
- string 转换char类型
将string转换成char类型 const char *c = string.c_str() char转换string char *c_name = "char" string ...
- Java中String转换Double类型 Java小数点后留两位
Java中String转换Double类型 double num1 = 0.0; String qq = "19.987"; num1 = Double.valueOf(qq.to ...
- android 中List转换String,String转换List 改进版本
原来博客地址http://blog.csdn.net/qq7342272/article/details/6830907 使用原作者贴的代码不是很好用,不能正常运行,所以我稍微改进了一下,特来分享给大 ...
随机推荐
- 易语言加壳SDK(宏标记)
转载 http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece76310508c31490797634b87834e29938448e435061 ...
- G - Traffic
vin is observing the cars at a crossroads. He finds that there are n cars running in the east-west d ...
- JuJu团队11月29号工作汇报
JuJu团队11月29号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 生成所有mini batch, 支持不同batch_size 优化代码 熟悉julia 婷婷 ...
- 配置thinkphp对mysql线上线下切换不同环境的配置 - (mysql-thinkphp) (1)
1.先打印出配置项的信息,在Index控制器下面的index->index里面设置 namespace app\index\controller; class Index { public fu ...
- 实训30 延时中断组织块0B20仿真
实训30 延时中断组织块的仿真试验 问题1 系统功能块SFC中提供了一些查询中断状态字的指令,举例说明 例如 SF34 "QRY_DINT" 用来查询 "延时中断&q ...
- 四十、SAP中CASE语句用法
一.上代码 二.选择内容 三.输出 四.我们选择一个其他的值 五.查看输出
- list实体数据分组
比如查询获取了60000条数据进行批量插入数据库,一次直接插入6万可能不是很好,可以将6万条数据按照5000分成几组,每组批量插入5000条 List<T> list = new List ...
- Python MySQL 插入表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- hdu 1950 Bridging signals 求最长子序列 ( 二分模板 )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU - 4576 Robot(概率dp+滚动数组)
题意:所有的格子围成一个圈,标号为1~n,若从格子1出发,每次指令告知行走的步数,但可能逆时针也可能顺时针走,概率都是1/2,那么问走了m次指令后位于格子l~r(1≤l≤r≤n)的概率. 分析: 1. ...