OpenFOAM logo
Open Source CFD Toolkit

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