enum to a string....

Started by
8 comments, last by gameplayprogammer 15 years, 9 months ago
I have and enum.... <code> enum MY_ENUM { FIRST_ENUM, SECOND_ENUM }; </code> at the moment to convert to a string and vice versa i do.... <code> std::string GetStringFromEnum(MY_ENUM theEnum) { if(theEnum == FIRST_ENUM) return "FIRST_ENUM"; } std::string GetEnumFromString(std::string enumString) { if(enumString.compare("FIRST_ENUM") == 0) return FIRST_ENUM; } </code> this is ok if you have a few enums but could get long if you have many enums is there not a way to do... <code> MY_ENUM theEnum = FIRST_ENUM printf("this is the string %s",theEnum); </code> and it wil out put... "this is the string FIRST_ENUM" and it will print out the variable name and not the variable value. anyone know how i can do this?
Advertisement
There's no standard way to do this in C++ with vanilla enums. One can abuse macros with Boost's preprocessor library to do the heavy lifting, though.

I recently added some macros to my libindustry project which do exactly that (on top of their original goal of strengthening the type safety of enumerations), with some example usage in the form of unit tests.

While the source code is not for the faint of heart, the following may help give you an idea of what to do if you wanted to write a version that you and your colleagues could understand and (maybe) not be fired for:

Lines 18 and 25 of industry/enum/basic_enum.hpp
Lines 18-29 of industry/enum/options/ostream.hpp
Lines 24-40 of industry/enum/options/istream.hpp

I took the road of implementing operator<< and operator>> allowing me to use boost::lexical_cast as is my preferred method for to/from string conversions.
well, as long as you aren't renumbering in your enum, then they are just in-order assending intergers starting at 0
so you could just do

enum My_Enum{  FIRST_ENUM_VAL,  SECOND_ENUM_VAL,  MY_ENUM_COUNT,};std::string My_Enum_str[MY_ENUM_COUNT] = { "First", "Second",};void Foo ( void ){  std::cout << My_Enum_str[FIRST_ENUM_VAL] << std::endl;}


--edit didn't read you wanted to go both ways
And the fastest way to convert back would be
std::map<std::string,MyEnum> g_reverseEnumLookup;void SetupEnumFromStringLookup ( void ){  for ( int i = 0; i < MY_ENUM_COUNT; ++i    g_reverseEnumLookup[My_Enum_str] = (MyEnum)i;}MyEnum enumFromString ( std::string str ){  std::map<std::string,MyEnum>::iterator i = g_reverseEnumLookup.find ( str );  if ( i != g_reverseEnumLookup.end() )    return i->second;  return ERROR; // or throw error}


This is a simple to understand, but you have to do for every enum sort of lookup. MaulingMonkey's solution is a works-everywhere solution, even if the code is harder to follow.
thanks all

found this on another forum


#include <iostream>

#define GET_NAME(n) #n

using namespace std;

int main()
{
int x = 7;
cout << x << endl;
cout << GET_NAME(x) << endl;
return 0;
}


should do the trick..
Quote:Original post by gameplayprogammer
#define GET_NAME(n) #n
cout << GET_NAME(x) << endl;

should do the trick..


Well, if your intent was to display the variable name then yes, it will do the trick.

what do you mean? Did you not read my first post, thats what i want, to print out the variable name, not the value.
Quote:Original post by gameplayprogammer
what do you mean? Did you not read my first post, thats what i want, to print out the variable name, not the value.


Your first post misuses the term "variable name". Consider your example statement:

MY_ENUM theEnum = FIRST_ENUMprintf("this is the string %s",theEnum);


In this example, the variable name is theEnum, the value is 0, and the name of the value in the enumeration is FIRST_ENUM. Yet, instead of printing the variable name (theEnum), you want to print FIRST_ENUM, leading me to believe that either your first post is confused, or you're going to have a bad surprise.
ah, i see your point. Here is what it should have said....

------------------------------------------------

I want to print the name of an enum( not the value or the variable name ).



<code>
enum MY_ENUM { FIRST_ENUM }
MY_ENUM theEnum = FIRST_ENUM;
printf("this is the string %s",theEnum);
</code>

I want it to output "this is the string FIRST_ENUM"

anyone know how i can do this, print the enum name?






enum Status{	First,	Second,	Third,	TOTAL,};Status GetStatus( const char* name ){#define TYPE_CASE(_type)	if ( stricmp( name, # _type ) == 0 )	return _type;	TYPE_CASE( First );	TYPE_CASE( Second );	TYPE_CASE( Third );#undef TYPE_CASE	return eStatTOTAL;}const char* GetStatusName( Status type ){#define NAME_CASE(_type)	case _type:	return #_type;	switch( type )	{		NAME_CASE( First )		NAME_CASE( Second )		NAME_CASE( Third )	}#undef NAME_CASE	return NULL;}


The above allows you to cut and paste the list from the enums and then you can use your editor to record a macro that will do all the TYPE_CASE/NAME_CASE for you. This isn't great but it's a better than managing the list yourself. At least it will complain when one is missing and you don't have to worry abput order. If the compiler is any good, it should make a jump table for you since he switch type is an enum.

This is also a good read. I don't actually use the above anymore, instead I use lua to auto-generate my enums. So I have a lua file that describes the enum and then I generate .hxx/.cxx code that adds extra features to the enum such as FromString and ToString.
Thanks for the help.

This topic is closed to new replies.

Advertisement