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-2019 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 
39 (
40  scalarRange::ALWAYS,
41  -GREAT,
42  GREAT
43 );
44 
45 
46 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
47 
48 bool Foam::scalarRange::parse(const std::string& str, scalarRange& range)
49 {
50  range.clear();
51 
52  const auto colon = str.find(':');
53 
54  if (colon == std::string::npos)
55  {
56  // No colon
57 
58  // Use Switch to accept none/true/false.
59  // Do not accept shorter ones though (f|n|t|y) or (on|off|yes|no)
60  // since these are not really appropriate here.
61 
62  if (str.size() >= 4)
63  {
64  Switch sw(str, true);
65 
66  if (sw.valid())
67  {
68  if (sw)
69  {
71  }
72 
73  return true; // parsed ok
74  }
75  }
76 
77  // "VALUE"
78  scalar val;
79  if (readScalar(str, val))
80  {
82  }
83  }
84  else if (str.find(':', colon+1) != std::string::npos)
85  {
86  // A second colon is a syntax error
87  return false;
88  }
89  else if (colon == 0)
90  {
91  // ":MAX"
92  scalar val;
93  if (readScalar(str.substr(1), val))
94  {
96  }
97  }
98  else if (colon == str.size()-1)
99  {
100  // "MIN:"
101  scalar val;
102  if (readScalar(str.substr(0, colon), val))
103  {
105  }
106  }
107  else
108  {
109  // "MIN:MAX"
110  scalar minVal, maxVal;
111  if
112  (
113  readScalar(str.substr(0, colon), minVal)
114  && readScalar(str.substr(colon+1), maxVal)
115  )
116  {
117  range = scalarRange(minVal, maxVal);
118  }
119  }
120 
121  return range.valid();
122 }
123 
124 
126 {
128 
129  if (!parse(str, range))
130  {
131  Info<< "Bad scalar-range while parsing: " << str << endl;
132  }
133 
134  return range;
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
139 
141 :
142  min_(range.min()),
143  max_(range.max()),
144  type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
145 {}
146 
147 
149 :
150  min_(range.min()),
151  max_(range.max()),
152  type_(max_ < min_ ? scalarRange::NONE : scalarRange::GE_LE)
153 {}
154 
155 
156 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
157 
159 {
160  switch (range.type_)
161  {
162  case scalarRange::EQ:
163  os << range.min();
164  break;
165 
166  case scalarRange::GE:
167  case scalarRange::GT:
168  os << range.min() << ":Inf";
169  break;
170 
171  case scalarRange::LE:
172  case scalarRange::LT:
173  os << "-Inf:" << range.max();
174  break;
175 
176  case scalarRange::GE_LE:
177  os << range.min() << ':' << range.max();
178  break;
179 
180  case scalarRange::ALWAYS:
181  os << "true";
182  break;
183 
184  default:
185  os << "none";
186  break;
187  }
188 
189  return os;
190 }
191 
192 
193 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:70
Foam::Switch::valid
bool valid() const noexcept
True if the Switch represents a valid enumeration.
Definition: Switch.C:203
MinMax.H
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::scalarRange::always
static const scalarRange always
A range that always matches.
Definition: scalarRange.H:117
Foam::scalarRange::parse
static bool parse(const std::string &str, scalarRange &range)
Construct by parsing string content.
Definition: scalarRange.C:48
Foam::scalarRange::null
static const scalarRange null
An empty/inverted range that never matches.
Definition: scalarRange.H:114
error.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::scalarRange
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:67
Switch.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::scalarRange::scalarRange
scalarRange() noexcept
Construct an empty (inverse) range.
Definition: scalarRangeI.H:43
Foam::scalarRange::le
static scalarRange le(const scalar maxVal) noexcept
Construct a less-equals bound.
Definition: scalarRangeI.H:98
Foam::scalarRange::ge
static scalarRange ge(const scalar minVal) noexcept
Construct a greater-equals bound.
Definition: scalarRangeI.H:74
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
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102