From e5b3af9c0538ecaeb3461a92eb629837a4f2549f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 8 Feb 2019 16:51:54 +0100 Subject: [PATCH] ENH: add failsafe version of Enum::get() --- src/OpenFOAM/primitives/enums/Enum.C | 20 +++++++++++++++++++- src/OpenFOAM/primitives/enums/Enum.H | 6 +++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index f74a393a56..1e269ff4ed 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -77,6 +77,24 @@ EnumType Foam::Enum::get(const word& enumName) const } +template +EnumType Foam::Enum::get +( + const word& enumName, + const EnumType defaultValue +) const +{ + const label idx = find(enumName); + + if (idx < 0) + { + return defaultValue; + } + + return EnumType(vals_[idx]); +} + + template EnumType Foam::Enum::read(Istream& is) const { diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index 25b73632e6..b5ef2a3624 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -130,6 +130,10 @@ public: // FatalError if not found. 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. // Return an empty word if not found. inline const word& get(const EnumType e) const;