1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
29 #include "dictionary.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 // NB: values chosen such that bitwise '&' 0x1 yields the bool value
34 // INVALID is also evaluates to false, but don't rely on that
35 const char* Foam::Switch::names[Foam::Switch::INVALID+1] =
41 "none", "true", // is there a reasonable counterpart to "none"?
46 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
48 Foam::Switch::switchType Foam::Switch::asEnum(const bool b)
50 return b ? Switch::TRUE : Switch::FALSE;
54 Foam::Switch::switchType Foam::Switch::asEnum
56 const std::string& str,
57 const bool allowInvalid
60 for (int sw = 0; sw < Switch::INVALID; sw++)
64 // convert n/y to no/yes (perhaps should deprecate y/n)
65 if (sw == Switch::NO_1 || sw == Switch::NONE)
69 else if (sw == Switch::YES_1)
75 return switchType(sw);
82 FatalErrorIn("Switch::asEnum(const std::string&)")
83 << "unknown switch word " << str << nl
91 bool Foam::Switch::asBool(const switchType sw)
93 // relies on (INVALID & 0x1) evaluating to false
98 bool Foam::Switch::asBool
100 const std::string& str,
101 const bool allowInvalid
104 // allow invalid values, but catch after for correct error message
105 switchType sw = asEnum(str, true);
107 if (sw == Switch::INVALID)
111 FatalErrorIn("Switch::asBool(const std::string&)")
112 << "unknown switch word " << str << nl
113 << abort(FatalError);
124 const char* Foam::Switch::asText(const bool b)
126 return b ? names[Switch::TRUE] : names[Switch::FALSE];
130 const char* Foam::Switch::asText(const switchType sw)
136 Foam::Switch Foam::Switch::lookupOrAddToDict
140 const Switch& defaultValue
143 return dict.lookupOrAddDefault<Switch>(name, defaultValue);
147 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
149 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
151 return dict.readIfPresent<Switch>(name, *this);
155 // ************************************************************************* //