ENH: provide list of enums for NamedEnum
- can be used to loop over all enumerations in the order of definition.
This commit is contained in:
parent
ba249f8e76
commit
bbec683e42
@ -1,3 +1,3 @@
|
||||
Test-namedEnum.C
|
||||
Test-NamedEnum.C
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum
|
||||
|
@ -34,26 +34,28 @@ class namedEnumTest
|
||||
{
|
||||
public:
|
||||
|
||||
enum options
|
||||
enum option
|
||||
{
|
||||
a,
|
||||
b,
|
||||
c
|
||||
c,
|
||||
d
|
||||
};
|
||||
|
||||
static const Foam::NamedEnum<options, 3> namedEnum;
|
||||
static const Foam::NamedEnum<option, 4> namedEnum;
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
const char* Foam::NamedEnum<namedEnumTest::options, 3>::names[] =
|
||||
const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
|
||||
{
|
||||
"a",
|
||||
"b",
|
||||
"c"
|
||||
"c",
|
||||
"d"
|
||||
};
|
||||
|
||||
const Foam::NamedEnum<namedEnumTest::options, 3> namedEnumTest::namedEnum;
|
||||
const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::namedEnum;
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
@ -61,11 +63,47 @@ const Foam::NamedEnum<namedEnumTest::options, 3> namedEnumTest::namedEnum;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Info<< namedEnumTest::namedEnum["a"] << endl;
|
||||
Info<< namedEnumTest::namedEnum[namedEnumTest::a] << endl;
|
||||
const List<namedEnumTest::option> 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<namedEnumTest::option>, 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;
|
||||
|
@ -120,4 +120,23 @@ Foam::wordList Foam::NamedEnum<Enum, nEnum>::words()
|
||||
}
|
||||
|
||||
|
||||
template<class Enum, int nEnum>
|
||||
Foam::List<Enum> Foam::NamedEnum<Enum, nEnum>::enums()
|
||||
{
|
||||
List<Enum> 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;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
@ -45,6 +45,9 @@ SourceFiles
|
||||
namespace Foam
|
||||
{
|
||||
|
||||
// Forward declaration
|
||||
template<class Enum, int> 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<Enum> enums();
|
||||
|
||||
|
||||
// Member Operators
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user