LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
future.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 <coroutine>
12#include <QFuture>
13#include <QFutureWatcher>
14#include "taskfwd.h"
15
16namespace LC::Util::detail
17{
18 template<typename R>
20 {
21 QFutureWatcher<R> Watcher_;
22
23 FutureAwaiter (const QFuture<R>& future)
24 {
25 Watcher_.setFuture (future);
26 }
27
28 bool await_ready () const noexcept
29 {
30 return Watcher_.future ().isFinished ();
31 }
32
33 void await_suspend (std::coroutine_handle<> handle) noexcept
34 {
35 QObject::connect (&Watcher_,
36 &QFutureWatcher<R>::finished,
37 handle);
38 }
39
40 R await_resume () const noexcept
41 {
42 if constexpr (!std::is_same_v<R, void>)
43 return Watcher_.future ().result ();
44 }
45 };
46}
47
48namespace LC::Util
49{
50 template<typename R>
52 {
53 return { future };
54 }
55}
56
57namespace LC
58{
59 template<typename R>
60 auto operator co_await (QFuture<R> future)
61 {
62 return Util::GetAwaiter (std::move (future), {});
63 }
64}
detail::EitherAwaiter< L, R, std::identity > GetAwaiter(const Either< L, R > &either, AwaiterCPO)
Definition either.h:119
Definition constants.h:15
bool await_ready() const noexcept
Definition future.h:28
FutureAwaiter(const QFuture< R > &future)
Definition future.h:23
void await_suspend(std::coroutine_handle<> handle) noexcept
Definition future.h:33
QFutureWatcher< R > Watcher_
Definition future.h:21
R await_resume() const noexcept
Definition future.h:40