1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "scalarRange.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 Foam::debug::debugSwitch
32 Foam::scalarRange::debug
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 Foam::scalarRange::scalarRange()
49 Foam::scalarRange::scalarRange(const scalar& lower, const scalar& upper)
55 // mark invalid range as empty
64 Foam::scalarRange::scalarRange(Istream& is)
72 if (scalarRange::debug)
74 Info<<"constructed scalarRange: " << *this << endl;
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
81 bool Foam::scalarRange::isDefined() const
83 return type_ != EMPTY;
87 bool Foam::scalarRange::isExact() const
89 return type_ == EXACT;
93 Foam::scalar Foam::scalarRange::value() const
99 Foam::scalar Foam::scalarRange::lower() const
111 Foam::scalar Foam::scalarRange::upper() const
130 bool Foam::scalarRange::selected(const scalar& value) const
135 return value >= value_;
138 return value <= value_;
141 return value >= value_ && value <= value2_;
144 return value == value_;
153 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
155 bool Foam::scalarRange::operator==(const scalarRange& range) const
160 && value_ == range.value_
161 && value2_ == range.value2_
171 bool Foam::scalarRange::operator!=(const scalarRange& range) const
173 return operator==(range) ? false : true;
177 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
179 Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range)
181 range.type_ = scalarRange::EXACT;
192 is.check("scalarRange token read");
196 toks[nTok].isPunctuation()
197 && toks[nTok].pToken() == token::COMMA
202 // looks like ':VALUE'
205 toks[nTok-1].isPunctuation()
206 && toks[nTok-1].pToken() == token::COLON
209 range.type_ = scalarRange::UPPER;
210 is.read(toks[nTok++]);
211 is.check("scalarRange token read");
214 // a number is now required
215 if (!toks[nTok-1].isNumber())
218 range.type_ = scalarRange::EMPTY;
219 range.value_ = range.value2_ = 0;
220 Info<< "rejected ill-formed or empty range:";
221 for (label i=0; i<nTok; ++i)
223 Info<< " " << toks[i];
229 range.value_ = toks[nTok-1].number();
230 is.read(toks[nTok++]);
231 is.check("scalarRange token read");
233 if (scalarRange::debug)
236 for (label i=0; i<nTok; ++i)
238 Info<< " " << toks[i];
243 // could be 'VALUE:' or 'VALUE:VALUE'
246 toks[nTok-1].isPunctuation()
247 && toks[nTok-1].pToken() == token::COLON
250 if (range.type_ == scalarRange::UPPER)
253 range.type_ = scalarRange::EMPTY;
254 range.value_ = range.value2_ = 0;
255 Info<< "rejected ill-formed range:";
256 for (label i=0; i<nTok; ++i)
258 Info<< " " << toks[i];
264 is.read(toks[nTok++]);
265 is.check("scalarRange token read");
267 if (scalarRange::debug)
270 for (label i=0; i<nTok; ++i)
272 Info<< " " << toks[i];
278 // if there is a number, we have 'VALUE:VALUE' and not simply 'VALUE:'
279 if (toks[nTok-1].isNumber())
281 range.type_ = scalarRange::RANGE;
282 range.value2_ = toks[nTok-1].number();
283 is.read(toks[nTok++]);
284 is.check("scalarRange token read");
288 range.type_ = scalarRange::LOWER;
292 if (scalarRange::debug)
295 for (label i=0; i<nTok; ++i)
297 Info<< " " << toks[i];
303 // some remaining tokens, but they are not the next comma
304 // - this is a problem!
310 !toks[nTok-1].isPunctuation()
311 || toks[nTok-1].pToken() != token::COMMA
316 range.type_ = scalarRange::EMPTY;
317 range.value_ = range.value2_ = 0;
319 Info<< "rejected ill-formed range:";
320 for (label i=0; i<nTok; ++i)
322 Info << " " << toks[i];
331 Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
335 case scalarRange::LOWER:
336 os << range.value_ << " <=> Inf";
339 case scalarRange::UPPER:
340 os << "-Inf <=> " << range.value_;
343 case scalarRange::RANGE:
344 os << range.value_ << " <=> " << range.value2_;
347 case scalarRange::EXACT:
360 // ************************************************************************* //