Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
result_example.cpp File Reference
#include <kcenon/common/patterns/result.h>
#include <iostream>
#include <fstream>
#include <string>
Include dependency graph for result_example.cpp:

Go to the source code of this file.

Functions

Result< int > divide (int a, int b)
 
Result< std::string > read_file (const std::string &path)
 
Result< int > parse_and_compute (const std::string &expr)
 
int main ()
 

Function Documentation

◆ divide()

Result< int > divide ( int a,
int b )
Examples
result_example.cpp, and unwrap_demo.cpp.

Definition at line 26 of file result_example.cpp.

26 {
27 if (b == 0) {
28 return make_error<int>(
30 "Division by zero",
31 "math_module"
32 );
33 }
34 return ok(a / b);
35}
constexpr int INVALID_ARGUMENT
Definition compat.h:31
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
Definition utilities.h:91
VoidResult ok()
Create a successful void result.
Definition utilities.h:71

References kcenon::common::error_codes::INVALID_ARGUMENT, kcenon::common::make_error(), and kcenon::common::ok().

Referenced by main(), and parse_and_compute().

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

◆ main()

int main ( )

Definition at line 86 of file result_example.cpp.

86 {
87 std::cout << "=== Result Pattern Examples ===\n\n";
88
89 // Example 1: Basic usage with member methods
90 std::cout << "1. Basic division:\n";
91 auto result1 = divide(10, 2);
92 if (result1.is_ok()) {
93 std::cout << " 10 / 2 = " << result1.value() << "\n";
94 }
95
96 auto result2 = divide(10, 0);
97 if (result2.is_err()) {
98 const auto& err = result2.error();
99 std::cout << " Error: " << err.message
100 << " (code: " << err.code << ")\n";
101 }
102
103 // Example 2: Using value_or / unwrap_or
104 std::cout << "\n2. Using value_or:\n";
105 auto value = divide(10, 0).unwrap_or(-1);
106 std::cout << " 10 / 0 with default -1 = " << value << "\n";
107
108 // Example 3: Pattern matching with if-else
109 std::cout << "\n3. Pattern matching:\n";
110 auto result3 = parse_and_compute("20/4");
111 if (result3.is_ok()) {
112 std::cout << " Success: " << result3.unwrap() << "\n";
113 } else {
114 std::cout << " Failed: " << result3.error().message << "\n";
115 }
116
117 // Example 4: Monadic operations using member methods
118 std::cout << "\n4. Monadic operations:\n";
119 auto result4 = divide(100, 5);
120 auto doubled = result4.map([](int x) { return x * 2; });
121 if (doubled.is_ok()) {
122 std::cout << " (100 / 5) * 2 = " << doubled.value() << "\n";
123 }
124
125 // Example 5: Chaining with and_then using member methods
126 std::cout << "\n5. Chaining operations:\n";
127 auto chain_result = divide(50, 5)
128 .and_then([](int x) { return divide(x, 2); });
129 if (chain_result.is_ok()) {
130 std::cout << " (50 / 5) / 2 = " << chain_result.value() << "\n";
131 }
132
133 // Example 6: Error recovery with or_else using member methods
134 std::cout << "\n6. Error recovery:\n";
135 auto recovered = divide(10, 0)
136 .or_else([](const error_info&) { return ok(0); });
137 std::cout << " 10 / 0 with recovery = " << recovered.value() << "\n";
138
139 // Example 7: try_catch wrapper
140 std::cout << "\n7. Exception wrapping:\n";
141 auto wrapped = try_catch<int>([]{
142 throw std::runtime_error("Something went wrong");
143 return 42;
144 }, "example_module");
145
146 if (wrapped.is_err()) {
147 const auto& err = wrapped.error();
148 std::cout << " Caught exception: " << err.message << "\n";
149 }
150
151 std::cout << "\n=== Examples completed ===\n";
152 return 0;
153}
const error_info & error() const
Get error reference.
Definition core.h:405
Result< T > try_catch(F &&func, const std::string &module="")
Convert exception to Result with automatic error code mapping.
Definition utilities.h:173
VoidResult err(const error_info &error)
Factory function to create error VoidResult.
Definition core.cppm:432
Result< int > parse_and_compute(const std::string &expr)
Result< int > divide(int a, int b)
Standard error information used by Result<T>.
Definition core.cppm:106

References divide(), kcenon::common::err(), kcenon::common::Result< T >::error(), kcenon::common::ok(), parse_and_compute(), and kcenon::common::try_catch().

Here is the call graph for this function:

◆ parse_and_compute()

Result< int > parse_and_compute ( const std::string & expr)
Examples
result_example.cpp.

Definition at line 62 of file result_example.cpp.

62 {
63 // Simple parser for "a/b" format
64 auto pos = expr.find('/');
65 if (pos == std::string::npos) {
66 return make_error<int>(
68 "Invalid expression format",
69 "parser"
70 );
71 }
72
73 try {
74 int a = std::stoi(expr.substr(0, pos));
75 int b = std::stoi(expr.substr(pos + 1));
76 return divide(a, b);
77 } catch (const std::exception& e) {
78 return make_error<int>(
80 std::string("Parse error: ") + e.what(),
81 "parser"
82 );
83 }
84}

References divide(), kcenon::common::error_codes::INVALID_ARGUMENT, and kcenon::common::make_error().

Referenced by main().

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

◆ read_file()

Result< std::string > read_file ( const std::string & path)
Examples
result_example.cpp.

Definition at line 38 of file result_example.cpp.

38 {
39 if (path.empty()) {
42 "Path cannot be empty",
43 "file_module"
44 );
45 }
46
47 std::ifstream file(path);
48 if (!file.is_open()) {
51 "File not found: " + path,
52 "file_module"
53 );
54 }
55
56 std::string content((std::istreambuf_iterator<char>(file)),
57 std::istreambuf_iterator<char>());
58 return ok(content);
59}
constexpr int NOT_FOUND
Definition compat.h:32

References kcenon::common::error_codes::INVALID_ARGUMENT, kcenon::common::make_error(), kcenon::common::error_codes::NOT_FOUND, and kcenon::common::ok().

Here is the call graph for this function: