token.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::token
26 
27 Description
28  A token holds items read from Istream.
29 
30 SourceFiles
31  tokenI.H
32  token.C
33  tokenIO.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef token_H
38 #define token_H
39 
40 #include "label.H"
41 #include "uLabel.H"
42 #include "scalar.H"
43 #include "word.H"
44 #include "InfoProxy.H"
45 #include "refCount.H"
46 #include "typeInfo.H"
47 
48 #define NoHashTableC
49 #include "runTimeSelectionTables.H"
50 
51 #include <iostream>
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declaration of friend functions and operators
59 
60 class token;
61 Istream& operator>>(Istream&, token&);
62 Ostream& operator<<(Ostream&, const token&);
63 
64 /*---------------------------------------------------------------------------*\
65  Class token Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class token
69 {
70 
71 public:
72 
73  //- Enumeration defining the types of token
74  enum tokenType
75  {
76  UNDEFINED,
77 
86  COMPOUND,
87 
88  ERROR
89  };
90 
91 
92  //- Standard punctuation tokens
93  enum punctuationToken
94  {
95  NULL_TOKEN = '\0',
96  SPACE = ' ',
97  TAB = '\t',
98  NL = '\n',
99 
101  BEGIN_LIST = '(',
102  END_LIST = ')',
103  BEGIN_SQR = '[',
104  END_SQR = ']',
105  BEGIN_BLOCK = '{',
106  END_BLOCK = '}',
107  COLON = ':',
108  COMMA = ',',
109  HASH = '#',
110 
113 
114  ASSIGN = '=',
115  ADD = '+',
116  SUBTRACT = '-',
117  MULTIPLY = '*',
118  DIVIDE = '/'
119  };
120 
121 
122  //- Abstract base class for complex tokens
123  class compound
124  :
125  public refCount
126  {
127  // Private data
128 
129  bool empty_;
130 
131 
132  // Private Member Functions
133 
134  //- Disallow default bitwise copy construct
135  compound(const compound&);
136 
137  //- Disallow default bitwise assignment
138  void operator=(const compound&);
139 
140 
141  public:
142 
143  //- Runtime type information
144  TypeName("compound");
145 
146 
147  //- Declare run-time constructor selection table
149  (
150  autoPtr,
151  compound,
152  Istream,
153  (Istream& is),
154  (is)
155  );
156 
157 
158  // Constructors
159 
160  //- Construct null
161  compound()
162  :
163  empty_(false)
164  {}
165 
166 
167  // Selectors
168 
169  //- Select null constructed
170  static autoPtr<compound> New(const word& type, Istream&);
171 
172 
173  //- Destructor
174  virtual ~compound();
175 
176 
177  // Member Functions
178 
179  // Access
180 
181  //- Return true if name is a compound type
182  static bool isCompound(const word& name);
183 
184  bool empty() const
185  {
186  return empty_;
187  }
188 
189  bool& empty()
190  {
191  return empty_;
192  }
193 
194  virtual label size() const = 0;
195 
196 
197  // Check
198 
199  // Edit
200 
201  // Write
202 
203  virtual void write(Ostream&) const = 0;
204 
205 
206  // IOstream Operators
207 
208  friend Ostream& operator<<(Ostream&, const compound&);
209  };
210 
211 
212  //- A templated class for holding compound tokens
213  template<class T>
214  class Compound
215  :
216  public token::compound,
217  public T
218  {
219  public:
220 
221  //- Runtime type information
222  TypeName("Compound<T>");
223 
224  Compound(Istream& is)
225  :
226  T(is)
227  {}
228 
229  label size() const
230  {
231  return T::size();
232  }
233 
234  void write(Ostream& os) const
235  {
236  operator<<(os, static_cast<const T&>(*this));
237  }
238  };
239 
240 
241  //- Static undefined token
242  static token undefinedToken;
243 
244 
245 private:
246 
247  // Private data
248 
249  //- The token type
251 
252  //- Anonymous Union of token types
253  union
254  {
261  mutable compound* compoundTokenPtr_;
262  };
263 
264  //- Line number in the file this token was read from
266 
267 
268  // Private Member Functions
269 
270  //- Clear any allocated storage (word or string)
271  inline void clear();
272 
273  // Parse error, expected 'expected', found ...
274  void parseError(const char* expected) const;
275 
276 
277 public:
278 
279  // Static data members
280 
281  static const char* const typeName;
282 
283 
284  // Constructors
285 
286  //- Construct null
287  inline token();
288 
289  //- Construct as copy
290  inline token(const token&);
291 
292  //- Construct punctuation character token
294 
295  //- Construct word token
296  inline token(const word&, label lineNumber=0);
297 
298  //- Construct string token
299  inline token(const string&, label lineNumber=0);
300 
301  //- Construct label token
302  inline token(const label, label lineNumber=0);
303 
304  //- Construct floatScalar token
305  inline token(const floatScalar, label lineNumber=0);
306 
307  //- Construct doubleScalar token
308  inline token(const doubleScalar, label lineNumber=0);
309 
310  //- Construct from Istream
311  token(Istream&);
312 
313 
314  //- Destructor
315  inline ~token();
316 
317 
318  // Member functions
319 
320  // Access
321 
322  inline tokenType type() const;
323  inline tokenType& type();
324 
325  inline bool good() const;
326  inline bool undefined() const;
327  inline bool error() const;
328 
329  inline bool isPunctuation() const;
330  inline punctuationToken pToken() const;
331 
332  inline bool isWord() const;
333  inline const word& wordToken() const;
334 
335  inline bool isVariable() const;
336 
337  inline bool isString() const;
338  inline const string& stringToken() const;
339 
340  inline bool isLabel() const;
341  inline label labelToken() const;
342 
343  inline bool isFloatScalar() const;
344  inline floatScalar floatScalarToken() const;
345 
346  inline bool isDoubleScalar() const;
347  inline doubleScalar doubleScalarToken() const;
348 
349  inline bool isScalar() const;
350  inline scalar scalarToken() const;
351 
352  inline bool isNumber() const;
353  inline scalar number() const;
354 
355  inline bool isCompound() const;
356  inline const compound& compoundToken() const;
358 
359  inline label lineNumber() const;
360  inline label& lineNumber();
361 
362 
363  // Edit
364 
365  //- Set bad
366  inline void setBad();
367 
368 
369  // Info
370 
371  //- Return info proxy.
372  // Used to print token information to a stream
373  InfoProxy<token> info() const
374  {
375  return *this;
376  }
377 
378 
379  // Member operators
380 
381  // Assignment
382 
383  inline void operator=(const token&);
384 
385  inline void operator=(const punctuationToken);
386 
387  inline void operator=(word*);
388  inline void operator=(const word&);
389 
390  inline void operator=(string*);
391  inline void operator=(const string&);
392 
393  inline void operator=(const label);
394  inline void operator=(const floatScalar);
395  inline void operator=(const doubleScalar);
396 
397  inline void operator=(compound*);
398 
399 
400  // Equality
401 
402  inline bool operator==(const token&) const;
403  inline bool operator==(const punctuationToken) const;
404  inline bool operator==(const word&) const;
405  inline bool operator==(const string&) const;
406  inline bool operator==(const label) const;
407  inline bool operator==(const floatScalar) const;
408  inline bool operator==(const doubleScalar) const;
409 
410 
411  // Inequality
412 
413  inline bool operator!=(const token&) const;
414  inline bool operator!=(const punctuationToken) const;
415  inline bool operator!=(const word&) const;
416  inline bool operator!=(const string&) const;
417  inline bool operator!=(const label) const;
418  inline bool operator!=(const floatScalar) const;
419  inline bool operator!=(const doubleScalar) const;
420 
421 
422  // IOstream operators
423 
424  friend Istream& operator>>(Istream&, token&);
425  friend Ostream& operator<<(Ostream&, const token&);
426 
427  friend Ostream& operator<<(Ostream&, const punctuationToken&);
428  friend ostream& operator<<(ostream&, const punctuationToken&);
429 
430  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
431 };
432 
433 
434 Ostream& operator<<(Ostream&, const token::punctuationToken&);
435 ostream& operator<<(ostream&, const token::punctuationToken&);
436 Ostream& operator<<(Ostream&, const token::compound&);
437 
438 ostream& operator<<(ostream&, const InfoProxy<token>&);
439 
440 template<>
441 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
442 
443 #define defineCompoundTypeName(Type, Name) \
444  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
445 
446 #define addCompoundToRunTimeSelectionTable(Type, Name) \
447  token::compound::addIstreamConstructorToTable<token::Compound<Type> > \
448  add##Name##IstreamConstructorToTable_;
449 
450 
451 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
452 
453 } // End namespace Foam
454 
455 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
456 
457 #include "tokenI.H"
458 #include "Istream.H"
459 
460 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
461 
462 #endif
463 
464 // ************************************************************************* //
Foam::token::isDoubleScalar
bool isDoubleScalar() const
Definition: tokenI.H:299
Foam::token::compound::New
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
Foam::token::Compound::Compound
Compound(Istream &is)
Definition: token.H:223
Foam::token::END_STATEMENT
@ END_STATEMENT
Definition: token.H:99
Foam::doubleScalar
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
Foam::token::ADD
@ ADD
Definition: token.H:114
Foam::token::isScalar
bool isScalar() const
Definition: tokenI.H:318
Foam::token::scalarToken
scalar scalarToken() const
Definition: tokenI.H:323
Foam::token::compound
Abstract base class for complex tokens.
Definition: token.H:122
Foam::token::compound::operator<<
friend Ostream & operator<<(Ostream &, const compound &)
Foam::token::wordTokenPtr_
word * wordTokenPtr_
Definition: token.H:255
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::token::Compound
A templated class for holding compound tokens.
Definition: token.H:213
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
Foam::token::Compound::size
label size() const
Definition: token.H:228
typeInfo.H
Foam::token::isNumber
bool isNumber() const
Definition: tokenI.H:340
Foam::token::compound::operator=
void operator=(const compound &)
Disallow default bitwise assignment.
Foam::refCount
Reference counter for various OpenFOAM components.
Definition: refCount.H:45
Foam::token::compound::~compound
virtual ~compound()
Destructor.
Definition: token.C:53
Foam::token::LABEL
@ LABEL
Definition: token.H:82
InfoProxy.H
Foam::token::COMMA
@ COMMA
Definition: token.H:107
Foam::token::isFloatScalar
bool isFloatScalar() const
Definition: tokenI.H:280
Foam::floatScalar
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
Foam::token::punctuationToken
punctuationToken
Standard punctuation tokens.
Definition: token.H:92
Foam::token::wordToken
const word & wordToken() const
Definition: tokenI.H:226
Foam::token::isString
bool isString() const
Definition: tokenI.H:244
Foam::token::SUBTRACT
@ SUBTRACT
Definition: token.H:115
Foam::token::PUNCTUATION
@ PUNCTUATION
Definition: token.H:77
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::token::lineNumber
label lineNumber() const
Definition: tokenI.H:381
Foam::token::floatScalarToken_
floatScalar floatScalarToken_
Definition: token.H:258
Foam::token::ERROR
@ ERROR
Definition: token.H:87
Foam::token::good
bool good() const
Definition: tokenI.H:188
Foam::token::compound::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, compound, Istream,(Istream &is),(is))
Declare run-time constructor selection table.
Foam::token::compoundToken
const compound & compoundToken() const
Definition: tokenI.H:367
Foam::token::isCompound
bool isCompound() const
Definition: tokenI.H:362
Foam::token::NL
@ NL
Definition: token.H:97
Foam::token::compound::TypeName
TypeName("compound")
Runtime type information.
Foam::token::compound::empty
bool empty() const
Definition: token.H:183
Foam::token::COLON
@ COLON
Definition: token.H:106
Foam::token::stringToken
const string & stringToken() const
Definition: tokenI.H:249
Foam::token::isLabel
bool isLabel() const
Definition: tokenI.H:262
Foam::token::type_
tokenType type_
The token type.
Definition: token.H:249
Foam::token::FLOAT_SCALAR
@ FLOAT_SCALAR
Definition: token.H:83
Foam::token::clear
void clear()
Clear any allocated storage (word or string)
Definition: tokenI.H:36
Foam::token::number
scalar number() const
Definition: tokenI.H:345
Foam::token::type
tokenType type() const
Definition: tokenI.H:178
Foam::token::operator!=
bool operator!=(const token &) const
Definition: tokenI.H:585
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::token::info
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::token::MULTIPLY
@ MULTIPLY
Definition: token.H:116
Foam::token::VARIABLE
@ VARIABLE
Definition: token.H:79
Foam::token::~token
~token()
Destructor.
Definition: tokenI.H:170
Foam::token::compound::empty
bool & empty()
Definition: token.H:188
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::token::isVariable
bool isVariable() const
Definition: tokenI.H:239
tokenI.H
Foam::token::floatScalarToken
floatScalar floatScalarToken() const
Definition: tokenI.H:285
Foam::token::transferCompoundToken
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
Foam::token::token
token()
Construct null.
Definition: tokenI.H:65
Foam::token::compound::isCompound
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
Istream.H
Foam::token::Compound::write
void write(Ostream &os) const
Definition: token.H:233
Foam::token::compound::write
virtual void write(Ostream &) const =0
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::token::BEGIN_STRING
@ BEGIN_STRING
Definition: token.H:110
Foam::token::pToken
punctuationToken pToken() const
Definition: tokenI.H:208
Foam::token::tokenType
tokenType
Enumeration defining the types of token.
Definition: token.H:73
Foam::token::COMPOUND
@ COMPOUND
Definition: token.H:85
Foam::token::isWord
bool isWord() const
Definition: tokenI.H:221
scalar.H
Foam::token::STRING
@ STRING
Definition: token.H:80
Foam::token::typeName
static const char *const typeName
Definition: token.H:280
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::token::operator>>
friend Istream & operator>>(Istream &, token &)
Foam::token::operator==
bool operator==(const token &) const
Definition: tokenI.H:509
Foam::token::compound::size
virtual label size() const =0
Foam::token::compoundTokenPtr_
compound * compoundTokenPtr_
Definition: token.H:260
Foam::token::stringTokenPtr_
string * stringTokenPtr_
Definition: token.H:256
Foam::token::Compound::TypeName
TypeName("Compound<T>")
Runtime type information.
Foam::token::operator=
void operator=(const token &)
Definition: tokenI.H:401
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::token::operator<<
friend Ostream & operator<<(Ostream &, const token &)
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::token::undefinedToken
static token undefinedToken
Static undefined token.
Definition: token.H:241
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::token::compound::empty_
bool empty_
Definition: token.H:128
Foam::token::parseError
void parseError(const char *expected) const
Definition: token.C:43
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
label.H
Foam::token::DIVIDE
@ DIVIDE
Definition: token.H:117
Foam::token::WORD
@ WORD
Definition: token.H:78
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::token::END_SQR
@ END_SQR
Definition: token.H:103
Foam::token::HASH
@ HASH
Definition: token.H:108
Foam::token::doubleScalarToken
doubleScalar doubleScalarToken() const
Definition: tokenI.H:304
Foam::token::DOUBLE_SCALAR
@ DOUBLE_SCALAR
Definition: token.H:84
Foam::token::compound::compound
compound()
Construct null.
Definition: token.H:160
Foam::token::labelToken_
label labelToken_
Definition: token.H:257
Foam::token::undefined
bool undefined() const
Definition: tokenI.H:193
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
Foam::token::BEGIN_SQR
@ BEGIN_SQR
Definition: token.H:102
Foam::token::punctuationToken_
punctuationToken punctuationToken_
Definition: token.H:254
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
word.H
Foam::token::ASSIGN
@ ASSIGN
Definition: token.H:113
uLabel.H
Foam::TAB
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:60
Foam::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.H:203
Foam::token::UNDEFINED
@ UNDEFINED
Definition: token.H:75
refCount.H
Foam::token::lineNumber_
label lineNumber_
Line number in the file this token was read from.
Definition: token.H:264
Foam::token::VERBATIMSTRING
@ VERBATIMSTRING
Definition: token.H:81
Foam::token::labelToken
label labelToken() const
Definition: tokenI.H:267
Foam::token::doubleScalarToken_
doubleScalar doubleScalarToken_
Definition: token.H:259
Foam::token::error
bool error() const
Definition: tokenI.H:198
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::token::NULL_TOKEN
@ NULL_TOKEN
Definition: token.H:94
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::SPACE
@ SPACE
Definition: token.H:95
Foam::token::setBad
void setBad()
Set bad.
Definition: tokenI.H:392
Foam::token::END_STRING
@ END_STRING
Definition: token.H:111