LeechCraft 0.6.70-17609-g3dde4097dd
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 struct IgnoreLeft {};
17
18 namespace detail
19 {
20 template<typename Promise>
21 [[noreturn]]
22 void TerminateLeftyCoroutine (std::coroutine_handle<Promise> handle, const auto& error)
23 {
24 auto& promise = handle.promise ();
25 if constexpr (Promise::IsVoid)
26 promise.return_void ();
27 else
28 promise.return_value (Left { error });
29
30 throw EitherFailureAbort {};
31 }
32
33 template<typename L, typename R, typename ErrorHandler>
35 {
37 ErrorHandler Handler_;
38
39 using HandlerReturn_t = decltype (Handler_ (Either_.GetLeft ()));
40
41 bool await_ready () const noexcept
42 {
43 return Either_.IsRight ();
44 }
45
46 void await_suspend (auto handle)
47 {
48 if constexpr (std::is_same_v<void, HandlerReturn_t>)
49 {
50 Handler_ (Either_.GetLeft ());
51 TerminateLeftyCoroutine (handle, Either_.GetLeft ());
52 }
53 else if constexpr (std::is_same_v<IgnoreLeft, HandlerReturn_t>)
54 {
55 static_assert (std::is_default_constructible_v<R>);
56 Handler_ (Either_.GetLeft ());
57 Either_ = R {};
58 }
59 else
60 TerminateLeftyCoroutine (handle, Handler_ (Either_.GetLeft ()));
61 }
62
63 R await_resume () const noexcept
64 {
65 return Either_.GetRight ();
66 }
67 };
68 }
69
70 template<typename L, typename R, typename F>
71 requires std::invocable<F, const L&>
73 {
74 return { either, std::forward<F> (errorHandler) };
75 }
76}
77
78namespace LC
79{
80 template<typename L, typename R>
82 {
83 return { either, {} };
84 }
85}
void TerminateLeftyCoroutine(std::coroutine_handle< Promise > handle, const auto &error)
Definition either.h:22
detail::EitherAwaiter< L, R, F > WithHandler(const Either< L, R > &either, F &&errorHandler)
Definition either.h:72
Definition constants.h:15
void await_suspend(auto handle)
Definition either.h:46
R await_resume() const noexcept
Definition either.h:63
decltype(Handler_(Either_.GetLeft())) HandlerReturn_t
Definition either.h:39
bool await_ready() const noexcept
Definition either.h:41