Istream.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-2023 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::Istream
29 
30 Description
31  An Istream is an abstract base class for all input systems
32  (streams, files, token lists etc). The basic operations
33  are construct, close, read token, read primitive and read binary
34  block.
35 
36  In addition, version control and line number counting is incorporated.
37  Usually one would use the read primitive member functions, but if one
38  were reading a stream on unknown data sequence one can read token by
39  token, and then analyse.
40 
41 SourceFiles
42  Istream.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_Istream_H
47 #define Foam_Istream_H
48 
49 #include "IOstream.H"
50 #include "token.H"
51 #include "contiguous.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 /*---------------------------------------------------------------------------*\
59  Class Istream Declaration
60 \*---------------------------------------------------------------------------*/
61 
62 class Istream
63 :
64  public IOstream
65 {
66  // Private Data
67 
68  //- The last token put back on the stream
69  token putBackToken_;
70 
71  //- Is a put-back token available?
72  bool putBackAvail_;
73 
74 
75 protected:
76 
77  // Protected Member Functions
78 
79  //- True if putback token is in use
80  bool hasPutback() const noexcept { return putBackAvail_; }
81 
82 
83 public:
84 
85  // Generated Methods
86 
87  //- Copy construct
88  Istream(const Istream&) = default;
89 
90  //- Destructor
91  virtual ~Istream() = default;
92 
93 
94  // Constructors
95 
96  //- Default construct (ASCII, uncompressed),
97  //- construct with specified stream option.
98  explicit Istream(IOstreamOption streamOpt = IOstreamOption())
99  :
100  IOstream(streamOpt),
101  putBackAvail_(false)
102  {}
103 
104  //- Construct with format (uncompressed)
105  explicit Istream
106  (
109  )
110  :
111  Istream(IOstreamOption(fmt, cmp))
112  {}
113 
114 
115  // Member Functions
116 
117  // Token put-back
118 
119  //- Examine putback token without removing it.
120  // Returns const reference to \c token::undefinedToken
121  // if a putback is unavailable.
122  const token& peekBack() const noexcept;
123 
124  //- Drop the putback token
125  void putBackClear();
126 
127  //- Put back a token (copy). Only a single put back is permitted
128  void putBack(const token& tok);
129 
130  //- Put back a token (move). Only a single put back is permitted
131  void putBack(token&& tok);
132 
133  //- Retrieve the put-back token if there is one.
134  // \return false and sets token to undefined if no put-back
135  // was available
136  bool getBack(token& tok);
137 
138 
139  // Read Functions
140 
141  //- Return next token from stream
142  virtual Istream& read(token&) = 0;
143 
144  //- Read a character
145  virtual Istream& read(char&) = 0;
146 
147  //- Read a word
148  virtual Istream& read(word&) = 0;
149 
150  //- Read a string (including enclosing double-quotes)
151  virtual Istream& read(string&) = 0;
152 
153  //- Read a label
154  virtual Istream& read(label&) = 0;
155 
156  //- Read a float
157  virtual Istream& read(float&) = 0;
158 
159  //- Read a double
160  virtual Istream& read(double&) = 0;
161 
162  //- Read binary block (with any possible block delimiters).
163  //- Reading into a null pointer shall ideally behave like a seek
164  //- operation.
165  virtual Istream& read(char* data, std::streamsize count) = 0;
166 
167  //- Start of low-level raw binary read
168  virtual bool beginRawRead() = 0;
169 
170  //- End of low-level raw binary read
171  virtual bool endRawRead() = 0;
172 
173  //- Low-level raw binary read (without possible block delimiters).
174  //- Reading into a null pointer shall ideally behave like a seek
175  //- operation.
176  virtual Istream& readRaw(char* data, std::streamsize count) = 0;
177 
178  //- Rewind the stream so that it may be read again
179  virtual void rewind() = 0;
180 
181 
182  // Read List punctuation tokens
183 
184  //- Begin read of data chunk, starts with '('.
185  // \return true or FatalIOError
186  bool readBegin(const char* funcName);
187 
188  //- End read of data chunk, ends with ')'
189  // \return true or FatalIOError
190  bool readEnd(const char* funcName);
191 
192  //- Begin read of list data, starts with '(' or '{'
193  // \return starting delimiter or FatalIOError
194  char readBeginList(const char* funcName);
195 
196  //- End read of list data, ends with ')' or '}'
197  // \return closing delimiter or FatalIOError
198  char readEndList(const char* funcName);
199 
200 
201  // Member Operators
202 
203  //- Return a non-const reference to const Istream
204  // Needed for read-constructors where the stream argument is temporary
205  Istream& operator()() const;
206 };
207 
208 
209 // --------------------------------------------------------------------
210 // ------ Manipulators (not taking arguments)
211 // --------------------------------------------------------------------
212 
213 //- An Istream manipulator
214 typedef Istream& (*IstreamManip)(Istream&);
215 
216 //- operator>> handling for manipulators without arguments
217 inline Istream& operator>>(Istream& is, IstreamManip f)
218 {
219  return f(is);
220 }
221 
222 //- operator>> handling for manipulators without arguments
224 {
225  f(is);
226  return is;
227 }
228 
229 
230 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 
232 namespace Detail
233 {
234  //- Read binary block of contiguous data, possibly with conversion
235  template<class T>
236  void readContiguous(Istream& is, char* data, std::streamsize byteCount)
237  {
238  is.beginRawRead();
239 
240  if (is_contiguous_label<T>::value)
241  {
243  (
244  is,
245  reinterpret_cast<label*>(data),
246  byteCount/sizeof(label)
247  );
248  }
249  else if (is_contiguous_scalar<T>::value)
250  {
251  readRawScalar
252  (
253  is,
254  reinterpret_cast<scalar*>(data),
255  byteCount/sizeof(scalar)
256  );
257  }
258  else
259  {
260  is.readRaw(data, byteCount);
261  }
262 
263  is.endRawRead();
264  }
265 
266 } // End namespace Detail
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
271 } // End namespace Foam
272 
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 // Previously excluded (via token.H)
277 #ifdef NoRepository
278  #include "HashTable.C"
279 #endif
280 
281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
282 
283 #endif
284 
285 // ************************************************************************* //
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:42
bool getBack(token &tok)
Retrieve the put-back token if there is one.
Definition: Istream.C:115
virtual void rewind()=0
Rewind the stream so that it may be read again.
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
bool readBegin(const char *funcName)
Begin read of data chunk, starts with &#39;(&#39;.
Definition: Istream.C:134
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:192
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void putBackClear()
Drop the putback token.
Definition: Istream.C:64
A token holds an item read from Istream.
Definition: token.H:65
Istream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition: Istream.H:105
virtual bool endRawRead()=0
End of low-level raw binary read.
A simple container for options an IOstream can normally have.
virtual Istream & readRaw(char *data, std::streamsize count)=0
Low-level raw binary read (without possible block delimiters). Reading into a null pointer shall idea...
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
virtual Istream & read(token &)=0
Return next token from stream.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
const direction noexcept
Definition: Scalar.H:258
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:152
labelList f(nPoints)
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:276
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:39
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
virtual bool beginRawRead()=0
Start of low-level raw binary read.
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:81
streamFormat
Data format (ascii | binary)
void readContiguous(Istream &is, char *data, std::streamsize byteCount)
Read binary block of contiguous data, possibly with conversion.
Definition: Istream.H:304
virtual ~Istream()=default
Destructor.
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:528
Namespace for OpenFOAM.
Istream(const Istream &)=default
Copy construct.