scalarRange.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "scalarRange.H"
29 #include "string.H"
30 #include "Switch.H"
31 #include "MinMax.H"
32 #include "error.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 (
38  scalarRange::ALWAYS,
39  -GREAT,
40  GREAT
41 );
42 
43 
44 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
45 
46 bool Foam::scalarRange::parse(const std::string& str, scalarRange& range)
47 {
48  range.clear();
49 
50  const auto colon = str.find(':');
51 
52  if (colon == std::string::npos)
53  {
54  // No colon
55 
56  // Use Switch to accept none/true/false.
57  // Others like (f|n|t|y) and (on|off|yes|no) are not really
58  // appropriate, but don't worry about that now.
59 
60  if (str.size() >= 4)
61  {
62  Switch sw = Switch::find(str);
63 
64  if (sw.good())
65  {
66  if (sw)
67  {
69  }
70 
71  return true; // parsed ok
72  }
73  }
74 
75  // "VALUE"
76  scalar val;
77  if (readScalar(str, val))
78  {
79  range = scalarRange(val);
80  }
81  }
82  else if (str[colon+1] == ':')
83  {
84  // A double colon ("::") is a syntax error
85  return false;
86  }
87  else if (colon == 0)
88  {
89  // ":MAX"
90  scalar val;
91  if (readScalar(str.substr(1), val))
92  {
93  range = scalarRange::le(val);
94  }
95  }
96  else if (colon == str.size()-1)
97  {
98  // "MIN:"
99  scalar val;
100  if (readScalar(str.substr(0, colon), val))
101  {
102  range = scalarRange::ge(val);
103  }
104  }
105  else
106  {
107  // "MIN:MAX"
108  scalar minVal, maxVal;
109  if
110  (
111  readScalar(str.substr(0, colon), minVal)
112  && readScalar(str.substr(colon+1), maxVal)
113  )
114  {
115  range = scalarRange(minVal, maxVal);
116  }
117  }
118 
119  return range.valid();
120 }
121 
122 
124 {
126 
127  if (!parse(str, range))
128  {
129  Info<< "Bad scalar-range while parsing: " << str << endl;
130  }
131 
132  return range;
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 :
140  min_(range.min()),
141  max_(range.max()),
142  type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
143 {}
144 
145 
147 :
148  min_(range.min()),
149  max_(range.max()),
150  type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
151 {}
152 
153 
154 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
155 
157 {
158  switch (range.type_)
159  {
160  case scalarRange::EQ:
161  os << range.min();
162  break;
163 
164  case scalarRange::GE:
165  case scalarRange::GT:
166  os << range.min() << ":Inf";
167  break;
168 
169  case scalarRange::LE:
170  case scalarRange::LT:
171  os << "-Inf:" << range.max();
172  break;
173 
174  case scalarRange::GE_LE:
175  os << range.min() << ':' << range.max();
176  break;
177 
178  case scalarRange::ALWAYS:
179  os << "true";
180  break;
181 
182  default:
183  os << "none";
184  break;
185  }
186 
187  return os;
188 }
189 
190 
191 // ************************************************************************* //
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:77
Foam::scalarRange::le
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:103
MinMax.H
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Switch::good
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:300
Foam::Switch::find
static Switch find(const std::string &str)
Definition: Switch.C:151
Foam::scalarRange::always
static const scalarRange always
A range that always matches.
Definition: scalarRange.H:115
Foam::scalarRange::parse
static bool parse(const std::string &str, scalarRange &range)
Construct by parsing string content.
Definition: scalarRange.C:46
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::scalarRange::scalarRange
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:43
error.H
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::scalarRange
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:68
Foam::scalarRange::ge
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:75
Switch.H
os
OBJstream os(runTime.globalPath()/outputName)
range
scalar range
Definition: LISASMDCalcMethod1.H:12
scalarRange.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::MinMax
A min/max value pair with additional methods. In addition to conveniently storing values,...
Definition: HashSet.H:76