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-2023 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 Class
28  Foam::Switch
29 
30 Description
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 
35 SourceFiles
36  Switch.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_Switch_H
41 #define Foam_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 
57 namespace Foam
58 {
59 
60 // Forward Declarations
61 class dictionary;
62 class token;
63 class word;
64 class Switch;
65 
66 // IOstream Operators
67 
68 //- Read Switch from stream using Foam::Switch(Istream&)
69 Istream& operator>>(Istream& is, Switch& sw);
70 
71 //- Write Switch to stream as its text value (eg, "true", "false")
72 Ostream& operator<<(Ostream& is, const Switch sw);
73 
74 /*---------------------------------------------------------------------------*\
75  Class Switch Declaration
76 \*---------------------------------------------------------------------------*/
77 
78 class Switch
79 {
80 public:
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 
99 private:
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 
114 public:
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  //- Implicit construct from enumerated value
134  constexpr Switch(const switchType sw) noexcept
135  :
136  value_(sw)
137  {}
138 
139  //- Implicit construct from bool
140  constexpr Switch(const bool b) noexcept
141  :
142  value_(b ? switchType::TRUE : switchType::FALSE)
143  {}
144 
145  //- Implicit construct from int (treat integer as bool value)
146  constexpr Switch(const int i) noexcept
147  :
148  value_(i ? switchType::TRUE : switchType::FALSE)
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 warnOnly downgrades the FatalIOError to an IOWarning.
183  Switch
184  (
185  const word& key,
186  const dictionary& dict,
187  const Switch deflt,
188  const bool warnOnly = false
189  );
190 
191  //- Construct from Istream by reading a token
192  explicit Switch(Istream& is);
193 
194 
195  // Helpers
196 
197  //- Construct Switch from dictionary, with default value
198  static Switch getOrDefault
199  (
200  const word& key,
201  const dictionary& dict,
202  const Switch deflt = switchType::FALSE
203  );
204 
205  //- Construct from dictionary, supplying default value so that if the
206  //- value is not found, it is added into the dictionary.
207  static Switch getOrAddToDict
208  (
209  const word& key,
210  dictionary& dict,
211  const Switch deflt = switchType::FALSE
212  );
213 
214 
215  // Static Member Functions
216 
217  //- A string representation of bool as "false" / "true"
218  static const char* name(const bool b) noexcept;
219 
220  //- True if there is a switch type corresponding to the given string.
221  static bool contains(const std::string& str);
222 
223  //- Find switchType for the given string, returning as a Switch that
224  //- can be tested for good() or bad().
225  static Switch find(const std::string& str);
226 
227 
228  // Member Functions
229 
230  //- True if the Switch represents a valid enumeration
231  bool good() const noexcept;
232 
233  //- True if the Switch does not represent a valid enumeration
234  bool bad() const noexcept { return !good(); }
235 
236  //- The underlying enumeration value
237  switchType type() const noexcept;
238 
239  //- Flip the type, so OFF becomes ON, etc.
240  // Ignored if the Switch is INVALID
241  void negate() noexcept;
242 
243  //- A C-string representation of the Switch value
244  const char* c_str() const noexcept;
245 
246  //- A string representation of the Switch value
247  std::string str() const;
248 
249  //- Update the value of the Switch if it is found in the dictionary
250  bool readIfPresent
251  (
252  const word& key,
253  const dictionary& dict
254  );
255 
256 
257  // Member Operators
258 
259  //- Conversion to bool
260  operator bool() const noexcept
261  {
262  return (value_ & 0x1);
263  }
264 
265  //- Assignment from enumerated value
267  {
268  value_ = sw;
269  return *this;
270  }
271 
272  //- Assignment from bool
273  Switch& operator=(const bool b) noexcept
274  {
275  value_ = (b ? Switch::TRUE : Switch::FALSE);
276  return *this;
277  }
278 
279 
280  // Housekeeping
281 
282  //- Same as contains()
283  static bool found(const std::string& str) { return contains(str); }
284 
285  //- Deprecated(2020-01) From string with/without bad input test
286  // \deprecated(2020-01) - confusing syntax, use static find() method
287  FOAM_DEPRECATED_FOR(2019-02, "static find() method")
288  Switch(const std::string& str, bool allowBad);
289 
290  //- Deprecated(2020-01) From string with/without bad input test
291  // \deprecated(2020-01) - confusing syntax, use static find() method
292  FOAM_DEPRECATED_FOR(2019-02, "static find() method")
293  Switch(const char* str, bool allowBad);
294 
295  //- Deprecated(2020-01) Use good() method, or static contains() method
296  // \deprecated(2020-01) Use good() method, or static contains() method
297  FOAM_DEPRECATED_FOR(2019-02, "good() or static contains() method")
298  bool valid() const noexcept { return good(); }
299 
300  //- Same as getOrAddToDict()
301  FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()")
303  (
304  const word& name,
305  dictionary& dict,
306  const Switch deflt = switchType::FALSE
307  )
308  {
309  return getOrAddToDict(name, dict, deflt);
310  }
311 };
312 
313 
314 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
315 
316 } // End namespace Foam
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 
320 #endif
321 
322 // ************************************************************************* //
void negate() noexcept
Flip the type, so OFF becomes ON, etc.
Definition: Switch.C:319
dictionary dict
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:307
static bool contains(const std::string &str)
True if there is a switch type corresponding to the given string.
Definition: Switch.C:153
std::string str() const
A string representation of the Switch value.
Definition: Switch.C:335
switchType
Switch enumerations corresponding to common text representations.
Definition: Switch.H:91
bool bad() const noexcept
True if the Switch does not represent a valid enumeration.
Definition: Switch.H:287
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
const char * c_str() const noexcept
A C-string representation of the Switch value.
Definition: Switch.C:329
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.
Definition: Switch.H:77
static Switch getOrDefault(const word &key, const dictionary &dict, const Switch deflt=switchType::FALSE)
Construct Switch from dictionary, with default value.
Definition: Switch.C:160
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, it is added into the dictionary.
Definition: Switch.C:171
static bool found(const std::string &str)
Same as contains()
Definition: Switch.H:355
static const char * name(const bool b) noexcept
A string representation of bool as "false" / "true".
Definition: Switch.C:141
constexpr Switch(const bool b) noexcept
Implicit construct from bool.
Definition: Switch.H:157
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:55
constexpr Switch() noexcept
Default construct as false.
Definition: Switch.H:141
switchType type() const noexcept
The underlying enumeration value.
Definition: Switch.C:313
const direction noexcept
Definition: Scalar.H:258
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
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:378
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
Switch & operator=(const Switch &) noexcept=default
Copy assignment.
static Switch lookupOrAddToDict(const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
Same as getOrAddToDict()
Definition: Switch.H:386
constexpr Switch(const int i) noexcept
Implicit construct from int (treat integer as bool value)
Definition: Switch.H:165
bool readIfPresent(const word &key, const dictionary &dict)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:342
static Switch find(const std::string &str)
Find switchType for the given string, returning as a Switch that can be tested for good() or bad()...
Definition: Switch.C:147
System bool.
Namespace for OpenFOAM.