From 9111f060f5e017c1724a8195804292873426d005 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 8 Aug 2013 12:47:24 +0200 Subject: [PATCH] Shortcut for 'Xml_node::Attribute::value()' Also provide accessor function 'has_attribute'. --- os/include/util/xml_node.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/os/include/util/xml_node.h b/os/include/util/xml_node.h index c330e857b1..9474092f11 100644 --- a/os/include/util/xml_node.h +++ b/os/include/util/xml_node.h @@ -671,6 +671,40 @@ namespace Genode { if (a.has_type(type)) return a; } + + /** + * Shortcut for reading an attribute value from XML node + * + * \param type attribute name + * \param default_value value returned if no attribute with the + * name 'type' is present. + * \return attribute value or specified default value + * + * Without this shortcut, attribute values can be obtained by + * 'node.attribute(...).value(...)' only. Because the attribute + * lookup may throw a 'Nonexistent_attribute' exception, code that + * reads optional attributes (those with default values) has to + * handle the exception accordingly. Such code tends to become + * clumsy, in particular when many attributes are processed in a + * subsequent fashion. This function template relieves the XML node + * user from implementing the exception handling manually. + */ + template + inline T attribute_value(char const *type, T default_value) const + { + T result = default_value; + try { attribute(type).value(&result); } catch (...) { } + return result; + } + + /** + * Return true if attribute of specified type exists + */ + inline bool has_attribute(char const *type) const + { + try { attribute(type); return true; } catch (...) { } + return false; + } }; }