dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
SystemConfig.hpp
1// SPDX-FileCopyrightText: 2025-2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <functional>
13#include <string_view>
14#include <variant>
15
16// Third-party includes
17#include <nlohmann/json.hpp>
18
19// Project includes
20#include "FileSystem.hpp"
21#include "StringMap.hpp"
22#include "MonotonicIdAllocator.hpp"
23
24namespace dfx::Utils
25{
84{
85public:
86 using StringList = std::vector<std::string>;
87
99 struct Value : public std::variant<bool, int64_t, double, std::string, StringList>
100 {
102 using std::variant<bool, int64_t, double, std::string, StringList>::variant;
103
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); }
114
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); }
129 StringList to_stringlist() const { return std::get<StringList>(*this); }
130
136 std::string convert_to_string() const;
137 };
138
139public:
142 using Setter = std::move_only_function<void (Value)>;
143
146 using Getter = std::move_only_function<Value ()>;
147
155 using Callback = std::move_only_function<void (std::string_view, Value const &, Value const &)>;
156
158 using Id = uint32_t;
159
161 using Callbacks = std::unordered_map<Id, Callback>;
162
164 struct Entry
165 {
166 mutable Getter getter;
168 std::string description;
170 bool readOnly = false;
171 };
172
173private:
174 struct Pimpl;
175 using PimplPtr = std::unique_ptr<Pimpl>;
176
177public:
184 explicit SystemConfig(UnorderedStringMap<Value> cliOverride = {});
185
192 explicit SystemConfig(fs::path const & path, UnorderedStringMap<Value> cliOverride = {});
193
196
197public:
210 void addEntry(std::string key, Value defaultValue, std::string description,
211 Getter getter, Setter setter, bool readOnly = false);
212
218 void removeEntry(std::string_view key);
219
221 bool hasEntry(std::string_view key) const;
222
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; }
228 Value entryDefaultValue(std::string_view configKey) const { return _entry(configKey).defaultValue; }
230 Value entryValue(std::string_view configKey) const { return _entry(configKey).getter(); }
231
251 void setEntryValue(std::string_view configKey, Value newValue);
252
259 nlohmann::json queryJson(std::string_view path) const;
260
266 std::unordered_map<std::string, Value> allEntryValues() const;
267
268public:
272 void loadConfig(fs::path const & path);
273
275 fs::path const & configPath() const noexcept { return _configPath; }
276
277public:
283 Id registerCallback(std::string_view configKey, Callback cb);
290
291private:
292 Entry const & _entry(std::string_view configKey) const;
293 Entry & _entry(std::string_view configKey);
294
295private:
296 fs::path _configPath;
297
298 UnorderedStringMap<Value> _cliOverride;
301
302 MonotonicIdAllocator<Id> _idAllocator;
303
304private:
305 PimplPtr _pimpl;
306};
307} // !namespace dfx::Utils
308
309template<>
310struct std::formatter<dfx::Utils::SystemConfig::Value> : public formatter<std::string_view>
311{
312 inline auto format(dfx::Utils::SystemConfig::Value const & val, format_context & ctx) const
313 { return formatter<std::string_view>::format(val.convert_to_string(), ctx); }
314};
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