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-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::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
165  virtual Istream& read(char*, std::streamsize) = 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
174  virtual Istream& readRaw(char*, std::streamsize) = 0;
175 
176  //- Rewind the stream so that it may be read again
177  virtual void rewind() = 0;
178 
179 
180  // Read List punctuation tokens
181 
182  //- Begin read of data chunk, starts with '('.
183  // \return true or FatalIOError
184  bool readBegin(const char* funcName);
185 
186  //- End read of data chunk, ends with ')'
187  // \return true or FatalIOError
188  bool readEnd(const char* funcName);
189 
190  //- Begin read of list data, starts with '(' or '{'
191  // \return starting delimiter or FatalIOError
192  char readBeginList(const char* funcName);
193 
194  //- End read of list data, ends with ')' or '}'
195  // \return closing delimiter or FatalIOError
196  char readEndList(const char* funcName);
197 
198 
199  // Member Operators
200 
201  //- Return a non-const reference to const Istream
202  // Needed for read-constructors where the stream argument is temporary
203  Istream& operator()() const;
204 };
205 
206 
207 // --------------------------------------------------------------------
208 // ------ Manipulators (not taking arguments)
209 // --------------------------------------------------------------------
210 
211 //- An Istream manipulator
212 typedef Istream& (*IstreamManip)(Istream&);
213 
214 //- operator>> handling for manipulators without arguments
215 inline Istream& operator>>(Istream& is, IstreamManip f)
216 {
217  return f(is);
218 }
219 
220 //- operator>> handling for manipulators without arguments
222 {
223  f(is);
224  return is;
225 }
226 
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 namespace Detail
231 {
232  //- Read binary block of contiguous data, possibly with conversion
233  template<class T>
234  void readContiguous(Istream& is, char* data, std::streamsize byteCount)
235  {
236  is.beginRawRead();
237 
238  if (is_contiguous_label<T>::value)
239  {
241  (
242  is,
243  reinterpret_cast<label*>(data),
244  byteCount/sizeof(label)
245  );
246  }
247  else if (is_contiguous_scalar<T>::value)
248  {
249  readRawScalar
250  (
251  is,
252  reinterpret_cast<scalar*>(data),
253  byteCount/sizeof(scalar)
254  );
255  }
256  else
257  {
258  is.readRaw(data, byteCount);
259  }
260 
261  is.endRawRead();
262  }
263 
264 } // End namespace Detail
265 
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 } // End namespace Foam
270 
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274 // Previously excluded (via token.H)
275 #ifdef NoRepository
276  #include "HashTable.C"
277 #endif
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 #endif
282 
283 // ************************************************************************* //
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.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
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
labelList f(nPoints)
virtual Istream & readRaw(char *, std::streamsize)=0
Low-level raw binary read.
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:273
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:75
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:301
virtual ~Istream()=default
Destructor.
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:531
Namespace for OpenFOAM.
Istream(const Istream &)=default
Copy construct.