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-2019 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  List<word> list(keys_);
58 
59  Foam::sort(list);
60 
61  return list;
62 }
63 
64 
65 template<class EnumType>
67 (
68  std::initializer_list<std::pair<EnumType, const char*>> list
69 )
70 {
71  label i = size();
72 
73  keys_.resize(i + list.size());
74  vals_.resize(i + list.size());
75 
76  for (const auto& pair : list)
77  {
78  keys_[i] = pair.second;
79  vals_[i] = int(pair.first);
80  ++i;
81  }
82 }
83 
84 
85 template<class EnumType>
86 EnumType Foam::Enum<EnumType>::get(const word& enumName) const
87 {
88  const label idx = find(enumName);
89 
90  if (idx < 0)
91  {
93  << enumName << " is not in enumeration: " << *this << nl
94  << exit(FatalError);
95  }
96 
97  return EnumType(vals_[idx]);
98 }
99 
100 
101 template<class EnumType>
103 (
104  const word& enumName,
105  const EnumType deflt
106 ) const
107 {
108  const label idx = find(enumName);
109 
110  if (idx < 0)
111  {
112  return deflt;
113  }
114 
115  return EnumType(vals_[idx]);
116 }
117 
118 
119 template<class EnumType>
121 {
122  const word enumName(is);
123 
124  const label idx = find(enumName);
125 
126  if (idx < 0)
127  {
129  << enumName << " is not in enumeration: " << *this << nl
130  << exit(FatalIOError);
131  }
132 
133  return EnumType(vals_[idx]);
134 }
135 
136 
137 template<class EnumType>
139 (
140  Istream& is,
141  EnumType& e,
142  const bool mandatory
143 ) const
144 {
145  const word enumName(is);
146 
147  const label idx = find(enumName);
148 
149  if (idx >= 0)
150  {
151  e = EnumType(vals_[idx]);
152  return true;
153  }
154 
155  if (mandatory)
156  {
158  << enumName << " is not in enumeration: " << *this << nl
159  << exit(FatalIOError);
160  }
161 
162  return false;
163 }
164 
165 
166 template<class EnumType>
168 (
169  const word& key,
170  const dictionary& dict
171 ) const
172 {
173  const word enumName(dict.get<word>(key, keyType::LITERAL));
174 
175  const label idx = find(enumName);
176 
177  if (idx < 0)
178  {
180  << enumName << " is not in enumeration: " << *this << nl
181  << exit(FatalIOError);
182  }
183 
184  return EnumType(vals_[idx]);
185 }
186 
187 
188 template<class EnumType>
190 (
191  const word& key,
192  const dictionary& dict,
193  const EnumType deflt,
194  const bool failsafe
195 ) const
196 {
197  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
198 
199  if (eptr)
200  {
201  const word enumName(eptr->get<word>());
202 
203  const label idx = find(enumName);
204 
205  if (idx >= 0)
206  {
207  return EnumType(vals_[idx]);
208  }
209 
210  // Found the entry, but failed the name lookup
211 
212  if (failsafe)
213  {
215  << enumName << " is not in enumeration: " << *this << nl
216  << "using failsafe " << get(deflt)
217  << " (value " << int(deflt) << ')' << endl;
218  }
219  else
220  {
222  << enumName << " is not in enumeration: " << *this << nl
223  << exit(FatalIOError);
224  }
225  }
226 
227  return deflt;
228 }
229 
230 
231 template<class EnumType>
233 (
234  const word& key,
235  const dictionary& dict,
236  EnumType& val,
237  const bool mandatory
238 ) const
239 {
240  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
241 
242  if (eptr)
243  {
244  const word enumName(eptr->get<word>());
245 
246  const label idx = find(enumName);
247 
248  if (idx >= 0)
249  {
250  val = EnumType(vals_[idx]);
251  return true;
252  }
253 
255  << enumName << " is not in enumeration: " << *this << nl
256  << exit(FatalIOError);
257  }
258  else if (mandatory)
259  {
261  << "'" << key << "' not found in dictionary " << dict.name() << nl
262  << exit(FatalIOError);
263  }
264 
265  return false;
266 }
267 
268 
269 template<class EnumType>
271 (
272  const word& key,
273  const dictionary& dict,
274  EnumType& val
275 ) const
276 {
277  // Reading is non-mandatory
278  return readEntry(key, dict, val, false);
279 }
280 
281 
282 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::FatalIOError
IOerror FatalIOError
Foam::Enum::Enum
Enum()=default
Construct null (empty list)
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:86
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Enum::getOrDefault
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:190
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:241
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::Enum::append
void append(std::initializer_list< std::pair< EnumType, const char * >> list)
Append value/key pairs to the lists of known enumerations.
Definition: Enum.C:67
Foam::entry::get
T get() const
Definition: entry.H:258
Foam::List< Foam::word >
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dictionary.H
Foam::Enum::readIfPresent
bool readIfPresent(const word &key, const dictionary &dict, EnumType &val) const
Find an entry if present, and assign to T val.
Definition: Enum.C:271
Foam::Enum::sortedToc
List< word > sortedToc() const
The sorted list of enum names.
Definition: Enum.C:55
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:306
Foam::Enum::read
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: Enum.C:120
Foam::Enum::readEntry
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:233
Enum.H