From fb4971644ffb7387069ab9d19cf9634a6cb0d202 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 29 May 2017 10:30:55 +0200
Subject: [PATCH] ENH: cleanup of NamedEnum

- Remove the unused enums() method since it delivers wholly unreliable
  results. It is not guaranteed to cover the full enumeration range,
  but only the listed names.

- Remove the unused strings() method.
  Duplicated functionality of the words(), but was never used.

- Change access of words() method from static to object.
  Better code isolation. Permits the constructor to take over
  as the single point of failure for bad input.

- Add values() method

- do not expose internal (HashTable) lookup since it makes it more
  difficult to enforce constness and the implementation detail should
  not be exposed. However leave toc() and sortedToc() for the interface.

STYLE: relocated NamedEnum under primitives (was containers)

- internal typedef as 'value_type' for some consistency with STL conventions
---
 applications/test/NamedEnum/Test-NamedEnum.C  |  85 ++++------
 .../DelaunayMeshToolsTemplates.C              |  21 +--
 .../preProcessing/mapFieldsPar/mapFieldsPar.C |   2 +-
 .../enums}/NamedEnum.C                        | 158 +++++++++++-------
 .../enums}/NamedEnum.H                        | 111 +++++++-----
 src/OpenFOAM/primitives/enums/NamedEnumI.H    |  81 +++++++++
 .../temperatureCoupledBase.C                  |  10 +-
 src/conversion/fire/FIREMeshReader.C          |   2 +-
 src/conversion/fire/FIREMeshWriter.C          |   2 +-
 .../functionObjects/volRegion/volRegion.C     |   2 +-
 .../surfaceFieldValue/surfaceFieldValue.C     |   5 +-
 .../field/mapFields/mapFields.C               |   6 +-
 .../utilities/writeObjects/writeObjects.C     |   3 +-
 src/fvOptions/cellSetOption/cellSetOption.C   |   6 +-
 ...irectionalPressureGradientExplicitSource.C |   2 +-
 .../derived/rotorDiskSource/rotorDiskSource.C |   3 +-
 .../mappedPolyPatch/mappedPatchBase.C         |   4 +-
 .../energyRegionCoupledFvPatchScalarField.C   |   2 +-
 .../boundaryRadiationPropertiesPatch.C        |   7 +-
 19 files changed, 311 insertions(+), 201 deletions(-)
 rename src/OpenFOAM/{containers/NamedEnum => primitives/enums}/NamedEnum.C (58%)
 rename src/OpenFOAM/{containers/NamedEnum => primitives/enums}/NamedEnum.H (66%)
 create mode 100644 src/OpenFOAM/primitives/enums/NamedEnumI.H

diff --git a/applications/test/NamedEnum/Test-NamedEnum.C b/applications/test/NamedEnum/Test-NamedEnum.C
index 079938b6b79..4fc20d182c2 100644
--- a/applications/test/NamedEnum/Test-NamedEnum.C
+++ b/applications/test/NamedEnum/Test-NamedEnum.C
@@ -34,15 +34,15 @@ class namedEnumTest
 {
 public:
 
-    enum option
+    enum class option
     {
-        a,
-        b,
-        c,
-        d
+        A,
+        B,
+        C,
+        D
     };
 
-    static const Foam::NamedEnum<option, 4> namedEnum;
+    static const Foam::NamedEnum<option, 4> optionNamed;
 };
 
 
@@ -52,10 +52,10 @@ const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
     "a",
     "b",
     "c",
-    "d"
+    "d",
 };
 
-const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::namedEnum;
+const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::optionNamed;
 
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@@ -69,56 +69,43 @@ int main(int argc, char *argv[])
     dictionary testDict;
     testDict.add("lookup1", "c");
 
-    Info<< "enums: " << options << nl;
-
-    Info<< "loop over enums (as list):" << nl;
-    forAll(options, i)
-    {
-        const namedEnumTest::option& opt = options[i];
-
-        Info<< "option[" << opt
-            << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl;
-    }
-
-    Info<< "loop over enums (C++11 for range):" << nl;
-    for (const auto& opt : options)
-    {
-        Info<< "option[" << opt
-            << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl;
-    }
-
     Info<< nl
-        << namedEnumTest::namedEnum["a"] << nl
-        << namedEnumTest::namedEnum[namedEnumTest::a] << nl;
+        << int(namedEnumTest::optionNamed["a"]) << nl
+        << namedEnumTest::optionNamed[namedEnumTest::option::A] << nl;
 
     Info<< "--- test dictionary lookup ---" << endl;
     {
         Info<< "dict: " << testDict << endl;
 
-        namedEnumTest::option gotOpt =
-            namedEnumTest::namedEnum.lookupOrDefault
-            (
-                "test",
-                testDict,
-                namedEnumTest::option::a
-            );
-
-        Info<< "got: " << gotOpt << endl;
-
-        gotOpt = namedEnumTest::namedEnum.lookupOrDefault
-        (
-            "lookup1",
-            testDict,
-            namedEnumTest::option::a
-        );
-
-        Info<< "got: " << gotOpt << endl;
+        Info<< "got: "
+            <<  int
+                (
+                    namedEnumTest::optionNamed.lookupOrDefault
+                    (
+                        "notFound",
+                        testDict,
+                        namedEnumTest::option::A
+                    )
+                )
+            << nl;
+
+        Info<< "got: "
+            <<  int
+                (
+                    namedEnumTest::optionNamed.lookupOrDefault
+                    (
+                        "lookup1",
+                        testDict,
+                        namedEnumTest::option::A
+                    )
+                )
+            << nl;
     }
 
-    Info<< "--- test read construction ---" << endl;
+    Info<< "--- test read ---" << endl;
 
-    namedEnumTest::option dummy(namedEnumTest::namedEnum.read(Sin));
-    Info<< namedEnumTest::namedEnum[dummy] << endl;
+    namedEnumTest::option dummy(namedEnumTest::optionNamed.read(Sin));
+    Info<< namedEnumTest::optionNamed[dummy] << endl;
 
     Info<< "End\n" << endl;
 
diff --git a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMeshTools/DelaunayMeshToolsTemplates.C b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMeshTools/DelaunayMeshToolsTemplates.C
index ed6818dd39b..da9f4ce3ed0 100644
--- a/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMeshTools/DelaunayMeshToolsTemplates.C
+++ b/applications/utilities/mesh/generation/foamyMesh/conformalVoronoiMesh/DelaunayMeshTools/DelaunayMeshToolsTemplates.C
@@ -44,22 +44,9 @@ void Foam::DelaunayMeshTools::writeOBJ
     OFstream str(fName);
 
     Pout<< nl
-        << "Writing points of types:" << nl;
-
-    forAllConstIter
-    (
-        HashTable<int>,
-        indexedVertexEnum::vertexTypeNames_,
-        iter
-    )
-    {
-        if (iter() >= startPointType && iter() <= endPointType)
-        {
-            Pout<< "    " << iter.key() << nl;
-        }
-    }
-
-    Pout<< "to " << str.name() << endl;
+        << "Writing points of types ("
+        << int(startPointType) << "-" << int(endPointType)
+        << ") to " << str.name() << endl;
 
     for
     (
@@ -265,7 +252,7 @@ void Foam::DelaunayMeshTools::drawDelaunayCell
         << "f " << 1 + offset << " " << 4 + offset << " " << 3 + offset << nl
         << "f " << 1 + offset << " " << 2 + offset << " " << 4 + offset << endl;
 
-//    os  << "# cicumcentre " << endl;
+//    os  << "# circumcentre " << endl;
 
 //    meshTools::writeOBJ(os, c->dual());
 
diff --git a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C
index 43caab7dcc4..d6df8f1a168 100644
--- a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C
+++ b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C
@@ -229,7 +229,7 @@ int main(int argc, char *argv[])
 
 
     word patchMapMethod;
-    if (meshToMesh::interpolationMethodNames_.found(mapMethod))
+    if (meshToMesh::interpolationMethodNames_.hasEnum(mapMethod))
     {
         // Lookup corresponding AMI method
         meshToMesh::interpolationMethod method =
diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C b/src/OpenFOAM/primitives/enums/NamedEnum.C
similarity index 58%
rename from src/OpenFOAM/containers/NamedEnum/NamedEnum.C
rename to src/OpenFOAM/primitives/enums/NamedEnum.C
index 0f5e05630f7..7b5cc701de3 100644
--- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C
+++ b/src/OpenFOAM/primitives/enums/NamedEnum.C
@@ -25,46 +25,25 @@ License
 
 #include "NamedEnum.H"
 #include "dictionary.H"
-
-// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
-
-template<class Enum, int nEnum>
-template<class StringType>
-Foam::List<StringType> Foam::NamedEnum<Enum, nEnum>::getNamesList()
-{
-    List<StringType> lst(nEnum);
-
-    label count = 0;
-    for (int enumi=0; enumi < nEnum; ++enumi)
-    {
-        if (names[enumi] && names[enumi][0])
-        {
-            lst[count++] = names[enumi];
-        }
-    }
-
-    lst.setSize(count);
-    return lst;
-}
-
+#include "stdFoam.H"
 
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
-template<class Enum, int nEnum>
-Foam::NamedEnum<Enum, nEnum>::NamedEnum()
+template<class EnumType, int nEnum>
+Foam::NamedEnum<EnumType, nEnum>::NamedEnum()
 :
-    table_type(2*nEnum)
+    lookup_(2*nEnum)
 {
     for (int enumi=0; enumi < nEnum; ++enumi)
     {
         if (names[enumi] && names[enumi][0])
         {
-            insert(names[enumi], enumi);
+            lookup_.insert(names[enumi], enumi);
         }
         else
         {
             // Bad name - generate error message
-            stringList goodNames(enumi);
+            List<string> goodNames(enumi);
 
             for (int i = 0; i < enumi; ++i)
             {
@@ -74,7 +53,7 @@ Foam::NamedEnum<Enum, nEnum>::NamedEnum()
             FatalErrorInFunction
                 << "Illegal enumeration name at position " << enumi << nl
                 << "after entries " << goodNames << nl
-                << "Possibly your NamedEnum<Enum, nEnum>::names array"
+                << "Possibly your NamedEnum<EnumType, nEnum>::names array"
                 << " is not of size " << nEnum << endl
                 << abort(FatalError);
         }
@@ -84,57 +63,93 @@ Foam::NamedEnum<Enum, nEnum>::NamedEnum()
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-template<class Enum, int nEnum>
-Enum Foam::NamedEnum<Enum, nEnum>::read(Istream& is) const
+template<class EnumType, int nEnum>
+Foam::wordList Foam::NamedEnum<EnumType, nEnum>::words() const
 {
-    const word enumName(is);
-    table_type::const_iterator iter = find(enumName);
+    List<word> lst(nEnum);
 
-    if (!iter.found())
+    label count = 0;
+    for (int enumi=0; enumi < nEnum; ++enumi)
     {
-        FatalIOErrorInFunction(is)
-            << enumName << " is not in enumeration: "
-            << sortedToc() << exit(FatalIOError);
+        if (names[enumi] && names[enumi][0])
+        {
+            lst[count++] = names[enumi];
+        }
+    }
+
+    lst.setSize(count);
+    return lst;
+}
+
+
+template<class EnumType, int nEnum>
+Foam::List<int> Foam::NamedEnum<EnumType, nEnum>::values() const
+{
+    List<int> lst(nEnum);
+
+    label count = 0;
+    for (int enumi=0; enumi < nEnum; ++enumi)
+    {
+        if (names[enumi] && names[enumi][0])
+        {
+            auto iter = lookup_.cfind(names[enumi]);
+
+            if (iter.found())
+            {
+                lst[count++] = iter.object();
+            }
+        }
     }
 
-    return Enum(iter.object());
+    lst.setSize(count);
+    return lst;
 }
 
 
-template<class Enum, int nEnum>
-void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
+template<class EnumType, int nEnum>
+bool Foam::NamedEnum<EnumType, nEnum>::hasName(const EnumType e) const
 {
-    os  << names[int(e)];
+    const int enumValue(e);
+
+    forAllConstIters(lookup_, iter)
+    {
+        if (iter.object() == enumValue)
+        {
+            return true;
+        }
+    }
+    return false;
 }
 
 
-template<class Enum, int nEnum>
-Enum Foam::NamedEnum<Enum, nEnum>::lookup
+template<class EnumType, int nEnum>
+EnumType Foam::NamedEnum<EnumType, nEnum>::lookup
 (
     const word& key,
     const dictionary& dict
 ) const
 {
     const word enumName(dict.lookup(key));
-    table_type::const_iterator iter = find(enumName);
+    auto iter = lookup_.cfind(enumName);
 
     if (!iter.found())
     {
         FatalIOErrorInFunction(dict)
             << enumName << " is not in enumeration: "
-            << sortedToc() << exit(FatalIOError);
+            << lookup_.sortedToc() << nl
+            << exit(FatalIOError);
     }
 
-    return Enum(iter.object());
+    return EnumType(iter.object());
 }
 
 
-template<class Enum, int nEnum>
-Enum Foam::NamedEnum<Enum, nEnum>::lookupOrDefault
+template<class EnumType, int nEnum>
+EnumType Foam::NamedEnum<EnumType, nEnum>::lookupOrDefault
 (
     const word& key,
     const dictionary& dict,
-    const enum_type deflt
+    const EnumType deflt
 ) const
 {
     if (dict.found(key))
@@ -148,36 +163,49 @@ Enum Foam::NamedEnum<Enum, nEnum>::lookupOrDefault
 }
 
 
-template<class Enum, int nEnum>
-Foam::List<Enum> Foam::NamedEnum<Enum, nEnum>::enums()
+template<class EnumType, int nEnum>
+EnumType Foam::NamedEnum<EnumType, nEnum>::read(Istream& is) const
 {
-    List<Enum> lst(nEnum);
+    const word enumName(is);
+    auto iter = lookup_.cfind(enumName);
 
-    label count = 0;
-    for (int enumi = 0; enumi < nEnum; ++enumi)
+    if (!iter.found())
     {
-        if (names[enumi] && names[enumi][0])
-        {
-            lst[count++] = Enum(enumi);
-        }
+        FatalIOErrorInFunction(is)
+            << enumName << " is not in enumeration: "
+            << lookup_.sortedToc() << nl
+            << exit(FatalIOError);
     }
 
-    lst.setSize(count);
-    return lst;
+    return EnumType(iter.object());
 }
 
 
-template<class Enum, int nEnum>
-Foam::stringList Foam::NamedEnum<Enum, nEnum>::strings()
+template<class EnumType, int nEnum>
+void Foam::NamedEnum<EnumType, nEnum>::write
+(
+    const EnumType e,
+    Ostream& os
+) const
 {
-    return getNamesList<string>();
+    const int idx = int(e);
+    if (idx >= 0 && idx < nEnum)
+    {
+        os  << names[idx];
+    }
 }
 
 
-template<class Enum, int nEnum>
-Foam::wordList Foam::NamedEnum<Enum, nEnum>::words()
+// * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
+
+template<class EnumType, int nEnum>
+Foam::Ostream& Foam::operator<<
+(
+    Ostream& os,
+    const NamedEnum<EnumType, nEnum>& wrapped
+)
 {
-    return getNamesList<word>();
+    return wrapped.lookup_.writeKeys(os, 10);
 }
 
 
diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H b/src/OpenFOAM/primitives/enums/NamedEnum.H
similarity index 66%
rename from src/OpenFOAM/containers/NamedEnum/NamedEnum.H
rename to src/OpenFOAM/primitives/enums/NamedEnum.H
index 0c1c0826fa3..13cff4eb44b 100644
--- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H
+++ b/src/OpenFOAM/primitives/enums/NamedEnum.H
@@ -25,9 +25,8 @@ Class
     Foam::NamedEnum
 
 Description
-    A NamedEnum is a wrapper around a static list of names that represent
-    a particular enumeration. Internally it uses a HashTable for quicker
-    lookups.
+    A NamedEnum is a wrapper around a list of names that represent
+    particular enumeration values.
 
 SourceFiles
     NamedEnum.C
@@ -38,7 +37,6 @@ SourceFiles
 #define NamedEnum_H
 
 #include "HashTable.H"
-#include "stringList.H"
 #include "wordList.H"
 #include <type_traits>
 
@@ -46,32 +44,31 @@ SourceFiles
 
 namespace Foam
 {
+// Forward declarations
 class dictionary;
+template<class EnumType, int nEnum> class NamedEnum;
+
+template<class EnumType, int nEnum>
+Ostream& operator<<(Ostream& os, const NamedEnum<EnumType, nEnum>& wrapped);
 
-// Forward declaration
-template<class Enum, int> class NamedEnum;
 
 /*---------------------------------------------------------------------------*\
                           Class NamedEnum Declaration
 \*---------------------------------------------------------------------------*/
 
-template<class Enum, int nEnum>
+template<class EnumType, int nEnum>
 class NamedEnum
-:
-    public HashTable<int>
 {
     //- The nEnum must be positive (non-zero)
     static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
 
-    //- The type of HashTable used for the lookup.
-    typedef HashTable<int> table_type;
+    // Private Member Data
 
+        //- The values for the enum
+        HashTable<int> lookup_;
 
-    // Private Member Functions
 
-        //- The names as a list of strings
-        template<class StringType>
-        static List<StringType> getNamesList();
+    // Private Member Functions
 
         //- Disallow default bitwise copy construct
         NamedEnum(const NamedEnum&) = delete;
@@ -83,12 +80,12 @@ class NamedEnum
 public:
 
     //- The type of enumeration wrapped by NamedEnum
-    typedef Enum enum_type;
+    typedef EnumType value_type;
 
 
     // Static data members
 
-        //- The set of names corresponding to the enumeration Enum
+        //- The set of names corresponding to the enumeration EnumType
         static const char* names[nEnum];
 
 
@@ -100,17 +97,39 @@ public:
 
     // Member Functions
 
-        //- Read a word from Istream and return the corresponding
-        //  enumeration element
-        enum_type read(Istream& is) const;
+      // Access
 
-        //- Write the name representation of the enumeration to an Ostream
-        void write(const enum_type e, Ostream& os) const;
+        //- The number of lookup names for the enumeration
+        inline label size() const;
+
+        //- The list of enum names
+        inline wordList toc() const;
+
+        //- The sorted list of enum names
+        inline wordList sortedToc() const;
+
+        //- The list of enum names, in construction order
+        wordList words() const;
+
+        //- The list of enum values, in construction order
+        List<int> values() const;
+
+
+      // Query
+
+        //- Test if there is an enumeration corresponding to the given name.
+        inline bool hasEnum(const word& enumName) const;
+
+        //- Test if there is a name corresponding to the given enumeration.
+        bool hasName(const EnumType e) const;
+
+
+      // Lookup
 
         //- Lookup the key in the dictionary and return the corresponding
         // enumeration element based on its name.
         // Fatal if anything is incorrect.
-        enum_type lookup
+        EnumType lookup
         (
             const word& key,
             const dictionary& dict
@@ -120,42 +139,42 @@ public:
         //  enumeration element based on its name.
         //  Return the default value if the key was not found in the dictionary.
         //  Fatal if enumerated name was incorrect.
-        enum_type lookupOrDefault
+        EnumType lookupOrDefault
         (
             const word& key,
             const dictionary& dict,
-            const enum_type deflt
+            const EnumType deflt
         ) const;
 
-        //- List of enumerations
-        static List<enum_type> enums();
 
-        //- The set of names as a list of strings
-        static stringList strings();
+      // IO
+
+        //- Read a word from Istream and return the corresponding enumeration
+        EnumType read(Istream& is) const;
 
-        //- The set of names as a list of words
-        static wordList words();
+        //- Write the name representation of the enumeration to an Ostream
+        //  A noop if the enumeration wasn't found.
+        void write(const EnumType e, Ostream& os) const;
 
 
     // Member Operators
 
         //- Return the enumeration element corresponding to the given name
-        inline const enum_type operator[](const char* name) const
-        {
-            return enum_type(table_type::operator[](name));
-        }
-
-        //- Return the enumeration element corresponding to the given name
-        inline const enum_type operator[](const word& name) const
-        {
-            return enum_type(table_type::operator[](name));
-        }
+        inline const EnumType operator[](const word& name) const;
 
         //- Return the name of the given enumeration element
-        inline const char* operator[](const enum_type e) const
-        {
-            return names[int(e)];
-        }
+        inline const char* operator[](const EnumType e) const;
+
+
+    // IOstream operators
+
+        //- Write names to Ostream, as per writeKeys() with shortListLen=10
+        friend Ostream& operator<< <EnumType, nEnum>
+        (
+            Ostream& os,
+            const NamedEnum<EnumType, nEnum>& wrapped
+        );
+
 };
 
 
@@ -165,6 +184,8 @@ public:
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
+#include "NamedEnumI.H"
+
 #ifdef NoRepository
     #include "NamedEnum.C"
 #endif
diff --git a/src/OpenFOAM/primitives/enums/NamedEnumI.H b/src/OpenFOAM/primitives/enums/NamedEnumI.H
new file mode 100644
index 00000000000..2e7c91b10d3
--- /dev/null
+++ b/src/OpenFOAM/primitives/enums/NamedEnumI.H
@@ -0,0 +1,81 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+\*---------------------------------------------------------------------------*/
+
+// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
+
+template<class EnumType, int nEnum>
+inline Foam::label Foam::NamedEnum<EnumType, nEnum>::size() const
+{
+    return lookup_.size();
+}
+
+
+template<class EnumType, int nEnum>
+inline Foam::wordList Foam::NamedEnum<EnumType, nEnum>::toc() const
+{
+    return lookup_.toc();
+}
+
+
+template<class EnumType, int nEnum>
+inline Foam::wordList Foam::NamedEnum<EnumType, nEnum>::sortedToc() const
+{
+    return lookup_.sortedToc();
+}
+
+
+template<class EnumType, int nEnum>
+inline bool Foam::NamedEnum<EnumType, nEnum>::hasEnum
+(
+    const word& enumName
+) const
+{
+    return lookup_.found(enumName);
+}
+
+
+// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
+
+template<class EnumType, int nEnum>
+inline const EnumType Foam::NamedEnum<EnumType, nEnum>::operator[]
+(
+    const word& name
+) const
+{
+    return EnumType(lookup_[name]);
+}
+
+
+template<class EnumType, int nEnum>
+inline const char* Foam::NamedEnum<EnumType, nEnum>::operator[]
+(
+    const EnumType e
+) const
+{
+    return names[int(e)];
+}
+
+
+// ************************************************************************* //
diff --git a/src/TurbulenceModels/compressible/turbulentFluidThermoModels/derivedFvPatchFields/temperatureCoupledBase/temperatureCoupledBase.C b/src/TurbulenceModels/compressible/turbulentFluidThermoModels/derivedFvPatchFields/temperatureCoupledBase/temperatureCoupledBase.C
index 8a7027a59c9..50c15be701b 100644
--- a/src/TurbulenceModels/compressible/turbulentFluidThermoModels/derivedFvPatchFields/temperatureCoupledBase/temperatureCoupledBase.C
+++ b/src/TurbulenceModels/compressible/turbulentFluidThermoModels/derivedFvPatchFields/temperatureCoupledBase/temperatureCoupledBase.C
@@ -111,7 +111,7 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
         {
             typedef compressible::turbulenceModel turbulenceModel;
 
-            word turbName(turbulenceModel::propertiesName);
+            const word turbName(turbulenceModel::propertiesName);
 
             if
             (
@@ -205,8 +205,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
                     << " on mesh " << mesh.name() << " patch " << patch_.name()
                     << nl
                     << "Please set 'kappaMethod' to one of "
-                    << KMethodTypeNames_.toc()
-                    << " and 'kappa' to the name of the volScalar"
+                    << flatOutput(KMethodTypeNames_.sortedToc()) << nl
+                    << "and 'kappa' to the name of the volScalar"
                     << " or volSymmTensor field (if kappaMethod=lookup)"
                     << exit(FatalError);
             }
@@ -219,8 +219,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
             FatalErrorInFunction
                 << "Unimplemented method " << KMethodTypeNames_[method_] << nl
                 << "Please set 'kappaMethod' to one of "
-                << KMethodTypeNames_.toc()
-                << " and 'kappa' to the name of the volScalar"
+                << flatOutput(KMethodTypeNames_.sortedToc()) << nl
+                << "and 'kappa' to the name of the volScalar"
                 << " or volSymmTensor field (if kappaMethod=lookup)"
                 << exit(FatalError);
         }
diff --git a/src/conversion/fire/FIREMeshReader.C b/src/conversion/fire/FIREMeshReader.C
index e79d70b060d..c57a1886158 100644
--- a/src/conversion/fire/FIREMeshReader.C
+++ b/src/conversion/fire/FIREMeshReader.C
@@ -375,7 +375,7 @@ bool Foam::fileFormats::FIREMeshReader::readGeometry(const scalar scaleFactor)
     IOstream::streamFormat fmt = IOstream::ASCII;
 
     const word ext = geometryFile_.ext();
-    bool supported = FIRECore::file3dExtensions.found(ext);
+    bool supported = FIRECore::file3dExtensions.hasEnum(ext);
     if (supported)
     {
         FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
diff --git a/src/conversion/fire/FIREMeshWriter.C b/src/conversion/fire/FIREMeshWriter.C
index 29746005d07..9c6146c3e00 100644
--- a/src/conversion/fire/FIREMeshWriter.C
+++ b/src/conversion/fire/FIREMeshWriter.C
@@ -278,7 +278,7 @@ bool Foam::fileFormats::FIREMeshWriter::write(const fileName& meshName) const
     {
         const word ext = baseName.ext();
 
-        if (FIRECore::file3dExtensions.found(ext))
+        if (FIRECore::file3dExtensions.hasEnum(ext))
         {
             FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
             if (fireFileType == FIRECore::POLY_ASCII)
diff --git a/src/finiteVolume/functionObjects/volRegion/volRegion.C b/src/finiteVolume/functionObjects/volRegion/volRegion.C
index 3a0dcede22b..07d4c1775ef 100644
--- a/src/finiteVolume/functionObjects/volRegion/volRegion.C
+++ b/src/finiteVolume/functionObjects/volRegion/volRegion.C
@@ -143,7 +143,7 @@ bool Foam::functionObjects::volRegion::read
         {
             FatalIOErrorInFunction(dict)
                 << "Unknown region type. Valid region types are:"
-                << regionTypeNames_
+                << regionTypeNames_.toc()
                 << exit(FatalIOError);
         }
     }
diff --git a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
index f28944d9959..3d39aa4655d 100644
--- a/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
+++ b/src/functionObjects/field/fieldValues/surfaceFieldValue/surfaceFieldValue.C
@@ -564,9 +564,10 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
         {
             FatalErrorInFunction
                 << type() << " " << name() << ": "
-                << regionTypeNames_[regionType_] << "(" << regionName_ << "):"
+                << int(regionType_) << "(" << regionName_ << "):"
                 << nl << "    Unknown region type. Valid region types are:"
-                << regionTypeNames_.sortedToc() << nl << exit(FatalError);
+                << regionTypeNames_ << nl
+                << exit(FatalError);
         }
     }
 
diff --git a/src/functionObjects/field/mapFields/mapFields.C b/src/functionObjects/field/mapFields/mapFields.C
index 00a387ad6e3..f0d250f659d 100644
--- a/src/functionObjects/field/mapFields/mapFields.C
+++ b/src/functionObjects/field/mapFields/mapFields.C
@@ -71,14 +71,14 @@ void Foam::functionObjects::mapFields::createInterpolation
         )
     );
     const fvMesh& mapRegion = mapRegionPtr_();
-    word mapMethodName(dict.lookup("mapMethod"));
-    if (!meshToMesh::interpolationMethodNames_.found(mapMethodName))
+    const word mapMethodName(dict.lookup("mapMethod"));
+    if (!meshToMesh::interpolationMethodNames_.hasEnum(mapMethodName))
     {
         FatalErrorInFunction
             << type() << " " << name() << ": unknown map method "
             << mapMethodName << nl
             << "Available methods include: "
-            << meshToMesh::interpolationMethodNames_.sortedToc()
+            << meshToMesh::interpolationMethodNames_
             << exit(FatalError);
     }
 
diff --git a/src/functionObjects/utilities/writeObjects/writeObjects.C b/src/functionObjects/utilities/writeObjects/writeObjects.C
index aad0e40554e..eade9464024 100644
--- a/src/functionObjects/utilities/writeObjects/writeObjects.C
+++ b/src/functionObjects/utilities/writeObjects/writeObjects.C
@@ -196,7 +196,8 @@ bool Foam::functionObjects::writeObjects::write()
                 FatalErrorInFunction
                     << "Unknown writeOption "
                     << writeOptionNames_[writeOption_]
-                    << ". Valid writeOption types are" << writeOptionNames_
+                    << ". Valid writeOption types are "
+                    << writeOptionNames_
                     << exit(FatalError);
             }
         }
diff --git a/src/fvOptions/cellSetOption/cellSetOption.C b/src/fvOptions/cellSetOption/cellSetOption.C
index 665b98343df..929b457a7ee 100644
--- a/src/fvOptions/cellSetOption/cellSetOption.C
+++ b/src/fvOptions/cellSetOption/cellSetOption.C
@@ -82,7 +82,8 @@ void Foam::fv::cellSetOption::setSelection(const dictionary& dict)
             FatalErrorInFunction
                 << "Unknown selectionMode "
                 << selectionModeTypeNames_[selectionMode_]
-                << ". Valid selectionMode types are" << selectionModeTypeNames_
+                << ". Valid selectionMode types are "
+                << selectionModeTypeNames_
                 << exit(FatalError);
         }
     }
@@ -186,7 +187,8 @@ void Foam::fv::cellSetOption::setCellSet()
             FatalErrorInFunction
                 << "Unknown selectionMode "
                 << selectionModeTypeNames_[selectionMode_]
-                << ". Valid selectionMode types are" << selectionModeTypeNames_
+                << ". Valid selectionMode types are "
+                << selectionModeTypeNames_
                 << exit(FatalError);
         }
     }
diff --git a/src/fvOptions/sources/derived/directionalPressureGradientExplicitSource/directionalPressureGradientExplicitSource.C b/src/fvOptions/sources/derived/directionalPressureGradientExplicitSource/directionalPressureGradientExplicitSource.C
index 7607aacf793..e0c0549f2fb 100644
--- a/src/fvOptions/sources/derived/directionalPressureGradientExplicitSource/directionalPressureGradientExplicitSource.C
+++ b/src/fvOptions/sources/derived/directionalPressureGradientExplicitSource/directionalPressureGradientExplicitSource.C
@@ -236,7 +236,7 @@ directionalPressureGradientExplicitSource
             << "Did not find mode " << model_
             << nl
             << "Please set 'model' to one of "
-            << PressureDropModelNames_.toc()
+            << PressureDropModelNames_
             << exit(FatalError);
     }
 
diff --git a/src/fvOptions/sources/derived/rotorDiskSource/rotorDiskSource.C b/src/fvOptions/sources/derived/rotorDiskSource/rotorDiskSource.C
index 129d5b16998..bf8594073bb 100644
--- a/src/fvOptions/sources/derived/rotorDiskSource/rotorDiskSource.C
+++ b/src/fvOptions/sources/derived/rotorDiskSource/rotorDiskSource.C
@@ -376,7 +376,8 @@ void Foam::fv::rotorDiskSource::createCoordinateSystem()
             FatalErrorInFunction
                 << "Unknown geometryMode " << geometryModeTypeNames_[gm]
                 << ". Available geometry modes include "
-                << geometryModeTypeNames_ << exit(FatalError);
+                << geometryModeTypeNames_
+                << exit(FatalError);
         }
     }
 
diff --git a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
index 1972082d788..ac6be7983de 100644
--- a/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
+++ b/src/meshTools/mappedPatches/mappedPolyPatch/mappedPatchBase.C
@@ -1070,7 +1070,7 @@ Foam::mappedPatchBase::mappedPatchBase
     {
         offsetMode_ = offsetModeNames_.read(dict.lookup("offsetMode"));
 
-        switch(offsetMode_)
+        switch (offsetMode_)
         {
             case UNIFORM:
             {
@@ -1109,7 +1109,7 @@ Foam::mappedPatchBase::mappedPatchBase
         (
             dict
         )   << "Please supply the offsetMode as one of "
-            << NamedEnum<offsetMode, 3>::words()
+            << offsetModeNames_
             << exit(FatalIOError);
     }
 }
diff --git a/src/regionCoupled/derivedFvPatchFields/energyRegionCoupled/energyRegionCoupledFvPatchScalarField.C b/src/regionCoupled/derivedFvPatchFields/energyRegionCoupled/energyRegionCoupledFvPatchScalarField.C
index 2935936e8bb..72afb55582a 100644
--- a/src/regionCoupled/derivedFvPatchFields/energyRegionCoupled/energyRegionCoupledFvPatchScalarField.C
+++ b/src/regionCoupled/derivedFvPatchFields/energyRegionCoupled/energyRegionCoupledFvPatchScalarField.C
@@ -134,7 +134,7 @@ kappa() const
                     << " on mesh " << this->db().name() << " patch "
                     << patch().name()
                     << " could not find a method in. Methods are:  "
-                    << methodTypeNames_.toc()
+                    << methodTypeNames_
                     << " Not turbulenceModel or thermophysicalProperties"
                     << " were found"
                     << exit(FatalError);
diff --git a/src/thermophysicalModels/radiation/submodels/boundaryRadiationProperties/boundaryRadiationPropertiesPatch.C b/src/thermophysicalModels/radiation/submodels/boundaryRadiationProperties/boundaryRadiationPropertiesPatch.C
index d5785cbbd98..369c9788a24 100644
--- a/src/thermophysicalModels/radiation/submodels/boundaryRadiationProperties/boundaryRadiationPropertiesPatch.C
+++ b/src/thermophysicalModels/radiation/submodels/boundaryRadiationProperties/boundaryRadiationPropertiesPatch.C
@@ -222,7 +222,8 @@ Foam::radiation::boundaryRadiationPropertiesPatch::emissivity
         default:
         {
             FatalErrorInFunction
-                << "Please set 'mode' to one of " << methodTypeNames_.toc()
+                << "Please set 'mode' to one of "
+                << methodTypeNames_
                 << exit(FatalError);
         }
         break;
@@ -302,7 +303,7 @@ Foam::radiation::boundaryRadiationPropertiesPatch::absorptivity
             FatalErrorInFunction
                 << "Unimplemented method " << method_ << endl
                 << "Please set 'mode' to one of "
-                << methodTypeNames_.toc()
+                << methodTypeNames_
                 << exit(FatalError);
         }
         break;
@@ -382,7 +383,7 @@ Foam::radiation::boundaryRadiationPropertiesPatch::transmissivity
             FatalErrorInFunction
                 << "Unimplemented method " << method_ << endl
                 << "Please set 'mode' to one of "
-                << methodTypeNames_.toc()
+                << methodTypeNames_
                 << exit(FatalError);
         }
         break;
-- 
GitLab