From 4093fcd66b078200c5a785da4708af19f732f7a8 Mon Sep 17 00:00:00 2001
From: mark <mark@opencfd>
Date: Wed, 28 Sep 2016 11:26:42 +0200
Subject: [PATCH] ENH: provide refOrNull method for autoPtr.

- Normally use '()' to deference. This has extra safety and issues a
  fatal error if the underlying pointer is not valid.

  However, in some cases we are happy with getting a null reference.
  The refOrNull() method returns the reference without any checking.

  Usage example:

      autoPtr<OFstream> osPtr;
      if (Pstream::master())
      {
          osPtr.reset(new OFstream(...));
      }
      writeViaMaster(osPtr.refOrNull());

      - The writeViaMaster() call takes an OFstream reference,
        but this is only used directly on the master.
        The slaves will pass things through to the master.
---
 src/OpenFOAM/memory/autoPtr/autoPtr.H  | 11 ++++++++++-
 src/OpenFOAM/memory/autoPtr/autoPtrI.H | 16 +++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H
index 748975b5cf3..a8802f23a92 100644
--- a/src/OpenFOAM/memory/autoPtr/autoPtr.H
+++ b/src/OpenFOAM/memory/autoPtr/autoPtr.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) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -104,6 +104,15 @@ public:
             inline void clear();
 
 
+        // Access
+
+            //- Return reference, without checking pointer validity.
+            inline T& refOrNull();
+
+            //- Return const reference, without checking pointer validity.
+            inline const T& refOrNull() const;
+
+
         // Member operators
 
             //- Return reference to the object data
diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H
index ef199ca20c6..7440bdce690 100644
--- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H
+++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.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) 2016 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -129,6 +129,20 @@ inline void Foam::autoPtr<T>::clear()
 }
 
 
+template<class T>
+inline T& Foam::autoPtr<T>::refOrNull()
+{
+    return *ptr_;
+}
+
+
+template<class T>
+inline const T& Foam::autoPtr<T>::refOrNull() const
+{
+    return *ptr_;
+}
+
+
 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
 
 template<class T>
-- 
GitLab