System configuration registry with typed values, entry metadata, and change callbacks.
More...
|
|
using | StringList = std::vector<std::string> |
| 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.
|
|
| | 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.
|
| nlohmann::json | queryJson (std::string_view path) const |
| | Query a sub-section of the configuration as JSON.
|
| 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.
|
| fs::path const & | configPath () const noexcept |
| | Get the path of the latest configuration loaded.
|
| 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:
- a default value,
- a human-readable description,
- a getter and setter supplied by the owning subsystem,
- an optional read-only flag.
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.
Example usage
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:
sysConfig.addEntry(
Keys::threadPoolSize,
1,
"Control the thread pool size. The thread pool will always contain at least 1 thread",
[this] noexcept { return _threadPool.threadCount(); },
{
DFX_ASSERT(value.is_integer(),
"The thread count value must be a valid integer");
auto const threadCount = value.to_integer();
DFX_ASSERT(threadCount >= 0,
"The thread count must be >= 0. Got {}", threadCount);
if (threadCount > _threadPool.threadCount())
_threadPool.addThread(threadCount - _threadPool.threadCount());
else
_threadPool.removeThread(_threadPool.threadCount() - threadCount);
}
);
#define DFX_ASSERT(expr, msg,...)
Assert-like check that throws an dfx::Utils::Exception on failure.
Definition Exception.hpp:268
Variant type used to represent configuration values.
Definition SystemConfig.hpp:100
In this example:
- The getter reflects the current runtime state.
- The setter validates the input and applies the change immediately.
- Invalid values are rejected via assertions.
- Callbacks (if registered) are only triggered when the effective thread count changes.
- Note
- Configuration entries do not store values internally; they delegate storage and validation entirely to the owning subsystem via getters and setters.