File indexing completed on 2024-05-12 17:08:32

0001 /*
0002  *   SPDX-FileCopyrightText: 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 //
0008 // W A R N I N G
0009 // -------------
0010 //
0011 // This file is not part of the AsynQt API. It exists purely as an
0012 // implementation detail. This header file may change from version to
0013 // version without notice, or even be removed.
0014 //
0015 // We mean it.
0016 //
0017 
0018 namespace AsynQt
0019 {
0020 namespace detail
0021 {
0022 template<typename _Result>
0023 class DelayedFutureInterface : public QObject, public QFutureInterface<_Result>
0024 {
0025 public:
0026     DelayedFutureInterface(_Result value, int milliseconds)
0027         : m_value(value)
0028         , m_milliseconds(milliseconds)
0029     {
0030     }
0031 
0032     QFuture<_Result> start()
0033     {
0034         auto future = this->future();
0035 
0036         this->reportStarted();
0037 
0038         QTimer::singleShot(m_milliseconds, [this] {
0039             this->reportResult(m_value);
0040             this->reportFinished();
0041             deleteLater();
0042         });
0043 
0044         return future;
0045     }
0046 
0047 private:
0048     _Result m_value;
0049     int m_milliseconds;
0050 };
0051 
0052 template<typename T = void>
0053 class DelayedVoidFutureInterface : public QObject, QFutureInterface<void>
0054 {
0055 public:
0056     DelayedVoidFutureInterface(int milliseconds)
0057         : m_milliseconds(milliseconds)
0058     {
0059     }
0060 
0061     QFuture<void> start()
0062     {
0063         auto future = this->future();
0064 
0065         this->reportStarted();
0066 
0067         QTimer::singleShot(m_milliseconds, [this] {
0068             this->reportFinished();
0069             deleteLater();
0070         });
0071 
0072         deleteLater();
0073 
0074         return future;
0075     }
0076 
0077 private:
0078     int m_milliseconds;
0079 };
0080 
0081 template<typename _Result>
0082 DelayedFutureInterface<typename std::decay<_Result>::type> *newDelayedFutureInterface(_Result &&result, int milliseconds)
0083 {
0084     return new DelayedFutureInterface<typename std::decay<_Result>::type>(std::forward<_Result>(result), milliseconds);
0085 }
0086 
0087 } // namespace detail
0088 } // namespace AsynQt