diff --git a/applications/test/refPtr/Test-refPtr.C b/applications/test/refPtr/Test-refPtr.C index 2d39ffa55c798a67deb713748330411084e7a2cf..5a5b872610192f4ac5fb48067a061e5f592b8b44 100644 --- a/applications/test/refPtr/Test-refPtr.C +++ b/applications/test/refPtr/Test-refPtr.C @@ -85,6 +85,12 @@ int main() ptr.reset(new scalarField(5, scalar(15))); tfld3.reset(std::move(ptr)); printInfo(tfld3, true); + + + ptr.reset(new scalarField(2, scalar(1))); + Info<< nl << "const-ref from pointer: " << name(ptr.get()) << nl; + tfld3.cref(ptr.get()); + printInfo(tfld3, true); } Info<< "\nEnd" << endl; diff --git a/applications/test/tmp/Test-tmp.C b/applications/test/tmp/Test-tmp.C index da0486af2340947f55fdf9e1b13367e0011c54a7..52cbde670e6adc4a5e7048fc9aa6205b93d9931b 100644 --- a/applications/test/tmp/Test-tmp.C +++ b/applications/test/tmp/Test-tmp.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM, distributed under GPL-3.0-or-later. @@ -110,6 +110,12 @@ int main() Info<< "Reset to some other tmp content : "; printInfo(tfld2); } + + std::unique_ptr<scalarField> ptr(new scalarField(2, scalar(1))); + + Info<< nl << "const-ref from pointer: " << name(ptr.get()) << nl; + tfld2.cref(ptr.get()); + printInfo(tfld2); } Info<< "\nEnd" << endl; diff --git a/src/OpenFOAM/memory/refPtr/refPtr.H b/src/OpenFOAM/memory/refPtr/refPtr.H index dabc065305501b6fb0df5833e1e79a3aae17dfdd..d7cf643fd86e1141d8168462c5425cf23271816e 100644 --- a/src/OpenFOAM/memory/refPtr/refPtr.H +++ b/src/OpenFOAM/memory/refPtr/refPtr.H @@ -215,12 +215,20 @@ public: //- Clear existing and transfer ownership. inline void reset(refPtr<T>&& other) noexcept; - //- Delete managed temporary object and set to (const) reference + //- Clear existing and set (const) reference inline void cref(const T& obj) noexcept; - //- Delete managed temporary object and set to (non-const) reference + //- Clear existing and set (const) reference to pointer content. + // A null pointer is permitted (treated as a managed pointer). + inline void cref(const T* p) noexcept; + + //- Clear existing and set (non-const) reference inline void ref(T& obj) noexcept; + //- Clear existing and set (non-const) reference to pointer content. + // A null pointer is permitted (treated as a managed pointer). + inline void ref(T* p) noexcept; + //- Swaps the managed object with other. inline void swap(refPtr<T>& other) noexcept; diff --git a/src/OpenFOAM/memory/refPtr/refPtrI.H b/src/OpenFOAM/memory/refPtr/refPtrI.H index 7a2019a27fb72f22bdfd326c089a49e99f1b024a..7736fa301f574fde665e0fd270917e7ea48e2788 100644 --- a/src/OpenFOAM/memory/refPtr/refPtrI.H +++ b/src/OpenFOAM/memory/refPtr/refPtrI.H @@ -300,6 +300,15 @@ inline void Foam::refPtr<T>::cref(const T& obj) noexcept } +template<class T> +inline void Foam::refPtr<T>::cref(const T* p) noexcept +{ + clear(); + ptr_ = const_cast<T*>(p); + type_ = (ptr_ ? CREF : PTR); +} + + template<class T> inline void Foam::refPtr<T>::ref(T& obj) noexcept { @@ -309,6 +318,15 @@ inline void Foam::refPtr<T>::ref(T& obj) noexcept } +template<class T> +inline void Foam::refPtr<T>::ref(T* p) noexcept +{ + clear(); + ptr_ = p; + type_ = (ptr_ ? REF : PTR); +} + + template<class T> inline void Foam::refPtr<T>::swap(refPtr<T>& other) noexcept { diff --git a/src/OpenFOAM/memory/tmp/tmp.H b/src/OpenFOAM/memory/tmp/tmp.H index f9e0fa52fcec4eeafa295d3d233476d80deec096..7a4272f0ee9655bd376942dddcb7779f09dfd2a3 100644 --- a/src/OpenFOAM/memory/tmp/tmp.H +++ b/src/OpenFOAM/memory/tmp/tmp.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -224,12 +224,20 @@ public: //- Clear existing and transfer ownership. inline void reset(tmp<T>&& other) noexcept; - //- Delete managed temporary object and set to (const) reference + //- Clear existing and set (const) reference inline void cref(const T& obj) noexcept; - //- Delete managed temporary object and set to (non-const) reference + //- Clear existing and set (const) reference to pointer content. + // A null pointer is permitted (treated as a managed pointer). + inline void cref(const T* p) noexcept; + + //- Clear existing and set to (non-const) reference inline void ref(T& obj) noexcept; + //- Clear existing and set (non-const) reference to pointer content. + // A null pointer is permitted (treated as a managed pointer). + inline void ref(T* p) noexcept; + //- Swaps the managed object with other. inline void swap(tmp<T>& other) noexcept; diff --git a/src/OpenFOAM/memory/tmp/tmpI.H b/src/OpenFOAM/memory/tmp/tmpI.H index 80650e3619840e2f9e996c65eae93f74799ee7b1..094eab36a7aaae1f4889c009a0f2c6fd8899413e 100644 --- a/src/OpenFOAM/memory/tmp/tmpI.H +++ b/src/OpenFOAM/memory/tmp/tmpI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -340,6 +340,15 @@ inline void Foam::tmp<T>::cref(const T& obj) noexcept } +template<class T> +inline void Foam::tmp<T>::cref(const T* p) noexcept +{ + clear(); + ptr_ = const_cast<T*>(p); + type_ = (ptr_ ? CREF : PTR); +} + + template<class T> inline void Foam::tmp<T>::ref(T& obj) noexcept { @@ -349,6 +358,15 @@ inline void Foam::tmp<T>::ref(T& obj) noexcept } +template<class T> +inline void Foam::tmp<T>::ref(T* p) noexcept +{ + clear(); + ptr_ = p; + type_ = (ptr_ ? REF : PTR); +} + + template<class T> inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept {