ITstream.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-2022 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::ITstream
29
30Description
31 An input stream of tokens.
32
33SourceFiles
34 ITstream.C
35
36\*---------------------------------------------------------------------------*/
37
38#ifndef Foam_ITstream_H
39#define Foam_ITstream_H
40
41#include "Istream.H"
42#include "tokenList.H"
43
44// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45
46namespace Foam
47{
48
49/*---------------------------------------------------------------------------*\
50 Class ITstream Declaration
51\*---------------------------------------------------------------------------*/
53class ITstream
54:
55 public Istream,
56 public tokenList
57{
58 // Private Data
59
60 //- Name associated with the stream
61 fileName name_;
62
63 //- Index of token currently being read
64 label tokenIndex_;
65
66
67 // Private Member Functions
68
69 //- An ad hoc combination of reserve and setCapacity somewhat
70 //- similar to DynamicList.
71 //
72 // In lazy mode, increase list size if needed, but leave any
73 // excess capacity - works like reserve.
74 //
75 // In non-lazy mode, set exact capacity
76 void reserveCapacity(const label nElem, const bool lazy);
77
78
79public:
80
81 // Constructors
82
83 //- Copy construct
84 ITstream(const ITstream& is);
85
86 //- Move construct
87 ITstream(ITstream&& is);
88
89 //- Default construct. Empty stream, optionally with given name
90 explicit ITstream
91 (
92 IOstreamOption streamOpt = IOstreamOption(),
93 const string& name = "input"
94 );
95
96 //- Construct empty, optionally with given name
97 explicit ITstream
98 (
99 const Foam::zero,
100 const string& name = "input",
101 IOstreamOption streamOpt = IOstreamOption()
102 );
103
104 //- Copy construct from tokens, with given name
106 (
107 const string& name,
108 const UList<token>& tokens,
109 IOstreamOption streamOpt = IOstreamOption()
110 );
111
112 //- Move construct from tokens, with given name
114 (
115 const string& name,
116 List<token>&& tokens,
117 IOstreamOption streamOpt = IOstreamOption()
118 );
119
120 //- Construct token list by parsing the input character sequence
121 // Uses static parse function internally.
122 explicit ITstream
123 (
124 const UList<char>& input,
125 IOstreamOption streamOpt = IOstreamOption(),
126 const string& name = "input"
127 );
128
129 //- Construct token list by parsing the input string
130 // Uses static parse function internally.
131 explicit ITstream
132 (
133 const std::string& input,
134 IOstreamOption streamOpt = IOstreamOption(),
135 const string& name = "input"
136 );
137
138 //- Construct token list by parsing the input character sequence
139 // Uses static parse function internally.
140 explicit ITstream
141 (
142 const char* input,
143 IOstreamOption streamOpt = IOstreamOption(),
144 const string& name = "input"
145 );
146
147
148 //- Destructor
149 virtual ~ITstream() = default;
150
151
152 // Static Functions
153
154 //- Create token list by parsing the input character sequence
155 //- until no good tokens remain.
156 static tokenList parse
157 (
158 const UList<char>& input,
159 IOstreamOption streamOpt = IOstreamOption()
160 );
161
162 //- Create token list by parsing the input string
163 //- until no good tokens remain.
164 static tokenList parse
165 (
166 const std::string& input,
167 IOstreamOption streamOpt = IOstreamOption()
168 );
169
170 //- Create token list by parsing the input character sequence
171 //- until no good tokens remain.
172 static tokenList parse
173 (
174 const char* input,
175 IOstreamOption streamOpt = IOstreamOption()
176 );
177
178
179 // Member Functions
180
181 // Token Access
182
183 //- True if putback token is in use
184 bool hasPutback() const noexcept
185 {
186 return Istream::hasPutback();
187 }
188
189 //- Failsafe peek at the \b first token in the list.
190 // \return \c undefinedToken if the list is empty.
191 const token& peekFirst() const;
192
193 //- Failsafe peek at the \b last token in the list.
194 // \return \c undefinedToken if the list is empty.
195 const token& peekLast() const;
196
197 //- Failsafe peek at what the next read would return,
198 // including handling of any putback
199 // \return \c undefinedToken if list is exhausted
200 const token& peek() const;
201
202 //- The current token index when reading, or the insertion point.
203 label tokenIndex() const noexcept
204 {
205 return tokenIndex_;
206 }
207
208 //- Non-const access to the current token index
209 label& tokenIndex() noexcept
210 {
211 return tokenIndex_;
212 }
213
214 //- Number of tokens remaining
215 label nRemainingTokens() const noexcept
216 {
217 return size() - tokenIndex_;
218 }
219
220 //- Move tokenIndex to the specified position
221 // Using seek(0) is identical to rewind.
222 // Using seek(-1) moves to the end.
223 void seek(label pos);
224
225 //- Move tokenIndex relative to the current position.
226 // Will not overrun the beginning or end positions.
227 //
228 // Use skip(2) to move forward two tokens.
229 // Use skip(-2) to move backward two tokens.
230 void skip(label n = 1);
231
232
233 // Inquiry
234
235 //- Get the name of the stream
236 virtual const fileName& name() const
237 {
238 return name_;
239 }
240
241 //- Return stream name for modification
242 virtual fileName& name()
243 {
244 return name_;
245 }
246
247
248 // Token list modification
249
250 //- Copy append a token at the current tokenIndex,
251 //- incrementing the index.
252 void append(const token& t, const bool lazy);
253
254 //- Move append a token at the current tokenIndex,
255 //- incrementing the index.
256 void append(token&& t, const bool lazy);
257
258 //- Copy append a list of tokens at the current tokenIndex,
259 //- incrementing the index.
260 //
261 // \param newTokens the list of tokens to copy append
262 // \param lazy leaves any excess capacity for further appends.
263 // The caller will be responsible for resizing later.
264 void append(const UList<token>& newTokens, const bool lazy);
265
266 //- Move append a list of tokens at the current tokenIndex,
267 //- incrementing the index.
268 //
269 // \param newTokens the list of tokens to move append
270 // \param lazy leaves any excess capacity for further appends.
271 // The caller will be responsible for resizing later.
272 void append(List<token>&& newTokens, const bool lazy);
273
274
275 // Stream State Functions
276
277 //- Get stream flags - always 0
278 virtual ios_base::fmtflags flags() const
279 {
280 return ios_base::fmtflags(0);
281 }
282
283 //- Set flags of stream - ignored
284 ios_base::fmtflags flags(const ios_base::fmtflags)
285 {
286 return ios_base::fmtflags(0);
287 }
288
289
290 // Read Functions
291
292 //- Return next token from stream
293 virtual Istream& read(token& tok);
294
295 //- Read a character
296 virtual Istream& read(char&);
297
298 //- Read a word
299 virtual Istream& read(word&);
300
301 // Read a string (including enclosing double-quotes)
302 virtual Istream& read(string&);
303
304 //- Read a label
305 virtual Istream& read(label&);
306
307 //- Read a floatScalar
308 virtual Istream& read(floatScalar&);
309
310 //- Read a doubleScalar
311 virtual Istream& read(doubleScalar&);
312
313 //- Read binary block
314 // \note Not implemented
315 virtual Istream& read(char* data, std::streamsize);
316
317 //- Low-level raw binary read
318 // \note Not implemented
319 virtual Istream& readRaw(char* data, std::streamsize count);
320
321 //- Start of low-level raw binary read
322 virtual bool beginRawRead()
323 {
324 return false;
325 }
326
327 //- End of low-level raw binary read
328 virtual bool endRawRead()
329 {
330 return false;
331 }
332
333 //- Rewind the stream so that it may be read again
334 virtual void rewind();
335
336
337 // Output
338
339 //- Print stream description to Ostream
340 void print(Ostream& os) const;
341
342 //- Concatenate tokens into a space-separated std::string.
343 //- The resulting string may contain quote characters.
344 std::string toString() const;
345
346
347 // Member Operators
348
349 //- Use operator() from Istream
350 using Istream::operator();
351
352 //- Copy assignment, with rewind()
353 void operator=(const ITstream& is);
354
355 //- Copy assignment of tokens, with rewind()
356 void operator=(const UList<token>& toks);
357
358 //- Move assignment of tokens, with rewind()
359 void operator=(List<token>&& toks);
360
361
362 // Additional constructors and methods (as per v2012 and earlier)
363 #ifdef Foam_IOstream_extras
364
365 //- Construct from components, copying the tokens
367 (
368 const string& name,
369 const UList<token>& tokens,
372 )
373 :
374 ITstream(name, tokens, IOstreamOption(fmt, ver))
375 {}
376
377 //- Construct from components, transferring the tokens
378 ITstream
379 (
380 const string& name,
381 List<token>&& tokens,
383 IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
384 )
385 :
386 ITstream(name, std::move(tokens), IOstreamOption(fmt, ver))
387 {}
388
389 #endif /* Foam_IOstream_extras */
390};
391
392
393// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394
395} // End namespace Foam
396
397// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398
399#endif
400
401// ************************************************************************* //
label n
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary)
static const versionNumber currentVersion
The current version number (2.0)
An input stream of tokens.
Definition: ITstream.H:56
void append(const token &t, const bool lazy)
Definition: ITstream.C:567
virtual fileName & name()
Return stream name for modification.
Definition: ITstream.H:241
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352
std::string toString() const
Definition: ITstream.C:320
virtual Istream & read(token &tok)
Return next token from stream.
Definition: ITstream.C:453
const token & peekFirst() const
Failsafe peek at the first token in the list.
Definition: ITstream.C:340
virtual const fileName & name() const
Get the name of the stream.
Definition: ITstream.H:235
virtual bool beginRawRead()
Start of low-level raw binary read.
Definition: ITstream.H:321
label tokenIndex() const noexcept
The current token index when reading, or the insertion point.
Definition: ITstream.H:202
const token & peekLast() const
Failsafe peek at the last token in the list.
Definition: ITstream.C:346
label nRemainingTokens() const noexcept
Number of tokens remaining.
Definition: ITstream.H:214
void print(Ostream &os) const
Print stream description to Ostream.
Definition: ITstream.C:295
void seek(label pos)
Move tokenIndex to the specified position.
Definition: ITstream.C:364
void skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:411
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream - ignored.
Definition: ITstream.H:283
label & tokenIndex() noexcept
Non-const access to the current token index.
Definition: ITstream.H:208
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
static tokenList parse(const UList< char > &input, IOstreamOption streamOpt=IOstreamOption())
Definition: ITstream.C:82
virtual ~ITstream()=default
Destructor.
bool hasPutback() const noexcept
True if putback token is in use.
Definition: ITstream.H:183
virtual Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
Definition: ITstream.C:547
virtual ios_base::fmtflags flags() const
Get stream flags - always 0.
Definition: ITstream.H:277
virtual bool endRawRead()
End of low-level raw binary read.
Definition: ITstream.H:327
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:617
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:79
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:420
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
A class for handling file names.
Definition: fileName.H:76
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:63
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
dimensionedScalar pos(const dimensionedScalar &ds)
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
const direction noexcept
Definition: Scalar.H:223