1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QDBUSREPLY_H |
5 | #define QDBUSREPLY_H |
6 | |
7 | #include <QtDBus/qtdbusglobal.h> |
8 | #include <QtCore/qvariant.h> |
9 | |
10 | #include <QtDBus/qdbusmessage.h> |
11 | #include <QtDBus/qdbuserror.h> |
12 | #include <QtDBus/qdbusextratypes.h> |
13 | #include <QtDBus/qdbuspendingreply.h> |
14 | |
15 | #ifndef QT_NO_DBUS |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | |
20 | Q_DBUS_EXPORT void qDBusReplyFill(const QDBusMessage &reply, QDBusError &error, QVariant &data); |
21 | |
22 | template<typename T> |
23 | class QDBusReply |
24 | { |
25 | typedef T Type; |
26 | public: |
27 | inline QDBusReply(const QDBusMessage &reply) |
28 | { |
29 | *this = reply; |
30 | } |
31 | inline QDBusReply(const QDBusReply &) = default; |
32 | inline QDBusReply& operator=(const QDBusMessage &reply) |
33 | { |
34 | QVariant data(QMetaType::fromType<Type>()); |
35 | qDBusReplyFill(reply, error&: m_error, data); |
36 | m_data = qvariant_cast<Type>(data); |
37 | return *this; |
38 | } |
39 | |
40 | inline QDBusReply(const QDBusPendingCall &pcall) |
41 | { |
42 | *this = pcall; |
43 | } |
44 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) |
45 | { |
46 | QDBusPendingCall other(pcall); |
47 | other.waitForFinished(); |
48 | return *this = other.reply(); |
49 | } |
50 | inline QDBusReply(const QDBusPendingReply<T> &reply) |
51 | { |
52 | *this = static_cast<QDBusPendingCall>(reply); |
53 | } |
54 | |
55 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) |
56 | : m_error(dbusError), m_data(Type()) |
57 | { |
58 | } |
59 | inline QDBusReply& operator=(const QDBusError& dbusError) |
60 | { |
61 | m_error = dbusError; |
62 | m_data = Type(); |
63 | return *this; |
64 | } |
65 | |
66 | inline QDBusReply& operator=(const QDBusReply& other) |
67 | { |
68 | m_error = other.m_error; |
69 | m_data = other.m_data; |
70 | return *this; |
71 | } |
72 | |
73 | inline bool isValid() const { return !m_error.isValid(); } |
74 | |
75 | inline const QDBusError& error() { return m_error; } |
76 | inline const QDBusError& error() const { return m_error; } |
77 | |
78 | inline Type value() const |
79 | { |
80 | return m_data; |
81 | } |
82 | |
83 | inline operator Type () const |
84 | { |
85 | return m_data; |
86 | } |
87 | |
88 | private: |
89 | QDBusError m_error; |
90 | Type m_data; |
91 | }; |
92 | |
93 | # ifndef Q_QDOC |
94 | // specialize for QVariant: |
95 | template<> inline QDBusReply<QVariant>& |
96 | QDBusReply<QVariant>::operator=(const QDBusMessage &reply) |
97 | { |
98 | QVariant data(QMetaType::fromType<QDBusVariant>()); |
99 | qDBusReplyFill(reply, error&: m_error, data); |
100 | m_data = qvariant_cast<QDBusVariant>(v: data).variant(); |
101 | return *this; |
102 | } |
103 | |
104 | // specialize for void: |
105 | template<> |
106 | class QDBusReply<void> |
107 | { |
108 | public: |
109 | inline QDBusReply(const QDBusMessage &reply) |
110 | : m_error(reply) |
111 | { |
112 | } |
113 | inline QDBusReply& operator=(const QDBusMessage &reply) |
114 | { |
115 | m_error = QDBusError(reply); |
116 | return *this; |
117 | } |
118 | inline QDBusReply(const QDBusError &dbusError = QDBusError()) |
119 | : m_error(dbusError) |
120 | { |
121 | } |
122 | inline QDBusReply(const QDBusPendingCall &pcall) |
123 | { |
124 | *this = pcall; |
125 | } |
126 | inline QDBusReply &operator=(const QDBusPendingCall &pcall) |
127 | { |
128 | QDBusPendingCall other(pcall); |
129 | other.waitForFinished(); |
130 | return *this = other.reply(); |
131 | } |
132 | inline QDBusReply& operator=(const QDBusError& dbusError) |
133 | { |
134 | m_error = dbusError; |
135 | return *this; |
136 | } |
137 | |
138 | inline QDBusReply(const QDBusReply &) = default; |
139 | |
140 | inline QDBusReply& operator=(const QDBusReply& other) |
141 | { |
142 | m_error = other.m_error; |
143 | return *this; |
144 | } |
145 | |
146 | inline bool isValid() const { return !m_error.isValid(); } |
147 | |
148 | inline const QDBusError& error() { return m_error; } |
149 | inline const QDBusError& error() const { return m_error; } |
150 | |
151 | private: |
152 | QDBusError m_error; |
153 | }; |
154 | # endif |
155 | |
156 | QT_END_NAMESPACE |
157 | |
158 | #endif // QT_NO_DBUS |
159 | #endif |
160 |