Advertisement
haochen_xie

appender

Mar 17th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <vector>
  2. #include <string>
  3. #include <cstdio>
  4. #include <functional>
  5.  
  6. typedef std::function<void(std::string)> Func;
  7. class Menu
  8. {
  9. public:
  10.   struct Entry
  11.   {
  12.     std::string cmd;
  13.     Func func;
  14.   };
  15.   const class AppendHelper
  16.   {
  17.   public:
  18.     AppendHelper(Menu &menu) : menu(menu) {}
  19.     const AppendHelper &operator()(std::string cmd, Func func) const
  20.     { menu._command_set.push_back({cmd, func}); return *this; }
  21.   private:
  22.     Menu &menu;
  23.   } append_menu = {*this};
  24.  
  25.   // For debugging
  26.   void list_commands()
  27.   {
  28.     for(Entry e : _command_set) {
  29.       printf("%s\t: %X\n", e.cmd.c_str(), &e.func);
  30.     }
  31.   }
  32.  
  33. private:
  34.   std::vector<Entry> _command_set;
  35. };
  36.  
  37. int main()
  38. {
  39.   Menu menu;
  40.   menu.append_menu
  41.     ("hehe", [] (std::string input) {printf("hehe");})
  42.     ("haha", [] (std::string input) {printf("haha");});
  43.  
  44.   menu.list_commands();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement