IOstream.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __IOstream_hpp__
23 #define __IOstream_hpp__
24 
25 // based on OpenFOAM stream, with some modifications/simplifications
26 // to be tailored to our needs
27 
28 #include <iostream>
29 
30 #include "bTypesFunctions.hpp"
31 
32 using std::ios_base;
33 using std::istream;
34 using std::ostream;
35 
36 using std::cin;
37 using std::cout;
38 using std::cerr;
39 
40 
41 
42 namespace pFlow
43 {
44 
45 class IOstream
46 {
47 
48 public:
49 
50 
51  enum streamAccess : char
52  {
53  CLOSED = 0,
55  };
56 
57 
58  //- Default precision
59  static unsigned int precision_;
60 
61 protected:
62 
63  //- Name for any generic stream - normally treat as readonly
64  static word staticName_;
65 
67 
68  ios_base::iostate ioState_;
69 
70 
71  //- The file line
73 
74 
75  // Protected Member Functions
76 
77  //- Set stream opened
78  void setOpened()
79  {
81  }
82 
83  //- Set stream closed
84  void setClosed()
85  {
87  }
88 
89  //- Set stream state
90  void setState(ios_base::iostate state)
91  {
92  ioState_ = state;
93  }
94 
95  //- Set stream to be good
96  void setGood()
97  {
98  ioState_ = ios_base::iostate(0);
99  }
100 
101 
102 public:
103 
104  // Constructors
105  explicit IOstream():
107  ioState_(ios_base::iostate(0)),
108  lineNumber_(0)
109  {
110  setBad();
111  }
112 
113  IOstream(const IOstream&) = default;
114 
115  //- Destructor
116  virtual ~IOstream() = default;
117 
118 
120 
121  //- Return the name of the stream
122  virtual const word& name() const;
123 
124  //- Return non-const access to the name of the stream
125  virtual word& name();
126 
127  //- Check IOstream status for given operation.
128  // Print IOstream state or generate a FatalIOError
129  // when an error has occurred.
130  // The base implementation is a fatalCheck
131  virtual bool check(const char* operation) const;
132 
133  //- Check IOstream status for given operation.
134  // Generate a FatalIOError when an error has occurred.
135  bool fatalCheck(const char* operation) const;
136 
137  //- Return true if stream has been opened
138  bool opened() const
139  {
140  return openClosed_ == OPENED;
141  }
142 
143  //- Return true if stream is closed
144  bool closed() const
145  {
146  return openClosed_ == CLOSED;
147  }
148 
149  //- Return true if next operation might succeed
150  bool good() const
151  {
152  return ioState_ == 0;
153  }
154 
155  //- Return true if end of input seen
156  bool eof() const
157  {
158  return ioState_ & ios_base::eofbit;
159  }
160 
161  //- Return true if next operation will fail
162  bool fail() const
163  {
164  return ioState_ & (ios_base::badbit | ios_base::failbit);
165  }
166 
167  //- Return true if stream is corrupted
168  bool bad() const
169  {
170  return ioState_ & ios_base::badbit;
171  }
172 
173  //- Return true if the stream has not failed
174  explicit operator bool() const
175  {
176  return !fail();
177  }
178 
179  //- Return true if the stream has failed
180  bool operator!() const
181  {
182  return fail();
183  }
184 
185 
186  //- Const access to the current stream line number
188  {
189  return lineNumber_;
190  }
191 
192  //- Non-const access to the current stream line number
194  {
195  return lineNumber_;
196  }
197 
198  //- Set the stream line number
199  // \return the previous value
200  int32 lineNumber(const int32 num)
201  {
202  const int32 old(lineNumber_);
203  lineNumber_ = num;
204  return old;
205  }
206 
207  //- Return flags of stream
208  virtual ios_base::fmtflags flags() const = 0;
209 
210  //- Return the default precision
211  static unsigned int defaultPrecision()
212  {
213  return precision_;
214  }
215 
216  //- Reset the default precision
217  // \return the previous value
218  static unsigned int defaultPrecision(unsigned int prec)
219  {
220  unsigned int old(precision_);
221  precision_ = prec;
222  return old;
223  }
224 
225  //- Set stream to have reached eof
226  void setEof()
227  {
228  ioState_ |= ios_base::eofbit;
229  }
230 
231  //- Set stream to have failed
232  void setFail()
233  {
234  ioState_ |= ios_base::failbit;
235  }
236 
237  //- Set stream to be bad
238  void setBad()
239  {
240  ioState_ |= ios_base::badbit;
241  }
242 
243  //- Set flags of stream
244  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
245 
246  //- Set flags of stream
247  ios_base::fmtflags setf(const ios_base::fmtflags f)
248  {
249  return flags(flags() | f);
250  }
251 
252  //- Set flags of given field of stream
253  ios_base::fmtflags setf
254  (
255  const ios_base::fmtflags f,
256  const ios_base::fmtflags mask
257  )
258  {
259  return flags((flags() & ~mask) | (f & mask));
260  }
261 
262  //- Unset flags of stream
263  void unsetf(const ios_base::fmtflags f)
264  {
265  flags(flags() & ~f);
266  }
267 
268 
269 }; // end of IOstream
270 
271 
272 //- An IOstream manipulator
273 typedef IOstream& (*IOstreamManip)(IOstream&);
274 
275 inline IOstream& dec(IOstream& io)
276 {
278  return io;
279 }
280 
281 inline IOstream& hex(IOstream& io)
282 {
284  return io;
285 }
286 
287 inline IOstream& oct(IOstream& io)
288 {
290  return io;
291 }
292 
293 inline IOstream& fixed(IOstream& io)
294 {
295  io.setf(ios_base::fixed, ios_base::floatfield);
296  return io;
297 }
298 
300 {
301  io.setf(ios_base::scientific, ios_base::floatfield);
302  return io;
303 }
304 
305 
306 
307 } // pFlow
308 
309 #endif // __IOstream__hpp__
pFlow::IOstream::eof
bool eof() const
Definition: IOstream.hpp:156
pFlow::IOstream::setGood
void setGood()
Definition: IOstream.hpp:96
pFlow::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Definition: IOstream.hpp:211
pFlow::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.hpp:293
pFlow::dec
IOstream & dec(IOstream &io)
Definition: IOstream.hpp:275
pFlow::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.hpp:299
pFlow::IOstream::lineNumber_
int32 lineNumber_
Definition: IOstream.hpp:72
pFlow::IOstream::operator!
bool operator!() const
Definition: IOstream.hpp:180
pFlow::hex
IOstream & hex(IOstream &io)
Definition: IOstream.hpp:281
pFlow::IOstream::closed
bool closed() const
Definition: IOstream.hpp:144
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::IOstream::staticName_
static word staticName_
Definition: IOstream.hpp:64
pFlow::IOstream::precision_
static unsigned int precision_
Definition: IOstream.hpp:59
pFlow
Definition: demComponent.hpp:28
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::IOstream::defaultPrecision
static unsigned int defaultPrecision(unsigned int prec)
Definition: IOstream.hpp:218
pFlow::IOstream::flags
virtual ios_base::fmtflags flags() const =0
pFlow::IOstream::bad
bool bad() const
Definition: IOstream.hpp:168
pFlow::IOstream::openClosed_
streamAccess openClosed_
Definition: IOstream.hpp:66
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::IOstream::setBad
void setBad()
Definition: IOstream.hpp:238
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::IOstream::IOstream
IOstream()
Definition: IOstream.hpp:105
pFlow::IOstream::fail
bool fail() const
Definition: IOstream.hpp:162
pFlow::IOstream::setClosed
void setClosed()
Definition: IOstream.hpp:84
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::IOstream::streamAccess
streamAccess
Definition: IOstream.hpp:51
pFlow::IOstream::good
bool good() const
Definition: IOstream.hpp:150
pFlow::IOstream::lineNumber
int32 & lineNumber()
Definition: IOstream.hpp:193
pFlow::IOstream::~IOstream
virtual ~IOstream()=default
pFlow::IOstream::opened
bool opened() const
Definition: IOstream.hpp:138
pFlow::IOstream::setState
void setState(ios_base::iostate state)
Definition: IOstream.hpp:90
pFlow::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Definition: IOstream.hpp:247
pFlow::IOstream::setOpened
void setOpened()
Definition: IOstream.hpp:78
pFlow::IOstream::setFail
void setFail()
Definition: IOstream.hpp:232
pFlow::oct
IOstream & oct(IOstream &io)
Definition: IOstream.hpp:287
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::IOstream::setEof
void setEof()
Definition: IOstream.hpp:226
bTypesFunctions.hpp
pFlow::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Definition: IOstream.hpp:263
pFlow::IOstream::ioState_
ios_base::iostate ioState_
Definition: IOstream.hpp:68
pFlow::IOstream
Definition: IOstream.hpp:45
pFlow::IOstream::CLOSED
@ CLOSED
stream is not open
Definition: IOstream.hpp:53
pFlow::IOstream::lineNumber
int32 lineNumber(const int32 num)
Definition: IOstream.hpp:200
pFlow::IOstream::OPENED
@ OPENED
stream is open
Definition: IOstream.hpp:54