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-2023 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 "Switch.H"
30 #include "MinMax.H"
31 #include "error.H"
32 #include "word.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 (
38  scalarRange::rangeTypes::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.reset();
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.good();
120 }
121 
122 
123 Foam::scalarRange Foam::scalarRange::parse(const std::string& str)
124 {
125  scalarRange range;
126 
127  if (!parse(str, range))
128  {
129  Info<< "Bad scalar-range while parsing: " << str << endl;
130  }
132  return range;
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
139 :
140  min_(range.min()),
141  max_(range.max()),
142  type_(max_ < min_ ? rangeTypes::NONE : rangeTypes::GE_LE)
143 {}
144 
145 
147 :
148  min_(range.min()),
149  max_(range.max()),
150  type_(max_ < min_ ? rangeTypes::NONE : rangeTypes::GE_LE)
151 {}
152 
153 
154 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
155 
157 {
158  switch (type_)
159  {
160  case rangeTypes::EQ:
161  os << min_;
162  break;
163 
164  case rangeTypes::GE:
165  case rangeTypes::GT:
166  os << min_ << ":Inf";
167  break;
168 
169  case rangeTypes::LE:
170  case rangeTypes::LT:
171  os << "-Inf:" << max_;
172  break;
173 
174  case rangeTypes::GE_LE:
175  os << min_ << ':' << max_;
176  break;
177 
178  case rangeTypes::ALWAYS:
179  os << "true";
180  break;
181 
182  default:
183  os << "none";
184  break;
185  }
186 }
187 
188 
189 Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
190 {
191  range.print(os);
192  return os;
193 }
194 
195 
196 // ************************************************************************* //
bool good() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:307
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:36
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.
Definition: Switch.H:77
scalar range
static const scalarRange always
A range that always matches.
Definition: scalarRange.H:124
static bool parse(const std::string &str, scalarRange &range)
Construct by parsing string content.
Definition: scalarRange.C:39
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:68
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
messageStream Info
Information stream (stdout output on master, null elsewhere)
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:96
void print(Ostream &os) const
Print information about the range.
Definition: scalarRange.C:149
static Switch find(const std::string &str)
Find switchType for the given string, returning as a Switch that can be tested for good() or bad()...
Definition: Switch.C:147
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:63