{"id":16849,"url":"https://patchwork.libcamera.org/api/patches/16849/?format=json","web_url":"https://patchwork.libcamera.org/patch/16849/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220727222149.30627-2-laurent.pinchart@ideasonboard.com>","date":"2022-07-27T22:21:41","name":"[libcamera-devel,v5,1/9] libcamera: base: utils: Provide defopt to simplify std::optional::value_or() usage","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d24c54ac8be4e6c16579877be0b7ba229a8803c9","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16849/mbox/","series":[{"id":3338,"url":"https://patchwork.libcamera.org/api/series/3338/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3338","date":"2022-07-27T22:21:40","name":"ipa: rkisp1: Add GSL, LSC, DPCC, Filter and CProc algorithms","version":5,"mbox":"https://patchwork.libcamera.org/series/3338/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16849/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16849/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 1BF59BE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Jul 2022 22:21:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CBC6763319;\n\tThu, 28 Jul 2022 00:21:56 +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 6F0B66330E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 28 Jul 2022 00:21:53 +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 0646A6D4;\n\tThu, 28 Jul 2022 00:21:52 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658960516;\n\tbh=PIigXf3EPXf2Qc0qZP4ZxnCRf5lsUJAuydOX16cXtI8=;\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=eZmLmI4hVrjAV7Hjwj2MRdS7acM0Vv1tfN4oK3ouRnlIBpJMl200GD//lhbd3reWQ\n\tN6JDRbr8p8VqqTMfVrC3yYc4d09Lg2QXmWP4Kcomj4W7Sa11W/WtXt0BYqnMo0XS8n\n\tP0MiJSsRkGLUyNeYLGS/FGkdsG6T0HKrQW7QhDceFCHcJwluFtxx8krsJvyOQuF472\n\tzUMnuquKgEtpzboAgVf9qGkzGME6blsVH4Fr+DOULZMOAmdra5BlPGGBnB9Zo3mYtr\n\tO0njocyOh5G8W4Edg6bSoOfBJBvxf8z2BdLOVWnTu4fCHedJHA3pNz1xq09FQ/fA8S\n\t4PN7YQqegjNXw==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1658960513;\n\tbh=PIigXf3EPXf2Qc0qZP4ZxnCRf5lsUJAuydOX16cXtI8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=saFVdasBrKeYxWcbRugLsydqshybywhgJarJqPxTs6tInfxw0ykN2YEOKEnhQFJEX\n\tLzu1hURqcb+MoixEkIeUVRX3uxZ4nNWYGcFjv6N49EdFbZ1jwIhVOv3hj67NRfxU/G\n\t8j0jMDg572ymb+X/yWS41fOd8zm03WDz2UQmy/gc="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"saFVdasB\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 28 Jul 2022 01:21:41 +0300","Message-Id":"<20220727222149.30627-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220727222149.30627-1-laurent.pinchart@ideasonboard.com>","References":"<20220727222149.30627-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 1/9] libcamera: base: utils: Provide\n\tdefopt to simplify std::optional::value_or() usage","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 std::optional<T>::value_or(U &&default_value) function returns the\ncontained value if available, or \\a default_value if the std::optional\nhas no value. If the desired default value is a default-constructed T,\nthe obvious option is to call std::optional<T>::value_or(T{}). This\napproach has two drawbacks:\n\n- The \\a default_value T{} is constructed even if the std::optional\n  instance has a value, which impacts efficiency.\n- The T{} default constructor needs to be spelled out explicitly in the\n  value_or() call, leading to long lines if the type is complex.\n\nIntroduce a defopt variable that solves these issues by providing a\nvalue that can be passed to std::optional<T>::value_or() and get\nimplicitly converted to a default-constructed T.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/utils.h | 14 +++++++++\n src/libcamera/base/utils.cpp   | 21 +++++++++++++\n test/utils.cpp                 | 54 ++++++++++++++++++++++++++++++++++\n 3 files changed, 89 insertions(+)","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex cfff05836de7..889bb4a2270e 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -361,6 +361,20 @@ decltype(auto) abs_diff(const T &a, const T &b)\n \t\treturn a - b;\n }\n \n+namespace details {\n+\n+struct defopt_t {\n+\ttemplate<typename T>\n+\toperator T() const\n+\t{\n+\t\treturn T{};\n+\t}\n+};\n+\n+} /* namespace details */\n+\n+constexpr details::defopt_t defopt;\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\nindex 6a307940448e..9cd6cb197243 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -463,6 +463,27 @@ std::string toAscii(const std::string &str)\n  * \\a b\n  */\n \n+/**\n+ * \\var defopt\n+ * \\brief Constant used with std::optional::value_or() to create a\n+ * default-constructed object\n+ *\n+ * The std::optional<T>::value_or(U &&default_value) function returns the\n+ * contained value if available, or \\a default_value if the std::optional has no\n+ * value. If the desired default value is a default-constructed T, the obvious\n+ * option is to call std::optional<T>::value_or(T{}). This approach has two\n+ * drawbacks:\n+ *\n+ * * The \\a default_value T{} is constructed even if the std::optional instance\n+ *   has a value, which impacts efficiency.\n+ * * The T{} default constructor needs to be spelled out explicitly in the\n+ *   value_or() call, leading to long lines if the type is complex.\n+ *\n+ * The defopt variable solves these issues by providing a value that can be\n+ * passed to std::optional<T>::value_or() and get implicitly converted to a\n+ * default-constructed T.\n+ */\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/test/utils.cpp b/test/utils.cpp\nindex d65467b5102c..129807a63ec6 100644\n--- a/test/utils.cpp\n+++ b/test/utils.cpp\n@@ -7,6 +7,7 @@\n \n #include <iostream>\n #include <map>\n+#include <optional>\n #include <sstream>\n #include <string>\n #include <vector>\n@@ -169,6 +170,55 @@ protected:\n \t\treturn TestPass;\n \t}\n \n+\tint testDefopt()\n+\t{\n+\t\tstatic bool defaultConstructed = false;\n+\n+\t\tstruct ValueType {\n+\t\t\tValueType()\n+\t\t\t\t: value_(-1)\n+\t\t\t{\n+\t\t\t\tdefaultConstructed = true;\n+\t\t\t}\n+\n+\t\t\tValueType(int value)\n+\t\t\t\t: value_(value)\n+\t\t\t{\n+\t\t\t}\n+\n+\t\t\tint value_;\n+\t\t};\n+\n+\t\t/*\n+\t\t * Test that utils::defopt doesn't cause default-construction\n+\t\t * of a ValueType instance when value_or(utils::defopt) is\n+\t\t * called on a std::optional that has a value.\n+\t\t */\n+\t\tstd::optional<ValueType> opt = ValueType(0);\n+\t\tValueType value = opt.value_or(utils::defopt);\n+\n+\t\tif (defaultConstructed || value.value_ != 0) {\n+\t\t\tstd::cerr << \"utils::defopt didn't prevent default construction\"\n+\t\t\t\t  << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\t/*\n+\t\t * Then test that the ValueType is correctly default-constructed\n+\t\t * when the std::optional has no value.\n+\t\t */\n+\t\topt = std::nullopt;\n+\t\tvalue = opt.value_or(utils::defopt);\n+\n+\t\tif (!defaultConstructed || value.value_ != -1) {\n+\t\t\tstd::cerr << \"utils::defopt didn't cause default construction\"\n+\t\t\t\t  << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n \tint run()\n \t{\n \t\t/* utils::hex() test. */\n@@ -281,6 +331,10 @@ protected:\n \t\tif (testDuration() != TestPass)\n \t\t\treturn TestFail;\n \n+\t\t/* utils::defopt test. */\n+\t\tif (testDefopt() != TestPass)\n+\t\t\treturn TestFail;\n+\n \t\treturn TestPass;\n \t}\n };\n","prefixes":["libcamera-devel","v5","1/9"]}