diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H index f0850dd8c7..8528bb6382 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynList.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -126,9 +126,6 @@ public: // Edit - //- Delete the allocated entries, but retain the list size. - using PtrList::free; - //- Squeeze out intermediate nullptr entries in the list of pointers //- and adjust the addressable size accordingly. // \return the number of non-null entries @@ -138,6 +135,13 @@ public: template inline void swap(PtrDynList& other); + //- Transfer contents of the argument PtrList into this. + inline void transfer(PtrList& list); + + //- Transfer contents of any sized PtrDynList into this. + template + inline void transfer(PtrDynList& list); + //- Construct an element at the end of the list, //- return reference to the new list element template diff --git a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H index 0320636bd6..841456774f 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrDynList/PtrDynListI.H @@ -216,6 +216,44 @@ inline void Foam::PtrDynList::swap } +template +inline void Foam::PtrDynList::transfer(PtrList& other) +{ + if + ( + static_cast*>(this) + == static_cast*>(&other) + ) + { + return; // Self assignment is a no-op + } + + UPtrList::swap(other); + other.clear(); +} + + +template +template +inline void Foam::PtrDynList::transfer +( + PtrDynList& other +) +{ + if + ( + static_cast*>(this) + == static_cast*>(&other) + ) + { + return; // Self assignment is a no-op + } + + this->swap(other); + other.clear(); +} + + template template inline T& Foam::PtrDynList::emplace_back(Args&&... args) diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H index 80374047f0..24a72273d1 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H @@ -78,8 +78,6 @@ protected: template void readIstream(Istream& is, const INew& inew); - //- Delete the allocated entries, but retain the list size. - inline void free(); public: @@ -142,6 +140,9 @@ public: //- Clear the PtrList. Delete allocated entries and set size to zero. inline void clear(); + //- Free memory and nullify all entries. Does not change the list size. + inline void free(); + //- Adjust size of PtrList. // New entries are initialized to nullptr, removed entries are deleted void resize(const label newLen); diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H index 150b2825af..5d2ce629fc 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H @@ -30,15 +30,6 @@ License #include "refPtr.H" #include "tmp.H" -// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // - -template -inline void Foam::PtrList::free() -{ - (this->ptrs_).free(); // free old pointers -} - - // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * // template @@ -96,11 +87,18 @@ inline Foam::PtrList::PtrList template inline void Foam::PtrList::clear() { - (this->ptrs_).free(); // free old pointers + (this->ptrs_).free(); // Free and nullify old pointers UPtrList::clear(); } +template +inline void Foam::PtrList::free() +{ + (this->ptrs_).free(); // Free and nullify old pointers +} + + template template inline T& Foam::PtrList::emplace_back(Args&&... args) @@ -260,7 +258,12 @@ inline Foam::autoPtr Foam::PtrList::release(const label i) template inline void Foam::PtrList::transfer(PtrList& list) { - (this->ptrs_).free(); // free old pointers + if (this == &list) + { + return; // Self-assignment is a no-op + } + + (this->ptrs_).free(); // Free and nullify old pointers UPtrList::transfer(list); } diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index 0dd1967be3..dcf622ba69 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H @@ -295,6 +295,9 @@ public: //- Set list size to zero. inline void clear(); + //- Nullify all entries. Does not change the list size. + inline void free(); + //- Change the size of the list. // New entries are initialized to nullptr. inline void resize(const label newLen); diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index 9180af1668..7782845cbe 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H @@ -205,6 +205,13 @@ inline void Foam::UPtrList::clear() } +template +inline void Foam::UPtrList::free() +{ + ptrs_.setNull(); +} + + template inline void Foam::UPtrList::swap(UPtrList& list) { diff --git a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBaseTemplates.C b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBaseTemplates.C index 9f6bdaf50d..a4fed62e7d 100644 --- a/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBaseTemplates.C +++ b/src/OpenFOAM/meshes/polyMesh/mapPolyMesh/mapDistribute/mapDistributeBaseTemplates.C @@ -191,7 +191,7 @@ void Foam::mapDistributeBase::send // Set up receives from neighbours - recvFields.clear(); + recvFields.free(); recvFields.resize(nProcs); for (const int domain : UPstream::allProcs(comm)) @@ -219,7 +219,7 @@ void Foam::mapDistributeBase::send // Set up sends to neighbours - sendFields.clear(); + recvFields.free(); sendFields.resize(nProcs); for (const int domain : UPstream::allProcs(comm)) diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index 4dca521071..a4d40ccc17 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -206,6 +206,13 @@ Foam::polyBoundaryMesh::polyBoundaryMesh // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // +void Foam::polyBoundaryMesh::clear() +{ + polyPatchList::clear(); + clearAddressing(); +} + + void Foam::polyBoundaryMesh::clearGeom() { polyPatchList& patches = *this; @@ -219,9 +226,9 @@ void Foam::polyBoundaryMesh::clearGeom() void Foam::polyBoundaryMesh::clearAddressing() { - neighbourEdgesPtr_.clear(); - patchIDPtr_.clear(); - groupIDsPtr_.clear(); + neighbourEdgesPtr_.reset(nullptr); + patchIDPtr_.reset(nullptr); + groupIDsPtr_.reset(nullptr); polyPatchList& patches = *this; @@ -493,7 +500,7 @@ void Foam::polyBoundaryMesh::setGroup const labelUList& patchIDs ) { - groupIDsPtr_.clear(); + groupIDsPtr_.reset(nullptr); polyPatchList& patches = *this; @@ -1179,9 +1186,9 @@ void Foam::polyBoundaryMesh::movePoints(const pointField& p) void Foam::polyBoundaryMesh::updateMesh() { - neighbourEdgesPtr_.clear(); - patchIDPtr_.clear(); - groupIDsPtr_.clear(); + neighbourEdgesPtr_.reset(nullptr); + patchIDPtr_.reset(nullptr); + groupIDsPtr_.reset(nullptr); PstreamBuffers pBufs(Pstream::defaultCommsType); diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H index fa2b321a8c..c747c780e3 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.H @@ -94,23 +94,24 @@ class polyBoundaryMesh //- Read if IOobject flags set. Return true if read. bool readContents(const bool allowReadIfPresent); - //- No copy construct - polyBoundaryMesh(const polyBoundaryMesh&) = delete; - - //- No copy assignment - void operator=(const polyBoundaryMesh&) = delete; - - public: //- Declare friendship with polyMesh friend class polyMesh; - //- Runtime type information TypeName("polyBoundaryMesh"); + // Generated Methods + + //- No copy construct + polyBoundaryMesh(const polyBoundaryMesh&) = delete; + + //- No copy assignment + void operator=(const polyBoundaryMesh&) = delete; + + // Constructors //- Read constructor given IOobject and a polyMesh reference @@ -142,15 +143,17 @@ public: ~polyBoundaryMesh() = default; + // Member Functions + + //- Clear the patch list and all demand-driven data + void clear(); + //- Clear geometry at this level and at patches void clearGeom(); //- Clear addressing at this level and at patches void clearAddressing(); - - // Member Functions - //- Return the mesh reference const polyMesh& mesh() const noexcept { diff --git a/src/OpenFOAM/meshes/polyMesh/polyMeshClear.C b/src/OpenFOAM/meshes/polyMesh/polyMeshClear.C index e657ee38a6..ca445ea9d4 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyMeshClear.C +++ b/src/OpenFOAM/meshes/polyMesh/polyMeshClear.C @@ -40,9 +40,7 @@ void Foam::polyMesh::removeBoundary() { DebugInFunction << "Removing boundary patches." << endl; - // Remove the point zones boundary_.clear(); - boundary_.setSize(0); clearOut(); } diff --git a/src/OpenFOAM/meshes/polyMesh/polyMeshIO.C b/src/OpenFOAM/meshes/polyMesh/polyMeshIO.C index 914d11d736..cab91b77bd 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyMeshIO.C +++ b/src/OpenFOAM/meshes/polyMesh/polyMeshIO.C @@ -212,7 +212,7 @@ Foam::polyMesh::readUpdateState Foam::polyMesh::readUpdate() << "unexpected consequences. Proceed with care." << endl; boundary_.clear(); - boundary_.setSize(newBoundary.size()); + boundary_.resize(newBoundary.size()); forAll(newBoundary, patchi) { diff --git a/src/dynamicMesh/boundaryMesh/boundaryMesh.C b/src/dynamicMesh/boundaryMesh/boundaryMesh.C index c98d1c6dbe..a478b7e5ca 100644 --- a/src/dynamicMesh/boundaryMesh/boundaryMesh.C +++ b/src/dynamicMesh/boundaryMesh/boundaryMesh.C @@ -462,8 +462,7 @@ void Foam::boundaryMesh::clearOut() void Foam::boundaryMesh::read(const polyMesh& mesh) { patches_.clear(); - - patches_.setSize(mesh.boundaryMesh().size()); + patches_.resize(mesh.boundaryMesh().size()); // Number of boundary faces const label nBFaces = mesh.nBoundaryFaces(); @@ -630,7 +629,7 @@ void Foam::boundaryMesh::readTriSurface(const fileName& fName) // There are as many surface patches as region numbers in triangles // so use the surface patches - patches_.setSize(surfPatches.size()); + patches_.resize(surfPatches.size()); // Take over patches, setting size to 0 for now. forAll(surfPatches, patchi) @@ -655,7 +654,7 @@ void Foam::boundaryMesh::readTriSurface(const fileName& fName) { // There are not enough surface patches. Make up my own. - patches_.setSize(regionToBoundaryPatch.size()); + patches_.resize(regionToBoundaryPatch.size()); forAll(patches_, patchi) { @@ -1627,9 +1626,7 @@ void Foam::boundaryMesh::deletePatch(const word& patchName) newPatches.set(patchi - 1, patches_[patchi].clone()); } - patches_.clear(); - - patches_ = newPatches; + patches_ = std::move(newPatches); if (debug) { diff --git a/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.C b/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.C index 47fc2ff480..1906cca5ab 100644 --- a/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.C +++ b/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.C @@ -176,6 +176,15 @@ Foam::faBoundaryMesh::faBoundaryMesh {} +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +void Foam::faBoundaryMesh::clear() +{ + faPatchList::clear(); + groupIDsPtr_.reset(nullptr); +} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // void Foam::faBoundaryMesh::calcGeometry() diff --git a/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.H b/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.H index ca64a6380c..5e399a3a21 100644 --- a/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.H +++ b/src/finiteArea/faMesh/faBoundaryMesh/faBoundaryMesh.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 Wikki Ltd - Copyright (C) 2018-2022 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -90,12 +90,6 @@ class faBoundaryMesh //- Read if IOobject flags set. Return true if read. bool readContents(const bool allowReadIfPresent); - //- No copy construct - faBoundaryMesh(const faBoundaryMesh&) = delete; - - //- No copy assignment - void operator=(const faBoundaryMesh&) = delete; - public: @@ -103,6 +97,15 @@ public: TypeName("faBoundaryMesh"); + // Generated Methods + + //- No copy construct + faBoundaryMesh(const faBoundaryMesh&) = delete; + + //- No copy assignment + void operator=(const faBoundaryMesh&) = delete; + + // Constructors //- Construct from faMesh @@ -127,6 +130,9 @@ public: // Member Functions + //- Clear the patch list and all demand-driven data + void clear(); + //- Return the mesh reference const faMesh& mesh() const noexcept { diff --git a/src/sampling/sampledSet/sampledSets/sampledSets.C b/src/sampling/sampledSet/sampledSets/sampledSets.C index 12242b7421..cfebcab812 100644 --- a/src/sampling/sampledSet/sampledSets/sampledSets.C +++ b/src/sampling/sampledSet/sampledSets/sampledSets.C @@ -155,7 +155,7 @@ void Foam::sampledSets::gatherAllSets() const PtrList& localSets = *this; - gatheredSets_.clear(); + gatheredSets_.free(); gatheredSets_.resize(localSets.size()); gatheredSorting_.resize_nocopy(localSets.size()); globalIndices_.resize_nocopy(localSets.size());