enum class的基于namespace的实现

之前实现的枚举类enum class虽然达到了当时期望的功能,但现在在用的时候出现了一些问题。比如新定义了一个Token的类,
定义如下,

class Token
{
public:
    Token(){}
    Token(int tp, string tx) {
        type = tp;
        text = tx;
    }
    int Type() const {
        return type;
    }
    string Text() const {
        return text;
    }
    friend ostream& operator << (ostream& out,const Token& token){
        out << "<" << TokenType.Name(token.Type()) << "," << token.Text() << ">" << endl;
    }
protected:
    int type;
    string text;
};

希望Token输出时,能够输出Token的类型的名字,这在之前的枚举类里面已经实现了,但是输出语句中TokenType.Name(type),直接使用了TokenType对象,如果type时ExprTokenType,那么就会出错,除非name是static的,在TokenType和ExprTokenType里面共用。如果这样做,name的初始化就会很麻烦,static成员需要在类外初始化,在构造函数中不能对它进行修改,那么,继承的ExprTokenType也不能在构造函数里面对它进行修改。这是一种解决方案,name在token对应的.cpp文件里面初始化,虽然目前只有main.cpp,其它的都是头文件,但是不得不增加.cpp了。这样貌似解决了,但Token里面输出的那一句话还是有问题啊。

另一种方案,使用namespace,将TokenType定义为namespace,扩展的时候也不需要继承,可以直接添加常量类型成员,而name的添加则可以放到对应Lexer的初始化里面。

// token.h
namespace TokenType{
    const static int _EOF = 0;
    const static int TEXT = 1;
    static vector<string> name = {"_EOF","TEXT"};
    const string Name(int x){
        return TokenType::name.at(x);    
    }
}

# exprtoken.h
#include "tname.h"
namespace TokenType{
    const static int NUMBER = 2;
    const static int OP = 3;

    static void SetNameExpr(){
        TokenType::name.push_back("NUMBER");
        //name.push_back("NUMBER");
        TokenType::name.push_back("OP");
    }
}

// main.cpp
int main()
{
    int type_a = TokenType::TEXT;
    string type_a_name = TokenType::Name(type_a);
    cout << "type_a:" << type_a_name << "," << type_a << endl;
    TokenType::SetNameExpr();
    int type_b = TokenType::NUMBER;
    string type_b_name = TokenType::Name(type_b);
    cout << "type_b:" << type_b_name << "," << type_b << endl;

    return 0;
}

本来应该很简单的问题,为什么现在搞得这么复杂呢?

posted @ 2012-10-08 18:29  Frandy.CH  阅读(739)  评论(2编辑  收藏  举报