From e3e0d7c8b95f49a5fc07ee632279b17dc9337e62 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 11 Feb 2019 18:23:06 +0100
Subject: [PATCH] ENH: add possibility to change const reference in tmp.

- previously it was only possible to reset a pointer, but not to
  change a const-reference directly (needed a swap() to do this).
---
 applications/test/tmp/Test-tmp.C  | 39 +++++++++++++++++++++++--------
 src/OpenFOAM/memory/tmp/tmp.H     |  5 +++-
 src/OpenFOAM/memory/tmp/tmpI.H    | 11 ++++++++-
 src/OpenFOAM/memory/tmp/tmpNrc.H  |  5 +++-
 src/OpenFOAM/memory/tmp/tmpNrcI.H | 11 ++++++++-
 5 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C
index 538ee68b095..ad6b02a6df2 100644
--- a/applications/test/tmp/Test-tmp.C
+++ b/applications/test/tmp/Test-tmp.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011 OpenFOAM Foundation
@@ -41,14 +41,30 @@ struct myScalarField : public scalarField
 };
 
 
+template<class T>
+void printInfo(const tmp<T>& tmpItem)
+{
+    Info<< "tmp valid:" << tmpItem.valid()
+        << " isTmp:" << tmpItem.isTmp()
+        << " addr: " << long(tmpItem.get());
+
+    if (tmpItem.valid())
+    {
+        Info<< " refCount:" << tmpItem->count();
+    }
+
+    Info<< nl;
+}
+
+
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 // Main program:
 
 int main()
 {
-    {
-        scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0);
+    scalarField f1(1000000, 1.0), f2(1000000, 2.0), f3(1000000, 3.0);
 
+    {
         for (int iter=0; iter < 50; ++iter)
         {
             f1 = f2 + f3 + f2 + f3;
@@ -60,30 +76,33 @@ int main()
     {
         auto tfld1 = tmp<scalarField>::New(20, Zero);
 
-        Info<< "tmp refCount = " << tfld1->count() << nl;
+        printInfo(tfld1);
+
         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);
 
-        Info<< "tmp refCount = " << tfld2->count() << nl;
+        printInfo(tfld2);
         if (tfld2.valid())
         {
             Info<<"tmp: " << tfld2() << nl;
         }
 
-        Info<<"tmp addr: " << long(tfld2.get()) << nl;
-
         tfld2.clear();
 
-        Info<<"after clear: " << long(tfld2.get()) << nl;
+        Info<<"After clear : ";
+        printInfo(tfld2);
+
+        tfld2.cref(f1);
+
+        Info<<"Reset const-ref : ";
+        printInfo(tfld2);
     }
 
     Info<< "\nEnd" << endl;
diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H
index 88c59f09672..32ca6d419c5 100644
--- a/src/OpenFOAM/memory/tmp/tmp.H
+++ b/src/OpenFOAM/memory/tmp/tmp.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011-2016 OpenFOAM Foundation
@@ -217,6 +217,9 @@ public:
         //- Delete managed temporary object and set to new given pointer
         inline void reset(T* p) noexcept;
 
+        //- Delete managed temporary object and set to const reference
+        inline void cref(const T& obj) 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 b49fbb56930..78f4a20234f 100644
--- a/src/OpenFOAM/memory/tmp/tmpI.H
+++ b/src/OpenFOAM/memory/tmp/tmpI.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011-2017 OpenFOAM Foundation
@@ -356,6 +356,15 @@ inline void Foam::tmp<T>::reset(T* p) noexcept
 }
 
 
+template<class T>
+inline void Foam::tmp<T>::cref(const T& obj) noexcept
+{
+    clear();
+    ptr_ = const_cast<T*>(&obj);
+    type_ = CREF;
+}
+
+
 template<class T>
 inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
 {
diff --git a/src/OpenFOAM/memory/tmp/tmpNrc.H b/src/OpenFOAM/memory/tmp/tmpNrc.H
index 802346ba5c0..d5a3b7b6f79 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrc.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrc.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2016 OpenFOAM Foundation
@@ -198,6 +198,9 @@ public:
         //- Delete managed temporary object and set to new given pointer
         inline void reset(T* p) noexcept;
 
+        //- Delete managed temporary object and set to const reference
+        inline void cref(const T& obj) 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 e93f497c1b3..4f8148b0396 100644
--- a/src/OpenFOAM/memory/tmp/tmpNrcI.H
+++ b/src/OpenFOAM/memory/tmp/tmpNrcI.H
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2018 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2018-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2016-2017 OpenFOAM Foundation
@@ -306,6 +306,15 @@ inline void Foam::tmpNrc<T>::reset(T* p) noexcept
 }
 
 
+template<class T>
+inline void Foam::tmpNrc<T>::cref(const T& obj) noexcept
+{
+    clear();
+    ptr_ = const_cast<T*>(&obj);
+    type_ = CREF;
+}
+
+
 template<class T>
 inline void Foam::tmpNrc<T>::swap(tmpNrc<T>& other) noexcept
 {
-- 
GitLab