Re: macro for enum to string?

Discussion in 'C++' started by Bernhard Holzmayer, Apr 1, 2004.

  1. Jeff Schwab wrote:

    > Bernhard Holzmayer wrote:
    >
    >> #define CREATE_ENUM(t,a,b,c) enum t { a, b, c }; \
    >> const char * name_##t(int i) { \
    >> switch (i) { \
    >> case a: return #a; \
    >> case b: return #b; \
    >> case c: return #c; \
    >> default: return "undefined color"; }}
    >>
    >>
    >> and this may be called with:
    >> CREATE_ENUM(red,green,blue)

    >
    > What happened to t?

    Got lost by copy&pasting the wrong line...sorry.
    >
    >> However, the discussed problem, that this macro must be defined
    >> once for every number of entries.

    >
    > It would be nice to be able to specify arbitrary values for the
    > enumerators, too. :)

    Feel free to modify the macro - there's no copyright on it.

    Bernhard
     
    Bernhard Holzmayer, Apr 1, 2004
    #1
    1. Advertisements

  2. Bernhard Holzmayer wrote:

    > Jeff Schwab wrote:
    >
    >> Bernhard Holzmayer wrote:
    >>
    >>> #define CREATE_ENUM(t,a,b,c) enum t { a, b, c }; \
    >>> const char * name_##t(int i) { \
    >>> switch (i) { \
    >>> case a: return #a; \
    >>> case b: return #b; \
    >>> case c: return #c; \
    >>> default: return "undefined color"; }}
    >>>
    >>>
    >>> and this may be called with:
    >>> CREATE_ENUM(t,red,green,blue)

    >>


    Just for completeness: a version with init values

    #define CREATE_ENUM(t,a,ai,b,bi,c,ci) \
    enum t { a=ai, b=bi, c=ci }; \
    const char * name_##t(int i) { \
    switch (i) { \
    case a: return #a "=" #ai; \
    case b: return #b "=" #bi; \
    case c: return #c "=" #ci; \
    default: return "undefined color"; }}


    and this may be called with:

    CREATE_ENUM(t,red,1 ,blue,2 ,green,3)


    Bernhard
     
    Bernhard Holzmayer, Apr 1, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    830
  2. Jerminia
    Replies:
    3
    Views:
    817
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    777
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    656
    Dave Thompson
    Jun 10, 2004
  5. Randy
    Replies:
    1
    Views:
    683
    David Harmon
    Jan 7, 2006
  6. Randy
    Replies:
    2
    Views:
    521
    Randy Yates
    Jan 8, 2006
  7. Eric Sosman
    Replies:
    3
    Views:
    906
    Mayeul
    Feb 26, 2010
  8. Wojtek
    Replies:
    1
    Views:
    607
Loading...