initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / bools / Switch / Switch.H
blobadda09cf4ecfa2895f61f167f3d66d300b79b845
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Class
26     Foam::Switch
28 Description
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.
32 SourceFiles
33     Switch.C
34     SwitchIO.C
36 \*---------------------------------------------------------------------------*/
38 #ifndef Switch_H
39 #define Switch_H
41 #include "bool.H"
42 #include "word.H"
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 namespace Foam
49 // Forward declaration of friend functions and operators
51 class Switch;
53 Istream& operator>>(Istream&, Switch&);
54 Ostream& operator<<(Ostream&, const Switch&);
56 class dictionary;
58 /*---------------------------------------------------------------------------*\
59                            Class Switch Declaration
60 \*---------------------------------------------------------------------------*/
62 class Switch
64 private:
66     // Private data
68         //- The logic and enumerated text representation stored as a single byte
69         unsigned char switch_;
71 public:
73     // Public data types
75         //- The various text representations for a switch value.
76         //  These also correspond to the entries in names.
77         enum switchType
78         {
79             FALSE = 0,  TRUE  = 1,
80             OFF   = 2,  ON    = 3,
81             NO    = 4,  YES   = 5,
82             NO_1  = 6,  YES_1 = 7,
83             NONE  = 8,  PLACEHOLDER = 9,
84             INVALID
85         };
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
103         (
104             const std::string&,
105             const bool allowInvalid=false
106         );
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
113         static bool asBool
114         (
115             const std::string&,
116             const bool allowInvalid=false
117         );
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);
126     // Constructors
128         //- Construct null as false
129         Switch()
130         :
131             switch_(Switch::FALSE)
132         {}
134         //- Construct from bool
135         Switch(const bool value)
136         :
137             switch_(asEnum(value))
138         {}
140         //- Construct from integer values (treats integer as bool value)
141         Switch(const int value)
142         :
143             switch_(asEnum(bool(value)))
144         {}
146         //- Construct from std::string, string, word
147         Switch(const std::string& value)
148         :
149             switch_(asEnum(value))
150         {}
152         //- Construct from character array
153         Switch(const char* value)
154         :
155             switch_(asEnum(std::string(value)))
156         {}
158         //- Construct from Istream
159         Switch(Istream& is);
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
164         (
165             const word&,
166             dictionary&,
167             const Switch& defaultValue = false
168         );
171     // Member Operators
173         //- Conversion to bool
174         operator bool() const
175         {
176             return (switch_ & 0x1);
177         }
179         //- Assignment from bool
180         const Switch& operator=(const bool b)
181         {
182             switch_ = (b ? Switch::TRUE : Switch::FALSE);
183             return *this;
184         }
187     // Member fuctions
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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 #endif
208 // ************************************************************************* //