LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
getresult.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 <atomic>
12 #include <type_traits>
13 #include <QEventLoop>
14 #include "task.h"
15 
16 namespace LC::Util
17 {
18  template<typename T, template<typename> typename... Extensions>
20  {
21  constexpr bool isVoid = std::is_same_v<T, void>;
22  std::conditional_t<isVoid, void*, std::unique_ptr<T>> result;
23 
24  std::exception_ptr exception;
25 
26  QEventLoop loop;
27  bool done = false;
28  [] (auto task, auto& result, auto& exception, auto& done, auto& loop) -> Task<void>
29  {
30  try
31  {
32  if constexpr (isVoid)
33  co_await task;
34  else
35  result = std::make_unique<T> (co_await task);
36  }
37  catch (...)
38  {
39  exception = std::current_exception ();
40  }
41  done = true;
42  loop.quit ();
43  } (task, result, exception, done, loop);
44  if (!done)
45  loop.exec ();
46 
47  if (exception)
48  std::rethrow_exception (exception);
49 
50  if constexpr (!isVoid)
51  return *result;
52  }
53 }
T GetTaskResult(Task< T, Extensions... > task)
Definition: getresult.h:19