From patchwork Tue Apr 7 15:34:08 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 26463 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 4804EC3307 for ; Tue, 7 Apr 2026 15:35:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 98EFE62E0E; Tue, 7 Apr 2026 17:35:05 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="M6DXkGC4"; 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 4686F62DD4 for ; Tue, 7 Apr 2026 17:35:00 +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 AAC2278E for ; Tue, 7 Apr 2026 17:33:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1775576012; bh=JqgS7hrHDzNYfjW9RzfFDyQAkTr40KmcOSJvIKKsprQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=M6DXkGC42+zdKVZ2Aqo8rcoh03dfM1iaZ/P7rQBajiDLxnJ/X9CsdOZgdKgq7hekQ 1sar4kxi4tUDQJyew0IMQFOIfeBtmIi9HBJU0DhSGcHyiiejhgSg6GSAYtJ4a/BBIQ r0EjiEB9NQua/8foMXNIcXAz6fIefjoR5fN0ANbQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 23/42] libcamera: value_node: Add mutable children accessors Date: Tue, 7 Apr 2026 18:34:08 +0300 Message-ID: <20260407153427.1825999-24-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 two at() functions that return mutable pointer to child nodes, based on an index for lists and a key for dictionaries. The API differs from const children accessors that use operator[] and return a reference in order to allow better error handling: while the const accessors return a reference to a global instance of an empty ValueNode when the requested child doesn't exist, a mutable accessor can't do the same without allowing modifying the empty global instance. Signed-off-by: Laurent Pinchart Reviewed-by: Barnabás Pőcze --- Changes since v1: - Improve documentation - Fix typo in commit message --- include/libcamera/internal/value_node.h | 2 ++ src/libcamera/value_node.cpp | 42 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h index 3bdb537e9eab..d237b2c1e0d6 100644 --- a/include/libcamera/internal/value_node.h +++ b/include/libcamera/internal/value_node.h @@ -230,9 +230,11 @@ public: ConstDictAdapter asDict() const { return ConstDictAdapter{ list_ }; } ConstListAdapter asList() const { return ConstListAdapter{ list_ }; } + ValueNode *at(std::size_t index); const ValueNode &operator[](std::size_t index) const; bool contains(std::string_view key) const; + ValueNode *at(std::string_view key); const ValueNode &operator[](std::string_view key) const; ValueNode *add(std::unique_ptr &&child); diff --git a/src/libcamera/value_node.cpp b/src/libcamera/value_node.cpp index 5aef72cd29a4..4c3a5e4d381d 100644 --- a/src/libcamera/value_node.cpp +++ b/src/libcamera/value_node.cpp @@ -384,6 +384,25 @@ template struct ValueNode::Accessor>; * \return An adapter of unspecified type compatible with range-based for loops */ +/** + * \brief Retrieve the element from list ValueNode by index + * \param[in] index The element index + * + * This function retrieves an element of the ValueNode. Only ValueNode + * instances of List type associate elements with an index, calling this + * function on other types of instances or with an invalid index returns a null + * pointer. + * + * \return The ValueNode corresponding to \a index + */ +ValueNode *ValueNode::at(std::size_t index) +{ + if (type_ != Type::List || index >= size()) + return nullptr; + + return list_[index].value.get(); +} + /** * \brief Retrieve the element from list ValueNode by index * \param[in] index The element index @@ -419,6 +438,29 @@ bool ValueNode::contains(std::string_view key) const return dictionary_.find(key) != dictionary_.end(); } +/** + * \brief Retrieve a member by key from the dictionary + * \param[in] key The element key + * + * This function retrieves a member of a ValueNode by \a key. Only ValueNode + * instances of Dictionary type associate elements with keys, calling this + * function on other types of instances or with a nonexistent key returns a null + * pointer. + * + * \return The ValueNode corresponding to the \a key member + */ +ValueNode *ValueNode::at(std::string_view key) +{ + if (type_ != Type::Dictionary) + return nullptr; + + auto iter = dictionary_.find(key); + if (iter == dictionary_.end()) + return nullptr; + + return iter->second; +} + /** * \brief Retrieve a member by key from the dictionary * \param[in] key The element key