YOrch 1.0.0
Loading...
Searching...
No Matches
catch_as_failure.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "concepts.hpp" // IWYU pragma: keep
4#include "../detail/task_adapters/result_helpers.hpp"
5
6namespace yorch {
7
18template <typename Task>
23 Task task;
24
25 constexpr explicit catch_failure_task(Task&& stored_task)
26 noexcept(std::is_nothrow_move_constructible_v<Task>)
27 : task(std::move(stored_task)) {}
28
29 constexpr explicit catch_failure_task(const Task& stored_task)
30 noexcept(std::is_nothrow_copy_constructible_v<Task>)
31 : task(stored_task) {}
32
42 template <typename Ctx, typename Prev>
44 constexpr auto invoke_raw(exec_context<Ctx, Prev>& ec) noexcept {
46
48 "Wrapped task raw_result_type must match invoke_raw(exec_context<...>&) return type");
50 "Default catch_as_failure only supports tasks returning void or step_result");
51
52 try {
53 if constexpr (std::is_void_v<raw_result_t>) {
54 task.invoke_raw(ec);
55 return step_result::success();
56 } else {
57 return task.invoke_raw(ec);
58 }
59 } catch (...) {
61 }
62 }
63
64 template <typename Ctx, typename Prev, typename U = Task>
69 try {
70 return task.invoke_into(ec, out);
71 } catch (...) {
72 return step_result::failure();
73 }
74 }
75};
76
90template <typename Task, typename Policy>
95 Task task;
96 Policy policy;
97
98 constexpr catch_failure_with_policy_task(Task&& stored_task, Policy&& stored_policy)
99 noexcept(std::is_nothrow_move_constructible_v<Task> &&
100 std::is_nothrow_move_constructible_v<Policy>)
101 : task(std::move(stored_task)),
102 policy(std::move(stored_policy)) {}
103
104 constexpr catch_failure_with_policy_task(const Task& stored_task, const Policy& stored_policy)
105 noexcept(std::is_nothrow_copy_constructible_v<Task> &&
106 std::is_nothrow_copy_constructible_v<Policy>)
107 : task(stored_task),
108 policy(stored_policy) {}
109
120 template <typename Ctx, typename Prev>
122 constexpr auto invoke_raw(exec_context<Ctx, Prev>& ec) noexcept {
124
126 "Wrapped task raw_result_type must match invoke_raw(exec_context<...>&) return type");
128 "catch_as_failure(task, policy) requires a noexcept fallback taking std::exception_ptr and returning a compatible result");
129
130 try {
131 if constexpr (std::is_void_v<raw_result_t>) {
132 task.invoke_raw(ec);
134 } else {
135 return task.invoke_raw(ec);
136 }
137 } catch (...) {
138 return detail::policy_catch_failure_result<raw_result_t>(policy, std::current_exception());
139 }
140 }
141
142 template <typename Ctx, typename Prev, typename U = Task>
147 try {
148 return task.invoke_into(ec, out);
149 } catch (...) {
150 return std::invoke(policy, std::current_exception());
151 }
152 }
153};
154
174template <typename Task>
175constexpr auto catch_as_failure(Task&& task) {
177 std::forward<Task>(task)
178 };
179}
180
202template <typename Task, typename Policy>
203constexpr auto catch_as_failure(Task&& task, Policy&& policy) {
205 std::decay_t<Task>,
206 std::decay_t<Policy>
207 >{
208 std::forward<Task>(task),
209 std::forward<Policy>(policy)
210 };
211}
212
213} // namespace yorch
Reports whether catch_as_failure(task, policy) can wrap Task using the provided fallback Policy.
Definition concepts.hpp:62
Reports whether catch_as_failure(task) can wrap Task for a concrete execution context without a custo...
Definition concepts.hpp:48
typename declared_task_output< Task >::type declared_task_output_t
Definition traits.hpp:41
decltype(std::declval< Task >().invoke_raw(std::declval< exec_context< Ctx, Prev > & >())) raw_task_result_t
Definition traits.hpp:16
constexpr bool is_adapter_descriptor_v
Definition adapters.hpp:63
constexpr auto catch_as_failure(Task &&task)
Wraps a task so thrown exceptions become default failure results.
constexpr auto task(F &&f)
Definition core.hpp:207
Exception-catching adapter for tasks that rely on the default failure mapping.
constexpr step_result invoke_into(exec_context< Ctx, Prev > &ec, direct_out< detail::declared_task_output_t< U > > out) noexcept
constexpr catch_failure_task(Task &&stored_task) noexcept(std::is_nothrow_move_constructible_v< Task >)
constexpr auto invoke_raw(exec_context< Ctx, Prev > &ec) noexcept
Executes the wrapped task and maps thrown exceptions to failure.
constexpr catch_failure_task(const Task &stored_task) noexcept(std::is_nothrow_copy_constructible_v< Task >)
Exception-catching adapter for tasks with a user-supplied fallback policy.
constexpr catch_failure_with_policy_task(const Task &stored_task, const Policy &stored_policy) noexcept(std::is_nothrow_copy_constructible_v< Task > &&std::is_nothrow_copy_constructible_v< Policy >)
constexpr catch_failure_with_policy_task(Task &&stored_task, Policy &&stored_policy) noexcept(std::is_nothrow_move_constructible_v< Task > &&std::is_nothrow_move_constructible_v< Policy >)
constexpr auto invoke_raw(exec_context< Ctx, Prev > &ec) noexcept
Executes the wrapped task and delegates exception handling to the stored fallback policy.
constexpr step_result invoke_into(exec_context< Ctx, Prev > &ec, direct_out< detail::declared_task_output_t< U > > out) noexcept
Output sink passed to direct-output tasks.
Lightweight borrowed view used during execution.
Definition context.hpp:187
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
static constexpr step_result success() noexcept
Creates a successful result.
Definition result.hpp:46