Home > OS >  How to append something to comma-separated list (implemented as macro) and convert it to strings usi
How to append something to comma-separated list (implemented as macro) and convert it to strings usi

Time:08-24

I have an list of tokens defined as:

#define TOKENS ACC, STOP, RUN, BACK

This list might change. I would like to create an array of function pointers based on that list by doing something similar to:

int (*callbacks[])(const char * arg) =
{
   some_macro_shenanigans(TOKENS)
};

And some_macro_shenanigans(TOKENS) should expand to ACC_callback, STOP_callback, ... and so on. Later I would like to create an array of strings based on TOKENS like this:

const char * const token_str[] = some_other_macro_shenanigans(TOKENS);

Which would expand to something equivalent to this:

const char * const token_str[] = [ "ACC", "STOP", "RUN", "BACK" /* and others if present */ ];

Is it doable?

CodePudding user response:

Sure, using x-macros (as comments point towards):

#define TOKENS(DO) \
    DO(ACC) \
    DO(STOP) \
    DO(RUN) \
    DO(BACK) 

#define GEN_CALLBACK(ID) ID##_callback,
#define GEN_NAME(ID) #ID,

int (*callbacks[])(const char * arg) = { TOKENS(GEN_CALLBACK) };
const char * const token_str[] = [ TOKENS(GEN_NAME) ];

preprocesses to (godbolt link)

int (*callbacks[])(const char * arg) = { ACC_callback, STOP_callback, RUN_callback, BACK_callback, };
const char * const token_str[] = [ "ACC", "STOP", "RUN", "BACK", ];
  • Related