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转换的更多相关文章

  1. 在C#中将String转换成Enum:

    一:  在C#中将String转换成Enum: object Enum.Parse(System.Type enumType, string value, bool ignoreCase); 所以,我 ...

  2. C# Enum,Int,String的互相转换 [转]

    C# Enum,Int,String的互相转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名 ...

  3. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  4. string转换成color转

    string转换成color             string col = "#FF8400";            this.BackColor = System.Draw ...

  5. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  6. JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类

    <pre name="code" class="java"></pre><pre name="code" cl ...

  7. string 转换char类型

    将string转换成char类型 const char *c = string.c_str() char转换string char *c_name = "char" string ...

  8. Java中String转换Double类型 Java小数点后留两位

    Java中String转换Double类型 double num1 = 0.0; String qq = "19.987"; num1 = Double.valueOf(qq.to ...

  9. android 中List转换String,String转换List 改进版本

    原来博客地址http://blog.csdn.net/qq7342272/article/details/6830907 使用原作者贴的代码不是很好用,不能正常运行,所以我稍微改进了一下,特来分享给大 ...

随机推荐

  1. Eclipse新建Maven中创建src文件夹报The folder is already a source folder错误解决办法

    问题: 解决办法:右击项目->Build Path->Configure Build Path选择(missing)文件夹remove,然后重新New Source Folder

  2. Day 26:Dom4j修改xml

    Dom4j:Writing a document to a file import java.io.File; import java.io.FileOutputStream; import org. ...

  3. Golang函数-不定参函数

    Golang函数-不定参函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  4. ORA-22813 ORA-06512

    ORA-22813:操作数值超出系统限制. 原因:   对象或集合值太大.SORT上下文中值的大小可能超过30k,或者可用内存的大小可能太大. 操作:  选择其他值并重试该操作. ORA-06512错 ...

  5. 对plotTree的解释

    1.>>>a = 1/2/2   >>>a >>>0.25 2.def plotMidText(cntrPt,parentPt,txtString ...

  6. Django——优美的Path()函数

    path( )作用:解析URL地址 path( ) 标准语法: (<>为必须的参数,[]为可选参数) path(<route>, <view>, [name=Non ...

  7. Java 统计整数二进制中1的个数

    package cookie; public class CountBinary_1 { public static void main(String[] args) { System.out.pri ...

  8. Python 简单统记Log 日记 下次用:python的内置logging模块 easy

    环境 win7  先来new一点log 日记   日记包含    "reason=", "error="  两个log级别 存放在D盘下得LOG目录下 先来 生 ...

  9. caffe 官方demo python api

    Jupyter https://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb 涉及: - ...

  10. ZOJ - 3123 Subsequence (滑动窗口)

    题意:给定N个数,求和大于等于S的最短连续子序列的长度. 分析:滑动窗口即可.两种写法. 1. #include<cstdio> #include<cstring> #incl ...