From c45c649d15b394584c0838543fdb9483d52f2b0e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
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 25177e5c670..9342fb122c5 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<class StringType>
-        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 c7ee333ea68..9447a4833dc 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<class StringType>
 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 d402678d8e0..e3e6875ae1f 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<ISstream*>(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<Foam::ISstream*>(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 791dfbfb16c..546d5db0c79 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<scalarIOField>(lookupName);
+        mesh().thisDb().getObjectPtr<scalarIOField>
+        (
+            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 93cc67fadcf..bda9620c394 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<word>("residual", "initialResidual:p")
+        dict.getOrDefault<word>
+        (
+            "residual",
+            IOobject::scopedName("initialResidual", "p")
+        )
     ),
     UName_
     (
diff --git a/src/functionObjects/utilities/solverInfo/solverInfo.C b/src/functionObjects/utilities/solverInfo/solverInfo.C
index 296ae571af7..8808cfa5606 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<IOField<scalar>>(residualName))
     {
-- 
GitLab