By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Python to c++ conversion problem

P: n/a
I have a code in python like
if eval('player.moveRoom(SeLinuxMud.Direction.' + x + ')'): # moveRoom
function takes Direction enum as a parameter

When I am trying to write this code in c++
if (player->moveRoom(s[1])) //s[1] is a string so it outputs an error
because of not taking enum as a parameter

How can I change string to enum in c++?
Thanks.
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,
Convert the string to int then cast the int to enum with static_cast.
Ahmed
"Akdes Serin" <e0******@student.tuwien.ac.at> wrote in message
news:42***********************@tunews.univie.ac.at ...
I have a code in python like
if eval('player.moveRoom(SeLinuxMud.Direction.' + x + ')'): # moveRoom
function takes Direction enum as a parameter

When I am trying to write this code in c++
if (player->moveRoom(s[1])) //s[1] is a string so it outputs an error
because of not taking enum as a parameter

How can I change string to enum in c++?
Thanks.

Jul 18 '05 #2

P: n/a
"Akdes Serin" <e0******@student.tuwien.ac.at> wrote in message
news:42***********************@tunews.univie.ac.at ...
I have a code in python like
if eval('player.moveRoom(SeLinuxMud.Direction.' + x + ')'): # moveRoom
function takes Direction enum as a parameter

When I am trying to write this code in c++
if (player->moveRoom(s[1])) //s[1] is a string so it outputs an error
because of not taking enum as a parameter

How can I change string to enum in c++?

Obviously C++ has no eval equivalent, but it also has limited
"introspection" at run-time. That is, the executing code has
no way to know the compile-time name associated with the enum
values.

One way to do this mapping manually would be:

#include <map>
#include <string>

enum ERoom
{ roomEntrance = 0
, roomLobby = 1
, roomCave = 2
};
typedef std::map<std::string,ERoom> RoomLookup;
typedef RoomLookup::value_type RoomLookupEntry;
RoomLookupEntry roomLookupData[3] =
{ RoomLookupEntry("Entrance", roomEntrance )
, RoomLookupEntry("Lobby", roomLobby )
, RoomLookupEntry("Entrance", roomCave )
};
RoomLookup sRoomLookup(roomLookupData,roomLookupData+3);

Then in your code:
if (player->moveRoom(sRoomLookup[ s[1] ]))

An annoyance with this approach is that your are writing
everything twice.
There are tricks to avoid that, using the preprocessor,
but it has its own drawbacks (see the thread associated with
http://groups.google.ca/groups?selm=...%40comcast.com )

But for a game, most typically, the code would load
room descriptions from a data file at run-time, and
dynamically generate the equivalent of sRoomLookup.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 18 '05 #3

P: n/a
Akdes Serin wrote:
I have a code in python like
if eval('player.moveRoom(SeLinuxMud.Direction.' + x + ')'): # moveRoom function takes Direction enum as a parameter

When I am trying to write this code in c++
if (player->moveRoom(s[1])) //s[1] is a string so it outputs an error
because of not taking enum as a parameter

How can I change string to enum in c++?


The most obvious way would be to use an std::map of strings and the
enum that's equivalent to each.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.