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 /*---------------------------------------------------------------------------*\
77  Class IOstream Declaration
78 \*---------------------------------------------------------------------------*/
79 
80 class IOstream
81 :
82  public IOstreamOption
83 {
84 public:
85 
86  // Public Data Types
87 
88  //- Enumeration for stream open/closed state
89  enum streamAccess : char
90  {
91  CLOSED = 0,
92  OPENED
93  };
94 
95 
96  // Public Static Data
97 
98  //- Default precision
99  static unsigned int precision_;
100 
101 
102 protected:
103 
104  // Protected Data
105 
106  //- Name for any generic stream - normally treat as readonly
107  static fileName staticName_;
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_;
124 
125  // Protected Member Functions
126 
127  // Access
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::goodbit;
151  }
152 
153 
154 public:
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::goodbit),
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)
182  IOstream
183  (
187  )
188  :
189  IOstream(IOstreamOption(fmt, ver, cmp))
190  {}
191 
192 
193  // Member Functions
195  // Access
196 
197  //- The name of the stream.
198  virtual const fileName& name() const;
199 
200  //- Return the name of the stream relative to the current case.
201  // Uses argList::envRelativePath()
202  fileName relativeName() const;
203 
204 
205  // Check
206 
207  //- Check IOstream status for given operation.
208  // Print IOstream state or generate a FatalIOError
209  // when an error has occurred.
210  // The base implementation is a fatalCheck
211  virtual bool check(const char* operation) const;
212 
213  //- Check IOstream status for given operation.
214  // Generate a FatalIOError when an error has occurred.
215  bool fatalCheck(const char* operation) const;
216 
217  //- True if stream has been opened
218  bool opened() const noexcept
219  {
220  return openClosed_ == OPENED;
221  }
222 
223  //- True if stream is closed
224  bool closed() const noexcept
225  {
226  return openClosed_ == CLOSED;
227  }
228 
229  //- True if next operation might succeed
230  bool good() const noexcept
231  {
232  return ioState_ == 0; // == goodbit
233  }
234 
235  //- True if end of input seen
236  bool eof() const noexcept
237  {
238  return ioState_ & std::ios_base::eofbit;
239  }
240 
241  //- True if next operation will fail
242  bool fail() const noexcept
243  {
244  return ioState_ & (std::ios_base::badbit | std::ios_base::failbit);
245  }
246 
247  //- True if stream is corrupted
248  bool bad() const noexcept
249  {
250  return ioState_ & std::ios_base::badbit;
251  }
252 
253  //- Return true if the stream has not failed
254  explicit operator bool() const noexcept
255  {
256  return !fail();
257  }
259  //- Return true if the stream has failed
260  bool operator!() const noexcept
261  {
262  return fail();
263  }
264 
265 
266  // Element sizes (precision)
267 
268  //- The sizeof (label) in bytes associated with the stream
269  unsigned labelByteSize() const noexcept
270  {
271  return static_cast<unsigned>(sizeofLabel_);
272  }
273 
274  //- The sizeof (scalar) in bytes associated with the stream
275  unsigned scalarByteSize() const noexcept
276  {
277  return static_cast<unsigned>(sizeofScalar_);
278  }
279 
280  //- Set the sizeof (label) in bytes associated with the stream
281  void setLabelByteSize(unsigned nbytes) noexcept
282  {
283  sizeofLabel_ = static_cast<unsigned char>(nbytes);
284  }
285 
286  //- Set the sizeof (scalar) in bytes associated with the stream
287  void setScalarByteSize(unsigned nbytes) noexcept
288  {
289  sizeofScalar_ = static_cast<unsigned char>(nbytes);
290  }
291 
292 
293  //- Check if the label byte-size associated with the stream
294  //- is the same as the given type
295  template<class T = label>
296  typename std::enable_if<std::is_integral<T>::value, bool>::type
297  checkLabelSize() const noexcept
298  {
299  return sizeofLabel_ == sizeof(T);
300  }
301 
302  //- Check if the scalar byte-size associated with the stream
303  //- is the same as the given type
304  template<class T = scalar>
305  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
307  {
308  return sizeofScalar_ == sizeof(T);
309  }
310 
311 
312  // Stream State Functions
313 
314  //- Const access to the current stream line number
315  label lineNumber() const noexcept
316  {
317  return lineNumber_;
318  }
319 
320  //- Non-const access to the current stream line number
321  label& lineNumber() noexcept
322  {
323  return lineNumber_;
324  }
326  //- Set the stream line number
327  // \return the previous value
328  label lineNumber(const label num) noexcept
329  {
330  label old(lineNumber_);
331  lineNumber_ = num;
332  return old;
333  }
334 
335  //- Return flags of stream
336  virtual ios_base::fmtflags flags() const = 0;
337 
338  //- Return the default precision
339  static unsigned int defaultPrecision() noexcept
340  {
341  return precision_;
342  }
343 
344  //- Reset the default precision
345  // \return the previous value
346  static unsigned int defaultPrecision(unsigned int prec) noexcept
347  {
348  unsigned int old(precision_);
349  precision_ = prec;
350  return old;
351  }
352 
353  //- Set stream state as reached 'eof'
354  void setEof() noexcept
355  {
356  ioState_ |= std::ios_base::eofbit;
357  }
358 
359  //- Set stream state as 'failed'
360  void setFail() noexcept
361  {
362  ioState_ |= std::ios_base::failbit;
363  }
364 
365  //- Set stream state to be 'bad'
366  void setBad()
367  {
368  ioState_ |= std::ios_base::badbit;
369  }
370 
371  //- Set flags of stream
372  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
373 
374  //- Set flags of stream
375  ios_base::fmtflags setf(const ios_base::fmtflags f)
376  {
377  return flags(flags() | f);
378  }
379 
380  //- Set flags of given field of stream
381  ios_base::fmtflags setf
382  (
383  const ios_base::fmtflags f,
384  const ios_base::fmtflags mask
385  )
386  {
387  return flags((flags() & ~mask) | (f & mask));
388  }
389 
390  //- Unset flags of stream
391  void unsetf(const ios_base::fmtflags f)
392  {
393  flags(flags() & ~f);
394  }
395 
396 
397  // Print
398 
399  //- Print stream description to Ostream
400  virtual void print(Ostream& os) const;
402  //- Print information about the stream state bits
403  void print(Ostream& os, const int streamState) const;
404 
405 
406  // Info
407 
408  //- Return info proxy.
409  // Used to print IOstream information to a stream
410  InfoProxy<IOstream> info() const
411  {
412  return *this;
413  }
414 };
415 
417 // Ostream Operator
418 
419 template<>
420 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
421 
422 
423 // --------------------------------------------------------------------
424 // ------ Manipulators (not taking arguments)
425 // --------------------------------------------------------------------
427 //- An IOstream manipulator
428 typedef IOstream& (*IOstreamManip)(IOstream&);
429 
430 //- operator<< handling for manipulators without arguments
432 {
433  return f(io);
434 }
435 
437 inline IOstream& dec(IOstream& io)
438 {
439  io.setf(std::ios_base::dec, std::ios_base::basefield);
440  return io;
441 }
442 
443 inline IOstream& hex(IOstream& io)
444 {
445  io.setf(std::ios_base::hex, std::ios_base::basefield);
446  return io;
447 }
448 
449 inline IOstream& oct(IOstream& io)
450 {
451  io.setf(std::ios_base::oct, std::ios_base::basefield);
452  return io;
453 }
454 
455 inline IOstream& fixed(IOstream& io)
456 {
457  io.setf(std::ios_base::fixed, std::ios_base::floatfield);
458  return io;
459 }
460 
461 inline IOstream& scientific(IOstream& io)
462 {
463  io.setf(std::ios_base::scientific, std::ios_base::floatfield);
464  return io;
465 }
466 
467 
468 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
469 
470 namespace Detail
471 {
472 
473 //- Termination for input looping (no-op)
474 template<class IS> inline void inputLoop(IS&) {}
475 
476 //- Termination for output looping (no-op)
477 template<class OS> inline void outputLoop(OS&) {}
478 
479 //- Input looping. Read into first parameter and recurse.
480 template<class IS, class Type, class... Args>
481 inline void inputLoop(IS& is, Type& arg1, Args&&... args)
482 {
483  is >> arg1;
484  Detail::inputLoop(is, std::forward<Args>(args)...);
485 }
486 
487 //- Output looping. Write first parameter and recurse.
488 template<class OS, class Type, class... Args>
489 inline void outputLoop(OS& os, const Type& arg1, Args&&... args)
490 {
491  os << arg1;
492  Detail::outputLoop(os, std::forward<Args>(args)...);
493 }
494 
495 } // End namespace Detail
496 
497 
498 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
499 
500 } // End namespace Foam
501 
502 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
503 
504 #endif
505 
506 // ************************************************************************* //
label lineNumber_
The file line.
Definition: IOstream.H:133
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:258
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:314
void setFail() noexcept
Set stream state as &#39;failed&#39;.
Definition: IOstream.H:444
A class for handling file names.
Definition: fileName.H:71
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:298
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:372
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:151
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:465
IOstream & hex(IOstream &io)
Definition: IOstream.H:548
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:159
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
streamAccess openClosed_
The stream open/closed state.
Definition: IOstream.H:118
streamAccess
Enumeration for stream open/closed state.
Definition: IOstream.H:86
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.H:108
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:485
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:416
A simple container for options an IOstream can normally have.
IOstream & fixed(IOstream &io)
Definition: IOstream.H:560
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:333
IOstream & oct(IOstream &io)
Definition: IOstream.H:554
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:452
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:752
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
IOstream(const IOstream &)=default
Copy construct.
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:341
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:581
unsigned char sizeofLabel_
The sizeof (label), possibly read from the header.
Definition: IOstream.H:123
bool fail() const noexcept
True if next operation will fail.
Definition: IOstream.H:290
The stream is open.
Definition: IOstream.H:89
unsigned char sizeofScalar_
The sizeof (scalar), possibly read from the header.
Definition: IOstream.H:128
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:55
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:383
virtual ~IOstream()=default
Destructor.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:76
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:75
static unsigned int precision_
Default precision.
Definition: IOstream.H:98
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:349
bool closed() const noexcept
True if stream is closed.
Definition: IOstream.H:266
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:274
streamFormat
Data format (ascii | binary)
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:143
IOstream & dec(IOstream &io)
Definition: IOstream.H:542
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:361
void setEof() noexcept
Set stream state as reached &#39;eof&#39;.
Definition: IOstream.H:436
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:167
Foam::argList args(argc, argv)
The stream is not open.
Definition: IOstream.H:88
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:586
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:325
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:282
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:531
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:511
System bool.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:566
Namespace for OpenFOAM.
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition: IOstream.H:113