From patchwork Sun May 3 22:53:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 26601 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 E2144BDCB5 for ; Sun, 3 May 2026 22:53:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8DE326301E; Mon, 4 May 2026 00:53:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VvYhfTVH"; dkim-atps=neutral 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 665ED62E6A for ; Mon, 4 May 2026 00:53:17 +0200 (CEST) Received: from killaraus.ideasonboard.com (2001-14ba-703d-e500--2a1.rev.dnainternet.fi [IPv6:2001:14ba:703d:e500::2a1]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DC6CD8F for ; Mon, 4 May 2026 00:53:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1777848796; bh=t6Pnk5UKKlPk0mSN71pmAeChkQEcKYGI14uFCTpVWWE=; h=From:To:Subject:Date:From; b=VvYhfTVH43ymNeQRsflawLwiKkxQSw1ha+hPFXMtlWofIlmbaEpfqaV5fEQSLjKGJ ik+EGo4bisxZKQtE7Y0kCBPFQNfQSlBibhFaoEtA02g5/JS1lJd1lSYOKSD8HIMKmu fGqGbjkwRwzbWvuau5jC6qZ7pQVOc61NQarC07gs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 1/2] Revert "libcamera: base: utils: Drop defopt" Date: Mon, 4 May 2026 01:53:14 +0300 Message-ID: <20260503225315.1272813-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This reverts commit e0e54965df015b954d75ebe945221372f2dffb80. utils::defopt was removed due to compilation errors with gcc 8 that were deemed impossible to work around. Now that libcamera has dropped support for gcc 8, restore the utility. Signed-off-by: Laurent Pinchart Reviewed-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- include/libcamera/base/utils.h | 14 +++++++++ src/libcamera/base/utils.cpp | 21 ++++++++++++++ test/utils.cpp | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) base-commit: 183e37362f57ff3ce7493abf0bc6f1b57b931f55 diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index b33a4c644a87..c962b2a3ba8e 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -430,6 +430,20 @@ scope_exit(EF) -> scope_exit; #endif /* __DOXYGEN__ */ +namespace details { + +struct defopt_t { + template + operator T() const + { + return T{}; + } +}; + +} /* namespace details */ + +constexpr details::defopt_t defopt; + #ifndef __DOXYGEN__ std::ostream &operator<<(std::ostream &os, const Duration &d); #endif diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 4ab2bd863e11..7d66547cd2a5 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -685,6 +685,27 @@ void ScopeExitActions::release() actions_.clear(); } +/** + * \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. + */ + #ifndef __DOXYGEN__ std::ostream &operator<<(std::ostream &os, const Duration &d) { diff --git a/test/utils.cpp b/test/utils.cpp index b5ce94e5e912..eba3ed9fbf43 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -178,6 +178,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. */ @@ -332,6 +381,10 @@ protected: if (testDuration() != TestPass) return TestFail; + /* utils::defopt test. */ + if (testDefopt() != TestPass) + return TestFail; + return TestPass; } };