YOrch 1.0.0
Loading...
Searching...
No Matches
result_helpers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts> // IWYU pragma: keep
4#include <functional>
5#include <utility>
6
7#include "traits.hpp"
8
9namespace yorch::detail {
10
20template <typename R>
22 static_assert(default_catch_supported_v<R>,
23 "Default catch_as_failure only supports void and step_result");
24
25 static_assert(std::is_void_v<R> || std::is_same_v<R, step_result>);
26 return step_result::failure();
27}
28
38template <typename R, typename Policy>
40 Policy& policy,
41 const std::exception_ptr& ep) noexcept {
43 "catch_as_failure(task, policy) requires a noexcept fallback taking std::exception_ptr and returning a compatible result");
44
45 if constexpr (std::is_void_v<R>) {
46 return std::invoke(policy, ep);
47 } else {
48 return static_cast<R>(std::invoke(policy, ep));
49 }
50}
51
62template <typename PolicyResult>
64 static_assert(std::is_same_v<PolicyResult, step_result>,
65 "Void task catch policy must return step_result");
66 return step_result::success();
67}
68
69template <typename R>
70[[nodiscard]] constexpr bool raw_result_requests_retry(const R& r) noexcept {
71 using raw_t = std::remove_cvref_t<R>;
72
73 if constexpr (std::is_same_v<raw_t, step_result>) {
74 return r.status == step_status::retry;
75 } else if constexpr (is_task_result_v<raw_t>) {
76 return r.step.status == step_status::retry;
77 } else {
78 static_cast<void>(r);
79 return false;
80 }
81}
82
83template <typename R>
84inline constexpr bool retry_status_capable_result_v =
85 std::is_same_v<std::remove_cvref_t<R>, step_result> ||
87
88template <typename Raw>
89[[nodiscard]] constexpr auto retry_exhausted_as_failure(const Raw& raw) noexcept {
90 using raw_t = std::remove_cvref_t<Raw>;
91
93 "Retry exhaustion fallback is only valid for results that carry step_status");
94
95 static_cast<void>(raw);
96
97 if constexpr (std::is_same_v<raw_t, step_result>) {
98 return step_result::failure();
99 } else {
100 return raw_t::failure();
101 }
102}
103
104template <typename Policy, typename Raw>
106 requires(const std::remove_cvref_t<Policy>& policy, Raw&& raw) {
107 { policy.on_exhausted(std::forward<Raw>(raw)) } noexcept
108 -> std::convertible_to<std::remove_cvref_t<Raw>>;
109 };
110
111template <typename Policy, typename Raw>
112[[nodiscard]] constexpr auto handle_retry_exhausted(const Policy& policy, Raw&& raw) noexcept {
114 return policy.on_exhausted(std::forward<Raw>(raw));
115 } else {
116 return std::forward<Raw>(raw);
117 }
118}
119
120} // namespace yorch::detail
constexpr bool raw_result_requests_retry(const R &r) noexcept
constexpr bool retry_status_capable_result_v
auto policy_catch_failure_result(Policy &policy, const std::exception_ptr &ep) noexcept
Invokes a fallback policy to produce the exception-path raw result.
constexpr auto default_catch_failure_result() noexcept
Produces the default failure result used by catch_as_failure(task).
constexpr auto retry_exhausted_as_failure(const Raw &raw) noexcept
constexpr auto handle_retry_exhausted(const Policy &policy, Raw &&raw) noexcept
constexpr auto void_task_success_result() noexcept
Produces the success-path raw result for a wrapped void task when a custom fallback policy is install...
constexpr bool is_adapter_descriptor_v
Definition adapters.hpp:63
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