1#ifndef RHYTHMGAME_PENDINGREPLY_H
2#define RHYTHMGAME_PENDINGREPLY_H
4#include <QCoreApplication>
33 const auto* application = QCoreApplication::instance();
34 Q_ASSERT(!application || QThread::currentThread() == application->thread());
40class PendingReplySource;
99 [[nodiscard]] QVariant
value() const;
113 const QJSValue& failed = QJSValue());
124 Q_INVOKABLE
void cancel();
136 explicit
PendingReply(std::shared_ptr<detail::PendingReplyState> state,
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);
145 std::shared_ptr<detail::PendingReplyState> state;
147 QPointer<QObject> ownedResult;
148 mutable QJSValue resultAnchor;
149 QJSValue successCallback;
150 QJSValue failedCallback;
152 bool successful = {};
186 : state(std::make_shared<detail::PendingReplyState>())
217 auto* pendingReply = state->reply;
220 if constexpr (std::is_same_v<std::remove_cv_t<T>, QVariant>)
221 return pendingReply->settle(
true,
value,
false);
223 return pendingReply->settle(
224 true, QVariant::fromValue(
value),
false);
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
238 auto* pendingReply = state->reply;
241 auto value = QVariant::fromValue(
object.
get());
242 return pendingReply->settleOwned(std::move(
value), std::move(
object));
252 [[nodiscard]]
bool fail()
const
255 auto* pendingReply = state->reply;
256 return pendingReply && pendingReply->settle(
false, QVariant{},
false);
268 return state->stopSource.get_token();
285 if (state->stopSource.stop_requested()) {
290 if (!state->reply || state->reply->isResultAvailable())
292 state->cancellationHandler = std::move(handler);
296 std::shared_ptr<detail::PendingReplyState> state;
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