Skip to content

Commit

Permalink
Add config::remove_children()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jyrkive committed Aug 10, 2017
1 parent 47ddc37 commit 59d59bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/config.cpp
Expand Up @@ -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<bool(const config&)> p)
{
check_valid();

child_map::iterator pos = children_.find(key);
if(pos == children_.end()) {
return;
}

const auto predicate = [p](const std::unique_ptr<config>& 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();
Expand Down
5 changes: 5 additions & 0 deletions src/config.hpp
Expand Up @@ -31,6 +31,7 @@

#include <climits>
#include <ctime>
#include <functional>
#include <iosfwd>
#include <iterator>
#include <map>
Expand Down Expand Up @@ -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<bool(const config&)> p);
void recursive_clear_value(config_key_type key);

void clear();
Expand Down

0 comments on commit 59d59bb

Please sign in to comment.