1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Ford Motor Company |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtRemoteObjects 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 QREMOTEOBJECTPENDINGCALL_H |
41 | #define QREMOTEOBJECTPENDINGCALL_H |
42 | |
43 | #include <QtRemoteObjects/qtremoteobjectglobal.h> |
44 | |
45 | #include <QtCore/qvariant.h> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | class QRemoteObjectPendingCallWatcherPrivate; |
50 | class QRemoteObjectPendingCallData; |
51 | |
52 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectPendingCall |
53 | { |
54 | public: |
55 | enum Error { |
56 | NoError, |
57 | InvalidMessage |
58 | }; |
59 | |
60 | QRemoteObjectPendingCall(); |
61 | QRemoteObjectPendingCall(const QRemoteObjectPendingCall &other); |
62 | ~QRemoteObjectPendingCall(); |
63 | |
64 | QRemoteObjectPendingCall &operator=(const QRemoteObjectPendingCall &other); |
65 | |
66 | QVariant returnValue() const; |
67 | QRemoteObjectPendingCall::Error error() const; |
68 | |
69 | bool isFinished() const; |
70 | |
71 | bool waitForFinished(int timeout = 30000); |
72 | |
73 | static QRemoteObjectPendingCall fromCompletedCall(const QVariant &returnValue); |
74 | |
75 | protected: |
76 | QRemoteObjectPendingCall(QRemoteObjectPendingCallData *dd); |
77 | |
78 | /// Shared data, note: might be null |
79 | QExplicitlySharedDataPointer<QRemoteObjectPendingCallData> d; |
80 | |
81 | private: |
82 | friend class QConnectedReplicaImplementation; |
83 | }; |
84 | |
85 | QT_END_NAMESPACE |
86 | Q_DECLARE_METATYPE(QRemoteObjectPendingCall) |
87 | QT_BEGIN_NAMESPACE |
88 | |
89 | class Q_REMOTEOBJECTS_EXPORT QRemoteObjectPendingCallWatcher: public QObject, public QRemoteObjectPendingCall |
90 | { |
91 | Q_OBJECT |
92 | |
93 | public: |
94 | QRemoteObjectPendingCallWatcher(const QRemoteObjectPendingCall &call, QObject *parent = nullptr); |
95 | ~QRemoteObjectPendingCallWatcher() override; |
96 | |
97 | bool isFinished() const; |
98 | |
99 | void waitForFinished(); |
100 | |
101 | Q_SIGNALS: |
102 | void finished(QRemoteObjectPendingCallWatcher *self); |
103 | |
104 | private: |
105 | Q_DECLARE_PRIVATE(QRemoteObjectPendingCallWatcher) |
106 | }; |
107 | |
108 | template<typename T> |
109 | class QRemoteObjectPendingReply : public QRemoteObjectPendingCall |
110 | { |
111 | public: |
112 | typedef T Type; |
113 | |
114 | QRemoteObjectPendingReply() = default; |
115 | explicit QRemoteObjectPendingReply(const QRemoteObjectPendingCall &call) |
116 | : QRemoteObjectPendingCall(call) |
117 | { |
118 | } |
119 | |
120 | QRemoteObjectPendingReply &operator=(const QRemoteObjectPendingCall &other) |
121 | { |
122 | QRemoteObjectPendingCall::operator=(other); |
123 | return *this; |
124 | } |
125 | |
126 | Type returnValue() const |
127 | { |
128 | return qvariant_cast<Type>(QRemoteObjectPendingCall::returnValue()); |
129 | } |
130 | |
131 | }; |
132 | |
133 | // NOTE: manual expansion of Q_DECLARE_METATYPE_TEMPLATE_1ARG, minus the IsSequentialContainer |
134 | template <typename T> |
135 | struct QMetaTypeId< QRemoteObjectPendingReply<T> > |
136 | { |
137 | enum { |
138 | Defined = QMetaTypeId2<T>::Defined |
139 | }; |
140 | static int qt_metatype_id() |
141 | { |
142 | static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); |
143 | if (const int id = metatype_id.loadRelaxed()) |
144 | return id; |
145 | const char *tName = QMetaType::typeName(type: qMetaTypeId<T>()); |
146 | Q_ASSERT(tName); |
147 | const int tNameLen = int(qstrlen(str: tName)); |
148 | QByteArray typeName; |
149 | typeName.reserve(asize: int(sizeof("QRemoteObjectPendingReply" )) + 1 + tNameLen + 1 + 1); |
150 | typeName.append(s: "QRemoteObjectPendingReply" , len: int(sizeof("QRemoteObjectPendingReply" )) - 1) |
151 | .append(c: '<').append(s: tName, len: tNameLen); |
152 | if (typeName.endsWith(c: '>')) |
153 | typeName.append(c: ' '); |
154 | typeName.append(c: '>'); |
155 | const int newId = qRegisterNormalizedMetaType< QRemoteObjectPendingReply<T> >( |
156 | typeName, |
157 | reinterpret_cast< QRemoteObjectPendingReply<T> *>(quintptr(-1))); |
158 | metatype_id.storeRelease(newValue: newId); |
159 | return newId; |
160 | } |
161 | }; |
162 | |
163 | QT_END_NAMESPACE |
164 | |
165 | #endif |
166 | |