I have a list of command ENUMs that can either be a request type, or a response type.
typedef enum {
ENUM1,
ENUM2,
...
} command_t;
And, I was planning on using X Macros to generate its ENUM, as well as name-string mapping table as follows:
// commmands.def
X(ENUM1),
X(ENUM2),
// main.h
#define X(ENUMVAL, ...) ENUMVAL
typedef enum {
#include "commands.def"
} myenum_e;
#undef X
#define X(ENUMVAL, NAME) {.name = NAME, .val = ENUMVAL}
name_val_map_t name_val_map_table = {
#include "commands.def"
} mytable_t;
#undef
Additionally, I also need to generate two sub-lists, namely, a req_list and a rsp_list. Is there a way I can keep just the one commands.def file and achieve this? I was trying something on the lines of:
// commmands.def
// NAME, REQ, RSP
X(ENUM1, 1, 0),
X(ENUM2, 0, 1),
// main.h
#define IF(cond, foo) IF_IMPL(cond, foo)
#define IF_IMPL(cond, foo) IF_ ## cond (foo)
#define IF_0(foo)
#define IF_1(foo) foo
#define X(ENUMVAL, REQ, RSP) IFCOND(REQ, case: ENUMVAL)
void _is_req(myenum_e command) {
switch(command) {
#include commands.def
return 1;
default: return 0;
}
}
#undef X
#define X(ENUMVAL, REQ, RSP) IFCOND(RSP, case: ENUMVAL)
void _is_rsp(myenum_e command) {
switch(command) {
#include commands.def
return 1;
default: return 0;
}
}
#undef X
Is something like this possible? Thanks in advance!