From bc98bba2f302922761209c0e91485e809354abc1 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 1 Mar 2013 10:18:36 +0100 Subject: [PATCH] Support connection to functor with multiple operator() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When variadic templates and decltype are supported, detect the best overload of operator() to call. Currently, the code takes the type of the operator(), which requires that the functor only has one, and that it has no template parameter. This feature is required if we want to connect to c++1y generic lambda (N3418) Change-Id: Ifa957da6955ea39ab804b58f320da9f98ff47d63 Reviewed-by: Jędrzej Nowacki --- src/corelib/kernel/qobject.h | 34 ++++- src/corelib/kernel/qobjectdefs_impl.h | 38 +++++- .../corelib/kernel/qobject/tst_qobject.cpp | 126 ++++++++++++++++++ 3 files changed, 191 insertions(+), 7 deletions(-) diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 7beaa32855..aaa09fac50 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2013 Olivier Goffart ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -265,27 +266,50 @@ public: static inline typename QtPrivate::QEnableIf::ArgumentCount == -1, QMetaObject::Connection>::Type connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, Func2 slot) { +#if defined (Q_COMPILER_DECLTYPE) && defined (Q_COMPILER_VARIADIC_TEMPLATES) + typedef QtPrivate::FunctionPointer SignalType; + const int FunctorArgumentCount = QtPrivate::ComputeFunctorArgumentCount::Value; + + Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0), + "Signal and slot arguments are not compatible."); + const int SlotArgumentCount = (FunctorArgumentCount >= 0) ? FunctorArgumentCount : 0; + typedef typename QtPrivate::FunctorReturnType::Value>::Value SlotReturnType; +#else + // Without variadic template, we don't detect the best overload of operator(). We just + // assume there is only one simple operator() and connect to &Func2::operator() + + /* If you get an error such as: + couldn't deduce template parameter 'Func2Operator' + or + cannot resolve address of overloaded function + It means the functor does not have a single operator(). + Functors with overloaded or templated operator() are only supported if the compiler supports + C++11 variadic templates + */ #ifndef Q_COMPILER_DECLTYPE //Workaround the lack of decltype using another function as indirection return connect_functor(sender, signal, slot, &Func2::operator()); } template static inline QMetaObject::Connection connect_functor(const QObject *sender, Func1 signal, Func2 slot, Func2Operator) { typedef QtPrivate::FunctionPointer SlotType ; #else - typedef QtPrivate::FunctionPointer SlotType ; #endif typedef QtPrivate::FunctionPointer SignalType; + typedef typename SlotType::ReturnType SlotReturnType; + const int SlotArgumentCount = SlotType::ArgumentCount; - Q_STATIC_ASSERT_X(int(SignalType::ArgumentCount) >= int(SlotType::ArgumentCount), + Q_STATIC_ASSERT_X(int(SignalType::ArgumentCount) >= SlotArgumentCount, "The slot requires more arguments than the signal provides."); Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments::value), "Signal and slot arguments are not compatible."); - Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible::value), +#endif + + Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible::value), "Return type of the slot is not compatible with the return type of the signal."); return connectImpl(sender, reinterpret_cast(&signal), sender, 0, - new QtPrivate::QFunctorSlotObject::Value, + new QtPrivate::QFunctorSlotObject::Value, typename SignalType::ReturnType>(slot), Qt::DirectConnection, 0, &SignalType::Object::staticMetaObject); } diff --git a/src/corelib/kernel/qobjectdefs_impl.h b/src/corelib/kernel/qobjectdefs_impl.h index 46a6eab253..4f44d9204e 100644 --- a/src/corelib/kernel/qobjectdefs_impl.h +++ b/src/corelib/kernel/qobjectdefs_impl.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2013 Olivier Goffart ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -597,10 +598,43 @@ namespace QtPrivate { enum { value = AreArgumentsCompatible::Type, typename RemoveConstRef::Type>::value && CheckCompatibleArguments, List>::value }; }; - #endif -} +#if defined(Q_COMPILER_DECLTYPE) && defined(Q_COMPILER_VARIADIC_TEMPLATES) + /* + Find the maximum number of arguments a functor object can take and be still compatible with + the arguments from the signal. + Value is the number of arguments, or -1 if nothing matches. + */ + template struct ComputeFunctorArgumentCount; + + template struct ComputeFunctorArgumentCountHelper + { enum { Value = -1 }; }; + template + struct ComputeFunctorArgumentCountHelper, false> + : ComputeFunctorArgumentCount, sizeof...(ArgList)>::Value> {}; + + template struct ComputeFunctorArgumentCount> + { + template static D dummy(); + template static auto test(F f) -> decltype(((f.operator()((dummy())...)), int())); + static char test(...); + enum { + Ok = sizeof(test(dummy())) == sizeof(int), + Value = Ok ? sizeof...(ArgList) : int(ComputeFunctorArgumentCountHelper, Ok>::Value) + }; + }; + + /* get the return type of a functor, given the signal argument list */ + template struct FunctorReturnType; + template struct FunctorReturnType> { + template static D dummy(); + typedef decltype(dummy().operator()((dummy())...)) Value; + }; +#endif + +} QT_END_NAMESPACE diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index dd88d863c9..06fc89f657 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -143,6 +143,7 @@ private slots: void connectVirtualSlots(); void connectPrivateSlots(); void connectFunctorArgDifference(); + void connectFunctorOverloads(); void disconnectDoesNotLeakFunctor(); }; @@ -5599,6 +5600,131 @@ void tst_QObject::connectFunctorArgDifference() QVERIFY(true); } +struct ComplexFunctor { + ComplexFunctor(int &overload, QList &result) : overload(overload), result(result) {} + void operator()(int a, int b) { + overload = 1; + result << a << b; + } + void operator()(double a, double b) { + overload = 2; + result << a << b; + } + void operator()(const QString &s) { + overload = 3; + result << s; + } + void operator()(const QString &) const { + Q_ASSERT(!"Should not be called because the non-const one should"); + overload = -1; + } + template + void operator()(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { + overload = 4; + result << QVariant::fromValue(t1) << QVariant::fromValue(t2) << QVariant::fromValue(t3) << QVariant::fromValue(t4); + } + int &overload; + QList &result; +protected: + void operator()() const { + Q_ASSERT(!"Should not be called because it is protected"); + overload = -1; + } +}; + +struct ComplexFunctorDeriv : ComplexFunctor { + ComplexFunctorDeriv(int &overload, QList &result) : ComplexFunctor(overload, result) {} + + void operator()() const { + overload = 10; + } + void operator()(int a, int b) { + overload = 11; + result << a << b; + } + using ComplexFunctor::operator(); +private: + void operator()(int) { + Q_ASSERT(!"Should not be called because it is private"); + overload = -1; + } +}; + +class FunctorArgDifferenceObject : public QObject +{ + Q_OBJECT +signals: + void signal_ii(int,int); + void signal_iiS(int,int, const QString &); + void signal_dd(double,double); + void signal_ddS(double,double, const QString &); + void signal_S(const QString &); + void signal_SSSS(const QString &, const QString &, const QString &, const QString &); + void signal_iiSS(int, int, const QString &, const QString &); + void signal_VV(const QVariant &, const QVariant &); +}; + +template +void connectFunctorOverload_impl(Signal signal, int expOverload, QList expResult) +{ + FunctorArgDifferenceObject obj; + int overload; + QList result; + QVERIFY(QObject::connect(&obj, signal, Functor(overload, result))); + + obj.signal_ii(1,2); + obj.signal_iiS(3,4,"5"); + obj.signal_dd(6.6,7.7); + obj.signal_ddS(8.8,9.9,"10"); + obj.signal_S("11"); + obj.signal_SSSS("12", "13", "14", "15"); + obj.signal_iiSS(16, 17, "18", "19"); + obj.signal_VV(20,21); + + QCOMPARE(overload, expOverload); + QCOMPARE(result, expResult); +} + +void tst_QObject::connectFunctorOverloads() +{ +#if defined (Q_COMPILER_DECLTYPE) && defined (Q_COMPILER_VARIADIC_TEMPLATES) + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_ii, 1, + (QList() << 1 << 2)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_iiS, 1, + (QList() << 3 << 4)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_dd, 2, + (QList() << 6.6 << 7.7)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_ddS, 2, + (QList() << 8.8 << 9.9)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_S, 3, + (QList() << QString("11"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_SSSS, 4, + (QList() << QString("12") << QString("13") << QString("14") << QString("15"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_iiSS, 4, + (QList() << 16 << 17 << QString("18") << QString("19"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_ii, 11, + (QList() << 1 << 2)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_iiS, 11, + (QList() << 3 << 4)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_dd, 2, + (QList() << 6.6 << 7.7)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_ddS, 2, + (QList() << 8.8 << 9.9)); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_S, 3, + (QList() << QString("11"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_SSSS, 4, + (QList() << QString("12") << QString("13") << QString("14") << QString("15"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_iiSS, 4, + (QList() << 16 << 17 << QString("18") << QString("19"))); + connectFunctorOverload_impl(&FunctorArgDifferenceObject::signal_VV, 10, + (QList())); + + +#else + QSKIP("Does not compile without C++11 variadic template"); +#endif +} + static int countedStructObjectsCount = 0; struct CountedStruct {