mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-13 10:32:57 +01:00
Add a get method that doesn't throw, but returns a default argument instead
This commit is contained in:
parent
a39b33b3e9
commit
fb94876de2
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user