Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
25#pragma once
26
27#include <concepts>
28#include <type_traits>
29#include <functional>
30
31namespace kcenon::common {
32namespace concepts {
33
53template<typename T>
54concept Resultable = requires(const T t) {
55 { t.is_ok() } -> std::convertible_to<bool>;
56 { t.is_err() } -> std::convertible_to<bool>;
57};
58
74template<typename T>
75concept Unwrappable = requires(T t) {
76 typename T::value_type;
77 { t.unwrap() } -> std::same_as<typename std::add_lvalue_reference<
78 typename std::add_const<typename T::value_type>::type>::type>;
79 { t.unwrap_or(std::declval<typename T::value_type>()) }
80 -> std::convertible_to<typename T::value_type>;
81};
82
98template<typename T>
99concept Mappable = requires(const T t) {
100 { t.map(std::declval<std::function<int(typename T::value_type)>>()) };
101};
102
120template<typename T>
121concept Chainable = requires(const T t) {
122 { t.and_then(std::declval<std::function<T(typename T::value_type)>>()) };
123};
124
142template<typename T>
144
162template<typename T>
163concept OptionalLike = requires(const T t) {
164 { t.has_value() } -> std::convertible_to<bool>;
165 { t.is_some() } -> std::convertible_to<bool>;
166 { t.is_none() } -> std::convertible_to<bool>;
167};
168
185template<typename T>
186concept ErrorInfo = requires(const T t) {
187 { t.code } -> std::convertible_to<int>;
188 { t.message } -> std::convertible_to<std::string>;
189 { t.module } -> std::convertible_to<std::string>;
190};
191
213template<typename T>
214concept ValueOrError = Resultable<T> && requires(const T t) {
215 { t.value() };
216 { t.error() };
217};
218
219} // namespace concepts
220} // namespace kcenon::common
A type that supports monadic chaining (flatMap/and_then).
Definition core.h:121
A type that contains error information.
Definition core.h:186
A type that supports monadic map operations.
Definition core.h:99
A complete Result-like type with all monadic operations.
Definition core.h:143
A type that represents an optional value (present or absent).
Definition core.h:163
A type that can contain either a value or an error.
Definition core.h:54
A type that supports value extraction (unwrapping).
Definition core.h:75
A type that holds either a value or error information.
Definition core.h:214
Core interfaces.
Definition adapter.h:21