From patchwork Wed Jun 12 22:46:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 20271 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 81793C3237 for ; Wed, 12 Jun 2024 22:46:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3439265469; Thu, 13 Jun 2024 00:46:21 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="EUuVTQqI"; dkim-atps=neutral Received: from mail-40135.protonmail.ch (mail-40135.protonmail.ch [185.70.40.135]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9BD3A65446 for ; Thu, 13 Jun 2024 00:46:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1718232378; x=1718491578; bh=6/49QokyRxShOovNVxjF1Bb5VGTnzs7Zzqk7ZY1gFBg=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=EUuVTQqI+IoV52ThZRo4wA64V2ukfNwmZ1YZyemr6ac4x/4oYL6veIR/oOLG3iDTB OrKaO71ne9pe3ChoEc1dtM13MotzzBPEVo8NreGoe4thCAhY5GUyG2/a9qS5e7kG2W smzxBQHbY1jisjmt7UUknmJ/c42VhOTtYdyoNmHwH54omnMSOnL8fuPtc44GTpW2cR HyRT7yoTGRSsimVTgT7m1TQE0JQBnGi8v/ESJi8Zt6R0DFLUKek6U/ZSU1EEgadr2X 8Jj1P4pdJsPgvMPue/SkI/0yXugqRM7QIBaNG5bH9puOPAK7vpZVNWl9l47x7YOPMP CwSJ6nbsmFBNg== Date: Wed, 12 Jun 2024 22:46:16 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v2] libcamera: yaml_parser: Make default value templated in `get()` Message-ID: <20240612224614.35807-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: c55b582ca106d76cfd2ebbe2670407e86fbb28c9 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 way the construction of the default value of type `T` can be delayed until it is really needed, which is useful, for example when `T == std::string` and the default value comes from a string literal, as the default value string would always be constructed otherwise, even if not needed. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- Changes in v2: * update documentation --- include/libcamera/internal/yaml_parser.h | 6 +++--- src/libcamera/yaml_parser.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) -- 2.45.2 diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index b6979d73..3ac27e06 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -179,10 +179,10 @@ public: #endif std::optional get() const; - template - T get(const T &defaultValue) const + template + T get(U &&defaultValue) const { - return get().value_or(defaultValue); + return get().value_or(std::forward(defaultValue)); } #ifndef __DOXYGEN__ diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index aac9a2bd..b68a44c1 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -104,7 +104,7 @@ std::size_t YamlObject::size() const */ /** - * \fn template YamlObject::get(const T &defaultValue) const + * \fn template YamlObject::get(U &&defaultValue) const * \brief Parse the YamlObject as a \a T value * \param[in] defaultValue The default value when failing to parse *