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) 2011 OpenFOAM Foundation
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 "error.H"
28 #include "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  "f", "t",
41  "none", "true", // is there a reasonable counterpart to "none"?
42  "invalid"
43 };
44 
45 
46 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
47 
48 Foam::Switch::switchType Foam::Switch::asEnum
49 (
50  const std::string& str,
51  const bool allowInvalid
52 )
53 {
54  for (int sw = 0; sw < Switch::INVALID; ++sw)
55  {
56  if (str == names[sw])
57  {
58  // handle aliases
59  switch (sw)
60  {
61  case Switch::NO_1:
62  case Switch::NONE:
63  {
64  return Switch::NO;
65  break;
66  }
67 
68  case Switch::YES_1:
69  {
70  return Switch::YES;
71  break;
72  }
73 
74  case Switch::FALSE_1:
75  {
76  return Switch::FALSE;
77  break;
78  }
79 
80  case Switch::TRUE_1:
81  {
82  return Switch::TRUE;
83  break;
84  }
85 
86  default:
87  {
88  return switchType(sw);
89  break;
90  }
91  }
92  }
93  }
94 
95  if (!allowInvalid)
96  {
97  FatalErrorIn("Switch::asEnum(const std::string&, const bool)")
98  << "unknown switch word " << str << nl
99  << abort(FatalError);
100  }
101 
102  return Switch::INVALID;
103 }
104 
105 
107 (
108  const word& name,
109  dictionary& dict,
110  const Switch& defaultValue
111 )
112 {
113  return dict.lookupOrAddDefault<Switch>(name, defaultValue);
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
118 
120 {
121  return switch_ <= Switch::NONE;
122 }
123 
124 
125 const char* Foam::Switch::asText() const
126 {
127  return names[switch_];
128 }
129 
130 
131 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
132 {
133  return dict.readIfPresent<Switch>(name, *this);
134 }
135 
136 
137 // ************************************************************************* //
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static Switch lookupOrAddToDict(const word &, dictionary &, const Switch &defaultValue=false)
Construct from dictionary, supplying default value so that if the.
Definition: Switch.C:107
A class for handling words, derived from string.
Definition: word.H:59
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none.
Definition: Switch.H:60
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
static const char nl
Definition: Ostream.H:260
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:131
error FatalError
static const char * names[INVALID+1]
The set of names corresponding to the switchType enumeration.
Definition: Switch.H:103
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
switchType
The various text representations for a switch value.
Definition: Switch.H:88
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:119
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:125