| Message ID | 20260407153427.1825999-27-laurent.pinchart@ideasonboard.com |
|---|---|
| State | Superseded |
| Headers | show |
| Series |
|
| Related | show |
On Thu, Apr 23, 2026 at 03:59:54PM +0200, Barnabás Pőcze wrote: > 2026. 04. 07. 17:34 keltezéssel, Laurent Pinchart írta: > > There will be a need to erase child nodes when implementing support for > > overriding configuration options. Add two erase() functions to the > > ValueNode class, mimicking the add() API. > > > > More erase() overloads could be added, for instance taking iterators as > > parameters, to improve efficiency. This should be considered later, when > > usage of the ValueNode class will expand, based on the actual usage > > patterns. > > > > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> > > --- > > include/libcamera/internal/value_node.h | 3 ++ > > src/libcamera/value_node.cpp | 51 +++++++++++++++++++++++++ > > 2 files changed, 54 insertions(+) > > > > diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h > > index 14c5e0eb0202..4e3757af7d09 100644 > > --- a/include/libcamera/internal/value_node.h > > +++ b/include/libcamera/internal/value_node.h > > @@ -244,6 +244,9 @@ public: > > ValueNode *add(std::initializer_list<std::string_view> path, > > std::unique_ptr<ValueNode> &&child); > > > > + void erase(const std::string &key); > > + void erase(std::initializer_list<std::string_view> path); > > + > > private: > > LIBCAMERA_DISABLE_COPY_AND_MOVE(ValueNode) > > > > diff --git a/src/libcamera/value_node.cpp b/src/libcamera/value_node.cpp > > index 990e46d70358..8a1af13a5d6e 100644 > > --- a/src/libcamera/value_node.cpp > > +++ b/src/libcamera/value_node.cpp > > @@ -621,4 +621,55 @@ ValueNode *ValueNode::add(std::initializer_list<std::string_view> path, > > return node; > > } > > > > +/** > > + * \brief Erase a child node in a dictionary > > + * \param[in] key The dictionary key > > + * > > + * Erase the child node referenced by \a key in a dictionary node. If the \a > > + * key does not exist, or if this node is not a dictionary, the function > > + * returns without performing any operation. > > + */ > > +void ValueNode::erase(const std::string &key) > > +{ > > + auto node = dictionary_.extract(key); > > `extract()` has an overload taking an iterator, I think that would make it > possible to use `std::string_view key` here. Or just `find()` + `erase(it)` at the end, > without any `extract()`. I'll for for find + erase. > > + if (node.empty()) > > + return; > > + > > + for (auto iter = list_.begin(); iter != list_.end(); ++iter) { > > I feel that this loop deserves a "\todo Not ideal algorithm". Ack. > > + if (iter->value.get() != node.mapped()) > > + continue; > > + > > + list_.erase(iter); > > + return; > > + } > > +} > > + > > +/** > > + * \brief Erase the child node at the given path > > + * \param[in] path The path > > + * > > + * Erase the child node at the given \a path. If no child node exists for the > > + * path, the function returns without performing any operation. > > + */ > > +void ValueNode::erase(std::initializer_list<std::string_view> path) > > +{ > > + if (!path.size()) > > + return; > > + > > + ValueNode *node = this; > > + > > + for (const auto [i, name] : utils::enumerate(path)) { > > + if (i == path.size() - 1) { > > + node->erase(std::string{ name }); > > + return; > > + } > > + > > + auto iter = node->dictionary_.find(name); > > + if (iter == node->dictionary_.end()) > > + return; > > + > > + node = iter->second; > > + } > > +} > > + > > } /* namespace libcamera */
diff --git a/include/libcamera/internal/value_node.h b/include/libcamera/internal/value_node.h index 14c5e0eb0202..4e3757af7d09 100644 --- a/include/libcamera/internal/value_node.h +++ b/include/libcamera/internal/value_node.h @@ -244,6 +244,9 @@ public: ValueNode *add(std::initializer_list<std::string_view> path, std::unique_ptr<ValueNode> &&child); + void erase(const std::string &key); + void erase(std::initializer_list<std::string_view> path); + private: LIBCAMERA_DISABLE_COPY_AND_MOVE(ValueNode) diff --git a/src/libcamera/value_node.cpp b/src/libcamera/value_node.cpp index 990e46d70358..8a1af13a5d6e 100644 --- a/src/libcamera/value_node.cpp +++ b/src/libcamera/value_node.cpp @@ -621,4 +621,55 @@ ValueNode *ValueNode::add(std::initializer_list<std::string_view> path, return node; } +/** + * \brief Erase a child node in a dictionary + * \param[in] key The dictionary key + * + * Erase the child node referenced by \a key in a dictionary node. If the \a + * key does not exist, or if this node is not a dictionary, the function + * returns without performing any operation. + */ +void ValueNode::erase(const std::string &key) +{ + auto node = dictionary_.extract(key); + if (node.empty()) + return; + + for (auto iter = list_.begin(); iter != list_.end(); ++iter) { + if (iter->value.get() != node.mapped()) + continue; + + list_.erase(iter); + return; + } +} + +/** + * \brief Erase the child node at the given path + * \param[in] path The path + * + * Erase the child node at the given \a path. If no child node exists for the + * path, the function returns without performing any operation. + */ +void ValueNode::erase(std::initializer_list<std::string_view> path) +{ + if (!path.size()) + return; + + ValueNode *node = this; + + for (const auto [i, name] : utils::enumerate(path)) { + if (i == path.size() - 1) { + node->erase(std::string{ name }); + return; + } + + auto iter = node->dictionary_.find(name); + if (iter == node->dictionary_.end()) + return; + + node = iter->second; + } +} + } /* namespace libcamera */
There will be a need to erase child nodes when implementing support for overriding configuration options. Add two erase() functions to the ValueNode class, mimicking the add() API. More erase() overloads could be added, for instance taking iterators as parameters, to improve efficiency. This should be considered later, when usage of the ValueNode class will expand, based on the actual usage patterns. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> --- include/libcamera/internal/value_node.h | 3 ++ src/libcamera/value_node.cpp | 51 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+)