SILICON
Toggle main menu visibility
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
28
namespace
SILICON::core
{
29
30
class
ActiveKeyGuard
{
31
public
:
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
47
~ActiveKeyGuard
()
48
{
49
if
(
pushed
)
50
stack
.pop_back();
51
}
52
53
ActiveKeyGuard
(
const
ActiveKeyGuard
&) =
delete
;
54
ActiveKeyGuard
&
operator=
(
const
ActiveKeyGuard
&) =
delete
;
55
56
private
:
57
std::vector<std::string>&
stack
;
58
bool
pushed
=
false
;
59
};
60
61
}
// namespace SILICON::core
SILICON::core::ActiveKeyGuard::ActiveKeyGuard
ActiveKeyGuard(const ActiveKeyGuard &)=delete
SILICON::core::ActiveKeyGuard::stack
std::vector< std::string > & stack
Definition
activeKeyGuard.hpp:57
SILICON::core::ActiveKeyGuard::pushed
bool pushed
Definition
activeKeyGuard.hpp:58
SILICON::core::ActiveKeyGuard::~ActiveKeyGuard
~ActiveKeyGuard()
Definition
activeKeyGuard.hpp:47
SILICON::core::ActiveKeyGuard::ActiveKeyGuard
ActiveKeyGuard(std::vector< std::string > &stack, std::string key, const std::string_view errorPrefix)
Definition
activeKeyGuard.hpp:32
SILICON::core::ActiveKeyGuard::operator=
ActiveKeyGuard & operator=(const ActiveKeyGuard &)=delete
SILICON::core
Definition
activeKeyGuard.hpp:28
src
core
activeKeyGuard.hpp
Generated by
1.17.0