Skip to content

ludus::foundation::core::UniquePtr

Ludus Wiki · API overview · Browse API

Class

Move-only owner of one object and the deleter that destroys it.
The pointer and deleter form one ownership state: moves and swaps transfer both, and destruction always uses the deleter that accompanied the pointer. Every destruction, move and swap operation is noexcept. An empty owner never invokes its deleter.
Callbacks run by the deleter may release other owners but must not access or mutate the owner whose destruction, reset or assignment is executing. Do not destroy an owner while holding a registry or publication lock: move it out under the lock and release it after unlocking.
With an empty deleter, sizeof(UniquePtr<T>) == sizeof(T*).
Template parameterDescription
ElementType
Owned object type. May be incomplete where the owner is declared (PIMPL); it must be complete wherever the default deleter is instantiated. Array types are rejected; use a Ludus container for sequences and byte buffers.
DeleterType
Value deleter satisfying Deleter<DeleterType, ElementType>.

Note

Thread affinity: a single owner is not synchronized. Different owners may be used concurrently; one owner variable needs external synchronization for concurrent access.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

#include <ludus/foundation/base/pointer.hpp>
template <typename ElementType, Deleter< ElementType > DeleterType = DefaultDeleter<ElementType>>
class ludus::foundation::core::UniquePtr final

Methods

UniquePtr

constexpr ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr() noexcept
requires std::is_nothrow_default_constructible_v<DeleterType>
Constructs an empty owner with a value-initialized deleter.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

constexpr ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(nullptr_t) noexcept
requires std::is_nothrow_default_constructible_v<DeleterType>
Constructs an empty owner with a value-initialized deleter.

Related types: nullptr_t

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

explicit ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(ElementType *&inoutPointer) noexcept
requires std::is_nothrow_default_constructible_v<DeleterType>
Adopts inoutPointer with a value-initialized deleter and nulls the source.
ParameterDescription
inoutPointer
Uniquely owned object created for this deleter's protocol, or nullptr. Set to nullptr on return.

Precondition

No other owner holds the object.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(ElementType *&inoutPointer, DeleterType deleter) noexcept
requires std::is_nothrow_move_constructible_v<DeleterType>
Adopts inoutPointer together with the deleter that must destroy it.
ParameterDescription
inoutPointer
Uniquely owned object matching deleter's protocol, or nullptr. Set to nullptr on return.
deleter
Destruction protocol, moved into the owner.

Precondition

No other owner holds the object.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(const UniquePtr &)=delete
Owners are not copyable.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(UniquePtr &&other) noexcept
requires std::is_nothrow_move_constructible_v<DeleterType>
Takes the pointer and deleter of other, leaving other empty.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

template <typename OtherElementType, typename OtherDeleterType>
ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(UniquePtr< OtherElementType, OtherDeleterType > &&other) noexcept
requires detail::ImplicitOwnerConversion<OtherElementType, OtherDeleterType, ElementType, DeleterType> && std::is_nothrow_default_constructible_v<DeleterType>
Converts an ordinary-delete owner of a derived object (or a less cv-qualified object) into an owner of ElementType.
Available only between primary DefaultDeleter owners when the pointer conversion is accessible and unambiguous and either the object types differ only in cv-qualification or ElementType has a virtual destructor. other is left empty.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

UniquePtr

template <typename OtherElementType, typename OtherDeleterType>
explicit ludus::foundation::core::UniquePtr< ElementType, DeleterType >::UniquePtr(UniquePtr< OtherElementType, OtherDeleterType > &&other) noexcept
requires detail::AdaptedOwnerConversion<OtherElementType, OtherDeleterType, ElementType, DeleterType>
Explicitly converts an owner through a deleter adapter.
DeleterType is nothrow constructed from other's deleter and must preserve its destruction protocol for the converted pointer. The same deleter type is accepted only when adding cv-qualification. other is left empty.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator=

UniquePtr & ludus::foundation::core::UniquePtr< ElementType, DeleterType >::operator=(UniquePtr &&other) noexcept
requires std::is_nothrow_move_constructible_v<DeleterType> && std::is_nothrow_move_assignable_v<DeleterType>
Replaces this owner's state with other's pointer and deleter.
Self-move leaves the owner unchanged. Otherwise the complete source state is staged and other emptied first, then the previous object is destroyed with its own deleter, then the staged state is installed. The source is never read after the previous object is destroyed, so root = Move(root->Child) is valid.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator=

UniquePtr & ludus::foundation::core::UniquePtr< ElementType, DeleterType >::operator=(const UniquePtr &)=delete
Owners are not copy-assignable.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator=

template <typename OtherElementType, typename OtherDeleterType>
UniquePtr & ludus::foundation::core::UniquePtr< ElementType, DeleterType >::operator=(UniquePtr< OtherElementType, OtherDeleterType > &&other) noexcept
requires detail::ImplicitOwnerConversion<OtherElementType, OtherDeleterType, ElementType, DeleterType>
Replaces this owner's object with a converted ordinary-delete owner.
Uses the same staging order as same-type move assignment and the same constraints as the implicit converting constructor. other is left empty.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

~UniquePtr

ludus::foundation::core::UniquePtr< ElementType, DeleterType >::~UniquePtr()
Destroys the owned object, if any, with the owner's deleter.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

Get

LUDUS_INLINE ElementType * ludus::foundation::core::UniquePtr< ElementType, DeleterType >::Get() const noexcept
Borrows the owned pointer without transferring ownership.

Returns

The owned object, or nullptr for an empty owner.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

GetDeleter

LUDUS_INLINE const DeleterType & ludus::foundation::core::UniquePtr< ElementType, DeleterType >::GetDeleter() const noexcept
Inspects the destruction protocol paired with the owned pointer.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

Release

ElementType * ludus::foundation::core::UniquePtr< ElementType, DeleterType >::Release() noexcept
requires detail::BareReleaseDeleter<DeleterType, ElementType>
Gives up ownership without destroying the object.
Available only for stateless members of the DefaultDeleter family, including native specializations. Owners with stateful deleters transfer the whole owner instead.

Returns

The previously owned pointer, or nullptr. The receiver must destroy it with exactly the DefaultDeleter<ElementType> protocol.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

Reset

void ludus::foundation::core::UniquePtr< ElementType, DeleterType >::Reset(ElementType *pointerOrNull=nullptr) noexcept
Destroys the owned object and adopts pointerOrNull with the existing deleter.
pointerOrNull == Get() leaves the owner unchanged. Otherwise the owner is detached to empty before the previous object is destroyed, then adopts pointerOrNull.
ParameterDescription
pointerOrNull
Uniquely owned object matching this owner's deleter protocol, or nullptr to leave the owner empty.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator*

LUDUS_INLINE ElementType & ludus::foundation::core::UniquePtr< ElementType, DeleterType >::operator*() const noexcept
Accesses the owned object.

Precondition

The owner is not empty. Development builds assert this; a dangling external pointer is not detected.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator->

LUDUS_INLINE ElementType * ludus::foundation::core::UniquePtr< ElementType, DeleterType >::operator->() const noexcept
Accesses a member of the owned object.

Precondition

The owner is not empty. Development builds assert this.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

operator bool

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

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp

Swap

void ludus::foundation::core::UniquePtr< ElementType, DeleterType >::Swap(UniquePtr &other) noexcept
requires std::is_nothrow_move_constructible_v<DeleterType> && std::is_nothrow_move_assignable_v<DeleterType>
Exchanges pointer and deleter with other as one pair.

Declared in modules/foundation/base/include/ludus/foundation/base/pointer.hpp