Commit 33f9ae50 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improvements to IOstreamOption

* Support default values for format/compress enum lookups.

  - Avoids situations where the preferred default format is not ASCII.
    For example, with dictionary input:

        format binar;

    The typing mistake would previously have caused formatEnum to
    default to ASCII. We can now properly control its behaviour.

        IOstream::formatEnum
        (
            dict.get<word>("format"), IOstream::BINARY
        );

    Allowing us to switch ascii/binary, using BINARY by default even in
    the case of spelling mistakes. The mistakes are flagged, but the
    return value can be non-ASCII.

* The format/compression lookup behave as pass-through if the lookup
  string is empty.

  - Allows the following to work without complaint

      IOstream::formatEnum
      (
          dict.getOrDefault("format", word::null), IOstream::BINARY
      );

  - Or use constructor-like failsafe method

      IOstream::formatEnum("format", dict, IOstream::BINARY);

  - Apply the same behaviour with setting stream format/compression
    from a word.

       is.format("binar");

    will emit a warning, but leave the stream format UNCHANGED

* Rationalize versionNumber construction

  - constexpr constructors where possible.
    Default construct is the "currentVersion"

  - Construct from token to shift the burden to versionNumber.
    Support token as argument to version().

    Now:

        is.version(headerDict.get<token>("version"));

    or failsafe constructor method

        is.version
        (
            IOstreamOption::versionNumber("version", headerDict)
        );

    Before (controlled input):

        is.version
        (
            IOstreamOption::versionNumber
            (
                headerDict.get<float>("version")
            )
        );

    Old, uncontrolled input - has been removed:

        is.version(headerDict.lookup("version"));

* improve consistency, default behaviour for IOstreamOption construct

  - constexpr constructors where possible

  - add copy construct with change of format.

  - construct IOstreamOption from streamFormat is now non-explicit.
    This is a commonly expected result with no ill-effects
parent bb53e8ad
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -66,20 +66,23 @@ int main(int argc, char *argv[])
if (true)
{
cout<<"IOstreamOption:" << sizeof(IOstreamOption) << nl;
cout<<"IOstream:" << sizeof(IOstream) << nl;
cout<<"Istream:" << sizeof(Istream) << nl;
cout<<"IPstream:" << sizeof(IPstream) << nl;
cout<<"Ostream:" << sizeof(Ostream) << nl;
cout<<"ISstream:" << sizeof(ISstream) << nl;
cout<<"OSstream:" << sizeof(OSstream) << nl;
cout<<"Ostream:" << sizeof(Ostream) << nl;
cout<<"IPstream:" << sizeof(IPstream) << nl;
cout<<"OPstream:" << sizeof(OPstream) << nl;
cout<<"OSstream:" << sizeof(OSstream) << nl;
}
{
nil x;
cout<<"nil:" << sizeof(x) << nl;
}
#if 0
{
argList x(argc, argv);
cout<<"argList:" << sizeof(x) << nl;
......@@ -87,6 +90,7 @@ int main(int argc, char *argv[])
TimePaths y(x);
cout<<"TimePaths:" << sizeof(y) << nl;
}
#endif
{
zero x;
cout<<"zero:" << sizeof(x) << nl;
......@@ -117,6 +121,7 @@ int main(int argc, char *argv[])
}
{
cout<<"short:" << sizeof(short) << nl;
cout<<"int:" << sizeof(int) << nl;
cout<<"long:" << sizeof(long) << nl;
cout<<"float:" << sizeof(float) << nl;
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -71,14 +71,7 @@ bool Foam::IOobject::readHeader(Istream& is)
{
const dictionary headerDict(is);
is.version
(
IOstreamOption::versionNumber
(
headerDict.get<float>("version")
)
);
is.version(headerDict.get<token>("version"));
is.format(headerDict.get<word>("format"));
headerClassName_ = headerDict.get<word>("class");
......
......@@ -1196,13 +1196,7 @@ Foam::label Foam::decomposedBlockData::numBlocks(const fileName& fName)
)
{
dictionary headerDict(is);
is.version
(
IOstreamOption::versionNumber
(
headerDict.get<float>("version")
)
);
is.version(headerDict.get<token>("version"));
is.format(headerDict.get<word>("format"));
// Obtain number of blocks directly
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -27,14 +27,13 @@ License
#include "IOstreamOption.H"
#include "error.H"
#include "dictionary.H"
#include "Enum.H"
#include "Switch.H"
// * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * * //
const Foam::IOstreamOption::versionNumber
Foam::IOstreamOption::currentVersion(2,0);
const Foam::IOstreamOption::versionNumber Foam::IOstreamOption::currentVersion;
const Foam::Enum
<
......@@ -50,55 +49,151 @@ Foam::IOstreamOption::formatNames
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::IOstreamOption::streamFormat
Foam::IOstreamOption::formatEnum(const word& formatName)
Foam::IOstreamOption::formatEnum
(
const word& formatName,
const streamFormat deflt
)
{
// Handle bad input graciously
if (formatNames.found(formatName))
// Handle bad input graciously. A no-op for an empty string
if (!formatName.empty())
{
return formatNames[formatName];
if (formatNames.found(formatName))
{
return formatNames[formatName];
}
// Fall-through to warning
WarningInFunction
<< "Unknown format specifier '" << formatName
<< "', using '" << formatNames[deflt] << "'\n";
}
WarningInFunction
<< "Unknown format specifier '" << formatName
<< "', using 'ascii'" << endl;
return deflt;
}
return streamFormat::ASCII;
Foam::IOstreamOption::streamFormat
Foam::IOstreamOption::formatEnum
(
const word& key,
const dictionary& dict,
const streamFormat deflt
)
{
return formatNames.getOrDefault(key, dict, deflt, true); // failsafe=true
}
Foam::IOstreamOption::compressionType
Foam::IOstreamOption::compressionEnum(const word& compName)
Foam::IOstreamOption::compressionEnum
(
const word& compName,
const compressionType deflt
)
{
// Handle bad input graciously
// Handle bad input graciously. A no-op for an empty string
const Switch sw = Switch::find(compName);
if (sw.good())
if (!compName.empty())
{
return
(
sw
? compressionType::COMPRESSED
: compressionType::UNCOMPRESSED
);
const Switch sw = Switch::find(compName);
if (sw.good())
{
return
(
sw
? compressionType::COMPRESSED
: compressionType::UNCOMPRESSED
);
}
// Fall-through to warning
WarningInFunction
<< "Unknown compression specifier '" << compName
<< "', using compression "
<< (deflt ? "on" : "off" ) << nl;
}
WarningInFunction
<< "Unknown compression specifier '" << compName
<< "', assuming no compression" << endl;
return deflt;
}
return compressionType::UNCOMPRESSED;
Foam::IOstreamOption::compressionType
Foam::IOstreamOption::compressionEnum
(
const word& key,
const dictionary& dict,
const compressionType deflt
)
{
return
(
Switch(key, dict, Switch(bool(deflt)), true) // failsafe=true
? compressionType::COMPRESSED
: compressionType::UNCOMPRESSED
);
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::IOstreamOption::versionNumber::versionNumber(const std::string& verNum)
:
versionNumber(readFloat(verNum))
{}
Foam::IOstreamOption::versionNumber::versionNumber(const token& tok)
:
versionNumber()
{
if (tok.isStringType())
{
(*this) = versionNumber(tok.stringToken());
}
else if (tok.isScalar())
{
(*this) = versionNumber(float(tok.scalarToken()));
}
else
{
WarningInFunction
<< "Wrong token for version - expected word/float, found "
<< tok.info() << nl;
}
}
Foam::IOstreamOption::versionNumber::versionNumber
(
const word& key,
const dictionary& dict
)
:
versionNumber()
{
token tok;
if (dict.readIfPresent<token>(key, tok, keyType::LITERAL))
{
(*this) = versionNumber(tok);
}
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const IOstreamOption::streamFormat& sf
const IOstreamOption::streamFormat& fmt
)
{
os << IOstreamOption::formatNames[sf];
os << IOstreamOption::formatNames[fmt];
return os;
}
......@@ -106,12 +201,13 @@ Foam::Ostream& Foam::operator<<
Foam::Ostream& Foam::operator<<
(
Ostream& os,
const IOstreamOption::versionNumber& vn
const IOstreamOption::versionNumber& ver
)
{
// Emit as char sequence instead of as individual characters
// in case this is needed for sending in parallel.
os << vn.str().c_str();
// Emit unquoted char sequence (eg, word)
// for correct behaviour when sending in parallel
os.writeQuoted(ver.str(), false);
return os;
}
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,7 +29,13 @@ Class
Description
The IOstreamOption is a simple container for options an IOstream
can normally have. For example, ascii/binary, uncompressed/compressed, ...
can normally have.
The format (ASCII | BINARY) is typically controlled by enumerated
names (ascii, binary).
The compression (UNCOMPRESSED | COMPRESSED) is typically controlled
by switch values (true/false, on/off, ...).
SourceFiles
IOstreamOption.C
......@@ -39,7 +45,6 @@ SourceFiles
#ifndef IOstreamOption_H
#define IOstreamOption_H
#include "scalar.H"
#include "word.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -47,8 +52,9 @@ SourceFiles
namespace Foam
{
// Forward declarations
class Ostream;
// Forward Declarations
class token;
class dictionary;
template<class EnumType> class Enum;
/*---------------------------------------------------------------------------*\
......@@ -64,7 +70,7 @@ public:
//- Data format (ascii | binary)
enum streamFormat : char
{
ASCII, //!< "ascii"
ASCII = 0, //!< "ascii" (normal default)
BINARY //!< "binary"
};
......@@ -86,6 +92,13 @@ public:
// Constructors
//- Default construct \em current version.
//- The value (2.0) corresponds to the \em current version.
constexpr versionNumber() noexcept
:
number_(20)
{}
//- Construct from major, number
constexpr versionNumber(int major, int minor) noexcept
:
......@@ -98,36 +111,40 @@ public:
number_(10*ver + 0.001) // Allow some rounding
{}
//- Construct from Istream by reading in a float.
// Non-explicit for convenience
versionNumber(Istream& is)
:
versionNumber(readFloat(is))
{}
//- Construct by parsing string "major.minor"
explicit versionNumber(const std::string& verNum)
:
versionNumber(readFloat(verNum))
{}
explicit versionNumber(const std::string& verNum);
//- Construct from token (float, word, string)
explicit versionNumber(const token& tok);
//- Failsafe construct from dictionary lookup.
versionNumber(const word& keyword, const dictionary& dict);
// Member Functions
//- Compare differences in the versions
// Negative when 'this' is less than other.
// Positive when 'this' is greater than other.
int compare(const versionNumber& other) const noexcept
{
return number_ - other.number_;
}
//- The canonical major/minor pair as an integer value.
inline int canonical() noexcept
int canonical() noexcept
{
return number_;
}
//- Return the major version number.
inline int getMajor() const noexcept
int getMajor() const noexcept
{
return int(number_ / 10);
}
//- Return the minor version number
inline int getMinor() const noexcept
int getMinor() const noexcept
{
return int(number_ % 10);
}
......@@ -140,45 +157,6 @@ public:
+ '.'
+ std::to_string(getMinor());
}
// Member Operators
//- Version number equality
bool operator==(const versionNumber& rhs) const noexcept
{
return number_ == rhs.number_;
}
//- Version number inequality
bool operator!=(const versionNumber& rhs) const noexcept
{
return number_ != rhs.number_;
}
//- Version number older than rhs
bool operator<(const versionNumber& rhs) const noexcept
{
return number_ < rhs.number_;
}
//- Version number is the same or older than rhs
bool operator<=(const versionNumber& rhs) const noexcept
{
return number_ <= rhs.number_;
}
//- Version number newer than rhs
bool operator>(const versionNumber& rhs) const noexcept
{
return number_ > rhs.number_;
}
//- Version number same or newer than rhs
bool operator>=(const versionNumber& rhs) const noexcept
{
return number_ >= rhs.number_;
}
};
......@@ -187,7 +165,7 @@ public:
//- Stream format names (ascii, binary)
static const Enum<streamFormat> formatNames;
//- The current version number
//- The current version number (2.0)
static const versionNumber currentVersion;
......@@ -195,7 +173,8 @@ private:
// Private Data
// NB: ordered with adjacent enums to minimize gaps
// NB: ordered with versionNumber first (short) and
// adjacent enums to minimize gaps
//- Stream version number (eg, 2.0 for current dictionary format)
versionNumber version_;
......@@ -211,58 +190,98 @@ public:
// Constructors
//- Construct null. (default: ASCII, uncompressed, currentVersion)
IOstreamOption() noexcept
//- Default construct (ASCII, UNCOMPRESSED, currentVersion)
//- or construct with format, compression
// \note non-explicit for convenient construction
constexpr IOstreamOption
(
streamFormat fmt = streamFormat::ASCII,
compressionType comp = compressionType::UNCOMPRESSED
) noexcept
:
version_(currentVersion),
format_(ASCII),
compression_(compressionType::UNCOMPRESSED)
version_(),
format_(fmt),
compression_(comp)
{}
//- Construct with format. (default: uncompressed, currentVersion)
explicit IOstreamOption(streamFormat format) noexcept
//- Construct from components (format, compression, version)
constexpr IOstreamOption
(
streamFormat fmt,
compressionType comp,
versionNumber ver
) noexcept
:
version_(currentVersion),
format_(format),
compression_(compressionType::UNCOMPRESSED)
version_(ver),
format_(fmt),
compression_(comp)
{}
//- Construct with format and compression, optionally with version.
IOstreamOption
//- Construct from components (format, version, compression)
constexpr IOstreamOption
(
streamFormat format,
compressionType compression,
versionNumber version=currentVersion
streamFormat fmt,
versionNumber ver,
compressionType comp = compressionType::UNCOMPRESSED
) noexcept
:
version_(version),
format_(format),
compression_(compression)
version_(ver),
format_(fmt),
compression_(comp)
{}
//- Construct with format, version, compression
IOstreamOption
(
streamFormat format,
versionNumber version,
compressionType compression
) noexcept
//- Copy construct with change of format
IOstreamOption(const IOstreamOption& opt, streamFormat fmt) noexcept
:
version_(version),
format_(format),
compression_(compression)
version_(opt.version_),
format_(fmt),
compression_(opt.compression_)
{}
// Static Member Functions
//- The stream format enum corresponding to the string
// Expected "ascii", "binary"
static streamFormat formatEnum(const word& formatName);
//- (ascii | binary).
//
// If the string is not recognized, emit warning and return default.
// Silent if the string itself is empty.
//
// \note Can be used as constructor substitute for the enumeration
static streamFormat formatEnum
(
const word& formatName,
const streamFormat deflt = streamFormat::ASCII
);
//- The compression enum corresponding to the string
// Expected "true", "false", "on", "off", etc.
static compressionType compressionEnum(const word& compName);
//- Failsafe construct streamFormat from optional dictionary lookup
static streamFormat formatEnum
(
const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary
const streamFormat deflt = streamFormat::ASCII
);
//- The compression enum corresponding to the string.
// Expects switch values (true/false, on/off, ...)
//
// If the string is not recognized, emit warning and return default.
// Silent if the string itself is empty.
//
// \note Can be used as constructor substitute for the enumeration
static compressionType compressionEnum
(
const word& compName,
const compressionType deflt = compressionType::UNCOMPRESSED