From c6c30a0692e5599f9a33608bf72423527d919cb5 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 15 Jan 2019 16:37:15 +0100 Subject: [PATCH] ENH: return const pointer instead of bool from PtrList::set(label) - can still test as a bool or use to 'peek' at the content. --- .../containers/PtrLists/PtrList/PtrList.H | 15 ++++++++------- .../containers/PtrLists/PtrList/PtrListI.H | 12 ++++++------ .../containers/PtrLists/UPtrList/UPtrList.H | 7 ++++--- .../containers/PtrLists/UPtrList/UPtrListI.H | 10 +++++----- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H index d1296de3f2..2650882573 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrList.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) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -150,21 +150,22 @@ public: //- Transfer into this list and annul the argument list inline void transfer(PtrList& list); - //- Return true if element is set (ie, not a nullptr) - inline bool set(const label i) const; + //- Return const pointer to element (if set) or nullptr. + // The return value can be tested as a bool. + inline const T* set(const label i) const; //- Set element to given pointer and return old element (can be null) // No-op if the new pointer value is identical to the current content. - inline autoPtr set(label i, T* ptr); + inline autoPtr set(const label i, T* ptr); //- Set element to given autoPtr and return old element - inline autoPtr set(label i, autoPtr& aptr); + inline autoPtr set(const label i, autoPtr& aptr); //- Set element to given autoPtr and return old element - inline autoPtr set(label i, autoPtr&& aptr); + inline autoPtr set(const label i, autoPtr&& aptr); //- Set element to given tmp and return old element - inline autoPtr set(label i, const tmp& tptr); + inline autoPtr set(const label i, const tmp& tptr); // Member Operators diff --git a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H index c05db5554b..9f34ef904e 100644 --- a/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/PtrList/PtrListI.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) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -123,35 +123,35 @@ inline void Foam::PtrList::append(const tmp& tptr) template -inline bool Foam::PtrList::set(const label i) const +inline const T* Foam::PtrList::set(const label i) const { return UPtrList::set(i); } template -inline Foam::autoPtr Foam::PtrList::set(label i, T* ptr) +inline Foam::autoPtr Foam::PtrList::set(const label i, T* ptr) { return autoPtr(UPtrList::set(i, ptr)); } template -inline Foam::autoPtr Foam::PtrList::set(label i, autoPtr& aptr) +inline Foam::autoPtr Foam::PtrList::set(const label i, autoPtr& aptr) { return set(i, aptr.release()); } template -inline Foam::autoPtr Foam::PtrList::set(label i, autoPtr&& aptr) +inline Foam::autoPtr Foam::PtrList::set(const label i, autoPtr&& aptr) { return set(i, aptr.release()); } template -inline Foam::autoPtr Foam::PtrList::set(label i, const tmp& tptr) +inline Foam::autoPtr Foam::PtrList::set(const label i, const tmp& tptr) { return set(i, tptr.ptr()); } diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H index 99664df337..c7eac064fe 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrList.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) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -177,8 +177,9 @@ public: //- Transfer contents into this list and annul the argument inline void transfer(UPtrList& list); - //- Return true if element is set (not a nullptr) - inline bool set(const label i) const; + //- Return const pointer to element (if set) or nullptr. + // The return value can be tested as a bool. + inline const T* set(const label i) const; //- Set element to specified pointer and return the old list element, //- which can be a nullptr. diff --git a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H index 6045c8ab3b..3df9254ddc 100644 --- a/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.H +++ b/src/OpenFOAM/containers/PtrLists/UPtrList/UPtrListI.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) 2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2018-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -170,9 +170,9 @@ inline void Foam::UPtrList::append(T* ptr) template -inline bool Foam::UPtrList::set(const label i) const +inline const T* Foam::UPtrList::set(const label i) const { - return ptrs_[i] != nullptr; + return ptrs_[i]; } @@ -199,7 +199,7 @@ inline const T& Foam::UPtrList::operator[](const label i) const if (!ptr) { FatalErrorInFunction - << "cannot dereference nullptr at index " << i + << "Cannot dereference nullptr at index " << i << " in range [0," << size() << ")" << abort(FatalError); } @@ -216,7 +216,7 @@ inline T& Foam::UPtrList::operator[](const label i) if (!ptr) { FatalErrorInFunction - << "cannot dereference nullptr at index " << i + << "Cannot dereference nullptr at index " << i << " in range [0," << size() << ")" << abort(FatalError); }