NamedEnum.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "NamedEnum.H"
27 #include "dictionary.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Enum, unsigned int nEnum>
33 :
34  FixedList<word, nEnum>(names),
35  table_(2*nEnum)
36 {
37  forAll(names, ei)
38  {
39  table_.insert(names[ei], ei);
40  }
41 }
42 
43 
44 template<class Enum, unsigned int nEnum>
45 Foam::NamedEnum<Enum, nEnum>::NamedEnum(std::initializer_list<word> lst)
46 :
47  NamedEnum(FixedList<word, nEnum>(lst))
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
53 template<class Enum, unsigned int nEnum>
55 {
56  const word name(is);
57 
59 
60  if (iter == HashTable<unsigned int>::end())
61  {
63  << name << " is not in enumeration: "
64  << *this << exit(FatalIOError);
65  }
66 
67  return Enum(iter());
68 }
69 
70 
71 template<class Enum, unsigned int nEnum>
73 (
74  const word& name,
75  const dictionary& dict,
76  const Enum defaultValue
77 ) const
78 {
79  return dict.found(name)
80  ? read(dict.lookup(name))
81  : defaultValue;
82 }
83 
84 
85 template<class Enum, unsigned int nEnum>
87 {
88  const FixedList<word, nEnum>& names = *this;
89 
90  Enum selection = Enum(0);
91  unsigned int nSelections = 0;
92  forAll(names, ei)
93  {
94  if (dict.found(names[ei]))
95  {
96  selection = Enum(ei);
97  nSelections++;
98  }
99  }
100 
101  if (nSelections == 0)
102  {
104  << "None of the options selected, please specify one of: "
105  << names << exit(FatalIOError);
106  }
107  else if (nSelections > 1)
108  {
110  << "More than one option selected, please specify one of: "
111  << names << exit(FatalIOError);
112  }
113 
114  return selection;
115 }
116 
117 
118 template<class Enum, unsigned int nEnum>
119 void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
120 {
121  os << operator[](e);
122 }
123 
124 
125 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
An STL-conforming hash table.
Definition: HashTable.H:127
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:167
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:54
Enum select(const dictionary &dict) const
Select the name from the dictionary and return the.
Definition: NamedEnum.C:86
Enum lookupOrDefault(const word &name, const dictionary &dict, const Enum defaultValue) const
Lookup the name from the dictionary if present and return the.
Definition: NamedEnum.C:73
NamedEnum(const FixedList< word, nEnum > &names)
Construct from list of names.
Definition: NamedEnum.C:32
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:119
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:740
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:539
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const doubleScalar e
Definition: doubleScalar.H:106
bool read(const char *, int32_t &)
Definition: int32IO.C:85
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict