IOstream.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-2015 OpenFOAM Foundation
9 Copyright (C) 2018-2022 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::IOstream
29
30Description
31 An IOstream is an abstract base class for all input/output systems; be
32 they streams, files, token lists etc.
33
34 The basic operations are construct, close, read token, read primitive
35 and read binary block. In addition version control and line number
36 counting is incorporated. Usually one would use the read primitive
37 member functions, but if one were reading a stream on unknown data
38 sequence one can read token by token, and then analyse.
39
40SourceFiles
41 IOstream.C
42
43\*---------------------------------------------------------------------------*/
44
45#ifndef Foam_IOstream_H
46#define Foam_IOstream_H
47
48#include "char.H"
49#include "bool.H"
50#include "label.H"
51#include "uLabel.H"
52#include "scalar.H"
53#include "fileName.H"
54#include "InfoProxy.H"
55#include "IOstreamOption.H"
56
57#include <iostream>
58
59using std::ios_base;
60using std::istream;
61using std::ostream;
62
63using std::cin;
64using std::cout;
65using std::cerr;
66
67// Additional constructors and methods (as per v2012 and earlier)
68#define Foam_IOstream_extras
69// COMPAT_OPENFOAM_ORG
70
71// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72
73namespace Foam
74{
75
76/*---------------------------------------------------------------------------*\
77 Class IOstream Declaration
78\*---------------------------------------------------------------------------*/
80class IOstream
81:
82 public IOstreamOption
83{
84public:
85
86 // Public Data Types
87
88 //- Enumeration for stream open/closed state
89 enum streamAccess : char
90 {
91 CLOSED = 0,
93 };
94
95
96 // Public Static Data
97
98 //- Default precision
99 static unsigned int precision_;
100
101
102protected:
103
104 // Protected Data
105
106 //- Name for any generic stream - normally treat as readonly
107 static fileName staticName_;
108
109 //- Mirror of internal stream io state
110 std::ios_base::iostate ioState_;
111
112 //- The stream open/closed state
114
115 //- The sizeof (label), possibly read from the header
116 unsigned char sizeofLabel_;
117
118 //- The sizeof (scalar), possibly read from the header
119 unsigned char sizeofScalar_;
120
121 //- The file line
122 label lineNumber_;
123
124
125 // Protected Member Functions
126
127 // Access
128
129 //- Set stream opened
130 void setOpened() noexcept
131 {
133 }
134
135 //- Set stream closed
136 void setClosed() noexcept
137 {
139 }
140
141 //- Set stream state
142 void setState(std::ios_base::iostate state) noexcept
143 {
144 ioState_ = state;
145 }
146
147 //- Set stream state to be good
148 void setGood() noexcept
149 {
150 ioState_ = std::ios_base::iostate(0);
151 }
152
153
154public:
155
156 // Generated Methods
157
158 //- Copy construct
159 IOstream(const IOstream&) = default;
160
161 //- Destructor
162 virtual ~IOstream() = default;
163
164
165 // Constructors
166
167 //- Default construct (ASCII, uncompressed),
168 //- construct with specified stream option
169 explicit IOstream(IOstreamOption streamOpt = IOstreamOption())
170 :
171 IOstreamOption(streamOpt),
172 ioState_(std::ios_base::iostate(0)),
174 sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
175 sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
176 lineNumber_(0)
177 {
178 setBad();
179 }
180
181 //- Construct with format, version (compression)
183 (
187 )
188 :
189 IOstream(IOstreamOption(fmt, ver, cmp))
190 {}
191
192
193 // Member Functions
194
195 // Access
196
197 //- Return the name of the stream.
198 // Useful for Fstream to remember the filename
199 virtual const fileName& name() const;
200
201 //- Return stream name for modification
202 virtual fileName& name();
203
204 //- Return the name of the stream relative to the current case.
205 // Uses argList::envRelativePath()
206 fileName relativeName() const;
207
208
209 // Check
210
211 //- Check IOstream status for given operation.
212 // Print IOstream state or generate a FatalIOError
213 // when an error has occurred.
214 // The base implementation is a fatalCheck
215 virtual bool check(const char* operation) const;
216
217 //- Check IOstream status for given operation.
218 // Generate a FatalIOError when an error has occurred.
219 bool fatalCheck(const char* operation) const;
220
221 //- True if stream has been opened
222 bool opened() const noexcept
223 {
224 return openClosed_ == OPENED;
225 }
226
227 //- True if stream is closed
228 bool closed() const noexcept
229 {
230 return openClosed_ == CLOSED;
231 }
232
233 //- True if next operation might succeed
234 bool good() const noexcept
235 {
236 return ioState_ == 0;
237 }
238
239 //- True if end of input seen
240 bool eof() const noexcept
241 {
242 return ioState_ & std::ios_base::eofbit;
243 }
244
245 //- True if next operation will fail
246 bool fail() const noexcept
247 {
248 return ioState_ & (std::ios_base::badbit | std::ios_base::failbit);
249 }
250
251 //- True if stream is corrupted
252 bool bad() const noexcept
253 {
254 return ioState_ & std::ios_base::badbit;
255 }
256
257 //- Return true if the stream has not failed
258 explicit operator bool() const noexcept
259 {
260 return !fail();
261 }
262
263 //- Return true if the stream has failed
264 bool operator!() const noexcept
265 {
266 return fail();
267 }
268
269
270 // Element sizes (precision)
271
272 //- The sizeof (label) in bytes associated with the stream
273 unsigned labelByteSize() const noexcept
274 {
275 return static_cast<unsigned>(sizeofLabel_);
276 }
277
278 //- The sizeof (scalar) in bytes associated with the stream
279 unsigned scalarByteSize() const noexcept
280 {
281 return static_cast<unsigned>(sizeofScalar_);
282 }
283
284 //- Set the sizeof (label) in bytes associated with the stream
285 void setLabelByteSize(unsigned nbytes) noexcept
286 {
287 sizeofLabel_ = static_cast<unsigned char>(nbytes);
288 }
289
290 //- Set the sizeof (scalar) in bytes associated with the stream
291 void setScalarByteSize(unsigned nbytes) noexcept
292 {
293 sizeofScalar_ = static_cast<unsigned char>(nbytes);
294 }
295
296
297 //- Check if the label byte-size associated with the stream
298 //- is the same as the given type
299 template<class T = label>
300 typename std::enable_if<std::is_integral<T>::value, bool>::type
302 {
303 return sizeofLabel_ == sizeof(T);
304 }
305
306 //- Check if the scalar byte-size associated with the stream
307 //- is the same as the given type
308 template<class T = scalar>
309 typename std::enable_if<std::is_floating_point<T>::value, bool>::type
311 {
312 return sizeofScalar_ == sizeof(T);
313 }
314
315
316 // Stream State Functions
317
318 //- Const access to the current stream line number
319 label lineNumber() const noexcept
320 {
321 return lineNumber_;
322 }
323
324 //- Non-const access to the current stream line number
325 label& lineNumber() noexcept
326 {
327 return lineNumber_;
328 }
329
330 //- Set the stream line number
331 // \return the previous value
332 label lineNumber(const label num) noexcept
333 {
334 const label old(lineNumber_);
335 lineNumber_ = num;
336 return old;
337 }
338
339 //- Return flags of stream
340 virtual ios_base::fmtflags flags() const = 0;
341
342 //- Return the default precision
343 static unsigned int defaultPrecision() noexcept
344 {
345 return precision_;
346 }
347
348 //- Reset the default precision
349 // \return the previous value
350 static unsigned int defaultPrecision(unsigned int prec) noexcept
351 {
352 unsigned int old(precision_);
353 precision_ = prec;
354 return old;
355 }
356
357 //- Set stream state as reached 'eof'
358 void setEof() noexcept
359 {
360 ioState_ |= std::ios_base::eofbit;
361 }
362
363 //- Set stream state as 'failed'
364 void setFail() noexcept
365 {
366 ioState_ |= std::ios_base::failbit;
367 }
368
369 //- Set stream state to be 'bad'
370 void setBad()
371 {
372 ioState_ |= std::ios_base::badbit;
373 }
374
375 //- Set flags of stream
376 virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
377
378 //- Set flags of stream
379 ios_base::fmtflags setf(const ios_base::fmtflags f)
380 {
381 return flags(flags() | f);
382 }
383
384 //- Set flags of given field of stream
385 ios_base::fmtflags setf
386 (
387 const ios_base::fmtflags f,
388 const ios_base::fmtflags mask
389 )
390 {
391 return flags((flags() & ~mask) | (f & mask));
392 }
393
394 //- Unset flags of stream
395 void unsetf(const ios_base::fmtflags f)
396 {
397 flags(flags() & ~f);
398 }
399
400
401 // Print
402
403 //- Print stream description to Ostream
404 virtual void print(Ostream& os) const;
405
406 //- Print information about the stream state bits
407 void print(Ostream& os, const int streamState) const;
408
409
410 // Info
411
412 //- Return info proxy.
413 // Used to print IOstream information to a stream
415 {
416 return *this;
417 }
418};
419
420
421// Ostream Operator
422
423template<>
424Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
425
426
427// --------------------------------------------------------------------
428// ------ Manipulators (not taking arguments)
429// --------------------------------------------------------------------
430
431//- An IOstream manipulator
432typedef IOstream& (*IOstreamManip)(IOstream&);
433
434//- operator<< handling for manipulators without arguments
436{
437 return f(io);
438}
439
441inline IOstream& dec(IOstream& io)
442{
443 io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
444 return io;
445}
447inline IOstream& hex(IOstream& io)
448{
449 io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
450 return io;
451}
453inline IOstream& oct(IOstream& io)
454{
455 io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
456 return io;
457}
459inline IOstream& fixed(IOstream& io)
460{
461 io.setf(ios_base::fixed, ios_base::floatfield);
462 return io;
463}
466{
467 io.setf(ios_base::scientific, ios_base::floatfield);
468 return io;
469}
470
471
472// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
473
474namespace Detail
475{
476
477//- Termination for input looping (no-op)
478template<class IS> inline void inputLoop(IS&) {}
479
480//- Termination for output looping (no-op)
481template<class OS> inline void outputLoop(OS&) {}
482
483//- Input looping. Read into first parameter and recurse.
484template<class IS, class Type, class... Args>
485inline void inputLoop(IS& is, Type& arg1, Args&&... args)
486{
487 is >> arg1;
488 Detail::inputLoop(is, std::forward<Args>(args)...);
489}
490
491//- Output looping. Write first parameter and recurse.
492template<class OS, class Type, class... Args>
493inline void outputLoop(OS& os, const Type& arg1, Args&&... args)
494{
495 os << arg1;
496 Detail::outputLoop(os, std::forward<Args>(args)...);
497}
498
499} // End namespace Detail
500
501
502// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
503
504} // End namespace Foam
505
506// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
507
508#endif
509
510// ************************************************************************* //
System bool.
A character and a pointer to a character string.
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
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
bool fail() const noexcept
True if next operation will fail.
Definition: IOstream.H:245
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Definition: IOstream.H:300
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:318
virtual ~IOstream()=default
Destructor.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:278
static unsigned int precision_
Default precision.
Definition: IOstream.H:98
IOstream(IOstreamOption streamOpt=IOstreamOption())
Definition: IOstream.H:168
label & lineNumber() noexcept
Non-const access to the current stream line number.
Definition: IOstream.H:324
unsigned char sizeofScalar_
The sizeof (scalar), possibly read from the header.
Definition: IOstream.H:118
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:221
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:394
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
IOstream(const IOstream &)=default
Copy construct.
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:80
label lineNumber_
The file line.
Definition: IOstream.H:121
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
ios_base::fmtflags setf(const ios_base::fmtflags f, const ios_base::fmtflags mask)
Set flags of given field of stream.
Definition: IOstream.H:385
void setEof() noexcept
Set stream state as reached 'eof'.
Definition: IOstream.H:357
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:413
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:135
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:284
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
streamAccess openClosed_
The stream open/closed state.
Definition: IOstream.H:112
IOstream(IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format, version (compression)
Definition: IOstream.H:182
bool operator!() const noexcept
Return true if the stream has failed.
Definition: IOstream.H:263
streamAccess
Enumeration for stream open/closed state.
Definition: IOstream.H:89
@ OPENED
The stream is open.
Definition: IOstream.H:91
@ CLOSED
The stream is not open.
Definition: IOstream.H:90
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:290
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:141
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
unsigned char sizeofLabel_
The sizeof (label), possibly read from the header.
Definition: IOstream.H:115
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.H:106
bool closed() const noexcept
True if stream is closed.
Definition: IOstream.H:227
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:272
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
void setFail() noexcept
Set stream state as 'failed'.
Definition: IOstream.H:363
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const noexcept
Definition: IOstream.H:309
virtual ios_base::fmtflags flags(const ios_base::fmtflags f)=0
Set flags of stream.
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:129
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition: IOstream.H:109
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling file names.
Definition: fileName.H:76
const volScalarField & T
bool
Definition: EEqn.H:20
const cellModel & hex
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:477
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:480
Namespace for OpenFOAM.
IOstream & fixed(IOstream &io)
Definition: IOstream.H:458
IOstream & oct(IOstream &io)
Definition: IOstream.H:452
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:431
IOstream & dec(IOstream &io)
Definition: IOstream.H:440
const direction noexcept
Definition: Scalar.H:223
labelList f(nPoints)
Foam::argList args(argc, argv)