stamen

Stamen - static menu generator
git clone git://git.dimitrijedobrota.com/stamen.git
Log | Files | Refs | README | LICENSE

menu.hpp (2039B)


      1 #ifndef STAMEN_MENU_HPP
      2 #define STAMEN_MENU_HPP
      3 
      4 #include "stamen.hpp"
      5 
      6 #include <cstring>
      7 #include <iostream>
      8 #include <string>
      9 #include <unordered_map>
     10 #include <vector>
     11 
     12 namespace stamen {
     13 namespace menu {
     14 
     15 class menu_t;
     16 
     17 extern std::unordered_map<std::string, callback_f> free_lookup;
     18 extern std::unordered_map<std::string, menu_t> menu_lookup;
     19 extern std::string display_stub_default;
     20 extern display_f display;
     21 
     22 void read(const char *filename);
     23 void insert(const char *code, callback_f callback);
     24 int dynamic(const char *code, display_f display);
     25 int display_stub(int idx);
     26 
     27 class menu_t {
     28     struct private_ctor_t {};
     29     friend void read(const char *filename);
     30 
     31   public:
     32     // Tag type dispatch
     33     menu_t(private_ctor_t, const std::string &code, const std::string &prompt)
     34         : menu_t(code, prompt) {}
     35 
     36     ~menu_t() noexcept {
     37         for (const auto [_, prompt] : items) {
     38             delete[] prompt;
     39         }
     40     }
     41 
     42     const std::string &getCode() const { return code; }
     43     const std::string &getTitle() const { return title; }
     44 
     45     const item_t *getItemv() const { return items.data(); }
     46     std::size_t getSize() const { return items.size(); }
     47 
     48     auto getCallback(std::size_t idx) const { return items[idx].callback; }
     49     const auto &getCode(std::size_t idx) const { return codes[idx].code; }
     50     const auto &getPrompt(std::size_t idx) const { return codes[idx].prompt; }
     51 
     52   private:
     53     menu_t(std::string code, std::string prompt)
     54         : code(std::move(code)), title(std::move(prompt)) {}
     55 
     56     menu_t(const menu_t &) = delete;
     57     menu_t &operator=(const menu_t &) = delete;
     58     menu_t(menu_t &&) = delete;
     59     menu_t &operator=(menu_t &&) = delete;
     60 
     61     void insert(const std::string &code, const std::string &prompt,
     62                 callback_f callback = display_stub);
     63 
     64     struct code_t {
     65         std::string code;
     66         std::string prompt;
     67     };
     68 
     69     const std::string code, title;
     70     std::vector<code_t> codes;
     71     std::vector<item_t> items;
     72 };
     73 
     74 } // namespace menu
     75 } // namespace stamen
     76 
     77 #endif