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