C++ 用于获取枚举值的名字 Post author:xfxia Post published:2023年12月26日 Post category:其他 template < class Child> 02 class EnumBase 03 { 04 protected : 05 //typedef typename Child::Et Et; 06 struct EnumProperty 07 { 08 const char * mName; 09 int mEt; 10 }; 11 12 public : 13 14 static int toEnum( const char * name) 15 { 16 int size = 0; 17 const EnumProperty * pP = Child::getEnumPropertys(size); 18 for ( int iter = 0;iter < size ; ++ iter) 19 { 20 if ( strcmp (pP[iter].mName,name) == 0) 21 return pP[iter].mEt; 22 } 23 return 0; 24 } 25 26 static const char * toString( int et) 27 { 28 int size = 0; 29 const EnumProperty * pP = Child::getEnumPropertys(size); 30 for ( int iter = 0;iter < size ; ++ iter) 31 { 32 if (pP[iter].mEt == et) 33 return pP[iter].mName; 34 } 35 return 0; 36 } 37 }; 38 39 #define TOSTRING(t) #t 40 #define MAP_TO_STRING(ot) TOSTRING(ot),ot 41 42 class DamageType : public EnumBase<DamageType> 43 { 44 public : 45 enum Et{ 46 Wood = 0x01, 47 Stone = 0x02, 48 Metal = 0x04, 49 Ice = 0x08, 50 Other = 0x10, 51 DamageAdditionEnd = 0x20, 52 }; 53 struct x{}; 54 55 static const EnumProperty* getEnumPropertys( int &size) 56 { 57 const static EnumProperty __[] = 58 { 59 {MAP_TO_STRING(Wood)}, 60 {MAP_TO_STRING(Stone)}, 61 {MAP_TO_STRING(Metal)}, 62 {MAP_TO_STRING(Ice)}, 63 {MAP_TO_STRING(Other)}, 64 }; 65 66 size = sizeof (__)/ sizeof (__[0]); 67 return __; 68 } 69 }; 70 int main() 71 { 72 printf ( "%d\n" ,DamageType::toEnum( "Wood" )); 73 printf ( "%d\n" ,DamageType::toEnum( "Stone" )); 74 printf ( "%d\n" ,DamageType::toEnum( "Metal" )); 75 printf ( "%d\n" ,DamageType::toEnum( "Ice" )); 76 printf ( "%d\n" ,DamageType::toEnum( "Other" )); 77 78 printf ( "%s\n" ,DamageType::toString(DamageType::Wood)); 79 printf ( "%s\n" ,DamageType::toString(DamageType::Stone)); 80 printf ( "%s\n" ,DamageType::toString(DamageType::Metal)); 81 printf ( "%s\n" ,DamageType::toString(DamageType::Ice)); 82 printf ( "%s\n" ,DamageType::toString(DamageType::Other)); 83 return 0; 84 } 你可能也喜欢 Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler 关于C++中static_cast和reinterpret_cast的区别 以及实例应用 axios官方文档详解 当XPath遇上NameSpace 利用Word2Vec在语料中构建种子词集同类词 ImportError:libGL.so.1:cannot open shared object file: No such file or directory. 【数论】 质数知识总结(质数判断、筛选、质因子分解、互质) Pyspark 时间窗口函数 Redis 的缓存穿透、缓存击穿和缓存雪崩 Data truncation: Incorrect date value这个问题解决方式 自动化分析Oracle buffer cache的优化思路 数据匹配之VlookUp 函数 Lucence(垂直化搜索引擎框架) 循环和递归神经网络 (RNN) 与 长短时记忆 (LSTM) MinGW-w64安装教程 2、气体灭火系统的设计灭火浓度 Graph Convolutional Networks for Temporal Action Localization 基于VCP虚拟串口的Rosserial接口教程 初始化浏览器样式——initCSS 编译可用的支持https的libcurl