Commit 8afc6cbd authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent 77a5b99e
......@@ -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;
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
\*---------------------------------------------------------------------------*/
#include "Enum.H"
#include "dictionary.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class EnumType>
Foam::label Foam::Enum<EnumType>::getIndex(const word& enumName) const
{
const label n = size();
for (label idx=0; idx < n; ++idx)
{
if (names_[idx] == enumName)
{
return idx;
}
}
return -1;
}
template<class EnumType>
Foam::label Foam::Enum<EnumType>::getIndex(const EnumType e) const
{
const int val = int(e);
const label n = size();
for (label idx=0; idx < n; ++idx)
{
if (values_[idx] == val)
{
return idx;
}
}
return -1;
}
template<class EnumType>
EnumType Foam::Enum<EnumType>::getEnum(const word& enumName) const
{
const label idx = getIndex(enumName);
if (idx < 0)
{
FatalErrorInFunction
<< enumName << " is not in enumeration: "
<< names_ << exit(FatalError);
}
return EnumType(values_[idx]);
}
template<class EnumType>
const Foam::word& Foam::Enum<EnumType>::getName(const EnumType e) const
{
const label idx = getIndex(e);
if (idx < 0)
{
return word::null;
}
else
{
return names_[idx];
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class EnumType>
Foam::Enum<EnumType>::Enum
(
std::initializer_list<std::pair<EnumType, word>> lst
)
:
names_(lst.size()),
values_(lst.size())
{
int idx = 0;
for (const auto& pair : lst)
{
names_[idx] = pair.second;
values_[idx] = int(pair.first);
++idx;
}
}
template<class EnumType>
Foam::Enum<EnumType>::Enum
(
const EnumType start,
std::initializer_list<word> lst
)
:
names_(lst.size()),
values_(lst.size())
{
int val = int(start);
int idx = 0;
for (const auto& key : lst)
{
names_[idx] = key;
values_[idx] = val;
++val;
++idx;
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class EnumType>
Foam::List<Foam::word> Foam::Enum<EnumType>::sortedToc() const
{
wordList lst(names_);
Foam::sort(lst);
return lst;
}
template<class EnumType>
EnumType Foam::Enum<EnumType>::read(Istream& is) const
{
const word enumName(is);
const label idx = getIndex(enumName);
if (idx < 0)
{
FatalIOErrorInFunction(is)
<< enumName << " is not in enumeration: "
<< names_ << nl
<< exit(FatalIOError);
}
return EnumType(values_[idx]);
}
template<class EnumType>
void Foam::Enum<EnumType>::write(const EnumType e, Ostream& os) const
{
const label idx = getIndex(e);
if (idx >= 0)
{
os << names_[idx];
}
}
template<class EnumType>
EnumType Foam::Enum<EnumType>::lookup
(
const word& key,
const dictionary& dict
) const
{
const word enumName(dict.lookup(key));
const label idx = getIndex(enumName);
if (idx < 0)
{
FatalIOErrorInFunction(dict)
<< enumName << " is not in enumeration: "
<< names_ << nl
<< exit(FatalIOError);
}
return EnumType(values_[idx]);
}
template<class EnumType>
EnumType Foam::Enum<EnumType>::lookupOrDefault
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const
{
if (dict.found(key))
{
return lookup(key, dict);
}
else
{
return deflt;
}
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class EnumType>
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const Enum<EnumType>& wrapped
)
{
return wrapped.names().writeList(os, 10);
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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/>.
Class
Foam::Enum
Description
A Enum is a wrapper around a list of names/values that represent
particular enumeration values.
SourceFiles
Enum.C
\*---------------------------------------------------------------------------*/
#ifndef Enum_H
#define Enum_H
#include "wordList.H"
#include <initializer_list>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declarations
class dictionary;
template<class EnumType> class Enum;
template<class EnumType>
Ostream& operator<<(Ostream& os, const Enum<EnumType>& wrapped);
/*---------------------------------------------------------------------------*\
Class Enum Declaration
\*---------------------------------------------------------------------------*/
template<class EnumType>
class Enum
{
// Private Member Data
//- The names for the enum
List<word> names_;
//- The values for the enum
List<int> values_;
// Private Member Functions
//- The index for the given name. -1 if not found.
label getIndex(const word& enumName) const;
//- The index for the given enumeration. -1 if not found.
label getIndex(const EnumType e) const;
//- Lookup enumeration corresponding to the given name.
// FatalError if not found.
EnumType getEnum(const word& enumName) const;
//- Lookup name corresponding to the given enumeration.
// Return empty word if not found.
const word& getName(const EnumType e) const;
//- Disallow default bitwise copy construct
Enum(const Enum&) = delete;
//- Disallow default bitwise assignment
void operator=(const Enum&) = delete;
public:
//- The type of enumeration wrapped by Enum
typedef EnumType value_type;
// Constructors
//- Construct from a values/names list.
// Duplicate values are permitted (eg, for aliases).
// Duplicate names are permitted, but won't make much sense.
explicit Enum(std::initializer_list<std::pair<EnumType, word>> lst);
//- Construct from a list of names with values incremented from the
// specified start value.
Enum(const EnumType start, std::initializer_list<word> lst);
// Member Functions
// Access
//- The number of lookup names for the enumeration
inline label size() const;
//- The list of enum names, in construction order
inline const List<word>& names() const;
//- The list of enum names, in construction order
inline const List<word>& toc() const;
//- The sorted list of enum names
List<word> sortedToc() const;
//- The list of enum values, in construction order
inline const 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.
inline bool hasName(const EnumType e) const;
// Lookup
//- Lookup the key in the dictionary and return the corresponding
// enumeration element based on its name.
// Fatal if anything is incorrect.
EnumType lookup
(
const word& key,
const dictionary& dict
) const;
//- Find the key in the dictionary and return the corresponding
// enumeration element based on its name.
// Return the default value if the key was not found in the dictionary.
// Fatal if the enumerated name was incorrect.
EnumType lookupOrDefault
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const;
// IO
//- Read a word from Istream and return the corresponding enumeration
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;
//- Write the names as a list to an Ostream
inline Ostream& writeList
(
Ostream& os,
const label shortListLen=0
) const;
// Member Operators
//- Return the enumeration corresponding to the given name
// Fatal if the name cannot be found.
inline const EnumType operator[](const word& name) const;
//- Return the first name corresponding to the given enumeration.
// Returns an empty word on failure.
inline const word& operator[](const EnumType e) const;
// IOstream operators
//- Write names to Ostream, as per writeList() with shortListLen=10
friend Ostream& operator<< <EnumType>
(
Ostream& os,
const Enum<EnumType>& wrapped
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "EnumI.H"
#ifdef NoRepository
#include "Enum.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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>
inline Foam::label Foam::Enum<EnumType>::size() const
{
return names_.size();