0

I have an enum class like this:

class ContentTypeEnum {

 public:

  // it might have more types
  enum Code { TEXT, XML, APPLICATION_JSON};

  static const char* to_c_str(unsigned);

};

I was using it like this in my code as of now.

ContentTypeEnum::APPLICATION_JSON

Problem Statement:-

Now I have a string given so I need to use that string and then find the actual ENUM type by iterating it my above enum.

Below is my code:

cout<<"Given String: " << data_args->pp_args->ter_strings[0].c_str() << endl;
const char* test_str = data_args->pp_args->ter_strings[0].c_str();

Now if test_str is xml or XML, then I need to set it like this:

TestClass::SetContentType(ContentTypeEnum::XML)

But if test_str is application_json or APPLICATION_JSON, then I need to set it like this:

TestClass::SetContentType(ContentTypeEnum::APPLICATION_JSON)

And similarly for others as well. Below is my full code:

cout<<"Given String: " << data_args->pp_args->ter_strings[0].c_str() << endl;
char* test_str = data_args->pp_args->ter_strings[0].c_str();

// look up the exact ContentType from the enum using test_str string
// and then set it to below method.
TestClass::SetContentType(set_it_here_basis_on_string_test_str)

If somebody is passing any unknown string which is not there in my enum, then it should use by default as TestClass::SetContentType(ContentTypeEnum::APPLICATION_JSON)

What is the right way to lookup the exact enum type given a string?

1
  • There's no built in facility, but it's straightforward to write your own function that takes a string, compares it to hardcoded literals, and returns an enumeration. Commented Apr 21, 2015 at 21:59

1 Answer 1

4

I suggest writing a function that returns the enum given a string.

Code getCode(std::string const& s)
{
   static std::map<std::string, Code> theMap{{"TEXT", TEXT},
                                             {"XML", XML}
                                             {"APPLICATION_JSON", APPLICATION_JSON}};

   std::map<std::string, Code>::iterator it = theMap.find(s);
   if ( it != theMap.end() )
   {     
      return it->second;
   }
   return APPLICATION_JSON;
}
Sign up to request clarification or add additional context in comments.

14 Comments

I'd use const and find, and be more explicit about the default of APPLICATION_JSON.
@MooingDuck - I'd probably toss an exception or otherwise let the caller know a match wasn't found and let the caller deal with what the default should be. I'd probably also convert the incoming string to upper case to make a case-insensitive match.
@david, the advantage of using the map is less code. It might also result in faster lookup but I won't vouch for that without hard data.
@david, right. If you add more tokens to the enum, you'll have to update the map.
@david: The core of the issue is the exe doesn't have the names of the enumerations, period. So to compare the input with names, you have to add the names to the exe, and store them somewhere. Thus, we make a map. You can use stricmp or some other case-insensitive string comparison for those cases.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.