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, 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 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 
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  //- 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  :
142  value_(b ? switchType::TRUE : switchType::FALSE)
143  {}
144 
145  //- 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 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.
199  static Switch getOrAddToDict
200  (
201  const word& key,
202  dictionary& dict,
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,
296  dictionary& dict,
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 // ************************************************************************* //
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:77
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Switch::lookupOrAddToDict
static Switch lookupOrAddToDict(const word &name, dictionary &dict, const Switch deflt=switchType::FALSE)
Same as getOrAddToDict()
Definition: Switch.H:293
Foam::Switch::FALSE
Definition: Switch.H:90
Foam::Switch::operator=
Switch & operator=(const switchType sw) noexcept
Assignment from enumerated value.
Definition: Switch.H:257
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::Switch::YES
Definition: Switch.H:91
Foam::Switch::operator=
Switch & operator=(const bool b) noexcept
Assignment from bool.
Definition: Switch.H:264
Foam::Switch::str
std::string str() const
A string representation of the Switch value.
Definition: Switch.C:328
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Switch::ON
Definition: Switch.H:92
Foam::Switch::Switch
constexpr Switch(const int i) noexcept
Construct from int (treat integer as bool value)
Definition: Switch.H:145
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:285
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:133
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:335
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:94
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:322
Foam::Switch::TRUE
Definition: Switch.H:90
Foam::Switch::ANY
Definition: Switch.H:93
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:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Switch::NO
Definition: Switch.H:91
Foam::Switch::type
switchType type() const noexcept
The underlying enumeration value.
Definition: Switch.C:306
stdFoam.H
Foam::Switch::OFF
Definition: Switch.H:92
Foam::Switch::Switch
constexpr Switch() noexcept
Default construct as false.
Definition: Switch.H:127
Foam::Switch::Switch
constexpr Switch(const bool b) noexcept
Construct from bool.
Definition: Switch.H:139
Foam::Switch::NONE
Definition: Switch.H:93
Foam::Switch::bad
bool bad() const noexcept
True if the Switch does not represent a valid enumeration.
Definition: Switch.H:225
Foam::Switch::switchType
switchType
Switch enumerations corresponding to common text representations.
Definition: Switch.H:88
Foam::Switch::negate
void negate() noexcept
Flip the type, so OFF becomes ON, etc.
Definition: Switch.C:312
Foam::Switch::getOrAddToDict
static Switch getOrAddToDict(const word &key, dictionary &dict, const Switch deflt=switchType::FALSE)
Definition: Switch.C:164