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
53QT_BEGIN_NAMESPACE
54
55
56Q_DBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data);
57
58template<typename T>
59class QDBusReply
60{
61 typedef T Type;
62public:
63 inline QDBusReply(const QDBusMessage &reply)
64 {
65 *this = reply;
66 }
67 inline QDBusReply& operator=(const QDBusMessage &reply)
68 {
69 QVariant data(qMetaTypeId<Type>(), nullptr);
70 qDBusReplyFill(reply, m_error, data);
71 m_data = qvariant_cast<Type>(data);
72 return *this;
73 }
74
75 inline QDBusReply(const QDBusPendingCall &pcall)
76 {
77 *this = pcall;
78 }
79 inline QDBusReply &operator=(const QDBusPendingCall &pcall)
80 {
81 QDBusPendingCall other(pcall);
82 other.waitForFinished();
83 return *this = other.reply();
84 }
85 inline QDBusReply(const QDBusPendingReply<T> &reply)
86 {
87 *this = static_cast<QDBusPendingCall>(reply);
88 }
89
90 inline QDBusReply(const QDBusError &dbusError = QDBusError())
91 : m_error(dbusError), m_data(Type())
92 {
93 }
94 inline QDBusReply& operator=(const QDBusError& dbusError)
95 {
96 m_error = dbusError;
97 m_data = Type();
98 return *this;
99 }
100
101 inline QDBusReply& operator=(const QDBusReply& other)
102 {
103 m_error = other.m_error;
104 m_data = other.m_data;
105 return *this;
106 }
107
108 inline bool isValid() const { return !m_error.isValid(); }
109
110 inline const QDBusError& error() { return m_error; }
111 inline const QDBusError& error() const { return m_error; }
112
113 inline Type value() const
114 {
115 return m_data;
116 }
117
118 inline operator Type () const
119 {
120 return m_data;
121 }
122
123private:
124 QDBusError m_error;
125 Type m_data;
126};
127
128# ifndef Q_QDOC
129// specialize for QVariant:
130template<> inline QDBusReply<QVariant>&
131QDBusReply<QVariant>::operator=(const QDBusMessage &reply)
132{
133 void *null = nullptr;
134 QVariant data(qMetaTypeId<QDBusVariant>(), null);
135 qDBusReplyFill(reply, m_error, data);
136 m_data = qvariant_cast<QDBusVariant>(data).variant();
137 return *this;
138}
139
140// specialize for void:
141template<>
142class QDBusReply<void>
143{
144public:
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& operator=(const QDBusReply& other)
175 {
176 m_error = other.m_error;
177 return *this;
178 }
179
180 inline bool isValid() const { return !m_error.isValid(); }
181
182 inline const QDBusError& error() { return m_error; }
183 inline const QDBusError& error() const { return m_error; }
184
185private:
186 QDBusError m_error;
187};
188# endif
189
190QT_END_NAMESPACE
191
192#endif // QT_NO_DBUS
193#endif
194