Jump to content
  • Advertisement
Sign in to follow this  
gameplayprogammer

enum to a string....

This topic is 3620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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_ENUM
printf("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.

Share this post


Link to post
Share on other sites
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?






Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!