FreeFOAM The Cross-Platform CFD Toolkit
Switch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Switch.H"
27 #include <OpenFOAM/error.H>
28 #include <OpenFOAM/dictionary.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 // NB: values chosen such that bitwise '&' 0x1 yields the bool value
33 // INVALID is also evaluates to false, but don't rely on that
35 {
36  "false", "true",
37  "off", "on",
38  "no", "yes",
39  "n", "y",
40  "none", "true", // is there a reasonable counterpart to "none"?
41  "invalid"
42 };
43 
44 
45 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
46 
48 {
49  return b ? Switch::TRUE : Switch::FALSE;
50 }
51 
52 
54 (
55  const std::string& str,
56  const bool allowInvalid
57 )
58 {
59  for (int sw = 0; sw < Switch::INVALID; sw++)
60  {
61  if (str == names[sw])
62  {
63  // convert n/y to no/yes (perhaps should deprecate y/n)
64  if (sw == Switch::NO_1 || sw == Switch::NONE)
65  {
66  return Switch::NO;
67  }
68  else if (sw == Switch::YES_1)
69  {
70  return Switch::YES;
71  }
72  else
73  {
74  return switchType(sw);
75  }
76  }
77  }
78 
79  if (!allowInvalid)
80  {
81  FatalErrorIn("Switch::asEnum(const std::string&)")
82  << "unknown switch word " << str << nl
83  << abort(FatalError);
84  }
85 
86  return INVALID;
87 }
88 
89 
91 {
92  // relies on (INVALID & 0x1) evaluating to false
93  return (sw & 0x1);
94 }
95 
96 
98 (
99  const std::string& str,
100  const bool allowInvalid
101 )
102 {
103  // allow invalid values, but catch after for correct error message
104  switchType sw = asEnum(str, true);
105 
106  if (sw == Switch::INVALID)
107  {
108  if (!allowInvalid)
109  {
110  FatalErrorIn("Switch::asBool(const std::string&)")
111  << "unknown switch word " << str << nl
112  << abort(FatalError);
113  }
114 
115  return false;
116  }
117 
118 
119  return (sw & 0x1);
120 }
121 
122 
123 const char* Foam::Switch::asText(const bool b)
124 {
125  return b ? names[Switch::TRUE] : names[Switch::FALSE];
126 }
127 
128 
129 const char* Foam::Switch::asText(const switchType sw)
130 {
131  return names[sw];
132 }
133 
134 
136 (
137  const word& name,
138  dictionary& dict,
139  const Switch& defaultValue
140 )
141 {
142  return dict.lookupOrAddDefault<Switch>(name, defaultValue);
143 }
144 
145 
146 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
147 
148 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
149 {
150  return dict.readIfPresent<Switch>(name, *this);
151 }
152 
153 
154 // ************************ vim: set sw=4 sts=4 et: ************************ //