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 使用原作者贴的代码不是很好用,不能正常运行,所以我稍微改进了一下,特来分享给大 ...
随机推荐
- C#:string、stringBuffer、stringBuilder的区别
好脑子不如烂笔头,总是记不住,记下来吧 依然 搬运 地址: http://blog.csdn.net/qq_28187979/article/details/76607253 ------------ ...
- java 根据值获取枚举对象
关键方法: /** * 值映射为枚举 * * @param enumClass 枚举类 * @param value 枚举值 * @param method 取值方法 * @param <E&g ...
- Codeforces Round #622 (Div. 2) 题解和我的分析
首先下午场非常适合中国人,开心 A 三种食物有个数限制,上菜,每次上菜跟以前的样式不能一样(食物的种类及个数各不相同),且每种食物最多用一次,问最多能上几次 对a,b,c排序,然后枚举上菜种类就可以了 ...
- tornado 获取 路径上的参数
https://www.cnblogs.com/quzq/p/10975766.html class JavaHandler(RequestHandler): #重写RequestHandler中in ...
- Spring Boot2(003):简要回顾“HelloWorld” web 工程
1.注解: @RestController 和 @RequestMapping HelloWorldExample 中的第1个注解 @RestController 是一个被熟知的原型注解(stereo ...
- 六、JavaScript之调用外部JavaScript文件
一.外部JavaScript文件,不需要写<SCRIPT>标签,myScript.js代码如下: 二.index.php代码如下 三.运行效果如下 四.点击之后 myScript.php如 ...
- 046-unset对静态变量无法销毁
<?php function digui() { static $count = 0; echo $count; $count++; unset($count); } digui(); digu ...
- Elasticsearch 修改数据
章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...
- 每天一点点之 taro 框架 - 生命周期 & state
注意:从vue过来的小朋友要注意,taro直接赋值时不会更新组件的,同react一致更新数据必须调用setState方法,例如:this.setState({name:'张三'}) 1.render函 ...
- mac安装vue没有使用权限不足
➜ ~ vue init webpack frontend ⠋ downloading template /usr/local/lib/node_modules/vue-cli/node_modul ...