From cb6f90879842fecc0b8bf16eccefa298b53418f7 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 3 May 2022 10:33:31 +0200
Subject: [PATCH] STYLE: IOobject/regIOobject - noexcept methods, isolate local
 functions

- local writeHeaderEntry helper was not marked as file-scope static.

- use do/while to simplify handling of padding spaces

ENH: IOobject - copy construct, resetting name and local component

- when copying with a new local component, this is simpler than
  constructing from all of the components, which was previously the
  only possibility for setting a new local component.
---
 src/OpenFOAM/db/IOobject/IOobject.C           | 57 ++++++++++++-------
 src/OpenFOAM/db/IOobject/IOobject.H           | 53 +++++++++--------
 src/OpenFOAM/db/IOobject/IOobjectI.H          |  4 +-
 src/OpenFOAM/db/IOobject/IOobjectReadHeader.C |  7 ++-
 .../db/IOobject/IOobjectWriteHeader.C         | 31 +++++-----
 src/OpenFOAM/db/IOobjectList/IOobjectList.C   |  8 +--
 src/OpenFOAM/db/IOobjectList/IOobjectList.H   |  4 +-
 .../decomposedBlockDataHeader.C               | 31 +++++-----
 src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C | 14 ++---
 src/OpenFOAM/db/regIOobject/regIOobject.H     | 31 +++++-----
 src/OpenFOAM/db/regIOobject/regIOobjectI.H    | 20 ++++---
 11 files changed, 143 insertions(+), 117 deletions(-)

diff --git a/src/OpenFOAM/db/IOobject/IOobject.C b/src/OpenFOAM/db/IOobject/IOobject.C
index ad2be69cbd1..0654d28772e 100644
--- a/src/OpenFOAM/db/IOobject/IOobject.C
+++ b/src/OpenFOAM/db/IOobject/IOobject.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2016-2021 OpenCFD Ltd.
+    Copyright (C) 2016-2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -312,8 +312,8 @@ Foam::IOobject::IOobject
     const word& name,
     const fileName& instance,
     const objectRegistry& registry,
-    readOption ro,
-    writeOption wo,
+    readOption rOpt,
+    writeOption wOpt,
     bool registerObject,
     bool globalObject
 )
@@ -323,11 +323,11 @@ Foam::IOobject::IOobject
     note_(),
     instance_(instance),
     local_(),
-    rOpt_(ro),
-    wOpt_(wo),
+    rOpt_(rOpt),
+    wOpt_(wOpt),
     registerObject_(registerObject),
     globalObject_(globalObject),
-    objState_(GOOD),
+    objState_(objectState::GOOD),
     sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
     sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
 
@@ -349,8 +349,8 @@ Foam::IOobject::IOobject
     const fileName& instance,
     const fileName& local,
     const objectRegistry& registry,
-    readOption ro,
-    writeOption wo,
+    readOption rOpt,
+    writeOption wOpt,
     bool registerObject,
     bool globalObject
 )
@@ -360,11 +360,11 @@ Foam::IOobject::IOobject
     note_(),
     instance_(instance),
     local_(local),
-    rOpt_(ro),
-    wOpt_(wo),
+    rOpt_(rOpt),
+    wOpt_(wOpt),
     registerObject_(registerObject),
     globalObject_(globalObject),
-    objState_(GOOD),
+    objState_(objectState::GOOD),
     sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
     sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
 
@@ -384,8 +384,8 @@ Foam::IOobject::IOobject
 (
     const fileName& path,
     const objectRegistry& registry,
-    readOption ro,
-    writeOption wo,
+    readOption rOpt,
+    writeOption wOpt,
     bool registerObject,
     bool globalObject
 )
@@ -395,11 +395,11 @@ Foam::IOobject::IOobject
     note_(),
     instance_(),
     local_(),
-    rOpt_(ro),
-    wOpt_(wo),
+    rOpt_(rOpt),
+    wOpt_(wOpt),
     registerObject_(registerObject),
     globalObject_(globalObject),
-    objState_(GOOD),
+    objState_(objectState::GOOD),
     sizeofLabel_(static_cast<unsigned char>(sizeof(label))),
     sizeofScalar_(static_cast<unsigned char>(sizeof(scalar))),
 
@@ -471,14 +471,27 @@ Foam::IOobject::IOobject
 Foam::IOobject::IOobject
 (
     const IOobject& io,
-    readOption ro,
-    writeOption wo
+    const word& name,
+    const fileName& local
+)
+:
+    IOobject(io, name)
+{
+    local_ = local;
+}
+
+
+Foam::IOobject::IOobject
+(
+    const IOobject& io,
+    readOption rOpt,
+    writeOption wOpt
 )
 :
     IOobject(io)
 {
-    rOpt_ = ro;
-    wOpt_ = wo;
+    rOpt_ = rOpt;
+    wOpt_ = wOpt;
 }
 
 
@@ -589,7 +602,7 @@ Foam::fileName Foam::IOobject::globalFilePath
 
 void Foam::IOobject::setBad(const string& s)
 {
-    if (objState_ != GOOD)
+    if (objState_ != objectState::GOOD)
     {
         FatalErrorInFunction
             << "Recurrent failure for object " << s
@@ -602,7 +615,7 @@ void Foam::IOobject::setBad(const string& s)
             << "Broken object " << s << info() << endl;
     }
 
-    objState_ = BAD;
+    objState_ = objectState::BAD;
 }
 
 
diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H
index ec18145cbe9..bd90938a0fa 100644
--- a/src/OpenFOAM/db/IOobject/IOobject.H
+++ b/src/OpenFOAM/db/IOobject/IOobject.H
@@ -270,7 +270,7 @@ public:
             return bannerEnabled_;
         }
 
-        //- Enable/disable an output file banner
+        //- Enable/disable output file banner
         //  \return the previous value
         static bool bannerEnabled(bool on) noexcept
         {
@@ -360,8 +360,8 @@ public:
             const word& name,
             const fileName& instance,
             const objectRegistry& registry,
-            readOption r=NO_READ,
-            writeOption w=NO_WRITE,
+            readOption rOpt = NO_READ,
+            writeOption wOpt = NO_WRITE,
             bool registerObject = true,
             bool globalObject = false
         );
@@ -373,8 +373,8 @@ public:
             const fileName& instance,
             const fileName& local,
             const objectRegistry& registry,
-            readOption r=NO_READ,
-            writeOption w=NO_WRITE,
+            readOption rOpt = NO_READ,
+            writeOption wOpt = NO_WRITE,
             bool registerObject = true,
             bool globalObject = false
         );
@@ -389,33 +389,24 @@ public:
         (
             const fileName& path,
             const objectRegistry& registry,
-            readOption r=NO_READ,
-            writeOption w=NO_WRITE,
+            readOption rOpt = NO_READ,
+            writeOption wOpt = NO_WRITE,
             bool registerObject = true,
             bool globalObject = false
         );
 
         //- Copy construct, resetting registry
-        IOobject
-        (
-            const IOobject& io,
-            const objectRegistry& registry
-        );
+        IOobject(const IOobject& io, const objectRegistry& registry);
 
         //- Copy construct, resetting name
-        IOobject
-        (
-            const IOobject& io,
-            const word& name
-        );
+        IOobject(const IOobject& io, const word& name);
+
+        //- Copy construct, resetting name and local component
+        IOobject(const IOobject& io, const word& name, const fileName& local);
+
+        //- Copy construct, resetting read/write options
+        IOobject(const IOobject& io, readOption rOpt, writeOption wOpt);
 
-        //- Copy construct, resetting io options
-        IOobject
-        (
-            const IOobject& io,
-            readOption,
-            writeOption
-        );
 
         //- Clone
         autoPtr<IOobject> clone() const
@@ -440,7 +431,7 @@ public:
         //- Return Time associated with the objectRegistry
         const Time& time() const;
 
-        //- Return name
+        //- Return the object name
         inline const word& name() const noexcept;
 
         //- Return name of the class name read from header
@@ -452,10 +443,10 @@ public:
         //- Return the optional note
         inline const string& note() const noexcept;
 
-        //- Return non-constant access to the optional note
+        //- Modifiable access to the optional note
         inline string& note() noexcept;
 
-        //- Rename
+        //- Rename the object
         virtual void rename(const word& newName)
         {
             name_ = newName;
@@ -522,14 +513,19 @@ public:
         //- Return member (name without the extension)
         inline word member() const;
 
+        //- Return the Time::rootPath()
         const fileName& rootPath() const;
 
+        //- Return the Time::caseName()
         const fileName& caseName() const;
 
+        //- Read access to instance path component
         inline const fileName& instance() const noexcept;
 
+        //- Write access to instance path component
         inline fileName& instance() noexcept;
 
+        //- Read access to local path component
         inline const fileName& local() const noexcept;
 
         //- The complete path
@@ -633,8 +629,10 @@ public:
 
     // Error Handling
 
+        //- Did last readHeader() succeed?
         inline bool good() const noexcept;
 
+        //- Did last readHeader() fail?
         inline bool bad() const noexcept;
 
 
@@ -650,6 +648,7 @@ public:
 
     // Member Operators
 
+        //- Copy assignment, copies all values (except the registry)
         void operator=(const IOobject& io);
 
 
diff --git a/src/OpenFOAM/db/IOobject/IOobjectI.H b/src/OpenFOAM/db/IOobject/IOobjectI.H
index 7bbd27c06cb..ab0a0d725bd 100644
--- a/src/OpenFOAM/db/IOobject/IOobjectI.H
+++ b/src/OpenFOAM/db/IOobject/IOobjectI.H
@@ -227,13 +227,13 @@ inline Foam::fileName Foam::IOobject::objectPath() const
 
 inline bool Foam::IOobject::good() const noexcept
 {
-    return objState_ == GOOD;
+    return objState_ == objectState::GOOD;
 }
 
 
 inline bool Foam::IOobject::bad() const noexcept
 {
-    return objState_ == BAD;
+    return objState_ == objectState::BAD;
 }
 
 
diff --git a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C
index de0efa053b3..ca0d3044332 100644
--- a/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C
+++ b/src/OpenFOAM/db/IOobject/IOobjectReadHeader.C
@@ -128,16 +128,17 @@ bool Foam::IOobject::readHeader(dictionary& headerDict, Istream& is)
     }
 
     // Check stream is still OK
-    objState_ = (is.good() ? GOOD : BAD);
+    objState_ = (is.good() ? objectState::GOOD : objectState::BAD);
 
     if (IOobject::debug)
     {
         Info<< " .... read - state: "
-            << (objState_ == GOOD ? "good" : "bad") << endl;
+            << (objState_ == objectState::GOOD ? "good" : "bad")
+            << endl;
 
     }
 
-    if (objState_ == BAD)
+    if (objState_ == objectState::BAD)
     {
         if
         (
diff --git a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C
index 1896920238f..8fa38748287 100644
--- a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C
+++ b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2016-2021 OpenCFD Ltd.
+    Copyright (C) 2016-2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -36,24 +36,27 @@ License
 namespace Foam
 {
 
-inline void writeSpaces(Ostream& os, label nSpaces)
+// Like Ostream::writeEntry, but with fewer spaces
+template<class T>
+static inline void writeHeaderEntry
+(
+    Ostream& os,
+    const word& key,
+    const T& value
+)
 {
-    if (nSpaces < 1)
-    {
-        nSpaces = 1;
-    }
-    while (nSpaces--)
+    os.indent();
+    os.write(key);
+
+    label padding = (12 - label(key.size()));
+
+    // Write padding spaces (always at least one)
+    do
     {
         os.write(char(token::SPACE));
     }
-}
+    while (--padding > 0);
 
-// Similar to writeEntry, but with fewer spaces
-template<class T>
-inline void writeHeaderEntry(Ostream& os, const word& key, const T& value)
-{
-    os << indent << key;
-    writeSpaces(os, 12 - label(key.size()));
     os << value << char(token::END_STATEMENT) << nl;
 }
 
diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C
index 876ce1f0078..e42d8125dcf 100644
--- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C
+++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C
@@ -107,8 +107,8 @@ Foam::IOobjectList::IOobjectList
     const objectRegistry& db,
     const fileName& instance,
     const fileName& local,
-    IOobject::readOption r,
-    IOobject::writeOption w,
+    IOobject::readOption rOpt,
+    IOobject::writeOption wOpt,
     bool registerObject
 )
 :
@@ -131,8 +131,8 @@ Foam::IOobjectList::IOobjectList
             newInstance,
             local,
             db,
-            r,
-            w,
+            rOpt,
+            wOpt,
             registerObject
         );
 
diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H
index e9350f5fa6f..139e6ed0b9d 100644
--- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H
+++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H
@@ -164,8 +164,8 @@ public:
             const objectRegistry& db,
             const fileName& instance,
             const fileName& local = "",
-            IOobject::readOption r = IOobject::MUST_READ,
-            IOobject::writeOption w = IOobject::NO_WRITE,
+            IOobject::readOption rOpt = IOobject::MUST_READ,
+            IOobject::writeOption wOpt = IOobject::NO_WRITE,
             bool registerObject = true
         );
 
diff --git a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockDataHeader.C b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockDataHeader.C
index e4bdbf47324..97787117751 100644
--- a/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockDataHeader.C
+++ b/src/OpenFOAM/db/IOobjects/decomposedBlockData/decomposedBlockDataHeader.C
@@ -5,7 +5,7 @@
     \\  /    A nd           | www.openfoam.com
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
-    Copyright (C) 2021 OpenCFD Ltd.
+    Copyright (C) 2021-2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -36,24 +36,27 @@ License
 namespace Foam
 {
 
-inline void writeSpaces(Ostream& os, label nSpaces)
+// Like Ostream::writeEntry, but with fewer spaces
+template<class T>
+static inline void writeHeaderEntry
+(
+    Ostream& os,
+    const word& key,
+    const T& value
+)
 {
-    if (nSpaces < 1)
-    {
-        nSpaces = 1;
-    }
-    while (nSpaces--)
+    os.indent();
+    os.write(key);
+
+    label padding = (12 - label(key.size()));
+
+    // Write padding spaces (always at least one)
+    do
     {
         os.write(char(token::SPACE));
     }
-}
+    while (--padding > 0);
 
-// Similar to writeEntry, but with fewer spaces
-template<class T>
-inline void writeHeaderEntry(Ostream& os, const word& key, const T& value)
-{
-    os << indent << key;
-    writeSpaces(os, 12 - label(key.size()));
     os << value << char(token::END_STATEMENT) << nl;
 }
 
diff --git a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
index cbafe31b240..1f650358833 100644
--- a/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
+++ b/src/OpenFOAM/db/IOstreams/IOstreams/Ostream.C
@@ -65,24 +65,20 @@ Foam::Ostream& Foam::Ostream::writeKeyword(const keyType& kw)
         return *this;
     }
 
-    label nSpaces = entryIndentation_ - label(kw.size());
+    label padding = (entryIndentation_ - label(kw.size()));
 
     // Account for quotes surrounding pattern
     if (kw.isPattern())
     {
-        nSpaces -= 2;
+        padding -= 2;
     }
 
-    // Could also increment by indentSize_ ...
-    if (nSpaces < 1)
-    {
-        nSpaces = 1;
-    }
-
-    while (nSpaces--)
+    // Write padding spaces (always at least one)
+    do
     {
         write(char(token::SPACE));
     }
+    while (--padding > 0);
 
     return *this;
 }
diff --git a/src/OpenFOAM/db/regIOobject/regIOobject.H b/src/OpenFOAM/db/regIOobject/regIOobject.H
index a02e5f9170a..cb0dff3f563 100644
--- a/src/OpenFOAM/db/regIOobject/regIOobject.H
+++ b/src/OpenFOAM/db/regIOobject/regIOobject.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2021 OpenCFD Ltd.
+    Copyright (C) 2018-2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -38,8 +38,8 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef regIOobject_H
-#define regIOobject_H
+#ifndef Foam_regIOobject_H
+#define Foam_regIOobject_H
 
 #include "IOobject.H"
 #include "refPtr.H"
@@ -174,8 +174,13 @@ public:
             //- Add file watch on object (if registered and READ_IF_MODIFIED)
             virtual void addWatch();
 
+            //- Query the registered state (ie, has been checked in).
+            //- This is not necessarily the same as registerObject(),
+            //- which is just a stated preference.
+            inline bool registered() const noexcept;
+
             //- Is this object owned by the registry?
-            inline bool ownedByRegistry() const;
+            inline bool ownedByRegistry() const noexcept;
 
             //- Register object with its registry
             //- and transfer ownership to the registry.
@@ -223,18 +228,18 @@ public:
             template<class Type>
             inline static Type& store(tmp<Type>&& ptr);
 
-            //- Release ownership of this object from its registry
-            //  \param unregister optionally set as non-registered
-            inline void release(const bool unregister = false);
+            //- Set object as \b not ownedByRegistry
+            //  \param unregister optionally set as non-registered too
+            inline void release(const bool unregister = false) noexcept;
 
 
         // Dependency Checking
 
             //- Event number at last update.
-            inline label eventNo() const;
+            inline label eventNo() const noexcept;
 
             //- Event number at last update.
-            inline label& eventNo();
+            inline label& eventNo() noexcept;
 
             //- Return true if up-to-date with respect to given object
             bool upToDate(const regIOobject&) const;
@@ -318,11 +323,11 @@ public:
             //  \return index of watch
             virtual label addWatch(const fileName&);
 
-            //- Return file-monitoring handles
-            inline const labelList& watchIndices() const;
+            //- Read access to file-monitoring handles
+            inline const labelList& watchIndices() const noexcept;
 
-            //- Return file-monitoring handles
-            inline labelList& watchIndices();
+            //- Write access to file-monitoring handles
+            inline labelList& watchIndices() noexcept;
 
             //- Return true if the object's file (or files for objectRegistry)
             //- have been modified. (modified state is cached by Time)
diff --git a/src/OpenFOAM/db/regIOobject/regIOobjectI.H b/src/OpenFOAM/db/regIOobject/regIOobjectI.H
index 144799397e9..f141544439b 100644
--- a/src/OpenFOAM/db/regIOobject/regIOobjectI.H
+++ b/src/OpenFOAM/db/regIOobject/regIOobjectI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2015 OpenFOAM Foundation
-    Copyright (C) 2018-2021 OpenCFD Ltd.
+    Copyright (C) 2018-2022 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -28,7 +28,13 @@ License
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-inline bool Foam::regIOobject::ownedByRegistry() const
+inline bool Foam::regIOobject::registered() const noexcept
+{
+    return registered_;
+}
+
+
+inline bool Foam::regIOobject::ownedByRegistry() const noexcept
 {
     return ownedByRegistry_;
 }
@@ -172,7 +178,7 @@ inline Type& Foam::regIOobject::store(tmp<Type>&& ptr)
 }
 
 
-inline void Foam::regIOobject::release(const bool unregister)
+inline void Foam::regIOobject::release(const bool unregister) noexcept
 {
     ownedByRegistry_ = false;
     if (unregister)
@@ -182,24 +188,24 @@ inline void Foam::regIOobject::release(const bool unregister)
 }
 
 
-inline Foam::label Foam::regIOobject::eventNo() const
+inline Foam::label Foam::regIOobject::eventNo() const noexcept
 {
     return eventNo_;
 }
 
-inline Foam::label& Foam::regIOobject::eventNo()
+inline Foam::label& Foam::regIOobject::eventNo() noexcept
 {
     return eventNo_;
 }
 
 
-inline const Foam::labelList& Foam::regIOobject::watchIndices() const
+inline const Foam::labelList& Foam::regIOobject::watchIndices() const noexcept
 {
     return watchIndices_;
 }
 
 
-inline Foam::labelList& Foam::regIOobject::watchIndices()
+inline Foam::labelList& Foam::regIOobject::watchIndices() noexcept
 {
     return watchIndices_;
 }
-- 
GitLab