Switch.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 "dictionary.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 const char* Foam::Switch::names[nSwitchType] =
32 {
33  "false",
34  "true",
35  "off",
36  "on",
37  "no",
38  "yes",
39  "n",
40  "y",
41  "f",
42  "t",
43  "none",
44  "any",
45  "invalid"
46 };
47 
48 
49 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
50 
51 Foam::Switch::switchType Foam::Switch::asEnum
52 (
53  const std::string& str,
54  const bool allowInvalid
55 )
56 {
58  {
59  if (str == names[toInt(sw)])
60  {
61  // Handle aliases
62  switch (sw)
63  {
64  case switchType::n:
65  case switchType::none:
66  {
67  return switchType::no;
68  break;
69  }
70 
71  case switchType::y:
72  case switchType::any:
73  {
74  return switchType::yes;
75  break;
76  }
77 
78  case switchType::f:
79  {
80  return switchType::False;
81  break;
82  }
83 
84  case switchType::t:
85  {
86  return switchType::True;
87  break;
88  }
89 
90  default:
91  {
92  return switchType(sw);
93  break;
94  }
95  }
96  }
97  }
98 
99  if (!allowInvalid)
100  {
102  << "unknown switch word " << str << nl
103  << abort(FatalError);
104  }
105 
106  return switchType::invalid;
107 }
108 
109 
111 (
112  const word& name,
113  dictionary& dict,
114  const Switch& defaultValue
115 )
116 {
117  return dict.lookupOrAddDefault<Switch>(name, defaultValue);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
122 
124 {
125  return switch_ <= switchType::none;
126 }
127 
128 
129 const char* Foam::Switch::asText() const
130 {
131  return names[toInt(switch_)];
132 }
133 
134 
135 bool Foam::Switch::readIfPresent(const word& name, const dictionary& dict)
136 {
137  return dict.readIfPresent<Switch>(name, *this);
138 }
139 
140 
141 // ************************************************************************* //
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:123
switchType
The various text representations for a switch value.
Definition: Switch.H:68
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:135
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
static unsigned char toInt(const switchType x)
Convert switchType to integer (unsigned char)
Definition: Switch.H:90
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/any.
Definition: Switch.H:60
static Switch lookupOrAddToDict(const word &, dictionary &, const Switch &defaultValue=false)
Construct from dictionary, supplying default value so that if the.
Definition: Switch.C:111
A class for handling words, derived from string.
Definition: word.H:59
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
static const char nl
Definition: Ostream.H:260
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:129
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47