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-2015 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 
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  {
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 
132 {
133  return dict.readIfPresent<Switch>(name, *this);
134 }
135 
136 
137 // ************************************************************************* //
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:60
Foam::Switch::asEnum
static switchType asEnum(const std::string &, const bool allowInvalid)
Return a switchType representation of a word.
Definition: Switch.C:49
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::Switch::NONE
@ NONE
Definition: Switch.H:95
Foam::Switch::valid
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:119
Foam::Switch::INVALID
@ INVALID
Definition: Switch.H:96
INVALID
@ INVALID
Definition: setSet.C:639
error.H
Foam::nl
static const char nl
Definition: Ostream.H:260
Switch.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::Switch::names
static const char * names[INVALID+1]
The set of names corresponding to the switchType enumeration.
Definition: Switch.H:103
Foam::Switch::asText
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:125
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::Switch::readIfPresent
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:131
Foam::Switch::switchType
switchType
The various text representations for a switch value.
Definition: Switch.H:88
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Switch::switch_
unsigned char switch_
The logic and enumerated text representation stored as a single byte.
Definition: Switch.H:65
dictionary.H
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::Switch::lookupOrAddToDict
static Switch lookupOrAddToDict(const word &, dictionary &, const Switch &defaultValue=false)
Construct from dictionary, supplying default value so that if the.
Definition: Switch.C:107