ENH: add syncState() method to serial streams (#2623)

- in places where direct reading from the std::stream is used,
  this method can be used to ensure that the OpenFOAM Sstream state
  is properly updated from the std::stream.

ENH: restrict stream renaming to ISstream

- non-const access was previously declared at the top-level (IOstream)
  but that not only added in potentially odd setting of the static
  fileName, but also meant that the OFstream name() could potentially
  be altered after opening a file and thus be inconsistent with the
  underlying file that had been opened.

  Now restrict name modification to ISstream (and ITstream
  counterpart). Does not affect any existing valid code.

STYLE: non-default OFstream destructor (for future file staging)
This commit is contained in:
Mark Olesen
2022-10-27 14:38:23 +02:00
parent bd000d89e9
commit 278378031e
15 changed files with 142 additions and 155 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -97,6 +97,12 @@ Foam::OFstream::OFstream
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::OFstream::~OFstream()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
std::ostream& Foam::OFstream::stdStream()

View File

@ -90,7 +90,7 @@ public:
//- Destructor
~OFstream() = default;
~OFstream();
// Member Functions

View File

@ -43,12 +43,6 @@ const Foam::fileName& Foam::IOstream::name() const
}
Foam::fileName& Foam::IOstream::name()
{
return staticName_;
}
Foam::fileName Foam::IOstream::relativeName() const
{
return argList::envRelativePath(this->name());

View File

@ -194,13 +194,9 @@ public:
// Access
//- Return the name of the stream.
// Useful for Fstream to remember the filename
//- The name of the stream.
virtual const fileName& name() const;
//- Return stream name for modification
virtual fileName& name();
//- Return the name of the stream relative to the current case.
// Uses argList::envRelativePath()
fileName relativeName() const;

View File

@ -743,7 +743,8 @@ Foam::Istream& Foam::ISstream::read(token& t)
}
buf[nChar] = '\0'; // Terminate string
setState(is_.rdstate());
syncState();
if (is_.bad())
{
t.setBad();
@ -996,7 +997,7 @@ Foam::Istream& Foam::ISstream::read(string& str)
Foam::Istream& Foam::ISstream::read(label& val)
{
is_ >> val;
setState(is_.rdstate());
syncState();
return *this;
}
@ -1004,7 +1005,7 @@ Foam::Istream& Foam::ISstream::read(label& val)
Foam::Istream& Foam::ISstream::read(float& val)
{
is_ >> val;
setState(is_.rdstate());
syncState();
return *this;
}
@ -1012,7 +1013,7 @@ Foam::Istream& Foam::ISstream::read(float& val)
Foam::Istream& Foam::ISstream::read(double& val)
{
is_ >> val;
setState(is_.rdstate());
syncState();
return *this;
}
@ -1030,8 +1031,7 @@ Foam::Istream& Foam::ISstream::read(char* buf, std::streamsize count)
Foam::Istream& Foam::ISstream::readRaw(char* buf, std::streamsize count)
{
is_.read(buf, count);
setState(is_.rdstate());
syncState();
return *this;
}
@ -1046,8 +1046,7 @@ bool Foam::ISstream::beginRawRead()
}
readBegin("binaryBlock");
setState(is_.rdstate());
syncState();
return is_.good();
}
@ -1055,8 +1054,7 @@ bool Foam::ISstream::beginRawRead()
bool Foam::ISstream::endRawRead()
{
readEnd("binaryBlock");
setState(is_.rdstate());
syncState();
return is_.good();
}
@ -1073,18 +1071,4 @@ void Foam::ISstream::rewind()
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
std::ios_base::fmtflags Foam::ISstream::flags() const
{
return is_.flags();
}
std::ios_base::fmtflags Foam::ISstream::flags(const ios_base::fmtflags f)
{
return is_.flags(f);
}
// ************************************************************************* //

View File

@ -121,21 +121,42 @@ public:
// Characteristics
//- Return the name of the stream.
// Useful for Fstream to return the filename
virtual const fileName& name() const
{
return name_;
}
//- The name of the input serial stream.
//- (eg, the name of the Fstream file name)
virtual const fileName& name() const { return name_; }
//- Return stream name for modification
virtual fileName& name()
{
return name_;
}
//- The name of the input serial stream, for modification.
virtual fileName& name() { return name_; }
// STL stream
//- Const access to underlying std::istream
virtual const std::istream& stdStream() const { return is_; }
//- Access to underlying std::istream
virtual std::istream& stdStream() { return is_; }
// Stream State
//- Return flags of output stream
virtual ios_base::fmtflags flags() const;
virtual ios_base::fmtflags flags() const
{
return is_.flags();
}
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags f)
{
return is_.flags(f);
}
//- Set stream state to match that of the std::istream
void syncState()
{
setState(is_.rdstate());
}
// Special-purpose Functions
@ -217,27 +238,6 @@ public:
virtual void rewind();
// Stream state functions
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags flags);
// STL stream
//- Access to underlying std::istream
virtual std::istream& stdStream()
{
return is_;
}
//- Const access to underlying std::istream
virtual const std::istream& stdStream() const
{
return is_;
}
// Print
//- Print stream description to Ostream

View File

@ -46,7 +46,7 @@ inline Foam::ISstream::ISstream
}
else
{
setState(is_.rdstate());
syncState();
}
}
@ -56,9 +56,9 @@ inline Foam::ISstream::ISstream
inline Foam::ISstream& Foam::ISstream::get(char& c)
{
is_.get(c);
setState(is_.rdstate());
syncState();
if (good() && c == '\n')
if (c == '\n' && good())
{
++lineNumber_;
}
@ -76,7 +76,7 @@ inline int Foam::ISstream::peek()
inline Foam::ISstream& Foam::ISstream::getLine(std::string& str, char delim)
{
std::getline(is_, str, delim);
setState(is_.rdstate());
syncState();
if (delim == '\n')
{
@ -90,11 +90,11 @@ inline Foam::ISstream& Foam::ISstream::getLine(std::string& str, char delim)
inline std::streamsize Foam::ISstream::getLine(std::nullptr_t, char delim)
{
is_.ignore(std::numeric_limits<std::streamsize>::max(), delim);
setState(is_.rdstate());
syncState();
std::streamsize count = is_.gcount();
if (count && delim == '\n')
if (delim == '\n' && count)
{
++lineNumber_;
}
@ -115,7 +115,7 @@ inline Foam::ISstream& Foam::ISstream::putback(const char c)
setBad();
}
setState(is_.rdstate());
syncState();
return *this;
}

View File

@ -93,7 +93,7 @@ Foam::Ostream& Foam::OSstream::write(const char c)
{
++lineNumber_;
}
setState(os_.rdstate());
syncState();
return *this;
}
@ -102,7 +102,7 @@ Foam::Ostream& Foam::OSstream::write(const char* str)
{
lineNumber_ += stringOps::count(str, token::NL);
os_ << str;
setState(os_.rdstate());
syncState();
return *this;
}
@ -110,7 +110,7 @@ Foam::Ostream& Foam::OSstream::write(const char* str)
Foam::Ostream& Foam::OSstream::write(const word& str)
{
os_ << str;
setState(os_.rdstate());
syncState();
return *this;
}
@ -127,7 +127,7 @@ Foam::Ostream& Foam::OSstream::writeQuoted
lineNumber_ += stringOps::count(str, token::NL);
os_ << str;
setState(os_.rdstate());
syncState();
return *this;
}
@ -169,7 +169,7 @@ Foam::Ostream& Foam::OSstream::writeQuoted
// they would otherwise appear like an escaped end-quote
os_ << token::DQUOTE;
setState(os_.rdstate());
syncState();
return *this;
}
@ -183,7 +183,7 @@ Foam::Ostream& Foam::OSstream::write(const string& str)
Foam::Ostream& Foam::OSstream::write(const int32_t val)
{
os_ << val;
setState(os_.rdstate());
syncState();
return *this;
}
@ -191,7 +191,7 @@ Foam::Ostream& Foam::OSstream::write(const int32_t val)
Foam::Ostream& Foam::OSstream::write(const int64_t val)
{
os_ << val;
setState(os_.rdstate());
syncState();
return *this;
}
@ -199,7 +199,7 @@ Foam::Ostream& Foam::OSstream::write(const int64_t val)
Foam::Ostream& Foam::OSstream::write(const float val)
{
os_ << val;
setState(os_.rdstate());
syncState();
return *this;
}
@ -207,7 +207,7 @@ Foam::Ostream& Foam::OSstream::write(const float val)
Foam::Ostream& Foam::OSstream::write(const double val)
{
os_ << val;
setState(os_.rdstate());
syncState();
return *this;
}
@ -232,8 +232,7 @@ bool Foam::OSstream::beginRawWrite(std::streamsize count)
}
os_ << token::BEGIN_LIST;
setState(os_.rdstate());
syncState();
return os_.good();
}
@ -241,8 +240,7 @@ bool Foam::OSstream::beginRawWrite(std::streamsize count)
bool Foam::OSstream::endRawWrite()
{
os_ << token::END_LIST;
setState(os_.rdstate());
syncState();
return os_.good();
}
@ -257,8 +255,7 @@ Foam::Ostream& Foam::OSstream::writeRaw
// beginRawWrite() method, or the caller knows what they are doing.
os_.write(data, count);
setState(os_.rdstate());
syncState();
return *this;
}
@ -269,6 +266,7 @@ void Foam::OSstream::indent()
{
os_ << ' ';
}
syncState();
}
@ -285,18 +283,6 @@ void Foam::OSstream::endl()
}
std::ios_base::fmtflags Foam::OSstream::flags() const
{
return os_.flags();
}
std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
{
return os_.flags(f);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
char Foam::OSstream::fill() const

View File

@ -115,21 +115,39 @@ public:
// Characteristics
//- Get the name of the stream.
// Useful for Fstream to remember the filename
virtual const fileName& name() const
{
return name_;
}
//- Get the name of the output serial stream.
//- (eg, the name of the Fstream file name)
virtual const fileName& name() const { return name_; }
//- Return stream name for modification
virtual fileName& name()
{
return name_;
}
// STL stream
//- Const access to underlying std::ostream
virtual const std::ostream& stdStream() const { return os_; }
//- Access to underlying std::ostream
virtual std::ostream& stdStream() { return os_; }
// Stream State
//- Get stream flags
virtual ios_base::fmtflags flags() const;
virtual ios_base::fmtflags flags() const
{
return os_.flags();
}
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags f)
{
return os_.flags(f);
}
//- Set stream state to match that of the std::ostream
void syncState()
{
setState(os_.rdstate());
}
// Write Functions
@ -197,9 +215,6 @@ public:
// Stream state functions
//- Set stream flags
virtual ios_base::fmtflags flags(const ios_base::fmtflags f);
//- Flush stream
virtual void flush();
@ -228,21 +243,6 @@ public:
virtual int precision(const int p);
// STL stream
//- Access to underlying std::ostream
virtual std::ostream& stdStream()
{
return os_;
}
//- Const access to underlying std::ostream
virtual const std::ostream& stdStream() const
{
return os_;
}
// Print
//- Print stream description to Ostream

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -49,7 +49,7 @@ inline Foam::OSstream::OSstream
}
else
{
setState(os_.rdstate());
syncState();
}
}

View File

@ -203,6 +203,15 @@ public:
// Member Functions
// Characteristics
//- The name of the input token stream.
virtual const fileName& name() const { return name_; }
//- The name of the input token stream, for modification.
virtual fileName& name() { return name_; }
// Token Access
//- True if putback token is in use
@ -255,21 +264,6 @@ public:
void skip(label n = 1);
// Inquiry
//- Get the name of the stream
virtual const fileName& name() const
{
return name_;
}
//- Return stream name for modification
virtual fileName& name()
{
return name_;
}
// Token list modification
//- Copy append a token at the current tokenIndex,

View File

@ -88,6 +88,8 @@ static void skipComments(ISstream& iss)
}
}
}
iss.syncState();
}

View File

@ -149,6 +149,7 @@ Foam::Ostream& Foam::ensightFile::writeString(const char* str)
{
buf[79] = 0; // max 79 in ASCII, ensure it is indeed nul-terminated
stdStream() << buf;
syncState();
}
return *this;
@ -186,6 +187,7 @@ Foam::Ostream& Foam::ensightFile::write
)
{
stdStream().write(buf, count);
syncState();
return *this;
}
@ -204,6 +206,7 @@ Foam::Ostream& Foam::ensightFile::write(const int32_t val)
{
stdStream().width(10);
stdStream() << val;
syncState();
}
return *this;
@ -232,6 +235,7 @@ Foam::Ostream& Foam::ensightFile::write(const float val)
{
stdStream().width(12);
stdStream() << val;
syncState();
}
return *this;
@ -260,6 +264,7 @@ Foam::Ostream& Foam::ensightFile::write
{
stdStream().width(fieldWidth);
stdStream() << value;
syncState();
}
return *this;
@ -271,6 +276,7 @@ void Foam::ensightFile::newline()
if (format() == IOstreamOption::ASCII)
{
stdStream() << nl;
syncState();
}
}

View File

@ -36,16 +36,16 @@ Foam::ensightReadFile::detectBinaryHeader(const fileName& pathname)
// Detect BINARY vs ASCII by testing for initial "(C|Fortran) Binary"
{
IFstream is(pathname, IOstreamOption::BINARY);
IFstream ifs(pathname, IOstreamOption::BINARY);
if (!is.good())
if (!ifs.good())
{
FatalErrorInFunction
<< "Cannot read file " << is.name() << nl
<< "Cannot read file " << ifs.name() << nl
<< exit(FatalError);
}
istream& iss = is.stdStream();
istream& iss = ifs.stdStream();
// Binary string is *exactly* 80 characters
string buf(size_t(80), '\0');
@ -105,6 +105,7 @@ Foam::Istream& Foam::ensightReadFile::read
)
{
stdStream().read(buf, count);
syncState();
return *this;
}
@ -119,6 +120,8 @@ Foam::Istream& Foam::ensightReadFile::read(string& value)
value.resize(80, '\0');
iss.read(&value[0], 80);
syncState();
if (!iss)
{
// Truncated - could also exit here, but no real advantage
@ -169,6 +172,7 @@ Foam::Istream& Foam::ensightReadFile::read(label& value)
else
{
stdStream() >> ivalue;
syncState();
}
value = ivalue;
@ -193,6 +197,7 @@ Foam::Istream& Foam::ensightReadFile::read(scalar& value)
else
{
stdStream() >> value;
syncState();
}
return *this;

View File

@ -96,6 +96,7 @@ Foam::label Foam::fileFormats::FIRECore::getFireLabel(ISstream& is)
reinterpret_cast<char *>(&ivalue),
sizeof(ivalue)
);
is.syncState();
return ivalue;
}
@ -119,6 +120,7 @@ Foam::point Foam::fileFormats::FIRECore::getFirePoint(ISstream& is)
reinterpret_cast<char *>(&coord),
sizeof(coord)
);
is.syncState();
pt.x() = coord[0];
pt.y() = coord[1];
@ -155,6 +157,8 @@ std::string Foam::fileFormats::FIRECore::getFireString(ISstream& is)
{
is.stdStream().read(&(str[pos]), sizeof(char));
}
is.syncState();
}
else
{
@ -213,6 +217,8 @@ void Foam::fileFormats::FIRECore::putFireLabel
reinterpret_cast<char const *>(&ivalue),
sizeof(ivalue)
);
os.syncState();
}
else
{
@ -247,6 +253,8 @@ void Foam::fileFormats::FIRECore::putFireLabels
sizeof(ivalue)
);
}
os.syncState();
}
else
{
@ -286,6 +294,8 @@ void Foam::fileFormats::FIRECore::putFireLabels
sizeof(ivalue)
);
}
os.syncState();
}
else
{
@ -319,6 +329,8 @@ void Foam::fileFormats::FIRECore::putFirePoint
reinterpret_cast<char const *>(&fvalue),
sizeof(fvalue)
);
os.syncState();
}
else
{
@ -353,6 +365,8 @@ void Foam::fileFormats::FIRECore::putFireString
// output without surrounding quotes
os.stdStream() << value << '\n';
}
os.syncState();
}