initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / Switch / Switch.H
blob71b25eaa3a3d9d61524785dfb7cc9702f954ab1a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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
30     on/off, yes/no or y/n.
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 data
66         word wordSwitch_;
67         bool logicalSwitch_;
69     // Private member functions
71         word wordValue(const bool) const;
72         bool boolValue(const word&) const;
75 public:
77     // Constructors
79         //- Construct from components
80         Switch(const bool value)
81         :
82             wordSwitch_(wordValue(value)),
83             logicalSwitch_(value)
84         {}
86         //- Construct from word
87         Switch(const word& w)
88         :
89             wordSwitch_(w),
90             logicalSwitch_(boolValue(w))
91         {}
93         //- Construct from Istream
94         Switch(Istream& is);
96         //- Construct from dictionary, supplying default value so that if the
97         //  value is not found, it is added into the dictionary.
98         static Switch lookupOrAddToDict
99         (
100             const word&,
101             dictionary&,
102             const Switch& defaultValue = false
103         );
106     // Member Operators
108         Switch& operator=(const Switch& s)
109         {
110             wordSwitch_ = s.wordSwitch_;
111             logicalSwitch_ = s.logicalSwitch_;
113             return *this;
114         }
116         operator bool() const
117         {
118             return logicalSwitch_;
119         }
121         operator const word&() const
122         {
123             return wordSwitch_;
124         }
127     // Member fuctions
129         //- Update the value of the Switch if it is found in the dictionary
130         bool readIfPresent(const word&, const dictionary&);
133     // IOstream Operators
135         friend Istream& operator>>(Istream&, Switch&);
136         friend Ostream& operator<<(Ostream&, const Switch&);
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
142 } // End namespace Foam
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146 #endif
148 // ************************************************************************* //