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 -------------------------------------------------------------------------------
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 flags of stream
343  virtual ios_base::fmtflags flags() const = 0;
344 
345  //- Return the default precision
346  static unsigned int defaultPrecision() noexcept
347  {
348  return precision_;
349  }
350 
351  //- Reset the default precision
352  // \return the previous value
353  static unsigned int defaultPrecision(unsigned int prec) noexcept
354  {
355  unsigned int old(precision_);
356  precision_ = prec;
357  return old;
358  }
359 
360  //- Set stream state as reached 'eof'
361  void setEof() noexcept
362  {
363  ioState_ |= std::ios_base::eofbit;
364  }
365 
366  //- Set stream state as 'failed'
367  void setFail() noexcept
368  {
369  ioState_ |= std::ios_base::failbit;
370  }
371 
372  //- Set stream state to be 'bad'
373  void setBad()
374  {
375  ioState_ |= std::ios_base::badbit;
376  }
377 
378  //- Set flags of stream
379  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
380 
381  //- Set flags of stream
382  ios_base::fmtflags setf(const ios_base::fmtflags f)
383  {
384  return flags(flags() | f);
385  }
386 
387  //- Set flags of given field of stream
388  ios_base::fmtflags setf
389  (
390  const ios_base::fmtflags f,
391  const ios_base::fmtflags mask
392  )
393  {
394  return flags((flags() & ~mask) | (f & mask));
395  }
396 
397  //- Unset flags of stream
398  void unsetf(const ios_base::fmtflags f)
399  {
400  flags(flags() & ~f);
401  }
402 
403 
404  // Print
405 
406  //- Print stream description to Ostream
407  virtual void print(Ostream& os) const;
409  //- Print information about the stream state bits
410  void print(Ostream& os, const int streamState) const;
411 
412 
413  // Info
414 
415  //- Return info proxy,
416  //- used to print IOstream information to a stream
417  InfoProxy<IOstream> info() const noexcept { return *this; }
418 };
419 
420 
421 // --------------------------------------------------------------------
422 // ------ Manipulators (not taking arguments)
423 // --------------------------------------------------------------------
424 
425 //- An IOstream manipulator
426 typedef IOstream& (*IOstreamManip)(IOstream&);
427 
428 //- operator<< handling for manipulators without arguments
430 {
431  return f(io);
432 }
434 
435 inline IOstream& dec(IOstream& io)
436 {
437  io.setf(std::ios_base::dec, std::ios_base::basefield);
438  return io;
439 }
440 
441 inline IOstream& hex(IOstream& io)
442 {
443  io.setf(std::ios_base::hex, std::ios_base::basefield);
444  return io;
445 }
446 
447 inline IOstream& oct(IOstream& io)
448 {
449  io.setf(std::ios_base::oct, std::ios_base::basefield);
450  return io;
451 }
452 
453 inline IOstream& fixed(IOstream& io)
454 {
455  io.setf(std::ios_base::fixed, std::ios_base::floatfield);
456  return io;
457 }
458 
460 {
461  io.setf(std::ios_base::scientific, std::ios_base::floatfield);
462  return io;
463 }
464 
465 
466 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
467 
468 namespace Detail
469 {
470 
471 //- Termination for input looping (no-op)
472 template<class IS> inline void inputLoop(IS&) {}
473 
474 //- Termination for output looping (no-op)
475 template<class OS> inline void outputLoop(OS&) {}
476 
477 //- Input looping. Read into first parameter and recurse.
478 template<class IS, class Type, class... Args>
479 inline void inputLoop(IS& is, Type& arg1, Args&&... args)
480 {
481  is >> arg1;
482  Detail::inputLoop(is, std::forward<Args>(args)...);
483 }
484 
485 //- Output looping. Write first parameter and recurse.
486 template<class OS, class Type, class... Args>
487 inline void outputLoop(OS& os, const Type& arg1, Args&&... args)
488 {
489  os << arg1;
490  Detail::outputLoop(os, std::forward<Args>(args)...);
491 }
493 } // End namespace Detail
494 
495 
496 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
497 
498 } // End namespace Foam
499 
500 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
501 
502 #endif
503 
504 // ************************************************************************* //
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:451
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)
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:472
IOstream & hex(IOstream &io)
Definition: IOstream.H:545
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
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
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
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:492
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition: IOstream.H:517
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:423
A simple container for options an IOstream can normally have.
IOstream & fixed(IOstream &io)
Definition: IOstream.H:557
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:340
IOstream & oct(IOstream &io)
Definition: IOstream.H:551
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.
void setBad()
Set stream state to be &#39;bad&#39;.
Definition: IOstream.H:459
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
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:578
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
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:43
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:539
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:443
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:583
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:528
System bool.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:563
Namespace for OpenFOAM.
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition: IOstream.H:120