ENH: cleanup of Enum class

- more dictionary-like methods, enforce keyType::LITERAL for all
  lookups to avoid any spurious keyword matching.

- new readEntry, readIfPresent methods

- The get() method replaces the now deprecate lookup() method.

- Deprecate lookupOrFailsafe()
  Failsafe behaviour is now an optional parameter for lookupOrDefault,
  which makes it easier to tailor behaviour at runtime.

- output of the names is now always flatted without line-breaks.
  Thus,

     os << flatOutput(someEnumNames.names()) << nl;
     os << someEnumNames << nl;

  both generate the same output.

- Constructor now uses C-string (const char*) directly instead of
  Foam::word in its initializer_list.

- Remove special enum + initializer_list constructor form since
  it can create unbounded lookup indices.

- Removd old hasEnum, hasName forms that were provided during initial
  transition from NamedEnum.

- Added static_assert on Enum contents to restrict to enum or
  integral values.  Should not likely be using this class to enumerate
  other things since it internally uses an 'int' for its values.

  Changed volumeType accordingly to enumerate on its type (enum),
  not the class itself.
This commit is contained in:
Mark Olesen
2018-10-18 12:57:32 +02:00
parent f2c78362e7
commit 3b74512231
117 changed files with 825 additions and 857 deletions

View File

@ -322,7 +322,7 @@ void Foam::radiation::laserDTRM::initialise()
Foam::radiation::laserDTRM::laserDTRM(const volScalarField& T)
:
radiationModel(typeName, T),
mode_(powerDistNames_.lookup("mode", *this)),
mode_(powerDistNames_.get("mode", *this)),
DTRMCloud_(mesh_, "DTRMCloud", IDLList<DTRMParticle>()),
nParticles_(0),
ndTheta_(get<label>("nTheta")),
@ -432,7 +432,7 @@ Foam::radiation::laserDTRM::laserDTRM
)
:
radiationModel(typeName, dict, T),
mode_(powerDistNames_.lookup("mode", *this)),
mode_(powerDistNames_.get("mode", *this)),
DTRMCloud_(mesh_, "DTRMCloud", IDLList<DTRMParticle>()),
nParticles_(0),
ndTheta_(get<label>("nTheta")),

View File

@ -50,10 +50,10 @@ const Foam::Enum
>
Foam::compressible::
alphatWallBoilingWallFunctionFvPatchScalarField::phaseTypeNames_
{
({
{ phaseType::vaporPhase, "vapor" },
{ phaseType::liquidPhase, "liquid" },
};
});
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -102,7 +102,7 @@ alphatWallBoilingWallFunctionFvPatchScalarField
)
:
alphatPhaseChangeJayatillekeWallFunctionFvPatchScalarField(p, iF, dict),
phaseType_(phaseTypeNames_.lookup("phaseType", dict)),
phaseType_(phaseTypeNames_.get("phaseType", dict)),
relax_(dict.lookupOrDefault<scalar>("relax", 0.5)),
AbyV_(p.size(), 0),
alphatConv_(p.size(), 0),

View File

@ -0,0 +1,3 @@
Test-Enum.C
EXE = $(FOAM_USER_APPBIN)/Test-Enum

View File

@ -0,0 +1,166 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 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/>.
Description
Testing of Enum lookups.
\*---------------------------------------------------------------------------*/
#include "Enum.H"
#include "dictionary.H"
#include "FlatOutput.H"
#include "IOstreams.H" // For 'Sin'
#include <array>
using namespace Foam;
struct testing
{
enum class option { A, B, C, D };
static const Foam::Enum<option> option1Names;
static const Foam::Enum<option> option2Names;
};
// All names
const Foam::Enum<testing::option> testing::option1Names
({
{ testing::option::A, "a" },
{ testing::option::B, "b" },
{ testing::option::C, "c" },
{ testing::option::D, "d" },
});
// Subset of names
const Foam::Enum<testing::option> testing::option2Names
({
{ testing::option::C, "c" },
{ testing::option::D, "d" },
});
// Can use for integers as well, but not scalar etc.
const Foam::Enum<int> otherNames1
({
{ 0, "a" },
{ 2, "b" },
{ 3, "c" },
{ 3, "d" },
});
// Can use for integers as well, but not scalar etc.
const Foam::Enum<int> otherNames2
({
{ 0, "a" },
{ 2, "b" },
{ 3, "c" },
{ 3, "asdasd" },
});
std::array<const char*, 2> myarray{ "false", "true" };
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<<"Enum 1" << nl
<< " names: " << testing::option1Names << nl
<< " values: " << flatOutput(testing::option1Names.values())
<< nl << nl;
Info<<"Enum 2" << nl
<< " names: " << testing::option2Names << nl
<< " values: " << flatOutput(testing::option2Names.values())
<< nl << nl;
Info<<"Other Enum" << nl
<< " names: " << otherNames2 << nl
<< " values: " << flatOutput(otherNames2.values())
<< nl << nl;
dictionary testDict;
testDict.add("lookup1", "c");
testDict.add("lookup2", "rubbish");
Info<< nl
<< int(testing::option1Names["a"]) << nl
<< testing::option1Names[testing::option::A] << nl;
Info<< "--- test dictionary lookup ---" << endl;
{
Info<< "dict: " << testDict << endl;
Info<< "lookupOrDefault(notFound) = "
<< int
(
testing::option1Names.lookupOrDefault
(
"notFound",
testDict,
testing::option::A
)
)
<< nl;
Info<< "lookupOrDefault(lookup1) = "
<< int
(
testing::option1Names.lookupOrDefault
(
"lookup1",
testDict,
testing::option::A
)
)
<< nl;
Info<< "lookupOrDefault(lookup1) = "
<< int
(
testing::option2Names.lookupOrDefault
(
"lookup1",
testDict,
testing::option::A
)
)
<< nl;
}
Info<< "--- test read ---" << endl;
testing::option dummy(testing::option1Names.read(Sin));
Info<< testing::option1Names[dummy] << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //

View File

@ -1,3 +0,0 @@
Test-NamedEnum.C
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum

View File

@ -1,161 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ 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/>.
Description
\*---------------------------------------------------------------------------*/
#include "NamedEnum.H"
#include "Enum.H"
#include "IOstreams.H"
using namespace Foam;
class namedEnumTest
{
public:
enum class option
{
A,
B,
C,
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;
};
template<>
const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
{
"a",
"b",
"c",
"d",
};
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[])
{
Info<<"NamedEnum: " << namedEnumTest::optionNamed << nl;
Info<<"Enum: " << namedEnumTest::optionEnum << nl;
Info<<"Enum: " << namedEnumTest::optionEnum2 << nl;
dictionary testDict;
testDict.add("lookup1", "c");
Info<< nl
<< 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;
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<< "got: "
<< int
(
namedEnumTest::optionEnum2.lookupOrDefault
(
"lookup1",
testDict,
namedEnumTest::option::A
)
)
<< nl;
}
Info<< "--- test read ---" << endl;
namedEnumTest::option dummy(namedEnumTest::optionNamed.read(Sin));
Info<< namedEnumTest::optionNamed[dummy] << endl;
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //

View File

@ -300,11 +300,7 @@ int main(int argc, char *argv[])
const bool flipNormals(dict.get<bool>("flipNormals"));
// What to extrude
const ExtrudeMode mode = ExtrudeModeNames.lookup
(
"constructFrom",
dict
);
const ExtrudeMode mode = ExtrudeModeNames.get("constructFrom", dict);
// Any merging of small edges
const scalar mergeTol(dict.lookupOrDefault<scalar>("mergeTol", 1e-4));

View File

@ -1534,7 +1534,7 @@ int main(int argc, char *argv[])
mappedPatchBase::sampleMode sampleMode =
mappedPatchBase::sampleModeNames_.lookup("sampleMode", dict);
mappedPatchBase::sampleModeNames_.get("sampleMode", dict);
const bool oneD(dict.get<bool>("oneD"));
bool oneDNonManifoldEdges(false);

View File

@ -49,13 +49,13 @@ const Foam::Enum
Foam::conformalVoronoiMesh::dualMeshPointType
>
Foam::conformalVoronoiMesh::dualMeshPointTypeNames_
{
({
{ dualMeshPointType::internal, "internal" },
{ dualMeshPointType::surface, "surface" },
{ dualMeshPointType::featureEdge, "featureEdge" },
{ dualMeshPointType::featurePoint, "featurePoint" },
{ dualMeshPointType::constrained, "constrained" },
};
});
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //

View File

@ -32,14 +32,14 @@ const Foam::Enum
Foam::indexedCellEnum::cellTypes
>
Foam::indexedCellEnum::cellTypesNames_
{
({
{ cellTypes::ctUnassigned, "Unassigned" },
{ cellTypes::ctFar, "Far" },
{ cellTypes::ctInternal, "Internal" },
{ cellTypes::ctSurface, "Surface" },
{ cellTypes::ctFeatureEdge, "FeatureEdge" },
{ cellTypes::ctFeaturePoint,"FeaturePoint" },
};
});
// ************************************************************************* //

View File

@ -33,7 +33,7 @@ const Foam::Enum
Foam::indexedVertexEnum::vertexType
>
Foam::indexedVertexEnum::vertexTypeNames_
{
({
{ vertexType::vtUnassigned, "Unassigned" },
{ vertexType::vtInternal, "Internal" },
{ vertexType::vtInternalNearBoundary, "InternalNearBoundary" },
@ -49,7 +49,7 @@ Foam::indexedVertexEnum::vertexTypeNames_
{ vertexType::vtExternalFeaturePoint, "ExternalFeaturePoint" },
{ vertexType::vtFar, "Far" },
{ vertexType::vtConstrained, "Constrained" },
};
});
const Foam::Enum

View File

@ -242,7 +242,7 @@ int main(int argc, char *argv[])
const word setType(dict.get<word>("type"));
const topoSetSource::setAction action =
topoSetSource::actionNames.lookup("action", dict);
topoSetSource::actionNames.get("action", dict);
autoPtr<topoSet> currentSet;
if

View File

@ -38,9 +38,11 @@ const Foam::Enum
Foam::vector::components
>
Foam::channelIndex::vectorComponentsNames_
(
Foam::vector::components::X, { "x", "y", "z" }
);
({
{ vector::components::X, "x" },
{ vector::components::Y, "y" },
{ vector::components::Z, "z" },
});
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -224,7 +226,7 @@ Foam::channelIndex::channelIndex
)
:
symmetric_(dict.get<bool>("symmetric")),
dir_(vectorComponentsNames_.lookup("component", dict))
dir_(vectorComponentsNames_.get("component", dict))
{
const polyBoundaryMesh& patches = mesh.boundaryMesh();

View File

@ -36,12 +36,12 @@ const Foam::Enum
Foam::solverTemplate::solverType
>
Foam::solverTemplate::solverTypeNames_
{
({
{ solverType::stCompressible, "compressible" },
{ solverType::stIncompressible, "incompressible" },
{ solverType::stBuoyant, "buoyant" },
{ solverType::stUnknown, "unknown" },
};
});
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -255,10 +255,10 @@ Foam::solverTemplate::solverTemplate
Info<< "Selecting " << solverName << ": ";
solverType_ = solverTypeNames_.lookup("solverType", solverDict);
Info<< solverTypeNames_[solverType_];
solverType_ = solverTypeNames_.get("solverType", solverDict);
multiRegion_ = solverDict.get<bool>("multiRegion");
Info<< solverTypeNames_[solverType_];
if (multiRegion_)
{
Info<< ", multi-region";

View File

@ -65,12 +65,12 @@ const Foam::Enum
shapeSelector::shapeType
>
shapeSelector::shapeTypeNames
{
({
{ shapeSelector::shapeType::PLANE, "plane" },
{ shapeSelector::shapeType::SPHERE, "sphere" },
{ shapeSelector::shapeType::CYLINDER, "cylinder" },
{ shapeSelector::shapeType::SIN, "sin" },
};
});
int main(int argc, char *argv[])
@ -95,7 +95,7 @@ int main(int argc, char *argv[])
const shapeSelector::shapeType surfType
(
shapeSelector::shapeTypeNames.lookup("type", dict)
shapeSelector::shapeTypeNames.get("type", dict)
);
const vector centre(dict.get<vector>("centre"));
const word fieldName(dict.get<word>("field"));

View File

@ -48,9 +48,9 @@ const Foam::Enum
Foam::tabulatedWallFunctions::general::interpolationType
>
Foam::tabulatedWallFunctions::general::interpolationTypeNames_
{
({
{ interpolationType::itLinear, "linear" },
};
});
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
@ -132,7 +132,7 @@ Foam::tabulatedWallFunctions::general::general
)
:
tabulatedWallFunction(dict, mesh, typeName),
interpType_(interpolationTypeNames_.lookup("interpType", coeffDict_)),
interpType_(interpolationTypeNames_.get("interpType", coeffDict_)),
yPlus_(),
uPlus_(),
log10YPlus_(coeffDict_.lookup("log10YPlus")),