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 QDBUSREPLY_H |
41 | #define QDBUSREPLY_H |
42 | |
43 | #include <QtDBus/qtdbusglobal.h> |
44 | #include <QtCore/qvariant.h> |
45 | |
46 | #include <QtDBus/qdbusmessage.h> |
47 | #include <QtDBus/qdbuserror.h> |
48 | #include <QtDBus/qdbusextratypes.h> |
49 | #include <QtDBus/qdbuspendingreply.h> |
50 | |
51 | #ifndef QT_NO_DBUS |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | |
56 | Q_DBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data); |
57 | |
58 | template<typename T> |
59 | class QDBusReply |
60 | { |
61 | typedef T Type; |
62 | public: |
63 | inline QDBusReply(const QDBusMessage &reply) |
64 | { |
65 | *this = reply; |
66 | } |
67 | inline QDBusReply(const QDBusReply &) = default; |
68 | inline QDBusReply& operator=(const QDBusMessage &reply) |
69 | { |
70 | QVariant data(QMetaType::fromType<Type>()); |
71 | qDBusReplyFill(reply, m_error, data); |
72 | m_data = qvariant_cast<Type>(data); |
73 | return *this; |
74 | } |
75 | |
76 | inline QDBusReply(const QDBusPendingCall &pcall) |
77 | { |
78 | *this = pcall; |
79 | } |
80 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) |
81 | { |
82 | QDBusPendingCall other(pcall); |
83 | other.waitForFinished(); |
84 | return *this = other.reply(); |
85 | } |
86 | inline QDBusReply(const QDBusPendingReply<T> &reply) |
87 | { |
88 | *this = static_cast<QDBusPendingCall>(reply); |
89 | } |
90 | |
91 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) |
92 | : m_error(dbusError), m_data(Type()) |
93 | { |
94 | } |
95 | inline QDBusReply& operator=(const QDBusError& dbusError) |
96 | { |
97 | m_error = dbusError; |
98 | m_data = Type(); |
99 | return *this; |
100 | } |
101 | |
102 | inline QDBusReply& operator=(const QDBusReply& other) |
103 | { |
104 | m_error = other.m_error; |
105 | m_data = other.m_data; |
106 | return *this; |
107 | } |
108 | |
109 | inline bool isValid() const { return !m_error.isValid(); } |
110 | |
111 | inline const QDBusError& error() { return m_error; } |
112 | inline const QDBusError& error() const { return m_error; } |
113 | |
114 | inline Type value() const |
115 | { |
116 | return m_data; |
117 | } |
118 | |
119 | inline operator Type () const |
120 | { |
121 | return m_data; |
122 | } |
123 | |
124 | private: |
125 | QDBusError m_error; |
126 | Type m_data; |
127 | }; |
128 | |
129 | # ifndef Q_QDOC |
130 | // specialize for QVariant: |
131 | template<> inline QDBusReply<QVariant>& |
132 | QDBusReply<QVariant>::operator=(const QDBusMessage &reply) |
133 | { |
134 | QVariant data(QMetaType::fromType<QDBusVariant>()); |
135 | qDBusReplyFill(reply, m_error, data); |
136 | m_data = qvariant_cast<QDBusVariant>(data).variant(); |
137 | return *this; |
138 | } |
139 | |
140 | // specialize for void: |
141 | template<> |
142 | class QDBusReply<void> |
143 | { |
144 | public: |
145 | inline QDBusReply(const QDBusMessage &reply) |
146 | : m_error(reply) |
147 | { |
148 | } |
149 | inline QDBusReply& operator=(const QDBusMessage &reply) |
150 | { |
151 | m_error = QDBusError(reply); |
152 | return *this; |
153 | } |
154 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) |
155 | : m_error(dbusError) |
156 | { |
157 | } |
158 | inline QDBusReply(const QDBusPendingCall &pcall) |
159 | { |
160 | *this = pcall; |
161 | } |
162 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) |
163 | { |
164 | QDBusPendingCall other(pcall); |
165 | other.waitForFinished(); |
166 | return *this = other.reply(); |
167 | } |
168 | inline QDBusReply& operator=(const QDBusError& dbusError) |
169 | { |
170 | m_error = dbusError; |
171 | return *this; |
172 | } |
173 | |
174 | inline QDBusReply(const QDBusReply &) = default; |
175 | |
176 | inline QDBusReply& operator=(const QDBusReply& other) |
177 | { |
178 | m_error = other.m_error; |
179 | return *this; |
180 | } |
181 | |
182 | inline bool isValid() const { return !m_error.isValid(); } |
183 | |
184 | inline const QDBusError& error() { return m_error; } |
185 | inline const QDBusError& error() const { return m_error; } |
186 | |
187 | private: |
188 | QDBusError m_error; |
189 | }; |
190 | # endif |
191 | |
192 | QT_END_NAMESPACE |
193 | |
194 | #endif // QT_NO_DBUS |
195 | #endif |
196 | |