From patchwork Wed Jul 27 22:21: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: 16849 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 1BF59BE173 for ; Wed, 27 Jul 2022 22:21:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CBC6763319; Thu, 28 Jul 2022 00:21:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658960516; bh=PIigXf3EPXf2Qc0qZP4ZxnCRf5lsUJAuydOX16cXtI8=; 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=eZmLmI4hVrjAV7Hjwj2MRdS7acM0Vv1tfN4oK3ouRnlIBpJMl200GD//lhbd3reWQ N6JDRbr8p8VqqTMfVrC3yYc4d09Lg2QXmWP4Kcomj4W7Sa11W/WtXt0BYqnMo0XS8n P0MiJSsRkGLUyNeYLGS/FGkdsG6T0HKrQW7QhDceFCHcJwluFtxx8krsJvyOQuF472 zUMnuquKgEtpzboAgVf9qGkzGME6blsVH4Fr+DOULZMOAmdra5BlPGGBnB9Zo3mYtr O0njocyOh5G8W4Edg6bSoOfBJBvxf8z2BdLOVWnTu4fCHedJHA3pNz1xq09FQ/fA8S 4PN7YQqegjNXw== 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 6F0B66330E for ; Thu, 28 Jul 2022 00:21:53 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="saFVdasB"; 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 0646A6D4; Thu, 28 Jul 2022 00:21:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1658960513; bh=PIigXf3EPXf2Qc0qZP4ZxnCRf5lsUJAuydOX16cXtI8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=saFVdasBrKeYxWcbRugLsydqshybywhgJarJqPxTs6tInfxw0ykN2YEOKEnhQFJEX Lzu1hURqcb+MoixEkIeUVRX3uxZ4nNWYGcFjv6N49EdFbZ1jwIhVOv3hj67NRfxU/G 8j0jMDg572ymb+X/yWS41fOd8zm03WDz2UQmy/gc= 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 Subject: [libcamera-devel] [PATCH v5 1/9] libcamera: base: utils: Provide defopt to simplify std::optional::value_or() usage 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 std::optional::value_or(U &&default_value) function returns the contained value if available, or \a default_value if the std::optional has no value. If the desired default value is a default-constructed T, the obvious option is to call std::optional::value_or(T{}). This approach has two drawbacks: - The \a default_value T{} is constructed even if the std::optional instance has a value, which impacts efficiency. - The T{} default constructor needs to be spelled out explicitly in the value_or() call, leading to long lines if the type is complex. Introduce a defopt variable that solves these issues by providing a value that can be passed to std::optional::value_or() and get implicitly converted to a default-constructed T. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Jacopo Mondi Reviewed-by: Florian Sylvestre --- include/libcamera/base/utils.h | 14 +++++++++ src/libcamera/base/utils.cpp | 21 +++++++++++++ test/utils.cpp | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index cfff05836de7..889bb4a2270e 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -361,6 +361,20 @@ decltype(auto) abs_diff(const T &a, const T &b) return a - b; } +namespace details { + +struct defopt_t { + template + operator T() const + { + return T{}; + } +}; + +} /* namespace details */ + +constexpr details::defopt_t defopt; + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 6a307940448e..9cd6cb197243 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -463,6 +463,27 @@ std::string toAscii(const std::string &str) * \a b */ +/** + * \var defopt + * \brief Constant used with std::optional::value_or() to create a + * default-constructed object + * + * The std::optional::value_or(U &&default_value) function returns the + * contained value if available, or \a default_value if the std::optional has no + * value. If the desired default value is a default-constructed T, the obvious + * option is to call std::optional::value_or(T{}). This approach has two + * drawbacks: + * + * * The \a default_value T{} is constructed even if the std::optional instance + * has a value, which impacts efficiency. + * * The T{} default constructor needs to be spelled out explicitly in the + * value_or() call, leading to long lines if the type is complex. + * + * The defopt variable solves these issues by providing a value that can be + * passed to std::optional::value_or() and get implicitly converted to a + * default-constructed T. + */ + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/test/utils.cpp b/test/utils.cpp index d65467b5102c..129807a63ec6 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -169,6 +170,55 @@ protected: return TestPass; } + int testDefopt() + { + static bool defaultConstructed = false; + + struct ValueType { + ValueType() + : value_(-1) + { + defaultConstructed = true; + } + + ValueType(int value) + : value_(value) + { + } + + int value_; + }; + + /* + * Test that utils::defopt doesn't cause default-construction + * of a ValueType instance when value_or(utils::defopt) is + * called on a std::optional that has a value. + */ + std::optional opt = ValueType(0); + ValueType value = opt.value_or(utils::defopt); + + if (defaultConstructed || value.value_ != 0) { + std::cerr << "utils::defopt didn't prevent default construction" + << std::endl; + return TestFail; + } + + /* + * Then test that the ValueType is correctly default-constructed + * when the std::optional has no value. + */ + opt = std::nullopt; + value = opt.value_or(utils::defopt); + + if (!defaultConstructed || value.value_ != -1) { + std::cerr << "utils::defopt didn't cause default construction" + << std::endl; + return TestFail; + } + + return TestPass; + } + int run() { /* utils::hex() test. */ @@ -281,6 +331,10 @@ protected: if (testDuration() != TestPass) return TestFail; + /* utils::defopt test. */ + if (testDefopt() != TestPass) + return TestFail; + return TestPass; } };