From 621edb2367e81860f6b6e89243ad7cbe3e0b6b7f Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Fri, 3 Jan 2020 20:56:05 +0200 Subject: libcamera: bound_method: Support bindings to non-void methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 d194cd41..b50072ff 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 c45165de..586c2cf6 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 57598335..7fbe5a2c 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 6ee348ac..19003331 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 */ -- cgit v1.2.1