# PhasicFlow: src/phasicFlow/streams/iStream/IOstream.hpp Source File

IOstream.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#ifndef __IOstream_hpp__
#define __IOstream_hpp__
// based on OpenFOAM stream, with some modifications/simplifications
// to be tailored to our needs
#include <iostream>
#include "bTypesFunctions.hpp"
using std::ios_base;
using std::istream;
using std::ostream;
using std::cin;
using std::cout;
using std::cerr;
namespace pFlow
{
class IOstream
{
public:
enum streamAccess : char
{
CLOSED = 0,
OPENED
};
//- Default precision
static unsigned int precision_;
protected:
//- Name for any generic stream - normally treat as readonly
static word staticName_;
streamAccess openClosed_;
ios_base::iostate ioState_;
//- The file line
int32 lineNumber_;
// Protected Member Functions
//- Set stream opened
void setOpened()
{
openClosed_ = OPENED;
}
//- Set stream closed
void setClosed()
{
openClosed_ = CLOSED;
}
//- Set stream state
void setState(ios_base::iostate state)
{
ioState_ = state;
}
//- Set stream to be good
void setGood()
{
ioState_ = ios_base::iostate(0);
}
public:
// Constructors
explicit IOstream():
openClosed_(CLOSED),
ioState_(ios_base::iostate(0)),
lineNumber_(0)
{
setBad();
}
IOstream(const IOstream&) = default;
//- Destructor
virtual ~IOstream() = default;
//- Return the name of the stream
virtual const word& name() const;
//- Return non-const access to the name of the stream
virtual word& name();
//- Check IOstream status for given operation.
// Print IOstream state or generate a FatalIOError
// when an error has occurred.
// The base implementation is a fatalCheck
virtual bool check(const char* operation) const;
//- Check IOstream status for given operation.
// Generate a FatalIOError when an error has occurred.
bool fatalCheck(const char* operation) const;
//- Return true if stream has been opened
bool opened() const
{
return openClosed_ == OPENED;
}
//- Return true if stream is closed
bool closed() const
{
return openClosed_ == CLOSED;
}
//- Return true if next operation might succeed
bool good() const
{
return ioState_ == 0;
}
//- Return true if end of input seen
bool eof() const
{
return ioState_ & ios_base::eofbit;
}
//- Return true if next operation will fail
bool fail() const
{
return ioState_ & (ios_base::badbit | ios_base::failbit);
}
//- Return true if stream is corrupted
bool bad() const
{
return ioState_ & ios_base::badbit;
}
//- Return true if the stream has not failed
explicit operator bool() const
{
return !fail();
}
//- Return true if the stream has failed
bool operator!() const
{
return fail();
}
//- Const access to the current stream line number
int32 lineNumber() const
{
return lineNumber_;
}
//- Non-const access to the current stream line number
int32& lineNumber()
{
return lineNumber_;
}
//- Set the stream line number
// \return the previous value
int32 lineNumber(const int32 num)
{
const int32 old(lineNumber_);
lineNumber_ = num;
return old;
}
//- Return flags of stream
virtual ios_base::fmtflags flags() const = 0;
//- Return the default precision
static unsigned int defaultPrecision()
{
return precision_;
}
//- Reset the default precision
// \return the previous value
static unsigned int defaultPrecision(unsigned int prec)
{
unsigned int old(precision_);
precision_ = prec;
return old;
}
//- Set stream to have reached eof
void setEof()
{
ioState_ |= ios_base::eofbit;
}
//- Set stream to have failed
void setFail()
{
ioState_ |= ios_base::failbit;
}
//- Set stream to be bad
void setBad()
{
ioState_ |= ios_base::badbit;
}
//- Set flags of stream
virtual ios_base::fmtflags flags(const ios_base::fmtflags f) = 0;
//- Set flags of stream
ios_base::fmtflags setf(const ios_base::fmtflags f)
{
return flags(flags() | f);
}
//- Set flags of given field of stream
ios_base::fmtflags setf
(
const ios_base::fmtflags f,
const ios_base::fmtflags mask
)
{
return flags((flags() & ~mask) | (f & mask));
}
//- Unset flags of stream
void unsetf(const ios_base::fmtflags f)
{
flags(flags() & ~f);
}
}; // end of IOstream
//- An IOstream manipulator
typedef IOstream& (*IOstreamManip)(IOstream&);
inline IOstream& dec(IOstream& io)
{
io.setf(ios_base::dec, ios_base::dec|ios_base::hex|ios_base::oct);
return io;
}
inline IOstream& hex(IOstream& io)
{
io.setf(ios_base::hex, ios_base::dec|ios_base::hex|ios_base::oct);
return io;
}
inline IOstream& oct(IOstream& io)
{
io.setf(ios_base::oct, ios_base::dec|ios_base::hex|ios_base::oct);
return io;
}
inline IOstream& fixed(IOstream& io)
{
io.setf(ios_base::fixed, ios_base::floatfield);
return io;
}
inline IOstream& scientific(IOstream& io)
{
io.setf(ios_base::scientific, ios_base::floatfield);
return io;
}
} // pFlow
#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