co_usb
Loading...
Searching...
No Matches
error_protocol.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/capy/io_task.hpp>
4#include <concepts>
5#include <optional>
6#include <system_error>
7#include <type_traits>
8#include <utility>
9
10#include <version>
11
12#ifdef __cpp_lib_expected
13#include <expected>
14#endif // __cpp_lib_expected
15
16namespace co_usb::detail
17{
18
38template <typename Ty, typename ValTy>
39concept ErrorProtocol = requires(Ty errp) {
40 typename Ty::template return_type<ValTy>;
41 {
42 errp.template with_error<ValTy>(std::declval<std::error_code>())
43 } -> std::same_as<typename Ty::template return_type<ValTy>>;
44 {
45 errp.template with_success<ValTy>(std::declval<ValTy>())
46 } -> std::same_as<typename Ty::template return_type<ValTy>>;
47};
48
60{
61 template <typename ValTy> using return_type = ValTy;
62
63 constexpr explicit as_exception_t ()
64 {
65 }
66
67 template <typename U> [[noreturn]] auto with_error (std::error_code ec) -> return_type<U>
68 {
69 throw std::system_error{ec};
70 }
71
72 template <typename U, typename... Args> auto with_success (Args &&...args) -> return_type<U>
73 {
74 return U(std::forward<Args>(args)...);
75 }
76};
77static_assert(ErrorProtocol<as_exception_t, int>, "Not an error protocol");
78
91{
92 template <typename ValTy> using return_type = std::optional<ValTy>;
93
94 constexpr explicit as_optional_t (std::error_code &ec) noexcept : m_ec_ref(ec)
95 {
96 }
97
103 template <typename U> auto with_error (std::error_code ec) noexcept -> return_type<U>
104 {
105 m_ec_ref = ec;
106 return std::nullopt;
107 }
108
114 template <typename U, typename... Args>
115 auto with_success (Args &&...args) noexcept(std::is_nothrow_constructible_v<U, Args...>)
117 {
118 m_ec_ref.clear();
119 return std::optional<U>(std::in_place, std::forward<Args>(args)...);
120 }
121
122 private:
123 std::error_code &m_ec_ref;
124};
125static_assert(ErrorProtocol<as_optional_t, int>, "Not an error protocol");
126
127#ifdef __cpp_lib_expected
136struct as_expected_t
137{
138 template <typename ValTy> using return_type = std::expected<ValTy, std::error_code>;
139
140 constexpr explicit as_expected_t () noexcept
141 {
142 }
143
149 template <typename U> auto with_error (std::error_code ec) noexcept -> return_type<U>
150 {
151 return std::unexpected(ec);
152 }
153
159 template <typename U, typename... Args>
160 auto with_success (Args &&...args) noexcept(std::is_nothrow_constructible_v<U, Args...>)
161 -> return_type<U>
162 {
163 return return_type<U>(std::in_place, std::forward<Args>(args)...);
164 }
165};
166static_assert(ErrorProtocol<as_expected_t, int>, "Not an error protocol");
167#endif // __cpp_lib_expected
168
170{
171 template <typename ValTy> using return_type = boost::capy::io_result<std::optional<ValTy>>;
172
173 constexpr explicit as_io_result_t () noexcept
174 {
175 }
176
177 template <typename U> auto with_error (std::error_code ec) noexcept -> return_type<U>
178 {
179 return {ec, std::nullopt};
180 }
181
182 template <typename U, typename... Args>
183 auto with_success (Args &&...args) noexcept(std::is_nothrow_constructible_v<U, Args...>)
185 {
186 return {{}, std::optional<U>(std::in_place, std::forward<Args>(args)...)};
187 }
188};
189static_assert(ErrorProtocol<as_io_result_t, int>, "Not an error protocol");
190
192{
193 template <typename ValTy> using return_type = boost::capy::io_task<std::optional<ValTy>>;
194
195 constexpr explicit as_io_task_t () noexcept
196 {
197 }
198
199 template <typename U> auto with_error (std::error_code ec) noexcept -> return_type<U>
200 {
201 co_return {ec, std::nullopt};
202 }
203
204 template <typename U, typename... Args>
205 auto with_success (Args &&...args) noexcept(std::is_nothrow_constructible_v<U, Args...>)
207 {
208 co_return {{}, std::optional<U>(std::in_place, std::forward<Args>(args)...)};
209 }
210};
211static_assert(ErrorProtocol<as_io_task_t, int>, "Not an error protocol");
212
213} // namespace co_usb::detail
Result protocol for signaling error via error code or full completion.
Definition error_protocol.hpp:39
Definition flag_type.hpp:6
Exception error protocol.
Definition error_protocol.hpp:60
auto with_success(Args &&...args) -> return_type< U >
Definition error_protocol.hpp:72
auto with_error(std::error_code ec) -> return_type< U >
Definition error_protocol.hpp:67
ValTy return_type
Definition error_protocol.hpp:61
constexpr as_exception_t()
Definition error_protocol.hpp:63
Definition error_protocol.hpp:170
auto with_error(std::error_code ec) noexcept -> return_type< U >
Definition error_protocol.hpp:177
constexpr as_io_result_t() noexcept
Definition error_protocol.hpp:173
boost::capy::io_result< std::optional< ValTy > > return_type
Definition error_protocol.hpp:171
auto with_success(Args &&...args) noexcept(std::is_nothrow_constructible_v< U, Args... >) -> return_type< U >
Definition error_protocol.hpp:183
Definition error_protocol.hpp:192
auto with_error(std::error_code ec) noexcept -> return_type< U >
Definition error_protocol.hpp:199
boost::capy::io_task< std::optional< ValTy > > return_type
Definition error_protocol.hpp:193
auto with_success(Args &&...args) noexcept(std::is_nothrow_constructible_v< U, Args... >) -> return_type< U >
Definition error_protocol.hpp:205
constexpr as_io_task_t() noexcept
Definition error_protocol.hpp:195
Error protocol signaling error into a given error code reference and returning an optional.
Definition error_protocol.hpp:91
auto with_success(Args &&...args) noexcept(std::is_nothrow_constructible_v< U, Args... >) -> return_type< U >
Signals success via returned optional.
Definition error_protocol.hpp:115
auto with_error(std::error_code ec) noexcept -> return_type< U >
Signals error to stored error code reference.
Definition error_protocol.hpp:103
constexpr as_optional_t(std::error_code &ec) noexcept
Definition error_protocol.hpp:94
std::optional< ValTy > return_type
Definition error_protocol.hpp:92