Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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// 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
14
15QT_BEGIN_NAMESPACE
16
17namespace QtPrivate {
18
19template<class T, class U>
21
22} // namespace QtPrivate
23
24template<typename T>
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;
33 QPromise(QPromise<T> &&other) = default;
34 QPromise(const QFutureInterface<T> &other) : d(other) {}
35 QPromise(QFutureInterface<T> &&other) noexcept : d(std::move(other)) {}
38 {
39 // If computation is not finished at this point, cancel
40 // potential waits
42 d.cancelAndFinish(); // cancel and finalize the state
44 }
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>
52 {
54 }
55 template<typename...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
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 {
64 }
65 bool addResults(const QList<T> &result)
66 { return d.reportResults(result); }
67#ifndef QT_NO_EXCEPTIONS
69#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
71#else
73#endif
74#endif
75 void start() { d.reportStarted(); }
76 void finish() { d.reportFinished(); }
77
79
80 bool isCanceled() const { return d.isCanceled(); }
81
82 // Progress methods
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
QPromise(const QFutureInterface< T > &other)
Definition qpromise.h:34
QPromise()=default
void swap(QPromise< T > &a, QPromise< T > &b) noexcept
Definition qpromise.h:104
QFuture< void > future
[5]