initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOstreams / token / token.H
blobc033d8f7c785f5ced73d78fc0cb2c7b94b0246d7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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     tokenIO.C
34     printToken.C
36 \*---------------------------------------------------------------------------*/
38 #ifndef token_H
39 #define token_H
41 #include "label.H"
42 #include "scalar.H"
43 #include "word.H"
44 #include "InfoProxy.H"
45 #include "refCount.H"
46 #include "typeInfo.H"
48 #define NoHashTableC
49 #include "runTimeSelectionTables.H"
51 #include <iostream>
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 namespace Foam
58 // Forward declaration of friend functions and operators
60 class token;
61 Istream& operator>>(Istream&, token&);
62 Ostream& operator<<(Ostream&, const token&);
64 /*---------------------------------------------------------------------------*\
65                            Class token Declaration
66 \*---------------------------------------------------------------------------*/
68 #include "CintDefs.H"
70 class token
73 public:
75     //- Enumeration defining the types of token
76     enum tokenType
77     {
78         UNDEFINED,
80         PUNCTUATION,
81         WORD,
82         STRING,
83         LABEL,
84         FLOAT_SCALAR,
85         DOUBLE_SCALAR,
86         COMPOUND,
88         ERROR
89     };
92     //- Standard punctuation tokens
93     enum punctuationToken
94     {
95         NULL_TOKEN    = '\0',
96         SPACE         = ' ',
97         TAB           = '\t',
98         NL            = '\n',
100         END_STATEMENT = ';',
101         BEGIN_LIST    = '(',
102         END_LIST      = ')',
103         BEGIN_SQR     = '[',
104         END_SQR       = ']',
105         BEGIN_BLOCK   = '{',
106         END_BLOCK     = '}',
107         COLON         = ':',
108         COMMA         = ',',
109         BEGIN_STRING  = '"',
110         END_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& is);
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             #ifndef __MAKECINT__
208             friend Ostream& operator<<(Ostream&, const compound&);
209             #endif
210     };
213     //- A templated class for holding compound tokens
214     template<class T>
215     class Compound
216     :
217         public token::compound,
218         public T
219     {
220     public:
222         //- Runtime type information
223         TypeName("Compound<T>");
225         Compound(Istream& is)
226         :
227             T(is)
228         {}
230         label size() const
231         {
232             return T::size();
233         }
235         void write(Ostream& os) const
236         {
237             operator<<(os, static_cast<const T&>(*this));
238         }
239     };
242     //- Static undefined token
243     static token undefinedToken;
246 private:
248     // Private data
250         //- The token type
251         tokenType type_;
253         //- Anonymous Union of token types
254         union
255         {
256             punctuationToken punctuationToken_;
257             word* wordTokenPtr_;
258             string* stringTokenPtr_;
259             label labelToken_;
260             floatScalar floatScalarToken_;
261             doubleScalar doubleScalarToken_;
262             mutable compound* compoundTokenPtr_;
263         };
265         //- Line number in the file this token was read from
266         label lineNumber_;
269     // Private member functions
271         //- Clear any allocated storage (word or string)
272         inline void clear();
274         // Parse error, expected 'expected', found ...
275         void parseError(const char* expected) const;
278 public:
280     // Static data members
282         static const char* const typeName;
285     // Constructors
287         //- Construct null
288         inline token();
290         //- Construct as copy
291         inline token(const token&);
293         //- Construct punctuation character token
294         inline token(punctuationToken p, label lineNumber=0);
296         //- Construct word token
297         inline token(const word& w, label lineNumber=0);
299         //- Construct string token
300         inline token(const string& s, label lineNumber=0);
302         //- Construct label token
303         inline token(const label, label lineNumber=0);
305         //- Construct floatScalar token
306         inline token(const floatScalar s, label lineNumber=0);
308         //- Construct doubleScalar token
309         inline token(const doubleScalar s, label lineNumber=0);
311         //- Construct from Istream
312         token(Istream&);
315     // Destructor
317         inline ~token();
320     // Member functions
322         // Access
324             inline tokenType type() const;
326             inline bool good() const;
327             inline bool undefined() const;
328             inline bool error() const;
330             inline bool isPunctuation() const;
331             inline punctuationToken pToken() const;
333             inline bool isWord() const;
334             inline const word& wordToken() const;
336             inline bool isString() const;
337             inline const string& stringToken() const;
339             inline bool isLabel() const;
340             inline label labelToken() const;
342             inline bool isFloatScalar() const;
343             inline floatScalar floatScalarToken() const;
345             inline bool isDoubleScalar() const;
346             inline doubleScalar doubleScalarToken() const;
348             inline bool isScalar() const;
349             inline scalar scalarToken() const;
351             inline bool isNumber() const;
352             inline scalar number() const;
354             inline bool isCompound() const;
355             inline const compound& compoundToken() const;
356             compound& transferCompoundToken();
358             inline label lineNumber() const;
359             inline label& lineNumber();
362         // Edit
364             //- Set bad
365             inline void setBad();
368         // Info
370             //- Return info proxy.
371             //  Used to print token information to a stream
372             InfoProxy<token> info() const
373             {
374                 return *this;
375             }
378     // Member operators
380         // Assignment
382             inline void operator=(const token&);
384             inline void operator=(const punctuationToken);
386             inline void operator=(word*);
387             inline void operator=(const word&);
389             inline void operator=(string*);
390             inline void operator=(const string&);
392             inline void operator=(const label);
393             inline void operator=(const floatScalar);
394             inline void operator=(const doubleScalar);
396             inline void operator=(compound*);
399         // Equality
401             inline bool operator==(const token&) const;
402             inline bool operator==(const punctuationToken) const;
403             inline bool operator==(const word&) const;
404             inline bool operator==(const string&) const;
405             inline bool operator==(const label) const;
406             inline bool operator==(const floatScalar) const;
407             inline bool operator==(const doubleScalar) const;
410         // Inequality
412             inline bool operator!=(const token&) const;
413             inline bool operator!=(const punctuationToken) const;
414             inline bool operator!=(const word&) const;
415             inline bool operator!=(const string&) const;
416             inline bool operator!=(const label) const;
417             inline bool operator!=(const floatScalar) const;
418             inline bool operator!=(const doubleScalar) const;
421     // IOstream operators
423         friend Istream& operator>>(Istream&, token&);
424         friend Ostream& operator<<(Ostream&, const token&);
426         friend Ostream& operator<<(Ostream&, const punctuationToken&);
427         friend ostream& operator<<(ostream&, const punctuationToken&);
429         friend ostream& operator<<(ostream&, const InfoProxy<token>&);
432 #include "CintUndefs.H"
434 Ostream& operator<<(Ostream&, const token::punctuationToken&);
435 ostream& operator<<(ostream&, const token::punctuationToken&);
436 ostream& operator<<(ostream&, const InfoProxy<token>&);
437 Ostream& operator<<(Ostream&, const token::compound&);
440 #define defineCompoundTypeName(Type, Name)                                    \
441     typedef token::Compound<Type > tokenCompound##Name##_;                    \
442     defineTemplateTypeNameAndDebugWithName(tokenCompound##Name##_, #Type, 0);
444 #define addCompoundToRunTimeSelectionTable(Type, Name)                        \
445     token::compound::addIstreamConstructorToTable<token::Compound<Type > >    \
446         add##Name##IstreamConstructorToTable_;
449 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
451 } // End namespace Foam
453 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
455 #include "tokenI.H"
456 #include "Istream.H"
458 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
460 #endif
462 // ************************************************************************* //