From c45c649d15b394584c0838543fdb9483d52f2b0e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 9 Nov 2021 09:27:26 +0100 Subject: [PATCH] ENH: portable scoping char for solver info names (#2224, #1675) COMP: implicit cast scope name to C++-string in IOobject::scopedName - handles 'const char*' and allows a check for an empty scope name COMP: avoid potential name conflict in local function (Istream) - reportedly some resolution issues (unconfirmed) with Fujitsu clang --- src/OpenFOAM/db/IOobject/IOobject.H | 9 ++++--- src/OpenFOAM/db/IOobject/IOobjectI.H | 9 ++----- src/OpenFOAM/db/IOstreams/IOstreams/Istream.C | 24 ++++++++----------- .../matrices/lduMatrix/lduMatrix/lduMatrix.C | 19 ++++++--------- .../stabilityBlendingFactor.C | 6 ++++- .../utilities/solverInfo/solverInfo.C | 5 +++- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index 25177e5c67..9342fb122c 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -314,9 +314,12 @@ public: static word member(const word& name); //- Create scope:name or scope_name string - // An empty scope or name is ignored. - template - static inline word scopedName(StringType scope, const word& name); + // An empty scope is ignored. + static inline word scopedName + ( + const std::string& scope, + const word& name + ); //- Return the IOobject, but also consider an alternative file name. // diff --git a/src/OpenFOAM/db/IOobject/IOobjectI.H b/src/OpenFOAM/db/IOobject/IOobjectI.H index c7ee333ea6..9447a4833d 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectI.H +++ b/src/OpenFOAM/db/IOobject/IOobjectI.H @@ -43,18 +43,13 @@ inline Foam::word Foam::IOobject::groupName } -template inline Foam::word Foam::IOobject::scopedName ( - StringType scope, + const std::string& scope, const word& name ) { - if (name.empty()) - { - return scope; - } - else if (scope.empty()) + if (scope.empty()) { return name; } diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C index d402678d8e..e3e6875ae1 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams/Istream.C @@ -31,22 +31,18 @@ License // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // -namespace Foam +namespace { - // Return the current get position for std input stream - static inline std::streampos tellg(Istream* isptr) - { - ISstream* sptr = dynamic_cast(isptr); - if (sptr) - { - return sptr->stdStream().tellg(); - } - - return 0; - } +// The current get position (std::istream only) +inline std::streampos stream_tellg(Foam::Istream* isptr) +{ + auto* sptr = dynamic_cast(isptr); + return sptr ? sptr->stdStream().tellg() : std::streampos(0); } +} // End anonymous namespace + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // @@ -141,7 +137,7 @@ bool Foam::Istream::readEnd(const char* funcName) << "Expected a '" << token::END_LIST << "' while reading " << funcName << ", found " << delimiter.info() - << " at stream position " << tellg(this) << nl + << " at stream position " << stream_tellg(this) << nl << exit(FatalIOError); } @@ -182,7 +178,7 @@ char Foam::Istream::readEndList(const char* funcName) << "' or a '" << token::END_BLOCK << "' while reading " << funcName << ", found " << delimiter.info() - << " at stream position " << tellg(this) << nl + << " at stream position " << stream_tellg(this) << nl << exit(FatalIOError); return '\0'; diff --git a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrix.C b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrix.C index 791dfbfb16..546d5db0c7 100644 --- a/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrix.C +++ b/src/OpenFOAM/matrices/lduMatrix/lduMatrix/lduMatrix.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019 OpenCFD Ltd. + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -330,18 +330,13 @@ void Foam::lduMatrix::setResidualField return; } - word lookupName; - if (initial) - { - lookupName = word("initialResidual:" + fieldName); - } - else - { - lookupName = word("residual:" + fieldName); - } - scalarIOField* residualPtr = - mesh().thisDb().getObjectPtr(lookupName); + mesh().thisDb().getObjectPtr + ( + initial + ? IOobject::scopedName("initialResidual", fieldName) + : IOobject::scopedName("residual", fieldName) + ); if (residualPtr) { diff --git a/src/functionObjects/field/stabilityBlendingFactor/stabilityBlendingFactor.C b/src/functionObjects/field/stabilityBlendingFactor/stabilityBlendingFactor.C index 93cc67fadc..bda9620c39 100644 --- a/src/functionObjects/field/stabilityBlendingFactor/stabilityBlendingFactor.C +++ b/src/functionObjects/field/stabilityBlendingFactor/stabilityBlendingFactor.C @@ -484,7 +484,11 @@ Foam::functionObjects::stabilityBlendingFactor::stabilityBlendingFactor ), residualName_ ( - dict.getOrDefault("residual", "initialResidual:p") + dict.getOrDefault + ( + "residual", + IOobject::scopedName("initialResidual", "p") + ) ), UName_ ( diff --git a/src/functionObjects/utilities/solverInfo/solverInfo.C b/src/functionObjects/utilities/solverInfo/solverInfo.C index 296ae571af..8808cfa560 100644 --- a/src/functionObjects/utilities/solverInfo/solverInfo.C +++ b/src/functionObjects/utilities/solverInfo/solverInfo.C @@ -86,7 +86,10 @@ void Foam::functionObjects::solverInfo::createResidualField return; } - const word residualName("initialResidual:" + fieldName); + const word residualName + ( + IOobject::scopedName("initialResidual", fieldName) + ); if (!mesh_.foundObject>(residualName)) {