Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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\*---------------------------------------------------------------------------*/
28#include "Enum.H"
29#include "dictionary.H"
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<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
54template<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
74template<class EnumType>
75EnumType 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
90template<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;
103
104 return EnumType(vals_[idx]);
105}
106
107
108template<class EnumType>
109EnumType 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);
121
122 return EnumType(vals_[idx]);
123}
124
125
126template<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);
150
151 return false;
152}
153
154
155template<class EnumType>
157(
158 const word& key,
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);
173
174 return EnumType(vals_[idx]);
175}
176
177
178template<class EnumType>
180(
181 const word& key,
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 }
218
219 return deflt;
220}
221
222
223template<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);
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);
272
273 return false;
274}
275
276
277// ************************************************************************* //
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
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
Enum() noexcept=default
Default construct, an empty list.
label size() const noexcept
The number of name/value pairs for the enumeration.
Definition EnumI.H:31
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition Enum.C:102
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
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition Enum.C:85
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition Enum.C:68
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const fileName & name() const noexcept
The dictionary name.
Definition dictionaryI.H:41
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
T get() const
Get a T from the stream, FatalIOError if the number of tokens is incorrect.
Definition entry.H:326
@ LITERAL
String literal.
Definition keyType.H:82
A class for handling words, derived from Foam::string.
Definition word.H:66
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict