Switch.H
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-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Switch
29
30Description
31 A simple wrapper around bool so that it can be read as a word:
32 true/false, on/off, yes/no, any/none.
33 Also accepts 0/1 as a string and shortcuts t/f, y/n.
34
35SourceFiles
36 Switch.C
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Switch_H
41#define Switch_H
42
43#include "bool.H"
44#include "stdFoam.H"
45
46// Avoid any pre-processor conflicts with enum names
47#undef FALSE
48#undef TRUE
49#undef NO
50#undef YES
51#undef OFF
52#undef ON
53#undef NONE
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56
57namespace Foam
58{
59
60// Forward Declarations
61class dictionary;
62class token;
63class word;
64class Switch;
65
66// IOstream Operators
67
68//- Read Switch from stream using Foam::Switch(Istream&)
69Istream& operator>>(Istream& is, Switch& sw);
70
71//- Write Switch to stream as its text value (eg, "true", "false")
72Ostream& operator<<(Ostream& is, const Switch& sw);
73
74/*---------------------------------------------------------------------------*\
75 Class Switch Declaration
76\*---------------------------------------------------------------------------*/
78class Switch
79{
80public:
81
82 // Data Types
83
84 //- Switch enumerations corresponding to common text representations.
85 // \note The values here are critical for its proper behaviour.
86 // The values correspond to an index into the predefined output names
87 // for the c_str() method and the lower bit is tested for
88 // determining the true/false bool value.
89 enum switchType : unsigned char
90 {
91 FALSE = 0 , TRUE = 1 ,
92 NO = 2 , YES = 3 ,
93 OFF = 4 , ON = 5 ,
94 NONE = 6 , ANY = 7 ,
95 INVALID = 8 ,
96 };
97
98
99private:
100
101 // Private Data
102
103 //- The logic and enumerated text representation stored in a byte
104 unsigned char value_;
105
106
107 // Private Member Functions
108
109 //- Find switchType for given string. Return 'INVALID' if not found.
110 // With failOnError, trigger FatalError if not found
111 static switchType parse(const std::string& str, const bool failOnError);
112
113
114public:
115
116 // Generated Methods
117
118 //- Copy construct
119 Switch(const Switch&) noexcept = default;
120
121 //- Copy assignment
122 Switch& operator=(const Switch&) noexcept = default;
123
124
125 // Constructors
126
127 //- Default construct as false
128 constexpr Switch() noexcept
129 :
130 value_(switchType::FALSE)
131 {}
132
133 //- Construct from enumerated value
134 constexpr Switch(const switchType sw) noexcept
135 :
136 value_(sw)
137 {}
138
139 //- Construct from bool
140 constexpr Switch(const bool b) noexcept
141 :
143 {}
144
145 //- Construct from int (treat integer as bool value)
146 constexpr Switch(const int i) noexcept
147 :
149 {}
150
151 //- Construct from string - catches bad input.
152 // Use static find() method for a failsafe alternative
153 explicit Switch(const std::string& str);
154
155 //- Construct from character array - catches bad input.
156 // Use static find() method for a failsafe alternative
157 explicit Switch(const char* str);
158
159 //- Construct from float with rounding to zero given by
160 //- the tolerance (default: 0.5)
161 explicit Switch(const float val, const float tol=0.5);
162
163 //- Construct from double with rounding to zero given by
164 //- the tolerance (default: 0.5)
165 explicit Switch(const double val, const double tol=0.5);
166
167 //- Construct from token. Handles bool/label/word types.
168 explicit Switch(const token& tok);
169
170 //- Construct from dictionary lookup.
171 // FatalError if anything is incorrect.
172 Switch
173 (
174 const word& key,
175 const dictionary& dict
176 );
177
178 //- Find the key in the dictionary and use the corresponding
179 //- switch value or the default if not found in dictionary.
180 //
181 // FatalIOError if the switch name is incorrect.
182 // Specifying failsafe downgrades the FatalIOError to an IOWarning.
183 Switch
184 (
185 const word& key,
186 const dictionary& dict,
187 const Switch deflt,
188 const bool failsafe = false
189 );
190
191 //- Construct from Istream by reading a token
192 explicit Switch(Istream& is);
193
194
195 // Helpers
196
197 //- Construct from dictionary, supplying default value so that if the
198 //- value is not found, it is added into the dictionary.
200 (
201 const word& key,
203 const Switch deflt = switchType::FALSE
204 );
205
206
207 // Static Member Functions
208
209 //- A string representation of bool as "false" / "true"
210 static const char* name(const bool b) noexcept;
211
212 //- Find switchType for the given string, returning as a Switch that
213 //- can be tested for good() or bad().
214 static Switch find(const std::string& str);
215
216 //- Test if there is a switch type corresponding to the given string.
217 static bool found(const std::string& str);
218
219
220 // Member Functions
221
222 //- True if the Switch represents a valid enumeration
223 bool good() const noexcept;
224
225 //- True if the Switch does not represent a valid enumeration
226 bool bad() const noexcept { return !good(); }
227
228 //- The underlying enumeration value
229 switchType type() const noexcept;
230
231 //- Flip the type, so OFF becomes ON, etc.
232 // Ignored if the Switch is INVALID
233 void negate() noexcept;
234
235 //- A C-string representation of the Switch value
236 const char* c_str() const noexcept;
237
238 //- A string representation of the Switch value
239 std::string str() const;
240
241 //- Update the value of the Switch if it is found in the dictionary
242 bool readIfPresent
243 (
244 const word& key,
245 const dictionary& dict
246 );
247
248
249 // Member Operators
250
251 //- Conversion to bool
252 operator bool() const noexcept
253 {
254 return (value_ & 0x1);
255 }
256
257 //- Assignment from enumerated value
258 Switch& operator=(const switchType sw) noexcept
259 {
260 value_ = sw;
261 return *this;
262 }
263
264 //- Assignment from bool
265 Switch& operator=(const bool b) noexcept
266 {
267 value_ = (b ? Switch::TRUE : Switch::FALSE);
268 return *this;
269 }
270
271
272 // Housekeeping
273
274 //- Deprecated(2020-01) From string with/without bad input test
275 // \deprecated(2020-01) - confusing syntax, use static find() method
276 FOAM_DEPRECATED_FOR(2019-02, "static find() method")
277 Switch(const std::string& str, bool allowBad);
278
279 //- Deprecated(2020-01) From string with/without bad input test
280 // \deprecated(2020-01) - confusing syntax, use static find() method
281 FOAM_DEPRECATED_FOR(2019-02, "static find() method")
282 Switch(const char* str, bool allowBad);
283
284 //- Deprecated(2020-01) Use good() method, or static found() method
285 // \deprecated(2020-01) Use good() method, or static found() method
286 FOAM_DEPRECATED_FOR(2019-02, "good() or static found() method")
287 bool valid() const noexcept
288 {
289 return good();
290 }
291
292 //- Same as getOrAddToDict()
294 (
295 const word& name,
297 const Switch deflt = switchType::FALSE
298 )
299 {
300 return getOrAddToDict(name, dict, deflt);
301 }
302};
303
304
305// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306
307} // End namespace Foam
308
309// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310
311#endif
312
313// ************************************************************************* //
bool found
System bool.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:78
constexpr Switch(const bool b) noexcept
Construct from bool.
Definition: Switch.H:139
switchType
Switch enumerations corresponding to common text representations.
Definition: Switch.H:89
Switch & operator=(const bool b) noexcept
Assignment from bool.
Definition: Switch.H:264
Switch & operator=(const switchType sw) noexcept
Assignment from enumerated value.
Definition: Switch.H:257
void negate() noexcept
Flip the type, so OFF becomes ON, etc.
Definition: Switch.C:312
static Switch lookupOrAddToDict(const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
Same as getOrAddToDict()
Definition: Switch.H:293
bool readIfPresent(const word &key, const dictionary &dict)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:335
constexpr Switch(const switchType sw) noexcept
Construct from enumerated value.
Definition: Switch.H:133
constexpr Switch() noexcept
Default construct as false.
Definition: Switch.H:127
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:300
bool bad() const noexcept
True if the Switch does not represent a valid enumeration.
Definition: Switch.H:225
switchType type() const noexcept
The underlying enumeration value.
Definition: Switch.C:306
static Switch getOrAddToDict(const word &key, dictionary &dict, const Switch deflt=switchType::FALSE)
Definition: Switch.C:164
constexpr Switch(const int i) noexcept
Construct from int (treat integer as bool value)
Definition: Switch.H:145
static const char * name(const bool b) noexcept
A string representation of bool as "false" / "true".
Definition: Switch.C:145
static Switch find(const std::string &str)
Definition: Switch.C:151
Switch & operator=(const Switch &) noexcept=default
Copy assignment.
const char * c_str() const noexcept
A C-string representation of the Switch value.
Definition: Switch.C:322
std::string str() const
A string representation of the Switch value.
Definition: Switch.C:328
Switch(const Switch &) noexcept=default
Copy construct.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
dictionary dict
volScalarField & b
Definition: createFields.H:27
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition: stdFoam.H:52