Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends | List of all members
pw::UniquePtr< T > Class Template Reference
Inheritance diagram for pw::UniquePtr< T >:

Public Types

using pointer = typename Base::element_type *
 
using element_type = typename Base::element_type
 

Public Member Functions

constexpr UniquePtr () noexcept
 
constexpr UniquePtr (std::nullptr_t) noexcept
 
template<typename U >
 UniquePtr (UniquePtr< U > &&other) noexcept
 
 ~UniquePtr ()
 Frees any currently-held value.
 
template<typename U >
UniquePtroperator= (UniquePtr< U > &&other) noexcept
 
UniquePtroperator= (std::nullptr_t) noexcept
 
size_t size () const
 
Deallocatordeallocator () 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 >
UniquePtr< T > & operator= (UniquePtr< U > &&other) noexcept
 
 UniquePtr (element_type *value, Deallocator &deallocator)
 
 UniquePtr (element_type *value, size_t size, Deallocator &deallocator)
 

Friends

class Deallocator
 TODO(b/326509341): Remove when downstream consumers migrate.
 

Detailed Description

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
TThe 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.

Constructor & Destructor Documentation

◆ UniquePtr() [1/4]

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

Creates an empty (nullptr) instance.

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

◆ UniquePtr() [2/4]

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

Creates an empty (nullptr) instance.

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

◆ UniquePtr() [3/4]

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

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.

◆ UniquePtr() [4/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>());.

Member Function Documentation

◆ operator=() [1/2]

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

Sets this UniquePtr to null, freeing 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)
noexcept

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.

◆ Release()

template<typename T >
UniquePtr< T >::element_type * pw::UniquePtr< T >::Release
noexcept

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
noexcept

Destroys 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.

◆ size()

template<typename T >
size_t pw::UniquePtr< T >::size ( ) const
inline

Returns the number of elements allocated.

This will fail to compile if it is called on a non-array type UniquePtr.


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