{"id":13525,"url":"https://patchwork.libcamera.org/api/1.1/patches/13525/?format=json","web_url":"https://patchwork.libcamera.org/patch/13525/","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":"<20210827023829.5871-5-laurent.pinchart@ideasonboard.com>","date":"2021-08-27T02:38:27","name":"[libcamera-devel,v1,4/6] libcamera: base: signal: Support connecting signals to functors","commit_ref":"58720e1dc98186e79ef4e758a851b58df562f7b4","pull_url":null,"state":"accepted","archived":false,"hash":"c3c74ab34f1c907b5f5096dad023f56cddca5951","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/13525/mbox/","series":[{"id":2402,"url":"https://patchwork.libcamera.org/api/1.1/series/2402/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2402","date":"2021-08-27T02:38:23","name":"libcamera: Drop emitter object pointer from signal arguments","version":1,"mbox":"https://patchwork.libcamera.org/series/2402/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13525/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13525/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 48D3FBD87D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 27 Aug 2021 02:38:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E0DED68928;\n\tFri, 27 Aug 2021 04:38:51 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3852268932\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 27 Aug 2021 04:38:47 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D09B8739\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 27 Aug 2021 04:38:46 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"quB1niBk\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1630031927;\n\tbh=cs4NVSKpxfneJ2591N2rLe10LLTCB1h3oRmVFwa8SjM=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=quB1niBkCj2TxxANOpxwtnTO/q0tBJxllYyMpnsadlLtsZ5+Jr03F+JPzNuT0PJKu\n\tgLrW5E9u7j5CnPDeSMwwIRNK3o5W+GjYScYZhlY+4DDgr7DDBvbFHXE9bOS6Pfy8oZ\n\tZNEZDXl24mwCaMrlH3p9iKXYWfkSaO9mmC02DJMM=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 27 Aug 2021 05:38:27 +0300","Message-Id":"<20210827023829.5871-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.31.1","In-Reply-To":"<20210827023829.5871-1-laurent.pinchart@ideasonboard.com>","References":"<20210827023829.5871-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v1 4/6] libcamera: base: signal: Support\n\tconnecting signals to functors","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"It can be useful to connect a signal to a functor, and in particular a\nlambda function, while still operating in the context of a receiver\nobject (to support both object-based disconnection and queued\nconnections to Object instances).\n\nAdd a BoundMethodFunctor class to bind a functor, and a corresponding\nSignal::connect() function. There is no corresponding disconnect()\nfunction, as a lambda passed to connect() can't be later passed to\ndisconnect(). Disconnection typically uses disconnect(T *object), which\nwill cover the vast majority of use cases.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n Documentation/Doxyfile.in             |  1 +\n include/libcamera/base/bound_method.h | 31 +++++++++++++++++++++\n include/libcamera/base/signal.h       | 19 +++++++++++++\n src/libcamera/base/signal.cpp         | 24 +++++++++++++++++\n test/signal.cpp                       | 39 +++++++++++++++++++++++++++\n 5 files changed, 114 insertions(+)","diff":"diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\nindex dc03cbea4b02..d562510e902e 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -878,6 +878,7 @@ EXCLUDE_PATTERNS       = @TOP_BUILDDIR@/include/libcamera/ipa/*_serializer.h \\\n \n EXCLUDE_SYMBOLS        = libcamera::BoundMethodArgs \\\n                          libcamera::BoundMethodBase \\\n+                         libcamera::BoundMethodFunctor \\\n                          libcamera::BoundMethodMember \\\n                          libcamera::BoundMethodPack \\\n                          libcamera::BoundMethodPackBase \\\ndiff --git a/include/libcamera/base/bound_method.h b/include/libcamera/base/bound_method.h\nindex 76ce8017e721..ebd297ab8209 100644\n--- a/include/libcamera/base/bound_method.h\n+++ b/include/libcamera/base/bound_method.h\n@@ -128,6 +128,37 @@ public:\n \tvirtual R invoke(Args... args) = 0;\n };\n \n+template<typename T, typename R, typename Func, typename... Args>\n+class BoundMethodFunctor : public BoundMethodArgs<R, Args...>\n+{\n+public:\n+\tusing PackType = typename BoundMethodArgs<R, Args...>::PackType;\n+\n+\tBoundMethodFunctor(T *obj, Object *object, Func func,\n+\t\t\t   ConnectionType type = ConnectionTypeAuto)\n+\t\t: BoundMethodArgs<R, Args...>(obj, object, type), func_(func)\n+\t{\n+\t}\n+\n+\tR activate(Args... args, bool deleteMethod = false) override\n+\t{\n+\t\tif (!this->object_)\n+\t\t\treturn func_(args...);\n+\n+\t\tauto pack = std::make_shared<PackType>(args...);\n+\t\tbool sync = BoundMethodBase::activatePack(pack, deleteMethod);\n+\t\treturn sync ? pack->returnValue() : R();\n+\t}\n+\n+\tR invoke(Args... args) override\n+\t{\n+\t\treturn func_(args...);\n+\t}\n+\n+private:\n+\tFunc func_;\n+};\n+\n template<typename T, typename R, typename... Args>\n class BoundMethodMember : public BoundMethodArgs<R, Args...>\n {\ndiff --git a/include/libcamera/base/signal.h b/include/libcamera/base/signal.h\nindex c2521769a843..8d9f82f62d0d 100644\n--- a/include/libcamera/base/signal.h\n+++ b/include/libcamera/base/signal.h\n@@ -61,6 +61,25 @@ public:\n \t\tSignalBase::connect(new BoundMethodMember<T, R, Args...>(obj, nullptr, func));\n \t}\n \n+#ifndef __DOXYGEN__\n+\ttemplate<typename T, typename Func,\n+\t\t typename std::enable_if_t<std::is_base_of<Object, T>::value> * = nullptr>\n+\tvoid connect(T *obj, Func func, ConnectionType type = ConnectionTypeAuto)\n+\t{\n+\t\tObject *object = static_cast<Object *>(obj);\n+\t\tSignalBase::connect(new BoundMethodFunctor<T, void, Func, Args...>(obj, object, func, type));\n+\t}\n+\n+\ttemplate<typename T, typename Func,\n+\t\t typename std::enable_if_t<!std::is_base_of<Object, T>::value> * = nullptr>\n+#else\n+\ttemplate<typename T, typename Func>\n+#endif\n+\tvoid connect(T *obj, Func func)\n+\t{\n+\t\tSignalBase::connect(new BoundMethodFunctor<T, void, Func, Args...>(obj, nullptr, func));\n+\t}\n+\n \ttemplate<typename R>\n \tvoid connect(R (*func)(Args...))\n \t{\ndiff --git a/src/libcamera/base/signal.cpp b/src/libcamera/base/signal.cpp\nindex adcfa796870e..9c2319c59106 100644\n--- a/src/libcamera/base/signal.cpp\n+++ b/src/libcamera/base/signal.cpp\n@@ -121,6 +121,30 @@ SignalBase::SlotList SignalBase::slots()\n  * \\context This function is \\threadsafe.\n  */\n \n+/**\n+ * \\fn Signal::connect(T *object, Func func)\n+ * \\brief Connect the signal to a function object slot\n+ * \\param[in] object The slot object pointer\n+ * \\param[in] func The function object\n+ *\n+ * If the typename T inherits from Object, the signal will be automatically\n+ * disconnected from the \\a func slot of \\a object when \\a object is destroyed.\n+ * Otherwise the caller shall disconnect signals manually before destroying \\a\n+ * object.\n+ *\n+ * The function object is typically a lambda function, but may be any object\n+ * that satisfies the FunctionObject named requirements. The types of the\n+ * function object arguments shall match the types of the signal arguments.\n+ *\n+ * No matching disconnect() function exist, as it wouldn't be possible to pass\n+ * to a disconnect() function the same lambda that was passed to connect(). The\n+ * connection created by this function can not be removed selectively if the\n+ * signal is connected to multiple slots of the same receiver, but may be\n+ * otherwise be removed using the disconnect(T *object) function.\n+ *\n+ * \\context This function is \\threadsafe.\n+ */\n+\n /**\n  * \\fn Signal::connect(R (*func)(Args...))\n  * \\brief Connect the signal to a static function slot\ndiff --git a/test/signal.cpp b/test/signal.cpp\nindex 595782a2cd6e..fcf2def18df4 100644\n--- a/test/signal.cpp\n+++ b/test/signal.cpp\n@@ -191,6 +191,24 @@ protected:\n \t\tsignalVoid_.connect(slotStaticReturn);\n \t\tsignalVoid_.connect(this, &SignalTest::slotReturn);\n \n+\t\t/* Test signal connection to a lambda. */\n+\t\tint value = 0;\n+\t\tsignalInt_.connect(this, [&](int v) { value = v; });\n+\t\tsignalInt_.emit(42);\n+\n+\t\tif (value != 42) {\n+\t\t\tcout << \"Signal connection to lambda failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tsignalInt_.disconnect(this);\n+\t\tsignalInt_.emit(0);\n+\n+\t\tif (value != 42) {\n+\t\t\tcout << \"Signal disconnection from lambda failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\t/* ----------------- Signal -> Object tests ----------------- */\n \n \t\t/*\n@@ -256,6 +274,27 @@ protected:\n \n \t\tdelete slotObject;\n \n+\t\t/* Test signal connection to a lambda. */\n+\t\tslotObject = new SlotObject();\n+\t\tvalue = 0;\n+\t\tsignalInt_.connect(slotObject, [&](int v) { value = v; });\n+\t\tsignalInt_.emit(42);\n+\n+\t\tif (value != 42) {\n+\t\t\tcout << \"Signal connection to Object lambda failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tsignalInt_.disconnect(slotObject);\n+\t\tsignalInt_.emit(0);\n+\n+\t\tif (value != 42) {\n+\t\t\tcout << \"Signal disconnection from Object lambda failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tdelete slotObject;\n+\n \t\t/* --------- Signal -> Object (multiple inheritance) -------- */\n \n \t\t/*\n","prefixes":["libcamera-devel","v1","4/6"]}