From 667f53b522d6181ca3794e0d45d52bc1b069a513 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 19 Jan 2020 04:29:33 +0200 Subject: libcamera: signal: Make slots list private MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The slots list is touched from most of the Signal template functions. In order to prepare for thread-safety, move handling of the list to a small number of non-template functions in the SignalBase class. This incidently fixes a bug in signal disconnection handling where the signal wasn't removed from the object's signals list, as pointed out by the signals unit test. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Niklas Söderlund --- include/libcamera/signal.h | 88 ++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 50 deletions(-) (limited to 'include/libcamera/signal.h') diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h index 432d95d0..c13bb30f 100644 --- a/include/libcamera/signal.h +++ b/include/libcamera/signal.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_SIGNAL_H__ #define __LIBCAMERA_SIGNAL_H__ +#include #include #include #include @@ -19,23 +20,18 @@ namespace libcamera { class SignalBase { public: - template - void disconnect(T *obj) - { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodBase *slot = *iter; - if (slot->match(obj)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } - } + void disconnect(Object *object); protected: - friend class Object; - std::list slots_; + using SlotList = std::list; + + void connect(BoundMethodBase *slot); + void disconnect(std::function match); + + SlotList slots(); + +private: + SlotList slots_; }; template @@ -45,12 +41,7 @@ public: Signal() {} ~Signal() { - for (BoundMethodBase *slot : slots_) { - Object *object = slot->object(); - if (object) - object->disconnect(this); - delete slot; - } + disconnect(); } #ifndef __DOXYGEN__ @@ -59,8 +50,7 @@ public: ConnectionType type = ConnectionTypeAuto) { Object *object = static_cast(obj); - object->connect(this); - slots_.push_back(new BoundMethodMember(obj, object, func, type)); + SignalBase::connect(new BoundMethodMember(obj, object, func, type)); } template::value>::type * = nullptr> @@ -69,63 +59,62 @@ public: #endif void connect(T *obj, R (T::*func)(Args...)) { - slots_.push_back(new BoundMethodMember(obj, nullptr, func)); + SignalBase::connect(new BoundMethodMember(obj, nullptr, func)); } template void connect(R (*func)(Args...)) { - slots_.push_back(new BoundMethodStatic(func)); + SignalBase::connect(new BoundMethodStatic(func)); } void disconnect() { - for (BoundMethodBase *slot : slots_) - delete slot; - slots_.clear(); + SignalBase::disconnect([](SlotList::iterator &iter) { + return true; + }); } template void disconnect(T *obj) { - SignalBase::disconnect(obj); + SignalBase::disconnect([obj](SlotList::iterator &iter) { + return (*iter)->match(obj); + }); } template void disconnect(T *obj, R (T::*func)(Args...)) { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SignalBase::disconnect([obj, func](SlotList::iterator &iter) { BoundMethodArgs *slot = static_cast *>(*iter); + + if (!slot->match(obj)) + return false; + /* * If the object matches the slot, the slot is * guaranteed to be a member slot, so we can safely * cast it to BoundMethodMember to match * func. */ - if (slot->match(obj) && - static_cast *>(slot)->match(func)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } + return static_cast *>(slot)->match(func); + }); } template void disconnect(R (*func)(Args...)) { - for (auto iter = slots_.begin(); iter != slots_.end(); ) { - BoundMethodArgs *slot = *iter; - if (slot->match(nullptr) && - static_cast *>(slot)->match(func)) { - iter = slots_.erase(iter); - delete slot; - } else { - ++iter; - } - } + SignalBase::disconnect([func](SlotList::iterator &iter) { + BoundMethodArgs *slot = + static_cast *>(*iter); + + if (!slot->match(nullptr)) + return false; + + return static_cast *>(slot)->match(func); + }); } void emit(Args... args) @@ -134,8 +123,7 @@ public: * Make a copy of the slots list as the slot could call the * disconnect operation, invalidating the iterator. */ - std::vector slots{ slots_.begin(), slots_.end() }; - for (BoundMethodBase *slot : slots) + for (BoundMethodBase *slot : slots()) static_cast *>(slot)->activate(args...); } }; -- cgit v1.2.1