FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "scalarRange.H"
00027 #include <OpenFOAM/token.H>
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 int Foam::scalarRange::debug(::Foam::debug::debugSwitch("scalarRange", 0));
00032 
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 Foam::scalarRange::scalarRange()
00037 :
00038     type_(EMPTY),
00039     value_(0),
00040     value2_(0)
00041 {}
00042 
00043 
00044 Foam::scalarRange::scalarRange(const scalar& lower, const scalar& upper)
00045 :
00046     type_(RANGE),
00047     value_(lower),
00048     value2_(upper)
00049 {
00050     // mark invalid range as empty
00051     if (lower > upper)
00052     {
00053         type_ = EMPTY;
00054         value_ = value2_ = 0;
00055     }
00056 }
00057 
00058 
00059 Foam::scalarRange::scalarRange(Istream& is)
00060 :
00061     type_(EXACT),
00062     value_(0),
00063     value2_(0)
00064 {
00065     is >> *this;
00066 
00067     if (scalarRange::debug)
00068     {
00069         Info<<"constructed scalarRange: " << *this << endl;
00070     }
00071 }
00072 
00073 
00074 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00075 
00076 bool Foam::scalarRange::isDefined() const
00077 {
00078     return type_ != EMPTY;
00079 }
00080 
00081 
00082 bool Foam::scalarRange::isExact() const
00083 {
00084     return type_ == EXACT;
00085 }
00086 
00087 
00088 Foam::scalar Foam::scalarRange::value() const
00089 {
00090     return value_;
00091 }
00092 
00093 
00094 Foam::scalar Foam::scalarRange::lower() const
00095 {
00096     if (type_ == UPPER)
00097     {
00098         return -Foam::GREAT;
00099     }
00100     else
00101     {
00102         return value_;
00103     }
00104 }
00105 
00106 Foam::scalar Foam::scalarRange::upper() const
00107 {
00108     switch (type_)
00109     {
00110         case LOWER:
00111             return Foam::GREAT;
00112             break;
00113 
00114         case RANGE:
00115             return value2_;
00116             break;
00117 
00118         default:
00119             return value_;
00120             break;
00121     }
00122 }
00123 
00124 
00125 bool Foam::scalarRange::selected(const scalar& value) const
00126 {
00127     switch (type_)
00128     {
00129         case LOWER:
00130             return value >= value_;
00131 
00132         case UPPER:
00133             return value <= value_;
00134 
00135         case RANGE:
00136             return value >= value_ && value <= value2_;
00137 
00138         case EXACT:
00139             return value == value_;
00140 
00141         default:
00142             return false;
00143     }
00144 }
00145 
00146 
00147 
00148 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00149 
00150 bool Foam::scalarRange::operator==(const scalarRange& range) const
00151 {
00152     if
00153     (
00154         type_ == range.type_
00155      && value_ == range.value_
00156      && value2_ == range.value2_
00157     )
00158     {
00159         return true;
00160     }
00161 
00162     return false;
00163 }
00164 
00165 
00166 bool Foam::scalarRange::operator!=(const scalarRange& range) const
00167 {
00168     return operator==(range) ? false : true;
00169 }
00170 
00171 
00172 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00173 
00174 Foam::Istream& Foam::operator>>(Istream& is, scalarRange& range)
00175 {
00176     range.type_ = scalarRange::EXACT;
00177     range.value_ = 0;
00178     range.value2_ = 0;
00179 
00180     List<token> toks(4);
00181     label nTok = 0;
00182 
00183     // skip leading ','
00184     do
00185     {
00186         is.read(toks[nTok]);
00187         is.check("scalarRange token read");
00188     }
00189     while
00190     (
00191         toks[nTok].isPunctuation()
00192      && toks[nTok].pToken() == token::COMMA
00193     );
00194 
00195     ++nTok;
00196 
00197     // looks like ':VALUE'
00198     if
00199     (
00200         toks[nTok-1].isPunctuation()
00201      && toks[nTok-1].pToken() == token::COLON
00202     )
00203     {
00204         range.type_ = scalarRange::UPPER;
00205         is.read(toks[nTok++]);
00206         is.check("scalarRange token read");
00207     }
00208 
00209     // a number is now required
00210     if (!toks[nTok-1].isNumber())
00211     {
00212         is.setBad();
00213         range.type_ = scalarRange::EMPTY;
00214         range.value_ = range.value2_ = 0;
00215         Info<< "rejected ill-formed or empty range:";
00216         for (label i=0; i<nTok; ++i)
00217         {
00218             Info<< " " << toks[i];
00219         }
00220         Info<< endl;
00221         return is;
00222     }
00223 
00224     range.value_ = toks[nTok-1].number();
00225     is.read(toks[nTok++]);
00226     is.check("scalarRange token read");
00227 
00228     if (scalarRange::debug)
00229     {
00230         Info<<"tokens:";
00231         for (label i=0; i<nTok; ++i)
00232         {
00233             Info<< " " << toks[i];
00234         }
00235         Info<< endl;
00236     }
00237 
00238     // could be 'VALUE:' or 'VALUE:VALUE'
00239     if
00240     (
00241         toks[nTok-1].isPunctuation()
00242      && toks[nTok-1].pToken() == token::COLON
00243     )
00244     {
00245         if (range.type_ == scalarRange::UPPER)
00246         {
00247             is.setBad();
00248             range.type_ = scalarRange::EMPTY;
00249             range.value_ = range.value2_ = 0;
00250             Info<< "rejected ill-formed range:";
00251             for (label i=0; i<nTok; ++i)
00252             {
00253                 Info<< " " << toks[i];
00254             }
00255             Info<< endl;
00256             return is;
00257         }
00258 
00259         is.read(toks[nTok++]);
00260         is.check("scalarRange token read");
00261 
00262         if (scalarRange::debug)
00263         {
00264             Info<<"tokens:";
00265             for (label i=0; i<nTok; ++i)
00266             {
00267                 Info<< " " << toks[i];
00268             }
00269             Info<< endl;
00270         }
00271 
00272 
00273         // if there is a number, we have 'VALUE:VALUE' and not simply 'VALUE:'
00274         if (toks[nTok-1].isNumber())
00275         {
00276             range.type_ = scalarRange::RANGE;
00277             range.value2_ = toks[nTok-1].number();
00278             is.read(toks[nTok++]);
00279             is.check("scalarRange token read");
00280         }
00281         else
00282         {
00283             range.type_ = scalarRange::LOWER;
00284         }
00285     }
00286 
00287     if (scalarRange::debug)
00288     {
00289         Info<<"tokens:";
00290         for (label i=0; i<nTok; ++i)
00291         {
00292             Info<< " " << toks[i];
00293         }
00294         Info<< endl;
00295     }
00296 
00297 
00298     // some remaining tokens, but they are not the next comma
00299     // - this is a problem!
00300     if
00301     (
00302         toks[nTok-1].good()
00303      &&
00304         (
00305             !toks[nTok-1].isPunctuation()
00306          || toks[nTok-1].pToken() != token::COMMA
00307         )
00308     )
00309     {
00310         is.setBad();
00311         range.type_ = scalarRange::EMPTY;
00312         range.value_ = range.value2_ = 0;
00313 
00314         Info<< "rejected ill-formed range:";
00315         for (label i=0; i<nTok; ++i)
00316         {
00317             Info << " " << toks[i];
00318         }
00319         Info<< endl;
00320     }
00321 
00322     return is;
00323 }
00324 
00325 
00326 Foam::Ostream& Foam::operator<<(Ostream& os, const scalarRange& range)
00327 {
00328     switch (range.type_)
00329     {
00330         case scalarRange::LOWER:
00331             os << range.value_ << " <=> Inf";
00332             break;
00333 
00334         case scalarRange::UPPER:
00335             os << "-Inf <=> " << range.value_;
00336             break;
00337 
00338         case scalarRange::RANGE:
00339             os << range.value_ << " <=> " << range.value2_;
00340             break;
00341 
00342         case scalarRange::EXACT:
00343             os << range.value_;
00344             break;
00345 
00346         default:
00347             os << "empty";
00348             break;
00349     }
00350 
00351     return os;
00352 }
00353 
00354 
00355 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines