FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 or y/n 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 <OpenFOAM/bool.H>
41 #include <OpenFOAM/word.H>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward declaration of friend functions and operators
49 
50 class Switch;
51 
52 Istream& operator>>(Istream&, Switch&);
53 Ostream& operator<<(Ostream&, const Switch&);
54 
55 class dictionary;
56 
57 /*---------------------------------------------------------------------------*\
58  Class Switch Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 class Switch
62 {
63 private:
64 
65  // Private data
66 
67  //- The logic and enumerated text representation stored as a single byte
68  unsigned char switch_;
69 
70 public:
71 
72  // Public data types
73 
74  //- The various text representations for a switch value.
75  // These also correspond to the entries in names.
77  {
78  FALSE = 0, TRUE = 1,
79  OFF = 2, ON = 3,
80  NO = 4, YES = 5,
81  NO_1 = 6, YES_1 = 7,
82  NONE = 8, PLACEHOLDER = 9,
84  };
85 
86 
87  // Static data members
88 
89  //- The set of names corresponding to the switchType enumeration
90  // Includes an extra entry for "invalid".
91  static const char* names[INVALID+1];
92 
93 
94  // Static Member Functions
95 
96  //- Return a switchType representation of a bool
97  static switchType asEnum(const bool);
98 
99  //- Return a switchType representation of a word
100  // Optionally allow bad words, and catch the error elsewhere
101  static switchType asEnum
102  (
103  const std::string&,
104  const bool allowInvalid=false
105  );
106 
107  //- Return a bool representation of a switchType
108  static bool asBool(const switchType);
109 
110  //- Return a bool representation of a word
111  // Optionally allow bad words, and catch the error elsewhere
112  static bool asBool
113  (
114  const std::string&,
115  const bool allowInvalid=false
116  );
117 
118  //- Return a text representation of a bool value
119  static const char* asText(const bool);
120 
121  //- Return a text representation of a switchType
122  static const char* asText(const switchType);
123 
124 
125  // Constructors
126 
127  //- Construct null as false
129  :
130  switch_(Switch::FALSE)
131  {}
132 
133  //- Construct from bool
134  Switch(const bool value)
135  :
136  switch_(asEnum(value))
137  {}
138 
139  //- Construct from integer values (treats integer as bool value)
140  Switch(const int value)
141  :
142  switch_(asEnum(bool(value)))
143  {}
144 
145  //- Construct from std::string, string, word
146  Switch(const std::string& value)
147  :
148  switch_(asEnum(value))
149  {}
150 
151  //- Construct from character array
152  Switch(const char* value)
153  :
154  switch_(asEnum(std::string(value)))
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 Operators
171 
172  //- Conversion to bool
173  operator bool() const
174  {
175  return (switch_ & 0x1);
176  }
177 
178  //- Assignment from bool
179  const Switch& operator=(const bool b)
180  {
181  switch_ = (b ? Switch::TRUE : Switch::FALSE);
182  return *this;
183  }
184 
185 
186  // Member fuctions
187 
188  //- Update the value of the Switch if it is found in the dictionary
189  bool readIfPresent(const word&, const dictionary&);
190 
191 
192  // IOstream Operators
193 
194  friend Istream& operator>>(Istream&, Switch&);
195  friend Ostream& operator<<(Ostream&, const Switch&);
196 };
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 } // End namespace Foam
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #endif
206 
207 // ************************ vim: set sw=4 sts=4 et: ************************ //