Loading...
Searching...
No Matches
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-2025 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.cxx
37
38\*---------------------------------------------------------------------------*/
39
40#ifndef Foam_Switch_H
41#define Foam_Switch_H
42
43#include "bool.H"
44#include "stdFoam.H"
45
46#include <string_view>
47#include <string>
48
49// Avoid any pre-processor conflicts with enum names
50#undef FALSE
51#undef TRUE
52#undef NO
53#undef YES
54#undef OFF
55#undef ON
56#undef NONE
57
58// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59
60namespace Foam
61{
62
63// Forward Declarations
64class dictionary;
65class token;
66class word;
67class Switch;
68
69// IOstream Operators
70
71//- Read Switch from stream
73
74//- Write Switch to stream as its text value (eg, "true", "false")
75Ostream& operator<<(Ostream& is, const Switch sw);
76
77/*---------------------------------------------------------------------------*\
78 Class Switch Declaration
79\*---------------------------------------------------------------------------*/
81class Switch
82{
83public:
84
85 // Data Types
86
87 //- Switch enumerations corresponding to common text representations.
88 // \note The values here are critical for its proper behaviour.
89 // The values correspond to an index into the predefined output names
90 // for the c_str() method and the lower bit is tested to
91 // determine the true/false bool value.
92 enum switchType : unsigned char
93 {
94 FALSE = 0 , TRUE = 1 ,
95 NO = 2 , YES = 3 ,
96 OFF = 4 , ON = 5 ,
97 NONE = 6 , ANY = 7 ,
98 INVALID = 8 ,
99 };
101private:
102
103 // Private Data
104
105 //- The logic and enumerated text representation stored in a byte
106 unsigned char value_;
107
108
109 // Private Member Functions
110
111 //- From string content to switchType.
112 // \returns switchType::INVALID if not found
113 static switchType parse(const char* s, size_t len) noexcept;
114
115 //- From char sequence (with nullptr protection) to switchType
116 static inline switchType parse(const char* s);
117
118public:
119
120 // Generated Methods
121
122 //- Copy construct
123 Switch(const Switch&) noexcept = default;
124
125 //- Copy assignment
126 Switch& operator=(const Switch&) noexcept = default;
127
128
129 // Constructors
130
131 //- Default construct as false
132 constexpr Switch() noexcept
133 :
135 {}
136
137 //- Implicit construct from enumerated value
138 constexpr Switch(switchType sw) noexcept
140 value_(sw)
141 {}
142
143 //- Implicit construct from bool
144 constexpr Switch(bool b) noexcept
145 :
147 {}
148
149 //- Implicit construct from int (treat integer as bool value)
150 constexpr Switch(int i) noexcept
151 :
153 {}
154
155 //- Construct from character array - catches bad input.
156 // Use static find() method for a failsafe alternative
157 explicit Switch(const char* s);
158
159 //- Construct from string - catches bad input.
160 // Use static find() method for a failsafe alternative
161 explicit Switch(const std::string& s);
162
163 //- Construct from string_view - catches bad input.
164 // Use static find() method for a failsafe alternative
165 explicit Switch(std::string_view s);
166
167 //- Construct from float with rounding to zero given by
168 //- the tolerance (default: 0.5)
169 explicit Switch(const float val, const float tol=0.5);
170
171 //- Construct from double with rounding to zero given by
172 //- the tolerance (default: 0.5)
173 explicit Switch(const double val, const double tol=0.5);
174
175 //- Construct from token. Handles bool/label/word types.
176 explicit Switch(const token& tok);
177
178 //- Construct from dictionary lookup.
179 // FatalError if anything is incorrect.
180 Switch
182 const word& key,
183 const dictionary& dict
184 );
185
186 //- Find the key in the dictionary and use the corresponding
187 //- switch value or the default if not found in dictionary.
188 //
189 // FatalIOError if the switch name is incorrect.
190 // Specifying warnOnly downgrades the FatalIOError to an IOWarning.
191 Switch
192 (
193 const word& key,
194 const dictionary& dict,
195 const Switch deflt,
196 const bool warnOnly = false
197 );
198
199 //- Construct from Istream by reading a token
200 explicit Switch(Istream& is);
202
203 // Helpers
204
205 //- Construct Switch from dictionary, with default value
206 static Switch getOrDefault
208 const word& key,
209 const dictionary& dict,
210 const Switch deflt = switchType::FALSE
211 );
213 //- Construct from dictionary, supplying default value so that if the
214 //- value is not found, it is added into the dictionary.
216 (
217 const word& key,
220 );
221
222
223 // Static Member Functions
224
225 //- A string representation of bool as "false" / "true"
226 static const char* name(bool b) noexcept;
227
228 //- True if there is a switch type corresponding to the given string.
229 static bool contains(std::string_view s) noexcept;
230
231 //- Find switchType for the given string, returning a Switch that
232 //- can be tested for good() or bad().
233 static Switch find(const char* s);
234
235 //- Find switchType for the given string, returning a Switch that
236 //- can be tested for good() or bad().
237 static Switch find(const std::string& s) noexcept;
238
239 //- Find switchType for the given string, returning a Switch that
240 //- can be tested for good() or bad().
241 static Switch find(std::string_view s) noexcept;
242
243
244 // Member Functions
245
246 //- True if the Switch represents a valid enumeration
247 bool good() const noexcept { return (value_ < switchType::INVALID); }
248
249 //- True if the Switch does not represent a valid enumeration
250 bool bad() const noexcept { return !good(); }
251
252 //- The underlying enumeration value
253 switchType type() const noexcept { return switchType(value_); }
254
255 //- Flip the type, so OFF becomes ON, etc.
256 // Ignored if the Switch is INVALID
257 void negate() noexcept;
258
259 //- A C-string representation of the Switch value
260 const char* c_str() const noexcept;
261
262 //- A string representation of the Switch value
263 std::string str() const;
264
265 //- Update the value of the Switch if it is found in the dictionary
266 bool readIfPresent
267 (
268 const word& key,
269 const dictionary& dict
270 );
271
272
273 // Convenience Tests
274
275 //- Test for 'any' type
276 bool is_any() const noexcept
277 {
278 return (value_ == switchType::ANY);
279 }
280
281 //- Test for 'none' type
282 bool is_none() const noexcept
283 {
284 return (value_ == switchType::NONE);
285 }
286
288 // Member Operators
289
290 //- Conversion to bool
291 operator bool() const noexcept
292 {
293 return (value_ & 0x1);
294 }
295
296 //- Assignment from enumerated value
297 Switch& operator=(switchType sw) noexcept
298 {
299 value_ = sw;
300 return *this;
301 }
302
303 //- Assignment from bool
304 Switch& operator=(bool b) noexcept
305 {
306 value_ = (b ? switchType::TRUE : switchType::FALSE);
307 return *this;
308 }
309
310
311 // Housekeeping
313 //- Same as contains()
314 static bool found(const std::string& str) { return contains(str); }
315
316 //- Deprecated(2020-01) From string with/without bad input test
317 // \deprecated(2020-01) - confusing syntax, use static find() method
318 FOAM_DEPRECATED_FOR(2019-02, "static find() method")
319 Switch(const std::string& str, bool allowBad);
320
321 //- Deprecated(2020-01) From string with/without bad input test
322 // \deprecated(2020-01) - confusing syntax, use static find() method
323 FOAM_DEPRECATED_FOR(2019-02, "static find() method")
324 Switch(const char* str, bool allowBad);
325
326 //- Deprecated(2020-01) Use good() method, or static contains() method
327 // \deprecated(2020-01) Use good() method, or static contains() method
328 FOAM_DEPRECATED_FOR(2019-02, "good() or static contains() method")
329 bool valid() const noexcept { return good(); }
330
331 //- Same as getOrAddToDict()
332 FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()")
335 const word& name,
337 const Switch deflt = switchType::FALSE
338 )
340 return getOrAddToDict(name, dict, deflt);
341 }
342};
343
344
345// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346
347} // End namespace Foam
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350
351#endif
352
353// ************************************************************************* //
bool found
System bool.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition Switch.H:81
switchType
Switch enumerations corresponding to common text representations.
Definition Switch.H:95
void negate() noexcept
Flip the type, so OFF becomes ON, etc.
static Switch find(std::string_view s) noexcept
Find switchType for the given string, returning a Switch that can be tested for good() or bad().
static Switch lookupOrAddToDict(const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
Same as getOrAddToDict().
Definition Switch.H:430
FOAM_DEPRECATED_FOR(2019-02, "good() or static contains() method") bool valid() const noexcept
Deprecated(2020-01) Use good() method, or static contains() method.
Definition Switch.H:422
static Switch getOrDefault(const word &key, const dictionary &dict, const Switch deflt=switchType::FALSE)
Construct Switch from dictionary, with default value.
static Switch find(const std::string &s) noexcept
Find switchType for the given string, returning a Switch that can be tested for good() or bad().
static bool contains(std::string_view s) noexcept
True if there is a switch type corresponding to the given string.
constexpr Switch(int i) noexcept
Implicit construct from int (treat integer as bool value).
Definition Switch.H:171
bool readIfPresent(const word &key, const dictionary &dict)
Update the value of the Switch if it is found in the dictionary.
constexpr Switch() noexcept
Default construct as false.
Definition Switch.H:147
Switch(const char *s)
Construct from character array - catches bad input.
Switch(const double val, const double tol=0.5)
Construct from double with rounding to zero given by the tolerance (default: 0.5).
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition Switch.H:307
Switch(Istream &is)
Construct from Istream by reading a token.
bool bad() const noexcept
True if the Switch does not represent a valid enumeration.
Definition Switch.H:312
Switch(std::string_view s)
Construct from string_view - catches bad input.
bool is_any() const noexcept
Test for 'any' type.
Definition Switch.H:351
constexpr Switch(switchType sw) noexcept
Implicit construct from enumerated value.
Definition Switch.H:155
static Switch getOrAddToDict(const word &key, dictionary &dict, const Switch deflt=switchType::FALSE)
Construct from dictionary, supplying default value so that if the value is not found,...
static Switch find(const char *s)
Find switchType for the given string, returning a Switch that can be tested for good() or bad().
Switch & operator=(bool b) noexcept
Assignment from bool.
Definition Switch.H:387
switchType type() const noexcept
The underlying enumeration value.
Definition Switch.H:317
static bool found(const std::string &str)
Same as contains().
Definition Switch.H:399
Switch(const word &key, const dictionary &dict, const Switch deflt, const bool warnOnly=false)
Find the key in the dictionary and use the corresponding switch value or the default if not found in ...
Switch(const token &tok)
Construct from token. Handles bool/label/word types.
bool is_none() const noexcept
Test for 'none' type.
Definition Switch.H:359
Switch & operator=(const Switch &) noexcept=default
Copy assignment.
Switch & operator=(switchType sw) noexcept
Assignment from enumerated value.
Definition Switch.H:378
constexpr Switch(bool b) noexcept
Implicit construct from bool.
Definition Switch.H:163
const char * c_str() const noexcept
A C-string representation of the Switch value.
Switch(const float val, const float tol=0.5)
Construct from float with rounding to zero given by the tolerance (default: 0.5).
Switch(const std::string &s)
Construct from string - catches bad input.
Switch(const word &key, const dictionary &dict)
Construct from dictionary lookup.
std::string str() const
A string representation of the Switch value.
static const char * name(bool b) noexcept
A string representation of bool as "false" / "true".
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:133
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
auto & name
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
dictionary dict
volScalarField & b
Includes some common C++ headers, defines global macros and templates used in multiple places by Open...
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition stdFoam.H:53