From patchwork Wed Jul 30 10:27:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 24037 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D3457BDC71 for ; Wed, 30 Jul 2025 10:27:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5EC3569202; Wed, 30 Jul 2025 12:27:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UyCYIvvJ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 112786914D for ; Wed, 30 Jul 2025 12:27:44 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id A2621A57 for ; Wed, 30 Jul 2025 12:27:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1753871220; bh=K4wNkv/sCNUkGZosj3RtG4NFEzhB+ftcy6CoLQRnD3w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UyCYIvvJus/2y/n34H0yZ37f2EWoEHs5fZCfRuBdRphMus440nFHb+rp+leKp6Ap7 lykV+xqpAKN/gohUlONVCm4Rkg+U34kTTGH5t11I150kyfIAYmfTaWfa1LDQCaLuFl cNucpGzKKnZLHOi5x/HA2icwv7HlbeARXnksUc1M= From: Laurent Pinchart 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 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The scope_exit class is an implementation of the identically named C++ library fundamentals TS v3 class, as documented in [1]. It is a simpler version of the libcamera-specific ScopeExitActions class and doesn't require dynamic heap memory allocation, making it more suitable for hot paths. The class is not documented as it implements a C++ standard (even if experimental) API. [1] https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit.html Signed-off-by: Laurent Pinchart Reviewed-by: Barnabás Pőcze --- Changes since v2: - Drop lvalue reference constraint in constructor - Delete move constructor I have named the class scope_exit instead of ScopeExit to match the C++ library fundamentals TS v3 class, and have not documented it as it's meant as an implementation of the standard. It is however not clear if and when scope_exit would become part of a ratified C++ standard, so I'm open to instead create a libcamera-specific ScopeExit class and document it. --- include/libcamera/base/utils.h | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) -- Regards, Laurent Pinchart diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 8d5c35782ee3..808e0255b004 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -428,6 +428,44 @@ private: std::vector> actions_; }; +#ifndef __DOXYGEN__ +template +class scope_exit +{ +public: + template>, scope_exit> && + std::is_constructible_v> * = nullptr> + explicit scope_exit(Fn &&fn) + : exitFunction_(std::forward(fn)) + { + static_assert(std::is_nothrow_constructible_v); + } + + scope_exit(scope_exit &&other) = delete; + scope_exit(const scope_exit &) = delete; + + ~scope_exit() + { + if (active_) + exitFunction_(); + } + + void release() + { + active_ = false; + } + +private: + EF exitFunction_; + bool active_ = true; +}; + +template +scope_exit(EF) -> scope_exit; + +#endif /* __DOXYGEN__ */ + } /* namespace utils */ #ifndef __DOXYGEN__