LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
either.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <util/sll/either.h>
12#include "task.h"
13
14namespace LC::Util
15{
16 template<typename L, std::default_initializable R>
18 {
20
21 bool await_ready () const noexcept
22 {
23 return true;
24 }
25
26 void await_suspend (auto) const noexcept
27 {
28 }
29
30 R await_resume () const noexcept
31 {
32 return RightOr (Result_, R {});
33 }
34 };
35
36 template<typename L, typename R>
38
39 namespace detail
40 {
41 template<typename Promise>
42 [[noreturn]]
43 void TerminateLeftyCoroutine (std::coroutine_handle<Promise> handle, const auto& error)
44 {
45 auto& promise = handle.promise ();
46 if constexpr (Promise::IsVoid)
47 promise.return_void ();
48 else
49 promise.return_value (Promise::ReturnType_t::Left (error));
50
51 throw EitherFailureAbort {};
52 }
53
54 template<typename ErrorHandler>
56 {
57 ErrorHandler Handler_;
58
59 template<typename L>
60 void HandleError (L&& left)
61 {
62 Handler_ (std::forward<L> (left));
63 }
64 };
65
66 template<>
68 {
69 void HandleError (auto&&)
70 {
71 }
72 };
73
74 template<typename L, typename R, typename ErrorHandler = void>
76 {
79
80 bool await_ready () const noexcept
81 {
82 return Either_.IsRight ();
83 }
84
85 void await_suspend (auto handle)
86 {
87 Handler_.HandleError (Either_.GetLeft ());
88 TerminateLeftyCoroutine (handle, Either_.GetLeft ());
89 }
90
91 R await_resume () const noexcept
92 {
93 return Either_.GetRight ();
94 }
95 };
96 }
97
98 template<typename L, typename R, typename F>
99 requires std::invocable<F, const L&>
101 {
102 return { either, { std::forward<F> (errorHandler) } };
103 }
104}
105
106namespace LC
107{
108 template<typename L, typename R>
110 {
111 return { either };
112 }
113}
void TerminateLeftyCoroutine(std::coroutine_handle< Promise > handle, const auto &error)
Definition either.h:43
IgnoreLeft(Either< L, R >) -> IgnoreLeft< L, R >
R RightOr(const Either< L, R > &either, F &&f)
Definition either.h:175
Util::detail::EitherAwaiter< L, R, F > WithHandler(const Util::Either< L, R > &either, F &&errorHandler)
Definition either.h:100
Definition constants.h:15
R await_resume() const noexcept
Definition either.h:30
Either< L, R > Result_
Definition either.h:19
void await_suspend(auto) const noexcept
Definition either.h:26
bool await_ready() const noexcept
Definition either.h:21
void await_suspend(auto handle)
Definition either.h:85
R await_resume() const noexcept
Definition either.h:91
EitherAwaiterErrorHandler< ErrorHandler > Handler_
Definition either.h:78
bool await_ready() const noexcept
Definition either.h:80