mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: cleanup of NamedEnum
- Remove the unused enums() method since it delivers wholly unreliable results. It is not guaranteed to cover the full enumeration range, but only the listed names. - Remove the unused strings() method. Duplicated functionality of the words(), but was never used. - Change access of words() method from static to object. Better code isolation. Permits the constructor to take over as the single point of failure for bad input. - Add values() method - do not expose internal (HashTable) lookup since it makes it more difficult to enforce constness and the implementation detail should not be exposed. However leave toc() and sortedToc() for the interface. STYLE: relocated NamedEnum under primitives (was containers) - internal typedef as 'value_type' for some consistency with STL conventions
This commit is contained in:
@ -34,15 +34,15 @@ class namedEnumTest
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum option
|
enum class option
|
||||||
{
|
{
|
||||||
a,
|
A,
|
||||||
b,
|
B,
|
||||||
c,
|
C,
|
||||||
d
|
D
|
||||||
};
|
};
|
||||||
|
|
||||||
static const Foam::NamedEnum<option, 4> namedEnum;
|
static const Foam::NamedEnum<option, 4> optionNamed;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -52,10 +52,10 @@ const char* Foam::NamedEnum<namedEnumTest::option, 4>::names[] =
|
|||||||
"a",
|
"a",
|
||||||
"b",
|
"b",
|
||||||
"c",
|
"c",
|
||||||
"d"
|
"d",
|
||||||
};
|
};
|
||||||
|
|
||||||
const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::namedEnum;
|
const Foam::NamedEnum<namedEnumTest::option, 4> namedEnumTest::optionNamed;
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
@ -69,56 +69,43 @@ int main(int argc, char *argv[])
|
|||||||
dictionary testDict;
|
dictionary testDict;
|
||||||
testDict.add("lookup1", "c");
|
testDict.add("lookup1", "c");
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Info<< "loop over enums (C++11 for range):" << nl;
|
|
||||||
for (const auto& opt : options)
|
|
||||||
{
|
|
||||||
Info<< "option[" << opt
|
|
||||||
<< "] = '" << namedEnumTest::namedEnum[opt] << "'" << nl;
|
|
||||||
}
|
|
||||||
|
|
||||||
Info<< nl
|
Info<< nl
|
||||||
<< namedEnumTest::namedEnum["a"] << nl
|
<< int(namedEnumTest::optionNamed["a"]) << nl
|
||||||
<< namedEnumTest::namedEnum[namedEnumTest::a] << nl;
|
<< namedEnumTest::optionNamed[namedEnumTest::option::A] << nl;
|
||||||
|
|
||||||
Info<< "--- test dictionary lookup ---" << endl;
|
Info<< "--- test dictionary lookup ---" << endl;
|
||||||
{
|
{
|
||||||
Info<< "dict: " << testDict << endl;
|
Info<< "dict: " << testDict << endl;
|
||||||
|
|
||||||
namedEnumTest::option gotOpt =
|
Info<< "got: "
|
||||||
namedEnumTest::namedEnum.lookupOrDefault
|
<< int
|
||||||
(
|
(
|
||||||
"test",
|
namedEnumTest::optionNamed.lookupOrDefault
|
||||||
|
(
|
||||||
|
"notFound",
|
||||||
testDict,
|
testDict,
|
||||||
namedEnumTest::option::a
|
namedEnumTest::option::A
|
||||||
);
|
)
|
||||||
|
)
|
||||||
|
<< nl;
|
||||||
|
|
||||||
Info<< "got: " << gotOpt << endl;
|
Info<< "got: "
|
||||||
|
<< int
|
||||||
gotOpt = namedEnumTest::namedEnum.lookupOrDefault
|
(
|
||||||
|
namedEnumTest::optionNamed.lookupOrDefault
|
||||||
(
|
(
|
||||||
"lookup1",
|
"lookup1",
|
||||||
testDict,
|
testDict,
|
||||||
namedEnumTest::option::a
|
namedEnumTest::option::A
|
||||||
);
|
)
|
||||||
|
)
|
||||||
Info<< "got: " << gotOpt << endl;
|
<< nl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Info<< "--- test read construction ---" << endl;
|
Info<< "--- test read ---" << endl;
|
||||||
|
|
||||||
namedEnumTest::option dummy(namedEnumTest::namedEnum.read(Sin));
|
namedEnumTest::option dummy(namedEnumTest::optionNamed.read(Sin));
|
||||||
Info<< namedEnumTest::namedEnum[dummy] << endl;
|
Info<< namedEnumTest::optionNamed[dummy] << endl;
|
||||||
|
|
||||||
Info<< "End\n" << endl;
|
Info<< "End\n" << endl;
|
||||||
|
|
||||||
|
|||||||
@ -44,22 +44,9 @@ void Foam::DelaunayMeshTools::writeOBJ
|
|||||||
OFstream str(fName);
|
OFstream str(fName);
|
||||||
|
|
||||||
Pout<< nl
|
Pout<< nl
|
||||||
<< "Writing points of types:" << nl;
|
<< "Writing points of types ("
|
||||||
|
<< int(startPointType) << "-" << int(endPointType)
|
||||||
forAllConstIter
|
<< ") to " << str.name() << endl;
|
||||||
(
|
|
||||||
HashTable<int>,
|
|
||||||
indexedVertexEnum::vertexTypeNames_,
|
|
||||||
iter
|
|
||||||
)
|
|
||||||
{
|
|
||||||
if (iter() >= startPointType && iter() <= endPointType)
|
|
||||||
{
|
|
||||||
Pout<< " " << iter.key() << nl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Pout<< "to " << str.name() << endl;
|
|
||||||
|
|
||||||
for
|
for
|
||||||
(
|
(
|
||||||
@ -265,7 +252,7 @@ void Foam::DelaunayMeshTools::drawDelaunayCell
|
|||||||
<< "f " << 1 + offset << " " << 4 + offset << " " << 3 + offset << nl
|
<< "f " << 1 + offset << " " << 4 + offset << " " << 3 + offset << nl
|
||||||
<< "f " << 1 + offset << " " << 2 + offset << " " << 4 + offset << endl;
|
<< "f " << 1 + offset << " " << 2 + offset << " " << 4 + offset << endl;
|
||||||
|
|
||||||
// os << "# cicumcentre " << endl;
|
// os << "# circumcentre " << endl;
|
||||||
|
|
||||||
// meshTools::writeOBJ(os, c->dual());
|
// meshTools::writeOBJ(os, c->dual());
|
||||||
|
|
||||||
|
|||||||
@ -229,7 +229,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
|
|
||||||
word patchMapMethod;
|
word patchMapMethod;
|
||||||
if (meshToMesh::interpolationMethodNames_.found(mapMethod))
|
if (meshToMesh::interpolationMethodNames_.hasEnum(mapMethod))
|
||||||
{
|
{
|
||||||
// Lookup corresponding AMI method
|
// Lookup corresponding AMI method
|
||||||
meshToMesh::interpolationMethod method =
|
meshToMesh::interpolationMethod method =
|
||||||
|
|||||||
@ -25,14 +25,48 @@ License
|
|||||||
|
|
||||||
#include "NamedEnum.H"
|
#include "NamedEnum.H"
|
||||||
#include "dictionary.H"
|
#include "dictionary.H"
|
||||||
|
#include "stdFoam.H"
|
||||||
|
|
||||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
template<class StringType>
|
Foam::NamedEnum<EnumType, nEnum>::NamedEnum()
|
||||||
Foam::List<StringType> Foam::NamedEnum<Enum, nEnum>::getNamesList()
|
:
|
||||||
|
lookup_(2*nEnum)
|
||||||
{
|
{
|
||||||
List<StringType> lst(nEnum);
|
for (int enumi=0; enumi < nEnum; ++enumi)
|
||||||
|
{
|
||||||
|
if (names[enumi] && names[enumi][0])
|
||||||
|
{
|
||||||
|
lookup_.insert(names[enumi], enumi);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Bad name - generate error message
|
||||||
|
List<string> goodNames(enumi);
|
||||||
|
|
||||||
|
for (int i = 0; i < enumi; ++i)
|
||||||
|
{
|
||||||
|
goodNames[i] = names[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
FatalErrorInFunction
|
||||||
|
<< "Illegal enumeration name at position " << enumi << nl
|
||||||
|
<< "after entries " << goodNames << nl
|
||||||
|
<< "Possibly your NamedEnum<EnumType, nEnum>::names array"
|
||||||
|
<< " is not of size " << nEnum << endl
|
||||||
|
<< abort(FatalError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
Foam::wordList Foam::NamedEnum<EnumType, nEnum>::words() const
|
||||||
|
{
|
||||||
|
List<word> lst(nEnum);
|
||||||
|
|
||||||
label count = 0;
|
label count = 0;
|
||||||
for (int enumi=0; enumi < nEnum; ++enumi)
|
for (int enumi=0; enumi < nEnum; ++enumi)
|
||||||
@ -48,93 +82,74 @@ Foam::List<StringType> Foam::NamedEnum<Enum, nEnum>::getNamesList()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
template<class EnumType, int nEnum>
|
||||||
|
Foam::List<int> Foam::NamedEnum<EnumType, nEnum>::values() const
|
||||||
template<class Enum, int nEnum>
|
|
||||||
Foam::NamedEnum<Enum, nEnum>::NamedEnum()
|
|
||||||
:
|
|
||||||
table_type(2*nEnum)
|
|
||||||
{
|
{
|
||||||
|
List<int> lst(nEnum);
|
||||||
|
|
||||||
|
label count = 0;
|
||||||
for (int enumi=0; enumi < nEnum; ++enumi)
|
for (int enumi=0; enumi < nEnum; ++enumi)
|
||||||
{
|
{
|
||||||
if (names[enumi] && names[enumi][0])
|
if (names[enumi] && names[enumi][0])
|
||||||
{
|
{
|
||||||
insert(names[enumi], enumi);
|
auto iter = lookup_.cfind(names[enumi]);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Bad name - generate error message
|
|
||||||
stringList goodNames(enumi);
|
|
||||||
|
|
||||||
for (int i = 0; i < enumi; ++i)
|
if (iter.found())
|
||||||
{
|
{
|
||||||
goodNames[i] = names[i];
|
lst[count++] = iter.object();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FatalErrorInFunction
|
lst.setSize(count);
|
||||||
<< "Illegal enumeration name at position " << enumi << nl
|
return lst;
|
||||||
<< "after entries " << goodNames << nl
|
|
||||||
<< "Possibly your NamedEnum<Enum, nEnum>::names array"
|
|
||||||
<< " is not of size " << nEnum << endl
|
|
||||||
<< abort(FatalError);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
template<class EnumType, int nEnum>
|
||||||
|
bool Foam::NamedEnum<EnumType, nEnum>::hasName(const EnumType e) const
|
||||||
template<class Enum, int nEnum>
|
|
||||||
Enum Foam::NamedEnum<Enum, nEnum>::read(Istream& is) const
|
|
||||||
{
|
{
|
||||||
const word enumName(is);
|
const int enumValue(e);
|
||||||
table_type::const_iterator iter = find(enumName);
|
|
||||||
|
|
||||||
if (!iter.found())
|
forAllConstIters(lookup_, iter)
|
||||||
{
|
{
|
||||||
FatalIOErrorInFunction(is)
|
if (iter.object() == enumValue)
|
||||||
<< enumName << " is not in enumeration: "
|
{
|
||||||
<< sortedToc() << exit(FatalIOError);
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return Enum(iter.object());
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
|
EnumType Foam::NamedEnum<EnumType, nEnum>::lookup
|
||||||
{
|
|
||||||
os << names[int(e)];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
|
||||||
Enum Foam::NamedEnum<Enum, nEnum>::lookup
|
|
||||||
(
|
(
|
||||||
const word& key,
|
const word& key,
|
||||||
const dictionary& dict
|
const dictionary& dict
|
||||||
) const
|
) const
|
||||||
{
|
{
|
||||||
const word enumName(dict.lookup(key));
|
const word enumName(dict.lookup(key));
|
||||||
table_type::const_iterator iter = find(enumName);
|
auto iter = lookup_.cfind(enumName);
|
||||||
|
|
||||||
if (!iter.found())
|
if (!iter.found())
|
||||||
{
|
{
|
||||||
FatalIOErrorInFunction(dict)
|
FatalIOErrorInFunction(dict)
|
||||||
<< enumName << " is not in enumeration: "
|
<< enumName << " is not in enumeration: "
|
||||||
<< sortedToc() << exit(FatalIOError);
|
<< lookup_.sortedToc() << nl
|
||||||
|
<< exit(FatalIOError);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Enum(iter.object());
|
return EnumType(iter.object());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
Enum Foam::NamedEnum<Enum, nEnum>::lookupOrDefault
|
EnumType Foam::NamedEnum<EnumType, nEnum>::lookupOrDefault
|
||||||
(
|
(
|
||||||
const word& key,
|
const word& key,
|
||||||
const dictionary& dict,
|
const dictionary& dict,
|
||||||
const enum_type deflt
|
const EnumType deflt
|
||||||
) const
|
) const
|
||||||
{
|
{
|
||||||
if (dict.found(key))
|
if (dict.found(key))
|
||||||
@ -148,36 +163,49 @@ Enum Foam::NamedEnum<Enum, nEnum>::lookupOrDefault
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
Foam::List<Enum> Foam::NamedEnum<Enum, nEnum>::enums()
|
EnumType Foam::NamedEnum<EnumType, nEnum>::read(Istream& is) const
|
||||||
{
|
{
|
||||||
List<Enum> lst(nEnum);
|
const word enumName(is);
|
||||||
|
auto iter = lookup_.cfind(enumName);
|
||||||
|
|
||||||
label count = 0;
|
if (!iter.found())
|
||||||
for (int enumi = 0; enumi < nEnum; ++enumi)
|
|
||||||
{
|
{
|
||||||
if (names[enumi] && names[enumi][0])
|
FatalIOErrorInFunction(is)
|
||||||
{
|
<< enumName << " is not in enumeration: "
|
||||||
lst[count++] = Enum(enumi);
|
<< lookup_.sortedToc() << nl
|
||||||
}
|
<< exit(FatalIOError);
|
||||||
}
|
}
|
||||||
|
|
||||||
lst.setSize(count);
|
return EnumType(iter.object());
|
||||||
return lst;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
Foam::stringList Foam::NamedEnum<Enum, nEnum>::strings()
|
void Foam::NamedEnum<EnumType, nEnum>::write
|
||||||
|
(
|
||||||
|
const EnumType e,
|
||||||
|
Ostream& os
|
||||||
|
) const
|
||||||
{
|
{
|
||||||
return getNamesList<string>();
|
const int idx = int(e);
|
||||||
|
if (idx >= 0 && idx < nEnum)
|
||||||
|
{
|
||||||
|
os << names[idx];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
|
||||||
Foam::wordList Foam::NamedEnum<Enum, nEnum>::words()
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
Foam::Ostream& Foam::operator<<
|
||||||
|
(
|
||||||
|
Ostream& os,
|
||||||
|
const NamedEnum<EnumType, nEnum>& wrapped
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return getNamesList<word>();
|
return wrapped.lookup_.writeKeys(os, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -25,9 +25,8 @@ Class
|
|||||||
Foam::NamedEnum
|
Foam::NamedEnum
|
||||||
|
|
||||||
Description
|
Description
|
||||||
A NamedEnum is a wrapper around a static list of names that represent
|
A NamedEnum is a wrapper around a list of names that represent
|
||||||
a particular enumeration. Internally it uses a HashTable for quicker
|
particular enumeration values.
|
||||||
lookups.
|
|
||||||
|
|
||||||
SourceFiles
|
SourceFiles
|
||||||
NamedEnum.C
|
NamedEnum.C
|
||||||
@ -38,7 +37,6 @@ SourceFiles
|
|||||||
#define NamedEnum_H
|
#define NamedEnum_H
|
||||||
|
|
||||||
#include "HashTable.H"
|
#include "HashTable.H"
|
||||||
#include "stringList.H"
|
|
||||||
#include "wordList.H"
|
#include "wordList.H"
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@ -46,33 +44,32 @@ SourceFiles
|
|||||||
|
|
||||||
namespace Foam
|
namespace Foam
|
||||||
{
|
{
|
||||||
|
// Forward declarations
|
||||||
class dictionary;
|
class dictionary;
|
||||||
|
template<class EnumType, int nEnum> class NamedEnum;
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
Ostream& operator<<(Ostream& os, const NamedEnum<EnumType, nEnum>& wrapped);
|
||||||
|
|
||||||
// Forward declaration
|
|
||||||
template<class Enum, int> class NamedEnum;
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*\
|
/*---------------------------------------------------------------------------*\
|
||||||
Class NamedEnum Declaration
|
Class NamedEnum Declaration
|
||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
template<class Enum, int nEnum>
|
template<class EnumType, int nEnum>
|
||||||
class NamedEnum
|
class NamedEnum
|
||||||
:
|
|
||||||
public HashTable<int>
|
|
||||||
{
|
{
|
||||||
//- The nEnum must be positive (non-zero)
|
//- The nEnum must be positive (non-zero)
|
||||||
static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
|
static_assert(nEnum > 0, "nEnum must be positive (non-zero)");
|
||||||
|
|
||||||
//- The type of HashTable used for the lookup.
|
// Private Member Data
|
||||||
typedef HashTable<int> table_type;
|
|
||||||
|
//- The values for the enum
|
||||||
|
HashTable<int> lookup_;
|
||||||
|
|
||||||
|
|
||||||
// Private Member Functions
|
// Private Member Functions
|
||||||
|
|
||||||
//- The names as a list of strings
|
|
||||||
template<class StringType>
|
|
||||||
static List<StringType> getNamesList();
|
|
||||||
|
|
||||||
//- Disallow default bitwise copy construct
|
//- Disallow default bitwise copy construct
|
||||||
NamedEnum(const NamedEnum&) = delete;
|
NamedEnum(const NamedEnum&) = delete;
|
||||||
|
|
||||||
@ -83,12 +80,12 @@ class NamedEnum
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
//- The type of enumeration wrapped by NamedEnum
|
//- The type of enumeration wrapped by NamedEnum
|
||||||
typedef Enum enum_type;
|
typedef EnumType value_type;
|
||||||
|
|
||||||
|
|
||||||
// Static data members
|
// Static data members
|
||||||
|
|
||||||
//- The set of names corresponding to the enumeration Enum
|
//- The set of names corresponding to the enumeration EnumType
|
||||||
static const char* names[nEnum];
|
static const char* names[nEnum];
|
||||||
|
|
||||||
|
|
||||||
@ -100,17 +97,39 @@ public:
|
|||||||
|
|
||||||
// Member Functions
|
// Member Functions
|
||||||
|
|
||||||
//- Read a word from Istream and return the corresponding
|
// Access
|
||||||
// enumeration element
|
|
||||||
enum_type read(Istream& is) const;
|
|
||||||
|
|
||||||
//- Write the name representation of the enumeration to an Ostream
|
//- The number of lookup names for the enumeration
|
||||||
void write(const enum_type e, Ostream& os) const;
|
inline label size() const;
|
||||||
|
|
||||||
|
//- The list of enum names
|
||||||
|
inline wordList toc() const;
|
||||||
|
|
||||||
|
//- The sorted list of enum names
|
||||||
|
inline wordList sortedToc() const;
|
||||||
|
|
||||||
|
//- The list of enum names, in construction order
|
||||||
|
wordList words() const;
|
||||||
|
|
||||||
|
//- The list of enum values, in construction order
|
||||||
|
List<int> values() const;
|
||||||
|
|
||||||
|
|
||||||
|
// Query
|
||||||
|
|
||||||
|
//- Test if there is an enumeration corresponding to the given name.
|
||||||
|
inline bool hasEnum(const word& enumName) const;
|
||||||
|
|
||||||
|
//- Test if there is a name corresponding to the given enumeration.
|
||||||
|
bool hasName(const EnumType e) const;
|
||||||
|
|
||||||
|
|
||||||
|
// Lookup
|
||||||
|
|
||||||
//- Lookup the key in the dictionary and return the corresponding
|
//- Lookup the key in the dictionary and return the corresponding
|
||||||
// enumeration element based on its name.
|
// enumeration element based on its name.
|
||||||
// Fatal if anything is incorrect.
|
// Fatal if anything is incorrect.
|
||||||
enum_type lookup
|
EnumType lookup
|
||||||
(
|
(
|
||||||
const word& key,
|
const word& key,
|
||||||
const dictionary& dict
|
const dictionary& dict
|
||||||
@ -120,42 +139,42 @@ public:
|
|||||||
// enumeration element based on its name.
|
// enumeration element based on its name.
|
||||||
// Return the default value if the key was not found in the dictionary.
|
// Return the default value if the key was not found in the dictionary.
|
||||||
// Fatal if enumerated name was incorrect.
|
// Fatal if enumerated name was incorrect.
|
||||||
enum_type lookupOrDefault
|
EnumType lookupOrDefault
|
||||||
(
|
(
|
||||||
const word& key,
|
const word& key,
|
||||||
const dictionary& dict,
|
const dictionary& dict,
|
||||||
const enum_type deflt
|
const EnumType deflt
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
//- List of enumerations
|
|
||||||
static List<enum_type> enums();
|
|
||||||
|
|
||||||
//- The set of names as a list of strings
|
// IO
|
||||||
static stringList strings();
|
|
||||||
|
|
||||||
//- The set of names as a list of words
|
//- Read a word from Istream and return the corresponding enumeration
|
||||||
static wordList words();
|
EnumType read(Istream& is) const;
|
||||||
|
|
||||||
|
//- Write the name representation of the enumeration to an Ostream
|
||||||
|
// A noop if the enumeration wasn't found.
|
||||||
|
void write(const EnumType e, Ostream& os) const;
|
||||||
|
|
||||||
|
|
||||||
// Member Operators
|
// Member Operators
|
||||||
|
|
||||||
//- Return the enumeration element corresponding to the given name
|
//- Return the enumeration element corresponding to the given name
|
||||||
inline const enum_type operator[](const char* name) const
|
inline const EnumType operator[](const word& name) const;
|
||||||
{
|
|
||||||
return enum_type(table_type::operator[](name));
|
|
||||||
}
|
|
||||||
|
|
||||||
//- Return the enumeration element corresponding to the given name
|
|
||||||
inline const enum_type operator[](const word& name) const
|
|
||||||
{
|
|
||||||
return enum_type(table_type::operator[](name));
|
|
||||||
}
|
|
||||||
|
|
||||||
//- Return the name of the given enumeration element
|
//- Return the name of the given enumeration element
|
||||||
inline const char* operator[](const enum_type e) const
|
inline const char* operator[](const EnumType e) const;
|
||||||
{
|
|
||||||
return names[int(e)];
|
|
||||||
}
|
// IOstream operators
|
||||||
|
|
||||||
|
//- Write names to Ostream, as per writeKeys() with shortListLen=10
|
||||||
|
friend Ostream& operator<< <EnumType, nEnum>
|
||||||
|
(
|
||||||
|
Ostream& os,
|
||||||
|
const NamedEnum<EnumType, nEnum>& wrapped
|
||||||
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -165,6 +184,8 @@ public:
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
#include "NamedEnumI.H"
|
||||||
|
|
||||||
#ifdef NoRepository
|
#ifdef NoRepository
|
||||||
#include "NamedEnum.C"
|
#include "NamedEnum.C"
|
||||||
#endif
|
#endif
|
||||||
81
src/OpenFOAM/primitives/enums/NamedEnumI.H
Normal file
81
src/OpenFOAM/primitives/enums/NamedEnumI.H
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
========= |
|
||||||
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||||
|
\\ / O peration |
|
||||||
|
\\ / A nd | Copyright (C) 2017 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/>.
|
||||||
|
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline Foam::label Foam::NamedEnum<EnumType, nEnum>::size() const
|
||||||
|
{
|
||||||
|
return lookup_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline Foam::wordList Foam::NamedEnum<EnumType, nEnum>::toc() const
|
||||||
|
{
|
||||||
|
return lookup_.toc();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline Foam::wordList Foam::NamedEnum<EnumType, nEnum>::sortedToc() const
|
||||||
|
{
|
||||||
|
return lookup_.sortedToc();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline bool Foam::NamedEnum<EnumType, nEnum>::hasEnum
|
||||||
|
(
|
||||||
|
const word& enumName
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
return lookup_.found(enumName);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline const EnumType Foam::NamedEnum<EnumType, nEnum>::operator[]
|
||||||
|
(
|
||||||
|
const word& name
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
return EnumType(lookup_[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class EnumType, int nEnum>
|
||||||
|
inline const char* Foam::NamedEnum<EnumType, nEnum>::operator[]
|
||||||
|
(
|
||||||
|
const EnumType e
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
return names[int(e)];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ************************************************************************* //
|
||||||
@ -111,7 +111,7 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
|
|||||||
{
|
{
|
||||||
typedef compressible::turbulenceModel turbulenceModel;
|
typedef compressible::turbulenceModel turbulenceModel;
|
||||||
|
|
||||||
word turbName(turbulenceModel::propertiesName);
|
const word turbName(turbulenceModel::propertiesName);
|
||||||
|
|
||||||
if
|
if
|
||||||
(
|
(
|
||||||
@ -205,8 +205,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
|
|||||||
<< " on mesh " << mesh.name() << " patch " << patch_.name()
|
<< " on mesh " << mesh.name() << " patch " << patch_.name()
|
||||||
<< nl
|
<< nl
|
||||||
<< "Please set 'kappaMethod' to one of "
|
<< "Please set 'kappaMethod' to one of "
|
||||||
<< KMethodTypeNames_.toc()
|
<< flatOutput(KMethodTypeNames_.sortedToc()) << nl
|
||||||
<< " and 'kappa' to the name of the volScalar"
|
<< "and 'kappa' to the name of the volScalar"
|
||||||
<< " or volSymmTensor field (if kappaMethod=lookup)"
|
<< " or volSymmTensor field (if kappaMethod=lookup)"
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
@ -219,8 +219,8 @@ Foam::tmp<Foam::scalarField> Foam::temperatureCoupledBase::kappa
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unimplemented method " << KMethodTypeNames_[method_] << nl
|
<< "Unimplemented method " << KMethodTypeNames_[method_] << nl
|
||||||
<< "Please set 'kappaMethod' to one of "
|
<< "Please set 'kappaMethod' to one of "
|
||||||
<< KMethodTypeNames_.toc()
|
<< flatOutput(KMethodTypeNames_.sortedToc()) << nl
|
||||||
<< " and 'kappa' to the name of the volScalar"
|
<< "and 'kappa' to the name of the volScalar"
|
||||||
<< " or volSymmTensor field (if kappaMethod=lookup)"
|
<< " or volSymmTensor field (if kappaMethod=lookup)"
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -375,7 +375,7 @@ bool Foam::fileFormats::FIREMeshReader::readGeometry(const scalar scaleFactor)
|
|||||||
IOstream::streamFormat fmt = IOstream::ASCII;
|
IOstream::streamFormat fmt = IOstream::ASCII;
|
||||||
|
|
||||||
const word ext = geometryFile_.ext();
|
const word ext = geometryFile_.ext();
|
||||||
bool supported = FIRECore::file3dExtensions.found(ext);
|
bool supported = FIRECore::file3dExtensions.hasEnum(ext);
|
||||||
if (supported)
|
if (supported)
|
||||||
{
|
{
|
||||||
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
|
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
|
||||||
|
|||||||
@ -278,7 +278,7 @@ bool Foam::fileFormats::FIREMeshWriter::write(const fileName& meshName) const
|
|||||||
{
|
{
|
||||||
const word ext = baseName.ext();
|
const word ext = baseName.ext();
|
||||||
|
|
||||||
if (FIRECore::file3dExtensions.found(ext))
|
if (FIRECore::file3dExtensions.hasEnum(ext))
|
||||||
{
|
{
|
||||||
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
|
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
|
||||||
if (fireFileType == FIRECore::POLY_ASCII)
|
if (fireFileType == FIRECore::POLY_ASCII)
|
||||||
|
|||||||
@ -143,7 +143,7 @@ bool Foam::functionObjects::volRegion::read
|
|||||||
{
|
{
|
||||||
FatalIOErrorInFunction(dict)
|
FatalIOErrorInFunction(dict)
|
||||||
<< "Unknown region type. Valid region types are:"
|
<< "Unknown region type. Valid region types are:"
|
||||||
<< regionTypeNames_
|
<< regionTypeNames_.toc()
|
||||||
<< exit(FatalIOError);
|
<< exit(FatalIOError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -564,9 +564,10 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
|
|||||||
{
|
{
|
||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< type() << " " << name() << ": "
|
<< type() << " " << name() << ": "
|
||||||
<< regionTypeNames_[regionType_] << "(" << regionName_ << "):"
|
<< int(regionType_) << "(" << regionName_ << "):"
|
||||||
<< nl << " Unknown region type. Valid region types are:"
|
<< nl << " Unknown region type. Valid region types are:"
|
||||||
<< regionTypeNames_.sortedToc() << nl << exit(FatalError);
|
<< regionTypeNames_ << nl
|
||||||
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -71,14 +71,14 @@ void Foam::functionObjects::mapFields::createInterpolation
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
const fvMesh& mapRegion = mapRegionPtr_();
|
const fvMesh& mapRegion = mapRegionPtr_();
|
||||||
word mapMethodName(dict.lookup("mapMethod"));
|
const word mapMethodName(dict.lookup("mapMethod"));
|
||||||
if (!meshToMesh::interpolationMethodNames_.found(mapMethodName))
|
if (!meshToMesh::interpolationMethodNames_.hasEnum(mapMethodName))
|
||||||
{
|
{
|
||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< type() << " " << name() << ": unknown map method "
|
<< type() << " " << name() << ": unknown map method "
|
||||||
<< mapMethodName << nl
|
<< mapMethodName << nl
|
||||||
<< "Available methods include: "
|
<< "Available methods include: "
|
||||||
<< meshToMesh::interpolationMethodNames_.sortedToc()
|
<< meshToMesh::interpolationMethodNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -196,7 +196,8 @@ bool Foam::functionObjects::writeObjects::write()
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unknown writeOption "
|
<< "Unknown writeOption "
|
||||||
<< writeOptionNames_[writeOption_]
|
<< writeOptionNames_[writeOption_]
|
||||||
<< ". Valid writeOption types are" << writeOptionNames_
|
<< ". Valid writeOption types are "
|
||||||
|
<< writeOptionNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -82,7 +82,8 @@ void Foam::fv::cellSetOption::setSelection(const dictionary& dict)
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unknown selectionMode "
|
<< "Unknown selectionMode "
|
||||||
<< selectionModeTypeNames_[selectionMode_]
|
<< selectionModeTypeNames_[selectionMode_]
|
||||||
<< ". Valid selectionMode types are" << selectionModeTypeNames_
|
<< ". Valid selectionMode types are "
|
||||||
|
<< selectionModeTypeNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,7 +187,8 @@ void Foam::fv::cellSetOption::setCellSet()
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unknown selectionMode "
|
<< "Unknown selectionMode "
|
||||||
<< selectionModeTypeNames_[selectionMode_]
|
<< selectionModeTypeNames_[selectionMode_]
|
||||||
<< ". Valid selectionMode types are" << selectionModeTypeNames_
|
<< ". Valid selectionMode types are "
|
||||||
|
<< selectionModeTypeNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -236,7 +236,7 @@ directionalPressureGradientExplicitSource
|
|||||||
<< "Did not find mode " << model_
|
<< "Did not find mode " << model_
|
||||||
<< nl
|
<< nl
|
||||||
<< "Please set 'model' to one of "
|
<< "Please set 'model' to one of "
|
||||||
<< PressureDropModelNames_.toc()
|
<< PressureDropModelNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -376,7 +376,8 @@ void Foam::fv::rotorDiskSource::createCoordinateSystem()
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unknown geometryMode " << geometryModeTypeNames_[gm]
|
<< "Unknown geometryMode " << geometryModeTypeNames_[gm]
|
||||||
<< ". Available geometry modes include "
|
<< ". Available geometry modes include "
|
||||||
<< geometryModeTypeNames_ << exit(FatalError);
|
<< geometryModeTypeNames_
|
||||||
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1070,7 +1070,7 @@ Foam::mappedPatchBase::mappedPatchBase
|
|||||||
{
|
{
|
||||||
offsetMode_ = offsetModeNames_.read(dict.lookup("offsetMode"));
|
offsetMode_ = offsetModeNames_.read(dict.lookup("offsetMode"));
|
||||||
|
|
||||||
switch(offsetMode_)
|
switch (offsetMode_)
|
||||||
{
|
{
|
||||||
case UNIFORM:
|
case UNIFORM:
|
||||||
{
|
{
|
||||||
@ -1109,7 +1109,7 @@ Foam::mappedPatchBase::mappedPatchBase
|
|||||||
(
|
(
|
||||||
dict
|
dict
|
||||||
) << "Please supply the offsetMode as one of "
|
) << "Please supply the offsetMode as one of "
|
||||||
<< NamedEnum<offsetMode, 3>::words()
|
<< offsetModeNames_
|
||||||
<< exit(FatalIOError);
|
<< exit(FatalIOError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -134,7 +134,7 @@ kappa() const
|
|||||||
<< " on mesh " << this->db().name() << " patch "
|
<< " on mesh " << this->db().name() << " patch "
|
||||||
<< patch().name()
|
<< patch().name()
|
||||||
<< " could not find a method in. Methods are: "
|
<< " could not find a method in. Methods are: "
|
||||||
<< methodTypeNames_.toc()
|
<< methodTypeNames_
|
||||||
<< " Not turbulenceModel or thermophysicalProperties"
|
<< " Not turbulenceModel or thermophysicalProperties"
|
||||||
<< " were found"
|
<< " were found"
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
|
|||||||
@ -222,7 +222,8 @@ Foam::radiation::boundaryRadiationPropertiesPatch::emissivity
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Please set 'mode' to one of " << methodTypeNames_.toc()
|
<< "Please set 'mode' to one of "
|
||||||
|
<< methodTypeNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -302,7 +303,7 @@ Foam::radiation::boundaryRadiationPropertiesPatch::absorptivity
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unimplemented method " << method_ << endl
|
<< "Unimplemented method " << method_ << endl
|
||||||
<< "Please set 'mode' to one of "
|
<< "Please set 'mode' to one of "
|
||||||
<< methodTypeNames_.toc()
|
<< methodTypeNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -382,7 +383,7 @@ Foam::radiation::boundaryRadiationPropertiesPatch::transmissivity
|
|||||||
FatalErrorInFunction
|
FatalErrorInFunction
|
||||||
<< "Unimplemented method " << method_ << endl
|
<< "Unimplemented method " << method_ << endl
|
||||||
<< "Please set 'mode' to one of "
|
<< "Please set 'mode' to one of "
|
||||||
<< methodTypeNames_.toc()
|
<< methodTypeNames_
|
||||||
<< exit(FatalError);
|
<< exit(FatalError);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user