Drop parameters in generator macro

Viewed 87

I'm currently working on a code base with an extreme amount of enums. There is a need to convert enum values to and from string representations. To do this there is a bunch of helper macros that operates on macros on the form

#define THINGS_MAP(G, P) \
    G(LAMP, P) \
    G(DESK, P)

And elsewhere in the code there are macros that works roughly like this:

#define MK_ENUM(SYM, P) P##_##SYM,
enum { THINGS_MAP(MK_ENUM, THING) }; /* expands to { THING_LAMP, THING_DESK, } */

I would like to avoid changing MK_ENUM and the call to THINGS_MAP inside the enum.

However, I now have one particularly long enum list, where I also need to add some classification to the entries. So I was thinking about doing something like

#define THINGS_PROP_MAP(G, P) \
    G(LAMP, WOODEN, P) \
    G(DESK, METALIC, P)

However, since all my helper macros require two arguments passed, not three, I was thinking if it would be possible to map some kind of macro that drops a parameter. Is this possible?

What I am looking for is something along the lines of:

#define THINGS_PROP_MAP(G, P) \
    G(LAMP, WOODEN, P) \
    G(DESK, METALIC, P)

/* DROP_PROP should create G(LAMP, P) G(DESK, P) */
#define THINGS_MAP(G, P) DROP_PROP(THINGS_PROP_MAP)

One idea I had was to add an extra transformation step, but I didn't get it to work

#define THINGS_PROP_MAP(G, T, P) \
    G(T(LAMP, WOODEN, P)) \
    G(T(DESK, METALIC, P))
#define DROP_PROP(SYM, _ , P) SYM, P
#define THING_MAP(G, P) THING_PROP_MAP(G, DROP_PROP, P)

And yes, I know this is waaay beyond macro abuse etc :-)

1 Answers
Related