Pigweed
Loading...
Searching...
No Matches
pw::UniquePtr< T > Class Template Reference

#include <unique_ptr.h>

Public Types

using Base = ::pw::allocator::internal::BaseUniquePtr
 

Public Member Functions

constexpr UniquePtr ()
 
constexpr UniquePtr (std::nullptr_t)
 
template<typename U >
 UniquePtr (UniquePtr< U > &&other) noexcept
 
 UniquePtr (const UniquePtr &)=delete
 
UniquePtroperator= (const UniquePtr &)=delete
 
template<typename U >
UniquePtroperator= (UniquePtr< U > &&other) noexcept
 
UniquePtroperator= (std::nullptr_t)
 
 ~UniquePtr ()
 Destructs and deallocates any currently-held value.
 
Deallocatordeallocator () const
 Returns a pointer to the object that can destroy the value.
 
T * Release ()
 
void Reset ()
 
 operator bool () const =delete
 
bool operator== (std::nullptr_t) const
 Returns whether this UniquePtr is in an "empty" (nullptr) state.
 
bool operator!= (std::nullptr_t) const
 
T * get ()
 Returns the underlying (possibly null) pointer.
 
const T * get () const
 Returns the underlying (possibly null) pointer.
 
T * operator-> ()
 
const T * operator-> () const
 
T & operator* ()
 
const T & operator* () const
 
 UniquePtr (PrivateConstructorType, T *value, Deallocator *deallocator)
 

Friends

template<typename U >
class UniquePtr
 
class Deallocator
 TODO(b/326509341): Remove when downstream consumers migrate.
 

Additional Inherited Members

- Protected Types inherited from pw::allocator::internal::BaseUniquePtr
using Capability = ::pw::allocator::Capability
 
- Static Protected Member Functions inherited from pw::allocator::internal::BaseUniquePtr
static bool HasCapability (Deallocator *deallocator, Capability capability)
 
static void Deallocate (Deallocator *deallocator, void *ptr)
 

Detailed Description

template<typename T>
class pw::UniquePtr< T >

An RAII pointer to a value of type T stored in memory provided by a Deallocator.

This is analogous to std::unique_ptr, but includes a few differences in order to support Deallocator and encourage safe usage. Most notably, UniquePtr<T> cannot be constructed from a T*.

Constructor & Destructor Documentation

◆ UniquePtr() [1/4]

template<typename T >
constexpr pw::UniquePtr< T >::UniquePtr ( )
inlineconstexpr

Creates an empty (nullptr) instance.

NOTE: Instances of this type are most commonly constructed using Deallocator::MakeUnique.

◆ UniquePtr() [2/4]

template<typename T >
constexpr pw::UniquePtr< T >::UniquePtr ( std::nullptr_t  )
inlineconstexpr

Creates an empty (nullptr) instance.

NOTE: Instances of this type are most commonly constructed using Deallocator::MakeUnique.

◆ UniquePtr() [3/4]

template<typename T >
template<typename U >
pw::UniquePtr< T >::UniquePtr ( UniquePtr< U > &&  other)
inlinenoexcept

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>());.

◆ UniquePtr() [4/4]

template<typename T >
pw::UniquePtr< T >::UniquePtr ( PrivateConstructorType  ,
T *  value,
Deallocator deallocator 
)
inline

Private constructor that is public only for use with emplace and other in-place construction functions.

Constructs a UniquePtr from an already-allocated value.

NOTE: Instances of this type are most commonly constructed using Deallocator::MakeUnique.

Member Function Documentation

◆ operator bool()

template<typename T >
pw::UniquePtr< T >::operator bool ( ) const
explicitdelete

operator bool is not provided in order to ensure that there is no confusion surrounding if (foo) vs. if (*foo).

nullptr checking should instead use if (foo == nullptr).

◆ operator!=()

template<typename T >
bool pw::UniquePtr< T >::operator!= ( std::nullptr_t  ) const
inline

Returns whether this UniquePtr is not in an "empty" (nullptr) state.

◆ operator*()

template<typename T >
T & pw::UniquePtr< T >::operator* ( )
inline

Returns a reference to any underlying value.

The behavior of this operation is undefined if this UniquePtr is in an "empty" (nullptr) state.

◆ operator->()

template<typename T >
T * pw::UniquePtr< T >::operator-> ( )
inline

Permits accesses to members of T via my_unique_ptr->Member.

The behavior of this operation is undefined if this UniquePtr is in an "empty" (nullptr) state.

◆ operator=() [1/2]

template<typename T >
UniquePtr & pw::UniquePtr< T >::operator= ( std::nullptr_t  )
inline

Sets this UniquePtr to null, destructing and deallocating any currently-held value.

After this function returns, this UniquePtr will be in an "empty" (nullptr) state until a new value is assigned.

◆ operator=() [2/2]

template<typename T >
template<typename U >
UniquePtr & pw::UniquePtr< T >::operator= ( UniquePtr< U > &&  other)
inlinenoexcept

Move-assigns a UniquePtr<T> from a UniquePtr<U>.

This operation destructs and deallocates any 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, like UniquePtr<Base> base = deallocator.MakeUnique<Child>();.

◆ Release()

template<typename T >
T * pw::UniquePtr< T >::Release ( )
inline

Releases a value from the UniquePtr without destructing or deallocating it.

After this call, the object will have an "empty" (nullptr) value.

◆ Reset()

template<typename T >
void pw::UniquePtr< T >::Reset ( )
inline

Destructs and deallocates any currently-held value.

After this function returns, this UniquePtr will be in an "empty" (nullptr) state until a new value is assigned.

Friends And Related Function Documentation

◆ UniquePtr

template<typename T >
template<typename U >
friend class UniquePtr
friend

Allow converting move constructor and assignment to access fields of this class.

Without this, UniquePtr<U> would not be able to access fields of UniquePtr<T>.


The documentation for this class was generated from the following file: