SHARE
TWEET

Untitled

a guest Dec 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <functional>
  5. #include <stdexcept>
  6. #include <sstream>
  7.  
  8.  
  9. using Func = std::function<void()>;
  10. using Comands = std::map<std::string, std::function<void()>>;
  11.  
  12. void first(){
  13.     std::cout << "first\n";
  14. }
  15. void second(){
  16.     std::cout << "second\n";
  17. }
  18. void third(){
  19.     std::cout << "third\n";
  20. }
  21.  
  22.  
  23. class Robot{
  24. private:
  25.     Comands comands;
  26. public:
  27.     void add(const std::string &comand, void (*func)()){
  28.         comands.emplace(std::make_pair(comand, func));
  29.     }
  30.     void run(const std::string& str){
  31.         std::stringstream buf(str);
  32.         std::string s;
  33.         while(buf >> s){
  34.             auto iter = comands.find(s);
  35.             if(iter != comands.end()) iter->second();
  36.             else throw std::invalid_argument("");
  37.         }
  38.     }
  39. };
  40.  
  41. struct MyCommand
  42. {
  43.     void doSomething()
  44.     {
  45.        
  46.     }
  47. };
  48.  
  49. int main(){
  50.     MyCommand b;
  51.     Robot robot;
  52.     robot.add("second", second);
  53. //    robot.add("second", b.doSomething);
  54.     robot.add("third", third);
  55.     robot.add("first", first);
  56.     try{
  57.         robot.run("third second first");
  58.     }catch(std::exception& e){
  59.         std::cout << "wow\n";
  60.     }
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top