added Node::first() and Node::last() to get the first or last child

This commit is contained in:
Marc Wäckerlin
2015-05-08 22:00:16 +00:00
parent ad7d4cddeb
commit 72be1818f1
3 changed files with 51 additions and 0 deletions

View File

@@ -813,6 +813,10 @@ namespace xml {
const throw(attribute_not_available);
const Attributes& attributes() const throw();
Attributes& attributes() throw();
const Node& first() const throw(out_of_range);
Node& first() throw(out_of_range);
const Node& last() const throw(out_of_range);
Node& last() throw(out_of_range);
Node& limits(size_type min=0, size_type max=0) throw();
List list(const std::string& name) const throw();
bool operator()(const std::string& child) const throw();

View File

@@ -455,6 +455,38 @@ namespace xml {
Attributes& Node::attributes() throw() {
return _attributes;
}
//! Get the first child node
/*! Returns the first child node or throws an exception, if there are
no children. */
Node& Node::first() throw(out_of_range) {
Contents::iterator it(_contents.begin());
if (it==_contents.end()) throw out_of_range(*this, 0);
return **it;
}
//! Get the first child node
/*! Returns the first child node or throws an exception, if there are
no children. */
const Node& Node::first() const throw(out_of_range) {
Contents::const_iterator it(_contents.begin());
if (it==_contents.end()) throw out_of_range(*this, 0);
return **it;
}
//! Get the last child node
/*! Returns the last child node or throws an exception, if there are
no children. */
const Node& Node::last() const throw(out_of_range) {
Contents::const_reverse_iterator it(_contents.rbegin());
if (it==_contents.rend()) throw out_of_range(*this, 0);
return **it;
}
//! Get the last child node
/*! Returns the last child node or throws an exception, if there are
no children. */
Node& Node::last() throw(out_of_range) {
Contents::reverse_iterator it(_contents.rbegin());
if (it==_contents.rend()) throw out_of_range(*this, 0);
return **it;
}
//! Pass a minimal and maximal number for this node in a xml file.
/*! Minimal and maximal values are verified when you use the node as
a template in a xml::Factory. When the factory reads a stucture