/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QFUTURE_H #define QFUTURE_H #include #include #include #include #include #include QT_REQUIRE_CONFIG(future); QT_BEGIN_NAMESPACE template class QFutureWatcher; template class QFuture { static_assert (std::is_move_constructible_v || std::is_same_v, "A move-constructible type or type void is required"); public: QFuture() : d(QFutureInterface::canceledResult()) { } template> explicit QFuture(QFutureInterface *p) // internal : d(*p) { } template> explicit QFuture(QFutureInterfaceBase *p) // internal : d(*p) { } template> explicit QFuture(const QFuture &other) : d(other.d) { } template> QFuture &operator=(const QFuture &other) { d = other.d; return *this; } #if defined(Q_CLANG_QDOC) ~QFuture() { } QFuture(const QFuture &) { } QFuture & operator=(const QFuture &) { } #endif void cancel() { d.cancel(); } bool isCanceled() const { return d.isCanceled(); } #if QT_DEPRECATED_SINCE(6, 0) QT_DEPRECATED_VERSION_X_6_0("Use setSuspended() instead.") void setPaused(bool paused) { d.setSuspended(paused); } QT_DEPRECATED_VERSION_X_6_0("Use isSuspending() or isSuspended() instead.") bool isPaused() const { QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED return d.isPaused(); QT_WARNING_POP } QT_DEPRECATED_VERSION_X_6_0("Use toggleSuspended() instead.") void togglePaused() { d.toggleSuspended(); } QT_DEPRECATED_VERSION_X_6_0("Use suspend() instead.") void pause() { suspend(); } #endif bool isSuspending() const { return d.isSuspending(); } bool isSuspended() const { return d.isSuspended(); } void setSuspended(bool suspend) { d.setSuspended(suspend); } void suspend() { setSuspended(true); } void resume() { setSuspended(false); } void toggleSuspended() { d.toggleSuspended(); } bool isStarted() const { return d.isStarted(); } bool isFinished() const { return d.isFinished(); } bool isRunning() const { return d.isRunning(); } int resultCount() const { return d.resultCount(); } int progressValue() const { return d.progressValue(); } int progressMinimum() const { return d.progressMinimum(); } int progressMaximum() const { return d.progressMaximum(); } QString progressText() const { return d.progressText(); } void waitForFinished() { d.waitForFinished(); } template> inline T result() const; template> inline T resultAt(int index) const; template> bool isResultReadyAt(int resultIndex) const { return d.isResultReadyAt(resultIndex); } template> QList results() const { return d.results(); } template> T takeResult() { return d.takeResult(); } #if 0 // TODO: Enable and make it return a QList, when QList is fixed to support move-only types template> std::vector takeResults() { return d.takeResults(); } #endif bool isValid() const { return d.isValid(); } template using ResultType = typename QtPrivate::ResultTypeHelper::ResultType; template QFuture> then(Function &&function); template QFuture> then(QtFuture::Launch policy, Function &&function); template QFuture> then(QThreadPool *pool, Function &&function); template QFuture> then(QObject *context, Function &&function); #ifndef QT_NO_EXCEPTIONS template::HasExtraArgs>> QFuture onFailed(Function &&handler); template::HasExtraArgs>> QFuture onFailed(QObject *context, Function &&handler); #endif template>> QFuture onCanceled(Function &&handler); template>> QFuture onCanceled(QObject *context, Function &&handler); class const_iterator { public: static_assert(!std::is_same_v, "It isn't possible to define QFuture::const_iterator"); typedef std::bidirectional_iterator_tag iterator_category; typedef qptrdiff difference_type; typedef T value_type; typedef const T *pointer; typedef const T &reference; inline const_iterator() {} inline const_iterator(QFuture const * const _future, int _index) : future(_future), index(advanceIndex(_index, 0)) { } inline const_iterator(const const_iterator &o) : future(o.future), index(o.index) {} inline const_iterator &operator=(const const_iterator &o) { future = o.future; index = o.index; return *this; } inline const T &operator*() const { return future->d.resultReference(index); } inline const T *operator->() const { return future->d.resultPointer(index); } inline bool operator!=(const const_iterator &other) const { return index != other.index; } inline bool operator==(const const_iterator &o) const { return !operator!=(o); } inline const_iterator &operator++() { index = advanceIndex(index, 1); return *this; } inline const_iterator &operator--() { index = advanceIndex(index, -1); return *this; } inline const_iterator operator++(int) { const_iterator r = *this; index = advanceIndex(index, 1); return r; } inline const_iterator operator--(int) { const_iterator r = *this; index = advanceIndex(index, -1); return r; } inline const_iterator operator+(int j) const { return const_iterator(future, advanceIndex(index, j)); } inline const_iterator operator-(int j) const { return const_iterator(future, advanceIndex(index, -j)); } inline const_iterator &operator+=(int j) { index = advanceIndex(index, j); return *this; } inline const_iterator &operator-=(int j) { index = advanceIndex(index, -j); return *this; } friend inline const_iterator operator+(int j, const_iterator k) { return const_iterator(k.future, k.advanceIndex(k.index, j)); } private: /*! \internal Advances the iterator index \a idx \a n steps, waits for the result at the target index, and returns the target index. The index may be -1, indicating the end iterator, either as the argument or as the return value. The end iterator may be decremented. The caller is responsible for not advancing the iterator before begin() or past end(), with the exception that attempting to advance a non-end iterator past end() for a running future is allowed and will return the end iterator. Note that n == 0 is valid and will wait for the result at the given index. */ int advanceIndex(int idx, int n) const { // The end iterator can be decremented, leave as-is for other cases if (idx == -1 && n >= 0) return idx; // Special case for decrementing the end iterator: wait for // finished to get the total result count. if (idx == -1 && future->isRunning()) future->d.waitForFinished(); // Wait for result at target index const int targetIndex = (idx == -1) ? future->resultCount() + n : idx + n; future->d.waitForResult(targetIndex); // After waiting there is either a result or the end was reached return (targetIndex < future->resultCount()) ? targetIndex : -1; } QFuture const * future; int index; }; friend class const_iterator; typedef const_iterator ConstIterator; template> const_iterator begin() const { return const_iterator(this, 0); } template> const_iterator constBegin() const { return const_iterator(this, 0); } template> const_iterator end() const { return const_iterator(this, -1); } template> const_iterator constEnd() const { return const_iterator(this, -1); } private: friend class QFutureWatcher; template friend class QFuture; friend class QFutureInterfaceBase; template friend class QtPrivate::Continuation; template friend class QtPrivate::CanceledHandler; #ifndef QT_NO_EXCEPTIONS template friend class QtPrivate::FailureHandler; #endif template friend struct QtPrivate::WhenAnyContext; using QFuturePrivate = std::conditional_t, QFutureInterfaceBase, QFutureInterface>; #ifdef QFUTURE_TEST public: #endif mutable QFuturePrivate d; }; template template inline T QFuture::result() const { d.waitForResult(0); return d.resultReference(0); } template template inline T QFuture::resultAt(int index) const { d.waitForResult(index); return d.resultReference(index); } template inline QFuture QFutureInterface::future() { return QFuture(this); } template template QFuture::template ResultType> QFuture::then(Function &&function) { return then(QtFuture::Launch::Sync, std::forward(function)); } template template QFuture::template ResultType> QFuture::then(QtFuture::Launch policy, Function &&function) { QFutureInterface> promise(QFutureInterfaceBase::State::Pending); QtPrivate::Continuation, ResultType, T>::create( std::forward(function), this, promise, policy); return promise.future(); } template template QFuture::template ResultType> QFuture::then(QThreadPool *pool, Function &&function) { QFutureInterface> promise(QFutureInterfaceBase::State::Pending); QtPrivate::Continuation, ResultType, T>::create( std::forward(function), this, promise, pool); return promise.future(); } template template QFuture::template ResultType> QFuture::then(QObject *context, Function &&function) { QFutureInterface> promise(QFutureInterfaceBase::State::Pending); QtPrivate::Continuation, ResultType, T>::create( std::forward(function), this, promise, context); return promise.future(); } #ifndef QT_NO_EXCEPTIONS template template QFuture QFuture::onFailed(Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); QtPrivate::FailureHandler, T>::create(std::forward(handler), this, promise); return promise.future(); } template template QFuture QFuture::onFailed(QObject *context, Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); QtPrivate::FailureHandler, T>::create(std::forward(handler), this, promise, context); return promise.future(); } #endif template template QFuture QFuture::onCanceled(Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); QtPrivate::CanceledHandler, T>::create(std::forward(handler), this, promise); return promise.future(); } template template QFuture QFuture::onCanceled(QObject *context, Function &&handler) { QFutureInterface promise(QFutureInterfaceBase::State::Pending); QtPrivate::CanceledHandler, T>::create(std::forward(handler), this, promise, context); return promise.future(); } inline QFuture QFutureInterface::future() { return QFuture(this); } template QFutureInterfaceBase QFutureInterfaceBase::get(const QFuture &future) { return future.d; } namespace QtPrivate { template struct MetaTypeQFutureHelper> { static bool registerConverter() { if constexpr (std::is_same_v) return false; return QMetaType::registerConverter, QFuture>( [](const QFuture &future) { return QFuture(future); }); } }; } // namespace QtPrivate namespace QtFuture { #ifndef Q_CLANG_QDOC template::value_type, std::enable_if_t, QtPrivate::IsRandomAccessible, QtPrivate::isQFuture>, int> = 0> QFuture whenAll(InputIt first, InputIt last) { return QtPrivate::whenAllImpl(first, last); } template::value_type, std::enable_if_t, QtPrivate::isQFuture>, int> = 0> QFuture> whenAll(InputIt first, InputIt last) { return QtPrivate::whenAllImpl, InputIt, ValueType>(first, last); } template, QtPrivate::NotEmpty, QtPrivate::isQFuture>...>, int> = 0> QFuture whenAll(Futures &&... futures) { return QtPrivate::whenAllImpl(std::forward(futures)...); } template, QtPrivate::isQFuture>...>, int> = 0> QFuture...>>> whenAll(Futures &&... futures) { return QtPrivate::whenAllImpl...>>, Futures...>( std::forward(futures)...); } template::value_type, std::enable_if_t, QtPrivate::isQFuture>, int> = 0> QFuture::type>> whenAny(InputIt first, InputIt last) { return QtPrivate::whenAnyImpl(first, last); } template, QtPrivate::isQFuture>...>, int> = 0> QFuture...>> whenAny(Futures &&... futures) { return QtPrivate::whenAnyImpl(std::forward(futures)...); } #else template QFuture whenAll(InputIt first, InputIt last); template QFuture whenAll(Futures &&... futures); template QFuture> whenAny(InputIt first, InputIt last); template QFuture...>> whenAny(Futures &&... futures); #endif // Q_CLANG_QDOC } // namespace QtFuture Q_DECLARE_SEQUENTIAL_ITERATOR(Future) QT_END_NAMESPACE Q_DECLARE_METATYPE_TEMPLATE_1ARG(QFuture) #endif // QFUTURE_H