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