From patchwork Tue Apr 7 15:33:57 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 26452 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 9BC35C32FC for ; Tue, 7 Apr 2026 15:34:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BBC1D62DE0; Tue, 7 Apr 2026 17:34:48 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="HZMsb4XP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D3C4262DA2 for ; Tue, 7 Apr 2026 17:34:44 +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 UTF8SMTPSA id 243C4596 for ; Tue, 7 Apr 2026 17:33:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1775575997; bh=7v9XADkIcESoBajheMtbeQniMG8/jRV6V+0ZQ5dVybk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=HZMsb4XPhih9F0lKgfGYwtVzQwH7JMXknu1cIFakhzfjB3cfa9N+TCRkoKsgO25gi WM54SNB1YaZ02qqPuzBA+WK5Isd9GPzht21nTgCkFkjAFGYJhYjGgRGeb/fKPRVC3t dGz7tfkuj4e8yotyQj2mVVtdqvOhxUH4wCZqyhao= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 12/42] libcamera: yaml_parser: Add function to set a YamlObject value Date: Tue, 7 Apr 2026 18:33:57 +0300 Message-ID: <20260407153427.1825999-13-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260407153427.1825999-1-laurent.pinchart@ideasonboard.com> References: <20260407153427.1825999-1-laurent.pinchart@ideasonboard.com> 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" Add a YamlObject::set() function to set the value of an object, with specializations for scalar types. Signed-off-by: Laurent Pinchart --- Changes since v1: - Remove cv in addition to reference in YamlObject::set() --- include/libcamera/internal/yaml_parser.h | 8 ++++ src/libcamera/yaml_parser.cpp | 59 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 7953befe11e2..0666762308ac 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -182,6 +182,13 @@ public: return get().value_or(std::forward(defaultValue)); } + template + void set(T &&value) + { + return Accessor>>{} + .set(*this, std::forward(value)); + } + DictAdapter asDict() const { return DictAdapter{ list_ }; } ListAdapter asList() const { return ListAdapter{ list_ }; } @@ -207,6 +214,7 @@ private: template struct Accessor { std::optional get(const YamlObject &obj) const; + void set(YamlObject &obj, T value); }; Type type_; diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 399e7529385f..3119ab41d89e 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -137,6 +137,20 @@ std::size_t YamlObject::size() const * \return The YamlObject value, or \a defaultValue if parsing failed */ +/** + * \fn template YamlObject::set(T &&value) + * \brief Set the value of a YamlObject + * \param[in] value The value + * + * This function sets the value stored in a YamlObject to \a value. The value is + * converted to a string in an implementation-specific way that guarantees that + * subsequent calls to get() will return the same value. + * + * Values can only be set on YamlObject of Type::Value type or empty YamlObject. + * Attempting to set a value on an object of type Type::Dict or Type::List does + * not modify the YamlObject. + */ + #ifndef __DOXYGEN__ template<> @@ -154,6 +168,16 @@ YamlObject::Accessor::get(const YamlObject &obj) const return std::nullopt; } +template<> +void YamlObject::Accessor::set(YamlObject &obj, bool value) +{ + if (obj.type_ != Type::Empty && obj.type_ != Type::Value) + return; + + obj.type_ = Type::Value; + obj.value_ = value ? "true" : "false"; +} + template struct YamlObject::Accessor || @@ -178,6 +202,15 @@ struct YamlObject::Accessor; @@ -194,6 +227,12 @@ YamlObject::Accessor::get(const YamlObject &obj) const return obj.get(); } +template<> +void YamlObject::Accessor::set(YamlObject &obj, float value) +{ + obj.set(std::forward(value)); +} + template<> std::optional YamlObject::Accessor::get(const YamlObject &obj) const @@ -215,6 +254,16 @@ YamlObject::Accessor::get(const YamlObject &obj) const return value; } +template<> +void YamlObject::Accessor::set(YamlObject &obj, double value) +{ + if (obj.type_ != Type::Empty && obj.type_ != Type::Value) + return; + + obj.type_ = Type::Value; + obj.value_ = std::to_string(value); +} + template<> std::optional YamlObject::Accessor::get(const YamlObject &obj) const @@ -225,6 +274,16 @@ YamlObject::Accessor::get(const YamlObject &obj) const return obj.value_; } +template<> +void YamlObject::Accessor::set(YamlObject &obj, std::string value) +{ + if (obj.type_ != Type::Empty && obj.type_ != Type::Value) + return; + + obj.type_ = Type::Value; + obj.value_ = std::move(value); +} + template<> std::optional YamlObject::Accessor::get(const YamlObject &obj) const