From patchwork Sun Oct 16 21:02:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17609 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 58B94C327C for ; Sun, 16 Oct 2022 21:02:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8FD0062DE3; Sun, 16 Oct 2022 23:02:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1665954148; bh=NG9GsThbupy6kW/EZ079LSSHxSrIktbTjoQVTgkBx0E=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=YVUpEIUBzj6mhVMo2zXROtdUI1M6veRgifdJhKm7DukzA45j2sxRvlRzwDiboZyRF SiXMkWuAfwMh/hy1zmQpNcYLXqmLd/5tJaabW95T7Am7CRFg/pN40rY0oX86WDK+x5 Aog59CWkZyN6dvjca3YRidSeyGtQqtSOrhvXe1iq8yjIN024QMFKwlvnx0ywbi7MOj iM5bh/kUjFjuXu6MCFqlW5l6vyR4mZUg8g+5/sfxTLeWoRkkU/+9hp34G4xei5fXVT ZCbs/YlL6Q0ITG13LfCYDaNxsgILTfQOD7wyM0/x5YmUGYN//QYErKNISFBoBF/ta4 wlcc2EomXnumw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 68DD362DDB for ; Sun, 16 Oct 2022 23:02:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ub1NQ8Tq"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EAF13E71; Sun, 16 Oct 2022 23:02:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1665954147; bh=NG9GsThbupy6kW/EZ079LSSHxSrIktbTjoQVTgkBx0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ub1NQ8TqAMOBwMNIVIJFrNS4TO1rQ2fLke6KxJVcqW72QA+OVpUBaReQ/48NG6UjP nZ1HF9pcaRQCe5ZNUDebZd/8zAYadKcz37jmgtB7NwXSBDJBPN6JoPxQZTbMwhJyq3 TrZcevLTREosAhc6RYzQnlgCWNKOa/ixSdMRL+WI= To: libcamera-devel@lists.libcamera.org Date: Mon, 17 Oct 2022 00:02:01 +0300 Message-Id: <20221016210202.1107-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221016210202.1107-1-laurent.pinchart@ideasonboard.com> References: <20221016210202.1107-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/2] libcamera: utils: Add ScopeExitActions class 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The ScopeExitActions class is a simple object that performs user-provided actions upon destruction. It is meant to simplify cleanup tasks in error handling paths. Signed-off-by: Laurent Pinchart Reviewed-by: Xavier Roumegue --- Changes since v1: - Rename to ScopeExitActions - Replace defer() with operator+=() - Replace clear() with release() --- include/libcamera/base/utils.h | 13 ++++ src/libcamera/base/utils.cpp | 132 +++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index eb7bcdf4c173..2941d9c45232 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -367,6 +368,18 @@ decltype(auto) abs_diff(const T &a, const T &b) return a - b; } +class ScopeExitActions +{ +public: + ~ScopeExitActions(); + + void operator+=(std::function &&action); + void release(); + +private: + std::vector> actions_; +}; + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 6a307940448e..eb91da297c57 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -463,6 +463,138 @@ std::string toAscii(const std::string &str) * \a b */ +/** + * \class ScopeExitActions + * \brief An object that performs actions upon destruction + * + * The ScopeExitActions class is a simple object that performs user-provided + * actions upon destruction. It is meant to simplify cleanup tasks in error + * handling paths. + * + * When the code flow performs multiple sequential actions that each need a + * corresponding cleanup action, error handling quickly become tedious: + * + * \code{.cpp} + * { + * int ret = allocateMemory(); + * if (ret) + * return ret; + * + * ret = startProducer(); + * if (ret) { + * freeMemory(); + * return ret; + * } + * + * ret = startConsumer(); + * if (ret) { + * stopProducer(); + * freeMemory(); + * return ret; + * } + * + * return 0; + * } + * \endcode + * + * This is prone to programming mistakes, as cleanup actions can easily be + * forgotten or ordered incorrectly. One strategy to simplify error handling is + * to use goto statements: + * + * \code{.cpp} + * { + * int ret = allocateMemory(); + * if (ret) + * return ret; + * + * ret = startProducer(); + * if (ret) + * goto error_free; + * + * ret = startConsumer(); + * if (ret) + * goto error_stop; + * + * return 0; + * + * error_stop: + * stopProducer(); + * error_free: + * freeMemory(); + * return ret; + * } + * \endcode + * + * While this may be considered better, this solution is still quite + * error-prone. Beside the risk of picking the wrong error label, the error + * handling logic is separated from the normal code flow, which increases the + * risk of error when refactoring the code. Additionally, C++ doesn't allow + * goto statements to jump over local variable declarations, which can make + * usage of this pattern more difficult. + * + * The ScopeExitActions class solves these issues by allowing code that + * requires cleanup actions to be grouped with its corresponding deferred error + * handling code: + * + * \code{.cpp} + * { + * ScopeExitActions actions; + * + * int ret = allocateMemory(); + * if (ret) + * return ret; + * + * actions += [&]() { freeMemory(); }; + * + * ret = startProducer(); + * if (ret) + * return ret; + * + * actions += [&]() { stopProducer(); }; + * + * ret = startConsumer(); + * if (ret) + * return ret; + * + * actions.release(); + * return 0; + * } + * \endcode + * + * Deferred error handlers are executed when the ScopeExitActions instance is + * destroyed, in the reverse order of their addition. + */ + +ScopeExitActions::~ScopeExitActions() +{ + for (const auto &action : utils::reverse(actions_)) + action(); +} + +/** + * \brief Add a deferred action + * \param[in] action The action + * + * Add a deferred action to the ScopeExitActions. Actions will be called upon + * destruction in the reverse order of their addition. + */ +void ScopeExitActions::operator+=(std::function &&action) +{ + actions_.push_back(std::move(action)); +} + +/** + * \brief Remove all deferred actions + * + * This function should be called in scope exit paths that don't need the + * actions to be executed, such as success return paths from a function when + * the ScopeExitActions is used for error cleanup. + */ +void ScopeExitActions::release() +{ + actions_.clear(); +} + } /* namespace utils */ #ifndef __DOXYGEN__