Commit 4cf932b2 authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: reorder/refactor stream format options

- IOstreamOption class to encapsulate format, compression, version.
  This is ordered to avoid internal padding in the structure, which
  reduces several bytes of memory overhead for stream objects
  and other things using this combination of data.

  Byte-sizes:
      old  IOstream:48  PstreamBuffers:88  Time:928
      new  IOstream:24  PstreamBuffers:72  Time:904

====

STYLE: remove support for deprecated uncompressed/compressed selectors

In older versions, the system/controlDict used these types of
specifications:

    writeCompression uncompressed;
    writeCompression compressed;

As of DEC-2009, these were deprecated in favour of using normal switch
names:

    writeCompression true;
    writeCompression false;
    writeCompression on;
    writeCompression off;

Now removed these deprecated names and treat like any other unknown
input and issue a warning. Eg,

   Unknown compression specifier 'compressed', assuming no compression

====

STYLE: provide Enum of stream format names (ascii, binary)

====

COMP: fixed incorrect IFstream construct in FIREMeshReader

- spurious bool argument (presumably meant as uncompressed) was being
  implicitly converted to a versionNumber. Now caught by making
  IOstreamOption::versionNumber constructor explicit.

- bad version specifier in changeDictionary
parent 756502da
......@@ -646,7 +646,7 @@ int main(int argc, char *argv[])
dictList.writeObject
(
runTime.writeFormat(),
runTime.writeFormat(),
IOstream::currentVersion,
IOstream::UNCOMPRESSED,
true
);
......
......@@ -158,6 +158,7 @@ Streams = db/IOstreams
$(Streams)/token/tokenIO.C
IOstreams = $(Streams)/IOstreams
$(IOstreams)/IOstreamOption.C
$(IOstreams)/IOstream.C
$(IOstreams)/Istream.C
$(IOstreams)/Ostream.C
......
......@@ -72,8 +72,7 @@ namespace Foam
::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
{}
virtual ~addfileModificationCheckingToOpt()
{}
virtual ~addfileModificationCheckingToOpt() = default;
virtual void readData(Foam::Istream& is)
{
......
......@@ -295,7 +295,7 @@ public:
// Member Functions
// General access
// General access
//- Return the local objectRegistry
const objectRegistry& db() const;
......@@ -337,7 +337,7 @@ public:
inline bool& globalObject();
// Read/write options
// Read/write options
//- The read option
inline readOption readOpt() const;
......@@ -352,7 +352,7 @@ public:
inline writeOption& writeOpt();
// Path components
// Path components
//- Return group (extension part of name)
word group() const;
......@@ -402,7 +402,7 @@ public:
) const;
// Reading
// Reading
//- Read header
bool readHeader(Istream& is);
......@@ -424,7 +424,7 @@ public:
void warnNoRereading() const;
// Writing
// Writing
//- Write the standard OpenFOAM file/dictionary banner
// Optionally without -*- C++ -*- editor hint (eg, for logs)
......@@ -443,14 +443,14 @@ public:
bool writeHeader(Ostream& os, const word& objectType) const;
// Error Handling
// Error Handling
inline bool good() const;
inline bool bad() const;
// Info
// Info
//- Return info proxy.
// Used to print token information to a stream
......
......@@ -60,7 +60,7 @@ class IOobjectList
{
// Private Member Functions
//- Disallow default bitwise assignment
//- No copy assignment
void operator=(const IOobjectList&) = delete;
......@@ -92,7 +92,7 @@ public:
// Member functions
// Basic methods
// Basic methods
//- Add an IOobject to the list
bool add(IOobject& io);
......@@ -101,7 +101,7 @@ public:
bool remove(IOobject& io);
// Lookup
// Lookup
//- Lookup a given name and return IOobject ptr if found else nullptr
IOobject* lookup(const word& name) const;
......@@ -116,7 +116,7 @@ public:
IOobjectList lookupClass(const word& clsName) const;
// Summary of classes
// Summary of classes
//- A summary hash of classes used and their associated object names.
// The HashTable representation allows us to leverage various
......@@ -196,7 +196,7 @@ public:
HashTable<wordHashSet> classes(const wordRes& matcher) const;
// Summary of names
// Summary of names
//- A list of names of the IOobjects
wordList names() const;
......@@ -213,7 +213,7 @@ public:
wordList names(const word& clsName, const wordRes& matcher) const;
// Summary of names (sorted)
// Summary of names (sorted)
//- A sorted list of names of the IOobjects
wordList sortedNames() const;
......
......@@ -644,7 +644,7 @@ Foam::autoPtr<Foam::ISstream> Foam::decomposedBlockData::readBlocks
// version
string versionString(realIsPtr().version().str());
Pstream::scatter(versionString, Pstream::msgType(), comm);
realIsPtr().version(IStringStream(versionString)());
realIsPtr().version(IOstream::versionNumber(versionString));
// stream
{
......@@ -1071,7 +1071,7 @@ bool Foam::decomposedBlockData::writeData(Ostream& os) const
writeHeader
(
os,
IOstream::versionNumber(IStringStream(versionString)()),
IOstream::versionNumber(versionString),
IOstream::formatEnum(formatString),
io.headerClassName(),
io.note(),
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -23,35 +23,30 @@ License
\*---------------------------------------------------------------------------*/
#include "IOstreamOption.H"
#include "IOstreams.H"
#include "OFstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
// Default output precision
unsigned int Foam::IOstream::precision_
(
Foam::debug::infoSwitch("writePrecision", 6)
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Define the default IOstream versions and precision
const IOstream::versionNumber IOstream::originalVersion(0.5);
const IOstream::versionNumber IOstream::currentVersion(2.0);
unsigned int IOstream::precision_(debug::infoSwitch("writePrecision", 6));
// * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Global IO streams
ISstream Sin(cin, "Sin");
OSstream Sout(cout, "Sout");
OSstream Serr(cerr, "Serr");
OFstream Snull("/dev/null");
prefixOSstream Pout(cout, "Pout");
prefixOSstream Perr(cerr, "Perr");
Foam::ISstream Foam::Sin(std::cin, "Sin");
Foam::OSstream Foam::Sout(std::cout, "Sout");
Foam::OSstream Foam::Serr(std::cerr, "Serr");
Foam::OFstream Foam::Snull("/dev/null");
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::prefixOSstream Foam::Pout(std::cout, "Pout");
Foam::prefixOSstream Foam::Perr(std::cerr, "Perr");
} // End namespace Foam
// ************************************************************************* //
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,68 +25,12 @@ License
#include "IOstream.H"
#include "error.H"
#include "Switch.H"
#include <sstream>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
Foam::fileName Foam::IOstream::staticName_("IOstream");
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::IOstream::streamFormat
Foam::IOstream::formatEnum(const word& format)
{
if (format == "ascii")
{
return IOstream::ASCII;
}
else if (format == "binary")
{
return IOstream::BINARY;
}
else
{
WarningInFunction
<< "bad format specifier '" << format << "', using 'ascii'"
<< endl;
return IOstream::ASCII;
}
}
Foam::IOstream::compressionType
Foam::IOstream::compressionEnum(const word& compression)
{
// get Switch (bool) value, but allow it to fail
Switch sw(compression, true);
if (sw.valid())
{
return sw ? IOstream::COMPRESSED : IOstream::UNCOMPRESSED;
}
else if (compression == "uncompressed")
{
return IOstream::UNCOMPRESSED;
}
else if (compression == "compressed")
{
return IOstream::COMPRESSED;
}
else
{
WarningInFunction
<< "bad compression specifier '" << compression
<< "', using 'uncompressed'"
<< endl;
return IOstream::UNCOMPRESSED;
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::fileName& Foam::IOstream::name() const
......@@ -125,32 +69,10 @@ void Foam::IOstream::fatalCheck(const char* operation) const
}
Foam::string Foam::IOstream::versionNumber::str() const
{
std::ostringstream os;
os.precision(1);
os.setf(ios_base::fixed, ios_base::floatfield);
os << versionNumber_;
return os.str();
}
void Foam::IOstream::print(Ostream& os) const
{
os << "IOstream: " << "Version " << version_ << ", format ";
switch (format_)
{
case ASCII:
os << "ASCII";
break;
case BINARY:
os << "BINARY";
break;
}
os << ", line " << lineNumber();
os << "IOstream: " << "Version " << version() << ", format "
<< format() << ", line " << lineNumber();
if (opened())
{
......@@ -213,28 +135,6 @@ void Foam::IOstream::print(Ostream& os, const int streamState) const
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<(Ostream& os, const IOstream::streamFormat& sf)
{
if (sf == IOstream::ASCII)
{
os << "ascii";
}
else
{
os << "binary";
}
return os;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const IOstream::versionNumber& vn)
{
os << vn.str().c_str();
return os;
}
template<>
Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<IOstream>& ip)
{
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -49,6 +49,7 @@ SourceFiles
#include "scalar.H"
#include "fileName.H"
#include "InfoProxy.H"
#include "IOstreamOption.H"
#include <iostream>
......@@ -70,468 +71,281 @@ namespace Foam
\*---------------------------------------------------------------------------*/
class IOstream
:
public IOstreamOption
{
public:
// Public data types
// Public Data Types
//- Enumeration for whether the stream open or closed
enum streamAccess
enum streamAccess : char
{
OPENED,
CLOSED
CLOSED = 0, //!< stream not open
OPENED //!< stream is open
};
//- Enumeration for the format of data in the stream
enum streamFormat
{
ASCII,
BINARY
};
//- Ostream operator
friend Ostream& operator<<(Ostream& os, const streamFormat& sf);
// Public Static Data
//- Version number type
class versionNumber
{
//- The version number
scalar versionNumber_;
//- Default precision
static unsigned int precision_;
//- The version number as an integer
int index_;
protected:
public:
// Protected Data
// Constructors
//- Name for any generic stream - normally treat as readonly
static fileName staticName_;
//- Construct from number
versionNumber(const scalar num)
:
versionNumber_(num),
index_(numberToIndex(num))
{}
streamAccess openClosed_;
//- Construct from Istream
versionNumber(Istream& is)
:
versionNumber_(readScalar(is)),
index_(numberToIndex(versionNumber_))
{}
ios_base::iostate ioState_;
label lineNumber_;
// Member functions
//- Convert a version number into an index
int numberToIndex(const scalar num) const
{
return int(10*num + SMALL);
}
// Protected Member Functions
//- Return major version
int majorVersion() const
{
return int(versionNumber_);
}
// Access
//- Return minor version
int minorVersion() const
{
return int(10.0*(versionNumber_ - majorVersion()));
}
//- Set stream opened
void setOpened()
{
openClosed_ = OPENED;
}
//- Return the versionNumber as a character string
string str() const;
//- Set stream closed
void setClosed()
{
openClosed_ = CLOSED;
}
//- Set stream state
void setState(ios_base::iostate state)
{
ioState_ = state;
}
// Member operators
//- Set stream to be good
void setGood()
{
ioState_ = ios_base::iostate(0);
}
//- Are these versionNumbers the same?
bool operator==(const versionNumber& vn)
{
return index_ == vn.index_;
}
//- Are these versionNumbers different?
bool operator!=(const versionNumber& vn)
{
return index_ != vn.index_;
}
public:
//- Is this version older than the one given
bool operator<(const versionNumber& vn)
{
return index_ < vn.index_;
}
// Constructors
//- Is this version the same as or older than the one given
bool operator<=(const versionNumber& vn)
{
return index_ <= vn.index_;
}
//- Construct with specified stream option
explicit IOstream(const IOstreamOption option)
:
IOstreamOption(option),
openClosed_(CLOSED),
ioState_(ios_base::iostate(0)),
lineNumber_(0)
{
setBad();
}
//- Is this version newer than the one given
bool operator>(const versionNumber& vn)
{
return index_ > vn.index_;
}
//- Construct setting format and version
IOstream
(
streamFormat format,
versionNumber version,
compressionType compression=UNCOMPRESSED
)
:
IOstream(IOstreamOption(format, version, compression))
{}
//- This version the same as or newer than the one given
bool operator>=(const versionNumber& vn)
{
return index_ >= vn.index_;
}
//- Destructor
virtual ~IOstream() = default;
//- Ostream operator
friend Ostream& operator<<(Ostream& os, const versionNumber& vn);
};
// Member Functions
//- Enumeration for the format of data in the stream
enum compressionType
{
UNCOMPRESSED,
COMPRESSED
};
// Access
//- Return the name of the stream
// Useful for Fstream to return the filename
virtual const fileName& name() const;
// Public static data
//- Return non-const access to the name of the stream
// Useful to alter the stream name
virtual fileName& name();
//- Original version number
static const versionNumber originalVersion;
//- Current version number
static const versionNumber currentVersion;
// Check
//- Default precision
static unsigned int precision_;
//- Check IOstream status for given operation
// print IOstream state if error has occured
virtual bool check(const char* operation) const;
//- Check IOstream status for given operation
// print IOstream state if error has occured and exit
void fatalCheck(const char* operation) const;
private:
//- Return true if stream has been opened
bool opened() const
{
return openClosed_ == OPENED;
}
// Private data
//- Return true if stream is closed
bool closed() const
{
return openClosed_ == CLOSED;
}
//- Name for any generic stream - normally treat as readonly
static fileName staticName_;
//- Return true if next operation might succeed
bool good() const
{