1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 A simple wrapper around bool so that it can be read as a word:
30 true/false, on/off, yes/no or y/n or none.
36 \*---------------------------------------------------------------------------*/
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 // Forward declaration of friend functions and operators
53 Istream& operator>>(Istream&, Switch&);
54 Ostream& operator<<(Ostream&, const Switch&);
58 /*---------------------------------------------------------------------------*\
59 Class Switch Declaration
60 \*---------------------------------------------------------------------------*/
68 //- The logic and enumerated text representation stored as a single byte
69 unsigned char switch_;
75 //- The various text representations for a switch value.
76 // These also correspond to the entries in names.
83 NONE = 8, PLACEHOLDER = 9,
88 // Static data members
90 //- The set of names corresponding to the switchType enumeration
91 // Includes an extra entry for "invalid".
92 static const char* names[INVALID+1];
95 // Static Member Functions
97 //- Return a switchType representation of a bool
98 static switchType asEnum(const bool);
100 //- Return a switchType representation of a word
101 // Optionally allow bad words, and catch the error elsewhere
102 static switchType asEnum
105 const bool allowInvalid=false
108 //- Return a bool representation of a switchType
109 static bool asBool(const switchType);
111 //- Return a bool representation of a word
112 // Optionally allow bad words, and catch the error elsewhere
116 const bool allowInvalid=false
119 //- Return a text representation of a bool value
120 static const char* asText(const bool);
122 //- Return a text representation of a switchType
123 static const char* asText(const switchType);
128 //- Construct null as false
131 switch_(Switch::FALSE)
134 //- Construct from bool
135 Switch(const bool value)
137 switch_(asEnum(value))
140 //- Construct from integer values (treats integer as bool value)
141 Switch(const int value)
143 switch_(asEnum(bool(value)))
146 //- Construct from std::string, string, word
147 Switch(const std::string& value)
149 switch_(asEnum(value))
152 //- Construct from character array
153 Switch(const char* value)
155 switch_(asEnum(std::string(value)))
158 //- Construct from Istream
161 //- Construct from dictionary, supplying default value so that if the
162 // value is not found, it is added into the dictionary.
163 static Switch lookupOrAddToDict
167 const Switch& defaultValue = false
173 //- Conversion to bool
174 operator bool() const
176 return (switch_ & 0x1);
179 //- Assignment from bool
180 const Switch& operator=(const bool b)
182 switch_ = (b ? Switch::TRUE : Switch::FALSE);
189 //- Update the value of the Switch if it is found in the dictionary
190 bool readIfPresent(const word&, const dictionary&);
193 // IOstream Operators
195 friend Istream& operator>>(Istream&, Switch&);
196 friend Ostream& operator<<(Ostream&, const Switch&);
200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 } // End namespace Foam
204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 // ************************************************************************* //