From bb1f1f1da913426bea7104a2e62a1b572f8f4250 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 28 Oct 2025 17:56:44 +0200 Subject: [PATCH] libcamera: value_node: Support looking up descendant node by path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Looking up a descendant node based on a path is a common operation. Add a helper function to do so, to avoid loops in the callers. Signed-off-by: Laurent Pinchart Reviewed-by: Barnabás Pőcze Reviewed-by: Isaac Scott --- include/libcamera/internal/value_node.h | 1 + src/libcamera/value_node.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h index 57841319..ccae69e2 100644 --- a/include/libcamera/internal/value_node.h +++ b/include/libcamera/internal/value_node.h @@ -237,6 +237,7 @@ public: bool contains(std::string_view key) const; ValueNode *at(std::string_view key); const ValueNode &operator[](std::string_view key) const; + const ValueNode &operator[](std::initializer_list path) const; ValueNode *add(std::unique_ptr &&child); ValueNode *add(std::string key, std::unique_ptr &&child); diff --git a/src/libcamera/value_node.cpp b/src/libcamera/value_node.cpp index 0f02b3c1..e1c0c37a 100644 --- a/src/libcamera/value_node.cpp +++ b/src/libcamera/value_node.cpp @@ -490,6 +490,29 @@ const ValueNode &ValueNode::operator[](std::string_view key) const return *iter->second; } +/** + * \brief Retrieve a descendant node by path + * \param[in] path The path + * + * This function retrieves a descendant of a ValueNode by following a \a path. + * The path is a list of keys that index nested dictionary nodes. If any node + * along the path is not a Dictionary node, an empty node is returned. + * + * \return The ValueNode corresponding to the \a path + */ +const ValueNode &ValueNode::operator[](std::initializer_list path) const +{ + const ValueNode *node = this; + + for (const auto &part : path) { + node = &(*node)[part]; + if (!*node) + return empty; + } + + return *node; +} + /** * \brief Add a child node to a list * \param[in] child The child node