StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
StormByte::Unique< T > Class Template Reference

Unique owner of a T allocated on Base's heap. More...

#include <StormByte/safe_pointers.hxx>

Public Types

using element_type = T
 Pointee type.
 
using pointer = T *
 Stored pointer.
 
using deleter_type = Heap::ObjectDeleter
 Deleter that calls Heap::Free.
 

Public Member Functions

 Unique () noexcept=default
 Empty owner.
 
 Unique (std::nullptr_t) noexcept
 Empty owner from nullptr.
 
template<class U >
requires Type::SameAs<U, T> || (Type::DerivedFrom<U, T> && std::has_virtual_destructor_v<T>)
 Unique (Unique< U > &&other) noexcept
 Take ownership from other.
 
 Unique (const Unique &)=delete
 Copy constructor.
 
 Unique (Unique &&) noexcept=default
 Move constructor.
 
Unique & operator= (const Unique &)=delete
 Copy assignment.
 
Unique & operator= (Unique &&) noexcept=default
 Move assignment.
 
 ~Unique ()=default
 Destructor.
 
T * get () const noexcept
 Raw pointer, or null.
 
T & operator* () const
 Dereference.
 
T * operator-> () const noexcept
 Member access.
 
 operator bool () const noexcept
 Whether this owner holds an object.
 
 operator std::unique_ptr< T, Heap::ObjectDeleter > () &&noexcept
 Release as std::unique_ptr with Base's deleter.
 
deleter_type & get_deleter () noexcept
 Deleter stored in this owner.
 
const deleter_type & get_deleter () const noexcept
 Deleter stored in this owner.
 
void reset () noexcept
 Destroy the object and drop this owner.
 
void swap (Unique &other) noexcept
 Exchange owners with other.
 

Static Public Member Functions

template<class Target , class... Args>
requires Type::SameAs<Target, T> || (Type::DerivedFrom<Target, T> && std::has_virtual_destructor_v<T>)
static Unique< T > MakePointer (Args &&... args)
 Allocate Target on Base's heap and own it as T.
 

Friends

template<class U >
class Unique
 
bool operator== (const Unique &lhs, std::nullptr_t) noexcept
 Equality with null.
 
bool operator!= (const Unique &lhs, std::nullptr_t) noexcept
 Inequality with null.
 
bool operator== (std::nullptr_t, const Unique &rhs) noexcept
 Equality with null.
 
bool operator!= (std::nullptr_t, const Unique &rhs) noexcept
 Inequality with null.
 
bool operator== (const Unique &lhs, const Unique &rhs) noexcept
 Same stored pointer.
 
auto operator<=> (const Unique &lhs, const Unique &rhs) noexcept
 Order of the stored pointers.
 
void swap (Unique &left, Unique &right) noexcept
 Exchange left and right.
 
template<class U , class... Args>
Unique< U > Heap::MakeUnique (Args &&...)
 

Detailed Description

template<class T>
class StormByte::Unique< T >

Unique owner of a T allocated on Base's heap.

Template Parameters
TPointee type.

Complements std::unique_ptr. It does not replace it. Use std::unique_ptr when the object does not cross a DLL. Use Unique when the object must be freed on Base's heap.

Construct the exact type with Heap::MakeUnique. Construct a derived type with Unique::MakePointer. ~T must be virtual when Target is not T, because the deleter calls ~T. There is no release and no constructor from a raw pointer. Converts on move to std::unique_ptr<T, Heap::ObjectDeleter>, not to std::unique_ptr<T>. A signature that takes std::unique_ptr<T> has to change.

Member Typedef Documentation

◆ deleter_type

template<class T >
using StormByte::Unique< T >::deleter_type = Heap::ObjectDeleter

Deleter that calls Heap::Free.

◆ element_type

template<class T >
using StormByte::Unique< T >::element_type = T

Pointee type.

◆ pointer

template<class T >
using StormByte::Unique< T >::pointer = T*

Stored pointer.

Constructor & Destructor Documentation

◆ Unique() [1/5]

template<class T >
StormByte::Unique< T >::Unique ( )
defaultnoexcept

Empty owner.

◆ Unique() [2/5]

template<class T >
StormByte::Unique< T >::Unique ( std::nullptr_t  )
inlinenoexcept

Empty owner from nullptr.

◆ Unique() [3/5]

template<class T >
template<class U >
requires Type::SameAs<U, T> || (Type::DerivedFrom<U, T> && std::has_virtual_destructor_v<T>)
StormByte::Unique< T >::Unique ( Unique< U > &&  other)
inlinenoexcept

Take ownership from other.

Template Parameters
UPointee convertible to T.
Parameters
otherOther owner.

◆ Unique() [4/5]

template<class T >
StormByte::Unique< T >::Unique ( const Unique< T > &  )
delete

Copy constructor.

Deleted.

◆ Unique() [5/5]

template<class T >
StormByte::Unique< T >::Unique ( Unique< T > &&  )
defaultnoexcept

Move constructor.

◆ ~Unique()

template<class T >
StormByte::Unique< T >::~Unique ( )
default

Destructor.

Member Function Documentation

◆ get()

template<class T >
T * StormByte::Unique< T >::get ( ) const
inlinenoexcept

Raw pointer, or null.

Returns
Pointee.

◆ get_deleter() [1/2]

template<class T >
const deleter_type & StormByte::Unique< T >::get_deleter ( ) const
inlinenoexcept

Deleter stored in this owner.

Returns
Heap::ObjectDeleter.

◆ get_deleter() [2/2]

template<class T >
deleter_type & StormByte::Unique< T >::get_deleter ( )
inlinenoexcept

Deleter stored in this owner.

Returns
Heap::ObjectDeleter.

◆ MakePointer()

template<class T >
template<class Target , class... Args>
requires Type::SameAs<Target, T> || (Type::DerivedFrom<Target, T> && std::has_virtual_destructor_v<T>)
static Unique< T > StormByte::Unique< T >::MakePointer ( Args &&...  args)
inlinestatic

Allocate Target on Base's heap and own it as T.

Template Parameters
TargetConcrete type (T or derived from T).
ArgsConstructor argument types.
Parameters
argsForwarded to Target.
Returns
Owner of the Target object.
Note
When Target is not T, T must have a virtual destructor.

◆ operator bool()

template<class T >
StormByte::Unique< T >::operator bool ( ) const
inlineexplicitnoexcept

Whether this owner holds an object.

Returns
true when non-empty.

◆ operator std::unique_ptr< T, Heap::ObjectDeleter >()

template<class T >
StormByte::Unique< T >::operator std::unique_ptr< T, Heap::ObjectDeleter > ( ) &&
inlinenoexcept

Release as std::unique_ptr with Base's deleter.

Returns
std::unique_ptr<T, Heap::ObjectDeleter>.

◆ operator*()

template<class T >
T & StormByte::Unique< T >::operator* ( ) const
inline

Dereference.

Returns
Pointee.

◆ operator->()

template<class T >
T * StormByte::Unique< T >::operator-> ( ) const
inlinenoexcept

Member access.

Returns
Pointee.

◆ operator=() [1/2]

template<class T >
Unique & StormByte::Unique< T >::operator= ( const Unique< T > &  )
delete

Copy assignment.

Deleted.

Returns
*this.

◆ operator=() [2/2]

template<class T >
Unique & StormByte::Unique< T >::operator= ( Unique< T > &&  )
defaultnoexcept

Move assignment.

Returns
*this.

◆ reset()

template<class T >
void StormByte::Unique< T >::reset ( )
inlinenoexcept

Destroy the object and drop this owner.

◆ swap()

template<class T >
void StormByte::Unique< T >::swap ( Unique< T > &  other)
inlinenoexcept

Exchange owners with other.

Parameters
otherOther owner.

Friends And Related Symbol Documentation

◆ Heap::MakeUnique

template<class T >
template<class U , class... Args>
Unique< U > Heap::MakeUnique ( Args &&  ...)
friend

◆ operator!= [1/2]

template<class T >
bool operator!= ( const Unique< T > &  lhs,
std::nullptr_t   
)
friend

Inequality with null.

Parameters
lhsOwner.
Returns
Whether lhs is non-empty.

◆ operator!= [2/2]

template<class T >
bool operator!= ( std::nullptr_t  ,
const Unique< T > &  rhs 
)
friend

Inequality with null.

Parameters
rhsOwner.
Returns
Whether rhs is non-empty.

◆ operator<=>

template<class T >
auto operator<=> ( const Unique< T > &  lhs,
const Unique< T > &  rhs 
)
friend

Order of the stored pointers.

Parameters
lhsOwner.
rhsOwner.
Returns
Three-way comparison of the addresses.

◆ operator== [1/3]

template<class T >
bool operator== ( const Unique< T > &  lhs,
const Unique< T > &  rhs 
)
friend

Same stored pointer.

Parameters
lhsOwner.
rhsOwner.
Returns
Whether both hold the same address.

◆ operator== [2/3]

template<class T >
bool operator== ( const Unique< T > &  lhs,
std::nullptr_t   
)
friend

Equality with null.

Parameters
lhsOwner.
Returns
Whether lhs is empty.

◆ operator== [3/3]

template<class T >
bool operator== ( std::nullptr_t  ,
const Unique< T > &  rhs 
)
friend

Equality with null.

Parameters
rhsOwner.
Returns
Whether rhs is empty.

◆ swap

template<class T >
void swap ( Unique< T > &  left,
Unique< T > &  right 
)
friend

Exchange left and right.

Parameters
leftOwner.
rightOwner.

◆ Unique

template<class T >
template<class U >
friend class Unique
friend

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