Patch Detail
Show a patch.
GET /api/1.1/patches/20759/?format=api
{ "id": 20759, "url": "https://patchwork.libcamera.org/api/1.1/patches/20759/?format=api", "web_url": "https://patchwork.libcamera.org/patch/20759/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240804153106.22167-2-laurent.pinchart@ideasonboard.com>", "date": "2024-08-04T15:31:05", "name": "[v3,1/2] libcamera: utils: Add ScopeExitActions class", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "cd3bdf53695c17687a0a545dccc9ee22b919a3af", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/20759/mbox/", "series": [ { "id": 4482, "url": "https://patchwork.libcamera.org/api/1.1/series/4482/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4482", "date": "2024-08-04T15:31:04", "name": "libcamera: Simplify error handling with ScopeExitActions class", "version": 3, "mbox": "https://patchwork.libcamera.org/series/4482/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/20759/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/20759/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 C5AC0C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 4 Aug 2024 15:31:33 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5E03563385;\n\tSun, 4 Aug 2024 17:31:31 +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 41A526195E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 4 Aug 2024 17:31:30 +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 ESMTPSA id 65B121BA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 4 Aug 2024 17:30:39 +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=\"cdbK7NBo\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1722785439;\n\tbh=R+GOanGW5/bMQAkPJJK1gUQdEya0hpEBmmdRoccSpv8=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=cdbK7NBoX43Pg76l93bOAIlDq5yjCxWndeaX3UoZsNFiqj/8028vcFgKudGbSy10m\n\tP6ZkhmZ6QAtY7hi6SA+63Y1Uok0NfS2rzoby489aDu6LQXA3C5q3AJ7pup7u08IizJ\n\ttyqN7BAvRYSf08QdUS4FPsKI8E4mis2PNffV/52M=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Subject": "[PATCH v3 1/2] libcamera: utils: Add ScopeExitActions class", "Date": "Sun, 4 Aug 2024 18:31:05 +0300", "Message-ID": "<20240804153106.22167-2-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.44.2", "In-Reply-To": "<20240804153106.22167-1-laurent.pinchart@ideasonboard.com>", "References": "<20240804153106.22167-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 ScopeExitActions class is a simple object that performs\nuser-provided actions upon destruction. It is meant to simplify cleanup\ntasks in error handling paths.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Xavier Roumegue <xavier.roumegue@oss.nxp.com>\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n---\nChanges since v2:\n\n- Reaplace the last references to \"defer\"\n\nChanges since v1:\n\n- Rename to ScopeExitActions\n- Replace defer() with operator+=()\n- Replace clear() with release()\n---\n include/libcamera/base/utils.h | 13 ++++\n src/libcamera/base/utils.cpp | 132 +++++++++++++++++++++++++++++++++\n 2 files changed, 145 insertions(+)", "diff": "diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 734ff81e2860..b87f9b0ade7a 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -9,6 +9,7 @@\n \n #include <algorithm>\n #include <chrono>\n+#include <functional>\n #include <iterator>\n #include <memory>\n #include <ostream>\n@@ -399,6 +400,18 @@ constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept\n \treturn static_cast<std::underlying_type_t<Enum>>(e);\n }\n \n+class ScopeExitActions\n+{\n+public:\n+\t~ScopeExitActions();\n+\n+\tvoid operator+=(std::function<void()> &&action);\n+\tvoid release();\n+\n+private:\n+\tstd::vector<std::function<void()>> actions_;\n+};\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\nindex ccb31063b922..67e5a8964680 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -531,6 +531,138 @@ double strtod(const char *__restrict nptr, char **__restrict endptr)\n * \\return The value of e converted to its underlying type\n */\n \n+/**\n+ * \\class ScopeExitActions\n+ * \\brief An object that performs actions upon destruction\n+ *\n+ * The ScopeExitActions class is a simple object that performs user-provided\n+ * actions upon destruction. It is meant to simplify cleanup tasks in error\n+ * handling paths.\n+ *\n+ * When the code flow performs multiple sequential actions that each need a\n+ * corresponding cleanup action, error handling quickly become tedious:\n+ *\n+ * \\code{.cpp}\n+ * {\n+ * \tint ret = allocateMemory();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tret = startProducer();\n+ * \tif (ret) {\n+ * \t\tfreeMemory();\n+ * \t\treturn ret;\n+ * \t}\n+ *\n+ * \tret = startConsumer();\n+ * \tif (ret) {\n+ * \t\tstopProducer();\n+ * \t\tfreeMemory();\n+ * \t\treturn ret;\n+ * \t}\n+ *\n+ * \treturn 0;\n+ * }\n+ * \\endcode\n+ *\n+ * This is prone to programming mistakes, as cleanup actions can easily be\n+ * forgotten or ordered incorrectly. One strategy to simplify error handling is\n+ * to use goto statements:\n+ *\n+ * \\code{.cpp}\n+ * {\n+ * \tint ret = allocateMemory();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tret = startProducer();\n+ * \tif (ret)\n+ * \t\tgoto error_free;\n+ *\n+ * \tret = startConsumer();\n+ * \tif (ret)\n+ * \t\tgoto error_stop;\n+ *\n+ * \treturn 0;\n+ *\n+ * error_stop:\n+ * \tstopProducer();\n+ * error_free:\n+ * \tfreeMemory();\n+ * \treturn ret;\n+ * }\n+ * \\endcode\n+ *\n+ * While this may be considered better, this solution is still quite\n+ * error-prone. Beside the risk of picking the wrong error label, the error\n+ * handling logic is separated from the normal code flow, which increases the\n+ * risk of error when refactoring the code. Additionally, C++ doesn't allow\n+ * goto statements to jump over local variable declarations, which can make\n+ * usage of this pattern more difficult.\n+ *\n+ * The ScopeExitActions class solves these issues by allowing code that\n+ * requires cleanup actions to be grouped with its corresponding error handling\n+ * code:\n+ *\n+ * \\code{.cpp}\n+ * {\n+ * \tScopeExitActions actions;\n+ *\n+ * \tint ret = allocateMemory();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tactions += [&]() { freeMemory(); };\n+ *\n+ * \tret = startProducer();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tactions += [&]() { stopProducer(); };\n+ *\n+ * \tret = startConsumer();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tactions.release();\n+ * \treturn 0;\n+ * }\n+ * \\endcode\n+ *\n+ * Error handlers are executed when the ScopeExitActions instance is destroyed,\n+ * in the reverse order of their addition.\n+ */\n+\n+ScopeExitActions::~ScopeExitActions()\n+{\n+\tfor (const auto &action : utils::reverse(actions_))\n+\t\taction();\n+}\n+\n+/**\n+ * \\brief Add an exit action\n+ * \\param[in] action The action\n+ *\n+ * Add an exit action to the ScopeExitActions. Actions will be called upon\n+ * destruction in the reverse order of their addition.\n+ */\n+void ScopeExitActions::operator+=(std::function<void()> &&action)\n+{\n+\tactions_.push_back(std::move(action));\n+}\n+\n+/**\n+ * \\brief Remove all exit actions\n+ *\n+ * This function should be called in scope exit paths that don't need the\n+ * actions to be executed, such as success return paths from a function when\n+ * the ScopeExitActions is used for error cleanup.\n+ */\n+void ScopeExitActions::release()\n+{\n+\tactions_.clear();\n+}\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\n", "prefixes": [ "v3", "1/2" ] }