LeechCraft 0.6.70-19424-g027e980fd1
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 &Watcher_,
38 handle);
39 }
40
41 R await_resume () const noexcept
42 {
43 if constexpr (!std::is_same_v<R, void>)
44 return Watcher_.future ().result ();
45 }
46 };
47}
48
49namespace LC::Util
50{
51 template<typename R>
53 {
54 return { future };
55 }
56}
57
58namespace LC
59{
60 template<typename R>
61 auto operator co_await (QFuture<R> future)
62 {
63 return Util::GetAwaiter (std::move (future), {});
64 }
65}
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:41