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-2024 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 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
111 
113 {
114  return switch_ <= switchType::none;
115 }
116 
117 
118 const char* Foam::Switch::asText() const
119 {
120  return names[toInt(switch_)];
121 }
122 
123 
125 {
126  return dict.readIfPresent<Switch>(name, *this);
127 }
128 
129 
130 // ************************************************************************* //
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
switchType
The various text representations for a switch value.
Definition: Switch.H:69
bool readIfPresent(const word &, const dictionary &)
Update the value of the Switch if it is found in the dictionary.
Definition: Switch.C:124
bool valid() const
Return true if the Switch has a valid value.
Definition: Switch.C:112
const char * asText() const
Return a text representation of the Switch.
Definition: Switch.C:118
static unsigned char toInt(const switchType x)
Convert switchType to integer (unsigned char)
Definition: Switch.H:90
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
errorManip< error > abort(error &err)
Definition: errorManip.H:131
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
static const char nl
Definition: Ostream.H:267
dictionary dict