scalarRange.H
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 Class
27  Foam::scalarRange
28 
29 Description
30  Scalar bounds to be used as a unary predicate.
31 
32  The bound can be specified as an "MIN:MAX" range, as a "MIN:" or ":MAX"
33  bound or simply as a single "VALUE".
34 
35  When defined via the parse() method, the special string "none" can be
36  used to define an empty (inverse) range.
37 
38 SeeAlso
39  Foam::MinMax
40  Foam::predicates::scalars
41 
42 SourceFiles
43  scalarRange.cxx
44  scalarRangeI.H
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef Foam_scalarRange_H
49 #define Foam_scalarRange_H
50 
51 #include "scalar.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward Declarations
59 class scalarRange;
60 template<class T> class MinMax;
61 
62 Ostream& operator<<(Ostream& os, const scalarRange& range);
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class scalarRange Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class scalarRange
70 {
71  //- The type of range
72  enum rangeTypes : unsigned char
73  {
74  NONE = 0,
75  EQ,
76  GE,
77  GT,
78  LE,
79  LT,
80  GE_LE,
81  ALWAYS
82  };
83 
84 
85  // Private Member Data
86 
87  //- The min value of the range
88  scalar min_;
89 
90  //- The max value of the range
91  scalar max_;
92 
93  //- The range type
94  enum rangeTypes type_;
95 
96  //- Construct from components, no checks
97  inline constexpr scalarRange
98  (
99  const rangeTypes type,
100  const scalar minVal,
101  const scalar maxVal
102  ) noexcept;
103 
104 
105 public:
106 
107  // STL type definitions
108 
109  //- Type of values the range contains
110  typedef scalar value_type;
111 
112 
113  // Static Data Members
114 
115  //- A range that always matches
116  static const scalarRange always;
118 
119  // Constructors
120 
121  //- Construct an empty (inverse, NONE) range - never matches
122  inline constexpr scalarRange() noexcept;
123 
124  //- Construct an exact value matcher
125  inline explicit constexpr scalarRange(const scalar val) noexcept;
126 
127  //- Construct a range from min-value to max-value
128  // Check validity of the range and sets to NONE or EQ if required.
129  inline scalarRange(const scalar minVal, const scalar maxVal) noexcept;
130 
131  //- Copy construct from a min/max range.
132  // Automatically decides if this is a GE_LE or NONE range
133  explicit scalarRange(const MinMax<label>& range) noexcept;
134 
135  //- Copy construct from a min/max range.
136  // Automatically decides if this is a GE_LE or NONE range
137  explicit scalarRange(const MinMax<scalar>& range) noexcept;
138 
139 
140  // Static Constructors
141 
142  //- Construct by parsing string content.
143  // A colon (:) is used as a range marker or when specifying
144  // greater-than or less-than bounds.
145  //
146  // \note The special string "none" can be used define an empty
147  // (inverse) range
148  //
149  // \return True if no parse problems were encountered.
150  static bool parse(const std::string& str, scalarRange& range);
151 
152  //- Construct by parsing string content.
153  // Any parse problems are emitted as information and the returned
154  // range is of type empty().
155  // \return The parsed range, which is empty() on any problems
156  static scalarRange parse(const std::string& str);
157 
158 
159  //- A greater-equals bound
160  inline static constexpr scalarRange ge(const scalar minVal) noexcept;
161 
162  //- A greater-than bound
163  inline static constexpr scalarRange gt(const scalar minVal) noexcept;
164 
165  //- A greater-equals zero bound
166  inline static constexpr scalarRange ge0() noexcept;
167 
168  //- A greater-than zero bound
169  inline static constexpr scalarRange gt0() noexcept;
170 
171  //- A less-equals bound
172  inline static constexpr scalarRange le(const scalar maxVal) noexcept;
173 
174  //- A less-than bound
175  inline static constexpr scalarRange lt(const scalar maxVal) noexcept;
176 
177  //- A greater-equals 0, less-equals 1 bound
178  inline static constexpr scalarRange zero_one() noexcept;
179 
180 
181  // Member Functions
182 
183  //- True if range is empty (eg, inverted, NONE)
184  inline bool empty() const noexcept;
185 
186  //- True if range is non-empty.
187  inline bool good() const noexcept;
188 
189  //- True if the range bounds represent a single value.
190  inline bool single() const noexcept;
191 
192  //- The min value of the range.
193  scalar min() const noexcept { return min_; }
194 
195  //- The max value of the range.
196  scalar max() const noexcept { return max_; }
197 
198  //- Reset to an empty (inverse, NONE) range.
199  inline void reset() noexcept;
200 
201  //- Same as reset() - reset to an empty (inverse, NONE) range.
202  void clear() noexcept { reset(); }
203 
204 
205  //- A representative (average) value for the range.
206  // For GE, LE bounds it is the min/max value, respectively.
207  inline scalar value() const;
208 
209  //- True if the value is matched by the condition
210  inline bool contains(const scalar val) const;
211 
212  //- True if the value is matched by the condition
213  bool match(const scalar value) const { return contains(value); }
214 
215 
216  // Member Operators
217 
218  //- For use as a predicate, same as contains(), match()
219  bool operator()(const scalar value) const { return contains(value); }
220 
221  inline constexpr bool operator==(const scalarRange& rhs) const noexcept;
222  inline constexpr bool operator!=(const scalarRange& rhs) const noexcept;
223 
224 
225  // IOstream Operators
226 
227  //- Print information about the range
228  void print(Ostream& os) const;
229 
230  //- Print information about the range
231  friend Ostream& operator<<(Ostream& os, const scalarRange& range);
232 
233 
234  // Housekeeping
235 
236  //- Same as good() or !empty()
237  bool valid() const noexcept { return good(); }
238 };
239 
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 } // End namespace Foam
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 #include "scalarRangeI.H"
249 #endif
250 
251 // ************************************************************************* //
constexpr scalarRange() noexcept
Construct an empty (inverse, NONE) range - never matches.
Definition: scalarRangeI.H:36
static constexpr scalarRange gt0() noexcept
A greater-than zero bound.
Definition: scalarRangeI.H:89
A min/max value pair with additional methods. In addition to conveniently storing values...
Definition: HashSet.H:72
bool contains(const scalar val) const
True if the value is matched by the condition.
Definition: scalarRangeI.H:166
void reset() noexcept
Reset to an empty (inverse, NONE) range.
Definition: scalarRangeI.H:117
bool good() const noexcept
True if range is non-empty.
Definition: scalarRangeI.H:131
scalar range
friend Ostream & operator<<(Ostream &os, const scalarRange &range)
Print information about the range.
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
scalar max() const noexcept
The max value of the range.
Definition: scalarRange.H:248
bool match(const scalar value) const
True if the value is matched by the condition.
Definition: scalarRange.H:276
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:801
static const scalarRange always
A range that always matches.
Definition: scalarRange.H:125
void clear() noexcept
Same as reset() - reset to an empty (inverse, NONE) range.
Definition: scalarRange.H:258
bool operator()(const scalar value) const
For use as a predicate, same as contains(), match()
Definition: scalarRange.H:284
static constexpr scalarRange ge(const scalar minVal) noexcept
A greater-equals bound.
Definition: scalarRangeI.H:68
scalar value_type
Type of values the range contains.
Definition: scalarRange.H:117
const direction noexcept
Definition: scalarImpl.H:265
static bool parse(const std::string &str, scalarRange &range)
Construct by parsing string content.
static constexpr scalarRange ge0() noexcept
A greater-equals zero bound.
Definition: scalarRangeI.H:82
OBJstream os(runTime.globalPath()/outputName)
static constexpr scalarRange gt(const scalar minVal) noexcept
A greater-than bound.
Definition: scalarRangeI.H:75
scalar min() const noexcept
The min value of the range.
Definition: scalarRange.H:243
Represents 0/1 range or concept. Used for tagged dispatch or clamping.
Definition: pTraits.H:51
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
constexpr bool operator!=(const scalarRange &rhs) const noexcept
Definition: scalarRangeI.H:192
bool empty() const noexcept
True if range is empty (eg, inverted, NONE)
Definition: scalarRangeI.H:125
bool valid() const noexcept
Same as good() or !empty()
Definition: scalarRange.H:308
static constexpr scalarRange le(const scalar maxVal) noexcept
A less-equals bound.
Definition: scalarRangeI.H:96
scalar value() const
A representative (average) value for the range.
Definition: scalarRangeI.H:143
void print(Ostream &os) const
Print information about the range.
constexpr bool operator==(const scalarRange &rhs) const noexcept
Definition: scalarRangeI.H:185
static constexpr scalarRange lt(const scalar maxVal) noexcept
A less-than bound.
Definition: scalarRangeI.H:102
Namespace for OpenFOAM.
bool single() const noexcept
True if the range bounds represent a single value.
Definition: scalarRangeI.H:137
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:64