From patchwork Mon May 4 14:06:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 26613 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 14599BE173 for ; Mon, 4 May 2026 14:06:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 03E0E6301E; Mon, 4 May 2026 16:06:50 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="d0w7nFsQ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7942C62FE1 for ; Mon, 4 May 2026 16:06:48 +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 61CC69C for ; Mon, 4 May 2026 16:06:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1777903606; bh=SkItsZtMvz7VeKwdXrsX2EfAExmQuiHqekk1gRj3phI=; h=From:To:Subject:Date:From; b=d0w7nFsQABXENa+ead1q3huSpEp5lhElNOBSIUvQEbiclT3jelN9rUZmkMYlHMNih A0V109KqC61YJ4xrHSf689fYCgy4ncgnEvyfd9Vnl+jSPCuRJEAvI7DHdLj0TksuIF u8HBJPQDEBsXRc2JJWJbQzQSOb8PbC3syaiKHuio= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 1/2] libcamera: base: Restore defopt Date: Mon, 4 May 2026 17:06:45 +0300 Message-ID: <20260504140646.1455101-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" 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. This reverts commit e0e54965df015b954d75ebe945221372f2dffb80, with a few extra changes on top: - Make defopt_t::operator T() constexpr - Rework the documentation to drop rationale invalidated by https://cplusplus.github.io/LWG/issue3886 Signed-off-by: Laurent Pinchart Reviewed-by: Barnabás Pőcze Reviewed-by: Kieran Bingham --- Changes since v1: - Update commit message - Make defopt_t::operator T() constexpr - Rework the documentation to drop rationale invalidated by https://cplusplus.github.io/LWG/issue3886 --- include/libcamera/base/utils.h | 15 ++++++++++ src/libcamera/base/utils.cpp | 17 +++++++++++ test/utils.cpp | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) base-commit: 183e37362f57ff3ce7493abf0bc6f1b57b931f55 diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index b33a4c644a87..9835fd7980dc 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -430,6 +430,21 @@ scope_exit(EF) -> scope_exit; #endif /* __DOXYGEN__ */ +namespace details { + +struct defopt_t { + template + constexpr operator T() const + { + static_assert(std::is_default_constructible_v); + 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..eca3efddb2e3 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -685,6 +685,23 @@ 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 however + * constructs the \a default_value T{} even if the std::optional instance has a + * value, which impacts efficiency. + * + * The defopt variable solves this issue 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; } };