Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <string>
- #include <cstdio>
- #include <functional>
- typedef std::function<void(std::string)> Func;
- class Menu
- {
- public:
- struct Entry
- {
- std::string cmd;
- Func func;
- };
- const class AppendHelper
- {
- public:
- AppendHelper(Menu &menu) : menu(menu) {}
- const AppendHelper &operator()(std::string cmd, Func func) const
- { menu._command_set.push_back({cmd, func}); return *this; }
- private:
- Menu &menu;
- } append_menu = {*this};
- // For debugging
- void list_commands()
- {
- for(Entry e : _command_set) {
- printf("%s\t: %X\n", e.cmd.c_str(), &e.func);
- }
- }
- private:
- std::vector<Entry> _command_set;
- };
- int main()
- {
- Menu menu;
- menu.append_menu
- ("hehe", [] (std::string input) {printf("hehe");})
- ("haha", [] (std::string input) {printf("haha");});
- menu.list_commands();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement