diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 88b5437683c..c79e02437f7 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -410,53 +410,30 @@ struct Q_CORE_EXPORT QMetaObject template static bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret); #else - - // invokeMethod() for member function pointer or function pointer template - static typename std::enable_if::ArgumentCount == 0, bool>::type + static std::enable_if_t, + QtPrivate::Invoke::AreOldStyleArgs>, + bool> invokeMethod(typename QtPrivate::ContextTypeForFunctor::ContextType *object, Func &&function, Qt::ConnectionType type = Qt::AutoConnection, - typename QtPrivate::FunctionPointer::ReturnType *ret = nullptr) + typename QtPrivate::Callable::ReturnType *ret = nullptr) { - return invokeMethodImpl(object, QtPrivate::makeSlotObject(std::forward(function)), type, ret); + using Prototype = typename QtPrivate::Callable::Function; + return invokeMethodImpl(object, QtPrivate::makeSlotObject(std::forward(function)), type, ret); } template - static typename std::enable_if::ArgumentCount == 0, bool>::type + static std::enable_if_t, + QtPrivate::Invoke::AreOldStyleArgs>, + bool> invokeMethod(typename QtPrivate::ContextTypeForFunctor::ContextType *object, Func &&function, - typename QtPrivate::FunctionPointer::ReturnType *ret) + typename QtPrivate::Callable::ReturnType *ret) { return invokeMethod(object, std::forward(function), Qt::AutoConnection, ret); } - // invokeMethod() for Functor - template - static typename std::enable_if::IsPointerToMemberFunction - && QtPrivate::FunctionPointer::ArgumentCount == -1 - && !std::is_convertible::value, bool>::type - invokeMethod(QObject *context, Func function, - Qt::ConnectionType type = Qt::AutoConnection, decltype(function()) *ret = nullptr) - { - return invokeMethodImpl(context, - new QtPrivate::QFunctorSlotObjectWithNoArgs(std::move(function)), - type, - ret); - } - - template - static typename std::enable_if::IsPointerToMemberFunction - && QtPrivate::FunctionPointer::ArgumentCount == -1 - && !std::is_convertible::value, bool>::type - invokeMethod(QObject *context, Func function, decltype(function()) *ret) - { - return invokeMethodImpl(context, - new QtPrivate::QFunctorSlotObjectWithNoArgs(std::move(function)), - Qt::AutoConnection, - ret); - } - #endif #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) diff --git a/src/corelib/kernel/qobjectdefs_impl.h b/src/corelib/kernel/qobjectdefs_impl.h index 156a6d6c42d..f87b928263e 100644 --- a/src/corelib/kernel/qobjectdefs_impl.h +++ b/src/corelib/kernel/qobjectdefs_impl.h @@ -239,14 +239,6 @@ namespace QtPrivate { } }; - template struct Functor - { - template - static void call(Function &f, void *, void **arg) { - FunctorCall::Value, SignalArgs, R, Function>::call(f, arg); - } - }; - // Traits to detect if there is a conversion between two types, // and that conversion does not include a narrowing conversion. template @@ -335,6 +327,43 @@ namespace QtPrivate { typedef decltype(std::declval().operator()((std::declval())...)) Value; }; + // Get the function prototype for a functor. There can only be one call operator + // in a functor, otherwise we get errors from ambiguity. But that's good enough. + template + using FunctionTypeForTypes = Ret(*)(Args...); + + template + FunctionTypeForTypes FunctorPrototype(Ret(Obj::*)(Args...) const) { return nullptr; } + template + FunctionTypeForTypes FunctorPrototype(Ret(Obj::*)(Args...)) { return nullptr; } + template + FunctionTypeForTypes FunctorPrototype(Ret(Obj::*)(Args...) const noexcept) { return nullptr; } + template + FunctionTypeForTypes FunctorPrototype(Ret(Obj::*)(Args...) noexcept) { return nullptr; } + + template struct Functor + { + template + static void call(Function &f, void *, void **arg) { + FunctorCall::Value, SignalArgs, R, Function>::call(f, arg); + } + }; + + template + struct ZeroArgFunctor : Functor + { + using Function = decltype(FunctorPrototype(&std::decay_t::operator())); + enum {ArgumentCount = 0}; + using Arguments = QtPrivate::List<>; + using ReturnType = typename FunctionPointer::ReturnType; + }; + + template + using Callable = std::conditional_t::ArgumentCount == -1, + ZeroArgFunctor, + FunctionPointer + >; + /* Wrapper around ComputeFunctorArgumentCount and CheckCompatibleArgument, depending on whether \a Functor is a PMF or not. Returns -1 if \a Func is @@ -440,19 +469,6 @@ namespace QtPrivate { explicit QFunctorSlotObject(const Func &f) : QSlotObjectBase(&impl), function(f) {} }; - // typedefs for readability for when there are no parameters - template - using QSlotObjectWithNoArgs = QFunctorSlotObject, - typename QtPrivate::FunctionPointer::ReturnType>; - - template - using QFunctorSlotObjectWithNoArgs = QFunctorSlotObject, R>; - - template - using QFunctorSlotObjectWithNoArgsImplicitReturn = QFunctorSlotObjectWithNoArgs::ReturnType>; - - // Helper to detect the context object type based on the functor type: // QObject for free functions and lambdas; the callee for member function // pointers. The default declaration doesn't have the ContextType typedef, diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index b8f808ddfe7..e721187b1eb 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)