diff --git a/applications/test/autoPtr/Test-autoPtr.C b/applications/test/autoPtr/Test-autoPtr.C index 05508d8d075bc08e003823230f2e35561713f07f..a134d49fa8f87c0f87404f3200162212ad857451 100644 --- a/applications/test/autoPtr/Test-autoPtr.C +++ b/applications/test/autoPtr/Test-autoPtr.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -23,6 +23,7 @@ License \*---------------------------------------------------------------------------*/ +#include <memory> #include "autoPtr.H" #include "labelList.H" #include "ListOps.H" @@ -80,10 +81,26 @@ int main(int argc, char *argv[]) { auto list = autoPtr<labelList>::New(10, label(-1)); - Info<<"create: " << list() << nl; + Info<<"create: " << *list << nl; + + const labelList* plist = list; + + Info<<"pointer: " << uintptr_t(plist) << nl + <<"content: " << *plist << nl; Info<<"create: " << autoPtr<labelList>::New(10, label(-1))() << nl << nl; + + // Transfer to unique_ptr + std::unique_ptr<labelList> list2(list.release()); + + Info<<"move to unique_ptr: " << *list2 << nl; + Info<<"old is " << Switch(bool(list)) << nl; + + autoPtr<labelList> list3(list2.release()); + + Info<<"move unique to autoPtr: " << *list3 << nl; + Info<<"old is " << Switch(bool(list2)) << nl; } // Confirm that forwarding with move construct actually works as expected diff --git a/src/OpenFOAM/memory/autoPtr/autoPtr.H b/src/OpenFOAM/memory/autoPtr/autoPtr.H index cf9efd44f82b83c00feb37583729766c134075c6..fd7598be8422a187567b136db78033a396e3bf98 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtr.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtr.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -121,17 +121,19 @@ public: inline explicit autoPtr(autoPtr<U>&& ap); //- A move construct disguised as a copy construct (transfers ownership) - // \remark This is a non-standard definition, and should ideally be - // marked as deleted - pending cleanup of code currently relying - // on this behaviour. + // \remark This should ideally be deleted - pending cleanup of code + // currently relying on this behaviour. #ifdef Foam_autoPtr_copyConstruct - inline autoPtr(const autoPtr<T>& ap) noexcept; + autoPtr(const autoPtr<T>& ap) noexcept + : + ptr_(const_cast<autoPtr<T>&>(ap).release()) + {} #else autoPtr(const autoPtr<T>& ap) = delete; #endif - //- Destructs the managed object if such is present + //- Deletes the managed object if such is present inline ~autoPtr() noexcept; @@ -179,16 +181,8 @@ public: inline void reset(T* p = nullptr) noexcept; //- Delete managed object and set to new given pointer - // \remark This is a non-standard definition, but may provide better - // code documentation than a simple move assign would. - inline void reset(autoPtr<T>&& other) noexcept; - - //- Delete managed object and set to new given pointer - //- Identical behaviour to reset(). - // \note Provided for backward compatibility - the older version - // enforced a run-time check (Fatal if pointer was already set) - // but this was rarely used. - inline void set(T* p) noexcept; + // \remark Same as move assign, but better for code documentation + inline void reset(autoPtr<T>&& ap) noexcept; //- Swaps the managed object with other autoPtr. inline void swap(autoPtr<T>& other) noexcept; @@ -239,6 +233,12 @@ public: operator const T&() const = delete; #endif + //- Cast to pointer type + operator const T*() const noexcept { return get(); } + + //- Cast to pointer type + operator T*() noexcept { return get(); } + //- True if the managed pointer is non-null explicit inline operator bool() const noexcept; @@ -252,17 +252,27 @@ public: #ifdef Foam_autoPtr_copyAssign //- A move assignment disguised as a copy assignment // \remark Non-standard definition - should just be movable - inline void operator=(const autoPtr<T>& ap) noexcept; + void operator=(const autoPtr<T>& ap) noexcept + { + operator=(std::move(const_cast<autoPtr<T>&>(ap))); + } #else void operator=(const autoPtr<T>& ap) = delete; #endif - //- Allow reset via assignment from literal nullptr + //- Clear via assignment from literal nullptr inline void operator=(std::nullptr_t) noexcept; // Housekeeping + //- Deprecated(2018-02) Identical to reset(). + // \note Provided for backward compatibility - the older version + // enforced a run-time check (Fatal if pointer was already set) + // but this was rarely used. + // \deprecated(2018-02) Identical to reset(). + void set(T* p) noexcept { reset(p); } + //- Deprecated(2018-02) No copy assignment from plain pointer // \deprecated(2018-02) Convenient, but uncontrolled access void operator=(T* p) = delete; diff --git a/src/OpenFOAM/memory/autoPtr/autoPtrI.H b/src/OpenFOAM/memory/autoPtr/autoPtrI.H index 3d2214e96e6a28ad4d31f727c515aa6844da8197..9baaf8290a8c1c4e341cac4a07f3f526b43e6227 100644 --- a/src/OpenFOAM/memory/autoPtr/autoPtrI.H +++ b/src/OpenFOAM/memory/autoPtr/autoPtrI.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2017 OpenFOAM Foundation @@ -84,15 +84,6 @@ inline Foam::autoPtr<T>::autoPtr(autoPtr<U>&& ap) {} -#ifdef Foam_autoPtr_copyConstruct -template<class T> -inline Foam::autoPtr<T>::autoPtr(const autoPtr<T>& ap) noexcept -: - ptr_(const_cast<autoPtr<T>&>(ap).release()) -{} -#endif - - // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // template<class T> @@ -177,13 +168,6 @@ inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept } -template<class T> -inline void Foam::autoPtr<T>::set(T* p) noexcept -{ - reset(p); -} - - template<class T> inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept { @@ -290,15 +274,6 @@ inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept } -#ifdef Foam_autoPtr_copyAssign -template<class T> -inline void Foam::autoPtr<T>::operator=(const autoPtr<T>& ap) noexcept -{ - operator=(std::move(const_cast<autoPtr<T>&>(ap))); -} -#endif - - template<class T> inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept {