C ++宏代码替换

时间:2011-03-25 14:42:53

标签: c++ macros

1) - 我有两个枚举

enum FixedScriptingEvent {
    FIXED_SCRIPTING_EVENT_NOTOUCH,  
    FIXED_SCRIPTING_EVENT_OVER_MOVED,
    FIXED_SCRIPTING_EVENT_OVER_RELEASED,    
    FIXED_SCRIPTING_EVENT_OUTSIDE_RELEASED,
    FIXED_SCRIPTING_EVENT_OUTSIDE_MOVED,    
...

enum InputState {

    INPUT_STATE_NOTOUCH,
    INPUT_STATE_OVER_MOVED,
    INPUT_STATE_OVER_RELEASED,
    INPUT_STATE_OUTSIDE_RELEASED,
    INPUT_STATE_OUTSIDE_MOVED,  
    INPUT_STATE_OUTSIDE_PRESSED,
...

2)我在这两个宏之间有关联,需要为每个

调用此代码
if (inputMonitor.state.current == INPUT_STATE_NOTOUCH) {
    executeScriptsForEvent( FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_NOTOUCH );
}
if (inputMonitor.state.current == INPUT_STATE_MOVED) {
    executeScriptsForEvent( FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_MOVED );
}

3)所以我做了这个宏

    #define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x, y) if (inputMonitor.state.current == x) {executeScriptsForEvent( y );}

    EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_NOTOUCH, FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_NOTOUCH);
    EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_OVER_MOVED, FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_OVER_MOVED);
    EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_OVER_RELEASED, FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_OVER_RELEASED);

    #undef EXECUTEFIXEDSCRIPTEVENTFORSTATE(x, y)

很好

4)问题,我怎么能这样做?

#define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x)\
if (inputMonitor.state.current == INPUT_STATE_(x)) {\
executeScriptsForEvent( FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_(x) );
}\

所以我只需要

    EXECUTEFIXEDSCRIPTEVENTFORSTATE(NOTOUCH);

应该等同于我的当前:

EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_NOTOUCH, FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_NOTOUCH);

但是编译器声明NOTOUCH没有定义,所以我不能从4开始

1 个答案:

答案 0 :(得分:4)

令牌连接?

#define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x)\
if (inputMonitor.state.current == INPUT_STATE_ ## x) {\
executeScriptsForEvent(FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_ ## x);\
}