C++ Macro code substitution
1) - i have two enums
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) i have an association between these two macros and need to call this code for each
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) so i made this macro
#define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x, y) if (inputMonitor.state.current == x) {executeScriptsForEvent( y );}
EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_NOTOUCH, FIXED_SCRIPTING_EVENT开发者_JS百科_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)
which is fine
4) the question, how can i do smth like this
#define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x)\
if (inputMonitor.state.current == INPUT_STATE_(x)) {\
executeScriptsForEvent( FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_(x) );
}\
so i only need to
EXECUTEFIXEDSCRIPTEVENTFORSTATE(NOTOUCH);
which should be equivalent to my current :
EXECUTEFIXEDSCRIPTEVENTFORSTATE(INPUT_STATE_NOTOUCH, FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_NOTOUCH);
but the compiler states that NOTOUCH is not defined, so i cant the macro from 4
Token concatenation?
#define EXECUTEFIXEDSCRIPTEVENTFORSTATE(x)\
if (inputMonitor.state.current == INPUT_STATE_ ## x) {\
executeScriptsForEvent(FIXED_SCRIPTING_EVENT_INPUT_STATE_CHANGED_TO_ ## x);\
}
精彩评论