From 59d59bb9f9a71998ebdc828b1bdfac1cf90cd0b7 Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Thu, 10 Aug 2017 20:32:25 +0300 Subject: [PATCH] Add config::remove_children() The function removes children for which the provided predicate returns true. I didn't need it in the end, but it may still be useful for someone else. --- src/config.cpp | 22 ++++++++++++++++++++++ src/config.hpp | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/src/config.cpp b/src/config.cpp index a7376def550c..6ebf2b1a0103 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -586,6 +586,28 @@ void config::remove_child(config_key_type key, unsigned index) remove_child(i, index); } +void config::remove_children(config_key_type key, std::function p) +{ + check_valid(); + + child_map::iterator pos = children_.find(key); + if(pos == children_.end()) { + return; + } + + const auto predicate = [p](const std::unique_ptr& child) + { + return p(*child); + }; + + auto child_it = std::find_if(pos->second.begin(), pos->second.end(), predicate); + while(child_it != pos->second.end()) { + unsigned index = child_it - pos->second.begin(); + remove_child(pos, index); + child_it = std::find_if(pos->second.begin() + index, pos->second.end(), predicate); + } +} + const config::attribute_value& config::operator[](config_key_type key) const { check_valid(); diff --git a/src/config.hpp b/src/config.hpp index 63f1107688c7..23410d0106c5 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -515,6 +516,10 @@ class config void splice_children(config &src, const std::string &key); void remove_child(config_key_type key, unsigned index); + /** + * Removes all children with tag @a key for which @a p returns true. + */ + void remove_children(config_key_type key, std::function p); void recursive_clear_value(config_key_type key); void clear();