| 1 |
#include <iostream> |
| 2 |
#include <string> |
| 3 |
#include <map> |
| 4 |
#include <sstream> |
| 5 |
|
| 6 |
//HEAD |
| 7 |
class Handler: private std::map<std::string, |
| 8 |
void (Handler::*)(std::string const &cmd)> |
| 9 |
//= |
| 10 |
{ |
| 11 |
//STATIC |
| 12 |
static value_type s_cmds[]; |
| 13 |
static value_type *const s_cmds_end; |
| 14 |
//= |
| 15 |
public: |
| 16 |
Handler(); |
| 17 |
void process(std::string const &line); |
| 18 |
|
| 19 |
private: |
| 20 |
void list(std::string const &line); |
| 21 |
}; |
| 22 |
|
| 23 |
//CONS |
| 24 |
inline Handler::Handler() |
| 25 |
: |
| 26 |
std::map<std::string, |
| 27 |
void (Handler::*)(std::string const &cmd)> |
| 28 |
(s_cmds, s_cmds_end) |
| 29 |
{} |
| 30 |
//= |
| 31 |
|
| 32 |
using namespace std; |
| 33 |
|
| 34 |
Handler::value_type Handler::s_cmds[] = |
| 35 |
{ |
| 36 |
value_type("list", &Handler::list), |
| 37 |
}; |
| 38 |
|
| 39 |
Handler::value_type *const Handler::s_cmds_end = |
| 40 |
s_cmds + |
| 41 |
sizeof(s_cmds) / sizeof(Handler::value_type); |
| 42 |
//PROCESS |
| 43 |
void Handler::process(std::string const &line) |
| 44 |
{ |
| 45 |
istringstream istr(line); |
| 46 |
string cmd; |
| 47 |
istr >> cmd; |
| 48 |
for (iterator it = begin(); it != end(); it++) |
| 49 |
{ |
| 50 |
if (it->first.find(cmd) == 0) |
| 51 |
{ |
| 52 |
(this->*it->second)(line); |
| 53 |
return; |
| 54 |
} |
| 55 |
} |
| 56 |
cout << "Unknown command: " << line << '\n'; |
| 57 |
} |
| 58 |
//= |
| 59 |
void Handler::list(std::string const &line) |
| 60 |
{ |
| 61 |
cout << "Handler::list() called using `" << line << "'\n"; |
| 62 |
} |
| 63 |
|
| 64 |
//MAIN |
| 65 |
int main() |
| 66 |
{ |
| 67 |
string line; |
| 68 |
Handler cmd; |
| 69 |
|
| 70 |
while (getline(cin, line)) |
| 71 |
cmd.process(line); |
| 72 |
} |
| 73 |
//= |