Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ design question: store identifiers

Reply
Thread Tools

c++ design question: store identifiers

 
 
puzzlecracker
Guest
Posts: n/a
 
      08-21-2008
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?
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      08-21-2008
On Aug 21, 9:14*am, puzzlecracker <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-21-2008
On Aug 21, 9:14*am, puzzlecracker <(E-Mail Removed)> 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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      08-21-2008
On Aug 21, 9:50*am, mlimber <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding identifiers to known or unknown identifiers latashag@live.com Java 1 04-22-2008 04:54 AM
to store or not to store an image =?Utf-8?B?UnVkeQ==?= ASP .Net 6 03-30-2005 05:51 AM
Advantages of denying keywords as identifiers valentin tihomirov VHDL 8 12-28-2004 06:44 PM
Why Does C++ Name-Mangle Identifiers? Karl Heinz Buchegger C++ 20 11-05-2004 10:31 AM
Link checker that checks fragment identifiers? Spartanicus HTML 2 05-25-2004 09:54 PM



Advertisments