SILICON
Loading...
Searching...
No Matches
activeKeyGuard.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <format>
21#include <ranges>
22#include <sstream>
23#include <stdexcept>
24#include <string>
25#include <string_view>
26#include <vector>
27
28namespace SILICON::core {
29
31public:
32 ActiveKeyGuard(std::vector<std::string>& stack, std::string key,
33 const std::string_view errorPrefix)
34 : stack(stack)
35 {
36 if (std::ranges::contains(stack, key)) {
37 std::ostringstream trace;
38 for (const auto& value : stack)
39 trace << value << " -> ";
40 trace << key;
41 throw std::runtime_error(std::format("{}{}", errorPrefix, trace.str()));
42 }
43 stack.push_back(std::move(key));
44 pushed = true;
45 }
46
48 {
49 if (pushed)
50 stack.pop_back();
51 }
52
55
56private:
57 std::vector<std::string>& stack;
58 bool pushed = false;
59};
60
61} // namespace SILICON::core
ActiveKeyGuard(const ActiveKeyGuard &)=delete
std::vector< std::string > & stack
ActiveKeyGuard(std::vector< std::string > &stack, std::string key, const std::string_view errorPrefix)
ActiveKeyGuard & operator=(const ActiveKeyGuard &)=delete