{"id":154,"url":"https://patchwork.libcamera.org/api/1.1/patches/154/?format=json","web_url":"https://patchwork.libcamera.org/patch/154/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20190106023328.10989-5-laurent.pinchart@ideasonboard.com>","date":"2019-01-06T02:33:22","name":"[libcamera-devel,05/11] libcamera: Add signal/slot communication mechanism","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e1d9cb672e0e706d7c78e21c53a31e0f456b15f0","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/154/mbox/","series":[{"id":53,"url":"https://patchwork.libcamera.org/api/1.1/series/53/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=53","date":"2019-01-06T02:33:18","name":"[libcamera-devel,01/11] libcamera: log: Add a LogFatal log level","version":1,"mbox":"https://patchwork.libcamera.org/series/53/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/154/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/154/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 309BA60B2E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jan 2019 03:32:28 +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 C202B513\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  6 Jan 2019 03:32:27 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1546741947;\n\tbh=oY+skbLzSkMGki+7qahzSd6PhQrV5oiC09QvwpXNQEA=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=OMZLnUFvjG8Ia+ktlwsnkSiZ2WGSuESdVITNeBhKDWMEiiC0UT5IqjEtUcAdn/VMb\n\t0FndixfZJpGn/v4Wa9/Cv3k/ox9m4yBCkqToXSxcgL/f3GLHtDWG4Yxw9DLYDbkKC2\n\teOTwEsUR5+UcEVlQN0UKwv33xdRzrFDahSe/OdWM=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun,  6 Jan 2019 04:33:22 +0200","Message-Id":"<20190106023328.10989-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190106023328.10989-1-laurent.pinchart@ideasonboard.com>","References":"<20190106023328.10989-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 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":"Sun, 06 Jan 2019 02:32:28 -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>\n---\n Documentation/Doxyfile.in     |   3 +-\n include/libcamera/meson.build |   1 +\n include/libcamera/signal.h    | 117 ++++++++++++++++++++++++++++++++++\n src/libcamera/meson.build     |   1 +\n src/libcamera/signal.cpp      |  44 +++++++++++++\n 5 files changed, 165 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..558a1ce04377 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -860,7 +860,8 @@ 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::Slot\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..fceb852158ec\n--- /dev/null\n+++ b/include/libcamera/signal.h\n@@ -0,0 +1,117 @@\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 Slot : public SlotBase<Args...>\n+{\n+public:\n+\tSlot(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 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 Slot<T, Args...>(object, 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\tif (slot->obj_ == object &&\n+\t\t\t    reinterpret_cast<Slot<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 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..8b5a6c285c55\n--- /dev/null\n+++ b/src/libcamera/signal.cpp\n@@ -0,0 +1,44 @@\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+\n+/**\n+ * \\fn Signal::connect()\n+ * \\brief Connect the signal to a slot\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 a slot of the \\a object\n+ */\n+\n+/**\n+ * \\fn Signal::emit()\n+ * \\brief Emit the signal and call all connected slots\n+ */\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","05/11"]}