Skip to content

ludus::foundation::WeakPtr

Ludus Wiki · API overview · Browse API

Class

Non-owning observer of an object owned by SharedPtr.
Keeps the controller (and the combined allocation) alive but not the object. Lock() obtains a temporary strong owner while the object is live and returns empty forever after the final strong release; a weak value never follows a replacement object. There is deliberately no Get, dereference or arrow operator: access the object only through Lock().
Template parameterDescription
ElementType
Observed object type, optionally cv-qualified. Arrays, references, functions and void are rejected.

Note

Thread safety: as for SharedPtr, distinct values may be used concurrently; one variable needs external synchronization.

Note

Retained weak values keep the whole combined block allocated, including the dead inline object storage, until the last weak value is released.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

#include <ludus/foundation/memory/shared_ptr.hpp>
template <typename ElementType>
class ludus::foundation::WeakPtr

Methods

WeakPtr

constexpr ludus::foundation::WeakPtr< ElementType >::WeakPtr() noexcept=default
Constructs an empty, expired observer.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

constexpr ludus::foundation::WeakPtr< ElementType >::WeakPtr(nullptr_t) noexcept
Constructs an empty, expired observer.

Related types: nullptr_t

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

template <typename OtherElementType>
ludus::foundation::WeakPtr< ElementType >::WeakPtr(const SharedPtr< OtherElementType > &owner) noexcept
requires std::is_convertible_v<OtherElementType*, ElementType*>
Observes owner's object, possibly through an upcast or cv-qualified view.
The view adjustment is computed here, while owner keeps the object alive.
ParameterDescription
owner
Strong owner to observe; an empty owner yields an empty observer.

Related types: SharedPtr

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

ludus::foundation::WeakPtr< ElementType >::WeakPtr(const WeakPtr &other) noexcept
Observes the same object as other.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

template <typename OtherElementType>
ludus::foundation::WeakPtr< ElementType >::WeakPtr(const WeakPtr< OtherElementType > &other) noexcept
requires detail::WeakViewConversion<OtherElementType, ElementType>
Observes the same object as other through a more cv-qualified view.
Derived-to-Base conversion from a weak value is rejected: the object may already be destroyed, so the adjustment cannot be computed safely.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

ludus::foundation::WeakPtr< ElementType >::WeakPtr(WeakPtr &&other) noexcept
Takes other's observation without reference traffic, leaving other empty.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

WeakPtr

template <typename OtherElementType>
ludus::foundation::WeakPtr< ElementType >::WeakPtr(WeakPtr< OtherElementType > &&other) noexcept
requires detail::WeakViewConversion<OtherElementType, ElementType>
Takes other's observation through a more cv-qualified view, leaving other empty.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

~WeakPtr

ludus::foundation::WeakPtr< ElementType >::~WeakPtr()
Releases this observer's weak hold, if any.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

operator=

WeakPtr & ludus::foundation::WeakPtr< ElementType >::operator=(const WeakPtr &other) noexcept
Observes other's object, then releases the previous weak hold.
Self-assignment leaves the observer unchanged.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

operator=

template <typename OtherElementType>
WeakPtr & ludus::foundation::WeakPtr< ElementType >::operator=(const SharedPtr< OtherElementType > &owner) noexcept
requires std::is_convertible_v<OtherElementType*, ElementType*>
Observes owner's object, then releases the previous weak hold.
ParameterDescription
owner
Strong owner to observe; an empty owner empties this observer.

Related types: SharedPtr

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

operator=

WeakPtr & ludus::foundation::WeakPtr< ElementType >::operator=(WeakPtr &&other) noexcept
Takes other's observation, then releases the previous weak hold.
Self-move leaves the observer unchanged.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

operator=

WeakPtr & ludus::foundation::WeakPtr< ElementType >::operator=(nullptr_t) noexcept
Releases this observer's weak hold, leaving it empty.

Related types: nullptr_t

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

Reset

void ludus::foundation::WeakPtr< ElementType >::Reset() noexcept
Releases this observer's weak hold, leaving it empty.
The observer is detached before the release.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

Swap

void ludus::foundation::WeakPtr< ElementType >::Swap(WeakPtr &inoutOther) noexcept
Exchanges view and controller with other without reference traffic.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

IsExpired

bool ludus::foundation::WeakPtr< ElementType >::IsExpired() const noexcept
Snapshot of whether the observed object has been released.
true is permanent. false can become stale immediately on another thread: never check and then access; use the owner returned by Lock().

Returns

true for an empty observer or once the final strong release committed.

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp

Lock

SharedPtr< ElementType > ludus::foundation::WeakPtr< ElementType >::Lock() const noexcept
Obtains a strong owner of the observed object if it is still live.
Promotion succeeds only while the strong count is nonzero; once the final strong release commits, every later lock returns empty. The returned view is the one stored at observation time; no adjustment is recomputed.

Returns

A new strong owner, or an empty owner if this observer is empty or expired.

Related types: SharedPtr

Declared in modules/foundation/memory/include/ludus/foundation/memory/shared_ptr.hpp