token.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 : char
77  {
78  UNDEFINED = 0,
79 
80  PUNCTUATION = char(128),
89  COMPOUND,
90 
91  ERROR
92  };
93 
94  //- Standard punctuation tokens
95  enum punctuationToken : char
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  //- Abstract base class for complex tokens
124  class compound
125  :
126  public refCount
127  {
128  // Private data
129 
130  bool empty_;
131 
132 
133  // Private Member Functions
134 
135  //- Disallow default bitwise copy construct
136  compound(const compound&);
137 
138  //- Disallow default bitwise assignment
139  void operator=(const compound&);
140 
141 
142  public:
143 
144  //- Runtime type information
145  TypeName("compound");
146 
147 
148  //- Declare run-time constructor selection table
150  (
151  autoPtr,
152  compound,
153  Istream,
154  (Istream& is),
155  (is)
156  );
157 
158 
159  // Constructors
160 
161  //- Construct null
162  compound()
163  :
164  empty_(false)
165  {}
166 
167 
168  // Selectors
169 
170  //- Select null constructed
171  static autoPtr<compound> New(const word& type, Istream&);
172 
173 
174  //- Destructor
175  virtual ~compound();
176 
177 
178  // Member Functions
179 
180  // Access
181 
182  //- Return true if name is a compound type
183  static bool isCompound(const word& name);
185  bool empty() const
186  {
187  return empty_;
188  }
190  bool& empty()
191  {
192  return empty_;
193  }
194 
195  virtual label size() const = 0;
196 
197 
198  // Check
199 
200  // Edit
201 
202  // Write
203 
204  virtual void write(Ostream&) const = 0;
205 
206 
207  // IOstream Operators
208 
209  friend Ostream& operator<<(Ostream&, const compound&);
210  };
211 
212 
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:
221 
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  };
240 
241 
242  //- Static undefined token
243  static token undefinedToken;
244 
245 
246 private:
247 
248  // Private data
249 
250  //- The token type
251  tokenType type_;
252 
253  //- Anonymous Union of token types
254  union
255  {
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 longDoubleScalar token
313  inline token(const longDoubleScalar, label lineNumber=0);
314 
315  //- Construct from Istream
316  token(Istream&);
317 
318 
319  //- Destructor
320  inline ~token();
321 
322 
323  // Member functions
324 
325  // Access
326 
327  inline tokenType type() const;
328  inline tokenType& type();
329 
330  inline bool good() const;
331  inline bool undefined() const;
332  inline bool error() const;
333 
334  inline bool isPunctuation() const;
335  inline punctuationToken pToken() const;
336 
337  inline bool isWord() const;
338  inline const word& wordToken() const;
339 
340  inline bool isVariable() const;
341 
342  inline bool isString() const;
343  inline const string& stringToken() const;
344 
345  inline bool isLabel() const;
346  inline label labelToken() const;
347 
348  inline bool isFloatScalar() const;
349  inline floatScalar floatScalarToken() const;
350 
351  inline bool isDoubleScalar() const;
352  inline doubleScalar doubleScalarToken() const;
353 
354  inline bool isLongDoubleScalar() const;
356 
357  inline bool isScalar() const;
358  inline scalar scalarToken() const;
359 
360  inline bool isNumber() const;
361  inline scalar number() const;
362 
363  inline bool isCompound() const;
364  inline const compound& compoundToken() const;
366 
367  inline label lineNumber() const;
368  inline label& lineNumber();
369 
370 
371  // Edit
372 
373  //- Set bad
374  inline void setBad();
375 
376 
377  // Info
378 
379  //- Return info proxy.
380  // Used to print token information to a stream
381  InfoProxy<token> info() const
382  {
383  return *this;
384  }
385 
386 
387  // Member operators
388 
389  // Assignment
390 
391  inline void operator=(const token&);
392 
393  inline void operator=(const punctuationToken);
394 
395  inline void operator=(word*);
396  inline void operator=(const word&);
397 
398  inline void operator=(string*);
399  inline void operator=(const string&);
400 
401  inline void operator=(const label);
402  inline void operator=(const floatScalar);
403  inline void operator=(const doubleScalar);
404  inline void operator=(const longDoubleScalar);
405 
406  inline void operator=(compound*);
407 
408 
409  // Equality
410 
411  inline bool operator==(const token&) const;
412  inline bool operator==(const punctuationToken) const;
413  inline bool operator==(const word&) const;
414  inline bool operator==(const string&) const;
415  inline bool operator==(const label) const;
416  inline bool operator==(const floatScalar) const;
417  inline bool operator==(const doubleScalar) const;
418  inline bool operator==(const longDoubleScalar) const;
419 
420 
421  // Inequality
422 
423  inline bool operator!=(const token&) const;
424  inline bool operator!=(const punctuationToken) const;
425  inline bool operator!=(const word&) const;
426  inline bool operator!=(const string&) const;
427  inline bool operator!=(const label) const;
428  inline bool operator!=(const floatScalar) const;
429  inline bool operator!=(const doubleScalar) const;
430  inline bool operator!=(const longDoubleScalar) const;
431 
432 
433  // IOstream operators
434 
435  friend Istream& operator>>(Istream&, token&);
436  friend Ostream& operator<<(Ostream&, const token&);
437 
438  friend Ostream& operator<<(Ostream&, const punctuationToken&);
439  friend ostream& operator<<(ostream&, const punctuationToken&);
440 
441  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
442 };
443 
444 
446 ostream& operator<<(ostream&, const token::punctuationToken&);
448 
449 ostream& operator<<(ostream&, const InfoProxy<token>&);
450 
451 template<>
452 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
454 #define defineCompoundTypeName(Type, Name) \
455  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
457 #define addCompoundToRunTimeSelectionTable(Type, Name) \
458  token::compound::addIstreamConstructorToTable<token::Compound<Type>> \
459  add##Name##IstreamConstructorToTable_;
460 
461 
462 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
463 
464 } // End namespace Foam
465 
466 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 
468 #include "tokenI.H"
469 #include "Istream.H"
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 #endif
474 
475 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
doubleScalar doubleScalarToken_
Definition: token.H:260
~token()
Destructor.
Definition: tokenI.H:179
virtual ~compound()
Destructor.
Definition: token.C:53
bool isWord() const
Definition: tokenI.H:230
punctuationToken pToken() const
Definition: tokenI.H:217
bool operator!=(const token &) const
Definition: tokenI.H:649
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
bool isLongDoubleScalar() const
Definition: tokenI.H:327
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:261
compound * compoundTokenPtr_
Definition: token.H:262
Reference counter for various OpenFOAM components.
Definition: refCount.H:49
tUEqn clear()
scalar number() const
Definition: tokenI.H:382
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:380
label labelToken_
Definition: token.H:258
const word & wordToken() const
Definition: tokenI.H:235
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
tokenType
Enumeration defining the types of token.
Definition: token.H:75
string * stringTokenPtr_
Definition: token.H:257
TypeName("compound")
Runtime type information.
bool operator==(const token &) const
Definition: tokenI.H:558
scalar scalarToken() const
Definition: tokenI.H:356
friend Istream & operator>>(Istream &, token &)
word * wordTokenPtr_
Definition: token.H:256
bool isNumber() const
Definition: tokenI.H:377
Abstract base class for complex tokens.
Definition: token.H:123
A templated class for holding compound tokens.
Definition: token.H:214
floatScalar floatScalarToken() const
Definition: tokenI.H:294
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
static token undefinedToken
Static undefined token.
Definition: token.H:242
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
void setBad()
Set bad.
Definition: tokenI.H:429
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
bool good() const
Definition: tokenI.H:197
bool isScalar() const
Definition: tokenI.H:346
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:418
const compound & compoundToken() const
Definition: tokenI.H:404
bool isVariable() const
Definition: tokenI.H:248
bool undefined() const
Definition: tokenI.H:202
bool empty() const
Definition: token.H:184
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual void write(Ostream &) const =0
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:60
bool isFloatScalar() const
Definition: tokenI.H:289
bool isDoubleScalar() const
Definition: tokenI.H:308
const string & stringToken() const
Definition: tokenI.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:332
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool isString() const
Definition: tokenI.H:253
long double longDoubleScalar
Lang double precision floating point scalar type.
bool error() const
Definition: tokenI.H:207
virtual label size() const =0
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
floatScalar floatScalarToken_
Definition: token.H:259
tokenType type() const
Definition: tokenI.H:187
punctuationToken punctuationToken_
Definition: token.H:255
Ostream & operator<<(Ostream &, const ensightPart &)
label labelToken() const
Definition: tokenI.H:276
token()
Construct null.
Definition: tokenI.H:62
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Macros to ease declaration of run-time selection tables.
static const char *const typeName
Definition: token.H:282
doubleScalar doubleScalarToken() const
Definition: tokenI.H:313
compound()
Construct null.
Definition: token.H:161
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
bool isPunctuation() const
Definition: tokenI.H:212
Namespace for OpenFOAM.