Switch.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::Switch
26 
27 Description
28  A simple wrapper around bool so that it can be read as a word:
29  true/false, on/off, yes/no, y/n, t/f, or none/any.
30 
31 SourceFiles
32  Switch.C
33  SwitchIO.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Switch_H
38 #define Switch_H
39 
40 #include "bool.H"
41 #include "word.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of friend functions and operators
49 
50 class Switch;
51 class dictionary;
52 
53 Istream& operator>>(Istream&, Switch&);
54 Ostream& operator<<(Ostream&, const Switch&);
55 
56 
57 /*---------------------------------------------------------------------------*\
58  Class Switch Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class Switch
62 {
63 public:
64 
65  // Public data types
66 
67  //- The various text representations for a switch value.
68  // These also correspond to the entries in names.
69  enum class switchType : unsigned char
70  {
71  False = 0,
72  True = 1,
73  off = 2,
74  on = 3,
75  no = 4,
76  yes = 5,
77  n = 6,
78  y = 7,
79  f = 8,
80  t = 9,
81  none = 10,
82  any = 11,
83  invalid
84  };
85 
86  //- Number of switchTypes
87  static const unsigned char nSwitchType =
88  static_cast<unsigned char>(switchType::invalid) + 1;
89 
90  //- Convert switchType to integer (unsigned char)
91  inline static unsigned char toInt(const switchType x)
92  {
93  return static_cast<unsigned char>(x);
94  }
95 
96  //- Increment the switchType counter
97  inline friend switchType operator++(switchType& x)
98  {
99  return x = switchType(toInt(x) + 1);
100  }
101 
102 
103 private:
104 
105  // Private Data
106 
107  //- The logic and enumerated text representation stored as a single byte
108  switchType switch_;
109 
110 
111  // Static Data Members
112 
113  //- The set of names corresponding to the switchType enumeration
114  // Includes an extra entry for "invalid".
115  static const char* names[nSwitchType];
116 
117 
118  // Static Member Functions
119 
120  //- Return a switchType representation of a word
121  static switchType asEnum(const std::string&, const bool allowInvalid);
122 
123 
124 public:
125 
126  // Constructors
127 
128  //- Construct null as false
129  Switch()
130  :
131  switch_(switchType::False)
132  {}
133 
134  //- Construct from enumerated value
135  Switch(const switchType sw)
136  :
137  switch_(sw)
138  {}
139 
140  //- Construct from bool
141  Switch(const bool b)
142  :
143  switch_(b ? switchType::True : switchType::False)
144  {}
145 
146  //- Construct from integer values (treats integer as bool value)
147  Switch(const int i)
148  :
149  switch_(i ? switchType::True : switchType::False)
150  {}
151 
152  //- Construct from std::string, string, word
153  // Optionally allow bad words, and catch the error elsewhere
154  Switch(const std::string& str, const bool allowInvalid=false)
155  :
156  switch_(asEnum(str, allowInvalid))
157  {}
158 
159  //- Construct from character array
160  // Optionally allow bad words, and catch the error elsewhere
161  Switch(const char* str, const bool allowInvalid=false)
162  :
163  switch_(asEnum(std::string(str), allowInvalid))
164  {}
165 
166  //- Construct from Istream
167  Switch(Istream& is);
168 
169  //- Construct from dictionary, supplying default value so that if the
170  // value is not found, it is added into the dictionary.
172  (
173  const word&,
174  dictionary&,
175  const Switch& defaultValue = false
176  );
177 
178 
179  // Member Functions
180 
181  //- Return true if the Switch has a valid value
182  bool valid() const;
183 
184  //- Return a text representation of the Switch
185  const char* asText() const;
186 
187  //- Update the value of the Switch if it is found in the dictionary
188  bool readIfPresent(const word&, const dictionary&);
189 
190 
191  // Member Operators
192 
193  //- Conversion to bool
194  operator bool() const
195  {
196  return (toInt(switch_) & 0x1);
197  }
198 
199  //- Assignment to enumerated value
200  void operator=(const switchType sw)
201  {
202  switch_ = sw;
203  }
204 
205  //- Assignment to bool
206  void operator=(const bool b)
207  {
208  switch_ = (b ? switchType::True : switchType::False);
209  }
210 
211 
212  // IOstream Operators
213 
214  friend Istream& operator>>(Istream&, Switch&);
215  friend Ostream& operator<<(Ostream&, const Switch&);
216 };
217 
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Foam
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #endif
226 
227 // ************************************************************************* //
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:123
switchType
The various text representations for a switch value.
Definition: Switch.H:68
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:135
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const dimensionedScalar b
Wien displacement law constant: default SI units: [m K].
Definition: createFields.H:27
static unsigned char toInt(const switchType x)
Convert switchType to integer (unsigned char)
Definition: Switch.H:90
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
static Switch lookupOrAddToDict(const word &, dictionary &, const Switch &defaultValue=false)
Construct from dictionary, supplying default value so that if the.
Definition: Switch.C:111
friend Ostream & operator<<(Ostream &, const Switch &)
A class for handling words, derived from string.
Definition: word.H:59
friend switchType operator++(switchType &x)
Increment the switchType counter.
Definition: Switch.H:96
Istream & operator>>(Istream &, directionInfo &)
friend Istream & operator>>(Istream &, Switch &)
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const unsigned char nSwitchType
Number of switchTypes.
Definition: Switch.H:86
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:129
Switch()
Construct null as false.
Definition: Switch.H:128
void operator=(const switchType sw)
Assignment to enumerated value.
Definition: Switch.H:199
Ostream & operator<<(Ostream &, const ensightPart &)
FvWallInfoData< WallInfo, bool > bool
A class for handling character strings derived from std::string.
Definition: string.H:76
System bool.
Namespace for OpenFOAM.