From patchwork Mon Jan 7 23:11:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 168 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 31AFD60B2F for ; Tue, 8 Jan 2019 00:10:49 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C18D3530 for ; Tue, 8 Jan 2019 00:10:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1546902648; bh=Tnpr9gZIVtVejz0SiinFSY7a4IvfEApBUvEcSHgVzKk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=IO/BmILEzRf9Y04IP/s9eESSrjhYJubpnrSXVBNUj1g+t3m7HBBqcU4i1R8nbfFO2 M4jRIgIp9qT8KTrsx+4D97eFga6VyVEMpy7rB91yNevcu2c6BeEAxQlIMFSunwYKJF GHx17M28P3b16J9i4EqPsrv0dhRi078MSejHM7/I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Jan 2019 01:11:45 +0200 Message-Id: <20190107231151.23291-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> References: <20190107231151.23291-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 05/11] libcamera: Add signal/slot communication mechanism X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Jan 2019 23:10:49 -0000 Introduce a Signal class that allows connecting event sources (signals) to event listeners (slots) without adding any boilerplate code usually associated with the observer or listener design patterns. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- Changes since v1: - Improve documentation - Add support for static slots --- Documentation/Doxyfile.in | 4 +- include/libcamera/meson.build | 1 + include/libcamera/signal.h | 154 ++++++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/signal.cpp | 84 +++++++++++++++++++ 5 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 include/libcamera/signal.h create mode 100644 src/libcamera/signal.cpp diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in index b1a70d36eee5..aac20839c837 100644 --- a/Documentation/Doxyfile.in +++ b/Documentation/Doxyfile.in @@ -860,7 +860,9 @@ EXCLUDE_PATTERNS = # Note that the wildcards are matched against the file with absolute path, so to # exclude all test directories use the pattern */test/* -EXCLUDE_SYMBOLS = +EXCLUDE_SYMBOLS = libcamera::SlotBase \ + libcamera::SlotMember \ + libcamera::SlotStatic # The EXAMPLE_PATH tag can be used to specify one or more files or directories # that contain example code fragments that are included (see the \include diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 3e04557d66b1..6f87689ea528 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -2,6 +2,7 @@ libcamera_api = files([ 'camera.h', 'camera_manager.h', 'libcamera.h', + 'signal.h', ]) install_headers(libcamera_api, diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h new file mode 100644 index 000000000000..458db1ac0060 --- /dev/null +++ b/include/libcamera/signal.h @@ -0,0 +1,154 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * signal.h - Signal & slot implementation + */ +#ifndef __LIBCAMERA_SIGNAL_H__ +#define __LIBCAMERA_SIGNAL_H__ + +#include +#include + +namespace libcamera { + +template +class Signal; + +template +class SlotBase +{ +public: + SlotBase(void *obj) + : obj_(obj) { } + virtual ~SlotBase() { } + + virtual void invoke(Args... args) = 0; + +protected: + friend class Signal; + void *obj_; +}; + +template +class SlotMember : public SlotBase +{ +public: + SlotMember(T *obj, void(T::*func)(Args...)) + : SlotBase(obj), func_(func) { } + + void invoke(Args... args) { (reinterpret_cast(this->obj_)->*func_)(args...); } + +private: + friend class Signal; + void(T::*func_)(Args...); +}; + +template +class SlotStatic : public SlotBase +{ +public: + SlotStatic(void(*func)(Args...)) + : SlotBase(nullptr), func_(func) { } + + void invoke(Args... args) { (*func_)(args...); } + +private: + friend class Signal; + void(*func_)(Args...); +}; + +template +class Signal +{ +public: + Signal() { } + ~Signal() + { + for (SlotBase *slot : slots_) + delete slot; + } + + template + void connect(T *object, void(T::*func)(Args...)) + { + slots_.push_back(new SlotMember(object, func)); + } + + void connect(void(*func)(Args...)) + { + slots_.push_back(new SlotStatic(func)); + } + + void disconnect() + { + for (SlotBase *slot : slots_) + delete slot; + slots_.clear(); + } + + template + void disconnect(T *object) + { + for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SlotBase *slot = *iter; + if (slot->obj_ == object) { + iter = slots_.erase(iter); + delete slot; + } else { + ++iter; + } + } + } + + template + void disconnect(T *object, void(T::*func)(Args...)) + { + for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SlotBase *slot = *iter; + /* + * If the obj_ pointer matches the object types must + * match, so we can safely case to SlotMember. + */ + if (slot->obj_ == object && + reinterpret_cast *>(slot)->func_ == func) { + iter = slots_.erase(iter); + delete slot; + } else { + ++iter; + } + } + } + + void disconnect(void(*func)(Args...)) + { + for (auto iter = slots_.begin(); iter != slots_.end(); ) { + SlotBase *slot = *iter; + if (slot->obj_ == nullptr && + reinterpret_cast *>(slot)->func_ == func) { + iter = slots_.erase(iter); + delete slot; + } else { + ++iter; + } + } + } + + void emit(Args... args) + { + /* + * 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 (SlotBase *slot : slots) + slot->invoke(args...); + } + +private: + std::list *> slots_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_SIGNAL_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 78562299fc42..3ec86e75b57e 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -6,6 +6,7 @@ libcamera_sources = files([ 'media_device.cpp', 'media_object.cpp', 'pipeline_handler.cpp', + 'signal.cpp', ]) libcamera_headers = files([ diff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp new file mode 100644 index 000000000000..0fd3bb2a34a1 --- /dev/null +++ b/src/libcamera/signal.cpp @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2019, Google Inc. + * + * signal.cpp - Signal & slot implementation + */ + +namespace libcamera { + +/** + * \class Signal + * \brief Generic signal and slot communication mechanism + * + * Signals and slots are a language construct aimed at communication between + * objects through the observer pattern without the need for boilerplate code. + * See http://doc.qt.io/qt-5/signalsandslots.html for more information. + * + * Signals model events that can be observed from objects unrelated to the event + * source. Slots are functions that are called in response to a signal. Signals + * can be connected to and disconnected from slots dynamically at runtime. When + * a signal is emitted, all connected slots are called sequentially in the order + * they have been connected. + * + * Signals are defined with zero, one or more typed parameters. They are emitted + * with a value for each of the parameters, and those values are passed to the + * connected slots. + * + * Slots are normal static or class member functions. In order to be connected + * to a signal, their signature must match the signal type (taking the same + * arguments as the signal and returning void). + * + * Connecting a signal to a slot results in the slot being called with the + * arguments passed to the emit() function when the signal is emitted. Multiple + * slots can be connected to the same signal, and multiple signals can connected + * to the same slot. Duplicate connections between a signal and a slot are + * allowed and result in the slot being called multiple times for the same + * signal emission. + */ + +/** + * \fn Signal::connect(T *object, void(T::*func)(Args...)) + * \brief Connect the signal to a member function slot + * \param object The slot object pointer + * \param func The slot member function + */ + +/** + * \fn Signal::connect(void(*func)(Args...)) + * \brief Connect the signal to a static function slot + * \param func The slot static function + */ + +/** + * \fn Signal::disconnect() + * \brief Disconnect the signal from all slots + */ + +/** + * \fn Signal::disconnect(T *object) + * \brief Disconnect the signal from all slots of the \a object + */ + +/** + * \fn Signal::disconnect(T *object, void(T::*func)(Args...)) + * \brief Disconnect the signal from the \a object slot member function \a func + */ + +/** + * \fn Signal::disconnect(void(*func)(Args...)) + * \brief Disconnect the signal from the slot static function \a func + */ + +/** + * \fn Signal::emit(Args... args) + * \brief Emit the signal and call all connected slots + * + * Emitting a signal calls all connected slots synchronously and sequentially in + * the order the slots have been connected. The arguments passed to the emit() + * function are passed to the slot functions unchanged. If a slot modifies one + * of the arguments (when passed by pointer or reference), the modification is + * thus visible to all subsequently called slots. + */ + +} /* namespace libcamera */