Loading...
Searching...
No Matches
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-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::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::cerr;
64using std::cin;
65using std::cout;
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// Forward Declarations
77class IOstream;
78
79template<>
80Ostream& operator<<(Ostream&, const InfoProxy<IOstream>&);
81
83/*---------------------------------------------------------------------------*\
84 Class IOstream Declaration
85\*---------------------------------------------------------------------------*/
86
87class IOstream
88:
89 public IOstreamOption
90{
91public:
92
93 // Public Data Types
94
95 //- Enumeration for stream open/closed state
96 enum streamAccess : char
97 {
98 CLOSED = 0,
99 OPENED
100 };
101
102
103 // Public Static Data
104
105 //- Default precision
106 static unsigned int precision_;
107
108
109protected:
110
111 // Protected Data
112
113 //- Name for any generic stream - normally treat as readonly
114 static fileName staticName_;
116 //- Mirror of internal stream io state
117 std::ios_base::iostate ioState_;
118
119 //- The stream open/closed state
121
122 //- The sizeof (label), possibly read from the header
123 unsigned char sizeofLabel_;
124
125 //- The sizeof (scalar), possibly read from the header
126 unsigned char sizeofScalar_;
127
128 //- The file line
129 label lineNumber_;
131
132 // Protected Member Functions
133
134 // Access
136 //- Set stream opened
137 void setOpened() noexcept
138 {
141
142 //- Set stream closed
143 void setClosed() noexcept
144 {
146 }
147
148 //- Set stream state
149 void setState(std::ios_base::iostate state) noexcept
151 ioState_ = state;
152 }
153
154 //- Set stream state to be good
155 void setGood() noexcept
156 {
157 ioState_ = std::ios_base::goodbit;
159
160
161public:
162
163 // Generated Methods
164
165 //- Copy construct
166 IOstream(const IOstream&) = default;
167
168 //- Destructor
169 virtual ~IOstream() = default;
170
171
172 // Constructors
173
174 //- Default construct (ASCII, uncompressed),
175 //- construct with specified stream option
176 explicit IOstream(IOstreamOption streamOpt = IOstreamOption())
177 :
178 IOstreamOption(streamOpt),
179 ioState_(std::ios_base::goodbit),
181 sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
182 sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
183 lineNumber_(0)
184 {
185 setBad();
186 }
188 //- Construct with format, version (compression)
190 (
194 )
195 :
196 IOstream(IOstreamOption(fmt, ver, cmp))
197 {}
198
199
200 // Member Functions
202 // Access
203
204 //- The name of the stream.
205 virtual const fileName& name() const;
206
207 //- Return the name of the stream relative to the current case.
208 // Uses argList::envRelativePath()
209 fileName relativeName() const;
210
211
212 // Check
213
214 //- Check IOstream status for given operation.
215 // Print IOstream state or generate a FatalIOError
216 // when an error has occurred.
217 // The base implementation is a fatalCheck
218 virtual bool check(const char* operation) const;
219
220 //- Check IOstream status for given operation.
221 // Generate a FatalIOError when an error has occurred.
222 bool fatalCheck(const char* operation) const;
223
224 //- True if stream has been opened
225 bool opened() const noexcept
226 {
227 return openClosed_ == OPENED;
228 }
229
230 //- True if stream is closed
231 bool closed() const noexcept
232 {
233 return openClosed_ == CLOSED;
234 }
235
236 //- True if next operation might succeed
237 bool good() const noexcept
238 {
239 return ioState_ == 0; // == goodbit
240 }
241
242 //- True if end of input seen
243 bool eof() const noexcept
244 {
245 return ioState_ & std::ios_base::eofbit;
246 }
247
248 //- True if next operation will fail
249 bool fail() const noexcept
250 {
251 return ioState_ & (std::ios_base::badbit | std::ios_base::failbit);
252 }
253
254 //- True if stream is corrupted
255 bool bad() const noexcept
256 {
257 return ioState_ & std::ios_base::badbit;
258 }
259
260 //- True if the stream has not failed
261 explicit operator bool() const noexcept
262 {
263 return !fail();
264 }
266 //- True if the stream has failed
267 bool operator!() const noexcept
268 {
269 return fail();
270 }
271
272
273 // Element sizes (precision)
274
275 //- The sizeof (label) in bytes associated with the stream
276 unsigned labelByteSize() const noexcept
277 {
278 return static_cast<unsigned>(sizeofLabel_);
279 }
280
281 //- The sizeof (scalar) in bytes associated with the stream
282 unsigned scalarByteSize() const noexcept
283 {
284 return static_cast<unsigned>(sizeofScalar_);
285 }
286
287 //- Set the sizeof (label) in bytes associated with the stream
288 void setLabelByteSize(unsigned nbytes) noexcept
290 sizeofLabel_ = static_cast<unsigned char>(nbytes);
291 }
292
293 //- Set the sizeof (scalar) in bytes associated with the stream
294 void setScalarByteSize(unsigned nbytes) noexcept
295 {
296 sizeofScalar_ = static_cast<unsigned char>(nbytes);
298
299
300 //- Test if the label byte-size associated with the stream
301 //- is the same as the given type
302 template<class T, class = std::enable_if_t<std::is_integral_v<T>>>
303 bool checkLabelSize() const noexcept
304 {
305 return sizeofLabel_ == sizeof(T);
306 }
307
308 //- Test if the scalar byte-size associated with the stream
309 //- is the same as the given type
310 template<class T, class = std::enable_if_t<std::is_floating_point_v<T>>>
311 bool checkScalarSize() const noexcept
312 {
313 return sizeofScalar_ == sizeof(T);
314 }
315
316 //- Test if the label/scalar byte-size associated with the stream
317 //- are the native label/scalar sizes
318 bool checkNativeSizes() const noexcept
319 {
320 return
322 sizeofLabel_ == sizeof(label)
323 && sizeofScalar_ == sizeof(scalar)
324 );
325 }
326
327 //- Assert that the label/scalar byte-size associated with the stream
328 //- are the native label/scalar sizes.
329 // Generate a FatalIOError for any mismatch.
330 bool fatalCheckNativeSizes(const char* operation) const;
331
333 // Stream State Functions
334
335 //- Const access to the current stream line number
336 label lineNumber() const noexcept
337 {
338 return lineNumber_;
339 }
341 //- Non-const access to the current stream line number
342 label& lineNumber() noexcept
343 {
344 return lineNumber_;
345 }
346
347 //- Set the stream line number
348 // \return the previous value
349 label lineNumber(const label num) noexcept
350 {
351 label old(lineNumber_);
352 lineNumber_ = num;
353 return old;
354 }
355
356 //- Return the default precision
357 static unsigned int defaultPrecision() noexcept
358 {
359 return precision_;
360 }
361
362 //- Reset the default precision
363 // \return the previous default precision
364 static unsigned int defaultPrecision(unsigned int prec) noexcept
365 {
366 unsigned int old(precision_);
368 return old;
369 }
370
371 //- Set the minimum default precision
372 // \return the previous default precision
373 static unsigned int minPrecision(unsigned int prec) noexcept
374 {
375 unsigned int old(precision_);
376 if (precision_ < prec)
378 precision_ = prec;
379 }
380 return old;
381 }
382
383 //- Set stream state as reached 'eof'
384 void setEof() noexcept
385 {
386 ioState_ |= std::ios_base::eofbit;
387 }
388
389 //- Set stream state as 'failed'
390 void setFail() noexcept
391 {
392 ioState_ |= std::ios_base::failbit;
393 }
394
395 //- Set stream state to be 'bad'
396 void setBad() noexcept
397 {
398 ioState_ |= std::ios_base::badbit;
399 }
400
401 //- Return current stream flags
402 virtual std::ios_base::fmtflags flags() const = 0;
403
404 //- Set stream flags, return old stream flags
405 virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) = 0;
406
407 //- Set stream flag(s), return old stream flags
408 std::ios_base::fmtflags setf(std::ios_base::fmtflags f)
410 return flags(flags() | f);
411 }
412
413 //- Set stream flag(s) with mask, return old stream flags
414 std::ios_base::fmtflags setf
415 (
416 const std::ios_base::fmtflags f,
417 const std::ios_base::fmtflags mask
418 )
419 {
420 return flags((flags() & ~mask) | (f & mask));
421 }
422
423 //- Unset stream flags, return old stream flags
424 std::ios_base::fmtflags unsetf(std::ios_base::fmtflags f)
425 {
426 return flags(flags() & ~f);
428
429
430 // Print
431
432 //- Print stream description to Ostream
433 virtual void print(Ostream& os) const;
434
435 //- Print information about the stream state bits
436 void print(Ostream& os, const int streamState) const;
438
439 // Info
440
441 //- Return info proxy,
442 //- used to print IOstream information to a stream
443 InfoProxy<IOstream> info() const noexcept { return *this; }
444};
445
446
447// --------------------------------------------------------------------
448// ------ Manipulators (not taking arguments)
449// --------------------------------------------------------------------
450
451//- An IOstream manipulator
452typedef IOstream& (*IOstreamManip)(IOstream&);
453
454//- operator<< handling for manipulators without arguments
456{
457 return f(io);
458}
460
461inline IOstream& dec(IOstream& io)
462{
463 io.setf(std::ios_base::dec, std::ios_base::basefield);
464 return io;
465}
466
467inline IOstream& hex(IOstream& io)
468{
469 io.setf(std::ios_base::hex, std::ios_base::basefield);
470 return io;
471}
473inline IOstream& oct(IOstream& io)
474{
475 io.setf(std::ios_base::oct, std::ios_base::basefield);
476 return io;
477}
478
479inline IOstream& fixed(IOstream& io)
481 io.setf(std::ios_base::fixed, std::ios_base::floatfield);
482 return io;
483}
484
486{
487 io.setf(std::ios_base::scientific, std::ios_base::floatfield);
488 return io;
489}
490
491
492// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
493
494namespace Detail
495{
497/*---------------------------------------------------------------------------*\
498 Class Detail::StreamAllocator Declaration
499\*---------------------------------------------------------------------------*/
500
501//- A wrapper to hold a std::stream type for OpenFOAM wrapped streams.
502//- This is necessary since the OpenFOAM streams hold a reference to
503//- the normal std::stream
504template<class StreamType>
505class StreamAllocator
507protected:
508
509 // Protected Data
510
511 //- The std::stream
512 StreamType stream_;
513
515 // Constructors
516
517 //- Default construct (empty)
518 StreamAllocator() = default;
519};
520
521
522} // End namespace Detail
523
524
525// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
527// Functions/Algorithms
528
529namespace Detail
530{
531
532//- Input looping. Read into first parameter and recurse.
533template<class IS, class Type, class... Args>
534void inputLoop(IS& is, Type& arg1, Args&&... args)
535{
536 is >> arg1;
537 if constexpr (sizeof...(args))
538 {
539 Detail::inputLoop(is, std::forward<Args>(args)...);
540 }
541}
542
543//- Output looping. Write first parameter and recurse.
544template<class OS, class Type, class... Args>
545void outputLoop(OS& os, const Type& arg1, Args&&... args)
546{
547 os << arg1;
548 if constexpr (sizeof...(args))
549 {
550 Detail::outputLoop(os, std::forward<Args>(args)...);
552}
553
554} // End namespace Detail
555
556
557// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
558
559} // End namespace Foam
560
561// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
563#endif
564
565// ************************************************************************* //
System bool.
A character and a pointer to a character string.
StreamAllocator()=default
Default construct (empty).
Representation of a major/minor version number.
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
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition IOstream.H:85
std::ios_base::fmtflags setf(std::ios_base::fmtflags f)
Set stream flag(s), return old stream flags.
Definition IOstream.H:506
bool fail() const noexcept
True if next operation will fail.
Definition IOstream.H:297
void setBad() noexcept
Set stream state to be 'bad'.
Definition IOstream.H:488
label lineNumber() const noexcept
Const access to the current stream line number.
Definition IOstream.H:409
virtual ~IOstream()=default
Destructor.
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:51
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition IOstream.H:340
static unsigned int precision_
Default precision.
Definition IOstream.H:105
IOstream(IOstreamOption streamOpt=IOstreamOption())
Default construct (ASCII, uncompressed), construct with specified stream option.
Definition IOstream.H:201
label & lineNumber() noexcept
Non-const access to the current stream line number.
Definition IOstream.H:417
unsigned char sizeofScalar_
The sizeof (scalar), possibly read from the header.
Definition IOstream.H:135
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
bool bad() const noexcept
True if stream is corrupted.
Definition IOstream.H:305
bool opened() const noexcept
True if stream has been opened.
Definition IOstream.H:265
IOstream(const IOstream &)=default
Copy construct.
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition IOstream.C:87
label lineNumber_
The file line.
Definition IOstream.H:140
virtual const fileName & name() const
The name of the stream.
Definition IOstream.C:33
bool checkScalarSize() const noexcept
Test if the scalar byte-size associated with the stream is the same as the given type.
Definition IOstream.H:377
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition IOstream.H:551
virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags)=0
Set stream flags, return old stream flags.
std::ios_base::fmtflags setf(const std::ios_base::fmtflags f, const std::ios_base::fmtflags mask)
Set stream flag(s) with mask, return old stream flags.
Definition IOstream.H:515
void setEof() noexcept
Set stream state as reached 'eof'.
Definition IOstream.H:472
virtual std::ios_base::fmtflags flags() const =0
Return current stream flags.
static unsigned int minPrecision(unsigned int prec) noexcept
Set the minimum default precision.
Definition IOstream.H:459
std::ios_base::fmtflags unsetf(std::ios_base::fmtflags f)
Unset stream flags, return old stream flags.
Definition IOstream.H:526
void setClosed() noexcept
Set stream closed.
Definition IOstream.H:158
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition IOstream.H:348
bool eof() const noexcept
True if end of input seen.
Definition IOstream.H:289
bool checkNativeSizes() const noexcept
Test if the label/scalar byte-size associated with the stream are the native label/scalar sizes.
Definition IOstream.H:386
label lineNumber(const label num) noexcept
Set the stream line number.
Definition IOstream.H:427
streamAccess openClosed_
The stream open/closed state.
Definition IOstream.H:125
IOstream(IOstreamOption::streamFormat fmt, IOstreamOption::versionNumber ver, IOstreamOption::compressionType cmp=IOstreamOption::UNCOMPRESSED)
Construct with format, version (compression).
Definition IOstream.H:217
static unsigned int defaultPrecision(unsigned int prec) noexcept
Reset the default precision.
Definition IOstream.H:447
bool operator!() const noexcept
True if the stream has failed.
Definition IOstream.H:321
streamAccess
Enumeration for stream open/closed state.
Definition IOstream.H:94
@ OPENED
The stream is open.
Definition IOstream.H:96
@ CLOSED
The stream is not open.
Definition IOstream.H:95
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition IOstream.H:356
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition IOstream.H:166
bool fatalCheckNativeSizes(const char *operation) const
Assert that the label/scalar byte-size associated with the stream are the native label/scalar sizes.
Definition IOstream.C:67
unsigned char sizeofLabel_
The sizeof (label), possibly read from the header.
Definition IOstream.H:130
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition IOstream.H:115
bool closed() const noexcept
True if stream is closed.
Definition IOstream.H:273
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition IOstream.H:332
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition IOstream.C:39
void setGood() noexcept
Set stream state to be good.
Definition IOstream.H:174
void setFail() noexcept
Set stream state as 'failed'.
Definition IOstream.H:480
void setOpened() noexcept
Set stream opened.
Definition IOstream.H:150
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition IOstream.H:120
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition IOstream.H:437
bool checkLabelSize() const noexcept
Test if the label byte-size associated with the stream is the same as the given type.
Definition IOstream.H:367
A helper class for outputting values to Ostream.
Definition InfoProxy.H:49
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A class for handling file names.
Definition fileName.H:75
OBJstream os(runTime.globalPath()/outputName)
const auto & io
Implementation details for various OpenFOAM classes.
Definition zoneSubSet.C:30
void outputLoop(OS &os, const Type &arg1, Args &&... args)
Output looping. Write first parameter and recurse.
Definition IOstream.H:667
void inputLoop(IS &is, Type &arg1, Args &&... args)
Input looping. Read into first parameter and recurse.
Definition IOstream.H:654
Namespace for OpenFOAM.
IOstream & fixed(IOstream &io)
Definition IOstream.H:591
IOstream & oct(IOstream &io)
Definition IOstream.H:585
IOstream & hex(IOstream &io)
Definition IOstream.H:579
IOstream & scientific(IOstream &io)
Definition IOstream.H:597
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition IOstream.H:562
IOstream & dec(IOstream &io)
Definition IOstream.H:573
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
labelList f(nPoints)
Foam::argList args(argc, argv)