c++添加swich case string 的支持


    #pragma once  
    // ----------------------------------------------------------------------------  
    // string_switch_case.h  
    //  
    // These macros together implement switch-case functionality for C strings and  
    // std::string.  When first encountered, the switch-case structure does two  
    // passes.  The first pass is a one-time initialization of a std::map with the  
    // strings as keys, and line numbers as values, to get the unique keys needed   
    // for a numeric switch-case.  All subsequent passes use the std::map to  
    // retrieve an unsigned integer for a given string, that was mapped during the  
    // first pass, to use for a numeric switch case.  This switch-case approach can  
    // be used repeatedly and even nested.  An example of the usage is as follows:  
    //  
    // switch_string(a)  
    // {  
    // case_string("first string")  
    //    printf("first string");  
    //    break;  
    // case_string("second string")  
    //    printf("second string");  
    // case_string("third string")  
    //    printf("falls through to third string");  
    //    break;  
    // default_case_string  
    //    printf("default");  
    // }  
    // end_switch_string  
    //  
    // The end_switch_string macro is required after the closing brace of the  
    // switch-case structure.  Each case_string statement must be on a unique line.  
    // Each case_string has its own local scope.  A case_string statement is  
    // ignored if declared within scope brackets beneath a sibling case statement.  
    // ----------------------------------------------------------------------------  
      
    #include <map>  
    #include <string>  
      
      
    // switch macro  
    #define switch_string(a) while (true) {     static std::map<std::string, unsigned int> _string_lookup_;   static bool _init_ = true;  unsigned int _val_ = 0;     if (!_init_) {      std::map<std::string, unsigned int>::iterator _iter_ =            _string_lookup_.find(a);            if (_iter_ != _string_lookup_.end()) {              _val_ = (*_iter_).second; }             else {              _val_ = 0xffffffff;             }   }   switch(_val_) {     case 0:  
      
      
    // case macro  
    #define case_string(a)      }   case __LINE__:      if (_init_)             _string_lookup_.insert              (std::pair<std::string, unsigned int>(a, __LINE__));      else {  
      
      
    // default case macro  
    #define default_case_string         }   default:        if (!_init_) {  
      
      
    // required macro for the end of the switch-case  
    #define end_switch_string   }   if (_init_) {       _init_ = false;     }   else {      break;  } }  





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值