docs
Loading...
Searching...
No Matches
WeakPtr< T >

#include <Ultralight/RefPtr.h>

Overview

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

A weak (non-owning) smart pointer that observes a WeakRefCounted object.

A WeakPtr does not keep its object alive. Use Lock() to atomically obtain a strong RefPtr to the object; the result is null if the object has already been destroyed.

Usage

WeakPtr<Bitmap> weak(bitmap);
if (RefPtr<Bitmap> strong = weak.Lock()) {
// The object is alive and stays alive while 'strong' is in scope.
}
static RefPtr< Bitmap > Create()
Create an empty Bitmap.
A nullable smart pointer.
Definition RefPtr.h:126
friend class WeakPtr
Definition RefPtr.h:521
@ BGRA8_UNORM_SRGB
Blue Green Red Alpha channels, 32-bits per pixel.
Definition Bitmap.h:33

Thread-safe: WeakPtr instances can be created, copied, and destroyed on any thread, and Lock() is race-free against concurrent destruction of the object.

Note
T must derive from WeakRefCounted; using WeakPtr with a plain RefCounted type is a compile error.
Note
Only construct a WeakPtr from a fully-constructed object: never from this inside a constructor.

Public Member Functions

constexpr WeakPtr ()
 Construct a null WeakPtr.
 WeakPtr (std::nullptr_t)
 Construct a null WeakPtr.
template<typename U>
 WeakPtr (const RefPtr< U > &strong)
 Construct a WeakPtr observing the object held by a RefPtr (may be null).
template<typename U>
 WeakPtr (U *instance)
 Construct a WeakPtr observing a raw pointer (may be null).
 WeakPtr (const WeakPtr &other)
 Copy constructor.
 WeakPtr (WeakPtr &&other)
 Move constructor.
 ~WeakPtr ()
WeakPtr & operator= (const WeakPtr &other)
WeakPtr & operator= (WeakPtr &&other)
WeakPtr & operator= (std::nullptr_t)
template<typename U>
WeakPtr & operator= (const RefPtr< U > &strong)
RefPtr< T > Lock () const
 Atomically take a strong reference to the observed object.
bool Expired () const
 Whether the observed object has been destroyed (or this WeakPtr is null).
void Reset ()
 Stop observing the object and become a null WeakPtr.
void swap (WeakPtr &other)
 Exchanges the observed objects of *this and other.

Friends

template<typename U>
class WeakPtr

Constructor & Destructor Documentation

◆ WeakPtr() [1/6]

template<typename T>
WeakPtr ( )
inlineconstexpr

Construct a null WeakPtr.

◆ WeakPtr() [2/6]

template<typename T>
WeakPtr ( std::nullptr_t )
inline

Construct a null WeakPtr.

◆ WeakPtr() [3/6]

template<typename T>
template<typename U>
WeakPtr ( const RefPtr< U > & strong)
inline

Construct a WeakPtr observing the object held by a RefPtr (may be null).

◆ WeakPtr() [4/6]

template<typename T>
template<typename U>
WeakPtr ( U * instance)
inlineexplicit

Construct a WeakPtr observing a raw pointer (may be null).

The object must currently be alive: the caller (or another owner) must hold a strong reference to it.

◆ WeakPtr() [5/6]

template<typename T>
WeakPtr ( const WeakPtr< T > & other)
inline

Copy constructor.

◆ WeakPtr() [6/6]

template<typename T>
WeakPtr ( WeakPtr< T > && other)
inline

Move constructor.

◆ ~WeakPtr()

template<typename T>
~WeakPtr ( )
inline

Member Function Documentation

◆ Expired()

template<typename T>
bool Expired ( ) const
inline

Whether the observed object has been destroyed (or this WeakPtr is null).

Warning
Racy hint only: the object may be destroyed by another thread immediately after this returns false. Never gate a dereference on it; use Lock() instead.

◆ Lock()

template<typename T>
RefPtr< T > Lock ( ) const
inline

Atomically take a strong reference to the observed object.

Returns
Returns a RefPtr keeping the object alive, or a null RefPtr if the object has been destroyed (or this WeakPtr is null).

◆ operator=() [1/4]

template<typename T>
template<typename U>
WeakPtr & operator= ( const RefPtr< U > & strong)
inline

◆ operator=() [2/4]

template<typename T>
WeakPtr & operator= ( const WeakPtr< T > & other)
inline

◆ operator=() [3/4]

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

◆ operator=() [4/4]

template<typename T>
WeakPtr & operator= ( WeakPtr< T > && other)
inline

◆ Reset()

template<typename T>
void Reset ( )
inline

Stop observing the object and become a null WeakPtr.

◆ swap()

template<typename T>
void swap ( WeakPtr< T > & other)
inline

Exchanges the observed objects of *this and other.

◆ WeakPtr

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

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