17#include "FileSystem.hpp"
18#include "StringMap.hpp"
19#include "MonotonicIdAllocator.hpp"
95 struct Value :
public std::variant<bool, int64_t, double, std::string>
98 using std::variant<bool, int64_t, double, std::string>::variant;
101 bool is_bool() const noexcept {
return std::holds_alternative<bool>(*
this); }
103 bool is_integer() const noexcept {
return std::holds_alternative<int64_t>(*
this); }
105 bool is_double() const noexcept {
return std::holds_alternative<double>(*
this); }
107 bool is_string() const noexcept {
return std::holds_alternative<std::string>(*
this); }
111 bool to_bool()
const {
return std::get<bool>(*
this); }
114 int64_t
to_integer()
const {
return std::get<int64_t>(*
this); }
117 double to_double()
const {
return std::get<double>(*
this); }
120 std::string
to_string()
const {
return std::get<std::string>(*
this); }
166 using PimplPtr = std::unique_ptr<Pimpl>;
215 bool isEntryReadOnly(std::string_view configKey)
const {
return _entry(configKey).readOnly; }
217 std::string
entryDescription(std::string_view configKey)
const {
return _entry(configKey).description; }
221 Value entryValue(std::string_view configKey)
const {
return _entry(configKey).getter(); }
272 Entry const & _entry(std::string_view configKey)
const;
273 Entry & _entry(std::string_view configKey);
288struct std::formatter<
dfx::Utils::SystemConfig::Value> :
public formatter<std::string_view>
Monotonically increasing ID allocator.
Definition MonotonicIdAllocator.hpp:52
SystemConfig(fs::path const &path, UnorderedStringMap< Value > cliOverride={})
Construct and load configuration from a file.
void deregisterCallback(Id id)
Deregister a callback by id.
std::move_only_function< void(Value)> Setter
Setter callable used by an entry to apply a new value. Stored as std::move_only_function,...
Definition SystemConfig.hpp:133
std::string entryDescription(std::string_view configKey) const
Get an entry's description.
Definition SystemConfig.hpp:217
Id registerCallback(std::string_view configKey, Callback cb)
Register a callback for a specific configuration key.
std::unordered_map< Id, Callback > Callbacks
Mapping from callback id to callback implementation.
Definition SystemConfig.hpp:152
void removeEntry(std::string_view key)
Remove a configuration entry.
Value entryDefaultValue(std::string_view configKey) const
Get an entry's default value.
Definition SystemConfig.hpp:219
std::move_only_function< void(std::string_view, Value const &, Value const &)> Callback
Callback invoked when an entry changes.
Definition SystemConfig.hpp:146
void addEntry(std::string key, Value defaultValue, std::string description, Getter getter, Setter setter, bool readOnly=false)
Add a configuration entry.
bool isEntryReadOnly(std::string_view configKey) const
Check whether an entry is read-only.
Definition SystemConfig.hpp:215
~SystemConfig()
Destructor.
uint32_t Id
Opaque identifier used to deregister callbacks.
Definition SystemConfig.hpp:149
void loadConfig(fs::path const &path)
Load (or reload) configuration values from a file.
bool hasEntry(std::string_view key) const
Check whether an entry exists.
SystemConfig(UnorderedStringMap< Value > cliOverride={})
Construct an empty configuration registry.
void setEntryValue(std::string_view configKey, Value newValue)
Set an entry's value.
std::move_only_function< Value()> Getter
Getter callable used by an entry to retrieve the current value. Stored as std::move_only_function,...
Definition SystemConfig.hpp:137
std::unordered_map< std::string, Value > allEntryValues() const
Retrieve the current values for all registered entries.
Value entryValue(std::string_view configKey) const
Get an entry's current value via its getter.
Definition SystemConfig.hpp:221
Definition SystemConfigCommandHandler.hpp:15
std::unordered_map< std::string, T, TransparentHash, TransparentEqual, Allocator > UnorderedStringMap
Convenience alias for an unordered map keyed by std::string with transparent lookup.
Definition StringMap.hpp:76
Definition Message.hpp:21
Metadata and accessors for a single configuration entry.
Definition SystemConfig.hpp:156
Getter getter
Retrieves the current value (mutable: move_only_function call operator is non-const).
Definition SystemConfig.hpp:157
Value defaultValue
Default value if not specified elsewhere.
Definition SystemConfig.hpp:160
Setter setter
Applies a new value.
Definition SystemConfig.hpp:158
bool readOnly
If true, the entry is not meant to be modified at runtime.
Definition SystemConfig.hpp:161
std::string description
Human-readable description (UI/help text).
Definition SystemConfig.hpp:159
Variant type used to represent configuration values.
Definition SystemConfig.hpp:96
int64_t to_integer() const
Get the value as int64_t.
Definition SystemConfig.hpp:114
bool is_integer() const noexcept
Definition SystemConfig.hpp:103
bool is_string() const noexcept
Definition SystemConfig.hpp:107
std::string convert_to_string() const
Convert the held value to a string representation.
bool to_bool() const
Get the value as bool.
Definition SystemConfig.hpp:111
bool is_bool() const noexcept
Definition SystemConfig.hpp:101
std::string to_string() const
Get the value as std::string.
Definition SystemConfig.hpp:120
bool is_double() const noexcept
Definition SystemConfig.hpp:105
double to_double() const
Get the value as double.
Definition SystemConfig.hpp:117