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-2020 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\*---------------------------------------------------------------------------*/
27
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 = 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 = find(enumName);
98
99 if (idx < 0)
100 {
101 return deflt;
102 }
103
104 return EnumType(vals_[idx]);
105}
106
107
108template<class EnumType>
110{
111 const word enumName(is);
112
113 const label idx = find(enumName);
114
115 if (idx < 0)
116 {
118 << enumName << " is not in enumeration: " << *this << nl
119 << exit(FatalIOError);
120 }
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
134 const word enumName(is);
135
136 const label idx = 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 }
150
151 return false;
152}
153
154
155template<class EnumType>
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 = find(enumName);
165
166 if (idx < 0)
167 {
169 << enumName << " is not in enumeration: " << *this << nl
170 << exit(FatalIOError);
171 }
172
173 return EnumType(vals_[idx]);
175
176
177template<class EnumType>
179(
180 const word& key,
181 const dictionary& dict,
182 const EnumType deflt,
183 const bool failsafe
184) const
185{
187
188 if (eptr)
189 {
190 const word enumName(eptr->get<word>());
191
192 const label idx = find(enumName);
193
194 if (idx >= 0)
195 {
196 return EnumType(vals_[idx]);
197 }
198
199 // Found the entry, but failed the name lookup
200
201 if (failsafe)
202 {
204 << enumName << " is not in enumeration: " << *this << nl
205 << "using failsafe " << get(deflt)
206 << " (value " << int(deflt) << ')' << endl;
207 }
208 else
209 {
211 << enumName << " is not in enumeration: " << *this << nl
212 << exit(FatalIOError);
213 }
214 }
215
216 return deflt;
217}
218
219
220template<class EnumType>
222(
223 const word& key,
224 const dictionary& dict,
225 EnumType& val,
226 const bool mandatory
227) const
228{
229 const entry* eptr = dict.findEntry(key, keyType::LITERAL);
230
231 if (eptr)
232 {
233 const word enumName(eptr->get<word>());
234
235 const label idx = find(enumName);
236
237 if (idx >= 0)
238 {
239 val = EnumType(vals_[idx]);
240 return true;
241 }
242
244 << enumName << " is not in enumeration: " << *this << nl
245 << exit(FatalIOError);
246 }
247 else if (mandatory)
248 {
250 << "'" << key << "' not found in dictionary " << dict.name() << nl
251 << exit(FatalIOError);
252 }
253
254 return false;
255}
256
257
258// ************************************************************************* //
Enum() noexcept=default
Default construct, an empty list.
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:179
bool readEntry(const word &key, const dictionary &dict, EnumType &val, const bool mandatory=true) const
Find entry and assign to T val.
Definition: Enum.C:222
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
virtual bool read()
Re-read model coefficients if they have changed.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find for an entry (non-const access) with the given keyword.
Definition: dictionaryI.H:97
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
T get() const
Definition: entry.H:269
@ LITERAL
String literal.
Definition: keyType.H:81
Lookup type of boundary radiation properties.
Definition: lookup.H:66
bool append() const noexcept
True if output format uses an append mode.
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict