473,508 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ design question: store identifiers

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?
Aug 21 '08 #1
3 1101
On Aug 21, 9:14*am, puzzlecracker <ironsel2...@gmail.comwrote:
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 '08 #2
On Aug 21, 9:14*am, puzzlecracker <ironsel2...@gmail.comwrote:
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,FnMyMap;

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
Aug 21 '08 #3
On Aug 21, 9:50*am, mlimber <mlim...@gmail.comwrote:
*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

Aug 21 '08 #4

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

Similar topics

4
1736
by: Danimal | last post by:
I have been using PHP for a long time... since it was called PHP/FI. I have a programming design question: Let say I have this class: class attrib { var $lenght; var $type; ... }
10
2111
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
0
1011
by: Allan Bredahl | last post by:
Hi all Im currently constructing a Windows control that inherits from RichTextBox, but I'm having a bit of trouble with updating the control at design time. The senario is SIMPLIFIED as...
29
3537
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
53
4020
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
17
2572
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do...
3
1204
by: CSharpguy | last post by:
I have a 03 .NET web that does not use Typed DataSets, it uses a Busines Layer/DataLayer classes. Alot of my reading on .NET 05 is using the DataSets for the datalayer/business layer. I have a 05...
4
1540
by: shuisheng | last post by:
Dear All, I want to develop an engine library for a particular purpose such as a physical phenomenon simulation. The input is the required physical data and geometry. The output is the...
12
6967
by: nyathancha | last post by:
Hi, I have a question regarding best practices in database design. In a relational database, is it wise/necessary to sometimes create tables that are not related to other tables through a...
0
7223
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
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
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
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
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
7489
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...
1
5047
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...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.