Show a patch.

GET /api/patches/17609/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 17609,
    "url": "https://patchwork.libcamera.org/api/patches/17609/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/17609/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20221016210202.1107-2-laurent.pinchart@ideasonboard.com>",
    "date": "2022-10-16T21:02:01",
    "name": "[libcamera-devel,v2,1/2] libcamera: utils: Add ScopeExitActions class",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "e8d032cacbdbc412686b7c0f16ac39a2ada6dee4",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/17609/mbox/",
    "series": [
        {
            "id": 3557,
            "url": "https://patchwork.libcamera.org/api/series/3557/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3557",
            "date": "2022-10-16T21:02:00",
            "name": "libcamera: Simplify error handling with ScopeExitActions class",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/3557/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/17609/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/17609/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 58B94C327C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 16 Oct 2022 21:02:30 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8FD0062DE3;\n\tSun, 16 Oct 2022 23:02:28 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 68DD362DDB\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 16 Oct 2022 23:02:27 +0200 (CEST)",
            "from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EAF13E71;\n\tSun, 16 Oct 2022 23:02:26 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1665954148;\n\tbh=NG9GsThbupy6kW/EZ079LSSHxSrIktbTjoQVTgkBx0E=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=YVUpEIUBzj6mhVMo2zXROtdUI1M6veRgifdJhKm7DukzA45j2sxRvlRzwDiboZyRF\n\tSiXMkWuAfwMh/hy1zmQpNcYLXqmLd/5tJaabW95T7Am7CRFg/pN40rY0oX86WDK+x5\n\tAog59CWkZyN6dvjca3YRidSeyGtQqtSOrhvXe1iq8yjIN024QMFKwlvnx0ywbi7MOj\n\tiM5bh/kUjFjuXu6MCFqlW5l6vyR4mZUg8g+5/sfxTLeWoRkkU/+9hp34G4xei5fXVT\n\tZCbs/YlL6Q0ITG13LfCYDaNxsgILTfQOD7wyM0/x5YmUGYN//QYErKNISFBoBF/ta4\n\twlcc2EomXnumw==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1665954147;\n\tbh=NG9GsThbupy6kW/EZ079LSSHxSrIktbTjoQVTgkBx0E=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=ub1NQ8TqAMOBwMNIVIJFrNS4TO1rQ2fLke6KxJVcqW72QA+OVpUBaReQ/48NG6UjP\n\tnZ1HF9pcaRQCe5ZNUDebZd/8zAYadKcz37jmgtB7NwXSBDJBPN6JoPxQZTbMwhJyq3\n\tTrZcevLTREosAhc6RYzQnlgCWNKOa/ixSdMRL+WI="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"ub1NQ8Tq\"; dkim-atps=neutral",
        "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",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 1/2] libcamera: utils: Add\n\tScopeExitActions class",
        "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>",
        "From": "Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>",
        "Reply-To": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "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>\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 eb7bcdf4c173..2941d9c45232 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@@ -367,6 +368,18 @@ decltype(auto) abs_diff(const T &a, const T &b)\n \t\treturn a - b;\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 6a307940448e..eb91da297c57 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -463,6 +463,138 @@ std::string toAscii(const std::string &str)\n  * \\a b\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 deferred error\n+ * handling 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+ * Deferred error handlers are executed when the ScopeExitActions instance is\n+ * destroyed, 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 a deferred action\n+ * \\param[in] action The action\n+ *\n+ * Add a deferred 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 deferred 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": [
        "libcamera-devel",
        "v2",
        "1/2"
    ]
}