From 1406f9ec26b45d9cf721e98b604637e24b630ce8 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Thu, 23 May 2024 21:13:14 +0200 Subject: [PATCH] ENH: robuster handling of unknown stream/float formats --- src/OpenFOAM/db/options/IOstreamOption.C | 40 +++++++++++++++++------- src/OpenFOAM/primitives/enums/Enum.H | 9 ++++-- src/OpenFOAM/primitives/enums/EnumI.H | 16 +++++++++- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/OpenFOAM/db/options/IOstreamOption.C b/src/OpenFOAM/db/options/IOstreamOption.C index 32e588aaa2..ad60be85dd 100644 --- a/src/OpenFOAM/db/options/IOstreamOption.C +++ b/src/OpenFOAM/db/options/IOstreamOption.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2023 OpenCFD Ltd. + Copyright (C) 2018-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -70,7 +70,7 @@ Foam::IOstreamOption::floatFormatEnum if (!fmtName.empty()) { - const auto iter = floatFormatNames.cfind(fmtName); + auto iter = floatFormatNames.cfind(fmtName); if (iter.good()) { @@ -79,10 +79,19 @@ Foam::IOstreamOption::floatFormatEnum // Fall-through to warning - WarningInFunction - << "Unknown float format specifier '" << fmtName - << "' using '" << floatFormatNames[deflt] - << "' from " << floatFormatNames << nl; + auto& err = WarningInFunction + << "Unknown float format '" << fmtName << "' using "; + + iter = floatFormatNames.cfind(deflt); + if (iter.good()) + { + err << '\'' << iter.key() << '\''; + } + else + { + err << "value=" << int(deflt); + } + err << " from " << floatFormatNames << nl; } return deflt; @@ -112,7 +121,7 @@ Foam::IOstreamOption::formatEnum if (!fmtName.empty()) { - const auto iter = formatNames.cfind(fmtName); + auto iter = formatNames.cfind(fmtName); if (iter.good()) { @@ -121,10 +130,19 @@ Foam::IOstreamOption::formatEnum // Fall-through to warning - WarningInFunction - << "Unknown stream format specifier '" << fmtName - << "' using '" << formatNames[deflt] - << "' from " << formatNames << nl; + auto& err = WarningInFunction + << "Unknown stream format '" << fmtName << "' using "; + + iter = formatNames.cfind(deflt); + if (iter.good()) + { + err << '\'' << iter.key() << '\''; + } + else + { + err << "value=" << int(deflt); + } + err << " from " << formatNames << nl; } return deflt; diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index d990c3ca47..0ee27e49a2 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2023 OpenCFD Ltd. + Copyright (C) 2017-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -298,11 +298,16 @@ public: const_iterator begin() const noexcept { return cbegin(); } 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() inline const_iterator cfind(const word& key) const; 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 diff --git a/src/OpenFOAM/primitives/enums/EnumI.H b/src/OpenFOAM/primitives/enums/EnumI.H index 891dbd7da9..06b175e1fc 100644 --- a/src/OpenFOAM/primitives/enums/EnumI.H +++ b/src/OpenFOAM/primitives/enums/EnumI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2023 OpenCFD Ltd. + Copyright (C) 2017-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -252,6 +252,20 @@ Foam::Enum::cfind(const word& key) const } +template +inline typename Foam::Enum::const_iterator +Foam::Enum::cfind(const EnumType e) const +{ + const label idx = vals_.find(int(e)); + + return typename Enum::const_iterator + ( + this, + (idx >= 0 ? idx : this->size()) + ); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template