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 = '#',
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);
184  bool empty() const
185  {
186  return empty_;
187  }
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>");
224  Compound(Istream& is)
225  :
226  T(is)
227  {}
229  label size() const
230  {
231  return T::size();
232  }
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
250  tokenType 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
265  label lineNumber_;
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
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 
435 ostream& operator<<(ostream&, const token::punctuationToken&);
437 
438 ostream& operator<<(ostream&, const InfoProxy<token>&);
439 
440 template<>
441 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
443 #define defineCompoundTypeName(Type, Name) \
444  defineTemplateTypeNameAndDebugWithName(token::Compound<Type>, #Type, 0);
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 // ************************************************************************* //
doubleScalar doubleScalarToken() const
Definition: tokenI.H:304
scalar number() const
Definition: tokenI.H:345
friend Ostream & operator<<(Ostream &, const compound &)
bool error() const
Definition: tokenI.H:198
bool isFloatScalar() const
Definition: tokenI.H:280
static token undefinedToken
Static undefined token.
Definition: token.H:241
bool isWord() const
Definition: tokenI.H:221
bool isLabel() const
Definition: tokenI.H:262
~token()
Destructor.
Definition: tokenI.H:170
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
punctuationToken punctuationToken_
Definition: token.H:254
compound()
Construct null.
Definition: token.H:160
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
bool isScalar() const
Definition: tokenI.H:318
floatScalar floatScalarToken() const
Definition: tokenI.H:285
Macros to ease declaration of run-time selection tables.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual ~compound()
Destructor.
Definition: token.C:53
friend Istream & operator>>(Istream &, token &)
doubleScalar doubleScalarToken_
Definition: token.H:259
A class for handling words, derived from string.
Definition: word.H:59
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
bool isPunctuation() const
Definition: tokenI.H:203
const word & wordToken() const
Definition: tokenI.H:226
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:133
token()
Construct null.
Definition: tokenI.H:65
const compound & compoundToken() const
Definition: tokenI.H:367
Namespace for OpenFOAM.
static const char *const typeName
Definition: token.H:280
string * stringTokenPtr_
Definition: token.H:256
bool isNumber() const
Definition: tokenI.H:340
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:83
bool operator!=(const token &) const
Definition: tokenI.H:585
punctuationToken
Standard punctuation tokens.
Definition: token.H:92
bool isString() const
Definition: tokenI.H:244
bool isDoubleScalar() const
Definition: tokenI.H:299
tokenType type() const
Definition: tokenI.H:178
void setBad()
Set bad.
Definition: tokenI.H:392
bool empty() const
Definition: token.H:183
label lineNumber() const
Definition: tokenI.H:381
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:372
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
floatScalar floatScalarToken_
Definition: token.H:258
TypeName("compound")
Runtime type information.
bool operator==(const token &) const
Definition: tokenI.H:509
A templated class for holding compound tokens.
Definition: token.H:213
word * wordTokenPtr_
Definition: token.H:255
A token holds items read from Istream.
Definition: token.H:67
const string & stringToken() const
Definition: tokenI.H:249
Reference counter for various OpenFOAM components.
Definition: refCount.H:45
tokenType
Enumeration defining the types of token.
Definition: token.H:73
declareRunTimeSelectionTable(autoPtr, compound, Istream,(Istream &is),(is))
Declare run-time constructor selection table.
compound & transferCompoundToken(const Istream &is)
Definition: token.C:93
label labelToken() const
Definition: tokenI.H:267
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:60
virtual void write(Ostream &) const =0
label labelToken_
Definition: token.H:257
Abstract base class for complex tokens.
Definition: token.H:122
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
virtual label size() const =0
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:60
bool good() const
Definition: tokenI.H:188
compound * compoundTokenPtr_
Definition: token.H:260
bool undefined() const
Definition: tokenI.H:193
punctuationToken pToken() const
Definition: tokenI.H:208
bool isVariable() const
Definition: tokenI.H:239
scalar scalarToken() const
Definition: tokenI.H:323
UEqn clear()
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117