From 638b6b2cf121fe79c2df3102424f5213e9efd032 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Thu, 26 Jul 2018 00:10:51 +0200
Subject: [PATCH] ENH: add get() dereferencing for PtrList iterators

- gets the pointer within the list.
---
 .../containers/PtrLists/PtrList/PtrList.C     |  2 +-
 .../containers/PtrLists/PtrList/PtrListI.H    |  6 +-
 .../containers/PtrLists/UPtrList/UPtrList.H   | 12 +++-
 .../containers/PtrLists/UPtrList/UPtrListI.H  | 14 +++++
 .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C | 55 +++++++++++++++++--
 .../meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H | 14 ++++-
 6 files changed, 91 insertions(+), 12 deletions(-)

diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C
index cad4676c675..867f57f6c4c 100644
--- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C
+++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.C
@@ -68,7 +68,7 @@ Foam::PtrList<T>::PtrList(const SLPtrList<T>& list)
 template<class T>
 Foam::PtrList<T>::~PtrList()
 {
-    (this->ptrs_).free();
+    (this->ptrs_).free(); // free old pointers
 }
 
 
diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H
index bb2a19064a4..c05db5554b9 100644
--- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H
+++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H
@@ -31,7 +31,7 @@ License
 template<class T>
 inline void Foam::PtrList<T>::free()
 {
-    (this->ptrs_).free();
+    (this->ptrs_).free(); // free old pointers
 }
 
 
@@ -82,7 +82,7 @@ inline Foam::PtrList<T>::PtrList
 template<class T>
 inline void Foam::PtrList<T>::clear()
 {
-    (this->ptrs_).free();
+    (this->ptrs_).free(); // free old pointers
     UPtrList<T>::clear();
 }
 
@@ -160,7 +160,7 @@ inline Foam::autoPtr<T> Foam::PtrList<T>::set(label i, const tmp<T>& tptr)
 template<class T>
 inline void Foam::PtrList<T>::transfer(PtrList<T>& list)
 {
-    this->free(); // free old pointers
+    (this->ptrs_).free(); // free old pointers
     UPtrList<T>::transfer(list);
 }
 
diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H
index 37bb8d97ec3..f778c850f92 100644
--- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H
+++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H
@@ -32,7 +32,7 @@ Description
 Note
     The class definition is such that it contains a list of pointers, but
     itself does not inherit from a list of pointers since this would
-    wreak havoc later inheritance resolution.
+    wreak havoc later with inheritance resolution.
 
 See Also
     Foam::PtrList
@@ -226,6 +226,11 @@ public:
             //- Construct for a given entry
             inline iterator(T** ptr);
 
+            // Member functions
+
+            //- Return pointer, can be nullptr.
+            inline pointer get() const;
+
             // Member operators
 
             inline bool operator==(const iterator& iter) const;
@@ -280,6 +285,11 @@ public:
             //- Copy construct from non-const iterator
             inline const_iterator(const iterator& iter);
 
+            // Member functions
+
+            //- Return pointer, can be nullptr.
+            inline pointer get() const;
+
             // Member operators
 
             inline bool operator==(const const_iterator& iter) const;
diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H
index a93b00df713..d416564a3b4 100644
--- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H
+++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H
@@ -237,6 +237,13 @@ inline Foam::UPtrList<T>::iterator::iterator(T** ptr)
 {}
 
 
+template<class T>
+inline T* Foam::UPtrList<T>::iterator::get() const
+{
+    return *ptr_;
+}
+
+
 template<class T>
 inline bool Foam::UPtrList<T>::iterator::operator==(const iterator& iter) const
 {
@@ -403,6 +410,13 @@ inline Foam::UPtrList<T>::const_iterator::const_iterator(const iterator& iter)
 {}
 
 
+template<class T>
+inline const T* Foam::UPtrList<T>::const_iterator::get() const
+{
+    return *ptr_;
+}
+
+
 template<class T>
 inline bool Foam::UPtrList<T>::const_iterator::operator==
 (
diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C
index 1abac79a9ad..6fe7f6fac10 100644
--- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C
+++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -366,7 +366,7 @@ Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::findIndices
     }
     else
     {
-        indices.setSize(this->size());
+        indices.resize(this->size());
         label count = 0;
         forAll(*this, i)
         {
@@ -375,7 +375,7 @@ Foam::labelList Foam::ZoneMesh<ZoneType, MeshType>::findIndices
                 indices[count++] = i;
             }
         }
-        indices.setSize(count);
+        indices.resize(count);
     }
 
     return indices;
@@ -487,11 +487,12 @@ Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::findMatching
     bitSet bitset;
 
     const labelList indices = this->findIndices(key);
-    forAll(indices, i)
+
+    for (const label zonei : indices)
     {
         bitset.set
         (
-            static_cast<const labelList&>(this->operator[](indices[i]))
+            static_cast<const labelList&>(this->operator[](zonei))
         );
     }
 
@@ -499,6 +500,50 @@ Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::findMatching
 }
 
 
+template<class ZoneType, class MeshType>
+const ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
+(
+    const word& zoneName
+) const
+{
+    const PtrList<ZoneType>& zones = *this;
+
+    for (auto iter = zones.begin(); iter != zones.end(); ++iter)
+    {
+        const ZoneType* ptr = iter.get();
+
+        if (ptr && zoneName == ptr->name())
+        {
+            return ptr;
+        }
+    }
+
+    return nullptr;
+}
+
+
+template<class ZoneType, class MeshType>
+ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
+(
+    const word& zoneName
+)
+{
+    PtrList<ZoneType>& zones = *this;
+
+    for (auto iter = zones.begin(); iter != zones.end(); ++iter)
+    {
+        ZoneType* ptr = iter.get();
+
+        if (ptr && zoneName == ptr->name())
+        {
+            return ptr;
+        }
+    }
+
+    return nullptr;
+}
+
+
 template<class ZoneType, class MeshType>
 void Foam::ZoneMesh<ZoneType, MeshType>::clearAddressing()
 {
diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H
index 59d1d220526..2389c8f5b48 100644
--- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.H
+++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.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  | Copyright (C) 2016-2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2016-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -49,13 +49,14 @@ namespace Foam
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
-// Forward declaration of friend functions and operators
+// Forward declarations
 
 template<class ZoneType, class MeshType> class ZoneMesh;
 
 template<class ZoneType, class MeshType>
 Ostream& operator<<(Ostream& os, const ZoneMesh<ZoneType, MeshType>& zones);
 
+
 /*---------------------------------------------------------------------------*\
                            Class ZoneMesh Declaration
 \*---------------------------------------------------------------------------*/
@@ -186,6 +187,14 @@ public:
         //- Mark items (cells, faces, points) that match the zone specification
         bitSet findMatching(const keyType& key) const;
 
+
+        //- Lookup zone by name and return const pointer, nullptr on error.
+        const ZoneType* zonePtr(const word& zoneName) const;
+
+        //- Lookup zone by name and return pointer, nullptr on error.
+        ZoneType* zonePtr(const word& zoneName);
+
+
         //- Clear addressing
         void clearAddressing();
 
@@ -205,6 +214,7 @@ public:
         //- writeData member function required by regIOobject
         bool writeData(Ostream& os) const;
 
+
     // Member Operators
 
         //- Return const and non-const reference to zone by index.
-- 
GitLab