mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
@ -1,3 +1,3 @@
|
|||||||
Test-namedEnum.C
|
Test-NamedEnum.C
|
||||||
|
|
||||||
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum
|
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum
|
||||||
|
|||||||
@ -34,26 +34,28 @@ class namedEnumTest
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum options
|
enum option
|
||||||
{
|
{
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
c
|
c,
|
||||||
|
d
|
||||||
};
|
};
|
||||||
|
|
||||||
static const Foam::NamedEnum<options, 3> namedEnum;
|
static const Foam::NamedEnum<option, 4> namedEnum;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
const char* Foam::NamedEnum<namedEnumTest::options, 3>::names[] =
|
const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
|
||||||
{
|
{
|
||||||
"a",
|
"a",
|
||||||
"b",
|
"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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Info<< namedEnumTest::namedEnum["a"] << endl;
|
const List<namedEnumTest::option> options
|
||||||
Info<< namedEnumTest::namedEnum[namedEnumTest::a] << endl;
|
= namedEnumTest::namedEnum.enums();
|
||||||
|
|
||||||
namedEnumTest::options hmm(namedEnumTest::namedEnum.read(Sin));
|
Info<< "enums: " << options << nl;
|
||||||
Info<< namedEnumTest::namedEnum[hmm] << endl;
|
|
||||||
|
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;
|
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
|
namespace Foam
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
template<class Enum, int> class NamedEnum;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*\
|
/*---------------------------------------------------------------------------*\
|
||||||
Class NamedEnum Declaration
|
Class NamedEnum Declaration
|
||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
@ -60,10 +63,10 @@ class NamedEnum
|
|||||||
// Private Member Functions
|
// Private Member Functions
|
||||||
|
|
||||||
//- Disallow default bitwise copy construct
|
//- Disallow default bitwise copy construct
|
||||||
NamedEnum(const NamedEnum&);
|
NamedEnum(const NamedEnum&) = delete;
|
||||||
|
|
||||||
//- Disallow default bitwise assignment
|
//- Disallow default bitwise assignment
|
||||||
void operator=(const NamedEnum&);
|
void operator=(const NamedEnum&) = delete;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -95,6 +98,9 @@ public:
|
|||||||
//- The set of names as a list of words
|
//- The set of names as a list of words
|
||||||
static wordList words();
|
static wordList words();
|
||||||
|
|
||||||
|
//- List of enumerations
|
||||||
|
static List<Enum> enums();
|
||||||
|
|
||||||
|
|
||||||
// Member Operators
|
// Member Operators
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user