[v2,25/42] libcamera: value_node: Support looking up descendant node by path
diff mbox series

Message ID 20260407153427.1825999-26-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • libcamera: Global configuration file improvements
Related show

Commit Message

Laurent Pinchart April 7, 2026, 3:34 p.m. UTC
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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
Changes since v1:

- Use reference in range-based for loop
- Improve commit message
---
 include/libcamera/internal/value_node.h |  1 +
 src/libcamera/value_node.cpp            | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h
index be28c2b73832..14c5e0eb0202 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<std::string_view> path) const;
 
 	ValueNode *add(std::unique_ptr<ValueNode> &&child);
 	ValueNode *add(std::string key, std::unique_ptr<ValueNode> &&child);
diff --git a/src/libcamera/value_node.cpp b/src/libcamera/value_node.cpp
index e8db7ef3c37f..990e46d70358 100644
--- a/src/libcamera/value_node.cpp
+++ b/src/libcamera/value_node.cpp
@@ -488,6 +488,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<std::string_view> 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