An enum
by itself is not well suited to checking a value against a set of valid values - C does not specify an operation to iterate over the enum values. One common way is to make use of an X-macro to create the enum and corresponding check function. Use of the X-macro allows the list values to be defined once and then be used in multiple contexts.
#define FIRST_MENU_KEYS
X(UP, 72)
X(DOWN, 80)
X(LEFT, 75)
X(RIGHT, 77)
X(ENTER, 101)
#define X(a, b) a = b,
enum firstMenuKeys {
FIRST_MENU_KEYS
};
#define X(a, b) case a:
int isFirstMenuKey(char input)
{
switch (input) {
FIRST_MENU_KEYS
return 1;
default:
return 0;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…