OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Switch.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::Switch
00027 
00028 Description
00029     A simple wrapper around bool so that it can be read as a word:
00030     true/false, on/off, yes/no or y/n or none.
00031 
00032 SourceFiles
00033     Switch.C
00034     SwitchIO.C
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef Switch_H
00039 #define Switch_H
00040 
00041 #include "bool.H"
00042 #include "word.H"
00043 
00044 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00045 
00046 namespace Foam
00047 {
00048 
00049 // Forward declaration of friend functions and operators
00050 
00051 class Switch;
00052 
00053 Istream& operator>>(Istream&, Switch&);
00054 Ostream& operator<<(Ostream&, const Switch&);
00055 
00056 class dictionary;
00057 
00058 /*---------------------------------------------------------------------------*\
00059                            Class Switch Declaration
00060 \*---------------------------------------------------------------------------*/
00061 
00062 class Switch
00063 {
00064 private:
00065 
00066     // Private data
00067 
00068         //- The logic and enumerated text representation stored as a single byte
00069         unsigned char switch_;
00070 
00071 public:
00072 
00073     // Public data types
00074 
00075         //- The various text representations for a switch value.
00076         //  These also correspond to the entries in names.
00077         enum switchType
00078         {
00079             FALSE = 0,  TRUE  = 1,
00080             OFF   = 2,  ON    = 3,
00081             NO    = 4,  YES   = 5,
00082             NO_1  = 6,  YES_1 = 7,
00083             NONE  = 8,  PLACEHOLDER = 9,
00084             INVALID
00085         };
00086 
00087 
00088     // Static data members
00089 
00090         //- The set of names corresponding to the switchType enumeration
00091         //  Includes an extra entry for "invalid".
00092         static const char* names[INVALID+1];
00093 
00094 
00095     // Static Member Functions
00096 
00097         //- Return a switchType representation of a bool
00098         static switchType asEnum(const bool);
00099 
00100         //- Return a switchType representation of a word
00101         //  Optionally allow bad words, and catch the error elsewhere
00102         static switchType asEnum
00103         (
00104             const std::string&,
00105             const bool allowInvalid=false
00106         );
00107 
00108         //- Return a bool representation of a switchType
00109         static bool asBool(const switchType);
00110 
00111         //- Return a bool representation of a word
00112         //  Optionally allow bad words, and catch the error elsewhere
00113         static bool asBool
00114         (
00115             const std::string&,
00116             const bool allowInvalid=false
00117         );
00118 
00119         //- Return a text representation of a bool value
00120         static const char* asText(const bool);
00121 
00122         //- Return a text representation of a switchType
00123         static const char* asText(const switchType);
00124 
00125 
00126     // Constructors
00127 
00128         //- Construct null as false
00129         Switch()
00130         :
00131             switch_(Switch::FALSE)
00132         {}
00133 
00134         //- Construct from bool
00135         Switch(const bool value)
00136         :
00137             switch_(asEnum(value))
00138         {}
00139 
00140         //- Construct from integer values (treats integer as bool value)
00141         Switch(const int value)
00142         :
00143             switch_(asEnum(bool(value)))
00144         {}
00145 
00146         //- Construct from std::string, string, word
00147         Switch(const std::string& value)
00148         :
00149             switch_(asEnum(value))
00150         {}
00151 
00152         //- Construct from character array
00153         Switch(const char* value)
00154         :
00155             switch_(asEnum(std::string(value)))
00156         {}
00157 
00158         //- Construct from Istream
00159         Switch(Istream& is);
00160 
00161         //- Construct from dictionary, supplying default value so that if the
00162         //  value is not found, it is added into the dictionary.
00163         static Switch lookupOrAddToDict
00164         (
00165             const word&,
00166             dictionary&,
00167             const Switch& defaultValue = false
00168         );
00169 
00170 
00171     // Member Operators
00172 
00173         //- Conversion to bool
00174         operator bool() const
00175         {
00176             return (switch_ & 0x1);
00177         }
00178 
00179         //- Assignment from bool
00180         const Switch& operator=(const bool b)
00181         {
00182             switch_ = (b ? Switch::TRUE : Switch::FALSE);
00183             return *this;
00184         }
00185 
00186 
00187     // Member fuctions
00188 
00189         //- Update the value of the Switch if it is found in the dictionary
00190         bool readIfPresent(const word&, const dictionary&);
00191 
00192 
00193     // IOstream Operators
00194 
00195         friend Istream& operator>>(Istream&, Switch&);
00196         friend Ostream& operator<<(Ostream&, const Switch&);
00197 };
00198 
00199 
00200 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00201 
00202 } // End namespace Foam
00203 
00204 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00205 
00206 #endif
00207 
00208 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd