mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-19 11:16:57 +00:00
Shortcut for 'Xml_node::Attribute::value()'
Also provide accessor function 'has_attribute'.
This commit is contained in:
parent
fd95637289
commit
9111f060f5
@ -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 <typename T>
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user