FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

Switch.C

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-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 \*---------------------------------------------------------------------------*/
00025 
00026 #include "Switch.H"
00027 #include <OpenFOAM/error.H>
00028 #include <OpenFOAM/dictionary.H>
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 // NB: values chosen such that bitwise '&' 0x1 yields the bool value
00033 // INVALID is also evaluates to false, but don't rely on that
00034 const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
00035 {
00036     "false", "true",
00037     "off",   "on",
00038     "no",    "yes",
00039     "n",     "y",
00040     "none",  "true",  // is there a reasonable counterpart to "none"?
00041     "invalid"
00042 };
00043 
00044 
00045 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
00046 
00047 Foam::Switch::switchType Foam::Switch::asEnum(const bool b)
00048 {
00049     return b ? Switch::TRUE : Switch::FALSE;
00050 }
00051 
00052 
00053 Foam::Switch::switchType Foam::Switch::asEnum
00054 (
00055     const std::string& str,
00056     const bool allowInvalid
00057 )
00058 {
00059     for (int sw = 0; sw < Switch::INVALID; sw++)
00060     {
00061         if (str == names[sw])
00062         {
00063             // convert n/y to no/yes (perhaps should deprecate y/n)
00064             if (sw == Switch::NO_1 || sw == Switch::NONE)
00065             {
00066                 return Switch::NO;
00067             }
00068             else if (sw == Switch::YES_1)
00069             {
00070                 return Switch::YES;
00071             }
00072             else
00073             {
00074                 return switchType(sw);
00075             }
00076         }
00077     }
00078 
00079     if (!allowInvalid)
00080     {
00081         FatalErrorIn("Switch::asEnum(const std::string&)")
00082             << "unknown switch word " << str << nl
00083             << abort(FatalError);
00084     }
00085 
00086     return INVALID;
00087 }
00088 
00089 
00090 bool Foam::Switch::asBool(const switchType sw)
00091 {
00092     // relies on (INVALID & 0x1) evaluating to false
00093     return (sw & 0x1);
00094 }
00095 
00096 
00097 bool Foam::Switch::asBool
00098 (
00099     const std::string& str,
00100     const bool allowInvalid
00101 )
00102 {
00103     // allow invalid values, but catch after for correct error message
00104     switchType sw = asEnum(str, true);
00105 
00106     if (sw == Switch::INVALID)
00107     {
00108         if (!allowInvalid)
00109         {
00110             FatalErrorIn("Switch::asBool(const std::string&)")
00111                 << "unknown switch word " << str << nl
00112                 << abort(FatalError);
00113         }
00114 
00115         return false;
00116     }
00117 
00118 
00119     return (sw & 0x1);
00120 }
00121 
00122 
00123 const char* Foam::Switch::asText(const bool b)
00124 {
00125     return b ? names[Switch::TRUE] : names[Switch::FALSE];
00126 }
00127 
00128 
00129 const char* Foam::Switch::asText(const switchType sw)
00130 {
00131     return names[sw];
00132 }
00133 
00134 
00135 Foam::Switch Foam::Switch::lookupOrAddToDict
00136 (
00137     const word& name,
00138     dictionary& dict,
00139     const Switch& defaultValue
00140 )
00141 {
00142     return dict.lookupOrAddDefault<Switch>(name, defaultValue);
00143 }
00144 
00145 
00146 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
00147 
00148 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
00149 {
00150     return dict.readIfPresent<Switch>(name, *this);
00151 }
00152 
00153 
00154 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines