// A Doer is a class that does something :-)
class Doer
{
virtual ~Doer() {}
virtual void doSomething()=0;
};
class DoThis : public Doer
{
virtual ~DoThis() {}
virtual void doSomething() { ... }
};
class DoThat : public Doer
{
virtual ~DoThat() {}
virtual void doSomething() { ... }
};
// Map a doer to a string
typedef std::pair<std::string, Doer*> StringDoer;
// Map StringDoers to locations
typedef std::map<int, StringDoer> Lookup;
// At some point define what location & char relate to a certain Doer
...
Lookup lookup;
DoThis doThis;
DoThat doThat;
lookup[0] = StringDoer("ab", &doThis);
lookup[3] = StringDoer("ab", &doThis);
lookup[4] = StringDoer("ab", &doThis);
lookup[6] = StringDoer("ab", &doThis);
lookup[2] = StringDoer("cd", &doThat);
lookup[5] = StringDoer("cd", &doThat);
...
// The checking is unaffected by the size of things...
Lookup::const_iterator i = lookup.find(location);
if (i!=lookup.end())
{
if ((*i).second.first.find(charArray[location])!=std::string::npos)
{
// Call the doer for this location/char combination
(*i).second.second->doSomething();
}
}