From patchwork Tue Oct 4 02:18:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17521 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 A82A2BD16B for ; Tue, 4 Oct 2022 02:18:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 06086603F6; Tue, 4 Oct 2022 04:18:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1664849929; bh=SB+AJ2CkDH9OEJ8ZJmpt/2McIbaZoAaVLuFzPdgXrWE=; 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=Fxsfw7cI8WYrEfQZ+wke5SeTw5ZFfgQVTYsB76eXTuZn6a/8MofOv4efCH7zaXeJ/ QQJP1uCkl0SJR+Wl7O5Z2z4kTyAgRyplb3RD67Lx3dSqtE9DqOIDy2ib4rekQvzHbA QX9jKE0AgG7AoXn9T4R9b2dYcmcHKnOCyB2CRHMA7EsXA4oVf1PQMzOmwvcvHN5uJj UNpx97Zn2RN9I8/4pIzLp47Q5WDWsKPMcANXjUsIr6fvV/sjcmCeyMcAJ469glOU+0 McEZL7HrmgnyckZzrm5y7lJQd6xb2MIzOBXik6fUu9JP37+w1tnx1deBddLBBI3p8K Y5mQhG9R1oeMg== 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 1D7F860A80 for ; Tue, 4 Oct 2022 04:18:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="u/7MqreV"; 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 8E6069DE; Tue, 4 Oct 2022 04:18:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1664849926; bh=SB+AJ2CkDH9OEJ8ZJmpt/2McIbaZoAaVLuFzPdgXrWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u/7MqreVyeGNfpBXWbDAk2/A02oLTdi2elKKXp7xlxdVWTD9pGe3cg7IpURYaLvor /CUPD6dZsE+emQYOjLdHFRzfR9HowTQUcVLDMPEd+whf7nlIUwWqBcxlLmF1ASZobv Lk0wKuOAaODMBBVGdR07MywPMKZwmr1SVC8Njd/s= To: libcamera-devel@lists.libcamera.org Date: Tue, 4 Oct 2022 05:18:41 +0300 Message-Id: <20221004021842.6345-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221004021842.6345-1-laurent.pinchart@ideasonboard.com> References: <20221004021842.6345-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] libcamera: utils: Add Cleaner 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 Cleaner class is a simple object that performs user-provided actions upon destruction. As its name implies, it is meant to simplify cleanup tasks in error handling paths. Signed-off-by: Laurent Pinchart Reviewed-by: Xavier Roumegue Reviewed-by: Paul Elder --- include/libcamera/base/utils.h | 13 ++++ src/libcamera/base/utils.cpp | 130 +++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index ed88b7163770..0de80eb89d15 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -381,6 +382,18 @@ struct defopt_t { constexpr details::defopt_t defopt; +class Cleaner +{ +public: + ~Cleaner(); + + void defer(std::function &&action); + void clear(); + +private: + std::vector> actions_; +}; + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 9cd6cb197243..45e115b7031e 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -484,6 +484,136 @@ std::string toAscii(const std::string &str) * default-constructed T. */ +/** + * \class Cleaner + * \brief An object that performs actions upon destruction + * + * The Cleaner class is a simple object that performs user-provided actions + * upon destruction. As its name implies, 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 = startProducder(); + * 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 = startProducder(); + * 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 Cleaner class solves these issues by allowing code that requires cleanup + * actions to be grouped with its corresponding deferred error handling code: + * + * \code{.cpp} + * { + * Cleaner cleaner; + * + * int ret = allocateMemory(); + * if (ret) + * return ret; + * + * cleaner.defer([&]() { freeMemory(); }); + * + * ret = startProducder(); + * if (ret) + * return ret; + * + * cleaner.defer([&]() { stopProducer(); }); + * + * ret = startConsumer(); + * if (ret) + * return ret; + * + * cleaner.clear(); + * return 0; + * } + * \endcode + * + * Deferred error handlers are executed when the Cleaner instance is destroyed, + * in the reverse order of their addition. + */ + +Cleaner::~Cleaner() +{ + for (const auto &action : utils::reverse(actions_)) + action(); +} + +/** + * \brief Add a deferred cleanup action + * \param[in] action The cleanup action + * + * Add a deferred cleanup action to the Cleaner. Cleanup actions will be called + * upon destruction in the reverse order of their addition. + */ +void Cleaner::defer(std::function &&action) +{ + actions_.push_back(std::move(action)); +} + +/** + * \brief Clear all deferred cleanup actions + * + * This function should be called in the success path to clear all deferred + * error cleanup actions. + */ +void Cleaner::clear() +{ + actions_.clear(); +} + } /* namespace utils */ #ifndef __DOXYGEN__