![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
System configuration registry with typed values, entry metadata, and change callbacks. More...
#include <dfx-utilities/SystemConfig.hpp>
Classes | |
| struct | Entry |
| Metadata and accessors for a single configuration entry. More... | |
| struct | Value |
| Variant type used to represent configuration values. More... | |
Public Types | |
| using | Setter = std::move_only_function<void (Value)> |
Setter callable used by an entry to apply a new value. Stored as std::move_only_function, so it can capture move-only state. | |
| using | Getter = std::move_only_function<Value ()> |
Getter callable used by an entry to retrieve the current value. Stored as std::move_only_function, so it can capture move-only state. | |
| using | Callback = std::move_only_function<void (std::string_view, Value const &, Value const &)> |
| Callback invoked when an entry changes. | |
| using | Id = uint32_t |
| Opaque identifier used to deregister callbacks. | |
| using | Callbacks = std::unordered_map<Id, Callback> |
| Mapping from callback id to callback implementation. | |
Public Member Functions | |
| SystemConfig (UnorderedStringMap< Value > cliOverride={}) | |
| Construct an empty configuration registry. | |
| SystemConfig (fs::path const &path, UnorderedStringMap< Value > cliOverride={}) | |
| Construct and load configuration from a file. | |
| ~SystemConfig () | |
| Destructor. | |
| void | addEntry (std::string key, Value defaultValue, std::string description, Getter getter, Setter setter, bool readOnly=false) |
| Add a configuration entry. | |
| void | removeEntry (std::string_view key) |
| Remove a configuration entry. | |
| bool | hasEntry (std::string_view key) const |
| Check whether an entry exists. | |
| bool | isEntryReadOnly (std::string_view configKey) const |
| Check whether an entry is read-only. | |
| std::string | entryDescription (std::string_view configKey) const |
| Get an entry's description. | |
| Value | entryDefaultValue (std::string_view configKey) const |
| Get an entry's default value. | |
| Value | entryValue (std::string_view configKey) const |
| Get an entry's current value via its getter. | |
| void | setEntryValue (std::string_view configKey, Value newValue) |
| Set an entry's value. | |
| std::unordered_map< std::string, Value > | allEntryValues () const |
| Retrieve the current values for all registered entries. | |
| void | loadConfig (fs::path const &path) |
| Load (or reload) configuration values from a file. | |
| Id | registerCallback (std::string_view configKey, Callback cb) |
| Register a callback for a specific configuration key. | |
| void | deregisterCallback (Id id) |
| Deregister a callback by id. | |
System configuration registry with typed values, entry metadata, and change callbacks.
The configuration holds a set of entries. Each entry provides:
Values are stored as Value, a small tagged union supporting boolean, integer, floating-point, and string types.
Setters are free to normalize, clamp, or reject values. A change is only considered effective if the value retrieved after applying the setter differs from the previous value. Callbacks are invoked only in that case.
CLI-provided overrides can be supplied at construction time and take precedence over defaults and file-loaded values.
The following example registers a configuration entry controlling the size of a thread pool. The owning component provides both the getter and the setter, allowing it to validate and apply changes immediately:
In this example:
| using dfx::Utils::SystemConfig::Callback = std::move_only_function<void (std::string_view, Value const &, Value const &)> |
Callback invoked when an entry changes.
| key | The configuration key that changed. |
| oldValue | Previous value. |
| newValue | New value. |
| using dfx::Utils::SystemConfig::Callbacks = std::unordered_map<Id, Callback> |
Mapping from callback id to callback implementation.
| using dfx::Utils::SystemConfig::Getter = std::move_only_function<Value ()> |
Getter callable used by an entry to retrieve the current value. Stored as std::move_only_function, so it can capture move-only state.
| using dfx::Utils::SystemConfig::Id = uint32_t |
Opaque identifier used to deregister callbacks.
| using dfx::Utils::SystemConfig::Setter = std::move_only_function<void (Value)> |
Setter callable used by an entry to apply a new value. Stored as std::move_only_function, so it can capture move-only state.
| dfx::Utils::SystemConfig::SystemConfig | ( | UnorderedStringMap< Value > | cliOverride = {} | ) |
Construct an empty configuration registry.
| cliOverride | Optional key/value overrides (typically from CLI parsing). |
Overrides provided here are stored and can be applied with higher priority than defaults and/or file-loaded values (depending on implementation).
| dfx::Utils::SystemConfig::SystemConfig | ( | fs::path const & | path, |
| UnorderedStringMap< Value > | cliOverride = {} ) |
Construct and load configuration from a file.
| path | Path to the configuration file to load. |
| cliOverride | Optional key/value overrides (typically from CLI parsing). |
Equivalent to constructing with overrides, then calling loadConfig.
| dfx::Utils::SystemConfig::~SystemConfig | ( | ) |
Destructor.
| void dfx::Utils::SystemConfig::addEntry | ( | std::string | key, |
| Value | defaultValue, | ||
| std::string | description, | ||
| Getter | getter, | ||
| Setter | setter, | ||
| bool | readOnly = false ) |
Add a configuration entry.
| key | Entry key |
| defaultValue | Default value. |
| description | Human-readable description. |
| getter | Callable returning the current value. |
| setter | Callable applying a new value. |
| readOnly | If true, the entry is treated as read-only. |
After registration, the entry can be queried with hasEntry and accessed with the various entry* methods. Setting a value via setEntryValue may trigger callbacks registered with registerCallback.
| std::unordered_map< std::string, Value > dfx::Utils::SystemConfig::allEntryValues | ( | ) | const |
Retrieve the current values for all registered entries.
Values are obtained via each entry's getter.
| void dfx::Utils::SystemConfig::deregisterCallback | ( | Id | id | ) |
Deregister a callback by id.
| id | Callback id previously returned by registerCallback. |
id is unknown, this is a no-op
|
inline |
Get an entry's default value.
|
inline |
Get an entry's description.
|
inline |
Get an entry's current value via its getter.
| bool dfx::Utils::SystemConfig::hasEntry | ( | std::string_view | key | ) | const |
Check whether an entry exists.
|
inline |
Check whether an entry is read-only.
| void dfx::Utils::SystemConfig::loadConfig | ( | fs::path const & | path | ) |
Load (or reload) configuration values from a file.
| path | Path to the configuration file. |
Register a callback for a specific configuration key.
| void dfx::Utils::SystemConfig::removeEntry | ( | std::string_view | key | ) |
Remove a configuration entry.
| key | Key of the entry to remove. |
After removal, querying the entry by key is invalid.
| void dfx::Utils::SystemConfig::setEntryValue | ( | std::string_view | configKey, |
| Value | newValue ) |
Set an entry's value.
| configKey | Entry key. |
| newValue | New value to apply. |
The update follows these steps:
newValue, the call is a no-op.newValue.Callbacks are only triggered if the value effectively changes after the setter has been applied. This allows setters to clamp, normalize, or reject values silently.