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