Sign in to follow this  
jchmack

converting a string to enum

Recommended Posts

is there a way i can convert an enum to a string and vice versa? Right now i have a 300 line function just to do so with one conversion:

OIS::KeyCode game4::StringToKeyCode(string input)
{
   KeyCode KC;

   if(input=="KC_UNASSIGNED")
      KC = KC_UNASSIGNED;
   else if(input=="KC_ESCAPE")
      KC = KC_ESCAPE;
   else if(input=="KC_1")
      KC = KC_1;
   else if(input=="KC_2")   
      KC = KC_2;
...................

return KC;
} 



i had a compiler error that said i had maxed out the nesting structure. THERE HAS GOT TO BE A BETTER WAY!!! ... Sigh...

Share this post


Link to post
Share on other sites
Quote:
Original post by Crazyfool
Why do you want to convert to a string or enum?



Im trying to make a user file that would store a user's keyboard configuration. Right now i am reading the data as strings so the text files are "relativly" understandable. In order to use this method i have to convert my strings to OIS::KeyCodes.

So Right now I have a HUGE function like this:

Code:

OIS::KeyCode game4::StringToKeyCode(string input)
{
KeyCode KC;

if(input=="KC_UNASSIGNED")
KC = KC_UNASSIGNED;
else if(input=="KC_ESCAPE")
KC = KC_ESCAPE;
else if(input=="KC_1")
KC = KC_1;
else if(input=="KC_2")
KC = KC_2;
...................
}


and because the KeyCode is an ENUM i cant just overload the >> operator.

Is there a way i can either overload the >> operator to read in KeyCodes from a text file. Or convert Keycodes into Strings without having a huge function like the one above.


The code works fine but its just a bit ugly... its 300 lines lol... the first time i compiled it i got an error saying i had maxed out the nesting possibilities. Also im probably going to have to write another one just as big (to reverse the process) when i need to start generating these files.

Share this post


Link to post
Share on other sites
well evidently there is an easy way to do it C#:

http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

Share this post


Link to post
Share on other sites
Quote:

Is there a way i can either overload the >> operator to read in KeyCodes from a text file. Or convert Keycodes into Strings without having a huge function like the one above.


Yes, but basically it would look like this:

istream& operator >> (istream& src, OIS::KeyCode& code)
{
std::string str;
src >> str;
code = game4::StringToKeyCode(str);
return src;
}


So it doesn't help you.

Quote:
Original post by jchmack
Im trying to make a user file that would store a user's keyboard configuration. Right now i am reading the data as strings so the text files are "relativly" understandable. In order to use this method i have to convert my strings to OIS::KeyCodes.


A std::map is definitely the way forward here - you'll probably need two to do the lookup in both directions, unless your key indices are contiguous and you can use a simple lookup table. Some macro magic might help you out a lot here - see Wikipedia on X-Macros.

However, there is another important question - do you really need your keyboard configuration to be platform portable? I assume that's why you're defining your own key ID macros (which again, I'm assuming you are). If not, then normally there are system functions for handling this type of thing (e.g. for SDL), but what APIs you're using isn't apparent. The other downside of doing it this way is that you have to try and have a name for every keyboard in the world, and they aren't all particularly alike - it's annoying enough for me with my UK keyboard when a program assumes it's US and moves the # sign around - you really have to use system functions to make sure the keyboard works properly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
I used a std::map for the keycode conversion in my GUI, check that out.

Dave


Seconded (well, I actually used a stdext::hash_map, but its the same principle). You'll need to set up the (hash_)map once, and then your 300+ line functions becomes:

assert(enum_map.find(str) != enum_map.end());
ENUM_NAME enum_var = enum_map[str];

Share this post


Link to post
Share on other sites
[quote]Original post by ZQJ
Quote:

However, there is another important question - do you really need your keyboard configuration to be platform portable? I assume that's why you're defining your own key ID macros (which again, I'm assuming you are). If not, then normally there are system functions for handling this type of thing (e.g. for SDL), but what APIs you're using isn't apparent. The other downside of doing it this way is that you have to try and have a name for every keyboard in the world, and they aren't all particularly alike - it's annoying enough for me with my UK keyboard when a program assumes it's US and moves the # sign around - you really have to use system functions to make sure the keyboard works properly.


well im using an input library called OIS its completly platform independent and already has support for International Keyboards and even most Joysticks/controllers. They are even working on a wiimote interface from what i understand.

Quote:

A std::map is definitely the way forward here - you'll probably need two to do the lookup in both directions, unless your key indices are contiguous and you can use a simple lookup table. Some macro magic might help you out a lot here - see Wikipedia on X-Macros.


Ill read up on how to do this. Ill probably end up using a map. But that article i saw looked too good. It would be pretty handy to be able to just do an enum to string so i dont have to make a map for each conversion. Thx to everyone for all the help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this