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