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-2025 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 
605 
606 inline bool Foam::token::isLabel() const noexcept
607 {
608  return
609  (
610  type_ == tokenType::LABEL
611  // FUTURE?
612  // || type_ == tokenType::INT32
613  // || type_ == tokenType::INT64
614  );
615 }
616 
617 
618 inline bool Foam::token::isLabel(const label value) const noexcept
619 {
620  // FUTURE?
621  // return
622  // (
623  // type_ == tokenType::LABEL
624  // ? value == data_.labelVal
625  // : type_ == tokenType::INT32
626  // ? value == data_.int32Val
627  // : type_ == tokenType::INT64
628  // ? value == data_.int64Val
629  // : false
630  // );
631 
632  return
633  (
634  type_ == tokenType::LABEL
635  && value == data_.labelVal
636  );
637 }
638 
639 
640 inline Foam::label Foam::token::labelToken() const
641 {
642  if (type_ == tokenType::LABEL)
643  {
644  return data_.labelVal;
645  }
646 
647  parseError("label");
648  return 0;
649 }
650 
652 inline bool Foam::token::isFloat() const noexcept
653 {
654  return (type_ == tokenType::FLOAT);
655 }
656 
657 
658 inline float Foam::token::floatToken() const
659 {
660  if (type_ == tokenType::FLOAT)
661  {
662  return data_.floatVal;
663  }
664 
665  parseError("float");
666  return 0;
667 }
668 
670 inline bool Foam::token::isDouble() const noexcept
671 {
672  return (type_ == tokenType::DOUBLE);
673 }
674 
675 
676 inline double Foam::token::doubleToken() const
677 {
678  if (type_ == tokenType::DOUBLE)
679  {
680  return data_.doubleVal;
681  }
682 
683  parseError("double");
684  return 0;
685 }
686 
687 
688 inline bool Foam::token::isScalar() const noexcept
689 {
690  return
691  (
692  type_ == tokenType::FLOAT
693  || type_ == tokenType::DOUBLE
694  );
695 }
696 
697 
698 inline Foam::scalar Foam::token::scalarToken() const
699 {
700  if (type_ == tokenType::FLOAT)
701  {
702  return data_.floatVal;
703  }
704  else if (type_ == tokenType::DOUBLE)
705  {
706  return data_.doubleVal;
707  }
708 
709  parseError("scalar");
710  return 0;
711 }
712 
714 inline bool Foam::token::isNumber() const noexcept
715 {
716  return (isLabel() || isScalar());
717 }
718 
719 
720 inline Foam::scalar Foam::token::number() const
721 {
722  if (isLabel())
723  {
724  return labelToken();
725  }
726  if (isScalar())
727  {
728  return scalarToken();
729  }
730 
731  parseError("number (label or scalar)");
732  return 0;
733 }
734 
736 inline bool Foam::token::isWord() const noexcept
737 {
738  return is_wordToken(type_);
739 }
740 
742 inline bool Foam::token::isWord(const std::string& s) const
743 {
744  return (isWord() && s == *data_.wordPtr);
745 }
746 
748 inline bool Foam::token::isDirective() const noexcept
749 {
750  return (type_ == tokenType::DIRECTIVE);
751 }
752 
753 
754 inline const Foam::word& Foam::token::wordToken() const
755 {
756  if (isWord())
757  {
758  return *data_.wordPtr;
759  }
760 
761  parseError("word");
762  return word::null;
763 }
764 
766 inline bool Foam::token::isQuotedString() const noexcept
767 {
768  return (type_ == tokenType::STRING);
769 }
770 
772 inline bool Foam::token::isString() const noexcept
773 {
774  return is_stringToken(type_);
775 }
776 
778 inline bool Foam::token::isExpression() const noexcept
779 {
780  return (type_ == tokenType::EXPRESSION);
781 }
782 
784 inline bool Foam::token::isVariable() const noexcept
785 {
786  return (type_ == tokenType::VARIABLE);
787 }
788 
790 inline bool Foam::token::isVerbatim() const noexcept
791 {
792  return (type_ == tokenType::VERBATIM);
793 }
794 
796 inline bool Foam::token::isCharData() const noexcept
797 {
798  return (type_ == tokenType::CHAR_DATA);
799 }
800 
802 inline bool Foam::token::isStringType() const noexcept
803 {
804  return (isWord() || isString());
805 }
806 
807 
808 inline const Foam::string& Foam::token::stringToken() const
809 {
810  if (isString())
811  {
812  return *data_.stringPtr;
813  }
814  else if (isWord())
815  {
816  // Foam::word derives from Foam::string, no need to cast.
817  return *data_.wordPtr;
818  }
819 
820  parseError("string");
821  return string::null;
822 }
823 
824 
825 // Could also have reference to stringToken() - eg refStringToken() ?
826 // but not really sure where this would be useful...
828 inline bool Foam::token::isCompound() const noexcept
829 {
830  return (type_ == tokenType::COMPOUND);
831 }
832 
833 
834 inline bool Foam::token::isCompound(const word& compoundType) const
835 {
836  return
837  (
838  type_ == tokenType::COMPOUND
839  && data_.compoundPtr->type() == compoundType
840  );
841 }
842 
843 
844 template<class Type>
845 inline const Type* Foam::token::isCompound() const
846 {
847  return
848  (
849  type_ == tokenType::COMPOUND
850  ? dynamic_cast<const Type*>(data_.compoundPtr)
851  : nullptr
852  );
853 }
854 
855 
857 {
858  if (type_ != tokenType::COMPOUND)
859  {
860  parseError("compound");
861  }
862  return *data_.compoundPtr;
863 }
864 
865 
867 {
868  if (type_ != tokenType::COMPOUND)
869  {
870  parseError("compound");
871  }
872  return *data_.compoundPtr;
873 }
874 
875 
876 template<class Type>
877 inline Type& Foam::token::refCompoundToken()
878 {
879  if (type_ != tokenType::COMPOUND)
880  {
881  parseError("compound");
882  }
883  return static_cast<Type&>
884  (
885  dynamicCast<token::Compound<Type>>
886  (
887  *data_.compoundPtr
888  )
889  );
890 }
891 
892 
893 inline Foam::token::compound&
895 {
896  return transferCompoundToken(&is);
897 }
898 
899 
900 template<class Type>
901 inline Type& Foam::token::transferCompoundToken(const Istream& is)
902 {
903  return static_cast<Type&>
904  (
905  dynamicCast<token::Compound<Type>>
906  (
907  transferCompoundToken(&is)
908  )
909  );
910 }
911 
912 
913 template<class T>
915 {
916  // Does not cover all possibilities perfectly, but should handle
917  // most of the common ones (bool, label, scalar, vector lists).
918  // Something like List<edge> will not be quite correct if we rely
919  // on nComponents
920 
921  typedef typename T::value_type valueType;
922 
923  if constexpr (std::is_same_v<bool, valueType>)
924  {
925  // List<bool>
926  return token::tokenType::BOOL;
927  }
928  else if constexpr (is_contiguous_label<valueType>::value)
929  {
930  // List<label>, List<labelVector> etc
931  return token::tokenType::LABEL;
932 
933  // FUTURE?
934  // return
935  // (
936  // sizeof(int32_t) == sizeof(Foam::label)
937  // ? token::tokenType::INT32
938  // : token::tokenType::INT64
939  // );
940 
941  }
942  else if constexpr (is_contiguous_scalar<valueType>::value)
943  {
944  // List<scalar>, List<vector>, List<tensor> etc
945  return
946  (
947  sizeof(float) == sizeof(Foam::scalar)
948  ? token::tokenType::FLOAT
949  : token::tokenType::DOUBLE
950  );
951  }
952  else if constexpr (std::is_same_v<char, valueType>)
953  {
954  // List<char>
955  return token::tokenType::PUNCTUATION;
956  }
957  else
958  {
959  // Do not handle List<word> or List<string> at the moment
960  // since filling non-contiguous data is probably not desirable
961  return token::tokenType::UNDEFINED;
962  }
963 }
964 
965 
966 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
967 
968 inline void Foam::token::operator=(const token& tok)
969 {
970  if (this == &tok)
971  {
972  return; // Self-assignment is a no-op
973  }
974 
975  reset();
976 
977  type_ = tok.type_;
978  data_ = tok.data_; // bit-wise copy of union content
979  line_ = tok.line_;
980 
981  // Fundamental: values already handled by bit-wise copy
982  // Pointer: duplicate content or increase refCount
983 
984  switch (type_)
985  {
986  // token::isWord()
987  case tokenType::WORD:
988  case tokenType::DIRECTIVE:
989  {
990  data_.wordPtr = new word(*tok.data_.wordPtr);
991  }
992  break;
993 
994  // token::isString()
995  case tokenType::STRING:
996  case tokenType::EXPRESSION:
997  case tokenType::VARIABLE:
998  case tokenType::VERBATIM:
999  case tokenType::CHAR_DATA:
1000  {
1001  data_.stringPtr = new string(*tok.data_.stringPtr);
1002  }
1003  break;
1004 
1005  case tokenType::COMPOUND:
1006  {
1007  // Identical pointers, but increase the refCount
1008  data_.compoundPtr = tok.data_.compoundPtr;
1009  data_.compoundPtr->refCount::operator++();
1010  }
1011  break;
1013  default:
1014  break;
1015  }
1016 }
1017 
1018 
1019 inline void Foam::token::operator=(token&& tok)
1020 {
1021  if (this == &tok)
1022  {
1023  return; // Self-assignment is a no-op
1024  }
1026  reset();
1027  line_ = 0;
1028  swap(tok);
1029 }
1030 
1031 
1032 inline void Foam::token::operator=(const punctuationToken p)
1034  reset();
1035  type_ = tokenType::PUNCTUATION;
1036  data_.punctuationVal = p;
1037 }
1038 
1039 
1040 inline void Foam::token::operator=(const label val)
1042  reset();
1043  type_ = tokenType::LABEL;
1044  data_.labelVal = val;
1045 }
1046 
1047 
1048 inline void Foam::token::operator=(const float val)
1050  reset();
1051  type_ = tokenType::FLOAT;
1052  data_.floatVal = val;
1053 }
1054 
1055 
1056 inline void Foam::token::operator=(const double val)
1058  reset();
1059  type_ = tokenType::DOUBLE;
1060  data_.doubleVal = val;
1061 }
1062 
1063 
1064 inline void Foam::token::operator=(const word& w)
1066  reset();
1067  type_ = tokenType::WORD;
1068  data_.wordPtr = new word(w);
1069 }
1070 
1071 
1072 inline void Foam::token::operator=(const string& str)
1074  reset();
1075  type_ = tokenType::STRING;
1076  data_.stringPtr = new string(str);
1077 }
1078 
1079 
1080 inline void Foam::token::operator=(word&& w)
1082  reset();
1083  type_ = tokenType::WORD;
1084  data_.wordPtr = new word(std::move(w));
1085 }
1086 
1087 
1088 inline void Foam::token::operator=(string&& s)
1090  reset();
1091  type_ = tokenType::STRING;
1092  data_.stringPtr = new string(std::move(s));
1093 }
1094 
1095 
1098  reset();
1099  type_ = tokenType::COMPOUND;
1100  data_.compoundPtr = ptr;
1101 }
1102 
1103 
1106  reset();
1107  type_ = tokenType::COMPOUND;
1108  data_.compoundPtr = ptr.release();
1109 }
1110 
1111 
1112 inline bool Foam::token::operator==(const token& tok) const
1113 {
1114  if (type_ != tok.type_)
1115  {
1116  return false;
1117  }
1118 
1119  switch (type_)
1120  {
1121  case tokenType::UNDEFINED:
1122  return true;
1123 
1124  case tokenType::BOOL:
1125  return data_.labelVal == tok.data_.labelVal;
1126 
1127  case tokenType::FLAG:
1128  return data_.flagVal == tok.data_.flagVal;
1129 
1130  case tokenType::PUNCTUATION:
1131  return data_.punctuationVal == tok.data_.punctuationVal;
1132 
1133  case tokenType::LABEL:
1134  return data_.labelVal == tok.data_.labelVal;
1135 
1136  case tokenType::FLOAT:
1137  return equal(data_.floatVal, tok.data_.floatVal);
1138 
1139  case tokenType::DOUBLE:
1140  return equal(data_.doubleVal, tok.data_.doubleVal);
1141 
1142  // token::isWord()
1143  case tokenType::WORD:
1144  case tokenType::DIRECTIVE:
1145  return *data_.wordPtr == *tok.data_.wordPtr;
1146 
1147  // token::isString()
1148  case tokenType::STRING:
1149  case tokenType::EXPRESSION:
1150  case tokenType::VARIABLE:
1151  case tokenType::VERBATIM:
1152  case tokenType::CHAR_DATA:
1153  return *data_.stringPtr == *tok.data_.stringPtr;
1154 
1155  case tokenType::COMPOUND:
1156  return data_.compoundPtr == tok.data_.compoundPtr;
1157 
1158  case tokenType::ERROR:
1159  return true;
1160  }
1161 
1162  return false;
1163 }
1164 
1166 inline bool Foam::token::operator==(const punctuationToken p) const noexcept
1167 {
1168  return isPunctuation(p);
1169 }
1170 
1171 
1172 inline bool Foam::token::operator==(const std::string& s) const
1173 {
1174  return
1175  (
1176  isWord()
1177  ? s == *data_.wordPtr
1178  : isString() && s == *data_.stringPtr
1179  );
1180 }
1181 
1183 inline bool Foam::token::operator==(const label val) const noexcept
1184 {
1185  return isLabel(val);
1186 }
1187 
1188 
1189 inline bool Foam::token::operator==(const float val) const noexcept
1190 {
1191  return
1192  (
1193  type_ == tokenType::FLOAT
1194  && equal(data_.floatVal, val)
1195  );
1196 }
1197 
1198 
1199 inline bool Foam::token::operator==(const double val) const noexcept
1200 {
1201  return
1202  (
1203  type_ == tokenType::DOUBLE
1204  && equal(data_.doubleVal, val)
1205  );
1206 }
1207 
1209 inline bool Foam::token::operator!=(const token& tok) const
1210 {
1211  return !operator==(tok);
1212 }
1213 
1215 inline bool Foam::token::operator!=(const punctuationToken p) const noexcept
1216 {
1217  return !isPunctuation(p);
1218 }
1219 
1221 inline bool Foam::token::operator!=(const label val) const noexcept
1222 {
1223  return !operator==(val);
1224 }
1225 
1227 inline bool Foam::token::operator!=(const float val) const noexcept
1228 {
1229  return !operator==(val);
1230 }
1231 
1233 inline bool Foam::token::operator!=(const double val) const noexcept
1234 {
1235  return !operator==(val);
1236 }
1237 
1238 
1239 inline bool Foam::token::operator!=(const std::string& s) const
1240 {
1241  return !operator==(s);
1242 }
1243 
1244 
1245 // ************************************************************************* //
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:1202
bool isDouble() const noexcept
Token is DOUBLE.
Definition: tokenI.H:663
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:729
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:907
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:821
scalar number() const
Return label, float or double value.
Definition: tokenI.H:713
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:747
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:651
bool isExpression() const noexcept
Token is EXPRESSION (string variant)
Definition: tokenI.H:771
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:691
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:681
bool isString() const noexcept
Token is string-variant (STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:765
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:669
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:795
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:849
static const string null
An empty string.
Definition: string.H:202
const direction noexcept
Definition: scalarImpl.H:255
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:801
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:1105
void operator=(const token &tok)
Copy assign.
Definition: tokenI.H:961
bool isCharData() const noexcept
Token is CHAR_DATA (string variant)
Definition: tokenI.H:789
bool isFloat() const noexcept
Token is FLOAT.
Definition: tokenI.H:645
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:335
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
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:633
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:82
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
compound & refCompoundToken()
Return reference to compound token. Fatal if the wrong type. No checks for released or pending states...
Definition: tokenI.H:859
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:777
bool isDirective() const noexcept
Token is DIRECTIVE (word variant)
Definition: tokenI.H:741
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:707
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:783
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:759