From patchwork Sat Jan 4 05:09:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2504 Return-Path: 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 9404060605 for ; Sat, 4 Jan 2020 06:10:05 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3982630F for ; Sat, 4 Jan 2020 06:10:05 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578114605; bh=WsJUHEKFZpMwIW/SZ2KfP9yBOqYhlIcjE1nKxAa3TBk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=jTjG/eMdnaLUaxTeSc5yqkH//C06u7qxafgF6fYKGDYTK6WteBLqSkCMUBRAPnIYU Ry/JlZsRHHTs0V0s1MO0WLDjT0ockfIqwIF//2VYvTrteJohYkve3p8pVGF8wBTKMD eWPZebzBuisuvICBdyo/GCEGMVJSxajBkd7aZUkU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 4 Jan 2020 07:09:41 +0200 Message-Id: <20200104050947.7673-9-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200104050947.7673-1-laurent.pinchart@ideasonboard.com> References: <20200104050947.7673-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 08/14] libcamera: bound_method: Support bindings to non-void methods 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-List-Received-Date: Sat, 04 Jan 2020 05:10:07 -0000 The bound method implementation is restricted to binding to void methods as return values are not supported. This complicates usage of bound methods, as non-void methods used a slots or Object::invokeMethod() targets need to be wrapped in a void method. Simplify this by supporting arbitrary return types and ignoring the return value. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- include/libcamera/bound_method.h | 28 ++++++++++++------------ include/libcamera/object.h | 6 +++--- include/libcamera/signal.h | 37 +++++++++++++++++--------------- src/libcamera/signal.cpp | 8 +++---- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h index d194cd4133bb..b50072ffc098 100644 --- a/include/libcamera/bound_method.h +++ b/include/libcamera/bound_method.h @@ -79,7 +79,7 @@ public: std::tuple::type...> args_; }; -template +template class BoundMethodArgs : public BoundMethodBase { public: @@ -107,19 +107,19 @@ public: virtual void invoke(Args... args) = 0; }; -template -class BoundMemberMethod : public BoundMethodArgs +template +class BoundMemberMethod : public BoundMethodArgs { public: - using PackType = typename BoundMethodArgs::PackType; + using PackType = typename BoundMethodArgs::PackType; - BoundMemberMethod(T *obj, Object *object, void (T::*func)(Args...), + BoundMemberMethod(T *obj, Object *object, R (T::*func)(Args...), ConnectionType type = ConnectionTypeAuto) - : BoundMethodArgs(obj, object, type), func_(func) + : BoundMethodArgs(obj, object, type), func_(func) { } - bool match(void (T::*func)(Args...)) const { return func == func_; } + bool match(R (T::*func)(Args...)) const { return func == func_; } void activate(Args... args, bool deleteMethod = false) override { @@ -135,20 +135,20 @@ public: } private: - void (T::*func_)(Args...); + R (T::*func_)(Args...); }; -template -class BoundStaticMethod : public BoundMethodArgs +template +class BoundStaticMethod : public BoundMethodArgs { public: - BoundStaticMethod(void (*func)(Args...)) - : BoundMethodArgs(nullptr, nullptr, ConnectionTypeAuto), + BoundStaticMethod(R (*func)(Args...)) + : BoundMethodArgs(nullptr, nullptr, ConnectionTypeAuto), func_(func) { } - bool match(void (*func)(Args...)) const { return func == func_; } + bool match(R (*func)(Args...)) const { return func == func_; } void activate(Args... args, bool deleteMethod = false) override { @@ -158,7 +158,7 @@ public: void invoke(Args...) override {} private: - void (*func_)(Args...); + R (*func_)(Args...); }; } /* namespace libcamera */ diff --git a/include/libcamera/object.h b/include/libcamera/object.h index c45165dec8ef..586c2cf6fc70 100644 --- a/include/libcamera/object.h +++ b/include/libcamera/object.h @@ -29,13 +29,13 @@ public: void postMessage(std::unique_ptr msg); - template::value>::type * = nullptr> - void invokeMethod(void (T::*func)(FuncArgs...), ConnectionType type, + void invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type, Args... args) { T *obj = static_cast(this); - auto *method = new BoundMemberMethod(obj, this, func, type); + auto *method = new BoundMemberMethod(obj, this, func, type); method->activate(args..., true); } diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h index 57598335932c..7fbe5a2c528f 100644 --- a/include/libcamera/signal.h +++ b/include/libcamera/signal.h @@ -54,27 +54,28 @@ public: } #ifndef __DOXYGEN__ - template::value>::type * = nullptr> - void connect(T *obj, void (T::*func)(Args...), + template::value>::type * = nullptr> + void connect(T *obj, R (T::*func)(Args...), ConnectionType type = ConnectionTypeAuto) { Object *object = static_cast(obj); object->connect(this); - slots_.push_back(new BoundMemberMethod(obj, object, func, type)); + slots_.push_back(new BoundMemberMethod(obj, object, func, type)); } - template::value>::type * = nullptr> + template::value>::type * = nullptr> #else - template + template #endif - void connect(T *obj, void (T::*func)(Args...)) + void connect(T *obj, R (T::*func)(Args...)) { - slots_.push_back(new BoundMemberMethod(obj, nullptr, func)); + slots_.push_back(new BoundMemberMethod(obj, nullptr, func)); } - void connect(void (*func)(Args...)) + template + void connect(R (*func)(Args...)) { - slots_.push_back(new BoundStaticMethod(func)); + slots_.push_back(new BoundStaticMethod(func)); } void disconnect() @@ -90,11 +91,12 @@ public: SignalBase::disconnect(obj); } - template - void disconnect(T *obj, void (T::*func)(Args...)) + template + void disconnect(T *obj, R (T::*func)(Args...)) { for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodArgs *slot = static_cast *>(*iter); + BoundMethodArgs *slot = + static_cast *>(*iter); /* * If the object matches the slot, the slot is * guaranteed to be a member slot, so we can safely @@ -102,7 +104,7 @@ public: * func. */ if (slot->match(obj) && - static_cast *>(slot)->match(func)) { + static_cast *>(slot)->match(func)) { iter = slots_.erase(iter); delete slot; } else { @@ -111,12 +113,13 @@ public: } } - void disconnect(void (*func)(Args...)) + template + void disconnect(R (*func)(Args...)) { for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodArgs *slot = *iter; + BoundMethodArgs *slot = *iter; if (slot->match(nullptr) && - static_cast *>(slot)->match(func)) { + static_cast *>(slot)->match(func)) { iter = slots_.erase(iter); delete slot; } else { @@ -133,7 +136,7 @@ public: */ std::vector slots{ slots_.begin(), slots_.end() }; for (BoundMethodBase *slot : slots) - static_cast *>(slot)->activate(args...); + static_cast *>(slot)->activate(args...); } }; diff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp index 6ee348acc8d4..190033317c72 100644 --- a/src/libcamera/signal.cpp +++ b/src/libcamera/signal.cpp @@ -54,7 +54,7 @@ namespace libcamera { */ /** - * \fn Signal::connect(T *object, void(T::*func)(Args...)) + * \fn Signal::connect(T *object, R (T::*func)(Args...)) * \brief Connect the signal to a member function slot * \param[in] object The slot object pointer * \param[in] func The slot member function @@ -66,7 +66,7 @@ namespace libcamera { */ /** - * \fn Signal::connect(void(*func)(Args...)) + * \fn Signal::connect(R (*func)(Args...)) * \brief Connect the signal to a static function slot * \param[in] func The slot static function */ @@ -83,14 +83,14 @@ namespace libcamera { */ /** - * \fn Signal::disconnect(T *object, void(T::*func)(Args...)) + * \fn Signal::disconnect(T *object, R (T::*func)(Args...)) * \brief Disconnect the signal from the \a object slot member function \a func * \param[in] object The object pointer whose slots to disconnect * \param[in] func The slot member function to disconnect */ /** - * \fn Signal::disconnect(void(*func)(Args...)) + * \fn Signal::disconnect(R (*func)(Args...)) * \brief Disconnect the signal from the slot static function \a func * \param[in] func The slot static function to disconnect */