Python to c++ conversion problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Akdes Serin

    #1

    Python to c++ conversion problem

    I have a code in python like
    if eval('player.mo veRoom(SeLinuxM ud.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

    #2
    Re: Python to c++ conversion problem

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


    "Akdes Serin" <e0427463@stude nt.tuwien.ac.at > wrote in message
    news:424141bb$0 $12642$3b214f66 @tunews.univie. ac.at...[color=blue]
    > I have a code in python like
    > if eval('player.mo veRoom(SeLinuxM ud.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.
    >
    >[/color]


    Comment

    • Ivan Vecerina

      #3
      Re: Python to c++ conversion problem

      "Akdes Serin" <e0427463@stude nt.tuwien.ac.at > wrote in message
      news:424141bb$0 $12642$3b214f66 @tunews.univie. ac.at...[color=blue]
      >I have a code in python like
      > if eval('player.mo veRoom(SeLinuxM ud.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++?[/color]
      Obviously C++ has no eval equivalent, but it also has limited
      "introspect ion" 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::s tring,ERoom> RoomLookup;
      typedef RoomLookup::val ue_type RoomLookupEntry ;
      RoomLookupEntry roomLookupData[3] =
      { RoomLookupEntry ("Entrance", roomEntrance )
      , RoomLookupEntry ("Lobby", roomLobby )
      , RoomLookupEntry ("Entrance", roomCave )
      };
      RoomLookup sRoomLookup(roo mLookupData,roo mLookupData+3);

      Then in your code:
      if (player->moveRoom(sRoom Lookup[ 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


      Comment

      • Jerry Coffin

        #4
        Re: Python to c++ conversion problem

        Akdes Serin wrote:[color=blue]
        > I have a code in python like
        > if eval('player.mo veRoom(SeLinuxM ud.Direction.' + x + ')'): #[/color]
        moveRoom[color=blue]
        > 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++?[/color]

        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.

        Comment

        Working...