Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid repeating myself when creating a C++ enum and a dependent data structure? [duplicate]

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?

like image 796
GrandOpener Avatar asked Oct 31 '12 09:10

GrandOpener


2 Answers

Something that you might or might not want to do, first define what is to be defined, in this case enums and char arrays:

#define DEFENUM(v) v,
#define DEFENUM_last(v) v
#define DEFINE_ENUM(n, LIST) enum n { LIST(DEFENUM) }

#define DEFARR(v) #v,
#define DEFARR_last(v) #v
#define DEFINE_ARRAY(n, LIST) const char *n[] = { LIST(DEFARR) }

Then make your list with this format:

#define LETTERS(GEN) \
        GEN(aaa) \
        GEN(bbb) \
        GEN(ccc) \
        GEN(ddd) \
        GEN##_last(eee)

Or this one:

#define LETTERS(GEN) \
        GEN(aaa) GEN(bbb) GEN(ccc) GEN(ddd) GEN##_last(eee)

Finally create what you want to create:

DEFINE_ENUM(LettersEnum, LETTERS);
DEFINE_ARRAY(letters_array, LETTERS);

And this will be converted to:

enum LettersEnum { aaa, bbb, ccc, ddd, eee };
const char *letters_array[] = { "aaa", "bbb", "ccc", "ddd", "eee" };
like image 27
aaronps Avatar answered Nov 20 '22 06:11

aaronps


There is an old pre-processor trick for this:

Gadget.data

DEFINE_GADGET(First)
DEFINE_GADGET(Second)

Gadget.**

#define QUOTE_VAL(X)  #X

enum Gadget
{
#define DEFINE_GADGET(X)   X,
#include "Gadget.data"
#undef DEFINE_GADGET(X)
}; 

const char* gadget_debug_names[] = {
#define DEFINE_GADGET(X)   QUOTE_VAL(X),
#include "Gadget.data"
#undef DEFINE_GADGET(X)
};
like image 105
Martin York Avatar answered Nov 20 '22 06:11

Martin York