stamen

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

commit 0ca7539acca2a05ac12c268598ee3cd1881515fc
parent d5a343ef60d087a9e9ed648a14478f491ffe5a56
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri,  1 Dec 2023 22:42:12 +0000

Modernize the codebase using clang-tidy

Diffstat:
MCMakeLists.txt | 2+-
Mdemo/main.cpp | 10+++++-----
Minclude/stamen.h | 27+++++++++++++++------------
Minclude/stamenc.h | 2+-
Msrc/generate.cpp | 8+++++---
Msrc/stamen.cpp | 7++++---
6 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project( Stamen - VERSION 0.0.14 + VERSION 0.0.15 DESCRIPTION "Static menu generator" LANGUAGES C CXX ) diff --git a/demo/main.cpp b/demo/main.cpp @@ -7,30 +7,30 @@ // in order to use stamen::BuiltinDisplay const stamen_display_f stamen_display = stamen::builtinDisplay; -int operation1(void) { +int operation1() { std::cout << "operation 1" << std::endl; std::cout << "Some operation is done" << std::endl; return 1; } -int operation2(void) { +int operation2() { std::cout << "operation 2" << std::endl; std::cout << "Some other operation is done" << std::endl; return 1; } -int operation3(void) { +int operation3() { std::cout << "operation 3" << std::endl; std::cout << "Yet another operation is done" << std::endl; return 1; } -int finish(void) { +int finish() { std::cout << "finishing..." << std::endl; exit(0); } -int main(void) { +int main() { stamen::menu_main(); return 0; } diff --git a/include/stamen.h b/include/stamen.h @@ -11,6 +11,7 @@ #include <string> #include <tuple> #include <unordered_map> +#include <utility> #include <vector> namespace stamen { @@ -21,11 +22,13 @@ class Menu { public: friend class Generator; - typedef stamen_callback_f callback_f; - typedef stamen_display_f display_f; + using callback_f = stamen_callback_f; + using display_f = stamen_display_f; Menu(const Menu &) = delete; + Menu(Menu &&) = delete; Menu &operator=(const Menu &) = delete; + Menu &operator=(Menu &&) = delete; // Tag type dispatch Menu(private_ctor_t, const std::string &code, const std::string &prompt) @@ -39,14 +42,14 @@ public: static void insert(const std::string &code, const callback_f callback); private: - Menu(const std::string &code, const std::string &prompt) - : code(code), title(prompt) {} + Menu(std::string code, std::string prompt) + : code(std::move(code)), title(std::move(prompt)) {} Menu(const std::string &code, const callback_f callback) : code(code), title(code), callback(callback) {} - typedef std::unordered_map<std::string, Menu> lookup_t; - static lookup_t &getLookup(void) { + using lookup_t = std::unordered_map<std::string, Menu>; + static lookup_t &getLookup() { static lookup_t lookup; return lookup; } @@ -54,7 +57,7 @@ private: static void internal_print(const std::string &entry, const int depth); static const Menu *getMenu(const std::string &code) { - static lookup_t &lookup = getLookup(); + const static lookup_t &lookup = getLookup(); const auto it = lookup.find(code); if (it == lookup.end()) return nullptr; return &it->second; @@ -63,17 +66,17 @@ private: const std::string code, title; const callback_f callback = nullptr; - typedef std::pair<std::string, std::string> lookup_item_t; + using lookup_item_t = std::pair<std::string, std::string>; std::vector<lookup_item_t> items; }; inline void Menu::read(const std::string &s) { std::string line, delim, code, prompt; std::fstream fs(s); - char tmp; + char tmp = 0; lookup_t &lookup = getLookup(); - lookup_t::iterator last = lookup.end(); + auto last = lookup.end(); while (std::getline(fs, line)) { if (line.empty()) continue; std::istringstream ss(line); @@ -85,7 +88,7 @@ inline void Menu::read(const std::string &s) { std::forward_as_tuple(private_ctor_t{}, code, prompt)); last = iter; } else { - last->second.items.push_back({code, prompt}); + last->second.items.emplace_back(code, prompt); } } } @@ -110,7 +113,7 @@ inline void Menu::internal_print(const std::string &code, const int depth) { } } -int builtinDisplay(const char *title, const ::item_t items[], int size); +int builtinDisplay(const char *title, const ::item_t itemv[], int size); } // namespace stamen diff --git a/include/stamenc.h b/include/stamenc.h @@ -12,6 +12,6 @@ EXTERNC void stamen_read(const char *file); EXTERNC void stamen_print(const char *entry); EXTERNC void stamen_insert(const char *code, stamen_callback_f callback); -EXTERNC int stamen_builtin_display(const char *title, const item_t items[], int size); +EXTERNC int stamen_builtin_display(const char *title, const item_t itemv[], int size); #endif diff --git a/src/generate.cpp b/src/generate.cpp @@ -8,7 +8,7 @@ namespace stamen { class Generator { public: class EGenerate : std::exception { - virtual const char *what() const noexcept override { + [[nodiscard]] const char *what() const noexcept override { return "Trying to access unknown code"; } }; @@ -56,14 +56,16 @@ public: } // namespace stamen int main(const int argc, const char *argv[]) { + const auto args = std::span(argv, size_t(argc)); + if (argc != 2 && argc != 3) { std::cout << "please enter exaclty one config file" << std::endl; return 1; } - const bool cpp = argc == 2 || std::string(argv[2]) == "cpp"; + const bool cpp = argc == 2 || std::string(args[2]) == "cpp"; - std::string path = argv[1]; + std::string path = args[1]; Menu::read(path); std::string::size_type pos = path.rfind('.'); diff --git a/src/stamen.cpp b/src/stamen.cpp @@ -7,9 +7,10 @@ namespace stamen { -int builtinDisplay(const char *title, const ::item_t items[], int size) { - const int digits = std::log10(size) + 1; - int choice; +int builtinDisplay(const char *title, const ::item_t itemv[], int size) { + const size_t digits = size_t(std::log10(size)) + 1; + const auto items = std::span(itemv, size_t(size)); + int choice = 0; while (true) { std::cout << std::format("{}:\n", title);