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

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