stamen

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

commit dbf1d6332cb703654cdac22af736871ab192cf66
parent a0f03cd75241bbb601e1b2eb7abbaa392ea9a3d1
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 16 Nov 2023 13:53:06 +0000

Remove copy constructor with a hack

Diffstat:
Minclude/menu.h | 24++++++++++++++----------
Msrc/menu.cpp | 2+-
2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/include/menu.h b/include/menu.h @@ -13,14 +13,20 @@ #include <vector> class Menu { - // Menu(const Menu &) = delete; - // Menu &operator=(const Menu &) = delete; + Menu(const Menu &) = delete; + Menu &operator=(const Menu &) = delete; - friend class std::allocator<std::pair<const std::string, Menu>>; + struct private_ctor_t {}; public: typedef int (*callback_f)(void); + Menu(private_ctor_t, const std::string &code, const std::string &prompt) + : Menu(code, prompt) {} + + Menu(private_ctor_t, const std::string &code, const callback_f callback) + : Menu(code, callback) {} + class EMenu : std::exception { virtual const char *what() const noexcept override { return "Trying to access an unknown menu"; @@ -57,10 +63,9 @@ public: ss >> delim >> code; ss.ignore(1, ' '), std::getline(ss, prompt); if (delim == "+") { - // const auto [iter, succ] = lookup.emplace( - // std::piecewise_construct, std::forward_as_tuple(code), - // std::forward_as_tuple(code, prompt)); - const auto [iter, succ] = lookup.insert({code, Menu(code, prompt)}); + const auto [iter, succ] = lookup.emplace( + std::piecewise_construct, std::forward_as_tuple(code), + std::forward_as_tuple(private_ctor_t{}, code, prompt)); last = iter; } else { last->second.items.push_back({code, prompt}); @@ -70,9 +75,8 @@ public: static int start(const std::string &entry) { return getMenu(entry)(); } static void insert(const std::string &code, const callback_f callback) { - // lookup.emplace(std::piecewise_construct, std::forward_as_tuple(code), - // std::forward_as_tuple(code, callback)); - lookup.insert({code, Menu(code, callback)}); + lookup.emplace(std::piecewise_construct, std::forward_as_tuple(code), + std::forward_as_tuple(private_ctor_t{}, code, callback)); } static void print(const std::string &entry) { print(entry, 1); } diff --git a/src/menu.cpp b/src/menu.cpp @@ -1,4 +1,4 @@ -#include "menu.h" +#include "../include/menu.h" #include <cmath> #include <format> #include <fstream>