Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qpromise.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QPROMISE_H
0005 #define QPROMISE_H
0006 
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qfutureinterface.h>
0009 
0010 #include <utility>
0011 
0012 QT_REQUIRE_CONFIG(future);
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 namespace QtPrivate {
0017 
0018 template<class T, class U>
0019 using EnableIfSameOrConvertible = std::enable_if_t<std::is_convertible_v<T, U>>;
0020 
0021 } // namespace QtPrivate
0022 
0023 template<typename T>
0024 class QPromise
0025 {
0026     static_assert (std::is_move_constructible_v<T>
0027                    || std::is_same_v<T, void>,
0028                    "A move-constructible type or type void is required");
0029 public:
0030     QPromise() = default;
0031     Q_DISABLE_COPY(QPromise)
0032     QPromise(QPromise<T> &&other) = default;
0033     QPromise(const QFutureInterface<T> &other) : d(other) {}
0034     QPromise(QFutureInterface<T> &&other) noexcept : d(std::move(other)) {}
0035     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QPromise)
0036     ~QPromise()
0037     {
0038         // If computation is not finished at this point, cancel
0039         // potential waits
0040         if (d.d && !(d.loadState() & QFutureInterfaceBase::State::Finished)) {
0041             d.cancelAndFinish(); // cancel and finalize the state
0042             d.runContinuation();
0043         }
0044         d.cleanContinuation();
0045     }
0046 
0047     // Core QPromise APIs
0048     QFuture<T> future() const { return d.future(); }
0049     template<typename...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
0050     bool emplaceResultAt(int index, Args&&...args)
0051     {
0052         return d.reportAndEmplaceResult(index, std::forward<Args>(args)...);
0053     }
0054     template<typename...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
0055     bool emplaceResult(Args&&...args)
0056     {
0057         return d.reportAndEmplaceResult(-1, std::forward<Args>(args)...);
0058     }
0059     template<typename U = T, typename = QtPrivate::EnableIfSameOrConvertible<U, T>>
0060     bool addResult(U &&result, int index = -1)
0061     {
0062         return d.reportAndEmplaceResult(index, std::forward<U>(result));
0063     }
0064     bool addResults(const QList<T> &result)
0065     { return d.reportResults(result); }
0066 #ifndef QT_NO_EXCEPTIONS
0067     void setException(const QException &e) { d.reportException(e); }
0068 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0069     void setException(std::exception_ptr e) { d.reportException(e); }
0070 #else
0071     void setException(const std::exception_ptr &e) { d.reportException(e); }
0072 #endif
0073 #endif
0074     void start() { d.reportStarted(); }
0075     void finish() { d.reportFinished(); }
0076 
0077     void suspendIfRequested() { d.suspendIfRequested(); }
0078 
0079     bool isCanceled() const { return d.isCanceled(); }
0080 
0081     // Progress methods
0082     void setProgressRange(int minimum, int maximum) { d.setProgressRange(minimum, maximum); }
0083     void setProgressValue(int progressValue) { d.setProgressValue(progressValue); }
0084     void setProgressValueAndText(int progressValue, const QString &progressText)
0085     {
0086         d.setProgressValueAndText(progressValue, progressText);
0087     }
0088 
0089     void swap(QPromise<T> &other) noexcept
0090     {
0091         d.swap(other.d);
0092     }
0093 
0094 #if defined(Q_QDOC)  // documentation-only simplified signatures
0095     bool addResult(const T &result, int index = -1) { }
0096     bool addResult(T &&result, int index = -1) { }
0097 #endif
0098 private:
0099     mutable QFutureInterface<T> d;
0100 };
0101 
0102 template<typename T>
0103 inline void swap(QPromise<T> &a, QPromise<T> &b) noexcept
0104 {
0105     a.swap(b);
0106 }
0107 
0108 QT_END_NAMESPACE
0109 
0110 #endif  // QPROMISE_H