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. Advertising

  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. Advertising

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. Paul Mensonides

    Re: macro for enum to string?

    Paul Mensonides, Apr 1, 2004, in forum: C++
    Replies:
    0
    Views:
    554
    Paul Mensonides
    Apr 1, 2004
  2. Eric Sosman
    Replies:
    3
    Views:
    795
    Mayeul
    Feb 26, 2010
  3. Wojtek
    Replies:
    1
    Views:
    509
  4. Lew
    Replies:
    0
    Views:
    545
  5. Roedy Green
    Replies:
    0
    Views:
    741
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page