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  enum writeFormat: char
58  {
59  ASCII = 0,
61  };
62 
64  static unsigned int precision_;
65 
66 protected:
67 
69  static word staticName_;
70 
73 
76 
78  ios_base::iostate ioState_;
79 
80 
83 
84 
85  //- Protected Member Functions
86 
88  void setOpened()
89  {
91  }
92 
94  void setClosed()
95  {
97  }
98 
100  void setState(ios_base::iostate state)
101  {
102  ioState_ = state;
103  }
104 
106  {
107  writeFormat_ = wF;
108  }
109 
111  void setGood()
112  {
113  ioState_ = ios_base::iostate(0);
114  }
115 
116 public:
117 
118  //- Constructors
119 
121  explicit IOstream():
124  ioState_(ios_base::iostate(0)),
125  lineNumber_(0)
126  {
127  setBad();
128  }
129 
131  explicit IOstream(writeFormat wF):
133  writeFormat_(wF),
134  ioState_(ios_base::iostate(0)),
135  lineNumber_(0)
136  {
137  setBad();
138  }
139 
141  IOstream(const IOstream&) = default;
142 
144  virtual ~IOstream() = default;
145 
146 
147  //- Member Functions
148 
150  virtual const word& name() const;
151 
153  virtual word& name();
154 
159  virtual bool check(const char* operation) const;
160 
163  bool fatalCheck(const char* operation) const;
164 
166  bool opened() const
167  {
168  return openClosed_ == OPENED;
169  }
170 
172  bool closed() const
173  {
174  return openClosed_ == CLOSED;
175  }
176 
178  bool isBinary()const
179  {
180  return writeFormat_ == BINARY;
181  }
182 
184  bool good() const
185  {
186  return ioState_ == 0;
187  }
188 
190  bool eof() const
191  {
192  return ioState_ & ios_base::eofbit;
193  }
194 
196  bool fail() const
197  {
198  return ioState_ & (ios_base::badbit | ios_base::failbit);
199  }
200 
202  bool bad() const
203  {
204  return ioState_ & ios_base::badbit;
205  }
206 
208  explicit operator bool() const
209  {
210  return !fail();
211  }
212 
214  bool operator!() const
215  {
216  return fail();
217  }
218 
219 
222  {
223  return lineNumber_;
224  }
225 
228  {
229  return lineNumber_;
230  }
231 
234  int32 lineNumber(const int32 num)
235  {
236  const int32 old(lineNumber_);
237  lineNumber_ = num;
238  return old;
239  }
240 
242  virtual ios_base::fmtflags flags() const = 0;
243 
245  static unsigned int defaultPrecision()
246  {
247  return precision_;
248  }
249 
252  static unsigned int defaultPrecision(unsigned int prec)
253  {
254  unsigned int old(precision_);
255  precision_ = prec;
256  return old;
257  }
258 
260  void setEof()
261  {
262  ioState_ |= ios_base::eofbit;
263  }
264 
266  void setFail()
267  {
268  ioState_ |= ios_base::failbit;
269  }
270 
272  void setBad()
273  {
274  ioState_ |= ios_base::badbit;
275  }
276 
278  virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
279 
281  ios_base::fmtflags setf(const ios_base::fmtflags f)
282  {
283  return flags(flags() | f);
284  }
285 
287  ios_base::fmtflags setf
288  (
289  const ios_base::fmtflags f,
290  const ios_base::fmtflags mask
291  )
292  {
293  return flags((flags() & ~mask) | (f & mask));
294  }
295 
297  void unsetf(const ios_base::fmtflags f)
298  {
299  flags(flags() & ~f);
300  }
301 
302 
303 }; // end of IOstream
304 
305 
307 typedef IOstream& (*IOstreamManip)(IOstream&);
308 
309 inline IOstream& dec(IOstream& io)
310 {
312  return io;
313 }
314 
315 inline IOstream& hex(IOstream& io)
316 {
318  return io;
319 }
320 
321 inline IOstream& oct(IOstream& io)
322 {
324  return io;
325 }
326 
327 inline IOstream& fixed(IOstream& io)
328 {
329  io.setf(ios_base::fixed, ios_base::floatfield);
330  return io;
331 }
332 
334 {
335  io.setf(ios_base::scientific, ios_base::floatfield);
336  return io;
337 }
338 
339 
340 
341 } // pFlow
342 
343 #endif // __IOstream__hpp__
pFlow::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.hpp:190
pFlow::IOstream::setGood
void setGood()
Set stream to be good.
Definition: IOstream.hpp:111
pFlow::IOstream::setWriteFormat
void setWriteFormat(writeFormat wF)
Definition: IOstream.hpp:105
pFlow::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.hpp:245
pFlow::fixed
IOstream & fixed(IOstream &io)
Definition: IOstream.hpp:327
pFlow::dec
IOstream & dec(IOstream &io)
Definition: IOstream.hpp:309
pFlow::IOstream::writeFormat_
writeFormat writeFormat_
write format
Definition: IOstream.hpp:75
pFlow::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.hpp:333
pFlow::IOstream::IOstream
IOstream(writeFormat wF)
Construct and set write format.
Definition: IOstream.hpp:131
pFlow::IOstream::lineNumber_
int32 lineNumber_
The file line.
Definition: IOstream.hpp:82
pFlow::IOstream::operator!
bool operator!() const
Return true if the stream has failed.
Definition: IOstream.hpp:214
pFlow::hex
IOstream & hex(IOstream &io)
Definition: IOstream.hpp:315
pFlow::IOstream::ASCII
@ ASCII
Definition: IOstream.hpp:59
pFlow::IOstream::closed
bool closed() const
Return true if stream is closed.
Definition: IOstream.hpp:172
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::IOstream::staticName_
static word staticName_
Name for any generic stream - normally treat as readonly.
Definition: IOstream.hpp:69
pFlow::IOstream::precision_
static unsigned int precision_
Default precision, only works for ASCII.
Definition: IOstream.hpp:64
pFlow
Definition: demComponent.hpp:28
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:252
pFlow::IOstream::flags
virtual ios_base::fmtflags flags() const =0
Return flags of stream.
pFlow::IOstream::BINARY
@ BINARY
Definition: IOstream.hpp:60
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:202
pFlow::IOstream::openClosed_
streamAccess openClosed_
Is stream open or closed.
Definition: IOstream.hpp:72
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.hpp:272
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:121
pFlow::IOstream::fail
bool fail() const
Return true if next operation will fail.
Definition: IOstream.hpp:196
pFlow::IOstream::setClosed
void setClosed()
Set stream closed.
Definition: IOstream.hpp:94
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:51
pFlow::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.hpp:184
pFlow::IOstream::lineNumber
int32 & lineNumber()
Non-const access to the current stream line number.
Definition: IOstream.hpp:227
pFlow::IOstream::~IOstream
virtual ~IOstream()=default
Destructor.
pFlow::IOstream::isBinary
bool isBinary() const
Return true if stream format is binray.
Definition: IOstream.hpp:178
pFlow::IOstream::opened
bool opened() const
Return true if stream has been opened.
Definition: IOstream.hpp:166
pFlow::IOstream::setState
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.hpp:100
pFlow::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.hpp:281
pFlow::IOstream::setOpened
void setOpened()
Set stream opened.
Definition: IOstream.hpp:88
pFlow::IOstream::setFail
void setFail()
Set stream to have failed.
Definition: IOstream.hpp:266
pFlow::oct
IOstream & oct(IOstream &io)
Definition: IOstream.hpp:321
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:221
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:57
pFlow::IOstream::setEof
void setEof()
Set stream to have reached eof.
Definition: IOstream.hpp:260
bTypesFunctions.hpp
pFlow::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.hpp:297
pFlow::IOstream::ioState_
ios_base::iostate ioState_
state
Definition: IOstream.hpp:78
pFlow::IOstream
Definition: IOstream.hpp:45
pFlow::IOstream::CLOSED
@ CLOSED
Definition: IOstream.hpp:53
pFlow::IOstream::lineNumber
int32 lineNumber(const int32 num)
Set the stream line number return the previous value.
Definition: IOstream.hpp:234
pFlow::IOstream::OPENED
@ OPENED
stream is not open
Definition: IOstream.hpp:54