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

tokenI.H

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 <iostream>
00027 
00028 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00029 
00030 namespace Foam
00031 {
00032 
00033 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00034 
00035 // Clear any allocated storage (word or string)
00036 inline void token::clear()
00037 {
00038     if (type_ == WORD)
00039     {
00040         delete wordTokenPtr_;
00041     }
00042     else if (type_ == STRING)
00043     {
00044         delete stringTokenPtr_;
00045     }
00046     else if (type_ == COMPOUND)
00047     {
00048         if (compoundTokenPtr_->okToDelete())
00049         {
00050             delete compoundTokenPtr_;
00051         }
00052         else
00053         {
00054             compoundTokenPtr_->refCount::operator--();
00055         }
00056     }
00057 
00058     type_ = UNDEFINED;
00059 }
00060 
00061 
00062 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00063 
00064 // Construct null
00065 inline token::token()
00066 :
00067     type_(UNDEFINED),
00068     lineNumber_(0)
00069 {}
00070 
00071 // Construct as copy
00072 inline token::token(const token& t)
00073 :
00074     type_(t.type_),
00075     lineNumber_(t.lineNumber_)
00076 {
00077     switch (type_)
00078     {
00079         case token::UNDEFINED:
00080         break;
00081 
00082         case PUNCTUATION:
00083             punctuationToken_ = t.punctuationToken_;
00084         break;
00085 
00086         case WORD:
00087             wordTokenPtr_ = new word(*t.wordTokenPtr_);
00088         break;
00089 
00090         case STRING:
00091             stringTokenPtr_ = new string(*t.stringTokenPtr_);
00092         break;
00093 
00094         case LABEL:
00095             labelToken_ = t.labelToken_;
00096         break;
00097 
00098         case FLOAT_SCALAR:
00099             floatScalarToken_ = t.floatScalarToken_;
00100         break;
00101 
00102         case DOUBLE_SCALAR:
00103             doubleScalarToken_ = t.doubleScalarToken_;
00104         break;
00105 
00106         case COMPOUND:
00107             compoundTokenPtr_ = t.compoundTokenPtr_;
00108             compoundTokenPtr_->refCount::operator++();
00109         break;
00110 
00111         case token::ERROR:
00112         break;
00113     }
00114 }
00115 
00116 // Construct punctuation character token
00117 inline token::token(punctuationToken p, label lineNumber)
00118 :
00119     type_(PUNCTUATION),
00120     punctuationToken_(p),
00121     lineNumber_(lineNumber)
00122 {}
00123 
00124 // Construct word token
00125 inline token::token(const word& w, label lineNumber)
00126 :
00127     type_(WORD),
00128     wordTokenPtr_(new word(w)),
00129     lineNumber_(lineNumber)
00130 {}
00131 
00132 // Construct string token
00133 inline token::token(const string& s, label lineNumber)
00134 :
00135     type_(STRING),
00136     stringTokenPtr_(new string(s)),
00137     lineNumber_(lineNumber)
00138 {}
00139 
00140 // Construct label token
00141 inline token::token(const label l, label lineNumber)
00142 :
00143     type_(LABEL),
00144     labelToken_(l),
00145     lineNumber_(lineNumber)
00146 {}
00147 
00148 // Construct floatScalar token
00149 inline token::token(const floatScalar s, label lineNumber)
00150 :
00151     type_(FLOAT_SCALAR),
00152     floatScalarToken_(s),
00153     lineNumber_(lineNumber)
00154 {}
00155 
00156 // Construct doubleScalar token
00157 inline token::token(const doubleScalar s, label lineNumber)
00158 :
00159     type_(DOUBLE_SCALAR),
00160     doubleScalarToken_(s),
00161     lineNumber_(lineNumber)
00162 {}
00163 
00164 
00165 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
00166 
00167 // Delete token clearing the storage used by word or string
00168 inline token::~token()
00169 {
00170     clear();
00171 }
00172 
00173 
00174 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00175 
00176 inline token::tokenType token::type() const
00177 {
00178     return type_;
00179 }
00180 
00181 inline bool token::good() const
00182 {
00183     return (type_ != ERROR && type_ != UNDEFINED);
00184 }
00185 
00186 inline bool token::undefined() const
00187 {
00188     return (type_ == UNDEFINED);
00189 }
00190 
00191 inline bool token::error() const
00192 {
00193     return (type_ == ERROR);
00194 }
00195 
00196 inline bool token::isPunctuation() const
00197 {
00198     return (type_ == PUNCTUATION);
00199 }
00200 
00201 inline token::punctuationToken token::pToken() const
00202 {
00203     if (type_ == PUNCTUATION)
00204     {
00205         return punctuationToken_;
00206     }
00207     else
00208     {
00209         parseError("punctuation character");
00210         return NULL_TOKEN;
00211     }
00212 }
00213 
00214 inline bool token::isWord() const
00215 {
00216     return (type_ == WORD);
00217 }
00218 
00219 inline const word& token::wordToken() const
00220 {
00221     if (type_ == WORD)
00222     {
00223         return *wordTokenPtr_;
00224     }
00225     else
00226     {
00227         parseError("word");
00228         return word::null;
00229     }
00230 }
00231 
00232 inline bool token::isString() const
00233 {
00234     return (type_ == STRING);
00235 }
00236 
00237 inline const string& token::stringToken() const
00238 {
00239     if (type_ == STRING)
00240     {
00241         return *stringTokenPtr_;
00242     }
00243     else
00244     {
00245         parseError("string");
00246         return string::null;
00247     }
00248 }
00249 
00250 inline bool token::isLabel() const
00251 {
00252     return (type_ == LABEL);
00253 }
00254 
00255 inline label token::labelToken() const
00256 {
00257     if (type_ == LABEL)
00258     {
00259         return labelToken_;
00260     }
00261     else
00262     {
00263         parseError("label");
00264         return 0;
00265     }
00266 }
00267 
00268 inline bool token::isFloatScalar() const
00269 {
00270     return (type_ == FLOAT_SCALAR);
00271 }
00272 
00273 inline floatScalar token::floatScalarToken() const
00274 {
00275     if (type_ == FLOAT_SCALAR)
00276     {
00277         return floatScalarToken_;
00278     }
00279     else
00280     {
00281         parseError("floatScalar");
00282         return 0.0;
00283     }
00284 }
00285 
00286 
00287 inline bool token::isDoubleScalar() const
00288 {
00289     return (type_ == DOUBLE_SCALAR);
00290 }
00291 
00292 inline doubleScalar token::doubleScalarToken() const
00293 {
00294     if (type_ == DOUBLE_SCALAR)
00295     {
00296         return doubleScalarToken_;
00297     }
00298     else
00299     {
00300         parseError("doubleScalar");
00301         return 0.0;
00302     }
00303 }
00304 
00305 
00306 inline bool token::isScalar() const
00307 {
00308     return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
00309 }
00310 
00311 inline scalar token::scalarToken() const
00312 {
00313     if (type_ == FLOAT_SCALAR)
00314     {
00315         return floatScalarToken_;
00316     }
00317     else if (type_ == DOUBLE_SCALAR)
00318     {
00319         return doubleScalarToken_;
00320     }
00321     else
00322     {
00323         parseError("scalar");
00324         return 0.0;
00325     }
00326 }
00327 
00328 inline bool token::isNumber() const
00329 {
00330     return (type_ == LABEL || isScalar());
00331 }
00332 
00333 inline scalar token::number() const
00334 {
00335     if (type_ == LABEL)
00336     {
00337         return labelToken_;
00338     }
00339     else if (isScalar())
00340     {
00341         return scalarToken();
00342     }
00343     else
00344     {
00345         parseError("number (label or scalar)");
00346         return 0.0;
00347     }
00348 }
00349 
00350 inline bool token::isCompound() const
00351 {
00352     return (type_ == COMPOUND);
00353 }
00354 
00355 inline const token::compound& token::compoundToken() const
00356 {
00357     if (type_ == COMPOUND)
00358     {
00359         return *compoundTokenPtr_;
00360     }
00361     else
00362     {
00363         parseError("compound");
00364         return *compoundTokenPtr_;
00365     }
00366 }
00367 
00368 
00369 inline label token::lineNumber() const
00370 {
00371     return lineNumber_;
00372 }
00373 
00374 inline label& token::lineNumber()
00375 {
00376     return lineNumber_;
00377 }
00378 
00379 
00380 inline void token::setBad()
00381 {
00382     clear();
00383     type_ = ERROR;
00384 }
00385 
00386 
00387 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00388 
00389 inline void token::operator=(const token& t)
00390 {
00391     clear();
00392     type_ = t.type_;
00393 
00394     switch (type_)
00395     {
00396         case token::UNDEFINED:
00397         break;
00398 
00399         case PUNCTUATION:
00400             punctuationToken_ = t.punctuationToken_;
00401         break;
00402 
00403         case WORD:
00404             wordTokenPtr_ = new word(*t.wordTokenPtr_);
00405         break;
00406 
00407         case STRING:
00408             stringTokenPtr_ = new string(*t.stringTokenPtr_);
00409         break;
00410 
00411         case LABEL:
00412             labelToken_ = t.labelToken_;
00413         break;
00414 
00415         case FLOAT_SCALAR:
00416             floatScalarToken_ = t.floatScalarToken_;
00417         break;
00418 
00419         case DOUBLE_SCALAR:
00420             doubleScalarToken_ = t.doubleScalarToken_;
00421         break;
00422 
00423         case COMPOUND:
00424             compoundTokenPtr_ = t.compoundTokenPtr_;
00425             compoundTokenPtr_->refCount::operator++();
00426         break;
00427 
00428         case token::ERROR:
00429         break;
00430     }
00431 
00432     lineNumber_ = t.lineNumber_;
00433 }
00434 
00435 inline void token::operator=(const punctuationToken p)
00436 {
00437     clear();
00438     type_ = PUNCTUATION;
00439     punctuationToken_ = p;
00440 }
00441 
00442 inline void token::operator=(word* wPtr)
00443 {
00444     clear();
00445     type_ = WORD;
00446     wordTokenPtr_ = wPtr;
00447 }
00448 
00449 inline void token::operator=(const word& w)
00450 {
00451     operator=(new word(w));
00452 }
00453 
00454 inline void token::operator=(string* sPtr)
00455 {
00456     clear();
00457     type_ = STRING;
00458     stringTokenPtr_ = sPtr;
00459 }
00460 
00461 inline void token::operator=(const string& s)
00462 {
00463     operator=(new string(s));
00464 }
00465 
00466 inline void token::operator=(const label l)
00467 {
00468     clear();
00469     type_ = LABEL;
00470     labelToken_ = l;
00471 }
00472 
00473 inline void token::operator=(const floatScalar s)
00474 {
00475     clear();
00476     type_ = FLOAT_SCALAR;
00477     floatScalarToken_ = s;
00478 }
00479 
00480 inline void token::operator=(const doubleScalar s)
00481 {
00482     clear();
00483     type_ = DOUBLE_SCALAR;
00484     doubleScalarToken_ = s;
00485 }
00486 
00487 inline void token::operator=(token::compound* cPtr)
00488 {
00489     clear();
00490     type_ = COMPOUND;
00491     compoundTokenPtr_ = cPtr;
00492 }
00493 
00494 
00495 inline bool token::operator==(const token& t) const
00496 {
00497     if (type_ != t.type_)
00498     {
00499         return false;
00500     }
00501 
00502     switch (type_)
00503     {
00504         case token::UNDEFINED:
00505             return true;
00506 
00507         case PUNCTUATION:
00508             return punctuationToken_ == t.punctuationToken_;
00509 
00510         case WORD:
00511             return *wordTokenPtr_ == *t.wordTokenPtr_;
00512 
00513         case STRING:
00514             return *stringTokenPtr_ == *t.stringTokenPtr_;
00515 
00516         case LABEL:
00517             return labelToken_ == t.labelToken_;
00518 
00519         case FLOAT_SCALAR:
00520             return equal(floatScalarToken_, t.floatScalarToken_);
00521 
00522         case DOUBLE_SCALAR:
00523             return equal(doubleScalarToken_, t.doubleScalarToken_);
00524 
00525         case COMPOUND:
00526             return compoundTokenPtr_ == t.compoundTokenPtr_;
00527 
00528         case token::ERROR:
00529             return true;
00530     }
00531 
00532     return false;
00533 }
00534 
00535 inline bool token::operator==(const punctuationToken p) const
00536 {
00537     return (type_ == PUNCTUATION && punctuationToken_ == p);
00538 }
00539 
00540 inline bool token::operator==(const word& w) const
00541 {
00542     return (type_ == WORD && wordToken() == w);
00543 }
00544 
00545 inline bool token::operator==(const string& s) const
00546 {
00547     return (type_ == STRING && stringToken() == s);
00548 }
00549 
00550 inline bool token::operator==(const label l) const
00551 {
00552     return (type_ == LABEL && labelToken_ == l);
00553 }
00554 
00555 inline bool token::operator==(const floatScalar s) const
00556 {
00557     return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
00558 }
00559 
00560 inline bool token::operator==(const doubleScalar s) const
00561 {
00562     return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
00563 }
00564 
00565 inline bool token::operator!=(const token& t) const
00566 {
00567     return !operator==(t);
00568 }
00569 
00570 inline bool token::operator!=(const punctuationToken p) const
00571 {
00572     return !operator==(p);
00573 }
00574 
00575 inline bool token::operator!=(const word& w) const
00576 {
00577     return !operator==(w);
00578 }
00579 
00580 inline bool token::operator!=(const string& s) const
00581 {
00582     return !operator==(s);
00583 }
00584 
00585 inline bool token::operator!=(const floatScalar s) const
00586 {
00587     return !operator==(s);
00588 }
00589 
00590 inline bool token::operator!=(const doubleScalar s) const
00591 {
00592     return !operator==(s);
00593 }
00594 
00595 inline bool token::operator!=(const label l) const
00596 {
00597     return !operator==(l);
00598 }
00599 
00600 
00601 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00602 
00603 } // End namespace Foam
00604 
00605 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines