mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: Enum class as drop-in alternative for NamedEnum
- the NamedEnum wrapper is somewhate too rigid.
* All enumerated values are contiguous, starting as zero.
* The implicit one-to-one mapping precludes using it for aliases.
* For example, perhaps we want to support alternative lookup names for an
enumeration, or manage an enumeration lookup for a sub-range.
This commit is contained in:
@ -42,13 +42,44 @@ Description
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
void infoHashString
|
||||
(
|
||||
unsigned modulus,
|
||||
std::initializer_list<std::string> lst
|
||||
)
|
||||
{
|
||||
if (modulus)
|
||||
{
|
||||
Info<< "basic string hashing (mod " << label(modulus) << ")" << endl;
|
||||
|
||||
for (const auto& str : lst)
|
||||
{
|
||||
Info<<"hash(" << str.c_str() << ")="
|
||||
<< (Hash<string>()(str) % modulus) << nl;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Info<< "basic string hashing" << nl;
|
||||
|
||||
for (const auto& str : lst)
|
||||
{
|
||||
Info<<"hash(" << str.c_str() << ")="
|
||||
<< Hash<string>()(str) << nl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
IFstream is("hashingTests");
|
||||
infoHashString(8, {"asdathis1", "adsxf", "hij", "klmpq"});
|
||||
|
||||
IFstream is("hashingTests");
|
||||
|
||||
while (is.good())
|
||||
{
|
||||
|
||||
@ -26,6 +26,7 @@ Description
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "NamedEnum.H"
|
||||
#include "Enum.H"
|
||||
#include "IOstreams.H"
|
||||
|
||||
using namespace Foam;
|
||||
@ -42,7 +43,19 @@ public:
|
||||
D
|
||||
};
|
||||
|
||||
enum class otherOption
|
||||
{
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
D
|
||||
};
|
||||
|
||||
static const Foam::NamedEnum<option, 4> optionNamed;
|
||||
|
||||
static const Foam::Enum<otherOption> optionEnum;
|
||||
|
||||
static const Foam::Enum<option> optionEnum2;
|
||||
};
|
||||
|
||||
|
||||
@ -57,14 +70,30 @@ const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
|
||||
|
||||
const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::optionNamed;
|
||||
|
||||
const Foam::Enum<namedEnumTest::otherOption> namedEnumTest::optionEnum
|
||||
{
|
||||
{ namedEnumTest::otherOption::A, "a" },
|
||||
{ namedEnumTest::otherOption::B, "b" },
|
||||
{ namedEnumTest::otherOption::C, "c" },
|
||||
{ namedEnumTest::otherOption::D, "d" },
|
||||
};
|
||||
|
||||
|
||||
const Foam::Enum<namedEnumTest::option> namedEnumTest::optionEnum2
|
||||
(
|
||||
namedEnumTest::option::C,
|
||||
{ "c", "d" }
|
||||
);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
// Main program:
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const List<namedEnumTest::option> options
|
||||
= namedEnumTest::namedEnum.enums();
|
||||
Info<<"NamedEnum: " << namedEnumTest::optionNamed << nl;
|
||||
Info<<"Enum: " << namedEnumTest::optionEnum << nl;
|
||||
Info<<"Enum: " << namedEnumTest::optionEnum2 << nl;
|
||||
|
||||
dictionary testDict;
|
||||
testDict.add("lookup1", "c");
|
||||
@ -73,6 +102,10 @@ int main(int argc, char *argv[])
|
||||
<< int(namedEnumTest::optionNamed["a"]) << nl
|
||||
<< namedEnumTest::optionNamed[namedEnumTest::option::A] << nl;
|
||||
|
||||
Info<< nl
|
||||
<< int(namedEnumTest::optionEnum["a"]) << nl
|
||||
<< namedEnumTest::optionEnum[namedEnumTest::otherOption::A] << nl;
|
||||
|
||||
Info<< "--- test dictionary lookup ---" << endl;
|
||||
{
|
||||
Info<< "dict: " << testDict << endl;
|
||||
@ -100,6 +133,18 @@ int main(int argc, char *argv[])
|
||||
)
|
||||
)
|
||||
<< nl;
|
||||
|
||||
Info<< "got: "
|
||||
<< int
|
||||
(
|
||||
namedEnumTest::optionEnum2.lookupOrDefault
|
||||
(
|
||||
"lookup1",
|
||||
testDict,
|
||||
namedEnumTest::option::A
|
||||
)
|
||||
)
|
||||
<< nl;
|
||||
}
|
||||
|
||||
Info<< "--- test read ---" << endl;
|
||||
|
||||
Reference in New Issue
Block a user