Show a patch.

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

{
    "id": 17521,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/17521/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/17521/",
    "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": "<20221004021842.6345-2-laurent.pinchart@ideasonboard.com>",
    "date": "2022-10-04T02:18:41",
    "name": "[libcamera-devel,1/2] libcamera: utils: Add Cleaner class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "268332e787a0d427c7de7cc2606d7375596bd10d",
    "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/17521/mbox/",
    "series": [
        {
            "id": 3530,
            "url": "https://patchwork.libcamera.org/api/1.1/series/3530/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3530",
            "date": "2022-10-04T02:18:40",
            "name": "libcamera: Simplify error handling with Cleaner class",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/3530/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/17521/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/17521/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 A82A2BD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  4 Oct 2022 02:18:49 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 06086603F6;\n\tTue,  4 Oct 2022 04:18:49 +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 1D7F860A80\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  4 Oct 2022 04:18:47 +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 8E6069DE;\n\tTue,  4 Oct 2022 04:18:46 +0200 (CEST)"
        ],
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664849929;\n\tbh=SB+AJ2CkDH9OEJ8ZJmpt/2McIbaZoAaVLuFzPdgXrWE=;\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=Fxsfw7cI8WYrEfQZ+wke5SeTw5ZFfgQVTYsB76eXTuZn6a/8MofOv4efCH7zaXeJ/\n\tQQJP1uCkl0SJR+Wl7O5Z2z4kTyAgRyplb3RD67Lx3dSqtE9DqOIDy2ib4rekQvzHbA\n\tQX9jKE0AgG7AoXn9T4R9b2dYcmcHKnOCyB2CRHMA7EsXA4oVf1PQMzOmwvcvHN5uJj\n\tUNpx97Zn2RN9I8/4pIzLp47Q5WDWsKPMcANXjUsIr6fvV/sjcmCeyMcAJ469glOU+0\n\tMcEZL7HrmgnyckZzrm5y7lJQd6xb2MIzOBXik6fUu9JP37+w1tnx1deBddLBBI3p8K\n\tY5mQhG9R1oeMg==",
            "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1664849926;\n\tbh=SB+AJ2CkDH9OEJ8ZJmpt/2McIbaZoAaVLuFzPdgXrWE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=u/7MqreVyeGNfpBXWbDAk2/A02oLTdi2elKKXp7xlxdVWTD9pGe3cg7IpURYaLvor\n\t/CUPD6dZsE+emQYOjLdHFRzfR9HowTQUcVLDMPEd+whf7nlIUwWqBcxlLmF1ASZobv\n\tLk0wKuOAaODMBBVGdR07MywPMKZwmr1SVC8Njd/s="
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"u/7MqreV\"; dkim-atps=neutral",
        "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",
        "Content-Transfer-Encoding": "8bit",
        "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": "<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 Cleaner class is a simple object that performs user-provided actions\nupon destruction. As its name implies, it is meant to simplify cleanup\ntasks in error handling paths.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/utils.h |  13 ++++\n src/libcamera/base/utils.cpp   | 130 +++++++++++++++++++++++++++++++++\n 2 files changed, 143 insertions(+)",
    "diff": "diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex ed88b7163770..0de80eb89d15 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@@ -381,6 +382,18 @@ struct defopt_t {\n \n constexpr details::defopt_t defopt;\n \n+class Cleaner\n+{\n+public:\n+\t~Cleaner();\n+\n+\tvoid defer(std::function<void()> &&action);\n+\tvoid clear();\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 9cd6cb197243..45e115b7031e 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -484,6 +484,136 @@ std::string toAscii(const std::string &str)\n  * default-constructed T.\n  */\n \n+/**\n+ * \\class Cleaner\n+ * \\brief An object that performs actions upon destruction\n+ *\n+ * The Cleaner class is a simple object that performs user-provided actions\n+ * upon destruction. As its name implies, it is meant to simplify cleanup tasks\n+ * in error 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 = startProducder();\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 = startProducder();\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 Cleaner class solves these issues by allowing code that requires cleanup\n+ * actions to be grouped with its corresponding deferred error handling code:\n+ *\n+ * \\code{.cpp}\n+ * {\n+ * \tCleaner cleaner;\n+ *\n+ * \tint ret = allocateMemory();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tcleaner.defer([&]() { freeMemory(); });\n+ *\n+ * \tret = startProducder();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tcleaner.defer([&]() { stopProducer(); });\n+ *\n+ * \tret = startConsumer();\n+ * \tif (ret)\n+ * \t\treturn ret;\n+ *\n+ * \tcleaner.clear();\n+ * \treturn 0;\n+ * }\n+ * \\endcode\n+ *\n+ * Deferred error handlers are executed when the Cleaner instance is destroyed,\n+ * in the reverse order of their addition.\n+ */\n+\n+Cleaner::~Cleaner()\n+{\n+\tfor (const auto &action : utils::reverse(actions_))\n+\t\taction();\n+}\n+\n+/**\n+ * \\brief Add a deferred cleanup action\n+ * \\param[in] action The cleanup action\n+ *\n+ * Add a deferred cleanup action to the Cleaner. Cleanup actions will be called\n+ * upon destruction in the reverse order of their addition.\n+ */\n+void Cleaner::defer(std::function<void()> &&action)\n+{\n+\tactions_.push_back(std::move(action));\n+}\n+\n+/**\n+ * \\brief Clear all deferred cleanup actions\n+ *\n+ * This function should be called in the success path to clear all deferred\n+ * error cleanup actions.\n+ */\n+void Cleaner::clear()\n+{\n+\tactions_.clear();\n+}\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\n",
    "prefixes": [
        "libcamera-devel",
        "1/2"
    ]
}