LeechCraft 0.6.70-19214-g121e5318a7
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 handle);
44 }
45
46 template<typename = void>
48
49 template<typename T> requires (sizeof... (Rets) == 0)
50 struct SuccessType<T> { using Type = Void; };
51
52 template<typename T> requires (sizeof... (Rets) == 1)
53 struct SuccessType<T> { using Type = Rets... [0]; };
54
55 template<typename T> requires (sizeof... (Rets) > 1)
56 struct SuccessType<T> { using Type = std::tuple<Rets...>; };
57
59
61 {
62 if (Reply_.isError ())
63 return { AsLeft, Reply_.error () };
64
65 if constexpr (sizeof... (Rets) == 0)
66 return { Void {} };
67 else if constexpr (sizeof... (Rets) == 1)
68 return { Reply_.value () };
69 else
70 return [&]<size_t... Idxs> (std::index_sequence<Idxs...>) -> std::tuple<Rets...>
71 {
72 return { Reply_.template argumentAt<Idxs> ()... };
73 } (std::make_index_sequence<sizeof... (Rets)> {});
74 }
75 };
76}
77
78namespace LC::Util
79{
80 template<typename... Rets>
81 detail::DBusAwaiter<Rets...> Reply (const QDBusPendingReply<Rets...>& reply)
82 {
83 return { reply };
84 }
85
86 template<typename... Rets>
87 detail::DBusAwaiter<Rets...> Typed (const QDBusPendingCall& asyncCall)
88 {
89 return { asyncCall };
90 }
91
92 template<typename... Rets, typename... Actual>
93 detail::DBusAwaiter<Rets...> Typed (const QDBusPendingReply<Actual...>&) = delete ("do not use Util::Typed to await typed QDBusPendingReply<...>");
94
95 template<typename... Rets>
96 detail::DBusAwaiter<Rets...> GetAwaiter (const QDBusPendingReply<Rets...>& reply, AwaiterCPO)
97 {
98 return { reply };
99 }
100}
101
102namespace LC
103{
104 template<typename... Rets>
105 auto operator co_await (const QDBusPendingReply<Rets...>& reply)
106 {
107 return Util::GetAwaiter (reply, {});
108 }
109}
110
111namespace LC::Util::DBus
112{
113 template<typename... Args>
115 {
116 QDBusPendingReply<Args...> Raw_;
117
118 detail::DBusAwaiter<Args...> operator co_await () const
119 {
120 return { Raw_ };
121 }
122 };
123}
constexpr auto AsLeft
Definition either.h:28
detail::DBusAwaiter< Rets... > Typed(const QDBusPendingCall &asyncCall)
Definition dbus.h:87
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:81
Definition constants.h:15
QDBusPendingReply< Args... > Raw_
Definition dbus.h:116
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:60
SuccessType<>::Type SuccessType_t
Definition dbus.h:58