25kcenon::common::Result<double>
divide(
double a,
double b) {
27 return kcenon::common::Result<double>::err(
error_info(monitoring_error_code::invalid_configuration,
"Division by zero").to_common_error());
29 return kcenon::common::ok(a / b);
33kcenon::common::VoidResult
validate_range(
double value,
double min,
double max) {
34 if (value < min || value > max) {
35 error_info err(monitoring_error_code::invalid_configuration,
36 "Value out of range [" + std::to_string(min) +
", " + std::to_string(max) +
"]");
39 return kcenon::common::ok();
45 return divide(100.0, value)
46 .map([](
double x) {
return x * 2; })
47 .and_then([](
double x) {
49 return kcenon::common::ok(
"High value: " + std::to_string(x));
51 return kcenon::common::ok(
"Normal value: " + std::to_string(x));
56 std::cout <<
"=== Result Pattern Example ===" << std::endl << std::endl;
59 std::cout <<
"Example 1: Successful division" << std::endl;
60 auto result1 =
divide(10.0, 2.0);
61 if (result1.is_ok()) {
62 std::cout <<
" Result: " << result1.value() << std::endl;
64 std::cout <<
" Error: " << result1.error().message << std::endl;
66 std::cout << std::endl;
69 std::cout <<
"Example 2: Division by zero" << std::endl;
70 auto result2 =
divide(10.0, 0.0);
71 if (result2.is_ok()) {
72 std::cout <<
" Result: " << result2.value() << std::endl;
74 std::cout <<
" Error: " << result2.error().message << std::endl;
76 std::cout << std::endl;
79 std::cout <<
"Example 3: Using value_or with default" << std::endl;
80 auto result3 =
divide(5.0, 0.0);
81 double value = result3.value_or(-1.0);
82 std::cout <<
" Value (with default): " << value << std::endl;
83 std::cout << std::endl;
86 std::cout <<
"Example 4: Validation with kcenon::common::VoidResult" << std::endl;
88 if (validation1.is_ok()) {
89 std::cout <<
" Validation passed" << std::endl;
91 std::cout <<
" Validation failed: " << validation1.error().message << std::endl;
95 if (validation2.is_ok()) {
96 std::cout <<
" Validation passed" << std::endl;
98 std::cout <<
" Validation failed: " << validation2.error().message << std::endl;
100 std::cout << std::endl;
103 std::cout <<
"Example 5: Chaining operations" << std::endl;
105 if (result4.is_ok()) {
106 std::cout <<
" " << result4.value() << std::endl;
108 std::cout <<
" Error: " << result4.error().message << std::endl;
112 if (result5.is_ok()) {
113 std::cout <<
" " << result5.value() << std::endl;
115 std::cout <<
" Error: " << result5.error().message << std::endl;
117 std::cout << std::endl;
120 std::cout <<
"Example 6: Metrics snapshot" << std::endl;
126 std::cout <<
" Metrics collected: " << snapshot.
metrics.size() << std::endl;
129 std::cout <<
" CPU Usage: " <<
cpu.value() <<
"%" << std::endl;
132 if (
auto mem = snapshot.
get_metric(
"memory_usage")) {
133 std::cout <<
" Memory Usage: " << mem.value() <<
" MB" << std::endl;
135 std::cout << std::endl;
138 std::cout <<
"Example 7: Configuration validation" << std::endl;
144 auto config_result = config.
validate();
145 if (config_result.is_ok()) {
146 std::cout <<
" Configuration is valid" << std::endl;
147 std::cout <<
" - History size: " << config.
history_size << std::endl;
148 std::cout <<
" - Collection interval: " << config.
collection_interval.count() <<
"ms" << std::endl;
149 std::cout <<
" - Buffer size: " << config.
buffer_size << std::endl;
151 std::cout <<
" Configuration error: " << config_result.error().message << std::endl;
Monitoring system specific error codes.
Core monitoring system interface definitions.
@ cpu
CPU power domain (RAPL)
kcenon::common::Result< double > divide(double a, double b)
kcenon::common::VoidResult validate_range(double value, double min, double max)
kcenon::common::Result< std::string > process_metric(double value)
Result pattern type definitions for monitoring system.
Extended error information with context.
common::error_info to_common_error() const
Convert to common_system error_info.
Complete snapshot of metrics at a point in time.
std::optional< double > get_metric(const std::string &name) const
Get a specific metric value.
std::vector< metric_value > metrics
void add_metric(const std::string &name, double value)
Add a metric to the snapshot.
Configuration for the monitoring system.
std::chrono::milliseconds collection_interval
common::VoidResult validate() const
Validate configuration parameters.