Python to c++ conversion problem

Discussion in 'C++' started by Akdes Serin, Mar 23, 2005.

  1. Akdes Serin

    Akdes Serin Guest

    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.
     
    Akdes Serin, Mar 23, 2005
    #1
    1. Advertising

  2. Hi,
    Convert the string to int then cast the int to enum with static_cast.
    Ahmed


    "Akdes Serin" <> wrote in message
    news:424141bb$0$12642$...
    > 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.
    >
    >
     
    Ahmed MOHAMED ALI, Mar 23, 2005
    #2
    1. Advertising

  3. "Akdes Serin" <> wrote in message
    news:424141bb$0$12642$...
    >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= )

    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
     
    Ivan Vecerina, Mar 23, 2005
    #3
  4. Akdes Serin

    Jerry Coffin Guest

    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.
     
    Jerry Coffin, Mar 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paradox

    Python Image Conversion with PIL

    Paradox, Jul 14, 2003, in forum: Python
    Replies:
    0
    Views:
    368
    Paradox
    Jul 14, 2003
  2. Akdes Serin

    Python to c++ conversion problem

    Akdes Serin, Mar 23, 2005, in forum: Python
    Replies:
    3
    Views:
    244
    Jerry Coffin
    Mar 23, 2005
  3. ronysk
    Replies:
    0
    Views:
    474
    ronysk
    Aug 6, 2007
  4. , India
    Replies:
    2
    Views:
    478
    Fraser Ross
    Sep 15, 2009
  5. John Nagle
    Replies:
    79
    Views:
    1,930
    Philip Semanchuk
    Jul 8, 2010
Loading...

Share This Page