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-2021 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 IOstream_H
46 #define 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::cin;
64 using std::cout;
65 using 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 
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_;
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 
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::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
301  checkLabelSize() const noexcept
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
310  checkScalarSize() const noexcept
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
414  InfoProxy<IOstream> info() const
415  {
416  return *this;
417  }
418 };
419 
420 
421 // Ostream Operator
422 
423 template<>
424 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
425 
426 
427 // --------------------------------------------------------------------
428 // ------ Manipulators (not taking arguments)
429 // --------------------------------------------------------------------
430 
431 //- An IOstream manipulator
432 typedef IOstream& (*IOstreamManip)(IOstream&);
433 
434 //- operator<< handling for manipulators without arguments
436 {
437  return f(io);
438 }
439 
440 
441 inline IOstream& dec(IOstream& io)
442 {
444  return io;
445 }
446 
447 inline IOstream& hex(IOstream& io)
448 {
450  return io;
451 }
452 
453 inline IOstream& oct(IOstream& io)
454 {
456  return io;
457 }
458 
459 inline IOstream& fixed(IOstream& io)
460 {
461  io.setf(ios_base::fixed, ios_base::floatfield);
462  return io;
463 }
464 
465 inline IOstream& scientific(IOstream& io)
466 {
467  io.setf(ios_base::scientific, ios_base::floatfield);
468  return io;
469 }
470 
471 
472 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
473 
474 } // End namespace Foam
475 
476 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
477 
478 #endif
479 
480 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
Foam::IOstream::sizeofLabel_
unsigned char sizeofLabel_
The sizeof (label), possibly read from the header.
Definition: IOstream.H:115
hex
const cellModel & hex
Definition: createBlockMesh.H:1
Foam::IOstream::sizeofScalar_
unsigned char sizeofScalar_
The sizeof (scalar), possibly read from the header.
Definition: IOstream.H:118
Foam::IOstream::IOstream
IOstream(const IOstream &)=default
Copy construct.
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:47
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::IOstream::setLabelByteSize
void setLabelByteSize(unsigned nbytes) noexcept
Set the sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:284
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
InfoProxy.H
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:431
Foam::IOstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:80
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
Foam::IOstream::setScalarByteSize
void setScalarByteSize(unsigned nbytes) noexcept
Set the sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:290
Foam::IOstream::lineNumber
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:318
Foam::oct
IOstream & oct(IOstream &io)
Definition: IOstream.H:452
Foam::IOstream::~IOstream
virtual ~IOstream()=default
Destructor.
Foam::IOstream::precision_
static unsigned int precision_
Default precision.
Definition: IOstream.H:98
Foam::IOstream::setOpened
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:129
Foam::IOstream::closed
bool closed() const noexcept
True if stream is closed.
Definition: IOstream.H:227
Foam::IOstream::setGood
void setGood() noexcept
Set stream state to be good.
Definition: IOstream.H:147
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::IOstream::labelByteSize
unsigned labelByteSize() const noexcept
The sizeof (label) in bytes associated with the stream.
Definition: IOstream.H:272
Foam::IOstream::setEof
void setEof() noexcept
Set stream state as reached 'eof'.
Definition: IOstream.H:357
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IOstream::checkLabelSize
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const noexcept
Definition: IOstream.H:300
Foam::IOstream::IOstream
IOstream(IOstreamOption streamOpt=IOstreamOption())
Definition: IOstream.H:168
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
Foam::IOstream::lineNumber_
label lineNumber_
The file line.
Definition: IOstream.H:121
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:440
Foam::IOstream::ioState_
std::ios_base::iostate ioState_
Mirror of internal stream io state.
Definition: IOstream.H:109
IOstreamOption.H
Foam::IOstream::eof
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::IOstream::opened
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:221
fileName.H
Foam::IOstream::info
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:413
Foam::IOstream::operator!
bool operator!() const noexcept
Return true if the stream has failed.
Definition: IOstream.H:263
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
Foam::IOstream::setFail
void setFail() noexcept
Set stream state as 'failed'.
Definition: IOstream.H:363
bool.H
System bool.
Foam::IOstream::checkScalarSize
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const noexcept
Definition: IOstream.H:309
Foam::IOstream::setClosed
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:135
Foam::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.H:458
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
scalar.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IOstream::relativeName
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
Foam::IOstream::bad
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::IOstream::openClosed_
streamAccess openClosed_
The stream open/closed state.
Definition: IOstream.H:112
Foam::IOstream::staticName_
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.H:106
Foam::IOstream::OPENED
The stream is open.
Definition: IOstream.H:91
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:446
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
Foam::IOstream::flags
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
Foam::IOstream::setState
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:141
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision() noexcept
Return the default precision.
Definition: IOstream.H:342
Foam::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:394
f
labelList f(nPoints)
Foam::IOstream::streamAccess
streamAccess
Enumeration for stream open/closed state.
Definition: IOstream.H:88
label.H
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::IOstream::CLOSED
The stream is not open.
Definition: IOstream.H:90
bool
bool
Definition: EEqn.H:20
Foam::IOstream::scalarByteSize
unsigned scalarByteSize() const noexcept
The sizeof (scalar) in bytes associated with the stream.
Definition: IOstream.H:278
Foam::IOstream::setBad
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
uLabel.H
Foam::IOstream::lineNumber
label & lineNumber() noexcept
Non-const access to the current stream line number.
Definition: IOstream.H:324
char.H
A character and a pointer to a character string.
Foam::IOstream::fail
bool fail() const noexcept
True if next operation will fail.
Definition: IOstream.H:245