{"id":24029,"url":"https://patchwork.libcamera.org/api/patches/24029/?format=json","web_url":"https://patchwork.libcamera.org/patch/24029/","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":"<20250729162101.13836-2-laurent.pinchart@ideasonboard.com>","date":"2025-07-29T16:21:00","name":"[v2,1/2] libcamera: utils: Add scope_exit class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"a5b6c9f63768454f7f998a4154985adfeb25e449","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/24029/mbox/","series":[{"id":5348,"url":"https://patchwork.libcamera.org/api/series/5348/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5348","date":"2025-07-29T16:20:59","name":"libcamera: Introduce and use scope_exit","version":2,"mbox":"https://patchwork.libcamera.org/series/5348/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/24029/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/24029/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 C8718BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 29 Jul 2025 16:21:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1BDA1691F2;\n\tTue, 29 Jul 2025 18:21:15 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E37D3691E6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 29 Jul 2025 18:21:11 +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 0E4226DC;\n\tTue, 29 Jul 2025 18:20:28 +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=\"PnLuO7Eo\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1753806029;\n\tbh=fELHw0T9n6rIt/jQQQnILmMTd+goY2x7E88yVNA6LEs=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=PnLuO7EosRnW/wcKys4ExaNQKE82ZH3KJ4pjpE/ZyllJKNjzWp6xfSv7T5bF+zMnk\n\tBp7YcFhf4cp05vWgCPaT06jE2lTMTU+wSaMEvRJbB/o5x+0QBh3aARC9+94B3tsuR5\n\tWjcOw9/wbrdh6YC0idPE4kM8I1zENz28zQXoTN/c=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Han-Lin Chen <hanlinchen@chromium.org>,\n\tHarvey Yang <chenghaoyang@chromium.org>","Subject":"[PATCH v2 1/2] libcamera: utils: Add scope_exit class","Date":"Tue, 29 Jul 2025 19:21:00 +0300","Message-ID":"<20250729162101.13836-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.49.1","In-Reply-To":"<20250729162101.13836-1-laurent.pinchart@ideasonboard.com>","References":"<20250729162101.13836-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---\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 | 45 ++++++++++++++++++++++++++++++++++\n 1 file changed, 45 insertions(+)\n\n--\nRegards,\n\nLaurent Pinchart","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 8d5c35782ee3..1d8e5aef9b54 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -428,6 +428,51 @@ 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> &&\n+\t\t\t\t  !std::is_lvalue_reference_v<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)\n+\t\t: exitFunction_(std::move(other.exitFunction_)),\n+\t\t  active_(other.active_)\n+\t{\n+\t\tother.release();\n+\t}\n+\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":["v2","1/2"]}