summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2020-10-12 11:30:51 +0200
committerAndrei Golubev <andrei.golubev@qt.io>2020-10-16 09:48:10 +0200
commit3ca600bd2d02a6950938789cef96229b15ec0cfa (patch)
tree1a6b5801efae7294582b12e10b4c2a7f67796d05 /src
parent696d94b132b2f352b5e6b889ad91c2437417fae8 (diff)
Make QPromise::addResult() return boolean status of operation
Changed QPromise::addResult() to return bool value. True is returned when result is added and false is returned when e.g. promise is in final state (canceled or finished) or when addResult() is called twice with the same index as argument (in which case new value is rejected) Updated QFutureInterface::reportFinished() that accepts optional result as argument to align with other result adding methods. This function is "internal" only (as of now), so no documentation update is needed Change-Id: I2d63069246e5e5c8cf04529c22bb296faaaae53d Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/thread/qfutureinterface.h82
-rw-r--r--src/corelib/thread/qpromise.h8
-rw-r--r--src/corelib/thread/qpromise.qdoc15
3 files changed, 56 insertions, 49 deletions
diff --git a/src/corelib/thread/qfutureinterface.h b/src/corelib/thread/qfutureinterface.h
index 9296c63f0b..8467047809 100644
--- a/src/corelib/thread/qfutureinterface.h
+++ b/src/corelib/thread/qfutureinterface.h
@@ -235,12 +235,12 @@ public:
inline QFuture<T> future(); // implemented in qfuture.h
- inline void reportResult(const T *result, int index = -1);
- inline void reportAndMoveResult(T &&result, int index = -1);
- inline void reportResult(T &&result, int index = -1);
- inline void reportResult(const T &result, int index = -1);
- inline void reportResults(const QList<T> &results, int beginIndex = -1, int count = -1);
- inline void reportFinished(const T *result);
+ inline bool reportResult(const T *result, int index = -1);
+ inline bool reportAndMoveResult(T &&result, int index = -1);
+ inline bool reportResult(T &&result, int index = -1);
+ inline bool reportResult(const T &result, int index = -1);
+ inline bool reportResults(const QList<T> &results, int beginIndex = -1, int count = -1);
+ inline bool reportFinished(const T *result);
void reportFinished()
{
QFutureInterfaceBase::reportFinished();
@@ -259,32 +259,32 @@ public:
};
template <typename T>
-inline void QFutureInterface<T>::reportResult(const T *result, int index)
+inline bool QFutureInterface<T>::reportResult(const T *result, int index)
{
std::lock_guard<QMutex> locker{mutex()};
- if (this->queryState(Canceled) || this->queryState(Finished)) {
- return;
- }
+ if (this->queryState(Canceled) || this->queryState(Finished))
+ return false;
QtPrivate::ResultStoreBase &store = resultStoreBase();
+ const int resultCountBefore = store.count();
+ const int insertIndex = store.addResult<T>(index, result);
+ if (insertIndex == -1)
+ return false;
if (store.filterMode()) {
- const int resultCountBefore = store.count();
- if (store.addResult<T>(index, result) != -1)
- this->reportResultsReady(resultCountBefore, store.count());
+ this->reportResultsReady(resultCountBefore, store.count());
} else {
- const int insertIndex = store.addResult<T>(index, result);
- if (insertIndex != -1)
- this->reportResultsReady(insertIndex, insertIndex + 1);
+ this->reportResultsReady(insertIndex, insertIndex + 1);
}
+ return true;
}
template<typename T>
-void QFutureInterface<T>::reportAndMoveResult(T &&result, int index)
+bool QFutureInterface<T>::reportAndMoveResult(T &&result, int index)
{
std::lock_guard<QMutex> locker{mutex()};
if (queryState(Canceled) || queryState(Finished))
- return;
+ return false;
QtPrivate::ResultStoreBase &store = resultStoreBase();
@@ -293,47 +293,50 @@ void QFutureInterface<T>::reportAndMoveResult(T &&result, int index)
// Let's make sure it's not in pending results.
if (insertIndex != -1 && (!store.filterMode() || oldResultCount < store.count()))
reportResultsReady(insertIndex, store.count());
+ return insertIndex != -1;
}
template<typename T>
-void QFutureInterface<T>::reportResult(T &&result, int index)
+bool QFutureInterface<T>::reportResult(T &&result, int index)
{
- reportAndMoveResult(std::move(result), index);
+ return reportAndMoveResult(std::move(result), index);
}
template <typename T>
-inline void QFutureInterface<T>::reportResult(const T &result, int index)
+inline bool QFutureInterface<T>::reportResult(const T &result, int index)
{
- reportResult(&result, index);
+ return reportResult(&result, index);
}
template<typename T>
-inline void QFutureInterface<T>::reportResults(const QList<T> &_results, int beginIndex, int count)
+inline bool QFutureInterface<T>::reportResults(const QList<T> &_results, int beginIndex, int count)
{
std::lock_guard<QMutex> locker{mutex()};
- if (this->queryState(Canceled) || this->queryState(Finished)) {
- return;
- }
+ if (this->queryState(Canceled) || this->queryState(Finished))
+ return false;
auto &store = resultStoreBase();
+ const int resultCountBefore = store.count();
+ const int insertIndex = store.addResults(beginIndex, &_results, count);
+ if (insertIndex == -1)
+ return false;
if (store.filterMode()) {
- const int resultCountBefore = store.count();
- if (store.addResults(beginIndex, &_results, count) != -1)
- this->reportResultsReady(resultCountBefore, store.count());
+ this->reportResultsReady(resultCountBefore, store.count());
} else {
- const int insertIndex = store.addResults(beginIndex, &_results, count);
- if (insertIndex != -1)
- this->reportResultsReady(insertIndex, insertIndex + _results.count());
+ this->reportResultsReady(insertIndex, insertIndex + _results.count());
}
+ return true;
}
template <typename T>
-inline void QFutureInterface<T>::reportFinished(const T *result)
+inline bool QFutureInterface<T>::reportFinished(const T *result)
{
+ bool resultReported = false;
if (result)
- reportResult(result);
+ resultReported = reportResult(result);
reportFinished();
+ return resultReported;
}
template <typename T>
@@ -427,9 +430,14 @@ public:
inline QFuture<void> future(); // implemented in qfuture.h
- void reportResult(const void *, int) { }
- void reportResults(const QList<void> &, int) { }
- void reportFinished(const void * = nullptr)
+ bool reportResult(const void *, int) { return false; }
+ bool reportResults(const QList<void> &, int) { return false; }
+ bool reportFinished(const void *)
+ {
+ reportFinished();
+ return false;
+ }
+ void reportFinished()
{
QFutureInterfaceBase::reportFinished();
QFutureInterfaceBase::runContinuation();
diff --git a/src/corelib/thread/qpromise.h b/src/corelib/thread/qpromise.h
index 59c31de212..4225abd771 100644
--- a/src/corelib/thread/qpromise.h
+++ b/src/corelib/thread/qpromise.h
@@ -89,9 +89,9 @@ public:
template<typename U = T,
typename = QtPrivate::EnableForNonVoid<std::decay_t<U>>,
typename = QtPrivate::EnableIfSameOrConvertible<std::decay_t<U>, std::decay_t<T>>>
- void addResult(U &&result, int index = -1)
+ bool addResult(U &&result, int index = -1)
{
- d.reportResult(std::forward<U>(result), index);
+ return d.reportResult(std::forward<U>(result), index);
}
#ifndef QT_NO_EXCEPTIONS
void setException(const QException &e) { d.reportException(e); }
@@ -118,8 +118,8 @@ public:
}
#if defined(Q_CLANG_QDOC) // documentation-only simplified signatures
- void addResult(const T &result, int index = -1) { }
- void addResult(T &&result, int index = -1) { }
+ bool addResult(const T &result, int index = -1) { }
+ bool addResult(T &&result, int index = -1) { }
#endif
private:
mutable QFutureInterface<T> d = QFutureInterface<T>();
diff --git a/src/corelib/thread/qpromise.qdoc b/src/corelib/thread/qpromise.qdoc
index 4b7b497b3f..279bc8766a 100644
--- a/src/corelib/thread/qpromise.qdoc
+++ b/src/corelib/thread/qpromise.qdoc
@@ -107,13 +107,17 @@
Returns a future associated with this promise.
*/
-/*! \fn template <typename T> void QPromise<T>::addResult(const T &result, int index = -1)
+/*! \fn template <typename T> bool QPromise<T>::addResult(const T &result, int index = -1)
+ \fn template <typename T> bool QPromise<T>::addResult(T &&result, int index = -1)
Adds \a result to the internal result collection at \a index position. If
index is unspecified, \a result is added to the end of the collection.
- \note addResult() rejects \a result if there's already another result in the
- collection stored at the same index.
+ Returns \c true when \a result is added to the collection.
+
+ Returns \c false when this promise is in cancelled or finished state or when
+ \a result is rejected. addResult() rejects \a result if there's already
+ another result in the collection stored at the same index.
You can get a result at a specific index by calling QFuture::resultAt().
@@ -124,11 +128,6 @@
thinking if there are index gaps or not, use QFuture::results().
*/
-/*! \fn template <typename T> void QPromise<T>::addResult(T &&result, int index = -1)
-
- \overload
-*/
-
/*! \fn template<typename T> void QPromise<T>::setException(const QException &e)
Sets exception \a e to be the result of the computation.