www.cemf.ir
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 #ifndef __IOstream_hpp__
21 #define __IOstream_hpp__
22 
23 
24 #include <iostream>
25 
26 #include "bTypesFunctions.hpp"
27 
28 using std::ios_base;
29 using std::istream;
30 using std::ostream;
31 
32 using std::cin;
33 using std::cout;
34 using std::cerr;
35 
36 
37 
38 namespace pFlow
39 {
40 
47 class IOstream
48 {
49 
50 public:
51 
52 
53  enum streamAccess : char
54  {
55  CLOSED = 0,
57  };
58 
59  enum writeFormat: char
60  {
61  ASCII = 0,
63  };
64 
66  static unsigned int precision_;
67 
68 protected:
69 
71  static word staticName_;
72 
75 
78 
80  ios_base::iostate ioState_;
81 
82 
85 
86 
87  //- Protected Member Functions
88 
90  void setOpened()
91  {
93  }
94 
96  void setClosed()
97  {
99  }
100 
102  void setState(ios_base::iostate state)
103  {
104  ioState_ = state;
105  }
106 
108  {
109  writeFormat_ = wF;
110  }
111 
113  void setGood()
114  {
115  ioState_ = ios_base::iostate(0);
116  }
117 
118 public:
119 
121 
123  explicit IOstream():
126  ioState_(ios_base::iostate(0)),
127  lineNumber_(0)
128  {
129  setBad();
130  }
131 
133  explicit IOstream(writeFormat wF):
135  writeFormat_(wF),
136  ioState_(ios_base::iostate(0)),
137  lineNumber_(0)
138  {
139  setBad();
140  }
141 
143  IOstream(const IOstream&) = default;
144 
146  virtual ~IOstream() = default;
147 
148 
150 
152  virtual const word& name() const;
153 
155  virtual word& name();
156 
161  virtual bool check(const char* operation) const;
162 
165  bool fatalCheck(const char* operation) const;
166 
168  bool opened() const
169  {
170  return openClosed_ == OPENED;
171  }
172 
174  bool closed() const
175  {
176  return openClosed_ == CLOSED;
177  }
178 
180  bool isBinary()const
181  {
182  return writeFormat_ == BINARY;
183  }
184 
186  bool good() const
187  {
188  return ioState_ == 0;
189  }
190 
192  bool eof() const
193  {
194  return ioState_ & ios_base::eofbit;
195  }
196 
198  bool fail() const
199  {
200  return ioState_ & (ios_base::badbit | ios_base::failbit);
201  }
202 
204  bool bad() const
205  {
206  return ioState_ & ios_base::badbit;
207  }
208 
210  explicit operator bool() const
211  {
212  return !fail();
213  }
214 
216  bool operator!() const
217  {
218  return fail();
219  }
220 
221 
224  {
225  return lineNumber_;
226  }
227 
230  {
231  return lineNumber_;
232  }
233 
236  int32 lineNumber(const int32 num)
237  {
238  const int32 old(lineNumber_);
239  lineNumber_ = num;
240  return old;
241  }
242 
244  virtual ios_base::fmtflags flags() const = 0;
245 
247  static unsigned int defaultPrecision()
248  {
249  return precision_;
250  }
251 
254  static unsigned int defaultPrecision(unsigned int prec)
255  {
256  unsigned int old(precision_);
257  precision_ = prec;
258  return old;
259  }
260 
262  void setEof()
263  {
264  ioState_ |= ios_base::eofbit;
265  }
266 
268  void setFail()
269  {
270  ioState_ |= ios_base::failbit;
271  }
272 
274  void setBad()
275  {
276  ioState_ |= ios_base::badbit;
277  }
278 
280  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
281 
283  ios_base::fmtflags setf(const ios_base::fmtflags f)
284  {
285  return flags(flags() | f);
286  }
287 
289  ios_base::fmtflags setf
290  (
291  const ios_base::fmtflags f,
292  const ios_base::fmtflags mask
293  )
294  {
295  return flags((flags() & ~mask) | (f & mask));
296  }
297 
299  void unsetf(const ios_base::fmtflags f)
300  {
301  flags(flags() & ~f);
302  }
303 
304 }; // end of IOstream
305 
306 
308 typedef IOstream& (*IOstreamManip)(IOstream&);
309 
310 
311 inline IOstream& dec(IOstream& io)
312 {
314  return io;
315 }
316 
317 inline IOstream& hex(IOstream& io)
318 {
320  return io;
321 }
322 
323 inline IOstream& oct(IOstream& io)
324 {
326  return io;
327 }
328 
329 inline IOstream& fixed(IOstream& io)
330 {
331  io.setf(ios_base::fixed, ios_base::floatfield);
332  return io;
333 }
334 
336 {
337  io.setf(ios_base::scientific, ios_base::floatfield);
338  return io;
339 }
340 
341 
342 } // pFlow
343 
344 #endif // __IOstream__hpp__
pFlow::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.hpp:192
pFlow::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.hpp:113
pFlow::IOstream::setWriteFormat
void setWriteFormat(writeFormat wF)
Definition: IOstream.hpp:107
pFlow::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.hpp:247
pFlow::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.hpp:329
pFlow::dec
IOstream & dec(IOstream &io)
Definition: IOstream.hpp:311
pFlow::IOstream::writeFormat_
writeFormat writeFormat_
write format
Definition: IOstream.hpp:77
pFlow::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.hpp:335
pFlow::IOstream::IOstream
IOstream(writeFormat wF)
Construct and set write format.
Definition: IOstream.hpp:133
pFlow::IOstream::lineNumber_
int32 lineNumber_
The file line.
Definition: IOstream.hpp:84
pFlow::IOstream::operator!
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.hpp:216
pFlow::hex
IOstream & hex(IOstream &io)
Definition: IOstream.hpp:317
pFlow::IOstream::ASCII
@ ASCII
Definition: IOstream.hpp:61
pFlow::IOstream::closed
bool closed() const
Return true if stream is closed.
Definition: IOstream.hpp:174
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::IOstream::staticName_
static word staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.hpp:71
pFlow::IOstream::precision_
static unsigned int precision_
Default precision, only works for ASCII.
Definition: IOstream.hpp:66
pFlow
Definition: demGeometry.hpp:27
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::IOstream::defaultPrecision
static unsigned int defaultPrecision(unsigned int prec)
Reset the default precision return the previous value.
Definition: IOstream.hpp:254
pFlow::IOstream::flags
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
pFlow::IOstream::BINARY
@ BINARY
Definition: IOstream.hpp:62
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:204
pFlow::IOstream::openClosed_
streamAccess openClosed_
Is stream open or closed.
Definition: IOstream.hpp:74
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.hpp:274
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::IOstream::IOstream
IOstream()
Default.
Definition: IOstream.hpp:123
pFlow::IOstream::fail
bool fail() const
Return true if next operation will fail.
Definition: IOstream.hpp:198
pFlow::IOstream::setClosed
void setClosed()
Set stream closed.
Definition: IOstream.hpp:96
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::IOstream::streamAccess
streamAccess
Definition: IOstream.hpp:53
pFlow::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.hpp:186
pFlow::IOstream::lineNumber
int32 & lineNumber()
Non-const access to the current stream line number.
Definition: IOstream.hpp:229
pFlow::IOstream::~IOstream
virtual ~IOstream()=default
Destructor.
pFlow::IOstream::isBinary
bool isBinary() const
Return true if stream format is binray.
Definition: IOstream.hpp:180
pFlow::IOstream::opened
bool opened() const
Return true if stream has been opened.
Definition: IOstream.hpp:168
pFlow::IOstream::setState
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.hpp:102
pFlow::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.hpp:283
pFlow::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.hpp:90
pFlow::IOstream::setFail
void setFail()
Set stream to have failed.
Definition: IOstream.hpp:268
pFlow::oct
IOstream & oct(IOstream &io)
Definition: IOstream.hpp:323
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:59
pFlow::IOstream::setEof
void setEof()
Set stream to have reached eof.
Definition: IOstream.hpp:262
bTypesFunctions.hpp
pFlow::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.hpp:299
pFlow::IOstream::ioState_
ios_base::iostate ioState_
state
Definition: IOstream.hpp:80
pFlow::IOstream
A base calss for input/output streams.
Definition: IOstream.hpp:47
pFlow::IOstream::CLOSED
@ CLOSED
Definition: IOstream.hpp:55
pFlow::IOstream::lineNumber
int32 lineNumber(const int32 num)
Set the stream line number return the previous value.
Definition: IOstream.hpp:236
pFlow::IOstream::OPENED
@ OPENED
stream is not open
Definition: IOstream.hpp:56