ENH: add failsafe version of Enum::get()

This commit is contained in:
Mark Olesen
2019-02-08 16:51:54 +01:00
committed by Andrew Heather
parent 27c58dde4c
commit e5b3af9c05
2 changed files with 24 additions and 2 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -77,6 +77,24 @@ EnumType Foam::Enum<EnumType>::get(const word& enumName) const
} }
template<class EnumType>
EnumType Foam::Enum<EnumType>::get
(
const word& enumName,
const EnumType defaultValue
) const
{
const label idx = find(enumName);
if (idx < 0)
{
return defaultValue;
}
return EnumType(vals_[idx]);
}
template<class EnumType> template<class EnumType>
EnumType Foam::Enum<EnumType>::read(Istream& is) const EnumType Foam::Enum<EnumType>::read(Istream& is) const
{ {

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -130,6 +130,10 @@ public:
// FatalError if not found. // FatalError if not found.
EnumType get(const word& enumName) const; EnumType get(const word& enumName) const;
//- The enumeration corresponding to the given name.
// \return The enumeration or default if not found.
EnumType get(const word& enumName, const EnumType defaultValue) const;
//- The name corresponding to the given enumeration. //- The name corresponding to the given enumeration.
// Return an empty word if not found. // Return an empty word if not found.
inline const word& get(const EnumType e) const; inline const word& get(const EnumType e) const;