From d282d1a2855ab8132e0f55bd7714a7e3c705aedf Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 16 Jul 2020 09:10:12 +0200
Subject: [PATCH] STYLE: minor code reduction/simplification for tmp (#1775)

- combine reset() methods by adding a default parameter

- improve top-level visibility of empty/valid/get methods for symmetry
  symmetry with autoPtr, future adjustment
---
 src/OpenFOAM/memory/refCount/refCount.H |   4 +-
 src/OpenFOAM/memory/tmp/tmp.H           |  49 +++++-----
 src/OpenFOAM/memory/tmp/tmpI.H          | 113 +++++++-----------------
 src/OpenFOAM/memory/tmp/tmpNrc.H        |  54 +++++------
 src/OpenFOAM/memory/tmp/tmpNrcI.H       |  93 +++++--------------
 5 files changed, 98 insertions(+), 215 deletions(-)

diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H
index 303188bda58..281d6535909 100644
--- a/src/OpenFOAM/memory/refCount/refCount.H
+++ b/src/OpenFOAM/memory/refCount/refCount.H
@@ -50,7 +50,7 @@ namespace Foam
 
 class refCount
 {
-    // Private data
+    // Private Data
     int count_;
 
 
@@ -62,7 +62,7 @@ public:
 
     // Constructors
 
-        //- Construct null initializing count to 0
+        //- Default construct, initializing count to 0
         constexpr refCount() noexcept
         :
             count_(0)
diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H
index dacb813c5ff..ebd8f676c95 100644
--- a/src/OpenFOAM/memory/tmp/tmp.H
+++ b/src/OpenFOAM/memory/tmp/tmp.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -69,20 +69,21 @@ class tmp
         enum refType
         {
             PTR,    //!< Managing a pointer (ref-counted)
-            CREF    //!< Using a const-reference to an object
+            CREF    //!< Using (const) reference to an object
         };
 
-        //- The managed pointer or the address of const-reference object
+        //- The managed pointer or address of the object (reference)
         mutable T* ptr_;
 
-        //- The type (managed pointer | const-reference object)
+        //- The type (managed pointer | object reference)
         mutable refType type_;
 
 
     // Private Member Operators
 
         //- Increment the ref-count for a managed pointer
-        inline void operator++();
+        //- and check that it is not oversubscribed
+        inline void incrCount();
 
 
 public:
@@ -122,7 +123,7 @@ public:
 
     // Constructors
 
-        //- Construct with no managed pointer.
+        //- Default construct, no managed pointer.
         inline constexpr tmp() noexcept;
 
         //- Construct with no managed pointer.
@@ -159,15 +160,14 @@ public:
 
     // Query
 
-        //- True if this is a managed pointer (not a const reference)
-        inline bool isTmp() const noexcept;
+        //- True if a null managed pointer
+        bool empty() const noexcept { return !ptr_ && type_ == PTR; }
 
-        //- True if this is a non-null managed pointer
-        inline bool empty() const noexcept;
+        //- True for non-null managed pointer or an object reference
+        bool valid() const noexcept { return ptr_ || type_ == CREF; }
 
-        //- True if this is a non-null managed pointer,
-        //- or is a const object reference
-        inline bool valid() const noexcept;
+        //- True if this is a managed pointer (not a reference)
+        bool isTmp() const noexcept { return type_ == PTR; }
 
         //- True if this is a non-null managed pointer with a unique ref-count
         inline bool movable() const noexcept;
@@ -179,10 +179,10 @@ public:
     // Access
 
         //- Return pointer without nullptr checking.
-        inline T* get() noexcept;
+        T* get() noexcept { return ptr_; }
 
         //- Return const pointer without nullptr checking.
-        inline const T* get() const noexcept;
+        const T* get() const noexcept { return ptr_; }
 
         //- Return the const object reference or a const reference to the
         //- contents of a non-null managed pointer.
@@ -211,12 +211,8 @@ public:
         //- delete object and set pointer to nullptr
         inline void clear() const noexcept;
 
-        //- Release ownership of managed temporary object.
-        //  After this call no object is managed.
-        inline void reset() noexcept;
-
         //- Delete managed temporary object and set to new given pointer
-        inline void reset(T* p) noexcept;
+        inline void reset(T* p = nullptr) noexcept;
 
         //- Clear existing and transfer ownership.
         inline void reset(tmp<T>&& other) noexcept;
@@ -245,12 +241,8 @@ public:
         //  Fatal for a null managed pointer or if the object is const.
         inline T* operator->();
 
-        //- Is non-null managed pointer or const object reference : valid()
-        explicit inline operator bool() const noexcept;
-
-        //- Take ownership of the pointer.
-        //  Fatal for a null pointer, or when the pointer is non-unique.
-        inline void operator=(T* p);
+        //- Non-null managed pointer or an object reference : valid()
+        explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
 
         //- Transfer ownership of the managed pointer.
         //  Fatal for a null managed pointer or if the object is const.
@@ -259,8 +251,9 @@ public:
         //- Clear existing and transfer ownership.
         inline void operator=(tmp<T>&& other) noexcept;
 
-
-    // Housekeeping
+        //- Take ownership of the pointer.
+        //  Fatal for a null pointer, or when the pointer is non-unique.
+        inline void operator=(T* p);
 
         //- No assignment from literal nullptr.
         //  Consistent with run-time check for nullptr on assignment.
diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H
index a035432d813..10f370ae517 100644
--- a/src/OpenFOAM/memory/tmp/tmpI.H
+++ b/src/OpenFOAM/memory/tmp/tmpI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -32,7 +32,7 @@ License
 // * * * * * * * * * * * * * Private Member Operators  * * * * * * * * * * * //
 
 template<class T>
-inline void Foam::tmp<T>::operator++()
+inline void Foam::tmp<T>::incrCount()
 {
     ptr_->operator++();
 
@@ -138,7 +138,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t)
     {
         if (ptr_)
         {
-            operator++();
+            this->incrCount();
         }
         else
         {
@@ -166,7 +166,7 @@ inline Foam::tmp<T>::tmp(const tmp<T>& t, bool reuse)
             }
             else
             {
-                operator++();
+                this->incrCount();
             }
         }
         else
@@ -188,27 +188,6 @@ inline Foam::tmp<T>::~tmp()
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-template<class T>
-inline bool Foam::tmp<T>::isTmp() const noexcept
-{
-    return type_ == PTR;
-}
-
-
-template<class T>
-inline bool Foam::tmp<T>::empty() const noexcept
-{
-    return (!ptr_ && isTmp());
-}
-
-
-template<class T>
-inline bool Foam::tmp<T>::valid() const noexcept
-{
-    return (ptr_ || type_ == CREF);
-}
-
-
 template<class T>
 inline bool Foam::tmp<T>::movable() const noexcept
 {
@@ -223,20 +202,6 @@ inline Foam::word Foam::tmp<T>::typeName() const
 }
 
 
-template<class T>
-inline T* Foam::tmp<T>::get() noexcept
-{
-    return ptr_; // non-const pointer
-}
-
-
-template<class T>
-inline const T* Foam::tmp<T>::get() const noexcept
-{
-    return ptr_; // const pointer
-}
-
-
 template<class T>
 inline const T& Foam::tmp<T>::cref() const
 {
@@ -266,7 +231,7 @@ inline T& Foam::tmp<T>::ref() const
                 << abort(FatalError);
         }
     }
-    else
+    else // if (type_ == CREF)
     {
         FatalErrorInFunction
             << "Attempted non-const reference to const object from a "
@@ -311,10 +276,10 @@ inline T* Foam::tmp<T>::ptr() const
                 << abort(FatalError);
         }
 
-        T* ptr = ptr_;
+        T* p = ptr_;
         ptr_ = nullptr;
 
-        return ptr;
+        return p;
     }
 
     return ptr_->clone().ptr();
@@ -339,15 +304,6 @@ inline void Foam::tmp<T>::clear() const noexcept
 }
 
 
-template<class T>
-inline void Foam::tmp<T>::reset() noexcept
-{
-    clear();
-    ptr_ = nullptr;
-    type_ = PTR;
-}
-
-
 template<class T>
 inline void Foam::tmp<T>::reset(T* p) noexcept
 {
@@ -455,37 +411,6 @@ inline T* Foam::tmp<T>::operator->()
 }
 
 
-template<class T>
-inline Foam::tmp<T>::operator bool() const noexcept
-{
-    return (ptr_ || type_ == CREF);
-}
-
-
-template<class T>
-inline void Foam::tmp<T>::operator=(T* p)
-{
-    clear();
-
-    if (!p)
-    {
-        FatalErrorInFunction
-            << "Attempted copy of a deallocated " << typeName()
-            << abort(FatalError);
-    }
-    else if (!p->unique())
-    {
-        FatalErrorInFunction
-            << "Attempted assignment of a " << typeName()
-            << " to non-unique pointer"
-            << abort(FatalError);
-    }
-
-    ptr_ = p;
-    type_ = PTR;
-}
-
-
 template<class T>
 inline void Foam::tmp<T>::operator=(const tmp<T>& t)
 {
@@ -536,4 +461,28 @@ inline void Foam::tmp<T>::operator=(tmp<T>&& other) noexcept
 }
 
 
+template<class T>
+inline void Foam::tmp<T>::operator=(T* p)
+{
+    clear();
+
+    if (!p)
+    {
+        FatalErrorInFunction
+            << "Attempted copy of a deallocated " << typeName()
+            << abort(FatalError);
+    }
+    else if (!p->unique())
+    {
+        FatalErrorInFunction
+            << "Attempted assignment of a " << typeName()
+            << " to non-unique pointer"
+            << abort(FatalError);
+    }
+
+    ptr_ = p;
+    type_ = PTR;
+}
+
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H
index c997b3c8000..037b2ab97df 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrc.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrc.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2016 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -62,13 +62,13 @@ class tmpNrc
         enum refType
         {
             PTR,    //!< Managing a pointer (not ref-counted)
-            CREF    //!< Using a const-reference to an object
+            CREF    //!< Using (const) reference to an object
         };
 
-        //- The managed pointer or the address of const-reference object
+        //- The managed pointer or address of the object (reference)
         mutable T* ptr_;
 
-        //- The type (managed pointer | const-reference object)
+        //- The type (managed pointer | object reference)
         mutable refType type_;
 
 
@@ -109,7 +109,7 @@ public:
 
     // Constructors
 
-        //- Construct with no managed pointer.
+        //- Default construct, no managed pointer.
         inline constexpr tmpNrc() noexcept;
 
         //- Construct with no managed pointer.
@@ -139,17 +139,16 @@ public:
 
     // Query
 
-        //- True if this is a managed pointer (not a const reference)
-        inline bool isTmp() const noexcept;
+        //- True if a null managed pointer
+        bool empty() const noexcept { return !ptr_ && type_ == PTR; }
 
-        //- True if this is a non-null managed pointer
-        inline bool empty() const noexcept;
+        //- True for non-null managed pointer or an object reference
+        bool valid() const noexcept { return ptr_ || type_ == CREF; }
 
-        //- True if this is a non-null managed pointer,
-        //- or is a const object reference
-        inline bool valid() const noexcept;
+        //- True if this is a managed pointer (not a reference)
+        bool isTmp() const noexcept { return type_ == PTR; }
 
-        //- True if this is a non-null managed pointer with a unique ref-count
+        //- True if this is a non-null managed pointer
         inline bool movable() const noexcept;
 
         //- Return type-name of the tmp, constructed from type-name of T
@@ -159,10 +158,10 @@ public:
     // Access
 
         //- Return pointer without nullptr checking.
-        inline T* get() noexcept;
+        T* get() noexcept { return ptr_; }
 
         //- Return const pointer without nullptr checking.
-        inline const T* get() const noexcept;
+        const T* get() const noexcept { return ptr_; }
 
         //- Return the const object reference or a const reference to the
         //- contents of a non-null managed pointer.
@@ -191,12 +190,8 @@ public:
         //- delete object and set pointer to nullptr
         inline void clear() const noexcept;
 
-        //- Release ownership of managed temporary object.
-        //  After this call no object is managed.
-        inline void reset() noexcept;
-
         //- Delete managed temporary object and set to new given pointer
-        inline void reset(T* p) noexcept;
+        inline void reset(T* p = nullptr) noexcept;
 
         //- Clear existing and transfer ownership.
         inline void reset(tmpNrc<T>&& other) noexcept;
@@ -225,12 +220,8 @@ public:
         //  Fatal for a null managed pointer or if the object is const.
         inline T* operator->();
 
-        //- Is non-null managed pointer or const object reference : valid()
-        explicit inline operator bool() const noexcept;
-
-        //- Take ownership of the pointer.
-        //  Fatal for a null pointer, or when the pointer is non-unique.
-        inline void operator=(T* p);
+        //- Non-null managed pointer or an object reference : valid()
+        explicit operator bool() const noexcept { return ptr_ ||type_ == CREF; }
 
         //- Transfer ownership of the managed pointer.
         //  Fatal for a null managed pointer or if the object is const.
@@ -239,15 +230,16 @@ public:
         //- Clear existing and transfer ownership.
         inline void operator=(tmpNrc<T>&& other) noexcept;
 
-        //- Conversion to tmp
-        inline operator tmp<T>();
-
-
-    // Housekeeping
+        //- Take ownership of the pointer.
+        //  Fatal for a null pointer
+        inline void operator=(T* p);
 
         //- No assignment from literal nullptr.
         //  Consistent with run-time check for nullptr on assignment.
         void operator=(std::nullptr_t) = delete;
+
+        //- Conversion to tmp - releases pointer or copies reference
+        inline operator tmp<T>();
 };
 
 
diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H
index 3ff8924c16b..def9c1a67ed 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrcI.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2016-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -152,27 +152,6 @@ inline Foam::tmpNrc<T>::~tmpNrc()
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
-template<class T>
-inline bool Foam::tmpNrc<T>::isTmp() const noexcept
-{
-    return type_ == PTR;
-}
-
-
-template<class T>
-inline bool Foam::tmpNrc<T>::empty() const noexcept
-{
-    return (!ptr_ && isTmp());
-}
-
-
-template<class T>
-inline bool Foam::tmpNrc<T>::valid() const noexcept
-{
-    return (ptr_ || type_ == CREF);
-}
-
-
 template<class T>
 inline bool Foam::tmpNrc<T>::movable() const noexcept
 {
@@ -187,20 +166,6 @@ inline Foam::word Foam::tmpNrc<T>::typeName() const
 }
 
 
-template<class T>
-inline T* Foam::tmpNrc<T>::get() noexcept
-{
-    return ptr_; // non-const pointer
-}
-
-
-template<class T>
-inline const T* Foam::tmpNrc<T>::get() const noexcept
-{
-    return ptr_; // const pointer
-}
-
-
 template<class T>
 inline const T& Foam::tmpNrc<T>::cref() const
 {
@@ -230,7 +195,7 @@ inline T& Foam::tmpNrc<T>::ref() const
                 << abort(FatalError);
         }
     }
-    else
+    else // if (type_ == CREF)
     {
         FatalErrorInFunction
             << "Attempted non-const reference to const object from a "
@@ -268,10 +233,10 @@ inline T* Foam::tmpNrc<T>::ptr() const
                 << abort(FatalError);
         }
 
-        T* ptr = ptr_;
+        T* p = ptr_;
         ptr_ = nullptr;
 
-        return ptr;
+        return p;
     }
 
     return ptr_->clone().ptr();
@@ -289,15 +254,6 @@ inline void Foam::tmpNrc<T>::clear() const noexcept
 }
 
 
-template<class T>
-inline void Foam::tmpNrc<T>::reset() noexcept
-{
-    clear();
-    ptr_ = nullptr;
-    type_ = PTR;
-}
-
-
 template<class T>
 inline void Foam::tmpNrc<T>::reset(T* p) noexcept
 {
@@ -405,30 +361,6 @@ inline T* Foam::tmpNrc<T>::operator->()
 }
 
 
-template<class T>
-inline Foam::tmpNrc<T>::operator bool() const noexcept
-{
-    return (ptr_ || type_ == CREF);
-}
-
-
-template<class T>
-inline void Foam::tmpNrc<T>::operator=(T* p)
-{
-    clear();
-
-    if (!p)
-    {
-        FatalErrorInFunction
-            << "Attempted copy of a deallocated " << typeName()
-            << abort(FatalError);
-    }
-
-    ptr_ = p;
-    type_ = PTR;
-}
-
-
 template<class T>
 inline void Foam::tmpNrc<T>::operator=(const tmpNrc<T>& t)
 {
@@ -479,6 +411,23 @@ inline void Foam::tmpNrc<T>::operator=(tmpNrc<T>&& other) noexcept
 }
 
 
+template<class T>
+inline void Foam::tmpNrc<T>::operator=(T* p)
+{
+    clear();
+
+    if (!p)
+    {
+        FatalErrorInFunction
+            << "Attempted copy of a deallocated " << typeName()
+            << abort(FatalError);
+    }
+
+    ptr_ = p;
+    type_ = PTR;
+}
+
+
 template<class T>
 inline Foam::tmpNrc<T>::operator tmp<T>()
 {
-- 
GitLab