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

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