diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h index b51d2f4b..da68a74c 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 6221a22a..33c688ed 100644 --- a/src/libcamera/value_node.cpp +++ b/src/libcamera/value_node.cpp @@ -386,6 +386,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 @@ -421,6 +440,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