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-2024 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::IOstream
29 
30 Description
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 
40 SourceFiles
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 
59 using std::ios_base;
60 using std::istream;
61 using std::ostream;
62 
63 using std::cerr;
64 using std::cin;
65 using 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 
73 namespace Foam
74 {
75 
76 // Forward Declarations
77 class IOstream;
78 
79 template<>
80 Ostream& operator<<(Ostream&, const InfoProxy<IOstream>&);
81 
82 
83 /*---------------------------------------------------------------------------*\
84  Class IOstream Declaration
85 \*---------------------------------------------------------------------------*/
86 
87 class IOstream
88 :
89  public IOstreamOption
90 {
91 public:
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 
109 protected:
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  {
140  }
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
150  {
151  ioState_ = state;
152  }
153 
154  //- Set stream state to be good
155  void setGood() noexcept
156  {
157  ioState_ = std::ios_base::goodbit;
158  }
159 
160 
161 public:
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  }
187 
188  //- Construct with format, version (compression)
189  IOstream
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  //- Return true if the stream has not failed
261  explicit operator bool() const noexcept
262  {
263  return !fail();
264  }
266  //- Return 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
289  {
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);
297  }
298 
299 
300  //- Check if the label byte-size associated with the stream
301  //- is the same as the given type
302  template<class T = label>
303  typename std::enable_if<std::is_integral<T>::value, bool>::type
304  checkLabelSize() const noexcept
305  {
306  return sizeofLabel_ == sizeof(T);
307  }
308 
309  //- Check if the scalar byte-size associated with the stream
310  //- is the same as the given type
311  template<class T = scalar>
312  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
314  {
315  return sizeofScalar_ == sizeof(T);
316  }
317 
318 
319  // Stream State Functions
320 
321  //- Const access to the current stream line number
322  label lineNumber() const noexcept
323  {
324  return lineNumber_;
325  }
326 
327  //- Non-const access to the current stream line number
328  label& lineNumber() noexcept
329  {
330  return lineNumber_;
331  }
333  //- Set the stream line number
334  // \return the previous value
335  label lineNumber(const label num) noexcept
336  {
337  label old(lineNumber_);
338  lineNumber_ = num;
339  return old;
340  }
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 default precision
350  static unsigned int defaultPrecision(unsigned int prec) noexcept
351  {
352  unsigned int old(precision_);
353  precision_ = prec;
354  return old;
355  }
357  //- Set the minimum default precision
358  // \return the previous default precision
359  static unsigned int minPrecision(unsigned int prec) noexcept
360  {
361  unsigned int old(precision_);
362  if (precision_ < prec)
363  {
364  precision_ = prec;
365  }
366  return old;
367  }
369  //- Set stream state as reached 'eof'
370  void setEof() noexcept
371  {
372  ioState_ |= std::ios_base::eofbit;
373  }
374 
375  //- Set stream state as 'failed'
376  void setFail() noexcept
377  {
378  ioState_ |= std::ios_base::failbit;
379  }
380 
381  //- Set stream state to be 'bad'
382  void setBad() noexcept
383  {
384  ioState_ |= std::ios_base::badbit;
385  }
386 
387  //- Return current stream flags
388  virtual std::ios_base::fmtflags flags() const = 0;
389 
390  //- Set stream flags, return old stream flags
391  virtual std::ios_base::fmtflags flags(std::ios_base::fmtflags) = 0;
392 
393  //- Set stream flag(s), return old stream flags
394  std::ios_base::fmtflags setf(std::ios_base::fmtflags f)
395  {
396  return flags(flags() | f);
397  }
399  //- Set stream flag(s) with mask, return old stream flags
400  std::ios_base::fmtflags setf
401  (
402  const std::ios_base::fmtflags f,
403  const std::ios_base::fmtflags mask
404  )
405  {
406  return flags((flags() & ~mask) | (f & mask));
407  }
409  //- Unset flags of stream
410  void unsetf(std::ios_base::fmtflags f)
411  {
412  flags(flags() & ~f);
413  }
414 
415 
416  // Print
417 
418  //- Print stream description to Ostream
419  virtual void print(Ostream& os) const;
420 
421  //- Print information about the stream state bits
422  void print(Ostream& os, const int streamState) const;
423 
424 
425  // Info
426 
427  //- Return info proxy,
428  //- used to print IOstream information to a stream
429  InfoProxy<IOstream> info() const noexcept { return *this; }
430 };
431 
432 
433 // --------------------------------------------------------------------
434 // ------ Manipulators (not taking arguments)
435 // --------------------------------------------------------------------
436 
437 //- An IOstream manipulator
438 typedef IOstream& (*IOstreamManip)(IOstream&);
439 
440 //- operator<< handling for manipulators without arguments
442 {
443  return f(io);
444 }
445 
446 
447 inline IOstream& dec(IOstream& io)
448 {
449  io.setf(std::ios_base::dec, std::ios_base::basefield);
450  return io;
451 }
452 
454 {
455  io.setf(std::ios_base::hex, std::ios_base::basefield);
456  return io;
457 }
458 
459 inline IOstream& oct(IOstream& io)
460 {
461  io.setf(std::ios_base::oct, std::ios_base::basefield);
462  return io;
463 }
464 
465 inline IOstream& fixed(IOstream& io)
466 {
467  io.setf(std::ios_base::fixed, std::ios_base::floatfield);
468  return io;
469 }
470 
471 inline IOstream& scientific(IOstream& io)
472 {
473  io.setf(std::ios_base::scientific, std::ios_base::floatfield);
474  return io;
475 }
476 
477 
478 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
479 
480 namespace Detail
481 {
482 
483 /*---------------------------------------------------------------------------*\
484  Class Detail::StreamAllocator Declaration
485 \*---------------------------------------------------------------------------*/
486 
487 //- A wrapper to hold a std::stream type for OpenFOAM wrapped streams.
488 //- This is necessary since the OpenFOAM streams hold a reference to
489 //- the normal std::stream
490 template<class StreamType>
491 class StreamAllocator
492 {
493 protected:
494 
495  // Protected Data
497  //- The std::stream
498  StreamType stream_;
499 
500 
501  // Constructors
502 
503  //- Default construct (empty)
504  StreamAllocator() = default;
505 };
506 
508 } // End namespace Detail
509 
510 
511 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
512 
513 // Functions/Algorithms
514 
515 namespace Detail
516 {
517 
518 //- Termination for input looping (no-op)
519 template<class IS> inline void inputLoop(IS&) {}
520 
521 //- Termination for output looping (no-op)
522 template<class OS> inline void outputLoop(OS&) {}
523 
524 //- Input looping. Read into first parameter and recurse.
525 template<class IS, class Type, class... Args>
526 inline void inputLoop(IS& is, Type& arg1, Args&&... args)
527 {
528  is >> arg1;
529  Detail::inputLoop(is, std::forward<Args>(args)...);
530 }
531 
532 //- Output looping. Write first parameter and recurse.
533 template<class OS, class Type, class... Args>
534 inline void outputLoop(OS& os, const Type& arg1, Args&&... args)
535 {
536  os << arg1;
537  Detail::outputLoop(os, std::forward<Args>(args)...);
538 }
539 
540 } // End namespace Detail
541 
542 
543 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
544 
545 } // End namespace Foam
546 
547 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
549 #endif
550 
551 // ************************************************************************* //
label lineNumber_
The file line.
Definition: IOstream.H:140
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:265
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:39
bool operator!() const noexcept
Return true if the stream has failed.
Definition: IOstream.H:321
void setFail() noexcept
Set stream state as &#39;failed&#39;.
Definition: IOstream.H:461
A class for handling file names.
Definition: fileName.H:72
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:305
virtual const fileName & name() const
The name of the stream.
Definition: IOstream.C:33
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const noexcept
Check if the scalar byte-size associated with the stream is the same as the given type...
Definition: IOstream.H:379
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:158
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
IOstream & hex(IOstream &io)
Definition: IOstream.H:560
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
void unsetf(std::ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:507
streamAccess openClosed_
The stream open/closed state.
Definition: IOstream.H:125
streamAccess
Enumeration for stream open/closed state.
Definition: IOstream.H:93
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.H:115
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition: IOstream.H:532
StreamAllocator()=default
Default construct (empty)
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:418
A simple container for options an IOstream can normally have.
std::ios_base::fmtflags setf(std::ios_base::fmtflags f)
Set stream flag(s), return old stream flags.
Definition: IOstream.H:487
IOstream & fixed(IOstream &io)
Definition: IOstream.H:572
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:340
IOstream & oct(IOstream &io)
Definition: IOstream.H:566
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Default construct (ASCII, UNCOMPRESSED, currentVersion) or construct with format, compression...
A character and a pointer to a character string.
virtual std::ios_base::fmtflags flags() const =0
Return current stream flags.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
StreamType stream_
The std::stream.
Definition: IOstream.H:609
IOstream(const IOstream &)=default
Copy construct.
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:348
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:634
unsigned char sizeofLabel_
The sizeof (label), possibly read from the header.
Definition: IOstream.H:130
bool fail() const noexcept
True if next operation will fail.
Definition: IOstream.H:297
The stream is open.
Definition: IOstream.H:96
unsigned char sizeofScalar_
The sizeof (scalar), possibly read from the header.
Definition: IOstream.H:135
static unsigned int minPrecision(unsigned int prec) noexcept
Set the minimum default precision.
Definition: IOstream.H:440
Representation of a major/minor version number.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
OBJstream os(runTime.globalPath()/outputName)
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:67
labelList f(nPoints)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:390
virtual ~IOstream()=default
Destructor.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
static unsigned int precision_
Default precision.
Definition: IOstream.H:105
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
A helper class for outputting values to Ostream.
Definition: ensightCells.H:44
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:356
bool closed() const noexcept
True if stream is closed.
Definition: IOstream.H:273
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
streamFormat
Data format (ascii | binary)
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:150
IOstream & dec(IOstream &io)
Definition: IOstream.H:554
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Check if the label byte-size associated with the stream is the same as the given type.
Definition: IOstream.H:368
void setEof() noexcept
Set stream state as reached &#39;eof&#39;.
Definition: IOstream.H:453
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:174
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Foam::argList args(argc, argv)
The stream is not open.
Definition: IOstream.H:95
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:639
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:332
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:289
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:543
void setBad() noexcept
Set stream state to be &#39;bad&#39;.
Definition: IOstream.H:469
System bool.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:578
Namespace for OpenFOAM.
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition: IOstream.H:120