Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qpromise.h
Go to the documentation of this file.
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
13
15
16namespace QtPrivate {
17
18template<class T, class U>
19using EnableIfSameOrConvertible = std::enable_if_t<std::is_convertible_v<T, U>>;
20
21} // namespace QtPrivate
22
23template<typename T>
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");
29public:
30 QPromise() = default;
31 Q_DISABLE_COPY(QPromise)
32 QPromise(QPromise<T> &&other) = default;
34 QPromise(QFutureInterface<T> &&other) noexcept : d(std::move(other)) {}
35 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(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
98private:
99 mutable QFutureInterface<T> d;
100};
101
102template<typename T>
103inline void swap(QPromise<T> &a, QPromise<T> &b) noexcept
104{
105 a.swap(b);
106}
107
109
110#endif // QPROMISE_H
\inmodule QtCore
Definition qexception.h:22
bool emplaceResultAt(int index, Args &&...args)
Definition qpromise.h:50
QPromise(QFutureInterface< T > &&other) noexcept
Definition qpromise.h:34
bool isCanceled() const
Definition qpromise.h:79
~QPromise()
Definition qpromise.h:36
void setProgressValue(int progressValue)
Definition qpromise.h:83
QFuture< T > future() const
Definition qpromise.h:48
void setException(const QException &e)
Definition qpromise.h:67
bool addResults(const QList< T > &result)
Definition qpromise.h:64
void setProgressRange(int minimum, int maximum)
Definition qpromise.h:82
QPromise()=default
void finish()
Definition qpromise.h:75
bool addResult(U &&result, int index=-1)
Definition qpromise.h:60
void setProgressValueAndText(int progressValue, const QString &progressText)
Definition qpromise.h:84
bool emplaceResult(Args &&...args)
Definition qpromise.h:55
void suspendIfRequested()
Definition qpromise.h:77
void setException(const std::exception_ptr &e)
Definition qpromise.h:71
void swap(QPromise< T > &other) noexcept
Definition qpromise.h:89
void start()
Definition qpromise.h:74
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
std::enable_if_t< std::is_convertible_v< T, U > > EnableIfSameOrConvertible
Definition qpromise.h:19
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLuint64EXT * result
[6]
void swap(QPromise< T > &a, QPromise< T > &b) noexcept
Definition qpromise.h:103
#define QT_REQUIRE_CONFIG(feature)
QFuture< void > future
[5]
QSharedPointer< T > other(t)
[5]
QJSValueList args