os: add 'Xml_node::for_each_sub_node'

The new function template simplifies the common case of iterating
through the sub nodes of an XML node.
This commit is contained in:
Norman Feske 2014-09-30 17:55:01 +02:00
parent 3a1ecdd5a7
commit 13bce287ad
1 changed files with 29 additions and 0 deletions

View File

@ -656,6 +656,35 @@ namespace Genode {
throw Nonexistent_sub_node();
}
/**
* Execute functor 'fn' for each sub node of specified type
*/
template <typename FN>
void for_each_sub_node(char const *type, FN const &fn)
{
if (_num_sub_nodes == 0)
return;
Xml_node node = sub_node();
for (int i = 0; ; node = node.next()) {
if (!type || node.has_type(type))
fn(node);
if (++i == _num_sub_nodes)
break;
}
}
/**
* Execute functor 'fn' for each sub node
*/
template <typename FN>
void for_each_sub_node(FN const &fn)
{
for_each_sub_node(nullptr, fn);
}
/**
* Return Nth attribute of XML node
*