473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Python to c++ conversion problem

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 23 '05 #1
3 1294
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 23 '05 #2
"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 23 '05 #3
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 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Willem | last post by:
When I run the follwing code using Python 2.3: from time import clock t1 = clock () for i in range (10000): a = int ('bbbbaaaa', 16) t2 = clock () for i in range (10000): a = long ('bbbbaaaa',...
3
by: Ajay | last post by:
hi! I have a c++ file that does some XML processing (visual c++ using ATL). The file builds fine. However when i add the header #include<Python.h>, i get a number of errors C:\Windows CE...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
8
by: John Salerno | last post by:
I figured my first step is to install the win32 extension, which I did, but I can't seem to find any documentation for it. A couple of the links on Mark Hammond's site don't seem to work. ...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 430 open ( -4) / 3447 closed (+17) / 3877 total (+13) Bugs : 922 open ( -7) / 6316 closed (+31) / 7238 total (+24) RFE : 245 open...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
0
by: ronysk | last post by:
Hi, I am posting here to seek for help on type conversion between Python (Numeric Python) and C. Attachment A is a math function written in C, which is called by a Python program. I had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.