tokenI.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 \*---------------------------------------------------------------------------*/
25 
26 #include <iostream>
27 
28 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
29 
30 inline void Foam::token::clear()
31 {
32  if (type_ == WORD)
33  {
34  delete wordTokenPtr_;
35  }
36  else if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
37  {
38  delete stringTokenPtr_;
39  }
40  else if (type_ == LONG_DOUBLE_SCALAR)
41  {
43  }
44  else if (type_ == COMPOUND)
45  {
47  {
48  delete compoundTokenPtr_;
49  }
50  else
51  {
52  compoundTokenPtr_->refCount::operator--();
53  }
54  }
55 
56  type_ = UNDEFINED;
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
63 :
64  type_(UNDEFINED),
65  lineNumber_(0)
66 {}
67 
68 
69 inline Foam::token::token(const token& t)
70 :
71  type_(t.type_),
72  lineNumber_(t.lineNumber_)
73 {
74  switch (type_)
75  {
76  case token::UNDEFINED:
77  break;
78 
79  case PUNCTUATION:
81  break;
82 
83  case WORD:
85  break;
86 
87  case STRING:
88  case VARIABLE:
89  case VERBATIMSTRING:
91  break;
92 
93  case LABEL:
95  break;
96 
97  case FLOAT_SCALAR:
99  break;
100 
101  case DOUBLE_SCALAR:
103  break;
104 
105  case LONG_DOUBLE_SCALAR:
108  break;
109 
110  case COMPOUND:
112  compoundTokenPtr_->refCount::operator++();
113  break;
114 
115  case token::ERROR:
116  break;
117  }
118 }
119 
120 
122 :
123  type_(PUNCTUATION),
125  lineNumber_(lineNumber)
126 {}
127 
128 
130 :
131  type_(WORD),
132  wordTokenPtr_(new word(w)),
133  lineNumber_(lineNumber)
134 {}
135 
136 
137 inline Foam::token::token(const string& s, label lineNumber)
138 :
139  type_(STRING),
140  stringTokenPtr_(new string(s)),
141  lineNumber_(lineNumber)
142 {}
143 
144 
146 :
147  type_(LABEL),
148  labelToken_(l),
149  lineNumber_(lineNumber)
150 {}
151 
152 
154 :
155  type_(FLOAT_SCALAR),
157  lineNumber_(lineNumber)
158 {}
159 
160 
162 :
163  type_(DOUBLE_SCALAR),
165  lineNumber_(lineNumber)
166 {}
167 
168 
170 :
171  type_(LONG_DOUBLE_SCALAR),
173  lineNumber_(lineNumber)
174 {}
175 
176 
177 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
178 
180 {
181  clear();
182 }
183 
184 
185 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
186 
188 {
189  return type_;
190 }
191 
193 {
194  return type_;
195 }
196 
197 inline bool Foam::token::good() const
198 {
199  return (type_ != ERROR && type_ != UNDEFINED);
200 }
201 
202 inline bool Foam::token::undefined() const
203 {
204  return (type_ == UNDEFINED);
205 }
206 
207 inline bool Foam::token::error() const
208 {
209  return (type_ == ERROR);
210 }
211 
212 inline bool Foam::token::isPunctuation() const
213 {
214  return (type_ == PUNCTUATION);
215 }
216 
218 {
219  if (type_ == PUNCTUATION)
220  {
221  return punctuationToken_;
222  }
223  else
224  {
225  parseError("punctuation character");
226  return NULL_TOKEN;
227  }
228 }
229 
230 inline bool Foam::token::isWord() const
231 {
232  return (type_ == WORD);
233 }
234 
235 inline const Foam::word& Foam::token::wordToken() const
236 {
237  if (type_ == WORD)
238  {
239  return *wordTokenPtr_;
240  }
241  else
242  {
243  parseError(word::typeName);
244  return word::null;
245  }
246 }
247 
248 inline bool Foam::token::isVariable() const
249 {
250  return (type_ == VARIABLE);
251 }
252 
253 inline bool Foam::token::isString() const
254 {
255  return (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING);
256 }
257 
259 {
260  if (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
261  {
262  return *stringTokenPtr_;
263  }
264  else
265  {
266  parseError(string::typeName);
267  return string::null;
268  }
269 }
270 
271 inline bool Foam::token::isLabel() const
272 {
273  return (type_ == LABEL);
274 }
275 
277 {
278  if (type_ == LABEL)
279  {
280  return labelToken_;
281  }
282  else
283  {
284  parseError(pTraits<label>::typeName);
285  return 0;
286  }
287 }
288 
289 inline bool Foam::token::isFloatScalar() const
290 {
291  return (type_ == FLOAT_SCALAR);
292 }
293 
295 {
296  if (type_ == FLOAT_SCALAR)
297  {
298  return floatScalarToken_;
299  }
300  else
301  {
302  parseError("floatScalar");
303  return 0.0;
304  }
305 }
306 
307 
308 inline bool Foam::token::isDoubleScalar() const
309 {
310  return (type_ == DOUBLE_SCALAR);
311 }
312 
314 {
315  if (type_ == DOUBLE_SCALAR)
316  {
317  return doubleScalarToken_;
318  }
319  else
320  {
321  parseError("doubleScalar");
322  return 0.0;
323  }
324 }
325 
326 
328 {
329  return (type_ == LONG_DOUBLE_SCALAR);
330 }
331 
333 {
334  if (type_ == LONG_DOUBLE_SCALAR)
335  {
337  }
338  else
339  {
340  parseError("longDoubleScalar");
341  return 0.0;
342  }
343 }
344 
345 
346 inline bool Foam::token::isScalar() const
347 {
348  return
349  (
350  type_ == FLOAT_SCALAR
351  || type_ == DOUBLE_SCALAR
352  || type_ == LONG_DOUBLE_SCALAR
353  );
354 }
355 
356 inline Foam::scalar Foam::token::scalarToken() const
357 {
358  if (type_ == FLOAT_SCALAR)
359  {
360  return floatScalarToken_;
361  }
362  else if (type_ == DOUBLE_SCALAR)
363  {
364  return doubleScalarToken_;
365  }
366  else if (type_ == LONG_DOUBLE_SCALAR)
367  {
369  }
370  else
371  {
372  parseError(pTraits<scalar>::typeName);
373  return 0.0;
374  }
375 }
376 
377 inline bool Foam::token::isNumber() const
378 {
379  return (type_ == LABEL || isScalar());
380 }
381 
382 inline Foam::scalar Foam::token::number() const
383 {
384  if (type_ == LABEL)
385  {
386  return labelToken_;
387  }
388  else if (isScalar())
389  {
390  return scalarToken();
391  }
392  else
393  {
394  parseError("number (label or scalar)");
395  return 0.0;
396  }
397 }
398 
399 inline bool Foam::token::isCompound() const
400 {
401  return (type_ == COMPOUND);
402 }
403 
405 {
406  if (type_ == COMPOUND)
407  {
408  return *compoundTokenPtr_;
409  }
410  else
411  {
412  parseError("compound");
413  return *compoundTokenPtr_;
414  }
415 }
416 
417 
419 {
420  return lineNumber_;
421 }
422 
424 {
425  return lineNumber_;
426 }
427 
428 
429 inline void Foam::token::setBad()
430 {
431  clear();
432  type_ = ERROR;
433 }
434 
435 
436 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
437 
438 inline void Foam::token::operator=(const token& t)
439 {
440  clear();
441  type_ = t.type_;
442 
443  switch (type_)
444  {
445  case token::UNDEFINED:
446  break;
447 
448  case PUNCTUATION:
450  break;
451 
452  case WORD:
453  wordTokenPtr_ = new word(*t.wordTokenPtr_);
454  break;
455 
456  case STRING:
457  case VARIABLE:
458  case VERBATIMSTRING:
460  break;
461 
462  case LABEL:
464  break;
465 
466  case FLOAT_SCALAR:
468  break;
469 
470  case DOUBLE_SCALAR:
472  break;
473 
474  case LONG_DOUBLE_SCALAR:
477  break;
478 
479  case COMPOUND:
481  compoundTokenPtr_->refCount::operator++();
482  break;
483 
484  case token::ERROR:
485  break;
486  }
487 
488  lineNumber_ = t.lineNumber_;
489 }
490 
492 {
493  clear();
494  type_ = PUNCTUATION;
496 }
497 
498 inline void Foam::token::operator=(word* wPtr)
499 {
500  clear();
501  type_ = WORD;
502  wordTokenPtr_ = wPtr;
503 }
504 
505 inline void Foam::token::operator=(const word& w)
506 {
507  operator=(new word(w));
508 }
509 
510 inline void Foam::token::operator=(string* sPtr)
511 {
512  clear();
513  type_ = STRING;
514  stringTokenPtr_ = sPtr;
515 }
516 
517 inline void Foam::token::operator=(const string& s)
518 {
519  operator=(new string(s));
520 }
521 
522 inline void Foam::token::operator=(const label l)
523 {
524  clear();
525  type_ = LABEL;
526  labelToken_ = l;
527 }
528 
530 {
531  clear();
532  type_ = FLOAT_SCALAR;
534 }
535 
537 {
538  clear();
539  type_ = DOUBLE_SCALAR;
541 }
542 
544 {
545  clear();
546  type_ = LONG_DOUBLE_SCALAR;
548 }
549 
551 {
552  clear();
553  type_ = COMPOUND;
554  compoundTokenPtr_ = cPtr;
555 }
556 
557 
558 inline bool Foam::token::operator==(const token& t) const
559 {
560  if (type_ != t.type_)
561  {
562  return false;
563  }
564 
565  switch (type_)
566  {
567  case token::UNDEFINED:
568  return true;
569 
570  case PUNCTUATION:
572 
573  case WORD:
574  return *wordTokenPtr_ == *t.wordTokenPtr_;
575 
576  case STRING:
577  case VARIABLE:
578  case VERBATIMSTRING:
579  return *stringTokenPtr_ == *t.stringTokenPtr_;
580 
581  case LABEL:
582  return labelToken_ == t.labelToken_;
583 
584  case FLOAT_SCALAR:
586 
587  case DOUBLE_SCALAR:
589 
590  case LONG_DOUBLE_SCALAR:
591  return equal
592  (
595  );
596 
597  case COMPOUND:
599 
600  case token::ERROR:
601  return true;
602  }
603 
604  return false;
605 }
606 
608 {
609  return (type_ == PUNCTUATION && punctuationToken_ == p);
610 }
611 
612 inline bool Foam::token::operator==(const word& w) const
613 {
614  return (type_ == WORD && wordToken() == w);
615 }
616 
617 inline bool Foam::token::operator==(const string& s) const
618 {
619  return
620  (
621  (type_ == STRING || type_ == VARIABLE || type_ == VERBATIMSTRING)
622  && stringToken() == s
623  );
624 }
625 
626 inline bool Foam::token::operator==(const label l) const
627 {
628  return (type_ == LABEL && labelToken_ == l);
629 }
630 
631 inline bool Foam::token::operator==(const floatScalar s) const
632 {
633  return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
634 }
635 
636 inline bool Foam::token::operator==(const doubleScalar s) const
637 {
638  return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
639 }
640 
642 {
643  return
644  (
646  );
647 }
648 
649 inline bool Foam::token::operator!=(const token& t) const
650 {
651  return !operator==(t);
652 }
653 
655 {
656  return !operator==(p);
657 }
658 
659 inline bool Foam::token::operator!=(const word& w) const
660 {
661  return !operator==(w);
662 }
663 
664 inline bool Foam::token::operator!=(const string& s) const
665 {
666  return !operator==(s);
667 }
668 
669 inline bool Foam::token::operator!=(const floatScalar s) const
670 {
671  return !operator==(s);
672 }
673 
674 inline bool Foam::token::operator!=(const doubleScalar s) const
675 {
676  return !operator==(s);
677 }
678 
680 {
681  return !operator==(s);
682 }
683 
684 inline bool Foam::token::operator!=(const label l) const
685 {
686  return !operator==(l);
687 }
688 
689 
690 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
doubleScalar doubleScalarToken_
Definition: token.H:251
~token()
Destructor.
Definition: tokenI.H:179
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
bool isLongDoubleScalar() const
Definition: tokenI.H:327
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:252
compound * compoundTokenPtr_
Definition: token.H:253
scalar number() const
Definition: tokenI.H:382
label labelToken_
Definition: token.H:249
const word & wordToken() const
Definition: tokenI.H:235
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:248
Traits class for primitives.
Definition: pTraits.H:50
bool operator==(const token &) const
Definition: tokenI.H:558
scalar scalarToken() const
Definition: tokenI.H:356
bool unique() const
Return true if the reference count is zero.
Definition: refCount.H:87
word * wordTokenPtr_
Definition: token.H:247
bool isNumber() const
Definition: tokenI.H:377
bool isCompound() const
Definition: tokenI.H:399
Abstract base class for complex tokens.
Definition: token.H:123
static const char *const typeName
Definition: string.H:82
void operator=(const token &)
Definition: tokenI.H:438
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
floatScalar floatScalarToken() const
Definition: tokenI.H:294
A class for handling words, derived from string.
Definition: word.H:59
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
static const word null
An empty word.
Definition: word.H:77
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
static const string null
An empty string.
Definition: string.H:86
bool isFloatScalar() const
Definition: tokenI.H:289
bool isDoubleScalar() const
Definition: tokenI.H:308
const string & stringToken() const
Definition: tokenI.H:258
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:332
bool isString() const
Definition: tokenI.H:253
long double longDoubleScalar
Lang double precision floating point scalar type.
bool error() const
Definition: tokenI.H:207
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
floatScalar floatScalarToken_
Definition: token.H:250
tokenType type() const
Definition: tokenI.H:187
punctuationToken punctuationToken_
Definition: token.H:246
label labelToken() const
Definition: tokenI.H:276
token()
Construct null.
Definition: tokenI.H:62
volScalarField & p
static const char *const typeName
Definition: word.H:73
doubleScalar doubleScalarToken() const
Definition: tokenI.H:313
A class for handling character strings derived from std::string.
Definition: string.H:74
bool isPunctuation() const
Definition: tokenI.H:212