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-2019 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_ == FUNCTIONNAME)
37  {
38  delete functionNameTokenPtr_;
39  }
40  else if (type_ == VARIABLE)
41  {
42  delete variableTokenPtr_;
43  }
44  else if (type_ == STRING)
45  {
46  delete stringTokenPtr_;
47  }
48  else if (type_ == VERBATIMSTRING)
49  {
51  }
52  else if (type_ == LONG_DOUBLE_SCALAR)
53  {
55  }
56  else if (type_ == COMPOUND)
57  {
59  {
60  delete compoundTokenPtr_;
61  }
62  else
63  {
64  compoundTokenPtr_->refCount::operator--();
65  }
66  }
67 
68  type_ = UNDEFINED;
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
73 
75 :
76  type_(UNDEFINED),
77  lineNumber_(0)
78 {}
79 
80 
81 inline Foam::token::token(const token& t)
82 :
83  type_(t.type_),
84  lineNumber_(t.lineNumber_)
85 {
86  switch (type_)
87  {
88  case token::UNDEFINED:
89  break;
90 
91  case PUNCTUATION:
93  break;
94 
95  case WORD:
97  break;
98 
99  case FUNCTIONNAME:
101  break;
102 
103  case VARIABLE:
105  break;
106 
107  case STRING:
109  break;
110 
111  case VERBATIMSTRING:
114  break;
115 
116  case LABEL:
118  break;
119 
120  case FLOAT_SCALAR:
122  break;
123 
124  case DOUBLE_SCALAR:
126  break;
127 
128  case LONG_DOUBLE_SCALAR:
131  break;
132 
133  case COMPOUND:
135  compoundTokenPtr_->refCount::operator++();
136  break;
137 
138  case token::ERROR:
139  break;
140  }
141 }
142 
143 
145 :
146  type_(PUNCTUATION),
148  lineNumber_(lineNumber)
149 {}
150 
151 
153 :
154  type_(WORD),
155  wordTokenPtr_(new word(w)),
156  lineNumber_(lineNumber)
157 {}
158 
159 
160 inline Foam::token::token(const string& s, label lineNumber)
161 :
162  type_(STRING),
163  stringTokenPtr_(new string(s)),
164  lineNumber_(lineNumber)
165 {}
166 
167 
169 :
170  type_(VERBATIMSTRING),
172  lineNumber_(lineNumber)
173 {}
174 
175 
177 :
178  type_(LABEL),
179  labelToken_(l),
180  lineNumber_(lineNumber)
181 {}
182 
183 
185 :
186  type_(FLOAT_SCALAR),
188  lineNumber_(lineNumber)
189 {}
190 
191 
193 :
194  type_(DOUBLE_SCALAR),
196  lineNumber_(lineNumber)
197 {}
198 
199 
201 :
202  type_(LONG_DOUBLE_SCALAR),
204  lineNumber_(lineNumber)
205 {}
206 
207 
208 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
209 
211 {
212  clear();
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 
219 {
220  return type_;
221 }
222 
224 {
225  return type_;
226 }
227 
228 inline bool Foam::token::good() const
229 {
230  return (type_ != ERROR && type_ != UNDEFINED);
231 }
232 
233 inline bool Foam::token::undefined() const
234 {
235  return (type_ == UNDEFINED);
236 }
237 
238 inline bool Foam::token::error() const
239 {
240  return (type_ == ERROR);
241 }
242 
243 inline bool Foam::token::isPunctuation() const
244 {
245  return (type_ == PUNCTUATION);
246 }
247 
249 {
250  if (type_ == PUNCTUATION)
251  {
252  return punctuationToken_;
253  }
254  else
255  {
256  parseError("punctuation character");
257  return NULL_TOKEN;
258  }
259 }
260 
261 inline bool Foam::token::isWord() const
262 {
263  return (type_ == WORD);
264 }
265 
266 inline const Foam::word& Foam::token::wordToken() const
267 {
268  if (type_ == WORD)
269  {
270  return *wordTokenPtr_;
271  }
272  else
273  {
274  parseError(word::typeName);
275  return word::null;
276  }
277 }
278 
279 inline bool Foam::token::isFunctionName() const
280 {
281  return (type_ == FUNCTIONNAME);
282 }
283 
285 {
286  if (type_ == FUNCTIONNAME)
287  {
288  return *functionNameTokenPtr_;
289  }
290  else
291  {
292  parseError(functionName::typeName);
293  return functionName::null;
294  }
295 }
296 
297 inline bool Foam::token::isVariable() const
298 {
299  return (type_ == VARIABLE);
300 }
301 
303 {
304  if (type_ == VARIABLE)
305  {
306  return *variableTokenPtr_;
307  }
308  else
309  {
310  parseError(variable::typeName);
311  return variable::null;
312  }
313 }
314 
315 inline bool Foam::token::isString() const
316 {
317  return (type_ == STRING);
318 }
319 
321 {
322  if (type_ == STRING)
323  {
324  return *stringTokenPtr_;
325  }
326  else
327  {
328  parseError(string::typeName);
329  return string::null;
330  }
331 }
332 
333 inline bool Foam::token::isVerbatimString() const
334 {
335  return (type_ == VERBATIMSTRING);
336 }
337 
339 {
340  if (type_ == VERBATIMSTRING)
341  {
342  return *verbatimStringTokenPtr_;
343  }
344  else
345  {
346  parseError(verbatimString::typeName);
347  return verbatimString::null;
348  }
349 }
350 
351 inline bool Foam::token::isAnyString() const
352 {
353  return
354  (
355  type_ == WORD
356  || type_ == FUNCTIONNAME
357  || type_ == VARIABLE
358  || type_ == STRING
359  || type_ == VERBATIMSTRING
360  );
361 }
362 
364 {
365  if (type_ == WORD)
366  {
367  return *wordTokenPtr_;
368  }
369  else if (type_ == FUNCTIONNAME)
370  {
371  return *functionNameTokenPtr_;
372  }
373  else if (type_ == VARIABLE)
374  {
375  return *variableTokenPtr_;
376  }
377  else if (type_ == STRING)
378  {
379  return *stringTokenPtr_;
380  }
381  else if (type_ == VERBATIMSTRING)
382  {
383  return *verbatimStringTokenPtr_;
384  }
385  else
386  {
387  parseError(string::typeName);
388  return string::null;
389  }
390 }
391 
392 inline bool Foam::token::isLabel() const
393 {
394  return (type_ == LABEL);
395 }
396 
398 {
399  if (type_ == LABEL)
400  {
401  return labelToken_;
402  }
403  else
404  {
405  parseError(pTraits<label>::typeName);
406  return 0;
407  }
408 }
409 
410 inline bool Foam::token::isFloatScalar() const
411 {
412  return (type_ == FLOAT_SCALAR);
413 }
414 
416 {
417  if (type_ == FLOAT_SCALAR)
418  {
419  return floatScalarToken_;
420  }
421  else
422  {
423  parseError("floatScalar");
424  return 0.0;
425  }
426 }
427 
428 
429 inline bool Foam::token::isDoubleScalar() const
430 {
431  return (type_ == DOUBLE_SCALAR);
432 }
433 
435 {
436  if (type_ == DOUBLE_SCALAR)
437  {
438  return doubleScalarToken_;
439  }
440  else
441  {
442  parseError("doubleScalar");
443  return 0.0;
444  }
445 }
446 
447 
449 {
450  return (type_ == LONG_DOUBLE_SCALAR);
451 }
452 
454 {
455  if (type_ == LONG_DOUBLE_SCALAR)
456  {
458  }
459  else
460  {
461  parseError("longDoubleScalar");
462  return 0.0;
463  }
464 }
465 
466 
467 inline bool Foam::token::isScalar() const
468 {
469  return
470  (
471  type_ == FLOAT_SCALAR
472  || type_ == DOUBLE_SCALAR
473  || type_ == LONG_DOUBLE_SCALAR
474  );
475 }
476 
477 inline Foam::scalar Foam::token::scalarToken() const
478 {
479  if (type_ == FLOAT_SCALAR)
480  {
481  return floatScalarToken_;
482  }
483  else if (type_ == DOUBLE_SCALAR)
484  {
485  return doubleScalarToken_;
486  }
487  else if (type_ == LONG_DOUBLE_SCALAR)
488  {
490  }
491  else
492  {
493  parseError(pTraits<scalar>::typeName);
494  return 0.0;
495  }
496 }
497 
498 inline bool Foam::token::isNumber() const
499 {
500  return (type_ == LABEL || isScalar());
501 }
502 
503 inline Foam::scalar Foam::token::number() const
504 {
505  if (type_ == LABEL)
506  {
507  return labelToken_;
508  }
509  else if (isScalar())
510  {
511  return scalarToken();
512  }
513  else
514  {
515  parseError("number (label or scalar)");
516  return 0.0;
517  }
518 }
519 
520 inline bool Foam::token::isCompound() const
521 {
522  return (type_ == COMPOUND);
523 }
524 
526 {
527  if (type_ == COMPOUND)
528  {
529  return *compoundTokenPtr_;
530  }
531  else
532  {
533  parseError("compound");
534  return *compoundTokenPtr_;
535  }
536 }
537 
538 
540 {
541  return lineNumber_;
542 }
543 
545 {
546  return lineNumber_;
547 }
548 
549 
550 inline void Foam::token::setBad()
551 {
552  clear();
553  type_ = ERROR;
554 }
555 
556 
557 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
558 
559 inline void Foam::token::operator=(const token& t)
560 {
561  clear();
562  type_ = t.type_;
563 
564  switch (type_)
565  {
566  case token::UNDEFINED:
567  break;
568 
569  case PUNCTUATION:
571  break;
572 
573  case WORD:
574  wordTokenPtr_ = new word(*t.wordTokenPtr_);
575  break;
576 
577  case FUNCTIONNAME:
579  break;
580 
581  case VARIABLE:
583  break;
584 
585  case STRING:
587  break;
588 
589  case VERBATIMSTRING:
592  break;
593 
594  case LABEL:
596  break;
597 
598  case FLOAT_SCALAR:
600  break;
601 
602  case DOUBLE_SCALAR:
604  break;
605 
606  case LONG_DOUBLE_SCALAR:
609  break;
610 
611  case COMPOUND:
613  compoundTokenPtr_->refCount::operator++();
614  break;
615 
616  case token::ERROR:
617  break;
618  }
619 
620  lineNumber_ = t.lineNumber_;
621 }
622 
624 {
625  clear();
626  type_ = PUNCTUATION;
628 }
629 
630 inline void Foam::token::operator=(word* wPtr)
631 {
632  clear();
633  type_ = WORD;
634  wordTokenPtr_ = wPtr;
635 }
636 
637 inline void Foam::token::operator=(const word& w)
638 {
639  operator=(new word(w));
640 }
641 
643 {
644  clear();
645  type_ = FUNCTIONNAME;
646  functionNameTokenPtr_ = fnPtr;
647 }
648 
649 inline void Foam::token::operator=(const functionName& fn)
650 {
651  operator=(new functionName(fn));
652 }
653 
655 {
656  clear();
657  type_ = VARIABLE;
658  variableTokenPtr_ = vPtr;
659 }
660 
661 inline void Foam::token::operator=(const variable& v)
662 {
663  operator=(new variable(v));
664 }
665 
666 inline void Foam::token::operator=(string* sPtr)
667 {
668  clear();
669  type_ = STRING;
670  stringTokenPtr_ = sPtr;
671 }
672 
673 inline void Foam::token::operator=(const string& s)
674 {
675  operator=(new string(s));
676 }
677 
679 {
680  clear();
681  type_ = VERBATIMSTRING;
682  verbatimStringTokenPtr_ = vsPtr;
683 }
684 
686 {
687  operator=(new verbatimString(vs));
688 }
689 
690 inline void Foam::token::operator=(const label l)
691 {
692  clear();
693  type_ = LABEL;
694  labelToken_ = l;
695 }
696 
698 {
699  clear();
700  type_ = FLOAT_SCALAR;
702 }
703 
705 {
706  clear();
707  type_ = DOUBLE_SCALAR;
709 }
710 
712 {
713  clear();
714  type_ = LONG_DOUBLE_SCALAR;
716 }
717 
719 {
720  clear();
721  type_ = COMPOUND;
722  compoundTokenPtr_ = cPtr;
723 }
724 
725 
726 inline bool Foam::token::operator==(const token& t) const
727 {
728  if (type_ != t.type_)
729  {
730  return false;
731  }
732 
733  switch (type_)
734  {
735  case token::UNDEFINED:
736  return true;
737 
738  case PUNCTUATION:
740 
741  case WORD:
742  return *wordTokenPtr_ == *t.wordTokenPtr_;
743 
744  case FUNCTIONNAME:
746 
747  case VARIABLE:
748  return *variableTokenPtr_ == *t.variableTokenPtr_;
749 
750  case STRING:
751  return *stringTokenPtr_ == *t.stringTokenPtr_;
752 
753  case VERBATIMSTRING:
755 
756  case LABEL:
757  return labelToken_ == t.labelToken_;
758 
759  case FLOAT_SCALAR:
761 
762  case DOUBLE_SCALAR:
764 
765  case LONG_DOUBLE_SCALAR:
766  return equal
767  (
770  );
771 
772  case COMPOUND:
774 
775  case token::ERROR:
776  return true;
777  }
778 
779  return false;
780 }
781 
783 {
784  return (type_ == PUNCTUATION && punctuationToken_ == p);
785 }
786 
787 inline bool Foam::token::operator==(const word& w) const
788 {
789  return (type_ == WORD && wordToken() == w);
790 }
791 
792 inline bool Foam::token::operator==(const functionName& fn) const
793 {
794  return (type_ == FUNCTIONNAME && functionNameToken() == fn);
795 }
796 
797 inline bool Foam::token::operator==(const variable& v) const
798 {
799  return (type_ == VARIABLE && variableToken() == v);
800 }
801 
802 inline bool Foam::token::operator==(const string& s) const
803 {
804  return (type_ == STRING && stringToken() == s);
805 }
806 
807 inline bool Foam::token::operator==(const verbatimString& vs) const
808 {
809  return (type_ == VERBATIMSTRING && verbatimStringToken() == vs);
810 }
811 
812 inline bool Foam::token::operator==(const label l) const
813 {
814  return (type_ == LABEL && labelToken_ == l);
815 }
816 
817 inline bool Foam::token::operator==(const floatScalar s) const
818 {
819  return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
820 }
821 
822 inline bool Foam::token::operator==(const doubleScalar s) const
823 {
824  return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
825 }
826 
828 {
829  return
830  (
832  );
833 }
834 
835 inline bool Foam::token::operator!=(const token& t) const
836 {
837  return !operator==(t);
838 }
839 
841 {
842  return !operator==(p);
843 }
844 
845 inline bool Foam::token::operator!=(const word& w) const
846 {
847  return !operator==(w);
848 }
849 
850 inline bool Foam::token::operator!=(const functionName& fn) const
851 {
852  return !operator==(fn);
853 }
854 
855 inline bool Foam::token::operator!=(const variable& v) const
856 {
857  return !operator==(v);
858 }
859 
860 inline bool Foam::token::operator!=(const string& s) const
861 {
862  return !operator==(s);
863 }
864 
865 inline bool Foam::token::operator!=(const verbatimString& vs) const
866 {
867  return !operator==(vs);
868 }
869 
870 inline bool Foam::token::operator!=(const floatScalar s) const
871 {
872  return !operator==(s);
873 }
874 
875 inline bool Foam::token::operator!=(const doubleScalar s) const
876 {
877  return !operator==(s);
878 }
879 
881 {
882  return !operator==(s);
883 }
884 
885 inline bool Foam::token::operator!=(const label l) const
886 {
887  return !operator==(l);
888 }
889 
890 
891 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
A class for handling verbatimStrings, derived from string.
const variable & variableToken() const
Definition: tokenI.H:302
doubleScalar doubleScalarToken_
Definition: token.H:258
const functionName & functionNameToken() const
Definition: tokenI.H:284
~token()
Destructor.
Definition: tokenI.H:210
bool isWord() const
Definition: tokenI.H:261
punctuationToken pToken() const
Definition: tokenI.H:248
bool operator!=(const token &) const
Definition: tokenI.H:835
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:448
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:259
compound * compoundTokenPtr_
Definition: token.H:260
static const char *const typeName
scalar number() const
Definition: tokenI.H:503
label labelToken_
Definition: token.H:256
const word & wordToken() const
Definition: tokenI.H:266
A token holds items read from Istream.
Definition: token.H:72
tokenType
Enumeration defining the types of token.
Definition: token.H:78
string * stringTokenPtr_
Definition: token.H:254
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:338
Traits class for primitives.
Definition: pTraits.H:50
bool operator==(const token &) const
Definition: tokenI.H:726
scalar scalarToken() const
Definition: tokenI.H:477
bool isVerbatimString() const
Definition: tokenI.H:333
bool unique() const
Return true if the reference count is zero.
Definition: refCount.H:87
functionName * functionNameTokenPtr_
Definition: token.H:252
word * wordTokenPtr_
Definition: token.H:251
bool isNumber() const
Definition: tokenI.H:498
bool isCompound() const
Definition: tokenI.H:520
Abstract base class for complex tokens.
Definition: token.H:127
static const char *const typeName
Definition: string.H:84
static const verbatimString null
An empty verbatimString.
void operator=(const token &)
Definition: tokenI.H:559
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:415
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
A class for handling words, derived from string.
Definition: word.H:59
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
verbatimString * verbatimStringTokenPtr_
Definition: token.H:255
void setBad()
Set bad.
Definition: tokenI.H:550
static const functionName null
An empty functionName.
Definition: functionName.H:70
static const char *const typeName
Definition: functionName.H:66
punctuationToken
Standard punctuation tokens.
Definition: token.H:98
static const word null
An empty word.
Definition: word.H:77
bool good() const
Definition: tokenI.H:228
bool isScalar() const
Definition: tokenI.H:467
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
label lineNumber() const
Definition: tokenI.H:539
const compound & compoundToken() const
Definition: tokenI.H:525
bool isVariable() const
Definition: tokenI.H:297
const string & anyStringToken() const
Definition: tokenI.H:363
bool undefined() const
Definition: tokenI.H:233
static const string null
An empty string.
Definition: string.H:88
static const variable null
An empty variable.
Definition: variable.H:76
bool isFloatScalar() const
Definition: tokenI.H:410
bool isDoubleScalar() const
Definition: tokenI.H:429
const string & stringToken() const
Definition: tokenI.H:320
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:453
bool isString() const
Definition: tokenI.H:315
long double longDoubleScalar
Lang double precision floating point scalar type.
bool error() const
Definition: tokenI.H:238
bool isAnyString() const
Definition: tokenI.H:351
bool equal(const T &s1, const T &s2)
Definition: doubleFloat.H:62
floatScalar floatScalarToken_
Definition: token.H:257
tokenType type() const
Definition: tokenI.H:218
punctuationToken punctuationToken_
Definition: token.H:250
label labelToken() const
Definition: tokenI.H:397
token()
Construct null.
Definition: tokenI.H:74
bool isFunctionName() const
Definition: tokenI.H:279
variable * variableTokenPtr_
Definition: token.H:253
volScalarField & p
static const char *const typeName
Definition: word.H:73
doubleScalar doubleScalarToken() const
Definition: tokenI.H:434
A class for handling character strings derived from std::string.
Definition: string.H:76
A variable is a word with support for additional characters, in particular &#39;$&#39; and &#39;/&#39;...
Definition: variable.H:58
bool isPunctuation() const
Definition: tokenI.H:243
static const char *const typeName
Definition: variable.H:72