From 215570915e5cc40dcf7d132e7bae2a68243e349c Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 30 Jul 2018 14:33:22 +0200
Subject: [PATCH] ENH: explicit convert to bool operator for autoPtr

- behaves the same as the valid() method, but can be queried directly
  like a normal raw pointer and as per std::unique_ptr.
  Eg,

      autoPtr<T> ptr = ...

      if (ptr) ...
---
 applications/test/autoPtr/Test-autoPtr.C | 18 +++++++++++++++++-
 src/OpenFOAM/memory/autoPtr/autoPtr.H    |  2 ++
 src/OpenFOAM/memory/autoPtr/autoPtrI.H   |  7 +++++++
 3 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/applications/test/autoPtr/Test-autoPtr.C b/applications/test/autoPtr/Test-autoPtr.C
index edd7f222e82..a7783b3a316 100644
--- a/applications/test/autoPtr/Test-autoPtr.C
+++ b/applications/test/autoPtr/Test-autoPtr.C
@@ -143,7 +143,7 @@ int main(int argc, char *argv[])
 
         Info<<"now have valid=" << Switch(list.valid()).c_str();
 
-        if (list.valid())
+        if (list)
         {
             Info<< nl
                 << flatOutput(*list) << " @ " << long(list->cdata())
@@ -153,6 +153,22 @@ int main(int argc, char *argv[])
         {
             Info<< nl;
         }
+
+        // These should fail to compile
+        #if 0
+        label val0 = 0;
+
+        if (true)
+        {
+            val0 = list;
+        }
+
+        label val1 = 10;
+
+        if (val1 == list)
+        {
+        }
+        #endif
     }
 
     // Memory transfer
diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H
index 0d88f1b61bd..d005924dd7d 100644
--- a/src/OpenFOAM/memory/autoPtr/autoPtr.H
+++ b/src/OpenFOAM/memory/autoPtr/autoPtr.H
@@ -234,6 +234,8 @@ public:
         operator const T&() const = delete;
         #endif
 
+        //- True if the managed pointer is non-null
+        explicit inline operator bool() const noexcept;
 
         //- Transfer object ownership from parameter
         inline void operator=(autoPtr<T>&& ap) noexcept;
diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H
index 128fabe5abd..9d2df12e6a7 100644
--- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H
+++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H
@@ -260,6 +260,13 @@ inline const T& Foam::autoPtr<T>::operator()() const
 }
 
 
+template<class T>
+inline Foam::autoPtr<T>::operator bool() const noexcept
+{
+    return ptr_;
+}
+
+
 template<class T>
 inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
 {
-- 
GitLab