From a76a3d760c394e9afb86702a547037332f5c0606 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 10 Mar 2021 11:43:05 +0100 Subject: [PATCH] ENH: add operator* to refPtr - makes it more like autoPtr - would ideally like the same for tmp, but there is too much existing code that uses a multiply operation that would be interpreted as a dereference. --- applications/test/refPtr/Test-refPtr.C | 3 +- src/OpenFOAM/memory/refPtr/refPtr.H | 16 +++++-- src/OpenFOAM/memory/refPtr/refPtrI.H | 64 ++++++++++++++++++-------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/applications/test/refPtr/Test-refPtr.C b/applications/test/refPtr/Test-refPtr.C index e60f676e76..2d39ffa55c 100644 --- a/applications/test/refPtr/Test-refPtr.C +++ b/applications/test/refPtr/Test-refPtr.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) 2020-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM, distributed under GPL-3.0-or-later. @@ -63,6 +63,7 @@ int main() auto tfld1 = refPtr::New(10, scalar(1)); printInfo(tfld1, true); + Info<< nl << "Dereferenced: " << *tfld1 << nl; Info<< nl << "Construct from autoPtr" << nl; refPtr tfld2(autoPtr::New(10, scalar(2))); diff --git a/src/OpenFOAM/memory/refPtr/refPtr.H b/src/OpenFOAM/memory/refPtr/refPtr.H index c264ddbeb2..dabc065305 100644 --- a/src/OpenFOAM/memory/refPtr/refPtr.H +++ b/src/OpenFOAM/memory/refPtr/refPtr.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -132,7 +132,7 @@ public: //- Move construct, transferring ownership. inline refPtr(refPtr&& rhs) noexcept; - //- Copy construct + //- Copy construct (shallow copy) inline refPtr(const refPtr& rhs); //- Copy/move construct. Optionally reusing pointer. @@ -227,12 +227,20 @@ public: // Member Operators + //- Return const reference to the object. + // Fatal if nothing is managed + inline const T& operator*() const; + + //- Return reference to the managed object. + // Fatal if nothing is managed or if the object is const. + inline T& operator*(); + //- Dereferences (const) pointer to the managed object. - // Fatal for a null managed pointer. + // Fatal if nothing is managed. inline const T* operator->() const; //- Dereferences (non-const) pointer to the managed object. - // Fatal for a null managed pointer or if the object is const. + // Fatal if nothing is managed or if the object is const. inline T* operator->(); //- Return const reference to the object - same as cref() method. diff --git a/src/OpenFOAM/memory/refPtr/refPtrI.H b/src/OpenFOAM/memory/refPtr/refPtrI.H index 6e0bfb58a2..7a2019a27f 100644 --- a/src/OpenFOAM/memory/refPtr/refPtrI.H +++ b/src/OpenFOAM/memory/refPtr/refPtrI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016-2017 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -326,17 +326,48 @@ inline void Foam::refPtr::swap(refPtr& other) noexcept // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +template +inline const T& Foam::refPtr::operator*() const +{ + if (!ptr_) + { + FatalErrorInFunction + << this->typeName() << " deallocated" + << abort(FatalError); + } + return *ptr_; +} + + +template +inline T& Foam::refPtr::operator*() +{ + if (type_ == CREF) + { + FatalErrorInFunction + << "Attempt to cast const object to non-const for a " + << this->typeName() + << abort(FatalError); + } + else if (!ptr_) + { + FatalErrorInFunction + << this->typeName() << " deallocated" + << abort(FatalError); + } + + return *ptr_; +} + + template inline const T* Foam::refPtr::operator->() const { - if (type_ == PTR) + if (!ptr_) { - if (!ptr_) - { - FatalErrorInFunction - << this->typeName() << " deallocated" - << abort(FatalError); - } + FatalErrorInFunction + << this->typeName() << " deallocated" + << abort(FatalError); } return ptr_; @@ -346,22 +377,19 @@ inline const T* Foam::refPtr::operator->() const template inline T* Foam::refPtr::operator->() { - if (type_ == PTR) - { - if (!ptr_) - { - FatalErrorInFunction - << this->typeName() << " deallocated" - << abort(FatalError); - } - } - else if (type_ == CREF) + if (type_ == CREF) { FatalErrorInFunction << "Attempt to cast const object to non-const for a " << this->typeName() << abort(FatalError); } + else if (!ptr_) + { + FatalErrorInFunction + << this->typeName() << " deallocated" + << abort(FatalError); + } return ptr_; }