c++ design question: store identifiers

Discussion in 'C++' started by puzzlecracker, Aug 21, 2008.

  1. I have this problem:

    I read string identifiers from the file, and based on identifier I
    want to invoke certain function. So, in theory, I need something
    like enum that supports strings, and then do switch that...Here is a
    rough draft of what it should be:

    class Value{

    private std::string identifier; //read from the file

    };

    In code

    void SomeFunction()
    {

    switch (VAlue.identifier)
    {
    case A: // do something, break
    case A: // do something, break
    case default: report an error error
    }
    }
    what is the common design for this sort of a problem?
     
    puzzlecracker, Aug 21, 2008
    #1
    1. Advertising

  2. puzzlecracker

    Guest

    On Aug 21, 9:14 am, puzzlecracker <> wrote:
    > I have this problem:
    >
    > I read string identifiers from the file, and based on identifier I
    > want to invoke certain function. So,  in  theory, I need something
    > like enum that supports strings, and then do switch  that...Here is a
    > rough draft of what it should be:
    >
    > class Value{
    >
    > private std::string identifier; //read from the file
    >
    > };
    >
    > In code
    >
    > void SomeFunction()
    > {
    >
    >      switch (VAlue.identifier)
    >     {
    >         case A: // do something, break
    >         case A: // do something, break
    >         case default: report an error error
    >     }}
    >
    > what is the common design for this sort of a problem?


    The string representation is really for external consumption - screen,
    file, or database. Interanally, you want to use an enum. So the task
    at hand is to create the functionality to convert a string to an
    enum. This is such a commona task that you can probably find some
    good solutions on the web.

    HTH
     
    , Aug 21, 2008
    #2
    1. Advertising

  3. puzzlecracker

    mlimber Guest

    On Aug 21, 9:14 am, puzzlecracker <> wrote:
    > I have this problem:
    >
    > I read string identifiers from the file, and based on identifier I
    > want to invoke certain function. So,  in  theory, I need something
    > like enum that supports strings, and then do switch  that...Here is a
    > rough draft of what it should be:
    >
    > class Value{
    >
    > private std::string identifier; //read from the file
    >
    > };
    >
    > In code
    >
    > void SomeFunction()
    > {
    >
    >      switch (VAlue.identifier)
    >     {
    >         case A: // do something, break
    >         case A: // do something, break
    >         case default: report an error error
    >     }}
    >
    > what is the common design for this sort of a problem?


    Use std::map, something like:

    typedef void (*Fn)();
    typedef std::map<std::string,Fn> MyMap;

    MyMap myMap; // global for the sake of simplicity

    void Hello() {/*...*/}
    void World() {/*...*/}

    void Call( const std::string& str )
    {
    MyMap::const_iterator it = myMap.find( "world" );
    if( it != myMap.end() )
    {
    it->second();
    }
    }

    int main()
    {
    myMap[ "hello" ] = &Hello;
    myMap[ "world" ] = &World;

    Call( "hello" );
    Call( "world" );
    }

    Cheers! --M
     
    mlimber, Aug 21, 2008
    #3
  4. puzzlecracker

    mlimber Guest

    On Aug 21, 9:50 am, mlimber <> wrote:
    >  void Call( const std::string& str )
    >  {
    >    MyMap::const_iterator it = myMap.find( "world" );


    That should be:

    const MyMap::const_iterator it = myMap.find( str );

    Cheers! --M
     
    mlimber, Aug 21, 2008
    #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. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    640
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. mj
    Replies:
    2
    Views:
    385
    Will Hartung
    Aug 25, 2004
  3. Replies:
    1
    Views:
    354
    Roedy Green
    Apr 22, 2008
  4. Chad

    A question about identifiers

    Chad, Nov 23, 2009, in forum: C Programming
    Replies:
    16
    Views:
    518
    David Thompson
    Dec 9, 2009
  5. Chad
    Replies:
    9
    Views:
    319
    William Hughes
    May 1, 2010
Loading...

Share This Page