ENH: robuster handling of unknown stream/float formats

This commit is contained in:
Mark Olesen
2024-05-23 21:13:14 +02:00
parent b81fe70830
commit 1406f9ec26
3 changed files with 51 additions and 14 deletions

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018-2023 OpenCFD Ltd. Copyright (C) 2018-2024 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -70,7 +70,7 @@ Foam::IOstreamOption::floatFormatEnum
if (!fmtName.empty()) if (!fmtName.empty())
{ {
const auto iter = floatFormatNames.cfind(fmtName); auto iter = floatFormatNames.cfind(fmtName);
if (iter.good()) if (iter.good())
{ {
@ -79,10 +79,19 @@ Foam::IOstreamOption::floatFormatEnum
// Fall-through to warning // Fall-through to warning
WarningInFunction auto& err = WarningInFunction
<< "Unknown float format specifier '" << fmtName << "Unknown float format '" << fmtName << "' using ";
<< "' using '" << floatFormatNames[deflt]
<< "' from " << floatFormatNames << nl; iter = floatFormatNames.cfind(deflt);
if (iter.good())
{
err << '\'' << iter.key() << '\'';
}
else
{
err << "value=" << int(deflt);
}
err << " from " << floatFormatNames << nl;
} }
return deflt; return deflt;
@ -112,7 +121,7 @@ Foam::IOstreamOption::formatEnum
if (!fmtName.empty()) if (!fmtName.empty())
{ {
const auto iter = formatNames.cfind(fmtName); auto iter = formatNames.cfind(fmtName);
if (iter.good()) if (iter.good())
{ {
@ -121,10 +130,19 @@ Foam::IOstreamOption::formatEnum
// Fall-through to warning // Fall-through to warning
WarningInFunction auto& err = WarningInFunction
<< "Unknown stream format specifier '" << fmtName << "Unknown stream format '" << fmtName << "' using ";
<< "' using '" << formatNames[deflt]
<< "' from " << formatNames << nl; iter = formatNames.cfind(deflt);
if (iter.good())
{
err << '\'' << iter.key() << '\'';
}
else
{
err << "value=" << int(deflt);
}
err << " from " << formatNames << nl;
} }
return deflt; return deflt;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2023 OpenCFD Ltd. Copyright (C) 2017-2024 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -298,11 +298,16 @@ public:
const_iterator begin() const noexcept { return cbegin(); } const_iterator begin() const noexcept { return cbegin(); }
const_iterator end() const noexcept { return cend(); } const_iterator end() const noexcept { return cend(); }
//- Find the enumeration by name. //- Find key/value pair by enumeration name.
// Equal to cend() if not found, or test if good() // Equal to cend() if not found, or test if good()
inline const_iterator cfind(const word& key) const; inline const_iterator cfind(const word& key) const;
const_iterator find(const word& key) const { return cfind(key); } const_iterator find(const word& key) const { return cfind(key); }
//- Find key/value pair by enumeration value.
// Equal to cend() if not found, or test if good()
inline const_iterator cfind(const EnumType e) const;
const_iterator find(const EnumType e) const { return cfind(e); }
// Housekeeping // Housekeeping

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2017-2023 OpenCFD Ltd. Copyright (C) 2017-2024 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -252,6 +252,20 @@ Foam::Enum<EnumType>::cfind(const word& key) const
} }
template<class EnumType>
inline typename Foam::Enum<EnumType>::const_iterator
Foam::Enum<EnumType>::cfind(const EnumType e) const
{
const label idx = vals_.find(int(e));
return typename Enum<EnumType>::const_iterator
(
this,
(idx >= 0 ? idx : this->size())
);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class EnumType> template<class EnumType>