1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QDBUSREPLY_H
43#define QDBUSREPLY_H
44
45#include <QtCore/qglobal.h>
46#include <QtCore/qvariant.h>
47
48#include <QtDBus/qdbusmacros.h>
49#include <QtDBus/qdbusmessage.h>
50#include <QtDBus/qdbuserror.h>
51#include <QtDBus/qdbusextratypes.h>
52#include <QtDBus/qdbuspendingreply.h>
53
54#ifndef QT_NO_DBUS
55
56QT_BEGIN_HEADER
57
58QT_BEGIN_NAMESPACE
59
60QT_MODULE(DBus)
61
62Q_DBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data);
63
64template<typename T>
65class QDBusReply
66{
67 typedef T Type;
68public:
69 inline QDBusReply(const QDBusMessage &reply)
70 {
71 *this = reply;
72 }
73 inline QDBusReply& operator=(const QDBusMessage &reply)
74 {
75 QVariant data(qMetaTypeId(&m_data), reinterpret_cast<void*>(0));
76 qDBusReplyFill(reply, m_error, data);
77 m_data = qvariant_cast<Type>(data);
78 return *this;
79 }
80
81 inline QDBusReply(const QDBusPendingCall &pcall)
82 {
83 *this = pcall;
84 }
85 inline QDBusReply &operator=(const QDBusPendingCall &pcall)
86 {
87 QDBusPendingCall other(pcall);
88 other.waitForFinished();
89 return *this = other.reply();
90 }
91 inline QDBusReply(const QDBusPendingReply<T> &reply)
92 {
93 *this = static_cast<QDBusPendingCall>(reply);
94 }
95
96 inline QDBusReply(const QDBusError &dbusError = QDBusError())
97 : m_error(dbusError), m_data(Type())
98 {
99 }
100 inline QDBusReply& operator=(const QDBusError& dbusError)
101 {
102 m_error = dbusError;
103 m_data = Type();
104 return *this;
105 }
106
107 inline QDBusReply& operator=(const QDBusReply& other)
108 {
109 m_error = other.m_error;
110 m_data = other.m_data;
111 return *this;
112 }
113
114 inline bool isValid() const { return !m_error.isValid(); }
115
116 inline const QDBusError& error() { return m_error; }
117
118 inline Type value() const
119 {
120 return m_data;
121 }
122
123 inline operator Type () const
124 {
125 return m_data;
126 }
127
128private:
129 QDBusError m_error;
130 Type m_data;
131};
132
133# ifndef Q_QDOC
134// specialize for QVariant:
135template<> inline QDBusReply<QVariant>&
136QDBusReply<QVariant>::operator=(const QDBusMessage &reply)
137{
138 void *null = 0;
139 QVariant data(qMetaTypeId<QDBusVariant>(), null);
140 qDBusReplyFill(reply, m_error, data);
141 m_data = qvariant_cast<QDBusVariant>(data).variant();
142 return *this;
143}
144
145// specialize for void:
146template<>
147class QDBusReply<void>
148{
149public:
150 inline QDBusReply(const QDBusMessage &reply)
151 : m_error(reply)
152 {
153 }
154 inline QDBusReply& operator=(const QDBusMessage &reply)
155 {
156 m_error = reply;
157 return *this;
158 }
159 inline QDBusReply(const QDBusError &dbusError = QDBusError())
160 : m_error(dbusError)
161 {
162 }
163 inline QDBusReply(const QDBusPendingCall &pcall)
164 {
165 *this = pcall;
166 }
167 inline QDBusReply &operator=(const QDBusPendingCall &pcall)
168 {
169 QDBusPendingCall other(pcall);
170 other.waitForFinished();
171 return *this = other.reply();
172 }
173 inline QDBusReply& operator=(const QDBusError& dbusError)
174 {
175 m_error = dbusError;
176 return *this;
177 }
178
179 inline QDBusReply& operator=(const QDBusReply& other)
180 {
181 m_error = other.m_error;
182 return *this;
183 }
184
185 inline bool isValid() const { return !m_error.isValid(); }
186
187 inline const QDBusError& error() { return m_error; }
188
189private:
190 QDBusError m_error;
191};
192# endif
193
194QT_END_NAMESPACE
195
196QT_END_HEADER
197
198#endif // QT_NO_DBUS
199#endif
200