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-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::Istream
29
30Description
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
41SourceFiles
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
55namespace Foam
56{
57
58/*---------------------------------------------------------------------------*\
59 Class Istream Declaration
60\*---------------------------------------------------------------------------*/
62class 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
75protected:
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
86public:
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, version (compression)
108 explicit Istream
109 (
113 )
114 :
115 Istream(IOstreamOption(fmt, ver, cmp))
116 {}
117
118
119 // Member Functions
120
121 // Token put-back
122
123 //- Examine putback token without removing it.
124 // Returns const reference to \c token::undefinedToken
125 // if a putback is unavailable.
126 const token& peekBack() const noexcept;
127
128 //- Fetch putback token without removing it.
129 // \return false sets the token to undefined if no put-back
130 // was available
131 bool peekBack(token& tok);
132
133 //- Put back a token. Only a single put back is permitted
134 void putBack(const token& tok);
135
136 //- Get the put-back token if there is one.
137 // \return false and sets token to undefined if no put-back
138 // was available
139 bool getBack(token& tok);
140
141
142 // Read Functions
143
144 //- Return next token from stream
145 virtual Istream& read(token&) = 0;
146
147 //- Read a character
148 virtual Istream& read(char&) = 0;
149
150 //- Read a word
151 virtual Istream& read(word&) = 0;
152
153 //- Read a string (including enclosing double-quotes)
154 virtual Istream& read(string&) = 0;
155
156 //- Read a label
157 virtual Istream& read(label&) = 0;
158
159 //- Read a floatScalar
160 virtual Istream& read(floatScalar&) = 0;
161
162 //- Read a doubleScalar
163 virtual Istream& read(doubleScalar&) = 0;
164
165 //- Read binary block
166 virtual Istream& read(char*, std::streamsize) = 0;
167
168 //- Start of low-level raw binary read
169 virtual bool beginRawRead() = 0;
170
171 //- End of low-level raw binary read
172 virtual bool endRawRead() = 0;
173
174 //- Low-level raw binary read
175 virtual Istream& readRaw(char*, std::streamsize) = 0;
176
177 //- Rewind the stream so that it may be read again
178 virtual void rewind() = 0;
179
180
181 // Read List punctuation tokens
182
183 //- Begin read of data chunk, starts with '('.
184 // \return true or FatalIOError
185 bool readBegin(const char* funcName);
186
187 //- End read of data chunk, ends with ')'
188 // \return true or FatalIOError
189 bool readEnd(const char* funcName);
190
191 //- Begin read of list data, starts with '(' or '{'
192 // \return starting delimiter or FatalIOError
193 char readBeginList(const char* funcName);
194
195 //- End read of list data, ends with ')' or '}'
196 // \return closing delimiter or FatalIOError
197 char readEndList(const char* funcName);
198
199
200 // Member Operators
201
202 //- Return a non-const reference to const Istream
203 // Needed for read-constructors where the stream argument is temporary
204 Istream& operator()() const;
205};
206
207
208// --------------------------------------------------------------------
209// ------ Manipulators (not taking arguments)
210// --------------------------------------------------------------------
211
212//- An Istream manipulator
213typedef Istream& (*IstreamManip)(Istream&);
214
215//- operator>> handling for manipulators without arguments
216inline Istream& operator>>(Istream& is, IstreamManip f)
217{
218 return f(is);
219}
220
221//- operator>> handling for manipulators without arguments
223{
224 f(is);
225 return is;
226}
227
228
229// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230
231namespace Detail
232{
233 //- Read binary block of contiguous data, possibly with conversion
234 template<class T>
235 void readContiguous(Istream& is, char* data, std::streamsize byteCount)
236 {
237 is.beginRawRead();
238
240 {
242 (
243 is,
244 reinterpret_cast<label*>(data),
245 byteCount/sizeof(label)
246 );
247 }
249 {
250 readRawScalar
251 (
252 is,
253 reinterpret_cast<scalar*>(data),
254 byteCount/sizeof(scalar)
255 );
256 }
257 else
258 {
259 is.readRaw(data, byteCount);
260 }
261
262 is.endRawRead();
263 }
264
265} // End namespace Detail
266
267
268// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269
270} // End namespace Foam
271
272
273// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
274
275// Previously excluded (via token.H)
276#ifdef NoRepository
277 #include "HashTable.C"
278#endif
279
280// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281
282#endif
283
284// ************************************************************************* //
Representation of a major/minor version number.
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat
Data format (ascii | binary)
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
@ UNCOMPRESSED
compression = false
static const versionNumber currentVersion
The current version number (2.0)
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Istream(IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver=IOstreamOption::currentVersion, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format, version (compression)
Definition: Istream.H:108
bool getBack(token &tok)
Get the put-back token if there is one.
Definition: Istream.C:92
virtual bool endRawRead()=0
End of low-level raw binary read.
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
virtual Istream & readRaw(char *, std::streamsize)=0
Low-level raw binary read.
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition: Istream.C:129
Istream(IOstreamOption streamOpt=IOstreamOption())
Definition: Istream.H:100
virtual ~Istream()=default
Destructor.
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition: Istream.C:49
bool hasPutback() const noexcept
True if putback token is in use.
Definition: Istream.H:79
virtual void rewind()=0
Rewind the stream so that it may be read again.
Istream(const Istream &)=default
Copy construct.
virtual bool beginRawRead()=0
Start of low-level raw binary read.
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: Istream.C:111
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
virtual Istream & read(token &)=0
Return next token from stream.
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
A token holds an item read from Istream.
Definition: token.H:69
A class for handling words, derived from Foam::string.
Definition: word.H:68
void readContiguous(Istream &is, char *data, std::streamsize byteCount)
Read binary block of contiguous data, possibly with conversion.
Definition: Istream.H:234
Namespace for OpenFOAM.
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
Istream & operator>>(Istream &, directionInfo &)
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
label readRawLabel(Istream &is)
Read raw label from binary stream.
Definition: label.C:46
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:431
const direction noexcept
Definition: Scalar.H:223
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition: Istream.H:212
labelList f(nPoints)
A template class to specify if a data type is composed solely of Foam::label elements.
Definition: contiguous.H:86
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition: contiguous.H:94