Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <functional>
- #include <stdexcept>
- #include <sstream>
- using Func = std::function<void()>;
- using Comands = std::map<std::string, std::function<void()>>;
- void first(){
- std::cout << "first\n";
- }
- void second(){
- std::cout << "second\n";
- }
- void third(){
- std::cout << "third\n";
- }
- class Robot{
- private:
- Comands comands;
- public:
- void add(const std::string &comand, void (*func)()){
- comands.emplace(std::make_pair(comand, func));
- }
- void run(const std::string& str){
- std::stringstream buf(str);
- std::string s;
- while(buf >> s){
- auto iter = comands.find(s);
- if(iter != comands.end()) iter->second();
- else throw std::invalid_argument("");
- }
- }
- };
- struct MyCommand
- {
- void doSomething()
- {
- }
- };
- int main(){
- MyCommand b;
- Robot robot;
- robot.add("second", second);
- // robot.add("second", b.doSomething);
- robot.add("third", third);
- robot.add("first", first);
- try{
- robot.run("third second first");
- }catch(std::exception& e){
- std::cout << "wow\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement