LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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 
15 namespace LC::Util::detail
16 {
17  template<typename R>
19  {
20  QFutureWatcher<R> Watcher_;
21 
22  FutureAwaiter (const QFuture<R>& future)
23  {
24  Watcher_.setFuture (future);
25  }
26 
27  bool await_ready () const noexcept
28  {
29  return Watcher_.future ().isFinished ();
30  }
31 
32  void await_suspend (std::coroutine_handle<> handle) noexcept
33  {
34  QObject::connect (&Watcher_,
35  &QFutureWatcher<R>::finished,
36  handle);
37  }
38 
40  {
41  if constexpr (!std::is_same_v<R, void>)
42  return Watcher_.future ().result ();
43  }
44  };
45 }
46 
47 namespace LC
48 {
49  template<typename R>
50  Util::detail::FutureAwaiter<R> operator co_await (QFuture<R> future)
51  {
52  return { future };
53  }
54 }
R await_resume() const noexcept
Definition: future.h:39
FutureAwaiter(const QFuture< R > &future)
Definition: future.h:22
bool await_ready() const noexcept
Definition: future.h:27
QFutureWatcher< R > Watcher_
Definition: future.h:20
void await_suspend(std::coroutine_handle<> handle) noexcept
Definition: future.h:32
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:68