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