Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::config_parser Class Reference

Unified configuration parsing utility. More...

#include <config_parser.h>

Collaboration diagram for kcenon::monitoring::config_parser:
Collaboration graph

Static Public Member Functions

template<typename T >
static T get (const config_map &config, const std::string &key, const T &default_value)
 Get a configuration value with type conversion.
 
template<typename T >
static std::optional< T > get_optional (const config_map &config, const std::string &key)
 Get a configuration value as optional.
 
static bool has_key (const config_map &config, const std::string &key)
 Check if a configuration key exists.
 
template<typename T >
static T get_clamped (const config_map &config, const std::string &key, const T &default_value, const T &min_value, const T &max_value)
 Get a configuration value with validation.
 
template<typename T >
static T get_enum (const config_map &config, const std::string &key, const T &default_value, const std::unordered_set< T > &allowed_values)
 Get a configuration value from a set of allowed values.
 
static std::string get_matching (const config_map &config, const std::string &key, const std::string &default_value, const std::string &pattern)
 Get a string configuration value matching a regex pattern.
 
template<typename T >
static T get_validated (const config_map &config, const std::string &key, const T &default_value, std::function< bool(const T &)> validator)
 Get a configuration value with custom validation.
 
template<typename Duration >
static Duration get_duration (const config_map &config, const std::string &key, const Duration &default_value)
 Get a duration value from configuration.
 
template<typename T >
static std::vector< T > get_list (const config_map &config, const std::string &key, const std::vector< T > &default_values)
 Get a list of values from a comma-separated string.
 

Static Private Member Functions

template<typename T >
static T parse_value (const std::string &str, const T &default_value)
 Parse a string value to target type with default fallback.
 
template<typename T >
static std::optional< T > parse_value_optional (const std::string &str)
 Parse a string value to target type as optional.
 
static bool parse_bool (const std::string &str)
 Parse boolean value from string.
 
template<typename T >
static T parse_integral (const std::string &str)
 Parse integral value from string.
 
template<typename T >
static T parse_floating (const std::string &str)
 Parse floating-point value from string.
 
template<typename Duration >
static Duration parse_duration (const std::string &str, const Duration &default_value)
 Parse duration string with optional suffix Supported suffixes: ms (milliseconds), s (seconds), m (minutes), h (hours)
 
template<typename T >
static std::vector< T > parse_list (const std::string &str, const std::vector< T > &default_values)
 Parse comma-separated list of values.
 

Detailed Description

Unified configuration parsing utility.

Provides type-safe parsing of configuration values with default value support. Handles boolean, integer, floating-point, and string types consistently.

Definition at line 52 of file config_parser.h.

Member Function Documentation

◆ get()

template<typename T >
static T kcenon::monitoring::config_parser::get ( const config_map & config,
const std::string & key,
const T & default_value )
inlinestatic

Get a configuration value with type conversion.

Template Parameters
TThe target type (bool, int, size_t, double, std::string, etc.)
Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default value if key is not found or parsing fails
Returns
The parsed value or default

Definition at line 63 of file config_parser.h.

63 {
64 auto it = config.find(key);
65 if (it == config.end()) {
66 return default_value;
67 }
68 return parse_value<T>(it->second, default_value);
69 }
static T parse_value(const std::string &str, const T &default_value)
Parse a string value to target type with default fallback.

References parse_value().

Referenced by get_clamped(), get_enum(), and get_validated().

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

◆ get_clamped()

template<typename T >
static T kcenon::monitoring::config_parser::get_clamped ( const config_map & config,
const std::string & key,
const T & default_value,
const T & min_value,
const T & max_value )
inlinestatic

Get a configuration value with validation.

Template Parameters
TThe target type
Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default value if key is not found
min_valueMinimum allowed value
max_valueMaximum allowed value
Returns
The parsed value clamped to [min_value, max_value] or default

Definition at line 108 of file config_parser.h.

109 {
110 static_assert(std::is_arithmetic_v<T>, "get_clamped requires arithmetic type");
111 T value = get<T>(config, key, default_value);
112 if (value < min_value) {
113 return min_value;
114 }
115 if (value > max_value) {
116 return max_value;
117 }
118 return value;
119 }
static T get(const config_map &config, const std::string &key, const T &default_value)
Get a configuration value with type conversion.
constexpr T min_value()
Get minimum (lowest) value for a type.
Definition statistics.h:87
constexpr T max_value()
Get maximum value for a type.
Definition statistics.h:75

References get().

Here is the call graph for this function:

◆ get_duration()

template<typename Duration >
static Duration kcenon::monitoring::config_parser::get_duration ( const config_map & config,
const std::string & key,
const Duration & default_value )
inlinestatic

Get a duration value from configuration.

Template Parameters
DurationThe chrono duration type (e.g., std::chrono::milliseconds)
Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default duration if key is not found
Returns
The parsed duration value

Supported formats:

  • Plain number: interpreted as the Duration's unit (e.g., 1000 = 1000ms for milliseconds)
  • With suffix: 100ms, 5s, 2m, 1h (milliseconds, seconds, minutes, hours)

Definition at line 197 of file config_parser.h.

198 {
199 auto it = config.find(key);
200 if (it == config.end()) {
201 return default_value;
202 }
203 return parse_duration<Duration>(it->second, default_value);
204 }
static Duration parse_duration(const std::string &str, const Duration &default_value)
Parse duration string with optional suffix Supported suffixes: ms (milliseconds), s (seconds),...

References parse_duration().

Here is the call graph for this function:

◆ get_enum()

template<typename T >
static T kcenon::monitoring::config_parser::get_enum ( const config_map & config,
const std::string & key,
const T & default_value,
const std::unordered_set< T > & allowed_values )
inlinestatic

Get a configuration value from a set of allowed values.

Template Parameters
TThe target type
Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default value if key is not found or invalid
allowed_valuesSet of valid values
Returns
The parsed value if valid, otherwise default

Definition at line 131 of file config_parser.h.

132 {
133 T value = get<T>(config, key, default_value);
134 if (allowed_values.find(value) != allowed_values.end()) {
135 return value;
136 }
137 return default_value;
138 }

References get().

Here is the call graph for this function:

◆ get_list()

template<typename T >
static std::vector< T > kcenon::monitoring::config_parser::get_list ( const config_map & config,
const std::string & key,
const std::vector< T > & default_values )
inlinestatic

Get a list of values from a comma-separated string.

Template Parameters
TThe element type
Parameters
configThe configuration map
keyThe configuration key to look up
default_valuesThe default list if key is not found
Returns
Vector of parsed values

Definition at line 215 of file config_parser.h.

216 {
217 auto it = config.find(key);
218 if (it == config.end()) {
219 return default_values;
220 }
221 return parse_list<T>(it->second, default_values);
222 }
static std::vector< T > parse_list(const std::string &str, const std::vector< T > &default_values)
Parse comma-separated list of values.

References parse_list().

Here is the call graph for this function:

◆ get_matching()

static std::string kcenon::monitoring::config_parser::get_matching ( const config_map & config,
const std::string & key,
const std::string & default_value,
const std::string & pattern )
inlinestatic

Get a string configuration value matching a regex pattern.

Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default value if key is not found or invalid
patternThe regex pattern to match against
Returns
The value if it matches the pattern, otherwise default

Definition at line 148 of file config_parser.h.

149 {
150 auto it = config.find(key);
151 if (it == config.end()) {
152 return default_value;
153 }
154 try {
155 std::regex regex(pattern);
156 if (std::regex_match(it->second, regex)) {
157 return it->second;
158 }
159 } catch (...) {
160 // Invalid regex or no match
161 }
162 return default_value;
163 }

◆ get_optional()

template<typename T >
static std::optional< T > kcenon::monitoring::config_parser::get_optional ( const config_map & config,
const std::string & key )
inlinestatic

Get a configuration value as optional.

Template Parameters
TThe target type
Parameters
configThe configuration map
keyThe configuration key to look up
Returns
Optional containing the value if found and parseable, empty otherwise

Definition at line 79 of file config_parser.h.

79 {
80 auto it = config.find(key);
81 if (it == config.end()) {
82 return std::nullopt;
83 }
84 return parse_value_optional<T>(it->second);
85 }
static std::optional< T > parse_value_optional(const std::string &str)
Parse a string value to target type as optional.

References parse_value_optional().

Here is the call graph for this function:

◆ get_validated()

template<typename T >
static T kcenon::monitoring::config_parser::get_validated ( const config_map & config,
const std::string & key,
const T & default_value,
std::function< bool(const T &)> validator )
inlinestatic

Get a configuration value with custom validation.

Template Parameters
TThe target type
Parameters
configThe configuration map
keyThe configuration key to look up
default_valueThe default value if key is not found or validation fails
validatorA function that returns true if the value is valid
Returns
The parsed value if valid, otherwise default

Definition at line 175 of file config_parser.h.

176 {
177 T value = get<T>(config, key, default_value);
178 if (validator(value)) {
179 return value;
180 }
181 return default_value;
182 }

References get().

Here is the call graph for this function:

◆ has_key()

static bool kcenon::monitoring::config_parser::has_key ( const config_map & config,
const std::string & key )
inlinestatic

Check if a configuration key exists.

Parameters
configThe configuration map
keyThe configuration key to check
Returns
true if the key exists

Definition at line 93 of file config_parser.h.

93 {
94 return config.find(key) != config.end();
95 }

◆ parse_bool()

static bool kcenon::monitoring::config_parser::parse_bool ( const std::string & str)
inlinestaticprivate

Parse boolean value from string.

Parameters
strString to parse ("true", "1", "yes", "on" -> true)
Returns
Parsed boolean value

Definition at line 274 of file config_parser.h.

274 {
275 if (str.empty()) {
276 return false;
277 }
278 // Case-insensitive comparison
279 std::string lower = str;
280 for (auto& c : lower) {
281 c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
282 }
283 return lower == "true" || lower == "1" || lower == "yes" || lower == "on";
284 }

Referenced by parse_value(), and parse_value_optional().

Here is the caller graph for this function:

◆ parse_duration()

template<typename Duration >
static Duration kcenon::monitoring::config_parser::parse_duration ( const std::string & str,
const Duration & default_value )
inlinestaticprivate

Parse duration string with optional suffix Supported suffixes: ms (milliseconds), s (seconds), m (minutes), h (hours)

Definition at line 321 of file config_parser.h.

321 {
322 try {
323 if (str.empty()) {
324 return default_value;
325 }
326
327 // Find where the number ends and suffix begins
328 size_t suffix_start = str.find_first_not_of("0123456789.-");
329
330 if (suffix_start == std::string::npos) {
331 // Plain number, interpret as Duration's unit
332 long long value = std::stoll(str);
333 return Duration(value);
334 }
335
336 // Parse the numeric part
337 long long value = std::stoll(str.substr(0, suffix_start));
338 std::string suffix = str.substr(suffix_start);
339
340 // Trim whitespace from suffix
341 while (!suffix.empty() && std::isspace(static_cast<unsigned char>(suffix.front()))) {
342 suffix.erase(0, 1);
343 }
344
345 // Convert to lowercase
346 for (auto& c : suffix) {
347 c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
348 }
349
350 // Parse suffix and convert to Duration
351 if (suffix == "ms" || suffix == "millisecond" || suffix == "milliseconds") {
352 return std::chrono::duration_cast<Duration>(std::chrono::milliseconds(value));
353 } else if (suffix == "s" || suffix == "sec" || suffix == "second" || suffix == "seconds") {
354 return std::chrono::duration_cast<Duration>(std::chrono::seconds(value));
355 } else if (suffix == "m" || suffix == "min" || suffix == "minute" || suffix == "minutes") {
356 return std::chrono::duration_cast<Duration>(std::chrono::minutes(value));
357 } else if (suffix == "h" || suffix == "hr" || suffix == "hour" || suffix == "hours") {
358 return std::chrono::duration_cast<Duration>(std::chrono::hours(value));
359 } else {
360 // Unknown suffix, treat as plain number
361 return Duration(value);
362 }
363 } catch (...) {
364 return default_value;
365 }
366 }

Referenced by get_duration().

Here is the caller graph for this function:

◆ parse_floating()

template<typename T >
static T kcenon::monitoring::config_parser::parse_floating ( const std::string & str)
inlinestaticprivate

Parse floating-point value from string.

Definition at line 305 of file config_parser.h.

305 {
306 static_assert(std::is_floating_point_v<T>, "parse_floating requires floating point type");
307 if constexpr (std::is_same_v<T, float>) {
308 return std::stof(str);
309 } else if constexpr (std::is_same_v<T, double>) {
310 return std::stod(str);
311 } else {
312 return static_cast<T>(std::stold(str));
313 }
314 }

Referenced by parse_value(), and parse_value_optional().

Here is the caller graph for this function:

◆ parse_integral()

template<typename T >
static T kcenon::monitoring::config_parser::parse_integral ( const std::string & str)
inlinestaticprivate

Parse integral value from string.

Definition at line 290 of file config_parser.h.

290 {
291 static_assert(std::is_integral_v<T>, "parse_integral requires integral type");
292 if constexpr (std::is_signed_v<T>) {
293 long long value = std::stoll(str);
294 return static_cast<T>(value);
295 } else {
296 unsigned long long value = std::stoull(str);
297 return static_cast<T>(value);
298 }
299 }

Referenced by parse_value(), and parse_value_optional().

Here is the caller graph for this function:

◆ parse_list()

template<typename T >
static std::vector< T > kcenon::monitoring::config_parser::parse_list ( const std::string & str,
const std::vector< T > & default_values )
inlinestaticprivate

Parse comma-separated list of values.

Definition at line 372 of file config_parser.h.

372 {
373 try {
374 if (str.empty()) {
375 return default_values;
376 }
377
378 std::vector<T> result;
379 std::string current;
380
381 for (char c : str) {
382 if (c == ',') {
383 // Trim whitespace
384 size_t start = current.find_first_not_of(" \t");
385 size_t end = current.find_last_not_of(" \t");
386 if (start != std::string::npos) {
387 std::string trimmed = current.substr(start, end - start + 1);
388 auto parsed = parse_value_optional<T>(trimmed);
389 if (parsed) {
390 result.push_back(*parsed);
391 }
392 }
393 current.clear();
394 } else {
395 current += c;
396 }
397 }
398
399 // Handle last element
400 if (!current.empty()) {
401 size_t start = current.find_first_not_of(" \t");
402 size_t end = current.find_last_not_of(" \t");
403 if (start != std::string::npos) {
404 std::string trimmed = current.substr(start, end - start + 1);
405 auto parsed = parse_value_optional<T>(trimmed);
406 if (parsed) {
407 result.push_back(*parsed);
408 }
409 }
410 }
411
412 return result.empty() ? default_values : result;
413 } catch (...) {
414 return default_values;
415 }
416 }

References parse_value_optional().

Referenced by get_list().

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

◆ parse_value()

template<typename T >
static T kcenon::monitoring::config_parser::parse_value ( const std::string & str,
const T & default_value )
inlinestaticprivate

Parse a string value to target type with default fallback.

Definition at line 229 of file config_parser.h.

229 {
230 try {
231 if constexpr (std::is_same_v<T, bool>) {
232 return parse_bool(str);
233 } else if constexpr (std::is_same_v<T, std::string>) {
234 return str;
235 } else if constexpr (std::is_integral_v<T>) {
236 return parse_integral<T>(str);
237 } else if constexpr (std::is_floating_point_v<T>) {
238 return parse_floating<T>(str);
239 } else {
240 return default_value;
241 }
242 } catch (...) {
243 return default_value;
244 }
245 }
static T parse_integral(const std::string &str)
Parse integral value from string.
static bool parse_bool(const std::string &str)
Parse boolean value from string.
static T parse_floating(const std::string &str)
Parse floating-point value from string.

References parse_bool(), parse_floating(), and parse_integral().

Referenced by get().

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

◆ parse_value_optional()

template<typename T >
static std::optional< T > kcenon::monitoring::config_parser::parse_value_optional ( const std::string & str)
inlinestaticprivate

Parse a string value to target type as optional.

Definition at line 251 of file config_parser.h.

251 {
252 try {
253 if constexpr (std::is_same_v<T, bool>) {
254 return parse_bool(str);
255 } else if constexpr (std::is_same_v<T, std::string>) {
256 return str;
257 } else if constexpr (std::is_integral_v<T>) {
258 return parse_integral<T>(str);
259 } else if constexpr (std::is_floating_point_v<T>) {
260 return parse_floating<T>(str);
261 } else {
262 return std::nullopt;
263 }
264 } catch (...) {
265 return std::nullopt;
266 }
267 }

References parse_bool(), parse_floating(), and parse_integral().

Referenced by get_optional(), and parse_list().

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

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