tokenIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "token.H"
28 
29 #include "IOstreams.H"
30 #include "scalar.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  type_(UNDEFINED)
37 {
38  is.read(*this);
39 }
40 
41 
42 // * * * * * * * * * * * * IOstream operators * * * * * * * * * * * * * * * //
43 
45 {
46  t.clear();
47  return is.read(t);
48 }
49 
50 
52 {
53  return os.write(t);
54 }
55 
56 
57 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
58 {
59  return os << char(pt);
60 }
61 
62 
64 {
65  return os << char(pt);
66 }
67 
68 
70 {
71  os << ct.type() << token::SPACE;
72  ct.write(os);
73 
74  return os;
75 }
76 
77 
78 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
79 
80 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
81 {
82  const token& t = ip.t_;
83 
84  os << "on line " << t.lineNumber();
85 
86  switch (t.type())
87  {
88  case token::UNDEFINED:
89  os << " an undefined token";
90  break;
91 
92  case token::PUNCTUATION:
93  os << " the punctuation token " << '\'' << t.pToken() << '\'';
94  break;
95 
96  case token::WORD:
97  os << " the word " << '\'' << t.wordToken() << '\'';
98  break;
99 
100  case token::STRING:
101  os << " the string " << t.stringToken();
102  break;
103 
105  os << " the verbatim string " << t.verbatimStringToken();
106  break;
107 
108  case token::FUNCTIONNAME:
109  os << " the functionName " << t.functionNameToken();
110  break;
111 
112  case token::VARIABLE:
113  os << " the variable " << t.variableToken();
114  break;
115 
116  case token::INTEGER_32:
117  os << " the 32-bit integer " << t.integer32Token();
118  break;
119 
120  case token::INTEGER_64:
121  os << " the 64-bit integer " << t.integer64Token();
122  break;
123 
125  os << " the unsigned 32-bit integer "
126  << t.unsignedInteger32Token();
127  break;
128 
130  os << " the unsigned 64-bit integer "
131  << t.unsignedInteger64Token();
132  break;
133 
134  case token::FLOAT_SCALAR:
135  os << " the floatScalar " << t.floatScalarToken();
136  break;
137 
139  os << " the doubleScalar " << t.doubleScalarToken();
140  break;
141 
143  os << " the longDoubleScalar " << t.longDoubleScalarToken();
144  break;
145 
146  case token::COMPOUND:
147  {
148  if (t.compoundToken().empty())
149  {
150  os << " the empty compound of type "
151  << t.compoundToken().type();
152  }
153  else
154  {
155  os << " the compound of type "
156  << t.compoundToken().type();
157  }
158  }
159  break;
160 
161  case token::ERROR:
162  os << " an error";
163  break;
164 
165  default:
166  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
167  }
168 
169  return os;
170 }
171 
172 
173 template<>
175 {
176  const token& t = ip.t_;
177 
178  os << "on line " << t.lineNumber();
179 
180  switch (t.type())
181  {
182  case token::UNDEFINED:
183  os << " an undefined token";
184  break;
185 
186  case token::PUNCTUATION:
187  os << " the punctuation token " << '\'' << t.pToken() << '\'';
188  break;
189 
190  case token::WORD:
191  os << " the word " << '\'' << t.wordToken() << '\'';
192  break;
193 
194  case token::STRING:
195  os << " the string " << t.stringToken();
196  break;
197 
199  os << " the verbatim string " << t.verbatimStringToken();
200  break;
201 
202  case token::FUNCTIONNAME:
203  os << " the functionName " << t.functionNameToken();
204  break;
205 
206  case token::VARIABLE:
207  os << " the variable " << t.variableToken();
208  break;
209 
210  break;
211  case token::INTEGER_32:
212  os << " the 32-bit integer " << t.integer32Token();
213  break;
214 
215  case token::INTEGER_64:
216  os << " the 64-bit integer " << t.integer64Token();
217  break;
218 
220  os << " the unsigned 32-bit integer "
221  << t.unsignedInteger32Token();
222  break;
223 
225  os << " the unsigned 64-bit integer "
226  << t.unsignedInteger64Token();
227  break;
228 
229  case token::FLOAT_SCALAR:
230  os << " the floatScalar " << t.floatScalarToken();
231  break;
232 
234  os << " the doubleScalar " << t.doubleScalarToken();
235  break;
236 
238  os << " the longDoubleScalar " << t.longDoubleScalarToken();
239  break;
240 
241  case token::COMPOUND:
242  {
243  if (t.compoundToken().empty())
244  {
245  os << " the empty compound of type "
246  << t.compoundToken().type();
247  }
248  else
249  {
250  os << " the compound of type "
251  << t.compoundToken().type();
252  }
253  }
254  break;
255 
256  case token::ERROR:
257  os << " an error";
258  break;
259 
260  default:
261  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
262  }
263 
264  return os;
265 }
266 
267 
268 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
const T & t_
Definition: InfoProxy.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
virtual Ostream & write(const token &)
Write token.
Definition: Ostream.C:51
Abstract base class for complex tokens.
Definition: token.H:134
virtual void write(Ostream &) const =0
bool empty() const
Definition: token.H:184
A token holds items read from Istream.
Definition: token.H:74
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:746
const variable & variableToken() const
Definition: tokenI.H:383
@ ERROR
Definition: token.H:98
@ VARIABLE
Definition: token.H:86
@ WORD
Definition: token.H:84
@ UNSIGNED_INTEGER_32
Definition: token.H:91
@ UNDEFINED
Definition: token.H:81
@ COMPOUND
Definition: token.H:96
@ FLOAT_SCALAR
Definition: token.H:93
@ INTEGER_64
Definition: token.H:90
@ DOUBLE_SCALAR
Definition: token.H:94
@ LONG_DOUBLE_SCALAR
Definition: token.H:95
@ VERBATIMSTRING
Definition: token.H:88
@ FUNCTIONNAME
Definition: token.H:85
@ UNSIGNED_INTEGER_64
Definition: token.H:92
@ INTEGER_32
Definition: token.H:89
@ STRING
Definition: token.H:87
@ PUNCTUATION
Definition: token.H:83
int32_t integer32Token() const
Definition: tokenI.H:485
const functionName & functionNameToken() const
Definition: tokenI.H:365
floatScalar floatScalarToken() const
Definition: tokenI.H:708
punctuationToken
Standard punctuation tokens.
Definition: token.H:103
const string & stringToken() const
Definition: tokenI.H:401
punctuationToken pToken() const
Definition: tokenI.H:329
uint32_t unsignedInteger32Token() const
Definition: tokenI.H:556
int64_t integer64Token() const
Definition: tokenI.H:519
tokenType type() const
Definition: tokenI.H:299
const compound & compoundToken() const
Definition: tokenI.H:833
uint64_t unsignedInteger64Token() const
Definition: tokenI.H:590
doubleScalar doubleScalarToken() const
Definition: tokenI.H:727
token()
Construct null.
Definition: tokenI.H:75
const word & wordToken() const
Definition: tokenI.H:347
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:419
label lineNumber() const
Definition: tokenI.H:847
Istream & operator>>(Istream &, pointEdgeDist &)
Definition: pointEdgeDist.C:41
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)