diff --git a/src/corelib/kernel/qtimer.cpp b/src/corelib/kernel/qtimer.cpp index 5d1a9c7497c..dccbf37d03b 100644 --- a/src/corelib/kernel/qtimer.cpp +++ b/src/corelib/kernel/qtimer.cpp @@ -441,125 +441,26 @@ void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiv } } -/*! \fn template void QTimer::singleShot(int msec, const QObject *receiver, PointerToMemberFunction method) - +/*! \fn template void QTimer::singleShot(int msec, const QObject *context, Functor &&functor) + \fn template void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor &&functor) + \fn template void QTimer::singleShot(int msec, Functor &&functor) + \fn template void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor &&functor) \since 5.4 - \overload \reentrant - This static function calls a member function of a QObject after a given time interval. + This static function calls \a functor after \a msec milliseconds. It is very convenient to use this function because you do not need to bother with a \l{QObject::timerEvent()}{timerEvent} or create a local QTimer object. - The \a receiver is the receiving object and the \a method is the member function. The - time interval is \a msec milliseconds. + If \a context is specified, then the \a functor will be called only if the + \a context object has not been destroyed before the interval occurs. The functor + will then be run the thread of \a context. The context's thread must have a + running Qt event loop. - If \a receiver is destroyed before the interval occurs, the method will not be called. - The function will be run in the thread of \a receiver. The receiver's thread must have - a running Qt event loop. - - \sa start() -*/ - -/*! \fn template void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method) - - \since 5.4 - - \overload - \reentrant - This static function calls a member function of a QObject after a given time interval. - - It is very convenient to use this function because you do not need - to bother with a \l{QObject::timerEvent()}{timerEvent} or - create a local QTimer object. - - The \a receiver is the receiving object and the \a method is the member function. The - time interval is \a msec milliseconds. The \a timerType affects the - accuracy of the timer. - - If \a receiver is destroyed before the interval occurs, the method will not be called. - The function will be run in the thread of \a receiver. The receiver's thread must have - a running Qt event loop. - - \sa start() -*/ - -/*! \fn template void QTimer::singleShot(int msec, Functor functor) - - \since 5.4 - - \overload - \reentrant - This static function calls \a functor after a given time interval. - - It is very convenient to use this function because you do not need - to bother with a \l{QObject::timerEvent()}{timerEvent} or - create a local QTimer object. - - The time interval is \a msec milliseconds. - - \sa start() -*/ - -/*! \fn template void QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor) - - \since 5.4 - - \overload - \reentrant - This static function calls \a functor after a given time interval. - - It is very convenient to use this function because you do not need - to bother with a \l{QObject::timerEvent()}{timerEvent} or - create a local QTimer object. - - The time interval is \a msec milliseconds. The \a timerType affects the - accuracy of the timer. - - \sa start() -*/ - -/*! \fn template void QTimer::singleShot(int msec, const QObject *context, Functor functor) - - \since 5.4 - - \overload - \reentrant - This static function calls \a functor after a given time interval. - - It is very convenient to use this function because you do not need - to bother with a \l{QObject::timerEvent()}{timerEvent} or - create a local QTimer object. - - The time interval is \a msec milliseconds. - - If \a context is destroyed before the interval occurs, the method will not be called. - The function will be run in the thread of \a context. The context's thread must have - a running Qt event loop. - - \sa start() -*/ - -/*! \fn template void QTimer::singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor) - - \since 5.4 - - \overload - \reentrant - This static function calls \a functor after a given time interval. - - It is very convenient to use this function because you do not need - to bother with a \l{QObject::timerEvent()}{timerEvent} or - create a local QTimer object. - - The time interval is \a msec milliseconds. The \a timerType affects the - accuracy of the timer. - - If \a context is destroyed before the interval occurs, the method will not be called. - The function will be run in the thread of \a context. The context's thread must have - a running Qt event loop. + If \a functor is a member + function of \a context, then the function will be called on the object. \sa start() */ diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h index a58eafa7d39..829d58a7c28 100644 --- a/src/corelib/kernel/qtimer.h +++ b/src/corelib/kernel/qtimer.h @@ -49,81 +49,50 @@ public: static void singleShot(int msec, const QObject *receiver, const char *member); static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, const char *member); + // singleShot with context + template + static inline void singleShot(Duration interval, #ifdef Q_QDOC - template - static void singleShot(int msec, const QObject *receiver, PointerToMemberFunction method); - template - static void singleShot(int msec, Qt::TimerType timerType, const QObject *receiver, PointerToMemberFunction method); - template - static void singleShot(int msec, Functor functor); - template - static void singleShot(int msec, Qt::TimerType timerType, Functor functor); - template - static void singleShot(int msec, const QObject *context, Functor functor); - template - static void singleShot(int msec, Qt::TimerType timerType, const QObject *context, Functor functor); - template - QMetaObject::Connection callOnTimeout(Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection); - template - QMetaObject::Connection callOnTimeout(const QObject *context, Functor slot, Qt::ConnectionType connectionType = Qt::AutoConnection); - template - QMetaObject::Connection callOnTimeout(const QObject *receiver, MemberFunction *slot, Qt::ConnectionType connectionType = Qt::AutoConnection); + const QObject *receiver, #else - // singleShot to a QObject slot - template - static inline void singleShot(Duration interval, const typename QtPrivate::FunctionPointer::Object *receiver, Func1 slot) + const typename QtPrivate::ContextTypeForFunctor::ContextType *receiver, +#endif + + Func1 &&slot) { - singleShot(interval, defaultTypeFor(interval), receiver, slot); + singleShot(interval, defaultTypeFor(interval), receiver, std::forward(slot)); } template - static inline void singleShot(Duration interval, Qt::TimerType timerType, const typename QtPrivate::FunctionPointer::Object *receiver, - Func1 slot) + static inline void singleShot(Duration interval, Qt::TimerType timerType, +#ifdef Q_QDOC + const QObject *receiver, +#else + const typename QtPrivate::ContextTypeForFunctor::ContextType *receiver, +#endif + Func1 &&slot) { - typedef QtPrivate::FunctionPointer SlotType; - - //compilation error if the slot has arguments. - static_assert(int(SlotType::ArgumentCount) == 0, - "The slot must not have any arguments."); - + using Prototype = void(*)(); singleShotImpl(interval, timerType, receiver, - new QtPrivate::QSlotObject(slot)); + QtPrivate::makeSlotObject(std::forward(slot))); } - // singleShot to a functor or function pointer (without context) + // singleShot without context template - static inline typename std::enable_if::IsPointerToMemberFunction && - !std::is_same::value, void>::type - singleShot(Duration interval, Func1 slot) + static inline void singleShot(Duration interval, Func1 &&slot) { - singleShot(interval, defaultTypeFor(interval), nullptr, std::move(slot)); + singleShot(interval, defaultTypeFor(interval), nullptr, std::forward(slot)); } template - static inline typename std::enable_if::IsPointerToMemberFunction && - !std::is_same::value, void>::type - singleShot(Duration interval, Qt::TimerType timerType, Func1 slot) + static inline void singleShot(Duration interval, Qt::TimerType timerType, Func1 &&slot) { - singleShot(interval, timerType, nullptr, std::move(slot)); - } - // singleShot to a functor or function pointer (with context) - template - static inline typename std::enable_if::IsPointerToMemberFunction && - !std::is_same::value, void>::type - singleShot(Duration interval, const QObject *context, Func1 slot) - { - singleShot(interval, defaultTypeFor(interval), context, std::move(slot)); - } - template - static inline typename std::enable_if::IsPointerToMemberFunction && - !std::is_same::value, void>::type - singleShot(Duration interval, Qt::TimerType timerType, const QObject *context, Func1 slot) - { - //compilation error if the slot has arguments. - typedef QtPrivate::FunctionPointer SlotType; - static_assert(int(SlotType::ArgumentCount) <= 0, "The slot must not have any arguments."); - - singleShotImpl(interval, timerType, context, - new QtPrivate::QFunctorSlotObject, void>(std::move(slot))); + singleShot(interval, timerType, nullptr, std::forward(slot)); } +#ifdef Q_QDOC + template + QMetaObject::Connection callOnTimeout(Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection); + template + QMetaObject::Connection callOnTimeout(const QObject *context, Functor &&slot, Qt::ConnectionType connectionType = Qt::AutoConnection); +#else template QMetaObject::Connection callOnTimeout(Args && ...args) {