Enum.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "Enum.H"
29 #include "dictionary.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class EnumType>
35 (
36  std::initializer_list<std::pair<EnumType, const char*>> list
37 )
38 :
39  keys_(list.size()),
40  vals_(list.size())
41 {
42  label i = 0;
43  for (const auto& pair : list)
44  {
45  keys_[i] = pair.second;
46  vals_[i] = int(pair.first);
47  ++i;
48  }
49 }
50 
51 
52 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
53 
54 template<class EnumType>
56 (
57  std::initializer_list<std::pair<EnumType, const char*>> list
58 )
59 {
60  label i = size();
61 
62  keys_.resize(i + list.size());
63  vals_.resize(i + list.size());
64 
65  for (const auto& pair : list)
66  {
67  keys_[i] = pair.second;
68  vals_[i] = int(pair.first);
69  ++i;
70  }
71 }
72 
73 
74 template<class EnumType>
75 EnumType Foam::Enum<EnumType>::get(const word& enumName) const
76 {
77  const label idx = keys_.find(enumName);
78 
79  if (idx < 0)
80  {
82  << enumName << " is not in enumeration: " << *this << nl
83  << exit(FatalError);
84  }
85 
86  return EnumType(vals_[idx]);
87 }
88 
89 
90 template<class EnumType>
92 (
93  const word& enumName,
94  const EnumType deflt
95 ) const
96 {
97  const label idx = keys_.find(enumName);
98 
99  if (idx < 0)
100  {
101  return deflt;
102  }
103 
104  return EnumType(vals_[idx]);
105 }
106 
107 
108 template<class EnumType>
109 EnumType Foam::Enum<EnumType>::read(Istream& is) const
110 {
111  const word enumName(is);
112 
113  const label idx = keys_.find(enumName);
114 
115  if (idx < 0)
116  {
118  << enumName << " is not in enumeration: " << *this << nl
119  << exit(FatalIOError);
120  }
122  return EnumType(vals_[idx]);
123 }
124 
125 
126 template<class EnumType>
128 (
129  Istream& is,
130  EnumType& val,
131  const bool mandatory
132 ) const
133 {
134  const word enumName(is);
135 
136  const label idx = keys_.find(enumName);
137 
138  if (idx >= 0)
139  {
140  val = EnumType(vals_[idx]);
141  return true;
142  }
143 
144  if (mandatory)
145  {
147  << enumName << " is not in enumeration: " << *this << nl
148  << exit(FatalIOError);
149  }
151  return false;
152 }
153 
154 
155 template<class EnumType>
157 (
158  const word& key,
159  const dictionary& dict
160 ) const
161 {
162  const word enumName(dict.get<word>(key, keyType::LITERAL));
163 
164  const label idx = keys_.find(enumName);
165 
166  if (idx < 0)
167  {
169  << "Lookup:" << key << " enumeration " << enumName
170  << " is not in enumeration: " << *this << nl
171  << exit(FatalIOError);
172  }
174  return EnumType(vals_[idx]);
175 }
176 
177 
178 template<class EnumType>
180 (
181  const word& key,
182  const dictionary& dict,
183  const EnumType deflt,
184  const bool warnOnly
185 ) const
186 {
187  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
188 
189  if (eptr)
190  {
191  const word enumName(eptr->get<word>());
192 
193  const label idx = keys_.find(enumName);
194 
195  if (idx >= 0)
196  {
197  return EnumType(vals_[idx]);
198  }
199 
200  // Found the dictionary entry, but the name not in enumeration
201 
202  if (warnOnly)
203  {
205  << "Lookup:" << key << " enumeration " << enumName
206  << " is not in enumeration: " << *this << nl
207  << "using default " << get(deflt)
208  << " (value " << int(deflt) << ')' << endl;
209  }
210  else
211  {
213  << "Lookup:" << key << " enumeration " << enumName
214  << " is not in enumeration: " << *this << nl
215  << exit(FatalIOError);
216  }
217  }
219  return deflt;
220 }
221 
222 
223 template<class EnumType>
225 (
226  const word& key,
227  const dictionary& dict,
228  EnumType& val,
229  const bool mandatory,
230  const bool warnOnly
231 ) const
232 {
233  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
234 
235  if (eptr)
236  {
237  const word enumName(eptr->get<word>());
238 
239  const label idx = keys_.find(enumName);
240 
241  if (idx >= 0)
242  {
243  val = EnumType(vals_[idx]);
244  return true;
245  }
246 
247  // Found the dictionary entry, but the name not in enumeration
248 
249  if (warnOnly)
250  {
252  << "Lookup:" << key << " enumeration " << enumName
253  << " is not in enumeration: " << *this << nl
254  << "leaving value unchanged"
255  << " (value " << int(val) << ')' << endl;
256  }
257  else
258  {
260  << "Lookup:" << key << " enumeration " << enumName
261  << " is not in enumeration: " << *this << nl
262  << exit(FatalIOError);
263  }
264  }
265  else if (mandatory)
266  {
268  << "Lookup:" << key
269  << " not found in dictionary " << dict.name() << nl
270  << exit(FatalIOError);
271  }
272 
273  return false;
274 }
275 
276 
277 // ************************************************************************* //
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:68
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: Enum.C:102
void push_back(std::initializer_list< std::pair< EnumType, const char *>> list)
Append value/key pairs to the lists of known enumerations.
Definition: Enum.C:49
dictionary dict
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name...
Definition: Enum.C:173
A class for handling words, derived from Foam::string.
Definition: word.H:63
T get() const
Get a T from the stream, FatalIOError if the number of tokens is incorrect.
Definition: entry.H:338
bool readEntry(const word &key, const dictionary &dict, EnumType &val, const bool mandatory=true, const bool warnOnly=false) const
Find entry and assign to T val.
Definition: Enum.C:218
Enum() noexcept=default
Default construct, an empty list.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:85
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...