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

token.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-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 Class
00026     Foam::token
00027 
00028 Description
00029     A token holds items read from Istream.
00030 
00031 SourceFiles
00032     tokenI.H
00033     token.C
00034     tokenIO.C
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef token_H
00039 #define token_H
00040 
00041 #include "label.H"
00042 #include "uLabel.H"
00043 #include "scalar.H"
00044 #include "word.H"
00045 #include "InfoProxy.H"
00046 #include "refCount.H"
00047 #include "typeInfo.H"
00048 
00049 #define NoHashTableC
00050 #include "runTimeSelectionTables.H"
00051 
00052 #include <iostream>
00053 
00054 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00055 
00056 namespace Foam
00057 {
00058 
00059 // Forward declaration of friend functions and operators
00060 
00061 class token;
00062 Istream& operator>>(Istream&, token&);
00063 Ostream& operator<<(Ostream&, const token&);
00064 
00065 /*---------------------------------------------------------------------------*\
00066                            Class token Declaration
00067 \*---------------------------------------------------------------------------*/
00068 
00069 class token
00070 {
00071 
00072 public:
00073 
00074     //- Enumeration defining the types of token
00075     enum tokenType
00076     {
00077         UNDEFINED,
00078 
00079         PUNCTUATION,
00080         WORD,
00081         STRING,
00082         LABEL,
00083         FLOAT_SCALAR,
00084         DOUBLE_SCALAR,
00085         COMPOUND,
00086 
00087         ERROR
00088     };
00089 
00090 
00091     //- Standard punctuation tokens
00092     enum punctuationToken
00093     {
00094         NULL_TOKEN     = '\0',
00095         SPACE          = ' ',
00096         TAB            = '\t',
00097         NL             = '\n',
00098 
00099         END_STATEMENT  = ';',
00100         BEGIN_LIST     = '(',
00101         END_LIST       = ')',
00102         BEGIN_SQR      = '[',
00103         END_SQR        = ']',
00104         BEGIN_BLOCK    = '{',
00105         END_BLOCK      = '}',
00106         COLON          = ':',
00107         COMMA          = ',',
00108 
00109         BEGIN_STRING   = '"',
00110         END_STRING     = BEGIN_STRING,
00111 
00112         ASSIGN         = '=',
00113         ADD            = '+',
00114         SUBTRACT       = '-',
00115         MULTIPLY       = '*',
00116         DIVIDE         = '/'
00117     };
00118 
00119 
00120     //- Abstract base class for complex tokens
00121     class compound
00122     :
00123         public refCount
00124     {
00125         // Private data
00126 
00127             bool empty_;
00128 
00129 
00130         // Private Member Functions
00131 
00132             //- Disallow default bitwise copy construct
00133             compound(const compound&);
00134 
00135             //- Disallow default bitwise assignment
00136             void operator=(const compound&);
00137 
00138 
00139     public:
00140 
00141         //- Runtime type information
00142         TypeName("compound");
00143 
00144 
00145         //- Declare run-time constructor selection table
00146         declareRunTimeSelectionTable
00147         (
00148             autoPtr,
00149             compound,
00150             Istream,
00151             (Istream& is),
00152             (is)
00153         );
00154 
00155 
00156         // Constructors
00157 
00158             //- Construct null
00159             compound()
00160             :
00161                 empty_(false)
00162             {}
00163 
00164 
00165         // Selectors
00166 
00167             //- Select null constructed
00168             static autoPtr<compound> New(const word& type, Istream&);
00169 
00170 
00171         // Destructor
00172 
00173             virtual ~compound();
00174 
00175 
00176         // Member Functions
00177 
00178             // Access
00179 
00180                 //- Return true if name is a compound type
00181                 static bool isCompound(const word& name);
00182 
00183                 bool empty() const
00184                 {
00185                     return empty_;
00186                 }
00187 
00188                 bool& empty()
00189                 {
00190                     return empty_;
00191                 }
00192 
00193                 virtual label size() const = 0;
00194 
00195 
00196             // Check
00197 
00198             // Edit
00199 
00200             // Write
00201 
00202                 virtual void write(Ostream&) const = 0;
00203 
00204 
00205         // IOstream Operators
00206 
00207             friend Ostream& operator<<(Ostream&, const compound&);
00208     };
00209 
00210 
00211     //- A templated class for holding compound tokens
00212     template<class T>
00213     class Compound
00214     :
00215         public token::compound,
00216         public T
00217     {
00218     public:
00219 
00220         //- Runtime type information
00221         TypeName("Compound<T>");
00222 
00223         Compound(Istream& is)
00224         :
00225             T(is)
00226         {}
00227 
00228         label size() const
00229         {
00230             return T::size();
00231         }
00232 
00233         void write(Ostream& os) const
00234         {
00235             operator<<(os, static_cast<const T&>(*this));
00236         }
00237     };
00238 
00239 
00240     //- Static undefined token
00241     static token undefinedToken;
00242 
00243 
00244 private:
00245 
00246     // Private data
00247 
00248         //- The token type
00249         tokenType type_;
00250 
00251         //- Anonymous Union of token types
00252         union
00253         {
00254             punctuationToken punctuationToken_;
00255             word* wordTokenPtr_;
00256             string* stringTokenPtr_;
00257             label labelToken_;
00258             floatScalar floatScalarToken_;
00259             doubleScalar doubleScalarToken_;
00260             mutable compound* compoundTokenPtr_;
00261         };
00262 
00263         //- Line number in the file this token was read from
00264         label lineNumber_;
00265 
00266 
00267     // Private member functions
00268 
00269         //- Clear any allocated storage (word or string)
00270         inline void clear();
00271 
00272         // Parse error, expected 'expected', found ...
00273         void parseError(const char* expected) const;
00274 
00275 
00276 public:
00277 
00278     // Static data members
00279 
00280         static const char* const typeName;
00281 
00282 
00283     // Constructors
00284 
00285         //- Construct null
00286         inline token();
00287 
00288         //- Construct as copy
00289         inline token(const token&);
00290 
00291         //- Construct punctuation character token
00292         inline token(punctuationToken, label lineNumber=0);
00293 
00294         //- Construct word token
00295         inline token(const word&, label lineNumber=0);
00296 
00297         //- Construct string token
00298         inline token(const string&, label lineNumber=0);
00299 
00300         //- Construct label token
00301         inline token(const label, label lineNumber=0);
00302 
00303         //- Construct floatScalar token
00304         inline token(const floatScalar, label lineNumber=0);
00305 
00306         //- Construct doubleScalar token
00307         inline token(const doubleScalar, label lineNumber=0);
00308 
00309         //- Construct from Istream
00310         token(Istream&);
00311 
00312 
00313     // Destructor
00314 
00315         inline ~token();
00316 
00317 
00318     // Member functions
00319 
00320         // Access
00321 
00322             inline tokenType type() const;
00323 
00324             inline bool good() const;
00325             inline bool undefined() const;
00326             inline bool error() const;
00327 
00328             inline bool isPunctuation() const;
00329             inline punctuationToken pToken() const;
00330 
00331             inline bool isWord() const;
00332             inline const word& wordToken() const;
00333 
00334             inline bool isString() const;
00335             inline const string& stringToken() const;
00336 
00337             inline bool isLabel() const;
00338             inline label labelToken() const;
00339 
00340             inline bool isFloatScalar() const;
00341             inline floatScalar floatScalarToken() const;
00342 
00343             inline bool isDoubleScalar() const;
00344             inline doubleScalar doubleScalarToken() const;
00345 
00346             inline bool isScalar() const;
00347             inline scalar scalarToken() const;
00348 
00349             inline bool isNumber() const;
00350             inline scalar number() const;
00351 
00352             inline bool isCompound() const;
00353             inline const compound& compoundToken() const;
00354             compound& transferCompoundToken();
00355 
00356             inline label lineNumber() const;
00357             inline label& lineNumber();
00358 
00359 
00360         // Edit
00361 
00362             //- Set bad
00363             inline void setBad();
00364 
00365 
00366         // Info
00367 
00368             //- Return info proxy.
00369             //  Used to print token information to a stream
00370             InfoProxy<token> info() const
00371             {
00372                 return *this;
00373             }
00374 
00375 
00376     // Member operators
00377 
00378         // Assignment
00379 
00380             inline void operator=(const token&);
00381 
00382             inline void operator=(const punctuationToken);
00383 
00384             inline void operator=(word*);
00385             inline void operator=(const word&);
00386 
00387             inline void operator=(string*);
00388             inline void operator=(const string&);
00389 
00390             inline void operator=(const label);
00391             inline void operator=(const floatScalar);
00392             inline void operator=(const doubleScalar);
00393 
00394             inline void operator=(compound*);
00395 
00396 
00397         // Equality
00398 
00399             inline bool operator==(const token&) const;
00400             inline bool operator==(const punctuationToken) const;
00401             inline bool operator==(const word&) const;
00402             inline bool operator==(const string&) const;
00403             inline bool operator==(const label) const;
00404             inline bool operator==(const floatScalar) const;
00405             inline bool operator==(const doubleScalar) const;
00406 
00407 
00408         // Inequality
00409 
00410             inline bool operator!=(const token&) const;
00411             inline bool operator!=(const punctuationToken) const;
00412             inline bool operator!=(const word&) const;
00413             inline bool operator!=(const string&) const;
00414             inline bool operator!=(const label) const;
00415             inline bool operator!=(const floatScalar) const;
00416             inline bool operator!=(const doubleScalar) const;
00417 
00418 
00419     // IOstream operators
00420 
00421         friend Istream& operator>>(Istream&, token&);
00422         friend Ostream& operator<<(Ostream&, const token&);
00423 
00424         friend Ostream& operator<<(Ostream&, const punctuationToken&);
00425         friend ostream& operator<<(ostream&, const punctuationToken&);
00426 
00427         friend ostream& operator<<(ostream&, const InfoProxy<token>&);
00428 };
00429 
00430 
00431 Ostream& operator<<(Ostream&, const token::punctuationToken&);
00432 ostream& operator<<(ostream&, const token::punctuationToken&);
00433 ostream& operator<<(ostream&, const InfoProxy<token>&);
00434 Ostream& operator<<(Ostream&, const token::compound&);
00435 
00436 
00437 #define defineCompoundTypeName(Type, Name)                                    \
00438     typedef token::Compound<Type > tokenCompound##Name##_;                    \
00439     defineTemplateTypeNameAndDebugWithName(tokenCompound##Name##_, #Type, 0);
00440 
00441 #define addCompoundToRunTimeSelectionTable(Type, Name)                        \
00442     token::compound::addIstreamConstructorToTable<token::Compound<Type > >    \
00443         add##Name##IstreamConstructorToTable_;
00444 
00445 
00446 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00447 
00448 } // End namespace Foam
00449 
00450 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00451 
00452 #include "tokenI.H"
00453 #include "Istream.H"
00454 
00455 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00456 
00457 #endif
00458 
00459 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd