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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <utility> // For std::move, std::swap
30 
31 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
32 
34 {
35  token tok;
36  tok.type_ = tokenType::BOOL;
37  tok.data_.labelVal = on;
38 
39  return tok;
40 }
41 
42 
43 inline Foam::token Foam::token::flag(int bitmask) noexcept
44 {
45  token tok;
46  tok.type_ = tokenType::FLAG;
47  tok.data_.flagVal = bitmask;
48 
49  return tok;
50 }
51 
52 
53 inline bool Foam::token::is_wordToken(tokenType tokType) noexcept
54 {
55  return
56  (
57  tokType == tokenType::WORD
58  || tokType == tokenType::DIRECTIVE
59  );
60 }
61 
62 
63 inline bool Foam::token::is_stringToken(tokenType tokType) noexcept
64 {
65  return
66  (
67  tokType == tokenType::STRING
68  || tokType == tokenType::EXPRESSION
69  || tokType == tokenType::VARIABLE
70  || tokType == tokenType::VERBATIM
71  || tokType == tokenType::CHAR_DATA
72  );
73 }
74 
75 
76 inline bool Foam::token::isseparator(int c) noexcept
77 {
78  // NOTE: keep synchronized with ISstream::read(token&)
79 
80  switch (c)
81  {
82  case token::END_STATEMENT :
83  case token::BEGIN_LIST :
84  case token::END_LIST :
85  case token::BEGIN_SQR :
86  case token::END_SQR :
87  case token::BEGIN_BLOCK :
88  case token::END_BLOCK :
89  case token::COLON :
90  case token::COMMA :
91  case token::ASSIGN :
92  case token::PLUS :
93  // Excluded token::MINUS since it could start a number
94  case token::MULTIPLY :
95  case token::DIVIDE :
96  {
97  return true;
98  }
99 
100  default:
101  break;
102  }
103 
104  return false;
105 }
106 
107 
108 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
109 
110 inline void Foam::token::setUndefined() noexcept
111 {
112  type_ = tokenType::UNDEFINED;
113  data_.int64Val = 0; // bit-wise zero for union content
114  // leave lineNumber untouched - may still be needed
115 }
116 
117 
118 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 
120 inline constexpr Foam::token::token() noexcept
121 :
122  data_(), // bit-wise zero for union content
123  type_(tokenType::UNDEFINED),
124  line_(0)
125 {}
126 
127 
128 inline Foam::token::token(const token& tok)
129 :
130  data_(tok.data_), // bit-wise copy of union content
131  type_(tok.type_),
132  line_(tok.line_)
133 {
134  // Fundamental: values already handled by bit-wise copy
135  // Pointer: duplicate content or increase refCount
136 
137  switch (type_)
138  {
139  // token::isWord()
140  case tokenType::WORD:
141  case tokenType::DIRECTIVE:
142  {
143  data_.wordPtr = new word(*tok.data_.wordPtr);
144  break;
145  }
146 
147  // token::isString()
148  case tokenType::STRING:
149  case tokenType::EXPRESSION:
150  case tokenType::VARIABLE:
151  case tokenType::VERBATIM:
152  case tokenType::CHAR_DATA:
153  {
154  data_.stringPtr = new string(*tok.data_.stringPtr);
155  break;
156  }
157 
158  case tokenType::COMPOUND:
159  {
160  // Identical pointers, but increase the refCount
161  data_.compoundPtr = tok.data_.compoundPtr;
162  data_.compoundPtr->refCount::operator++();
163  break;
164  }
166  default:
167  break;
168  }
169 }
170 
171 
172 inline Foam::token::token(token&& tok) noexcept
173 :
174  data_(tok.data_), // bit-wise copy of union content
175  type_(tok.type_),
176  line_(tok.line_)
177 {
178  tok.setUndefined(); // zero the union content without any checking
179  tok.line_ = 0;
180 }
181 
182 
183 inline Foam::token::token(punctuationToken p, label lineNum) noexcept
184 :
185  data_(),
186  type_(tokenType::PUNCTUATION),
187  line_(lineNum)
188 {
189  data_.punctuationVal = p;
190 }
191 
192 
193 inline Foam::token::token(const label val, label lineNum) noexcept
194 :
195  data_(),
196  type_(tokenType::LABEL),
197  line_(lineNum)
198 {
199  data_.labelVal = val;
200 }
201 
202 
203 inline Foam::token::token(const float val, label lineNum) noexcept
204 :
205  data_(),
206  type_(tokenType::FLOAT),
207  line_(lineNum)
208 {
209  data_.floatVal = val;
210 }
211 
212 
213 inline Foam::token::token(const double val, label lineNum) noexcept
214 :
215  data_(),
216  type_(tokenType::DOUBLE),
217  line_(lineNum)
218 {
219  data_.doubleVal = val;
220 }
221 
222 
223 inline Foam::token::token(const word& w, label lineNum)
224 :
225  data_(),
226  type_(tokenType::WORD),
227  line_(lineNum)
228 {
229  data_.wordPtr = new word(w);
230 }
231 
232 
233 inline Foam::token::token(const string& str, label lineNum)
234 :
235  data_(),
236  type_(tokenType::STRING),
237  line_(lineNum)
238 {
239  data_.stringPtr = new string(str);
240 }
241 
242 
243 inline Foam::token::token(word&& w, label lineNum)
244 :
245  data_(),
246  type_(tokenType::WORD),
247  line_(lineNum)
248 {
249  data_.wordPtr = new word(std::move(w));
250 }
251 
252 
253 inline Foam::token::token(string&& str, label lineNum)
254 :
255  data_(),
256  type_(tokenType::STRING),
257  line_(lineNum)
258 {
259  data_.stringPtr = new string(std::move(str));
260 }
261 
262 
263 inline Foam::token::token
264 (
265  tokenType tokType,
266  const std::string& str,
267  label lineNum
268 )
269 :
270  data_(),
271  type_(tokenType::STRING),
272  line_(lineNum)
273 {
274  if (is_wordToken(tokType))
275  {
276  type_ = tokType;
277  data_.wordPtr = new word(str, false); // no stripping
278  }
279  else
280  {
281  if (is_stringToken(tokType)) type_ = tokType;
282  data_.stringPtr = new string(str); // never strips
283  }
284 }
285 
286 
287 inline Foam::token::token
288 (
289  tokenType tokType,
290  std::string&& str,
291  label lineNum
292 )
293 :
294  data_(),
295  type_(tokenType::STRING),
296  line_(lineNum)
297 {
298  if (is_wordToken(tokType))
299  {
300  type_ = tokType;
301  data_.wordPtr = new word(std::move(str), false); // no stripping
302  }
303  else
304  {
305  if (is_stringToken(tokType)) type_ = tokType;
306  data_.stringPtr = new string(std::move(str)); // never strips
307  }
308 }
309 
310 
311 inline Foam::token::token(token::compound* ptr, label lineNum)
312 :
313  data_(),
314  type_(tokenType::COMPOUND),
315  line_(lineNum)
316 {
317  data_.compoundPtr = ptr;
318  if (!data_.compoundPtr)
319  {
320  // Could handle nullptr as Fatal, but this is simpler
321  setUndefined();
322  }
323 }
324 
325 
326 inline Foam::token::token(autoPtr<token::compound>&& ptr, label lineNum)
327 :
328  token(ptr.release(), lineNum)
329 {}
330 
331 
332 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
333 
334 inline Foam::token::~token()
335 {
336  reset();
337 }
338 
339 
340 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
341 
342 inline void Foam::token::reset()
343 {
344  switch (type_)
345  {
346  // token::isWord()
347  case tokenType::WORD:
348  case tokenType::DIRECTIVE:
349  {
350  delete data_.wordPtr;
351  break;
352  }
353 
354  // token::isString()
355  case tokenType::STRING:
356  case tokenType::EXPRESSION:
357  case tokenType::VARIABLE:
358  case tokenType::VERBATIM:
359  case tokenType::CHAR_DATA:
360  {
361  delete data_.stringPtr;
362  break;
363  }
364 
365  case tokenType::COMPOUND:
366  {
367  if (data_.compoundPtr->refCount::unique())
368  {
369  delete data_.compoundPtr;
370  }
371  else
372  {
373  data_.compoundPtr->refCount::operator--();
374  }
375  break;
376  }
377 
378  default:
379  break;
380  }
381 
382  setUndefined();
383 }
384 
385 
386 inline void Foam::token::setBad()
387 {
388  reset();
389  type_ = tokenType::ERROR;
390 }
391 
392 
393 inline void Foam::token::swap(token& tok) noexcept
394 {
395  if (this == &tok)
396  {
397  return; // Self-swap is a no-op
398  }
400  std::swap(data_, tok.data_);
401  std::swap(type_, tok.type_);
402  std::swap(line_, tok.line_);
403 }
404 
406 inline Foam::word Foam::token::name() const
407 {
408  return token::name(type_);
409 }
410 
413 {
414  return type_;
415 }
416 
417 
419 {
420  if (type_ == tokType)
421  {
422  // No change required
423  return true;
424  }
425 
426  switch (tokType)
427  {
428  case tokenType::BOOL:
429  case tokenType::LABEL:
430  {
431  switch (type_)
432  {
433  case tokenType::BOOL:
434  case tokenType::LABEL:
435  type_ = tokType;
436  return true;
437  break;
438 
439  default:
440  break;
441  }
442  }
443  break;
444 
445  // token::isWord()
446  case tokenType::WORD:
447  case tokenType::DIRECTIVE:
448  {
449  switch (type_)
450  {
451  // token::isWord()
452  case tokenType::WORD:
453  case tokenType::DIRECTIVE:
454  type_ = tokType;
455  return true;
456  break;
457 
458  default:
459  break;
460  }
461  }
462  break;
463 
464  // token::isString()
465  case tokenType::STRING:
466  case tokenType::EXPRESSION:
467  case tokenType::VARIABLE:
468  case tokenType::VERBATIM:
469  case tokenType::CHAR_DATA:
470  {
471  switch (type_)
472  {
473  // token::isWord()
474  // could also go from WORD to STRING etc - to be decided
475 
476  // token::isString()
477  case tokenType::STRING:
478  case tokenType::EXPRESSION:
479  case tokenType::VARIABLE:
480  case tokenType::VERBATIM:
481  case tokenType::CHAR_DATA:
482  type_ = tokType;
483  return true;
484  break;
485 
486  default:
487  break;
488  }
489  }
490  break;
491 
492  default:
493  break;
494  }
495 
496  return false;
497 }
498 
500 inline Foam::label Foam::token::lineNumber() const noexcept
501 {
502  return line_;
503 }
504 
505 
506 inline Foam::label Foam::token::lineNumber(const label lineNum) noexcept
507 {
508  label old(line_);
509  line_ = lineNum;
510  return old;
511 }
512 
514 inline bool Foam::token::good() const noexcept
515 {
516  return (type_ != tokenType::UNDEFINED && type_ != tokenType::ERROR);
517 }
518 
520 inline bool Foam::token::undefined() const noexcept
521 {
522  return (type_ == tokenType::UNDEFINED);
523 }
524 
526 inline bool Foam::token::error() const noexcept
527 {
528  return (type_ == tokenType::ERROR);
529 }
530 
532 inline bool Foam::token::isBool() const noexcept
533 {
534  return (type_ == tokenType::BOOL);
535 }
536 
537 
538 inline bool Foam::token::boolToken() const
539 {
540  if (type_ == tokenType::BOOL || type_ == tokenType::LABEL)
541  {
542  return data_.labelVal;
543  }
544 
545  parseError("bool");
546  return false;
547 }
548 
550 inline bool Foam::token::isFlag() const noexcept
551 {
552  return (type_ == tokenType::FLAG);
553 }
554 
555 
556 inline int Foam::token::flagToken() const
557 {
558  if (type_ == tokenType::FLAG)
559  {
560  return data_.flagVal;
561  }
562 
563  parseError("flag bitmask");
564  return flagType::NO_FLAG;
565 }
566 
568 inline bool Foam::token::isPunctuation() const noexcept
569 {
570  return (type_ == tokenType::PUNCTUATION);
571 }
572 
573 
574 inline bool Foam::token::isPunctuation(const punctuationToken p) const noexcept
575 {
576  return
577  (
578  type_ == tokenType::PUNCTUATION
579  && data_.punctuationVal == p
580  );
581 }
582 
583 
584 inline bool Foam::token::isSeparator() const noexcept
585 {
586  return
587  (
588  type_ == tokenType::PUNCTUATION
589  && isseparator(data_.punctuationVal)
590  );
591 }
592 
593 
595 {
596  if (type_ == tokenType::PUNCTUATION)
597  {
598  return data_.punctuationVal;
599  }
600 
601  parseError("punctuation character");
602  return punctuationToken::NULL_TOKEN;
603 }
604 
606 inline bool Foam::token::isLabel() const noexcept
607 {
608  return (type_ == tokenType::LABEL);
609 }
610 
611 
612 inline bool Foam::token::isLabel(const label val) const noexcept
613 {
614  return
615  (
616  type_ == tokenType::LABEL
617  && data_.labelVal == val
618  );
619 }
620 
621 
622 inline Foam::label Foam::token::labelToken() const
623 {
624  if (type_ == tokenType::LABEL)
625  {
626  return data_.labelVal;
627  }
628 
629  parseError("label");
630  return 0;
631 }
632 
634 inline bool Foam::token::isFloat() const noexcept
635 {
636  return (type_ == tokenType::FLOAT);
637 }
638 
639 
640 inline float Foam::token::floatToken() const
641 {
642  if (type_ == tokenType::FLOAT)
643  {
644  return data_.floatVal;
645  }
646 
647  parseError("float");
648  return 0;
649 }
650 
652 inline bool Foam::token::isDouble() const noexcept
653 {
654  return (type_ == tokenType::DOUBLE);
655 }
656 
657 
658 inline double Foam::token::doubleToken() const
659 {
660  if (type_ == tokenType::DOUBLE)
661  {
662  return data_.doubleVal;
663  }
664 
665  parseError("double");
666  return 0;
667 }
668 
669 
670 inline bool Foam::token::isScalar() const noexcept
671 {
672  return
673  (
674  type_ == tokenType::FLOAT
675  || type_ == tokenType::DOUBLE
676  );
677 }
678 
679 
680 inline Foam::scalar Foam::token::scalarToken() const
681 {
682  if (type_ == tokenType::FLOAT)
683  {
684  return data_.floatVal;
685  }
686  else if (type_ == tokenType::DOUBLE)
687  {
688  return data_.doubleVal;
689  }
690 
691  parseError("scalar");
692  return 0;
693 }
694 
696 inline bool Foam::token::isNumber() const noexcept
697 {
698  return (type_ == tokenType::LABEL || isScalar());
699 }
700 
701 
702 inline Foam::scalar Foam::token::number() const
703 {
704  if (isLabel())
705  {
706  return labelToken();
707  }
708  if (isScalar())
709  {
710  return scalarToken();
711  }
712 
713  parseError("number (label or scalar)");
714  return 0;
715 }
716 
718 inline bool Foam::token::isWord() const noexcept
719 {
720  return is_wordToken(type_);
721 }
722 
724 inline bool Foam::token::isWord(const std::string& s) const
725 {
726  return (isWord() && s == *data_.wordPtr);
727 }
728 
730 inline bool Foam::token::isDirective() const noexcept
731 {
732  return (type_ == tokenType::DIRECTIVE);
733 }
734 
735 
736 inline const Foam::word& Foam::token::wordToken() const
737 {
738  if (isWord())
739  {
740  return *data_.wordPtr;
741  }
742 
743  parseError("word");
744  return word::null;
745 }
746 
748 inline bool Foam::token::isQuotedString() const noexcept
749 {
750  return (type_ == tokenType::STRING);
751 }
752 
754 inline bool Foam::token::isString() const noexcept
755 {
756  return is_stringToken(type_);
757 }
758 
760 inline bool Foam::token::isExpression() const noexcept
761 {
762  return (type_ == tokenType::EXPRESSION);
763 }
764 
766 inline bool Foam::token::isVariable() const noexcept
767 {
768  return (type_ == tokenType::VARIABLE);
769 }
770 
772 inline bool Foam::token::isVerbatim() const noexcept
773 {
774  return (type_ == tokenType::VERBATIM);
775 }
776 
778 inline bool Foam::token::isCharData() const noexcept
779 {
780  return (type_ == tokenType::CHAR_DATA);
781 }
782 
784 inline bool Foam::token::isStringType() const noexcept
785 {
786  return (isWord() || isString());
787 }
788 
789 
790 inline const Foam::string& Foam::token::stringToken() const
791 {
792  if (isString())
793  {
794  return *data_.stringPtr;
795  }
796  else if (isWord())
797  {
798  // Foam::word derives from Foam::string, no need to cast.
799  return *data_.wordPtr;
800  }
801 
802  parseError("string");
803  return string::null;
804 }
805 
806 
807 // Could also have reference to stringToken() - eg refStringToken() ?
808 // but not really sure where this would be useful...
810 inline bool Foam::token::isCompound() const noexcept
811 {
812  return (type_ == tokenType::COMPOUND);
813 }
814 
815 
816 inline bool Foam::token::isCompound(const word& compoundType) const
817 {
818  return
819  (
820  type_ == tokenType::COMPOUND
821  && data_.compoundPtr->type() == compoundType
822  );
823 }
824 
825 
826 template<class Type>
827 inline const Type* Foam::token::isCompound() const
828 {
829  return
830  (
831  type_ == tokenType::COMPOUND
832  ? dynamic_cast<const Type*>(data_.compoundPtr)
833  : nullptr
834  );
835 }
836 
837 
839 {
840  if (type_ != tokenType::COMPOUND)
841  {
842  parseError("compound");
843  }
844  return *data_.compoundPtr;
845 }
846 
847 
849 {
850  if (type_ != tokenType::COMPOUND)
851  {
852  parseError("compound");
853  }
854  return *data_.compoundPtr;
855 }
856 
857 
858 inline Foam::token::compound&
860 {
861  return transferCompoundToken(&is);
862 }
863 
864 
865 template<class Type>
866 inline Type& Foam::token::transferCompoundToken(const Istream& is)
867 {
868  return static_cast<Type&>
869  (
870  dynamicCast<token::Compound<Type>>
871  (
872  transferCompoundToken(&is)
873  )
874  );
875 }
876 
877 
878 template<class T>
880 {
881  // Does not cover all possibilities perfectly, but should handle
882  // most of the common ones (bool, label, scalar, vector lists).
883  // Something like List<edge> will not be quite correct if we rely
884  // on nComponents
885 
886  typedef typename T::value_type valueType;
887 
888  if (std::is_same<valueType, bool>::value)
889  {
890  // List<bool>
891  return token::tokenType::BOOL;
892  }
894  {
895  // List<label>, List<labelVector> etc
896  return token::tokenType::LABEL;
897  }
898  else if (is_contiguous_scalar<valueType>::value)
899  {
900  // List<scalar>, List<vector>, List<tensor> etc
901  return
902  (
903  sizeof(float) == sizeof(Foam::scalar)
904  ? token::tokenType::FLOAT
905  : token::tokenType::DOUBLE
906  );
907  }
908  else if (std::is_same<valueType, char>::value)
909  {
910  // List<char>
911  return token::tokenType::PUNCTUATION;
912  }
913  else
914  {
915  // Do not handle List<word> or List<string> at the moment
916  // since filling non-contiguous data is probably not desirable
917  return token::tokenType::UNDEFINED;
918  }
919 }
920 
921 
922 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
923 
924 inline void Foam::token::operator=(const token& tok)
925 {
926  if (this == &tok)
927  {
928  return; // Self-assignment is a no-op
929  }
930 
931  reset();
932 
933  type_ = tok.type_;
934  data_ = tok.data_; // bit-wise copy of union content
935  line_ = tok.line_;
936 
937  // Fundamental: values already handled by bit-wise copy
938  // Pointer: duplicate content or increase refCount
939 
940  switch (type_)
941  {
942  // token::isWord()
943  case tokenType::WORD:
944  case tokenType::DIRECTIVE:
945  {
946  data_.wordPtr = new word(*tok.data_.wordPtr);
947  }
948  break;
949 
950  // token::isString()
951  case tokenType::STRING:
952  case tokenType::EXPRESSION:
953  case tokenType::VARIABLE:
954  case tokenType::VERBATIM:
955  case tokenType::CHAR_DATA:
956  {
957  data_.stringPtr = new string(*tok.data_.stringPtr);
958  }
959  break;
960 
961  case tokenType::COMPOUND:
962  {
963  // Identical pointers, but increase the refCount
964  data_.compoundPtr = tok.data_.compoundPtr;
965  data_.compoundPtr->refCount::operator++();
966  }
967  break;
969  default:
970  break;
971  }
972 }
973 
974 
975 inline void Foam::token::operator=(token&& tok)
976 {
977  if (this == &tok)
978  {
979  return; // Self-assignment is a no-op
980  }
982  reset();
983  line_ = 0;
984  swap(tok);
985 }
986 
987 
988 inline void Foam::token::operator=(const punctuationToken p)
989 {
990  reset();
991  type_ = tokenType::PUNCTUATION;
992  data_.punctuationVal = p;
993 }
994 
995 
996 inline void Foam::token::operator=(const label val)
997 {
998  reset();
999  type_ = tokenType::LABEL;
1000  data_.labelVal = val;
1001 }
1002 
1003 
1004 inline void Foam::token::operator=(const float val)
1006  reset();
1007  type_ = tokenType::FLOAT;
1008  data_.floatVal = val;
1009 }
1010 
1011 
1012 inline void Foam::token::operator=(const double val)
1014  reset();
1015  type_ = tokenType::DOUBLE;
1016  data_.doubleVal = val;
1017 }
1018 
1019 
1020 inline void Foam::token::operator=(const word& w)
1022  reset();
1023  type_ = tokenType::WORD;
1024  data_.wordPtr = new word(w);
1025 }
1026 
1027 
1028 inline void Foam::token::operator=(const string& str)
1030  reset();
1031  type_ = tokenType::STRING;
1032  data_.stringPtr = new string(str);
1033 }
1034 
1035 
1036 inline void Foam::token::operator=(word&& w)
1038  reset();
1039  type_ = tokenType::WORD;
1040  data_.wordPtr = new word(std::move(w));
1041 }
1042 
1043 
1044 inline void Foam::token::operator=(string&& s)
1046  reset();
1047  type_ = tokenType::STRING;
1048  data_.stringPtr = new string(std::move(s));
1049 }
1050 
1051 
1054  reset();
1055  type_ = tokenType::COMPOUND;
1056  data_.compoundPtr = ptr;
1057 }
1058 
1059 
1062  reset();
1063  type_ = tokenType::COMPOUND;
1064  data_.compoundPtr = ptr.release();
1065 }
1066 
1067 
1068 inline bool Foam::token::operator==(const token& tok) const
1069 {
1070  if (type_ != tok.type_)
1071  {
1072  return false;
1073  }
1074 
1075  switch (type_)
1076  {
1077  case tokenType::UNDEFINED:
1078  return true;
1079 
1080  case tokenType::BOOL:
1081  return data_.labelVal == tok.data_.labelVal;
1082 
1083  case tokenType::FLAG:
1084  return data_.flagVal == tok.data_.flagVal;
1085 
1086  case tokenType::PUNCTUATION:
1087  return data_.punctuationVal == tok.data_.punctuationVal;
1088 
1089  case tokenType::LABEL:
1090  return data_.labelVal == tok.data_.labelVal;
1091 
1092  case tokenType::FLOAT:
1093  return equal(data_.floatVal, tok.data_.floatVal);
1094 
1095  case tokenType::DOUBLE:
1096  return equal(data_.doubleVal, tok.data_.doubleVal);
1097 
1098  // token::isWord()
1099  case tokenType::WORD:
1100  case tokenType::DIRECTIVE:
1101  return *data_.wordPtr == *tok.data_.wordPtr;
1102 
1103  // token::isString()
1104  case tokenType::STRING:
1105  case tokenType::EXPRESSION:
1106  case tokenType::VARIABLE:
1107  case tokenType::VERBATIM:
1108  case tokenType::CHAR_DATA:
1109  return *data_.stringPtr == *tok.data_.stringPtr;
1110 
1111  case tokenType::COMPOUND:
1112  return data_.compoundPtr == tok.data_.compoundPtr;
1113 
1114  case tokenType::ERROR:
1115  return true;
1116  }
1117 
1118  return false;
1119 }
1120 
1122 inline bool Foam::token::operator==(const punctuationToken p) const noexcept
1123 {
1124  return isPunctuation(p);
1125 }
1126 
1127 
1128 inline bool Foam::token::operator==(const std::string& s) const
1129 {
1130  return
1131  (
1132  isWord()
1133  ? s == *data_.wordPtr
1134  : isString() && s == *data_.stringPtr
1135  );
1136 }
1137 
1139 inline bool Foam::token::operator==(const label val) const noexcept
1140 {
1141  return isLabel(val);
1142 }
1143 
1144 
1145 inline bool Foam::token::operator==(const float val) const noexcept
1146 {
1147  return
1148  (
1149  type_ == tokenType::FLOAT
1150  && equal(data_.floatVal, val)
1151  );
1152 }
1153 
1154 
1155 inline bool Foam::token::operator==(const double val) const noexcept
1156 {
1157  return
1158  (
1159  type_ == tokenType::DOUBLE
1160  && equal(data_.doubleVal, val)
1161  );
1162 }
1163 
1165 inline bool Foam::token::operator!=(const token& tok) const
1166 {
1167  return !operator==(tok);
1168 }
1169 
1171 inline bool Foam::token::operator!=(const punctuationToken p) const noexcept
1172 {
1173  return !isPunctuation(p);
1174 }
1175 
1177 inline bool Foam::token::operator!=(const label val) const noexcept
1178 {
1179  return !operator==(val);
1180 }
1181 
1183 inline bool Foam::token::operator!=(const float val) const noexcept
1184 {
1185  return !operator==(val);
1186 }
1187 
1189 inline bool Foam::token::operator!=(const double val) const noexcept
1190 {
1191  return !operator==(val);
1192 }
1193 
1194 
1195 inline bool Foam::token::operator!=(const std::string& s) const
1196 {
1197  return !operator==(s);
1198 }
1199 
1200 
1201 // ************************************************************************* //
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:507
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:561
~token()
Destructor.
Definition: tokenI.H:327
bool operator!=(const token &tok) const
Definition: tokenI.H:1158
bool isDouble() const noexcept
Token is DOUBLE.
Definition: tokenI.H:645
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:711
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:587
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.H:113
virtual tokenType typeCode() const
The token type (if any) corresponding to the contained component type (LABEL, FLOAT, DOUBLE, etc).
Definition: tokenI.H:872
bool equal(const T &a, const T &b)
Compare two values for equality.
Definition: label.H:164
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:803
scalar number() const
Return label, float or double value.
Definition: tokenI.H:695
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:729
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:65
tokenType
Enumeration defining the types of token.
Definition: token.H:76
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:405
bool isFlag() const noexcept
Token is FLAG.
Definition: tokenI.H:543
float floatToken() const
Return float value.
Definition: tokenI.H:633
bool isExpression() const noexcept
Token is EXPRESSION (string variant)
Definition: tokenI.H:753
bool isSeparator() const noexcept
Token is PUNCTUATION and isseparator.
Definition: tokenI.H:577
bool undefined() const noexcept
Token is UNDEFINED.
Definition: tokenI.H:513
scalar scalarToken() const
Return float or double value.
Definition: tokenI.H:673
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:531
word name() const
Return the name of the current token type.
Definition: tokenI.H:399
bool isScalar() const noexcept
Token is FLOAT or DOUBLE.
Definition: tokenI.H:663
bool isString() const noexcept
Token is string-variant (STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:747
bool error() const noexcept
Token is ERROR.
Definition: tokenI.H:519
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:549
Abstract base class for complex tokens.
Definition: token.H:176
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:493
double doubleToken() const
Return double value.
Definition: tokenI.H:651
compound & transferCompoundToken(const Istream *is=nullptr)
Return reference to compound and mark internally as released.
Definition: token.C:157
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:777
A class for handling words, derived from Foam::string.
Definition: word.H:63
static bool isseparator(int c) noexcept
True if the character is a punctuation separator (eg, in ISstream).
Definition: tokenI.H:69
void swap(token &tok) noexcept
Swap token contents: type, data, line-number.
Definition: tokenI.H:386
void setBad()
Clear token and set to be ERROR.
Definition: tokenI.H:379
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:126
static const word null
An empty word.
Definition: word.H:84
const compound & compoundToken() const
Const reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:831
static const string null
An empty string.
Definition: string.H:202
const direction noexcept
Definition: Scalar.H:258
bool isBool() const noexcept
Token is BOOL.
Definition: tokenI.H:525
bool setType(const tokenType tokType) noexcept
Change the token type, for similar types.
Definition: tokenI.H:411
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:783
static token flag(int bitmask) noexcept
Create a token with stream flags, no sanity check.
Definition: tokenI.H:36
bool operator==(const token &tok) const
Definition: tokenI.H:1061
void operator=(const token &tok)
Copy assign.
Definition: tokenI.H:917
bool isCharData() const noexcept
Token is CHAR_DATA (string variant)
Definition: tokenI.H:771
bool isFloat() const noexcept
Token is FLOAT.
Definition: tokenI.H:627
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:335
static token boolean(bool on) noexcept
Create a bool token.
Definition: tokenI.H:26
const dimensionedScalar c
Speed of light in a vacuum.
label labelToken() const
Return label value.
Definition: tokenI.H:615
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:75
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
compound & refCompoundToken()
Return reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:841
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
volScalarField & p
tmp< faMatrix< Type > > operator==(const faMatrix< Type > &, const faMatrix< Type > &)
bool isVariable() const noexcept
Token is VARIABLE (string variant)
Definition: tokenI.H:759
bool isDirective() const noexcept
Token is DIRECTIVE (word variant)
Definition: tokenI.H:723
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.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))
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:689
A class for handling character strings derived from std::string.
Definition: string.H:72
bool isVerbatim() const noexcept
Token is VERBATIM string (string variant)
Definition: tokenI.H:765
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:741