diff --git a/applications/test/NamedEnum/Make/files b/applications/test/NamedEnum/Make/files index 755b72ba5e..a1a7086041 100644 --- a/applications/test/NamedEnum/Make/files +++ b/applications/test/NamedEnum/Make/files @@ -1,3 +1,3 @@ -Test-namedEnum.C +Test-NamedEnum.C EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum diff --git a/applications/test/NamedEnum/Test-namedEnum.C b/applications/test/NamedEnum/Test-NamedEnum.C similarity index 52% rename from applications/test/NamedEnum/Test-namedEnum.C rename to applications/test/NamedEnum/Test-NamedEnum.C index 22f3596932..63386ed79e 100644 --- a/applications/test/NamedEnum/Test-namedEnum.C +++ b/applications/test/NamedEnum/Test-NamedEnum.C @@ -34,26 +34,28 @@ class namedEnumTest { public: - enum options + enum option { a, b, - c + c, + d }; - static const Foam::NamedEnum namedEnum; + static const Foam::NamedEnum namedEnum; }; template<> -const char* Foam::NamedEnum::names[] = +const char* Foam::NamedEnum::names[] = { "a", "b", - "c" + "c", + "d" }; -const Foam::NamedEnum namedEnumTest::namedEnum; +const Foam::NamedEnum namedEnumTest::namedEnum; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -61,11 +63,47 @@ const Foam::NamedEnum namedEnumTest::namedEnum; int main(int argc, char *argv[]) { - Info<< namedEnumTest::namedEnum["a"] << endl; - Info<< namedEnumTest::namedEnum[namedEnumTest::a] << endl; + const List options + = namedEnumTest::namedEnum.enums(); - namedEnumTest::options hmm(namedEnumTest::namedEnum.read(Sin)); - Info<< namedEnumTest::namedEnum[hmm] << endl; + 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; + } + +#if __cplusplus > 201100L + // C++11 + Info<< "loop over enums (C++11 for range):" << nl; + for (auto const& opt : options) + { + Info<< "option[" << opt + << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; + } +#else + Info<< "loop over enums (via iterator):" << nl; + forAllConstIter(List, options, iter) + { + const namedEnumTest::option& opt = *iter; + + Info<< "option[" << opt + << "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl; + } +#endif + + Info<< nl + << namedEnumTest::namedEnum["a"] << nl + << namedEnumTest::namedEnum[namedEnumTest::a] << nl; + + Info<< "--- test read construction ---" << endl; + + namedEnumTest::option dummy(namedEnumTest::namedEnum.read(Sin)); + Info<< namedEnumTest::namedEnum[dummy] << endl; Info<< "End\n" << endl; diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C index ba5bd4999d..99f723d987 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.C +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.C @@ -120,4 +120,23 @@ Foam::wordList Foam::NamedEnum::words() } +template +Foam::List Foam::NamedEnum::enums() +{ + List lst(nEnum); + + label nElem = 0; + for (int enumI = 0; enumI < nEnum; ++enumI) + { + if (names[enumI] && names[enumI][0]) + { + lst[nElem++] = Enum(enumI); + } + } + + lst.setSize(nElem); + return lst; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H index f4af06792c..6005f86a60 100644 --- a/src/OpenFOAM/containers/NamedEnum/NamedEnum.H +++ b/src/OpenFOAM/containers/NamedEnum/NamedEnum.H @@ -45,6 +45,9 @@ SourceFiles namespace Foam { +// Forward declaration +template class NamedEnum; + /*---------------------------------------------------------------------------*\ Class NamedEnum Declaration \*---------------------------------------------------------------------------*/ @@ -60,10 +63,10 @@ class NamedEnum // Private Member Functions //- Disallow default bitwise copy construct - NamedEnum(const NamedEnum&); + NamedEnum(const NamedEnum&) = delete; //- Disallow default bitwise assignment - void operator=(const NamedEnum&); + void operator=(const NamedEnum&) = delete; public: @@ -95,6 +98,9 @@ public: //- The set of names as a list of words static wordList words(); + //- List of enumerations + static List enums(); + // Member Operators