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-2020 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
68 
69 namespace Foam
70 {
71 
72 /*---------------------------------------------------------------------------*\
73  Class IOstream Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 class IOstream
77 :
78  public IOstreamOption
79 {
80 public:
81 
82  // Public Data Types
83 
84  //- Enumeration for stream open/closed state
85  enum streamAccess : char
86  {
87  CLOSED = 0,
88  OPENED
89  };
90 
91 
92  // Public Static Data
93 
94  //- Default precision
95  static unsigned int precision_;
96 
97 
98 protected:
99 
100  // Protected Data
101 
102  //- Name for any generic stream - normally treat as readonly
103  static fileName staticName_;
104 
106 
107  ios_base::iostate ioState_;
108 
109  //- The label byte-size (could also be stored as byte)
110  unsigned short labelByteSize_;
111 
112  //- The scalar byte-size (could also be stored as byte)
113  unsigned short scalarByteSize_;
114 
115  //- The file line
116  label lineNumber_;
117 
118 
119  // Protected Member Functions
120 
121  // Access
122 
123  //- Set stream opened
124  void setOpened()
125  {
127  }
128 
129  //- Set stream closed
130  void setClosed()
131  {
133  }
134 
135  //- Set stream state
136  void setState(ios_base::iostate state)
137  {
138  ioState_ = state;
139  }
140 
141  //- Set stream to be good
142  void setGood()
143  {
144  ioState_ = ios_base::iostate(0);
145  }
146 
147 
148 public:
149 
150  // Generated Methods
151 
152  //- Copy construct
153  IOstream(const IOstream&) = default;
154 
155  //- Destructor
156  virtual ~IOstream() = default;
157 
158 
159  // Constructors
160 
161  //- Default construct (ASCII, uncompressed),
162  //- construct with specified stream option
163  explicit IOstream(IOstreamOption streamOpt = IOstreamOption())
164  :
165  IOstreamOption(streamOpt),
167  ioState_(ios_base::iostate(0)),
168  labelByteSize_(sizeof(label)),
169  scalarByteSize_(sizeof(scalar)),
170  lineNumber_(0)
171  {
172  setBad();
173  }
174 
175  //- Construct with format, version (compression)
177  (
178  streamFormat fmt,
179  versionNumber ver,
180  compressionType comp = compressionType::UNCOMPRESSED
181  )
182  :
183  IOstream(IOstreamOption(fmt, comp, ver))
184  {}
185 
186 
187  // Member Functions
188 
189  // Access
190 
191  //- Return the name of the stream
192  // Useful for Fstream to return the filename
193  virtual const fileName& name() const;
194 
195  //- Return non-const access to the name of the stream
196  // Useful to alter the stream name
197  virtual fileName& name();
198 
199 
200  // Check
201 
202  //- Check IOstream status for given operation.
203  // Print IOstream state or generate a FatalIOError
204  // when an error has occurred.
205  // The base implementation is a fatalCheck
206  virtual bool check(const char* operation) const;
207 
208  //- Check IOstream status for given operation.
209  // Generate a FatalIOError when an error has occurred.
210  bool fatalCheck(const char* operation) const;
211 
212  //- Return true if stream has been opened
213  bool opened() const
214  {
215  return openClosed_ == OPENED;
216  }
217 
218  //- Return true if stream is closed
219  bool closed() const
220  {
221  return openClosed_ == CLOSED;
222  }
223 
224  //- Return true if next operation might succeed
225  bool good() const
226  {
227  return ioState_ == 0;
228  }
229 
230  //- Return true if end of input seen
231  bool eof() const
232  {
233  return ioState_ & ios_base::eofbit;
234  }
235 
236  //- Return true if next operation will fail
237  bool fail() const
238  {
239  return ioState_ & (ios_base::badbit | ios_base::failbit);
240  }
241 
242  //- Return true if stream is corrupted
243  bool bad() const
244  {
245  return ioState_ & ios_base::badbit;
246  }
247 
248  //- Return true if the stream has not failed
249  explicit operator bool() const
250  {
251  return !fail();
252  }
253 
254  //- Return true if the stream has failed
255  bool operator!() const
256  {
257  return fail();
258  }
259 
260 
261  // Element sizes (precision)
262 
263  //- The label byte-size associated with the stream
264  unsigned labelByteSize() const
265  {
266  return labelByteSize_;
267  }
268 
269  //- The scalar byte-size associated with the stream
270  unsigned scalarByteSize() const
271  {
272  return scalarByteSize_;
273  }
274 
275  //- Set the label byte-size associated with the stream
276  void setLabelByteSize(unsigned nbytes)
277  {
278  labelByteSize_ = nbytes;
279  }
280 
281  //- Set the scalar byte-size associated with the stream
282  void setScalarByteSize(unsigned nbytes)
283  {
284  scalarByteSize_ = nbytes;
285  }
286 
287 
288  //- Check if the label byte-size associated with the stream
289  //- is the same as the given type
290  template<class T = label>
291  typename std::enable_if<std::is_integral<T>::value, bool>::type
292  checkLabelSize() const
293  {
294  return labelByteSize_ == sizeof(T);
295  }
296 
297  //- Check if the scalar byte-size associated with the stream
298  //- is the same as the given type
299  template<class T = scalar>
300  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
301  checkScalarSize() const
302  {
303  return scalarByteSize_ == sizeof(T);
304  }
305 
306 
307  // Stream State Functions
308 
309  //- Const access to the current stream line number
310  label lineNumber() const
311  {
312  return lineNumber_;
313  }
314 
315  //- Non-const access to the current stream line number
316  label& lineNumber()
317  {
318  return lineNumber_;
319  }
320 
321  //- Set the stream line number
322  // \return the previous value
323  label lineNumber(const label num)
324  {
325  const label old(lineNumber_);
326  lineNumber_ = num;
327  return old;
328  }
329 
330  //- Return flags of stream
331  virtual ios_base::fmtflags flags() const = 0;
332 
333  //- Return the default precision
334  static unsigned int defaultPrecision()
335  {
336  return precision_;
337  }
338 
339  //- Reset the default precision
340  // \return the previous value
341  static unsigned int defaultPrecision(unsigned int prec)
342  {
343  unsigned int old(precision_);
344  precision_ = prec;
345  return old;
346  }
347 
348  //- Set stream to have reached eof
349  void setEof()
350  {
351  ioState_ |= ios_base::eofbit;
352  }
353 
354  //- Set stream to have failed
355  void setFail()
356  {
357  ioState_ |= ios_base::failbit;
358  }
359 
360  //- Set stream to be bad
361  void setBad()
362  {
363  ioState_ |= ios_base::badbit;
364  }
365 
366  //- Set flags of stream
367  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
368 
369  //- Set flags of stream
370  ios_base::fmtflags setf(const ios_base::fmtflags f)
371  {
372  return flags(flags() | f);
373  }
374 
375  //- Set flags of given field of stream
376  ios_base::fmtflags setf
377  (
378  const ios_base::fmtflags f,
379  const ios_base::fmtflags mask
380  )
381  {
382  return flags((flags() & ~mask) | (f & mask));
383  }
384 
385  //- Unset flags of stream
386  void unsetf(const ios_base::fmtflags f)
387  {
388  flags(flags() & ~f);
389  }
390 
391 
392  // Print
393 
394  //- Print stream description to Ostream
395  virtual void print(Ostream& os) const;
396 
397  //- Print information about the stream state bits
398  void print(Ostream& os, const int streamState) const;
399 
400 
401  // Info
402 
403  //- Return info proxy.
404  // Used to print IOstream information to a stream
405  InfoProxy<IOstream> info() const
406  {
407  return *this;
408  }
409 };
410 
411 
412 // Ostream Operator
413 
414 template<>
415 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
416 
417 
418 // --------------------------------------------------------------------
419 // ------ Manipulators (not taking arguments)
420 // --------------------------------------------------------------------
421 
422 //- An IOstream manipulator
423 typedef IOstream& (*IOstreamManip)(IOstream&);
424 
425 //- operator<< handling for manipulators without arguments
427 {
428  return f(io);
429 }
430 
431 
432 inline IOstream& dec(IOstream& io)
433 {
435  return io;
436 }
437 
438 inline IOstream& hex(IOstream& io)
439 {
441  return io;
442 }
443 
444 inline IOstream& oct(IOstream& io)
445 {
447  return io;
448 }
449 
450 inline IOstream& fixed(IOstream& io)
451 {
452  io.setf(ios_base::fixed, ios_base::floatfield);
453  return io;
454 }
455 
456 inline IOstream& scientific(IOstream& io)
457 {
458  io.setf(ios_base::scientific, ios_base::floatfield);
459  return io;
460 }
461 
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 } // End namespace Foam
466 
467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
468 
469 #endif
470 
471 // ************************************************************************* //
Foam::IOstream::labelByteSize
unsigned labelByteSize() const
The label byte-size associated with the stream.
Definition: IOstream.H:263
hex
const cellModel & hex
Definition: createBlockMesh.H:1
Foam::IOstream::IOstream
IOstream(const IOstream &)=default
Copy construct.
Foam::IOstream::opened
bool opened() const
Return true if stream has been opened.
Definition: IOstream.H:212
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:69
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:196
Foam::IOstream::fail
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:236
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:230
InfoProxy.H
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:422
Foam::IOstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IOstream.C:72
Foam::IOstream::setEof
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:348
Foam::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:57
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::oct
IOstream & oct(IOstream &io)
Definition: IOstream.H:443
Foam::IOstream::~IOstream
virtual ~IOstream()=default
Destructor.
Foam::IOstream::precision_
static unsigned int precision_
Default precision.
Definition: IOstream.H:94
Foam::IOstream::labelByteSize_
unsigned short labelByteSize_
The label byte-size (could also be stored as byte)
Definition: IOstream.H:109
Foam::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.H:123
Foam::IOstream::ioState_
ios_base::iostate ioState_
Definition: IOstream.H:106
Foam::IOstream::scalarByteSize_
unsigned short scalarByteSize_
The scalar byte-size (could also be stored as byte)
Definition: IOstream.H:112
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IOstream::setState
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:135
Foam::IOstream::IOstream
IOstream(IOstreamOption streamOpt=IOstreamOption())
Definition: IOstream.H:162
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:115
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:431
Foam::IOstream::operator!
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.H:254
Foam::IOstream::checkLabelSize
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const
Definition: IOstream.H:291
IOstreamOption.H
Foam::IOstream::setLabelByteSize
void setLabelByteSize(unsigned nbytes)
Set the label byte-size associated with the stream.
Definition: IOstream.H:275
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
fileName.H
Foam::IOstream::info
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:404
Foam::IOstream::lineNumber
label & lineNumber()
Non-const access to the current stream line number.
Definition: IOstream.H:315
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
bool.H
System bool.
Foam::IOstream::closed
bool closed() const
Return true if stream is closed.
Definition: IOstream.H:218
Foam::IOstream::setClosed
void setClosed()
Set stream closed.
Definition: IOstream.H:129
Foam::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.H:449
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:51
Foam::IOstream::setScalarByteSize
void setScalarByteSize(unsigned nbytes)
Set the scalar byte-size associated with the stream.
Definition: IOstream.H:281
scalar.H
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:242
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:455
Foam::IOstream::openClosed_
streamAccess openClosed_
Definition: IOstream.H:104
Foam::IOstream::staticName_
static fileName staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.H:102
Foam::IOstream::OPENED
stream is open
Definition: IOstream.H:87
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:437
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:369
Foam::IOstream::flags
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
Foam::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:385
Foam::IOstream::checkScalarSize
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const
Definition: IOstream.H:300
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:333
Foam::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.H:141
f
labelList f(nPoints)
Foam::IOstream::streamAccess
streamAccess
Enumeration for stream open/closed state.
Definition: IOstream.H:84
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
stream not open
Definition: IOstream.H:86
bool
bool
Definition: EEqn.H:20
Foam::IOstream::lineNumber
label lineNumber() const
Const access to the current stream line number.
Definition: IOstream.H:309
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:360
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::IOstream::setFail
void setFail()
Set stream to have failed.
Definition: IOstream.H:354
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::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
char.H
A character and a pointer to a character string.
Foam::IOstream::scalarByteSize
unsigned scalarByteSize() const
The scalar byte-size associated with the stream.
Definition: IOstream.H:269