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-2026 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 #include "printDictionary.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Enum, unsigned int nEnum>
34 :
35  FixedList<word, nEnum>(names),
36  table_(2*nEnum)
37 {
38  forAll(names, ei)
39  {
40  table_.insert(names[ei], ei);
41  }
42 }
43 
44 
45 template<class Enum, unsigned int nEnum>
46 Foam::NamedEnum<Enum, nEnum>::NamedEnum(std::initializer_list<word> lst)
47 :
48  NamedEnum(FixedList<word, nEnum>(lst))
49 {}
50 
51 
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 
54 template<class Enum, unsigned int nEnum>
56 {
57  const word name(is);
58 
60 
61  if (iter == HashTable<unsigned int>::end())
62  {
64  << name << " is not in enumeration: "
65  << *this << exit(FatalIOError);
66  }
67 
68  return Enum(iter());
69 }
70 
71 
72 template<class Enum, unsigned int nEnum>
74 (
75  const word& name,
76  const dictionary& dict,
77  const Enum defaultValue
78 ) const
79 {
80  if (dict.found(name))
81  {
82  return read(dict.lookup(name));
83  }
84  else
85  {
87  {
88  printDictionary::defaults(dict).add(name, operator[](defaultValue));
89  }
90  return defaultValue;
91  }
92 }
93 
94 
95 template<class Enum, unsigned int nEnum>
97 {
98  const FixedList<word, nEnum>& names = *this;
99 
100  Enum selection = Enum(0);
101  unsigned int nSelections = 0;
102  forAll(names, ei)
103  {
104  if (dict.found(names[ei]))
105  {
106  selection = Enum(ei);
107  nSelections++;
108  }
109  }
110 
111  if (nSelections == 0)
112  {
114  << "None of the options selected, please specify one of: "
115  << names << exit(FatalIOError);
116  }
117  else if (nSelections > 1)
118  {
120  << "More than one option selected, please specify one of: "
121  << names << exit(FatalIOError);
122  }
123 
124  return selection;
125 }
126 
127 
128 template<class Enum, unsigned int nEnum>
129 void Foam::NamedEnum<Enum, nEnum>::write(const Enum e, Ostream& os) const
130 {
131  os << operator[](e);
132 }
133 
134 
135 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
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:55
Enum select(const dictionary &dict) const
Select the name from the dictionary and return the.
Definition: NamedEnum.C:96
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:74
NamedEnum(const FixedList< word, nEnum > &names)
Construct from list of names.
Definition: NamedEnum.C:33
void write(const Enum e, Ostream &) const
Write the name representation of the enumeration to an Ostream.
Definition: NamedEnum.C:129
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:669
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1019
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
static bool haveDefaults(const dictionary &dict)
Return if a dictionary exists to add defaults to for a given.
static dictionary & defaults(const dictionary &dict)
Return the dictionary to add defaults to for a given dictionary.
A class for handling words, derived from string.
Definition: word.H:63
#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
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict