OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

scalarRange.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "scalarRange.H"
00028 #include "token.H"
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 int Foam::scalarRange::debug(::Foam::debug::debugSwitch("scalarRange", 0));
00033 
00034 
00035 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00036 
00037 Foam::scalarRange::scalarRange()
00038 :
00039     type_(EMPTY),
00040     value_(0),
00041     value2_(0)
00042 {}
00043 
00044 
00045 Foam::scalarRange::scalarRange(const scalar& lower, const scalar& upper)
00046 :
00047     type_(RANGE),
00048     value_(lower),
00049     value2_(upper)
00050 {
00051     // mark invalid range as empty
00052     if (lower > upper)
00053     {
00054         type_ = EMPTY;
00055         value_ = value2_ = 0;
00056     }
00057 }
00058 
00059 
00060 Foam::scalarRange::scalarRange(Istream& is)
00061 :
00062     type_(EXACT),
00063     value_(0),
00064     value2_(0)
00065 {
00066     is >> *this;
00067 
00068     if (scalarRange::debug)
00069     {
00070         Info<<"constructed scalarRange: " << *this << endl;
00071     }
00072 }
00073 
00074 
00075 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00076 
00077 bool Foam::scalarRange::isDefined() const
00078 {
00079     return type_ != EMPTY;
00080 }
00081 
00082 
00083 bool Foam::scalarRange::isExact() const
00084 {
00085     return type_ == EXACT;
00086 }
00087 
00088 
00089 Foam::scalar Foam::scalarRange::value() const
00090 {
00091     return value_;
00092 }
00093 
00094 
00095 Foam::scalar Foam::scalarRange::lower() const
00096 {
00097     if (type_ == UPPER)
00098     {
00099         return -Foam::GREAT;
00100     }
00101     else
00102     {
00103         return value_;
00104     }
00105 }
00106 
00107 Foam::scalar Foam::scalarRange::upper() const
00108 {
00109     switch (type_)
00110     {
00111         case LOWER:
00112             return Foam::GREAT;
00113             break;
00114 
00115         case RANGE:
00116             return value2_;
00117             break;
00118 
00119         default:
00120             return value_;
00121             break;
00122     }
00123 }
00124 
00125 
00126 bool Foam::scalarRange::selected(const scalar& value) const
00127 {
00128     switch (type_)
00129     {
00130         case LOWER:
00131             return value >= value_;
00132 
00133         case UPPER:
00134             return value <= value_;
00135 
00136         case RANGE:
00137             return value >= value_ && value <= value2_;
00138 
00139         case EXACT:
00140             return value == value_;
00141 
00142         default:
00143             return false;
00144     }
00145 }
00146 
00147 
00148 
00149 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00150 
00151 bool Foam::scalarRange::operator==(const scalarRange& range) const
00152 {
00153     if
00154     (
00155         type_ == range.type_
00156      && value_ == range.value_
00157      && value2_ == range.value2_
00158     )
00159     {
00160         return true;
00161     }
00162 
00163     return false;
00164 }
00165 
00166 
00167 bool Foam::scalarRange::operator!=(const scalarRange& range) const
00168 {
00169     return operator==(range) ? false : true;
00170 }
00171 
00172 
00173 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00174 
00175 Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range)
00176 {
00177     range.type_ = scalarRange::EXACT;
00178     range.value_ = 0;
00179     range.value2_ = 0;
00180 
00181     List<token> toks(4);
00182     label nTok = 0;
00183 
00184     // skip leading ','
00185     do
00186     {
00187         is.read(toks[nTok]);
00188         is.check("scalarRange token read");
00189     }
00190     while
00191     (
00192         toks[nTok].isPunctuation()
00193      && toks[nTok].pToken() == token::COMMA
00194     );
00195 
00196     ++nTok;
00197 
00198     // looks like ':VALUE'
00199     if
00200     (
00201         toks[nTok-1].isPunctuation()
00202      && toks[nTok-1].pToken() == token::COLON
00203     )
00204     {
00205         range.type_ = scalarRange::UPPER;
00206         is.read(toks[nTok++]);
00207         is.check("scalarRange token read");
00208     }
00209 
00210     // a number is now required
00211     if (!toks[nTok-1].isNumber())
00212     {
00213         is.setBad();
00214         range.type_ = scalarRange::EMPTY;
00215         range.value_ = range.value2_ = 0;
00216         Info<< "rejected ill-formed or empty range:";
00217         for (label i=0; i<nTok; ++i)
00218         {
00219             Info<< " " << toks[i];
00220         }
00221         Info<< endl;
00222         return is;
00223     }
00224 
00225     range.value_ = toks[nTok-1].number();
00226     is.read(toks[nTok++]);
00227     is.check("scalarRange token read");
00228 
00229     if (scalarRange::debug)
00230     {
00231         Info<<"tokens:";
00232         for (label i=0; i<nTok; ++i)
00233         {
00234             Info<< " " << toks[i];
00235         }
00236         Info<< endl;
00237     }
00238 
00239     // could be 'VALUE:' or 'VALUE:VALUE'
00240     if
00241     (
00242         toks[nTok-1].isPunctuation()
00243      && toks[nTok-1].pToken() == token::COLON
00244     )
00245     {
00246         if (range.type_ == scalarRange::UPPER)
00247         {
00248             is.setBad();
00249             range.type_ = scalarRange::EMPTY;
00250             range.value_ = range.value2_ = 0;
00251             Info<< "rejected ill-formed range:";
00252             for (label i=0; i<nTok; ++i)
00253             {
00254                 Info<< " " << toks[i];
00255             }
00256             Info<< endl;
00257             return is;
00258         }
00259 
00260         is.read(toks[nTok++]);
00261         is.check("scalarRange token read");
00262 
00263         if (scalarRange::debug)
00264         {
00265             Info<<"tokens:";
00266             for (label i=0; i<nTok; ++i)
00267             {
00268                 Info<< " " << toks[i];
00269             }
00270             Info<< endl;
00271         }
00272 
00273 
00274         // if there is a number, we have 'VALUE:VALUE' and not simply 'VALUE:'
00275         if (toks[nTok-1].isNumber())
00276         {
00277             range.type_ = scalarRange::RANGE;
00278             range.value2_ = toks[nTok-1].number();
00279             is.read(toks[nTok++]);
00280             is.check("scalarRange token read");
00281         }
00282         else
00283         {
00284             range.type_ = scalarRange::LOWER;
00285         }
00286     }
00287 
00288     if (scalarRange::debug)
00289     {
00290         Info<<"tokens:";
00291         for (label i=0; i<nTok; ++i)
00292         {
00293             Info<< " " << toks[i];
00294         }
00295         Info<< endl;
00296     }
00297 
00298 
00299     // some remaining tokens, but they are not the next comma
00300     // - this is a problem!
00301     if
00302     (
00303         toks[nTok-1].good()
00304      &&
00305         (
00306             !toks[nTok-1].isPunctuation()
00307          || toks[nTok-1].pToken() != token::COMMA
00308         )
00309     )
00310     {
00311         is.setBad();
00312         range.type_ = scalarRange::EMPTY;
00313         range.value_ = range.value2_ = 0;
00314 
00315         Info<< "rejected ill-formed range:";
00316         for (label i=0; i<nTok; ++i)
00317         {
00318             Info << " " << toks[i];
00319         }
00320         Info<< endl;
00321     }
00322 
00323     return is;
00324 }
00325 
00326 
00327 Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
00328 {
00329     switch (range.type_)
00330     {
00331         case scalarRange::LOWER:
00332             os << range.value_ << " <=> Inf";
00333             break;
00334 
00335         case scalarRange::UPPER:
00336             os << "-Inf <=> " << range.value_;
00337             break;
00338 
00339         case scalarRange::RANGE:
00340             os << range.value_ << " <=> " << range.value2_;
00341             break;
00342 
00343         case scalarRange::EXACT:
00344             os << range.value_;
00345             break;
00346 
00347         default:
00348             os << "empty";
00349             break;
00350     }
00351 
00352     return os;
00353 }
00354 
00355 
00356 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd