RhythmGame 1.4.3
Loading...
Searching...
No Matches
PendingReply.h
Go to the documentation of this file.
1#ifndef RHYTHMGAME_PENDINGREPLY_H
2#define RHYTHMGAME_PENDINGREPLY_H
3
4#include <QCoreApplication>
5#include <QJSValue>
6#include <QObject>
7#include <QPointer>
8#include <QThread>
9#include <QVariant>
10
11#include <functional>
12#include <memory>
13#include <stop_token>
14#include <type_traits>
15#include <utility>
16
17namespace support {
18
19class PendingReply;
20
21namespace detail {
22
24{
26 std::stop_source stopSource;
27 std::function<void()> cancellationHandler;
28};
29
30inline void
32{
33 const auto* application = QCoreApplication::instance();
34 Q_ASSERT(!application || QThread::currentThread() == application->thread());
35}
36
37} // namespace detail
38
39template<typename T>
40class PendingReplySource;
41
66class PendingReply final : public QObject
67{
68 Q_OBJECT
69 Q_PROPERTY(
70 bool resultAvailable READ isResultAvailable NOTIFY finished FINAL)
71 Q_PROPERTY(bool success READ isSuccessful NOTIFY finished FINAL)
72 Q_PROPERTY(QVariant value READ value NOTIFY finished FINAL)
73
74 public:
78 ~PendingReply() override;
79
85 [[nodiscard]] bool isResultAvailable() const;
86
92 [[nodiscard]] bool isSuccessful() const;
93
99 [[nodiscard]] QVariant value() const;
100
112 Q_INVOKABLE void then(const QJSValue& success,
113 const QJSValue& failed = QJSValue());
114
124 Q_INVOKABLE void cancel();
125
126 signals:
130 void finished();
131
132 private:
133 template<typename T>
134 friend class PendingReplySource;
135
136 explicit PendingReply(std::shared_ptr<detail::PendingReplyState> state,
137 QObject* parent);
138 bool settle(bool success, QVariant value, bool requestCancellation);
139 bool settleOwned(QVariant value, std::unique_ptr<QObject> object);
140 void retainOwnedResult() const;
141 QJSValue takeCallback();
142 void invokeCallback(QJSValue callback);
143 void releaseToQml();
144
145 std::shared_ptr<detail::PendingReplyState> state;
146 QVariant result;
147 QPointer<QObject> ownedResult;
148 mutable QJSValue resultAnchor;
149 QJSValue successCallback;
150 QJSValue failedCallback;
151 bool resultAvailable = {};
152 bool successful = {};
153};
154
173template<typename T>
175{
176 public:
185 explicit PendingReplySource(QObject* owner)
186 : state(std::make_shared<detail::PendingReplyState>())
187 {
189 state->reply = new PendingReply(state, owner);
190 }
191
196 [[nodiscard]] PendingReply* reply() const
197 {
199 return state->reply;
200 }
201
214 [[nodiscard]] bool succeed(const T& value) const
215 {
217 auto* pendingReply = state->reply;
218 if (!pendingReply)
219 return false;
220 if constexpr (std::is_same_v<std::remove_cv_t<T>, QVariant>)
221 return pendingReply->settle(true, value, false);
222 else
223 return pendingReply->settle(
224 true, QVariant::fromValue(value), false);
225 }
226
231 template<typename Object>
232 requires std::is_base_of_v<QObject, Object> &&
233 (std::is_same_v<T, QVariant> ||
234 std::is_convertible_v<Object*, T>)
235 [[nodiscard]] bool succeed(std::unique_ptr<Object> object) const
236 {
238 auto* pendingReply = state->reply;
239 if (!pendingReply)
240 return false;
241 auto value = QVariant::fromValue(object.get());
242 return pendingReply->settleOwned(std::move(value), std::move(object));
243 }
244
252 [[nodiscard]] bool fail() const
253 {
255 auto* pendingReply = state->reply;
256 return pendingReply && pendingReply->settle(false, QVariant{}, false);
257 }
258
266 [[nodiscard]] std::stop_token stopToken() const
267 {
268 return state->stopSource.get_token();
269 }
270
282 void setCancellationHandler(std::function<void()> handler) const
283 {
285 if (state->stopSource.stop_requested()) {
286 if (handler)
287 handler();
288 return;
289 }
290 if (!state->reply || state->reply->isResultAvailable())
291 return;
292 state->cancellationHandler = std::move(handler);
293 }
294
295 private:
296 std::shared_ptr<detail::PendingReplyState> state;
297};
298
299} // namespace support
300
301#endif // RHYTHMGAME_PENDINGREPLY_H
double value
Definition BmsNotesData.cpp:35
Producer-side control for a PendingReply.
Definition PendingReply.h:175
std::stop_token stopToken() const
Returns the operation's cooperative stop token.
Definition PendingReply.h:266
void setCancellationHandler(std::function< void()> handler) const
Sets the cancellation handler for the producer's current stage.
Definition PendingReply.h:282
bool succeed(const T &value) const
Attempts to settle the reply successfully with value.
Definition PendingReply.h:214
PendingReplySource(QObject *owner)
Creates a pending reply, optionally parented until settlement.
Definition PendingReply.h:185
bool fail() const
Attempts to settle the reply as a failure without requesting cancellation.
Definition PendingReply.h:252
bool succeed(std::unique_ptr< Object > object) const
Definition PendingReply.h:235
PendingReply * reply() const
Definition PendingReply.h:196
QML-facing handle for one asynchronous result.
Definition PendingReply.h:67
Q_INVOKABLE void then(const QJSValue &success, const QJSValue &failed=QJSValue())
Registers callbacks for the terminal result.
Definition PendingReply.cpp:86
bool isResultAvailable() const
Definition PendingReply.cpp:35
bool resultAvailable
Definition PendingReply.h:70
bool success
Definition PendingReply.h:71
bool isSuccessful() const
Definition PendingReply.cpp:41
Q_INVOKABLE void cancel()
Settles the reply as a cancelled terminal failure.
Definition PendingReply.cpp:105
QVariant value
Definition PendingReply.h:72
void finished()
Reports that a terminal result is available.
void assertApplicationThread()
Definition PendingReply.h:31
Contains various helper classes and functions.
Definition Capitalize.h:9
constexpr auto get(T &&t) -> auto &
Returns the nth element of a type that supports std::get.
Definition get.h:21
Definition PendingReply.h:24
std::stop_source stopSource
Definition PendingReply.h:26
PendingReply * reply
Definition PendingReply.h:25
std::function< void()> cancellationHandler
Definition PendingReply.h:27