Switch.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "Switch.H"
30 #include "scalar.H"
31 #include "error.H"
32 #include "dictionary.H"
33 #include "IOstreams.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace
38 {
39 static_assert
40 (
41  Foam::Switch::INVALID+1 == 9,
42  "Switch::switchType does not have 9 entries"
43 );
44 
45 //- The names corresponding to the Switch::switchType enumeration.
46 // Includes extra entries for "invalid".
47 static const char* names[9] =
48 {
49  "false", "true",
50  "no", "yes",
51  "off", "on",
52  "none", "any",
53  "invalid"
54 };
55 
56 } // End anonymous namespace
57 
58 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
59 
60 const char* Foam::Switch::name(const bool b) noexcept
61 {
62  return names[(b ? 1 : 0)];
63 }
64 
65 
66 Foam::Switch::switchType Foam::Switch::parse
67 (
68  const std::string& str,
69  bool allowBad
70 )
71 {
72  switch (str.size())
73  {
74  case 1: // (f|n|t|y) - single-character forms
75  {
76  switch (str[0])
77  {
78  case 'f': return switchType::FALSE;
79  case 'n': return switchType::NO;
80  case 't': return switchType::TRUE;
81  case 'y': return switchType::YES;
82  }
83  break;
84  }
85  case 2: // (no|on)
86  {
87  if (str == names[switchType::NO]) return switchType::NO;
88  if (str == names[switchType::ON]) return switchType::ON;
89  break;
90  }
91  case 3: // (off|yes|any)
92  {
93  if (str == names[switchType::OFF]) return switchType::OFF;
94  if (str == names[switchType::YES]) return switchType::YES;
95  if (str == names[switchType::ANY]) return switchType::ANY;
96  break;
97  }
98  case 4: // (none|true)
99  {
100  if (str == names[switchType::NONE]) return switchType::NONE;
101  if (str == names[switchType::TRUE]) return switchType::TRUE;
102  break;
103  }
104  case 5: // (false)
105  {
106  if (str == names[switchType::FALSE]) return switchType::FALSE;
107  break;
108  }
109  }
110 
111  if (!allowBad)
112  {
114  << "Unknown switch word " << str << nl
115  << abort(FatalError);
116  }
117 
118  return switchType::INVALID;
119 }
120 
121 
123 (
124  const word& name,
125  dictionary& dict,
126  const Switch deflt
127 )
128 {
129  return dict.getOrAdd<Switch>(name, deflt);
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
134 
135 Foam::Switch::Switch(const float val, const float tol)
136 :
137  switch_((mag(val) > tol) ? switchType::TRUE : switchType::FALSE)
138 {}
139 
140 
141 Foam::Switch::Switch(const double val, const double tol)
142 :
143  switch_((mag(val) > tol) ? switchType::TRUE : switchType::FALSE)
144 {}
145 
146 
148 (
149  const word& key,
150  const dictionary& dict
151 )
152 {
153  const word str(dict.get<word>(key, keyType::LITERAL));
154 
155  (*this) = parse(str, true);
156 
157  if (!valid())
158  {
160  << "Expected 'true/false', 'on/off' ... found " << str << nl
161  << exit(FatalIOError);
162  }
163 }
164 
165 
167 (
168  const word& key,
169  const dictionary& dict,
170  const Switch deflt
171 )
172 :
173  Switch(deflt)
174 {
175  const entry* eptr = dict.findEntry(key, keyType::LITERAL);
176 
177  if (eptr)
178  {
179  const word str(eptr->get<word>());
180 
181  (*this) = parse(str, true);
182 
183  if (!valid())
184  {
185  // Found entry, but was bad input
186 
188  << "Expected 'true/false', 'on/off' ... found " << str << nl
189  << exit(FatalIOError);
190  }
191  }
192 }
193 
194 
196 {
197  is >> *this;
198 }
199 
200 
201 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
202 
203 bool Foam::Switch::valid() const noexcept
204 {
205  return switch_ != switchType::INVALID;
206 }
207 
208 
210 {
211  return switchType(switch_);
212 }
213 
214 
215 const char* Foam::Switch::c_str() const noexcept
216 {
217  return names[(switch_ & 0x0F)];
218 }
219 
220 
221 std::string Foam::Switch::str() const
222 {
223  return names[(switch_ & 0x0F)];
224 }
225 
226 
228 {
229  return dict.readIfPresent<Switch>(name, *this);
230 }
231 
232 
233 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
234 
236 {
237  token t(is);
238 
239  if (!t.good())
240  {
242  << "Bad token - could not get bool"
243  << exit(FatalIOError);
244  is.setBad();
245  return is;
246  }
247 
248  if (t.isLabel())
249  {
250  sw = bool(t.labelToken());
251  }
252  else if (t.isWord())
253  {
254  // Permit invalid value, but catch immediately for better messages
255  sw = Switch(t.wordToken(), true);
256 
257  if (!sw.valid())
258  {
260  << "Expected 'true/false', 'on/off' ... found "
261  << t.wordToken()
262  << exit(FatalIOError);
263  is.setBad();
264  return is;
265  }
266  }
267  else
268  {
270  << "Wrong token type - expected bool, found "
271  << t.info()
272  << exit(FatalIOError);
273  is.setBad();
274  return is;
275  }
276 
277  is.check(FUNCTION_NAME);
278  return is;
279 }
280 
281 
283 {
284  os << sw.c_str();
285  return os;
286 }
287 
288 
289 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:456
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:70
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::dictionary::getOrAdd
T getOrAdd(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX)
Definition: dictionaryTemplates.C:162
Foam::Switch::valid
bool valid() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:203
Foam::FatalIOError
IOerror FatalIOError
Foam::Switch::str
std::string str() const
A string representation of the Switch value.
Definition: Switch.C:221
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::token::good
bool good() const
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:368
Foam::token::isLabel
bool isLabel() const
Token is LABEL.
Definition: tokenI.H:450
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
error.H
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:513
Foam::Switch::INVALID
Definition: Switch.H:85
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::Switch::c_str
const char * c_str() const noexcept
A string representation of the Switch value.
Definition: Switch.C:215
Switch.H
Foam::Switch::name
static const char * name(const bool b) noexcept
A string representation of bool as "false" / "true".
Definition: Switch.C:60
Foam::Switch::readIfPresent
bool readIfPresent(const word &name, const dictionary &dict)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:227
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
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::token::isWord
bool isWord() const
Token is WORD.
Definition: tokenI.H:552
scalar.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:558
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::Switch::type
switchType type() const noexcept
The underlying enumeration value.
Definition: Switch.C:209
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::entry::get
T get() const
Definition: entry.H:258
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::dictionary::findEntry
entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find for an entry (non-const access) with the given keyword.
Definition: dictionary.C:369
dictionary.H
bool
bool
Definition: EEqn.H:20
Foam::Switch::Switch
constexpr Switch() noexcept
Null constructible as false.
Definition: Switch.H:108
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:352
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:73
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Switch::switchType
switchType
Switch enumerations corresponding to common text representations.
Definition: Switch.H:79
Foam::Switch::getOrAddToDict
static Switch getOrAddToDict(const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
Definition: Switch.C:123
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417