From 6ef5e186a99d365471232581df89163b8380ec93 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 2 May 2023 17:23:56 +0200 Subject: [PATCH] Simplify invokeMethod implementations using QSlotObject helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove duplication for overloads covering member function pointer or function pointer cases, as those are now covered by the new QtPrivate::makeSlotObject helper. Change-Id: Ife1e05416958e40a4759ca06cf7db185031b8a86 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/kernel/qmetaobject.cpp | 20 +++---------- src/corelib/kernel/qobjectdefs.h | 48 +++++++----------------------- 2 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index ec3e6a1916e..0ca1c672499 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -1720,31 +1720,19 @@ bool QMetaObject::invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase * */ /*! - \fn template bool QMetaObject::invokeMethod(QObject *context, Functor function, Qt::ConnectionType type, FunctorReturnType *ret) + \fn template bool QMetaObject::invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type, FunctorReturnType *ret) + \fn template bool QMetaObject::invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret) \since 5.10 - \threadsafe - \overload Invokes the \a function in the event loop of \a context. \a function can be a functor or a pointer to a member function. Returns \c true if the function could be invoked. Returns \c false if there is no such function or the parameters did not match. The return value of the function call is placed in \a ret. -*/ -/*! - \fn template bool QMetaObject::invokeMethod(QObject *context, Functor function, FunctorReturnType *ret) - - \since 5.10 - - \threadsafe - \overload - - Invokes the \a function in the event loop of \a context using the connection type Qt::AutoConnection. - \a function can be a functor or a pointer to a member function. Returns \c true if the function could - be invoked. Returns \c false if there is no such member or the parameters did not match. - The return value of the function call is placed in \a ret. + If \a type is set, then the function is invoked using that connection type. Otherwise, + Qt::AutoConnection will be used. */ /*! diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 28b4e368fe8..88b5437683c 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -406,55 +406,29 @@ struct Q_CORE_EXPORT QMetaObject #ifdef Q_QDOC template - static bool invokeMethod(QObject *context, Functor function, Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType *ret = nullptr); + static bool invokeMethod(QObject *context, Functor &&function, Qt::ConnectionType type = Qt::AutoConnection, FunctorReturnType *ret = nullptr); template - static bool invokeMethod(QObject *context, Functor function, FunctorReturnType *ret); + static bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret); #else - // invokeMethod() for member function pointer + // invokeMethod() for member function pointer or function pointer template - static typename std::enable_if::IsPointerToMemberFunction - && !std::is_convertible::value - && QtPrivate::FunctionPointer::ArgumentCount == 0, bool>::type - invokeMethod(typename QtPrivate::FunctionPointer::Object *object, - Func function, + static typename std::enable_if::ArgumentCount == 0, bool>::type + invokeMethod(typename QtPrivate::ContextTypeForFunctor::ContextType *object, + Func &&function, Qt::ConnectionType type = Qt::AutoConnection, typename QtPrivate::FunctionPointer::ReturnType *ret = nullptr) { - return invokeMethodImpl(object, new QtPrivate::QSlotObjectWithNoArgs(function), type, ret); + return invokeMethodImpl(object, QtPrivate::makeSlotObject(std::forward(function)), type, ret); } template - static typename std::enable_if::IsPointerToMemberFunction - && !std::is_convertible::value - && QtPrivate::FunctionPointer::ArgumentCount == 0, bool>::type - invokeMethod(typename QtPrivate::FunctionPointer::Object *object, - Func function, + static typename std::enable_if::ArgumentCount == 0, bool>::type + invokeMethod(typename QtPrivate::ContextTypeForFunctor::ContextType *object, + Func &&function, typename QtPrivate::FunctionPointer::ReturnType *ret) { - return invokeMethodImpl(object, new QtPrivate::QSlotObjectWithNoArgs(function), Qt::AutoConnection, ret); - } - - // invokeMethod() for function pointer (not member) - template - static typename std::enable_if::IsPointerToMemberFunction - && !std::is_convertible::value - && QtPrivate::FunctionPointer::ArgumentCount == 0, bool>::type - invokeMethod(QObject *context, Func function, - Qt::ConnectionType type = Qt::AutoConnection, - typename QtPrivate::FunctionPointer::ReturnType *ret = nullptr) - { - return invokeMethodImpl(context, new QtPrivate::QFunctorSlotObjectWithNoArgsImplicitReturn(function), type, ret); - } - - template - static typename std::enable_if::IsPointerToMemberFunction - && !std::is_convertible::value - && QtPrivate::FunctionPointer::ArgumentCount == 0, bool>::type - invokeMethod(QObject *context, Func function, - typename QtPrivate::FunctionPointer::ReturnType *ret) - { - return invokeMethodImpl(context, new QtPrivate::QFunctorSlotObjectWithNoArgsImplicitReturn(function), Qt::AutoConnection, ret); + return invokeMethod(object, std::forward(function), Qt::AutoConnection, ret); } // invokeMethod() for Functor