gardian06
gardian06

Reputation: 1546

enum as argument returning string

My current program uses 3 different enums:

enum ThingEnum{
    Thing1 = 0,
    Thing2 = 1,
    Thing3 = 2,
    OtherThing = 3
};
enum ReturnEnum {
    Success = 4,// the method did what it is supposed to
    Error1 = 5,// an error occured
    Error2 = 6,// an error occured
    Error3 = 7,// an error occured
    Error4 = 8,// a fatal error occured program must terminate
    Pointer = 9// the method may need to be called again
};
enum TypeEnum {
    Type1 = 10,
    Type2 = 11,
    Type3 = 12,
    Type4 = 13,
    OtherType = 14
};  

What I want to do is create a global function that takes an enum and returns a string (as the value of an enum is really just a specialized variable name that always has a value). Is it possible to create a function that takes a generic enum? e.g.

string enumToString (enum _enum){}

or would I have to make a function for each different enum? Just a possible thought I did some reading, and some compilers allow for a Enum to resolve to an int so could I pass the enum as an int and then work with it?

Upvotes: 3

Views: 1065

Answers (4)

Tom Tanner
Tom Tanner

Reputation: 9354

This is one of those cases where I'd advocate the use of macros to make your code more intelligible / maintainable (and remove at least one source of errors)

Thus

  #define TOSTRING(name) case name: return #name
  switch (val)
  {
      TOSTRING(Thing1);
      TOSTRING(Thing2);
      default:
         //bad things happened.
   }

Upvotes: 2

There are two options of "ToString-like" function implementation:

  1. Implement a simple static switch-case function.

Code:

std::string ThingEnumToString(ThingEnum thing)
{
    switch (thing) {
    case Thing1:
        return std::string("Thing1");
    case Thing2:    
        return std::string("Thing2");
    case Thing3:
        return std::string("Thing3");        
    case OtherThing:
        return std::string("OtherThing");
    default:
        throw std::invalid_argument("thing");
        break;
    }
}
  1. Implement a static function with a dictionary lookup.

Code:

typedef std::map<ThingEnum, std::string> ThingsMap;

static ThingsMap GetThingsMap()
{
    ThingsMap things_map;
    things_map.insert(ThingsMap::value_type(Thing1, std::string("Thing1")));
    things_map.insert(ThingsMap::value_type(Thing2, std::string("Thing2")));
    things_map.insert(ThingsMap::value_type(Thing3, std::string("Thing3")));
    things_map.insert(ThingsMap::value_type(OtherThing, std::string("OtherThing")));
    return things_map;
}

static std::string ThingEnumToString(ThingEnum thing)
{
    static const ThingsMap things(GetThingsMap());
    ThingsMap::const_iterator it = things.find(thing);
    if (it != things.end()) {
        return it->second;
    } else {
        throw std::invalid_argument("thing");
    }
}

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385395

No, enums have no implicit base class.

You can use templates, or function overloading. But, yes, you're going to have to do it by hand.

There are some tricks for enum→string "conversion" listed here.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258678

enum names only make sense before compilation. I don't think there's a portable or standard-defined way of doing this in a clean fashion.

You could do it as follows:

std::string getName(ThingEnum x)
{
   switch (x)
   {
   case Thing1:
      return "Thing1";
   case Thing2:
      return "Thing2";
   //....
   }
}

Upvotes: 0

Related Questions