{"id":24037,"url":"https://patchwork.libcamera.org/api/1.1/patches/24037/?format=json","web_url":"https://patchwork.libcamera.org/patch/24037/","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":"<20250730102733.21314-2-laurent.pinchart@ideasonboard.com>","date":"2025-07-30T10:27:32","name":"[v3,1/2] libcamera: utils: Add scope_exit class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c7c7316e5043f5bf5d32e788725a6cc44915b5db","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/24037/mbox/","series":[{"id":5350,"url":"https://patchwork.libcamera.org/api/1.1/series/5350/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5350","date":"2025-07-30T10:27:31","name":"libcamera: Introduce and use scope_exit","version":3,"mbox":"https://patchwork.libcamera.org/series/5350/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/24037/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/24037/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 D3457BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 30 Jul 2025 10:27:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5EC3569202;\n\tWed, 30 Jul 2025 12:27:47 +0200 (CEST)","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 112786914D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 30 Jul 2025 12:27:44 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id A2621A57\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 30 Jul 2025 12:27:00 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"UyCYIvvJ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1753871220;\n\tbh=K4wNkv/sCNUkGZosj3RtG4NFEzhB+ftcy6CoLQRnD3w=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=UyCYIvvJus/2y/n34H0yZ37f2EWoEHs5fZCfRuBdRphMus440nFHb+rp+leKp6Ap7\n\tlykV+xqpAKN/gohUlONVCm4Rkg+U34kTTGH5t11I150kyfIAYmfTaWfa1LDQCaLuFl\n\tcNucpGzKKnZLHOi5x/HA2icwv7HlbeARXnksUc1M=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH v3 1/2] libcamera: utils: Add scope_exit class","Date":"Wed, 30 Jul 2025 13:27:32 +0300","Message-ID":"<20250730102733.21314-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.49.1","In-Reply-To":"<20250730102733.21314-1-laurent.pinchart@ideasonboard.com>","References":"<20250730102733.21314-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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":"The scope_exit class is an implementation of the identically named C++\nlibrary fundamentals TS v3 class, as documented in [1]. It is a simpler\nversion of the libcamera-specific ScopeExitActions class and doesn't\nrequire dynamic heap memory allocation, making it more suitable for hot\npaths.\n\nThe class is not documented as it implements a C++ standard (even if\nexperimental) API.\n\n[1] https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit.html\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v2:\n\n- Drop lvalue reference constraint in constructor\n- Delete move constructor\n\nI have named the class scope_exit instead of ScopeExit to match the C++\nlibrary fundamentals TS v3 class, and have not documented it as it's\nmeant as an implementation of the standard. It is however not clear if\nand when scope_exit would become part of a ratified C++ standard, so I'm\nopen to instead create a libcamera-specific ScopeExit class and document\nit.\n---\n include/libcamera/base/utils.h | 38 ++++++++++++++++++++++++++++++++++\n 1 file changed, 38 insertions(+)\n\n--\nRegards,\n\nLaurent Pinchart","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 8d5c35782ee3..808e0255b004 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -428,6 +428,44 @@ private:\n \tstd::vector<std::function<void()>> actions_;\n };\n\n+#ifndef __DOXYGEN__\n+template<typename EF>\n+class scope_exit\n+{\n+public:\n+\ttemplate<typename Fn,\n+\t\t std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Fn>>, scope_exit> &&\n+\t\t\t\t  std::is_constructible_v<EF, Fn>> * = nullptr>\n+\texplicit scope_exit(Fn &&fn)\n+\t\t: exitFunction_(std::forward<Fn>(fn))\n+\t{\n+\t\tstatic_assert(std::is_nothrow_constructible_v<EF, Fn>);\n+\t}\n+\n+\tscope_exit(scope_exit &&other) = delete;\n+\tscope_exit(const scope_exit &) = delete;\n+\n+\t~scope_exit()\n+\t{\n+\t\tif (active_)\n+\t\t\texitFunction_();\n+\t}\n+\n+\tvoid release()\n+\t{\n+\t\tactive_ = false;\n+\t}\n+\n+private:\n+\tEF exitFunction_;\n+\tbool active_ = true;\n+};\n+\n+template<typename EF>\n+scope_exit(EF) -> scope_exit<EF>;\n+\n+#endif /* __DOXYGEN__ */\n+\n } /* namespace utils */\n\n #ifndef __DOXYGEN__\n","prefixes":["v3","1/2"]}