From 08335beb6fbf5c46c87c9346c45d91532c2d45d9 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 20 Dec 2018 17:29:51 +0100
Subject: [PATCH] ENH: add get() accessor to tmp classes

- similar to autoPtr and unique_ptr. Returns the pointer value without
  any checks. This provides a simple way for use to use either
  an autoPtr or a tmp for local memory management without accidentally
  stealing the pointer.

  Eg,

     volVectorField* ptr;
     tmp<volVectorField> tempField;

     if (someField.valid())
     {
         ptr = someField.get();
     }
     else
     {
         tempField.reset(new volVectorField(....));
         ptr = tmpField.get();
     }

     const volVectorField& withField = *ptr;

STYLE: make more tmp methods noexcept
---
 applications/test/tmp/Test-tmp.C        | 14 ++++++++++---
 src/OpenFOAM/memory/refCount/refCount.H | 10 ++++-----
 src/OpenFOAM/memory/tmp/tmp.H           | 20 +++++++++++-------
 src/OpenFOAM/memory/tmp/tmpI.H          | 28 ++++++++++++++++++-------
 src/OpenFOAM/memory/tmp/tmpNrc.H        | 20 +++++++++++-------
 src/OpenFOAM/memory/tmp/tmpNrcI.H       | 28 ++++++++++++++++++-------
 6 files changed, 84 insertions(+), 36 deletions(-)

diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C
index 73ba2360634..80691b3393e 100644
--- a/applications/test/tmp/Test-tmp.C
+++ b/applications/test/tmp/Test-tmp.C
@@ -56,16 +56,18 @@ int main()
     }
 
     {
-        tmp<scalarField> tfld1 = tmp<scalarField>::New(20, Zero);
+        auto tfld1 = tmp<scalarField>::New(20, Zero);
 
         Info<< "tmp refCount = " << tfld1->count() << nl;
         if (tfld1.valid())
         {
             Info<<"tmp: " << tfld1() << nl;
         }
-    }
 
-    {
+        Info<<"tmp addr: " << long(tfld1.get()) << nl;
+
+        // Hold on to the old content for a bit
+
         tmp<scalarField> tfld2 =
             tmp<scalarField>::NewFrom<myScalarField>(20, Zero);
 
@@ -74,6 +76,12 @@ int main()
         {
             Info<<"tmp: " << tfld2() << nl;
         }
+
+        Info<<"tmp addr: " << long(tfld2.get()) << nl;
+
+        tfld2.clear();
+
+        Info<<"after clear: " << long(tfld2.get()) << nl;
     }
 
     Info<< "\nEnd" << endl;
diff --git a/src/OpenFOAM/memory/refCount/refCount.H b/src/OpenFOAM/memory/refCount/refCount.H
index ba4a362a921..a5e1c97924e 100644
--- a/src/OpenFOAM/memory/refCount/refCount.H
+++ b/src/OpenFOAM/memory/refCount/refCount.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  |
+     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -84,25 +84,25 @@ public:
     // Member Operators
 
         //- Increment the reference count
-        void operator++()
+        void operator++() noexcept
         {
             ++count_;
         }
 
         //- Increment the reference count
-        void operator++(int)
+        void operator++(int) noexcept
         {
             ++count_;
         }
 
         //- Decrement the reference count
-        void operator--()
+        void operator--() noexcept
         {
             --count_;
         }
 
         //- Decrement the reference count
-        void operator--(int)
+        void operator--(int) noexcept
         {
             --count_;
         }
diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H
index b315ed34efc..00a26a8a811 100644
--- a/src/OpenFOAM/memory/tmp/tmp.H
+++ b/src/OpenFOAM/memory/tmp/tmp.H
@@ -157,17 +157,17 @@ public:
     // Query
 
         //- True if this is a managed pointer (not a const reference)
-        inline bool isTmp() const;
+        inline bool isTmp() const noexcept;
 
         //- True if this is a non-null managed pointer
-        inline bool empty() const;
+        inline bool empty() const noexcept;
 
         //- True if this is a non-null managed pointer,
         //- or is a const object reference
-        inline bool valid() const;
+        inline bool valid() const noexcept;
 
         //- True if this is a non-null managed pointer with a unique ref-count
-        inline bool movable() const;
+        inline bool movable() const noexcept;
 
         //- Return type-name of the tmp, constructed from type-name of T
         inline word typeName() const;
@@ -175,6 +175,12 @@ public:
 
     // Access
 
+        //- Return pointer without nullptr checking.
+        inline T* get() noexcept;
+
+        //- Return const pointer without nullptr checking.
+        inline const T* get() const noexcept;
+
         //- Return the const object reference or a const reference to the
         //- contents of a non-null managed pointer.
         //  Fatal for a null managed pointer
@@ -200,14 +206,14 @@ public:
 
         //- If object pointer points to valid object:
         //- delete object and set pointer to nullptr
-        inline void clear() const;
+        inline void clear() const noexcept;
 
         //- Release ownership of managed temporary object.
         //  After this call no object is managed.
-        inline void reset();
+        inline void reset() noexcept;
 
         //- Delete managed temporary object and set to new given pointer
-        inline void reset(T* p);
+        inline void reset(T* p) noexcept;
 
         //- Swaps the managed object with other tmp.
         inline void swap(tmp<T>& other) noexcept;
diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H
index c2729bc1c7c..63dd3668a06 100644
--- a/src/OpenFOAM/memory/tmp/tmpI.H
+++ b/src/OpenFOAM/memory/tmp/tmpI.H
@@ -186,28 +186,28 @@ inline Foam::tmp<T>::~tmp()
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 template<class T>
-inline bool Foam::tmp<T>::isTmp() const
+inline bool Foam::tmp<T>::isTmp() const noexcept
 {
     return type_ == PTR;
 }
 
 
 template<class T>
-inline bool Foam::tmp<T>::empty() const
+inline bool Foam::tmp<T>::empty() const noexcept
 {
     return (!ptr_ && isTmp());
 }
 
 
 template<class T>
-inline bool Foam::tmp<T>::valid() const
+inline bool Foam::tmp<T>::valid() const noexcept
 {
     return (ptr_ || type_ == CREF);
 }
 
 
 template<class T>
-inline bool Foam::tmp<T>::movable() const
+inline bool Foam::tmp<T>::movable() const noexcept
 {
     return (type_ == PTR && ptr_ && ptr_->unique());
 }
@@ -220,6 +220,20 @@ 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
 {
@@ -305,7 +319,7 @@ inline T* Foam::tmp<T>::ptr() const
 
 
 template<class T>
-inline void Foam::tmp<T>::clear() const
+inline void Foam::tmp<T>::clear() const noexcept
 {
     if (isTmp() && ptr_)
     {
@@ -323,7 +337,7 @@ inline void Foam::tmp<T>::clear() const
 
 
 template<class T>
-inline void Foam::tmp<T>::reset()
+inline void Foam::tmp<T>::reset() noexcept
 {
     clear();
     ptr_ = nullptr;
@@ -332,7 +346,7 @@ inline void Foam::tmp<T>::reset()
 
 
 template<class T>
-inline void Foam::tmp<T>::reset(T* p)
+inline void Foam::tmp<T>::reset(T* p) noexcept
 {
     clear();
     ptr_ = p;
diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H
index a5bd9c761b9..b5b4dd36e31 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrc.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrc.H
@@ -138,17 +138,17 @@ public:
     // Query
 
         //- True if this is a managed pointer (not a const reference)
-        inline bool isTmp() const;
+        inline bool isTmp() const noexcept;
 
         //- True if this is a non-null managed pointer
-        inline bool empty() const;
+        inline bool empty() const noexcept;
 
         //- True if this is a non-null managed pointer,
         //- or is a const object reference
-        inline bool valid() const;
+        inline bool valid() const noexcept;
 
         //- True if this is a non-null managed pointer with a unique ref-count
-        inline bool movable() const;
+        inline bool movable() const noexcept;
 
         //- Return type-name of the tmp, constructed from type-name of T
         inline word typeName() const;
@@ -156,6 +156,12 @@ public:
 
     // Access
 
+        //- Return pointer without nullptr checking.
+        inline T* get() noexcept;
+
+        //- Return const pointer without nullptr checking.
+        inline const T* get() const noexcept;
+
         //- Return the const object reference or a const reference to the
         //- contents of a non-null managed pointer.
         //  Fatal for a null managed pointer
@@ -181,14 +187,14 @@ public:
 
         //- If object pointer points to valid object:
         //- delete object and set pointer to nullptr
-        inline void clear() const;
+        inline void clear() const noexcept;
 
         //- Release ownership of managed temporary object.
         //  After this call no object is managed.
-        inline void reset();
+        inline void reset() noexcept;
 
         //- Delete managed temporary object and set to new given pointer
-        inline void reset(T* p);
+        inline void reset(T* p) noexcept;
 
         //- Swaps the managed object with other tmpNrc.
         inline void swap(tmpNrc<T>& other) noexcept;
diff --git a/src/OpenFOAM/memory/tmp/tmpNrcI.H b/src/OpenFOAM/memory/tmp/tmpNrcI.H
index 8d3f0683de8..0a86958b294 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrcI.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H
@@ -150,28 +150,28 @@ inline Foam::tmpNrc<T>::~tmpNrc()
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 template<class T>
-inline bool Foam::tmpNrc<T>::isTmp() const
+inline bool Foam::tmpNrc<T>::isTmp() const noexcept
 {
     return type_ == PTR;
 }
 
 
 template<class T>
-inline bool Foam::tmpNrc<T>::empty() const
+inline bool Foam::tmpNrc<T>::empty() const noexcept
 {
     return (!ptr_ && isTmp());
 }
 
 
 template<class T>
-inline bool Foam::tmpNrc<T>::valid() const
+inline bool Foam::tmpNrc<T>::valid() const noexcept
 {
     return (ptr_ || type_ == CREF);
 }
 
 
 template<class T>
-inline bool Foam::tmpNrc<T>::movable() const
+inline bool Foam::tmpNrc<T>::movable() const noexcept
 {
     return (type_ == PTR && ptr_);
 }
@@ -184,6 +184,20 @@ 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
 {
@@ -262,7 +276,7 @@ inline T* Foam::tmpNrc<T>::ptr() const
 
 
 template<class T>
-inline void Foam::tmpNrc<T>::clear() const
+inline void Foam::tmpNrc<T>::clear() const noexcept
 {
     if (isTmp() && ptr_)
     {
@@ -273,7 +287,7 @@ inline void Foam::tmpNrc<T>::clear() const
 
 
 template<class T>
-inline void Foam::tmpNrc<T>::reset()
+inline void Foam::tmpNrc<T>::reset() noexcept
 {
     clear();
     ptr_ = nullptr;
@@ -282,7 +296,7 @@ inline void Foam::tmpNrc<T>::reset()
 
 
 template<class T>
-inline void Foam::tmpNrc<T>::reset(T* p)
+inline void Foam::tmpNrc<T>::reset(T* p) noexcept
 {
     clear();
     ptr_ = p;
-- 
GitLab