Update copyright year in license headers.
[profile/ivi/qtbase.git] / src / dbus / qdbusreply.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDBus module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
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
56 QT_BEGIN_HEADER
57
58 QT_BEGIN_NAMESPACE
59
60 QT_MODULE(DBus)
61
62 Q_DBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data);
63
64 template<typename T>
65 class QDBusReply
66 {
67     typedef T Type;
68 public:
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
128 private:
129     QDBusError m_error;
130     Type m_data;
131 };
132
133 # ifndef Q_QDOC
134 // specialize for QVariant:
135 template<> inline QDBusReply<QVariant>&
136 QDBusReply<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:
146 template<>
147 class QDBusReply<void>
148 {
149 public:
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
189 private:
190     QDBusError m_error;
191 };
192 # endif
193
194 QT_END_NAMESPACE
195
196 QT_END_HEADER
197
198 #endif // QT_NO_DBUS
199 #endif