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 | Copyright (C) 2011-2016 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.
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  // Private data
64 
65  //- The logic and enumerated text representation stored as a single byte
66  unsigned char switch_;
67 
68 public:
69 
70  // Public data types
71 
72  // avoid issues with pre-processor defines
73  #undef FALSE
74  #undef TRUE
75  #undef OFF
76  #undef ON
77  #undef NO
78  #undef YES
79  #undef NO_1
80  #undef YES_1
81  #undef FALSE_1
82  #undef TRUE_1
83  #undef NONE
84  #undef PLACEHOLDER
85  #undef INVALID
86 
87  //- The various text representations for a switch value.
88  // These also correspond to the entries in names.
89  enum switchType
90  {
91  FALSE = 0, TRUE = 1,
92  OFF = 2, ON = 3,
93  NO = 4, YES = 5,
94  NO_1 = 6, YES_1 = 7,
95  FALSE_1 = 8, TRUE_1 = 9,
96  NONE = 10, PLACEHOLDER = 11,
97  INVALID
98  };
99 
100  // Static data members
101 
102  //- The set of names corresponding to the switchType enumeration
103  // Includes an extra entry for "invalid".
104  static const char* names[INVALID+1];
105 
106 
107 private:
108 
109  // Static Member Functions
110 
111  //- Return a switchType representation of a word
112  static switchType asEnum(const std::string&, const bool allowInvalid);
113 
114 
115 public:
116 
117  // Constructors
118 
119  //- Construct null as false
120  Switch()
121  :
122  switch_(Switch::FALSE)
123  {}
124 
125  //- Construct from enumerated value
126  Switch(const switchType sw)
127  :
128  switch_(sw)
129  {}
130 
131  //- Construct from bool
132  Switch(const bool b)
133  :
134  switch_(b ? Switch::TRUE : Switch::FALSE)
135  {}
136 
137  //- Construct from integer values (treats integer as bool value)
138  Switch(const int i)
139  :
140  switch_(i ? Switch::TRUE : Switch::FALSE)
141  {}
142 
143  //- Construct from std::string, string, word
144  // Optionally allow bad words, and catch the error elsewhere
145  Switch(const std::string& str, const bool allowInvalid=false)
146  :
147  switch_(asEnum(str, allowInvalid))
148  {}
149 
150  //- Construct from character array
151  // Optionally allow bad words, and catch the error elsewhere
152  Switch(const char* str, const bool allowInvalid=false)
153  :
154  switch_(asEnum(std::string(str), allowInvalid))
155  {}
156 
157  //- Construct from Istream
158  Switch(Istream& is);
159 
160  //- Construct from dictionary, supplying default value so that if the
161  // value is not found, it is added into the dictionary.
163  (
164  const word&,
165  dictionary&,
166  const Switch& defaultValue = false
167  );
168 
169 
170  // Member Functions
171 
172  //- Return true if the Switch has a valid value
173  bool valid() const;
174 
175  //- Return a text representation of the Switch
176  const char* asText() const;
177 
178  //- Update the value of the Switch if it is found in the dictionary
179  bool readIfPresent(const word&, const dictionary&);
180 
181 
182  // Member Operators
183 
184  //- Conversion to bool
185  operator bool() const
186  {
187  return (switch_ & 0x1);
188  }
189 
190  //- Assignment to enumerated value
191  void operator=(const switchType sw)
192  {
193  switch_ = sw;
194  }
195 
196  //- Assignment to bool
197  void operator=(const bool b)
198  {
199  switch_ = (b ? Switch::TRUE : Switch::FALSE);
200  }
201 
202 
203  // IOstream Operators
204 
205  friend Istream& operator>>(Istream&, Switch&);
206  friend Ostream& operator<<(Ostream&, const Switch&);
207 };
208 
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 } // End namespace Foam
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 #endif
217 
218 // ************************************************************************* //
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:117
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:129
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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.
Definition: Switch.H:60
static const char * names[INVALID+1]
The set of names corresponding to the switchType enumeration.
Definition: Switch.H:103
static Switch lookupOrAddToDict(const word &, dictionary &, const Switch &defaultValue=false)
Construct from dictionary, supplying default value so that if the.
Definition: Switch.C:105
friend Ostream & operator<<(Ostream &, const Switch &)
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
A class for handling words, derived from string.
Definition: word.H:59
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:53
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:123
Switch()
Construct null as false.
Definition: Switch.H:119
void operator=(const switchType sw)
Assignment to enumerated value.
Definition: Switch.H:190
Ostream & operator<<(Ostream &, const ensightPart &)
switchType
The various text representations for a switch value.
Definition: Switch.H:88
A class for handling character strings derived from std::string.
Definition: string.H:74
System bool.
Namespace for OpenFOAM.