template<typename T>
class pw::UniquePtr< T >
A std::unique_ptr<T>
-like type that integrates with pw::Deallocator
.
This is a RAII smart pointer that deallocates any memory it points to when it goes out of scope.
Its most notable difference from std::unique_ptr<T>
is that it cannot be constructed from a T*
. Use Allocator::MakeUnique<T>(...)
instead.
- Template Parameters
-
T | The type being pointed to. This may be an array type, e.g. pw::UniquePtr<T[]> . |
TODO(b/399441816): This class should be marked final, but at least one downstream has extended it. Resolve and mark final.
|
constexpr | UniquePtr () noexcept |
|
constexpr | UniquePtr (std::nullptr_t) noexcept |
|
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> |
| UniquePtr (UniquePtr< U > &&other) noexcept |
|
| ~UniquePtr () |
| Frees any currently-held value.
|
|
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> |
UniquePtr & | operator= (UniquePtr< U > &&other) noexcept |
|
UniquePtr & | operator= (std::nullptr_t) noexcept |
|
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> |
constexpr | operator UniquePtr< U > () && |
|
size_t | size () const |
|
Deallocator * | deallocator () const |
| Returns a pointer to the object that can destroy the value.
|
|
element_type * | Release () noexcept |
|
void | Reset () noexcept |
|
void | Swap (UniquePtr &other) |
| Swaps the managed pointer and deallocator of this and another object.
|
|
template<typename U , typename > |
UniquePtr< T > & | operator= (UniquePtr< U > &&other) noexcept |
|
|
| UniquePtr (element_type *value, Deallocator &deallocator) |
|
| UniquePtr (element_type *value, size_t size, Deallocator &deallocator) |
|
|
class | Deallocator |
| TODO(b/326509341): Remove when downstream consumers migrate.
|
|
constexpr bool | operator== (const UniquePtr &lhs, std::nullptr_t) |
|
constexpr bool | operator== (std::nullptr_t, const UniquePtr &rhs) |
|
constexpr bool | operator== (const UniquePtr &lhs, const UniquePtr &rhs) |
|
constexpr bool | operator!= (const UniquePtr &lhs, std::nullptr_t) |
|
constexpr bool | operator!= (std::nullptr_t, const UniquePtr &rhs) |
|
constexpr bool | operator!= (const UniquePtr &lhs, const UniquePtr &rhs) |
|
Constructs a UniquePtr
from an already-allocated value.
The deallocator MUST be able to deallocate the given value
. Typically, this implies it is the same object that allocated the value.
This constructor "adopts" the value, that is, it assumes responsibility for its lifetime. Callers should not access the value directly after this call, and MUST not deallocate the value directly or pass it to another managed pointer.
NOTE: Instances of this type are most commonly constructed using MakeUnique
. Prefer that method when possible.
template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
Move-constructs a UniquePtr<T>
from a UniquePtr<U>
.
This allows not only pure move construction where T == U
, but also converting construction where T
is a base class of U
, like UniquePtr<Base> base(deallocator.MakeUnique<Child>());
.
template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
Move-assigns a UniquePtr<T>
from a UniquePtr<U>
.
This operation frees the value currently stored in this
.
This allows not only pure move assignment where T == U
, but also converting assignment where T
is a base class of U
.