{"id":168,"url":"https://patchwork.libcamera.org/api/patches/168/?format=json","web_url":"https://patchwork.libcamera.org/patch/168/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190107231151.23291-6-laurent.pinchart@ideasonboard.com>","date":"2019-01-07T23:11:45","name":"[libcamera-devel,v2,05/11] libcamera: Add signal/slot communication mechanism","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"50a66f04b5205a5d25998e7065e29ebacc381acc","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/168/mbox/","series":[{"id":56,"url":"https://patchwork.libcamera.org/api/series/56/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=56","date":"2019-01-07T23:11:40","name":"libcamera: Add event notification and timers","version":2,"mbox":"https://patchwork.libcamera.org/series/56/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/168/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/168/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 31AFD60B2F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Jan 2019 00:10:49 +0100 (CET)","from avalon.bb.dnainternet.fi\n\t(dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C18D3530\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Jan 2019 00:10:48 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1546902648;\n\tbh=Tnpr9gZIVtVejz0SiinFSY7a4IvfEApBUvEcSHgVzKk=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=IO/BmILEzRf9Y04IP/s9eESSrjhYJubpnrSXVBNUj1g+t3m7HBBqcU4i1R8nbfFO2\n\tM4jRIgIp9qT8KTrsx+4D97eFga6VyVEMpy7rB91yNevcu2c6BeEAxQlIMFSunwYKJF\n\tGHx17M28P3b16J9i4EqPsrv0dhRi078MSejHM7/I=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 05/11] libcamera: Add signal/slot\n\tcommunication mechanism","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 07 Jan 2019 23:10:49 -0000"},"content":"Introduce a Signal class that allows connecting event sources (signals)\nto event listeners (slots) without adding any boilerplate code usually\nassociated with the observer or listener design patterns.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\nChanges since v1:\n\n- Improve documentation\n- Add support for static slots\n---\n Documentation/Doxyfile.in     |   4 +-\n include/libcamera/meson.build |   1 +\n include/libcamera/signal.h    | 154 ++++++++++++++++++++++++++++++++++\n src/libcamera/meson.build     |   1 +\n src/libcamera/signal.cpp      |  84 +++++++++++++++++++\n 5 files changed, 243 insertions(+), 1 deletion(-)\n create mode 100644 include/libcamera/signal.h\n create mode 100644 src/libcamera/signal.cpp","diff":"diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\nindex b1a70d36eee5..aac20839c837 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -860,7 +860,9 @@ EXCLUDE_PATTERNS       =\n # Note that the wildcards are matched against the file with absolute path, so to\n # exclude all test directories use the pattern */test/*\n \n-EXCLUDE_SYMBOLS        =\n+EXCLUDE_SYMBOLS        = libcamera::SlotBase \\\n+                         libcamera::SlotMember \\\n+                         libcamera::SlotStatic\n \n # The EXAMPLE_PATH tag can be used to specify one or more files or directories\n # that contain example code fragments that are included (see the \\include\ndiff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex 3e04557d66b1..6f87689ea528 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -2,6 +2,7 @@ libcamera_api = files([\n     'camera.h',\n     'camera_manager.h',\n     'libcamera.h',\n+    'signal.h',\n ])\n \n install_headers(libcamera_api,\ndiff --git a/include/libcamera/signal.h b/include/libcamera/signal.h\nnew file mode 100644\nindex 000000000000..458db1ac0060\n--- /dev/null\n+++ b/include/libcamera/signal.h\n@@ -0,0 +1,154 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * signal.h - Signal & slot implementation\n+ */\n+#ifndef __LIBCAMERA_SIGNAL_H__\n+#define __LIBCAMERA_SIGNAL_H__\n+\n+#include <list>\n+#include <vector>\n+\n+namespace libcamera {\n+\n+template<typename... Args>\n+class Signal;\n+\n+template<typename... Args>\n+class SlotBase\n+{\n+public:\n+\tSlotBase(void *obj)\n+\t\t: obj_(obj) { }\n+\tvirtual ~SlotBase() { }\n+\n+\tvirtual void invoke(Args... args) = 0;\n+\n+protected:\n+\tfriend class Signal<Args...>;\n+\tvoid *obj_;\n+};\n+\n+template<typename T, typename... Args>\n+class SlotMember : public SlotBase<Args...>\n+{\n+public:\n+\tSlotMember(T *obj, void(T::*func)(Args...))\n+\t\t: SlotBase<Args...>(obj), func_(func) { }\n+\n+\tvoid invoke(Args... args) { (reinterpret_cast<T *>(this->obj_)->*func_)(args...); }\n+\n+private:\n+\tfriend class Signal<Args...>;\n+\tvoid(T::*func_)(Args...);\n+};\n+\n+template<typename... Args>\n+class SlotStatic : public SlotBase<Args...>\n+{\n+public:\n+\tSlotStatic(void(*func)(Args...))\n+\t\t: SlotBase<Args...>(nullptr), func_(func) { }\n+\n+\tvoid invoke(Args... args) { (*func_)(args...); }\n+\n+private:\n+\tfriend class Signal<Args...>;\n+\tvoid(*func_)(Args...);\n+};\n+\n+template<typename... Args>\n+class Signal\n+{\n+public:\n+\tSignal() { }\n+\t~Signal()\n+\t{\n+\t\tfor (SlotBase<Args...> *slot : slots_)\n+\t\t\tdelete slot;\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid connect(T *object, void(T::*func)(Args...))\n+\t{\n+\t\tslots_.push_back(new SlotMember<T, Args...>(object, func));\n+\t}\n+\n+\tvoid connect(void(*func)(Args...))\n+\t{\n+\t\tslots_.push_back(new SlotStatic<Args...>(func));\n+\t}\n+\n+\tvoid disconnect()\n+\t{\n+\t\tfor (SlotBase<Args...> *slot : slots_)\n+\t\t\tdelete slot;\n+\t\tslots_.clear();\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid disconnect(T *object)\n+\t{\n+\t\tfor (auto iter = slots_.begin(); iter != slots_.end(); ) {\n+\t\t\tSlotBase<Args...> *slot = *iter;\n+\t\t\tif (slot->obj_ == object) {\n+\t\t\t\titer = slots_.erase(iter);\n+\t\t\t\tdelete slot;\n+\t\t\t} else {\n+\t\t\t\t++iter;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\ttemplate<typename T>\n+\tvoid disconnect(T *object, void(T::*func)(Args...))\n+\t{\n+\t\tfor (auto iter = slots_.begin(); iter != slots_.end(); ) {\n+\t\t\tSlotBase<Args...> *slot = *iter;\n+\t\t\t/*\n+\t\t\t * If the obj_ pointer matches the object types must\n+\t\t\t * match, so we can safely case to SlotMember<T, Args>.\n+\t\t\t */\n+\t\t\tif (slot->obj_ == object &&\n+\t\t\t    reinterpret_cast<SlotMember<T, Args...> *>(slot)->func_ == func) {\n+\t\t\t\titer = slots_.erase(iter);\n+\t\t\t\tdelete slot;\n+\t\t\t} else {\n+\t\t\t\t++iter;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tvoid disconnect(void(*func)(Args...))\n+\t{\n+\t\tfor (auto iter = slots_.begin(); iter != slots_.end(); ) {\n+\t\t\tSlotBase<Args...> *slot = *iter;\n+\t\t\tif (slot->obj_ == nullptr &&\n+\t\t\t    reinterpret_cast<SlotStatic<Args...> *>(slot)->func_ == func) {\n+\t\t\t\titer = slots_.erase(iter);\n+\t\t\t\tdelete slot;\n+\t\t\t} else {\n+\t\t\t\t++iter;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tvoid emit(Args... args)\n+\t{\n+\t\t/*\n+\t\t * Make a copy of the slots list as the slot could call the\n+\t\t * disconnect operation, invalidating the iterator.\n+\t\t */\n+\t\tstd::vector<SlotBase<Args...> *> slots{ slots_.begin(), slots_.end() };\n+\t\tfor (SlotBase<Args...> *slot : slots)\n+\t\t\tslot->invoke(args...);\n+\t}\n+\n+private:\n+\tstd::list<SlotBase<Args...> *> slots_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_SIGNAL_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 78562299fc42..3ec86e75b57e 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -6,6 +6,7 @@ libcamera_sources = files([\n     'media_device.cpp',\n     'media_object.cpp',\n     'pipeline_handler.cpp',\n+    'signal.cpp',\n ])\n \n libcamera_headers = files([\ndiff --git a/src/libcamera/signal.cpp b/src/libcamera/signal.cpp\nnew file mode 100644\nindex 000000000000..0fd3bb2a34a1\n--- /dev/null\n+++ b/src/libcamera/signal.cpp\n@@ -0,0 +1,84 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * signal.cpp - Signal & slot implementation\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class Signal\n+ * \\brief Generic signal and slot communication mechanism\n+ *\n+ * Signals and slots are a language construct aimed at communication between\n+ * objects through the observer pattern without the need for boilerplate code.\n+ * See http://doc.qt.io/qt-5/signalsandslots.html for more information.\n+ *\n+ * Signals model events that can be observed from objects unrelated to the event\n+ * source. Slots are functions that are called in response to a signal. Signals\n+ * can be connected to and disconnected from slots dynamically at runtime. When\n+ * a signal is emitted, all connected slots are called sequentially in the order\n+ * they have been connected.\n+ *\n+ * Signals are defined with zero, one or more typed parameters. They are emitted\n+ * with a value for each of the parameters, and those values are passed to the\n+ * connected slots.\n+ *\n+ * Slots are normal static or class member functions. In order to be connected\n+ * to a signal, their signature must match the signal type (taking the same\n+ * arguments as the signal and returning void).\n+ *\n+ * Connecting a signal to a slot results in the slot being called with the\n+ * arguments passed to the emit() function when the signal is emitted. Multiple\n+ * slots can be connected to the same signal, and multiple signals can connected\n+ * to the same slot. Duplicate connections between a signal and a slot are\n+ * allowed and result in the slot being called multiple times for the same\n+ * signal emission.\n+ */\n+\n+/**\n+ * \\fn Signal::connect(T *object, void(T::*func)(Args...))\n+ * \\brief Connect the signal to a member function slot\n+ * \\param object The slot object pointer\n+ * \\param func The slot member function\n+ */\n+\n+/**\n+ * \\fn Signal::connect(void(*func)(Args...))\n+ * \\brief Connect the signal to a static function slot\n+ * \\param func The slot static function\n+ */\n+\n+/**\n+ * \\fn Signal::disconnect()\n+ * \\brief Disconnect the signal from all slots\n+ */\n+\n+/**\n+ * \\fn Signal::disconnect(T *object)\n+ * \\brief Disconnect the signal from all slots of the \\a object\n+ */\n+\n+/**\n+ * \\fn Signal::disconnect(T *object, void(T::*func)(Args...))\n+ * \\brief Disconnect the signal from the \\a object slot member function \\a func\n+ */\n+\n+/**\n+ * \\fn Signal::disconnect(void(*func)(Args...))\n+ * \\brief Disconnect the signal from the slot static function \\a func\n+ */\n+\n+/**\n+ * \\fn Signal::emit(Args... args)\n+ * \\brief Emit the signal and call all connected slots\n+ *\n+ * Emitting a signal calls all connected slots synchronously and sequentially in\n+ * the order the slots have been connected. The arguments passed to the emit()\n+ * function are passed to the slot functions unchanged. If a slot modifies one\n+ * of the arguments (when passed by pointer or reference), the modification is\n+ * thus visible to all subsequently called slots.\n+ */\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v2","05/11"]}