YOrch 1.0.0
Loading...
Searching...
No Matches
result.hpp
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3#include <utility>
4
5#include "assert.hpp"
7
8namespace yorch {
9
27enum class step_status : unsigned char {
28 success,
29 failure,
30 retry,
33};
34
44
46 [[nodiscard]] static constexpr step_result success() noexcept {
47 return {step_status::success};
48 }
49
51 [[nodiscard]] static constexpr step_result failure() noexcept {
52 return {step_status::failure};
53 }
54
56 [[nodiscard]] static constexpr step_result retry() noexcept {
57 return {step_status::retry};
58 }
59
61 [[nodiscard]] static constexpr step_result abort_branch() noexcept {
63 }
64
66 [[nodiscard]] static constexpr step_result abort_execution() noexcept {
68 }
69
71 [[nodiscard]] constexpr bool ok() const noexcept {
73 }
74
76 [[nodiscard]] constexpr explicit operator bool() const noexcept {
77 return ok();
78 }
79};
80
95template <typename T>
97 static_assert(!std::is_reference_v<T>,
98 "yorch::task_result<T> does not support reference types");
99 static_assert(!std::is_void_v<T>,
100 "yorch::task_result<void> is not supported; use `void` for success-only tasks or `yorch::step_result` for status-only tasks");
101
102public:
104 using value_type = T;
105
106 task_result() = delete;
107 task_result(const task_result&) = default;
111 ~task_result() = default;
112
114 step_result step {}; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
115
116 template <typename U>
117 [[nodiscard]] static constexpr task_result success(U&& value)
118 noexcept(std::is_nothrow_constructible_v<T, U&&>) {
119 return task_result(step_result::success(), std::forward<U>(value));
120 }
121
122 [[nodiscard]] static constexpr task_result failure() noexcept {
124 }
125
126 [[nodiscard]] static constexpr task_result retry() noexcept {
128 }
129
130 [[nodiscard]] static constexpr task_result abort_branch() noexcept {
132 }
133
134 [[nodiscard]] static constexpr task_result abort_execution() noexcept {
136 }
137
138 [[nodiscard]] static constexpr task_result from_step(step_result s) noexcept {
139 YORCH_ASSERT(!s.ok() &&
140 "yorch::task_result<T>::from_step(...) requires a non-success status");
141 return task_result(s);
142 }
143
144 [[nodiscard]] constexpr bool has_value() const noexcept {
145 return value_.has_value();
146 }
147
148 [[nodiscard]] constexpr T& value() & noexcept {
149 return value_.get();
150 }
151
152 [[nodiscard]] constexpr const T& value() const& noexcept {
153 return value_.get();
154 }
155
156 [[nodiscard]] constexpr T&& value() && noexcept {
157 return std::move(value_).get();
158 }
159
160private:
161 constexpr explicit task_result(step_result s) noexcept
162 : step(s) {}
163
164 template <typename U>
165 constexpr task_result(step_result s, U&& value)
166 noexcept(std::is_nothrow_constructible_v<T, U&&>)
167 : step(s) {
168 value_.emplace(std::forward<U>(value));
169 }
170
171 detail::maybe_storage<T> value_ {};
172};
173
183template <typename T>
184struct is_task_result : std::false_type {};
185
186template <typename T>
187struct is_task_result<task_result<T>> : std::true_type {};
188
189template <typename T>
190inline constexpr bool is_task_result_v =
192
198template <typename T>
200
201template <typename T>
203 using type = T;
204};
205
206template <typename T>
209
210} // namespace yorch
#define YORCH_ASSERT(condition)
Definition assert.hpp:10
constexpr bool is_adapter_descriptor_v
Definition adapters.hpp:63
constexpr bool is_task_result_v
Definition result.hpp:190
constexpr auto value(T &&v) -> value_t< std::remove_cvref_t< T > >
Wraps a value as an owning spec.
Definition specs.hpp:179
typename task_result_value< std::remove_cvref_t< T > >::type task_result_value_t
Definition result.hpp:208
step_status
Describes the execution status of a single step.
Definition result.hpp:27
Type trait that detects task_result<T>.
Definition result.hpp:184
Represents the basic outcome of a task step.
Definition result.hpp:42
static constexpr step_result failure() noexcept
Creates a failed result.
Definition result.hpp:51
step_status status
Definition result.hpp:43
static constexpr step_result retry() noexcept
Creates a retry result.
Definition result.hpp:56
constexpr bool ok() const noexcept
Returns whether the status is success.
Definition result.hpp:71
static constexpr step_result abort_execution() noexcept
Creates a result that aborts the whole execution.
Definition result.hpp:66
static constexpr step_result abort_branch() noexcept
Creates a result that aborts the current branch.
Definition result.hpp:61
static constexpr step_result success() noexcept
Creates a successful result.
Definition result.hpp:46
Extracts the payload type from a supported task_result<T>.
Definition result.hpp:199
Represents a step result that may carry a produced value.
Definition result.hpp:96
static constexpr task_result failure() noexcept
Definition result.hpp:122
step_result step
Execution status of the current step.
Definition result.hpp:114
constexpr const T & value() const &noexcept
Definition result.hpp:152
constexpr T && value() &&noexcept
Definition result.hpp:156
constexpr T & value() &noexcept
Definition result.hpp:148
T value_type
Value type carried by this result wrapper.
Definition result.hpp:104
static constexpr task_result success(U &&value) noexcept(std::is_nothrow_constructible_v< T, U && >)
Definition result.hpp:117
task_result & operator=(const task_result &)=default
static constexpr task_result retry() noexcept
Definition result.hpp:126
static constexpr task_result abort_branch() noexcept
Definition result.hpp:130
constexpr bool has_value() const noexcept
Definition result.hpp:144
task_result(task_result &&)=default
task_result & operator=(task_result &&)=default
~task_result()=default
task_result(const task_result &)=default
static constexpr task_result from_step(step_result s) noexcept
Definition result.hpp:138
static constexpr task_result abort_execution() noexcept
Definition result.hpp:134