Add a get method that doesn't throw, but returns a default argument instead

This commit is contained in:
fbraem 2013-02-19 22:38:00 +01:00
parent a39b33b3e9
commit fb94876de2

View File

@ -141,6 +141,32 @@ public:
}
}
template<typename T>
T get(const std::string& name, const T& def) const
/// Returns the element with the given name and tries to convert
/// it to the template type. When the element is not found, or
/// has the wrong type, the def argument will be returned.
{
Element::Ptr element = get(name);
if ( element.isNull() )
{
return def;
}
if ( ElementTraits<T>::TypeId == element->type() )
{
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
if ( concrete != NULL )
{
return concrete->value();
}
}
return def;
}
Element::Ptr get(const std::string& name) const;
/// Returns the element with the given name.
/// An empty element will be returned when the element is not found.