Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. pw_allocator
  5. Core interfaces
  6. pw::WeakPtr< T > Class Template Reference
Loading...
Searching...
No Matches
pw::WeakPtr< T > Class Template Reference

Overview

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

A std::weak_ptr<T>-like type that integrates with pw::SharedPtr.

Template Parameters
TThe type being pointed to. This may be an array type, e.g. pw::WeakPtr<T[]>.
Inheritance diagram for pw::WeakPtr< T >:

Public Types

using element_type = typename Base::element_type
 

Public Member Functions

constexpr WeakPtr () noexcept=default
 Creates an empty (nullptr) instance.
 
constexpr WeakPtr (std::nullptr_t) noexcept
 Creates an empty (nullptr) instance.
 
 WeakPtr (const WeakPtr &other) noexcept
 Copy-constructs a WeakPtr<T> from a WeakPtr<T>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 WeakPtr (const WeakPtr< U > &other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 WeakPtr (const SharedPtr< U > &other) noexcept
 
 WeakPtr (WeakPtr &&other) noexcept
 Move-constructs a WeakPtr<T> from a WeakPtr<T>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 WeakPtr (WeakPtr< U > &&other) noexcept
 
WeakPtr & operator= (const WeakPtr &other) noexcept
 Copy-assigns a WeakPtr<T> from a WeakPtr<T>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & operator= (const WeakPtr< U > &other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & operator= (const SharedPtr< U > &other) noexcept
 
WeakPtr & operator= (WeakPtr &&other) noexcept
 Move-assigns a WeakPtr<T> from a WeakPtr<T>.
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & operator= (WeakPtr< U > &&other) noexcept
 
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
 operator WeakPtr< U > () const
 
void reset () noexcept
 
void swap (WeakPtr &other) noexcept
 Swaps the managed pointer and control block of this and another object.
 
int32_t use_count () const noexcept
 
bool expired () const noexcept
 
SharedPtr< T > Lock () const noexcept
 
template<typename PtrType >
bool owner_before (const PtrType &other) const noexcept
 
template<typename U , typename >
WeakPtr< T > & operator= (const WeakPtr< U > &other) noexcept
 
template<typename U , typename >
WeakPtr< T > & operator= (const SharedPtr< U > &other) noexcept
 
template<typename U , typename >
WeakPtr< T > & operator= (WeakPtr< U > &&other) noexcept
 

Constructor & Destructor Documentation

◆ WeakPtr() [1/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::WeakPtr< T >::WeakPtr ( const WeakPtr< U > &  other)
inlinenoexcept

Copy-constructs a WeakPtr<T> from a WeakPtr<U>.

This allows not only pure copy construction where T == U, but also converting construction where T is a base class of U.

◆ WeakPtr() [2/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::WeakPtr< T >::WeakPtr ( const SharedPtr< U > &  other)
inlinenoexcept

Copy-constructs a WeakPtr<T> from a SharedPtr<U>.

This allows converting copy construction where T is a base class of U.

◆ WeakPtr() [3/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::WeakPtr< T >::WeakPtr ( WeakPtr< U > &&  other)
inlinenoexcept

Move-constructs a WeakPtr<T> from a WeakPtr<U>.

This allows not only pure move construction where T == U, but also converting construction where T is a base class of U.

Member Function Documentation

◆ expired()

template<typename T >
bool pw::WeakPtr< T >::expired ( ) const
inlinenoexcept

Returns true if the associated object has been destroyed; otherwise returns false.

◆ operator WeakPtr< U >()

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
pw::WeakPtr< T >::operator WeakPtr< U > ( ) const
inlineexplicit

Explicit conversion operator for downcasting.

If an arbitrary type A derives from another type B, a shared or weak pointer to A can be automatically upcast to one of type B. This operator performs the reverse operation with an explicit cast.

pw::SharedPtr<A> a1 = allocator.MakeShared<A>();
pw::WeakPtr<A> a2 = static_cast<pw::WeakPtr<A>>(b);
Definition: shared_ptr.h:81
Definition: weak_ptr.h:41

◆ operator=() [1/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & pw::WeakPtr< T >::operator= ( const SharedPtr< U > &  other)
noexcept

Copy-assigns a WeakPtr<T> from a SharedPtr<U>.

This allows converting copy assignment where T is a base class of U.

◆ operator=() [2/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & pw::WeakPtr< T >::operator= ( const WeakPtr< U > &  other)
noexcept

Copy-assigns a WeakPtr<T> from a WeakPtr<U>.

This allows not only pure copy assignment where T == U, but also converting assignment where T is a base class of U.

◆ operator=() [3/3]

template<typename T >
template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>>
WeakPtr & pw::WeakPtr< T >::operator= ( WeakPtr< U > &&  other)
noexcept

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

This allows not only pure move assignment where T == U, but also converting assignment where T is a base class of U.

◆ owner_before()

template<typename T >
template<typename PtrType >
bool pw::WeakPtr< T >::owner_before ( const PtrType &  other) const
inlinenoexcept

Checks whether this precedes other based on an ordering of their control blocks.

◆ use_count()

template<typename T >
int32_t pw::WeakPtr< T >::use_count ( ) const
inlinenoexcept

Returns the number of shared pointers to the associated object, or 0 if this object is empty.


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