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 -------------------------------------------------------------------------------
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 = 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 = 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>
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 
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 = 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 
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 = 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]);
174 }
175 
176 
177 template<class EnumType>
179 (
180  const word& key,
181  const dictionary& dict,
182  const EnumType deflt,
183  const bool failsafe
184 ) const
185 {
186  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
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 
220 template<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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Enum::get
EnumType get(const word &enumName) const
The enumeration corresponding to the given name.
Definition: Enum.C:75
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:179
Foam::Enum::Enum
Enum() noexcept=default
Default construct, an empty list.
Foam::Enum::lookup
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:92
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:123
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:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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:56
Foam::entry::get
T get() const
Definition: entry.H:269
dictionary.H
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:340
Foam::Enum::read
EnumType read(Istream &is) const
Read a word from Istream and return the corresponding enumeration.
Definition: Enum.C:109
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:222
Enum.H