10

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?

Community
  • 1
  • 1
GrandOpener
  • 1,943
  • 1
  • 17
  • 25
  • 2
    Use the [X macros](http://en.wikibooks.org/wiki/C_Programming/Preprocessor#X-Macros)? (You don't really need a header, see also http://stackoverflow.com/questions/6635851/real-world-use-of-x-macros.) – kennytm Oct 31 '12 at 09:18
  • 2
    Look at my solution here: [enum to string and vice-versa](http://stackoverflow.com/a/11685909/415784) – Nawaz Oct 31 '12 at 09:23

3 Answers3

11

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)
};
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • why not `#define DEFINE_GADGET(X) #X,` in the second case? (I haven't tried to compile) – BЈовић Oct 31 '12 at 09:46
  • @BЈовић: There are cases (which I forget) where doing it directly does not work. You need to add a layer of indirection to guarantee that quoting works. – Martin York Oct 31 '12 at 09:53
  • Awesome. I'd never heard of X Macros before. After following some of the links above, I also want to add this [link](http://www.drdobbs.com/the-new-c-x-macros/184401387), which explains very clearly some of the hidden details here, like why it might be important to put the definitions in another file. – GrandOpener Nov 01 '12 at 10:47
1

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" };
aaronps
  • 324
  • 1
  • 6
0

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
}
Ylisar
  • 4,293
  • 21
  • 27
  • In the question I referenced as duplicate I show a way to use Boost.Preprocessor for this. Rather than definining a table, I would advise using a switch instead (and let the compiler worry about optimizting it). – Matthieu M. Oct 31 '12 at 09:43