17#include <nlohmann/json.hpp>
20#include "FileSystem.hpp"
21#include "StringMap.hpp"
22#include "MonotonicIdAllocator.hpp"
86 using StringList = std::vector<std::string>;
99 struct Value :
public std::variant<bool, int64_t, double, std::string, StringList>
102 using std::variant<bool, int64_t, double, std::string, StringList>::variant;
105 bool is_bool() const noexcept {
return std::holds_alternative<bool>(*
this); }
107 bool is_integer() const noexcept {
return std::holds_alternative<int64_t>(*
this); }
109 bool is_double() const noexcept {
return std::holds_alternative<double>(*
this); }
111 bool is_string() const noexcept {
return std::holds_alternative<std::string>(*
this); }
113 bool is_stringlist() const noexcept {
return std::holds_alternative<StringList>(*
this); }
117 bool to_bool()
const {
return std::get<bool>(*
this); }
120 int64_t
to_integer()
const {
return std::get<int64_t>(*
this); }
123 double to_double()
const {
return std::get<double>(*
this); }
126 std::string
to_string()
const {
return std::get<std::string>(*
this); }
175 using PimplPtr = std::unique_ptr<Pimpl>;
224 bool isEntryReadOnly(std::string_view configKey)
const {
return _entry(configKey).readOnly; }
226 std::string
entryDescription(std::string_view configKey)
const {
return _entry(configKey).description; }
230 Value entryValue(std::string_view configKey)
const {
return _entry(configKey).getter(); }
275 fs::path
const &
configPath() const noexcept {
return _configPath; }
292 Entry const & _entry(std::string_view configKey)
const;
293 Entry & _entry(std::string_view configKey);
296 fs::path _configPath;
310struct 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:142
std::string entryDescription(std::string_view configKey) const
Get an entry's description.
Definition SystemConfig.hpp:226
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:161
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:228
std::move_only_function< void(std::string_view, Value const &, Value const &)> Callback
Callback invoked when an entry changes.
Definition SystemConfig.hpp:155
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:224
~SystemConfig()
Destructor.
uint32_t Id
Opaque identifier used to deregister callbacks.
Definition SystemConfig.hpp:158
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.
fs::path const & configPath() const noexcept
Get the path of the latest configuration loaded.
Definition SystemConfig.hpp:275
void setEntryValue(std::string_view configKey, Value newValue)
Set an entry's value.
nlohmann::json queryJson(std::string_view path) const
Query a sub-section of the configuration as JSON.
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:146
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:230
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:165
Getter getter
Retrieves the current value (mutable: move_only_function call operator is non-const).
Definition SystemConfig.hpp:166
Value defaultValue
Default value if not specified elsewhere.
Definition SystemConfig.hpp:169
Setter setter
Applies a new value.
Definition SystemConfig.hpp:167
bool readOnly
If true, the entry is not meant to be modified at runtime.
Definition SystemConfig.hpp:170
std::string description
Human-readable description (UI/help text).
Definition SystemConfig.hpp:168
Variant type used to represent configuration values.
Definition SystemConfig.hpp:100
bool is_stringlist() const noexcept
Definition SystemConfig.hpp:113
int64_t to_integer() const
Get the value as int64_t.
Definition SystemConfig.hpp:120
bool is_integer() const noexcept
Definition SystemConfig.hpp:107
StringList to_stringlist() const
Get the value as std::vector<std::string>.
Definition SystemConfig.hpp:129
bool is_string() const noexcept
Definition SystemConfig.hpp:111
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:117
bool is_bool() const noexcept
Definition SystemConfig.hpp:105
std::string to_string() const
Get the value as std::string.
Definition SystemConfig.hpp:126
bool is_double() const noexcept
Definition SystemConfig.hpp:109
double to_double() const
Get the value as double.
Definition SystemConfig.hpp:123