initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / primitives / bools / Switch / Switch.C
blob8648532a32d30d5609cccee44d57677d8bc13cbb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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 \*---------------------------------------------------------------------------*/
27 #include "Switch.H"
28 #include "error.H"
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] =
37     "false", "true",
38     "off",   "on",
39     "no",    "yes",
40     "n",     "y",
41     "none",  "true",  // is there a reasonable counterpart to "none"?
42     "invalid"
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++)
61     {
62         if (str == names[sw])
63         {
64             // convert n/y to no/yes (perhaps should deprecate y/n)
65             if (sw == Switch::NO_1 || sw == Switch::NONE)
66             {
67                 return Switch::NO;
68             }
69             else if (sw == Switch::YES_1)
70             {
71                 return Switch::YES;
72             }
73             else
74             {
75                 return switchType(sw);
76             }
77         }
78     }
80     if (!allowInvalid)
81     {
82         FatalErrorIn("Switch::asEnum(const std::string&)")
83             << "unknown switch word " << str << nl
84             << abort(FatalError);
85     }
87     return INVALID;
91 bool Foam::Switch::asBool(const switchType sw)
93     // relies on (INVALID & 0x1) evaluating to false
94     return (sw & 0x1);
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)
108     {
109         if (!allowInvalid)
110         {
111             FatalErrorIn("Switch::asBool(const std::string&)")
112                 << "unknown switch word " << str << nl
113                 << abort(FatalError);
114         }
116         return false;
117     }
120     return (sw & 0x1);
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)
132     return names[sw];
136 Foam::Switch Foam::Switch::lookupOrAddToDict
138     const word& name,
139     dictionary& dict,
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 // ************************************************************************* //