LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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#include "taskfwd.h"
19
20namespace LC::Util::detail
21{
22 template<typename... Rets>
24 {
25 QDBusPendingReply<Rets...> Reply_;
26 std::unique_ptr<QDBusPendingCallWatcher> Watcher_;
27
28 DBusAwaiter (const QDBusPendingReply<Rets...>& reply)
29 : Reply_ { reply }
30 {
31 }
32
33 bool await_ready () const noexcept
34 {
35 return Reply_.isFinished ();
36 }
37
38 void await_suspend (std::coroutine_handle<> handle) noexcept
39 {
40 Watcher_ = std::make_unique<QDBusPendingCallWatcher> (Reply_);
41 QObject::connect (Watcher_.get (),
42 &QDBusPendingCallWatcher::finished,
43 Watcher_.get (),
44 handle);
45 }
46
47 template<typename = void>
49
50 template<typename T> requires (sizeof... (Rets) == 0)
51 struct SuccessType<T> { using Type = Void; };
52
53 template<typename T> requires (sizeof... (Rets) == 1)
54 struct SuccessType<T> { using Type = Rets... [0]; };
55
56 template<typename T> requires (sizeof... (Rets) > 1)
57 struct SuccessType<T> { using Type = std::tuple<Rets...>; };
58
60
62 {
63 if (Reply_.isError ())
64 return { AsLeft, Reply_.error () };
65
66 if constexpr (sizeof... (Rets) == 0)
67 return { Void {} };
68 else if constexpr (sizeof... (Rets) == 1)
69 return { Reply_.value () };
70 else
71 return [&]<size_t... Idxs> (std::index_sequence<Idxs...>) -> std::tuple<Rets...>
72 {
73 return { Reply_.template argumentAt<Idxs> ()... };
74 } (std::make_index_sequence<sizeof... (Rets)> {});
75 }
76 };
77}
78
79namespace LC::Util
80{
81 template<typename... Rets>
82 detail::DBusAwaiter<Rets...> Reply (const QDBusPendingReply<Rets...>& reply)
83 {
84 return { reply };
85 }
86
87 template<typename... Rets>
88 detail::DBusAwaiter<Rets...> Typed (const QDBusPendingCall& asyncCall)
89 {
90 return { asyncCall };
91 }
92
93 template<typename... Rets, typename... Actual>
94 detail::DBusAwaiter<Rets...> Typed (const QDBusPendingReply<Actual...>&) = delete ("do not use Util::Typed to await typed QDBusPendingReply<...>");
95
96 template<typename... Rets>
97 detail::DBusAwaiter<Rets...> GetAwaiter (const QDBusPendingReply<Rets...>& reply, AwaiterCPO)
98 {
99 return { reply };
100 }
101}
102
103namespace LC
104{
105 template<typename... Rets>
106 auto operator co_await (const QDBusPendingReply<Rets...>& reply)
107 {
108 return Util::GetAwaiter (reply, {});
109 }
110}
111
112namespace LC::Util::DBus
113{
114 template<typename... Args>
116 {
117 QDBusPendingReply<Args...> Raw_;
118
119 detail::DBusAwaiter<Args...> operator co_await () const
120 {
121 return { Raw_ };
122 }
123 };
124}
constexpr auto AsLeft
Definition either.h:28
detail::DBusAwaiter< Rets... > Typed(const QDBusPendingCall &asyncCall)
Definition dbus.h:88
detail::EitherAwaiter< L, R, std::identity > GetAwaiter(const Either< L, R > &either, AwaiterCPO)
Definition either.h:119
detail::DBusAwaiter< Rets... > Reply(const QDBusPendingReply< Rets... > &reply)
Definition dbus.h:82
Definition constants.h:15
QDBusPendingReply< Args... > Raw_
Definition dbus.h:117
A proper void type, akin to unit (or ()) type in functional languages.
Definition void.h:21
bool await_ready() const noexcept
Definition dbus.h:33
std::unique_ptr< QDBusPendingCallWatcher > Watcher_
Definition dbus.h:26
QDBusPendingReply< Rets... > Reply_
Definition dbus.h:25
DBusAwaiter(const QDBusPendingReply< Rets... > &reply)
Definition dbus.h:28
void await_suspend(std::coroutine_handle<> handle) noexcept
Definition dbus.h:38
Either< QDBusError, SuccessType_t > await_resume() const noexcept
Definition dbus.h:61
SuccessType<>::Type SuccessType_t
Definition dbus.h:59