Skip to content

ludus::foundation::SharedPtr

Ludus Wiki · API overview · Browse API

Class

Atomic shared owner of one object created by TryMakeShared.
Holds two words: the typed view and the controller of the original allocation. Copies share ownership; the final release destroys the exact created object (never through the view's type) and keeps the controller alive for remaining WeakPtr observers. An upcast changes only the view.
There is no constructor or Reset that adopts a raw pointer: the only source of ownership is TryMakeShared (or WeakPtr::Lock of an object it created). Never create a second owner from Get() or this.
Template parameterDescription
ElementType
Owned object type, optionally cv-qualified. Arrays, references, functions and void are rejected. It may be incomplete where an owner is declared, copied or destroyed: destruction runs through the creator's type-erased thunk.

Note

Thread safety: distinct owner values that share one object may be copied, moved and destroyed concurrently. One owner variable is not synchronized: concurrent reads and writes of the same SharedPtr value need external synchronization. Ownership does not synchronize access to the object; prefer immutable SharedPtr<const T> snapshots.

Note

The final release runs the destructor synchronously on whichever thread releases last. Objects that need thread-affine or deferred retirement (windows, GPU, audio) do not belong in a SharedPtr.

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::SharedPtr

Members

detail::SharedOwnerAccess

friend struct detail::SharedOwnerAccess
Lets TryMakeShared adopt the hold of a newly created controller; not caller API.

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

operator==

LUDUS_INLINE bool operator==(const SharedPtr &owner, nullptr_t) noexcept
Reports whether owner is empty (also provides !=, and either operand order).

Related types: nullptr_t

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

Methods

SharedPtr

constexpr ludus::foundation::SharedPtr< ElementType >::SharedPtr() noexcept=default
Constructs an empty owner.

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

SharedPtr

constexpr ludus::foundation::SharedPtr< ElementType >::SharedPtr(nullptr_t) noexcept
Constructs an empty owner.

Related types: nullptr_t

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

SharedPtr

ludus::foundation::SharedPtr< ElementType >::SharedPtr(const SharedPtr &other) noexcept
Shares other's object, adding one strong owner.
ParameterDescription
other
Owner to copy; may be empty.

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

SharedPtr

template <typename OtherElementType>
ludus::foundation::SharedPtr< ElementType >::SharedPtr(const SharedPtr< OtherElementType > &other) noexcept
requires detail::SharedViewConversion<OtherElementType, ElementType>
Shares other's object through an upcast or cv-qualified view.
The view adjustment is computed while other keeps the object alive; destruction remains that of the created type.
ParameterDescription
other
Owner to copy; may be empty.

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

SharedPtr

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

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

SharedPtr

template <typename OtherElementType>
ludus::foundation::SharedPtr< ElementType >::SharedPtr(SharedPtr< OtherElementType > &&other) noexcept
requires detail::SharedViewConversion<OtherElementType, ElementType>
Takes other's ownership through an upcast or cv-qualified view, leaving other empty.

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

~SharedPtr

ludus::foundation::SharedPtr< ElementType >::~SharedPtr()
Releases this owner's hold, if any.

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

operator=

SharedPtr & ludus::foundation::SharedPtr< ElementType >::operator=(const SharedPtr &other) noexcept
Shares other's object, then releases the previous one.
Self-assignment leaves the owner unchanged. Otherwise the incoming hold is acquired before the previous hold is released, so assigning from a value owned by the previous object is safe.

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

operator=

template <typename OtherElementType>
SharedPtr & ludus::foundation::SharedPtr< ElementType >::operator=(const SharedPtr< OtherElementType > &other) noexcept
requires detail::SharedViewConversion<OtherElementType, ElementType>
Shares other's object through a converted view, then releases the previous one.

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

operator=

SharedPtr & ludus::foundation::SharedPtr< ElementType >::operator=(SharedPtr &&other) noexcept
Takes other's ownership, then releases the previous one.
Self-move leaves the owner unchanged. Otherwise both source fields are staged and other emptied before the previous hold is released, so head = Move(head->Next) is valid.

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

operator=

template <typename OtherElementType>
SharedPtr & ludus::foundation::SharedPtr< ElementType >::operator=(SharedPtr< OtherElementType > &&other) noexcept
requires detail::SharedViewConversion<OtherElementType, ElementType>
Takes other's ownership through a converted view, then releases the previous one.

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

operator=

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

Related types: nullptr_t

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

Reset

void ludus::foundation::SharedPtr< ElementType >::Reset() noexcept
Releases this owner's hold, leaving it empty.
The owner is detached before the release, so a destructor run by this call observes an empty owner. It must not access this owner otherwise.

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

Swap

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

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

Get

LUDUS_INLINE ElementType * ludus::foundation::SharedPtr< ElementType >::Get() const noexcept
Borrows the typed view without affecting ownership.

Returns

The shared object, or nullptr for an empty owner. Valid only while some strong owner keeps the object alive.

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

operator*

LUDUS_INLINE ElementType & ludus::foundation::SharedPtr< ElementType >::operator*() const noexcept
Accesses the shared object.

Precondition

The owner is not empty. Development builds assert this.

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

operator->

LUDUS_INLINE ElementType * ludus::foundation::SharedPtr< ElementType >::operator->() const noexcept
Accesses a member of the shared object.

Precondition

The owner is not empty. Development builds assert this.

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

operator bool

explicit LUDUS_INLINE ludus::foundation::SharedPtr< ElementType >::operator bool() const noexcept
Reports whether the owner holds an object.

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