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

Go to the source code of this file.

Functions

template<concepts::Resultable R>
void inspect_result (const R &result, const std::string &label)
 
template<typename T >
safe_unwrap (const Result< T > &val, const T &fallback)
 
template<concepts::MonadicResult R>
auto transform_chain (R result)
 
template<typename T >
void process (const T &value)
 
int main ()
 

Function Documentation

◆ inspect_result()

template<concepts::Resultable R>
void inspect_result ( const R & result,
const std::string & label )
Examples
concepts_showcase_example.cpp.

Definition at line 25 of file concepts_showcase_example.cpp.

26{
27 std::cout << " " << label << ": "
28 << (result.is_ok() ? "OK" : "ERROR") << "\n";
29}

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 62 of file concepts_showcase_example.cpp.

63{
64 std::cout << "=== C++20 Concepts Showcase ===\n\n";
65
66 // 1. Resultable concept
67 std::cout << "1. Resultable concept:\n";
68 auto success = ok(42);
69 auto failure = make_error<int>(error_codes::INVALID_ARGUMENT, "bad input", "demo");
70
71 inspect_result(success, "ok(42)");
72 inspect_result(failure, "error");
73
74 // Compile-time check
75 static_assert(concepts::Resultable<Result<int>>,
76 "Result<int> must satisfy Resultable");
77 std::cout << " static_assert: Result<int> is Resultable\n";
78
79 // 2. Unwrappable concept
80 std::cout << "\n2. Unwrappable concept:\n";
81 auto val = safe_unwrap(success, -1);
82 std::cout << " safe_unwrap(ok(42), -1) = " << val << "\n";
83
84 auto val2 = safe_unwrap(failure, -1);
85 std::cout << " safe_unwrap(error, -1) = " << val2 << "\n";
86
87 // 3. MonadicResult concept (map + and_then)
88 std::cout << "\n3. MonadicResult concept:\n";
89 auto chained = transform_chain(ok(5));
90 if (chained.is_ok())
91 {
92 std::cout << " transform_chain(ok(5)): 5 * 2 + 10 = " << chained.value() << "\n";
93 }
94
95 auto chained_err = transform_chain(
97 std::cout << " transform_chain(error): is_ok = "
98 << (chained_err.is_ok() ? "true" : "false") << "\n";
99
100 // 4. Concept-based dispatch
101 std::cout << "\n4. Concept-based dispatch:\n";
102 process(ok(100));
103 process(42);
104 process(std::string("hello"));
105
106 // 5. Compile-time concept checks
107 std::cout << "\n5. Compile-time type traits:\n";
108 std::cout << " Result<int> is Resultable: "
109 << concepts::Resultable<Result<int>> << "\n";
110 std::cout << " Result<int> is Mappable: "
111 << concepts::Mappable<Result<int>> << "\n";
112 std::cout << " int is Resultable: "
113 << concepts::Resultable<int> << "\n";
114
115 std::cout << "\nDone.\n";
116 return 0;
117}
auto transform_chain(R result)
void process(const T &value)
T safe_unwrap(const Result< T > &val, const T &fallback)
void inspect_result(const R &result, const std::string &label)
constexpr int INVALID_ARGUMENT
Definition compat.h:31
constexpr int INTERNAL_ERROR
Definition compat.h:42
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 inspect_result(), kcenon::common::error_codes::INTERNAL_ERROR, kcenon::common::error_codes::INVALID_ARGUMENT, kcenon::common::make_error(), kcenon::common::ok(), process(), safe_unwrap(), and transform_chain().

Here is the call graph for this function:

◆ process()

template<typename T >
void process ( const T & value)
Examples
concepts_showcase_example.cpp.

Definition at line 49 of file concepts_showcase_example.cpp.

50{
51 if constexpr (concepts::Resultable<T>)
52 {
53 std::cout << " Processing Result type: "
54 << (value.is_ok() ? "success" : "failure") << "\n";
55 }
56 else
57 {
58 std::cout << " Processing non-Result type: " << value << "\n";
59 }
60}

Referenced by main().

Here is the caller graph for this function:

◆ safe_unwrap()

template<typename T >
T safe_unwrap ( const Result< T > & val,
const T & fallback )
Examples
concepts_showcase_example.cpp.

Definition at line 33 of file concepts_showcase_example.cpp.

34{
35 return val.unwrap_or(fallback);
36}
T unwrap_or(T default_value) const
Get value or return default.
Definition core.h:372

References kcenon::common::Result< T >::unwrap_or().

Referenced by main().

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

◆ transform_chain()

template<concepts::MonadicResult R>
auto transform_chain ( R result)
Examples
concepts_showcase_example.cpp.

Definition at line 40 of file concepts_showcase_example.cpp.

41{
42 return result
43 .map([](const auto& v) { return v * 2; })
44 .map([](const auto& v) { return v + 10; });
45}

Referenced by main().

Here is the caller graph for this function: