Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
console_variables.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/export.hpp>
5
6#include <concepts>
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10#include <map>
11#include <optional>
12#include <string>
13#include <string_view>
14#include <variant>
15#include <vector>
16
17namespace aegis2d::core {
18
20struct VarFlags {
22 bool saved = false;
24 bool cheat = false;
26 bool read_only = false;
27};
28
30using VarValue = std::variant<bool, std::int64_t, double, std::string>;
31
33enum class VarResult : std::uint8_t {
35 Changed,
37 Unknown,
41 Cheat,
45 BadText,
46};
47
51[[nodiscard]] AEGIS2D_CORE_EXPORT std::string_view to_string(VarResult result) noexcept;
52
64
80class AEGIS2D_CORE_EXPORT ConsoleVariables {
81public:
89 void add(std::string_view name, VarValue default_value, std::string_view description,
90 VarFlags flags = {});
91
101 template <std::same_as<bool> Bool>
102 void add(std::string_view name, Bool default_value, std::string_view description,
103 VarFlags flags = {}) {
104 add(name, VarValue{default_value}, description, flags);
105 }
106
113 template <std::integral Integer>
114 requires(!std::same_as<Integer, bool>)
115 void add(std::string_view name, Integer default_value, std::string_view description,
116 VarFlags flags = {}) {
117 add(name, VarValue{static_cast<std::int64_t>(default_value)}, description, flags);
118 }
119
126 void add(std::string_view name, const char* default_value, std::string_view description,
127 VarFlags flags = {});
128
132 [[nodiscard]] bool contains(std::string_view name) const;
133
138 [[nodiscard]] const VarInfo& info(std::string_view name) const;
139
146 template <typename Value>
147 [[nodiscard]] const Value& get(std::string_view name) const {
148 return std::get<Value>(info(name).value);
149 }
150
155 [[nodiscard]] std::string text(std::string_view name) const;
156
161 VarResult set(std::string_view name, VarValue value);
162
167 VarResult set_text(std::string_view name, std::string_view text);
168
172 void reset(std::string_view name);
173
175 void reset_all();
176
179 void set_cheats(bool enabled) noexcept;
180
183 [[nodiscard]] bool cheats() const noexcept;
184
187 [[nodiscard]] std::vector<std::string_view> names() const;
188
191 [[nodiscard]] std::size_t size() const noexcept;
192
193private:
194 std::map<std::string, VarInfo, std::less<>> variables_;
195 bool cheats_ = false;
196};
197
202[[nodiscard]] AEGIS2D_CORE_EXPORT std::string to_text(const VarValue& value);
203
210[[nodiscard]] AEGIS2D_CORE_EXPORT std::optional<VarValue> read_value(std::string_view text,
211 const VarValue& like);
212
217[[nodiscard]] AEGIS2D_CORE_EXPORT bool is_console_name(std::string_view name) noexcept;
218
219} // namespace aegis2d::core
The settings a console reads and changes by name while the game runs.
Definition console_variables.hpp:80
void reset_all()
Puts every variable back at its default.
std::string text(std::string_view name) const
Value of a variable as text, as set_text() reads it back.
void add(std::string_view name, Bool default_value, std::string_view description, VarFlags flags={})
Declares a boolean variable.
Definition console_variables.hpp:102
void add(std::string_view name, Integer default_value, std::string_view description, VarFlags flags={})
Declares a variable from an integer, kept as a 64-bit integer.
Definition console_variables.hpp:115
const Value & get(std::string_view name) const
Value of a variable, in its type.
Definition console_variables.hpp:147
VarResult set(std::string_view name, VarValue value)
Changes a variable, in its type; an integer sets a number too.
bool cheats() const noexcept
Tells whether cheats are on.
void add(std::string_view name, const char *default_value, std::string_view description, VarFlags flags={})
Declares a text variable from a C string.
const VarInfo & info(std::string_view name) const
A variable, as declared, with its value now.
void set_cheats(bool enabled) noexcept
Turns cheats on or off; cheat variables keep their values.
VarResult set_text(std::string_view name, std::string_view text)
Changes a variable from text, as a console or a command line gives it.
bool contains(std::string_view name) const
Tells whether a variable is declared.
void reset(std::string_view name)
Puts a variable back at its default, whatever its flags.
void add(std::string_view name, VarValue default_value, std::string_view description, VarFlags flags={})
Declares a variable, at its default.
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::variant< bool, std::int64_t, double, std::string > VarValue
Value of a console variable: a boolean, an integer, a number or a text.
Definition console_variables.hpp:30
std::string_view to_string(VarResult result) noexcept
Sentence for a VarResult, as a console shows it.
VarResult
What became of a change of a console variable.
Definition console_variables.hpp:33
@ ReadOnly
The variable is read-only.
@ Cheat
The variable is a cheat, and cheats are off.
@ BadText
The text does not read as a value of the variable's type.
@ Changed
The variable has its new value.
@ WrongType
The value is of another type than the variable's.
@ Unknown
No command and no variable has this name.
Definition endpoint.hpp:85
What a console variable allows: {.saved = true, .cheat = true}.
Definition console_variables.hpp:20
bool read_only
Keeps its default: only the code that declares it sets it.
Definition console_variables.hpp:26
bool saved
Kept in the player's settings between runs.
Definition console_variables.hpp:22
bool cheat
Changes only while cheats are on (ConsoleVariables::set_cheats()).
Definition console_variables.hpp:24
A console variable as declared, with its value now.
Definition console_variables.hpp:54
VarValue value
Value now.
Definition console_variables.hpp:56
VarValue default_value
Value it was declared with, which reset() restores.
Definition console_variables.hpp:58
std::string description
What it is for, as a console's help shows it.
Definition console_variables.hpp:60
VarFlags flags
Saved, cheat, read-only.
Definition console_variables.hpp:62