From 9d918495ee56dca5826070df888d20adfcc29641 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sat, 22 Apr 2017 13:21:28 +0200 Subject: Add a few std::move in functions accepting slots This allows the use of move-only function objects Task-number: QTBUG-60339 Change-Id: If3595fca338cf7f3039eb566cc02e4e73cd04c86 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qtimer.h | 8 ++++---- src/network/kernel/qhostinfo.h | 4 ++-- src/widgets/widgets/qmenu.h | 8 ++++---- src/widgets/widgets/qtoolbar.h | 4 ++-- tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp | 11 +++++++++-- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/corelib/kernel/qtimer.h b/src/corelib/kernel/qtimer.h index 6e61ca10cb..7db5fc759b 100644 --- a/src/corelib/kernel/qtimer.h +++ b/src/corelib/kernel/qtimer.h @@ -122,14 +122,14 @@ public: !std::is_same::value, void>::type singleShot(Duration interval, Func1 slot) { - singleShot(interval, defaultTypeFor(interval), nullptr, slot); + singleShot(interval, defaultTypeFor(interval), nullptr, std::move(slot)); } template static inline typename std::enable_if::IsPointerToMemberFunction && !std::is_same::value, void>::type singleShot(Duration interval, Qt::TimerType timerType, Func1 slot) { - singleShot(interval, timerType, nullptr, slot); + singleShot(interval, timerType, nullptr, std::move(slot)); } // singleShot to a functor or function pointer (with context) template @@ -137,7 +137,7 @@ public: !std::is_same::value, void>::type singleShot(Duration interval, QObject *context, Func1 slot) { - singleShot(interval, defaultTypeFor(interval), context, slot); + singleShot(interval, defaultTypeFor(interval), context, std::move(slot)); } template static inline typename std::enable_if::IsPointerToMemberFunction && @@ -150,7 +150,7 @@ public: singleShotImpl(interval, timerType, context, new QtPrivate::QFunctorSlotObject::Value, void>(slot)); + typename QtPrivate::List_Left::Value, void>(std::move(slot))); } #endif diff --git a/src/network/kernel/qhostinfo.h b/src/network/kernel/qhostinfo.h index 4484d718bd..00c70ae9b9 100644 --- a/src/network/kernel/qhostinfo.h +++ b/src/network/kernel/qhostinfo.h @@ -125,7 +125,7 @@ public: !std::is_same::value, int>::type lookupHost(const QString &name, Func slot) { - return lookupHost(name, nullptr, slot); + return lookupHost(name, nullptr, std::move(slot)); } // lookupHost to a functor or function pointer (with context) @@ -141,7 +141,7 @@ public: auto slotObj = new QtPrivate::QFunctorSlotObject, - void>(slot); + void>(std::move(slot)); return lookupHostImpl(name, context, slotObj); } #endif // Q_QDOC diff --git a/src/widgets/widgets/qmenu.h b/src/widgets/widgets/qmenu.h index e9a5db1112..dbfd12124c 100644 --- a/src/widgets/widgets/qmenu.h +++ b/src/widgets/widgets/qmenu.h @@ -108,7 +108,7 @@ public: #else result->setShortcut(shortcut); #endif - connect(result, &QAction::triggered, object, slot); + connect(result, &QAction::triggered, object, std::move(slot)); return result; } // addAction(QString): Connect to a functor or function pointer (without context) @@ -121,7 +121,7 @@ public: #else result->setShortcut(shortcut); #endif - connect(result, &QAction::triggered, slot); + connect(result, &QAction::triggered, std::move(slot)); return result; } // addAction(QIcon, QString): Connect to a QObject slot / functor or function pointer (with context) @@ -136,7 +136,7 @@ public: #else result->setShortcut(shortcut); #endif - connect(result, &QAction::triggered, object, slot); + connect(result, &QAction::triggered, object, std::move(slot)); return result; } // addAction(QIcon, QString): Connect to a functor or function pointer (without context) @@ -149,7 +149,7 @@ public: #else result->setShortcut(shortcut); #endif - connect(result, &QAction::triggered, slot); + connect(result, &QAction::triggered, std::move(slot)); return result; } #endif // !Q_QDOC diff --git a/src/widgets/widgets/qtoolbar.h b/src/widgets/widgets/qtoolbar.h index e0f2d9b073..9ffb472c76 100644 --- a/src/widgets/widgets/qtoolbar.h +++ b/src/widgets/widgets/qtoolbar.h @@ -121,7 +121,7 @@ public: addAction(const QString &text, const Obj *object, Func1 slot) { QAction *result = addAction(text); - connect(result, &QAction::triggered, object, slot); + connect(result, &QAction::triggered, object, std::move(slot)); return result; } // addAction(QString): Connect to a functor or function pointer (without context) @@ -139,7 +139,7 @@ public: addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot) { QAction *result = addAction(actionIcon, text); - connect(result, &QAction::triggered, object, slot); + connect(result, &QAction::triggered, object, std::move(slot)); return result; } // addAction(QIcon, QString): Connect to a functor or function pointer (without context) diff --git a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp index 3c2989831e..bcae39af3d 100644 --- a/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp +++ b/tests/auto/corelib/kernel/qtimer/tst_qtimer.cpp @@ -834,7 +834,6 @@ void tst_QTimer::singleShotToFunctors() QTest::qWait(800); QCOMPARE(count, 2); -#if defined(Q_COMPILER_LAMBDA) QTimer::singleShot(0, [&count] { ++count; }); QCoreApplication::processEvents(); QCOMPARE(count, 3); @@ -853,7 +852,15 @@ void tst_QTimer::singleShotToFunctors() thread.quit(); thread.wait(); -#endif + + struct MoveOnly : CountedStruct { + Q_DISABLE_COPY(MoveOnly); + MoveOnly(MoveOnly &&o) : CountedStruct(std::move(o)) {}; + MoveOnly(int *c) : CountedStruct(c) {} + }; + QTimer::singleShot(0, MoveOnly(&count)); + QCoreApplication::processEvents(); + QCOMPARE(count, 5); _e.reset(); _t = Q_NULLPTR; -- cgit v1.2.3