TheAtlasEngine
 
Loading...
Searching...
No Matches
atlas::memory::weak_ptr< T > Class Template Reference

A weak reference to a strong_ptr. More...

#include <memory.hpp>

Public Types

using element_type = T
 

Public Member Functions

 weak_ptr () noexcept=default
 Default constructor creates empty weak_ptr.
 
 weak_ptr (strong_ptr< T > const &p_strong) noexcept
 Create weak_ptr from strong_ptr.
 
 weak_ptr (weak_ptr const &p_other) noexcept
 Copy constructor.
 
 weak_ptr (weak_ptr &&p_other) noexcept
 Move constructor.
 
template<typename U >
requires (std::is_convertible_v<U*, T*>)
 weak_ptr (weak_ptr< U > const &p_other) noexcept
 Converting copy constructor.
 
template<typename U >
requires (std::is_convertible_v<U*, T*>)
 weak_ptr (weak_ptr< U > &&p_other) noexcept
 Converting move constructor.
 
template<typename U >
requires (std::is_convertible_v<U*, T*>)
 weak_ptr (strong_ptr< U > const &p_other) noexcept
 Converting constructor from strong_ptr.
 
 ~weak_ptr ()
 Destructor.
 
weak_ptroperator= (weak_ptr const &p_other) noexcept
 Copy assignment operator.
 
weak_ptroperator= (weak_ptr &&p_other) noexcept
 Move assignment operator.
 
weak_ptroperator= (strong_ptr< T > const &p_strong) noexcept
 Assignment from strong_ptr.
 
void swap (weak_ptr &p_other) noexcept
 Swap the contents of this weak_ptr with another.
 
bool expired () const noexcept
 Check if the referenced object has been destroyed.
 
optional_ptr< T > lock () const noexcept
 Attempt to obtain a strong_ptr to the referenced object.
 
auto use_count () const noexcept
 Get the current strong reference count.
 

Friends

template<typename U >
class strong_ptr
 

Detailed Description

template<typename T>
class atlas::memory::weak_ptr< T >

A weak reference to a strong_ptr.

weak_ptr provides a non-owning reference to an object managed by strong_ptr. It can be used to break reference cycles or to create an optional_ptr.

A weak_ptr doesn't increase the strong reference count, so it doesn't prevent the object from being destroyed when the last strong_ptr goes away.

Example usage:

// Create a strong_ptr to an object
auto ptr = hal::make_strong_ptr<my_driver>(allocator, args...);
// Create a weak reference
weak_ptr<my_driver> weak = ptr;
// Later, try to get a strong reference
if (auto locked = weak.lock()) {
// Use the object via locked
locked->doSomething();
} else {
// Object has been destroyed
}
Template Parameters
TThe type of the referenced object

Constructor & Destructor Documentation

◆ weak_ptr() [1/6]

template<typename T >
atlas::memory::weak_ptr< T >::weak_ptr ( strong_ptr< T > const &  p_strong)
inlinenoexcept

Create weak_ptr from strong_ptr.

Parameters
p_strongThe strong_ptr to create a weak reference to

◆ weak_ptr() [2/6]

template<typename T >
atlas::memory::weak_ptr< T >::weak_ptr ( weak_ptr< T > const &  p_other)
inlinenoexcept

Copy constructor.

Parameters
p_otherThe weak_ptr to copy from

◆ weak_ptr() [3/6]

template<typename T >
atlas::memory::weak_ptr< T >::weak_ptr ( weak_ptr< T > &&  p_other)
inlinenoexcept

Move constructor.

Parameters
p_otherThe weak_ptr to move from

◆ weak_ptr() [4/6]

template<typename T >
template<typename U >
requires (std::is_convertible_v<U*, T*>)
atlas::memory::weak_ptr< T >::weak_ptr ( weak_ptr< U > const &  p_other)
inlinenoexcept

Converting copy constructor.

Creates a weak_ptr of T from a weak_ptr of U where U is convertible to T.

Template Parameters
UA type convertible to T
Parameters
p_otherThe weak_ptr to copy from

◆ weak_ptr() [5/6]

template<typename T >
template<typename U >
requires (std::is_convertible_v<U*, T*>)
atlas::memory::weak_ptr< T >::weak_ptr ( weak_ptr< U > &&  p_other)
inlinenoexcept

Converting move constructor.

Moves a weak_ptr of U to a weak_ptr T where U is convertible to T.

Template Parameters
UA type convertible to T
Parameters
p_otherThe weak_ptr to move from

◆ weak_ptr() [6/6]

template<typename T >
template<typename U >
requires (std::is_convertible_v<U*, T*>)
atlas::memory::weak_ptr< T >::weak_ptr ( strong_ptr< U > const &  p_other)
inlinenoexcept

Converting constructor from strong_ptr.

Creates a weak_ptr<T> from a strong_ptr where U is convertible to T.

Template Parameters
UA type convertible to T
Parameters
p_otherThe strong_ptr to create a weak reference to

◆ ~weak_ptr()

template<typename T >
atlas::memory::weak_ptr< T >::~weak_ptr ( )
inline

Destructor.

Decrements the weak reference count and potentially deallocates memory if this was the last reference.

Member Function Documentation

◆ expired()

template<typename T >
bool atlas::memory::weak_ptr< T >::expired ( ) const
inlinenoexcept

Check if the referenced object has been destroyed.

Returns
true if the object has been destroyed, false otherwise

◆ lock()

template<typename T >
optional_ptr< T > atlas::memory::weak_ptr< T >::lock ( ) const
inlinenoexcept

Attempt to obtain a strong_ptr to the referenced object.

Implement weak_ptr::lock() now that optional_ptr is defined.

If the object still exists, returns an optional_ptr containing a strong_ptr to it. Otherwise, returns an empty optional_ptr.

Returns
An optional_ptr that is either empty or contains a strong_ptr

This function attempts to obtain a strong_ptr from a weak_ptr. If the referenced object still exists, it returns an optional_ptr containing a strong_ptr to it. Otherwise, it returns an empty optional_ptr.

Template Parameters
TThe type of the referenced object
Returns
An optional_ptr that is either empty or contains a strong_ptr

◆ operator=() [1/3]

template<typename T >
weak_ptr & atlas::memory::weak_ptr< T >::operator= ( strong_ptr< T > const &  p_strong)
inlinenoexcept

Assignment from strong_ptr.

Parameters
p_strongThe strong_ptr to create a weak reference to
Returns
Reference to *this

◆ operator=() [2/3]

template<typename T >
weak_ptr & atlas::memory::weak_ptr< T >::operator= ( weak_ptr< T > &&  p_other)
inlinenoexcept

Move assignment operator.

Parameters
p_otherThe weak_ptr to move from
Returns
Reference to *this

◆ operator=() [3/3]

template<typename T >
weak_ptr & atlas::memory::weak_ptr< T >::operator= ( weak_ptr< T > const &  p_other)
inlinenoexcept

Copy assignment operator.

Parameters
p_otherThe weak_ptr to copy from
Returns
Reference to *this

◆ swap()

template<typename T >
void atlas::memory::weak_ptr< T >::swap ( weak_ptr< T > &  p_other)
inlinenoexcept

Swap the contents of this weak_ptr with another.

Parameters
p_otherThe weak_ptr to swap with

◆ use_count()

template<typename T >
auto atlas::memory::weak_ptr< T >::use_count ( ) const
inlinenoexcept

Get the current strong reference count.

This is primarily for testing purposes.

Returns
The number of strong references to the managed object

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