FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/label.H>
41 #include <OpenFOAM/uLabel.H>
42 #include <OpenFOAM/scalar.H>
43 #include <OpenFOAM/word.H>
44 #include <OpenFOAM/InfoProxy.H>
45 #include <OpenFOAM/refCount.H>
46 #include <OpenFOAM/typeInfo.H>
47 
48 #define NoHashTableC
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  {
77 
85 
87  };
88 
89 
90  //- Standard punctuation tokens
92  {
93  NULL_TOKEN = '\0',
94  SPACE = ' ',
95  TAB = '\t',
96  NL = '\n',
97 
99  BEGIN_LIST = '(',
100  END_LIST = ')',
101  BEGIN_SQR = '[',
102  END_SQR = ']',
103  BEGIN_BLOCK = '{',
104  END_BLOCK = '}',
105  COLON = ':',
106  COMMA = ',',
107 
110 
111  ASSIGN = '=',
112  ADD = '+',
113  SUBTRACT = '-',
114  MULTIPLY = '*',
115  DIVIDE = '/'
116  };
117 
118 
119  //- Abstract base class for complex tokens
120  class compound
121  :
122  public refCount
123  {
124  // Private data
125 
126  bool empty_;
127 
128 
129  // Private Member Functions
130 
131  //- Disallow default bitwise copy construct
132  compound(const compound&);
133 
134  //- Disallow default bitwise assignment
135  void operator=(const compound&);
136 
137 
138  public:
139 
140  //- Runtime type information
141  TypeName("compound");
142 
143 
144  //- Declare run-time constructor selection table
146  (
147  autoPtr,
148  compound,
149  Istream,
150  (Istream& is),
151  (is)
152  );
153 
154 
155  // Constructors
156 
157  //- Construct null
159  :
160  empty_(false)
161  {}
162 
163 
164  // Selectors
165 
166  //- Select null constructed
167  static autoPtr<compound> New(const word& type, Istream&);
168 
169 
170  // Destructor
171 
172  virtual ~compound();
173 
174 
175  // Member Functions
176 
177  // Access
178 
179  //- Return true if name is a compound type
180  static bool isCompound(const word& name);
181 
182  bool empty() const
183  {
184  return empty_;
185  }
186 
187  bool& empty()
188  {
189  return empty_;
190  }
191 
192  virtual label size() const = 0;
193 
194 
195  // Check
196 
197  // Edit
198 
199  // Write
200 
201  virtual void write(Ostream&) const = 0;
202 
203 
204  // IOstream Operators
205 
206  friend Ostream& operator<<(Ostream&, const compound&);
207  };
208 
209 
210  //- A templated class for holding compound tokens
211  template<class T>
212  class Compound
213  :
214  public token::compound,
215  public T
216  {
217  public:
218 
219  //- Runtime type information
220  TypeName("Compound<T>");
221 
223  :
224  T(is)
225  {}
226 
227  label size() const
228  {
229  return T::size();
230  }
231 
232  void write(Ostream& os) const
233  {
234  operator<<(os, static_cast<const T&>(*this));
235  }
236  };
237 
238 
239  //- Static undefined token
241 
242 
243 private:
244 
245  // Private data
246 
247  //- The token type
248  tokenType type_;
249 
250  //- Anonymous Union of token types
251  union
252  {
256  label labelToken_;
260  };
261 
262  //- Line number in the file this token was read from
263  label lineNumber_;
264 
265 
266  // Private member functions
267 
268  //- Clear any allocated storage (word or string)
269  inline void clear();
270 
271  // Parse error, expected 'expected', found ...
272  void parseError(const char* expected) const;
273 
274 
275 public:
276 
277  // Static data members
278 
279  static const char* const typeName;
280 
281 
282  // Constructors
283 
284  //- Construct null
285  inline token();
286 
287  //- Construct as copy
288  inline token(const token&);
289 
290  //- Construct punctuation character token
291  inline token(punctuationToken, label lineNumber=0);
292 
293  //- Construct word token
294  inline token(const word&, label lineNumber=0);
295 
296  //- Construct string token
297  inline token(const string&, label lineNumber=0);
298 
299  //- Construct label token
300  inline token(const label, label lineNumber=0);
301 
302  //- Construct floatScalar token
303  inline token(const floatScalar, label lineNumber=0);
304 
305  //- Construct doubleScalar token
306  inline token(const doubleScalar, label lineNumber=0);
307 
308  //- Construct from Istream
309  token(Istream&);
310 
311 
312  // Destructor
313 
314  inline ~token();
315 
316 
317  // Member functions
318 
319  // Access
320 
321  inline tokenType type() const;
322 
323  inline bool good() const;
324  inline bool undefined() const;
325  inline bool error() const;
326 
327  inline bool isPunctuation() const;
328  inline punctuationToken pToken() const;
329 
330  inline bool isWord() const;
331  inline const word& wordToken() const;
332 
333  inline bool isString() const;
334  inline const string& stringToken() const;
335 
336  inline bool isLabel() const;
337  inline label labelToken() const;
338 
339  inline bool isFloatScalar() const;
340  inline floatScalar floatScalarToken() const;
341 
342  inline bool isDoubleScalar() const;
343  inline doubleScalar doubleScalarToken() const;
344 
345  inline bool isScalar() const;
346  inline scalar scalarToken() const;
347 
348  inline bool isNumber() const;
349  inline scalar number() const;
350 
351  inline bool isCompound() const;
352  inline const compound& compoundToken() const;
354 
355  inline label lineNumber() const;
356  inline label& lineNumber();
357 
358 
359  // Edit
360 
361  //- Set bad
362  inline void setBad();
363 
364 
365  // Info
366 
367  //- Return info proxy.
368  // Used to print token information to a stream
370  {
371  return *this;
372  }
373 
374 
375  // Member operators
376 
377  // Assignment
378 
379  inline void operator=(const token&);
380 
381  inline void operator=(const punctuationToken);
382 
383  inline void operator=(word*);
384  inline void operator=(const word&);
385 
386  inline void operator=(string*);
387  inline void operator=(const string&);
388 
389  inline void operator=(const label);
390  inline void operator=(const floatScalar);
391  inline void operator=(const doubleScalar);
392 
393  inline void operator=(compound*);
394 
395 
396  // Equality
397 
398  inline bool operator==(const token&) const;
399  inline bool operator==(const punctuationToken) const;
400  inline bool operator==(const word&) const;
401  inline bool operator==(const string&) const;
402  inline bool operator==(const label) const;
403  inline bool operator==(const floatScalar) const;
404  inline bool operator==(const doubleScalar) const;
405 
406 
407  // Inequality
408 
409  inline bool operator!=(const token&) const;
410  inline bool operator!=(const punctuationToken) const;
411  inline bool operator!=(const word&) const;
412  inline bool operator!=(const string&) const;
413  inline bool operator!=(const label) const;
414  inline bool operator!=(const floatScalar) const;
415  inline bool operator!=(const doubleScalar) const;
416 
417 
418  // IOstream operators
419 
420  friend Istream& operator>>(Istream&, token&);
421  friend Ostream& operator<<(Ostream&, const token&);
422 
423  friend Ostream& operator<<(Ostream&, const punctuationToken&);
424  friend ostream& operator<<(ostream&, const punctuationToken&);
425 
426  friend ostream& operator<<(ostream&, const InfoProxy<token>&);
427 };
428 
429 
430 Ostream& operator<<(Ostream&, const token::punctuationToken&);
431 ostream& operator<<(ostream&, const token::punctuationToken&);
432 ostream& operator<<(ostream&, const InfoProxy<token>&);
433 Ostream& operator<<(Ostream&, const token::compound&);
434 
435 
436 #define defineCompoundTypeName(Type, Name) \
437  typedef token::Compound<Type > tokenCompound##Name##_; \
438  defineTemplateTypeNameAndDebugWithName(tokenCompound##Name##_, #Type, 0);
439 
440 #define addCompoundToRunTimeSelectionTable(Type, Name) \
441  token::compound::addIstreamConstructorToTable<token::Compound<Type > > \
442  add##Name##IstreamConstructorToTable_;
443 
444 
445 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
446 
447 } // End namespace Foam
448 
449 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
450 
451 #include "tokenI.H"
452 #include <OpenFOAM/Istream.H>
453 
454 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
455 
456 #endif
457 
458 // ************************ vim: set sw=4 sts=4 et: ************************ //