Loading...
Searching...
No Matches
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,2024 OpenFOAM Foundation
9 Copyright (C) 2017-2025 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{
58/*---------------------------------------------------------------------------*\
59 Class Istream Declaration
60\*---------------------------------------------------------------------------*/
61
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 { return putBackAvail_; }
82
83public:
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)
110 :
111 Istream(IOstreamOption(fmt, cmp))
112 {}
113
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 int32_t
154 virtual Istream& read(int32_t&) = 0;
155
156 //- Read int64_t
157 virtual Istream& read(int64_t&) = 0;
158
159 //- Read uint32_t
160 virtual Istream& read(uint32_t&) = 0;
161
162 //- Read uint64_t
163 virtual Istream& read(uint64_t&) = 0;
164
165 //- Read a float
166 virtual Istream& read(float&) = 0;
167
168 //- Read a double
169 virtual Istream& read(double&) = 0;
171 //- Read binary block (with any possible block delimiters).
172 //- Reading into a null pointer shall ideally behave like a seek
173 //- operation.
174 virtual Istream& read(char* data, std::streamsize count) = 0;
176 //- Start of low-level raw binary read
177 virtual bool beginRawRead() = 0;
178
179 //- End of low-level raw binary read
180 virtual bool endRawRead() = 0;
181
182 //- Low-level raw binary read (without possible block delimiters).
183 //- Reading into a null pointer shall ideally behave like a seek
184 //- operation.
185 virtual Istream& readRaw(char* data, std::streamsize count) = 0;
186
187 //- Rewind the stream so that it may be read again.
188 // The base implementation clears any putback.
189 virtual void rewind() = 0;
191
192 // Read List punctuation tokens
193
194 //- Begin read of data chunk, starts with '('.
195 // \return true or FatalIOError
196 bool readBegin(const char* funcName);
197
198 //- End read of data chunk, ends with ')'
199 // \return true or FatalIOError
200 bool readEnd(const char* funcName);
201
202 //- Begin read of list data, starts with '(' or '{'
203 // \return starting delimiter or FatalIOError
204 char readBeginList(const char* funcName);
206 //- End read of list data, ends with ')' or '}'
207 // \return closing delimiter or FatalIOError
208 char readEndList(const char* funcName);
209
211 // Member Operators
212
213 //- Return a non-const reference to const Istream
214 // Needed for read-constructors where the stream argument is temporary
215 Istream& operator()() const;
216};
218
219// --------------------------------------------------------------------
220// ------ Manipulators (not taking arguments)
221// --------------------------------------------------------------------
223//- An Istream manipulator
224typedef Istream& (*IstreamManip)(Istream&);
225
226//- operator>> handling for manipulators without arguments
227inline Istream& operator>>(Istream& is, IstreamManip f)
228{
229 return f(is);
230}
231
232//- operator>> handling for manipulators without arguments
235 f(is);
236 return is;
237}
238
239
240// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241
242namespace Detail
243{
244
245//- Read binary block of contiguous data, possibly with conversion
246template<class T>
247void readContiguous(Istream& is, char* data, std::streamsize byteCount)
248{
249 if constexpr (is_contiguous_label<T>::value)
250 {
251 is.beginRawRead();
253 (
254 is,
255 reinterpret_cast<label*>(data),
256 byteCount/sizeof(label)
257 );
258 is.endRawRead();
259 }
260 else if constexpr (is_contiguous_scalar<T>::value)
261 {
262 is.beginRawRead();
263 readRawScalar
264 (
265 is,
266 reinterpret_cast<scalar*>(data),
267 byteCount/sizeof(scalar)
268 );
269 is.endRawRead();
270 }
271 else
272 {
273 is.read(data, byteCount);
274 }
275}
276
277} // End namespace Detail
278} // End namespace Foam
279
280
281// * * * * * * * * * * * * * * * Specializations * * * * * * * * * * * * * * //
282
283// Contiguous classes with mixed internal representations require specialized
284// versions of readContiguous.
285//
286// The declarations must be known when reading files, so declare them here.
287// This approach obviously does not scale well, but fortunately there
288// are not too many of these classes.
289
290namespace Foam
291{
292
293// Forward Declarations
294class pointConstraint;
295
296namespace Detail
297{
299//- Read binary block of pointConstraint data (possibly with conversion)
300template<>
302(
303 Istream& is, char* data, std::streamsize byteCount
304);
305
306} // End namespace Detail
307} // End namespace Foam
308
309// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310
311// Previously excluded (via token.H)
312#ifdef NoRepository
313 #include "HashTable.C"
314#endif
315
316// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317
318#endif
319
320// ************************************************************************* //
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.
streamFormat
Data format (ascii | binary | coherent).
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED).
@ UNCOMPRESSED
compression = false
IOstream(const IOstream &)=default
Copy construct.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
bool getBack(token &tok)
Retrieve the put-back token if there is one.
Definition Istream.C:115
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:192
virtual void rewind()=0
Rewind the stream so that it may be read again.
Definition Istream.C:214
Istream(IOstreamOption::streamFormat fmt, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format (uncompressed).
Definition Istream.H:115
bool readEnd(const char *funcName)
End read of data chunk, ends with ')'.
Definition Istream.C:152
Istream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition Istream.H:105
virtual ~Istream()=default
Destructor.
const token & peekBack() const noexcept
Examine putback token without removing it.
Definition Istream.C:42
bool hasPutback() const noexcept
True if putback token is in use.
Definition Istream.H:81
Istream(const Istream &)=default
Copy construct.
virtual bool beginRawRead()=0
Start of low-level raw binary read.
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...
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition Istream.C:171
void putBackClear()
Drop the putback token.
Definition Istream.C:64
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition Istream.C:134
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.
Accumulates point constraints through successive applications of the applyConstraint function.
A token holds an item read from Istream.
Definition token.H:70
A class for handling words, derived from Foam::string.
Definition word.H:66
Implementation details for various OpenFOAM classes.
Definition zoneSubSet.C:30
void readContiguous< pointConstraint >(Istream &is, char *data, std::streamsize byteCount)
Read binary block of pointConstraint data (possibly with conversion).
void readContiguous(Istream &is, char *data, std::streamsize byteCount)
Read binary block of contiguous data, possibly with conversion.
Definition Istream.H:322
Namespace for OpenFOAM.
void readRawLabel(Istream &is, label *data, size_t nElem=1)
Read raw label(s) from binary stream.
Definition label.C:77
Istream & operator>>(Istream &, directionInfo &)
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition IOstream.H:562
const direction noexcept
Definition scalarImpl.H:265
Istream &(* IstreamManip)(Istream &)
An Istream manipulator.
Definition Istream.H:293
labelList f(nPoints)
A template class to specify if a data type is composed solely of Foam::label elements.
Definition contiguous.H:82
A template class to specify if a data type is composed solely of Foam::scalar elements.
Definition contiguous.H:87