From patchwork Thu Apr 23 23:00:29 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 26529 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 4ABA1BDCB5 for ; Thu, 23 Apr 2026 23:01:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 463A962F72; Fri, 24 Apr 2026 01:01:16 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VAT97Ofa"; 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 7DD9862F6E for ; Fri, 24 Apr 2026 01:01:10 +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 F2E02802 for ; Fri, 24 Apr 2026 00:59:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1776985171; bh=8Dvn0nleUiwPSWalrMfXsahKemTcaEBSCwQKPDOQBbY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VAT97Ofa4TafhyiUZj60p+nP3LnrruxbPuZCAJ2ruVSZbN0UKEwjYjrr3VDiamAfZ gDEP7rk4WRQbaISS+EWTZFLj+oOWGExuJ31z+xKStfqqUop7KwZOaV3ArrGiF6DMpP QytRp2pKtVTnATaiBuSysQFMHuX2lCgsrW1oH3UE= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v3 07/37] libcamera: yaml_parser: Add functions to add children Date: Fri, 24 Apr 2026 02:00:29 +0300 Message-ID: <20260423230059.3180987-8-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260423230059.3180987-1-laurent.pinchart@ideasonboard.com> References: <20260423230059.3180987-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 YamlObject::add() functions to add children to a list or dictionary object. This will be used by the YamlParserContext to replace direct access to YamlObject member variables to decouple the two classes. Signed-off-by: Laurent Pinchart Reviewed-by: Isaac Scott Reviewed-by: Barnabás Pőcze --- Changes since v2: - Fix typo in documentation Changes since v1: - Avoid std::map lookup before insertion in YamlObject::add() - Don't move child node if add fails --- include/libcamera/internal/yaml_parser.h | 5 ++- src/libcamera/yaml_parser.cpp | 55 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 0666762308ac..3be61c503c88 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -28,7 +28,7 @@ class YamlObject { private: struct Value { - Value(std::string &&k, std::unique_ptr &&v) + Value(std::string k, std::unique_ptr &&v) : key(std::move(k)), value(std::move(v)) { } @@ -197,6 +197,9 @@ public: bool contains(std::string_view key) const; const YamlObject &operator[](std::string_view key) const; + YamlObject *add(std::unique_ptr &&child); + YamlObject *add(std::string key, std::unique_ptr &&child); + private: LIBCAMERA_DISABLE_COPY_AND_MOVE(YamlObject) diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 8c84f560ff0f..5b2e743ee41a 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -436,6 +436,61 @@ const YamlObject &YamlObject::operator[](std::string_view key) const return *iter->second; } +/** + * \brief Add a child object to a list + * \param[in] child The child object + * + * Append the \a child object as the last element of this object's children + * list. This object must be empty, in which case it is converted to the + * Type::List type, or be a list. Otherwise, the function returns a nullptr and + * the \a child is not modified. + * + * \return A pointer to the child object if successfully added, nullptr + * otherwise + */ +YamlObject *YamlObject::add(std::unique_ptr &&child) +{ + if (type_ == Type::Empty) + type_ = Type::List; + + if (type_ != Type::List) + return nullptr; + + Value &elem = list_.emplace_back(std::string{}, std::move(child)); + return elem.value.get(); +} + +/** + * \brief Add a child object to a dictionary + * \param[in] key The dictionary key + * \param[in] child The child object + * + * Add the \a child object with the given \a key to this object's children. This + * object must be empty, in which case it is converted to the Type::Dictionary + * type, or be a dictionary. Otherwise, the function returns a nullptr and the + * \a child is not modified. + * + * Keys are unique. If a child with the same \a key already exists, the function + * returns a nullptr and the \a child is not modified. + * + * \return A pointer to the child object if successfully added, nullptr + * otherwise + */ +YamlObject *YamlObject::add(std::string key, std::unique_ptr &&child) +{ + if (type_ == Type::Empty) + type_ = Type::Dictionary; + + if (type_ != Type::Dictionary) + return nullptr; + + auto [it, inserted] = dictionary_.try_emplace(std::move(key), child.get()); + if (!inserted) + return nullptr; + + return list_.emplace_back(it->first, std::move(child)).value.get(); +} + #ifndef __DOXYGEN__ class YamlParserContext