LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
dbus.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 <memory>
13 #include <tuple>
14 #include <QDBusPendingCallWatcher>
15 #include <QDBusPendingReply>
16 #include <util/sll/either.h>
17 #include <util/sll/void.h>
18 
19 namespace LC::Util::detail
20 {
21  template<typename... Rets>
22  struct DBusAwaiter
23  {
24  QDBusPendingReply<Rets...> Reply_;
25  std::unique_ptr<QDBusPendingCallWatcher> Watcher_;
26 
27  DBusAwaiter (const QDBusPendingReply<Rets...>& reply)
28  : Reply_ { reply }
29  {
30  }
31 
32  bool await_ready () const noexcept
33  {
34  return Reply_.isFinished ();
35  }
36 
37  void await_suspend (std::coroutine_handle<> handle) noexcept
38  {
39  Watcher_ = std::make_unique<QDBusPendingCallWatcher> (Reply_);
40  QObject::connect (Watcher_.get (),
41  &QDBusPendingCallWatcher::finished,
42  handle);
43  }
44 
45  template<typename = void>
46  struct SuccessType;
47 
48  template<typename T> requires (sizeof... (Rets) == 0)
49  struct SuccessType<T> { using Type = Void; };
50 
51  template<typename T> requires (sizeof... (Rets) == 1)
52  struct SuccessType<T> { using Type = Rets... [0]; };
53 
54  template<typename T> requires (sizeof... (Rets) > 1)
55  struct SuccessType<T> { using Type = std::tuple<Rets...>; };
56 
58 
60  {
61  if (Reply_.isError ())
62  return { AsLeft, Reply_.error () };
63 
64  if constexpr (sizeof... (Rets) == 0)
65  return { Void {} };
66  else if constexpr (sizeof... (Rets) == 1)
67  return { Reply_.value () };
68  else
69  return [&]<size_t... Idxs> (std::index_sequence<Idxs...>) -> std::tuple<Rets...>
70  {
71  return { Reply_.template argumentAt<Idxs> ()... };
72  } (std::make_index_sequence<sizeof... (Rets)> {});
73  }
74  };
75 }
76 
77 namespace LC
78 {
79  template<typename... Rets>
80  Util::detail::DBusAwaiter<Rets...> operator co_await (const QDBusPendingReply<Rets...>& reply)
81  {
82  return { reply };
83  }
84 }
85 
86 namespace LC::Util
87 {
88  template<typename... Rets>
89  detail::DBusAwaiter<Rets...> Typed (const QDBusPendingCall& asyncCall)
90  {
91  return { asyncCall };
92  }
93 }
detail::DBusAwaiter< Rets... > Typed(const QDBusPendingCall &asyncCall)
Definition: dbus.h:89
QDBusPendingReply< Rets... > Reply_
Definition: dbus.h:24
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptrs... > > tuple
Definition: oral.h:1086
Type
Describes the various types of XDG .desktop files.
Definition: itemtypes.h:19
Either< QDBusError, SuccessType_t > await_resume() const noexcept
Definition: dbus.h:59
DBusAwaiter(const QDBusPendingReply< Rets... > &reply)
Definition: dbus.h:27
requires(sizeof...(Rets)==1) struct SuccessType< T >
Definition: dbus.h:51
constexpr auto AsLeft
Definition: either.h:27
SuccessType<>::Type SuccessType_t
Definition: dbus.h:57
bool await_ready() const noexcept
Definition: dbus.h:32
void await_suspend(std::coroutine_handle<> handle) noexcept
Definition: dbus.h:37
requires(sizeof...(Rets) > 1) struct SuccessType< T >
Definition: dbus.h:54
auto Tup2 &&tup2 noexcept
Definition: ctstringutils.h:68
std::unique_ptr< QDBusPendingCallWatcher > Watcher_
Definition: dbus.h:25
requires(sizeof...(Rets)==0) struct SuccessType< T >
Definition: dbus.h:48
A proper void type, akin to unit (or ()) type in functional languages.
Definition: void.h:20