QtBase  v6.3.1
qpromise.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QPROMISE_H
41 #define QPROMISE_H
42 
43 #include <QtCore/qglobal.h>
44 #include <QtCore/qfutureinterface.h>
45 
46 #include <utility>
47 
49 
51 
52 namespace QtPrivate {
53 
54 template<class T, class U>
55 using EnableIfSameOrConvertible = std::enable_if_t<std::is_convertible_v<T, U>>;
56 
57 } // namespace QtPrivate
58 
59 template<typename T>
60 class QPromise
61 {
62  static_assert (std::is_move_constructible_v<T>
63  || std::is_same_v<T, void>,
64  "A move-constructible type or type void is required");
65 public:
66  QPromise() = default;
70  QPromise(QFutureInterface<T> &&other) noexcept : d(std::move(other)) {}
73  {
74  // If computation is not finished at this point, cancel
75  // potential waits
76  if (d.d && !(d.loadState() & QFutureInterfaceBase::State::Finished)) {
77  d.cancelAndFinish(); // cancel and finalize the state
78  d.cleanContinuation();
79  }
80  }
81 
82  // Core QPromise APIs
83  QFuture<T> future() const { return d.future(); }
84  template<typename U, typename = QtPrivate::EnableIfSameOrConvertible<U, T>>
85  bool addResult(U &&result, int index = -1)
86  {
87  return d.reportResult(std::forward<U>(result), index);
88  }
89 #ifndef QT_NO_EXCEPTIONS
90  void setException(const QException &e) { d.reportException(e); }
91 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
92  void setException(std::exception_ptr e) { d.reportException(e); }
93 #else
94  void setException(const std::exception_ptr &e) { d.reportException(e); }
95 #endif
96 #endif
97  void start() { d.reportStarted(); }
98  void finish() { d.reportFinished(); }
99 
100  void suspendIfRequested() { d.suspendIfRequested(); }
101 
102  bool isCanceled() const { return d.isCanceled(); }
103 
104  // Progress methods
105  void setProgressRange(int minimum, int maximum) { d.setProgressRange(minimum, maximum); }
106  void setProgressValue(int progressValue) { d.setProgressValue(progressValue); }
107  void setProgressValueAndText(int progressValue, const QString &progressText)
108  {
109  d.setProgressValueAndText(progressValue, progressText);
110  }
111 
112  void swap(QPromise<T> &other) noexcept
113  {
114  d.swap(other.d);
115  }
116 
117 #if defined(Q_CLANG_QDOC) // documentation-only simplified signatures
118  bool addResult(const T &result, int index = -1) { }
119  bool addResult(T &&result, int index = -1) { }
120 #endif
121 private:
122  mutable QFutureInterface<T> d;
123 };
124 
125 template<typename T>
126 inline void swap(QPromise<T> &a, QPromise<T> &b) noexcept
127 {
128  a.swap(b);
129 }
130 
132 
133 #endif // QPROMISE_H
The QException class provides a base class for exceptions that can be transferred across threads.
Definition: qexception.h:58
QPromise(QFutureInterface< T > &&other) noexcept
Definition: qpromise.h:70
bool isCanceled() const
Definition: qpromise.h:102
~QPromise()
Definition: qpromise.h:72
void setProgressValue(int progressValue)
Definition: qpromise.h:106
QFuture< T > future() const
Definition: qpromise.h:83
void setException(const QException &e)
Definition: qpromise.h:90
void setProgressRange(int minimum, int maximum)
Definition: qpromise.h:105
QPromise()=default
void finish()
Definition: qpromise.h:98
bool addResult(U &&result, int index=-1)
Definition: qpromise.h:85
void setProgressValueAndText(int progressValue, const QString &progressText)
Definition: qpromise.h:107
void suspendIfRequested()
Definition: qpromise.h:100
void setException(const std::exception_ptr &e)
Definition: qpromise.h:94
void swap(QPromise< T > &other) noexcept
Definition: qpromise.h:112
void start()
Definition: qpromise.h:97
The QString class provides a Unicode character string.
Definition: qstring.h:388
double e
auto it unsigned count const
Definition: hb-iter.hh:848
std::enable_if_t< std::is_convertible_v< T, U > > EnableIfSameOrConvertible
Definition: qpromise.h:55
default
Definition: devices.py:76
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Class)
Definition: qglobal.h:556
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
QT_REQUIRE_CONFIG(future)
void swap(QPromise< T > &a, QPromise< T > &b) noexcept
Definition: qpromise.h:126
QFuture< void > future
[5]
QSharedPointer< T > other(t)
[5]
Definition: main.cpp:38