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 -------------------------------------------------------------------------------
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::ITstream
29 
30 Description
31  An input stream of tokens.
32 
33 SourceFiles
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 
46 namespace Foam
47 {
48 
49 /*---------------------------------------------------------------------------*\
50  Class ITstream Declaration
51 \*---------------------------------------------------------------------------*/
52 
53 class 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 
79  //- Failsafe read-access to token at specified location
80  //- or undefinedToken
81  inline const token& peekAt(const label i) const
82  {
83  return
84  (
85  i >= 0 && i < tokenList::size()
86  ? tokenList::operator[](i)
88  );
89  }
90 
91 
92 public:
93 
94  // Constructors
95 
96  //- Copy construct
97  ITstream(const ITstream& is);
98 
99  //- Move construct
100  ITstream(ITstream&& is);
101 
102  //- Default construct. Empty stream, optionally with given name
103  explicit ITstream
104  (
105  IOstreamOption streamOpt = IOstreamOption(),
106  const string& name = "input"
107  );
108 
109  //- Construct empty, optionally with given name
110  explicit ITstream
111  (
112  const Foam::zero,
113  const string& name = "input",
114  IOstreamOption streamOpt = IOstreamOption()
115  );
116 
117  //- Copy construct from tokens, optionally with given name
118  explicit ITstream
119  (
120  const UList<token>& tokens,
121  IOstreamOption streamOpt = IOstreamOption(),
122  const string& name = "input"
123  );
124 
125  //- Move construct from tokens, optionally with given name
126  explicit ITstream
127  (
128  List<token>&& tokens,
129  IOstreamOption streamOpt = IOstreamOption(),
130  const string& name = "input"
131  );
132 
133  //- Construct token list by parsing the input character sequence
134  // Uses static parse function internally.
135  explicit ITstream
136  (
137  const UList<char>& input,
138  IOstreamOption streamOpt = IOstreamOption(),
139  const string& name = "input"
140  );
141 
142  //- Construct token list by parsing the input string
143  // Uses static parse function internally.
144  explicit ITstream
145  (
146  const std::string& input,
147  IOstreamOption streamOpt = IOstreamOption(),
148  const string& name = "input"
149  );
150 
151  //- Construct token list by parsing the input character sequence
152  // Uses static parse function internally.
153  explicit ITstream
154  (
155  const char* input,
156  IOstreamOption streamOpt = IOstreamOption(),
157  const string& name = "input"
158  );
159 
160 
161  // Additional constructors
162 
163  //- Copy construct from tokens, with given name
164  ITstream
165  (
166  const string& name,
167  const UList<token>& tokens,
168  IOstreamOption streamOpt = IOstreamOption()
169  )
170  :
171  ITstream(tokens, streamOpt, name)
172  {}
173 
174  //- Move construct from tokens, with given name
175  ITstream
176  (
177  const string& name,
178  List<token>&& tokens,
179  IOstreamOption streamOpt = IOstreamOption()
180  )
181  :
182  ITstream(std::move(tokens), streamOpt, name)
183  {}
184 
185 
186  //- Destructor
187  virtual ~ITstream() = default;
188 
189 
190  // Static Functions
191 
192  //- Create token list by parsing the input character sequence
193  //- until no good tokens remain.
194  static tokenList parse
195  (
196  const UList<char>& input,
197  IOstreamOption streamOpt = IOstreamOption()
198  );
199 
200  //- Create token list by parsing the input string
201  //- until no good tokens remain.
202  static tokenList parse
203  (
204  const std::string& input,
206  );
207 
208  //- Create token list by parsing the input character sequence
209  //- until no good tokens remain.
210  static tokenList parse
211  (
212  const char* input,
213  IOstreamOption streamOpt = IOstreamOption()
214  );
215 
216 
217  // Member Functions
218 
219  // Characteristics
220 
221  //- The name of the input token stream.
222  virtual const fileName& name() const { return name_; }
223 
224  //- The name of the input token stream, for modification.
225  virtual fileName& name() { return name_; }
226 
227 
228  // Token Access
229 
230  //- True if putback token is in use
231  bool hasPutback() const noexcept
232  {
233  return Istream::hasPutback();
234  }
235 
236  //- Failsafe peek at the \b first token in the list.
237  // \return \c undefinedToken if the list is empty.
238  const token& front() const { return peekAt(0); }
239 
240  //- Failsafe peek at the \b last token in the list.
241  // \return \c undefinedToken if the list is empty.
242  const token& back() const { return peekAt(tokenList::size()-1); }
243 
244  //- Failsafe peek at what the next read would return,
245  //- including handling of any putback
246  // \return \c undefinedToken if list is exhausted
247  const token& peek() const;
248 
249  //- The current token index when reading, or the insertion point.
250  label tokenIndex() const noexcept { return tokenIndex_; }
251 
252  //- Non-const access to the current token index
253  label& tokenIndex() noexcept { return tokenIndex_; }
254 
255  //- Number of tokens remaining
256  label nRemainingTokens() const noexcept
257  {
258  return size() - tokenIndex_;
259  }
260 
261  //- Move tokenIndex to the specified position
262  // Using seek(0) is identical to rewind.
263  // Using seek(-1) moves to the end.
264  void seek(label pos);
265 
266  //- Move tokenIndex relative to the current position.
267  // Will not overrun the beginning or end positions.
268  //
269  // Use skip(2) to move forward two tokens.
270  // Use skip(-2) to move backward two tokens.
271  void skip(label n = 1);
272 
273 
274  // Token list modification
275 
276  //- Copy append a token at the current tokenIndex,
277  //- incrementing the index.
278  void push_back(const token& t, const bool lazy);
279 
280  //- Move append a token at the current tokenIndex,
281  //- incrementing the index.
282  void push_back(token&& t, const bool lazy);
283 
284  //- Copy append a list of tokens at the current tokenIndex,
285  //- incrementing the index.
286  //
287  // \param newTokens the list of tokens to copy append
288  // \param lazy leaves any excess capacity for further appends.
289  // The caller will be responsible for resizing later.
290  void push_back(const UList<token>& newTokens, const bool lazy);
292  //- Move append a list of tokens at the current tokenIndex,
293  //- incrementing the index.
294  //
295  // \param newTokens the list of tokens to move append
296  // \param lazy leaves any excess capacity for further appends.
297  // The caller will be responsible for resizing later.
298  void push_back(List<token>&& newTokens, const bool lazy);
299 
300 
301  // Stream State Functions
302 
303  //- Get stream flags - always 0
304  virtual ios_base::fmtflags flags() const
305  {
306  return ios_base::fmtflags(0);
307  }
308 
309  //- Set flags of stream - ignored
310  ios_base::fmtflags flags(const ios_base::fmtflags)
311  {
312  return ios_base::fmtflags(0);
313  }
315 
316  // Read Functions
317 
318  //- Return next token from stream
319  virtual Istream& read(token& tok);
320 
321  //- Read a character
322  virtual Istream& read(char&);
323 
324  //- Read a word
325  virtual Istream& read(word&);
326 
327  // Read a string (including enclosing double-quotes)
328  virtual Istream& read(string&);
329 
330  //- Read a label
331  virtual Istream& read(label&);
332 
333  //- Read a float
334  virtual Istream& read(float&);
335 
336  //- Read a double
337  virtual Istream& read(double&);
338 
339  //- Read binary block
340  // \note Not implemented
341  virtual Istream& read(char* data, std::streamsize);
342 
343  //- Low-level raw binary read
344  // \note Not implemented
345  virtual Istream& readRaw(char* data, std::streamsize count);
346 
347  //- Start of low-level raw binary read
348  virtual bool beginRawRead()
349  {
350  return false;
351  }
352 
353  //- End of low-level raw binary read
354  virtual bool endRawRead()
355  {
356  return false;
357  }
358 
359  //- Rewind the stream so that it may be read again
360  virtual void rewind();
361 
362 
363  // Output
364 
365  //- Print stream description to Ostream
366  void print(Ostream& os) const;
367 
368  //- Concatenate tokens into a space-separated std::string.
369  //- The resulting string may contain quote characters.
370  std::string toString() const;
371 
372 
373  // Member Operators
374 
375  //- Use operator() from Istream
376  using Istream::operator();
377 
378  //- Copy assignment, with rewind()
379  void operator=(const ITstream& is);
381  //- Copy assignment of tokens, with rewind()
382  void operator=(const UList<token>& toks);
383 
384  //- Move assignment of tokens, with rewind()
385  void operator=(List<token>&& toks);
386 
387 
388  // Additional constructors and methods (as per v2012 and earlier)
389  #ifdef Foam_IOstream_extras
390 
391  //- Construct from components, copying the tokens
392  ITstream
393  (
394  const string& name,
395  const UList<token>& tokens,
397  )
398  :
399  ITstream(name, tokens, IOstreamOption(fmt))
400  {}
401 
402  //- Construct from components, transferring the tokens
403  ITstream
404  (
405  const string& name,
406  List<token>&& tokens,
408  )
409  :
410  ITstream(name, std::move(tokens), IOstreamOption(fmt))
411  {}
412 
413  #endif /* Foam_IOstream_extras */
414 
415 
416  // Housekeeping
417 
418  //- Same as front()
419  const token& peekFirst() const { return front(); }
420 
421  //- Copy append a token at the current tokenIndex,
422  //- incrementing the index.
423  void append(const token& t, const bool lazy)
424  {
425  this->push_back(std::move(t), lazy);
426  }
427 
428  //- Move append a token at the current tokenIndex,
429  //- incrementing the index.
430  void append(token&& t, const bool lazy)
431  {
432  this->push_back(std::move(t), lazy);
433  }
434 
435  //- Copy append a list of tokens at the current tokenIndex,
436  //- incrementing the index.
437  //
438  // \param newTokens the list of tokens to copy append
439  // \param lazy leaves any excess capacity for further appends.
440  // The caller will be responsible for resizing later.
441  void append(const UList<token>& newTokens, const bool lazy)
442  {
443  this->push_back(newTokens, lazy);
444  }
445 
446  //- Move append a list of tokens at the current tokenIndex,
447  //- incrementing the index.
448  //
449  // \param newTokens the list of tokens to move append
450  // \param lazy leaves any excess capacity for further appends.
451  // The caller will be responsible for resizing later.
452  void append(List<token>&& newTokens, const bool lazy)
453  {
454  this->push_back(std::move(newTokens), lazy);
455  }
456 };
457 
458 
459 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
460 
461 } // End namespace Foam
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 #endif
466 
467 // ************************************************************************* //
static const token undefinedToken
An undefined token.
Definition: token.H:343
const token & front() const
Failsafe peek at the first token in the list.
Definition: ITstream.H:284
A class for handling file names.
Definition: fileName.H:71
void seek(label pos)
Move tokenIndex to the specified position.
Definition: ITstream.C:328
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
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:64
void skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:375
A simple container for options an IOstream can normally have.
std::string toString() const
Concatenate tokens into a space-separated std::string. The resulting string may contain quote charact...
Definition: ITstream.C:296
label tokenIndex() const noexcept
The current token index when reading, or the insertion point.
Definition: ITstream.H:304
label nRemainingTokens() const noexcept
Number of tokens remaining.
Definition: ITstream.H:314
virtual bool endRawRead()
End of low-level raw binary read.
Definition: ITstream.H:454
const token & peek() const
Failsafe peek at what the next read would return, including handling of any putback.
Definition: ITstream.C:316
static tokenList parse(const UList< char > &input, IOstreamOption streamOpt=IOstreamOption())
Create token list by parsing the input character sequence until no good tokens remain.
Definition: ITstream.C:58
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
dimensionedScalar pos(const dimensionedScalar &ds)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual bool beginRawRead()
Start of low-level raw binary read.
Definition: ITstream.H:446
void append(const token &t, const bool lazy)
Copy append a token at the current tokenIndex, incrementing the index.
Definition: ITstream.H:545
A class for handling words, derived from Foam::string.
Definition: word.H:63
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:48
bool hasPutback() const noexcept
True if putback token is in use.
Definition: ITstream.H:274
virtual Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
Definition: ITstream.C:511
const token & peekFirst() const
Same as front()
Definition: ITstream.H:539
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:99
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:525
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
virtual ios_base::fmtflags flags() const
Get stream flags - always 0.
Definition: ITstream.H:380
virtual ~ITstream()=default
Destructor.
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:81
streamFormat
Data format (ascii | binary)
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:413
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:58
label n
virtual Istream & read(token &tok)
Return next token from stream.
Definition: ITstream.C:417
void push_back(const token &t, const bool lazy)
Copy append a token at the current tokenIndex, incrementing the index.
Definition: ITstream.C:531
virtual const fileName & name() const
The name of the input token stream.
Definition: ITstream.H:261
ITstream(const ITstream &is)
Copy construct.
Definition: ITstream.C:136
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:581
void print(Ostream &os) const
Print stream description to Ostream.
Definition: ITstream.C:271
const token & back() const
Failsafe peek at the last token in the list.
Definition: ITstream.H:291
An input stream of tokens.
Definition: ITstream.H:48
Namespace for OpenFOAM.