From 4c2ccffacf33b0629d67dec3e1b2d21316528a48 Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Thu, 27 Jan 2022 10:01:18 +0000 Subject: [PATCH] UPtrList: Added convert method to return a UPtrList of a different type This is useful for creating a UPtrList from a UPtrList, or creating a UPtrList from a UPtrList. The new pointers are constructed by implicit conversion of the old ones, so the new type must be a base class of the old type and it must not remove const-ness. --- .../containers/Lists/UPtrList/UPtrList.C | 29 ++++++++++++++++++- .../containers/Lists/UPtrList/UPtrList.H | 13 ++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C index 40967b25c9..8cf962d193 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -157,6 +157,33 @@ void Foam::UPtrList::shuffle(const labelUList& newToOld) } + +template +template +Foam::UPtrList Foam::UPtrList::convert() +{ + UPtrList result(size()); + forAll(ptrs_, i) + { + result.set(i, ptrs_[i]); + } + return result; +} + + +template +template +Foam::UPtrList Foam::UPtrList::convert() const +{ + UPtrList result(size()); + forAll(ptrs_, i) + { + result.set(i, ptrs_[i]); + } + return result; +} + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #include "UPtrListIO.C" diff --git a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H index b03bfdd298..0ce584ba66 100644 --- a/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/Lists/UPtrList/UPtrList.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2022 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -150,6 +150,17 @@ public: void shuffle(const labelUList& newToOld); + // Conversion + + //- Convert to list of different pointer type + template + UPtrList convert(); + + //- Convert to list of different pointer type + template + UPtrList convert() const; + + // Member Operators //- Return element const reference