From 3bf5b5f8944dd417530b09dd6f1cd568717c8cc1 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 2 May 2023 17:39:34 +0200 Subject: Use QSlotObject helpers in functor-cases of QMetaObject::invoke MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add helper that allows us to determine the argument list and return type of a functor. This triggers a compile time error if the functor has operator()() overloads (we only support zero-argument call operators, but there might be const/noexcept variations). Use that helper to declare a ZeroArgFunctor type which also declares a ReturnType and Arguments alias. Add a Callable alias that now combines FunctionPointer and ZeroArgFunctor into a single type that we can then use to merge the specializations of QMetaObject::invokeMethod. [ChangeLog][Potentially source-incompatible changes] Using a functor with several operator() overloads in QMetaObject::invokeMethod now causes a compile time error. Qt would previously ignore const and noexcept overloads and always call the mutable version on a copy of the functor. Change-Id: I3eb62c1128014b729575540deab615469290daeb Reviewed-by: MÃ¥rten Nordheim --- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index b8f808ddfe..e721187b1e 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -8561,6 +8561,30 @@ void tst_QObject::asyncCallbackHelper() QVERIFY(caller.callMe0(mutableLambda2)); // this copies the lambda caller.slotObject->call(nullptr, argv); // this call doesn't change mutableLambda2 QCOMPARE(mutableLambda2(), 2); // so we are still at 2 + + { + int called = -1; + struct MutableFunctor { + void operator()() { called = 0; } + int &called; + }; + struct ConstFunctor + { + void operator()() const { called = 1; } + int &called; + }; + + MutableFunctor mf{called}; + QMetaObject::invokeMethod(this, mf); + QCOMPARE(called, 0); + ConstFunctor cf{called}; + QMetaObject::invokeMethod(this, cf); + QCOMPARE(called, 1); + QMetaObject::invokeMethod(this, [&called, u = std::unique_ptr()]{ called = 2; }); + QCOMPARE(called, 2); + QMetaObject::invokeMethod(this, [&called, count = 0]() mutable { called = 3; }); + QCOMPARE(called, 3); + } } QTEST_MAIN(tst_QObject) -- cgit v1.2.3