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

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

#include <StormByte/safe_pointers.hxx>

Public Types

using element_type = T
 Pointee type.
 
using weak_type = Weak< T >
 Matching Weak.
 

Public Member Functions

 Shared () noexcept=default
 Empty owner.
 
 Shared (std::nullptr_t) noexcept
 Empty owner from nullptr.
 
template<class U >
requires Type::SameAs<U, T> || Type::DerivedFrom<U, T>
 Shared (const Shared< U > &other) noexcept
 Share ownership with other.
 
template<class U >
requires Type::SameAs<U, T> || Type::DerivedFrom<U, T>
 Shared (Shared< U > &&other) noexcept
 Take ownership from other.
 
 Shared (const Shared &) noexcept=default
 Copy constructor.
 
 Shared (Shared &&) noexcept=default
 Move constructor.
 
 Shared (const Weak< T > &weak)
 Take ownership from weak.
 
Shared & operator= (const Shared &) noexcept=default
 Copy assignment.
 
Shared & operator= (Shared &&) noexcept=default
 Move assignment.
 
 ~Shared ()=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::shared_ptr< T > () const noexcept
 Same control block as std::shared_ptr.
 
long use_count () const noexcept
 Number of Shared sharing this object.
 
void reset () noexcept
 Drop this owner's reference.
 
void swap (Shared &other) noexcept
 Exchange owners with other.
 
template<class U >
bool owner_before (const Shared< U > &other) const noexcept
 Ownership order, same as std::shared_ptr::owner_before.
 
template<class U >
bool owner_before (const Weak< U > &other) const noexcept
 Ownership order against a Weak.
 

Static Public Member Functions

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

Friends

template<class U >
class Shared
 
template<class U >
class Weak
 
bool operator== (const Shared &lhs, std::nullptr_t) noexcept
 Equality with null.
 
bool operator!= (const Shared &lhs, std::nullptr_t) noexcept
 Inequality with null.
 
bool operator== (std::nullptr_t, const Shared &rhs) noexcept
 Equality with null.
 
bool operator!= (std::nullptr_t, const Shared &rhs) noexcept
 Inequality with null.
 
bool operator== (const Shared &lhs, const Shared &rhs) noexcept
 Same stored pointer.
 
auto operator<=> (const Shared &lhs, const Shared &rhs) noexcept
 Order of the stored pointers.
 
void swap (Shared &left, Shared &right) noexcept
 Exchange left and right.
 
template<class U , class... Args>
Shared< U > Heap::MakeShared (Args &&...)
 
template<class X , class Y >
Shared< X > StaticPointerCast (const Shared< Y > &) noexcept
 
template<class X , class Y >
Shared< X > DynamicPointerCast (const Shared< Y > &) noexcept
 
template<class X , class Y >
Shared< X > ConstPointerCast (const Shared< Y > &) noexcept
 
template<class X , class Y >
Shared< X > ReinterpretPointerCast (const Shared< Y > &) noexcept
 

Detailed Description

template<class T>
class StormByte::Shared< T >

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

Template Parameters
TPointee type.

Complements std::shared_ptr. It does not replace it. Use std::shared_ptr when the object does not cross a DLL. Use Shared when the object and its control block must be freed on Base's heap.

Construct the exact type with Heap::MakeShared. Construct a derived type with Shared::MakePointer. The daily operations match std::shared_ptr. There is no constructor from a raw pointer or from std::shared_ptr, and no release. Implicit conversion to std::shared_ptr<T> keeps Base's deleter, so a std::shared_ptr parameter does not need a new signature. There is no conversion back.

Member Typedef Documentation

◆ element_type

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

Pointee type.

◆ weak_type

template<class T >
using StormByte::Shared< T >::weak_type = Weak<T>

Matching Weak.

Constructor & Destructor Documentation

◆ Shared() [1/7]

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

Empty owner.

◆ Shared() [2/7]

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

Empty owner from nullptr.

◆ Shared() [3/7]

template<class T >
template<class U >
requires Type::SameAs<U, T> || Type::DerivedFrom<U, T>
StormByte::Shared< T >::Shared ( const Shared< U > &  other)
inlinenoexcept

Share ownership with other.

Template Parameters
UPointee convertible to T.
Parameters
otherOther owner.

◆ Shared() [4/7]

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

Take ownership from other.

Template Parameters
UPointee convertible to T.
Parameters
otherOther owner.

◆ Shared() [5/7]

template<class T >
StormByte::Shared< T >::Shared ( const Shared< T > &  )
defaultnoexcept

Copy constructor.

◆ Shared() [6/7]

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

Move constructor.

◆ Shared() [7/7]

template<class T >
StormByte::Shared< T >::Shared ( const Weak< T > &  weak)
explicit

Take ownership from weak.

Parameters
weakObserver of a Shared control block.
Exceptions
std::bad_weak_ptrWhen weak is empty or expired.

◆ ~Shared()

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

Destructor.

Member Function Documentation

◆ get()

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

Raw pointer, or null.

Returns
Pointee.

◆ MakePointer()

template<class T >
template<class Target , class... Args>
requires Type::SameAs<Target, T> || Type::DerivedFrom<Target, T>
static Shared< T > StormByte::Shared< 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. The deleter still destroys Target.

◆ operator bool()

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

Whether this owner holds an object.

Returns
true when non-empty.

◆ operator std::shared_ptr< T >()

template<class T >
StormByte::Shared< T >::operator std::shared_ptr< T > ( ) const
inlinenoexcept

Same control block as std::shared_ptr.

Deleter stays Base.

Returns
std::shared_ptr<T> that still frees on Base's heap.

◆ operator*()

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

Dereference.

Returns
Pointee.

◆ operator->()

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

Member access.

Returns
Pointee.

◆ operator=() [1/2]

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

Copy assignment.

Returns
*this.

◆ operator=() [2/2]

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

Move assignment.

Returns
*this.

◆ owner_before() [1/2]

template<class T >
template<class U >
bool StormByte::Shared< T >::owner_before ( const Shared< U > &  other) const
inlinenoexcept

Ownership order, same as std::shared_ptr::owner_before.

Template Parameters
UOther pointee.
Parameters
otherOther owner.
Returns
Whether this control block precedes other.

◆ owner_before() [2/2]

template<class T >
template<class U >
bool StormByte::Shared< T >::owner_before ( const Weak< U > &  other) const
noexcept

Ownership order against a Weak.

Template Parameters
UOther pointee.
Parameters
otherObserver.
Returns
Whether this control block precedes other.

◆ reset()

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

Drop this owner's reference.

◆ swap()

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

Exchange owners with other.

Parameters
otherOther owner.

◆ use_count()

template<class T >
long StormByte::Shared< T >::use_count ( ) const
inlinenoexcept

Number of Shared sharing this object.

Returns
Use count.

Friends And Related Symbol Documentation

◆ ConstPointerCast

template<class T >
template<class X , class Y >
Shared< X > ConstPointerCast ( const Shared< Y > &  )
friend

◆ DynamicPointerCast

template<class T >
template<class X , class Y >
Shared< X > DynamicPointerCast ( const Shared< Y > &  )
friend

◆ Heap::MakeShared

template<class T >
template<class U , class... Args>
Shared< U > Heap::MakeShared ( Args &&  ...)
friend

◆ operator!= [1/2]

template<class T >
bool operator!= ( const Shared< 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 Shared< T > &  rhs 
)
friend

Inequality with null.

Parameters
rhsOwner.
Returns
Whether rhs is non-empty.

◆ operator<=>

template<class T >
auto operator<=> ( const Shared< T > &  lhs,
const Shared< 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 Shared< T > &  lhs,
const Shared< 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 Shared< 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 Shared< T > &  rhs 
)
friend

Equality with null.

Parameters
rhsOwner.
Returns
Whether rhs is empty.

◆ ReinterpretPointerCast

template<class T >
template<class X , class Y >
Shared< X > ReinterpretPointerCast ( const Shared< Y > &  )
friend

◆ Shared

template<class T >
template<class U >
friend class Shared
friend

◆ StaticPointerCast

template<class T >
template<class X , class Y >
Shared< X > StaticPointerCast ( const Shared< Y > &  )
friend

◆ swap

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

Exchange left and right.

Parameters
leftOwner.
rightOwner.

◆ Weak

template<class T >
template<class U >
friend class Weak
friend

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