1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2007-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "scalarRange.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 int Foam::scalarRange::debug(::Foam::debug::debugSwitch("scalarRange", 0));
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
36 Foam::scalarRange::scalarRange()
44 Foam::scalarRange::scalarRange(const scalar lower, const scalar upper)
50 // mark invalid range as empty
59 Foam::scalarRange::scalarRange(Istream& is)
67 if (scalarRange::debug)
69 Info<<"constructed scalarRange: " << *this << endl;
74 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76 bool Foam::scalarRange::empty() const
78 return type_ == EMPTY;
82 bool Foam::scalarRange::valid() const
84 return type_ != EMPTY;
88 bool Foam::scalarRange::isExact() const
90 return type_ == EXACT;
94 Foam::scalar Foam::scalarRange::value() const
100 Foam::scalar Foam::scalarRange::lower() const
112 Foam::scalar Foam::scalarRange::upper() const
131 bool Foam::scalarRange::selected(const scalar value) const
136 return value >= value_;
139 return value <= value_;
142 return value >= value_ && value <= value2_;
145 return value == value_;
154 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
156 bool Foam::scalarRange::operator==(const scalarRange& range) const
161 && value_ == range.value_
162 && value2_ == range.value2_
167 bool Foam::scalarRange::operator!=(const scalarRange& range) const
169 return !(operator==(range));
173 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
175 Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range)
177 range.type_ = scalarRange::EXACT;
188 is.check("scalarRange token read");
192 toks[nTok].isPunctuation()
193 && toks[nTok].pToken() == token::COMMA
198 // looks like ':VALUE'
201 toks[nTok-1].isPunctuation()
202 && toks[nTok-1].pToken() == token::COLON
205 range.type_ = scalarRange::UPPER;
206 is.read(toks[nTok++]);
207 is.check("scalarRange token read");
210 // a number is now required
211 if (!toks[nTok-1].isNumber())
214 range.type_ = scalarRange::EMPTY;
215 range.value_ = range.value2_ = 0;
216 Info<< "rejected ill-formed or empty range:";
217 for (label i=0; i<nTok; ++i)
219 Info<< " " << toks[i];
225 range.value_ = toks[nTok-1].number();
226 is.read(toks[nTok++]);
227 is.check("scalarRange token read");
229 if (scalarRange::debug)
232 for (label i=0; i<nTok; ++i)
234 Info<< " " << toks[i];
239 // could be 'VALUE:' or 'VALUE:VALUE'
242 toks[nTok-1].isPunctuation()
243 && toks[nTok-1].pToken() == token::COLON
246 if (range.type_ == scalarRange::UPPER)
249 range.type_ = scalarRange::EMPTY;
250 range.value_ = range.value2_ = 0;
251 Info<< "rejected ill-formed range:";
252 for (label i=0; i<nTok; ++i)
254 Info<< " " << toks[i];
260 is.read(toks[nTok++]);
261 is.check("scalarRange token read");
263 if (scalarRange::debug)
266 for (label i=0; i<nTok; ++i)
268 Info<< " " << toks[i];
274 // if there is a number, we have 'VALUE:VALUE' and not simply 'VALUE:'
275 if (toks[nTok-1].isNumber())
277 range.type_ = scalarRange::RANGE;
278 range.value2_ = toks[nTok-1].number();
279 is.read(toks[nTok++]);
280 is.check("scalarRange token read");
284 range.type_ = scalarRange::LOWER;
288 if (scalarRange::debug)
291 for (label i=0; i<nTok; ++i)
293 Info<< " " << toks[i];
299 // some remaining tokens, but they are not the next comma
300 // - this is a problem!
306 !toks[nTok-1].isPunctuation()
307 || toks[nTok-1].pToken() != token::COMMA
312 range.type_ = scalarRange::EMPTY;
313 range.value_ = range.value2_ = 0;
315 Info<< "rejected ill-formed range:";
316 for (label i=0; i<nTok; ++i)
318 Info<< " " << toks[i];
327 Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
331 case scalarRange::LOWER:
332 os << range.value_ << " <=> Inf";
335 case scalarRange::UPPER:
336 os << "-Inf <=> " << range.value_;
339 case scalarRange::RANGE:
340 os << range.value_ << " <=> " << range.value2_;
343 case scalarRange::EXACT:
356 // ************************************************************************* //