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