token.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 Class
28  Foam::token
29 
30 Description
31  A token holds an item read from Istream.
32 
33 SourceFiles
34  tokenI.H
35  token.C
36  tokenIO.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_token_H
41 #define Foam_token_H
42 
43 #include "label.H"
44 #include "uLabel.H"
45 #include "scalar.H"
46 #include "word.H"
47 #include "contiguous.H"
48 #include "refCount.H"
49 #include "InfoProxy.H"
50 #include "typeInfo.H"
51 
52 #define NoHashTableC
53 #include "runTimeSelectionTables.H"
54 
55 #include <iostream>
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward Declarations
63 class token;
64 Ostream& operator<<(Ostream& os, const token& tok);
65 
66 /*---------------------------------------------------------------------------*\
67  Class token Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class token
71 {
72 public:
73 
74  //- Enumeration defining the types of token.
75  // Since the enumeration is used to tag content in Pstream, it is of
76  // type \c char and shall have values that do not overlap with regular
77  // punctuation characters.
78  enum tokenType : char
79  {
80  UNDEFINED = '\0',
81  ERROR = '\x80',
82 
83  // Fundamental types
84  FLAG,
86  BOOL,
88  FLOAT,
89  DOUBLE,
90 
91  // Pointer types
92  WORD,
93  STRING,
95 
96  DIRECTIVE,
97 
100  VARIABLE,
102  VERBATIM,
104  CHAR_DATA,
107  // Aliases
111  };
112 
113 
114  //- Stream or output control flags (1-byte width)
115  enum flagType
116  {
117  NO_FLAG = 0,
118  ASCII = 1,
119  BINARY = 2
120  };
121 
122 
123  //- Standard punctuation tokens (a character)
124  enum punctuationToken : char
125  {
126  NULL_TOKEN = '\0',
127  TAB = '\t',
128  NL = '\n',
129  SPACE = ' ',
131  COLON = ':',
132  SEMICOLON = ';',
133  COMMA = ',',
134  HASH = '#',
135  DOLLAR = '$',
136  QUESTION = '?',
137  ATSYM = '@',
138  SQUOTE = '\'',
139  DQUOTE = '"',
141  ASSIGN = '=',
142  PLUS = '+',
143  MINUS = '-',
144  MULTIPLY = '*',
145  DIVIDE = '/',
147  LPAREN = '(',
148  RPAREN = ')',
149  LSQUARE = '[',
150  RSQUARE = ']',
151  LBRACE = '{',
152  RBRACE = '}',
154  // With semantically meaning
155 
156  ADD = PLUS,
157  SUBTRACT = MINUS,
167  END_STRING = DQUOTE
168  };
170 
171  //- Abstract base class for complex tokens
172  class compound
173  :
174  public refCount
175  {
176  protected:
177 
178  //- The compound token state, internally used values only
179  // (0: initial, 1: moved, 2: pending read...)
180  unsigned char state_;
181 
182  public:
183 
184  //- Declare type-name, virtual type (without debug switch)
185  TypeNameNoDebug("compound");
186 
187  //- Declare run-time constructor selection table
189  (
190  autoPtr,
191  compound,
192  empty,
193  (),
194  ()
195  );
196 
197 
198  // Constructors
199 
200  //- Default construct
201  constexpr compound() noexcept
202  :
203  state_(0)
204  {}
205 
206 
207  //- Destructor
208  virtual ~compound() noexcept = default;
209 
210 
211  // Selectors
212 
213  //- Default construct specified compound type
214  static autoPtr<compound> New(const word& compoundType);
215 
216  //- Default construct specified compound type
217  //- and read from stream
218  static autoPtr<compound> New
219  (
220  const word& compoundType,
221  Istream& is,
222  const bool readContent = true
223  );
224 
225  //- Check if dynamic_cast to \c Type is possible.
226  template<class Type>
227  const Type* isA() const
228  {
229  return dynamic_cast<const Type*>(this);
230  }
231 
232 
233  // Member Functions
234 
235  //- True if a known (registered) compound type
236  static bool isCompound(const word& compoundType);
237 
238  //- Get compound transferred status
239  bool moved() const noexcept { return (state_ & 1); }
240 
241  //- Set compound transferred status
242  void moved(bool b) noexcept
243  {
244  if (b) { state_ |= 1; } else { state_ &= ~1; }
245  }
246 
247  //- Get compound pending-read status
248  bool pending() const noexcept { return (state_ & 2); }
249 
250  //- Set compound pending-read status
251  void pending(bool b) noexcept
252  {
253  if (b) { state_ |= 2; } else { state_ &= ~2; }
254  }
255 
256  //- The size of the underlying content
257  virtual label size() const = 0;
258 
259  //- Change the size of the underlying container content
260  virtual void resize(const label n) = 0;
261 
262  //- Fill with zero values, or with default constructed
263  virtual void fill_zero() = 0;
265  //- Read from stream into the underlying content
266  virtual void read(Istream& is) = 0;
267 
268  //- Write the underlying content
269  virtual void write(Ostream& os) const = 0;
270 
271 
272  // Attributes and access methods
273 
274  //- The token type (if any) corresponding to the contained
275  //- component type (LABEL, FLOAT, DOUBLE, etc).
276  virtual tokenType typeCode() const = 0;
278  //- The number of vector-space or other components
279  //- of the underlying data content
280  virtual direction nComponents() const = 0;
281 
282  //- Pointer to the underlying data as byte data
283  virtual const char* cdata_bytes() const = 0;
284 
285  //- Pointer to the underlying data as byte data
286  virtual char* data_bytes() = 0;
287 
288  //- Size of the (contiguous) byte data
289  virtual std::streamsize size_bytes() const = 0;
290 
291 
292  // Operators
293 
294  //- Output operator
295  friend Ostream& operator<<(Ostream& os, const compound& ct);
296  };
297 
298 
299  //- A templated class for holding compound tokens.
300  //- The underlying container is normally a List of values,
301  //- it must have a \c value_type typedef as well as
302  //- size(), resize(), cdata_bytes(), data_bytes(), size_bytes() methods
303  template<class T>
304  class Compound
305  :
306  public token::compound,
307  public T
308  {
309  // Private Member Functions
310 
311  //- Fill with zero (contiguous types)
312  template<class ValT = typename T::value_type>
313  typename std::enable_if<pTraits_has_zero<ValT>::value, void>::type
314  _m_fill_zero()
315  {
316  T::operator=(pTraits<ValT>::zero);
317  }
318 
319  //- Fill with default value initialized (non-contiguous types)
320  template<class ValT = typename T::value_type>
321  typename std::enable_if<!pTraits_has_zero<ValT>::value, void>::type
322  _m_fill_zero()
323  {
324  T::operator=(ValT());
325  }
326 
327  public:
328 
329  //- The type of values held by the compound
330  typedef typename T::value_type value_type;
331 
332  //- Declare type-name, virtual type (without debug switch)
333  TypeNameNoDebug("Compound<T>");
334 
335  //- No copy construct
336  Compound<T>(const Compound<T>&) = delete;
337 
338  //- No copy assignment
339  Compound<T>& operator=(const Compound<T>&) = delete;
340 
341  // Constructors
342 
343  //- Default construct
344  Compound() = default;
345 
346  //- Copy construct from base content
347  explicit Compound(const T& content)
348  :
349  T(content)
350  {}
351 
352  //- Move construct from base content
353  explicit Compound(T&& content)
354  :
355  T(std::move(content))
356  {}
357 
358  //- Read construct from Istream
359  explicit Compound(Istream& is)
360  :
361  T(is)
362  {}
363 
364 
365  // Selectors
366 
367  //- Construct autoPtr compound type with forwarding arguments
368  template<class... Args>
369  static autoPtr<Compound<T>> New(Args&&... args)
370  {
371  return autoPtr<Compound<T>>
372  (
373  new Compound<T>(T(std::forward<Args>(args)...))
374  );
375  }
376 
377 
378  // Member Functions
379 
380  //- The size of the underlying content
381  virtual label size() const
382  {
383  return T::size();
384  }
385 
386  //- Change the size of the underlying container content
387  virtual void resize(const label n)
388  {
389  T::resize(n);
390  }
392  //- Fill with zero value or with default value initialized
393  virtual void fill_zero() { _m_fill_zero(); }
394 
395  //- Redirect read to underlying content
396  virtual void read(Istream& is)
397  {
398  token::compound::state_ = 0; // Clean state
399  operator>>(is, static_cast<T&>(*this));
400  }
401 
402  //- Redirect write to underlying content
403  virtual void write(Ostream& os) const
404  {
405  operator<<(os, static_cast<const T&>(*this));
406  }
407 
408 
409  // Attributes and access methods
410 
411  //- The token type (if any) corresponding to the contained
412  //- component type (LABEL, FLOAT, DOUBLE, etc).
413  virtual tokenType typeCode() const;
414 
415  //- The number of vector-space or other components
416  //- of the underlying data content
417  virtual direction nComponents() const
418  {
420  }
421 
422  //- Pointer to the underlying data as byte data
423  virtual const char* cdata_bytes() const
424  {
425  return T::cdata_bytes();
426  }
427 
428  //- Pointer to the underlying data as byte data
429  virtual char* data_bytes()
430  {
431  return T::data_bytes();
432  }
433 
434  //- Size of the (contiguous) byte data
435  virtual std::streamsize size_bytes() const
436  {
437  return T::size_bytes();
438  }
439  };
440 
441 
442  //- An undefined token
443  static const token undefinedToken;
444 
445 
446 private:
447 
448  //- A %union of token types
449  union content
450  {
451  // Fundamental values. Largest first for any {} initialization.
452  int64_t int64Val;
453  int32_t int32Val;
454 
455  int flagVal; // bitmask - stored as int, not enum
456  punctuationToken punctuationVal;
457  label labelVal;
458  float floatVal;
459  double doubleVal;
461  // Pointers
462  word* wordPtr;
463  string* stringPtr;
464  mutable compound* compoundPtr;
465  };
466 
467 
468  // Private Data
469 
470  //- The data content (as a union).
471  // For memory alignment this should appear as the first member.
472  content data_;
473 
474  //- The token type
475  tokenType type_;
477  //- Line number in the file the token was read from
478  label line_;
479 
480 
481  // Private Member Functions
482 
483  //- Set as UNDEFINED and zero the union content without any checking
484  inline void setUndefined() noexcept;
485 
486  //- True if token type corresponds to WORD or WORD-variant.
487  inline static bool is_wordToken(tokenType tokType) noexcept;
488 
489  //- True if token type corresponds to STRING or STRING-variant
490  inline static bool is_stringToken(tokenType tokType) noexcept;
491 
492  // Parse error, expected 'expected', found ...
493  void parseError(const char* expected) const;
494 
495 
496 public:
497 
498  // Static Data Members
499 
500  //- The type name is "token"
501  static constexpr const char* const typeName = "token";
502 
503 
504  // Constructors
505 
506  //- Default construct, initialized to an UNDEFINED token.
507  inline constexpr token() noexcept;
509  //- Copy construct
510  inline token(const token& t);
511 
512  //- Move construct. The original token is left as UNDEFINED.
513  inline token(token&& t) noexcept;
514 
515  //- Construct punctuation character token
516  inline explicit token(punctuationToken p, label lineNum=0) noexcept;
517 
518  //- Construct label token
519  inline explicit token(const label val, label lineNum=0) noexcept;
520 
521  //- Construct float token
522  inline explicit token(const float val, label lineNum=0) noexcept;
523 
524  //- Construct double token
525  inline explicit token(const double val, label lineNum=0) noexcept;
526 
527  //- Copy construct word token
528  inline explicit token(const word& w, label lineNum=0);
529 
530  //- Copy construct string token
531  inline explicit token(const string& str, label lineNum=0);
533  //- Move construct word token
534  inline explicit token(word&& w, label lineNum=0);
535 
536  //- Move construct string token
537  inline explicit token(string&& str, label lineNum=0);
538 
539  //- Copy construct word/string token with the specified variant.
540  // A invalid word/string variant type is silently treated as STRING.
541  // No character stripping
542  inline explicit token(tokenType typ, const std::string&, label line=0);
543 
544  //- Copy construct word/string token with the specified variant.
545  // A invalid word/string variant type is silently treated as STRING.
546  // No character stripping
547  inline explicit token(tokenType typ, std::string&&, label line=0);
548 
549  //- Construct from a compound pointer, taking ownership
550  inline explicit token(token::compound* ptr, label lineNum=0);
551 
552  //- Move construct from a compound pointer, taking ownership
553  inline explicit token(autoPtr<token::compound>&& ptr, label lineNum=0);
554 
555  //- Construct from Istream
556  explicit token(Istream& is);
557 
558 
559  //- Destructor
560  inline ~token();
561 
562 
563  // Static Functions
564 
565  //- Create a bool token.
566  inline static token boolean(bool on) noexcept;
567 
568  //- Create a token with stream flags, no sanity check
569  //
570  // \param bitmask the flags to set
571  inline static token flag(int bitmask) noexcept;
572 
573  //- True if the character is a punctuation separator (eg, in ISstream).
574  // Since it could also start a number, SUBTRACT is not included as
575  // a separator.
576  //
577  // \param c the character to test, passed as int for consistency with
578  // isdigit, isspace etc.
579  inline static bool isseparator(int c) noexcept;
580 
581 
582  // Member Functions
583 
584  // Status
585 
586  //- Return the name for the token type
587  static word name(const token::tokenType tokType);
588 
589  //- Return the name of the current token type
590  inline word name() const;
591 
592  //- Return the token type
593  inline tokenType type() const noexcept;
594 
595  //- Change the token type, for similar types.
596  // This can be used to change between string-like variants
597  // (eg, STRING, VARIABLE, etc)
598  // To change types entirely (eg, STRING to DOUBLE),
599  // use the corresponding assignment operator.
600  //
601  // \return true if the change was successful or no change was required
602  inline bool setType(const tokenType tokType) noexcept;
603 
604  //- The line number for the token
605  inline label lineNumber() const noexcept;
606 
607  //- Change token line number, return old value
608  inline label lineNumber(const label lineNum) noexcept;
609 
610  //- True if token is not UNDEFINED or ERROR
611  inline bool good() const noexcept;
612 
613  //- Token is UNDEFINED
614  inline bool undefined() const noexcept;
615 
616  //- Token is ERROR
617  inline bool error() const noexcept;
618 
619  //- Token is BOOL
620  inline bool isBool() const noexcept;
621 
622  //- Token is FLAG
623  inline bool isFlag() const noexcept;
624 
625  //- Token is PUNCTUATION
626  inline bool isPunctuation() const noexcept;
627 
628  //- True if token is PUNCTUATION and equal to parameter
629  inline bool isPunctuation(const punctuationToken p) const noexcept;
630 
631  //- Token is PUNCTUATION and isseparator
632  inline bool isSeparator() const noexcept;
633 
634  //- Token is LABEL
635  inline bool isLabel() const noexcept;
636 
637  //- True if token is LABEL and equal to parameter
638  inline bool isLabel(const label val) const noexcept;
639 
640  //- Token is FLOAT
641  inline bool isFloat() const noexcept;
642 
643  //- Token is DOUBLE
644  inline bool isDouble() const noexcept;
645 
646  //- Token is FLOAT or DOUBLE
647  inline bool isScalar() const noexcept;
648 
649  //- Token is LABEL, FLOAT or DOUBLE
650  inline bool isNumber() const noexcept;
651 
652  //- Token is word-variant (WORD, DIRECTIVE)
653  inline bool isWord() const noexcept;
654 
655  //- Token is word-variant and equal to parameter
656  inline bool isWord(const std::string& s) const;
657 
658  //- Token is DIRECTIVE (word variant)
659  inline bool isDirective() const noexcept;
660 
661  //- Token is (quoted) STRING (string variant)
662  inline bool isQuotedString() const noexcept;
663 
664  //- Token is string-variant
665  //- (STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
666  inline bool isString() const noexcept;
667 
668  //- Token is EXPRESSION (string variant)
669  inline bool isExpression() const noexcept;
670 
671  //- Token is VARIABLE (string variant)
672  inline bool isVariable() const noexcept;
673 
674  //- Token is VERBATIM string (string variant)
675  inline bool isVerbatim() const noexcept;
676 
677  //- Token is CHAR_DATA (string variant)
678  inline bool isCharData() const noexcept;
679 
680  //- Token is word-variant or string-variant
681  //- (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
682  inline bool isStringType() const noexcept;
683 
684  //- Token is COMPOUND
685  inline bool isCompound() const noexcept;
686 
687  //- True if token is COMPOUND and its type() is equal to parameter
688  inline bool isCompound(const word& compoundType) const;
689 
690  //- If token is COMPOUND and can be dynamic_cast to the \c Type
691  //- return a const pointer to the content.
692  // Return nullptr on any failure.
693  template<class Type>
694  inline const Type* isCompound() const;
695 
696  //- True if token is not UNDEFINED or ERROR. Same as good().
697  explicit operator bool() const noexcept { return good(); }
698 
699 
700  // Access
701 
702  //- Return boolean token value.
703  // Report FatalIOError and return false if token is not BOOL or LABEL
704  inline bool boolToken() const;
705 
706  //- Return flag bitmask value.
707  // Report FatalIOError and return NO_FLAG if token is not FLAG
708  inline int flagToken() const;
709 
710  //- Return punctuation character.
711  // Report FatalIOError and return \b \\0 if token is not PUNCTUATION
712  inline punctuationToken pToken() const;
713 
714  //- Return label value.
715  // Report FatalIOError and return \b 0 if token is not LABEL
716  inline label labelToken() const;
717 
718  //- Return float value.
719  // Report FatalIOError and return \b 0 if token is not FLOAT
720  inline float floatToken() const;
721 
722  //- Return double value.
723  // Report FatalIOError and return \b 0 if token is not DOUBLE
724  inline double doubleToken() const;
725 
726  //- Return float or double value.
727  // Report FatalIOError and return \b 0 if token is not a
728  // FLOAT or DOUBLE
729  inline scalar scalarToken() const;
730 
731  //- Return label, float or double value.
732  // Report FatalIOError and return \b 0 if token is not a
733  // LABEL, FLOAT or DOUBLE
734  inline scalar number() const;
735 
736  //- Return const reference to the word contents.
737  // Report FatalIOError and return \b "" if token is not a
738  // WORD or DIRECTIVE
739  inline const word& wordToken() const;
740 
741  //- Return const reference to the string contents.
742  // Report FatalIOError and return \b "" if token is not a
743  // STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA
744  // or an upcast WORD or DIRECTIVE
745  inline const string& stringToken() const;
746 
747  //- Const reference to compound token. Fatal if the wrong type.
748  inline const compound& compoundToken() const;
749 
750  //- Return reference to compound token. Fatal if the wrong type.
751  inline compound& refCompoundToken();
752 
753  //- Default construct the specified compound type and read from stream.
754  // A no-op and returns false if compound type is unknown.
755  // Modify the token and return true on success.
756  bool readCompoundToken
757  (
758  const word& compoundType,
759  Istream& is,
760  const bool readContent = true
761  );
762 
763  //- Return reference to compound and mark internally as \em released.
764  // Optional Istream parameter only as reference for errors messages.
765  compound& transferCompoundToken(const Istream* is = nullptr);
766 
767  //- Return reference to compound and mark internally as \em released.
768  // The Istream is used for reference error messages only.
769  inline compound& transferCompoundToken(const Istream& is);
770 
771  //- Mark the compound as \em released and return a reference
772  //- to the underlying encapsulated container - eg, \c List<label>
773  // The Istream is used for reference error messages only.
774  template<class Type>
775  inline Type& transferCompoundToken(const Istream& is);
776 
777 
778  // Edit
779 
780  //- Reset token to UNDEFINED and clear any allocated storage
781  inline void reset();
782 
783  //- Clear token and set to be ERROR.
784  inline void setBad();
785 
786  //- Swap token contents: type, data, line-number
787  inline void swap(token& tok) noexcept;
788 
789 
790  // Info
791 
792  //- Return info proxy,
793  //- for printing token information to a stream
794  InfoProxy<token> info() const noexcept { return *this; }
795 
796 
797  // Member Operators
798 
799  // Assignment
800 
801  //- Copy assign
802  inline void operator=(const token& tok);
803 
804  //- Move assign
805  inline void operator=(token&& tok);
806 
807  //- Copy assign from punctuation
808  inline void operator=(const punctuationToken p);
809 
810  //- Copy assign from label
811  inline void operator=(const label val);
812 
813  //- Copy assign from float
814  inline void operator=(const float val);
815 
816  //- Copy assign from double
817  inline void operator=(const double val);
818 
819  //- Copy assign from word content
820  inline void operator=(const word& w);
821 
822  //- Copy assign from string content
823  inline void operator=(const string& str);
824 
825  //- Move assign from word content
826  inline void operator=(word&& w);
827 
828  //- Move assign from string content
829  inline void operator=(string&& str);
830 
831  //- Assign compound with reference counting to token
832  inline void operator=(token::compound* ptr);
833 
834  //- Move assign from compound pointer
835  inline void operator=(autoPtr<token::compound>&& ptr);
836 
837 
838  // Equality
839 
840  inline bool operator==(const token& tok) const;
841  inline bool operator==(const punctuationToken p) const noexcept;
842  inline bool operator==(const label val) const noexcept;
843  inline bool operator==(const float val) const noexcept;
844  inline bool operator==(const double val) const noexcept;
845  inline bool operator==(const std::string& s) const;
846 
847 
848  // Inequality
849 
850  inline bool operator!=(const token& tok) const;
851  inline bool operator!=(const punctuationToken p) const noexcept;
852  inline bool operator!=(const label val) const noexcept;
853  inline bool operator!=(const float val) const noexcept;
854  inline bool operator!=(const double val) const noexcept;
855  inline bool operator!=(const std::string& s) const;
856 
857 
858  // IOstream Operators
859 
860  friend Ostream& operator<<(Ostream& os, const token& tok);
861 
862  friend Ostream& operator<<(Ostream& os, const punctuationToken& pt);
863  friend ostream& operator<<(ostream& os, const punctuationToken& pt);
864 
865  friend ostream& operator<<(ostream& os, const InfoProxy<token>& ip);
866 
867 
868  // Housekeeping
869 
870  //- Write access for the token line number
871  // \deprecated(2021-03) - use lineNumber(label)
872  label& lineNumber() noexcept { return line_; }
873 
874  //- Token is FLOAT
875  // \deprecated(2020-01) - isFloat()
876  bool isFloatScalar() const { return isFloat(); };
877 
878  //- Token is DOUBLE
879  // \deprecated(2020-01) - isDouble()
880  bool isDoubleScalar() const { return isDouble(); }
881 
882  //- Return float value.
883  // \deprecated(2020-01) - floatToken()
884  float floatScalarToken() const { return floatToken(); }
885 
886  //- Return double value.
887  // \deprecated(2020-01) - doubleToken()
888  double doubleScalarToken() const { return doubleToken(); }
889 
890  //- Deprecated(2017-11) transfer word pointer to the token
891  // \deprecated(2017-11) - use move assign from word
892  void operator=(word*) = delete;
893 
894  //- Deprecated(2017-11) transfer string pointer to the token
895  // \deprecated(2017-11) - use move assign from string
896  void operator=(string*) = delete;
897 };
898 
899 
900 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
901 
902 // IOstream Operators
903 
904 Istream& operator>>(Istream& is, token& tok);
905 Ostream& operator<<(Ostream& os, const token::punctuationToken& pt);
906 ostream& operator<<(ostream& os, const token::punctuationToken& pt);
907 Ostream& operator<<(Ostream& os, const token::compound& ct);
908 
909 ostream& operator<<(ostream& os, const InfoProxy<token>& ip);
910 
911 template<>
912 Ostream& operator<<(Ostream& os, const InfoProxy<token>& ip);
913 
914 
915 // Handling of compound types
916 
917 //- Define compound using \a Type for its name
918 #define defineCompoundTypeName(Type, UnusedTag) \
919  defineTemplateTypeNameWithName(token::Compound<Type>, #Type);
920 
921 //- Define compound using \a Name for its name
922 #define defineNamedCompoundTypeName(Type, Name) \
923  defineTemplateTypeNameWithName(token::Compound<Type>, #Name);
924 
925 //- Add compound to selection tables, lookup using typeName
926 #define addCompoundToRunTimeSelectionTable(Type, Tag) \
927  token::compound::addemptyConstructorToTable<token::Compound<Type>> \
928  add##Tag##emptyConstructorToTable_;
929 
930 //- Add compound to selection tables, lookup as \a Name
931 #define addNamedCompoundToRunTimeSelectionTable(Type, Tag, Name) \
932  token::compound::addemptyConstructorToTable<token::Compound<Type>> \
933  add##Tag##emptyConstructorToTable_(#Name);
934 
935 
936 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
937 
938 } // End namespace Foam
939 
940 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
941 
942 #include "tokenI.H"
943 #include "Istream.H"
944 
945 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
946 
947 #endif
948 
949 // ************************************************************************* //
Subtract or start of negative number.
Definition: token.H:145
static bool isCompound(const word &compoundType)
True if a known (registered) compound type.
Definition: token.C:104
virtual void read(Istream &is)=0
Read from stream into the underlying content.
Begin block [isseparator].
Definition: token.H:165
static const token undefinedToken
An undefined token.
Definition: token.H:542
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:507
double doubleScalarToken() const
Return double value.
Definition: token.H:1211
static autoPtr< compound > New(const word &compoundType)
Default construct specified compound type.
Definition: token.C:52
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:561
bool operator!=(const token &tok) const
Definition: tokenI.H:1158
bool isDouble() const noexcept
Token is DOUBLE.
Definition: tokenI.H:645
boolean type
Definition: token.H:84
Left parenthesis [isseparator].
Definition: token.H:149
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:711
BINARY-mode stream.
Definition: token.H:119
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:587
uint8_t direction
Definition: direction.H:46
A line primitive.
Definition: line.H:52
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
single character punctuation
Definition: token.H:83
No flags.
Definition: token.H:117
patchWriters resize(patchIds.size())
Reference counter for various OpenFOAM components.
Definition: refCount.H:44
stream flag (1-byte bitmask)
Definition: token.H:82
Double quote.
Definition: token.H:141
bool isCompound() const noexcept
Token is COMPOUND.
Definition: tokenI.H:803
scalar number() const
Return label, float or double value.
Definition: tokenI.H:695
Begin dimensions [isseparator].
Definition: token.H:163
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
TypeNameNoDebug("Compound<T>")
Declare type-name, virtual type (without debug switch)
tokenType
Enumeration defining the types of token.
Definition: token.H:76
Newline [isspace].
Definition: token.H:130
bool moved() const noexcept
Get compound transferred status.
Definition: token.H:264
The vector-space number of components: default is 1.
Definition: pTraits.H:168
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:405
bool isFlag() const noexcept
Token is FLAG.
Definition: tokenI.H:543
Begin string with double quote.
Definition: token.H:168
friend Ostream & operator<<(Ostream &os, const token &tok)
Begin list [isseparator].
Definition: token.H:161
Subtract or start of negative number.
Definition: token.H:159
Addition [isseparator].
Definition: token.H:158
Assignment/equals [isseparator].
Definition: token.H:143
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
virtual ~compound() noexcept=default
Destructor.
End entry [isseparator].
Definition: token.H:160
bool undefined() const noexcept
Token is UNDEFINED.
Definition: tokenI.H:513
virtual void resize(const label n)
Change the size of the underlying container content.
Definition: token.H:468
scalar scalarToken() const
Return float or double value.
Definition: tokenI.H:673
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:531
Dollar - start variable or expression.
Definition: token.H:137
bool readCompoundToken(const word &compoundType, Istream &is, const bool readContent=true)
Default construct the specified compound type and read from stream.
Definition: token.C:126
word name() const
Return the name of the current token type.
Definition: tokenI.H:399
Foam::word.
Definition: token.H:90
declareRunTimeSelectionTable(autoPtr, compound, empty,(),())
Declare run-time constructor selection table.
static constexpr const char *const typeName
The type name is "token".
Definition: token.H:617
virtual void fill_zero()=0
Fill with zero values, or with default constructed.
virtual direction nComponents() const
The number of vector-space or other components of the underlying data content.
Definition: token.H:508
virtual std::streamsize size_bytes() const
Size of the (contiguous) byte data.
Definition: token.H:532
End dimensions [isseparator].
Definition: token.H:164
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
virtual void fill_zero()
Fill with zero value or with default value initialized.
Definition: token.H:476
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:549
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
Right square bracket [isseparator].
Definition: token.H:152
Abstract base class for complex tokens.
Definition: token.H:176
Compound()=default
Default construct.
A templated class for holding compound tokens. The underlying container is normally a List of values...
Definition: token.H:359
static autoPtr< Compound< T > > New(Args &&... args)
Construct autoPtr compound type with forwarding arguments.
Definition: token.H:446
label (integer) type
Definition: token.H:85
Addition [isseparator].
Definition: token.H:144
Right parenthesis [isseparator].
Definition: token.H:150
Comma [isseparator].
Definition: token.H:135
Compound type such as List<label> etc.
Definition: token.H:92
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
Left square bracket [isseparator].
Definition: token.H:151
Single quote.
Definition: token.H:140
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
bool isStringType() const noexcept
Token is word-variant or string-variant (WORD, DIRECTIVE, STRING, EXPRESSION, VARIABLE, VERBATIM, CHAR_DATA)
Definition: tokenI.H:777
virtual std::streamsize size_bytes() const =0
Size of the (contiguous) byte data.
virtual const char * cdata_bytes() const =0
Pointer to the underlying data as byte data.
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
virtual void resize(const label n)=0
Change the size of the underlying container content.
Istream & operator>>(Istream &, directionInfo &)
End string with double quote.
Definition: token.H:169
flagType
Stream or output control flags (1-byte width)
Definition: token.H:115
Space [isspace].
Definition: token.H:131
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
virtual void read(Istream &is)
Redirect read to underlying content.
Definition: token.H:481
const compound & compoundToken() const
Const reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:831
End list [isseparator].
Definition: token.H:162
float (single-precision) type
Definition: token.H:86
virtual direction nComponents() const =0
The number of vector-space or other components of the underlying data content.
float floatScalarToken() const
Return float value.
Definition: token.H:1204
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
Divide [isseparator].
Definition: token.H:147
bool isBool() const noexcept
Token is BOOL.
Definition: tokenI.H:525
constexpr compound() noexcept
Default construct.
Definition: token.H:214
Token error encountered.
Definition: token.H:79
bool setType(const tokenType tokType) noexcept
Change the token type, for similar types.
Definition: tokenI.H:411
The TAB Method for Numerical Calculation of Spray Droplet Breakup.
Definition: TAB.H:60
OBJstream os(runTime.globalPath()/outputName)
bool isFloatScalar() const
Token is FLOAT.
Definition: token.H:1190
bool isDoubleScalar() const
Token is DOUBLE.
Definition: token.H:1197
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:783
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Left brace [isseparator].
Definition: token.H:153
unsigned char state_
The compound token state, internally used values only.
Definition: token.H:187
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
Foam::string (usually double-quoted)
Definition: token.H:91
virtual tokenType typeCode() const =0
The token type (if any) corresponding to the contained component type (LABEL, FLOAT, DOUBLE, etc).
void operator=(const token &tok)
Copy assign.
Definition: tokenI.H:917
TypeNameNoDebug("compound")
Declare type-name, virtual type (without debug switch)
virtual const char * cdata_bytes() const
Pointer to the underlying data as byte data.
Definition: token.H:516
bool isCharData() const noexcept
Token is CHAR_DATA (string variant)
Definition: tokenI.H:771
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
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
virtual label size() const =0
The size of the underlying content.
A helper class for outputting values to Ostream.
Definition: ensightCells.H:43
ASCII-mode stream.
Definition: token.H:118
const dimensionedScalar c
Speed of light in a vacuum.
virtual char * data_bytes()=0
Pointer to the underlying data as byte data.
label labelToken() const
Return label value.
Definition: tokenI.H:615
Semicolon [isseparator].
Definition: token.H:134
double (double-precision) type
Definition: token.H:87
Right brace [isseparator].
Definition: token.H:154
T::value_type value_type
The type of values held by the compound.
Definition: token.H:391
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
The &#39;at&#39; symbol.
Definition: token.H:139
InfoProxy< token > info() const noexcept
Return info proxy, for printing token information to a stream.
Definition: token.H:1078
compound & refCompoundToken()
Return reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:841
label n
String-variant: plain character content.
Definition: token.H:103
virtual char * data_bytes()
Pointer to the underlying data as byte data.
Definition: token.H:524
virtual void write(Ostream &os) const
Redirect write to underlying content.
Definition: token.H:490
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
const Type * isA() const
Check if dynamic_cast to Type is possible.
Definition: token.H:248
Macros to ease declaration of run-time selection tables.
Question mark (eg, ternary)
Definition: token.H:138
volScalarField & p
Compound< T > & operator=(const Compound< T > &)=delete
No copy assignment.
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))
End block [isseparator].
Definition: token.H:166
Foam::argList args(argc, argv)
Nul character.
Definition: token.H:128
bool isNumber() const noexcept
Token is LABEL, FLOAT or DOUBLE.
Definition: tokenI.H:689
An undefined token-type.
Definition: token.H:78
virtual void write(Ostream &os) const =0
Write the underlying content.
friend Ostream & operator<<(Ostream &os, const compound &ct)
Output operator.
bool isVerbatim() const noexcept
Token is VERBATIM string (string variant)
Definition: tokenI.H:765
Multiply [isseparator].
Definition: token.H:146
bool pending() const noexcept
Get compound pending-read status.
Definition: token.H:277
Namespace for OpenFOAM.
Hash - directive or start verbatim string.
Definition: token.H:136
virtual label size() const
The size of the underlying content.
Definition: token.H:460
bool isQuotedString() const noexcept
Token is (quoted) STRING (string variant)
Definition: tokenI.H:741
Colon [isseparator].
Definition: token.H:133