1// Copyright (C) 2020 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// Qt-Security score:significant reason:default
4
5#ifndef QPROMISE_H
6#define QPROMISE_H
7
8#include <QtCore/qglobal.h>
9#include <QtCore/qfutureinterface.h>
10
11#include <utility>
12
13QT_REQUIRE_CONFIG(future);
14
15QT_BEGIN_NAMESPACE
16
17namespace QtPrivate {
18
19template<class T, class U>
20using EnableIfSameOrConvertible = std::enable_if_t<std::is_convertible_v<T, U>>;
21
22} // namespace QtPrivate
23
24template<typename T>
25class QPromise
26{
27 static_assert (std::is_move_constructible_v<T>
28 || std::is_same_v<T, void>,
29 "A move-constructible type or type void is required");
30public:
31 QPromise() = default;
32 Q_DISABLE_COPY(QPromise)
33 QPromise(QPromise<T> &&other) = default;
34 QPromise(const QFutureInterface<T> &other) : d(other) {}
35 QPromise(QFutureInterface<T> &&other) noexcept : d(std::move(other)) {}
36 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPromise)
37 ~QPromise()
38 {
39 // If computation is not finished at this point, cancel
40 // potential waits
41 if (d.d && !(d.loadState() & QFutureInterfaceBase::State::Finished)) {
42 d.cancelAndFinish(); // cancel and finalize the state
43 d.runContinuation();
44 }
45 d.cleanContinuation();
46 }
47
48 // Core QPromise APIs
49 QFuture<T> future() const { return d.future(); }
50 template<typename...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
51 bool emplaceResultAt(int index, Args&&...args)
52 {
53 return d.reportAndEmplaceResult(index, std::forward<Args>(args)...);
54 }
55 template<typename...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
56 bool emplaceResult(Args&&...args)
57 {
58 return d.reportAndEmplaceResult(-1, std::forward<Args>(args)...);
59 }
60 template<typename U = T, typename = QtPrivate::EnableIfSameOrConvertible<U, T>>
61 bool addResult(U &&result, int index = -1)
62 {
63 return d.reportAndEmplaceResult(index, std::forward<U>(result));
64 }
65 bool addResults(const QList<T> &result)
66 { return d.reportResults(result); }
67#ifndef QT_NO_EXCEPTIONS
68 void setException(const QException &e) { d.reportException(e); }
69#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
70 void setException(std::exception_ptr e) { d.reportException(e); }
71#else
72 void setException(const std::exception_ptr &e) { d.reportException(e); }
73#endif
74#endif
75 void start() { d.reportStarted(); }
76 void finish() { d.reportFinished(); }
77
78 void suspendIfRequested() { d.suspendIfRequested(); }
79
80 bool isCanceled() const { return d.isCanceled(); }
81
82 // Progress methods
83 void setProgressRange(int minimum, int maximum) { d.setProgressRange(minimum, maximum); }
84 void setProgressValue(int progressValue) { d.setProgressValue(progressValue); }
85 void setProgressValueAndText(int progressValue, const QString &progressText)
86 {
87 d.setProgressValueAndText(progressValue, progressText);
88 }
89
90 void swap(QPromise<T> &other) noexcept
91 {
92 d.swap(other.d);
93 }
94
95#if defined(Q_QDOC) // documentation-only simplified signatures
96 bool addResult(const T &result, int index = -1) { }
97 bool addResult(T &&result, int index = -1) { }
98#endif
99private:
100 mutable QFutureInterface<T> d;
101};
102
103template<typename T>
104inline void swap(QPromise<T> &a, QPromise<T> &b) noexcept
105{
106 a.swap(b);
107}
108
109QT_END_NAMESPACE
110
111#endif // QPROMISE_H
112

source code of qtbase/src/corelib/thread/qpromise.h