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-2019 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
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  // Constructors
151 
152  //- Construct with specified stream option
153  explicit IOstream(const IOstreamOption option)
154  :
155  IOstreamOption(option),
157  ioState_(ios_base::iostate(0)),
158  labelByteSize_(sizeof(label)),
159  scalarByteSize_(sizeof(scalar)),
160  lineNumber_(0)
161  {
162  setBad();
163  }
164 
165  //- Construct with format, version
167  (
171  )
172  :
174  {}
175 
176 
177  //- Destructor
178  virtual ~IOstream() = default;
179 
180 
181  // Member Functions
182 
183  // Access
184 
185  //- Return the name of the stream
186  // Useful for Fstream to return the filename
187  virtual const fileName& name() const;
188 
189  //- Return non-const access to the name of the stream
190  // Useful to alter the stream name
191  virtual fileName& name();
192 
193 
194  // Check
195 
196  //- Check IOstream status for given operation.
197  // Print IOstream state if error has occurred
198  virtual bool check(const char* operation) const;
199 
200  //- Check IOstream status for given operation.
201  // Print IOstream state if error has occurred and exit
202  void fatalCheck(const char* operation) const;
203 
204  //- Return true if stream has been opened
205  bool opened() const
206  {
207  return openClosed_ == OPENED;
208  }
209 
210  //- Return true if stream is closed
211  bool closed() const
212  {
213  return openClosed_ == CLOSED;
214  }
215 
216  //- Return true if next operation might succeed
217  bool good() const
218  {
219  return ioState_ == 0;
220  }
221 
222  //- Return true if end of input seen
223  bool eof() const
224  {
225  return ioState_ & ios_base::eofbit;
226  }
227 
228  //- Return true if next operation will fail
229  bool fail() const
230  {
231  return ioState_ & (ios_base::badbit | ios_base::failbit);
232  }
233 
234  //- Return true if stream is corrupted
235  bool bad() const
236  {
237  return ioState_ & ios_base::badbit;
238  }
239 
240  //- Return true if the stream has not failed
241  explicit operator bool() const
242  {
243  return !fail();
244  }
245 
246  //- Return true if the stream has failed
247  bool operator!() const
248  {
249  return fail();
250  }
251 
252 
253  // Element sizes (precision)
254 
255  //- The label byte-size associated with the stream
256  unsigned labelByteSize() const
257  {
258  return labelByteSize_;
259  }
260 
261  //- The scalar byte-size associated with the stream
262  unsigned scalarByteSize() const
263  {
264  return scalarByteSize_;
265  }
266 
267  //- Set the label byte-size associated with the stream
268  void setLabelByteSize(unsigned nbytes)
269  {
270  labelByteSize_ = nbytes;
271  }
272 
273  //- Set the scalar byte-size associated with the stream
274  void setScalarByteSize(unsigned nbytes)
275  {
276  scalarByteSize_ = nbytes;
277  }
278 
279 
280  //- Check if the label byte-size associated with the stream
281  //- is the same as the given type
282  template<class T = label>
283  typename std::enable_if<std::is_integral<T>::value, bool>::type
284  checkLabelSize() const
285  {
286  return labelByteSize_ == sizeof(T);
287  }
288 
289  //- Check if the scalar byte-size associated with the stream
290  //- is the same as the given type
291  template<class T = scalar>
292  typename std::enable_if<std::is_floating_point<T>::value, bool>::type
293  checkScalarSize() const
294  {
295  return scalarByteSize_ == sizeof(T);
296  }
297 
298 
299  // Stream State Functions
300 
301  //- Const access to the current stream line number
302  label lineNumber() const
303  {
304  return lineNumber_;
305  }
306 
307  //- Non-const access to the current stream line number
308  label& lineNumber()
309  {
310  return lineNumber_;
311  }
312 
313  //- Set the stream line number
314  // \return the previous value
315  label lineNumber(const label num)
316  {
317  const label old(lineNumber_);
318  lineNumber_ = num;
319  return old;
320  }
321 
322  //- Return flags of stream
323  virtual ios_base::fmtflags flags() const = 0;
324 
325  //- Return the default precision
326  static unsigned int defaultPrecision()
327  {
328  return precision_;
329  }
330 
331  //- Reset the default precision
332  // \return the previous value
333  static unsigned int defaultPrecision(unsigned int prec)
334  {
335  unsigned int old(precision_);
336  precision_ = prec;
337  return old;
338  }
339 
340  //- Set stream to have reached eof
341  void setEof()
342  {
343  ioState_ |= ios_base::eofbit;
344  }
345 
346  //- Set stream to have failed
347  void setFail()
348  {
349  ioState_ |= ios_base::failbit;
350  }
351 
352  //- Set stream to be bad
353  void setBad()
354  {
355  ioState_ |= ios_base::badbit;
356  }
357 
358  //- Set flags of stream
359  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
360 
361  //- Set flags of stream
362  ios_base::fmtflags setf(const ios_base::fmtflags f)
363  {
364  return flags(flags() | f);
365  }
366 
367  //- Set flags of given field of stream
368  ios_base::fmtflags setf
369  (
370  const ios_base::fmtflags f,
371  const ios_base::fmtflags mask
372  )
373  {
374  return flags((flags() & ~mask) | (f & mask));
375  }
376 
377  //- Unset flags of stream
378  void unsetf(const ios_base::fmtflags f)
379  {
380  flags(flags() & ~f);
381  }
382 
383 
384  // Print
385 
386  //- Print description of IOstream to Ostream
387  virtual void print(Ostream& os) const;
388 
389  //- Print information about the stream state bits
390  void print(Ostream& os, const int streamState) const;
391 
392 
393  // Info
394 
395  //- Return info proxy.
396  // Used to print IOstream information to a stream
397  InfoProxy<IOstream> info() const
398  {
399  return *this;
400  }
401 };
402 
403 
404 // Ostream Operator
405 
406 template<>
407 Ostream& operator<<(Ostream& os, const InfoProxy<IOstream>& ip);
408 
409 
410 // --------------------------------------------------------------------
411 // ------ Manipulators (not taking arguments)
412 // --------------------------------------------------------------------
413 
414 //- An IOstream manipulator
415 typedef IOstream& (*IOstreamManip)(IOstream&);
416 
417 //- operator<< handling for manipulators without arguments
419 {
420  return f(io);
421 }
422 
423 
424 inline IOstream& dec(IOstream& io)
425 {
427  return io;
428 }
429 
430 inline IOstream& hex(IOstream& io)
431 {
433  return io;
434 }
435 
436 inline IOstream& oct(IOstream& io)
437 {
439  return io;
440 }
441 
442 inline IOstream& fixed(IOstream& io)
443 {
444  io.setf(ios_base::fixed, ios_base::floatfield);
445  return io;
446 }
447 
448 inline IOstream& scientific(IOstream& io)
449 {
450  io.setf(ios_base::scientific, ios_base::floatfield);
451  return io;
452 }
453 
454 
455 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
456 
457 } // End namespace Foam
458 
459 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
460 
461 #endif
462 
463 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:73
Foam::IOstream::labelByteSize
unsigned labelByteSize() const
The label byte-size associated with the stream.
Definition: IOstream.H:255
hex
const cellModel & hex
Definition: createBlockMesh.H:1
Foam::IOstream::fatalCheck
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::IOstream::opened
bool opened() const
Return true if stream has been opened.
Definition: IOstream.H:204
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::IOstream::fail
bool fail() const
Return true if next operation will fail.
Definition: IOstream.H:228
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:222
InfoProxy.H
Foam::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.H:414
Foam::IOstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: IOstream.C:75
Foam::IOstream::setEof
void setEof()
Set stream to have reached eof.
Definition: IOstream.H:340
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::oct
IOstream & oct(IOstream &io)
Definition: IOstream.H:435
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::IOstream::setState
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:135
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::IOstream::lineNumber_
label lineNumber_
The file line.
Definition: IOstream.H:115
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:423
Foam::IOstream::operator!
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.H:246
Foam::IOstream::checkLabelSize
std::enable_if< std::is_integral< T >::value, bool >::type checkLabelSize() const
Definition: IOstream.H:283
Foam::IOstream::IOstream
IOstream(const IOstreamOption option)
Construct with specified stream option.
Definition: IOstream.H:152
IOstreamOption.H
Foam::IOstream::setLabelByteSize
void setLabelByteSize(unsigned nbytes)
Set the label byte-size associated with the stream.
Definition: IOstream.H:267
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:57
fileName.H
Foam::IOstream::info
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:396
Foam::IOstream::lineNumber
label & lineNumber()
Non-const access to the current stream line number.
Definition: IOstream.H:307
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:210
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::IOstream::setClosed
void setClosed()
Set stream closed.
Definition: IOstream.H:129
Foam::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.H:441
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
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:273
scalar.H
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:234
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:447
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:429
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:361
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:377
Foam::IOstreamOption::IOstreamOption
IOstreamOption() noexcept
Construct null. (default: ASCII, uncompressed, currentVersion)
Definition: IOstreamOption.H:217
Foam::IOstream::checkScalarSize
std::enable_if< std::is_floating_point< T >::value, bool >::type checkScalarSize() const
Definition: IOstream.H:292
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:325
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:301
Foam::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.H:352
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::IOstream::setFail
void setFail()
Set stream to have failed.
Definition: IOstream.H:346
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:216
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:297
char.H
A character and a pointer to a character string.
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::IOstream::scalarByteSize
unsigned scalarByteSize() const
The scalar byte-size associated with the stream.
Definition: IOstream.H:261