Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
concepts_showcase_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13
16
17#include <iostream>
18#include <string>
19#include <type_traits>
20
21using namespace kcenon::common;
22
23// Constrained function: only accepts Resultable types
24template <concepts::Resultable R>
25void inspect_result(const R& result, const std::string& label)
26{
27 std::cout << " " << label << ": "
28 << (result.is_ok() ? "OK" : "ERROR") << "\n";
29}
30
31// Unwrap with fallback using Result API directly
32template <typename T>
33T safe_unwrap(const Result<T>& val, const T& fallback)
34{
35 return val.unwrap_or(fallback);
36}
37
38// Constrained function: full monadic chain
39template <concepts::MonadicResult R>
40auto transform_chain(R result)
41{
42 return result
43 .map([](const auto& v) { return v * 2; })
44 .map([](const auto& v) { return v + 10; });
45}
46
47// Concept-based overload: demonstrate SFINAE replacement
48template <typename T>
49void process(const T& value)
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}
61
62int main()
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}
Result type for error handling with member function support.
Definition core.cppm:165
T unwrap_or(T default_value) const
Get value or return default.
Definition core.h:372
Core C++20 concepts for Result/Optional types.
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
Core interfaces.
Definition adapter.h:21
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
Umbrella header for Result<T> type and related utilities.