Commit 3b745122 authored by Mark Olesen's avatar Mark Olesen
Browse files

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 ...
parent f2c78362
......@@ -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")),
......
......@@ -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),
......
Test-Enum.C
EXE = $(FOAM_USER_APPBIN)/Test-Enum
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -22,126 +22,131 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Description
Testing of Enum lookups.
\*---------------------------------------------------------------------------*/
#include "NamedEnum.H"
#include "Enum.H"
#include "IOstreams.H"
#include "dictionary.H"
#include "FlatOutput.H"
#include "IOstreams.H" // For 'Sin'
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;
};
#include <array>
using namespace Foam;
template<>
const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
struct testing
{
"a",
"b",
"c",
"d",
};
const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::optionNamed;
enum class option { A, B, C, D };
const Foam::Enum<namedEnumTest::otherOption> namedEnumTest::optionEnum
{
{ namedEnumTest::otherOption::A, "a" },
{ namedEnumTest::otherOption::B, "b" },
{ namedEnumTest::otherOption::C, "c" },
{ namedEnumTest::otherOption::D, "d" },
static const Foam::Enum<option> option1Names;
static const Foam::Enum<option> option2Names;
};
const Foam::Enum<namedEnumTest::option> namedEnumTest::optionEnum2
(
namedEnumTest::option::C,
{ "c", "d" }
);
// 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<<"NamedEnum: " << namedEnumTest::optionNamed << nl;
Info<<"Enum: " << namedEnumTest::optionEnum << nl;
Info<<"Enum: " << namedEnumTest::optionEnum2 << nl;
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(namedEnumTest::optionNamed["a"]) << nl
<< namedEnumTest::optionNamed[namedEnumTest::option::A] << nl;
Info<< nl
<< int(namedEnumTest::optionEnum["a"]) << nl
<< namedEnumTest::optionEnum[namedEnumTest::otherOption::A] << nl;
<< int(testing::option1Names["a"]) << nl
<< testing::option1Names[testing::option::A] << nl;
Info<< "--- test dictionary lookup ---" << endl;
{
Info<< "dict: " << testDict << endl;
Info<< "got: "
Info<< "lookupOrDefault(notFound) = "
<< int
(
namedEnumTest::optionNamed.lookupOrDefault
testing::option1Names.lookupOrDefault
(
"notFound",
testDict,
namedEnumTest::option::A
testing::option::A
)
)
<< nl;
Info<< "got: "
Info<< "lookupOrDefault(lookup1) = "
<< int
(
namedEnumTest::optionNamed.lookupOrDefault
testing::option1Names.lookupOrDefault
(
"lookup1",
testDict,
namedEnumTest::option::A
testing::option::A
)
)
<< nl;
Info<< "got: "
Info<< "lookupOrDefault(lookup1) = "
<< int
(
namedEnumTest::optionEnum2.lookupOrDefault
testing::option2Names.lookupOrDefault
(
"lookup1",
testDict,
namedEnumTest::option::A
testing::option::A
)
)
<< nl;
......@@ -149,10 +154,10 @@ int main(int argc, char *argv[])
Info<< "--- test read ---" << endl;
namedEnumTest::option dummy(namedEnumTest::optionNamed.read(Sin));
Info<< namedEnumTest::optionNamed[dummy] << endl;
testing::option dummy(testing::option1Names.read(Sin));
Info<< testing::option1Names[dummy] << endl;
Info<< "End\n" << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
......
Test-NamedEnum.C
EXE = $(FOAM_USER_APPBIN)/Test-NamedEnum
......@@ -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));
......
......@@ -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);
......
......@@ -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 * * * * * * * * * * * * //
......
......@@ -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" },
};
});
// ************************************************************************* //
......@@ -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
......
......@@ -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
......
......@@ -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();
......
......@@ -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";
......
......@@ -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"));
......
......@@ -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")),
......
......@@ -48,11 +48,11 @@ const Foam::Enum
Foam::fileMonitor::fileState
>
Foam::fileMonitor::fileStateNames_
{
({
{ fileState::UNMODIFIED, "unmodified" },
{ fileState::MODIFIED, "modified" },
{ fileState::DELETED, "deleted" },
};
});
namespace Foam
......
......@@ -30,15 +30,15 @@ License
const Foam::Enum
<
Foam::volumeType
Foam::volumeType::type
>
Foam::volumeType::names
{
{ type::UNKNOWN, "unknown" },
{ type::INSIDE, "inside" },
{ type::OUTSIDE, "outside" },
{ type::MIXED, "mixed" },
};
({
{ volumeType::type::UNKNOWN, "unknown" },
{ volumeType::type::INSIDE, "inside" },
{ volumeType::type::OUTSIDE, "outside" },
{ volumeType::type::MIXED, "mixed" },
});
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......
......@@ -70,7 +70,7 @@ public:
// Static data
//- Names for the classification enumeration
static const Enum<volumeType> names;
static const Enum<volumeType::type> names;
private:
......
......@@ -39,18 +39,18 @@ const Foam::Enum
Foam::IOobject::fileCheckTypes
>
Foam::IOobject::fileCheckTypesNames
{
({
{ fileCheckTypes::timeStamp, "timeStamp" },
{ fileCheckTypes::timeStampMaster, "timeStampMaster" },
{ fileCheckTypes::inotify, "inotify" },
{ fileCheckTypes::inotifyMaster, "inotifyMaster" },
};
});
// Default fileCheck type
Foam::IOobject::fileCheckTypes Foam::IOobject::fileModificationChecking
(
fileCheckTypesNames.lookup
fileCheckTypesNames.get
(
"fileModificationChecking",
debug::optimisationSwitches()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment