简体   繁体   English

C ++中的变量枚举“类”

[英]Variable enum 'class' in c++

I use something like in the following code fairly often:我经常在下面的代码中使用类似的东西:

// myclass.h

class MyClass {
 public:
  enum MyEnum { E1, E2, NUMMYENUM };
  const char* kMyEnum[NUMMYENUM] = {"e1", "e2"};
  const char* MyEnum2Char(MyEnum me) { return kMyEnum[me]; }

  enum MySEnum { SE1, SE2, NUMMYSENUM };
  static const char* kMySEnum[NUMMYSENUM];
  static const char* MySEnum2Char(MySEnum se) { return kMySEnum[se]; }

  void foo(MyEnum me) {
    //do something, e.g. print
    std::cout << MyEnum2Char(me) << "maps to " << emap[me] << "\n";
  }

  void bar(MySEnum se) {
    //do something, e.g. print
    std::cout << MySEnum2Char(se) << "maps to " << semap[se] << "\n";
  }

 private:
  std::map<MyEnum, int> emap;
  std::map<MySEnum, int> semap;
 };


// myclass.cc

#include "myclass.h"

const char* MyClass::kMySEnum[MyClass::MySEnum::NUMMYSENUM] = {"se1", "se2"};

The way of generating an enum , a char* array and a function converting enum to char seems to add avoidable clutter and I am wondering if there isn't another way to achieve this?生成enumchar*数组和将enum转换为char的函数的方法似乎增加了可避免的混乱,我想知道是否没有另一种方法来实现这一点? Something like the following isn't possible for multiple reasons, but might give you an idea of what I'd like to have:由于多种原因,以下内容是不可能的,但可能会让您了解我想要的内容:

// myclass.h

class MyClass {
 public:
  MyVariableEnumClass MyEnum(E1, "e1", E2, "e2");    
  static MyVariableEnumClass MySEnum;

  void foo(MyEnum me) {
    //do something, e.g. print
    std::cout << me.string() << "maps to " << emap[me] << "\n";
  }

  void bar(MySEnum se) {
    //do something, e.g. print
    std::cout << se.string() << "maps to " << semap[se] << "\n";
  }

 private:
  std::map<MyEnum, int> emap;
  std::map<MySEnum, int> semap;
 };


// myclass.cc

#include "myclass.h"

MyVariableEnumClass MyClass::MySEnum = MyVariableEnumClass(SE1, "se1", SE2, "se2");

Is there a way to achieve something 'clutter-free' like this?有没有办法实现这样的“无杂乱”? Maybe using macros?也许使用宏?

A technique called XMacro can be used in C++ 11 as well as older versions of C++ and C to easily define consistent look-up tables for translating enums to strings.一种称为 XMacro 的技术可以在 C++ 11 以及旧版本的 C++ 和 C 中使用,以轻松定义一致的查找表以将枚举转换为字符串。

First, you write an external file (let's call it my_class.xmacro) which includes the following:首先,您编写一个外部文件(我们称之为 my_class.xmacro),其中包括以下内容:

#define SE_ENUM_TABLE  \
    ENTRY(SE1) \
    ENTRY(SE2) 

Next in your H file you include my_class.xmacro define the following:接下来在您的 H 文件中包含 my_class.xmacro 定义以下内容:

#include "my_class.xmacro"

    
enum SeEnum{
#define ENTRY(a) a,
       SE_ENUM_TABLE
#undef ENTRY
    };

const std::string seEnumString[] = {
#define str_macro(a) #a
#define ENTRY(a) str_macro(a),
    SE_ENUM_TABLE
#undef ENTRY
};

The macros inside seEnumStringId take ENTRY(a) and transform it to "a". seEnumStringId 中的宏采用 ENTRY(a) 并将其转换为“a”。 So after pre-processor is finished with this file, it actually looks to the compiler as follows:所以在预处理器完成这个文件后,它实际上看起来像下面这样的编译器:

enum SeEnum{
    SE1,
    SE2 
};
    
const std::string seEnumString[] = {
    "SE1",
    "SE2" 
    };

Using this technique you ensure that adding an enum will automatically create string id in the RIGHT order, and this technique can be used to generate additional types of look-up tables as well.使用此技术,您可以确保添加枚举将自动以正确的顺序创建字符串 id,并且此技术还可用于生成其他类型的查找表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM