Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::configuration_manager Class Reference

Configuration manager for unified system configuration. More...

#include <configuration_manager.h>

Collaboration diagram for kcenon::thread::configuration_manager:
Collaboration graph

Public Types

using change_callback = std::function<void(const std::string&, const config_value&)>
 Configuration change callback.
 
using validator_func = std::function<validation_result(const std::string&, const config_value&)>
 Configuration validator.
 

Public Member Functions

 configuration_manager (std::shared_ptr< event_bus > bus=nullptr)
 Constructor.
 
bool load_from_file (const std::filesystem::path &config_file)
 Load configuration from file.
 
bool save_to_file (const std::filesystem::path &config_file) const
 Save configuration to file.
 
bool set (const std::string &path, const config_value &value)
 Set a configuration value.
 
template<typename T >
get (const std::string &path, const T &default_value=T{}) const
 Get a configuration value.
 
template<typename T >
std::optional< T > get_optional (const std::string &path) const
 Get a configuration value as optional.
 
bool has (const std::string &path) const
 Check if configuration exists.
 
bool remove (const std::string &path)
 Remove a configuration value.
 
std::size_t on_change (const std::string &path, change_callback callback)
 Register a change callback.
 
void remove_callback (const std::string &path, std::size_t id)
 Unregister a change callback.
 
void add_validator (const std::string &path, validator_func validator)
 Register a configuration validator.
 
validation_result validate_all () const
 Validate all configuration.
 
void apply_system_config (const std::string &system_name, const std::unordered_map< std::string, config_value > &config)
 Apply configuration for a specific system.
 
std::unordered_map< std::string, config_valueget_system_config (const std::string &system_name) const
 Get configuration for a specific system.
 
void clear ()
 Clear all configuration.
 

Static Public Member Functions

static configuration_managerinstance ()
 Get singleton instance.
 

Private Member Functions

void parse_config_line (const std::string &line)
 Parse a configuration line.
 
std::string value_to_string (const config_value &value) const
 Convert value to string.
 
bool values_equal (const config_value &a, const config_value &b) const
 Check if two values are equal.
 
void notify_change (const std::string &path, const config_value &value)
 Notify change callbacks.
 

Private Attributes

std::mutex mutex_
 
std::mutex callbacks_mutex_
 
std::unordered_map< std::string, config_valueconfig_
 
std::unordered_map< std::string, validator_funcvalidators_
 
std::unordered_map< std::string, std::unordered_map< std::size_t, change_callback > > callbacks_
 
std::shared_ptr< event_busevent_bus_
 
std::size_t next_callback_id_ {1}
 

Detailed Description

Configuration manager for unified system configuration.

Definition at line 61 of file configuration_manager.h.

Member Typedef Documentation

◆ change_callback

using kcenon::thread::configuration_manager::change_callback = std::function<void(const std::string&, const config_value&)>

Configuration change callback.

Definition at line 66 of file configuration_manager.h.

◆ validator_func

using kcenon::thread::configuration_manager::validator_func = std::function<validation_result(const std::string&, const config_value&)>

Configuration validator.

Definition at line 71 of file configuration_manager.h.

Constructor & Destructor Documentation

◆ configuration_manager()

kcenon::thread::configuration_manager::configuration_manager ( std::shared_ptr< event_bus > bus = nullptr)
inlineexplicit

Constructor.

Parameters
event_busOptional event bus for change notifications

Definition at line 77 of file configuration_manager.h.

78 : event_bus_(bus) {
79 if (!event_bus_) {
80 event_bus_ = std::make_shared<event_bus>();
81 }
82 }

References event_bus_.

Member Function Documentation

◆ add_validator()

void kcenon::thread::configuration_manager::add_validator ( const std::string & path,
validator_func validator )
inline

Register a configuration validator.

Parameters
pathConfiguration path
validatorValidator function

Definition at line 268 of file configuration_manager.h.

268 {
269 std::lock_guard<std::mutex> lock(mutex_);
270 validators_[path] = std::move(validator);
271 }
std::unordered_map< std::string, validator_func > validators_

References mutex_, and validators_.

◆ apply_system_config()

void kcenon::thread::configuration_manager::apply_system_config ( const std::string & system_name,
const std::unordered_map< std::string, config_value > & config )
inline

Apply configuration for a specific system.

Parameters
system_nameSystem name (e.g., "thread_system")
configConfiguration map

Definition at line 304 of file configuration_manager.h.

305 {
306 for (const auto& [key, value] : config) {
307 set(system_name + "." + key, value);
308 }
309 }
bool set(const std::string &path, const config_value &value)
Set a configuration value.

References set().

Here is the call graph for this function:

◆ clear()

void kcenon::thread::configuration_manager::clear ( )
inline

Clear all configuration.

Definition at line 334 of file configuration_manager.h.

334 {
335 std::lock_guard<std::mutex> lock(mutex_);
336 config_.clear();
337 }
std::unordered_map< std::string, config_value > config_

References config_, and mutex_.

◆ get()

template<typename T >
T kcenon::thread::configuration_manager::get ( const std::string & path,
const T & default_value = T{} ) const
inline

Get a configuration value.

Template Parameters
TValue type
Parameters
pathConfiguration path
default_valueDefault value if not found
Returns
Configuration value

Definition at line 182 of file configuration_manager.h.

182 {}) const {
183 std::lock_guard<std::mutex> lock(mutex_);
184 auto it = config_.find(path);
185 if (it != config_.end()) {
186 try {
187 return std::get<T>(it->second);
188 } catch (const std::bad_variant_access&) {
189 // Type mismatch
190 }
191 }
192 return default_value;
193 }

◆ get_optional()

template<typename T >
std::optional< T > kcenon::thread::configuration_manager::get_optional ( const std::string & path) const
inline

Get a configuration value as optional.

Template Parameters
TValue type
Parameters
pathConfiguration path
Returns
Optional configuration value

Definition at line 202 of file configuration_manager.h.

202 {
203 std::lock_guard<std::mutex> lock(mutex_);
204 auto it = config_.find(path);
205 if (it != config_.end()) {
206 try {
207 return std::get<T>(it->second);
208 } catch (const std::bad_variant_access&) {
209 // Type mismatch
210 }
211 }
212 return std::nullopt;
213 }

References config_, and mutex_.

◆ get_system_config()

std::unordered_map< std::string, config_value > kcenon::thread::configuration_manager::get_system_config ( const std::string & system_name) const
inline

Get configuration for a specific system.

Parameters
system_nameSystem name
Returns
Configuration map

Definition at line 316 of file configuration_manager.h.

317 {
318 std::unordered_map<std::string, config_value> result;
319 std::string prefix = system_name + ".";
320
321 std::lock_guard<std::mutex> lock(mutex_);
322 for (const auto& [key, value] : config_) {
323 if (key.starts_with(prefix)) {
324 result[key.substr(prefix.length())] = value;
325 }
326 }
327
328 return result;
329 }

References config_, and mutex_.

◆ has()

bool kcenon::thread::configuration_manager::has ( const std::string & path) const
inline

Check if configuration exists.

Parameters
pathConfiguration path
Returns
True if exists

Definition at line 220 of file configuration_manager.h.

220 {
221 std::lock_guard<std::mutex> lock(mutex_);
222 return config_.find(path) != config_.end();
223 }

References config_, and mutex_.

◆ instance()

static configuration_manager & kcenon::thread::configuration_manager::instance ( )
inlinestatic

Get singleton instance.

Returns
Configuration manager instance

Definition at line 343 of file configuration_manager.h.

343 {
345 return instance;
346 }
configuration_manager(std::shared_ptr< event_bus > bus=nullptr)
Constructor.
static configuration_manager & instance()
Get singleton instance.

References instance().

Referenced by instance().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_from_file()

bool kcenon::thread::configuration_manager::load_from_file ( const std::filesystem::path & config_file)
inline

Load configuration from file.

Parameters
config_filePath to configuration file
Returns
True if successful

Definition at line 89 of file configuration_manager.h.

89 {
90 if (!std::filesystem::exists(config_file)) {
91 return false;
92 }
93
94 try {
95 std::ifstream file(config_file);
96 if (!file.is_open()) {
97 return false;
98 }
99
100 // Simple key=value parser for demonstration
101 // In production, use JSON/YAML parser
102 std::string line;
103 while (std::getline(file, line)) {
104 parse_config_line(line);
105 }
106
107 return true;
108 } catch (...) {
109 return false;
110 }
111 }
void parse_config_line(const std::string &line)
Parse a configuration line.

References parse_config_line().

Here is the call graph for this function:

◆ notify_change()

void kcenon::thread::configuration_manager::notify_change ( const std::string & path,
const config_value & value )
inlineprivate

Notify change callbacks.

Definition at line 428 of file configuration_manager.h.

428 {
429 std::lock_guard<std::mutex> lock(callbacks_mutex_);
430
431 // Notify specific path callbacks
432 if (auto it = callbacks_.find(path); it != callbacks_.end()) {
433 for (const auto& [id, callback] : it->second) {
434 callback(path, value);
435 }
436 }
437
438 // Notify global callbacks
439 if (auto it = callbacks_.find(""); it != callbacks_.end()) {
440 for (const auto& [id, callback] : it->second) {
441 callback(path, value);
442 }
443 }
444 }
std::unordered_map< std::string, std::unordered_map< std::size_t, change_callback > > callbacks_
@ callback
Call user callback for custom decision.

References kcenon::thread::callback, callbacks_, and callbacks_mutex_.

Referenced by set().

Here is the caller graph for this function:

◆ on_change()

std::size_t kcenon::thread::configuration_manager::on_change ( const std::string & path,
change_callback callback )
inline

Register a change callback.

Parameters
pathConfiguration path (empty for all changes)
callbackCallback function
Returns
Callback ID for unregistration

Definition at line 241 of file configuration_manager.h.

241 {
242 std::lock_guard<std::mutex> lock(callbacks_mutex_);
243 auto id = next_callback_id_++;
244 callbacks_[path][id] = std::move(callback);
245 return id;
246 }

References kcenon::thread::callback, callbacks_, callbacks_mutex_, and next_callback_id_.

◆ parse_config_line()

void kcenon::thread::configuration_manager::parse_config_line ( const std::string & line)
inlineprivate

Parse a configuration line.

Definition at line 352 of file configuration_manager.h.

352 {
353 if (line.empty() || line[0] == '#' || line[0] == ';') {
354 return; // Skip comments and empty lines
355 }
356
357 auto pos = line.find('=');
358 if (pos != std::string::npos) {
359 std::string key = line.substr(0, pos);
360 std::string value_str = line.substr(pos + 1);
361
362 // Trim whitespace
363 key.erase(0, key.find_first_not_of(" \t"));
364 key.erase(key.find_last_not_of(" \t") + 1);
365 value_str.erase(0, value_str.find_first_not_of(" \t"));
366 value_str.erase(value_str.find_last_not_of(" \t") + 1);
367
368 // Parse value type
369 config_value value;
370 if (value_str == "true" || value_str == "false") {
371 value = (value_str == "true");
372 } else if (std::all_of(value_str.begin(), value_str.end(), ::isdigit)) {
373 value = std::stoi(value_str);
374 } else {
375 try {
376 value = std::stod(value_str);
377 } catch (...) {
378 value = value_str; // Default to string
379 }
380 }
381
382 set(key, value);
383 }
384 }
std::variant< bool, int, double, std::string, std::vector< std::string >, std::unordered_map< std::string, std::any > > config_value
Configuration value type.

References set().

Referenced by load_from_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove()

bool kcenon::thread::configuration_manager::remove ( const std::string & path)
inline

Remove a configuration value.

Parameters
pathConfiguration path
Returns
True if removed

Definition at line 230 of file configuration_manager.h.

230 {
231 std::lock_guard<std::mutex> lock(mutex_);
232 return config_.erase(path) > 0;
233 }

References config_, and mutex_.

◆ remove_callback()

void kcenon::thread::configuration_manager::remove_callback ( const std::string & path,
std::size_t id )
inline

Unregister a change callback.

Parameters
pathConfiguration path
idCallback ID

Definition at line 253 of file configuration_manager.h.

253 {
254 std::lock_guard<std::mutex> lock(callbacks_mutex_);
255 if (auto it = callbacks_.find(path); it != callbacks_.end()) {
256 it->second.erase(id);
257 if (it->second.empty()) {
258 callbacks_.erase(it);
259 }
260 }
261 }

References callbacks_, and callbacks_mutex_.

◆ save_to_file()

bool kcenon::thread::configuration_manager::save_to_file ( const std::filesystem::path & config_file) const
inline

Save configuration to file.

Parameters
config_filePath to configuration file
Returns
True if successful

Definition at line 118 of file configuration_manager.h.

118 {
119 try {
120 std::ofstream file(config_file);
121 if (!file.is_open()) {
122 return false;
123 }
124
125 std::lock_guard<std::mutex> lock(mutex_);
126 for (const auto& [key, value] : config_) {
127 file << key << "=" << value_to_string(value) << "\n";
128 }
129
130 return true;
131 } catch (...) {
132 return false;
133 }
134 }
std::string value_to_string(const config_value &value) const
Convert value to string.

References config_, mutex_, and value_to_string().

Here is the call graph for this function:

◆ set()

bool kcenon::thread::configuration_manager::set ( const std::string & path,
const config_value & value )
inline

Set a configuration value.

Parameters
pathConfiguration path (e.g., "thread_system.pool_size")
valueConfiguration value
Returns
True if successful

Definition at line 142 of file configuration_manager.h.

142 {
143 // Validate if validator exists
144 if (auto it = validators_.find(path); it != validators_.end()) {
145 auto result = it->second(path, value);
146 if (!result.is_valid) {
147 return false;
148 }
149 }
150
151 config_value old_value;
152 bool had_value = false;
153
154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 if (auto it = config_.find(path); it != config_.end()) {
157 old_value = it->second;
158 had_value = true;
159 }
160 config_[path] = value;
161 }
162
163 // Notify callbacks
164 notify_change(path, value);
165
166 // Publish event if value changed
167 if (event_bus_ && (!had_value || !values_equal(old_value, value))) {
168 event_bus_->publish(config_changed_event(path, old_value, value));
169 }
170
171 return true;
172 }
void notify_change(const std::string &path, const config_value &value)
Notify change callbacks.
bool values_equal(const config_value &a, const config_value &b) const
Check if two values are equal.

References config_, event_bus_, mutex_, notify_change(), validators_, and values_equal().

Referenced by apply_system_config(), and parse_config_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_all()

validation_result kcenon::thread::configuration_manager::validate_all ( ) const
inline

Validate all configuration.

Returns
Validation result

Definition at line 277 of file configuration_manager.h.

277 {
278 validation_result result;
279 std::lock_guard<std::mutex> lock(mutex_);
280
281 for (const auto& [path, value] : config_) {
282 if (auto it = validators_.find(path); it != validators_.end()) {
283 auto path_result = it->second(path, value);
284 if (!path_result.is_valid) {
285 result.is_valid = false;
286 for (const auto& error : path_result.errors) {
287 result.add_error(path + ": " + error);
288 }
289 }
290 for (const auto& warning : path_result.warnings) {
291 result.add_warning(path + ": " + warning);
292 }
293 }
294 }
295
296 return result;
297 }
@ error
Error events that might still allow continuation.

References config_, mutex_, validators_, and kcenon::thread::warning.

◆ value_to_string()

std::string kcenon::thread::configuration_manager::value_to_string ( const config_value & value) const
inlineprivate

Convert value to string.

Definition at line 389 of file configuration_manager.h.

389 {
390 return std::visit([](const auto& v) -> std::string {
391 using T = std::decay_t<decltype(v)>;
392 if constexpr (std::is_same_v<T, bool>) {
393 return v ? "true" : "false";
394 } else if constexpr (std::is_same_v<T, std::string>) {
395 return v;
396 } else if constexpr (std::is_arithmetic_v<T>) {
397 return std::to_string(v);
398 } else {
399 return ""; // Complex types need custom serialization
400 }
401 }, value);
402 }

Referenced by save_to_file().

Here is the caller graph for this function:

◆ values_equal()

bool kcenon::thread::configuration_manager::values_equal ( const config_value & a,
const config_value & b ) const
inlineprivate

Check if two values are equal.

Uses std::visit to compare per-type because std::unordered_map<std::string, std::any> does not support operator==.

Definition at line 410 of file configuration_manager.h.

410 {
411 if (a.index() != b.index()) {
412 return false;
413 }
414 return std::visit([&b](const auto& val_a) -> bool {
415 using T = std::decay_t<decltype(val_a)>;
416 const auto& val_b = std::get<T>(b);
417 if constexpr (std::is_same_v<T, std::unordered_map<std::string, std::any>>) {
418 return false; // std::any has no operator==
419 } else {
420 return val_a == val_b;
421 }
422 }, a);
423 }

Referenced by set().

Here is the caller graph for this function:

Member Data Documentation

◆ callbacks_

std::unordered_map<std::string, std::unordered_map<std::size_t, change_callback> > kcenon::thread::configuration_manager::callbacks_
private

Definition at line 450 of file configuration_manager.h.

Referenced by notify_change(), on_change(), and remove_callback().

◆ callbacks_mutex_

std::mutex kcenon::thread::configuration_manager::callbacks_mutex_
mutableprivate

Definition at line 447 of file configuration_manager.h.

Referenced by notify_change(), on_change(), and remove_callback().

◆ config_

std::unordered_map<std::string, config_value> kcenon::thread::configuration_manager::config_
private

◆ event_bus_

std::shared_ptr<event_bus> kcenon::thread::configuration_manager::event_bus_
private

Definition at line 451 of file configuration_manager.h.

Referenced by configuration_manager(), and set().

◆ mutex_

std::mutex kcenon::thread::configuration_manager::mutex_
mutableprivate

◆ next_callback_id_

std::size_t kcenon::thread::configuration_manager::next_callback_id_ {1}
private

Definition at line 452 of file configuration_manager.h.

452{1};

Referenced by on_change().

◆ validators_

std::unordered_map<std::string, validator_func> kcenon::thread::configuration_manager::validators_
private

Definition at line 449 of file configuration_manager.h.

Referenced by add_validator(), set(), and validate_all().


The documentation for this class was generated from the following file: