File indexing completed on 2024-05-05 05:37:15

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 #ifndef ASYNQT_CONS_DELAYED_FUTURE_H
0008 #define ASYNQT_CONS_DELAYED_FUTURE_H
0009 
0010 #include <QFuture>
0011 #include <QFutureInterface>
0012 #include <QObject>
0013 #include <QTimer>
0014 
0015 #include "../private/basic/delayedfuture_p.h"
0016 
0017 namespace AsynQt
0018 {
0019 /**
0020  * Creates a future that will arrive in the specified number of milliseconds,
0021  * and contain the specified value.
0022  * @arg value value to return
0023  * @arg milliseconds how much to wait in milliseconds until the future arrives
0024  */
0025 template<typename _Result>
0026 QFuture<typename std::decay<_Result>::type> makeDelayedFuture(_Result &&value, int milliseconds)
0027 {
0028     using namespace detail;
0029 
0030     return newDelayedFutureInterface(std::forward<_Result>(value), milliseconds)->start();
0031 }
0032 
0033 /**
0034  * Creates a void future that will arrive in the specified
0035  * number of milliseconds.
0036  */
0037 inline QFuture<void> makeDelayedFuture(int milliseconds)
0038 {
0039     using namespace detail;
0040     return (new DelayedVoidFutureInterface<>(milliseconds))->start();
0041 }
0042 
0043 } // namespace AsynQt
0044 
0045 #ifndef ASYNQT_DISABLE_STD_CHRONO
0046 
0047 #include <chrono>
0048 
0049 namespace AsynQt
0050 {
0051 /**
0052  * Convenience method for using with std::chrono library. It allows
0053  * type-safe syntax for specifying the duration (as of C++14)
0054  *
0055  * <code>
0056  * makeDelayedFuture(42, 500ms);
0057  * makeDelayedFuture(42, 1h + 30min);
0058  * </code>
0059  */
0060 template<typename _Result, typename Rep, typename Period>
0061 QFuture<typename std::decay<_Result>::type> makeDelayedFuture(_Result &&value, std::chrono::duration<Rep, Period> duration)
0062 {
0063     using namespace std::chrono;
0064 
0065     return makeDelayedFuture(std::forward<_Result>(value), duration_cast<milliseconds>(duration).count());
0066 }
0067 
0068 /**
0069  * Convenience method for using with std::chrono library. It allows
0070  * type-safe syntax for specifying the duration (as of C++14)
0071  *
0072  * <code>
0073  * makeDelayedFuture(500ms);
0074  * makeDelayedFuture(1h + 30min);
0075  * </code>
0076  */
0077 template<typename Rep, typename Period>
0078 QFuture<void> makeDelayedFuture(std::chrono::duration<Rep, Period> duration)
0079 {
0080     using namespace std::chrono;
0081 
0082     return makeDelayedFuture(duration_cast<milliseconds>(duration).count());
0083 }
0084 
0085 } // namespace AsynQt
0086 
0087 #endif // ASYNQT_DISABLE_STD_CHRONO
0088 
0089 #endif // ASYNQT_CONS_DELAYED_FUTURE_H