FreeFOAM The Cross-Platform CFD Toolkit
tokenI.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 \*---------------------------------------------------------------------------*/
25 
26 #include <iostream>
27 
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 // Clear any allocated storage (word or string)
36 inline void token::clear()
37 {
38  if (type_ == WORD)
39  {
40  delete wordTokenPtr_;
41  }
42  else if (type_ == STRING)
43  {
44  delete stringTokenPtr_;
45  }
46  else if (type_ == COMPOUND)
47  {
49  {
50  delete compoundTokenPtr_;
51  }
52  else
53  {
54  compoundTokenPtr_->refCount::operator--();
55  }
56  }
57 
58  type_ = UNDEFINED;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 // Construct null
65 inline token::token()
66 :
67  type_(UNDEFINED),
68  lineNumber_(0)
69 {}
70 
71 // Construct as copy
72 inline token::token(const token& t)
73 :
74  type_(t.type_),
75  lineNumber_(t.lineNumber_)
76 {
77  switch (type_)
78  {
79  case token::UNDEFINED:
80  break;
81 
82  case PUNCTUATION:
84  break;
85 
86  case WORD:
88  break;
89 
90  case STRING:
92  break;
93 
94  case LABEL:
96  break;
97 
98  case FLOAT_SCALAR:
100  break;
101 
102  case DOUBLE_SCALAR:
104  break;
105 
106  case COMPOUND:
108  compoundTokenPtr_->refCount::operator++();
109  break;
110 
111  case token::ERROR:
112  break;
113  }
114 }
115 
116 // Construct punctuation character token
117 inline token::token(punctuationToken p, label lineNumber)
118 :
119  type_(PUNCTUATION),
120  punctuationToken_(p),
121  lineNumber_(lineNumber)
122 {}
123 
124 // Construct word token
125 inline token::token(const word& w, label lineNumber)
126 :
127  type_(WORD),
128  wordTokenPtr_(new word(w)),
129  lineNumber_(lineNumber)
130 {}
131 
132 // Construct string token
133 inline token::token(const string& s, label lineNumber)
134 :
135  type_(STRING),
136  stringTokenPtr_(new string(s)),
137  lineNumber_(lineNumber)
138 {}
139 
140 // Construct label token
141 inline token::token(const label l, label lineNumber)
142 :
143  type_(LABEL),
144  labelToken_(l),
145  lineNumber_(lineNumber)
146 {}
147 
148 // Construct floatScalar token
149 inline token::token(const floatScalar s, label lineNumber)
150 :
151  type_(FLOAT_SCALAR),
152  floatScalarToken_(s),
153  lineNumber_(lineNumber)
154 {}
155 
156 // Construct doubleScalar token
157 inline token::token(const doubleScalar s, label lineNumber)
158 :
159  type_(DOUBLE_SCALAR),
160  doubleScalarToken_(s),
161  lineNumber_(lineNumber)
162 {}
163 
164 
165 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
166 
167 // Delete token clearing the storage used by word or string
169 {
170  clear();
171 }
172 
173 
174 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175 
177 {
178  return type_;
179 }
180 
181 inline bool token::good() const
182 {
183  return (type_ != ERROR && type_ != UNDEFINED);
184 }
185 
186 inline bool token::undefined() const
187 {
188  return (type_ == UNDEFINED);
189 }
190 
191 inline bool token::error() const
192 {
193  return (type_ == ERROR);
194 }
195 
196 inline bool token::isPunctuation() const
197 {
198  return (type_ == PUNCTUATION);
199 }
200 
202 {
203  if (type_ == PUNCTUATION)
204  {
205  return punctuationToken_;
206  }
207  else
208  {
209  parseError("punctuation character");
210  return NULL_TOKEN;
211  }
212 }
213 
214 inline bool token::isWord() const
215 {
216  return (type_ == WORD);
217 }
218 
219 inline const word& token::wordToken() const
220 {
221  if (type_ == WORD)
222  {
223  return *wordTokenPtr_;
224  }
225  else
226  {
227  parseError("word");
228  return word::null;
229  }
230 }
231 
232 inline bool token::isString() const
233 {
234  return (type_ == STRING);
235 }
236 
237 inline const string& token::stringToken() const
238 {
239  if (type_ == STRING)
240  {
241  return *stringTokenPtr_;
242  }
243  else
244  {
245  parseError("string");
246  return string::null;
247  }
248 }
249 
250 inline bool token::isLabel() const
251 {
252  return (type_ == LABEL);
253 }
254 
255 inline label token::labelToken() const
256 {
257  if (type_ == LABEL)
258  {
259  return labelToken_;
260  }
261  else
262  {
263  parseError("label");
264  return 0;
265  }
266 }
267 
268 inline bool token::isFloatScalar() const
269 {
270  return (type_ == FLOAT_SCALAR);
271 }
272 
274 {
275  if (type_ == FLOAT_SCALAR)
276  {
277  return floatScalarToken_;
278  }
279  else
280  {
281  parseError("floatScalar");
282  return 0.0;
283  }
284 }
285 
286 
287 inline bool token::isDoubleScalar() const
288 {
289  return (type_ == DOUBLE_SCALAR);
290 }
291 
293 {
294  if (type_ == DOUBLE_SCALAR)
295  {
296  return doubleScalarToken_;
297  }
298  else
299  {
300  parseError("doubleScalar");
301  return 0.0;
302  }
303 }
304 
305 
306 inline bool token::isScalar() const
307 {
308  return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
309 }
310 
311 inline scalar token::scalarToken() const
312 {
313  if (type_ == FLOAT_SCALAR)
314  {
315  return floatScalarToken_;
316  }
317  else if (type_ == DOUBLE_SCALAR)
318  {
319  return doubleScalarToken_;
320  }
321  else
322  {
323  parseError("scalar");
324  return 0.0;
325  }
326 }
327 
328 inline bool token::isNumber() const
329 {
330  return (type_ == LABEL || isScalar());
331 }
332 
333 inline scalar token::number() const
334 {
335  if (type_ == LABEL)
336  {
337  return labelToken_;
338  }
339  else if (isScalar())
340  {
341  return scalarToken();
342  }
343  else
344  {
345  parseError("number (label or scalar)");
346  return 0.0;
347  }
348 }
349 
350 inline bool token::isCompound() const
351 {
352  return (type_ == COMPOUND);
353 }
354 
356 {
357  if (type_ == COMPOUND)
358  {
359  return *compoundTokenPtr_;
360  }
361  else
362  {
363  parseError("compound");
364  return *compoundTokenPtr_;
365  }
366 }
367 
368 
369 inline label token::lineNumber() const
370 {
371  return lineNumber_;
372 }
373 
374 inline label& token::lineNumber()
375 {
376  return lineNumber_;
377 }
378 
379 
380 inline void token::setBad()
381 {
382  clear();
383  type_ = ERROR;
384 }
385 
386 
387 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
388 
389 inline void token::operator=(const token& t)
390 {
391  clear();
392  type_ = t.type_;
393 
394  switch (type_)
395  {
396  case token::UNDEFINED:
397  break;
398 
399  case PUNCTUATION:
401  break;
402 
403  case WORD:
404  wordTokenPtr_ = new word(*t.wordTokenPtr_);
405  break;
406 
407  case STRING:
409  break;
410 
411  case LABEL:
413  break;
414 
415  case FLOAT_SCALAR:
417  break;
418 
419  case DOUBLE_SCALAR:
421  break;
422 
423  case COMPOUND:
425  compoundTokenPtr_->refCount::operator++();
426  break;
427 
428  case token::ERROR:
429  break;
430  }
431 
432  lineNumber_ = t.lineNumber_;
433 }
434 
436 {
437  clear();
438  type_ = PUNCTUATION;
440 }
441 
442 inline void token::operator=(word* wPtr)
443 {
444  clear();
445  type_ = WORD;
446  wordTokenPtr_ = wPtr;
447 }
448 
449 inline void token::operator=(const word& w)
450 {
451  operator=(new word(w));
452 }
453 
454 inline void token::operator=(string* sPtr)
455 {
456  clear();
457  type_ = STRING;
458  stringTokenPtr_ = sPtr;
459 }
460 
461 inline void token::operator=(const string& s)
462 {
463  operator=(new string(s));
464 }
465 
466 inline void token::operator=(const label l)
467 {
468  clear();
469  type_ = LABEL;
470  labelToken_ = l;
471 }
472 
473 inline void token::operator=(const floatScalar s)
474 {
475  clear();
476  type_ = FLOAT_SCALAR;
477  floatScalarToken_ = s;
478 }
479 
480 inline void token::operator=(const doubleScalar s)
481 {
482  clear();
483  type_ = DOUBLE_SCALAR;
484  doubleScalarToken_ = s;
485 }
486 
488 {
489  clear();
490  type_ = COMPOUND;
491  compoundTokenPtr_ = cPtr;
492 }
493 
494 
495 inline bool token::operator==(const token& t) const
496 {
497  if (type_ != t.type_)
498  {
499  return false;
500  }
501 
502  switch (type_)
503  {
504  case token::UNDEFINED:
505  return true;
506 
507  case PUNCTUATION:
509 
510  case WORD:
511  return *wordTokenPtr_ == *t.wordTokenPtr_;
512 
513  case STRING:
514  return *stringTokenPtr_ == *t.stringTokenPtr_;
515 
516  case LABEL:
517  return labelToken_ == t.labelToken_;
518 
519  case FLOAT_SCALAR:
521 
522  case DOUBLE_SCALAR:
524 
525  case COMPOUND:
527 
528  case token::ERROR:
529  return true;
530  }
531 
532  return false;
533 }
534 
535 inline bool token::operator==(const punctuationToken p) const
536 {
537  return (type_ == PUNCTUATION && punctuationToken_ == p);
538 }
539 
540 inline bool token::operator==(const word& w) const
541 {
542  return (type_ == WORD && wordToken() == w);
543 }
544 
545 inline bool token::operator==(const string& s) const
546 {
547  return (type_ == STRING && stringToken() == s);
548 }
549 
550 inline bool token::operator==(const label l) const
551 {
552  return (type_ == LABEL && labelToken_ == l);
553 }
554 
555 inline bool token::operator==(const floatScalar s) const
556 {
557  return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
558 }
559 
560 inline bool token::operator==(const doubleScalar s) const
561 {
562  return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
563 }
564 
565 inline bool token::operator!=(const token& t) const
566 {
567  return !operator==(t);
568 }
569 
570 inline bool token::operator!=(const punctuationToken p) const
571 {
572  return !operator==(p);
573 }
574 
575 inline bool token::operator!=(const word& w) const
576 {
577  return !operator==(w);
578 }
579 
580 inline bool token::operator!=(const string& s) const
581 {
582  return !operator==(s);
583 }
584 
585 inline bool token::operator!=(const floatScalar s) const
586 {
587  return !operator==(s);
588 }
589 
590 inline bool token::operator!=(const doubleScalar s) const
591 {
592  return !operator==(s);
593 }
594 
595 inline bool token::operator!=(const label l) const
596 {
597  return !operator==(l);
598 }
599 
600 
601 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
602 
603 } // End namespace Foam
604 
605 // ************************ vim: set sw=4 sts=4 et: ************************ //