问题
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?
回答1:
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)
};
回答2:
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" };
回答3:
Short example of what I use:
#include <iostream>
#include <cassert>
#include "boost/preprocessor.hpp"
#include "boost/algorithm/string/predicate.hpp"
#define ENUMIFY_FOREACH( r, data, elem ) \
BOOST_PP_STRINGIZE( elem ) BOOST_PP_COMMA()
#define ENUMIFY( name, values ) \
struct name \
{ \
static const unsigned int Size = BOOST_PP_SEQ_SIZE( values ); \
typedef enum{ \
BOOST_PP_SEQ_ENUM( values ) \
} Values; \
static const char* (&Mappings())[ Size ] \
{ \
static const char* mappings[] = \
{ \
BOOST_PP_SEQ_FOR_EACH( ENUMIFY_FOREACH, _, values ) \
}; \
return mappings; \
}; \
static const char* String( Values a_Val ) \
{ \
return Mappings()[ static_cast< unsigned int >( a_Val ) ]; \
} \
static Values Value( const char* a_Key ) \
{ \
for( unsigned int i = 0; i < Size; ++i ) \
if( boost::iequals( a_Key, Mappings()[i] ) ) return static_cast< Values >( i ); \
assert( 0 && "Didn't find the value of string " ); \
return static_cast< Values >( 0 ); \
} \
\
};
ENUMIFY( SomeEnum, (Long)(Short)(Etc) );
int main()
{
std::cout << SomeEnum::String( SomeEnum::Long ) << std::endl; // Outputs Long
}
来源:https://stackoverflow.com/questions/13154543/how-can-i-avoid-repeating-myself-when-creating-a-c-enum-and-a-dependent-data-s