From patchwork Tue Aug 30 03:06:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17248 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 4D361C0DA4 for ; Tue, 30 Aug 2022 03:06:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8E24C61FC1; Tue, 30 Aug 2022 05:06:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661828786; bh=AFlfpybCwUKEMj/4KfEi4jsLzT1hkHhJMmsTIIMRX34=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=4GRU7xCyJ/iN4mx2ZfvjK74Lh7aa1fzmerUnn6Y4Mbl/rfOV8oDyW6QoRUC/GCFXi N8xJPAnYuAdIUgWBcDZHJAukxzhVflXy0rUv87a37Ar0P98rek4tA5EOok5UzJJOAt ubAa9lBjsBiBL4pLCEXpYWCwsZitS8I1DXZwu4ptLpd2L31L5mo2dGVaI4gzQPti0q D82vgzqo5PzhS85Oy398tNaIfeEu9x5+HFSzVJPS6A1w8MTo5pZ0Gt0/Kfo6SREX8j h2bWmW+kUzwag6xcpi6/ZAaZS/N37m3qF3bvTlWBrlPVptCAO8YBRxPdrfJaCWSinf 07QXipu4Gr6kw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5D8F2603E0 for ; Tue, 30 Aug 2022 05:06:25 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Pyu737Wr"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A139C4A8; Tue, 30 Aug 2022 05:06:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661828784; bh=AFlfpybCwUKEMj/4KfEi4jsLzT1hkHhJMmsTIIMRX34=; h=From:To:Cc:Subject:Date:From; b=Pyu737Wrb/fAg4wVL+SQwpCz+vVm/JquotlsiC6/YY3mvh8Idj+9ey1yUtVBI5xSc 5JprolfeE1UrdWl6iHsxMqY96IiOiLedId4Hq/+QeCOEEp1ZxbWvQIWNllxpMIhDxw B0w/TRR7B+2Gi0jlcGiE1DxmcLqQJwuJmoHdx+4A= To: libcamera-devel@lists.libcamera.org Date: Tue, 30 Aug 2022 06:06:15 +0300 Message-Id: <20220830030615.29089-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: base: signal: Disable connect() for functor if args mismatch X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" If a pointer-to-member is passed to the Signal::connect() function with arguments that don't match the Signal type, the pointer-to-member version of connect() will not match during template argument resolution, but the functor version will. This results in a compilation error in the BoundMethodFunctor class, due to the pointer-to-member not being a functor and thus not being callable directly. The error messages are quite cryptic. With the following error applied, Reviewed-by: Paul Elder Acked-by: Kieran Bingham diff --git a/test/signal.cpp b/test/signal.cpp index 5c6b304dac0b..6dd11ac45313 100644 --- a/test/signal.cpp +++ b/test/signal.cpp @@ -107,6 +107,7 @@ protected: /* Test signal emission and reception. */ called_ = false; signalVoid_.connect(this, &SignalTest::slotVoid); + signalVoid_.connect(this, &SignalTest::slotInteger1); signalVoid_.emit(); if (!called_) { gcc outputs ../../include/libcamera/base/bound_method.h: In instantiation of ‘R libcamera::BoundMethodFunctor::activate(Args ..., bool) [with T = SignalTest; R = void; Func = void (SignalTest::*)(int); Args = {}]’: ../../include/libcamera/base/bound_method.h:143:4: required from here ../../include/libcamera/base/bound_method.h:146:37: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((libcamera::BoundMethodFunctor*)this)->libcamera::BoundMethodFunctor::func_ (...)’, e.g. ‘(... ->* ((libcamera::BoundMethodFunctor*)this)->libcamera::BoundMethodFunctor::func_) (...)’ 146 | return func_(args...); | ~~~~~^~~~~~~~~ and clang isn't much better: ../../include/libcamera/base/bound_method.h:146:11: error: called object type 'void (SignalTest::*)(int)' is not a function or function pointer return func_(args...); ^~~~~ ../../include/libcamera/base/bound_method.h:137:2: note: in instantiation of member function 'libcamera::BoundMethodFunctor::activate' requested here BoundMethodFunctor(T *obj, Object *object, Func func, ^ ../../include/libcamera/base/signal.h:80:27: note: in instantiation of member function 'libcamera::BoundMethodFunctor::BoundMethodFunctor' requested here SignalBase::connect(new BoundMethodFunctor(obj, nullptr, func)); ^ ../../test/signal.cpp:110:15: note: in instantiation of function template specialization 'libcamera::Signal<>::connect' requested here signalVoid_.connect(this, &SignalTest::slotInteger1); ^ Improve error reporting by disabling the functor version of connect() when the Func argument isn't invocable with the Signal arguments. gcc will then complain with ../../test/signal.cpp:110:36: error: no matching function for call to ‘libcamera::Signal<>::connect(SignalTest*, void (SignalTest::*)(int))’ 110 | signalVoid_.connect(this, &SignalTest::slotInteger1); | ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ and clang with ../../test/signal.cpp:110:15: error: no matching member function for call to 'connect' signalVoid_.connect(this, &SignalTest::slotInteger1); ~~~~~~~~~~~~^~~~~~~ which are more readable. This change requires usage of std::is_invocable<>, which is only available starting in C++17. This is fine for usage of the Signal class within libcamera, as the project is compiled with C++17, but we try to keep the public API compatible C++14. Condition the additional checks based on the C++ version. Signed-off-by: Laurent Pinchart --- include/libcamera/base/signal.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/libcamera/base/signal.h b/include/libcamera/base/signal.h index efb591bc5073..841e4b4ca15c 100644 --- a/include/libcamera/base/signal.h +++ b/include/libcamera/base/signal.h @@ -63,7 +63,11 @@ public: #ifndef __DOXYGEN__ template::value> * = nullptr> + std::enable_if_t::value +#if __cplusplus >= 201703L + && std::is_invocable_v +#endif + > * = nullptr> void connect(T *obj, Func func, ConnectionType type = ConnectionTypeAuto) { Object *object = static_cast(obj); @@ -71,7 +75,11 @@ public: } template::value> * = nullptr> + std::enable_if_t::value +#if __cplusplus >= 201703L + && std::is_invocable_v +#endif + > * = nullptr> #else template #endif