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-2021 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 ITstream_H
39 #define 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 public:
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
105  ITstream
106  (
107  const string& name,
108  const UList<token>& tokens,
109  IOstreamOption streamOpt = IOstreamOption()
110  );
111 
112  //- Move construct from tokens, with given name
113  ITstream
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  //- Copy assignment, with rewind()
350  void operator=(const ITstream& is);
351 
352  //- Copy assignment of tokens, with rewind()
353  void operator=(const UList<token>& toks);
354 
355  //- Move assignment of tokens, with rewind()
356  void operator=(List<token>&& toks);
357 
358 
359  // Additional constructors and methods (as per v2012 and earlier)
360  #ifdef Foam_IOstream_extras
361 
362  //- Construct from components, copying the tokens
363  ITstream
364  (
365  const string& name,
366  const UList<token>& tokens,
369  )
370  :
371  ITstream(name, tokens, IOstreamOption(fmt, ver))
372  {}
373 
374  //- Construct from components, transferring the tokens
375  ITstream
376  (
377  const string& name,
378  List<token>&& tokens,
380  IOstreamOption::versionNumber ver = IOstreamOption::currentVersion
381  )
382  :
383  ITstream(name, std::move(tokens), IOstreamOption(fmt, ver))
384  {}
385 
386  #endif /* Foam_IOstream_extras */
387 };
388 
389 
390 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
391 
392 } // End namespace Foam
393 
394 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
395 
396 #endif
397 
398 // ************************************************************************* //
Foam::ITstream::beginRawRead
virtual bool beginRawRead()
Start of low-level raw binary read.
Definition: ITstream.H:321
Foam::ITstream::append
void append(const token &t, const bool lazy)
Definition: ITstream.C:567
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Foam::ITstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ITstream.C:561
Foam::ITstream::readRaw
virtual Istream & readRaw(char *data, std::streamsize count)
Low-level raw binary read.
Definition: ITstream.C:547
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::ITstream::name
virtual fileName & name()
Return stream name for modification.
Definition: ITstream.H:241
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::ITstream::flags
virtual ios_base::fmtflags flags() const
Get stream flags - always 0.
Definition: ITstream.H:277
Foam::ITstream::read
virtual Istream & read(token &tok)
Return next token from stream.
Definition: ITstream.C:453
Foam::ITstream::name
virtual const fileName & name() const
Get the name of the stream.
Definition: ITstream.H:235
Foam::ITstream::tokenIndex
label & tokenIndex() noexcept
Non-const access to the current token index.
Definition: ITstream.H:208
Foam::ITstream::operator=
void operator=(const ITstream &is)
Copy assignment, with rewind()
Definition: ITstream.C:617
Foam::ITstream::~ITstream
virtual ~ITstream()=default
Destructor.
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::ITstream::ITstream
ITstream(const ITstream &is)
Copy construct.
Definition: ITstream.C:160
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::ITstream
An input stream of tokens.
Definition: ITstream.H:52
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::ITstream::print
void print(Ostream &os) const
Print stream description to Ostream.
Definition: ITstream.C:295
tokenList.H
Foam::ITstream::flags
ios_base::fmtflags flags(const ios_base::fmtflags)
Set flags of stream - ignored.
Definition: ITstream.H:283
Foam::ITstream::peekFirst
const token & peekFirst() const
Failsafe peek at the first token in the list.
Definition: ITstream.C:340
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Istream.H
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::ITstream::seek
void seek(label pos)
Move tokenIndex to the specified position.
Definition: ITstream.C:364
Foam::ITstream::skip
void skip(label n=1)
Move tokenIndex relative to the current position.
Definition: ITstream.C:411
Foam::Istream::hasPutback
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:79
Foam::ITstream::toString
std::string toString() const
Definition: ITstream.C:320
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::ITstream::peekLast
const token & peekLast() const
Failsafe peek at the last token in the list.
Definition: ITstream.C:346
Foam::ITstream::tokenIndex
label tokenIndex() const noexcept
The current token index when reading, or the insertion point.
Definition: ITstream.H:202
Foam::ITstream::endRawRead
virtual bool endRawRead()
End of low-level raw binary read.
Definition: ITstream.H:327
Foam::UList
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:103
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::ITstream::nRemainingTokens
label nRemainingTokens() const noexcept
Number of tokens remaining.
Definition: ITstream.H:214
Foam::ITstream::peek
const token & peek() const
Failsafe peek at what the next read would return,.
Definition: ITstream.C:352
Foam::ITstream::parse
static tokenList parse(const UList< char > &input, IOstreamOption streamOpt=IOstreamOption())
Definition: ITstream.C:82
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::ITstream::hasPutback
bool hasPutback() const noexcept
True if putback token is in use.
Definition: ITstream.H:183
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177