1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtDBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QDBUSPENDINGREPLY_H
41#define QDBUSPENDINGREPLY_H
42
43#include <QtDBus/qtdbusglobal.h>
44#include <QtDBus/qdbusargument.h>
45#include <QtDBus/qdbuspendingcall.h>
46
47#ifndef QT_NO_DBUS
48
49QT_BEGIN_NAMESPACE
50
51
52class Q_DBUS_EXPORT QDBusPendingReplyBase : public QDBusPendingCall
53{
54protected:
55 QDBusPendingReplyBase();
56 ~QDBusPendingReplyBase();
57 void assign(const QDBusPendingCall &call);
58 void assign(const QDBusMessage &message);
59
60 QVariant argumentAt(int index) const;
61 void setMetaTypes(int count, const QMetaType *metaTypes);
62};
63
64namespace QDBusPendingReplyTypes {
65 template<int Index, typename T, typename... Types>
66 struct Select
67 {
68 typedef Select<Index - 1, Types...> Next;
69 typedef typename Next::Type Type;
70 };
71 template<typename T, typename... Types>
72 struct Select<0, T, Types...>
73 {
74 typedef T Type;
75 };
76
77 template<typename T> inline QMetaType metaTypeFor()
78 { return QMetaType::fromType<T>(); }
79 // specialize for QVariant, allowing it to be used in place of QDBusVariant
80 template<> inline QMetaType metaTypeFor<QVariant>()
81 { return QMetaType::fromType<QDBusVariant>(); }
82}
83
84
85template<typename... Types>
86class QDBusPendingReply : public QDBusPendingReplyBase
87{
88 template<int Index> using Select = QDBusPendingReplyTypes::Select<Index, Types...>;
89public:
90 enum { Count = std::is_same_v<typename Select<0>::Type, void> ? 0 : sizeof...(Types) };
91
92 inline constexpr int count() const { return Count; }
93
94
95 inline QDBusPendingReply() = default;
96 inline QDBusPendingReply(const QDBusPendingReply &other)
97 : QDBusPendingReplyBase(other)
98 { }
99 inline Q_IMPLICIT QDBusPendingReply(const QDBusPendingCall &call) // required by qdbusxml2cpp-generated code
100 { *this = call; }
101 inline Q_IMPLICIT QDBusPendingReply(const QDBusMessage &message)
102 { *this = message; }
103
104 inline QDBusPendingReply &operator=(const QDBusPendingReply &other)
105 { assign(other); return *this; }
106 inline QDBusPendingReply &operator=(const QDBusPendingCall &call)
107 { assign(call); return *this; }
108 inline QDBusPendingReply &operator=(const QDBusMessage &message)
109 { assign(message); return *this; }
110
111 using QDBusPendingReplyBase::argumentAt;
112 template<int Index> inline
113 typename Select<Index>::Type argumentAt() const
114 {
115 static_assert(Index >= 0 && Index < Count, "Index out of bounds");
116 typedef typename Select<Index>::Type ResultType;
117 return qdbus_cast<ResultType>(argumentAt(Index));
118 }
119
120#if defined(Q_CLANG_QDOC)
121 bool isFinished() const;
122 void waitForFinished();
123 QVariant argumentAt(int index) const;
124
125 bool isValid() const;
126 bool isError() const;
127 QDBusError error() const;
128 QDBusMessage reply() const;
129#endif
130
131 inline typename Select<0>::Type value() const
132 {
133 return argumentAt<0>();
134 }
135
136 inline operator typename Select<0>::Type() const
137 {
138 return argumentAt<0>();
139 }
140
141private:
142 inline void calculateMetaTypes()
143 {
144 if (!d) return;
145 if constexpr (Count == 0) {
146 setMetaTypes(0, nullptr);
147 } else {
148 std::array<QMetaType, Count> typeIds = { QDBusPendingReplyTypes::metaTypeFor<Types>()... };
149 setMetaTypes(Count, typeIds.data());
150 }
151 }
152
153 inline void assign(const QDBusPendingCall &call)
154 {
155 QDBusPendingReplyBase::assign(call);
156 calculateMetaTypes();
157 }
158
159 inline void assign(const QDBusMessage &message)
160 {
161 QDBusPendingReplyBase::assign(message);
162 calculateMetaTypes();
163 }
164};
165
166template<>
167class QDBusPendingReply<> : public QDBusPendingReplyBase
168{
169public:
170 enum { Count = 0 };
171 inline int count() const { return Count; }
172
173 inline QDBusPendingReply() = default;
174 inline QDBusPendingReply(const QDBusPendingReply &other)
175 : QDBusPendingReplyBase(other)
176 { }
177 inline Q_IMPLICIT QDBusPendingReply(const QDBusPendingCall &call) // required by qdbusxml2cpp-generated code
178 { *this = call; }
179 inline Q_IMPLICIT QDBusPendingReply(const QDBusMessage &message)
180 { *this = message; }
181
182 inline QDBusPendingReply &operator=(const QDBusPendingReply &other)
183 { assign(other); return *this; }
184 inline QDBusPendingReply &operator=(const QDBusPendingCall &call)
185 { assign(call); return *this; }
186 inline QDBusPendingReply &operator=(const QDBusMessage &message)
187 { assign(message); return *this; }
188
189private:
190 inline void assign(const QDBusPendingCall &call)
191 {
192 QDBusPendingReplyBase::assign(call);
193 if (d)
194 setMetaTypes(0, nullptr);
195 }
196
197 inline void assign(const QDBusMessage &message)
198 {
199 QDBusPendingReplyBase::assign(message);
200 if (d)
201 setMetaTypes(0, nullptr);
202 }
203
204};
205
206QT_END_NAMESPACE
207
208#endif // QT_NO_DBUS
209#endif
210