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