Re: macro for enum to string?

From: Ahti Legonkov (lego_at_127.0.0.1)
Date: 03/29/04


Date: Mon, 29 Mar 2004 18:57:31 +0300

Jeff Schwab wrote:

> TheFerryman wrote:
>
>> can a macro be created that converts the name of an enumeration to a
>> string? Something like:
>>
>> enum{red, green, blue};
>>
>> cout << ENUM_TO_STRING(blue);
>
>
> Why a macro?
>
> enum Color { red, green, blue };
>
> char const* enum_to_string[ ] = { "red", "green", "blue" };
>
> #include <iostream>
>
> int main( )
> {
> std::cout << "Green is represented by the enumerator "
> << enum_to_string[ green ] << ".\n";
> }

#define ENUM_TO_STRING(x) #x

This macro converts the name of an enumeration to string but it is not
limited to enums. And it only converts at compile time. So, this
probably does not suit your needs.

The solution that Jeff proposed works only if the enum values start from
0 and are continuous. To make that more flexible, you should create a map:

#include <iostream>
#include <map>

enum colors { red, green, blue };
enum fruits { apple = 12, orange = 22, banana = 78 };

template <typename Enum>
std::map<int, char const*>& make_map();

std::map<int, char const*> colors_enum_to_string;
std::map<int, char const*> fruits_enum_to_string;

#define ENUM_ITEM(en, item) en##_enum_to_string[item] = #item

template <>
std::map<int, char const*>& make_map<colors>()
{
     ENUM_ITEM(colors, red);
     ENUM_ITEM(colors, green);
     ENUM_ITEM(colors, blue);

     return colors_enum_to_string;
}

template <>
std::map<int, char const*>& make_map<fruits>()
{
     ENUM_ITEM(fruits, apple);
     ENUM_ITEM(fruits, orange);
     ENUM_ITEM(fruits, banana);

     return fruits_enum_to_string;
}

template <typename Enum>
char const* enum_to_str(Enum e)
{
     static std::map<int, char const*>& m = make_map<Enum>();
     return m[e];
}

int main()
{
     std::cout << "Green is represented by the enumerator "
               << enum_to_str(green) << ".\n";
     std::cout << "Banana is represented by the enumerator "
               << enum_to_str(banana) << ".\n";
     return 0;
}

Creating map for each enum you create and creating the function to
populate each map is very tiresome and error prone to do by hand. To
make it easier, a script that does it would be a helpful tool.

--
Ahti Legonkov


Relevant Pages

  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Re: enum type int or unsigned int?
    ... that have type int and may appear wherever such are permitted. ... values of all the members of the enumeration. ... "enum" types are declared via the following syntax: ... so the loop will continue to run. ...
    (comp.lang.c)
  • Re: Behaviour of enumerated types (was: Re: Immutable instances, constant values)
    ... >> My notion of enum comes from Pascal ... I will look in PyPI. ... Does your concept of enumeration not have a fixed order of a set of names? ... >> So I'd say enums should also be int subtypes... ...
    (comp.lang.python)
  • Re: Variable arguments of enum type
    ... the implementation type used for the enumeration type as a whole. ... I used (unsigned int) instead of against the off chance that the ... enum EnumType1 { ... enum EnumType2 can be distinct from enum EnumType1; ...
    (comp.lang.c)
  • Re: Behaviour of enumerated types
    ... If you want sequence numbers, ... the enum object itself can be used directly as an iterable. ... an enumeration is an instance of Enum. ... have special methods of their own, and inherit methods from int). ...
    (comp.lang.python)