Possible Duplicate:
Enum to string : return the enum integer value if invalid / not found
In brief, the (working) definition code that I have is something like this:
enum Gadget
{
First,
Second,
};
const char* gadget_debug_names[] = {
"First",
"Second",
// note: strings are same as enum tokens here, but bonus points if
// they can optionally be given different values
};
However, it's error prone that the information is in multiple separate places that have to be maintained manually. (And in some cases in the code base I'm working with those two--or more--places aren't even currently in the same file.) So it would be really nice to just name those things once.
Now we could do this with code generation and a declarative data file, but I'd prefer not to add another step to the existing build process if there's a better way to do this. It would be perfect to have something that looks like
DEFINE_GADGET(First)
DEFINE_GADGET(Second)
(optionally with start/stop macros if needed) But, since macros are just plain text substitution, I can't figure out any way to have the preprocessor "remember" the tokens while it's writing out the enum definition.
I've thought that this might also be possible through meta-programming, but I'm at a loss on how to do it. All of the examples I've seen there involve recursively building a data structure. I can see how I might build the array of strings that way, but I'm not sure how I could pass in a token name, or how to build an enum. (And of course using metaprogramming just to build an array of strings would be pretty ridiculous.)
Is there any way for me to keep DRY here, without using code generation?