ENH: allow use of std::unique_ptr directly as Foam::unique_ptr

- placed in stdFoam.H (as well as autoPtr.H) for general availability

STYLE: minor adjustments to autoPtr code layout
This commit is contained in:
Mark Olesen 2020-03-25 13:21:11 +01:00
parent a5dbd27ddb
commit 8a4ea197cd
3 changed files with 27 additions and 77 deletions

View File

@ -47,6 +47,7 @@ SeeAlso
#define stdFoam_H
#include <initializer_list>
#include <memory>
#include <utility>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -75,6 +76,11 @@ namespace Foam
//- Additional OpenFOAM modules
namespace Module {}
// Standard items
//- Allow use of std::unique_ptr directly as Foam::unique_ptr
using std::unique_ptr;
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -51,7 +51,7 @@ SourceFiles
#define Foam_autoPtr_copyAssign
#define Foam_autoPtr_castOperator
#include <utility>
#include "stdFoam.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -143,26 +143,26 @@ public:
// Check
//- True if the managed pointer is null
inline bool empty() const noexcept;
bool empty() const noexcept { return !ptr_; }
//- True if the managed pointer is non-null
inline bool valid() const noexcept;
bool valid() const noexcept { return ptr_; }
// Access
//- Return pointer to managed object without nullptr checking.
// Pointer remains under autoPtr management.
inline T* get() noexcept;
T* get() noexcept { return ptr_; }
//- Return const pointer to managed object without nullptr checking.
// Pointer remains under autoPtr management.
inline const T* get() const noexcept;
const T* get() const noexcept { return ptr_; }
//- Return reference to the managed object without nullptr checking.
// When get() == nullptr, additional guards may be required to avoid
// inadvertent access to a nullptr.
inline T& ref();
T& ref() { return *ptr_; }
// Edit
@ -170,13 +170,13 @@ public:
//- Return pointer to the managed object and release ownership.
inline T* release() noexcept;
//- Return pointer to the managed object and release ownership.
//- Identical behaviour to release().
// \note Provided for method naming consistent with Foam::tmp
inline T* ptr() noexcept;
//- Same as \c release().
// \remark Method naming consistent with Foam::tmp
T* ptr() noexcept { return release(); }
//- Delete managed object and set pointer to nullptr
inline void clear() noexcept;
//- Same as \c reset(nullptr)
// \remark Method naming consistent with Foam::tmp
void clear() noexcept { reset(nullptr); }
//- Delete managed object and set to new given pointer
inline void reset(T* p = nullptr) noexcept;
@ -191,7 +191,7 @@ public:
// Other
//- Construct copy by invoking clone on underlying managed object
//- Copy construct by invoking clone on underlying managed object
// A no-op if no pointer is managed
// \param args list of arguments for clone
template<class... Args>
@ -241,8 +241,8 @@ public:
//- Cast to pointer type
operator T*() noexcept { return get(); }
//- True if the managed pointer is non-null - same as valid()
explicit inline operator bool() const noexcept;
//- True if the managed pointer is non-null
explicit operator bool() const noexcept { return ptr_; }
//- Transfer object ownership from parameter
inline void operator=(autoPtr<T>&& ap) noexcept;
@ -262,7 +262,7 @@ public:
void operator=(const autoPtr<T>& ap) = delete;
#endif
//- Clear via assignment from literal nullptr
//- Reset via assignment from literal nullptr
inline void operator=(std::nullptr_t) noexcept;

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2016-2019 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -96,41 +96,6 @@ inline Foam::autoPtr<T>::~autoPtr() noexcept
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline bool Foam::autoPtr<T>::empty() const noexcept
{
return !ptr_;
}
template<class T>
inline bool Foam::autoPtr<T>::valid() const noexcept
{
return ptr_;
}
template<class T>
inline T* Foam::autoPtr<T>::get() noexcept
{
return ptr_;
}
template<class T>
inline const T* Foam::autoPtr<T>::get() const noexcept
{
return ptr_;
}
template<class T>
inline T& Foam::autoPtr<T>::ref()
{
return *ptr_;
}
template<class T>
inline T* Foam::autoPtr<T>::release() noexcept
{
@ -140,20 +105,6 @@ inline T* Foam::autoPtr<T>::release() noexcept
}
template<class T>
inline T* Foam::autoPtr<T>::ptr() noexcept
{
return release();
}
template<class T>
inline void Foam::autoPtr<T>::clear() noexcept
{
reset(nullptr);
}
template<class T>
inline void Foam::autoPtr<T>::reset(T* p) noexcept
{
@ -200,7 +151,7 @@ inline T& Foam::autoPtr<T>::operator*()
if (!ptr_)
{
FatalErrorInFunction
<< "object of type " << typeid(T).name() << " is unallocated"
<< "unallocated autoPtr of type " << typeid(T).name()
<< abort(FatalError);
}
return *ptr_;
@ -220,7 +171,7 @@ inline T* Foam::autoPtr<T>::operator->()
if (!ptr_)
{
FatalErrorInFunction
<< "object of type " << typeid(T).name() << " is unallocated"
<< "unallocated autoPtr of type " << typeid(T).name()
<< abort(FatalError);
}
return ptr_;
@ -248,13 +199,6 @@ inline const T& Foam::autoPtr<T>::operator()() const
}
template<class T>
inline Foam::autoPtr<T>::operator bool() const noexcept
{
return ptr_;
}
template<class T>
inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
{
@ -281,7 +225,7 @@ inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
template<class T>
inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
{
reset();
reset(nullptr);
}