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
81  {
82  return putBackAvail_;
83  }
84 
85 
86 public:
87 
88  // Generated Methods
89 
90  //- Copy construct
91  Istream(const Istream&) = default;
92 
93  //- Destructor
94  virtual ~Istream() = default;
95 
96 
97  // Constructors
98 
99  //- Default construct (ASCII, uncompressed),
100  //- construct with specified stream option.
101  explicit Istream(IOstreamOption streamOpt = IOstreamOption())
102  :
103  IOstream(streamOpt),
104  putBackAvail_(false)
105  {}
106 
107  //- Construct with format (uncompressed)
108  explicit Istream
109  (
112  )
113  :
114  Istream(IOstreamOption(fmt, cmp))
115  {}
116 
117 
118  // Member Functions
119 
120  // Token put-back
121 
122  //- Examine putback token without removing it.
123  // Returns const reference to \c token::undefinedToken
124  // if a putback is unavailable.
125  const token& peekBack() const noexcept;
126 
127  //- Fetch putback token without removing it.
128  // \return false sets the token to undefined if no put-back
129  // was available
130  bool peekBack(token& tok);
131 
132  //- Put back a token. Only a single put back is permitted
133  void putBack(const token& tok);
134 
135  //- Get the put-back token if there is one.
136  // \return false and sets token to undefined if no put-back
137  // was available
138  bool getBack(token& tok);
139 
140 
141  // Read Functions
142 
143  //- Return next token from stream
144  virtual Istream& read(token&) = 0;
145 
146  //- Read a character
147  virtual Istream& read(char&) = 0;
148 
149  //- Read a word
150  virtual Istream& read(word&) = 0;
151 
152  //- Read a string (including enclosing double-quotes)
153  virtual Istream& read(string&) = 0;
154 
155  //- Read a label
156  virtual Istream& read(label&) = 0;
157 
158  //- Read a float
159  virtual Istream& read(float&) = 0;
160 
161  //- Read a double
162  virtual Istream& read(double&) = 0;
163 
164  //- Read binary block (with any possible block delimiters).
165  //- Reading into a null pointer shall ideally behave like a seek
166  //- operation.
167  virtual Istream& read(char* data, std::streamsize count) = 0;
168 
169  //- Start of low-level raw binary read
170  virtual bool beginRawRead() = 0;
171 
172  //- End of low-level raw binary read
173  virtual bool endRawRead() = 0;
174 
175  //- Low-level raw binary read (without possible block delimiters).
176  //- Reading into a null pointer shall ideally behave like a seek
177  //- operation.
178  virtual Istream& readRaw(char* data, std::streamsize count) = 0;
179 
180  //- Rewind the stream so that it may be read again
181  virtual void rewind() = 0;
182 
183 
184  // Read List punctuation tokens
185 
186  //- Begin read of data chunk, starts with '('.
187  // \return true or FatalIOError
188  bool readBegin(const char* funcName);
189 
190  //- End read of data chunk, ends with ')'
191  // \return true or FatalIOError
192  bool readEnd(const char* funcName);
193 
194  //- Begin read of list data, starts with '(' or '{'
195  // \return starting delimiter or FatalIOError
196  char readBeginList(const char* funcName);
197 
198  //- End read of list data, ends with ')' or '}'
199  // \return closing delimiter or FatalIOError
200  char readEndList(const char* funcName);
201 
202 
203  // Member Operators
204 
205  //- Return a non-const reference to const Istream
206  // Needed for read-constructors where the stream argument is temporary
207  Istream& operator()() const;
208 };
209 
210 
211 // --------------------------------------------------------------------
212 // ------ Manipulators (not taking arguments)
213 // --------------------------------------------------------------------
214 
215 //- An Istream manipulator
216 typedef Istream& (*IstreamManip)(Istream&);
217 
218 //- operator>> handling for manipulators without arguments
219 inline Istream& operator>>(Istream& is, IstreamManip f)
220 {
221  return f(is);
222 }
223 
224 //- operator>> handling for manipulators without arguments
226 {
227  f(is);
228  return is;
229 }
230 
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 namespace Detail
235 {
236  //- Read binary block of contiguous data, possibly with conversion
237  template<class T>
238  void readContiguous(Istream& is, char* data, std::streamsize byteCount)
239  {
240  is.beginRawRead();
241 
242  if (is_contiguous_label<T>::value)
243  {
245  (
246  is,
247  reinterpret_cast<label*>(data),
248  byteCount/sizeof(label)
249  );
250  }
251  else if (is_contiguous_scalar<T>::value)
252  {
253  readRawScalar
254  (
255  is,
256  reinterpret_cast<scalar*>(data),
257  byteCount/sizeof(scalar)
258  );
259  }
260  else
261  {
262  is.readRaw(data, byteCount);
263  }
264 
265  is.endRawRead();
266  }
267 
268 } // End namespace Detail
269 
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 } // End namespace Foam
274 
275 
276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
278 // Previously excluded (via token.H)
279 #ifdef NoRepository
280  #include "HashTable.C"
281 #endif
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #endif
286 
287 // ************************************************************************* //
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:42
bool getBack(token &tok)
Get the put-back token if there is one.
Definition: Istream.C:85
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:104
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:162
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
Istream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition: Istream.H:108
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. Only a single put back is permitted.
Definition: Istream.C:63
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:141
const direction noexcept
Definition: Scalar.H:258
bool readEnd(const char *funcName)
End read of data chunk, ends with &#39;)&#39;.
Definition: Istream.C:122
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
labelList f(nPoints)
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:277
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:305
virtual ~Istream()=default
Destructor.
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:528
Namespace for OpenFOAM.
Istream(const Istream &)=default
Copy construct.