initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / IOstreams / token / token.H
blob0880b13d13262ef949a75b7de29932fcb87b6f82
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::token
28 Description
29     A token holds items read from Istream.
31 SourceFiles
32     tokenI.H
33     token.C
34     tokenIO.C
36 \*---------------------------------------------------------------------------*/
38 #ifndef token_H
39 #define token_H
41 #include "label.H"
42 #include "uLabel.H"
43 #include "scalar.H"
44 #include "word.H"
45 #include "InfoProxy.H"
46 #include "refCount.H"
47 #include "typeInfo.H"
49 #define NoHashTableC
50 #include "runTimeSelectionTables.H"
52 #include <iostream>
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 namespace Foam
59 // Forward declaration of friend functions and operators
61 class token;
62 Istream& operator>>(Istream&, token&);
63 Ostream& operator<<(Ostream&, const token&);
65 /*---------------------------------------------------------------------------*\
66                            Class token Declaration
67 \*---------------------------------------------------------------------------*/
69 class token
72 public:
74     //- Enumeration defining the types of token
75     enum tokenType
76     {
77         UNDEFINED,
79         PUNCTUATION,
80         WORD,
81         STRING,
82         LABEL,
83         FLOAT_SCALAR,
84         DOUBLE_SCALAR,
85         COMPOUND,
87         ERROR
88     };
91     //- Standard punctuation tokens
92     enum punctuationToken
93     {
94         NULL_TOKEN     = '\0',
95         SPACE          = ' ',
96         TAB            = '\t',
97         NL             = '\n',
99         END_STATEMENT  = ';',
100         BEGIN_LIST     = '(',
101         END_LIST       = ')',
102         BEGIN_SQR      = '[',
103         END_SQR        = ']',
104         BEGIN_BLOCK    = '{',
105         END_BLOCK      = '}',
106         COLON          = ':',
107         COMMA          = ',',
109         BEGIN_STRING   = '"',
110         END_STRING     = BEGIN_STRING,
112         ASSIGN         = '=',
113         ADD            = '+',
114         SUBTRACT       = '-',
115         MULTIPLY       = '*',
116         DIVIDE         = '/'
117     };
120     //- Abstract base class for complex tokens
121     class compound
122     :
123         public refCount
124     {
125         // Private data
127             bool empty_;
130         // Private Member Functions
132             //- Disallow default bitwise copy construct
133             compound(const compound&);
135             //- Disallow default bitwise assignment
136             void operator=(const compound&);
139     public:
141         //- Runtime type information
142         TypeName("compound");
145         //- Declare run-time constructor selection table
146         declareRunTimeSelectionTable
147         (
148             autoPtr,
149             compound,
150             Istream,
151             (Istream& is),
152             (is)
153         );
156         // Constructors
158             //- Construct null
159             compound()
160             :
161                 empty_(false)
162             {}
165         // Selectors
167             //- Select null constructed
168             static autoPtr<compound> New(const word& type, Istream&);
171         // Destructor
173             virtual ~compound();
176         // Member Functions
178             // Access
180                 //- Return true if name is a compound type
181                 static bool isCompound(const word& name);
183                 bool empty() const
184                 {
185                     return empty_;
186                 }
188                 bool& empty()
189                 {
190                     return empty_;
191                 }
193                 virtual label size() const = 0;
196             // Check
198             // Edit
200             // Write
202                 virtual void write(Ostream&) const = 0;
205         // IOstream Operators
207             friend Ostream& operator<<(Ostream&, const compound&);
208     };
211     //- A templated class for holding compound tokens
212     template<class T>
213     class Compound
214     :
215         public token::compound,
216         public T
217     {
218     public:
220         //- Runtime type information
221         TypeName("Compound<T>");
223         Compound(Istream& is)
224         :
225             T(is)
226         {}
228         label size() const
229         {
230             return T::size();
231         }
233         void write(Ostream& os) const
234         {
235             operator<<(os, static_cast<const T&>(*this));
236         }
237     };
240     //- Static undefined token
241     static token undefinedToken;
244 private:
246     // Private data
248         //- The token type
249         tokenType type_;
251         //- Anonymous Union of token types
252         union
253         {
254             punctuationToken punctuationToken_;
255             word* wordTokenPtr_;
256             string* stringTokenPtr_;
257             label labelToken_;
258             floatScalar floatScalarToken_;
259             doubleScalar doubleScalarToken_;
260             mutable compound* compoundTokenPtr_;
261         };
263         //- Line number in the file this token was read from
264         label lineNumber_;
267     // Private member functions
269         //- Clear any allocated storage (word or string)
270         inline void clear();
272         // Parse error, expected 'expected', found ...
273         void parseError(const char* expected) const;
276 public:
278     // Static data members
280         static const char* const typeName;
283     // Constructors
285         //- Construct null
286         inline token();
288         //- Construct as copy
289         inline token(const token&);
291         //- Construct punctuation character token
292         inline token(punctuationToken, label lineNumber=0);
294         //- Construct word token
295         inline token(const word&, label lineNumber=0);
297         //- Construct string token
298         inline token(const string&, label lineNumber=0);
300         //- Construct label token
301         inline token(const label, label lineNumber=0);
303         //- Construct floatScalar token
304         inline token(const floatScalar, label lineNumber=0);
306         //- Construct doubleScalar token
307         inline token(const doubleScalar, label lineNumber=0);
309         //- Construct from Istream
310         token(Istream&);
313     // Destructor
315         inline ~token();
318     // Member functions
320         // Access
322             inline tokenType type() const;
324             inline bool good() const;
325             inline bool undefined() const;
326             inline bool error() const;
328             inline bool isPunctuation() const;
329             inline punctuationToken pToken() const;
331             inline bool isWord() const;
332             inline const word& wordToken() const;
334             inline bool isString() const;
335             inline const string& stringToken() const;
337             inline bool isLabel() const;
338             inline label labelToken() const;
340             inline bool isFloatScalar() const;
341             inline floatScalar floatScalarToken() const;
343             inline bool isDoubleScalar() const;
344             inline doubleScalar doubleScalarToken() const;
346             inline bool isScalar() const;
347             inline scalar scalarToken() const;
349             inline bool isNumber() const;
350             inline scalar number() const;
352             inline bool isCompound() const;
353             inline const compound& compoundToken() const;
354             compound& transferCompoundToken();
356             inline label lineNumber() const;
357             inline label& lineNumber();
360         // Edit
362             //- Set bad
363             inline void setBad();
366         // Info
368             //- Return info proxy.
369             //  Used to print token information to a stream
370             InfoProxy<token> info() const
371             {
372                 return *this;
373             }
376     // Member operators
378         // Assignment
380             inline void operator=(const token&);
382             inline void operator=(const punctuationToken);
384             inline void operator=(word*);
385             inline void operator=(const word&);
387             inline void operator=(string*);
388             inline void operator=(const string&);
390             inline void operator=(const label);
391             inline void operator=(const floatScalar);
392             inline void operator=(const doubleScalar);
394             inline void operator=(compound*);
397         // Equality
399             inline bool operator==(const token&) const;
400             inline bool operator==(const punctuationToken) const;
401             inline bool operator==(const word&) const;
402             inline bool operator==(const string&) const;
403             inline bool operator==(const label) const;
404             inline bool operator==(const floatScalar) const;
405             inline bool operator==(const doubleScalar) const;
408         // Inequality
410             inline bool operator!=(const token&) const;
411             inline bool operator!=(const punctuationToken) const;
412             inline bool operator!=(const word&) const;
413             inline bool operator!=(const string&) const;
414             inline bool operator!=(const label) const;
415             inline bool operator!=(const floatScalar) const;
416             inline bool operator!=(const doubleScalar) const;
419     // IOstream operators
421         friend Istream& operator>>(Istream&, token&);
422         friend Ostream& operator<<(Ostream&, const token&);
424         friend Ostream& operator<<(Ostream&, const punctuationToken&);
425         friend ostream& operator<<(ostream&, const punctuationToken&);
427         friend ostream& operator<<(ostream&, const InfoProxy<token>&);
431 Ostream& operator<<(Ostream&, const token::punctuationToken&);
432 ostream& operator<<(ostream&, const token::punctuationToken&);
433 ostream& operator<<(ostream&, const InfoProxy<token>&);
434 Ostream& operator<<(Ostream&, const token::compound&);
437 #define defineCompoundTypeName(Type, Name)                                    \
438     typedef token::Compound<Type > tokenCompound##Name##_;                    \
439     defineTemplateTypeNameAndDebugWithName(tokenCompound##Name##_, #Type, 0);
441 #define addCompoundToRunTimeSelectionTable(Type, Name)                        \
442     token::compound::addIstreamConstructorToTable<token::Compound<Type > >    \
443         add##Name##IstreamConstructorToTable_;
446 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
448 } // End namespace Foam
450 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 #include "tokenI.H"
453 #include "Istream.H"
455 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457 #endif
459 // ************************************************************************* //